blob: ff39040cf716b502e0592d7906d6af73fa447639 [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 Moolenaar325b7a22004-07-05 15:58:32 +0000246#ifndef FEAT_MZSCHEME
247# define ex_mzscheme ex_script_ni
248# define ex_mzfile ex_ni
249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifndef FEAT_PERL
251# define ex_perl ex_script_ni
252# define ex_perldo ex_ni
253#endif
254#ifndef FEAT_PYTHON
255# define ex_python ex_script_ni
256# define ex_pyfile ex_ni
257#endif
258#ifndef FEAT_TCL
259# define ex_tcl ex_script_ni
260# define ex_tcldo ex_ni
261# define ex_tclfile ex_ni
262#endif
263#ifndef FEAT_RUBY
264# define ex_ruby ex_script_ni
265# define ex_rubydo ex_ni
266# define ex_rubyfile ex_ni
267#endif
268#ifndef FEAT_SNIFF
269# define ex_sniff ex_ni
270#endif
271#ifndef FEAT_KEYMAP
272# define ex_loadkeymap ex_ni
273#endif
274static void ex_swapname __ARGS((exarg_T *eap));
275static void ex_syncbind __ARGS((exarg_T *eap));
276static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277static void ex_pwd __ARGS((exarg_T *eap));
278static void ex_equal __ARGS((exarg_T *eap));
279static void ex_sleep __ARGS((exarg_T *eap));
280static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
281static void ex_winsize __ARGS((exarg_T *eap));
282#ifdef FEAT_WINDOWS
283static void ex_wincmd __ARGS((exarg_T *eap));
284#else
285# define ex_wincmd ex_ni
286#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000287#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288static void ex_winpos __ARGS((exarg_T *eap));
289#else
290# define ex_winpos ex_ni
291#endif
292static void ex_operators __ARGS((exarg_T *eap));
293static void ex_put __ARGS((exarg_T *eap));
294static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000295static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void ex_submagic __ARGS((exarg_T *eap));
297static void ex_join __ARGS((exarg_T *eap));
298static void ex_at __ARGS((exarg_T *eap));
299static void ex_bang __ARGS((exarg_T *eap));
300static void ex_undo __ARGS((exarg_T *eap));
301static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000302static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303static void ex_redir __ARGS((exarg_T *eap));
304static void ex_redraw __ARGS((exarg_T *eap));
305static void ex_redrawstatus __ARGS((exarg_T *eap));
306static void close_redir __ARGS((void));
307static void ex_mkrc __ARGS((exarg_T *eap));
308static void ex_mark __ARGS((exarg_T *eap));
309#ifdef FEAT_USR_CMDS
310static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000311static 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 +0000312#endif
313#ifdef FEAT_EX_EXTRA
314static void ex_normal __ARGS((exarg_T *eap));
315static void ex_startinsert __ARGS((exarg_T *eap));
316static void ex_stopinsert __ARGS((exarg_T *eap));
317#else
318# define ex_normal ex_ni
319# define ex_align ex_ni
320# define ex_retab ex_ni
321# define ex_startinsert ex_ni
322# define ex_stopinsert ex_ni
323# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000324# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#endif
326#ifdef FEAT_FIND_ID
327static void ex_checkpath __ARGS((exarg_T *eap));
328static void ex_findpat __ARGS((exarg_T *eap));
329#else
330# define ex_findpat ex_ni
331# define ex_checkpath ex_ni
332#endif
333#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
334static void ex_psearch __ARGS((exarg_T *eap));
335#else
336# define ex_psearch ex_ni
337#endif
338static void ex_tag __ARGS((exarg_T *eap));
339static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
340#ifndef FEAT_EVAL
341# define ex_scriptnames ex_ni
342# define ex_finish ex_ni
343# define ex_echo ex_ni
344# define ex_echohl ex_ni
345# define ex_execute ex_ni
346# define ex_call ex_ni
347# define ex_if ex_ni
348# define ex_endif ex_ni
349# define ex_else ex_ni
350# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000351# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352# define ex_continue ex_ni
353# define ex_break ex_ni
354# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000355# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356# define ex_throw ex_ni
357# define ex_try ex_ni
358# define ex_catch ex_ni
359# define ex_finally ex_ni
360# define ex_endtry ex_ni
361# define ex_endfunction ex_ni
362# define ex_let ex_ni
363# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000364# define ex_lockvar ex_ni
365# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366# define ex_function ex_ni
367# define ex_delfunction ex_ni
368# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000369# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#endif
371static char_u *arg_all __ARGS((void));
372#ifdef FEAT_SESSION
373static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000374static 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 +0000375static void ex_loadview __ARGS((exarg_T *eap));
376static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000377static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#else
379# define ex_loadview ex_ni
380#endif
381#ifndef FEAT_EVAL
382# define ex_compiler ex_ni
383#endif
384#ifdef FEAT_VIMINFO
385static void ex_viminfo __ARGS((exarg_T *eap));
386#else
387# define ex_viminfo ex_ni
388#endif
389static void ex_behave __ARGS((exarg_T *eap));
390#ifdef FEAT_AUTOCMD
391static void ex_filetype __ARGS((exarg_T *eap));
392static void ex_setfiletype __ARGS((exarg_T *eap));
393#else
394# define ex_filetype ex_ni
395# define ex_setfiletype ex_ni
396#endif
397#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000398# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399# define ex_diffpatch ex_ni
400# define ex_diffgetput ex_ni
401# define ex_diffsplit ex_ni
402# define ex_diffthis ex_ni
403# define ex_diffupdate ex_ni
404#endif
405static void ex_digraphs __ARGS((exarg_T *eap));
406static void ex_set __ARGS((exarg_T *eap));
407#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
408# define ex_options ex_ni
409#endif
410#ifdef FEAT_SEARCH_EXTRA
411static void ex_nohlsearch __ARGS((exarg_T *eap));
412static void ex_match __ARGS((exarg_T *eap));
413#else
414# define ex_nohlsearch ex_ni
415# define ex_match ex_ni
416#endif
417#ifdef FEAT_CRYPT
418static void ex_X __ARGS((exarg_T *eap));
419#else
420# define ex_X ex_ni
421#endif
422#ifdef FEAT_FOLDING
423static void ex_fold __ARGS((exarg_T *eap));
424static void ex_foldopen __ARGS((exarg_T *eap));
425static void ex_folddo __ARGS((exarg_T *eap));
426#else
427# define ex_fold ex_ni
428# define ex_foldopen ex_ni
429# define ex_folddo ex_ni
430#endif
431#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
432 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
433# define ex_language ex_ni
434#endif
435#ifndef FEAT_SIGNS
436# define ex_sign ex_ni
437#endif
438#ifndef FEAT_SUN_WORKSHOP
439# define ex_wsverb ex_ni
440#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000441#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200442# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000443# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200444# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447#ifndef FEAT_EVAL
448# define ex_debug ex_ni
449# define ex_breakadd ex_ni
450# define ex_debuggreedy ex_ni
451# define ex_breakdel ex_ni
452# define ex_breaklist ex_ni
453#endif
454
455#ifndef FEAT_CMDHIST
456# define ex_history ex_ni
457#endif
458#ifndef FEAT_JUMPLIST
459# define ex_jumps ex_ni
460# define ex_changes ex_ni
461#endif
462
Bram Moolenaar05159a02005-02-26 23:04:13 +0000463#ifndef FEAT_PROFILE
464# define ex_profile ex_ni
465#endif
466
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467/*
468 * Declare cmdnames[].
469 */
470#define DO_DECLARE_EXCMD
471#include "ex_cmds.h"
472
473/*
474 * Table used to quickly search for a command, based on its first character.
475 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000476static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
478 CMD_append,
479 CMD_buffer,
480 CMD_change,
481 CMD_delete,
482 CMD_edit,
483 CMD_file,
484 CMD_global,
485 CMD_help,
486 CMD_insert,
487 CMD_join,
488 CMD_k,
489 CMD_list,
490 CMD_move,
491 CMD_next,
492 CMD_open,
493 CMD_print,
494 CMD_quit,
495 CMD_read,
496 CMD_substitute,
497 CMD_t,
498 CMD_undo,
499 CMD_vglobal,
500 CMD_write,
501 CMD_xit,
502 CMD_yank,
503 CMD_z,
504 CMD_bang
505};
506
507static char_u dollar_command[2] = {'$', 0};
508
509
510#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000511/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512typedef struct
513{
514 char_u *line; /* command line */
515 linenr_T lnum; /* sourcing_lnum of the line */
516} wcmd_T;
517
518/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000519 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000521 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000523struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 garray_T *lines_gap; /* growarray with line info */
526 int current_line; /* last read line from growarray */
527 int repeating; /* TRUE when looping a second time */
528 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
529 char_u *(*getline) __ARGS((int, void *, int));
530 void *cookie;
531};
532
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000533static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
534static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000536
537/* Struct to save a few things while debugging. Used in do_cmdline() only. */
538struct dbg_stuff
539{
540 int trylevel;
541 int force_abort;
542 except_T *caught_stack;
543 char_u *vv_exception;
544 char_u *vv_throwpoint;
545 int did_emsg;
546 int got_int;
547 int did_throw;
548 int need_rethrow;
549 int check_cstack;
550 except_T *current_exception;
551};
552
553static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
554static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
555
556 static void
557save_dbg_stuff(dsp)
558 struct dbg_stuff *dsp;
559{
560 dsp->trylevel = trylevel; trylevel = 0;
561 dsp->force_abort = force_abort; force_abort = FALSE;
562 dsp->caught_stack = caught_stack; caught_stack = NULL;
563 dsp->vv_exception = v_exception(NULL);
564 dsp->vv_throwpoint = v_throwpoint(NULL);
565
566 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
567 dsp->did_emsg = did_emsg; did_emsg = FALSE;
568 dsp->got_int = got_int; got_int = FALSE;
569 dsp->did_throw = did_throw; did_throw = FALSE;
570 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
571 dsp->check_cstack = check_cstack; check_cstack = FALSE;
572 dsp->current_exception = current_exception; current_exception = NULL;
573}
574
575 static void
576restore_dbg_stuff(dsp)
577 struct dbg_stuff *dsp;
578{
579 suppress_errthrow = FALSE;
580 trylevel = dsp->trylevel;
581 force_abort = dsp->force_abort;
582 caught_stack = dsp->caught_stack;
583 (void)v_exception(dsp->vv_exception);
584 (void)v_throwpoint(dsp->vv_throwpoint);
585 did_emsg = dsp->did_emsg;
586 got_int = dsp->got_int;
587 did_throw = dsp->did_throw;
588 need_rethrow = dsp->need_rethrow;
589 check_cstack = dsp->check_cstack;
590 current_exception = dsp->current_exception;
591}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592#endif
593
594
595/*
596 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
597 * command is given.
598 */
599 void
600do_exmode(improved)
601 int improved; /* TRUE for "improved Ex" mode */
602{
603 int save_msg_scroll;
604 int prev_msg_row;
605 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000606 int changedtick;
607
608 if (improved)
609 exmode_active = EXMODE_VIM;
610 else
611 exmode_active = EXMODE_NORMAL;
612 State = NORMAL;
613
614 /* When using ":global /pat/ visual" and then "Q" we return to continue
615 * the :global command. */
616 if (global_busy)
617 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 save_msg_scroll = msg_scroll;
620 ++RedrawingDisabled; /* don't redisplay the window */
621 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#ifdef FEAT_GUI
623 /* Ignore scrollbar and mouse events in Ex mode */
624 ++hold_gui_events;
625#endif
626#ifdef FEAT_SNIFF
627 want_sniff_request = 0; /* No K_SNIFF wanted */
628#endif
629
630 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
631 while (exmode_active)
632 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000633#ifdef FEAT_EX_EXTRA
634 /* Check for a ":normal" command and no more characters left. */
635 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
636 {
637 exmode_active = FALSE;
638 break;
639 }
640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 msg_scroll = TRUE;
642 need_wait_return = FALSE;
643 ex_pressedreturn = FALSE;
644 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000645 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 prev_msg_row = msg_row;
647 prev_line = curwin->w_cursor.lnum;
648#ifdef FEAT_SNIFF
649 ProcessSniffRequests();
650#endif
651 if (improved)
652 {
653 cmdline_row = msg_row;
654 do_cmdline(NULL, getexline, NULL, 0);
655 }
656 else
657 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
658 lines_left = Rows - 1;
659
Bram Moolenaardf177f62005-02-22 08:39:57 +0000660 if ((prev_line != curwin->w_cursor.lnum
661 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000663 if (curbuf->b_ml.ml_flags & ML_EMPTY)
664 EMSG(_(e_emptybuf));
665 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000667 if (ex_pressedreturn)
668 {
669 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000670 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000671 msg_row = prev_msg_row;
672 if (prev_msg_row == Rows - 1)
673 msg_row--;
674 }
675 msg_col = 0;
676 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
677 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000680 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
681 {
682 if (curbuf->b_ml.ml_flags & ML_EMPTY)
683 EMSG(_(e_emptybuf));
684 else
685 EMSG(_("E501: At end-of-file"));
686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688
689#ifdef FEAT_GUI
690 --hold_gui_events;
691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 --RedrawingDisabled;
693 --no_wait_return;
694 update_screen(CLEAR);
695 need_wait_return = FALSE;
696 msg_scroll = save_msg_scroll;
697}
698
699/*
700 * Execute a simple command line. Used for translated commands like "*".
701 */
702 int
703do_cmdline_cmd(cmd)
704 char_u *cmd;
705{
706 return do_cmdline(cmd, NULL, NULL,
707 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
708}
709
710/*
711 * do_cmdline(): execute one Ex command line
712 *
713 * 1. Execute "cmdline" when it is not NULL.
714 * If "cmdline" is NULL, or more lines are needed, getline() is used.
715 * 2. Split up in parts separated with '|'.
716 *
717 * This function can be called recursively!
718 *
719 * flags:
720 * DOCMD_VERBOSE - The command will be included in the error message.
721 * DOCMD_NOWAIT - Don't call wait_return() and friends.
722 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
723 * DOCMD_KEYTYPED - Don't reset KeyTyped.
724 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
725 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
726 *
727 * return FAIL if cmdline could not be executed, OK otherwise
728 */
729 int
730do_cmdline(cmdline, getline, cookie, flags)
731 char_u *cmdline;
732 char_u *(*getline) __ARGS((int, void *, int));
733 void *cookie; /* argument for getline() */
734 int flags;
735{
736 char_u *next_cmdline; /* next cmd to execute */
737 char_u *cmdline_copy = NULL; /* copy of cmd line */
738 int used_getline = FALSE; /* used "getline" to obtain command */
739 static int recursive = 0; /* recursive depth */
740 int msg_didout_before_start = 0;
741 int count = 0; /* line number count */
742 int did_inc = FALSE; /* incremented RedrawingDisabled */
743 int retval = OK;
744#ifdef FEAT_EVAL
745 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000746 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 int current_line = 0; /* active line in lines_ga */
748 char_u *fname = NULL; /* function or script name */
749 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
750 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000751 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 int initial_trylevel;
753 struct msglist **saved_msg_list = NULL;
754 struct msglist *private_msg_list;
755
756 /* "getline" and "cookie" passed to do_one_cmd() */
757 char_u *(*cmd_getline) __ARGS((int, void *, int));
758 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000759 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000761 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762#else
763# define cmd_getline getline
764# define cmd_cookie cookie
765#endif
766 static int call_depth = 0; /* recursiveness */
767
768#ifdef FEAT_EVAL
769 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
770 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000771 * This ensures that the do_errthrow() call in do_one_cmd() does not
772 * combine the messages stored by an earlier invocation of do_one_cmd()
773 * with the command name of the later one. This would happen when
774 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 saved_msg_list = msg_list;
776 msg_list = &private_msg_list;
777 private_msg_list = NULL;
778#endif
779
780 /* It's possible to create an endless loop with ":execute", catch that
781 * here. The value of 200 allows nested function calls, ":source", etc. */
782 if (call_depth == 200)
783 {
784 EMSG(_("E169: Command too recursive"));
785#ifdef FEAT_EVAL
786 /* When converting to an exception, we do not include the command name
787 * since this is not an error of the specific command. */
788 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
789 msg_list = saved_msg_list;
790#endif
791 return FAIL;
792 }
793 ++call_depth;
794
795#ifdef FEAT_EVAL
796 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000797 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 cstack.cs_trylevel = 0;
799 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000800 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
802
803 real_cookie = getline_cookie(getline, cookie);
804
805 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806 getline_is_func = getline_equal(getline, cookie, get_func_line);
807 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 ++ex_nesting_level;
809
810 /* Get the function or script name and the address where the next breakpoint
811 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
814 fname = func_name(real_cookie);
815 breakpoint = func_breakpoint(real_cookie);
816 dbg_tick = func_dbg_tick(real_cookie);
817 }
818 else if (getline_equal(getline, cookie, getsourceline))
819 {
820 fname = sourcing_name;
821 breakpoint = source_breakpoint(real_cookie);
822 dbg_tick = source_dbg_tick(real_cookie);
823 }
824
825 /*
826 * Initialize "force_abort" and "suppress_errthrow" at the top level.
827 */
828 if (!recursive)
829 {
830 force_abort = FALSE;
831 suppress_errthrow = FALSE;
832 }
833
834 /*
835 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000836 * exception handling (used when debugging). Otherwise clear it to avoid
837 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000839 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000840 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000841 else
842 memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844 initial_trylevel = trylevel;
845
846 /*
847 * "did_throw" will be set to TRUE when an exception is being thrown.
848 */
849 did_throw = FALSE;
850#endif
851 /*
852 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000853 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 * If force_abort is set, we cancel everything.
855 */
856 did_emsg = FALSE;
857
858 /*
859 * KeyTyped is only set when calling vgetc(). Reset it here when not
860 * calling vgetc() (sourced command lines).
861 */
862 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
863 KeyTyped = FALSE;
864
865 /*
866 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 * - for multiple commands on one line, separated with '|'
869 * - when repeating until there are no more lines (for ":source")
870 */
871 next_cmdline = cmdline;
872 do
873 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000874#ifdef FEAT_EVAL
875 getline_is_func = getline_equal(getline, cookie, get_func_line);
876#endif
877
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000878 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (next_cmdline == NULL
880#ifdef FEAT_EVAL
881 && !force_abort
882 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000883 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#endif
885 )
886 did_emsg = FALSE;
887
888 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * 2. If no line given: Get an allocated line with getline().
891 * 3. If a line is given: Make a copy, so we can mess with it.
892 */
893
894#ifdef FEAT_EVAL
895 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000896 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
898 /* Each '|' separated command is stored separately in lines_ga, to
899 * be able to jump to it. Don't use next_cmdline now. */
900 vim_free(cmdline_copy);
901 cmdline_copy = NULL;
902
903 /* Check if a function has returned or, unless it has an unclosed
904 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000907# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000908 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909 func_line_end(real_cookie);
910# endif
911 if (func_has_ended(real_cookie))
912 {
913 retval = FAIL;
914 break;
915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000918 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000919 && getline_equal(getline, cookie, getsourceline))
920 script_line_end();
921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922
923 /* Check if a sourced file hit a ":finish" command. */
924 if (source_finished(getline, cookie))
925 {
926 retval = FAIL;
927 break;
928 }
929
930 /* If breakpoints have been added/deleted need to check for it. */
931 if (breakpoint != NULL && dbg_tick != NULL
932 && *dbg_tick != debug_tick)
933 {
934 *breakpoint = dbg_find_breakpoint(
935 getline_equal(getline, cookie, getsourceline),
936 fname, sourcing_lnum);
937 *dbg_tick = debug_tick;
938 }
939
940 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
941 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
942
943 /* Did we encounter a breakpoint? */
944 if (breakpoint != NULL && *breakpoint != 0
945 && *breakpoint <= sourcing_lnum)
946 {
947 dbg_breakpoint(fname, sourcing_lnum);
948 /* Find next breakpoint. */
949 *breakpoint = dbg_find_breakpoint(
950 getline_equal(getline, cookie, getsourceline),
951 fname, sourcing_lnum);
952 *dbg_tick = debug_tick;
953 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000954# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000955 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000956 {
957 if (getline_is_func)
958 func_line_start(real_cookie);
959 else if (getline_equal(getline, cookie, getsourceline))
960 script_line_start();
961 }
962# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 }
964
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000965 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000967 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 * again. Pass a different "getline" function to do_one_cmd()
969 * below, so that it stores lines in or reads them from
970 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000971 * while/for loop. */
972 cmd_getline = get_loop_line;
973 cmd_cookie = (void *)&cmd_loop_cookie;
974 cmd_loop_cookie.lines_gap = &lines_ga;
975 cmd_loop_cookie.current_line = current_line;
976 cmd_loop_cookie.getline = getline;
977 cmd_loop_cookie.cookie = cookie;
978 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980 else
981 {
982 cmd_getline = getline;
983 cmd_cookie = cookie;
984 }
985#endif
986
987 /* 2. If no line given, get an allocated line with getline(). */
988 if (next_cmdline == NULL)
989 {
990 /*
991 * Need to set msg_didout for the first line after an ":if",
992 * otherwise the ":if" will be overwritten.
993 */
994 if (count == 1 && getline_equal(getline, cookie, getexline))
995 msg_didout = TRUE;
996 if (getline == NULL || (next_cmdline = getline(':', cookie,
997#ifdef FEAT_EVAL
998 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
999#else
1000 0
1001#endif
1002 )) == NULL)
1003 {
1004 /* Don't call wait_return for aborted command line. The NULL
1005 * returned for the end of a sourced file or executed function
1006 * doesn't do this. */
1007 if (KeyTyped && !(flags & DOCMD_REPEAT))
1008 need_wait_return = FALSE;
1009 retval = FAIL;
1010 break;
1011 }
1012 used_getline = TRUE;
1013
1014 /*
1015 * Keep the first typed line. Clear it when more lines are typed.
1016 */
1017 if (flags & DOCMD_KEEPLINE)
1018 {
1019 vim_free(repeat_cmdline);
1020 if (count == 0)
1021 repeat_cmdline = vim_strsave(next_cmdline);
1022 else
1023 repeat_cmdline = NULL;
1024 }
1025 }
1026
1027 /* 3. Make a copy of the command so we can mess with it. */
1028 else if (cmdline_copy == NULL)
1029 {
1030 next_cmdline = vim_strsave(next_cmdline);
1031 if (next_cmdline == NULL)
1032 {
1033 EMSG(_(e_outofmem));
1034 retval = FAIL;
1035 break;
1036 }
1037 }
1038 cmdline_copy = next_cmdline;
1039
1040#ifdef FEAT_EVAL
1041 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 * Save the current line when inside a ":while" or ":for", and when
1043 * the command looks like a ":while" or ":for", because we may need it
1044 * later. When there is a '|' and another command, it is stored
1045 * separately, because we need to be able to jump back to it from an
1046 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 if (current_line == lines_ga.ga_len
1049 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
1053 retval = FAIL;
1054 break;
1055 }
1056 }
1057 did_endif = FALSE;
1058#endif
1059
1060 if (count++ == 0)
1061 {
1062 /*
1063 * All output from the commands is put below each other, without
1064 * waiting for a return. Don't do this when executing commands
1065 * from a script or when being called recursive (e.g. for ":e
1066 * +command file").
1067 */
1068 if (!(flags & DOCMD_NOWAIT) && !recursive)
1069 {
1070 msg_didout_before_start = msg_didout;
1071 msg_didany = FALSE; /* no output yet */
1072 msg_start();
1073 msg_scroll = TRUE; /* put messages below each other */
1074 ++no_wait_return; /* dont wait for return until finished */
1075 ++RedrawingDisabled;
1076 did_inc = TRUE;
1077 }
1078 }
1079
1080 if (p_verbose >= 15 && sourcing_name != NULL)
1081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001083 verbose_enter_scroll();
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 smsg((char_u *)_("line %ld: %s"),
1086 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001087 if (msg_silent == 0)
1088 msg_puts((char_u *)"\n"); /* don't overwrite this */
1089
1090 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 --no_wait_return;
1092 }
1093
1094 /*
1095 * 2. Execute one '|' separated command.
1096 * do_one_cmd() will return NULL if there is no trailing '|'.
1097 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1098 */
1099 ++recursive;
1100 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1101#ifdef FEAT_EVAL
1102 &cstack,
1103#endif
1104 cmd_getline, cmd_cookie);
1105 --recursive;
1106
1107#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 if (cmd_cookie == (void *)&cmd_loop_cookie)
1109 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112#endif
1113
1114 if (next_cmdline == NULL)
1115 {
1116 vim_free(cmdline_copy);
1117 cmdline_copy = NULL;
1118#ifdef FEAT_CMDHIST
1119 /*
1120 * If the command was typed, remember it for the ':' register.
1121 * Do this AFTER executing the command to make :@: work.
1122 */
1123 if (getline_equal(getline, cookie, getexline)
1124 && new_last_cmdline != NULL)
1125 {
1126 vim_free(last_cmdline);
1127 last_cmdline = new_last_cmdline;
1128 new_last_cmdline = NULL;
1129 }
1130#endif
1131 }
1132 else
1133 {
1134 /* need to copy the command after the '|' to cmdline_copy, for the
1135 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001136 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 next_cmdline = cmdline_copy;
1138 }
1139
1140
1141#ifdef FEAT_EVAL
1142 /* reset did_emsg for a function that is not aborted by an error */
1143 if (did_emsg && !force_abort
1144 && getline_equal(getline, cookie, get_func_line)
1145 && !func_has_abort(real_cookie))
1146 did_emsg = FALSE;
1147
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
1150 ++current_line;
1151
1152 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 * An ":endwhile", ":endfor" and ":continue" is handled here.
1154 * If we were executing commands, jump back to the ":while" or
1155 * ":for".
1156 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 /* Jump back to the matching ":while" or ":for". Be careful
1163 * not to use a cs_line[] from an entry that isn't a ":while"
1164 * or ":for": It would make "current_line" invalid and can
1165 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (!did_emsg && !got_int && !did_throw
1167 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 && (cstack.cs_flags[cstack.cs_idx]
1169 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 && cstack.cs_line[cstack.cs_idx] >= 0
1171 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1172 {
1173 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 /* remember we jumped there */
1175 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 line_breakcheck(); /* check if CTRL-C typed */
1177
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 /* Check for the next breakpoint at or after the ":while"
1179 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (breakpoint != NULL)
1181 {
1182 *breakpoint = dbg_find_breakpoint(
1183 getline_equal(getline, cookie, getsourceline),
1184 fname,
1185 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1186 *dbg_tick = debug_tick;
1187 }
1188 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001193 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1194 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 }
1196 }
1197
1198 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001203 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1205 }
1206 }
1207
1208 /*
1209 * When not inside any ":while" loop, clear remembered lines.
1210 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
1213 if (lines_ga.ga_len > 0)
1214 {
1215 sourcing_lnum =
1216 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1217 free_cmdlines(&lines_ga);
1218 }
1219 current_line = 0;
1220 }
1221
1222 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1224 * being restored at the ":endtry". Reset them here and set the
1225 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1226 * This includes the case where a missing ":endif", ":endwhile" or
1227 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001229 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 cstack.cs_lflags &= ~CSL_HAD_FINA;
1232 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1233 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 did_throw ? (void *)current_exception : NULL);
1235 did_emsg = got_int = did_throw = FALSE;
1236 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1237 }
1238
1239 /* Update global "trylevel" for recursive calls to do_cmdline() from
1240 * within this loop. */
1241 trylevel = initial_trylevel + cstack.cs_trylevel;
1242
1243 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001244 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 * files) is aborted because of an error, an interrupt, or an uncaught
1246 * exception, cancel everything. If it is left normally, reset
1247 * force_abort to get the non-EH compatible abortion behavior for
1248 * the rest of the script.
1249 */
1250 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1251 force_abort = FALSE;
1252
1253 /* Convert an interrupt to an exception if appropriate. */
1254 (void)do_intthrow(&cstack);
1255#endif /* FEAT_EVAL */
1256
1257 }
1258 /*
1259 * Continue executing command lines when:
1260 * - no CTRL-C typed, no aborting error, no exception thrown or try
1261 * conditionals need to be checked for executing finally clauses or
1262 * catching an interrupt exception
1263 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001264 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 * looping for ":source" command or function call.
1266 */
1267 while (!((got_int
1268#ifdef FEAT_EVAL
1269 || (did_emsg && force_abort) || did_throw
1270#endif
1271 )
1272#ifdef FEAT_EVAL
1273 && cstack.cs_trylevel == 0
1274#endif
1275 )
1276 && !(did_emsg && used_getline
1277 && (getline_equal(getline, cookie, getexmodeline)
1278 || getline_equal(getline, cookie, getexline)))
1279 && (next_cmdline != NULL
1280#ifdef FEAT_EVAL
1281 || cstack.cs_idx >= 0
1282#endif
1283 || (flags & DOCMD_REPEAT)));
1284
1285 vim_free(cmdline_copy);
1286#ifdef FEAT_EVAL
1287 free_cmdlines(&lines_ga);
1288 ga_clear(&lines_ga);
1289
1290 if (cstack.cs_idx >= 0)
1291 {
1292 /*
1293 * If a sourced file or executed function ran to its end, report the
1294 * unclosed conditional.
1295 */
1296 if (!got_int && !did_throw
1297 && ((getline_equal(getline, cookie, getsourceline)
1298 && !source_finished(getline, cookie))
1299 || (getline_equal(getline, cookie, get_func_line)
1300 && !func_has_ended(real_cookie))))
1301 {
1302 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1303 EMSG(_(e_endtry));
1304 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1305 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001306 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1307 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 else
1309 EMSG(_(e_endif));
1310 }
1311
1312 /*
1313 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1314 * ":endtry" in a sourced file or executed function. If the try
1315 * conditional is in its finally clause, ignore anything pending.
1316 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001317 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 */
1319 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001320 {
1321 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1322
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001323 if (idx >= 0)
1324 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001325 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1326 &cstack.cs_looplevel);
1327 }
1328 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 trylevel = initial_trylevel;
1330 }
1331
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001332 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1333 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * exception, do this now after rewinding the cstack. */
1335 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1336 ? (char_u *)"endfunction" : (char_u *)NULL);
1337
1338 if (trylevel == 0)
1339 {
1340 /*
1341 * When an exception is being thrown out of the outermost try
1342 * conditional, discard the uncaught exception, disable the conversion
1343 * of interrupts or errors to exceptions, and ensure that no more
1344 * commands are executed.
1345 */
1346 if (did_throw)
1347 {
1348 void *p = NULL;
1349 char_u *saved_sourcing_name;
1350 int saved_sourcing_lnum;
1351 struct msglist *messages = NULL, *next;
1352
1353 /*
1354 * If the uncaught exception is a user exception, report it as an
1355 * error. If it is an error exception, display the saved error
1356 * message now. For an interrupt exception, do nothing; the
1357 * interrupt message is given elsewhere.
1358 */
1359 switch (current_exception->type)
1360 {
1361 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001362 vim_snprintf((char *)IObuff, IOSIZE,
1363 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 current_exception->value);
1365 p = vim_strsave(IObuff);
1366 break;
1367 case ET_ERROR:
1368 messages = current_exception->messages;
1369 current_exception->messages = NULL;
1370 break;
1371 case ET_INTERRUPT:
1372 break;
1373 default:
1374 p = vim_strsave((char_u *)_(e_internal));
1375 }
1376
1377 saved_sourcing_name = sourcing_name;
1378 saved_sourcing_lnum = sourcing_lnum;
1379 sourcing_name = current_exception->throw_name;
1380 sourcing_lnum = current_exception->throw_lnum;
1381 current_exception->throw_name = NULL;
1382
1383 discard_current_exception(); /* uses IObuff if 'verbose' */
1384 suppress_errthrow = TRUE;
1385 force_abort = TRUE;
1386
1387 if (messages != NULL)
1388 {
1389 do
1390 {
1391 next = messages->next;
1392 emsg(messages->msg);
1393 vim_free(messages->msg);
1394 vim_free(messages);
1395 messages = next;
1396 }
1397 while (messages != NULL);
1398 }
1399 else if (p != NULL)
1400 {
1401 emsg(p);
1402 vim_free(p);
1403 }
1404 vim_free(sourcing_name);
1405 sourcing_name = saved_sourcing_name;
1406 sourcing_lnum = saved_sourcing_lnum;
1407 }
1408
1409 /*
1410 * On an interrupt or an aborting error not converted to an exception,
1411 * disable the conversion of errors to exceptions. (Interrupts are not
1412 * converted any more, here.) This enables also the interrupt message
1413 * when force_abort is set and did_emsg unset in case of an interrupt
1414 * from a finally clause after an error.
1415 */
1416 else if (got_int || (did_emsg && force_abort))
1417 suppress_errthrow = TRUE;
1418 }
1419
1420 /*
1421 * The current cstack will be freed when do_cmdline() returns. An uncaught
1422 * exception will have to be rethrown in the previous cstack. If a function
1423 * has just returned or a script file was just finished and the previous
1424 * cstack belongs to the same function or, respectively, script file, it
1425 * will have to be checked for finally clauses to be executed due to the
1426 * ":return" or ":finish". This is done in do_one_cmd().
1427 */
1428 if (did_throw)
1429 need_rethrow = TRUE;
1430 if ((getline_equal(getline, cookie, getsourceline)
1431 && ex_nesting_level > source_level(real_cookie))
1432 || (getline_equal(getline, cookie, get_func_line)
1433 && ex_nesting_level > func_level(real_cookie) + 1))
1434 {
1435 if (!did_throw)
1436 check_cstack = TRUE;
1437 }
1438 else
1439 {
1440 /* When leaving a function, reduce nesting level. */
1441 if (getline_equal(getline, cookie, get_func_line))
1442 --ex_nesting_level;
1443 /*
1444 * Go to debug mode when returning from a function in which we are
1445 * single-stepping.
1446 */
1447 if ((getline_equal(getline, cookie, getsourceline)
1448 || getline_equal(getline, cookie, get_func_line))
1449 && ex_nesting_level + 1 <= debug_break_level)
1450 do_debug(getline_equal(getline, cookie, getsourceline)
1451 ? (char_u *)_("End of sourced file")
1452 : (char_u *)_("End of function"));
1453 }
1454
1455 /*
1456 * Restore the exception environment (done after returning from the
1457 * debugger).
1458 */
1459 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001460 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462 msg_list = saved_msg_list;
1463#endif /* FEAT_EVAL */
1464
1465 /*
1466 * If there was too much output to fit on the command line, ask the user to
1467 * hit return before redrawing the screen. With the ":global" command we do
1468 * this only once after the command is finished.
1469 */
1470 if (did_inc)
1471 {
1472 --RedrawingDisabled;
1473 --no_wait_return;
1474 msg_scroll = FALSE;
1475
1476 /*
1477 * When just finished an ":if"-":else" which was typed, no need to
1478 * wait for hit-return. Also for an error situation.
1479 */
1480 if (retval == FAIL
1481#ifdef FEAT_EVAL
1482 || (did_endif && KeyTyped && !did_emsg)
1483#endif
1484 )
1485 {
1486 need_wait_return = FALSE;
1487 msg_didany = FALSE; /* don't wait when restarting edit */
1488 }
1489 else if (need_wait_return)
1490 {
1491 /*
1492 * The msg_start() above clears msg_didout. The wait_return we do
1493 * here should not overwrite the command that may be shown before
1494 * doing that.
1495 */
1496 msg_didout |= msg_didout_before_start;
1497 wait_return(FALSE);
1498 }
1499 }
1500
1501#ifndef FEAT_EVAL
1502 /*
1503 * Reset if_level, in case a sourced script file contains more ":if" than
1504 * ":endif" (could be ":if x | foo | endif").
1505 */
1506 if_level = 0;
1507#endif
1508
1509 --call_depth;
1510 return retval;
1511}
1512
1513#ifdef FEAT_EVAL
1514/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001515 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 */
1517 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001518get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 int c;
1520 void *cookie;
1521 int indent;
1522{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 wcmd_T *wp;
1525 char_u *line;
1526
1527 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1528 {
1529 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001530 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (cp->getline == NULL)
1534 line = getcmdline(c, 0L, indent);
1535 else
1536 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001537 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 ++cp->current_line;
1539
1540 return line;
1541 }
1542
1543 KeyTyped = FALSE;
1544 ++cp->current_line;
1545 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1546 sourcing_lnum = wp->lnum;
1547 return vim_strsave(wp->line);
1548}
1549
1550/*
1551 * Store a line in "gap" so that a ":while" loop can execute it again.
1552 */
1553 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 garray_T *gap;
1556 char_u *line;
1557{
1558 if (ga_grow(gap, 1) == FAIL)
1559 return FAIL;
1560 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1561 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1562 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 return OK;
1564}
1565
1566/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 */
1569 static void
1570free_cmdlines(gap)
1571 garray_T *gap;
1572{
1573 while (gap->ga_len > 0)
1574 {
1575 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1576 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578}
1579#endif
1580
1581/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001582 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1583 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586getline_equal(fgetline, cookie, func)
1587 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001588 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 char_u *(*func) __ARGS((int, void *, int));
1590{
1591#ifdef FEAT_EVAL
1592 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001593 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
Bram Moolenaar89d40322006-08-29 15:30:07 +00001595 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001596 * function that's originally used to obtain the lines. This may be
1597 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001598 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001599 cp = (struct loop_cookie *)cookie;
1600 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
1602 gp = cp->getline;
1603 cp = cp->cookie;
1604 }
1605 return gp == func;
1606#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001607 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608#endif
1609}
1610
1611#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1612/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001613 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 * getline function. Otherwise return "cookie".
1615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001617getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001618 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621# ifdef FEAT_EVAL
1622 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001623 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaar89d40322006-08-29 15:30:07 +00001625 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001626 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001629 cp = (struct loop_cookie *)cookie;
1630 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
1632 gp = cp->getline;
1633 cp = cp->cookie;
1634 }
1635 return cp;
1636# else
1637 return cookie;
1638# endif
1639}
1640#endif
1641
1642/*
1643 * Execute one Ex command.
1644 *
1645 * If 'sourcing' is TRUE, the command will be included in the error message.
1646 *
1647 * 1. skip comment lines and leading space
1648 * 2. handle command modifiers
1649 * 3. parse range
1650 * 4. parse command
1651 * 5. parse arguments
1652 * 6. switch on command name
1653 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001654 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 *
1656 * This function may be called recursively!
1657 */
1658#if (_MSC_VER == 1200)
1659/*
Bram Moolenaared203462004-06-16 11:19:22 +00001660 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001662 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663#endif
1664 static char_u *
1665do_one_cmd(cmdlinep, sourcing,
1666#ifdef FEAT_EVAL
1667 cstack,
1668#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001669 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 char_u **cmdlinep;
1671 int sourcing;
1672#ifdef FEAT_EVAL
1673 struct condstack *cstack;
1674#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001675 char_u *(*fgetline) __ARGS((int, void *, int));
1676 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677{
1678 char_u *p;
1679 linenr_T lnum;
1680 long n;
1681 char_u *errormsg = NULL; /* error message */
1682 exarg_T ea; /* Ex command arguments */
1683 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001684 int save_msg_scroll = msg_scroll;
1685 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001687#ifdef HAVE_SANDBOX
1688 int did_sandbox = FALSE;
1689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 cmdmod_T save_cmdmod;
1691 int ni; /* set when Not Implemented */
1692
1693 vim_memset(&ea, 0, sizeof(ea));
1694 ea.line1 = 1;
1695 ea.line2 = 1;
1696#ifdef FEAT_EVAL
1697 ++ex_nesting_level;
1698#endif
1699
1700 /* when not editing the last file :q has to be typed twice */
1701 if (quitmore
1702#ifdef FEAT_EVAL
1703 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001704 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#endif
1706 )
1707 --quitmore;
1708
1709 /*
1710 * Reset browse, confirm, etc.. They are restored when returning, for
1711 * recursive calls.
1712 */
1713 save_cmdmod = cmdmod;
1714 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1715
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001716 /* "#!anything" is handled like a comment. */
1717 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1718 goto doend;
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 /*
1721 * Repeat until no more command modifiers are found.
1722 */
1723 ea.cmd = *cmdlinep;
1724 for (;;)
1725 {
1726/*
1727 * 1. skip comment lines and leading white space and colons
1728 */
1729 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1730 ++ea.cmd;
1731
1732 /* in ex mode, an empty line works like :+ */
1733 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001734 && (getline_equal(fgetline, cookie, getexmodeline)
1735 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001736 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
1738 ea.cmd = (char_u *)"+";
1739 ex_pressedreturn = TRUE;
1740 }
1741
1742 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001743 if (*ea.cmd == '"')
1744 goto doend;
1745 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001746 {
1747 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751/*
1752 * 2. handle command modifiers.
1753 */
1754 p = ea.cmd;
1755 if (VIM_ISDIGIT(*ea.cmd))
1756 p = skipwhite(skipdigits(ea.cmd));
1757 switch (*p)
1758 {
1759 /* When adding an entry, also modify cmd_exists(). */
1760 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1761 break;
1762#ifdef FEAT_WINDOWS
1763 cmdmod.split |= WSP_ABOVE;
1764#endif
1765 continue;
1766
1767 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1768 {
1769#ifdef FEAT_WINDOWS
1770 cmdmod.split |= WSP_BELOW;
1771#endif
1772 continue;
1773 }
1774 if (checkforcmd(&ea.cmd, "browse", 3))
1775 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001776#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 cmdmod.browse = TRUE;
1778#endif
1779 continue;
1780 }
1781 if (!checkforcmd(&ea.cmd, "botright", 2))
1782 break;
1783#ifdef FEAT_WINDOWS
1784 cmdmod.split |= WSP_BOT;
1785#endif
1786 continue;
1787
1788 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1789 break;
1790#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1791 cmdmod.confirm = TRUE;
1792#endif
1793 continue;
1794
1795 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1796 {
1797 cmdmod.keepmarks = TRUE;
1798 continue;
1799 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001800 if (checkforcmd(&ea.cmd, "keepalt", 5))
1801 {
1802 cmdmod.keepalt = TRUE;
1803 continue;
1804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1806 break;
1807 cmdmod.keepjumps = TRUE;
1808 continue;
1809
1810 /* ":hide" and ":hide | cmd" are not modifiers */
1811 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1812 || *p == NUL || ends_excmd(*p))
1813 break;
1814 ea.cmd = p;
1815 cmdmod.hide = TRUE;
1816 continue;
1817
1818 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1819 {
1820 cmdmod.lockmarks = TRUE;
1821 continue;
1822 }
1823
1824 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1825 break;
1826#ifdef FEAT_WINDOWS
1827 cmdmod.split |= WSP_ABOVE;
1828#endif
1829 continue;
1830
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001831 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1832 break;
1833#ifdef FEAT_AUTOCMD
1834 if (cmdmod.save_ei == NULL)
1835 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001836 /* Set 'eventignore' to "all". Restore the
1837 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001838 cmdmod.save_ei = vim_strsave(p_ei);
1839 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001840 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001841 }
1842#endif
1843 continue;
1844
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1846 break;
1847#ifdef FEAT_WINDOWS
1848 cmdmod.split |= WSP_BELOW;
1849#endif
1850 continue;
1851
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001852 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1853 {
1854#ifdef HAVE_SANDBOX
1855 if (!did_sandbox)
1856 ++sandbox;
1857 did_sandbox = TRUE;
1858#endif
1859 continue;
1860 }
1861 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001863 if (save_msg_silent == -1)
1864 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1867 {
1868 /* ":silent!", but not "silent !cmd" */
1869 ea.cmd = skipwhite(ea.cmd + 1);
1870 ++emsg_silent;
1871 ++did_esilent;
1872 }
1873 continue;
1874
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001875 case 't': if (checkforcmd(&p, "tab", 3))
1876 {
1877#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001878 if (vim_isdigit(*ea.cmd))
1879 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1880 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001881 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001882 ea.cmd = p;
1883#endif
1884 continue;
1885 }
1886 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 break;
1888#ifdef FEAT_WINDOWS
1889 cmdmod.split |= WSP_TOP;
1890#endif
1891 continue;
1892
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001893 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1894 break;
1895 if (save_msg_silent == -1)
1896 save_msg_silent = msg_silent;
1897 msg_silent = 0;
1898 continue;
1899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1901 {
1902#ifdef FEAT_VERTSPLIT
1903 cmdmod.split |= WSP_VERT;
1904#endif
1905 continue;
1906 }
1907 if (!checkforcmd(&p, "verbose", 4))
1908 break;
1909 if (verbose_save < 0)
1910 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001911 if (vim_isdigit(*ea.cmd))
1912 p_verbose = atoi((char *)ea.cmd);
1913 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 p_verbose = 1;
1915 ea.cmd = p;
1916 continue;
1917 }
1918 break;
1919 }
1920
1921#ifdef FEAT_EVAL
1922 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1923 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1924#else
1925 ea.skip = (if_level > 0);
1926#endif
1927
1928#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001929# ifdef FEAT_PROFILE
1930 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001931 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001932 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001933 if (getline_equal(fgetline, cookie, get_func_line))
1934 func_line_exec(getline_cookie(fgetline, cookie));
1935 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001936 script_line_exec();
1937 }
1938#endif
1939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 /* May go to debug mode. If this happens and the ">quit" debug command is
1941 * used, throw an interrupt exception and skip the next command. */
1942 dbg_check_breakpoint(&ea);
1943 if (!ea.skip && got_int)
1944 {
1945 ea.skip = TRUE;
1946 (void)do_intthrow(cstack);
1947 }
1948#endif
1949
1950/*
1951 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1952 *
1953 * where 'addr' is:
1954 *
1955 * % (entire file)
1956 * $ [+-NUM]
1957 * 'x [+-NUM] (where x denotes a currently defined mark)
1958 * . [+-NUM]
1959 * [+-NUM]..
1960 * NUM
1961 *
1962 * The ea.cmd pointer is updated to point to the first character following the
1963 * range spec. If an initial address is found, but no second, the upper bound
1964 * is equal to the lower.
1965 */
1966
1967 /* repeat for all ',' or ';' separated addresses */
1968 for (;;)
1969 {
1970 ea.line1 = ea.line2;
1971 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1972 ea.cmd = skipwhite(ea.cmd);
1973 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1974 if (ea.cmd == NULL) /* error detected */
1975 goto doend;
1976 if (lnum == MAXLNUM)
1977 {
1978 if (*ea.cmd == '%') /* '%' - all lines */
1979 {
1980 ++ea.cmd;
1981 ea.line1 = 1;
1982 ea.line2 = curbuf->b_ml.ml_line_count;
1983 ++ea.addr_count;
1984 }
1985 /* '*' - visual area */
1986 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1987 {
1988 pos_T *fp;
1989
1990 ++ea.cmd;
1991 if (!ea.skip)
1992 {
1993 fp = getmark('<', FALSE);
1994 if (check_mark(fp) == FAIL)
1995 goto doend;
1996 ea.line1 = fp->lnum;
1997 fp = getmark('>', FALSE);
1998 if (check_mark(fp) == FAIL)
1999 goto doend;
2000 ea.line2 = fp->lnum;
2001 ++ea.addr_count;
2002 }
2003 }
2004 }
2005 else
2006 ea.line2 = lnum;
2007 ea.addr_count++;
2008
2009 if (*ea.cmd == ';')
2010 {
2011 if (!ea.skip)
2012 curwin->w_cursor.lnum = ea.line2;
2013 }
2014 else if (*ea.cmd != ',')
2015 break;
2016 ++ea.cmd;
2017 }
2018
2019 /* One address given: set start and end lines */
2020 if (ea.addr_count == 1)
2021 {
2022 ea.line1 = ea.line2;
2023 /* ... but only implicit: really no address given */
2024 if (lnum == MAXLNUM)
2025 ea.addr_count = 0;
2026 }
2027
2028 /* Don't leave the cursor on an illegal line (caused by ';') */
2029 check_cursor_lnum();
2030
2031/*
2032 * 4. parse command
2033 */
2034
2035 /*
2036 * Skip ':' and any white space
2037 */
2038 ea.cmd = skipwhite(ea.cmd);
2039 while (*ea.cmd == ':')
2040 ea.cmd = skipwhite(ea.cmd + 1);
2041
2042 /*
2043 * If we got a line, but no command, then go to the line.
2044 * If we find a '|' or '\n' we set ea.nextcmd.
2045 */
2046 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2047 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2048 {
2049 /*
2050 * strange vi behaviour:
2051 * ":3" jumps to line 3
2052 * ":3|..." prints line 3
2053 * ":|" prints current line
2054 */
2055 if (ea.skip) /* skip this if inside :if */
2056 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002057 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
2059 ea.cmdidx = CMD_print;
2060 ea.argt = RANGE+COUNT+TRLBAR;
2061 if ((errormsg = invalid_range(&ea)) == NULL)
2062 {
2063 correct_range(&ea);
2064 ex_print(&ea);
2065 }
2066 }
2067 else if (ea.addr_count != 0)
2068 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002069 if (ea.line2 > curbuf->b_ml.ml_line_count)
2070 {
2071 /* With '-' in 'cpoptions' a line number past the file is an
2072 * error, otherwise put it at the end of the file. */
2073 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2074 ea.line2 = -1;
2075 else
2076 ea.line2 = curbuf->b_ml.ml_line_count;
2077 }
2078
2079 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002080 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 else
2082 {
2083 if (ea.line2 == 0)
2084 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 else
2086 curwin->w_cursor.lnum = ea.line2;
2087 beginline(BL_SOL | BL_FIX);
2088 }
2089 }
2090 goto doend;
2091 }
2092
2093 /* Find the command and let "p" point to after it. */
2094 p = find_command(&ea, NULL);
2095
2096#ifdef FEAT_USR_CMDS
2097 if (p == NULL)
2098 {
2099 if (!ea.skip)
2100 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2101 goto doend;
2102 }
2103 /* Check for wrong commands. */
2104 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2105 {
2106 errormsg = uc_fun_cmd();
2107 goto doend;
2108 }
2109#endif
2110 if (ea.cmdidx == CMD_SIZE)
2111 {
2112 if (!ea.skip)
2113 {
2114 STRCPY(IObuff, _("E492: Not an editor command"));
2115 if (!sourcing)
2116 {
2117 STRCAT(IObuff, ": ");
2118 STRNCAT(IObuff, *cmdlinep, 40);
2119 }
2120 errormsg = IObuff;
2121 }
2122 goto doend;
2123 }
2124
2125 ni = (
2126#ifdef FEAT_USR_CMDS
2127 !USER_CMDIDX(ea.cmdidx) &&
2128#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002129 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002130#ifdef HAVE_EX_SCRIPT_NI
2131 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2132#endif
2133 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135#ifndef FEAT_EVAL
2136 /*
2137 * When the expression evaluation is disabled, recognize the ":if" and
2138 * ":endif" commands and ignore everything in between it.
2139 */
2140 if (ea.cmdidx == CMD_if)
2141 ++if_level;
2142 if (if_level)
2143 {
2144 if (ea.cmdidx == CMD_endif)
2145 --if_level;
2146 goto doend;
2147 }
2148
2149#endif
2150
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002151 /* forced commands */
2152 if (*p == '!' && ea.cmdidx != CMD_substitute
2153 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
2155 ++p;
2156 ea.forceit = TRUE;
2157 }
2158 else
2159 ea.forceit = FALSE;
2160
2161/*
2162 * 5. parse arguments
2163 */
2164#ifdef FEAT_USR_CMDS
2165 if (!USER_CMDIDX(ea.cmdidx))
2166#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002167 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169 if (!ea.skip)
2170 {
2171#ifdef HAVE_SANDBOX
2172 if (sandbox != 0 && !(ea.argt & SBOXOK))
2173 {
2174 /* Command not allowed in sandbox. */
2175 errormsg = (char_u *)_(e_sandbox);
2176 goto doend;
2177 }
2178#endif
2179 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2180 {
2181 /* Command not allowed in non-'modifiable' buffer */
2182 errormsg = (char_u *)_(e_modifiable);
2183 goto doend;
2184 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002185
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002186 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187# ifdef FEAT_USR_CMDS
2188 && !USER_CMDIDX(ea.cmdidx)
2189# endif
2190 )
2191 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002192 /* Command not allowed when editing the command line. */
2193#ifdef FEAT_CMDWIN
2194 if (cmdwin_type != 0)
2195 errormsg = (char_u *)_(e_cmdwin);
2196 else
2197#endif
2198 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 goto doend;
2200 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002202 /* Disallow editing another buffer when "curbuf_lock" is set.
2203 * Do allow ":edit" (check for argument later).
2204 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002205 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002206 && ea.cmdidx != CMD_edit
2207 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002208# ifdef FEAT_USR_CMDS
2209 && !USER_CMDIDX(ea.cmdidx)
2210# endif
2211 && curbuf_locked())
2212 goto doend;
2213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
2215 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2216 {
2217 /* no range allowed */
2218 errormsg = (char_u *)_(e_norange);
2219 goto doend;
2220 }
2221 }
2222
2223 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2224 {
2225 errormsg = (char_u *)_(e_nobang);
2226 goto doend;
2227 }
2228
2229 /*
2230 * Don't complain about the range if it is not used
2231 * (could happen if line_count is accidentally set to 0).
2232 */
2233 if (!ea.skip && !ni)
2234 {
2235 /*
2236 * If the range is backwards, ask for confirmation and, if given, swap
2237 * ea.line1 & ea.line2 so it's forwards again.
2238 * When global command is busy, don't ask, will fail below.
2239 */
2240 if (!global_busy && ea.line1 > ea.line2)
2241 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002242 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002244 if (sourcing || exmode_active)
2245 {
2246 errormsg = (char_u *)_("E493: Backwards range given");
2247 goto doend;
2248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (ask_yesno((char_u *)
2250 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002251 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253 lnum = ea.line1;
2254 ea.line1 = ea.line2;
2255 ea.line2 = lnum;
2256 }
2257 if ((errormsg = invalid_range(&ea)) != NULL)
2258 goto doend;
2259 }
2260
2261 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2262 ea.line2 = 1;
2263
2264 correct_range(&ea);
2265
2266#ifdef FEAT_FOLDING
2267 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2268 {
2269 /* Put the first line at the start of a closed fold, put the last line
2270 * at the end of a closed fold. */
2271 (void)hasFolding(ea.line1, &ea.line1, NULL);
2272 (void)hasFolding(ea.line2, NULL, &ea.line2);
2273 }
2274#endif
2275
2276#ifdef FEAT_QUICKFIX
2277 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002278 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 * option here, so things like % get expanded.
2280 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002281 p = replace_makeprg(&ea, p, cmdlinep);
2282 if (p == NULL)
2283 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#endif
2285
2286 /*
2287 * Skip to start of argument.
2288 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2289 */
2290 if (ea.cmdidx == CMD_bang)
2291 ea.arg = p;
2292 else
2293 ea.arg = skipwhite(p);
2294
2295 /*
2296 * Check for "++opt=val" argument.
2297 * Must be first, allow ":w ++enc=utf8 !cmd"
2298 */
2299 if (ea.argt & ARGOPT)
2300 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2301 if (getargopt(&ea) == FAIL && !ni)
2302 {
2303 errormsg = (char_u *)_(e_invarg);
2304 goto doend;
2305 }
2306
2307 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2308 {
2309 if (*ea.arg == '>') /* append */
2310 {
2311 if (*++ea.arg != '>') /* typed wrong */
2312 {
2313 errormsg = (char_u *)_("E494: Use w or w>>");
2314 goto doend;
2315 }
2316 ea.arg = skipwhite(ea.arg + 1);
2317 ea.append = TRUE;
2318 }
2319 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2320 {
2321 ++ea.arg;
2322 ea.usefilter = TRUE;
2323 }
2324 }
2325
2326 if (ea.cmdidx == CMD_read)
2327 {
2328 if (ea.forceit)
2329 {
2330 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2331 ea.forceit = FALSE;
2332 }
2333 else if (*ea.arg == '!') /* :r !filter */
2334 {
2335 ++ea.arg;
2336 ea.usefilter = TRUE;
2337 }
2338 }
2339
2340 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2341 {
2342 ea.amount = 1;
2343 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2344 {
2345 ++ea.arg;
2346 ++ea.amount;
2347 }
2348 ea.arg = skipwhite(ea.arg);
2349 }
2350
2351 /*
2352 * Check for "+command" argument, before checking for next command.
2353 * Don't do this for ":read !cmd" and ":write !cmd".
2354 */
2355 if ((ea.argt & EDITCMD) && !ea.usefilter)
2356 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2357
2358 /*
2359 * Check for '|' to separate commands and '"' to start comments.
2360 * Don't do this for ":read !cmd" and ":write !cmd".
2361 */
2362 if ((ea.argt & TRLBAR) && !ea.usefilter)
2363 separate_nextcmd(&ea);
2364
2365 /*
2366 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002367 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2368 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002370 else if (ea.cmdidx == CMD_bang
2371 || ea.cmdidx == CMD_global
2372 || ea.cmdidx == CMD_vglobal
2373 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
2375 for (p = ea.arg; *p; ++p)
2376 {
2377 /* Remove one backslash before a newline, so that it's possible to
2378 * pass a newline to the shell and also a newline that is preceded
2379 * with a backslash. This makes it impossible to end a shell
2380 * command in a backslash, but that doesn't appear useful.
2381 * Halving the number of backslashes is incompatible with previous
2382 * versions. */
2383 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002384 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 else if (*p == '\n')
2386 {
2387 ea.nextcmd = p + 1;
2388 *p = NUL;
2389 break;
2390 }
2391 }
2392 }
2393
2394 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2395 {
2396 ea.line1 = 1;
2397 ea.line2 = curbuf->b_ml.ml_line_count;
2398 }
2399
2400 /* accept numbered register only when no count allowed (:put) */
2401 if ( (ea.argt & REGSTR)
2402 && *ea.arg != NUL
2403#ifdef FEAT_USR_CMDS
2404 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2405 && USER_CMDIDX(ea.cmdidx)))
2406 /* Do not allow register = for user commands */
2407 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2408#else
2409 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2410#endif
2411 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2412 {
2413 ea.regname = *ea.arg++;
2414#ifdef FEAT_EVAL
2415 /* for '=' register: accept the rest of the line as an expression */
2416 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2417 {
2418 set_expr_line(vim_strsave(ea.arg));
2419 ea.arg += STRLEN(ea.arg);
2420 }
2421#endif
2422 ea.arg = skipwhite(ea.arg);
2423 }
2424
2425 /*
2426 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2427 * count, it's a buffer name.
2428 */
2429 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2430 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2431 || vim_iswhite(*p)))
2432 {
2433 n = getdigits(&ea.arg);
2434 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002435 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
2437 errormsg = (char_u *)_(e_zerocount);
2438 goto doend;
2439 }
2440 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2441 {
2442 ea.line2 = n;
2443 if (ea.addr_count == 0)
2444 ea.addr_count = 1;
2445 }
2446 else
2447 {
2448 ea.line1 = ea.line2;
2449 ea.line2 += n - 1;
2450 ++ea.addr_count;
2451 /*
2452 * Be vi compatible: no error message for out of range.
2453 */
2454 if (ea.line2 > curbuf->b_ml.ml_line_count)
2455 ea.line2 = curbuf->b_ml.ml_line_count;
2456 }
2457 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002458
2459 /*
2460 * Check for flags: 'l', 'p' and '#'.
2461 */
2462 if (ea.argt & EXFLAGS)
2463 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002465 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002466 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
2468 errormsg = (char_u *)_(e_trailing);
2469 goto doend;
2470 }
2471
2472 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2473 {
2474 errormsg = (char_u *)_(e_argreq);
2475 goto doend;
2476 }
2477
2478#ifdef FEAT_EVAL
2479 /*
2480 * Skip the command when it's not going to be executed.
2481 * The commands like :if, :endif, etc. always need to be executed.
2482 * Also make an exception for commands that handle a trailing command
2483 * themselves.
2484 */
2485 if (ea.skip)
2486 {
2487 switch (ea.cmdidx)
2488 {
2489 /* commands that need evaluation */
2490 case CMD_while:
2491 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002492 case CMD_for:
2493 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_if:
2495 case CMD_elseif:
2496 case CMD_else:
2497 case CMD_endif:
2498 case CMD_try:
2499 case CMD_catch:
2500 case CMD_finally:
2501 case CMD_endtry:
2502 case CMD_function:
2503 break;
2504
2505 /* Commands that handle '|' themselves. Check: A command should
2506 * either have the TRLBAR flag, appear in this list or appear in
2507 * the list at ":help :bar". */
2508 case CMD_aboveleft:
2509 case CMD_and:
2510 case CMD_belowright:
2511 case CMD_botright:
2512 case CMD_browse:
2513 case CMD_call:
2514 case CMD_confirm:
2515 case CMD_delfunction:
2516 case CMD_djump:
2517 case CMD_dlist:
2518 case CMD_dsearch:
2519 case CMD_dsplit:
2520 case CMD_echo:
2521 case CMD_echoerr:
2522 case CMD_echomsg:
2523 case CMD_echon:
2524 case CMD_execute:
2525 case CMD_help:
2526 case CMD_hide:
2527 case CMD_ijump:
2528 case CMD_ilist:
2529 case CMD_isearch:
2530 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002531 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 case CMD_keepjumps:
2533 case CMD_keepmarks:
2534 case CMD_leftabove:
2535 case CMD_let:
2536 case CMD_lockmarks:
2537 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002538 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 case CMD_perl:
2540 case CMD_psearch:
2541 case CMD_python:
2542 case CMD_return:
2543 case CMD_rightbelow:
2544 case CMD_ruby:
2545 case CMD_silent:
2546 case CMD_smagic:
2547 case CMD_snomagic:
2548 case CMD_substitute:
2549 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002550 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 case CMD_tcl:
2552 case CMD_throw:
2553 case CMD_tilde:
2554 case CMD_topleft:
2555 case CMD_unlet:
2556 case CMD_verbose:
2557 case CMD_vertical:
2558 break;
2559
2560 default: goto doend;
2561 }
2562 }
2563#endif
2564
2565 if (ea.argt & XFILE)
2566 {
2567 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2568 goto doend;
2569 }
2570
2571#ifdef FEAT_LISTCMDS
2572 /*
2573 * Accept buffer name. Cannot be used at the same time with a buffer
2574 * number. Don't do this for a user command.
2575 */
2576 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2577# ifdef FEAT_USR_CMDS
2578 && !USER_CMDIDX(ea.cmdidx)
2579# endif
2580 )
2581 {
2582 /*
2583 * :bdelete, :bwipeout and :bunload take several arguments, separated
2584 * by spaces: find next space (skipping over escaped characters).
2585 * The others take one argument: ignore trailing spaces.
2586 */
2587 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2588 || ea.cmdidx == CMD_bunload)
2589 p = skiptowhite_esc(ea.arg);
2590 else
2591 {
2592 p = ea.arg + STRLEN(ea.arg);
2593 while (p > ea.arg && vim_iswhite(p[-1]))
2594 --p;
2595 }
2596 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2597 if (ea.line2 < 0) /* failed */
2598 goto doend;
2599 ea.addr_count = 1;
2600 ea.arg = skipwhite(p);
2601 }
2602#endif
2603
2604/*
2605 * 6. switch on command name
2606 *
2607 * The "ea" structure holds the arguments that can be used.
2608 */
2609 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002610 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 ea.cookie = cookie;
2612#ifdef FEAT_EVAL
2613 ea.cstack = cstack;
2614#endif
2615
2616#ifdef FEAT_USR_CMDS
2617 if (USER_CMDIDX(ea.cmdidx))
2618 {
2619 /*
2620 * Execute a user-defined command.
2621 */
2622 do_ucmd(&ea);
2623 }
2624 else
2625#endif
2626 {
2627 /*
2628 * Call the function to execute the command.
2629 */
2630 ea.errmsg = NULL;
2631 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2632 if (ea.errmsg != NULL)
2633 errormsg = (char_u *)_(ea.errmsg);
2634 }
2635
2636#ifdef FEAT_EVAL
2637 /*
2638 * If the command just executed called do_cmdline(), any throw or ":return"
2639 * or ":finish" encountered there must also check the cstack of the still
2640 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2641 * exception, or reanimate a returned function or finished script file and
2642 * return or finish it again.
2643 */
2644 if (need_rethrow)
2645 do_throw(cstack);
2646 else if (check_cstack)
2647 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002648 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002650 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 && current_func_returned())
2652 do_return(&ea, TRUE, FALSE, NULL);
2653 }
2654 need_rethrow = check_cstack = FALSE;
2655#endif
2656
2657doend:
2658 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2659 curwin->w_cursor.lnum = 1;
2660
2661 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2662 {
2663 if (sourcing)
2664 {
2665 if (errormsg != IObuff)
2666 {
2667 STRCPY(IObuff, errormsg);
2668 errormsg = IObuff;
2669 }
2670 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002671 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 }
2673 emsg(errormsg);
2674 }
2675#ifdef FEAT_EVAL
2676 do_errthrow(cstack,
2677 (ea.cmdidx != CMD_SIZE
2678# ifdef FEAT_USR_CMDS
2679 && !USER_CMDIDX(ea.cmdidx)
2680# endif
2681 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2682#endif
2683
2684 if (verbose_save >= 0)
2685 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002686#ifdef FEAT_AUTOCMD
2687 if (cmdmod.save_ei != NULL)
2688 {
2689 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002690 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2691 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002692 free_string_option(cmdmod.save_ei);
2693 }
2694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 cmdmod = save_cmdmod;
2697
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002698 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
2700 /* messages could be enabled for a serious error, need to check if the
2701 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002702 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002703 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 emsg_silent -= did_esilent;
2705 if (emsg_silent < 0)
2706 emsg_silent = 0;
2707 /* Restore msg_scroll, it's set by file I/O commands, even when no
2708 * message is actually displayed. */
2709 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002710
2711 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2712 * somewhere in the line. Put it back in the first column. */
2713 if (redirecting())
2714 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002717#ifdef HAVE_SANDBOX
2718 if (did_sandbox)
2719 --sandbox;
2720#endif
2721
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2723 ea.nextcmd = NULL;
2724
2725#ifdef FEAT_EVAL
2726 --ex_nesting_level;
2727#endif
2728
2729 return ea.nextcmd;
2730}
2731#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002732 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733#endif
2734
2735/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002736 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 * If there is a match advance "pp" to the argument and return TRUE.
2738 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002739 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002741 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 char *cmd; /* name of command */
2743 int len; /* required length */
2744{
2745 int i;
2746
2747 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002748 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 break;
2750 if (i >= len && !isalpha((*pp)[i]))
2751 {
2752 *pp = skipwhite(*pp + i);
2753 return TRUE;
2754 }
2755 return FALSE;
2756}
2757
2758/*
2759 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002760 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002762 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 * Returns NULL for an ambiguous user command.
2764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 static char_u *
2766find_command(eap, full)
2767 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002768 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 int len;
2771 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002772 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774 /*
2775 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002776 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 * - the 'k' command can directly be followed by any character.
2778 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2779 * but :sre[wind] is another command, as are :scrip[tnames],
2780 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002781 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 */
2783 p = eap->cmd;
2784 if (*p == 'k')
2785 {
2786 eap->cmdidx = CMD_k;
2787 ++p;
2788 }
2789 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002790 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2791 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 || p[1] == 'g'
2793 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2794 || p[1] == 'I'
2795 || (p[1] == 'r' && p[2] != 'e')))
2796 {
2797 eap->cmdidx = CMD_substitute;
2798 ++p;
2799 }
2800 else
2801 {
2802 while (ASCII_ISALPHA(*p))
2803 ++p;
2804 /* check for non-alpha command */
2805 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2806 ++p;
2807 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002808 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2809 {
2810 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2811 * :delete with the 'l' flag. Same for 'p'. */
2812 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002813 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002814 break;
2815 if (i == len - 1)
2816 {
2817 --len;
2818 if (p[-1] == 'l')
2819 eap->flags |= EXFLAG_LIST;
2820 else
2821 eap->flags |= EXFLAG_PRINT;
2822 }
2823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
2825 if (ASCII_ISLOWER(*eap->cmd))
2826 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2827 else
2828 eap->cmdidx = cmdidxs[26];
2829
2830 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2831 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2832 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2833 (size_t)len) == 0)
2834 {
2835#ifdef FEAT_EVAL
2836 if (full != NULL
2837 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2838 *full = TRUE;
2839#endif
2840 break;
2841 }
2842
2843#ifdef FEAT_USR_CMDS
2844 /* Look for a user defined command as a last resort */
2845 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2846 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002847 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 while (ASCII_ISALNUM(*p))
2849 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002850 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002853 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 eap->cmdidx = CMD_SIZE;
2855 }
2856
2857 return p;
2858}
2859
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002860#ifdef FEAT_USR_CMDS
2861/*
2862 * Search for a user command that matches "eap->cmd".
2863 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2864 * Return a pointer to just after the command.
2865 * Return NULL if there is no matching command.
2866 */
2867 static char_u *
2868find_ucmd(eap, p, full, xp, compl)
2869 exarg_T *eap;
2870 char_u *p; /* end of the command (possibly including count) */
2871 int *full; /* set to TRUE for a full match */
2872 expand_T *xp; /* used for completion, NULL otherwise */
2873 int *compl; /* completion flags or NULL */
2874{
2875 int len = (int)(p - eap->cmd);
2876 int j, k, matchlen = 0;
2877 ucmd_T *uc;
2878 int found = FALSE;
2879 int possible = FALSE;
2880 char_u *cp, *np; /* Point into typed cmd and test name */
2881 garray_T *gap;
2882 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2883 only full match global is accepted. */
2884
2885 /*
2886 * Look for buffer-local user commands first, then global ones.
2887 */
2888 gap = &curbuf->b_ucmds;
2889 for (;;)
2890 {
2891 for (j = 0; j < gap->ga_len; ++j)
2892 {
2893 uc = USER_CMD_GA(gap, j);
2894 cp = eap->cmd;
2895 np = uc->uc_name;
2896 k = 0;
2897 while (k < len && *np != NUL && *cp++ == *np++)
2898 k++;
2899 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2900 {
2901 /* If finding a second match, the command is ambiguous. But
2902 * not if a buffer-local command wasn't a full match and a
2903 * global command is a full match. */
2904 if (k == len && found && *np != NUL)
2905 {
2906 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002907 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002908 amb_local = TRUE;
2909 }
2910
2911 if (!found || (k == len && *np == NUL))
2912 {
2913 /* If we matched up to a digit, then there could
2914 * be another command including the digit that we
2915 * should use instead.
2916 */
2917 if (k == len)
2918 found = TRUE;
2919 else
2920 possible = TRUE;
2921
2922 if (gap == &ucmds)
2923 eap->cmdidx = CMD_USER;
2924 else
2925 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002926 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002927 eap->useridx = j;
2928
2929# ifdef FEAT_CMDL_COMPL
2930 if (compl != NULL)
2931 *compl = uc->uc_compl;
2932# ifdef FEAT_EVAL
2933 if (xp != NULL)
2934 {
2935 xp->xp_arg = uc->uc_compl_arg;
2936 xp->xp_scriptID = uc->uc_scriptID;
2937 }
2938# endif
2939# endif
2940 /* Do not search for further abbreviations
2941 * if this is an exact match. */
2942 matchlen = k;
2943 if (k == len && *np == NUL)
2944 {
2945 if (full != NULL)
2946 *full = TRUE;
2947 amb_local = FALSE;
2948 break;
2949 }
2950 }
2951 }
2952 }
2953
2954 /* Stop if we found a full match or searched all. */
2955 if (j < gap->ga_len || gap == &ucmds)
2956 break;
2957 gap = &ucmds;
2958 }
2959
2960 /* Only found ambiguous matches. */
2961 if (amb_local)
2962 {
2963 if (xp != NULL)
2964 xp->xp_context = EXPAND_UNSUCCESSFUL;
2965 return NULL;
2966 }
2967
2968 /* The match we found may be followed immediately by a number. Move "p"
2969 * back to point to it. */
2970 if (found || possible)
2971 return p + (matchlen - len);
2972 return p;
2973}
2974#endif
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00002977static struct cmdmod
2978{
2979 char *name;
2980 int minlen;
2981 int has_count; /* :123verbose :3tab */
2982} cmdmods[] = {
2983 {"aboveleft", 3, FALSE},
2984 {"belowright", 3, FALSE},
2985 {"botright", 2, FALSE},
2986 {"browse", 3, FALSE},
2987 {"confirm", 4, FALSE},
2988 {"hide", 3, FALSE},
2989 {"keepalt", 5, FALSE},
2990 {"keepjumps", 5, FALSE},
2991 {"keepmarks", 3, FALSE},
2992 {"leftabove", 5, FALSE},
2993 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00002994 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00002995 {"rightbelow", 6, FALSE},
2996 {"sandbox", 3, FALSE},
2997 {"silent", 3, FALSE},
2998 {"tab", 3, TRUE},
2999 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003000 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003001 {"verbose", 4, TRUE},
3002 {"vertical", 4, FALSE},
3003};
3004
3005/*
3006 * Return length of a command modifier (including optional count).
3007 * Return zero when it's not a modifier.
3008 */
3009 int
3010modifier_len(cmd)
3011 char_u *cmd;
3012{
3013 int i, j;
3014 char_u *p = cmd;
3015
3016 if (VIM_ISDIGIT(*cmd))
3017 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003018 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003019 {
3020 for (j = 0; p[j] != NUL; ++j)
3021 if (p[j] != cmdmods[i].name[j])
3022 break;
3023 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3024 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003025 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003026 }
3027 return 0;
3028}
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030/*
3031 * Return > 0 if an Ex command "name" exists.
3032 * Return 2 if there is an exact match.
3033 * Return 3 if there is an ambiguous match.
3034 */
3035 int
3036cmd_exists(name)
3037 char_u *name;
3038{
3039 exarg_T ea;
3040 int full = FALSE;
3041 int i;
3042 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003043 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
3045 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003046 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 {
3048 for (j = 0; name[j] != NUL; ++j)
3049 if (name[j] != cmdmods[i].name[j])
3050 break;
3051 if (name[j] == NUL && j >= cmdmods[i].minlen)
3052 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3053 }
3054
Bram Moolenaara9587612006-05-04 21:47:50 +00003055 /* Check built-in commands and user defined commands.
3056 * For ":2match" and ":3match" we need to skip the number. */
3057 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003059 p = find_command(&ea, &full);
3060 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003062 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3063 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003064 if (*skipwhite(p) != NUL)
3065 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3067}
3068#endif
3069
3070/*
3071 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3072 * we don't need/want deleted. Maybe this could be done better if we didn't
3073 * repeat all this stuff. The only problem is that they may not stay
3074 * perfectly compatible with each other, but then the command line syntax
3075 * probably won't change that much -- webb.
3076 */
3077 char_u *
3078set_one_cmd_context(xp, buff)
3079 expand_T *xp;
3080 char_u *buff; /* buffer for command string */
3081{
3082 char_u *p;
3083 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003084 int len = 0;
3085 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3087 int compl = EXPAND_NOTHING;
3088#endif
3089#ifdef FEAT_CMDL_COMPL
3090 int delim;
3091#endif
3092 int forceit = FALSE;
3093 int usefilter = FALSE; /* filter instead of file name */
3094
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003095 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 xp->xp_pattern = buff;
3097 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003098 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
3100/*
3101 * 2. skip comment lines and leading space, colons or bars
3102 */
3103 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3104 ;
3105 xp->xp_pattern = cmd;
3106
3107 if (*cmd == NUL)
3108 return NULL;
3109 if (*cmd == '"') /* ignore comment lines */
3110 {
3111 xp->xp_context = EXPAND_NOTHING;
3112 return NULL;
3113 }
3114
3115/*
3116 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3117 */
3118 cmd = skip_range(cmd, &xp->xp_context);
3119
3120/*
3121 * 4. parse command
3122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 xp->xp_pattern = cmd;
3124 if (*cmd == NUL)
3125 return NULL;
3126 if (*cmd == '"')
3127 {
3128 xp->xp_context = EXPAND_NOTHING;
3129 return NULL;
3130 }
3131
3132 if (*cmd == '|' || *cmd == '\n')
3133 return cmd + 1; /* There's another command */
3134
3135 /*
3136 * Isolate the command and search for it in the command table.
3137 * Exceptions:
3138 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003139 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3141 */
3142 if (*cmd == 'k' && cmd[1] != 'e')
3143 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003144 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 p = cmd + 1;
3146 }
3147 else
3148 {
3149 p = cmd;
3150 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3151 ++p;
3152 /* check for non-alpha command */
3153 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3154 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003155 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003157 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
3159 xp->xp_context = EXPAND_UNSUCCESSFUL;
3160 return NULL;
3161 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003162 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003163 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3164 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3165 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 break;
3167
3168#ifdef FEAT_USR_CMDS
3169 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3171 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#endif
3173 }
3174
3175 /*
3176 * If the cursor is touching the command, and it ends in an alpha-numeric
3177 * character, complete the command name.
3178 */
3179 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3180 return NULL;
3181
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003182 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3185 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 p = cmd + 1;
3188 }
3189#ifdef FEAT_USR_CMDS
3190 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3191 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003192 ea.cmd = cmd;
3193 p = find_ucmd(&ea, p, NULL, xp,
3194# if defined(FEAT_CMDL_COMPL)
3195 &compl
3196# else
3197 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003199 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003200 if (p == NULL)
3201 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
3203#endif
3204 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003205 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 {
3207 /* Not still touching the command and it was an illegal one */
3208 xp->xp_context = EXPAND_UNSUCCESSFUL;
3209 return NULL;
3210 }
3211
3212 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3213
3214 if (*p == '!') /* forced commands */
3215 {
3216 forceit = TRUE;
3217 ++p;
3218 }
3219
3220/*
3221 * 5. parse arguments
3222 */
3223#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003224 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003226 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 arg = skipwhite(p);
3229
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003230 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
3232 if (*arg == '>') /* append */
3233 {
3234 if (*++arg == '>')
3235 ++arg;
3236 arg = skipwhite(arg);
3237 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 ++arg;
3241 usefilter = TRUE;
3242 }
3243 }
3244
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 usefilter = forceit; /* :r! filter if forced */
3248 if (*arg == '!') /* :r !filter */
3249 {
3250 ++arg;
3251 usefilter = TRUE;
3252 }
3253 }
3254
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003255 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
3257 while (*arg == *cmd) /* allow any number of '>' or '<' */
3258 ++arg;
3259 arg = skipwhite(arg);
3260 }
3261
3262 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003263 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 /* Check if we're in the +command */
3266 p = arg + 1;
3267 arg = skip_cmd_arg(arg, FALSE);
3268
3269 /* Still touching the command after '+'? */
3270 if (*arg == NUL)
3271 return p;
3272
3273 /* Skip space(s) after +command to get to the real argument */
3274 arg = skipwhite(arg);
3275 }
3276
3277 /*
3278 * Check for '|' to separate commands and '"' to start comments.
3279 * Don't do this for ":read !cmd" and ":write !cmd".
3280 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003281 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
3283 p = arg;
3284 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003285 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 p += 2;
3287 while (*p)
3288 {
3289 if (*p == Ctrl_V)
3290 {
3291 if (p[1] != NUL)
3292 ++p;
3293 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003294 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 || *p == '|' || *p == '\n')
3296 {
3297 if (*(p - 1) != '\\')
3298 {
3299 if (*p == '|' || *p == '\n')
3300 return p + 1;
3301 return NULL; /* It's a comment */
3302 }
3303 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003304 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306 }
3307
3308 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003309 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 vim_strchr((char_u *)"|\"", *arg) == NULL)
3311 return NULL;
3312
3313 /* Find start of last argument (argument just before cursor): */
3314 p = buff + STRLEN(buff);
3315 while (p != arg && *p != ' ' && *p != TAB)
3316 p--;
3317 if (*p == ' ' || *p == TAB)
3318 p++;
3319 xp->xp_pattern = p;
3320
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003321 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003323 int c;
3324 int in_quote = FALSE;
3325 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327 /*
3328 * Allow spaces within back-quotes to count as part of the argument
3329 * being expanded.
3330 */
3331 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003332 p = xp->xp_pattern;
3333 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003335#ifdef FEAT_MBYTE
3336 if (has_mbyte)
3337 c = mb_ptr2char(p);
3338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003340 c = *p;
3341 if (c == '\\' && p[1] != NUL)
3342 ++p;
3343 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
3345 if (!in_quote)
3346 {
3347 xp->xp_pattern = p;
3348 bow = p + 1;
3349 }
3350 in_quote = !in_quote;
3351 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003352 /* An argument can contain just about everything, except
3353 * characters that end the command and white space. */
3354 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003355#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003356 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003357#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003358 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003359 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003360 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003361 while (*p != NUL)
3362 {
3363#ifdef FEAT_MBYTE
3364 if (has_mbyte)
3365 c = mb_ptr2char(p);
3366 else
3367#endif
3368 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003369 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003370 break;
3371#ifdef FEAT_MBYTE
3372 if (has_mbyte)
3373 len = (*mb_ptr2len)(p);
3374 else
3375#endif
3376 len = 1;
3377 mb_ptr_adv(p);
3378 }
3379 if (in_quote)
3380 bow = p;
3381 else
3382 xp->xp_pattern = p;
3383 p -= len;
3384 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003385 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387
3388 /*
3389 * If we are still inside the quotes, and we passed a space, just
3390 * expand from there.
3391 */
3392 if (bow != NULL && in_quote)
3393 xp->xp_pattern = bow;
3394 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003395
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003396#ifndef BACKSLASH_IN_FILENAME
3397 /* For a shell command more chars need to be escaped. */
3398 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003399 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003400 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003401
3402 /* When still after the command name expand executables. */
3403 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003404 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003405 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 /* Check for environment variable */
3409 if (*xp->xp_pattern == '$'
3410#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3411 || *xp->xp_pattern == '%'
3412#endif
3413 )
3414 {
3415 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3416 if (!vim_isIDc(*p))
3417 break;
3418 if (*p == NUL)
3419 {
3420 xp->xp_context = EXPAND_ENV_VARS;
3421 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003422#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3423 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003424 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003425 compl = EXPAND_ENV_VARS;
3426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428 }
3429 }
3430
3431/*
3432 * 6. switch on command name
3433 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003434 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
3436 case CMD_cd:
3437 case CMD_chdir:
3438 case CMD_lcd:
3439 case CMD_lchdir:
3440 if (xp->xp_context == EXPAND_FILES)
3441 xp->xp_context = EXPAND_DIRECTORIES;
3442 break;
3443 case CMD_help:
3444 xp->xp_context = EXPAND_HELP;
3445 xp->xp_pattern = arg;
3446 break;
3447
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003448 /* Command modifiers: return the argument.
3449 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003451 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 case CMD_belowright:
3453 case CMD_botright:
3454 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003455 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003457 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 case CMD_folddoclosed:
3459 case CMD_folddoopen:
3460 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003461 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 case CMD_keepjumps:
3463 case CMD_keepmarks:
3464 case CMD_leftabove:
3465 case CMD_lockmarks:
3466 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003467 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003469 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 case CMD_topleft:
3471 case CMD_verbose:
3472 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003473 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 return arg;
3475
Bram Moolenaar4f688582007-07-24 12:34:30 +00003476#ifdef FEAT_CMDL_COMPL
3477# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 case CMD_match:
3479 if (*arg == NUL || !ends_excmd(*arg))
3480 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003481 /* also complete "None" */
3482 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 arg = skipwhite(skiptowhite(arg));
3484 if (*arg != NUL)
3485 {
3486 xp->xp_context = EXPAND_NOTHING;
3487 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3488 }
3489 }
3490 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493/*
3494 * All completion for the +cmdline_compl feature goes here.
3495 */
3496
3497# ifdef FEAT_USR_CMDS
3498 case CMD_command:
3499 /* Check for attributes */
3500 while (*arg == '-')
3501 {
3502 arg++; /* Skip "-" */
3503 p = skiptowhite(arg);
3504 if (*p == NUL)
3505 {
3506 /* Cursor is still in the attribute */
3507 p = vim_strchr(arg, '=');
3508 if (p == NULL)
3509 {
3510 /* No "=", so complete attribute names */
3511 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3512 xp->xp_pattern = arg;
3513 return NULL;
3514 }
3515
3516 /* For the -complete and -nargs attributes, we complete
3517 * their arguments as well.
3518 */
3519 if (STRNICMP(arg, "complete", p - arg) == 0)
3520 {
3521 xp->xp_context = EXPAND_USER_COMPLETE;
3522 xp->xp_pattern = p + 1;
3523 return NULL;
3524 }
3525 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3526 {
3527 xp->xp_context = EXPAND_USER_NARGS;
3528 xp->xp_pattern = p + 1;
3529 return NULL;
3530 }
3531 return NULL;
3532 }
3533 arg = skipwhite(p);
3534 }
3535
3536 /* After the attributes comes the new command name */
3537 p = skiptowhite(arg);
3538 if (*p == NUL)
3539 {
3540 xp->xp_context = EXPAND_USER_COMMANDS;
3541 xp->xp_pattern = arg;
3542 break;
3543 }
3544
3545 /* And finally comes a normal command */
3546 return skipwhite(p);
3547
3548 case CMD_delcommand:
3549 xp->xp_context = EXPAND_USER_COMMANDS;
3550 xp->xp_pattern = arg;
3551 break;
3552# endif
3553
3554 case CMD_global:
3555 case CMD_vglobal:
3556 delim = *arg; /* get the delimiter */
3557 if (delim)
3558 ++arg; /* skip delimiter if there is one */
3559
3560 while (arg[0] != NUL && arg[0] != delim)
3561 {
3562 if (arg[0] == '\\' && arg[1] != NUL)
3563 ++arg;
3564 ++arg;
3565 }
3566 if (arg[0] != NUL)
3567 return arg + 1;
3568 break;
3569 case CMD_and:
3570 case CMD_substitute:
3571 delim = *arg;
3572 if (delim)
3573 {
3574 /* skip "from" part */
3575 ++arg;
3576 arg = skip_regexp(arg, delim, p_magic, NULL);
3577 }
3578 /* skip "to" part */
3579 while (arg[0] != NUL && arg[0] != delim)
3580 {
3581 if (arg[0] == '\\' && arg[1] != NUL)
3582 ++arg;
3583 ++arg;
3584 }
3585 if (arg[0] != NUL) /* skip delimiter */
3586 ++arg;
3587 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3588 ++arg;
3589 if (arg[0] != NUL)
3590 return arg;
3591 break;
3592 case CMD_isearch:
3593 case CMD_dsearch:
3594 case CMD_ilist:
3595 case CMD_dlist:
3596 case CMD_ijump:
3597 case CMD_psearch:
3598 case CMD_djump:
3599 case CMD_isplit:
3600 case CMD_dsplit:
3601 arg = skipwhite(skipdigits(arg)); /* skip count */
3602 if (*arg == '/') /* Match regexp, not just whole words */
3603 {
3604 for (++arg; *arg && *arg != '/'; arg++)
3605 if (*arg == '\\' && arg[1] != NUL)
3606 arg++;
3607 if (*arg)
3608 {
3609 arg = skipwhite(arg + 1);
3610
3611 /* Check for trailing illegal characters */
3612 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3613 xp->xp_context = EXPAND_NOTHING;
3614 else
3615 return arg;
3616 }
3617 }
3618 break;
3619#ifdef FEAT_AUTOCMD
3620 case CMD_autocmd:
3621 return set_context_in_autocmd(xp, arg, FALSE);
3622
3623 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003624 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 return set_context_in_autocmd(xp, arg, TRUE);
3626#endif
3627 case CMD_set:
3628 set_context_in_set_cmd(xp, arg, 0);
3629 break;
3630 case CMD_setglobal:
3631 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3632 break;
3633 case CMD_setlocal:
3634 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3635 break;
3636 case CMD_tag:
3637 case CMD_stag:
3638 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003639 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 case CMD_tselect:
3641 case CMD_stselect:
3642 case CMD_ptselect:
3643 case CMD_tjump:
3644 case CMD_stjump:
3645 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003646 if (*p_wop != NUL)
3647 xp->xp_context = EXPAND_TAGS_LISTFILES;
3648 else
3649 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 xp->xp_pattern = arg;
3651 break;
3652 case CMD_augroup:
3653 xp->xp_context = EXPAND_AUGROUP;
3654 xp->xp_pattern = arg;
3655 break;
3656#ifdef FEAT_SYN_HL
3657 case CMD_syntax:
3658 set_context_in_syntax_cmd(xp, arg);
3659 break;
3660#endif
3661#ifdef FEAT_EVAL
3662 case CMD_let:
3663 case CMD_if:
3664 case CMD_elseif:
3665 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003666 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 case CMD_echo:
3668 case CMD_echon:
3669 case CMD_execute:
3670 case CMD_echomsg:
3671 case CMD_echoerr:
3672 case CMD_call:
3673 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003674 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 break;
3676
3677 case CMD_unlet:
3678 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3679 arg = xp->xp_pattern + 1;
3680 xp->xp_context = EXPAND_USER_VARS;
3681 xp->xp_pattern = arg;
3682 break;
3683
3684 case CMD_function:
3685 case CMD_delfunction:
3686 xp->xp_context = EXPAND_USER_FUNC;
3687 xp->xp_pattern = arg;
3688 break;
3689
3690 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003691 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 break;
3693#endif
3694 case CMD_highlight:
3695 set_context_in_highlight_cmd(xp, arg);
3696 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003697#ifdef FEAT_CSCOPE
3698 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003699 case CMD_lcscope:
3700 case CMD_scscope:
3701 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003702 break;
3703#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003704#ifdef FEAT_SIGNS
3705 case CMD_sign:
3706 set_context_in_sign_cmd(xp, arg);
3707 break;
3708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709#ifdef FEAT_LISTCMDS
3710 case CMD_bdelete:
3711 case CMD_bwipeout:
3712 case CMD_bunload:
3713 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3714 arg = xp->xp_pattern + 1;
3715 /*FALLTHROUGH*/
3716 case CMD_buffer:
3717 case CMD_sbuffer:
3718 case CMD_checktime:
3719 xp->xp_context = EXPAND_BUFFERS;
3720 xp->xp_pattern = arg;
3721 break;
3722#endif
3723#ifdef FEAT_USR_CMDS
3724 case CMD_USER:
3725 case CMD_USER_BUF:
3726 if (compl != EXPAND_NOTHING)
3727 {
3728 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003729 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
3731# ifdef FEAT_MENU
3732 if (compl == EXPAND_MENUS)
3733 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3734# endif
3735 if (compl == EXPAND_COMMANDS)
3736 return arg;
3737 if (compl == EXPAND_MAPPINGS)
3738 return set_context_in_map_cmd(xp, (char_u *)"map",
3739 arg, forceit, FALSE, FALSE, CMD_map);
3740 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3741 arg = xp->xp_pattern + 1;
3742 xp->xp_pattern = arg;
3743 }
3744 xp->xp_context = compl;
3745 }
3746 break;
3747#endif
3748 case CMD_map: case CMD_noremap:
3749 case CMD_nmap: case CMD_nnoremap:
3750 case CMD_vmap: case CMD_vnoremap:
3751 case CMD_omap: case CMD_onoremap:
3752 case CMD_imap: case CMD_inoremap:
3753 case CMD_cmap: case CMD_cnoremap:
3754 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003755 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 case CMD_unmap:
3757 case CMD_nunmap:
3758 case CMD_vunmap:
3759 case CMD_ounmap:
3760 case CMD_iunmap:
3761 case CMD_cunmap:
3762 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003763 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 case CMD_abbreviate: case CMD_noreabbrev:
3765 case CMD_cabbrev: case CMD_cnoreabbrev:
3766 case CMD_iabbrev: case CMD_inoreabbrev:
3767 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003768 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 case CMD_unabbreviate:
3770 case CMD_cunabbrev:
3771 case CMD_iunabbrev:
3772 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003773 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774#ifdef FEAT_MENU
3775 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3776 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3777 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3778 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3779 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3780 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3781 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3782 case CMD_tmenu: case CMD_tunmenu:
3783 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3784 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3785#endif
3786
3787 case CMD_colorscheme:
3788 xp->xp_context = EXPAND_COLORS;
3789 xp->xp_pattern = arg;
3790 break;
3791
3792 case CMD_compiler:
3793 xp->xp_context = EXPAND_COMPILER;
3794 xp->xp_pattern = arg;
3795 break;
3796
3797#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3798 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3799 case CMD_language:
3800 if (*skiptowhite(arg) == NUL)
3801 {
3802 xp->xp_context = EXPAND_LANGUAGE;
3803 xp->xp_pattern = arg;
3804 }
3805 else
3806 xp->xp_context = EXPAND_NOTHING;
3807 break;
3808#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003809#if defined(FEAT_PROFILE)
3810 case CMD_profile:
3811 set_context_in_profile_cmd(xp, arg);
3812 break;
3813#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003814 case CMD_behave:
3815 xp->xp_context = EXPAND_BEHAVE;
3816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
3818#endif /* FEAT_CMDL_COMPL */
3819
3820 default:
3821 break;
3822 }
3823 return NULL;
3824}
3825
3826/*
3827 * skip a range specifier of the form: addr [,addr] [;addr] ..
3828 *
3829 * Backslashed delimiters after / or ? will be skipped, and commands will
3830 * not be expanded between /'s and ?'s or after "'".
3831 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003832 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 * Returns the "cmd" pointer advanced to beyond the range.
3834 */
3835 char_u *
3836skip_range(cmd, ctx)
3837 char_u *cmd;
3838 int *ctx; /* pointer to xp_context or NULL */
3839{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003840 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaardf177f62005-02-22 08:39:57 +00003842 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
3844 if (*cmd == '\'')
3845 {
3846 if (*++cmd == NUL && ctx != NULL)
3847 *ctx = EXPAND_NOTHING;
3848 }
3849 else if (*cmd == '/' || *cmd == '?')
3850 {
3851 delim = *cmd++;
3852 while (*cmd != NUL && *cmd != delim)
3853 if (*cmd++ == '\\' && *cmd != NUL)
3854 ++cmd;
3855 if (*cmd == NUL && ctx != NULL)
3856 *ctx = EXPAND_NOTHING;
3857 }
3858 if (*cmd != NUL)
3859 ++cmd;
3860 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003861
3862 /* Skip ":" and white space. */
3863 while (*cmd == ':')
3864 cmd = skipwhite(cmd + 1);
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 return cmd;
3867}
3868
3869/*
3870 * get a single EX address
3871 *
3872 * Set ptr to the next character after the part that was interpreted.
3873 * Set ptr to NULL when an error is encountered.
3874 *
3875 * Return MAXLNUM when no Ex address was found.
3876 */
3877 static linenr_T
3878get_address(ptr, skip, to_other_file)
3879 char_u **ptr;
3880 int skip; /* only skip the address, don't use it */
3881 int to_other_file; /* flag: may jump to other file */
3882{
3883 int c;
3884 int i;
3885 long n;
3886 char_u *cmd;
3887 pos_T pos;
3888 pos_T *fp;
3889 linenr_T lnum;
3890
3891 cmd = skipwhite(*ptr);
3892 lnum = MAXLNUM;
3893 do
3894 {
3895 switch (*cmd)
3896 {
3897 case '.': /* '.' - Cursor position */
3898 ++cmd;
3899 lnum = curwin->w_cursor.lnum;
3900 break;
3901
3902 case '$': /* '$' - last line */
3903 ++cmd;
3904 lnum = curbuf->b_ml.ml_line_count;
3905 break;
3906
3907 case '\'': /* ''' - mark */
3908 if (*++cmd == NUL)
3909 {
3910 cmd = NULL;
3911 goto error;
3912 }
3913 if (skip)
3914 ++cmd;
3915 else
3916 {
3917 /* Only accept a mark in another file when it is
3918 * used by itself: ":'M". */
3919 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3920 ++cmd;
3921 if (fp == (pos_T *)-1)
3922 /* Jumped to another file. */
3923 lnum = curwin->w_cursor.lnum;
3924 else
3925 {
3926 if (check_mark(fp) == FAIL)
3927 {
3928 cmd = NULL;
3929 goto error;
3930 }
3931 lnum = fp->lnum;
3932 }
3933 }
3934 break;
3935
3936 case '/':
3937 case '?': /* '/' or '?' - search */
3938 c = *cmd++;
3939 if (skip) /* skip "/pat/" */
3940 {
3941 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3942 if (*cmd == c)
3943 ++cmd;
3944 }
3945 else
3946 {
3947 pos = curwin->w_cursor; /* save curwin->w_cursor */
3948 /*
3949 * When '/' or '?' follows another address, start
3950 * from there.
3951 */
3952 if (lnum != MAXLNUM)
3953 curwin->w_cursor.lnum = lnum;
3954 /*
3955 * Start a forward search at the end of the line.
3956 * Start a backward search at the start of the line.
3957 * This makes sure we never match in the current
3958 * line, and can match anywhere in the
3959 * next/previous line.
3960 */
3961 if (c == '/')
3962 curwin->w_cursor.col = MAXCOL;
3963 else
3964 curwin->w_cursor.col = 0;
3965 searchcmdlen = 0;
3966 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00003967 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 {
3969 curwin->w_cursor = pos;
3970 cmd = NULL;
3971 goto error;
3972 }
3973 lnum = curwin->w_cursor.lnum;
3974 curwin->w_cursor = pos;
3975 /* adjust command string pointer */
3976 cmd += searchcmdlen;
3977 }
3978 break;
3979
3980 case '\\': /* "\?", "\/" or "\&", repeat search */
3981 ++cmd;
3982 if (*cmd == '&')
3983 i = RE_SUBST;
3984 else if (*cmd == '?' || *cmd == '/')
3985 i = RE_SEARCH;
3986 else
3987 {
3988 EMSG(_(e_backslash));
3989 cmd = NULL;
3990 goto error;
3991 }
3992
3993 if (!skip)
3994 {
3995 /*
3996 * When search follows another address, start from
3997 * there.
3998 */
3999 if (lnum != MAXLNUM)
4000 pos.lnum = lnum;
4001 else
4002 pos.lnum = curwin->w_cursor.lnum;
4003
4004 /*
4005 * Start the search just like for the above
4006 * do_search().
4007 */
4008 if (*cmd != '?')
4009 pos.col = MAXCOL;
4010 else
4011 pos.col = 0;
4012 if (searchit(curwin, curbuf, &pos,
4013 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004014 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004015 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 lnum = pos.lnum;
4017 else
4018 {
4019 cmd = NULL;
4020 goto error;
4021 }
4022 }
4023 ++cmd;
4024 break;
4025
4026 default:
4027 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4028 lnum = getdigits(&cmd);
4029 }
4030
4031 for (;;)
4032 {
4033 cmd = skipwhite(cmd);
4034 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4035 break;
4036
4037 if (lnum == MAXLNUM)
4038 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4039 if (VIM_ISDIGIT(*cmd))
4040 i = '+'; /* "number" is same as "+number" */
4041 else
4042 i = *cmd++;
4043 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4044 n = 1;
4045 else
4046 n = getdigits(&cmd);
4047 if (i == '-')
4048 lnum -= n;
4049 else
4050 lnum += n;
4051 }
4052 } while (*cmd == '/' || *cmd == '?');
4053
4054error:
4055 *ptr = cmd;
4056 return lnum;
4057}
4058
4059/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004060 * Get flags from an Ex command argument.
4061 */
4062 static void
4063get_flags(eap)
4064 exarg_T *eap;
4065{
4066 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4067 {
4068 if (*eap->arg == 'l')
4069 eap->flags |= EXFLAG_LIST;
4070 else if (*eap->arg == 'p')
4071 eap->flags |= EXFLAG_PRINT;
4072 else
4073 eap->flags |= EXFLAG_NR;
4074 eap->arg = skipwhite(eap->arg + 1);
4075 }
4076}
4077
4078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 * Function called for command which is Not Implemented. NI!
4080 */
4081 void
4082ex_ni(eap)
4083 exarg_T *eap;
4084{
4085 if (!eap->skip)
4086 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4087}
4088
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004089#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090/*
4091 * Function called for script command which is Not Implemented. NI!
4092 * Skips over ":perl <<EOF" constructs.
4093 */
4094 static void
4095ex_script_ni(eap)
4096 exarg_T *eap;
4097{
4098 if (!eap->skip)
4099 ex_ni(eap);
4100 else
4101 vim_free(script_get(eap, eap->arg));
4102}
4103#endif
4104
4105/*
4106 * Check range in Ex command for validity.
4107 * Return NULL when valid, error message when invalid.
4108 */
4109 static char_u *
4110invalid_range(eap)
4111 exarg_T *eap;
4112{
4113 if ( eap->line1 < 0
4114 || eap->line2 < 0
4115 || eap->line1 > eap->line2
4116 || ((eap->argt & RANGE)
4117 && !(eap->argt & NOTADR)
4118 && eap->line2 > curbuf->b_ml.ml_line_count
4119#ifdef FEAT_DIFF
4120 + (eap->cmdidx == CMD_diffget)
4121#endif
4122 ))
4123 return (char_u *)_(e_invrange);
4124 return NULL;
4125}
4126
4127/*
4128 * Correct the range for zero line number, if required.
4129 */
4130 static void
4131correct_range(eap)
4132 exarg_T *eap;
4133{
4134 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4135 {
4136 if (eap->line1 == 0)
4137 eap->line1 = 1;
4138 if (eap->line2 == 0)
4139 eap->line2 = 1;
4140 }
4141}
4142
Bram Moolenaar748bf032005-02-02 23:04:36 +00004143#ifdef FEAT_QUICKFIX
4144static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4145
4146/*
4147 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4148 * pattern. Otherwise return eap->arg.
4149 */
4150 static char_u *
4151skip_grep_pat(eap)
4152 exarg_T *eap;
4153{
4154 char_u *p = eap->arg;
4155
Bram Moolenaara37420f2006-02-04 22:37:47 +00004156 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4157 || eap->cmdidx == CMD_vimgrepadd
4158 || eap->cmdidx == CMD_lvimgrepadd
4159 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004160 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004161 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004162 if (p == NULL)
4163 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004164 }
4165 return p;
4166}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004167
4168/*
4169 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4170 * in the command line, so that things like % get expanded.
4171 */
4172 static char_u *
4173replace_makeprg(eap, p, cmdlinep)
4174 exarg_T *eap;
4175 char_u *p;
4176 char_u **cmdlinep;
4177{
4178 char_u *new_cmdline;
4179 char_u *program;
4180 char_u *pos;
4181 char_u *ptr;
4182 int len;
4183 int i;
4184
4185 /*
4186 * Don't do it when ":vimgrep" is used for ":grep".
4187 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004188 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4189 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4190 || eap->cmdidx == CMD_grepadd
4191 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004192 && !grep_internal(eap->cmdidx))
4193 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004194 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4195 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004196 {
4197 if (*curbuf->b_p_gp == NUL)
4198 program = p_gp;
4199 else
4200 program = curbuf->b_p_gp;
4201 }
4202 else
4203 {
4204 if (*curbuf->b_p_mp == NUL)
4205 program = p_mp;
4206 else
4207 program = curbuf->b_p_mp;
4208 }
4209
4210 p = skipwhite(p);
4211
4212 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4213 {
4214 /* replace $* by given arguments */
4215 i = 1;
4216 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4217 ++i;
4218 len = (int)STRLEN(p);
4219 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4220 if (new_cmdline == NULL)
4221 return NULL; /* out of memory */
4222 ptr = new_cmdline;
4223 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4224 {
4225 i = (int)(pos - program);
4226 STRNCPY(ptr, program, i);
4227 STRCPY(ptr += i, p);
4228 ptr += len;
4229 program = pos + 2;
4230 }
4231 STRCPY(ptr, program);
4232 }
4233 else
4234 {
4235 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4236 if (new_cmdline == NULL)
4237 return NULL; /* out of memory */
4238 STRCPY(new_cmdline, program);
4239 STRCAT(new_cmdline, " ");
4240 STRCAT(new_cmdline, p);
4241 }
4242 msg_make(p);
4243
4244 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4245 vim_free(*cmdlinep);
4246 *cmdlinep = new_cmdline;
4247 p = new_cmdline;
4248 }
4249 return p;
4250}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004251#endif
4252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253/*
4254 * Expand file name in Ex command argument.
4255 * Return FAIL for failure, OK otherwise.
4256 */
4257 int
4258expand_filename(eap, cmdlinep, errormsgp)
4259 exarg_T *eap;
4260 char_u **cmdlinep;
4261 char_u **errormsgp;
4262{
4263 int has_wildcards; /* need to expand wildcards */
4264 char_u *repl;
4265 int srclen;
4266 char_u *p;
4267 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004268 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
Bram Moolenaar748bf032005-02-02 23:04:36 +00004270#ifdef FEAT_QUICKFIX
4271 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4272 p = skip_grep_pat(eap);
4273#else
4274 p = eap->arg;
4275#endif
4276
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 /*
4278 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4279 * the file name contains a wildcard it should not cause expanding.
4280 * (it will be expanded anyway if there is a wildcard before replacing).
4281 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004282 has_wildcards = mch_has_wildcard(p);
4283 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004285#ifdef FEAT_EVAL
4286 /* Skip over `=expr`, wildcards in it are not expanded. */
4287 if (p[0] == '`' && p[1] == '=')
4288 {
4289 p += 2;
4290 (void)skip_expr(&p);
4291 if (*p == '`')
4292 ++p;
4293 continue;
4294 }
4295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 /*
4297 * Quick check if this cannot be the start of a special string.
4298 * Also removes backslash before '%', '#' and '<'.
4299 */
4300 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4301 {
4302 ++p;
4303 continue;
4304 }
4305
4306 /*
4307 * Try to find a match at this position.
4308 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004309 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4310 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (*errormsgp != NULL) /* error detected */
4312 return FAIL;
4313 if (repl == NULL) /* no match found */
4314 {
4315 p += srclen;
4316 continue;
4317 }
4318
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004319 /* Wildcards won't be expanded below, the replacement is taken
4320 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4321 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4322 {
4323 char_u *l = repl;
4324
4325 repl = expand_env_save(repl);
4326 vim_free(l);
4327 }
4328
Bram Moolenaar63b92542007-03-27 14:57:09 +00004329 /* Need to escape white space et al. with a backslash.
4330 * Don't do this for:
4331 * - replacement that already has been escaped: "##"
4332 * - shell commands (may have to use quotes instead).
4333 * - non-unix systems when there is a single argument (spaces don't
4334 * separate arguments then).
4335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004337 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 && eap->cmdidx != CMD_bang
4339 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004340 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004342 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004344 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345#ifndef UNIX
4346 && !(eap->argt & NOSPC)
4347#endif
4348 )
4349 {
4350 char_u *l;
4351#ifdef BACKSLASH_IN_FILENAME
4352 /* Don't escape a backslash here, because rem_backslash() doesn't
4353 * remove it later. */
4354 static char_u *nobslash = (char_u *)" \t\"|";
4355# define ESCAPE_CHARS nobslash
4356#else
4357# define ESCAPE_CHARS escape_chars
4358#endif
4359
4360 for (l = repl; *l; ++l)
4361 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4362 {
4363 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4364 if (l != NULL)
4365 {
4366 vim_free(repl);
4367 repl = l;
4368 }
4369 break;
4370 }
4371 }
4372
4373 /* For a shell command a '!' must be escaped. */
4374 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004375 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
4377 char_u *l;
4378
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004379 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 if (l != NULL)
4381 {
4382 vim_free(repl);
4383 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004384 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (strstr((char *)p_sh, "sh") != NULL)
4386 {
4387 l = vim_strsave_escaped(repl, (char_u *)"!");
4388 if (l != NULL)
4389 {
4390 vim_free(repl);
4391 repl = l;
4392 }
4393 }
4394 }
4395 }
4396
4397 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4398 vim_free(repl);
4399 if (p == NULL)
4400 return FAIL;
4401 }
4402
4403 /*
4404 * One file argument: Expand wildcards.
4405 * Don't do this with ":r !command" or ":w !command".
4406 */
4407 if ((eap->argt & NOSPC) && !eap->usefilter)
4408 {
4409 /*
4410 * May do this twice:
4411 * 1. Replace environment variables.
4412 * 2. Replace any other wildcards, remove backslashes.
4413 */
4414 for (n = 1; n <= 2; ++n)
4415 {
4416 if (n == 2)
4417 {
4418#ifdef UNIX
4419 /*
4420 * Only for Unix we check for more than one file name.
4421 * For other systems spaces are considered to be part
4422 * of the file name.
4423 * Only check here if there is no wildcard, otherwise
4424 * ExpandOne() will check for errors. This allows
4425 * ":e `ls ve*.c`" on Unix.
4426 */
4427 if (!has_wildcards)
4428 for (p = eap->arg; *p; ++p)
4429 {
4430 /* skip escaped characters */
4431 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4432 ++p;
4433 else if (vim_iswhite(*p))
4434 {
4435 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4436 return FAIL;
4437 }
4438 }
4439#endif
4440
4441 /*
4442 * Halve the number of backslashes (this is Vi compatible).
4443 * For Unix and OS/2, when wildcards are expanded, this is
4444 * done by ExpandOne() below.
4445 */
4446#if defined(UNIX) || defined(OS2)
4447 if (!has_wildcards)
4448#endif
4449 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
4451
4452 if (has_wildcards)
4453 {
4454 if (n == 1)
4455 {
4456 /*
4457 * First loop: May expand environment variables. This
4458 * can be done much faster with expand_env() than with
4459 * something else (e.g., calling a shell).
4460 * After expanding environment variables, check again
4461 * if there are still wildcards present.
4462 */
4463 if (vim_strchr(eap->arg, '$') != NULL
4464 || vim_strchr(eap->arg, '~') != NULL)
4465 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004466 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004467 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 has_wildcards = mch_has_wildcard(NameBuff);
4469 p = NameBuff;
4470 }
4471 else
4472 p = NULL;
4473 }
4474 else /* n == 2 */
4475 {
4476 expand_T xpc;
4477
4478 ExpandInit(&xpc);
4479 xpc.xp_context = EXPAND_FILES;
4480 p = ExpandOne(&xpc, eap->arg, NULL,
4481 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4482 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (p == NULL)
4484 return FAIL;
4485 }
4486 if (p != NULL)
4487 {
4488 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4489 p, cmdlinep);
4490 if (n == 2) /* p came from ExpandOne() */
4491 vim_free(p);
4492 }
4493 }
4494 }
4495 }
4496 return OK;
4497}
4498
4499/*
4500 * Replace part of the command line, keeping eap->cmd, eap->arg and
4501 * eap->nextcmd correct.
4502 * "src" points to the part that is to be replaced, of length "srclen".
4503 * "repl" is the replacement string.
4504 * Returns a pointer to the character after the replaced string.
4505 * Returns NULL for failure.
4506 */
4507 static char_u *
4508repl_cmdline(eap, src, srclen, repl, cmdlinep)
4509 exarg_T *eap;
4510 char_u *src;
4511 int srclen;
4512 char_u *repl;
4513 char_u **cmdlinep;
4514{
4515 int len;
4516 int i;
4517 char_u *new_cmdline;
4518
4519 /*
4520 * The new command line is build in new_cmdline[].
4521 * First allocate it.
4522 * Careful: a "+cmd" argument may have been NUL terminated.
4523 */
4524 len = (int)STRLEN(repl);
4525 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004526 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4528 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4529 return NULL; /* out of memory! */
4530
4531 /*
4532 * Copy the stuff before the expanded part.
4533 * Copy the expanded stuff.
4534 * Copy what came after the expanded part.
4535 * Copy the next commands, if there are any.
4536 */
4537 i = (int)(src - *cmdlinep); /* length of part before match */
4538 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 mch_memmove(new_cmdline + i, repl, (size_t)len);
4541 i += len; /* remember the end of the string */
4542 STRCPY(new_cmdline + i, src + srclen);
4543 src = new_cmdline + i; /* remember where to continue */
4544
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004545 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 i = (int)STRLEN(new_cmdline) + 1;
4548 STRCPY(new_cmdline + i, eap->nextcmd);
4549 eap->nextcmd = new_cmdline + i;
4550 }
4551 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4552 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4553 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4554 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4555 vim_free(*cmdlinep);
4556 *cmdlinep = new_cmdline;
4557
4558 return src;
4559}
4560
4561/*
4562 * Check for '|' to separate commands and '"' to start comments.
4563 */
4564 void
4565separate_nextcmd(eap)
4566 exarg_T *eap;
4567{
4568 char_u *p;
4569
Bram Moolenaar86b68352004-12-27 21:59:20 +00004570#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004571 p = skip_grep_pat(eap);
4572#else
4573 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004574#endif
4575
4576 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
4578 if (*p == Ctrl_V)
4579 {
4580 if (eap->argt & (USECTRLV | XFILE))
4581 ++p; /* skip CTRL-V and next char */
4582 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004583 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004584 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (*p == NUL) /* stop at NUL after CTRL-V */
4586 break;
4587 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004588
4589#ifdef FEAT_EVAL
4590 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004591 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004592 {
4593 p += 2;
4594 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004595 }
4596#endif
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 /* Check for '"': start of comment or '|': next command */
4599 /* :@" and :*" do not start a comment!
4600 * :redir @" doesn't either. */
4601 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4602 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4603 || p != eap->arg)
4604 && (eap->cmdidx != CMD_redir
4605 || p != eap->arg + 1 || p[-1] != '@'))
4606 || *p == '|' || *p == '\n')
4607 {
4608 /*
4609 * We remove the '\' before the '|', unless USECTRLV is used
4610 * AND 'b' is present in 'cpoptions'.
4611 */
4612 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4613 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4614 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004615 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 --p;
4617 }
4618 else
4619 {
4620 eap->nextcmd = check_nextcmd(p);
4621 *p = NUL;
4622 break;
4623 }
4624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4628 del_trailing_spaces(eap->arg);
4629}
4630
4631/*
4632 * get + command from ex argument
4633 */
4634 static char_u *
4635getargcmd(argp)
4636 char_u **argp;
4637{
4638 char_u *arg = *argp;
4639 char_u *command = NULL;
4640
4641 if (*arg == '+') /* +[command] */
4642 {
4643 ++arg;
4644 if (vim_isspace(*arg))
4645 command = dollar_command;
4646 else
4647 {
4648 command = arg;
4649 arg = skip_cmd_arg(command, TRUE);
4650 if (*arg != NUL)
4651 *arg++ = NUL; /* terminate command with NUL */
4652 }
4653
4654 arg = skipwhite(arg); /* skip over spaces */
4655 *argp = arg;
4656 }
4657 return command;
4658}
4659
4660/*
4661 * Find end of "+command" argument. Skip over "\ " and "\\".
4662 */
4663 static char_u *
4664skip_cmd_arg(p, rembs)
4665 char_u *p;
4666 int rembs; /* TRUE to halve the number of backslashes */
4667{
4668 while (*p && !vim_isspace(*p))
4669 {
4670 if (*p == '\\' && p[1] != NUL)
4671 {
4672 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004673 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 else
4675 ++p;
4676 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004677 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 return p;
4680}
4681
4682/*
4683 * Get "++opt=arg" argument.
4684 * Return FAIL or OK.
4685 */
4686 static int
4687getargopt(eap)
4688 exarg_T *eap;
4689{
4690 char_u *arg = eap->arg + 2;
4691 int *pp = NULL;
4692#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004693 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 char_u *p;
4695#endif
4696
4697 /* ":edit ++[no]bin[ary] file" */
4698 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4699 {
4700 if (*arg == 'n')
4701 {
4702 arg += 2;
4703 eap->force_bin = FORCE_NOBIN;
4704 }
4705 else
4706 eap->force_bin = FORCE_BIN;
4707 if (!checkforcmd(&arg, "binary", 3))
4708 return FAIL;
4709 eap->arg = skipwhite(arg);
4710 return OK;
4711 }
4712
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004713 /* ":read ++edit file" */
4714 if (STRNCMP(arg, "edit", 4) == 0)
4715 {
4716 eap->read_edit = TRUE;
4717 eap->arg = skipwhite(arg + 4);
4718 return OK;
4719 }
4720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 if (STRNCMP(arg, "ff", 2) == 0)
4722 {
4723 arg += 2;
4724 pp = &eap->force_ff;
4725 }
4726 else if (STRNCMP(arg, "fileformat", 10) == 0)
4727 {
4728 arg += 10;
4729 pp = &eap->force_ff;
4730 }
4731#ifdef FEAT_MBYTE
4732 else if (STRNCMP(arg, "enc", 3) == 0)
4733 {
4734 arg += 3;
4735 pp = &eap->force_enc;
4736 }
4737 else if (STRNCMP(arg, "encoding", 8) == 0)
4738 {
4739 arg += 8;
4740 pp = &eap->force_enc;
4741 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004742 else if (STRNCMP(arg, "bad", 3) == 0)
4743 {
4744 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004745 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747#endif
4748
4749 if (pp == NULL || *arg != '=')
4750 return FAIL;
4751
4752 ++arg;
4753 *pp = (int)(arg - eap->cmd);
4754 arg = skip_cmd_arg(arg, FALSE);
4755 eap->arg = skipwhite(arg);
4756 *arg = NUL;
4757
4758#ifdef FEAT_MBYTE
4759 if (pp == &eap->force_ff)
4760 {
4761#endif
4762 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4763 return FAIL;
4764#ifdef FEAT_MBYTE
4765 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004766 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 {
4768 /* Make 'fileencoding' lower case. */
4769 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4770 *p = TOLOWER_ASC(*p);
4771 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004772 else
4773 {
4774 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4775 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004776 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004777 if (STRICMP(p, "keep") == 0)
4778 eap->bad_char = BAD_KEEP;
4779 else if (STRICMP(p, "drop") == 0)
4780 eap->bad_char = BAD_DROP;
4781 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4782 eap->bad_char = *p;
4783 else
4784 return FAIL;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786#endif
4787
4788 return OK;
4789}
4790
4791/*
4792 * ":abbreviate" and friends.
4793 */
4794 static void
4795ex_abbreviate(eap)
4796 exarg_T *eap;
4797{
4798 do_exmap(eap, TRUE); /* almost the same as mapping */
4799}
4800
4801/*
4802 * ":map" and friends.
4803 */
4804 static void
4805ex_map(eap)
4806 exarg_T *eap;
4807{
4808 /*
4809 * If we are sourcing .exrc or .vimrc in current directory we
4810 * print the mappings for security reasons.
4811 */
4812 if (secure)
4813 {
4814 secure = 2;
4815 msg_outtrans(eap->cmd);
4816 msg_putchar('\n');
4817 }
4818 do_exmap(eap, FALSE);
4819}
4820
4821/*
4822 * ":unmap" and friends.
4823 */
4824 static void
4825ex_unmap(eap)
4826 exarg_T *eap;
4827{
4828 do_exmap(eap, FALSE);
4829}
4830
4831/*
4832 * ":mapclear" and friends.
4833 */
4834 static void
4835ex_mapclear(eap)
4836 exarg_T *eap;
4837{
4838 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4839}
4840
4841/*
4842 * ":abclear" and friends.
4843 */
4844 static void
4845ex_abclear(eap)
4846 exarg_T *eap;
4847{
4848 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4849}
4850
4851#ifdef FEAT_AUTOCMD
4852 static void
4853ex_autocmd(eap)
4854 exarg_T *eap;
4855{
4856 /*
4857 * Disallow auto commands from .exrc and .vimrc in current
4858 * directory for security reasons.
4859 */
4860 if (secure)
4861 {
4862 secure = 2;
4863 eap->errmsg = e_curdir;
4864 }
4865 else if (eap->cmdidx == CMD_autocmd)
4866 do_autocmd(eap->arg, eap->forceit);
4867 else
4868 do_augroup(eap->arg, eap->forceit);
4869}
4870
4871/*
4872 * ":doautocmd": Apply the automatic commands to the current buffer.
4873 */
4874 static void
4875ex_doautocmd(eap)
4876 exarg_T *eap;
4877{
4878 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004879 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880}
4881#endif
4882
4883#ifdef FEAT_LISTCMDS
4884/*
4885 * :[N]bunload[!] [N] [bufname] unload buffer
4886 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4887 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4888 */
4889 static void
4890ex_bunload(eap)
4891 exarg_T *eap;
4892{
4893 eap->errmsg = do_bufdel(
4894 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4895 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4896 : DOBUF_UNLOAD, eap->arg,
4897 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4898}
4899
4900/*
4901 * :[N]buffer [N] to buffer N
4902 * :[N]sbuffer [N] to buffer N
4903 */
4904 static void
4905ex_buffer(eap)
4906 exarg_T *eap;
4907{
4908 if (*eap->arg)
4909 eap->errmsg = e_trailing;
4910 else
4911 {
4912 if (eap->addr_count == 0) /* default is current buffer */
4913 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4914 else
4915 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4916 }
4917}
4918
4919/*
4920 * :[N]bmodified [N] to next mod. buffer
4921 * :[N]sbmodified [N] to next mod. buffer
4922 */
4923 static void
4924ex_bmodified(eap)
4925 exarg_T *eap;
4926{
4927 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4928}
4929
4930/*
4931 * :[N]bnext [N] to next buffer
4932 * :[N]sbnext [N] split and to next buffer
4933 */
4934 static void
4935ex_bnext(eap)
4936 exarg_T *eap;
4937{
4938 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4939}
4940
4941/*
4942 * :[N]bNext [N] to previous buffer
4943 * :[N]bprevious [N] to previous buffer
4944 * :[N]sbNext [N] split and to previous buffer
4945 * :[N]sbprevious [N] split and to previous buffer
4946 */
4947 static void
4948ex_bprevious(eap)
4949 exarg_T *eap;
4950{
4951 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4952}
4953
4954/*
4955 * :brewind to first buffer
4956 * :bfirst to first buffer
4957 * :sbrewind split and to first buffer
4958 * :sbfirst split and to first buffer
4959 */
4960 static void
4961ex_brewind(eap)
4962 exarg_T *eap;
4963{
4964 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4965}
4966
4967/*
4968 * :blast to last buffer
4969 * :sblast split and to last buffer
4970 */
4971 static void
4972ex_blast(eap)
4973 exarg_T *eap;
4974{
4975 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4976}
4977#endif
4978
4979 int
4980ends_excmd(c)
4981 int c;
4982{
4983 return (c == NUL || c == '|' || c == '"' || c == '\n');
4984}
4985
4986#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4987 || defined(PROTO)
4988/*
4989 * Return the next command, after the first '|' or '\n'.
4990 * Return NULL if not found.
4991 */
4992 char_u *
4993find_nextcmd(p)
4994 char_u *p;
4995{
4996 while (*p != '|' && *p != '\n')
4997 {
4998 if (*p == NUL)
4999 return NULL;
5000 ++p;
5001 }
5002 return (p + 1);
5003}
5004#endif
5005
5006/*
5007 * Check if *p is a separator between Ex commands.
5008 * Return NULL if it isn't, (p + 1) if it is.
5009 */
5010 char_u *
5011check_nextcmd(p)
5012 char_u *p;
5013{
5014 p = skipwhite(p);
5015 if (*p == '|' || *p == '\n')
5016 return (p + 1);
5017 else
5018 return NULL;
5019}
5020
5021/*
5022 * - if there are more files to edit
5023 * - and this is the last window
5024 * - and forceit not used
5025 * - and not repeated twice on a row
5026 * return FAIL and give error message if 'message' TRUE
5027 * return OK otherwise
5028 */
5029 static int
5030check_more(message, forceit)
5031 int message; /* when FALSE check only, no messages */
5032 int forceit;
5033{
5034 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5035
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005036 if (!forceit && only_one_window()
5037 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
5039 if (message)
5040 {
5041#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5042 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5043 {
5044 char_u buff[IOSIZE];
5045
5046 if (n == 1)
5047 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5048 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005049 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 _("%d more files to edit. Quit anyway?"), n);
5051 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5052 return OK;
5053 return FAIL;
5054 }
5055#endif
5056 if (n == 1)
5057 EMSG(_("E173: 1 more file to edit"));
5058 else
5059 EMSGN(_("E173: %ld more files to edit"), n);
5060 quitmore = 2; /* next try to quit is allowed */
5061 }
5062 return FAIL;
5063 }
5064 return OK;
5065}
5066
5067#ifdef FEAT_CMDL_COMPL
5068/*
5069 * Function given to ExpandGeneric() to obtain the list of command names.
5070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 char_u *
5072get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005073 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 int idx;
5075{
5076 if (idx >= (int)CMD_SIZE)
5077# ifdef FEAT_USR_CMDS
5078 return get_user_command_name(idx);
5079# else
5080 return NULL;
5081# endif
5082 return cmdnames[idx].cmd_name;
5083}
5084#endif
5085
5086#if defined(FEAT_USR_CMDS) || defined(PROTO)
5087static 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));
5088static void uc_list __ARGS((char_u *name, size_t name_len));
5089static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5090static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5091static 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));
5092
5093 static int
5094uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5095 char_u *name;
5096 size_t name_len;
5097 char_u *rep;
5098 long argt;
5099 long def;
5100 int flags;
5101 int compl;
5102 char_u *compl_arg;
5103 int force;
5104{
5105 ucmd_T *cmd = NULL;
5106 char_u *p;
5107 int i;
5108 int cmp = 1;
5109 char_u *rep_buf = NULL;
5110 garray_T *gap;
5111
Bram Moolenaar9c102382006-05-03 21:26:49 +00005112 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (rep_buf == NULL)
5114 {
5115 /* Can't replace termcodes - try using the string as is */
5116 rep_buf = vim_strsave(rep);
5117
5118 /* Give up if out of memory */
5119 if (rep_buf == NULL)
5120 return FAIL;
5121 }
5122
5123 /* get address of growarray: global or in curbuf */
5124 if (flags & UC_BUFFER)
5125 {
5126 gap = &curbuf->b_ucmds;
5127 if (gap->ga_itemsize == 0)
5128 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5129 }
5130 else
5131 gap = &ucmds;
5132
5133 /* Search for the command in the already defined commands. */
5134 for (i = 0; i < gap->ga_len; ++i)
5135 {
5136 size_t len;
5137
5138 cmd = USER_CMD_GA(gap, i);
5139 len = STRLEN(cmd->uc_name);
5140 cmp = STRNCMP(name, cmd->uc_name, name_len);
5141 if (cmp == 0)
5142 {
5143 if (name_len < len)
5144 cmp = -1;
5145 else if (name_len > len)
5146 cmp = 1;
5147 }
5148
5149 if (cmp == 0)
5150 {
5151 if (!force)
5152 {
5153 EMSG(_("E174: Command already exists: add ! to replace it"));
5154 goto fail;
5155 }
5156
5157 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005158 cmd->uc_rep = NULL;
5159#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5160 vim_free(cmd->uc_compl_arg);
5161 cmd->uc_compl_arg = NULL;
5162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164 }
5165
5166 /* Stop as soon as we pass the name to add */
5167 if (cmp < 0)
5168 break;
5169 }
5170
5171 /* Extend the array unless we're replacing an existing command */
5172 if (cmp != 0)
5173 {
5174 if (ga_grow(gap, 1) != OK)
5175 goto fail;
5176 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5177 goto fail;
5178
5179 cmd = USER_CMD_GA(gap, i);
5180 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5181
5182 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183
5184 cmd->uc_name = p;
5185 }
5186
5187 cmd->uc_rep = rep_buf;
5188 cmd->uc_argt = argt;
5189 cmd->uc_def = def;
5190 cmd->uc_compl = compl;
5191#ifdef FEAT_EVAL
5192 cmd->uc_scriptID = current_SID;
5193# ifdef FEAT_CMDL_COMPL
5194 cmd->uc_compl_arg = compl_arg;
5195# endif
5196#endif
5197
5198 return OK;
5199
5200fail:
5201 vim_free(rep_buf);
5202#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5203 vim_free(compl_arg);
5204#endif
5205 return FAIL;
5206}
5207
5208/*
5209 * List of names for completion for ":command" with the EXPAND_ flag.
5210 * Must be alphabetical for completion.
5211 */
5212static struct
5213{
5214 int expand;
5215 char *name;
5216} command_complete[] =
5217{
5218 {EXPAND_AUGROUP, "augroup"},
5219 {EXPAND_BUFFERS, "buffer"},
5220 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005221#if defined(FEAT_CSCOPE)
5222 {EXPAND_CSCOPE, "cscope"},
5223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5225 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005226 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227#endif
5228 {EXPAND_DIRECTORIES, "dir"},
5229 {EXPAND_ENV_VARS, "environment"},
5230 {EXPAND_EVENTS, "event"},
5231 {EXPAND_EXPRESSION, "expression"},
5232 {EXPAND_FILES, "file"},
5233 {EXPAND_FUNCTIONS, "function"},
5234 {EXPAND_HELP, "help"},
5235 {EXPAND_HIGHLIGHT, "highlight"},
5236 {EXPAND_MAPPINGS, "mapping"},
5237 {EXPAND_MENUS, "menu"},
5238 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005239 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005240#if defined(FEAT_SIGNS)
5241 {EXPAND_SIGN, "sign"},
5242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {EXPAND_TAGS, "tag"},
5244 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5245 {EXPAND_USER_VARS, "var"},
5246 {0, NULL}
5247};
5248
5249 static void
5250uc_list(name, name_len)
5251 char_u *name;
5252 size_t name_len;
5253{
5254 int i, j;
5255 int found = FALSE;
5256 ucmd_T *cmd;
5257 int len;
5258 long a;
5259 garray_T *gap;
5260
5261 gap = &curbuf->b_ucmds;
5262 for (;;)
5263 {
5264 for (i = 0; i < gap->ga_len; ++i)
5265 {
5266 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005267 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268
5269 /* Skip commands which don't match the requested prefix */
5270 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5271 continue;
5272
5273 /* Put out the title first time */
5274 if (!found)
5275 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5276 found = TRUE;
5277 msg_putchar('\n');
5278 if (got_int)
5279 break;
5280
5281 /* Special cases */
5282 msg_putchar(a & BANG ? '!' : ' ');
5283 msg_putchar(a & REGSTR ? '"' : ' ');
5284 msg_putchar(gap != &ucmds ? 'b' : ' ');
5285 msg_putchar(' ');
5286
5287 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5288 len = (int)STRLEN(cmd->uc_name) + 4;
5289
5290 do {
5291 msg_putchar(' ');
5292 ++len;
5293 } while (len < 16);
5294
5295 len = 0;
5296
5297 /* Arguments */
5298 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5299 {
5300 case 0: IObuff[len++] = '0'; break;
5301 case (EXTRA): IObuff[len++] = '*'; break;
5302 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5303 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5304 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5305 }
5306
5307 do {
5308 IObuff[len++] = ' ';
5309 } while (len < 5);
5310
5311 /* Range */
5312 if (a & (RANGE|COUNT))
5313 {
5314 if (a & COUNT)
5315 {
5316 /* -count=N */
5317 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5318 len += (int)STRLEN(IObuff + len);
5319 }
5320 else if (a & DFLALL)
5321 IObuff[len++] = '%';
5322 else if (cmd->uc_def >= 0)
5323 {
5324 /* -range=N */
5325 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5326 len += (int)STRLEN(IObuff + len);
5327 }
5328 else
5329 IObuff[len++] = '.';
5330 }
5331
5332 do {
5333 IObuff[len++] = ' ';
5334 } while (len < 11);
5335
5336 /* Completion */
5337 for (j = 0; command_complete[j].expand != 0; ++j)
5338 if (command_complete[j].expand == cmd->uc_compl)
5339 {
5340 STRCPY(IObuff + len, command_complete[j].name);
5341 len += (int)STRLEN(IObuff + len);
5342 break;
5343 }
5344
5345 do {
5346 IObuff[len++] = ' ';
5347 } while (len < 21);
5348
5349 IObuff[len] = '\0';
5350 msg_outtrans(IObuff);
5351
5352 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005353#ifdef FEAT_EVAL
5354 if (p_verbose > 0)
5355 last_set_msg(cmd->uc_scriptID);
5356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 out_flush();
5358 ui_breakcheck();
5359 if (got_int)
5360 break;
5361 }
5362 if (gap == &ucmds || i < gap->ga_len)
5363 break;
5364 gap = &ucmds;
5365 }
5366
5367 if (!found)
5368 MSG(_("No user-defined commands found"));
5369}
5370
5371 static char_u *
5372uc_fun_cmd()
5373{
5374 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5375 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5376 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5377 0xb9, 0x7f, 0};
5378 int i;
5379
5380 for (i = 0; fcmd[i]; ++i)
5381 IObuff[i] = fcmd[i] - 0x40;
5382 IObuff[i] = 0;
5383 return IObuff;
5384}
5385
5386 static int
5387uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5388 char_u *attr;
5389 size_t len;
5390 long *argt;
5391 long *def;
5392 int *flags;
5393 int *compl;
5394 char_u **compl_arg;
5395{
5396 char_u *p;
5397
5398 if (len == 0)
5399 {
5400 EMSG(_("E175: No attribute specified"));
5401 return FAIL;
5402 }
5403
5404 /* First, try the simple attributes (no arguments) */
5405 if (STRNICMP(attr, "bang", len) == 0)
5406 *argt |= BANG;
5407 else if (STRNICMP(attr, "buffer", len) == 0)
5408 *flags |= UC_BUFFER;
5409 else if (STRNICMP(attr, "register", len) == 0)
5410 *argt |= REGSTR;
5411 else if (STRNICMP(attr, "bar", len) == 0)
5412 *argt |= TRLBAR;
5413 else
5414 {
5415 int i;
5416 char_u *val = NULL;
5417 size_t vallen = 0;
5418 size_t attrlen = len;
5419
5420 /* Look for the attribute name - which is the part before any '=' */
5421 for (i = 0; i < (int)len; ++i)
5422 {
5423 if (attr[i] == '=')
5424 {
5425 val = &attr[i + 1];
5426 vallen = len - i - 1;
5427 attrlen = i;
5428 break;
5429 }
5430 }
5431
5432 if (STRNICMP(attr, "nargs", attrlen) == 0)
5433 {
5434 if (vallen == 1)
5435 {
5436 if (*val == '0')
5437 /* Do nothing - this is the default */;
5438 else if (*val == '1')
5439 *argt |= (EXTRA | NOSPC | NEEDARG);
5440 else if (*val == '*')
5441 *argt |= EXTRA;
5442 else if (*val == '?')
5443 *argt |= (EXTRA | NOSPC);
5444 else if (*val == '+')
5445 *argt |= (EXTRA | NEEDARG);
5446 else
5447 goto wrong_nargs;
5448 }
5449 else
5450 {
5451wrong_nargs:
5452 EMSG(_("E176: Invalid number of arguments"));
5453 return FAIL;
5454 }
5455 }
5456 else if (STRNICMP(attr, "range", attrlen) == 0)
5457 {
5458 *argt |= RANGE;
5459 if (vallen == 1 && *val == '%')
5460 *argt |= DFLALL;
5461 else if (val != NULL)
5462 {
5463 p = val;
5464 if (*def >= 0)
5465 {
5466two_count:
5467 EMSG(_("E177: Count cannot be specified twice"));
5468 return FAIL;
5469 }
5470
5471 *def = getdigits(&p);
5472 *argt |= (ZEROR | NOTADR);
5473
5474 if (p != val + vallen || vallen == 0)
5475 {
5476invalid_count:
5477 EMSG(_("E178: Invalid default value for count"));
5478 return FAIL;
5479 }
5480 }
5481 }
5482 else if (STRNICMP(attr, "count", attrlen) == 0)
5483 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005484 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486 if (val != NULL)
5487 {
5488 p = val;
5489 if (*def >= 0)
5490 goto two_count;
5491
5492 *def = getdigits(&p);
5493
5494 if (p != val + vallen)
5495 goto invalid_count;
5496 }
5497
5498 if (*def < 0)
5499 *def = 0;
5500 }
5501 else if (STRNICMP(attr, "complete", attrlen) == 0)
5502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 if (val == NULL)
5504 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005505 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 return FAIL;
5507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005509 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5510 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 }
5513 else
5514 {
5515 char_u ch = attr[len];
5516 attr[len] = '\0';
5517 EMSG2(_("E181: Invalid attribute: %s"), attr);
5518 attr[len] = ch;
5519 return FAIL;
5520 }
5521 }
5522
5523 return OK;
5524}
5525
Bram Moolenaara850a712009-01-28 14:42:59 +00005526/*
5527 * ":command ..."
5528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 static void
5530ex_command(eap)
5531 exarg_T *eap;
5532{
5533 char_u *name;
5534 char_u *end;
5535 char_u *p;
5536 long argt = 0;
5537 long def = -1;
5538 int flags = 0;
5539 int compl = EXPAND_NOTHING;
5540 char_u *compl_arg = NULL;
5541 int has_attr = (eap->arg[0] == '-');
5542
5543 p = eap->arg;
5544
5545 /* Check for attributes */
5546 while (*p == '-')
5547 {
5548 ++p;
5549 end = skiptowhite(p);
5550 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5551 == FAIL)
5552 return;
5553 p = skipwhite(end);
5554 }
5555
5556 /* Get the name (if any) and skip to the following argument */
5557 name = p;
5558 if (ASCII_ISALPHA(*p))
5559 while (ASCII_ISALNUM(*p))
5560 ++p;
5561 if (!ends_excmd(*p) && !vim_iswhite(*p))
5562 {
5563 EMSG(_("E182: Invalid command name"));
5564 return;
5565 }
5566 end = p;
5567
5568 /* If there is nothing after the name, and no attributes were specified,
5569 * we are listing commands
5570 */
5571 p = skipwhite(end);
5572 if (!has_attr && ends_excmd(*p))
5573 {
5574 uc_list(name, end - name);
5575 }
5576 else if (!ASCII_ISUPPER(*name))
5577 {
5578 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5579 return;
5580 }
5581 else
5582 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5583 eap->forceit);
5584}
5585
5586/*
5587 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 * Clear all user commands, global and for current buffer.
5589 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005590 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005592 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 uc_clear(&ucmds);
5595 uc_clear(&curbuf->b_ucmds);
5596}
5597
5598/*
5599 * Clear all user commands for "gap".
5600 */
5601 void
5602uc_clear(gap)
5603 garray_T *gap;
5604{
5605 int i;
5606 ucmd_T *cmd;
5607
5608 for (i = 0; i < gap->ga_len; ++i)
5609 {
5610 cmd = USER_CMD_GA(gap, i);
5611 vim_free(cmd->uc_name);
5612 vim_free(cmd->uc_rep);
5613# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5614 vim_free(cmd->uc_compl_arg);
5615# endif
5616 }
5617 ga_clear(gap);
5618}
5619
5620 static void
5621ex_delcommand(eap)
5622 exarg_T *eap;
5623{
5624 int i = 0;
5625 ucmd_T *cmd = NULL;
5626 int cmp = -1;
5627 garray_T *gap;
5628
5629 gap = &curbuf->b_ucmds;
5630 for (;;)
5631 {
5632 for (i = 0; i < gap->ga_len; ++i)
5633 {
5634 cmd = USER_CMD_GA(gap, i);
5635 cmp = STRCMP(eap->arg, cmd->uc_name);
5636 if (cmp <= 0)
5637 break;
5638 }
5639 if (gap == &ucmds || cmp == 0)
5640 break;
5641 gap = &ucmds;
5642 }
5643
5644 if (cmp != 0)
5645 {
5646 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5647 return;
5648 }
5649
5650 vim_free(cmd->uc_name);
5651 vim_free(cmd->uc_rep);
5652# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5653 vim_free(cmd->uc_compl_arg);
5654# endif
5655
5656 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
5658 if (i < gap->ga_len)
5659 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5660}
5661
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005662/*
5663 * split and quote args for <f-args>
5664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 static char_u *
5666uc_split_args(arg, lenp)
5667 char_u *arg;
5668 size_t *lenp;
5669{
5670 char_u *buf;
5671 char_u *p;
5672 char_u *q;
5673 int len;
5674
5675 /* Precalculate length */
5676 p = arg;
5677 len = 2; /* Initial and final quotes */
5678
5679 while (*p)
5680 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005681 if (p[0] == '\\' && p[1] == '\\')
5682 {
5683 len += 2;
5684 p += 2;
5685 }
5686 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 {
5688 len += 1;
5689 p += 2;
5690 }
5691 else if (*p == '\\' || *p == '"')
5692 {
5693 len += 2;
5694 p += 1;
5695 }
5696 else if (vim_iswhite(*p))
5697 {
5698 p = skipwhite(p);
5699 if (*p == NUL)
5700 break;
5701 len += 3; /* "," */
5702 }
5703 else
5704 {
5705 ++len;
5706 ++p;
5707 }
5708 }
5709
5710 buf = alloc(len + 1);
5711 if (buf == NULL)
5712 {
5713 *lenp = 0;
5714 return buf;
5715 }
5716
5717 p = arg;
5718 q = buf;
5719 *q++ = '"';
5720 while (*p)
5721 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005722 if (p[0] == '\\' && p[1] == '\\')
5723 {
5724 *q++ = '\\';
5725 *q++ = '\\';
5726 p += 2;
5727 }
5728 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
5730 *q++ = p[1];
5731 p += 2;
5732 }
5733 else if (*p == '\\' || *p == '"')
5734 {
5735 *q++ = '\\';
5736 *q++ = *p++;
5737 }
5738 else if (vim_iswhite(*p))
5739 {
5740 p = skipwhite(p);
5741 if (*p == NUL)
5742 break;
5743 *q++ = '"';
5744 *q++ = ',';
5745 *q++ = '"';
5746 }
5747 else
5748 {
5749 *q++ = *p++;
5750 }
5751 }
5752 *q++ = '"';
5753 *q = 0;
5754
5755 *lenp = len;
5756 return buf;
5757}
5758
5759/*
5760 * Check for a <> code in a user command.
5761 * "code" points to the '<'. "len" the length of the <> (inclusive).
5762 * "buf" is where the result is to be added.
5763 * "split_buf" points to a buffer used for splitting, caller should free it.
5764 * "split_len" is the length of what "split_buf" contains.
5765 * Returns the length of the replacement, which has been added to "buf".
5766 * Returns -1 if there was no match, and only the "<" has been copied.
5767 */
5768 static size_t
5769uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5770 char_u *code;
5771 size_t len;
5772 char_u *buf;
5773 ucmd_T *cmd; /* the user command we're expanding */
5774 exarg_T *eap; /* ex arguments */
5775 char_u **split_buf;
5776 size_t *split_len;
5777{
5778 size_t result = 0;
5779 char_u *p = code + 1;
5780 size_t l = len - 2;
5781 int quote = 0;
5782 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5783 ct_LT, ct_NONE } type = ct_NONE;
5784
5785 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5786 {
5787 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5788 p += 2;
5789 l -= 2;
5790 }
5791
Bram Moolenaar371d5402006-03-20 21:47:49 +00005792 ++l;
5793 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005795 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005797 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005799 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005801 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005803 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005805 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005807 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 type = ct_REGISTER;
5809
5810 switch (type)
5811 {
5812 case ct_ARGS:
5813 /* Simple case first */
5814 if (*eap->arg == NUL)
5815 {
5816 if (quote == 1)
5817 {
5818 result = 2;
5819 if (buf != NULL)
5820 STRCPY(buf, "''");
5821 }
5822 else
5823 result = 0;
5824 break;
5825 }
5826
5827 /* When specified there is a single argument don't split it.
5828 * Works for ":Cmd %" when % is "a b c". */
5829 if ((eap->argt & NOSPC) && quote == 2)
5830 quote = 1;
5831
5832 switch (quote)
5833 {
5834 case 0: /* No quoting, no splitting */
5835 result = STRLEN(eap->arg);
5836 if (buf != NULL)
5837 STRCPY(buf, eap->arg);
5838 break;
5839 case 1: /* Quote, but don't split */
5840 result = STRLEN(eap->arg) + 2;
5841 for (p = eap->arg; *p; ++p)
5842 {
5843 if (*p == '\\' || *p == '"')
5844 ++result;
5845 }
5846
5847 if (buf != NULL)
5848 {
5849 *buf++ = '"';
5850 for (p = eap->arg; *p; ++p)
5851 {
5852 if (*p == '\\' || *p == '"')
5853 *buf++ = '\\';
5854 *buf++ = *p;
5855 }
5856 *buf = '"';
5857 }
5858
5859 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005860 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 /* This is hard, so only do it once, and cache the result */
5862 if (*split_buf == NULL)
5863 *split_buf = uc_split_args(eap->arg, split_len);
5864
5865 result = *split_len;
5866 if (buf != NULL && result != 0)
5867 STRCPY(buf, *split_buf);
5868
5869 break;
5870 }
5871 break;
5872
5873 case ct_BANG:
5874 result = eap->forceit ? 1 : 0;
5875 if (quote)
5876 result += 2;
5877 if (buf != NULL)
5878 {
5879 if (quote)
5880 *buf++ = '"';
5881 if (eap->forceit)
5882 *buf++ = '!';
5883 if (quote)
5884 *buf = '"';
5885 }
5886 break;
5887
5888 case ct_LINE1:
5889 case ct_LINE2:
5890 case ct_COUNT:
5891 {
5892 char num_buf[20];
5893 long num = (type == ct_LINE1) ? eap->line1 :
5894 (type == ct_LINE2) ? eap->line2 :
5895 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5896 size_t num_len;
5897
5898 sprintf(num_buf, "%ld", num);
5899 num_len = STRLEN(num_buf);
5900 result = num_len;
5901
5902 if (quote)
5903 result += 2;
5904
5905 if (buf != NULL)
5906 {
5907 if (quote)
5908 *buf++ = '"';
5909 STRCPY(buf, num_buf);
5910 buf += num_len;
5911 if (quote)
5912 *buf = '"';
5913 }
5914
5915 break;
5916 }
5917
5918 case ct_REGISTER:
5919 result = eap->regname ? 1 : 0;
5920 if (quote)
5921 result += 2;
5922 if (buf != NULL)
5923 {
5924 if (quote)
5925 *buf++ = '\'';
5926 if (eap->regname)
5927 *buf++ = eap->regname;
5928 if (quote)
5929 *buf = '\'';
5930 }
5931 break;
5932
5933 case ct_LT:
5934 result = 1;
5935 if (buf != NULL)
5936 *buf = '<';
5937 break;
5938
5939 default:
5940 /* Not recognized: just copy the '<' and return -1. */
5941 result = (size_t)-1;
5942 if (buf != NULL)
5943 *buf = '<';
5944 break;
5945 }
5946
5947 return result;
5948}
5949
5950 static void
5951do_ucmd(eap)
5952 exarg_T *eap;
5953{
5954 char_u *buf;
5955 char_u *p;
5956 char_u *q;
5957
5958 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00005959 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00005960 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 size_t len, totlen;
5962
5963 size_t split_len = 0;
5964 char_u *split_buf = NULL;
5965 ucmd_T *cmd;
5966#ifdef FEAT_EVAL
5967 scid_T save_current_SID = current_SID;
5968#endif
5969
5970 if (eap->cmdidx == CMD_USER)
5971 cmd = USER_CMD(eap->useridx);
5972 else
5973 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5974
5975 /*
5976 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00005977 * First round: "buf" is NULL, compute length, allocate "buf".
5978 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 */
5980 buf = NULL;
5981 for (;;)
5982 {
Bram Moolenaara850a712009-01-28 14:42:59 +00005983 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005984 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00005986
5987 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {
Bram Moolenaara850a712009-01-28 14:42:59 +00005989 start = vim_strchr(p, '<');
5990 if (start != NULL)
5991 end = vim_strchr(start + 1, '>');
5992 if (buf != NULL)
5993 {
5994 ksp = vim_strchr(p, K_SPECIAL);
5995 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
5996 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
5997# ifdef FEAT_GUI
5998 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
5999# endif
6000 ))
6001 {
6002 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6003 * KS_SPECIAL KE_FILLER, like for mappings, but
6004 * do_cmdline() doesn't handle that, so convert it back.
6005 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6006 len = ksp - p;
6007 if (len > 0)
6008 {
6009 mch_memmove(q, p, len);
6010 q += len;
6011 }
6012 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6013 p = ksp + 3;
6014 continue;
6015 }
6016 }
6017
6018 /* break if there no <item> is found */
6019 if (start == NULL || end == NULL)
6020 break;
6021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 /* Include the '>' */
6023 ++end;
6024
6025 /* Take everything up to the '<' */
6026 len = start - p;
6027 if (buf == NULL)
6028 totlen += len;
6029 else
6030 {
6031 mch_memmove(q, p, len);
6032 q += len;
6033 }
6034
6035 len = uc_check_code(start, end - start, q, cmd, eap,
6036 &split_buf, &split_len);
6037 if (len == (size_t)-1)
6038 {
6039 /* no match, continue after '<' */
6040 p = start + 1;
6041 len = 1;
6042 }
6043 else
6044 p = end;
6045 if (buf == NULL)
6046 totlen += len;
6047 else
6048 q += len;
6049 }
6050 if (buf != NULL) /* second time here, finished */
6051 {
6052 STRCPY(q, p);
6053 break;
6054 }
6055
6056 totlen += STRLEN(p); /* Add on the trailing characters */
6057 buf = alloc((unsigned)(totlen + 1));
6058 if (buf == NULL)
6059 {
6060 vim_free(split_buf);
6061 return;
6062 }
6063 }
6064
6065#ifdef FEAT_EVAL
6066 current_SID = cmd->uc_scriptID;
6067#endif
6068 (void)do_cmdline(buf, eap->getline, eap->cookie,
6069 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6070#ifdef FEAT_EVAL
6071 current_SID = save_current_SID;
6072#endif
6073 vim_free(buf);
6074 vim_free(split_buf);
6075}
6076
6077# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6078 static char_u *
6079get_user_command_name(idx)
6080 int idx;
6081{
6082 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6083}
6084
6085/*
6086 * Function given to ExpandGeneric() to obtain the list of user command names.
6087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 char_u *
6089get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006090 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 int idx;
6092{
6093 if (idx < curbuf->b_ucmds.ga_len)
6094 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6095 idx -= curbuf->b_ucmds.ga_len;
6096 if (idx < ucmds.ga_len)
6097 return USER_CMD(idx)->uc_name;
6098 return NULL;
6099}
6100
6101/*
6102 * Function given to ExpandGeneric() to obtain the list of user command
6103 * attributes.
6104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 char_u *
6106get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006107 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 int idx;
6109{
6110 static char *user_cmd_flags[] =
6111 {"bang", "bar", "buffer", "complete", "count",
6112 "nargs", "range", "register"};
6113
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006114 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 return NULL;
6116 return (char_u *)user_cmd_flags[idx];
6117}
6118
6119/*
6120 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 char_u *
6123get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006124 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 int idx;
6126{
6127 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6128
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006129 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 return NULL;
6131 return (char_u *)user_cmd_nargs[idx];
6132}
6133
6134/*
6135 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 char_u *
6138get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006139 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 int idx;
6141{
6142 return (char_u *)command_complete[idx].name;
6143}
6144# endif /* FEAT_CMDL_COMPL */
6145
6146#endif /* FEAT_USR_CMDS */
6147
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006148#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6149/*
6150 * Parse a completion argument "value[vallen]".
6151 * The detected completion goes in "*complp", argument type in "*argt".
6152 * When there is an argument, for function and user defined completion, it's
6153 * copied to allocated memory and stored in "*compl_arg".
6154 * Returns FAIL if something is wrong.
6155 */
6156 int
6157parse_compl_arg(value, vallen, complp, argt, compl_arg)
6158 char_u *value;
6159 int vallen;
6160 int *complp;
6161 long *argt;
6162 char_u **compl_arg;
6163{
6164 char_u *arg = NULL;
6165 size_t arglen = 0;
6166 int i;
6167 int valend = vallen;
6168
6169 /* Look for any argument part - which is the part after any ',' */
6170 for (i = 0; i < vallen; ++i)
6171 {
6172 if (value[i] == ',')
6173 {
6174 arg = &value[i + 1];
6175 arglen = vallen - i - 1;
6176 valend = i;
6177 break;
6178 }
6179 }
6180
6181 for (i = 0; command_complete[i].expand != 0; ++i)
6182 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006183 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006184 && STRNCMP(value, command_complete[i].name, valend) == 0)
6185 {
6186 *complp = command_complete[i].expand;
6187 if (command_complete[i].expand == EXPAND_BUFFERS)
6188 *argt |= BUFNAME;
6189 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6190 || command_complete[i].expand == EXPAND_FILES)
6191 *argt |= XFILE;
6192 break;
6193 }
6194 }
6195
6196 if (command_complete[i].expand == 0)
6197 {
6198 EMSG2(_("E180: Invalid complete value: %s"), value);
6199 return FAIL;
6200 }
6201
6202# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6203 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6204 && arg != NULL)
6205# else
6206 if (arg != NULL)
6207# endif
6208 {
6209 EMSG(_("E468: Completion argument only allowed for custom completion"));
6210 return FAIL;
6211 }
6212
6213# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6214 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6215 && arg == NULL)
6216 {
6217 EMSG(_("E467: Custom completion requires a function argument"));
6218 return FAIL;
6219 }
6220
6221 if (arg != NULL)
6222 *compl_arg = vim_strnsave(arg, (int)arglen);
6223# endif
6224 return OK;
6225}
6226#endif
6227
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 static void
6229ex_colorscheme(eap)
6230 exarg_T *eap;
6231{
Bram Moolenaare6850792010-05-14 15:28:44 +02006232 if (*eap->arg == NUL)
6233 {
6234#ifdef FEAT_EVAL
6235 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6236 char_u *p = NULL;
6237
6238 if (expr != NULL)
6239 {
6240 ++emsg_off;
6241 p = eval_to_string(expr, NULL, FALSE);
6242 --emsg_off;
6243 vim_free(expr);
6244 }
6245 if (p != NULL)
6246 {
6247 MSG(p);
6248 vim_free(p);
6249 }
6250 else
6251 MSG("default");
6252#else
6253 MSG(_("unknown"));
6254#endif
6255 }
6256 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6258}
6259
6260 static void
6261ex_highlight(eap)
6262 exarg_T *eap;
6263{
6264 if (*eap->arg == NUL && eap->cmd[2] == '!')
6265 MSG(_("Greetings, Vim user!"));
6266 do_highlight(eap->arg, eap->forceit, FALSE);
6267}
6268
6269
6270/*
6271 * Call this function if we thought we were going to exit, but we won't
6272 * (because of an error). May need to restore the terminal mode.
6273 */
6274 void
6275not_exiting()
6276{
6277 exiting = FALSE;
6278 settmode(TMODE_RAW);
6279}
6280
6281/*
6282 * ":quit": quit current window, quit Vim if closed the last window.
6283 */
6284 static void
6285ex_quit(eap)
6286 exarg_T *eap;
6287{
6288#ifdef FEAT_CMDWIN
6289 if (cmdwin_type != 0)
6290 {
6291 cmdwin_result = Ctrl_C;
6292 return;
6293 }
6294#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006295 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006296 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006297 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006298 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006299 return;
6300 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006301#ifdef FEAT_AUTOCMD
6302 if (curbuf_locked())
6303 return;
6304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
6306#ifdef FEAT_NETBEANS_INTG
6307 netbeansForcedQuit = eap->forceit;
6308#endif
6309
6310 /*
6311 * If there are more files or windows we won't exit.
6312 */
6313 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6314 exiting = TRUE;
6315 if ((!P_HID(curbuf)
6316 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6317 || check_more(TRUE, eap->forceit) == FAIL
6318 || (only_one_window() && check_changed_any(eap->forceit)))
6319 {
6320 not_exiting();
6321 }
6322 else
6323 {
6324#ifdef FEAT_WINDOWS
6325 if (only_one_window()) /* quit last window */
6326#endif
6327 getout(0);
6328#ifdef FEAT_WINDOWS
6329# ifdef FEAT_GUI
6330 need_mouse_correct = TRUE;
6331# endif
6332 /* close window; may free buffer */
6333 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6334#endif
6335 }
6336}
6337
6338/*
6339 * ":cquit".
6340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 static void
6342ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006343 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 getout(1); /* this does not always pass on the exit code to the Manx
6346 compiler. why? */
6347}
6348
6349/*
6350 * ":qall": try to quit all windows
6351 */
6352 static void
6353ex_quit_all(eap)
6354 exarg_T *eap;
6355{
6356# ifdef FEAT_CMDWIN
6357 if (cmdwin_type != 0)
6358 {
6359 if (eap->forceit)
6360 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6361 else
6362 cmdwin_result = K_XF2;
6363 return;
6364 }
6365# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006366
6367 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006368 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006369 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006370 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006371 return;
6372 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006373#ifdef FEAT_AUTOCMD
6374 if (curbuf_locked())
6375 return;
6376#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006377
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 exiting = TRUE;
6379 if (eap->forceit || !check_changed_any(FALSE))
6380 getout(0);
6381 not_exiting();
6382}
6383
Bram Moolenaard9967712006-03-11 21:18:15 +00006384#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385/*
6386 * ":close": close current window, unless it is the last one
6387 */
6388 static void
6389ex_close(eap)
6390 exarg_T *eap;
6391{
6392# ifdef FEAT_CMDWIN
6393 if (cmdwin_type != 0)
6394 cmdwin_result = K_IGNORE;
6395 else
6396# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006397 if (!text_locked()
6398#ifdef FEAT_AUTOCMD
6399 && !curbuf_locked()
6400#endif
6401 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006402 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403}
6404
Bram Moolenaard9967712006-03-11 21:18:15 +00006405# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006406/*
6407 * ":pclose": Close any preview window.
6408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006410ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006412{
6413 win_T *win;
6414
6415 for (win = firstwin; win != NULL; win = win->w_next)
6416 if (win->w_p_pvw)
6417 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006418 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006419 break;
6420 }
6421}
Bram Moolenaard9967712006-03-11 21:18:15 +00006422# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006423
Bram Moolenaarf740b292006-02-16 22:11:02 +00006424/*
6425 * Close window "win" and take care of handling closing the last window for a
6426 * modified buffer.
6427 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006428 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006429ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006430 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006432 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 int need_hide;
6435 buf_T *buf = win->w_buffer;
6436
6437 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006438 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006440# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if ((p_confirm || cmdmod.confirm) && p_write)
6442 {
6443 dialog_changed(buf, FALSE);
6444 if (buf_valid(buf) && bufIsChanged(buf))
6445 return;
6446 need_hide = FALSE;
6447 }
6448 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 {
6451 EMSG(_(e_nowrtmsg));
6452 return;
6453 }
6454 }
6455
Bram Moolenaard9967712006-03-11 21:18:15 +00006456# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006458# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006459
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006461 if (tp == NULL)
6462 win_close(win, !need_hide && !P_HID(buf));
6463 else
6464 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465}
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006468 * ":tabclose": close current tab page, unless it is the last one.
6469 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 */
6471 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006472ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 exarg_T *eap;
6474{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006475 tabpage_T *tp;
6476
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477# ifdef FEAT_CMDWIN
6478 if (cmdwin_type != 0)
6479 cmdwin_result = K_IGNORE;
6480 else
6481# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006482 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006483 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006486 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006487 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006488 tp = find_tabpage((int)eap->line2);
6489 if (tp == NULL)
6490 {
6491 beep_flush();
6492 return;
6493 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006494 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006495 {
6496 tabpage_close_other(tp, eap->forceit);
6497 return;
6498 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006499 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006500 if (!text_locked()
6501#ifdef FEAT_AUTOCMD
6502 && !curbuf_locked()
6503#endif
6504 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006505 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006507}
6508
6509/*
6510 * ":tabonly": close all tab pages except the current one
6511 */
6512 static void
6513ex_tabonly(eap)
6514 exarg_T *eap;
6515{
6516 tabpage_T *tp;
6517 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006518
6519# ifdef FEAT_CMDWIN
6520 if (cmdwin_type != 0)
6521 cmdwin_result = K_IGNORE;
6522 else
6523# endif
6524 if (first_tabpage->tp_next == NULL)
6525 MSG(_("Already only one tab page"));
6526 else
6527 {
6528 /* Repeat this up to a 1000 times, because autocommands may mess
6529 * up the lists. */
6530 for (done = 0; done < 1000; ++done)
6531 {
6532 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6533 if (tp->tp_topframe != topframe)
6534 {
6535 tabpage_close_other(tp, eap->forceit);
6536 /* if we failed to close it quit */
6537 if (valid_tabpage(tp))
6538 done = 1000;
6539 /* start over, "tp" is now invalid */
6540 break;
6541 }
6542 if (first_tabpage->tp_next == NULL)
6543 break;
6544 }
6545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
6548/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006549 * Close the current tab page.
6550 */
6551 void
6552tabpage_close(forceit)
6553 int forceit;
6554{
6555 /* First close all the windows but the current one. If that worked then
6556 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006557 if (lastwin != firstwin)
6558 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006559 if (lastwin == firstwin)
6560 ex_win_close(forceit, curwin, NULL);
6561# ifdef FEAT_GUI
6562 need_mouse_correct = TRUE;
6563# endif
6564}
6565
6566/*
6567 * Close tab page "tp", which is not the current tab page.
6568 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006569 * Also takes care of the tab pages line disappearing when closing the
6570 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006571 */
6572 void
6573tabpage_close_other(tp, forceit)
6574 tabpage_T *tp;
6575 int forceit;
6576{
6577 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006578 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006579 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006580
6581 /* Limit to 1000 windows, autocommands may add a window while we close
6582 * one. OK, so I'm paranoid... */
6583 while (++done < 1000)
6584 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006585 wp = tp->tp_firstwin;
6586 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006587
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006588 /* Autocommands may delete the tab page under our fingers and we may
6589 * fail to close a window with a modified buffer. */
6590 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006591 break;
6592 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006593
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006594 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006595 if (h != tabline_height())
6596 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006597}
6598
6599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 * ":only".
6601 */
6602 static void
6603ex_only(eap)
6604 exarg_T *eap;
6605{
6606# ifdef FEAT_GUI
6607 need_mouse_correct = TRUE;
6608# endif
6609 close_others(TRUE, eap->forceit);
6610}
6611
6612/*
6613 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006614 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006616 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617ex_all(eap)
6618 exarg_T *eap;
6619{
6620 if (eap->addr_count == 0)
6621 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006622 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624#endif /* FEAT_WINDOWS */
6625
6626 static void
6627ex_hide(eap)
6628 exarg_T *eap;
6629{
6630 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6631 eap->errmsg = e_invarg;
6632 else
6633 {
6634 /* ":hide" or ":hide | cmd": hide current window */
6635 eap->nextcmd = check_nextcmd(eap->arg);
6636#ifdef FEAT_WINDOWS
6637 if (!eap->skip)
6638 {
6639# ifdef FEAT_GUI
6640 need_mouse_correct = TRUE;
6641# endif
6642 win_close(curwin, FALSE); /* don't free buffer */
6643 }
6644#endif
6645 }
6646}
6647
6648/*
6649 * ":stop" and ":suspend": Suspend Vim.
6650 */
6651 static void
6652ex_stop(eap)
6653 exarg_T *eap;
6654{
6655 /*
6656 * Disallow suspending for "rvim".
6657 */
6658 if (!check_restricted()
6659#ifdef WIN3264
6660 /*
6661 * Check if external commands are allowed now.
6662 */
6663 && can_end_termcap_mode(TRUE)
6664#endif
6665 )
6666 {
6667 if (!eap->forceit)
6668 autowrite_all();
6669 windgoto((int)Rows - 1, 0);
6670 out_char('\n');
6671 out_flush();
6672 stoptermcap();
6673 out_flush(); /* needed for SUN to restore xterm buffer */
6674#ifdef FEAT_TITLE
6675 mch_restore_title(3); /* restore window titles */
6676#endif
6677 ui_suspend(); /* call machine specific function */
6678#ifdef FEAT_TITLE
6679 maketitle();
6680 resettitle(); /* force updating the title */
6681#endif
6682 starttermcap();
6683 scroll_start(); /* scroll screen before redrawing */
6684 redraw_later_clear();
6685 shell_resized(); /* may have resized window */
6686 }
6687}
6688
6689/*
6690 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6691 */
6692 static void
6693ex_exit(eap)
6694 exarg_T *eap;
6695{
6696#ifdef FEAT_CMDWIN
6697 if (cmdwin_type != 0)
6698 {
6699 cmdwin_result = Ctrl_C;
6700 return;
6701 }
6702#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006703 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006704 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006705 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006706 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006707 return;
6708 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006709#ifdef FEAT_AUTOCMD
6710 if (curbuf_locked())
6711 return;
6712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
6714 /*
6715 * if more files or windows we won't exit
6716 */
6717 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6718 exiting = TRUE;
6719 if ( ((eap->cmdidx == CMD_wq
6720 || curbufIsChanged())
6721 && do_write(eap) == FAIL)
6722 || check_more(TRUE, eap->forceit) == FAIL
6723 || (only_one_window() && check_changed_any(eap->forceit)))
6724 {
6725 not_exiting();
6726 }
6727 else
6728 {
6729#ifdef FEAT_WINDOWS
6730 if (only_one_window()) /* quit last window, exit Vim */
6731#endif
6732 getout(0);
6733#ifdef FEAT_WINDOWS
6734# ifdef FEAT_GUI
6735 need_mouse_correct = TRUE;
6736# endif
6737 /* quit current window, may free buffer */
6738 win_close(curwin, !P_HID(curwin->w_buffer));
6739#endif
6740 }
6741}
6742
6743/*
6744 * ":print", ":list", ":number".
6745 */
6746 static void
6747ex_print(eap)
6748 exarg_T *eap;
6749{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006750 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6751 EMSG(_(e_emptybuf));
6752 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006754 for ( ;!got_int; ui_breakcheck())
6755 {
6756 print_line(eap->line1,
6757 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6758 || (eap->flags & EXFLAG_NR)),
6759 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6760 if (++eap->line1 > eap->line2)
6761 break;
6762 out_flush(); /* show one line at a time */
6763 }
6764 setpcmark();
6765 /* put cursor at last line */
6766 curwin->w_cursor.lnum = eap->line2;
6767 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 }
6769
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771}
6772
6773#ifdef FEAT_BYTEOFF
6774 static void
6775ex_goto(eap)
6776 exarg_T *eap;
6777{
6778 goto_byte(eap->line2);
6779}
6780#endif
6781
6782/*
6783 * ":shell".
6784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 static void
6786ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006787 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 do_shell(NULL, 0);
6790}
6791
6792#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6793 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006794 || defined(FEAT_GUI_MSWIN) \
6795 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 || defined(PROTO)
6797
6798/*
6799 * Handle a file drop. The code is here because a drop is *nearly* like an
6800 * :args command, but not quite (we have a list of exact filenames, so we
6801 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6802 * code is very similar to :args and hence needs access to a lot of the static
6803 * functions in this file.
6804 *
6805 * The list should be allocated using alloc(), as should each item in the
6806 * list. This function takes over responsibility for freeing the list.
6807 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006808 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6810 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6811 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6812 * file functionality is (currently) not in EMX this is not presently a
6813 * problem.
6814 */
6815 void
6816handle_drop(filec, filev, split)
6817 int filec; /* the number of files dropped */
6818 char_u **filev; /* the list of files dropped */
6819 int split; /* force splitting the window */
6820{
6821 exarg_T ea;
6822 int save_msg_scroll = msg_scroll;
6823
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006824 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006825 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006827#ifdef FEAT_AUTOCMD
6828 if (curbuf_locked())
6829 return;
6830#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006831 /* When the screen is being updated we should not change buffers and
6832 * windows structures, it may cause freed memory to be used. */
6833 if (updating_screen)
6834 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
6836 /* Check whether the current buffer is changed. If so, we will need
6837 * to split the current window or data could be lost.
6838 * We don't need to check if the 'hidden' option is set, as in this
6839 * case the buffer won't be lost.
6840 */
6841 if (!P_HID(curbuf) && !split)
6842 {
6843 ++emsg_off;
6844 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6845 --emsg_off;
6846 }
6847 if (split)
6848 {
6849# ifdef FEAT_WINDOWS
6850 if (win_split(0, 0) == FAIL)
6851 return;
6852# ifdef FEAT_SCROLLBIND
6853 curwin->w_p_scb = FALSE;
6854# endif
6855
6856 /* When splitting the window, create a new alist. Otherwise the
6857 * existing one is overwritten. */
6858 alist_unlink(curwin->w_alist);
6859 alist_new();
6860# else
6861 return; /* can't split, always fail */
6862# endif
6863 }
6864
6865 /*
6866 * Set up the new argument list.
6867 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006868 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869
6870 /*
6871 * Move to the first file.
6872 */
6873 /* Fake up a minimal "next" command for do_argfile() */
6874 vim_memset(&ea, 0, sizeof(ea));
6875 ea.cmd = (char_u *)"next";
6876 do_argfile(&ea, 0);
6877
6878 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6879 * mode that is not needed here. */
6880 need_start_insertmode = FALSE;
6881
6882 /* Restore msg_scroll, otherwise a following command may cause scrolling
6883 * unexpectedly. The screen will be redrawn by the caller, thus
6884 * msg_scroll being set by displaying a message is irrelevant. */
6885 msg_scroll = save_msg_scroll;
6886}
6887#endif
6888
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889/*
6890 * Clear an argument list: free all file names and reset it to zero entries.
6891 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006892 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893alist_clear(al)
6894 alist_T *al;
6895{
6896 while (--al->al_ga.ga_len >= 0)
6897 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6898 ga_clear(&al->al_ga);
6899}
6900
6901/*
6902 * Init an argument list.
6903 */
6904 void
6905alist_init(al)
6906 alist_T *al;
6907{
6908 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6909}
6910
6911#if defined(FEAT_WINDOWS) || defined(PROTO)
6912
6913/*
6914 * Remove a reference from an argument list.
6915 * Ignored when the argument list is the global one.
6916 * If the argument list is no longer used by any window, free it.
6917 */
6918 void
6919alist_unlink(al)
6920 alist_T *al;
6921{
6922 if (al != &global_alist && --al->al_refcount <= 0)
6923 {
6924 alist_clear(al);
6925 vim_free(al);
6926 }
6927}
6928
6929# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6930/*
6931 * Create a new argument list and use it for the current window.
6932 */
6933 void
6934alist_new()
6935{
6936 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6937 if (curwin->w_alist == NULL)
6938 {
6939 curwin->w_alist = &global_alist;
6940 ++global_alist.al_refcount;
6941 }
6942 else
6943 {
6944 curwin->w_alist->al_refcount = 1;
6945 alist_init(curwin->w_alist);
6946 }
6947}
6948# endif
6949#endif
6950
6951#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6952/*
6953 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006954 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6955 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 */
6957 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006958alist_expand(fnum_list, fnum_len)
6959 int *fnum_list;
6960 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
6962 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006963 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 char_u **new_arg_files;
6965 int new_arg_file_count;
6966 char_u *save_p_su = p_su;
6967 int i;
6968
6969 /* Don't use 'suffixes' here. This should work like the shell did the
6970 * expansion. Also, the vimrc file isn't read yet, thus the user
6971 * can't set the options. */
6972 p_su = empty_option;
6973 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6974 if (old_arg_files != NULL)
6975 {
6976 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006977 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6978 old_arg_count = GARGCOUNT;
6979 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 &new_arg_file_count, &new_arg_files,
6981 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6982 && new_arg_file_count > 0)
6983 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006984 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6985 TRUE, fnum_list, fnum_len);
6986 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 }
6989 p_su = save_p_su;
6990}
6991#endif
6992
6993/*
6994 * Set the argument list for the current window.
6995 * Takes over the allocated files[] and the allocated fnames in it.
6996 */
6997 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006998alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 alist_T *al;
7000 int count;
7001 char_u **files;
7002 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007003 int *fnum_list;
7004 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 int i;
7007
7008 alist_clear(al);
7009 if (ga_grow(&al->al_ga, count) == OK)
7010 {
7011 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007012 {
7013 if (got_int)
7014 {
7015 /* When adding many buffers this can take a long time. Allow
7016 * interrupting here. */
7017 while (i < count)
7018 vim_free(files[i++]);
7019 break;
7020 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007021
7022 /* May set buffer name of a buffer previously used for the
7023 * argument list, so that it's re-used by alist_add. */
7024 if (fnum_list != NULL && i < fnum_len)
7025 buf_set_name(fnum_list[i], files[i]);
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007028 ui_breakcheck();
7029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 vim_free(files);
7031 }
7032 else
7033 FreeWild(count, files);
7034#ifdef FEAT_WINDOWS
7035 if (al == &global_alist)
7036#endif
7037 arg_had_last = FALSE;
7038}
7039
7040/*
7041 * Add file "fname" to argument list "al".
7042 * "fname" must have been allocated and "al" must have been checked for room.
7043 */
7044 void
7045alist_add(al, fname, set_fnum)
7046 alist_T *al;
7047 char_u *fname;
7048 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7049{
7050 if (fname == NULL) /* don't add NULL file names */
7051 return;
7052#ifdef BACKSLASH_IN_FILENAME
7053 slash_adjust(fname);
7054#endif
7055 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7056 if (set_fnum > 0)
7057 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7058 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7059 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060}
7061
7062#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7063/*
7064 * Adjust slashes in file names. Called after 'shellslash' was set.
7065 */
7066 void
7067alist_slash_adjust()
7068{
7069 int i;
7070# ifdef FEAT_WINDOWS
7071 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007072 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073# endif
7074
7075 for (i = 0; i < GARGCOUNT; ++i)
7076 if (GARGLIST[i].ae_fname != NULL)
7077 slash_adjust(GARGLIST[i].ae_fname);
7078# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007079 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (wp->w_alist != &global_alist)
7081 for (i = 0; i < WARGCOUNT(wp); ++i)
7082 if (WARGLIST(wp)[i].ae_fname != NULL)
7083 slash_adjust(WARGLIST(wp)[i].ae_fname);
7084# endif
7085}
7086#endif
7087
7088/*
7089 * ":preserve".
7090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 static void
7092ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007093 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007095 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 ml_preserve(curbuf, TRUE);
7097}
7098
7099/*
7100 * ":recover".
7101 */
7102 static void
7103ex_recover(eap)
7104 exarg_T *eap;
7105{
7106 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7107 recoverymode = TRUE;
7108 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7109 && (*eap->arg == NUL
7110 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7111 ml_recover();
7112 recoverymode = FALSE;
7113}
7114
7115/*
7116 * Command modifier used in a wrong way.
7117 */
7118 static void
7119ex_wrongmodifier(eap)
7120 exarg_T *eap;
7121{
7122 eap->errmsg = e_invcmd;
7123}
7124
7125#ifdef FEAT_WINDOWS
7126/*
7127 * :sview [+command] file split window with new file, read-only
7128 * :split [[+command] file] split window with current or new file
7129 * :vsplit [[+command] file] split window vertically with current or new file
7130 * :new [[+command] file] split window with no or new file
7131 * :vnew [[+command] file] split vertically window with no or new file
7132 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007133 *
7134 * :tabedit open new Tab page with empty window
7135 * :tabedit [+command] file open new Tab page and edit "file"
7136 * :tabnew [[+command] file] just like :tabedit
7137 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 */
7139 void
7140ex_splitview(eap)
7141 exarg_T *eap;
7142{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007143 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007144# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007146# endif
7147# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007151# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7153 {
7154 ex_ni(eap);
7155 return;
7156 }
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# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007163# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007165 * quickfix windows. But it's OK when doing ":tab split". */
7166 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 {
7168 if (eap->cmdidx == CMD_split)
7169 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007170# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 if (eap->cmdidx == CMD_vsplit)
7172 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007177# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007178 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
7180 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7181 FNAME_MESS, TRUE, curbuf->b_ffname);
7182 if (fname == NULL)
7183 goto theend;
7184 eap->arg = fname;
7185 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007186# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007188# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007190# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007192# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 && eap->cmdidx != CMD_new)
7196 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007197# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007198 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007199# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007200 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007201# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007202 au_has_group((char_u *)"FileExplorer"))
7203 {
7204 /* No browsing supported but we do have the file explorer:
7205 * Edit the directory. */
7206 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7207 eap->arg = (char_u *)".";
7208 }
7209 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007210# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007211 {
7212 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007214 if (fname == NULL)
7215 goto theend;
7216 eap->arg = fname;
7217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 }
7219 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007222 /*
7223 * Either open new tab page or split the window.
7224 */
7225 if (eap->cmdidx == CMD_tabedit
7226 || eap->cmdidx == CMD_tabfind
7227 || eap->cmdidx == CMD_tabnew)
7228 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007229 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7230 : eap->addr_count == 0 ? 0
7231 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007232 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007233 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007234
7235 /* set the alternate buffer for the window we came from */
7236 if (curwin != old_curwin
7237 && win_valid(old_curwin)
7238 && old_curwin->w_buffer != curbuf
7239 && !cmdmod.keepalt)
7240 old_curwin->w_alt_fnum = curbuf->b_fnum;
7241 }
7242 }
7243 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7245 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007246# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 /* Reset 'scrollbind' when editing another file, but keep it when
7248 * doing ":split" without arguments. */
7249 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007250# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 )
7254 curwin->w_p_scb = FALSE;
7255 else
7256 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 do_exedit(eap, old_curwin);
7259 }
7260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007261# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007265# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266theend:
7267 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007270
7271/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007272 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007273 */
7274 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007275tabpage_new()
7276{
7277 exarg_T ea;
7278
7279 vim_memset(&ea, 0, sizeof(ea));
7280 ea.cmdidx = CMD_tabnew;
7281 ea.cmd = (char_u *)"tabn";
7282 ea.arg = (char_u *)"";
7283 ex_splitview(&ea);
7284}
7285
7286/*
7287 * :tabnext command
7288 */
7289 static void
7290ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007291 exarg_T *eap;
7292{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007293 switch (eap->cmdidx)
7294 {
7295 case CMD_tabfirst:
7296 case CMD_tabrewind:
7297 goto_tabpage(1);
7298 break;
7299 case CMD_tablast:
7300 goto_tabpage(9999);
7301 break;
7302 case CMD_tabprevious:
7303 case CMD_tabNext:
7304 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7305 break;
7306 default: /* CMD_tabnext */
7307 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7308 break;
7309 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007310}
7311
7312/*
7313 * :tabmove command
7314 */
7315 static void
7316ex_tabmove(eap)
7317 exarg_T *eap;
7318{
7319 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007320}
7321
7322/*
7323 * :tabs command: List tabs and their contents.
7324 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007325 static void
7326ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007327 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007328{
7329 tabpage_T *tp;
7330 win_T *wp;
7331 int tabcount = 1;
7332
7333 msg_start();
7334 msg_scroll = TRUE;
7335 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7336 {
7337 msg_putchar('\n');
7338 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7339 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7340 out_flush(); /* output one line at a time */
7341 ui_breakcheck();
7342
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007343 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007344 wp = firstwin;
7345 else
7346 wp = tp->tp_firstwin;
7347 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7348 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007349 msg_putchar('\n');
7350 msg_putchar(wp == curwin ? '>' : ' ');
7351 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007352 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7353 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007354 if (buf_spname(wp->w_buffer) != NULL)
7355 STRCPY(IObuff, buf_spname(wp->w_buffer));
7356 else
7357 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7358 IObuff, IOSIZE, TRUE);
7359 msg_outtrans(IObuff);
7360 out_flush(); /* output one line at a time */
7361 ui_breakcheck();
7362 }
7363 }
7364}
7365
7366#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
7368/*
7369 * ":mode": Set screen mode.
7370 * If no argument given, just get the screen size and redraw.
7371 */
7372 static void
7373ex_mode(eap)
7374 exarg_T *eap;
7375{
7376 if (*eap->arg == NUL)
7377 shell_resized();
7378 else
7379 mch_screenmode(eap->arg);
7380}
7381
7382#ifdef FEAT_WINDOWS
7383/*
7384 * ":resize".
7385 * set, increment or decrement current window height
7386 */
7387 static void
7388ex_resize(eap)
7389 exarg_T *eap;
7390{
7391 int n;
7392 win_T *wp = curwin;
7393
7394 if (eap->addr_count > 0)
7395 {
7396 n = eap->line2;
7397 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7398 ;
7399 }
7400
7401#ifdef FEAT_GUI
7402 need_mouse_correct = TRUE;
7403#endif
7404 n = atol((char *)eap->arg);
7405#ifdef FEAT_VERTSPLIT
7406 if (cmdmod.split & WSP_VERT)
7407 {
7408 if (*eap->arg == '-' || *eap->arg == '+')
7409 n += W_WIDTH(curwin);
7410 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7411 n = 9999;
7412 win_setwidth_win((int)n, wp);
7413 }
7414 else
7415#endif
7416 {
7417 if (*eap->arg == '-' || *eap->arg == '+')
7418 n += curwin->w_height;
7419 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7420 n = 9999;
7421 win_setheight_win((int)n, wp);
7422 }
7423}
7424#endif
7425
7426/*
7427 * ":find [+command] <file>" command.
7428 */
7429 static void
7430ex_find(eap)
7431 exarg_T *eap;
7432{
7433#ifdef FEAT_SEARCHPATH
7434 char_u *fname;
7435 int count;
7436
7437 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7438 TRUE, curbuf->b_ffname);
7439 if (eap->addr_count > 0)
7440 {
7441 /* Repeat finding the file "count" times. This matters when it
7442 * appears several times in the path. */
7443 count = eap->line2;
7444 while (fname != NULL && --count > 0)
7445 {
7446 vim_free(fname);
7447 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7448 FALSE, curbuf->b_ffname);
7449 }
7450 }
7451
7452 if (fname != NULL)
7453 {
7454 eap->arg = fname;
7455#endif
7456 do_exedit(eap, NULL);
7457#ifdef FEAT_SEARCHPATH
7458 vim_free(fname);
7459 }
7460#endif
7461}
7462
7463/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007464 * ":open" simulation: for now just work like ":visual".
7465 */
7466 static void
7467ex_open(eap)
7468 exarg_T *eap;
7469{
7470 regmatch_T regmatch;
7471 char_u *p;
7472
7473 curwin->w_cursor.lnum = eap->line2;
7474 beginline(BL_SOL | BL_FIX);
7475 if (*eap->arg == '/')
7476 {
7477 /* ":open /pattern/": put cursor in column found with pattern */
7478 ++eap->arg;
7479 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7480 *p = NUL;
7481 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7482 if (regmatch.regprog != NULL)
7483 {
7484 regmatch.rm_ic = p_ic;
7485 p = ml_get_curline();
7486 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007487 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007488 else
7489 EMSG(_(e_nomatch));
7490 vim_free(regmatch.regprog);
7491 }
7492 /* Move to the NUL, ignore any other arguments. */
7493 eap->arg += STRLEN(eap->arg);
7494 }
7495 check_cursor();
7496
7497 eap->cmdidx = CMD_visual;
7498 do_exedit(eap, NULL);
7499}
7500
7501/*
7502 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 */
7504 static void
7505ex_edit(eap)
7506 exarg_T *eap;
7507{
7508 do_exedit(eap, NULL);
7509}
7510
7511/*
7512 * ":edit <file>" command and alikes.
7513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 void
7515do_exedit(eap, old_curwin)
7516 exarg_T *eap;
7517 win_T *old_curwin; /* curwin before doing a split or NULL */
7518{
7519 int n;
7520#ifdef FEAT_WINDOWS
7521 int need_hide;
7522#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007523 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524
7525 /*
7526 * ":vi" command ends Ex mode.
7527 */
7528 if (exmode_active && (eap->cmdidx == CMD_visual
7529 || eap->cmdidx == CMD_view))
7530 {
7531 exmode_active = FALSE;
7532 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007533 {
7534 /* Special case: ":global/pat/visual\NLvi-commands" */
7535 if (global_busy)
7536 {
7537 int rd = RedrawingDisabled;
7538 int nwr = no_wait_return;
7539 int ms = msg_scroll;
7540#ifdef FEAT_GUI
7541 int he = hold_gui_events;
7542#endif
7543
7544 if (eap->nextcmd != NULL)
7545 {
7546 stuffReadbuff(eap->nextcmd);
7547 eap->nextcmd = NULL;
7548 }
7549
7550 if (exmode_was != EXMODE_VIM)
7551 settmode(TMODE_RAW);
7552 RedrawingDisabled = 0;
7553 no_wait_return = 0;
7554 need_wait_return = FALSE;
7555 msg_scroll = 0;
7556#ifdef FEAT_GUI
7557 hold_gui_events = 0;
7558#endif
7559 must_redraw = CLEAR;
7560
7561 main_loop(FALSE, TRUE);
7562
7563 RedrawingDisabled = rd;
7564 no_wait_return = nwr;
7565 msg_scroll = ms;
7566#ifdef FEAT_GUI
7567 hold_gui_events = he;
7568#endif
7569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 }
7573
7574 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007575 || eap->cmdidx == CMD_tabnew
7576 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577#ifdef FEAT_VERTSPLIT
7578 || eap->cmdidx == CMD_vnew
7579#endif
7580 ) && *eap->arg == NUL)
7581 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007582 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 setpcmark();
7584 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007585 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7586 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588 else if ((eap->cmdidx != CMD_split
7589#ifdef FEAT_VERTSPLIT
7590 && eap->cmdidx != CMD_vsplit
7591#endif
7592 )
7593 || *eap->arg != NUL
7594#ifdef FEAT_BROWSE
7595 || cmdmod.browse
7596#endif
7597 )
7598 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007599#ifdef FEAT_AUTOCMD
7600 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7601 * can bring us here, others are stopped earlier. */
7602 if (*eap->arg != NUL && curbuf_locked())
7603 return;
7604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 n = readonlymode;
7606 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7607 readonlymode = TRUE;
7608 else if (eap->cmdidx == CMD_enew)
7609 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7610 empty buffer */
7611 setpcmark();
7612 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7613 NULL, eap,
7614 /* ":edit" goes to first line if Vi compatible */
7615 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7616 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7617 ? ECMD_ONE : eap->do_ecmd_lnum,
7618 (P_HID(curbuf) ? ECMD_HIDE : 0)
7619 + (eap->forceit ? ECMD_FORCEIT : 0)
7620#ifdef FEAT_LISTCMDS
7621 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7622#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007623 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
7625 /* Editing the file failed. If the window was split, close it. */
7626#ifdef FEAT_WINDOWS
7627 if (old_curwin != NULL)
7628 {
7629 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7630 if (!need_hide || P_HID(curbuf))
7631 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007632# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7633 cleanup_T cs;
7634
7635 /* Reset the error/interrupt/exception state here so that
7636 * aborting() returns FALSE when closing a window. */
7637 enter_cleanup(&cs);
7638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639# ifdef FEAT_GUI
7640 need_mouse_correct = TRUE;
7641# endif
7642 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007643
7644# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7645 /* Restore the error/interrupt/exception state if not
7646 * discarded by a new aborting error, interrupt, or
7647 * uncaught exception. */
7648 leave_cleanup(&cs);
7649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
7651 }
7652#endif
7653 }
7654 else if (readonlymode && curbuf->b_nwindows == 1)
7655 {
7656 /* When editing an already visited buffer, 'readonly' won't be set
7657 * but the previous value is kept. With ":view" and ":sview" we
7658 * want the file to be readonly, except when another window is
7659 * editing the same buffer. */
7660 curbuf->b_p_ro = TRUE;
7661 }
7662 readonlymode = n;
7663 }
7664 else
7665 {
7666 if (eap->do_ecmd_cmd != NULL)
7667 do_cmdline_cmd(eap->do_ecmd_cmd);
7668#ifdef FEAT_TITLE
7669 n = curwin->w_arg_idx_invalid;
7670#endif
7671 check_arg_idx(curwin);
7672#ifdef FEAT_TITLE
7673 if (n != curwin->w_arg_idx_invalid)
7674 maketitle();
7675#endif
7676 }
7677
7678#ifdef FEAT_WINDOWS
7679 /*
7680 * if ":split file" worked, set alternate file name in old window to new
7681 * file
7682 */
7683 if (old_curwin != NULL
7684 && *eap->arg != NUL
7685 && curwin != old_curwin
7686 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007687 && old_curwin->w_buffer != curbuf
7688 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 old_curwin->w_alt_fnum = curbuf->b_fnum;
7690#endif
7691
7692 ex_no_reprint = TRUE;
7693}
7694
7695#ifndef FEAT_GUI
7696/*
7697 * ":gui" and ":gvim" when there is no GUI.
7698 */
7699 static void
7700ex_nogui(eap)
7701 exarg_T *eap;
7702{
7703 eap->errmsg = e_nogvim;
7704}
7705#endif
7706
7707#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7708 static void
7709ex_tearoff(eap)
7710 exarg_T *eap;
7711{
7712 gui_make_tearoff(eap->arg);
7713}
7714#endif
7715
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007716#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 static void
7718ex_popup(eap)
7719 exarg_T *eap;
7720{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007721 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722}
7723#endif
7724
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 static void
7726ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007727 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
7729 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7730 MSG(_("No swap file"));
7731 else
7732 msg(curbuf->b_ml.ml_mfp->mf_fname);
7733}
7734
7735/*
7736 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7737 * offset.
7738 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 static void
7741ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007742 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
7744#ifdef FEAT_SCROLLBIND
7745 win_T *wp;
7746 long topline;
7747 long y;
7748 linenr_T old_linenr = curwin->w_cursor.lnum;
7749
7750 setpcmark();
7751
7752 /*
7753 * determine max topline
7754 */
7755 if (curwin->w_p_scb)
7756 {
7757 topline = curwin->w_topline;
7758 for (wp = firstwin; wp; wp = wp->w_next)
7759 {
7760 if (wp->w_p_scb && wp->w_buffer)
7761 {
7762 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7763 if (topline > y)
7764 topline = y;
7765 }
7766 }
7767 if (topline < 1)
7768 topline = 1;
7769 }
7770 else
7771 {
7772 topline = 1;
7773 }
7774
7775
7776 /*
7777 * set all scrollbind windows to the same topline
7778 */
7779 wp = curwin;
7780 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7781 {
7782 if (curwin->w_p_scb)
7783 {
7784 y = topline - curwin->w_topline;
7785 if (y > 0)
7786 scrollup(y, TRUE);
7787 else
7788 scrolldown(-y, TRUE);
7789 curwin->w_scbind_pos = topline;
7790 redraw_later(VALID);
7791 cursor_correct();
7792#ifdef FEAT_WINDOWS
7793 curwin->w_redr_status = TRUE;
7794#endif
7795 }
7796 }
7797 curwin = wp;
7798 if (curwin->w_p_scb)
7799 {
7800 did_syncbind = TRUE;
7801 checkpcmark();
7802 if (old_linenr != curwin->w_cursor.lnum)
7803 {
7804 char_u ctrl_o[2];
7805
7806 ctrl_o[0] = Ctrl_O;
7807 ctrl_o[1] = 0;
7808 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7809 }
7810 }
7811#endif
7812}
7813
7814
7815 static void
7816ex_read(eap)
7817 exarg_T *eap;
7818{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007819 int i;
7820 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7821 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822
7823 if (eap->usefilter) /* :r!cmd */
7824 do_bang(1, eap, FALSE, FALSE, TRUE);
7825 else
7826 {
7827 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7828 return;
7829
7830#ifdef FEAT_BROWSE
7831 if (cmdmod.browse)
7832 {
7833 char_u *browseFile;
7834
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007835 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 NULL, NULL, NULL, curbuf);
7837 if (browseFile != NULL)
7838 {
7839 i = readfile(browseFile, NULL,
7840 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7841 vim_free(browseFile);
7842 }
7843 else
7844 i = OK;
7845 }
7846 else
7847#endif
7848 if (*eap->arg == NUL)
7849 {
7850 if (check_fname() == FAIL) /* check for no file name */
7851 return;
7852 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7853 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7854 }
7855 else
7856 {
7857 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7858 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7859 i = readfile(eap->arg, NULL,
7860 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7861
7862 }
7863 if (i == FAIL)
7864 {
7865#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7866 if (!aborting())
7867#endif
7868 EMSG2(_(e_notopen), eap->arg);
7869 }
7870 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007871 {
7872 if (empty && exmode_active)
7873 {
7874 /* Delete the empty line that remains. Historically ex does
7875 * this but vi doesn't. */
7876 if (eap->line2 == 0)
7877 lnum = curbuf->b_ml.ml_line_count;
7878 else
7879 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007880 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007881 {
7882 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007883 if (curwin->w_cursor.lnum > 1
7884 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007885 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007886 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007887 }
7888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892}
7893
Bram Moolenaarea408852005-06-25 22:49:46 +00007894static char_u *prev_dir = NULL;
7895
7896#if defined(EXITFREE) || defined(PROTO)
7897 void
7898free_cd_dir()
7899{
7900 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007901 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007902
7903 vim_free(globaldir);
7904 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007905}
7906#endif
7907
7908
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909/*
7910 * ":cd", ":lcd", ":chdir" and ":lchdir".
7911 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007912 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913ex_cd(eap)
7914 exarg_T *eap;
7915{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 char_u *new_dir;
7917 char_u *tofree;
7918
7919 new_dir = eap->arg;
7920#if !defined(UNIX) && !defined(VMS)
7921 /* for non-UNIX ":cd" means: print current directory */
7922 if (*new_dir == NUL)
7923 ex_pwd(NULL);
7924 else
7925#endif
7926 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007927#ifdef FEAT_AUTOCMD
7928 if (allbuf_locked())
7929 return;
7930#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007931 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7932 && !eap->forceit)
7933 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007934 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007935 return;
7936 }
7937
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 /* ":cd -": Change to previous directory */
7939 if (STRCMP(new_dir, "-") == 0)
7940 {
7941 if (prev_dir == NULL)
7942 {
7943 EMSG(_("E186: No previous directory"));
7944 return;
7945 }
7946 new_dir = prev_dir;
7947 }
7948
7949 /* Save current directory for next ":cd -" */
7950 tofree = prev_dir;
7951 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7952 prev_dir = vim_strsave(NameBuff);
7953 else
7954 prev_dir = NULL;
7955
7956#if defined(UNIX) || defined(VMS)
7957 /* for UNIX ":cd" means: go to home directory */
7958 if (*new_dir == NUL)
7959 {
7960 /* use NameBuff for home directory name */
7961# ifdef VMS
7962 char_u *p;
7963
7964 p = mch_getenv((char_u *)"SYS$LOGIN");
7965 if (p == NULL || *p == NUL) /* empty is the same as not set */
7966 NameBuff[0] = NUL;
7967 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007968 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969# else
7970 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7971# endif
7972 new_dir = NameBuff;
7973 }
7974#endif
7975 if (new_dir == NULL || vim_chdir(new_dir))
7976 EMSG(_(e_failed));
7977 else
7978 {
7979 vim_free(curwin->w_localdir);
7980 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7981 {
7982 /* If still in global directory, need to remember current
7983 * directory as global directory. */
7984 if (globaldir == NULL && prev_dir != NULL)
7985 globaldir = vim_strsave(prev_dir);
7986 /* Remember this local directory for the window. */
7987 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7988 curwin->w_localdir = vim_strsave(NameBuff);
7989 }
7990 else
7991 {
7992 /* We are now in the global directory, no need to remember its
7993 * name. */
7994 vim_free(globaldir);
7995 globaldir = NULL;
7996 curwin->w_localdir = NULL;
7997 }
7998
7999 shorten_fnames(TRUE);
8000
8001 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008002 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 ex_pwd(eap);
8004 }
8005 vim_free(tofree);
8006 }
8007}
8008
8009/*
8010 * ":pwd".
8011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 static void
8013ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008014 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
8016 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8017 {
8018#ifdef BACKSLASH_IN_FILENAME
8019 slash_adjust(NameBuff);
8020#endif
8021 msg(NameBuff);
8022 }
8023 else
8024 EMSG(_("E187: Unknown"));
8025}
8026
8027/*
8028 * ":=".
8029 */
8030 static void
8031ex_equal(eap)
8032 exarg_T *eap;
8033{
8034 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008035 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036}
8037
8038 static void
8039ex_sleep(eap)
8040 exarg_T *eap;
8041{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008042 int n;
8043 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
8045 if (cursor_valid())
8046 {
8047 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8048 if (n >= 0)
8049 windgoto((int)n, curwin->w_wcol);
8050 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008051
8052 len = eap->line2;
8053 switch (*eap->arg)
8054 {
8055 case 'm': break;
8056 case NUL: len *= 1000L; break;
8057 default: EMSG2(_(e_invarg2), eap->arg); return;
8058 }
8059 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
8062/*
8063 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8064 */
8065 void
8066do_sleep(msec)
8067 long msec;
8068{
8069 long done;
8070
8071 cursor_on();
8072 out_flush();
8073 for (done = 0; !got_int && done < msec; done += 1000L)
8074 {
8075 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8076 ui_breakcheck();
8077 }
8078}
8079
8080 static void
8081do_exmap(eap, isabbrev)
8082 exarg_T *eap;
8083 int isabbrev;
8084{
8085 int mode;
8086 char_u *cmdp;
8087
8088 cmdp = eap->cmd;
8089 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8090
8091 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8092 eap->arg, mode, isabbrev))
8093 {
8094 case 1: EMSG(_(e_invarg));
8095 break;
8096 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8097 break;
8098 }
8099}
8100
8101/*
8102 * ":winsize" command (obsolete).
8103 */
8104 static void
8105ex_winsize(eap)
8106 exarg_T *eap;
8107{
8108 int w, h;
8109 char_u *arg = eap->arg;
8110 char_u *p;
8111
8112 w = getdigits(&arg);
8113 arg = skipwhite(arg);
8114 p = arg;
8115 h = getdigits(&arg);
8116 if (*p != NUL && *arg == NUL)
8117 set_shellsize(w, h, TRUE);
8118 else
8119 EMSG(_("E465: :winsize requires two number arguments"));
8120}
8121
8122#ifdef FEAT_WINDOWS
8123 static void
8124ex_wincmd(eap)
8125 exarg_T *eap;
8126{
8127 int xchar = NUL;
8128 char_u *p;
8129
8130 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8131 {
8132 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8133 if (eap->arg[1] == NUL)
8134 {
8135 EMSG(_(e_invarg));
8136 return;
8137 }
8138 xchar = eap->arg[1];
8139 p = eap->arg + 2;
8140 }
8141 else
8142 p = eap->arg + 1;
8143
8144 eap->nextcmd = check_nextcmd(p);
8145 p = skipwhite(p);
8146 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8147 EMSG(_(e_invarg));
8148 else
8149 {
8150 /* Pass flags on for ":vertical wincmd ]". */
8151 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008152 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8154 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008155 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 }
8157}
8158#endif
8159
Bram Moolenaar843ee412004-06-30 16:16:41 +00008160#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161/*
8162 * ":winpos".
8163 */
8164 static void
8165ex_winpos(eap)
8166 exarg_T *eap;
8167{
8168 int x, y;
8169 char_u *arg = eap->arg;
8170 char_u *p;
8171
8172 if (*arg == NUL)
8173 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008174# if defined(FEAT_GUI) || defined(MSWIN)
8175# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008177# else
8178 if (mch_get_winpos(&x, &y) != FAIL)
8179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {
8181 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8182 msg(IObuff);
8183 }
8184 else
8185# endif
8186 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8187 }
8188 else
8189 {
8190 x = getdigits(&arg);
8191 arg = skipwhite(arg);
8192 p = arg;
8193 y = getdigits(&arg);
8194 if (*p == NUL || *arg != NUL)
8195 {
8196 EMSG(_("E466: :winpos requires two number arguments"));
8197 return;
8198 }
8199# ifdef FEAT_GUI
8200 if (gui.in_use)
8201 gui_mch_set_winpos(x, y);
8202 else if (gui.starting)
8203 {
8204 /* Remember the coordinates for when the window is opened. */
8205 gui_win_x = x;
8206 gui_win_y = y;
8207 }
8208# ifdef HAVE_TGETENT
8209 else
8210# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008211# else
8212# ifdef MSWIN
8213 mch_set_winpos(x, y);
8214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215# endif
8216# ifdef HAVE_TGETENT
8217 if (*T_CWP)
8218 term_set_winpos(x, y);
8219# endif
8220 }
8221}
8222#endif
8223
8224/*
8225 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8226 */
8227 static void
8228ex_operators(eap)
8229 exarg_T *eap;
8230{
8231 oparg_T oa;
8232
8233 clear_oparg(&oa);
8234 oa.regname = eap->regname;
8235 oa.start.lnum = eap->line1;
8236 oa.end.lnum = eap->line2;
8237 oa.line_count = eap->line2 - eap->line1 + 1;
8238 oa.motion_type = MLINE;
8239#ifdef FEAT_VIRTUALEDIT
8240 virtual_op = FALSE;
8241#endif
8242 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8243 {
8244 setpcmark();
8245 curwin->w_cursor.lnum = eap->line1;
8246 beginline(BL_SOL | BL_FIX);
8247 }
8248
8249 switch (eap->cmdidx)
8250 {
8251 case CMD_delete:
8252 oa.op_type = OP_DELETE;
8253 op_delete(&oa);
8254 break;
8255
8256 case CMD_yank:
8257 oa.op_type = OP_YANK;
8258 (void)op_yank(&oa, FALSE, TRUE);
8259 break;
8260
8261 default: /* CMD_rshift or CMD_lshift */
8262 if ((eap->cmdidx == CMD_rshift)
8263#ifdef FEAT_RIGHTLEFT
8264 ^ curwin->w_p_rl
8265#endif
8266 )
8267 oa.op_type = OP_RSHIFT;
8268 else
8269 oa.op_type = OP_LSHIFT;
8270 op_shift(&oa, FALSE, eap->amount);
8271 break;
8272 }
8273#ifdef FEAT_VIRTUALEDIT
8274 virtual_op = MAYBE;
8275#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008276 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277}
8278
8279/*
8280 * ":put".
8281 */
8282 static void
8283ex_put(eap)
8284 exarg_T *eap;
8285{
8286 /* ":0put" works like ":1put!". */
8287 if (eap->line2 == 0)
8288 {
8289 eap->line2 = 1;
8290 eap->forceit = TRUE;
8291 }
8292 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008293 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8294 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295}
8296
8297/*
8298 * Handle ":copy" and ":move".
8299 */
8300 static void
8301ex_copymove(eap)
8302 exarg_T *eap;
8303{
8304 long n;
8305
8306 n = get_address(&eap->arg, FALSE, FALSE);
8307 if (eap->arg == NULL) /* error detected */
8308 {
8309 eap->nextcmd = NULL;
8310 return;
8311 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008312 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313
8314 /*
8315 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8316 */
8317 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8318 {
8319 EMSG(_(e_invaddr));
8320 return;
8321 }
8322
8323 if (eap->cmdidx == CMD_move)
8324 {
8325 if (do_move(eap->line1, eap->line2, n) == FAIL)
8326 return;
8327 }
8328 else
8329 ex_copy(eap->line1, eap->line2, n);
8330 u_clearline();
8331 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008332 ex_may_print(eap);
8333}
8334
8335/*
8336 * Print the current line if flags were given to the Ex command.
8337 */
8338 static void
8339ex_may_print(eap)
8340 exarg_T *eap;
8341{
8342 if (eap->flags != 0)
8343 {
8344 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8345 (eap->flags & EXFLAG_LIST));
8346 ex_no_reprint = TRUE;
8347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348}
8349
8350/*
8351 * ":smagic" and ":snomagic".
8352 */
8353 static void
8354ex_submagic(eap)
8355 exarg_T *eap;
8356{
8357 int magic_save = p_magic;
8358
8359 p_magic = (eap->cmdidx == CMD_smagic);
8360 do_sub(eap);
8361 p_magic = magic_save;
8362}
8363
8364/*
8365 * ":join".
8366 */
8367 static void
8368ex_join(eap)
8369 exarg_T *eap;
8370{
8371 curwin->w_cursor.lnum = eap->line1;
8372 if (eap->line1 == eap->line2)
8373 {
8374 if (eap->addr_count >= 2) /* :2,2join does nothing */
8375 return;
8376 if (eap->line2 == curbuf->b_ml.ml_line_count)
8377 {
8378 beep_flush();
8379 return;
8380 }
8381 ++eap->line2;
8382 }
8383 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8384 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008385 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386}
8387
8388/*
8389 * ":[addr]@r" or ":[addr]*r": execute register
8390 */
8391 static void
8392ex_at(eap)
8393 exarg_T *eap;
8394{
8395 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008396 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
8398 curwin->w_cursor.lnum = eap->line2;
8399
8400#ifdef USE_ON_FLY_SCROLL
8401 dont_scroll = TRUE; /* disallow scrolling here */
8402#endif
8403
8404 /* get the register name. No name means to use the previous one */
8405 c = *eap->arg;
8406 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8407 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008408 /* Put the register in the typeahead buffer with the "silent" flag. */
8409 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8410 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 else
8415 {
8416 int save_efr = exec_from_reg;
8417
8418 exec_from_reg = TRUE;
8419
8420 /*
8421 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008422 * Continue until the stuff buffer is empty and all added characters
8423 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008425 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8427
8428 exec_from_reg = save_efr;
8429 }
8430}
8431
8432/*
8433 * ":!".
8434 */
8435 static void
8436ex_bang(eap)
8437 exarg_T *eap;
8438{
8439 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8440}
8441
8442/*
8443 * ":undo".
8444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 static void
8446ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008447 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008449 if (eap->addr_count == 1) /* :undo 123 */
8450 undo_time(eap->line2, FALSE, TRUE);
8451 else
8452 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453}
8454
8455/*
8456 * ":redo".
8457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 static void
8459ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008460 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
8462 u_redo(1);
8463}
8464
8465/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008466 * ":earlier" and ":later".
8467 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008468 static void
8469ex_later(eap)
8470 exarg_T *eap;
8471{
8472 long count = 0;
8473 int sec = FALSE;
8474 char_u *p = eap->arg;
8475
8476 if (*p == NUL)
8477 count = 1;
8478 else if (isdigit(*p))
8479 {
8480 count = getdigits(&p);
8481 switch (*p)
8482 {
8483 case 's': ++p; sec = TRUE; break;
8484 case 'm': ++p; sec = TRUE; count *= 60; break;
8485 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
8486 }
8487 }
8488
8489 if (*p != NUL)
8490 EMSG2(_(e_invarg2), eap->arg);
8491 else
Bram Moolenaard3667a22006-03-16 21:35:52 +00008492 undo_time(eap->cmdidx == CMD_earlier ? -count : count, sec, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008493}
8494
8495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 * ":redir": start/stop redirection.
8497 */
8498 static void
8499ex_redir(eap)
8500 exarg_T *eap;
8501{
8502 char *mode;
8503 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008504 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505
8506 if (STRICMP(eap->arg, "END") == 0)
8507 close_redir();
8508 else
8509 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008510 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008512 ++arg;
8513 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008515 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 mode = "a";
8517 }
8518 else
8519 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008520 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521
8522 close_redir();
8523
8524 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008525 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (fname == NULL)
8527 return;
8528#ifdef FEAT_BROWSE
8529 if (cmdmod.browse)
8530 {
8531 char_u *browseFile;
8532
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008533 browseFile = do_browse(BROWSE_SAVE,
8534 (char_u *)_("Save Redirection"),
8535 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 if (browseFile == NULL)
8537 return; /* operation cancelled */
8538 vim_free(fname);
8539 fname = browseFile;
8540 eap->forceit = TRUE; /* since dialog already asked */
8541 }
8542#endif
8543
8544 redir_fd = open_exfile(fname, eap->forceit, mode);
8545 vim_free(fname);
8546 }
8547#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008548 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 {
8550 /* redirect to a register a-z (resp. A-Z for appending) */
8551 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008552 ++arg;
8553 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008555 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008556 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008558 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008560 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008561 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008562 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008565 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008566 if (*arg == '>')
8567 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008568 /* Make register empty when not using @A-@Z and the
8569 * command is valid. */
8570 if (*arg == NUL && !isupper(redir_reg))
8571 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008573 }
8574 if (*arg != NUL)
8575 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008576 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008577 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008578 }
8579 }
8580 else if (*arg == '=' && arg[1] == '>')
8581 {
8582 int append;
8583
8584 /* redirect to a variable */
8585 close_redir();
8586 arg += 2;
8587
8588 if (*arg == '>')
8589 {
8590 ++arg;
8591 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 }
8593 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008594 append = FALSE;
8595
8596 if (var_redir_start(skipwhite(arg), append) == OK)
8597 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 }
8599#endif
8600
8601 /* TODO: redirect to a buffer */
8602
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008604 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008606
8607 /* Make sure redirection is not off. Can happen for cmdline completion
8608 * that indirectly invokes a command to catch its output. */
8609 if (redir_fd != NULL
8610#ifdef FEAT_EVAL
8611 || redir_reg || redir_vname
8612#endif
8613 )
8614 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615}
8616
8617/*
8618 * ":redraw": force redraw
8619 */
8620 static void
8621ex_redraw(eap)
8622 exarg_T *eap;
8623{
8624 int r = RedrawingDisabled;
8625 int p = p_lz;
8626
8627 RedrawingDisabled = 0;
8628 p_lz = FALSE;
8629 update_topline();
8630 update_screen(eap->forceit ? CLEAR :
8631#ifdef FEAT_VISUAL
8632 VIsual_active ? INVERTED :
8633#endif
8634 0);
8635#ifdef FEAT_TITLE
8636 if (need_maketitle)
8637 maketitle();
8638#endif
8639 RedrawingDisabled = r;
8640 p_lz = p;
8641
8642 /* Reset msg_didout, so that a message that's there is overwritten. */
8643 msg_didout = FALSE;
8644 msg_col = 0;
8645
8646 out_flush();
8647}
8648
8649/*
8650 * ":redrawstatus": force redraw of status line(s)
8651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 static void
8653ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008654 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656#if defined(FEAT_WINDOWS)
8657 int r = RedrawingDisabled;
8658 int p = p_lz;
8659
8660 RedrawingDisabled = 0;
8661 p_lz = FALSE;
8662 if (eap->forceit)
8663 status_redraw_all();
8664 else
8665 status_redraw_curbuf();
8666 update_screen(
8667# ifdef FEAT_VISUAL
8668 VIsual_active ? INVERTED :
8669# endif
8670 0);
8671 RedrawingDisabled = r;
8672 p_lz = p;
8673 out_flush();
8674#endif
8675}
8676
8677 static void
8678close_redir()
8679{
8680 if (redir_fd != NULL)
8681 {
8682 fclose(redir_fd);
8683 redir_fd = NULL;
8684 }
8685#ifdef FEAT_EVAL
8686 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008687 if (redir_vname)
8688 {
8689 var_redir_stop();
8690 redir_vname = 0;
8691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692#endif
8693}
8694
8695#if defined(FEAT_SESSION) && defined(USE_CRNL)
8696# define MKSESSION_NL
8697static int mksession_nl = FALSE; /* use NL only in put_eol() */
8698#endif
8699
8700/*
8701 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8702 */
8703 static void
8704ex_mkrc(eap)
8705 exarg_T *eap;
8706{
8707 FILE *fd;
8708 int failed = FALSE;
8709 char_u *fname;
8710#ifdef FEAT_BROWSE
8711 char_u *browseFile = NULL;
8712#endif
8713#ifdef FEAT_SESSION
8714 int view_session = FALSE;
8715 int using_vdir = FALSE; /* using 'viewdir'? */
8716 char_u *viewFile = NULL;
8717 unsigned *flagp;
8718#endif
8719
8720 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8721 {
8722#ifdef FEAT_SESSION
8723 view_session = TRUE;
8724#else
8725 ex_ni(eap);
8726 return;
8727#endif
8728 }
8729
8730#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008731 /* Use the short file name until ":lcd" is used. We also don't use the
8732 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008733 did_lcd = FALSE;
8734
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8736 if (eap->cmdidx == CMD_mkview
8737 && (*eap->arg == NUL
8738 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8739 {
8740 eap->forceit = TRUE;
8741 fname = get_view_file(*eap->arg);
8742 if (fname == NULL)
8743 return;
8744 viewFile = fname;
8745 using_vdir = TRUE;
8746 }
8747 else
8748#endif
8749 if (*eap->arg != NUL)
8750 fname = eap->arg;
8751 else if (eap->cmdidx == CMD_mkvimrc)
8752 fname = (char_u *)VIMRC_FILE;
8753#ifdef FEAT_SESSION
8754 else if (eap->cmdidx == CMD_mksession)
8755 fname = (char_u *)SESSION_FILE;
8756#endif
8757 else
8758 fname = (char_u *)EXRC_FILE;
8759
8760#ifdef FEAT_BROWSE
8761 if (cmdmod.browse)
8762 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008763 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764# ifdef FEAT_SESSION
8765 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8766 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8767# endif
8768 (char_u *)_("Save Setup"),
8769 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8770 if (browseFile == NULL)
8771 goto theend;
8772 fname = browseFile;
8773 eap->forceit = TRUE; /* since dialog already asked */
8774 }
8775#endif
8776
8777#if defined(FEAT_SESSION) && defined(vim_mkdir)
8778 /* When using 'viewdir' may have to create the directory. */
8779 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008780 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781#endif
8782
8783 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8784 if (fd != NULL)
8785 {
8786#ifdef FEAT_SESSION
8787 if (eap->cmdidx == CMD_mkview)
8788 flagp = &vop_flags;
8789 else
8790 flagp = &ssop_flags;
8791#endif
8792
8793#ifdef MKSESSION_NL
8794 /* "unix" in 'sessionoptions': use NL line separator */
8795 if (view_session && (*flagp & SSOP_UNIX))
8796 mksession_nl = TRUE;
8797#endif
8798
8799 /* Write the version command for :mkvimrc */
8800 if (eap->cmdidx == CMD_mkvimrc)
8801 (void)put_line(fd, "version 6.0");
8802
8803#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008804 if (eap->cmdidx == CMD_mksession)
8805 {
8806 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8807 failed = TRUE;
8808 }
8809
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 if (eap->cmdidx != CMD_mkview)
8811#endif
8812 {
8813 /* Write setting 'compatible' first, because it has side effects.
8814 * For that same reason only do it when needed. */
8815 if (p_cp)
8816 (void)put_line(fd, "if !&cp | set cp | endif");
8817 else
8818 (void)put_line(fd, "if &cp | set nocp | endif");
8819 }
8820
8821#ifdef FEAT_SESSION
8822 if (!view_session
8823 || (eap->cmdidx == CMD_mksession
8824 && (*flagp & SSOP_OPTIONS)))
8825#endif
8826 failed |= (makemap(fd, NULL) == FAIL
8827 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8828
8829#ifdef FEAT_SESSION
8830 if (!failed && view_session)
8831 {
8832 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8833 failed = TRUE;
8834 if (eap->cmdidx == CMD_mksession)
8835 {
8836 char_u dirnow[MAXPATHL]; /* current directory */
8837
8838 /*
8839 * Change to session file's dir.
8840 */
8841 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8842 || mch_chdir((char *)dirnow) != 0)
8843 *dirnow = NUL;
8844 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8845 {
8846 if (vim_chdirfile(fname) == OK)
8847 shorten_fnames(TRUE);
8848 }
8849 else if (*dirnow != NUL
8850 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8851 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008852 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008853 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 }
8855
8856 failed |= (makeopens(fd, dirnow) == FAIL);
8857
8858 /* restore original dir */
8859 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8860 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8861 {
8862 if (mch_chdir((char *)dirnow) != 0)
8863 EMSG(_(e_prev_dir));
8864 shorten_fnames(TRUE);
8865 }
8866 }
8867 else
8868 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008869 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8870 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8873 == FAIL)
8874 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008875 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8876 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008877 if (eap->cmdidx == CMD_mksession)
8878 {
8879 if (put_line(fd, "unlet SessionLoad") == FAIL)
8880 failed = TRUE;
8881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 }
8883#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008884 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8885 failed = TRUE;
8886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 failed |= fclose(fd);
8888
8889 if (failed)
8890 EMSG(_(e_write));
8891#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8892 else if (eap->cmdidx == CMD_mksession)
8893 {
8894 /* successful session write - set this_session var */
8895 char_u tbuf[MAXPATHL];
8896
8897 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8898 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8899 }
8900#endif
8901#ifdef MKSESSION_NL
8902 mksession_nl = FALSE;
8903#endif
8904 }
8905
8906#ifdef FEAT_BROWSE
8907theend:
8908 vim_free(browseFile);
8909#endif
8910#ifdef FEAT_SESSION
8911 vim_free(viewFile);
8912#endif
8913}
8914
Bram Moolenaardf177f62005-02-22 08:39:57 +00008915#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8916 || defined(PROTO)
8917 int
8918vim_mkdir_emsg(name, prot)
8919 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00008920 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008921{
8922 if (vim_mkdir(name, prot) != 0)
8923 {
8924 EMSG2(_("E739: Cannot create directory: %s"), name);
8925 return FAIL;
8926 }
8927 return OK;
8928}
8929#endif
8930
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931/*
8932 * Open a file for writing for an Ex command, with some checks.
8933 * Return file descriptor, or NULL on failure.
8934 */
8935 FILE *
8936open_exfile(fname, forceit, mode)
8937 char_u *fname;
8938 int forceit;
8939 char *mode; /* "w" for create new file or "a" for append */
8940{
8941 FILE *fd;
8942
8943#ifdef UNIX
8944 /* with Unix it is possible to open a directory */
8945 if (mch_isdir(fname))
8946 {
8947 EMSG2(_(e_isadir2), fname);
8948 return NULL;
8949 }
8950#endif
8951 if (!forceit && *mode != 'a' && vim_fexists(fname))
8952 {
8953 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8954 return NULL;
8955 }
8956
8957 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8958 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8959
8960 return fd;
8961}
8962
8963/*
8964 * ":mark" and ":k".
8965 */
8966 static void
8967ex_mark(eap)
8968 exarg_T *eap;
8969{
8970 pos_T pos;
8971
8972 if (*eap->arg == NUL) /* No argument? */
8973 EMSG(_(e_argreq));
8974 else if (eap->arg[1] != NUL) /* more than one character? */
8975 EMSG(_(e_trailing));
8976 else
8977 {
8978 pos = curwin->w_cursor; /* save curwin->w_cursor */
8979 curwin->w_cursor.lnum = eap->line2;
8980 beginline(BL_WHITE | BL_FIX);
8981 if (setmark(*eap->arg) == FAIL) /* set mark */
8982 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8983 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8984 }
8985}
8986
8987/*
8988 * Update w_topline, w_leftcol and the cursor position.
8989 */
8990 void
8991update_topline_cursor()
8992{
8993 check_cursor(); /* put cursor on valid line */
8994 update_topline();
8995 if (!curwin->w_p_wrap)
8996 validate_cursor();
8997 update_curswant();
8998}
8999
9000#ifdef FEAT_EX_EXTRA
9001/*
9002 * ":normal[!] {commands}": Execute normal mode commands.
9003 */
9004 static void
9005ex_normal(eap)
9006 exarg_T *eap;
9007{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 int save_msg_scroll = msg_scroll;
9009 int save_restart_edit = restart_edit;
9010 int save_msg_didout = msg_didout;
9011 int save_State = State;
9012 tasave_T tabuf;
9013 int save_insertmode = p_im;
9014 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009015 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016#ifdef FEAT_MBYTE
9017 char_u *arg = NULL;
9018 int l;
9019 char_u *p;
9020#endif
9021
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009022 if (ex_normal_lock > 0)
9023 {
9024 EMSG(_(e_secure));
9025 return;
9026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 if (ex_normal_busy >= p_mmd)
9028 {
9029 EMSG(_("E192: Recursive use of :normal too deep"));
9030 return;
9031 }
9032 ++ex_normal_busy;
9033
9034 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9035 restart_edit = 0; /* don't go to Insert mode */
9036 p_im = FALSE; /* don't use 'insertmode' */
9037
9038#ifdef FEAT_MBYTE
9039 /*
9040 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9041 * this for the K_SPECIAL leading byte, otherwise special keys will not
9042 * work.
9043 */
9044 if (has_mbyte)
9045 {
9046 int len = 0;
9047
9048 /* Count the number of characters to be escaped. */
9049 for (p = eap->arg; *p != NUL; ++p)
9050 {
9051# ifdef FEAT_GUI
9052 if (*p == CSI) /* leadbyte CSI */
9053 len += 2;
9054# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009055 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9057# ifdef FEAT_GUI
9058 || *p == CSI
9059# endif
9060 )
9061 len += 2;
9062 }
9063 if (len > 0)
9064 {
9065 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9066 if (arg != NULL)
9067 {
9068 len = 0;
9069 for (p = eap->arg; *p != NUL; ++p)
9070 {
9071 arg[len++] = *p;
9072# ifdef FEAT_GUI
9073 if (*p == CSI)
9074 {
9075 arg[len++] = KS_EXTRA;
9076 arg[len++] = (int)KE_CSI;
9077 }
9078# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009079 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 {
9081 arg[len++] = *++p;
9082 if (*p == K_SPECIAL)
9083 {
9084 arg[len++] = KS_SPECIAL;
9085 arg[len++] = KE_FILLER;
9086 }
9087# ifdef FEAT_GUI
9088 else if (*p == CSI)
9089 {
9090 arg[len++] = KS_EXTRA;
9091 arg[len++] = (int)KE_CSI;
9092 }
9093# endif
9094 }
9095 arg[len] = NUL;
9096 }
9097 }
9098 }
9099 }
9100#endif
9101
9102 /*
9103 * Save the current typeahead. This is required to allow using ":normal"
9104 * from an event handler and makes sure we don't hang when the argument
9105 * ends with half a command.
9106 */
9107 save_typeahead(&tabuf);
9108 if (tabuf.typebuf_valid)
9109 {
9110 /*
9111 * Repeat the :normal command for each line in the range. When no
9112 * range given, execute it just once, without positioning the cursor
9113 * first.
9114 */
9115 do
9116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 if (eap->addr_count != 0)
9118 {
9119 curwin->w_cursor.lnum = eap->line1++;
9120 curwin->w_cursor.col = 0;
9121 }
9122
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009123 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124#ifdef FEAT_MBYTE
9125 arg != NULL ? arg :
9126#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009127 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 }
9129 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9130 }
9131
9132 /* Might not return to the main loop when in an event handler. */
9133 update_topline_cursor();
9134
9135 /* Restore the previous typeahead. */
9136 restore_typeahead(&tabuf);
9137
9138 --ex_normal_busy;
9139 msg_scroll = save_msg_scroll;
9140 restart_edit = save_restart_edit;
9141 p_im = save_insertmode;
9142 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009143 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9145
9146 /* Restore the state (needed when called from a function executed for
9147 * 'indentexpr'). */
9148 State = save_State;
9149#ifdef FEAT_MBYTE
9150 vim_free(arg);
9151#endif
9152}
9153
9154/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009155 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 */
9157 static void
9158ex_startinsert(eap)
9159 exarg_T *eap;
9160{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009161 if (eap->forceit)
9162 {
9163 coladvance((colnr_T)MAXCOL);
9164 curwin->w_curswant = MAXCOL;
9165 curwin->w_set_curswant = FALSE;
9166 }
9167
Bram Moolenaara40c5002005-01-09 21:16:21 +00009168 /* Ignore the command when already in Insert mode. Inserting an
9169 * expression register that invokes a function can do this. */
9170 if (State & INSERT)
9171 return;
9172
Bram Moolenaar64969662005-12-14 21:59:55 +00009173 if (eap->cmdidx == CMD_startinsert)
9174 restart_edit = 'a';
9175 else if (eap->cmdidx == CMD_startreplace)
9176 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009178 restart_edit = 'V';
9179
9180 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009182 if (eap->cmdidx == CMD_startinsert)
9183 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 curwin->w_curswant = 0; /* avoid MAXCOL */
9185 }
9186}
9187
9188/*
9189 * ":stopinsert"
9190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 static void
9192ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009193 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
9195 restart_edit = 0;
9196 stop_insert_mode = TRUE;
9197}
9198#endif
9199
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009200#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9201/*
9202 * Execute normal mode command "cmd".
9203 * "remap" can be REMAP_NONE or REMAP_YES.
9204 */
9205 void
9206exec_normal_cmd(cmd, remap, silent)
9207 char_u *cmd;
9208 int remap;
9209 int silent;
9210{
9211 oparg_T oa;
9212
9213 /*
9214 * Stuff the argument into the typeahead buffer.
9215 * Execute normal_cmd() until there is no typeahead left.
9216 */
9217 clear_oparg(&oa);
9218 finish_op = FALSE;
9219 ins_typebuf(cmd, remap, 0, TRUE, silent);
9220 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9221 && !got_int)
9222 {
9223 update_topline_cursor();
9224 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9225 }
9226}
9227#endif
9228
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229#ifdef FEAT_FIND_ID
9230 static void
9231ex_checkpath(eap)
9232 exarg_T *eap;
9233{
9234 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9235 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9236 (linenr_T)1, (linenr_T)MAXLNUM);
9237}
9238
9239#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9240/*
9241 * ":psearch"
9242 */
9243 static void
9244ex_psearch(eap)
9245 exarg_T *eap;
9246{
9247 g_do_tagpreview = p_pvh;
9248 ex_findpat(eap);
9249 g_do_tagpreview = 0;
9250}
9251#endif
9252
9253 static void
9254ex_findpat(eap)
9255 exarg_T *eap;
9256{
9257 int whole = TRUE;
9258 long n;
9259 char_u *p;
9260 int action;
9261
9262 switch (cmdnames[eap->cmdidx].cmd_name[2])
9263 {
9264 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9265 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9266 action = ACTION_GOTO;
9267 else
9268 action = ACTION_SHOW;
9269 break;
9270 case 'i': /* ":ilist" and ":dlist" */
9271 action = ACTION_SHOW_ALL;
9272 break;
9273 case 'u': /* ":ijump" and ":djump" */
9274 action = ACTION_GOTO;
9275 break;
9276 default: /* ":isplit" and ":dsplit" */
9277 action = ACTION_SPLIT;
9278 break;
9279 }
9280
9281 n = 1;
9282 if (vim_isdigit(*eap->arg)) /* get count */
9283 {
9284 n = getdigits(&eap->arg);
9285 eap->arg = skipwhite(eap->arg);
9286 }
9287 if (*eap->arg == '/') /* Match regexp, not just whole words */
9288 {
9289 whole = FALSE;
9290 ++eap->arg;
9291 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9292 if (*p)
9293 {
9294 *p++ = NUL;
9295 p = skipwhite(p);
9296
9297 /* Check for trailing illegal characters */
9298 if (!ends_excmd(*p))
9299 eap->errmsg = e_trailing;
9300 else
9301 eap->nextcmd = check_nextcmd(p);
9302 }
9303 }
9304 if (!eap->skip)
9305 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9306 whole, !eap->forceit,
9307 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9308 n, action, eap->line1, eap->line2);
9309}
9310#endif
9311
9312#ifdef FEAT_WINDOWS
9313
9314# ifdef FEAT_QUICKFIX
9315/*
9316 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9317 */
9318 static void
9319ex_ptag(eap)
9320 exarg_T *eap;
9321{
9322 g_do_tagpreview = p_pvh;
9323 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9324}
9325
9326/*
9327 * ":pedit"
9328 */
9329 static void
9330ex_pedit(eap)
9331 exarg_T *eap;
9332{
9333 win_T *curwin_save = curwin;
9334
9335 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009336 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 keep_help_flag = curwin_save->w_buffer->b_help;
9338 do_exedit(eap, NULL);
9339 keep_help_flag = FALSE;
9340 if (curwin != curwin_save && win_valid(curwin_save))
9341 {
9342 /* Return cursor to where we were */
9343 validate_cursor();
9344 redraw_later(VALID);
9345 win_enter(curwin_save, TRUE);
9346 }
9347 g_do_tagpreview = 0;
9348}
9349# endif
9350
9351/*
9352 * ":stag", ":stselect" and ":stjump".
9353 */
9354 static void
9355ex_stag(eap)
9356 exarg_T *eap;
9357{
9358 postponed_split = -1;
9359 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009360 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9362 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009363 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364}
9365#endif
9366
9367/*
9368 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9369 */
9370 static void
9371ex_tag(eap)
9372 exarg_T *eap;
9373{
9374 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9375}
9376
9377 static void
9378ex_tag_cmd(eap, name)
9379 exarg_T *eap;
9380 char_u *name;
9381{
9382 int cmd;
9383
9384 switch (name[1])
9385 {
9386 case 'j': cmd = DT_JUMP; /* ":tjump" */
9387 break;
9388 case 's': cmd = DT_SELECT; /* ":tselect" */
9389 break;
9390 case 'p': cmd = DT_PREV; /* ":tprevious" */
9391 break;
9392 case 'N': cmd = DT_PREV; /* ":tNext" */
9393 break;
9394 case 'n': cmd = DT_NEXT; /* ":tnext" */
9395 break;
9396 case 'o': cmd = DT_POP; /* ":pop" */
9397 break;
9398 case 'f': /* ":tfirst" */
9399 case 'r': cmd = DT_FIRST; /* ":trewind" */
9400 break;
9401 case 'l': cmd = DT_LAST; /* ":tlast" */
9402 break;
9403 default: /* ":tag" */
9404#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009405 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 {
9407 do_cstag(eap);
9408 return;
9409 }
9410#endif
9411 cmd = DT_TAG;
9412 break;
9413 }
9414
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009415 if (name[0] == 'l')
9416 {
9417#ifndef FEAT_QUICKFIX
9418 ex_ni(eap);
9419 return;
9420#else
9421 cmd = DT_LTAG;
9422#endif
9423 }
9424
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9426 eap->forceit, TRUE);
9427}
9428
9429/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009430 * Check "str" for starting with a special cmdline variable.
9431 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9432 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9433 */
9434 int
9435find_cmdline_var(src, usedlen)
9436 char_u *src;
9437 int *usedlen;
9438{
9439 int len;
9440 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009441 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009442 "%",
9443#define SPEC_PERC 0
9444 "#",
9445#define SPEC_HASH 1
9446 "<cword>", /* cursor word */
9447#define SPEC_CWORD 2
9448 "<cWORD>", /* cursor WORD */
9449#define SPEC_CCWORD 3
9450 "<cfile>", /* cursor path name */
9451#define SPEC_CFILE 4
9452 "<sfile>", /* ":so" file name */
9453#define SPEC_SFILE 5
9454#ifdef FEAT_AUTOCMD
9455 "<afile>", /* autocommand file name */
9456# define SPEC_AFILE 6
9457 "<abuf>", /* autocommand buffer number */
9458# define SPEC_ABUF 7
9459 "<amatch>", /* autocommand match name */
9460# define SPEC_AMATCH 8
9461#endif
9462#ifdef FEAT_CLIENTSERVER
9463 "<client>"
9464# define SPEC_CLIENT 9
9465#endif
9466 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009467
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009468 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009469 {
9470 len = (int)STRLEN(spec_str[i]);
9471 if (STRNCMP(src, spec_str[i], len) == 0)
9472 {
9473 *usedlen = len;
9474 return i;
9475 }
9476 }
9477 return -1;
9478}
9479
9480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 * Evaluate cmdline variables.
9482 *
9483 * change '%' to curbuf->b_ffname
9484 * '#' to curwin->w_altfile
9485 * '<cword>' to word under the cursor
9486 * '<cWORD>' to WORD under the cursor
9487 * '<cfile>' to path name under the cursor
9488 * '<sfile>' to sourced file name
9489 * '<afile>' to file name for autocommand
9490 * '<abuf>' to buffer number for autocommand
9491 * '<amatch>' to matching name for autocommand
9492 *
9493 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9494 * "" for error without a message) and NULL is returned.
9495 * Returns an allocated string if a valid match was found.
9496 * Returns NULL if no match was found. "usedlen" then still contains the
9497 * number of characters to skip.
9498 */
9499 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009500eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009502 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 int *usedlen; /* characters after src that are used */
9504 linenr_T *lnump; /* line number for :e command, or NULL */
9505 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009506 int *escaped; /* return value has escaped white space (can
9507 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
9509 int i;
9510 char_u *s;
9511 char_u *result;
9512 char_u *resultbuf = NULL;
9513 int resultlen;
9514 buf_T *buf;
9515 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9516 int spec_idx;
9517#ifdef FEAT_MODIFY_FNAME
9518 int skip_mod = FALSE;
9519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520
9521#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9522 char_u strbuf[30];
9523#endif
9524
9525 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009526 if (escaped != NULL)
9527 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
9529 /*
9530 * Check if there is something to do.
9531 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009532 spec_idx = find_cmdline_var(src, usedlen);
9533 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 {
9535 *usedlen = 1;
9536 return NULL;
9537 }
9538
9539 /*
9540 * Skip when preceded with a backslash "\%" and "\#".
9541 * Note: In "\\%" the % is also not recognized!
9542 */
9543 if (src > srcstart && src[-1] == '\\')
9544 {
9545 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009546 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 return NULL;
9548 }
9549
9550 /*
9551 * word or WORD under cursor
9552 */
9553 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9554 {
9555 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9556 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9557 if (resultlen == 0)
9558 {
9559 *errormsg = (char_u *)"";
9560 return NULL;
9561 }
9562 }
9563
9564 /*
9565 * '#': Alternate file name
9566 * '%': Current file name
9567 * File name under the cursor
9568 * File name for autocommand
9569 * and following modifiers
9570 */
9571 else
9572 {
9573 switch (spec_idx)
9574 {
9575 case SPEC_PERC: /* '%': current file */
9576 if (curbuf->b_fname == NULL)
9577 {
9578 result = (char_u *)"";
9579 valid = 0; /* Must have ":p:h" to be valid */
9580 }
9581 else
9582#ifdef RISCOS
9583 /* Always use the full path for RISC OS if possible. */
9584 result = curbuf->b_ffname;
9585 if (result == NULL)
9586 result = curbuf->b_fname;
9587#else
9588 result = curbuf->b_fname;
9589#endif
9590 break;
9591
9592 case SPEC_HASH: /* '#' or "#99": alternate file */
9593 if (src[1] == '#') /* "##": the argument list */
9594 {
9595 result = arg_all();
9596 resultbuf = result;
9597 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009598 if (escaped != NULL)
9599 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600#ifdef FEAT_MODIFY_FNAME
9601 skip_mod = TRUE;
9602#endif
9603 break;
9604 }
9605 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009606 if (*s == '<') /* "#<99" uses v:oldfiles */
9607 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 i = (int)getdigits(&s);
9609 *usedlen = (int)(s - src); /* length of what we expand */
9610
Bram Moolenaard812df62008-11-09 12:46:09 +00009611 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009613 if (*usedlen < 2)
9614 {
9615 /* Should we give an error message for #<text? */
9616 *usedlen = 1;
9617 return NULL;
9618 }
9619#ifdef FEAT_EVAL
9620 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9621 (long)i);
9622 if (result == NULL)
9623 {
9624 *errormsg = (char_u *)"";
9625 return NULL;
9626 }
9627#else
9628 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 }
9632 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009633 {
9634 buf = buflist_findnr(i);
9635 if (buf == NULL)
9636 {
9637 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9638 return NULL;
9639 }
9640 if (lnump != NULL)
9641 *lnump = ECMD_LAST;
9642 if (buf->b_fname == NULL)
9643 {
9644 result = (char_u *)"";
9645 valid = 0; /* Must have ":p:h" to be valid */
9646 }
9647 else
9648 result = buf->b_fname;
9649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 break;
9651
9652#ifdef FEAT_SEARCHPATH
9653 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009654 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 if (result == NULL)
9656 {
9657 *errormsg = (char_u *)"";
9658 return NULL;
9659 }
9660 resultbuf = result; /* remember allocated string */
9661 break;
9662#endif
9663
9664#ifdef FEAT_AUTOCMD
9665 case SPEC_AFILE: /* file name for autocommand */
9666 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009667 if (result != NULL && !autocmd_fname_full)
9668 {
9669 /* Still need to turn the fname into a full path. It is
9670 * postponed to avoid a delay when <afile> is not used. */
9671 autocmd_fname_full = TRUE;
9672 result = FullName_save(autocmd_fname, FALSE);
9673 vim_free(autocmd_fname);
9674 autocmd_fname = result;
9675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 if (result == NULL)
9677 {
9678 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9679 return NULL;
9680 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009681 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 break;
9683
9684 case SPEC_ABUF: /* buffer number for autocommand */
9685 if (autocmd_bufnr <= 0)
9686 {
9687 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9688 return NULL;
9689 }
9690 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9691 result = strbuf;
9692 break;
9693
9694 case SPEC_AMATCH: /* match name for autocommand */
9695 result = autocmd_match;
9696 if (result == NULL)
9697 {
9698 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9699 return NULL;
9700 }
9701 break;
9702
9703#endif
9704 case SPEC_SFILE: /* file name for ":so" command */
9705 result = sourcing_name;
9706 if (result == NULL)
9707 {
9708 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9709 return NULL;
9710 }
9711 break;
9712#if defined(FEAT_CLIENTSERVER)
9713 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009714 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9715 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 result = strbuf;
9717 break;
9718#endif
9719 }
9720
9721 resultlen = (int)STRLEN(result); /* length of new string */
9722 if (src[*usedlen] == '<') /* remove the file name extension */
9723 {
9724 ++*usedlen;
9725#ifdef RISCOS
9726 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9727#else
9728 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9729#endif
9730 resultlen = (int)(s - result);
9731 }
9732#ifdef FEAT_MODIFY_FNAME
9733 else if (!skip_mod)
9734 {
9735 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9736 &resultlen);
9737 if (result == NULL)
9738 {
9739 *errormsg = (char_u *)"";
9740 return NULL;
9741 }
9742 }
9743#endif
9744 }
9745
9746 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9747 {
9748 if (valid != VALID_HEAD + VALID_PATH)
9749 /* xgettext:no-c-format */
9750 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9751 else
9752 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9753 result = NULL;
9754 }
9755 else
9756 result = vim_strnsave(result, resultlen);
9757 vim_free(resultbuf);
9758 return result;
9759}
9760
9761/*
9762 * Concatenate all files in the argument list, separated by spaces, and return
9763 * it in one allocated string.
9764 * Spaces and backslashes in the file names are escaped with a backslash.
9765 * Returns NULL when out of memory.
9766 */
9767 static char_u *
9768arg_all()
9769{
9770 int len;
9771 int idx;
9772 char_u *retval = NULL;
9773 char_u *p;
9774
9775 /*
9776 * Do this loop two times:
9777 * first time: compute the total length
9778 * second time: concatenate the names
9779 */
9780 for (;;)
9781 {
9782 len = 0;
9783 for (idx = 0; idx < ARGCOUNT; ++idx)
9784 {
9785 p = alist_name(&ARGLIST[idx]);
9786 if (p != NULL)
9787 {
9788 if (len > 0)
9789 {
9790 /* insert a space in between names */
9791 if (retval != NULL)
9792 retval[len] = ' ';
9793 ++len;
9794 }
9795 for ( ; *p != NUL; ++p)
9796 {
9797 if (*p == ' ' || *p == '\\')
9798 {
9799 /* insert a backslash */
9800 if (retval != NULL)
9801 retval[len] = '\\';
9802 ++len;
9803 }
9804 if (retval != NULL)
9805 retval[len] = *p;
9806 ++len;
9807 }
9808 }
9809 }
9810
9811 /* second time: break here */
9812 if (retval != NULL)
9813 {
9814 retval[len] = NUL;
9815 break;
9816 }
9817
9818 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009819 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (retval == NULL)
9821 break;
9822 }
9823
9824 return retval;
9825}
9826
9827#if defined(FEAT_AUTOCMD) || defined(PROTO)
9828/*
9829 * Expand the <sfile> string in "arg".
9830 *
9831 * Returns an allocated string, or NULL for any error.
9832 */
9833 char_u *
9834expand_sfile(arg)
9835 char_u *arg;
9836{
9837 char_u *errormsg;
9838 int len;
9839 char_u *result;
9840 char_u *newres;
9841 char_u *repl;
9842 int srclen;
9843 char_u *p;
9844
9845 result = vim_strsave(arg);
9846 if (result == NULL)
9847 return NULL;
9848
9849 for (p = result; *p; )
9850 {
9851 if (STRNCMP(p, "<sfile>", 7) != 0)
9852 ++p;
9853 else
9854 {
9855 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009856 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 if (errormsg != NULL)
9858 {
9859 if (*errormsg)
9860 emsg(errormsg);
9861 vim_free(result);
9862 return NULL;
9863 }
9864 if (repl == NULL) /* no match (cannot happen) */
9865 {
9866 p += srclen;
9867 continue;
9868 }
9869 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9870 newres = alloc(len);
9871 if (newres == NULL)
9872 {
9873 vim_free(repl);
9874 vim_free(result);
9875 return NULL;
9876 }
9877 mch_memmove(newres, result, (size_t)(p - result));
9878 STRCPY(newres + (p - result), repl);
9879 len = (int)STRLEN(newres);
9880 STRCAT(newres, p + srclen);
9881 vim_free(repl);
9882 vim_free(result);
9883 result = newres;
9884 p = newres + len; /* continue after the match */
9885 }
9886 }
9887
9888 return result;
9889}
9890#endif
9891
9892#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009893static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9894 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9896static frame_T *ses_skipframe __ARGS((frame_T *fr));
9897static int ses_do_frame __ARGS((frame_T *fr));
9898static int ses_do_win __ARGS((win_T *wp));
9899static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9900static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9901static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9902
9903/*
9904 * Write openfile commands for the current buffers to an .exrc file.
9905 * Return FAIL on error, OK otherwise.
9906 */
9907 static int
9908makeopens(fd, dirnow)
9909 FILE *fd;
9910 char_u *dirnow; /* Current directory name */
9911{
9912 buf_T *buf;
9913 int only_save_windows = TRUE;
9914 int nr;
9915 int cnr = 1;
9916 int restore_size = TRUE;
9917 win_T *wp;
9918 char_u *sname;
9919 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009920 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009921 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009922 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009923 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009924 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
9926 if (ssop_flags & SSOP_BUFFERS)
9927 only_save_windows = FALSE; /* Save ALL buffers */
9928
9929 /*
9930 * Begin by setting the this_session variable, and then other
9931 * sessionable variables.
9932 */
9933#ifdef FEAT_EVAL
9934 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9935 return FAIL;
9936 if (ssop_flags & SSOP_GLOBALS)
9937 if (store_session_globals(fd) == FAIL)
9938 return FAIL;
9939#endif
9940
9941 /*
9942 * Close all windows but one.
9943 */
9944 if (put_line(fd, "silent only") == FAIL)
9945 return FAIL;
9946
9947 /*
9948 * Now a :cd command to the session directory or the current directory
9949 */
9950 if (ssop_flags & SSOP_SESDIR)
9951 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009952 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9953 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 return FAIL;
9955 }
9956 else if (ssop_flags & SSOP_CURDIR)
9957 {
9958 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9959 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009960 || fputs("cd ", fd) < 0
9961 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9962 || put_eol(fd) == FAIL)
9963 {
9964 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 vim_free(sname);
9968 }
9969
9970 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009971 * If there is an empty, unnamed buffer we will wipe it out later.
9972 * Remember the buffer number.
9973 */
9974 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9975 return FAIL;
9976 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9977 return FAIL;
9978 if (put_line(fd, "endif") == FAIL)
9979 return FAIL;
9980
9981 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 * Now save the current files, current buffer first.
9983 */
9984 if (put_line(fd, "set shortmess=aoO") == FAIL)
9985 return FAIL;
9986
9987 /* Now put the other buffers into the buffer list */
9988 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9989 {
9990 if (!(only_save_windows && buf->b_nwindows == 0)
9991 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9992 && buf->b_fname != NULL
9993 && buf->b_p_bl)
9994 {
9995 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9996 : buf->b_wininfo->wi_fpos.lnum) < 0
9997 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9998 return FAIL;
9999 }
10000 }
10001
10002 /* the global argument list */
10003 if (ses_arglist(fd, "args", &global_alist.al_ga,
10004 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10005 return FAIL;
10006
10007 if (ssop_flags & SSOP_RESIZE)
10008 {
10009 /* Note: after the restore we still check it worked!*/
10010 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10011 || put_eol(fd) == FAIL)
10012 return FAIL;
10013 }
10014
10015#ifdef FEAT_GUI
10016 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10017 {
10018 int x, y;
10019
10020 if (gui_mch_get_winpos(&x, &y) == OK)
10021 {
10022 /* Note: after the restore we still check it worked!*/
10023 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10024 return FAIL;
10025 }
10026 }
10027#endif
10028
10029 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010030 * May repeat putting Windows for each tab, when "tabpages" is in
10031 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010032 * Don't use goto_tabpage(), it may change directory and trigger
10033 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010035 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010036 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010037 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010039 int need_tabnew = FALSE;
10040
Bram Moolenaar18144c82006-04-12 21:52:12 +000010041 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010043 tabpage_T *tp = find_tabpage(tabnr);
10044
10045 if (tp == NULL)
10046 break; /* done all tab pages */
10047 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010048 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010049 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010050 tab_topframe = topframe;
10051 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010052 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010053 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010054 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010055 tab_topframe = tp->tp_topframe;
10056 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010057 if (tabnr > 1)
10058 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060
Bram Moolenaar18144c82006-04-12 21:52:12 +000010061 /*
10062 * Before creating the window layout, try loading one file. If this
10063 * is aborted we don't end up with a number of useless windows.
10064 * This may have side effects! (e.g., compressed or network file).
10065 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010066 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010067 {
10068 if (ses_do_win(wp)
10069 && wp->w_buffer->b_ffname != NULL
10070 && !wp->w_buffer->b_help
10071#ifdef FEAT_QUICKFIX
10072 && !bt_nofile(wp->w_buffer)
10073#endif
10074 )
10075 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010076 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010077 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10078 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010079 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010080 if (!wp->w_arg_idx_invalid)
10081 edited_win = wp;
10082 break;
10083 }
10084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010086 /* If no file got edited create an empty tab page. */
10087 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10088 return FAIL;
10089
Bram Moolenaar18144c82006-04-12 21:52:12 +000010090 /*
10091 * Save current window layout.
10092 */
10093 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010095 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010097 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10098 return FAIL;
10099 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10100 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
Bram Moolenaar18144c82006-04-12 21:52:12 +000010102 /*
10103 * Check if window sizes can be restored (no windows omitted).
10104 * Remember the window number of the current window after restoring.
10105 */
10106 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010107 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010108 {
10109 if (ses_do_win(wp))
10110 ++nr;
10111 else
10112 restore_size = FALSE;
10113 if (curwin == wp)
10114 cnr = nr;
10115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116
Bram Moolenaar18144c82006-04-12 21:52:12 +000010117 /* Go to the first window. */
10118 if (put_line(fd, "wincmd t") == FAIL)
10119 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010120
Bram Moolenaar18144c82006-04-12 21:52:12 +000010121 /*
10122 * If more than one window, see if sizes can be restored.
10123 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10124 * resized when moving between windows.
10125 * Do this before restoring the view, so that the topline and the
10126 * cursor can be set. This is done again below.
10127 */
10128 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10129 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010130 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010131 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
Bram Moolenaar18144c82006-04-12 21:52:12 +000010133 /*
10134 * Restore the view of the window (options, file, cursor, etc.).
10135 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010136 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010137 {
10138 if (!ses_do_win(wp))
10139 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010140 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10141 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010142 return FAIL;
10143 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10144 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010145 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010146 }
10147
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010148 /* The argument index in the first tab page is zero, need to set it in
10149 * each window. For further tab pages it's the window where we do
10150 * "tabedit". */
10151 cur_arg_idx = next_arg_idx;
10152
Bram Moolenaar18144c82006-04-12 21:52:12 +000010153 /*
10154 * Restore cursor to the current window if it's not the first one.
10155 */
10156 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10157 || put_eol(fd) == FAIL))
10158 return FAIL;
10159
10160 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010161 * Restore window sizes again after jumping around in windows, because
10162 * the current window has a minimum size while others may not.
10163 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010164 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010165 return FAIL;
10166
Bram Moolenaar18144c82006-04-12 21:52:12 +000010167 /* Don't continue in another tab page when doing only the current one
10168 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010169 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010170 break;
10171 }
10172
10173 if (ssop_flags & SSOP_TABPAGES)
10174 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010175 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10176 || put_eol(fd) == FAIL)
10177 return FAIL;
10178 }
10179
Bram Moolenaar9c102382006-05-03 21:26:49 +000010180 /*
10181 * Wipe out an empty unnamed buffer we started in.
10182 */
10183 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10184 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010185 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010186 return FAIL;
10187 if (put_line(fd, "endif") == FAIL)
10188 return FAIL;
10189 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10190 return FAIL;
10191
10192 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10193 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10194 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10195 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196
10197 /*
10198 * Lastly, execute the x.vim file if it exists.
10199 */
10200 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10201 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010202 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 || put_line(fd, "endif") == FAIL)
10204 return FAIL;
10205
10206 return OK;
10207}
10208
10209 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010210ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 FILE *fd;
10212 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010213 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 int n = 0;
10216 win_T *wp;
10217
10218 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10219 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010220 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 {
10222 if (!ses_do_win(wp))
10223 continue;
10224 ++n;
10225
10226 /* restore height when not full height */
10227 if (wp->w_height + wp->w_status_height < topframe->fr_height
10228 && (fprintf(fd,
10229 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10230 n, (long)wp->w_height, Rows / 2, Rows) < 0
10231 || put_eol(fd) == FAIL))
10232 return FAIL;
10233
10234 /* restore width when not full width */
10235 if (wp->w_width < Columns && (fprintf(fd,
10236 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10237 n, (long)wp->w_width, Columns / 2, Columns) < 0
10238 || put_eol(fd) == FAIL))
10239 return FAIL;
10240 }
10241 }
10242 else
10243 {
10244 /* Just equalise window sizes */
10245 if (put_line(fd, "wincmd =") == FAIL)
10246 return FAIL;
10247 }
10248 return OK;
10249}
10250
10251/*
10252 * Write commands to "fd" to recursively create windows for frame "fr",
10253 * horizontally and vertically split.
10254 * After the commands the last window in the frame is the current window.
10255 * Returns FAIL when writing the commands to "fd" fails.
10256 */
10257 static int
10258ses_win_rec(fd, fr)
10259 FILE *fd;
10260 frame_T *fr;
10261{
10262 frame_T *frc;
10263 int count = 0;
10264
10265 if (fr->fr_layout != FR_LEAF)
10266 {
10267 /* Find first frame that's not skipped and then create a window for
10268 * each following one (first frame is already there). */
10269 frc = ses_skipframe(fr->fr_child);
10270 if (frc != NULL)
10271 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10272 {
10273 /* Make window as big as possible so that we have lots of room
10274 * to split. */
10275 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10276 || put_line(fd, fr->fr_layout == FR_COL
10277 ? "split" : "vsplit") == FAIL)
10278 return FAIL;
10279 ++count;
10280 }
10281
10282 /* Go back to the first window. */
10283 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10284 ? "%dwincmd k" : "%dwincmd h", count) < 0
10285 || put_eol(fd) == FAIL))
10286 return FAIL;
10287
10288 /* Recursively create frames/windows in each window of this column or
10289 * row. */
10290 frc = ses_skipframe(fr->fr_child);
10291 while (frc != NULL)
10292 {
10293 ses_win_rec(fd, frc);
10294 frc = ses_skipframe(frc->fr_next);
10295 /* Go to next window. */
10296 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10297 return FAIL;
10298 }
10299 }
10300 return OK;
10301}
10302
10303/*
10304 * Skip frames that don't contain windows we want to save in the Session.
10305 * Returns NULL when there none.
10306 */
10307 static frame_T *
10308ses_skipframe(fr)
10309 frame_T *fr;
10310{
10311 frame_T *frc;
10312
10313 for (frc = fr; frc != NULL; frc = frc->fr_next)
10314 if (ses_do_frame(frc))
10315 break;
10316 return frc;
10317}
10318
10319/*
10320 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10321 * the Session.
10322 */
10323 static int
10324ses_do_frame(fr)
10325 frame_T *fr;
10326{
10327 frame_T *frc;
10328
10329 if (fr->fr_layout == FR_LEAF)
10330 return ses_do_win(fr->fr_win);
10331 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10332 if (ses_do_frame(frc))
10333 return TRUE;
10334 return FALSE;
10335}
10336
10337/*
10338 * Return non-zero if window "wp" is to be stored in the Session.
10339 */
10340 static int
10341ses_do_win(wp)
10342 win_T *wp;
10343{
10344 if (wp->w_buffer->b_fname == NULL
10345#ifdef FEAT_QUICKFIX
10346 /* When 'buftype' is "nofile" can't restore the window contents. */
10347 || bt_nofile(wp->w_buffer)
10348#endif
10349 )
10350 return (ssop_flags & SSOP_BLANK);
10351 if (wp->w_buffer->b_help)
10352 return (ssop_flags & SSOP_HELP);
10353 return TRUE;
10354}
10355
10356/*
10357 * Write commands to "fd" to restore the view of a window.
10358 * Caller must make sure 'scrolloff' is zero.
10359 */
10360 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010361put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 FILE *fd;
10363 win_T *wp;
10364 int add_edit; /* add ":edit" command to view */
10365 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010366 int current_arg_idx; /* current argument index of the window, use
10367 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368{
10369 win_T *save_curwin;
10370 int f;
10371 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010372 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373
10374 /* Always restore cursor position for ":mksession". For ":mkview" only
10375 * when 'viewoptions' contains "cursor". */
10376 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10377
10378 /*
10379 * Local argument list.
10380 */
10381 if (wp->w_alist == &global_alist)
10382 {
10383 if (put_line(fd, "argglobal") == FAIL)
10384 return FAIL;
10385 }
10386 else
10387 {
10388 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10389 flagp == &vop_flags
10390 || !(*flagp & SSOP_CURDIR)
10391 || wp->w_localdir != NULL, flagp) == FAIL)
10392 return FAIL;
10393 }
10394
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010395 /* Only when part of a session: restore the argument index. Some
10396 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010397 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx <= WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010398 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010400 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 || put_eol(fd) == FAIL)
10402 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010403 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 }
10405
10406 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010407 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 {
10409 /*
10410 * Load the file.
10411 */
10412 if (wp->w_buffer->b_ffname != NULL
10413#ifdef FEAT_QUICKFIX
10414 && !bt_nofile(wp->w_buffer)
10415#endif
10416 )
10417 {
10418 /*
10419 * Editing a file in this buffer: use ":edit file".
10420 * This may have side effects! (e.g., compressed or network file).
10421 */
10422 if (fputs("edit ", fd) < 0
10423 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10424 return FAIL;
10425 }
10426 else
10427 {
10428 /* No file in this buffer, just make it empty. */
10429 if (put_line(fd, "enew") == FAIL)
10430 return FAIL;
10431#ifdef FEAT_QUICKFIX
10432 if (wp->w_buffer->b_ffname != NULL)
10433 {
10434 /* The buffer does have a name, but it's not a file name. */
10435 if (fputs("file ", fd) < 0
10436 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10437 return FAIL;
10438 }
10439#endif
10440 do_cursor = FALSE;
10441 }
10442 }
10443
10444 /*
10445 * Local mappings and abbreviations.
10446 */
10447 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10448 && makemap(fd, wp->w_buffer) == FAIL)
10449 return FAIL;
10450
10451 /*
10452 * Local options. Need to go to the window temporarily.
10453 * Store only local values when using ":mkview" and when ":mksession" is
10454 * used and 'sessionoptions' doesn't include "options".
10455 * Some folding options are always stored when "folds" is included,
10456 * otherwise the folds would not be restored correctly.
10457 */
10458 save_curwin = curwin;
10459 curwin = wp;
10460 curbuf = curwin->w_buffer;
10461 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10462 f = makeset(fd, OPT_LOCAL,
10463 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10464#ifdef FEAT_FOLDING
10465 else if (*flagp & SSOP_FOLDS)
10466 f = makefoldset(fd);
10467#endif
10468 else
10469 f = OK;
10470 curwin = save_curwin;
10471 curbuf = curwin->w_buffer;
10472 if (f == FAIL)
10473 return FAIL;
10474
10475#ifdef FEAT_FOLDING
10476 /*
10477 * Save Folds when 'buftype' is empty and for help files.
10478 */
10479 if ((*flagp & SSOP_FOLDS)
10480 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010481# ifdef FEAT_QUICKFIX
10482 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10483# endif
10484 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 {
10486 if (put_folds(fd, wp) == FAIL)
10487 return FAIL;
10488 }
10489#endif
10490
10491 /*
10492 * Set the cursor after creating folds, since that moves the cursor.
10493 */
10494 if (do_cursor)
10495 {
10496
10497 /* Restore the cursor line in the file and relatively in the
10498 * window. Don't use "G", it changes the jumplist. */
10499 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10500 (long)wp->w_cursor.lnum,
10501 (long)(wp->w_cursor.lnum - wp->w_topline),
10502 (long)wp->w_height / 2, (long)wp->w_height) < 0
10503 || put_eol(fd) == FAIL
10504 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10505 || put_line(fd, "exe s:l") == FAIL
10506 || put_line(fd, "normal! zt") == FAIL
10507 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10508 || put_eol(fd) == FAIL)
10509 return FAIL;
10510 /* Restore the cursor column and left offset when not wrapping. */
10511 if (wp->w_cursor.col == 0)
10512 {
10513 if (put_line(fd, "normal! 0") == FAIL)
10514 return FAIL;
10515 }
10516 else
10517 {
10518 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10519 {
10520 if (fprintf(fd,
10521 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10522 (long)wp->w_cursor.col,
10523 (long)(wp->w_cursor.col - wp->w_leftcol),
10524 (long)wp->w_width / 2, (long)wp->w_width) < 0
10525 || put_eol(fd) == FAIL
10526 || put_line(fd, "if s:c > 0") == FAIL
10527 || fprintf(fd,
10528 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10529 (long)wp->w_cursor.col) < 0
10530 || put_eol(fd) == FAIL
10531 || put_line(fd, "else") == FAIL
10532 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10533 || put_eol(fd) == FAIL
10534 || put_line(fd, "endif") == FAIL)
10535 return FAIL;
10536 }
10537 else
10538 {
10539 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10540 || put_eol(fd) == FAIL)
10541 return FAIL;
10542 }
10543 }
10544 }
10545
10546 /*
10547 * Local directory.
10548 */
10549 if (wp->w_localdir != NULL)
10550 {
10551 if (fputs("lcd ", fd) < 0
10552 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10553 || put_eol(fd) == FAIL)
10554 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010555 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 }
10557
10558 return OK;
10559}
10560
10561/*
10562 * Write an argument list to the session file.
10563 * Returns FAIL if writing fails.
10564 */
10565 static int
10566ses_arglist(fd, cmd, gap, fullname, flagp)
10567 FILE *fd;
10568 char *cmd;
10569 garray_T *gap;
10570 int fullname; /* TRUE: use full path name */
10571 unsigned *flagp;
10572{
10573 int i;
10574 char_u buf[MAXPATHL];
10575 char_u *s;
10576
10577 if (gap->ga_len == 0)
10578 return put_line(fd, "silent! argdel *");
10579 if (fputs(cmd, fd) < 0)
10580 return FAIL;
10581 for (i = 0; i < gap->ga_len; ++i)
10582 {
10583 /* NULL file names are skipped (only happens when out of memory). */
10584 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10585 if (s != NULL)
10586 {
10587 if (fullname)
10588 {
10589 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10590 s = buf;
10591 }
10592 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10593 return FAIL;
10594 }
10595 }
10596 return put_eol(fd);
10597}
10598
10599/*
10600 * Write a buffer name to the session file.
10601 * Also ends the line.
10602 * Returns FAIL if writing fails.
10603 */
10604 static int
10605ses_fname(fd, buf, flagp)
10606 FILE *fd;
10607 buf_T *buf;
10608 unsigned *flagp;
10609{
10610 char_u *name;
10611
10612 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010613 * the session file will be sourced.
10614 * Don't do this for ":mkview", we don't know the current directory.
10615 * Don't do this after ":lcd", we don't keep track of what the current
10616 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 if (buf->b_sfname != NULL
10618 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010619 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010620#ifdef FEAT_AUTOCHDIR
10621 && !p_acd
10622#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010623 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 name = buf->b_sfname;
10625 else
10626 name = buf->b_ffname;
10627 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10628 return FAIL;
10629 return OK;
10630}
10631
10632/*
10633 * Write a file name to the session file.
10634 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10635 * characters.
10636 * Returns FAIL if writing fails.
10637 */
10638 static int
10639ses_put_fname(fd, name, flagp)
10640 FILE *fd;
10641 char_u *name;
10642 unsigned *flagp;
10643{
10644 char_u *sname;
10645 int retval = OK;
10646 int c;
10647
10648 sname = home_replace_save(NULL, name);
10649 if (sname != NULL)
10650 name = sname;
10651 while (*name != NUL)
10652 {
10653#ifdef FEAT_MBYTE
10654 {
10655 int l;
10656
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010657 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 {
10659 /* copy a multibyte char */
10660 while (--l >= 0)
10661 {
10662 if (putc(*name, fd) != *name)
10663 retval = FAIL;
10664 ++name;
10665 }
10666 continue;
10667 }
10668 }
10669#endif
10670 c = *name++;
10671 if (c == '\\' && (*flagp & SSOP_SLASH))
10672 /* change a backslash to a forward slash */
10673 c = '/';
10674 else if ((vim_strchr(escape_chars, c) != NULL
10675#ifdef BACKSLASH_IN_FILENAME
10676 && c != '\\'
10677#endif
10678 ) || c == '#' || c == '%')
10679 {
10680 /* escape a special character with a backslash */
10681 if (putc('\\', fd) != '\\')
10682 retval = FAIL;
10683 }
10684 if (putc(c, fd) != c)
10685 retval = FAIL;
10686 }
10687 vim_free(sname);
10688 return retval;
10689}
10690
10691/*
10692 * ":loadview [nr]"
10693 */
10694 static void
10695ex_loadview(eap)
10696 exarg_T *eap;
10697{
10698 char_u *fname;
10699
10700 fname = get_view_file(*eap->arg);
10701 if (fname != NULL)
10702 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010703 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 vim_free(fname);
10705 }
10706}
10707
10708/*
10709 * Get the name of the view file for the current buffer.
10710 */
10711 static char_u *
10712get_view_file(c)
10713 int c;
10714{
10715 int len = 0;
10716 char_u *p, *s;
10717 char_u *retval;
10718 char_u *sname;
10719
10720 if (curbuf->b_ffname == NULL)
10721 {
10722 EMSG(_(e_noname));
10723 return NULL;
10724 }
10725 sname = home_replace_save(NULL, curbuf->b_ffname);
10726 if (sname == NULL)
10727 return NULL;
10728
10729 /*
10730 * We want a file name without separators, because we're not going to make
10731 * a directory.
10732 * "normal" path separator -> "=+"
10733 * "=" -> "=="
10734 * ":" path separator -> "=-"
10735 */
10736 for (p = sname; *p; ++p)
10737 if (*p == '=' || vim_ispathsep(*p))
10738 ++len;
10739 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10740 if (retval != NULL)
10741 {
10742 STRCPY(retval, p_vdir);
10743 add_pathsep(retval);
10744 s = retval + STRLEN(retval);
10745 for (p = sname; *p; ++p)
10746 {
10747 if (*p == '=')
10748 {
10749 *s++ = '=';
10750 *s++ = '=';
10751 }
10752 else if (vim_ispathsep(*p))
10753 {
10754 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010755#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 || defined(VMS)
10757 if (*p == ':')
10758 *s++ = '-';
10759 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010761 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 }
10763 else
10764 *s++ = *p;
10765 }
10766 *s++ = '=';
10767 *s++ = c;
10768 STRCPY(s, ".vim");
10769 }
10770
10771 vim_free(sname);
10772 return retval;
10773}
10774
10775#endif /* FEAT_SESSION */
10776
10777/*
10778 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10779 * Return FAIL for a write error.
10780 */
10781 int
10782put_eol(fd)
10783 FILE *fd;
10784{
10785 if (
10786#ifdef USE_CRNL
10787 (
10788# ifdef MKSESSION_NL
10789 !mksession_nl &&
10790# endif
10791 (putc('\r', fd) < 0)) ||
10792#endif
10793 (putc('\n', fd) < 0))
10794 return FAIL;
10795 return OK;
10796}
10797
10798/*
10799 * Write a line to "fd".
10800 * Return FAIL for a write error.
10801 */
10802 int
10803put_line(fd, s)
10804 FILE *fd;
10805 char *s;
10806{
10807 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10808 return FAIL;
10809 return OK;
10810}
10811
10812#ifdef FEAT_VIMINFO
10813/*
10814 * ":rviminfo" and ":wviminfo".
10815 */
10816 static void
10817ex_viminfo(eap)
10818 exarg_T *eap;
10819{
10820 char_u *save_viminfo;
10821
10822 save_viminfo = p_viminfo;
10823 if (*p_viminfo == NUL)
10824 p_viminfo = (char_u *)"'100";
10825 if (eap->cmdidx == CMD_rviminfo)
10826 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010827 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10828 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 EMSG(_("E195: Cannot open viminfo file for reading"));
10830 }
10831 else
10832 write_viminfo(eap->arg, eap->forceit);
10833 p_viminfo = save_viminfo;
10834}
10835#endif
10836
10837#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010838/*
10839 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010840 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 void
10843dialog_msg(buff, format, fname)
10844 char_u *buff;
10845 char *format;
10846 char_u *fname;
10847{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 if (fname == NULL)
10849 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010850 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851}
10852#endif
10853
10854/*
10855 * ":behave {mswin,xterm}"
10856 */
10857 static void
10858ex_behave(eap)
10859 exarg_T *eap;
10860{
10861 if (STRCMP(eap->arg, "mswin") == 0)
10862 {
10863 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10864 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10865 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10866 set_option_value((char_u *)"keymodel", 0L,
10867 (char_u *)"startsel,stopsel", 0);
10868 }
10869 else if (STRCMP(eap->arg, "xterm") == 0)
10870 {
10871 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10872 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10873 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10874 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10875 }
10876 else
10877 EMSG2(_(e_invarg2), eap->arg);
10878}
10879
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010880#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10881/*
10882 * Function given to ExpandGeneric() to obtain the possible arguments of the
10883 * ":behave {mswin,xterm}" command.
10884 */
10885 char_u *
10886get_behave_arg(xp, idx)
10887 expand_T *xp UNUSED;
10888 int idx;
10889{
10890 if (idx == 0)
10891 return (char_u *)"mswin";
10892 if (idx == 1)
10893 return (char_u *)"xterm";
10894 return NULL;
10895}
10896#endif
10897
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898#ifdef FEAT_AUTOCMD
10899static int filetype_detect = FALSE;
10900static int filetype_plugin = FALSE;
10901static int filetype_indent = FALSE;
10902
10903/*
10904 * ":filetype [plugin] [indent] {on,off,detect}"
10905 * on: Load the filetype.vim file to install autocommands for file types.
10906 * off: Load the ftoff.vim file to remove all autocommands for file types.
10907 * plugin on: load filetype.vim and ftplugin.vim
10908 * plugin off: load ftplugof.vim
10909 * indent on: load filetype.vim and indent.vim
10910 * indent off: load indoff.vim
10911 */
10912 static void
10913ex_filetype(eap)
10914 exarg_T *eap;
10915{
10916 char_u *arg = eap->arg;
10917 int plugin = FALSE;
10918 int indent = FALSE;
10919
10920 if (*eap->arg == NUL)
10921 {
10922 /* Print current status. */
10923 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10924 filetype_detect ? "ON" : "OFF",
10925 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10926 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10927 return;
10928 }
10929
10930 /* Accept "plugin" and "indent" in any order. */
10931 for (;;)
10932 {
10933 if (STRNCMP(arg, "plugin", 6) == 0)
10934 {
10935 plugin = TRUE;
10936 arg = skipwhite(arg + 6);
10937 continue;
10938 }
10939 if (STRNCMP(arg, "indent", 6) == 0)
10940 {
10941 indent = TRUE;
10942 arg = skipwhite(arg + 6);
10943 continue;
10944 }
10945 break;
10946 }
10947 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10948 {
10949 if (*arg == 'o' || !filetype_detect)
10950 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010951 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 filetype_detect = TRUE;
10953 if (plugin)
10954 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010955 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 filetype_plugin = TRUE;
10957 }
10958 if (indent)
10959 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010960 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 filetype_indent = TRUE;
10962 }
10963 }
10964 if (*arg == 'd')
10965 {
10966 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010967 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 }
10969 }
10970 else if (STRCMP(arg, "off") == 0)
10971 {
10972 if (plugin || indent)
10973 {
10974 if (plugin)
10975 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010976 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 filetype_plugin = FALSE;
10978 }
10979 if (indent)
10980 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010981 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 filetype_indent = FALSE;
10983 }
10984 }
10985 else
10986 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010987 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 filetype_detect = FALSE;
10989 }
10990 }
10991 else
10992 EMSG2(_(e_invarg2), arg);
10993}
10994
10995/*
10996 * ":setfiletype {name}"
10997 */
10998 static void
10999ex_setfiletype(eap)
11000 exarg_T *eap;
11001{
11002 if (!did_filetype)
11003 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11004}
11005#endif
11006
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 static void
11008ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011009 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010{
11011#ifdef FEAT_DIGRAPHS
11012 if (*eap->arg != NUL)
11013 putdigraph(eap->arg);
11014 else
11015 listdigraphs();
11016#else
11017 EMSG(_("E196: No digraphs in this version"));
11018#endif
11019}
11020
11021 static void
11022ex_set(eap)
11023 exarg_T *eap;
11024{
11025 int flags = 0;
11026
11027 if (eap->cmdidx == CMD_setlocal)
11028 flags = OPT_LOCAL;
11029 else if (eap->cmdidx == CMD_setglobal)
11030 flags = OPT_GLOBAL;
11031#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11032 if (cmdmod.browse && flags == 0)
11033 ex_options(eap);
11034 else
11035#endif
11036 (void)do_set(eap->arg, flags);
11037}
11038
11039#ifdef FEAT_SEARCH_EXTRA
11040/*
11041 * ":nohlsearch"
11042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 static void
11044ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011045 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046{
11047 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011048 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049}
11050
11051/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011052 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 * Sets nextcmd to the start of the next command, if any. Also called when
11054 * skipping commands to find the next command.
11055 */
11056 static void
11057ex_match(eap)
11058 exarg_T *eap;
11059{
11060 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011061 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 char_u *end;
11063 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011064 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011065
11066 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011067 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011068 else
11069 {
11070 EMSG(e_invcmd);
11071 return;
11072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073
11074 /* First clear any old pattern. */
11075 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011076 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077
11078 if (ends_excmd(*eap->arg))
11079 end = eap->arg;
11080 else if ((STRNICMP(eap->arg, "none", 4) == 0
11081 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11082 end = eap->arg + 4;
11083 else
11084 {
11085 p = skiptowhite(eap->arg);
11086 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011087 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 p = skipwhite(p);
11089 if (*p == NUL)
11090 {
11091 /* There must be two arguments. */
11092 EMSG2(_(e_invarg2), eap->arg);
11093 return;
11094 }
11095 end = skip_regexp(p + 1, *p, TRUE, NULL);
11096 if (!eap->skip)
11097 {
11098 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11099 {
11100 eap->errmsg = e_trailing;
11101 return;
11102 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011103 if (*end != *p)
11104 {
11105 EMSG2(_(e_invarg2), p);
11106 return;
11107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108
11109 c = *end;
11110 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011111 match_add(curwin, g, p + 1, 10, id);
11112 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011113 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 }
11115 }
11116 eap->nextcmd = find_nextcmd(end);
11117}
11118#endif
11119
11120#ifdef FEAT_CRYPT
11121/*
11122 * ":X": Get crypt key
11123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 static void
11125ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011126 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127{
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011128 if (curbuf->b_p_cm == 0 || blowfish_self_test() == OK)
11129 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130}
11131#endif
11132
11133#ifdef FEAT_FOLDING
11134 static void
11135ex_fold(eap)
11136 exarg_T *eap;
11137{
11138 if (foldManualAllowed(TRUE))
11139 foldCreate(eap->line1, eap->line2);
11140}
11141
11142 static void
11143ex_foldopen(eap)
11144 exarg_T *eap;
11145{
11146 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11147 eap->forceit, FALSE);
11148}
11149
11150 static void
11151ex_folddo(eap)
11152 exarg_T *eap;
11153{
11154 linenr_T lnum;
11155
11156 /* First set the marks for all lines closed/open. */
11157 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11158 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11159 ml_setmarked(lnum);
11160
11161 /* Execute the command on the marked lines. */
11162 global_exe(eap->arg);
11163 ml_clearmarked(); /* clear rest of the marks */
11164}
11165#endif