blob: f1c6bb1e04523b60b157e26254921d65569b5f3b [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
16#ifdef HAVE_FCNTL_H
17# include <fcntl.h> /* for chdir() */
18#endif
19
20static int quitmore = 0;
21static int ex_pressedreturn = FALSE;
22#ifndef FEAT_PRINTER
23# define ex_hardcopy ex_ni
24#endif
25
26#ifdef FEAT_USR_CMDS
27typedef struct ucmd
28{
29 char_u *uc_name; /* The command name */
30 long_u uc_argt; /* The argument type */
31 char_u *uc_rep; /* The command's replacement string */
32 long uc_def; /* The default value for a range/count */
33 scid_T uc_scriptID; /* SID where the command was defined */
34 int uc_compl; /* completion type */
35# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
36 char_u *uc_compl_arg; /* completion argument if any */
37# endif
38} ucmd_T;
39
40#define UC_BUFFER 1 /* -buffer: local to current buffer */
41
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000042static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
45#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
46
47static void do_ucmd __ARGS((exarg_T *eap));
48static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void ex_delcommand __ARGS((exarg_T *eap));
50# ifdef FEAT_CMDL_COMPL
51static char_u *get_user_command_name __ARGS((int idx));
52# endif
53
54#else
55# define ex_command ex_ni
56# define ex_comclear ex_ni
57# define ex_delcommand ex_ni
58#endif
59
60#ifdef FEAT_EVAL
61static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*getline)(int, void *, int), void *cookie));
62#else
63static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*getline)(int, void *, int), void *cookie));
64static int if_level = 0; /* depth in :if */
65#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static char_u *find_command __ARGS((exarg_T *eap, int *full));
67
68static void ex_abbreviate __ARGS((exarg_T *eap));
69static void ex_map __ARGS((exarg_T *eap));
70static void ex_unmap __ARGS((exarg_T *eap));
71static void ex_mapclear __ARGS((exarg_T *eap));
72static void ex_abclear __ARGS((exarg_T *eap));
73#ifndef FEAT_MENU
74# define ex_emenu ex_ni
75# define ex_menu ex_ni
76# define ex_menutranslate ex_ni
77#endif
78#ifdef FEAT_AUTOCMD
79static void ex_autocmd __ARGS((exarg_T *eap));
80static void ex_doautocmd __ARGS((exarg_T *eap));
81#else
82# define ex_autocmd ex_ni
83# define ex_doautocmd ex_ni
84# define ex_doautoall ex_ni
85#endif
86#ifdef FEAT_LISTCMDS
87static void ex_bunload __ARGS((exarg_T *eap));
88static void ex_buffer __ARGS((exarg_T *eap));
89static void ex_bmodified __ARGS((exarg_T *eap));
90static void ex_bnext __ARGS((exarg_T *eap));
91static void ex_bprevious __ARGS((exarg_T *eap));
92static void ex_brewind __ARGS((exarg_T *eap));
93static void ex_blast __ARGS((exarg_T *eap));
94#else
95# define ex_bunload ex_ni
96# define ex_buffer ex_ni
97# define ex_bmodified ex_ni
98# define ex_bnext ex_ni
99# define ex_bprevious ex_ni
100# define ex_brewind ex_ni
101# define ex_blast ex_ni
102# define buflist_list ex_ni
103# define ex_checktime ex_ni
104#endif
105#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
106# define ex_buffer_all ex_ni
107#endif
108static char_u *getargcmd __ARGS((char_u **));
109static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
110static int getargopt __ARGS((exarg_T *eap));
111#ifndef FEAT_QUICKFIX
112# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000113# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114# define ex_cc ex_ni
115# define ex_cnext ex_ni
116# define ex_cfile ex_ni
117# define qf_list ex_ni
118# define qf_age ex_ni
119# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000120# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#endif
122#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
123# define ex_cclose ex_ni
124# define ex_copen ex_ni
125# define ex_cwindow ex_ni
126#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000127#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
128# define ex_cexpr ex_ni
129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131static int check_more __ARGS((int, int));
132static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000133static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000135 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static void ex_script_ni __ARGS((exarg_T *eap));
137#endif
138static char_u *invalid_range __ARGS((exarg_T *eap));
139static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000140#ifdef FEAT_QUICKFIX
141static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
144static void ex_highlight __ARGS((exarg_T *eap));
145static void ex_colorscheme __ARGS((exarg_T *eap));
146static void ex_quit __ARGS((exarg_T *eap));
147static void ex_cquit __ARGS((exarg_T *eap));
148static void ex_quit_all __ARGS((exarg_T *eap));
149#ifdef FEAT_WINDOWS
150static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000151static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static void ex_resize __ARGS((exarg_T *eap));
154static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000155static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000156static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000157static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000158static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#else
161# define ex_close ex_ni
162# define ex_only ex_ni
163# define ex_all ex_ni
164# define ex_resize ex_ni
165# define ex_splitview ex_ni
166# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000167# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000168# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169# define ex_tabs ex_ni
170# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000171# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
174static void ex_pclose __ARGS((exarg_T *eap));
175static void ex_ptag __ARGS((exarg_T *eap));
176static void ex_pedit __ARGS((exarg_T *eap));
177#else
178# define ex_pclose ex_ni
179# define ex_ptag ex_ni
180# define ex_pedit ex_ni
181#endif
182static void ex_hide __ARGS((exarg_T *eap));
183static void ex_stop __ARGS((exarg_T *eap));
184static void ex_exit __ARGS((exarg_T *eap));
185static void ex_print __ARGS((exarg_T *eap));
186#ifdef FEAT_BYTEOFF
187static void ex_goto __ARGS((exarg_T *eap));
188#else
189# define ex_goto ex_ni
190#endif
191static void ex_shell __ARGS((exarg_T *eap));
192static void ex_preserve __ARGS((exarg_T *eap));
193static void ex_recover __ARGS((exarg_T *eap));
194#ifndef FEAT_LISTCMDS
195# define ex_argedit ex_ni
196# define ex_argadd ex_ni
197# define ex_argdelete ex_ni
198# define ex_listdo ex_ni
199#endif
200static void ex_mode __ARGS((exarg_T *eap));
201static void ex_wrongmodifier __ARGS((exarg_T *eap));
202static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000203static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204static void ex_edit __ARGS((exarg_T *eap));
205#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
206# define ex_drop ex_ni
207#endif
208#ifndef FEAT_GUI
209# define ex_gui ex_nogui
210static void ex_nogui __ARGS((exarg_T *eap));
211#endif
212#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
213static void ex_tearoff __ARGS((exarg_T *eap));
214#else
215# define ex_tearoff ex_ni
216#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000217#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218static void ex_popup __ARGS((exarg_T *eap));
219#else
220# define ex_popup ex_ni
221#endif
222#ifndef FEAT_GUI_MSWIN
223# define ex_simalt ex_ni
224#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000225#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226# define gui_mch_find_dialog ex_ni
227# define gui_mch_replace_dialog ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define ex_helpfind ex_ni
231#endif
232#ifndef FEAT_CSCOPE
233# define do_cscope ex_ni
234# define do_scscope ex_ni
235# define do_cstag ex_ni
236#endif
237#ifndef FEAT_SYN_HL
238# define ex_syntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000239#endif
240#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000241# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000243# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000244# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000245# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000247#ifndef FEAT_MZSCHEME
248# define ex_mzscheme ex_script_ni
249# define ex_mzfile ex_ni
250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifndef FEAT_PERL
252# define ex_perl ex_script_ni
253# define ex_perldo ex_ni
254#endif
255#ifndef FEAT_PYTHON
256# define ex_python ex_script_ni
257# define ex_pyfile ex_ni
258#endif
259#ifndef FEAT_TCL
260# define ex_tcl ex_script_ni
261# define ex_tcldo ex_ni
262# define ex_tclfile ex_ni
263#endif
264#ifndef FEAT_RUBY
265# define ex_ruby ex_script_ni
266# define ex_rubydo ex_ni
267# define ex_rubyfile ex_ni
268#endif
269#ifndef FEAT_SNIFF
270# define ex_sniff ex_ni
271#endif
272#ifndef FEAT_KEYMAP
273# define ex_loadkeymap ex_ni
274#endif
275static void ex_swapname __ARGS((exarg_T *eap));
276static void ex_syncbind __ARGS((exarg_T *eap));
277static void ex_read __ARGS((exarg_T *eap));
278static void ex_cd __ARGS((exarg_T *eap));
279static void ex_pwd __ARGS((exarg_T *eap));
280static void ex_equal __ARGS((exarg_T *eap));
281static void ex_sleep __ARGS((exarg_T *eap));
282static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
283static void ex_winsize __ARGS((exarg_T *eap));
284#ifdef FEAT_WINDOWS
285static void ex_wincmd __ARGS((exarg_T *eap));
286#else
287# define ex_wincmd ex_ni
288#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000289#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290static void ex_winpos __ARGS((exarg_T *eap));
291#else
292# define ex_winpos ex_ni
293#endif
294static void ex_operators __ARGS((exarg_T *eap));
295static void ex_put __ARGS((exarg_T *eap));
296static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000297static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static void ex_submagic __ARGS((exarg_T *eap));
299static void ex_join __ARGS((exarg_T *eap));
300static void ex_at __ARGS((exarg_T *eap));
301static void ex_bang __ARGS((exarg_T *eap));
302static void ex_undo __ARGS((exarg_T *eap));
303static void ex_redo __ARGS((exarg_T *eap));
304static void ex_redir __ARGS((exarg_T *eap));
305static void ex_redraw __ARGS((exarg_T *eap));
306static void ex_redrawstatus __ARGS((exarg_T *eap));
307static void close_redir __ARGS((void));
308static void ex_mkrc __ARGS((exarg_T *eap));
309static void ex_mark __ARGS((exarg_T *eap));
310#ifdef FEAT_USR_CMDS
311static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000312static 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 +0000313#endif
314#ifdef FEAT_EX_EXTRA
315static void ex_normal __ARGS((exarg_T *eap));
316static void ex_startinsert __ARGS((exarg_T *eap));
317static void ex_stopinsert __ARGS((exarg_T *eap));
318#else
319# define ex_normal ex_ni
320# define ex_align ex_ni
321# define ex_retab ex_ni
322# define ex_startinsert ex_ni
323# define ex_stopinsert ex_ni
324# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000325# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#endif
327#ifdef FEAT_FIND_ID
328static void ex_checkpath __ARGS((exarg_T *eap));
329static void ex_findpat __ARGS((exarg_T *eap));
330#else
331# define ex_findpat ex_ni
332# define ex_checkpath ex_ni
333#endif
334#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
335static void ex_psearch __ARGS((exarg_T *eap));
336#else
337# define ex_psearch ex_ni
338#endif
339static void ex_tag __ARGS((exarg_T *eap));
340static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
341#ifndef FEAT_EVAL
342# define ex_scriptnames ex_ni
343# define ex_finish ex_ni
344# define ex_echo ex_ni
345# define ex_echohl ex_ni
346# define ex_execute ex_ni
347# define ex_call ex_ni
348# define ex_if ex_ni
349# define ex_endif ex_ni
350# define ex_else ex_ni
351# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000352# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353# define ex_continue ex_ni
354# define ex_break ex_ni
355# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000356# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357# define ex_throw ex_ni
358# define ex_try ex_ni
359# define ex_catch ex_ni
360# define ex_finally ex_ni
361# define ex_endtry ex_ni
362# define ex_endfunction ex_ni
363# define ex_let ex_ni
364# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000365# define ex_lockvar ex_ni
366# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367# define ex_function ex_ni
368# define ex_delfunction ex_ni
369# define ex_return ex_ni
370#endif
371static char_u *arg_all __ARGS((void));
372#ifdef FEAT_SESSION
373static int makeopens __ARGS((FILE *fd, char_u *dirnow));
374static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
375static void ex_loadview __ARGS((exarg_T *eap));
376static char_u *get_view_file __ARGS((int c));
377#else
378# define ex_loadview ex_ni
379#endif
380#ifndef FEAT_EVAL
381# define ex_compiler ex_ni
382#endif
383#ifdef FEAT_VIMINFO
384static void ex_viminfo __ARGS((exarg_T *eap));
385#else
386# define ex_viminfo ex_ni
387#endif
388static void ex_behave __ARGS((exarg_T *eap));
389#ifdef FEAT_AUTOCMD
390static void ex_filetype __ARGS((exarg_T *eap));
391static void ex_setfiletype __ARGS((exarg_T *eap));
392#else
393# define ex_filetype ex_ni
394# define ex_setfiletype ex_ni
395#endif
396#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000397# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398# define ex_diffpatch ex_ni
399# define ex_diffgetput ex_ni
400# define ex_diffsplit ex_ni
401# define ex_diffthis ex_ni
402# define ex_diffupdate ex_ni
403#endif
404static void ex_digraphs __ARGS((exarg_T *eap));
405static void ex_set __ARGS((exarg_T *eap));
406#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
407# define ex_options ex_ni
408#endif
409#ifdef FEAT_SEARCH_EXTRA
410static void ex_nohlsearch __ARGS((exarg_T *eap));
411static void ex_match __ARGS((exarg_T *eap));
412#else
413# define ex_nohlsearch ex_ni
414# define ex_match ex_ni
415#endif
416#ifdef FEAT_CRYPT
417static void ex_X __ARGS((exarg_T *eap));
418#else
419# define ex_X ex_ni
420#endif
421#ifdef FEAT_FOLDING
422static void ex_fold __ARGS((exarg_T *eap));
423static void ex_foldopen __ARGS((exarg_T *eap));
424static void ex_folddo __ARGS((exarg_T *eap));
425#else
426# define ex_fold ex_ni
427# define ex_foldopen ex_ni
428# define ex_folddo ex_ni
429#endif
430#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
431 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
432# define ex_language ex_ni
433#endif
434#ifndef FEAT_SIGNS
435# define ex_sign ex_ni
436#endif
437#ifndef FEAT_SUN_WORKSHOP
438# define ex_wsverb ex_ni
439#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000440#ifndef FEAT_NETBEANS_INTG
441# define ex_nbkey ex_ni
442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
444#ifndef FEAT_EVAL
445# define ex_debug ex_ni
446# define ex_breakadd ex_ni
447# define ex_debuggreedy ex_ni
448# define ex_breakdel ex_ni
449# define ex_breaklist ex_ni
450#endif
451
452#ifndef FEAT_CMDHIST
453# define ex_history ex_ni
454#endif
455#ifndef FEAT_JUMPLIST
456# define ex_jumps ex_ni
457# define ex_changes ex_ni
458#endif
459
Bram Moolenaar05159a02005-02-26 23:04:13 +0000460#ifndef FEAT_PROFILE
461# define ex_profile ex_ni
462#endif
463
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464/*
465 * Declare cmdnames[].
466 */
467#define DO_DECLARE_EXCMD
468#include "ex_cmds.h"
469
470/*
471 * Table used to quickly search for a command, based on its first character.
472 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000473static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
475 CMD_append,
476 CMD_buffer,
477 CMD_change,
478 CMD_delete,
479 CMD_edit,
480 CMD_file,
481 CMD_global,
482 CMD_help,
483 CMD_insert,
484 CMD_join,
485 CMD_k,
486 CMD_list,
487 CMD_move,
488 CMD_next,
489 CMD_open,
490 CMD_print,
491 CMD_quit,
492 CMD_read,
493 CMD_substitute,
494 CMD_t,
495 CMD_undo,
496 CMD_vglobal,
497 CMD_write,
498 CMD_xit,
499 CMD_yank,
500 CMD_z,
501 CMD_bang
502};
503
504static char_u dollar_command[2] = {'$', 0};
505
506
507#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000508/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509typedef struct
510{
511 char_u *line; /* command line */
512 linenr_T lnum; /* sourcing_lnum of the line */
513} wcmd_T;
514
515/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000516 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000518 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000520struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
522 garray_T *lines_gap; /* growarray with line info */
523 int current_line; /* last read line from growarray */
524 int repeating; /* TRUE when looping a second time */
525 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
526 char_u *(*getline) __ARGS((int, void *, int));
527 void *cookie;
528};
529
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000530static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
531static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000533
534/* Struct to save a few things while debugging. Used in do_cmdline() only. */
535struct dbg_stuff
536{
537 int trylevel;
538 int force_abort;
539 except_T *caught_stack;
540 char_u *vv_exception;
541 char_u *vv_throwpoint;
542 int did_emsg;
543 int got_int;
544 int did_throw;
545 int need_rethrow;
546 int check_cstack;
547 except_T *current_exception;
548};
549
550static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
551static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
552
553 static void
554save_dbg_stuff(dsp)
555 struct dbg_stuff *dsp;
556{
557 dsp->trylevel = trylevel; trylevel = 0;
558 dsp->force_abort = force_abort; force_abort = FALSE;
559 dsp->caught_stack = caught_stack; caught_stack = NULL;
560 dsp->vv_exception = v_exception(NULL);
561 dsp->vv_throwpoint = v_throwpoint(NULL);
562
563 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
564 dsp->did_emsg = did_emsg; did_emsg = FALSE;
565 dsp->got_int = got_int; got_int = FALSE;
566 dsp->did_throw = did_throw; did_throw = FALSE;
567 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
568 dsp->check_cstack = check_cstack; check_cstack = FALSE;
569 dsp->current_exception = current_exception; current_exception = NULL;
570}
571
572 static void
573restore_dbg_stuff(dsp)
574 struct dbg_stuff *dsp;
575{
576 suppress_errthrow = FALSE;
577 trylevel = dsp->trylevel;
578 force_abort = dsp->force_abort;
579 caught_stack = dsp->caught_stack;
580 (void)v_exception(dsp->vv_exception);
581 (void)v_throwpoint(dsp->vv_throwpoint);
582 did_emsg = dsp->did_emsg;
583 got_int = dsp->got_int;
584 did_throw = dsp->did_throw;
585 need_rethrow = dsp->need_rethrow;
586 check_cstack = dsp->check_cstack;
587 current_exception = dsp->current_exception;
588}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589#endif
590
591
592/*
593 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
594 * command is given.
595 */
596 void
597do_exmode(improved)
598 int improved; /* TRUE for "improved Ex" mode */
599{
600 int save_msg_scroll;
601 int prev_msg_row;
602 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000603 int changedtick;
604
605 if (improved)
606 exmode_active = EXMODE_VIM;
607 else
608 exmode_active = EXMODE_NORMAL;
609 State = NORMAL;
610
611 /* When using ":global /pat/ visual" and then "Q" we return to continue
612 * the :global command. */
613 if (global_busy)
614 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 save_msg_scroll = msg_scroll;
617 ++RedrawingDisabled; /* don't redisplay the window */
618 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#ifdef FEAT_GUI
620 /* Ignore scrollbar and mouse events in Ex mode */
621 ++hold_gui_events;
622#endif
623#ifdef FEAT_SNIFF
624 want_sniff_request = 0; /* No K_SNIFF wanted */
625#endif
626
627 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
628 while (exmode_active)
629 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000630#ifdef FEAT_EX_EXTRA
631 /* Check for a ":normal" command and no more characters left. */
632 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
633 {
634 exmode_active = FALSE;
635 break;
636 }
637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 msg_scroll = TRUE;
639 need_wait_return = FALSE;
640 ex_pressedreturn = FALSE;
641 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000642 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 prev_msg_row = msg_row;
644 prev_line = curwin->w_cursor.lnum;
645#ifdef FEAT_SNIFF
646 ProcessSniffRequests();
647#endif
648 if (improved)
649 {
650 cmdline_row = msg_row;
651 do_cmdline(NULL, getexline, NULL, 0);
652 }
653 else
654 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
655 lines_left = Rows - 1;
656
Bram Moolenaardf177f62005-02-22 08:39:57 +0000657 if ((prev_line != curwin->w_cursor.lnum
658 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000660 if (curbuf->b_ml.ml_flags & ML_EMPTY)
661 EMSG(_(e_emptybuf));
662 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000664 if (ex_pressedreturn)
665 {
666 /* go up one line, to overwrite the ":<CR>" line, so the
667 * output doensn't contain empty lines. */
668 msg_row = prev_msg_row;
669 if (prev_msg_row == Rows - 1)
670 msg_row--;
671 }
672 msg_col = 0;
673 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
674 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000677 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
678 {
679 if (curbuf->b_ml.ml_flags & ML_EMPTY)
680 EMSG(_(e_emptybuf));
681 else
682 EMSG(_("E501: At end-of-file"));
683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
685
686#ifdef FEAT_GUI
687 --hold_gui_events;
688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 --RedrawingDisabled;
690 --no_wait_return;
691 update_screen(CLEAR);
692 need_wait_return = FALSE;
693 msg_scroll = save_msg_scroll;
694}
695
696/*
697 * Execute a simple command line. Used for translated commands like "*".
698 */
699 int
700do_cmdline_cmd(cmd)
701 char_u *cmd;
702{
703 return do_cmdline(cmd, NULL, NULL,
704 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
705}
706
707/*
708 * do_cmdline(): execute one Ex command line
709 *
710 * 1. Execute "cmdline" when it is not NULL.
711 * If "cmdline" is NULL, or more lines are needed, getline() is used.
712 * 2. Split up in parts separated with '|'.
713 *
714 * This function can be called recursively!
715 *
716 * flags:
717 * DOCMD_VERBOSE - The command will be included in the error message.
718 * DOCMD_NOWAIT - Don't call wait_return() and friends.
719 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
720 * DOCMD_KEYTYPED - Don't reset KeyTyped.
721 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
722 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
723 *
724 * return FAIL if cmdline could not be executed, OK otherwise
725 */
726 int
727do_cmdline(cmdline, getline, cookie, flags)
728 char_u *cmdline;
729 char_u *(*getline) __ARGS((int, void *, int));
730 void *cookie; /* argument for getline() */
731 int flags;
732{
733 char_u *next_cmdline; /* next cmd to execute */
734 char_u *cmdline_copy = NULL; /* copy of cmd line */
735 int used_getline = FALSE; /* used "getline" to obtain command */
736 static int recursive = 0; /* recursive depth */
737 int msg_didout_before_start = 0;
738 int count = 0; /* line number count */
739 int did_inc = FALSE; /* incremented RedrawingDisabled */
740 int retval = OK;
741#ifdef FEAT_EVAL
742 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 int current_line = 0; /* active line in lines_ga */
745 char_u *fname = NULL; /* function or script name */
746 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
747 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000748 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 int initial_trylevel;
750 struct msglist **saved_msg_list = NULL;
751 struct msglist *private_msg_list;
752
753 /* "getline" and "cookie" passed to do_one_cmd() */
754 char_u *(*cmd_getline) __ARGS((int, void *, int));
755 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000756 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000758 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759#else
760# define cmd_getline getline
761# define cmd_cookie cookie
762#endif
763 static int call_depth = 0; /* recursiveness */
764
765#ifdef FEAT_EVAL
766 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
767 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000768 * This ensures that the do_errthrow() call in do_one_cmd() does not
769 * combine the messages stored by an earlier invocation of do_one_cmd()
770 * with the command name of the later one. This would happen when
771 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 saved_msg_list = msg_list;
773 msg_list = &private_msg_list;
774 private_msg_list = NULL;
775#endif
776
777 /* It's possible to create an endless loop with ":execute", catch that
778 * here. The value of 200 allows nested function calls, ":source", etc. */
779 if (call_depth == 200)
780 {
781 EMSG(_("E169: Command too recursive"));
782#ifdef FEAT_EVAL
783 /* When converting to an exception, we do not include the command name
784 * since this is not an error of the specific command. */
785 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
786 msg_list = saved_msg_list;
787#endif
788 return FAIL;
789 }
790 ++call_depth;
791
792#ifdef FEAT_EVAL
793 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000794 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 cstack.cs_trylevel = 0;
796 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000797 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
799
800 real_cookie = getline_cookie(getline, cookie);
801
802 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803 getline_is_func = getline_equal(getline, cookie, get_func_line);
804 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 ++ex_nesting_level;
806
807 /* Get the function or script name and the address where the next breakpoint
808 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
811 fname = func_name(real_cookie);
812 breakpoint = func_breakpoint(real_cookie);
813 dbg_tick = func_dbg_tick(real_cookie);
814 }
815 else if (getline_equal(getline, cookie, getsourceline))
816 {
817 fname = sourcing_name;
818 breakpoint = source_breakpoint(real_cookie);
819 dbg_tick = source_dbg_tick(real_cookie);
820 }
821
822 /*
823 * Initialize "force_abort" and "suppress_errthrow" at the top level.
824 */
825 if (!recursive)
826 {
827 force_abort = FALSE;
828 suppress_errthrow = FALSE;
829 }
830
831 /*
832 * If requested, store and reset the global values controlling the
833 * exception handling (used when debugging).
834 */
835 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000836 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
838 initial_trylevel = trylevel;
839
840 /*
841 * "did_throw" will be set to TRUE when an exception is being thrown.
842 */
843 did_throw = FALSE;
844#endif
845 /*
846 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000847 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 * If force_abort is set, we cancel everything.
849 */
850 did_emsg = FALSE;
851
852 /*
853 * KeyTyped is only set when calling vgetc(). Reset it here when not
854 * calling vgetc() (sourced command lines).
855 */
856 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
857 KeyTyped = FALSE;
858
859 /*
860 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000861 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * - for multiple commands on one line, separated with '|'
863 * - when repeating until there are no more lines (for ":source")
864 */
865 next_cmdline = cmdline;
866 do
867 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868#ifdef FEAT_EVAL
869 getline_is_func = getline_equal(getline, cookie, get_func_line);
870#endif
871
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000872 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (next_cmdline == NULL
874#ifdef FEAT_EVAL
875 && !force_abort
876 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000877 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#endif
879 )
880 did_emsg = FALSE;
881
882 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000883 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 * 2. If no line given: Get an allocated line with getline().
885 * 3. If a line is given: Make a copy, so we can mess with it.
886 */
887
888#ifdef FEAT_EVAL
889 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000890 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
892 /* Each '|' separated command is stored separately in lines_ga, to
893 * be able to jump to it. Don't use next_cmdline now. */
894 vim_free(cmdline_copy);
895 cmdline_copy = NULL;
896
897 /* Check if a function has returned or, unless it has an unclosed
898 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000899 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000901# ifdef FEAT_PROFILE
902 if (do_profiling)
903 func_line_end(real_cookie);
904# endif
905 if (func_has_ended(real_cookie))
906 {
907 retval = FAIL;
908 break;
909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000911#ifdef FEAT_PROFILE
912 else if (do_profiling
913 && getline_equal(getline, cookie, getsourceline))
914 script_line_end();
915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 /* Check if a sourced file hit a ":finish" command. */
918 if (source_finished(getline, cookie))
919 {
920 retval = FAIL;
921 break;
922 }
923
924 /* If breakpoints have been added/deleted need to check for it. */
925 if (breakpoint != NULL && dbg_tick != NULL
926 && *dbg_tick != debug_tick)
927 {
928 *breakpoint = dbg_find_breakpoint(
929 getline_equal(getline, cookie, getsourceline),
930 fname, sourcing_lnum);
931 *dbg_tick = debug_tick;
932 }
933
934 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
935 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
936
937 /* Did we encounter a breakpoint? */
938 if (breakpoint != NULL && *breakpoint != 0
939 && *breakpoint <= sourcing_lnum)
940 {
941 dbg_breakpoint(fname, sourcing_lnum);
942 /* Find next breakpoint. */
943 *breakpoint = dbg_find_breakpoint(
944 getline_equal(getline, cookie, getsourceline),
945 fname, sourcing_lnum);
946 *dbg_tick = debug_tick;
947 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000948# ifdef FEAT_PROFILE
949 if (do_profiling)
950 {
951 if (getline_is_func)
952 func_line_start(real_cookie);
953 else if (getline_equal(getline, cookie, getsourceline))
954 script_line_start();
955 }
956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 }
958
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000959 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000961 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 * again. Pass a different "getline" function to do_one_cmd()
963 * below, so that it stores lines in or reads them from
964 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000965 * while/for loop. */
966 cmd_getline = get_loop_line;
967 cmd_cookie = (void *)&cmd_loop_cookie;
968 cmd_loop_cookie.lines_gap = &lines_ga;
969 cmd_loop_cookie.current_line = current_line;
970 cmd_loop_cookie.getline = getline;
971 cmd_loop_cookie.cookie = cookie;
972 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974 else
975 {
976 cmd_getline = getline;
977 cmd_cookie = cookie;
978 }
979#endif
980
981 /* 2. If no line given, get an allocated line with getline(). */
982 if (next_cmdline == NULL)
983 {
984 /*
985 * Need to set msg_didout for the first line after an ":if",
986 * otherwise the ":if" will be overwritten.
987 */
988 if (count == 1 && getline_equal(getline, cookie, getexline))
989 msg_didout = TRUE;
990 if (getline == NULL || (next_cmdline = getline(':', cookie,
991#ifdef FEAT_EVAL
992 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
993#else
994 0
995#endif
996 )) == NULL)
997 {
998 /* Don't call wait_return for aborted command line. The NULL
999 * returned for the end of a sourced file or executed function
1000 * doesn't do this. */
1001 if (KeyTyped && !(flags & DOCMD_REPEAT))
1002 need_wait_return = FALSE;
1003 retval = FAIL;
1004 break;
1005 }
1006 used_getline = TRUE;
1007
1008 /*
1009 * Keep the first typed line. Clear it when more lines are typed.
1010 */
1011 if (flags & DOCMD_KEEPLINE)
1012 {
1013 vim_free(repeat_cmdline);
1014 if (count == 0)
1015 repeat_cmdline = vim_strsave(next_cmdline);
1016 else
1017 repeat_cmdline = NULL;
1018 }
1019 }
1020
1021 /* 3. Make a copy of the command so we can mess with it. */
1022 else if (cmdline_copy == NULL)
1023 {
1024 next_cmdline = vim_strsave(next_cmdline);
1025 if (next_cmdline == NULL)
1026 {
1027 EMSG(_(e_outofmem));
1028 retval = FAIL;
1029 break;
1030 }
1031 }
1032 cmdline_copy = next_cmdline;
1033
1034#ifdef FEAT_EVAL
1035 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 * Save the current line when inside a ":while" or ":for", and when
1037 * the command looks like a ":while" or ":for", because we may need it
1038 * later. When there is a '|' and another command, it is stored
1039 * separately, because we need to be able to jump back to it from an
1040 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (current_line == lines_ga.ga_len
1043 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001045 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
1047 retval = FAIL;
1048 break;
1049 }
1050 }
1051 did_endif = FALSE;
1052#endif
1053
1054 if (count++ == 0)
1055 {
1056 /*
1057 * All output from the commands is put below each other, without
1058 * waiting for a return. Don't do this when executing commands
1059 * from a script or when being called recursive (e.g. for ":e
1060 * +command file").
1061 */
1062 if (!(flags & DOCMD_NOWAIT) && !recursive)
1063 {
1064 msg_didout_before_start = msg_didout;
1065 msg_didany = FALSE; /* no output yet */
1066 msg_start();
1067 msg_scroll = TRUE; /* put messages below each other */
1068 ++no_wait_return; /* dont wait for return until finished */
1069 ++RedrawingDisabled;
1070 did_inc = TRUE;
1071 }
1072 }
1073
1074 if (p_verbose >= 15 && sourcing_name != NULL)
1075 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001077 verbose_enter_scroll();
1078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 smsg((char_u *)_("line %ld: %s"),
1080 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001081 if (msg_silent == 0)
1082 msg_puts((char_u *)"\n"); /* don't overwrite this */
1083
1084 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 --no_wait_return;
1086 }
1087
1088 /*
1089 * 2. Execute one '|' separated command.
1090 * do_one_cmd() will return NULL if there is no trailing '|'.
1091 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1092 */
1093 ++recursive;
1094 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1095#ifdef FEAT_EVAL
1096 &cstack,
1097#endif
1098 cmd_getline, cmd_cookie);
1099 --recursive;
1100
1101#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 if (cmd_cookie == (void *)&cmd_loop_cookie)
1103 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001105 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106#endif
1107
1108 if (next_cmdline == NULL)
1109 {
1110 vim_free(cmdline_copy);
1111 cmdline_copy = NULL;
1112#ifdef FEAT_CMDHIST
1113 /*
1114 * If the command was typed, remember it for the ':' register.
1115 * Do this AFTER executing the command to make :@: work.
1116 */
1117 if (getline_equal(getline, cookie, getexline)
1118 && new_last_cmdline != NULL)
1119 {
1120 vim_free(last_cmdline);
1121 last_cmdline = new_last_cmdline;
1122 new_last_cmdline = NULL;
1123 }
1124#endif
1125 }
1126 else
1127 {
1128 /* need to copy the command after the '|' to cmdline_copy, for the
1129 * next do_one_cmd() */
1130 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1131 next_cmdline = cmdline_copy;
1132 }
1133
1134
1135#ifdef FEAT_EVAL
1136 /* reset did_emsg for a function that is not aborted by an error */
1137 if (did_emsg && !force_abort
1138 && getline_equal(getline, cookie, get_func_line)
1139 && !func_has_abort(real_cookie))
1140 did_emsg = FALSE;
1141
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001142 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
1144 ++current_line;
1145
1146 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 * An ":endwhile", ":endfor" and ":continue" is handled here.
1148 * If we were executing commands, jump back to the ":while" or
1149 * ":for".
1150 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001152 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001154 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001156 /* Jump back to the matching ":while" or ":for". Be careful
1157 * not to use a cs_line[] from an entry that isn't a ":while"
1158 * or ":for": It would make "current_line" invalid and can
1159 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (!did_emsg && !got_int && !did_throw
1161 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 && (cstack.cs_flags[cstack.cs_idx]
1163 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 && cstack.cs_line[cstack.cs_idx] >= 0
1165 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1166 {
1167 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 /* remember we jumped there */
1169 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 line_breakcheck(); /* check if CTRL-C typed */
1171
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 /* Check for the next breakpoint at or after the ":while"
1173 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (breakpoint != NULL)
1175 {
1176 *breakpoint = dbg_find_breakpoint(
1177 getline_equal(getline, cookie, getsourceline),
1178 fname,
1179 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1180 *dbg_tick = debug_tick;
1181 }
1182 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001187 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1188 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 }
1191
1192 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001193 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1199 }
1200 }
1201
1202 /*
1203 * When not inside any ":while" loop, clear remembered lines.
1204 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
1207 if (lines_ga.ga_len > 0)
1208 {
1209 sourcing_lnum =
1210 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1211 free_cmdlines(&lines_ga);
1212 }
1213 current_line = 0;
1214 }
1215
1216 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1218 * being restored at the ":endtry". Reset them here and set the
1219 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1220 * This includes the case where a missing ":endif", ":endwhile" or
1221 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 cstack.cs_lflags &= ~CSL_HAD_FINA;
1226 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1227 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 did_throw ? (void *)current_exception : NULL);
1229 did_emsg = got_int = did_throw = FALSE;
1230 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1231 }
1232
1233 /* Update global "trylevel" for recursive calls to do_cmdline() from
1234 * within this loop. */
1235 trylevel = initial_trylevel + cstack.cs_trylevel;
1236
1237 /*
1238 * If the outermost try conditional (accross function calls and sourced
1239 * files) is aborted because of an error, an interrupt, or an uncaught
1240 * exception, cancel everything. If it is left normally, reset
1241 * force_abort to get the non-EH compatible abortion behavior for
1242 * the rest of the script.
1243 */
1244 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1245 force_abort = FALSE;
1246
1247 /* Convert an interrupt to an exception if appropriate. */
1248 (void)do_intthrow(&cstack);
1249#endif /* FEAT_EVAL */
1250
1251 }
1252 /*
1253 * Continue executing command lines when:
1254 * - no CTRL-C typed, no aborting error, no exception thrown or try
1255 * conditionals need to be checked for executing finally clauses or
1256 * catching an interrupt exception
1257 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001258 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 * looping for ":source" command or function call.
1260 */
1261 while (!((got_int
1262#ifdef FEAT_EVAL
1263 || (did_emsg && force_abort) || did_throw
1264#endif
1265 )
1266#ifdef FEAT_EVAL
1267 && cstack.cs_trylevel == 0
1268#endif
1269 )
1270 && !(did_emsg && used_getline
1271 && (getline_equal(getline, cookie, getexmodeline)
1272 || getline_equal(getline, cookie, getexline)))
1273 && (next_cmdline != NULL
1274#ifdef FEAT_EVAL
1275 || cstack.cs_idx >= 0
1276#endif
1277 || (flags & DOCMD_REPEAT)));
1278
1279 vim_free(cmdline_copy);
1280#ifdef FEAT_EVAL
1281 free_cmdlines(&lines_ga);
1282 ga_clear(&lines_ga);
1283
1284 if (cstack.cs_idx >= 0)
1285 {
1286 /*
1287 * If a sourced file or executed function ran to its end, report the
1288 * unclosed conditional.
1289 */
1290 if (!got_int && !did_throw
1291 && ((getline_equal(getline, cookie, getsourceline)
1292 && !source_finished(getline, cookie))
1293 || (getline_equal(getline, cookie, get_func_line)
1294 && !func_has_ended(real_cookie))))
1295 {
1296 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1297 EMSG(_(e_endtry));
1298 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1299 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001300 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1301 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 else
1303 EMSG(_(e_endif));
1304 }
1305
1306 /*
1307 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1308 * ":endtry" in a sourced file or executed function. If the try
1309 * conditional is in its finally clause, ignore anything pending.
1310 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001311 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 */
1313 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001314 {
1315 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1316
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001317 if (idx >= 0)
1318 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001319 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1320 &cstack.cs_looplevel);
1321 }
1322 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 trylevel = initial_trylevel;
1324 }
1325
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001326 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1327 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * exception, do this now after rewinding the cstack. */
1329 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1330 ? (char_u *)"endfunction" : (char_u *)NULL);
1331
1332 if (trylevel == 0)
1333 {
1334 /*
1335 * When an exception is being thrown out of the outermost try
1336 * conditional, discard the uncaught exception, disable the conversion
1337 * of interrupts or errors to exceptions, and ensure that no more
1338 * commands are executed.
1339 */
1340 if (did_throw)
1341 {
1342 void *p = NULL;
1343 char_u *saved_sourcing_name;
1344 int saved_sourcing_lnum;
1345 struct msglist *messages = NULL, *next;
1346
1347 /*
1348 * If the uncaught exception is a user exception, report it as an
1349 * error. If it is an error exception, display the saved error
1350 * message now. For an interrupt exception, do nothing; the
1351 * interrupt message is given elsewhere.
1352 */
1353 switch (current_exception->type)
1354 {
1355 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001356 vim_snprintf((char *)IObuff, IOSIZE,
1357 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 current_exception->value);
1359 p = vim_strsave(IObuff);
1360 break;
1361 case ET_ERROR:
1362 messages = current_exception->messages;
1363 current_exception->messages = NULL;
1364 break;
1365 case ET_INTERRUPT:
1366 break;
1367 default:
1368 p = vim_strsave((char_u *)_(e_internal));
1369 }
1370
1371 saved_sourcing_name = sourcing_name;
1372 saved_sourcing_lnum = sourcing_lnum;
1373 sourcing_name = current_exception->throw_name;
1374 sourcing_lnum = current_exception->throw_lnum;
1375 current_exception->throw_name = NULL;
1376
1377 discard_current_exception(); /* uses IObuff if 'verbose' */
1378 suppress_errthrow = TRUE;
1379 force_abort = TRUE;
1380
1381 if (messages != NULL)
1382 {
1383 do
1384 {
1385 next = messages->next;
1386 emsg(messages->msg);
1387 vim_free(messages->msg);
1388 vim_free(messages);
1389 messages = next;
1390 }
1391 while (messages != NULL);
1392 }
1393 else if (p != NULL)
1394 {
1395 emsg(p);
1396 vim_free(p);
1397 }
1398 vim_free(sourcing_name);
1399 sourcing_name = saved_sourcing_name;
1400 sourcing_lnum = saved_sourcing_lnum;
1401 }
1402
1403 /*
1404 * On an interrupt or an aborting error not converted to an exception,
1405 * disable the conversion of errors to exceptions. (Interrupts are not
1406 * converted any more, here.) This enables also the interrupt message
1407 * when force_abort is set and did_emsg unset in case of an interrupt
1408 * from a finally clause after an error.
1409 */
1410 else if (got_int || (did_emsg && force_abort))
1411 suppress_errthrow = TRUE;
1412 }
1413
1414 /*
1415 * The current cstack will be freed when do_cmdline() returns. An uncaught
1416 * exception will have to be rethrown in the previous cstack. If a function
1417 * has just returned or a script file was just finished and the previous
1418 * cstack belongs to the same function or, respectively, script file, it
1419 * will have to be checked for finally clauses to be executed due to the
1420 * ":return" or ":finish". This is done in do_one_cmd().
1421 */
1422 if (did_throw)
1423 need_rethrow = TRUE;
1424 if ((getline_equal(getline, cookie, getsourceline)
1425 && ex_nesting_level > source_level(real_cookie))
1426 || (getline_equal(getline, cookie, get_func_line)
1427 && ex_nesting_level > func_level(real_cookie) + 1))
1428 {
1429 if (!did_throw)
1430 check_cstack = TRUE;
1431 }
1432 else
1433 {
1434 /* When leaving a function, reduce nesting level. */
1435 if (getline_equal(getline, cookie, get_func_line))
1436 --ex_nesting_level;
1437 /*
1438 * Go to debug mode when returning from a function in which we are
1439 * single-stepping.
1440 */
1441 if ((getline_equal(getline, cookie, getsourceline)
1442 || getline_equal(getline, cookie, get_func_line))
1443 && ex_nesting_level + 1 <= debug_break_level)
1444 do_debug(getline_equal(getline, cookie, getsourceline)
1445 ? (char_u *)_("End of sourced file")
1446 : (char_u *)_("End of function"));
1447 }
1448
1449 /*
1450 * Restore the exception environment (done after returning from the
1451 * debugger).
1452 */
1453 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001454 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
1456 msg_list = saved_msg_list;
1457#endif /* FEAT_EVAL */
1458
1459 /*
1460 * If there was too much output to fit on the command line, ask the user to
1461 * hit return before redrawing the screen. With the ":global" command we do
1462 * this only once after the command is finished.
1463 */
1464 if (did_inc)
1465 {
1466 --RedrawingDisabled;
1467 --no_wait_return;
1468 msg_scroll = FALSE;
1469
1470 /*
1471 * When just finished an ":if"-":else" which was typed, no need to
1472 * wait for hit-return. Also for an error situation.
1473 */
1474 if (retval == FAIL
1475#ifdef FEAT_EVAL
1476 || (did_endif && KeyTyped && !did_emsg)
1477#endif
1478 )
1479 {
1480 need_wait_return = FALSE;
1481 msg_didany = FALSE; /* don't wait when restarting edit */
1482 }
1483 else if (need_wait_return)
1484 {
1485 /*
1486 * The msg_start() above clears msg_didout. The wait_return we do
1487 * here should not overwrite the command that may be shown before
1488 * doing that.
1489 */
1490 msg_didout |= msg_didout_before_start;
1491 wait_return(FALSE);
1492 }
1493 }
1494
1495#ifndef FEAT_EVAL
1496 /*
1497 * Reset if_level, in case a sourced script file contains more ":if" than
1498 * ":endif" (could be ":if x | foo | endif").
1499 */
1500 if_level = 0;
1501#endif
1502
1503 --call_depth;
1504 return retval;
1505}
1506
1507#ifdef FEAT_EVAL
1508/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001512get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 int c;
1514 void *cookie;
1515 int indent;
1516{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001517 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 wcmd_T *wp;
1519 char_u *line;
1520
1521 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1522 {
1523 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (cp->getline == NULL)
1528 line = getcmdline(c, 0L, indent);
1529 else
1530 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 ++cp->current_line;
1533
1534 return line;
1535 }
1536
1537 KeyTyped = FALSE;
1538 ++cp->current_line;
1539 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1540 sourcing_lnum = wp->lnum;
1541 return vim_strsave(wp->line);
1542}
1543
1544/*
1545 * Store a line in "gap" so that a ":while" loop can execute it again.
1546 */
1547 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 garray_T *gap;
1550 char_u *line;
1551{
1552 if (ga_grow(gap, 1) == FAIL)
1553 return FAIL;
1554 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1555 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1556 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 return OK;
1558}
1559
1560/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001561 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
1563 static void
1564free_cmdlines(gap)
1565 garray_T *gap;
1566{
1567 while (gap->ga_len > 0)
1568 {
1569 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1570 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572}
1573#endif
1574
1575/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001576 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 * "func". * Otherwise return TRUE when "getline" equals "func".
1578 */
1579/*ARGSUSED*/
1580 int
1581getline_equal(getline, cookie, func)
1582 char_u *(*getline) __ARGS((int, void *, int));
1583 void *cookie; /* argument for getline() */
1584 char_u *(*func) __ARGS((int, void *, int));
1585{
1586#ifdef FEAT_EVAL
1587 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001588 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 * function that's orignally used to obtain the lines. This may be nested
1592 * several levels. */
1593 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001594 cp = (struct loop_cookie *)cookie;
1595 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 gp = cp->getline;
1598 cp = cp->cookie;
1599 }
1600 return gp == func;
1601#else
1602 return getline == func;
1603#endif
1604}
1605
1606#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1607/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001608 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 * getline function. Otherwise return "cookie".
1610 */
1611/*ARGSUSED*/
1612 void *
1613getline_cookie(getline, cookie)
1614 char_u *(*getline) __ARGS((int, void *, int));
1615 void *cookie; /* argument for getline() */
1616{
1617# ifdef FEAT_EVAL
1618 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001619 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 * cookie that's orignally used to obtain the lines. This may be nested
1623 * several levels. */
1624 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001625 cp = (struct loop_cookie *)cookie;
1626 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628 gp = cp->getline;
1629 cp = cp->cookie;
1630 }
1631 return cp;
1632# else
1633 return cookie;
1634# endif
1635}
1636#endif
1637
1638/*
1639 * Execute one Ex command.
1640 *
1641 * If 'sourcing' is TRUE, the command will be included in the error message.
1642 *
1643 * 1. skip comment lines and leading space
1644 * 2. handle command modifiers
1645 * 3. parse range
1646 * 4. parse command
1647 * 5. parse arguments
1648 * 6. switch on command name
1649 *
1650 * Note: "getline" can be NULL.
1651 *
1652 * This function may be called recursively!
1653 */
1654#if (_MSC_VER == 1200)
1655/*
Bram Moolenaared203462004-06-16 11:19:22 +00001656 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001658 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659#endif
1660 static char_u *
1661do_one_cmd(cmdlinep, sourcing,
1662#ifdef FEAT_EVAL
1663 cstack,
1664#endif
1665 getline, cookie)
1666 char_u **cmdlinep;
1667 int sourcing;
1668#ifdef FEAT_EVAL
1669 struct condstack *cstack;
1670#endif
1671 char_u *(*getline) __ARGS((int, void *, int));
1672 void *cookie; /* argument for getline() */
1673{
1674 char_u *p;
1675 linenr_T lnum;
1676 long n;
1677 char_u *errormsg = NULL; /* error message */
1678 exarg_T ea; /* Ex command arguments */
1679 long verbose_save = -1;
1680 int save_msg_scroll = 0;
1681 int did_silent = 0;
1682 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001683#ifdef HAVE_SANDBOX
1684 int did_sandbox = FALSE;
1685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 cmdmod_T save_cmdmod;
1687 int ni; /* set when Not Implemented */
1688
1689 vim_memset(&ea, 0, sizeof(ea));
1690 ea.line1 = 1;
1691 ea.line2 = 1;
1692#ifdef FEAT_EVAL
1693 ++ex_nesting_level;
1694#endif
1695
1696 /* when not editing the last file :q has to be typed twice */
1697 if (quitmore
1698#ifdef FEAT_EVAL
1699 /* avoid that a function call in 'statusline' does this */
1700 && !getline_equal(getline, cookie, get_func_line)
1701#endif
1702 )
1703 --quitmore;
1704
1705 /*
1706 * Reset browse, confirm, etc.. They are restored when returning, for
1707 * recursive calls.
1708 */
1709 save_cmdmod = cmdmod;
1710 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1711
1712 /*
1713 * Repeat until no more command modifiers are found.
1714 */
1715 ea.cmd = *cmdlinep;
1716 for (;;)
1717 {
1718/*
1719 * 1. skip comment lines and leading white space and colons
1720 */
1721 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1722 ++ea.cmd;
1723
1724 /* in ex mode, an empty line works like :+ */
1725 if (*ea.cmd == NUL && exmode_active
1726 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001727 || getline_equal(getline, cookie, getexline))
1728 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 {
1730 ea.cmd = (char_u *)"+";
1731 ex_pressedreturn = TRUE;
1732 }
1733
1734 /* ignore comment and empty lines */
1735 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001736 {
1737 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
1741/*
1742 * 2. handle command modifiers.
1743 */
1744 p = ea.cmd;
1745 if (VIM_ISDIGIT(*ea.cmd))
1746 p = skipwhite(skipdigits(ea.cmd));
1747 switch (*p)
1748 {
1749 /* When adding an entry, also modify cmd_exists(). */
1750 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1751 break;
1752#ifdef FEAT_WINDOWS
1753 cmdmod.split |= WSP_ABOVE;
1754#endif
1755 continue;
1756
1757 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1758 {
1759#ifdef FEAT_WINDOWS
1760 cmdmod.split |= WSP_BELOW;
1761#endif
1762 continue;
1763 }
1764 if (checkforcmd(&ea.cmd, "browse", 3))
1765 {
1766#ifdef FEAT_BROWSE
1767 cmdmod.browse = TRUE;
1768#endif
1769 continue;
1770 }
1771 if (!checkforcmd(&ea.cmd, "botright", 2))
1772 break;
1773#ifdef FEAT_WINDOWS
1774 cmdmod.split |= WSP_BOT;
1775#endif
1776 continue;
1777
1778 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1779 break;
1780#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1781 cmdmod.confirm = TRUE;
1782#endif
1783 continue;
1784
1785 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1786 {
1787 cmdmod.keepmarks = TRUE;
1788 continue;
1789 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001790 if (checkforcmd(&ea.cmd, "keepalt", 5))
1791 {
1792 cmdmod.keepalt = TRUE;
1793 continue;
1794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1796 break;
1797 cmdmod.keepjumps = TRUE;
1798 continue;
1799
1800 /* ":hide" and ":hide | cmd" are not modifiers */
1801 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1802 || *p == NUL || ends_excmd(*p))
1803 break;
1804 ea.cmd = p;
1805 cmdmod.hide = TRUE;
1806 continue;
1807
1808 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1809 {
1810 cmdmod.lockmarks = TRUE;
1811 continue;
1812 }
1813
1814 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1815 break;
1816#ifdef FEAT_WINDOWS
1817 cmdmod.split |= WSP_ABOVE;
1818#endif
1819 continue;
1820
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001821 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1822 break;
1823#ifdef FEAT_AUTOCMD
1824 if (cmdmod.save_ei == NULL)
1825 {
1826 /* Set 'eventignore' to "all". Don't free the
1827 * existing option value, we restore it later. */
1828 cmdmod.save_ei = vim_strsave(p_ei);
1829 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001830 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001831 }
1832#endif
1833 continue;
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1836 break;
1837#ifdef FEAT_WINDOWS
1838 cmdmod.split |= WSP_BELOW;
1839#endif
1840 continue;
1841
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001842 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1843 {
1844#ifdef HAVE_SANDBOX
1845 if (!did_sandbox)
1846 ++sandbox;
1847 did_sandbox = TRUE;
1848#endif
1849 continue;
1850 }
1851 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 break;
1853 ++did_silent;
1854 ++msg_silent;
1855 save_msg_scroll = msg_scroll;
1856 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1857 {
1858 /* ":silent!", but not "silent !cmd" */
1859 ea.cmd = skipwhite(ea.cmd + 1);
1860 ++emsg_silent;
1861 ++did_esilent;
1862 }
1863 continue;
1864
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001865 case 't': if (checkforcmd(&p, "tab", 3))
1866 {
1867#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001868 if (vim_isdigit(*ea.cmd))
1869 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1870 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001871 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001872 ea.cmd = p;
1873#endif
1874 continue;
1875 }
1876 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 break;
1878#ifdef FEAT_WINDOWS
1879 cmdmod.split |= WSP_TOP;
1880#endif
1881 continue;
1882
1883 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1884 {
1885#ifdef FEAT_VERTSPLIT
1886 cmdmod.split |= WSP_VERT;
1887#endif
1888 continue;
1889 }
1890 if (!checkforcmd(&p, "verbose", 4))
1891 break;
1892 if (verbose_save < 0)
1893 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001894 if (vim_isdigit(*ea.cmd))
1895 p_verbose = atoi((char *)ea.cmd);
1896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 p_verbose = 1;
1898 ea.cmd = p;
1899 continue;
1900 }
1901 break;
1902 }
1903
1904#ifdef FEAT_EVAL
1905 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1906 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1907#else
1908 ea.skip = (if_level > 0);
1909#endif
1910
1911#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001912# ifdef FEAT_PROFILE
1913 /* Count this line for profiling if ea.skip is FALSE. */
1914 if (do_profiling && !ea.skip)
1915 {
1916 if (getline_equal(getline, cookie, get_func_line))
1917 func_line_exec(getline_cookie(getline, cookie));
1918 else if (getline_equal(getline, cookie, getsourceline))
1919 script_line_exec();
1920 }
1921#endif
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 /* May go to debug mode. If this happens and the ">quit" debug command is
1924 * used, throw an interrupt exception and skip the next command. */
1925 dbg_check_breakpoint(&ea);
1926 if (!ea.skip && got_int)
1927 {
1928 ea.skip = TRUE;
1929 (void)do_intthrow(cstack);
1930 }
1931#endif
1932
1933/*
1934 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1935 *
1936 * where 'addr' is:
1937 *
1938 * % (entire file)
1939 * $ [+-NUM]
1940 * 'x [+-NUM] (where x denotes a currently defined mark)
1941 * . [+-NUM]
1942 * [+-NUM]..
1943 * NUM
1944 *
1945 * The ea.cmd pointer is updated to point to the first character following the
1946 * range spec. If an initial address is found, but no second, the upper bound
1947 * is equal to the lower.
1948 */
1949
1950 /* repeat for all ',' or ';' separated addresses */
1951 for (;;)
1952 {
1953 ea.line1 = ea.line2;
1954 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1955 ea.cmd = skipwhite(ea.cmd);
1956 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1957 if (ea.cmd == NULL) /* error detected */
1958 goto doend;
1959 if (lnum == MAXLNUM)
1960 {
1961 if (*ea.cmd == '%') /* '%' - all lines */
1962 {
1963 ++ea.cmd;
1964 ea.line1 = 1;
1965 ea.line2 = curbuf->b_ml.ml_line_count;
1966 ++ea.addr_count;
1967 }
1968 /* '*' - visual area */
1969 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1970 {
1971 pos_T *fp;
1972
1973 ++ea.cmd;
1974 if (!ea.skip)
1975 {
1976 fp = getmark('<', FALSE);
1977 if (check_mark(fp) == FAIL)
1978 goto doend;
1979 ea.line1 = fp->lnum;
1980 fp = getmark('>', FALSE);
1981 if (check_mark(fp) == FAIL)
1982 goto doend;
1983 ea.line2 = fp->lnum;
1984 ++ea.addr_count;
1985 }
1986 }
1987 }
1988 else
1989 ea.line2 = lnum;
1990 ea.addr_count++;
1991
1992 if (*ea.cmd == ';')
1993 {
1994 if (!ea.skip)
1995 curwin->w_cursor.lnum = ea.line2;
1996 }
1997 else if (*ea.cmd != ',')
1998 break;
1999 ++ea.cmd;
2000 }
2001
2002 /* One address given: set start and end lines */
2003 if (ea.addr_count == 1)
2004 {
2005 ea.line1 = ea.line2;
2006 /* ... but only implicit: really no address given */
2007 if (lnum == MAXLNUM)
2008 ea.addr_count = 0;
2009 }
2010
2011 /* Don't leave the cursor on an illegal line (caused by ';') */
2012 check_cursor_lnum();
2013
2014/*
2015 * 4. parse command
2016 */
2017
2018 /*
2019 * Skip ':' and any white space
2020 */
2021 ea.cmd = skipwhite(ea.cmd);
2022 while (*ea.cmd == ':')
2023 ea.cmd = skipwhite(ea.cmd + 1);
2024
2025 /*
2026 * If we got a line, but no command, then go to the line.
2027 * If we find a '|' or '\n' we set ea.nextcmd.
2028 */
2029 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2030 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2031 {
2032 /*
2033 * strange vi behaviour:
2034 * ":3" jumps to line 3
2035 * ":3|..." prints line 3
2036 * ":|" prints current line
2037 */
2038 if (ea.skip) /* skip this if inside :if */
2039 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002040 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 ea.cmdidx = CMD_print;
2043 ea.argt = RANGE+COUNT+TRLBAR;
2044 if ((errormsg = invalid_range(&ea)) == NULL)
2045 {
2046 correct_range(&ea);
2047 ex_print(&ea);
2048 }
2049 }
2050 else if (ea.addr_count != 0)
2051 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002052 if (ea.line2 > curbuf->b_ml.ml_line_count)
2053 {
2054 /* With '-' in 'cpoptions' a line number past the file is an
2055 * error, otherwise put it at the end of the file. */
2056 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2057 ea.line2 = -1;
2058 else
2059 ea.line2 = curbuf->b_ml.ml_line_count;
2060 }
2061
2062 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002063 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 else
2065 {
2066 if (ea.line2 == 0)
2067 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 else
2069 curwin->w_cursor.lnum = ea.line2;
2070 beginline(BL_SOL | BL_FIX);
2071 }
2072 }
2073 goto doend;
2074 }
2075
2076 /* Find the command and let "p" point to after it. */
2077 p = find_command(&ea, NULL);
2078
2079#ifdef FEAT_USR_CMDS
2080 if (p == NULL)
2081 {
2082 if (!ea.skip)
2083 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2084 goto doend;
2085 }
2086 /* Check for wrong commands. */
2087 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2088 {
2089 errormsg = uc_fun_cmd();
2090 goto doend;
2091 }
2092#endif
2093 if (ea.cmdidx == CMD_SIZE)
2094 {
2095 if (!ea.skip)
2096 {
2097 STRCPY(IObuff, _("E492: Not an editor command"));
2098 if (!sourcing)
2099 {
2100 STRCAT(IObuff, ": ");
2101 STRNCAT(IObuff, *cmdlinep, 40);
2102 }
2103 errormsg = IObuff;
2104 }
2105 goto doend;
2106 }
2107
2108 ni = (
2109#ifdef FEAT_USR_CMDS
2110 !USER_CMDIDX(ea.cmdidx) &&
2111#endif
2112 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2113
2114#ifndef FEAT_EVAL
2115 /*
2116 * When the expression evaluation is disabled, recognize the ":if" and
2117 * ":endif" commands and ignore everything in between it.
2118 */
2119 if (ea.cmdidx == CMD_if)
2120 ++if_level;
2121 if (if_level)
2122 {
2123 if (ea.cmdidx == CMD_endif)
2124 --if_level;
2125 goto doend;
2126 }
2127
2128#endif
2129
2130 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2131 {
2132 ++p;
2133 ea.forceit = TRUE;
2134 }
2135 else
2136 ea.forceit = FALSE;
2137
2138/*
2139 * 5. parse arguments
2140 */
2141#ifdef FEAT_USR_CMDS
2142 if (!USER_CMDIDX(ea.cmdidx))
2143#endif
2144 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2145
2146 if (!ea.skip)
2147 {
2148#ifdef HAVE_SANDBOX
2149 if (sandbox != 0 && !(ea.argt & SBOXOK))
2150 {
2151 /* Command not allowed in sandbox. */
2152 errormsg = (char_u *)_(e_sandbox);
2153 goto doend;
2154 }
2155#endif
2156 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2157 {
2158 /* Command not allowed in non-'modifiable' buffer */
2159 errormsg = (char_u *)_(e_modifiable);
2160 goto doend;
2161 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002162
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002163 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164# ifdef FEAT_USR_CMDS
2165 && !USER_CMDIDX(ea.cmdidx)
2166# endif
2167 )
2168 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002169 /* Command not allowed when editing the command line. */
2170#ifdef FEAT_CMDWIN
2171 if (cmdwin_type != 0)
2172 errormsg = (char_u *)_(e_cmdwin);
2173 else
2174#endif
2175 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 goto doend;
2177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2180 {
2181 /* no range allowed */
2182 errormsg = (char_u *)_(e_norange);
2183 goto doend;
2184 }
2185 }
2186
2187 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2188 {
2189 errormsg = (char_u *)_(e_nobang);
2190 goto doend;
2191 }
2192
2193 /*
2194 * Don't complain about the range if it is not used
2195 * (could happen if line_count is accidentally set to 0).
2196 */
2197 if (!ea.skip && !ni)
2198 {
2199 /*
2200 * If the range is backwards, ask for confirmation and, if given, swap
2201 * ea.line1 & ea.line2 so it's forwards again.
2202 * When global command is busy, don't ask, will fail below.
2203 */
2204 if (!global_busy && ea.line1 > ea.line2)
2205 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002206 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002208 if (sourcing || exmode_active)
2209 {
2210 errormsg = (char_u *)_("E493: Backwards range given");
2211 goto doend;
2212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (ask_yesno((char_u *)
2214 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002215 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217 lnum = ea.line1;
2218 ea.line1 = ea.line2;
2219 ea.line2 = lnum;
2220 }
2221 if ((errormsg = invalid_range(&ea)) != NULL)
2222 goto doend;
2223 }
2224
2225 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2226 ea.line2 = 1;
2227
2228 correct_range(&ea);
2229
2230#ifdef FEAT_FOLDING
2231 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2232 {
2233 /* Put the first line at the start of a closed fold, put the last line
2234 * at the end of a closed fold. */
2235 (void)hasFolding(ea.line1, &ea.line1, NULL);
2236 (void)hasFolding(ea.line2, NULL, &ea.line2);
2237 }
2238#endif
2239
2240#ifdef FEAT_QUICKFIX
2241 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002242 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 * option here, so things like % get expanded.
2244 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002245 p = replace_makeprg(&ea, p, cmdlinep);
2246 if (p == NULL)
2247 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248#endif
2249
2250 /*
2251 * Skip to start of argument.
2252 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2253 */
2254 if (ea.cmdidx == CMD_bang)
2255 ea.arg = p;
2256 else
2257 ea.arg = skipwhite(p);
2258
2259 /*
2260 * Check for "++opt=val" argument.
2261 * Must be first, allow ":w ++enc=utf8 !cmd"
2262 */
2263 if (ea.argt & ARGOPT)
2264 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2265 if (getargopt(&ea) == FAIL && !ni)
2266 {
2267 errormsg = (char_u *)_(e_invarg);
2268 goto doend;
2269 }
2270
2271 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2272 {
2273 if (*ea.arg == '>') /* append */
2274 {
2275 if (*++ea.arg != '>') /* typed wrong */
2276 {
2277 errormsg = (char_u *)_("E494: Use w or w>>");
2278 goto doend;
2279 }
2280 ea.arg = skipwhite(ea.arg + 1);
2281 ea.append = TRUE;
2282 }
2283 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2284 {
2285 ++ea.arg;
2286 ea.usefilter = TRUE;
2287 }
2288 }
2289
2290 if (ea.cmdidx == CMD_read)
2291 {
2292 if (ea.forceit)
2293 {
2294 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2295 ea.forceit = FALSE;
2296 }
2297 else if (*ea.arg == '!') /* :r !filter */
2298 {
2299 ++ea.arg;
2300 ea.usefilter = TRUE;
2301 }
2302 }
2303
2304 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2305 {
2306 ea.amount = 1;
2307 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2308 {
2309 ++ea.arg;
2310 ++ea.amount;
2311 }
2312 ea.arg = skipwhite(ea.arg);
2313 }
2314
2315 /*
2316 * Check for "+command" argument, before checking for next command.
2317 * Don't do this for ":read !cmd" and ":write !cmd".
2318 */
2319 if ((ea.argt & EDITCMD) && !ea.usefilter)
2320 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2321
2322 /*
2323 * Check for '|' to separate commands and '"' to start comments.
2324 * Don't do this for ":read !cmd" and ":write !cmd".
2325 */
2326 if ((ea.argt & TRLBAR) && !ea.usefilter)
2327 separate_nextcmd(&ea);
2328
2329 /*
2330 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002331 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2332 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002334 else if (ea.cmdidx == CMD_bang
2335 || ea.cmdidx == CMD_global
2336 || ea.cmdidx == CMD_vglobal
2337 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 for (p = ea.arg; *p; ++p)
2340 {
2341 /* Remove one backslash before a newline, so that it's possible to
2342 * pass a newline to the shell and also a newline that is preceded
2343 * with a backslash. This makes it impossible to end a shell
2344 * command in a backslash, but that doesn't appear useful.
2345 * Halving the number of backslashes is incompatible with previous
2346 * versions. */
2347 if (*p == '\\' && p[1] == '\n')
2348 mch_memmove(p, p + 1, STRLEN(p));
2349 else if (*p == '\n')
2350 {
2351 ea.nextcmd = p + 1;
2352 *p = NUL;
2353 break;
2354 }
2355 }
2356 }
2357
2358 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2359 {
2360 ea.line1 = 1;
2361 ea.line2 = curbuf->b_ml.ml_line_count;
2362 }
2363
2364 /* accept numbered register only when no count allowed (:put) */
2365 if ( (ea.argt & REGSTR)
2366 && *ea.arg != NUL
2367#ifdef FEAT_USR_CMDS
2368 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2369 && USER_CMDIDX(ea.cmdidx)))
2370 /* Do not allow register = for user commands */
2371 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2372#else
2373 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2374#endif
2375 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2376 {
2377 ea.regname = *ea.arg++;
2378#ifdef FEAT_EVAL
2379 /* for '=' register: accept the rest of the line as an expression */
2380 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2381 {
2382 set_expr_line(vim_strsave(ea.arg));
2383 ea.arg += STRLEN(ea.arg);
2384 }
2385#endif
2386 ea.arg = skipwhite(ea.arg);
2387 }
2388
2389 /*
2390 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2391 * count, it's a buffer name.
2392 */
2393 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2394 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2395 || vim_iswhite(*p)))
2396 {
2397 n = getdigits(&ea.arg);
2398 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002399 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
2401 errormsg = (char_u *)_(e_zerocount);
2402 goto doend;
2403 }
2404 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2405 {
2406 ea.line2 = n;
2407 if (ea.addr_count == 0)
2408 ea.addr_count = 1;
2409 }
2410 else
2411 {
2412 ea.line1 = ea.line2;
2413 ea.line2 += n - 1;
2414 ++ea.addr_count;
2415 /*
2416 * Be vi compatible: no error message for out of range.
2417 */
2418 if (ea.line2 > curbuf->b_ml.ml_line_count)
2419 ea.line2 = curbuf->b_ml.ml_line_count;
2420 }
2421 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002422
2423 /*
2424 * Check for flags: 'l', 'p' and '#'.
2425 */
2426 if (ea.argt & EXFLAGS)
2427 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002429 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002430 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432 errormsg = (char_u *)_(e_trailing);
2433 goto doend;
2434 }
2435
2436 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2437 {
2438 errormsg = (char_u *)_(e_argreq);
2439 goto doend;
2440 }
2441
2442#ifdef FEAT_EVAL
2443 /*
2444 * Skip the command when it's not going to be executed.
2445 * The commands like :if, :endif, etc. always need to be executed.
2446 * Also make an exception for commands that handle a trailing command
2447 * themselves.
2448 */
2449 if (ea.skip)
2450 {
2451 switch (ea.cmdidx)
2452 {
2453 /* commands that need evaluation */
2454 case CMD_while:
2455 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002456 case CMD_for:
2457 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_if:
2459 case CMD_elseif:
2460 case CMD_else:
2461 case CMD_endif:
2462 case CMD_try:
2463 case CMD_catch:
2464 case CMD_finally:
2465 case CMD_endtry:
2466 case CMD_function:
2467 break;
2468
2469 /* Commands that handle '|' themselves. Check: A command should
2470 * either have the TRLBAR flag, appear in this list or appear in
2471 * the list at ":help :bar". */
2472 case CMD_aboveleft:
2473 case CMD_and:
2474 case CMD_belowright:
2475 case CMD_botright:
2476 case CMD_browse:
2477 case CMD_call:
2478 case CMD_confirm:
2479 case CMD_delfunction:
2480 case CMD_djump:
2481 case CMD_dlist:
2482 case CMD_dsearch:
2483 case CMD_dsplit:
2484 case CMD_echo:
2485 case CMD_echoerr:
2486 case CMD_echomsg:
2487 case CMD_echon:
2488 case CMD_execute:
2489 case CMD_help:
2490 case CMD_hide:
2491 case CMD_ijump:
2492 case CMD_ilist:
2493 case CMD_isearch:
2494 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002495 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 case CMD_keepjumps:
2497 case CMD_keepmarks:
2498 case CMD_leftabove:
2499 case CMD_let:
2500 case CMD_lockmarks:
2501 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002502 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_perl:
2504 case CMD_psearch:
2505 case CMD_python:
2506 case CMD_return:
2507 case CMD_rightbelow:
2508 case CMD_ruby:
2509 case CMD_silent:
2510 case CMD_smagic:
2511 case CMD_snomagic:
2512 case CMD_substitute:
2513 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002514 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 case CMD_tcl:
2516 case CMD_throw:
2517 case CMD_tilde:
2518 case CMD_topleft:
2519 case CMD_unlet:
2520 case CMD_verbose:
2521 case CMD_vertical:
2522 break;
2523
2524 default: goto doend;
2525 }
2526 }
2527#endif
2528
2529 if (ea.argt & XFILE)
2530 {
2531 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2532 goto doend;
2533 }
2534
2535#ifdef FEAT_LISTCMDS
2536 /*
2537 * Accept buffer name. Cannot be used at the same time with a buffer
2538 * number. Don't do this for a user command.
2539 */
2540 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2541# ifdef FEAT_USR_CMDS
2542 && !USER_CMDIDX(ea.cmdidx)
2543# endif
2544 )
2545 {
2546 /*
2547 * :bdelete, :bwipeout and :bunload take several arguments, separated
2548 * by spaces: find next space (skipping over escaped characters).
2549 * The others take one argument: ignore trailing spaces.
2550 */
2551 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2552 || ea.cmdidx == CMD_bunload)
2553 p = skiptowhite_esc(ea.arg);
2554 else
2555 {
2556 p = ea.arg + STRLEN(ea.arg);
2557 while (p > ea.arg && vim_iswhite(p[-1]))
2558 --p;
2559 }
2560 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2561 if (ea.line2 < 0) /* failed */
2562 goto doend;
2563 ea.addr_count = 1;
2564 ea.arg = skipwhite(p);
2565 }
2566#endif
2567
2568/*
2569 * 6. switch on command name
2570 *
2571 * The "ea" structure holds the arguments that can be used.
2572 */
2573 ea.cmdlinep = cmdlinep;
2574 ea.getline = getline;
2575 ea.cookie = cookie;
2576#ifdef FEAT_EVAL
2577 ea.cstack = cstack;
2578#endif
2579
2580#ifdef FEAT_USR_CMDS
2581 if (USER_CMDIDX(ea.cmdidx))
2582 {
2583 /*
2584 * Execute a user-defined command.
2585 */
2586 do_ucmd(&ea);
2587 }
2588 else
2589#endif
2590 {
2591 /*
2592 * Call the function to execute the command.
2593 */
2594 ea.errmsg = NULL;
2595 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2596 if (ea.errmsg != NULL)
2597 errormsg = (char_u *)_(ea.errmsg);
2598 }
2599
2600#ifdef FEAT_EVAL
2601 /*
2602 * If the command just executed called do_cmdline(), any throw or ":return"
2603 * or ":finish" encountered there must also check the cstack of the still
2604 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2605 * exception, or reanimate a returned function or finished script file and
2606 * return or finish it again.
2607 */
2608 if (need_rethrow)
2609 do_throw(cstack);
2610 else if (check_cstack)
2611 {
2612 if (source_finished(getline, cookie))
2613 do_finish(&ea, TRUE);
2614 else if (getline_equal(getline, cookie, get_func_line)
2615 && current_func_returned())
2616 do_return(&ea, TRUE, FALSE, NULL);
2617 }
2618 need_rethrow = check_cstack = FALSE;
2619#endif
2620
2621doend:
2622 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2623 curwin->w_cursor.lnum = 1;
2624
2625 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2626 {
2627 if (sourcing)
2628 {
2629 if (errormsg != IObuff)
2630 {
2631 STRCPY(IObuff, errormsg);
2632 errormsg = IObuff;
2633 }
2634 STRCAT(errormsg, ": ");
2635 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2636 }
2637 emsg(errormsg);
2638 }
2639#ifdef FEAT_EVAL
2640 do_errthrow(cstack,
2641 (ea.cmdidx != CMD_SIZE
2642# ifdef FEAT_USR_CMDS
2643 && !USER_CMDIDX(ea.cmdidx)
2644# endif
2645 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2646#endif
2647
2648 if (verbose_save >= 0)
2649 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002650#ifdef FEAT_AUTOCMD
2651 if (cmdmod.save_ei != NULL)
2652 {
2653 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002654 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2655 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002656 free_string_option(cmdmod.save_ei);
2657 }
2658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 cmdmod = save_cmdmod;
2661
2662 if (did_silent > 0)
2663 {
2664 /* messages could be enabled for a serious error, need to check if the
2665 * counters don't become negative */
2666 msg_silent -= did_silent;
2667 if (msg_silent < 0)
2668 msg_silent = 0;
2669 emsg_silent -= did_esilent;
2670 if (emsg_silent < 0)
2671 emsg_silent = 0;
2672 /* Restore msg_scroll, it's set by file I/O commands, even when no
2673 * message is actually displayed. */
2674 msg_scroll = save_msg_scroll;
2675 }
2676
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002677#ifdef HAVE_SANDBOX
2678 if (did_sandbox)
2679 --sandbox;
2680#endif
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2683 ea.nextcmd = NULL;
2684
2685#ifdef FEAT_EVAL
2686 --ex_nesting_level;
2687#endif
2688
2689 return ea.nextcmd;
2690}
2691#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002692 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693#endif
2694
2695/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002696 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 * If there is a match advance "pp" to the argument and return TRUE.
2698 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002699 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002701 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 char *cmd; /* name of command */
2703 int len; /* required length */
2704{
2705 int i;
2706
2707 for (i = 0; cmd[i] != NUL; ++i)
2708 if (cmd[i] != (*pp)[i])
2709 break;
2710 if (i >= len && !isalpha((*pp)[i]))
2711 {
2712 *pp = skipwhite(*pp + i);
2713 return TRUE;
2714 }
2715 return FALSE;
2716}
2717
2718/*
2719 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002720 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002722 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 * Returns NULL for an ambiguous user command.
2724 */
2725/*ARGSUSED*/
2726 static char_u *
2727find_command(eap, full)
2728 exarg_T *eap;
2729 int *full;
2730{
2731 int len;
2732 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002733 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
2735 /*
2736 * Isolate the command and search for it in the command table.
2737 * Exeptions:
2738 * - the 'k' command can directly be followed by any character.
2739 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2740 * but :sre[wind] is another command, as are :scrip[tnames],
2741 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002742 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 */
2744 p = eap->cmd;
2745 if (*p == 'k')
2746 {
2747 eap->cmdidx = CMD_k;
2748 ++p;
2749 }
2750 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002751 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2752 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 || p[1] == 'g'
2754 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2755 || p[1] == 'I'
2756 || (p[1] == 'r' && p[2] != 'e')))
2757 {
2758 eap->cmdidx = CMD_substitute;
2759 ++p;
2760 }
2761 else
2762 {
2763 while (ASCII_ISALPHA(*p))
2764 ++p;
2765 /* check for non-alpha command */
2766 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2767 ++p;
2768 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002769 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2770 {
2771 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2772 * :delete with the 'l' flag. Same for 'p'. */
2773 for (i = 0; i < len; ++i)
2774 if (eap->cmd[i] != "delete"[i])
2775 break;
2776 if (i == len - 1)
2777 {
2778 --len;
2779 if (p[-1] == 'l')
2780 eap->flags |= EXFLAG_LIST;
2781 else
2782 eap->flags |= EXFLAG_PRINT;
2783 }
2784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786 if (ASCII_ISLOWER(*eap->cmd))
2787 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2788 else
2789 eap->cmdidx = cmdidxs[26];
2790
2791 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2792 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2793 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2794 (size_t)len) == 0)
2795 {
2796#ifdef FEAT_EVAL
2797 if (full != NULL
2798 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2799 *full = TRUE;
2800#endif
2801 break;
2802 }
2803
2804#ifdef FEAT_USR_CMDS
2805 /* Look for a user defined command as a last resort */
2806 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2807 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002808 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 while (ASCII_ISALNUM(*p))
2810 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002811 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002814 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 eap->cmdidx = CMD_SIZE;
2816 }
2817
2818 return p;
2819}
2820
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002821#ifdef FEAT_USR_CMDS
2822/*
2823 * Search for a user command that matches "eap->cmd".
2824 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2825 * Return a pointer to just after the command.
2826 * Return NULL if there is no matching command.
2827 */
2828 static char_u *
2829find_ucmd(eap, p, full, xp, compl)
2830 exarg_T *eap;
2831 char_u *p; /* end of the command (possibly including count) */
2832 int *full; /* set to TRUE for a full match */
2833 expand_T *xp; /* used for completion, NULL otherwise */
2834 int *compl; /* completion flags or NULL */
2835{
2836 int len = (int)(p - eap->cmd);
2837 int j, k, matchlen = 0;
2838 ucmd_T *uc;
2839 int found = FALSE;
2840 int possible = FALSE;
2841 char_u *cp, *np; /* Point into typed cmd and test name */
2842 garray_T *gap;
2843 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2844 only full match global is accepted. */
2845
2846 /*
2847 * Look for buffer-local user commands first, then global ones.
2848 */
2849 gap = &curbuf->b_ucmds;
2850 for (;;)
2851 {
2852 for (j = 0; j < gap->ga_len; ++j)
2853 {
2854 uc = USER_CMD_GA(gap, j);
2855 cp = eap->cmd;
2856 np = uc->uc_name;
2857 k = 0;
2858 while (k < len && *np != NUL && *cp++ == *np++)
2859 k++;
2860 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2861 {
2862 /* If finding a second match, the command is ambiguous. But
2863 * not if a buffer-local command wasn't a full match and a
2864 * global command is a full match. */
2865 if (k == len && found && *np != NUL)
2866 {
2867 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002868 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002869 amb_local = TRUE;
2870 }
2871
2872 if (!found || (k == len && *np == NUL))
2873 {
2874 /* If we matched up to a digit, then there could
2875 * be another command including the digit that we
2876 * should use instead.
2877 */
2878 if (k == len)
2879 found = TRUE;
2880 else
2881 possible = TRUE;
2882
2883 if (gap == &ucmds)
2884 eap->cmdidx = CMD_USER;
2885 else
2886 eap->cmdidx = CMD_USER_BUF;
2887 eap->argt = uc->uc_argt;
2888 eap->useridx = j;
2889
2890# ifdef FEAT_CMDL_COMPL
2891 if (compl != NULL)
2892 *compl = uc->uc_compl;
2893# ifdef FEAT_EVAL
2894 if (xp != NULL)
2895 {
2896 xp->xp_arg = uc->uc_compl_arg;
2897 xp->xp_scriptID = uc->uc_scriptID;
2898 }
2899# endif
2900# endif
2901 /* Do not search for further abbreviations
2902 * if this is an exact match. */
2903 matchlen = k;
2904 if (k == len && *np == NUL)
2905 {
2906 if (full != NULL)
2907 *full = TRUE;
2908 amb_local = FALSE;
2909 break;
2910 }
2911 }
2912 }
2913 }
2914
2915 /* Stop if we found a full match or searched all. */
2916 if (j < gap->ga_len || gap == &ucmds)
2917 break;
2918 gap = &ucmds;
2919 }
2920
2921 /* Only found ambiguous matches. */
2922 if (amb_local)
2923 {
2924 if (xp != NULL)
2925 xp->xp_context = EXPAND_UNSUCCESSFUL;
2926 return NULL;
2927 }
2928
2929 /* The match we found may be followed immediately by a number. Move "p"
2930 * back to point to it. */
2931 if (found || possible)
2932 return p + (matchlen - len);
2933 return p;
2934}
2935#endif
2936
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937#if defined(FEAT_EVAL) || defined(PROTO)
2938/*
2939 * Return > 0 if an Ex command "name" exists.
2940 * Return 2 if there is an exact match.
2941 * Return 3 if there is an ambiguous match.
2942 */
2943 int
2944cmd_exists(name)
2945 char_u *name;
2946{
2947 exarg_T ea;
2948 int full = FALSE;
2949 int i;
2950 int j;
2951 static struct cmdmod
2952 {
2953 char *name;
2954 int minlen;
2955 } cmdmods[] = {
2956 {"aboveleft", 3},
2957 {"belowright", 3},
2958 {"botright", 2},
2959 {"browse", 3},
2960 {"confirm", 4},
2961 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002962 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 {"keepjumps", 5},
2964 {"keepmarks", 3},
2965 {"leftabove", 5},
2966 {"lockmarks", 3},
2967 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002968 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 {"silent", 3},
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002970 {"tab", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 {"topleft", 2},
2972 {"verbose", 4},
2973 {"vertical", 4},
2974 };
2975
2976 /* Check command modifiers. */
2977 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2978 {
2979 for (j = 0; name[j] != NUL; ++j)
2980 if (name[j] != cmdmods[i].name[j])
2981 break;
2982 if (name[j] == NUL && j >= cmdmods[i].minlen)
2983 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2984 }
2985
2986 /* Check built-in commands and user defined commands. */
2987 ea.cmd = name;
2988 ea.cmdidx = (cmdidx_T)0;
2989 if (find_command(&ea, &full) == NULL)
2990 return 3;
2991 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2992}
2993#endif
2994
2995/*
2996 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2997 * we don't need/want deleted. Maybe this could be done better if we didn't
2998 * repeat all this stuff. The only problem is that they may not stay
2999 * perfectly compatible with each other, but then the command line syntax
3000 * probably won't change that much -- webb.
3001 */
3002 char_u *
3003set_one_cmd_context(xp, buff)
3004 expand_T *xp;
3005 char_u *buff; /* buffer for command string */
3006{
3007 char_u *p;
3008 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003009 int len = 0;
3010 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3012 int compl = EXPAND_NOTHING;
3013#endif
3014#ifdef FEAT_CMDL_COMPL
3015 int delim;
3016#endif
3017 int forceit = FALSE;
3018 int usefilter = FALSE; /* filter instead of file name */
3019
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003020 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 xp->xp_pattern = buff;
3022 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003023 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
3025/*
3026 * 2. skip comment lines and leading space, colons or bars
3027 */
3028 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3029 ;
3030 xp->xp_pattern = cmd;
3031
3032 if (*cmd == NUL)
3033 return NULL;
3034 if (*cmd == '"') /* ignore comment lines */
3035 {
3036 xp->xp_context = EXPAND_NOTHING;
3037 return NULL;
3038 }
3039
3040/*
3041 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3042 */
3043 cmd = skip_range(cmd, &xp->xp_context);
3044
3045/*
3046 * 4. parse command
3047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 xp->xp_pattern = cmd;
3049 if (*cmd == NUL)
3050 return NULL;
3051 if (*cmd == '"')
3052 {
3053 xp->xp_context = EXPAND_NOTHING;
3054 return NULL;
3055 }
3056
3057 if (*cmd == '|' || *cmd == '\n')
3058 return cmd + 1; /* There's another command */
3059
3060 /*
3061 * Isolate the command and search for it in the command table.
3062 * Exceptions:
3063 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003064 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3066 */
3067 if (*cmd == 'k' && cmd[1] != 'e')
3068 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003069 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 p = cmd + 1;
3071 }
3072 else
3073 {
3074 p = cmd;
3075 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3076 ++p;
3077 /* check for non-alpha command */
3078 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3079 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003080 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003082 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
3084 xp->xp_context = EXPAND_UNSUCCESSFUL;
3085 return NULL;
3086 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003087 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3088 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3089 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 break;
3091
3092#ifdef FEAT_USR_CMDS
3093 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3094 {
3095 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3096 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003097 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
3099#endif
3100 }
3101
3102 /*
3103 * If the cursor is touching the command, and it ends in an alpha-numeric
3104 * character, complete the command name.
3105 */
3106 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3107 return NULL;
3108
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003109 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
3111 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3112 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003113 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 p = cmd + 1;
3115 }
3116#ifdef FEAT_USR_CMDS
3117 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3118 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003119 ea.cmd = cmd;
3120 p = find_ucmd(&ea, p, NULL, xp,
3121# if defined(FEAT_CMDL_COMPL)
3122 &compl
3123# else
3124 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003127 if (p == NULL)
3128 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
3130#endif
3131 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003132 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
3134 /* Not still touching the command and it was an illegal one */
3135 xp->xp_context = EXPAND_UNSUCCESSFUL;
3136 return NULL;
3137 }
3138
3139 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3140
3141 if (*p == '!') /* forced commands */
3142 {
3143 forceit = TRUE;
3144 ++p;
3145 }
3146
3147/*
3148 * 5. parse arguments
3149 */
3150#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003151 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003153 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 arg = skipwhite(p);
3156
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003157 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
3159 if (*arg == '>') /* append */
3160 {
3161 if (*++arg == '>')
3162 ++arg;
3163 arg = skipwhite(arg);
3164 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003165 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
3167 ++arg;
3168 usefilter = TRUE;
3169 }
3170 }
3171
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003172 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
3174 usefilter = forceit; /* :r! filter if forced */
3175 if (*arg == '!') /* :r !filter */
3176 {
3177 ++arg;
3178 usefilter = TRUE;
3179 }
3180 }
3181
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003182 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 while (*arg == *cmd) /* allow any number of '>' or '<' */
3185 ++arg;
3186 arg = skipwhite(arg);
3187 }
3188
3189 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
3192 /* Check if we're in the +command */
3193 p = arg + 1;
3194 arg = skip_cmd_arg(arg, FALSE);
3195
3196 /* Still touching the command after '+'? */
3197 if (*arg == NUL)
3198 return p;
3199
3200 /* Skip space(s) after +command to get to the real argument */
3201 arg = skipwhite(arg);
3202 }
3203
3204 /*
3205 * Check for '|' to separate commands and '"' to start comments.
3206 * Don't do this for ":read !cmd" and ":write !cmd".
3207 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003208 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
3210 p = arg;
3211 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003212 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 p += 2;
3214 while (*p)
3215 {
3216 if (*p == Ctrl_V)
3217 {
3218 if (p[1] != NUL)
3219 ++p;
3220 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003221 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 || *p == '|' || *p == '\n')
3223 {
3224 if (*(p - 1) != '\\')
3225 {
3226 if (*p == '|' || *p == '\n')
3227 return p + 1;
3228 return NULL; /* It's a comment */
3229 }
3230 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003231 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233 }
3234
3235 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003236 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 vim_strchr((char_u *)"|\"", *arg) == NULL)
3238 return NULL;
3239
3240 /* Find start of last argument (argument just before cursor): */
3241 p = buff + STRLEN(buff);
3242 while (p != arg && *p != ' ' && *p != TAB)
3243 p--;
3244 if (*p == ' ' || *p == TAB)
3245 p++;
3246 xp->xp_pattern = p;
3247
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003248 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
3250 int in_quote = FALSE;
3251 char_u *bow = NULL; /* Beginning of word */
3252
3253 /*
3254 * Allow spaces within back-quotes to count as part of the argument
3255 * being expanded.
3256 */
3257 xp->xp_pattern = skipwhite(arg);
3258 for (p = xp->xp_pattern; *p; )
3259 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003260 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 ++p;
3262#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003263 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#else
3265 else if (vim_iswhite(*p))
3266#endif
3267 {
3268 p = skipwhite(p);
3269 if (in_quote)
3270 bow = p;
3271 else
3272 xp->xp_pattern = p;
3273 --p;
3274 }
3275 else if (*p == '`')
3276 {
3277 if (!in_quote)
3278 {
3279 xp->xp_pattern = p;
3280 bow = p + 1;
3281 }
3282 in_quote = !in_quote;
3283 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003284 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286
3287 /*
3288 * If we are still inside the quotes, and we passed a space, just
3289 * expand from there.
3290 */
3291 if (bow != NULL && in_quote)
3292 xp->xp_pattern = bow;
3293 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003294
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003295#ifndef BACKSLASH_IN_FILENAME
3296 /* For a shell command more chars need to be escaped. */
3297 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003298 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003299 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003300
3301 /* When still after the command name expand executables. */
3302 if (xp->xp_pattern == skipwhite(arg))
3303 xp->xp_context = EXPAND_SHELLCMD;
3304 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 /* Check for environment variable */
3308 if (*xp->xp_pattern == '$'
3309#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3310 || *xp->xp_pattern == '%'
3311#endif
3312 )
3313 {
3314 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3315 if (!vim_isIDc(*p))
3316 break;
3317 if (*p == NUL)
3318 {
3319 xp->xp_context = EXPAND_ENV_VARS;
3320 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003321#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3322 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003323 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003324 compl = EXPAND_ENV_VARS;
3325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327 }
3328 }
3329
3330/*
3331 * 6. switch on command name
3332 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
3335 case CMD_cd:
3336 case CMD_chdir:
3337 case CMD_lcd:
3338 case CMD_lchdir:
3339 if (xp->xp_context == EXPAND_FILES)
3340 xp->xp_context = EXPAND_DIRECTORIES;
3341 break;
3342 case CMD_help:
3343 xp->xp_context = EXPAND_HELP;
3344 xp->xp_pattern = arg;
3345 break;
3346
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003347 /* Command modifiers: return the argument.
3348 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003350 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 case CMD_belowright:
3352 case CMD_botright:
3353 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003354 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003356 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 case CMD_folddoclosed:
3358 case CMD_folddoopen:
3359 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003360 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 case CMD_keepjumps:
3362 case CMD_keepmarks:
3363 case CMD_leftabove:
3364 case CMD_lockmarks:
3365 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003366 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003368 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 case CMD_topleft:
3370 case CMD_verbose:
3371 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003372 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 return arg;
3374
3375#ifdef FEAT_SEARCH_EXTRA
3376 case CMD_match:
3377 if (*arg == NUL || !ends_excmd(*arg))
3378 {
3379 /* Dummy call to clear variables. */
3380 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3381 xp->xp_context = EXPAND_HIGHLIGHT;
3382 xp->xp_pattern = arg;
3383 arg = skipwhite(skiptowhite(arg));
3384 if (*arg != NUL)
3385 {
3386 xp->xp_context = EXPAND_NOTHING;
3387 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3388 }
3389 }
3390 return find_nextcmd(arg);
3391#endif
3392
3393#ifdef FEAT_CMDL_COMPL
3394/*
3395 * All completion for the +cmdline_compl feature goes here.
3396 */
3397
3398# ifdef FEAT_USR_CMDS
3399 case CMD_command:
3400 /* Check for attributes */
3401 while (*arg == '-')
3402 {
3403 arg++; /* Skip "-" */
3404 p = skiptowhite(arg);
3405 if (*p == NUL)
3406 {
3407 /* Cursor is still in the attribute */
3408 p = vim_strchr(arg, '=');
3409 if (p == NULL)
3410 {
3411 /* No "=", so complete attribute names */
3412 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3413 xp->xp_pattern = arg;
3414 return NULL;
3415 }
3416
3417 /* For the -complete and -nargs attributes, we complete
3418 * their arguments as well.
3419 */
3420 if (STRNICMP(arg, "complete", p - arg) == 0)
3421 {
3422 xp->xp_context = EXPAND_USER_COMPLETE;
3423 xp->xp_pattern = p + 1;
3424 return NULL;
3425 }
3426 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3427 {
3428 xp->xp_context = EXPAND_USER_NARGS;
3429 xp->xp_pattern = p + 1;
3430 return NULL;
3431 }
3432 return NULL;
3433 }
3434 arg = skipwhite(p);
3435 }
3436
3437 /* After the attributes comes the new command name */
3438 p = skiptowhite(arg);
3439 if (*p == NUL)
3440 {
3441 xp->xp_context = EXPAND_USER_COMMANDS;
3442 xp->xp_pattern = arg;
3443 break;
3444 }
3445
3446 /* And finally comes a normal command */
3447 return skipwhite(p);
3448
3449 case CMD_delcommand:
3450 xp->xp_context = EXPAND_USER_COMMANDS;
3451 xp->xp_pattern = arg;
3452 break;
3453# endif
3454
3455 case CMD_global:
3456 case CMD_vglobal:
3457 delim = *arg; /* get the delimiter */
3458 if (delim)
3459 ++arg; /* skip delimiter if there is one */
3460
3461 while (arg[0] != NUL && arg[0] != delim)
3462 {
3463 if (arg[0] == '\\' && arg[1] != NUL)
3464 ++arg;
3465 ++arg;
3466 }
3467 if (arg[0] != NUL)
3468 return arg + 1;
3469 break;
3470 case CMD_and:
3471 case CMD_substitute:
3472 delim = *arg;
3473 if (delim)
3474 {
3475 /* skip "from" part */
3476 ++arg;
3477 arg = skip_regexp(arg, delim, p_magic, NULL);
3478 }
3479 /* skip "to" part */
3480 while (arg[0] != NUL && arg[0] != delim)
3481 {
3482 if (arg[0] == '\\' && arg[1] != NUL)
3483 ++arg;
3484 ++arg;
3485 }
3486 if (arg[0] != NUL) /* skip delimiter */
3487 ++arg;
3488 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3489 ++arg;
3490 if (arg[0] != NUL)
3491 return arg;
3492 break;
3493 case CMD_isearch:
3494 case CMD_dsearch:
3495 case CMD_ilist:
3496 case CMD_dlist:
3497 case CMD_ijump:
3498 case CMD_psearch:
3499 case CMD_djump:
3500 case CMD_isplit:
3501 case CMD_dsplit:
3502 arg = skipwhite(skipdigits(arg)); /* skip count */
3503 if (*arg == '/') /* Match regexp, not just whole words */
3504 {
3505 for (++arg; *arg && *arg != '/'; arg++)
3506 if (*arg == '\\' && arg[1] != NUL)
3507 arg++;
3508 if (*arg)
3509 {
3510 arg = skipwhite(arg + 1);
3511
3512 /* Check for trailing illegal characters */
3513 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3514 xp->xp_context = EXPAND_NOTHING;
3515 else
3516 return arg;
3517 }
3518 }
3519 break;
3520#ifdef FEAT_AUTOCMD
3521 case CMD_autocmd:
3522 return set_context_in_autocmd(xp, arg, FALSE);
3523
3524 case CMD_doautocmd:
3525 return set_context_in_autocmd(xp, arg, TRUE);
3526#endif
3527 case CMD_set:
3528 set_context_in_set_cmd(xp, arg, 0);
3529 break;
3530 case CMD_setglobal:
3531 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3532 break;
3533 case CMD_setlocal:
3534 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3535 break;
3536 case CMD_tag:
3537 case CMD_stag:
3538 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003539 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 case CMD_tselect:
3541 case CMD_stselect:
3542 case CMD_ptselect:
3543 case CMD_tjump:
3544 case CMD_stjump:
3545 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003546 if (*p_wop != NUL)
3547 xp->xp_context = EXPAND_TAGS_LISTFILES;
3548 else
3549 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 xp->xp_pattern = arg;
3551 break;
3552 case CMD_augroup:
3553 xp->xp_context = EXPAND_AUGROUP;
3554 xp->xp_pattern = arg;
3555 break;
3556#ifdef FEAT_SYN_HL
3557 case CMD_syntax:
3558 set_context_in_syntax_cmd(xp, arg);
3559 break;
3560#endif
3561#ifdef FEAT_EVAL
3562 case CMD_let:
3563 case CMD_if:
3564 case CMD_elseif:
3565 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003566 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 case CMD_echo:
3568 case CMD_echon:
3569 case CMD_execute:
3570 case CMD_echomsg:
3571 case CMD_echoerr:
3572 case CMD_call:
3573 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003574 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 break;
3576
3577 case CMD_unlet:
3578 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3579 arg = xp->xp_pattern + 1;
3580 xp->xp_context = EXPAND_USER_VARS;
3581 xp->xp_pattern = arg;
3582 break;
3583
3584 case CMD_function:
3585 case CMD_delfunction:
3586 xp->xp_context = EXPAND_USER_FUNC;
3587 xp->xp_pattern = arg;
3588 break;
3589
3590 case CMD_echohl:
3591 xp->xp_context = EXPAND_HIGHLIGHT;
3592 xp->xp_pattern = arg;
3593 break;
3594#endif
3595 case CMD_highlight:
3596 set_context_in_highlight_cmd(xp, arg);
3597 break;
3598#ifdef FEAT_LISTCMDS
3599 case CMD_bdelete:
3600 case CMD_bwipeout:
3601 case CMD_bunload:
3602 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3603 arg = xp->xp_pattern + 1;
3604 /*FALLTHROUGH*/
3605 case CMD_buffer:
3606 case CMD_sbuffer:
3607 case CMD_checktime:
3608 xp->xp_context = EXPAND_BUFFERS;
3609 xp->xp_pattern = arg;
3610 break;
3611#endif
3612#ifdef FEAT_USR_CMDS
3613 case CMD_USER:
3614 case CMD_USER_BUF:
3615 if (compl != EXPAND_NOTHING)
3616 {
3617 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003618 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
3620# ifdef FEAT_MENU
3621 if (compl == EXPAND_MENUS)
3622 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3623# endif
3624 if (compl == EXPAND_COMMANDS)
3625 return arg;
3626 if (compl == EXPAND_MAPPINGS)
3627 return set_context_in_map_cmd(xp, (char_u *)"map",
3628 arg, forceit, FALSE, FALSE, CMD_map);
3629 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3630 arg = xp->xp_pattern + 1;
3631 xp->xp_pattern = arg;
3632 }
3633 xp->xp_context = compl;
3634 }
3635 break;
3636#endif
3637 case CMD_map: case CMD_noremap:
3638 case CMD_nmap: case CMD_nnoremap:
3639 case CMD_vmap: case CMD_vnoremap:
3640 case CMD_omap: case CMD_onoremap:
3641 case CMD_imap: case CMD_inoremap:
3642 case CMD_cmap: case CMD_cnoremap:
3643 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003644 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 case CMD_unmap:
3646 case CMD_nunmap:
3647 case CMD_vunmap:
3648 case CMD_ounmap:
3649 case CMD_iunmap:
3650 case CMD_cunmap:
3651 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003652 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 case CMD_abbreviate: case CMD_noreabbrev:
3654 case CMD_cabbrev: case CMD_cnoreabbrev:
3655 case CMD_iabbrev: case CMD_inoreabbrev:
3656 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003657 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 case CMD_unabbreviate:
3659 case CMD_cunabbrev:
3660 case CMD_iunabbrev:
3661 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003662 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663#ifdef FEAT_MENU
3664 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3665 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3666 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3667 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3668 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3669 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3670 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3671 case CMD_tmenu: case CMD_tunmenu:
3672 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3673 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3674#endif
3675
3676 case CMD_colorscheme:
3677 xp->xp_context = EXPAND_COLORS;
3678 xp->xp_pattern = arg;
3679 break;
3680
3681 case CMD_compiler:
3682 xp->xp_context = EXPAND_COMPILER;
3683 xp->xp_pattern = arg;
3684 break;
3685
3686#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3687 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3688 case CMD_language:
3689 if (*skiptowhite(arg) == NUL)
3690 {
3691 xp->xp_context = EXPAND_LANGUAGE;
3692 xp->xp_pattern = arg;
3693 }
3694 else
3695 xp->xp_context = EXPAND_NOTHING;
3696 break;
3697#endif
3698
3699#endif /* FEAT_CMDL_COMPL */
3700
3701 default:
3702 break;
3703 }
3704 return NULL;
3705}
3706
3707/*
3708 * skip a range specifier of the form: addr [,addr] [;addr] ..
3709 *
3710 * Backslashed delimiters after / or ? will be skipped, and commands will
3711 * not be expanded between /'s and ?'s or after "'".
3712 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003713 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 * Returns the "cmd" pointer advanced to beyond the range.
3715 */
3716 char_u *
3717skip_range(cmd, ctx)
3718 char_u *cmd;
3719 int *ctx; /* pointer to xp_context or NULL */
3720{
3721 int delim;
3722
Bram Moolenaardf177f62005-02-22 08:39:57 +00003723 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
3725 if (*cmd == '\'')
3726 {
3727 if (*++cmd == NUL && ctx != NULL)
3728 *ctx = EXPAND_NOTHING;
3729 }
3730 else if (*cmd == '/' || *cmd == '?')
3731 {
3732 delim = *cmd++;
3733 while (*cmd != NUL && *cmd != delim)
3734 if (*cmd++ == '\\' && *cmd != NUL)
3735 ++cmd;
3736 if (*cmd == NUL && ctx != NULL)
3737 *ctx = EXPAND_NOTHING;
3738 }
3739 if (*cmd != NUL)
3740 ++cmd;
3741 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003742
3743 /* Skip ":" and white space. */
3744 while (*cmd == ':')
3745 cmd = skipwhite(cmd + 1);
3746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 return cmd;
3748}
3749
3750/*
3751 * get a single EX address
3752 *
3753 * Set ptr to the next character after the part that was interpreted.
3754 * Set ptr to NULL when an error is encountered.
3755 *
3756 * Return MAXLNUM when no Ex address was found.
3757 */
3758 static linenr_T
3759get_address(ptr, skip, to_other_file)
3760 char_u **ptr;
3761 int skip; /* only skip the address, don't use it */
3762 int to_other_file; /* flag: may jump to other file */
3763{
3764 int c;
3765 int i;
3766 long n;
3767 char_u *cmd;
3768 pos_T pos;
3769 pos_T *fp;
3770 linenr_T lnum;
3771
3772 cmd = skipwhite(*ptr);
3773 lnum = MAXLNUM;
3774 do
3775 {
3776 switch (*cmd)
3777 {
3778 case '.': /* '.' - Cursor position */
3779 ++cmd;
3780 lnum = curwin->w_cursor.lnum;
3781 break;
3782
3783 case '$': /* '$' - last line */
3784 ++cmd;
3785 lnum = curbuf->b_ml.ml_line_count;
3786 break;
3787
3788 case '\'': /* ''' - mark */
3789 if (*++cmd == NUL)
3790 {
3791 cmd = NULL;
3792 goto error;
3793 }
3794 if (skip)
3795 ++cmd;
3796 else
3797 {
3798 /* Only accept a mark in another file when it is
3799 * used by itself: ":'M". */
3800 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3801 ++cmd;
3802 if (fp == (pos_T *)-1)
3803 /* Jumped to another file. */
3804 lnum = curwin->w_cursor.lnum;
3805 else
3806 {
3807 if (check_mark(fp) == FAIL)
3808 {
3809 cmd = NULL;
3810 goto error;
3811 }
3812 lnum = fp->lnum;
3813 }
3814 }
3815 break;
3816
3817 case '/':
3818 case '?': /* '/' or '?' - search */
3819 c = *cmd++;
3820 if (skip) /* skip "/pat/" */
3821 {
3822 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3823 if (*cmd == c)
3824 ++cmd;
3825 }
3826 else
3827 {
3828 pos = curwin->w_cursor; /* save curwin->w_cursor */
3829 /*
3830 * When '/' or '?' follows another address, start
3831 * from there.
3832 */
3833 if (lnum != MAXLNUM)
3834 curwin->w_cursor.lnum = lnum;
3835 /*
3836 * Start a forward search at the end of the line.
3837 * Start a backward search at the start of the line.
3838 * This makes sure we never match in the current
3839 * line, and can match anywhere in the
3840 * next/previous line.
3841 */
3842 if (c == '/')
3843 curwin->w_cursor.col = MAXCOL;
3844 else
3845 curwin->w_cursor.col = 0;
3846 searchcmdlen = 0;
3847 if (!do_search(NULL, c, cmd, 1L,
3848 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3849 {
3850 curwin->w_cursor = pos;
3851 cmd = NULL;
3852 goto error;
3853 }
3854 lnum = curwin->w_cursor.lnum;
3855 curwin->w_cursor = pos;
3856 /* adjust command string pointer */
3857 cmd += searchcmdlen;
3858 }
3859 break;
3860
3861 case '\\': /* "\?", "\/" or "\&", repeat search */
3862 ++cmd;
3863 if (*cmd == '&')
3864 i = RE_SUBST;
3865 else if (*cmd == '?' || *cmd == '/')
3866 i = RE_SEARCH;
3867 else
3868 {
3869 EMSG(_(e_backslash));
3870 cmd = NULL;
3871 goto error;
3872 }
3873
3874 if (!skip)
3875 {
3876 /*
3877 * When search follows another address, start from
3878 * there.
3879 */
3880 if (lnum != MAXLNUM)
3881 pos.lnum = lnum;
3882 else
3883 pos.lnum = curwin->w_cursor.lnum;
3884
3885 /*
3886 * Start the search just like for the above
3887 * do_search().
3888 */
3889 if (*cmd != '?')
3890 pos.col = MAXCOL;
3891 else
3892 pos.col = 0;
3893 if (searchit(curwin, curbuf, &pos,
3894 *cmd == '?' ? BACKWARD : FORWARD,
3895 (char_u *)"", 1L,
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003896 SEARCH_MSG + SEARCH_START,
3897 i, (linenr_T)0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 lnum = pos.lnum;
3899 else
3900 {
3901 cmd = NULL;
3902 goto error;
3903 }
3904 }
3905 ++cmd;
3906 break;
3907
3908 default:
3909 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3910 lnum = getdigits(&cmd);
3911 }
3912
3913 for (;;)
3914 {
3915 cmd = skipwhite(cmd);
3916 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3917 break;
3918
3919 if (lnum == MAXLNUM)
3920 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3921 if (VIM_ISDIGIT(*cmd))
3922 i = '+'; /* "number" is same as "+number" */
3923 else
3924 i = *cmd++;
3925 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3926 n = 1;
3927 else
3928 n = getdigits(&cmd);
3929 if (i == '-')
3930 lnum -= n;
3931 else
3932 lnum += n;
3933 }
3934 } while (*cmd == '/' || *cmd == '?');
3935
3936error:
3937 *ptr = cmd;
3938 return lnum;
3939}
3940
3941/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003942 * Get flags from an Ex command argument.
3943 */
3944 static void
3945get_flags(eap)
3946 exarg_T *eap;
3947{
3948 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3949 {
3950 if (*eap->arg == 'l')
3951 eap->flags |= EXFLAG_LIST;
3952 else if (*eap->arg == 'p')
3953 eap->flags |= EXFLAG_PRINT;
3954 else
3955 eap->flags |= EXFLAG_NR;
3956 eap->arg = skipwhite(eap->arg + 1);
3957 }
3958}
3959
3960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 * Function called for command which is Not Implemented. NI!
3962 */
3963 void
3964ex_ni(eap)
3965 exarg_T *eap;
3966{
3967 if (!eap->skip)
3968 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3969}
3970
3971#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003972 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973/*
3974 * Function called for script command which is Not Implemented. NI!
3975 * Skips over ":perl <<EOF" constructs.
3976 */
3977 static void
3978ex_script_ni(eap)
3979 exarg_T *eap;
3980{
3981 if (!eap->skip)
3982 ex_ni(eap);
3983 else
3984 vim_free(script_get(eap, eap->arg));
3985}
3986#endif
3987
3988/*
3989 * Check range in Ex command for validity.
3990 * Return NULL when valid, error message when invalid.
3991 */
3992 static char_u *
3993invalid_range(eap)
3994 exarg_T *eap;
3995{
3996 if ( eap->line1 < 0
3997 || eap->line2 < 0
3998 || eap->line1 > eap->line2
3999 || ((eap->argt & RANGE)
4000 && !(eap->argt & NOTADR)
4001 && eap->line2 > curbuf->b_ml.ml_line_count
4002#ifdef FEAT_DIFF
4003 + (eap->cmdidx == CMD_diffget)
4004#endif
4005 ))
4006 return (char_u *)_(e_invrange);
4007 return NULL;
4008}
4009
4010/*
4011 * Correct the range for zero line number, if required.
4012 */
4013 static void
4014correct_range(eap)
4015 exarg_T *eap;
4016{
4017 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4018 {
4019 if (eap->line1 == 0)
4020 eap->line1 = 1;
4021 if (eap->line2 == 0)
4022 eap->line2 = 1;
4023 }
4024}
4025
Bram Moolenaar748bf032005-02-02 23:04:36 +00004026#ifdef FEAT_QUICKFIX
4027static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4028
4029/*
4030 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4031 * pattern. Otherwise return eap->arg.
4032 */
4033 static char_u *
4034skip_grep_pat(eap)
4035 exarg_T *eap;
4036{
4037 char_u *p = eap->arg;
4038
Bram Moolenaara37420f2006-02-04 22:37:47 +00004039 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4040 || eap->cmdidx == CMD_vimgrepadd
4041 || eap->cmdidx == CMD_lvimgrepadd
4042 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004043 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004044 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004045 if (p == NULL)
4046 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004047 }
4048 return p;
4049}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004050
4051/*
4052 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4053 * in the command line, so that things like % get expanded.
4054 */
4055 static char_u *
4056replace_makeprg(eap, p, cmdlinep)
4057 exarg_T *eap;
4058 char_u *p;
4059 char_u **cmdlinep;
4060{
4061 char_u *new_cmdline;
4062 char_u *program;
4063 char_u *pos;
4064 char_u *ptr;
4065 int len;
4066 int i;
4067
4068 /*
4069 * Don't do it when ":vimgrep" is used for ":grep".
4070 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004071 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4072 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4073 || eap->cmdidx == CMD_grepadd
4074 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004075 && !grep_internal(eap->cmdidx))
4076 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004077 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4078 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004079 {
4080 if (*curbuf->b_p_gp == NUL)
4081 program = p_gp;
4082 else
4083 program = curbuf->b_p_gp;
4084 }
4085 else
4086 {
4087 if (*curbuf->b_p_mp == NUL)
4088 program = p_mp;
4089 else
4090 program = curbuf->b_p_mp;
4091 }
4092
4093 p = skipwhite(p);
4094
4095 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4096 {
4097 /* replace $* by given arguments */
4098 i = 1;
4099 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4100 ++i;
4101 len = (int)STRLEN(p);
4102 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4103 if (new_cmdline == NULL)
4104 return NULL; /* out of memory */
4105 ptr = new_cmdline;
4106 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4107 {
4108 i = (int)(pos - program);
4109 STRNCPY(ptr, program, i);
4110 STRCPY(ptr += i, p);
4111 ptr += len;
4112 program = pos + 2;
4113 }
4114 STRCPY(ptr, program);
4115 }
4116 else
4117 {
4118 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4119 if (new_cmdline == NULL)
4120 return NULL; /* out of memory */
4121 STRCPY(new_cmdline, program);
4122 STRCAT(new_cmdline, " ");
4123 STRCAT(new_cmdline, p);
4124 }
4125 msg_make(p);
4126
4127 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4128 vim_free(*cmdlinep);
4129 *cmdlinep = new_cmdline;
4130 p = new_cmdline;
4131 }
4132 return p;
4133}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004134#endif
4135
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136/*
4137 * Expand file name in Ex command argument.
4138 * Return FAIL for failure, OK otherwise.
4139 */
4140 int
4141expand_filename(eap, cmdlinep, errormsgp)
4142 exarg_T *eap;
4143 char_u **cmdlinep;
4144 char_u **errormsgp;
4145{
4146 int has_wildcards; /* need to expand wildcards */
4147 char_u *repl;
4148 int srclen;
4149 char_u *p;
4150 int n;
4151
Bram Moolenaar748bf032005-02-02 23:04:36 +00004152#ifdef FEAT_QUICKFIX
4153 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4154 p = skip_grep_pat(eap);
4155#else
4156 p = eap->arg;
4157#endif
4158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 /*
4160 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4161 * the file name contains a wildcard it should not cause expanding.
4162 * (it will be expanded anyway if there is a wildcard before replacing).
4163 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004164 has_wildcards = mch_has_wildcard(p);
4165 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004167#ifdef FEAT_EVAL
4168 /* Skip over `=expr`, wildcards in it are not expanded. */
4169 if (p[0] == '`' && p[1] == '=')
4170 {
4171 p += 2;
4172 (void)skip_expr(&p);
4173 if (*p == '`')
4174 ++p;
4175 continue;
4176 }
4177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 /*
4179 * Quick check if this cannot be the start of a special string.
4180 * Also removes backslash before '%', '#' and '<'.
4181 */
4182 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4183 {
4184 ++p;
4185 continue;
4186 }
4187
4188 /*
4189 * Try to find a match at this position.
4190 */
4191 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4192 if (*errormsgp != NULL) /* error detected */
4193 return FAIL;
4194 if (repl == NULL) /* no match found */
4195 {
4196 p += srclen;
4197 continue;
4198 }
4199
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004200 /* Wildcards won't be expanded below, the replacement is taken
4201 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4202 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4203 {
4204 char_u *l = repl;
4205
4206 repl = expand_env_save(repl);
4207 vim_free(l);
4208 }
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 /* Need to escape white space et al. with a backslash. Don't do this
4211 * for shell commands (may have to use quotes instead). Don't do this
4212 * for non-unix systems when there is a single argument (spaces don't
4213 * separate arguments then). */
4214 if (!eap->usefilter
4215 && eap->cmdidx != CMD_bang
4216 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004217 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004219 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004221 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#ifndef UNIX
4223 && !(eap->argt & NOSPC)
4224#endif
4225 )
4226 {
4227 char_u *l;
4228#ifdef BACKSLASH_IN_FILENAME
4229 /* Don't escape a backslash here, because rem_backslash() doesn't
4230 * remove it later. */
4231 static char_u *nobslash = (char_u *)" \t\"|";
4232# define ESCAPE_CHARS nobslash
4233#else
4234# define ESCAPE_CHARS escape_chars
4235#endif
4236
4237 for (l = repl; *l; ++l)
4238 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4239 {
4240 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4241 if (l != NULL)
4242 {
4243 vim_free(repl);
4244 repl = l;
4245 }
4246 break;
4247 }
4248 }
4249
4250 /* For a shell command a '!' must be escaped. */
4251 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004252 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
4254 char_u *l;
4255
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004256 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (l != NULL)
4258 {
4259 vim_free(repl);
4260 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004261 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (strstr((char *)p_sh, "sh") != NULL)
4263 {
4264 l = vim_strsave_escaped(repl, (char_u *)"!");
4265 if (l != NULL)
4266 {
4267 vim_free(repl);
4268 repl = l;
4269 }
4270 }
4271 }
4272 }
4273
4274 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4275 vim_free(repl);
4276 if (p == NULL)
4277 return FAIL;
4278 }
4279
4280 /*
4281 * One file argument: Expand wildcards.
4282 * Don't do this with ":r !command" or ":w !command".
4283 */
4284 if ((eap->argt & NOSPC) && !eap->usefilter)
4285 {
4286 /*
4287 * May do this twice:
4288 * 1. Replace environment variables.
4289 * 2. Replace any other wildcards, remove backslashes.
4290 */
4291 for (n = 1; n <= 2; ++n)
4292 {
4293 if (n == 2)
4294 {
4295#ifdef UNIX
4296 /*
4297 * Only for Unix we check for more than one file name.
4298 * For other systems spaces are considered to be part
4299 * of the file name.
4300 * Only check here if there is no wildcard, otherwise
4301 * ExpandOne() will check for errors. This allows
4302 * ":e `ls ve*.c`" on Unix.
4303 */
4304 if (!has_wildcards)
4305 for (p = eap->arg; *p; ++p)
4306 {
4307 /* skip escaped characters */
4308 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4309 ++p;
4310 else if (vim_iswhite(*p))
4311 {
4312 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4313 return FAIL;
4314 }
4315 }
4316#endif
4317
4318 /*
4319 * Halve the number of backslashes (this is Vi compatible).
4320 * For Unix and OS/2, when wildcards are expanded, this is
4321 * done by ExpandOne() below.
4322 */
4323#if defined(UNIX) || defined(OS2)
4324 if (!has_wildcards)
4325#endif
4326 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328
4329 if (has_wildcards)
4330 {
4331 if (n == 1)
4332 {
4333 /*
4334 * First loop: May expand environment variables. This
4335 * can be done much faster with expand_env() than with
4336 * something else (e.g., calling a shell).
4337 * After expanding environment variables, check again
4338 * if there are still wildcards present.
4339 */
4340 if (vim_strchr(eap->arg, '$') != NULL
4341 || vim_strchr(eap->arg, '~') != NULL)
4342 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004343 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4344 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 has_wildcards = mch_has_wildcard(NameBuff);
4346 p = NameBuff;
4347 }
4348 else
4349 p = NULL;
4350 }
4351 else /* n == 2 */
4352 {
4353 expand_T xpc;
4354
4355 ExpandInit(&xpc);
4356 xpc.xp_context = EXPAND_FILES;
4357 p = ExpandOne(&xpc, eap->arg, NULL,
4358 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4359 WILD_EXPAND_FREE);
4360 ExpandCleanup(&xpc);
4361 if (p == NULL)
4362 return FAIL;
4363 }
4364 if (p != NULL)
4365 {
4366 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4367 p, cmdlinep);
4368 if (n == 2) /* p came from ExpandOne() */
4369 vim_free(p);
4370 }
4371 }
4372 }
4373 }
4374 return OK;
4375}
4376
4377/*
4378 * Replace part of the command line, keeping eap->cmd, eap->arg and
4379 * eap->nextcmd correct.
4380 * "src" points to the part that is to be replaced, of length "srclen".
4381 * "repl" is the replacement string.
4382 * Returns a pointer to the character after the replaced string.
4383 * Returns NULL for failure.
4384 */
4385 static char_u *
4386repl_cmdline(eap, src, srclen, repl, cmdlinep)
4387 exarg_T *eap;
4388 char_u *src;
4389 int srclen;
4390 char_u *repl;
4391 char_u **cmdlinep;
4392{
4393 int len;
4394 int i;
4395 char_u *new_cmdline;
4396
4397 /*
4398 * The new command line is build in new_cmdline[].
4399 * First allocate it.
4400 * Careful: a "+cmd" argument may have been NUL terminated.
4401 */
4402 len = (int)STRLEN(repl);
4403 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004404 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4406 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4407 return NULL; /* out of memory! */
4408
4409 /*
4410 * Copy the stuff before the expanded part.
4411 * Copy the expanded stuff.
4412 * Copy what came after the expanded part.
4413 * Copy the next commands, if there are any.
4414 */
4415 i = (int)(src - *cmdlinep); /* length of part before match */
4416 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 mch_memmove(new_cmdline + i, repl, (size_t)len);
4419 i += len; /* remember the end of the string */
4420 STRCPY(new_cmdline + i, src + srclen);
4421 src = new_cmdline + i; /* remember where to continue */
4422
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004423 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
4425 i = (int)STRLEN(new_cmdline) + 1;
4426 STRCPY(new_cmdline + i, eap->nextcmd);
4427 eap->nextcmd = new_cmdline + i;
4428 }
4429 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4430 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4431 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4432 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4433 vim_free(*cmdlinep);
4434 *cmdlinep = new_cmdline;
4435
4436 return src;
4437}
4438
4439/*
4440 * Check for '|' to separate commands and '"' to start comments.
4441 */
4442 void
4443separate_nextcmd(eap)
4444 exarg_T *eap;
4445{
4446 char_u *p;
4447
Bram Moolenaar86b68352004-12-27 21:59:20 +00004448#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004449 p = skip_grep_pat(eap);
4450#else
4451 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004452#endif
4453
4454 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
4456 if (*p == Ctrl_V)
4457 {
4458 if (eap->argt & (USECTRLV | XFILE))
4459 ++p; /* skip CTRL-V and next char */
4460 else
4461 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4462 if (*p == NUL) /* stop at NUL after CTRL-V */
4463 break;
4464 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004465
4466#ifdef FEAT_EVAL
4467 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004468 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004469 {
4470 p += 2;
4471 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004472 }
4473#endif
4474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 /* Check for '"': start of comment or '|': next command */
4476 /* :@" and :*" do not start a comment!
4477 * :redir @" doesn't either. */
4478 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4479 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4480 || p != eap->arg)
4481 && (eap->cmdidx != CMD_redir
4482 || p != eap->arg + 1 || p[-1] != '@'))
4483 || *p == '|' || *p == '\n')
4484 {
4485 /*
4486 * We remove the '\' before the '|', unless USECTRLV is used
4487 * AND 'b' is present in 'cpoptions'.
4488 */
4489 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4490 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4491 {
4492 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4493 --p;
4494 }
4495 else
4496 {
4497 eap->nextcmd = check_nextcmd(p);
4498 *p = NUL;
4499 break;
4500 }
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4505 del_trailing_spaces(eap->arg);
4506}
4507
4508/*
4509 * get + command from ex argument
4510 */
4511 static char_u *
4512getargcmd(argp)
4513 char_u **argp;
4514{
4515 char_u *arg = *argp;
4516 char_u *command = NULL;
4517
4518 if (*arg == '+') /* +[command] */
4519 {
4520 ++arg;
4521 if (vim_isspace(*arg))
4522 command = dollar_command;
4523 else
4524 {
4525 command = arg;
4526 arg = skip_cmd_arg(command, TRUE);
4527 if (*arg != NUL)
4528 *arg++ = NUL; /* terminate command with NUL */
4529 }
4530
4531 arg = skipwhite(arg); /* skip over spaces */
4532 *argp = arg;
4533 }
4534 return command;
4535}
4536
4537/*
4538 * Find end of "+command" argument. Skip over "\ " and "\\".
4539 */
4540 static char_u *
4541skip_cmd_arg(p, rembs)
4542 char_u *p;
4543 int rembs; /* TRUE to halve the number of backslashes */
4544{
4545 while (*p && !vim_isspace(*p))
4546 {
4547 if (*p == '\\' && p[1] != NUL)
4548 {
4549 if (rembs)
4550 mch_memmove(p, p + 1, STRLEN(p));
4551 else
4552 ++p;
4553 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004554 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556 return p;
4557}
4558
4559/*
4560 * Get "++opt=arg" argument.
4561 * Return FAIL or OK.
4562 */
4563 static int
4564getargopt(eap)
4565 exarg_T *eap;
4566{
4567 char_u *arg = eap->arg + 2;
4568 int *pp = NULL;
4569#ifdef FEAT_MBYTE
4570 char_u *p;
4571#endif
4572
4573 /* ":edit ++[no]bin[ary] file" */
4574 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4575 {
4576 if (*arg == 'n')
4577 {
4578 arg += 2;
4579 eap->force_bin = FORCE_NOBIN;
4580 }
4581 else
4582 eap->force_bin = FORCE_BIN;
4583 if (!checkforcmd(&arg, "binary", 3))
4584 return FAIL;
4585 eap->arg = skipwhite(arg);
4586 return OK;
4587 }
4588
4589 if (STRNCMP(arg, "ff", 2) == 0)
4590 {
4591 arg += 2;
4592 pp = &eap->force_ff;
4593 }
4594 else if (STRNCMP(arg, "fileformat", 10) == 0)
4595 {
4596 arg += 10;
4597 pp = &eap->force_ff;
4598 }
4599#ifdef FEAT_MBYTE
4600 else if (STRNCMP(arg, "enc", 3) == 0)
4601 {
4602 arg += 3;
4603 pp = &eap->force_enc;
4604 }
4605 else if (STRNCMP(arg, "encoding", 8) == 0)
4606 {
4607 arg += 8;
4608 pp = &eap->force_enc;
4609 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004610 else if (STRNCMP(arg, "bad", 3) == 0)
4611 {
4612 arg += 3;
4613 pp = &eap->bad_char;
4614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#endif
4616
4617 if (pp == NULL || *arg != '=')
4618 return FAIL;
4619
4620 ++arg;
4621 *pp = (int)(arg - eap->cmd);
4622 arg = skip_cmd_arg(arg, FALSE);
4623 eap->arg = skipwhite(arg);
4624 *arg = NUL;
4625
4626#ifdef FEAT_MBYTE
4627 if (pp == &eap->force_ff)
4628 {
4629#endif
4630 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4631 return FAIL;
4632#ifdef FEAT_MBYTE
4633 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004634 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
4636 /* Make 'fileencoding' lower case. */
4637 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4638 *p = TOLOWER_ASC(*p);
4639 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004640 else
4641 {
4642 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4643 * "drop". */
4644 p = eap->cmd + eap->bad_char;
4645 if (STRICMP(p, "keep") == 0)
4646 eap->bad_char = BAD_KEEP;
4647 else if (STRICMP(p, "drop") == 0)
4648 eap->bad_char = BAD_DROP;
4649 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4650 eap->bad_char = *p;
4651 else
4652 return FAIL;
4653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#endif
4655
4656 return OK;
4657}
4658
4659/*
4660 * ":abbreviate" and friends.
4661 */
4662 static void
4663ex_abbreviate(eap)
4664 exarg_T *eap;
4665{
4666 do_exmap(eap, TRUE); /* almost the same as mapping */
4667}
4668
4669/*
4670 * ":map" and friends.
4671 */
4672 static void
4673ex_map(eap)
4674 exarg_T *eap;
4675{
4676 /*
4677 * If we are sourcing .exrc or .vimrc in current directory we
4678 * print the mappings for security reasons.
4679 */
4680 if (secure)
4681 {
4682 secure = 2;
4683 msg_outtrans(eap->cmd);
4684 msg_putchar('\n');
4685 }
4686 do_exmap(eap, FALSE);
4687}
4688
4689/*
4690 * ":unmap" and friends.
4691 */
4692 static void
4693ex_unmap(eap)
4694 exarg_T *eap;
4695{
4696 do_exmap(eap, FALSE);
4697}
4698
4699/*
4700 * ":mapclear" and friends.
4701 */
4702 static void
4703ex_mapclear(eap)
4704 exarg_T *eap;
4705{
4706 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4707}
4708
4709/*
4710 * ":abclear" and friends.
4711 */
4712 static void
4713ex_abclear(eap)
4714 exarg_T *eap;
4715{
4716 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4717}
4718
4719#ifdef FEAT_AUTOCMD
4720 static void
4721ex_autocmd(eap)
4722 exarg_T *eap;
4723{
4724 /*
4725 * Disallow auto commands from .exrc and .vimrc in current
4726 * directory for security reasons.
4727 */
4728 if (secure)
4729 {
4730 secure = 2;
4731 eap->errmsg = e_curdir;
4732 }
4733 else if (eap->cmdidx == CMD_autocmd)
4734 do_autocmd(eap->arg, eap->forceit);
4735 else
4736 do_augroup(eap->arg, eap->forceit);
4737}
4738
4739/*
4740 * ":doautocmd": Apply the automatic commands to the current buffer.
4741 */
4742 static void
4743ex_doautocmd(eap)
4744 exarg_T *eap;
4745{
4746 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004747 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748}
4749#endif
4750
4751#ifdef FEAT_LISTCMDS
4752/*
4753 * :[N]bunload[!] [N] [bufname] unload buffer
4754 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4755 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4756 */
4757 static void
4758ex_bunload(eap)
4759 exarg_T *eap;
4760{
4761 eap->errmsg = do_bufdel(
4762 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4763 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4764 : DOBUF_UNLOAD, eap->arg,
4765 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4766}
4767
4768/*
4769 * :[N]buffer [N] to buffer N
4770 * :[N]sbuffer [N] to buffer N
4771 */
4772 static void
4773ex_buffer(eap)
4774 exarg_T *eap;
4775{
4776 if (*eap->arg)
4777 eap->errmsg = e_trailing;
4778 else
4779 {
4780 if (eap->addr_count == 0) /* default is current buffer */
4781 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4782 else
4783 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4784 }
4785}
4786
4787/*
4788 * :[N]bmodified [N] to next mod. buffer
4789 * :[N]sbmodified [N] to next mod. buffer
4790 */
4791 static void
4792ex_bmodified(eap)
4793 exarg_T *eap;
4794{
4795 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4796}
4797
4798/*
4799 * :[N]bnext [N] to next buffer
4800 * :[N]sbnext [N] split and to next buffer
4801 */
4802 static void
4803ex_bnext(eap)
4804 exarg_T *eap;
4805{
4806 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4807}
4808
4809/*
4810 * :[N]bNext [N] to previous buffer
4811 * :[N]bprevious [N] to previous buffer
4812 * :[N]sbNext [N] split and to previous buffer
4813 * :[N]sbprevious [N] split and to previous buffer
4814 */
4815 static void
4816ex_bprevious(eap)
4817 exarg_T *eap;
4818{
4819 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4820}
4821
4822/*
4823 * :brewind to first buffer
4824 * :bfirst to first buffer
4825 * :sbrewind split and to first buffer
4826 * :sbfirst split and to first buffer
4827 */
4828 static void
4829ex_brewind(eap)
4830 exarg_T *eap;
4831{
4832 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4833}
4834
4835/*
4836 * :blast to last buffer
4837 * :sblast split and to last buffer
4838 */
4839 static void
4840ex_blast(eap)
4841 exarg_T *eap;
4842{
4843 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4844}
4845#endif
4846
4847 int
4848ends_excmd(c)
4849 int c;
4850{
4851 return (c == NUL || c == '|' || c == '"' || c == '\n');
4852}
4853
4854#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4855 || defined(PROTO)
4856/*
4857 * Return the next command, after the first '|' or '\n'.
4858 * Return NULL if not found.
4859 */
4860 char_u *
4861find_nextcmd(p)
4862 char_u *p;
4863{
4864 while (*p != '|' && *p != '\n')
4865 {
4866 if (*p == NUL)
4867 return NULL;
4868 ++p;
4869 }
4870 return (p + 1);
4871}
4872#endif
4873
4874/*
4875 * Check if *p is a separator between Ex commands.
4876 * Return NULL if it isn't, (p + 1) if it is.
4877 */
4878 char_u *
4879check_nextcmd(p)
4880 char_u *p;
4881{
4882 p = skipwhite(p);
4883 if (*p == '|' || *p == '\n')
4884 return (p + 1);
4885 else
4886 return NULL;
4887}
4888
4889/*
4890 * - if there are more files to edit
4891 * - and this is the last window
4892 * - and forceit not used
4893 * - and not repeated twice on a row
4894 * return FAIL and give error message if 'message' TRUE
4895 * return OK otherwise
4896 */
4897 static int
4898check_more(message, forceit)
4899 int message; /* when FALSE check only, no messages */
4900 int forceit;
4901{
4902 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4903
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004904 if (!forceit && only_one_window()
4905 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
4907 if (message)
4908 {
4909#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4910 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4911 {
4912 char_u buff[IOSIZE];
4913
4914 if (n == 1)
4915 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4916 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004917 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 _("%d more files to edit. Quit anyway?"), n);
4919 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4920 return OK;
4921 return FAIL;
4922 }
4923#endif
4924 if (n == 1)
4925 EMSG(_("E173: 1 more file to edit"));
4926 else
4927 EMSGN(_("E173: %ld more files to edit"), n);
4928 quitmore = 2; /* next try to quit is allowed */
4929 }
4930 return FAIL;
4931 }
4932 return OK;
4933}
4934
4935#ifdef FEAT_CMDL_COMPL
4936/*
4937 * Function given to ExpandGeneric() to obtain the list of command names.
4938 */
4939/*ARGSUSED*/
4940 char_u *
4941get_command_name(xp, idx)
4942 expand_T *xp;
4943 int idx;
4944{
4945 if (idx >= (int)CMD_SIZE)
4946# ifdef FEAT_USR_CMDS
4947 return get_user_command_name(idx);
4948# else
4949 return NULL;
4950# endif
4951 return cmdnames[idx].cmd_name;
4952}
4953#endif
4954
4955#if defined(FEAT_USR_CMDS) || defined(PROTO)
4956static 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));
4957static void uc_list __ARGS((char_u *name, size_t name_len));
4958static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4959static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4960static 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));
4961
4962 static int
4963uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4964 char_u *name;
4965 size_t name_len;
4966 char_u *rep;
4967 long argt;
4968 long def;
4969 int flags;
4970 int compl;
4971 char_u *compl_arg;
4972 int force;
4973{
4974 ucmd_T *cmd = NULL;
4975 char_u *p;
4976 int i;
4977 int cmp = 1;
4978 char_u *rep_buf = NULL;
4979 garray_T *gap;
4980
4981 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4982 if (rep_buf == NULL)
4983 {
4984 /* Can't replace termcodes - try using the string as is */
4985 rep_buf = vim_strsave(rep);
4986
4987 /* Give up if out of memory */
4988 if (rep_buf == NULL)
4989 return FAIL;
4990 }
4991
4992 /* get address of growarray: global or in curbuf */
4993 if (flags & UC_BUFFER)
4994 {
4995 gap = &curbuf->b_ucmds;
4996 if (gap->ga_itemsize == 0)
4997 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4998 }
4999 else
5000 gap = &ucmds;
5001
5002 /* Search for the command in the already defined commands. */
5003 for (i = 0; i < gap->ga_len; ++i)
5004 {
5005 size_t len;
5006
5007 cmd = USER_CMD_GA(gap, i);
5008 len = STRLEN(cmd->uc_name);
5009 cmp = STRNCMP(name, cmd->uc_name, name_len);
5010 if (cmp == 0)
5011 {
5012 if (name_len < len)
5013 cmp = -1;
5014 else if (name_len > len)
5015 cmp = 1;
5016 }
5017
5018 if (cmp == 0)
5019 {
5020 if (!force)
5021 {
5022 EMSG(_("E174: Command already exists: add ! to replace it"));
5023 goto fail;
5024 }
5025
5026 vim_free(cmd->uc_rep);
5027 cmd->uc_rep = 0;
5028 break;
5029 }
5030
5031 /* Stop as soon as we pass the name to add */
5032 if (cmp < 0)
5033 break;
5034 }
5035
5036 /* Extend the array unless we're replacing an existing command */
5037 if (cmp != 0)
5038 {
5039 if (ga_grow(gap, 1) != OK)
5040 goto fail;
5041 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5042 goto fail;
5043
5044 cmd = USER_CMD_GA(gap, i);
5045 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5046
5047 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 cmd->uc_name = p;
5050 }
5051
5052 cmd->uc_rep = rep_buf;
5053 cmd->uc_argt = argt;
5054 cmd->uc_def = def;
5055 cmd->uc_compl = compl;
5056#ifdef FEAT_EVAL
5057 cmd->uc_scriptID = current_SID;
5058# ifdef FEAT_CMDL_COMPL
5059 cmd->uc_compl_arg = compl_arg;
5060# endif
5061#endif
5062
5063 return OK;
5064
5065fail:
5066 vim_free(rep_buf);
5067#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5068 vim_free(compl_arg);
5069#endif
5070 return FAIL;
5071}
5072
5073/*
5074 * List of names for completion for ":command" with the EXPAND_ flag.
5075 * Must be alphabetical for completion.
5076 */
5077static struct
5078{
5079 int expand;
5080 char *name;
5081} command_complete[] =
5082{
5083 {EXPAND_AUGROUP, "augroup"},
5084 {EXPAND_BUFFERS, "buffer"},
5085 {EXPAND_COMMANDS, "command"},
5086#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5087 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005088 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089#endif
5090 {EXPAND_DIRECTORIES, "dir"},
5091 {EXPAND_ENV_VARS, "environment"},
5092 {EXPAND_EVENTS, "event"},
5093 {EXPAND_EXPRESSION, "expression"},
5094 {EXPAND_FILES, "file"},
5095 {EXPAND_FUNCTIONS, "function"},
5096 {EXPAND_HELP, "help"},
5097 {EXPAND_HIGHLIGHT, "highlight"},
5098 {EXPAND_MAPPINGS, "mapping"},
5099 {EXPAND_MENUS, "menu"},
5100 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005101 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {EXPAND_TAGS, "tag"},
5103 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5104 {EXPAND_USER_VARS, "var"},
5105 {0, NULL}
5106};
5107
5108 static void
5109uc_list(name, name_len)
5110 char_u *name;
5111 size_t name_len;
5112{
5113 int i, j;
5114 int found = FALSE;
5115 ucmd_T *cmd;
5116 int len;
5117 long a;
5118 garray_T *gap;
5119
5120 gap = &curbuf->b_ucmds;
5121 for (;;)
5122 {
5123 for (i = 0; i < gap->ga_len; ++i)
5124 {
5125 cmd = USER_CMD_GA(gap, i);
5126 a = cmd->uc_argt;
5127
5128 /* Skip commands which don't match the requested prefix */
5129 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5130 continue;
5131
5132 /* Put out the title first time */
5133 if (!found)
5134 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5135 found = TRUE;
5136 msg_putchar('\n');
5137 if (got_int)
5138 break;
5139
5140 /* Special cases */
5141 msg_putchar(a & BANG ? '!' : ' ');
5142 msg_putchar(a & REGSTR ? '"' : ' ');
5143 msg_putchar(gap != &ucmds ? 'b' : ' ');
5144 msg_putchar(' ');
5145
5146 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5147 len = (int)STRLEN(cmd->uc_name) + 4;
5148
5149 do {
5150 msg_putchar(' ');
5151 ++len;
5152 } while (len < 16);
5153
5154 len = 0;
5155
5156 /* Arguments */
5157 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5158 {
5159 case 0: IObuff[len++] = '0'; break;
5160 case (EXTRA): IObuff[len++] = '*'; break;
5161 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5162 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5163 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5164 }
5165
5166 do {
5167 IObuff[len++] = ' ';
5168 } while (len < 5);
5169
5170 /* Range */
5171 if (a & (RANGE|COUNT))
5172 {
5173 if (a & COUNT)
5174 {
5175 /* -count=N */
5176 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5177 len += (int)STRLEN(IObuff + len);
5178 }
5179 else if (a & DFLALL)
5180 IObuff[len++] = '%';
5181 else if (cmd->uc_def >= 0)
5182 {
5183 /* -range=N */
5184 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5185 len += (int)STRLEN(IObuff + len);
5186 }
5187 else
5188 IObuff[len++] = '.';
5189 }
5190
5191 do {
5192 IObuff[len++] = ' ';
5193 } while (len < 11);
5194
5195 /* Completion */
5196 for (j = 0; command_complete[j].expand != 0; ++j)
5197 if (command_complete[j].expand == cmd->uc_compl)
5198 {
5199 STRCPY(IObuff + len, command_complete[j].name);
5200 len += (int)STRLEN(IObuff + len);
5201 break;
5202 }
5203
5204 do {
5205 IObuff[len++] = ' ';
5206 } while (len < 21);
5207
5208 IObuff[len] = '\0';
5209 msg_outtrans(IObuff);
5210
5211 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005212#ifdef FEAT_EVAL
5213 if (p_verbose > 0)
5214 last_set_msg(cmd->uc_scriptID);
5215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 out_flush();
5217 ui_breakcheck();
5218 if (got_int)
5219 break;
5220 }
5221 if (gap == &ucmds || i < gap->ga_len)
5222 break;
5223 gap = &ucmds;
5224 }
5225
5226 if (!found)
5227 MSG(_("No user-defined commands found"));
5228}
5229
5230 static char_u *
5231uc_fun_cmd()
5232{
5233 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5234 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5235 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5236 0xb9, 0x7f, 0};
5237 int i;
5238
5239 for (i = 0; fcmd[i]; ++i)
5240 IObuff[i] = fcmd[i] - 0x40;
5241 IObuff[i] = 0;
5242 return IObuff;
5243}
5244
5245 static int
5246uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5247 char_u *attr;
5248 size_t len;
5249 long *argt;
5250 long *def;
5251 int *flags;
5252 int *compl;
5253 char_u **compl_arg;
5254{
5255 char_u *p;
5256
5257 if (len == 0)
5258 {
5259 EMSG(_("E175: No attribute specified"));
5260 return FAIL;
5261 }
5262
5263 /* First, try the simple attributes (no arguments) */
5264 if (STRNICMP(attr, "bang", len) == 0)
5265 *argt |= BANG;
5266 else if (STRNICMP(attr, "buffer", len) == 0)
5267 *flags |= UC_BUFFER;
5268 else if (STRNICMP(attr, "register", len) == 0)
5269 *argt |= REGSTR;
5270 else if (STRNICMP(attr, "bar", len) == 0)
5271 *argt |= TRLBAR;
5272 else
5273 {
5274 int i;
5275 char_u *val = NULL;
5276 size_t vallen = 0;
5277 size_t attrlen = len;
5278
5279 /* Look for the attribute name - which is the part before any '=' */
5280 for (i = 0; i < (int)len; ++i)
5281 {
5282 if (attr[i] == '=')
5283 {
5284 val = &attr[i + 1];
5285 vallen = len - i - 1;
5286 attrlen = i;
5287 break;
5288 }
5289 }
5290
5291 if (STRNICMP(attr, "nargs", attrlen) == 0)
5292 {
5293 if (vallen == 1)
5294 {
5295 if (*val == '0')
5296 /* Do nothing - this is the default */;
5297 else if (*val == '1')
5298 *argt |= (EXTRA | NOSPC | NEEDARG);
5299 else if (*val == '*')
5300 *argt |= EXTRA;
5301 else if (*val == '?')
5302 *argt |= (EXTRA | NOSPC);
5303 else if (*val == '+')
5304 *argt |= (EXTRA | NEEDARG);
5305 else
5306 goto wrong_nargs;
5307 }
5308 else
5309 {
5310wrong_nargs:
5311 EMSG(_("E176: Invalid number of arguments"));
5312 return FAIL;
5313 }
5314 }
5315 else if (STRNICMP(attr, "range", attrlen) == 0)
5316 {
5317 *argt |= RANGE;
5318 if (vallen == 1 && *val == '%')
5319 *argt |= DFLALL;
5320 else if (val != NULL)
5321 {
5322 p = val;
5323 if (*def >= 0)
5324 {
5325two_count:
5326 EMSG(_("E177: Count cannot be specified twice"));
5327 return FAIL;
5328 }
5329
5330 *def = getdigits(&p);
5331 *argt |= (ZEROR | NOTADR);
5332
5333 if (p != val + vallen || vallen == 0)
5334 {
5335invalid_count:
5336 EMSG(_("E178: Invalid default value for count"));
5337 return FAIL;
5338 }
5339 }
5340 }
5341 else if (STRNICMP(attr, "count", attrlen) == 0)
5342 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005343 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
5345 if (val != NULL)
5346 {
5347 p = val;
5348 if (*def >= 0)
5349 goto two_count;
5350
5351 *def = getdigits(&p);
5352
5353 if (p != val + vallen)
5354 goto invalid_count;
5355 }
5356
5357 if (*def < 0)
5358 *def = 0;
5359 }
5360 else if (STRNICMP(attr, "complete", attrlen) == 0)
5361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 if (val == NULL)
5363 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005364 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 return FAIL;
5366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005368 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5369 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
5372 else
5373 {
5374 char_u ch = attr[len];
5375 attr[len] = '\0';
5376 EMSG2(_("E181: Invalid attribute: %s"), attr);
5377 attr[len] = ch;
5378 return FAIL;
5379 }
5380 }
5381
5382 return OK;
5383}
5384
5385 static void
5386ex_command(eap)
5387 exarg_T *eap;
5388{
5389 char_u *name;
5390 char_u *end;
5391 char_u *p;
5392 long argt = 0;
5393 long def = -1;
5394 int flags = 0;
5395 int compl = EXPAND_NOTHING;
5396 char_u *compl_arg = NULL;
5397 int has_attr = (eap->arg[0] == '-');
5398
5399 p = eap->arg;
5400
5401 /* Check for attributes */
5402 while (*p == '-')
5403 {
5404 ++p;
5405 end = skiptowhite(p);
5406 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5407 == FAIL)
5408 return;
5409 p = skipwhite(end);
5410 }
5411
5412 /* Get the name (if any) and skip to the following argument */
5413 name = p;
5414 if (ASCII_ISALPHA(*p))
5415 while (ASCII_ISALNUM(*p))
5416 ++p;
5417 if (!ends_excmd(*p) && !vim_iswhite(*p))
5418 {
5419 EMSG(_("E182: Invalid command name"));
5420 return;
5421 }
5422 end = p;
5423
5424 /* If there is nothing after the name, and no attributes were specified,
5425 * we are listing commands
5426 */
5427 p = skipwhite(end);
5428 if (!has_attr && ends_excmd(*p))
5429 {
5430 uc_list(name, end - name);
5431 }
5432 else if (!ASCII_ISUPPER(*name))
5433 {
5434 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5435 return;
5436 }
5437 else
5438 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5439 eap->forceit);
5440}
5441
5442/*
5443 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 * Clear all user commands, global and for current buffer.
5445 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005446/*ARGSUSED*/
5447 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448ex_comclear(eap)
5449 exarg_T *eap;
5450{
5451 uc_clear(&ucmds);
5452 uc_clear(&curbuf->b_ucmds);
5453}
5454
5455/*
5456 * Clear all user commands for "gap".
5457 */
5458 void
5459uc_clear(gap)
5460 garray_T *gap;
5461{
5462 int i;
5463 ucmd_T *cmd;
5464
5465 for (i = 0; i < gap->ga_len; ++i)
5466 {
5467 cmd = USER_CMD_GA(gap, i);
5468 vim_free(cmd->uc_name);
5469 vim_free(cmd->uc_rep);
5470# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5471 vim_free(cmd->uc_compl_arg);
5472# endif
5473 }
5474 ga_clear(gap);
5475}
5476
5477 static void
5478ex_delcommand(eap)
5479 exarg_T *eap;
5480{
5481 int i = 0;
5482 ucmd_T *cmd = NULL;
5483 int cmp = -1;
5484 garray_T *gap;
5485
5486 gap = &curbuf->b_ucmds;
5487 for (;;)
5488 {
5489 for (i = 0; i < gap->ga_len; ++i)
5490 {
5491 cmd = USER_CMD_GA(gap, i);
5492 cmp = STRCMP(eap->arg, cmd->uc_name);
5493 if (cmp <= 0)
5494 break;
5495 }
5496 if (gap == &ucmds || cmp == 0)
5497 break;
5498 gap = &ucmds;
5499 }
5500
5501 if (cmp != 0)
5502 {
5503 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5504 return;
5505 }
5506
5507 vim_free(cmd->uc_name);
5508 vim_free(cmd->uc_rep);
5509# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5510 vim_free(cmd->uc_compl_arg);
5511# endif
5512
5513 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
5515 if (i < gap->ga_len)
5516 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5517}
5518
5519 static char_u *
5520uc_split_args(arg, lenp)
5521 char_u *arg;
5522 size_t *lenp;
5523{
5524 char_u *buf;
5525 char_u *p;
5526 char_u *q;
5527 int len;
5528
5529 /* Precalculate length */
5530 p = arg;
5531 len = 2; /* Initial and final quotes */
5532
5533 while (*p)
5534 {
5535 if (p[0] == '\\' && vim_iswhite(p[1]))
5536 {
5537 len += 1;
5538 p += 2;
5539 }
5540 else if (*p == '\\' || *p == '"')
5541 {
5542 len += 2;
5543 p += 1;
5544 }
5545 else if (vim_iswhite(*p))
5546 {
5547 p = skipwhite(p);
5548 if (*p == NUL)
5549 break;
5550 len += 3; /* "," */
5551 }
5552 else
5553 {
5554 ++len;
5555 ++p;
5556 }
5557 }
5558
5559 buf = alloc(len + 1);
5560 if (buf == NULL)
5561 {
5562 *lenp = 0;
5563 return buf;
5564 }
5565
5566 p = arg;
5567 q = buf;
5568 *q++ = '"';
5569 while (*p)
5570 {
5571 if (p[0] == '\\' && vim_iswhite(p[1]))
5572 {
5573 *q++ = p[1];
5574 p += 2;
5575 }
5576 else if (*p == '\\' || *p == '"')
5577 {
5578 *q++ = '\\';
5579 *q++ = *p++;
5580 }
5581 else if (vim_iswhite(*p))
5582 {
5583 p = skipwhite(p);
5584 if (*p == NUL)
5585 break;
5586 *q++ = '"';
5587 *q++ = ',';
5588 *q++ = '"';
5589 }
5590 else
5591 {
5592 *q++ = *p++;
5593 }
5594 }
5595 *q++ = '"';
5596 *q = 0;
5597
5598 *lenp = len;
5599 return buf;
5600}
5601
5602/*
5603 * Check for a <> code in a user command.
5604 * "code" points to the '<'. "len" the length of the <> (inclusive).
5605 * "buf" is where the result is to be added.
5606 * "split_buf" points to a buffer used for splitting, caller should free it.
5607 * "split_len" is the length of what "split_buf" contains.
5608 * Returns the length of the replacement, which has been added to "buf".
5609 * Returns -1 if there was no match, and only the "<" has been copied.
5610 */
5611 static size_t
5612uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5613 char_u *code;
5614 size_t len;
5615 char_u *buf;
5616 ucmd_T *cmd; /* the user command we're expanding */
5617 exarg_T *eap; /* ex arguments */
5618 char_u **split_buf;
5619 size_t *split_len;
5620{
5621 size_t result = 0;
5622 char_u *p = code + 1;
5623 size_t l = len - 2;
5624 int quote = 0;
5625 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5626 ct_LT, ct_NONE } type = ct_NONE;
5627
5628 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5629 {
5630 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5631 p += 2;
5632 l -= 2;
5633 }
5634
5635 if (l < 1)
5636 type = ct_NONE;
5637 else if (STRNICMP(p, "args", l) == 0)
5638 type = ct_ARGS;
5639 else if (STRNICMP(p, "bang", l) == 0)
5640 type = ct_BANG;
5641 else if (STRNICMP(p, "count", l) == 0)
5642 type = ct_COUNT;
5643 else if (STRNICMP(p, "line1", l) == 0)
5644 type = ct_LINE1;
5645 else if (STRNICMP(p, "line2", l) == 0)
5646 type = ct_LINE2;
5647 else if (STRNICMP(p, "lt", l) == 0)
5648 type = ct_LT;
5649 else if (STRNICMP(p, "register", l) == 0)
5650 type = ct_REGISTER;
5651
5652 switch (type)
5653 {
5654 case ct_ARGS:
5655 /* Simple case first */
5656 if (*eap->arg == NUL)
5657 {
5658 if (quote == 1)
5659 {
5660 result = 2;
5661 if (buf != NULL)
5662 STRCPY(buf, "''");
5663 }
5664 else
5665 result = 0;
5666 break;
5667 }
5668
5669 /* When specified there is a single argument don't split it.
5670 * Works for ":Cmd %" when % is "a b c". */
5671 if ((eap->argt & NOSPC) && quote == 2)
5672 quote = 1;
5673
5674 switch (quote)
5675 {
5676 case 0: /* No quoting, no splitting */
5677 result = STRLEN(eap->arg);
5678 if (buf != NULL)
5679 STRCPY(buf, eap->arg);
5680 break;
5681 case 1: /* Quote, but don't split */
5682 result = STRLEN(eap->arg) + 2;
5683 for (p = eap->arg; *p; ++p)
5684 {
5685 if (*p == '\\' || *p == '"')
5686 ++result;
5687 }
5688
5689 if (buf != NULL)
5690 {
5691 *buf++ = '"';
5692 for (p = eap->arg; *p; ++p)
5693 {
5694 if (*p == '\\' || *p == '"')
5695 *buf++ = '\\';
5696 *buf++ = *p;
5697 }
5698 *buf = '"';
5699 }
5700
5701 break;
5702 case 2: /* Quote and split */
5703 /* This is hard, so only do it once, and cache the result */
5704 if (*split_buf == NULL)
5705 *split_buf = uc_split_args(eap->arg, split_len);
5706
5707 result = *split_len;
5708 if (buf != NULL && result != 0)
5709 STRCPY(buf, *split_buf);
5710
5711 break;
5712 }
5713 break;
5714
5715 case ct_BANG:
5716 result = eap->forceit ? 1 : 0;
5717 if (quote)
5718 result += 2;
5719 if (buf != NULL)
5720 {
5721 if (quote)
5722 *buf++ = '"';
5723 if (eap->forceit)
5724 *buf++ = '!';
5725 if (quote)
5726 *buf = '"';
5727 }
5728 break;
5729
5730 case ct_LINE1:
5731 case ct_LINE2:
5732 case ct_COUNT:
5733 {
5734 char num_buf[20];
5735 long num = (type == ct_LINE1) ? eap->line1 :
5736 (type == ct_LINE2) ? eap->line2 :
5737 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5738 size_t num_len;
5739
5740 sprintf(num_buf, "%ld", num);
5741 num_len = STRLEN(num_buf);
5742 result = num_len;
5743
5744 if (quote)
5745 result += 2;
5746
5747 if (buf != NULL)
5748 {
5749 if (quote)
5750 *buf++ = '"';
5751 STRCPY(buf, num_buf);
5752 buf += num_len;
5753 if (quote)
5754 *buf = '"';
5755 }
5756
5757 break;
5758 }
5759
5760 case ct_REGISTER:
5761 result = eap->regname ? 1 : 0;
5762 if (quote)
5763 result += 2;
5764 if (buf != NULL)
5765 {
5766 if (quote)
5767 *buf++ = '\'';
5768 if (eap->regname)
5769 *buf++ = eap->regname;
5770 if (quote)
5771 *buf = '\'';
5772 }
5773 break;
5774
5775 case ct_LT:
5776 result = 1;
5777 if (buf != NULL)
5778 *buf = '<';
5779 break;
5780
5781 default:
5782 /* Not recognized: just copy the '<' and return -1. */
5783 result = (size_t)-1;
5784 if (buf != NULL)
5785 *buf = '<';
5786 break;
5787 }
5788
5789 return result;
5790}
5791
5792 static void
5793do_ucmd(eap)
5794 exarg_T *eap;
5795{
5796 char_u *buf;
5797 char_u *p;
5798 char_u *q;
5799
5800 char_u *start;
5801 char_u *end;
5802 size_t len, totlen;
5803
5804 size_t split_len = 0;
5805 char_u *split_buf = NULL;
5806 ucmd_T *cmd;
5807#ifdef FEAT_EVAL
5808 scid_T save_current_SID = current_SID;
5809#endif
5810
5811 if (eap->cmdidx == CMD_USER)
5812 cmd = USER_CMD(eap->useridx);
5813 else
5814 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5815
5816 /*
5817 * Replace <> in the command by the arguments.
5818 */
5819 buf = NULL;
5820 for (;;)
5821 {
5822 p = cmd->uc_rep;
5823 q = buf;
5824 totlen = 0;
5825 while ((start = vim_strchr(p, '<')) != NULL
5826 && (end = vim_strchr(start + 1, '>')) != NULL)
5827 {
5828 /* Include the '>' */
5829 ++end;
5830
5831 /* Take everything up to the '<' */
5832 len = start - p;
5833 if (buf == NULL)
5834 totlen += len;
5835 else
5836 {
5837 mch_memmove(q, p, len);
5838 q += len;
5839 }
5840
5841 len = uc_check_code(start, end - start, q, cmd, eap,
5842 &split_buf, &split_len);
5843 if (len == (size_t)-1)
5844 {
5845 /* no match, continue after '<' */
5846 p = start + 1;
5847 len = 1;
5848 }
5849 else
5850 p = end;
5851 if (buf == NULL)
5852 totlen += len;
5853 else
5854 q += len;
5855 }
5856 if (buf != NULL) /* second time here, finished */
5857 {
5858 STRCPY(q, p);
5859 break;
5860 }
5861
5862 totlen += STRLEN(p); /* Add on the trailing characters */
5863 buf = alloc((unsigned)(totlen + 1));
5864 if (buf == NULL)
5865 {
5866 vim_free(split_buf);
5867 return;
5868 }
5869 }
5870
5871#ifdef FEAT_EVAL
5872 current_SID = cmd->uc_scriptID;
5873#endif
5874 (void)do_cmdline(buf, eap->getline, eap->cookie,
5875 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5876#ifdef FEAT_EVAL
5877 current_SID = save_current_SID;
5878#endif
5879 vim_free(buf);
5880 vim_free(split_buf);
5881}
5882
5883# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5884 static char_u *
5885get_user_command_name(idx)
5886 int idx;
5887{
5888 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5889}
5890
5891/*
5892 * Function given to ExpandGeneric() to obtain the list of user command names.
5893 */
5894/*ARGSUSED*/
5895 char_u *
5896get_user_commands(xp, idx)
5897 expand_T *xp;
5898 int idx;
5899{
5900 if (idx < curbuf->b_ucmds.ga_len)
5901 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5902 idx -= curbuf->b_ucmds.ga_len;
5903 if (idx < ucmds.ga_len)
5904 return USER_CMD(idx)->uc_name;
5905 return NULL;
5906}
5907
5908/*
5909 * Function given to ExpandGeneric() to obtain the list of user command
5910 * attributes.
5911 */
5912/*ARGSUSED*/
5913 char_u *
5914get_user_cmd_flags(xp, idx)
5915 expand_T *xp;
5916 int idx;
5917{
5918 static char *user_cmd_flags[] =
5919 {"bang", "bar", "buffer", "complete", "count",
5920 "nargs", "range", "register"};
5921
5922 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5923 return NULL;
5924 return (char_u *)user_cmd_flags[idx];
5925}
5926
5927/*
5928 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5929 */
5930/*ARGSUSED*/
5931 char_u *
5932get_user_cmd_nargs(xp, idx)
5933 expand_T *xp;
5934 int idx;
5935{
5936 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5937
5938 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5939 return NULL;
5940 return (char_u *)user_cmd_nargs[idx];
5941}
5942
5943/*
5944 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5945 */
5946/*ARGSUSED*/
5947 char_u *
5948get_user_cmd_complete(xp, idx)
5949 expand_T *xp;
5950 int idx;
5951{
5952 return (char_u *)command_complete[idx].name;
5953}
5954# endif /* FEAT_CMDL_COMPL */
5955
5956#endif /* FEAT_USR_CMDS */
5957
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005958#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5959/*
5960 * Parse a completion argument "value[vallen]".
5961 * The detected completion goes in "*complp", argument type in "*argt".
5962 * When there is an argument, for function and user defined completion, it's
5963 * copied to allocated memory and stored in "*compl_arg".
5964 * Returns FAIL if something is wrong.
5965 */
5966 int
5967parse_compl_arg(value, vallen, complp, argt, compl_arg)
5968 char_u *value;
5969 int vallen;
5970 int *complp;
5971 long *argt;
5972 char_u **compl_arg;
5973{
5974 char_u *arg = NULL;
5975 size_t arglen = 0;
5976 int i;
5977 int valend = vallen;
5978
5979 /* Look for any argument part - which is the part after any ',' */
5980 for (i = 0; i < vallen; ++i)
5981 {
5982 if (value[i] == ',')
5983 {
5984 arg = &value[i + 1];
5985 arglen = vallen - i - 1;
5986 valend = i;
5987 break;
5988 }
5989 }
5990
5991 for (i = 0; command_complete[i].expand != 0; ++i)
5992 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005993 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005994 && STRNCMP(value, command_complete[i].name, valend) == 0)
5995 {
5996 *complp = command_complete[i].expand;
5997 if (command_complete[i].expand == EXPAND_BUFFERS)
5998 *argt |= BUFNAME;
5999 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6000 || command_complete[i].expand == EXPAND_FILES)
6001 *argt |= XFILE;
6002 break;
6003 }
6004 }
6005
6006 if (command_complete[i].expand == 0)
6007 {
6008 EMSG2(_("E180: Invalid complete value: %s"), value);
6009 return FAIL;
6010 }
6011
6012# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6013 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6014 && arg != NULL)
6015# else
6016 if (arg != NULL)
6017# endif
6018 {
6019 EMSG(_("E468: Completion argument only allowed for custom completion"));
6020 return FAIL;
6021 }
6022
6023# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6024 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6025 && arg == NULL)
6026 {
6027 EMSG(_("E467: Custom completion requires a function argument"));
6028 return FAIL;
6029 }
6030
6031 if (arg != NULL)
6032 *compl_arg = vim_strnsave(arg, (int)arglen);
6033# endif
6034 return OK;
6035}
6036#endif
6037
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 static void
6039ex_colorscheme(eap)
6040 exarg_T *eap;
6041{
6042 if (load_colors(eap->arg) == FAIL)
6043 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6044}
6045
6046 static void
6047ex_highlight(eap)
6048 exarg_T *eap;
6049{
6050 if (*eap->arg == NUL && eap->cmd[2] == '!')
6051 MSG(_("Greetings, Vim user!"));
6052 do_highlight(eap->arg, eap->forceit, FALSE);
6053}
6054
6055
6056/*
6057 * Call this function if we thought we were going to exit, but we won't
6058 * (because of an error). May need to restore the terminal mode.
6059 */
6060 void
6061not_exiting()
6062{
6063 exiting = FALSE;
6064 settmode(TMODE_RAW);
6065}
6066
6067/*
6068 * ":quit": quit current window, quit Vim if closed the last window.
6069 */
6070 static void
6071ex_quit(eap)
6072 exarg_T *eap;
6073{
6074#ifdef FEAT_CMDWIN
6075 if (cmdwin_type != 0)
6076 {
6077 cmdwin_result = Ctrl_C;
6078 return;
6079 }
6080#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006081 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006082 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006083 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006084 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006085 return;
6086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
6088#ifdef FEAT_NETBEANS_INTG
6089 netbeansForcedQuit = eap->forceit;
6090#endif
6091
6092 /*
6093 * If there are more files or windows we won't exit.
6094 */
6095 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6096 exiting = TRUE;
6097 if ((!P_HID(curbuf)
6098 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6099 || check_more(TRUE, eap->forceit) == FAIL
6100 || (only_one_window() && check_changed_any(eap->forceit)))
6101 {
6102 not_exiting();
6103 }
6104 else
6105 {
6106#ifdef FEAT_WINDOWS
6107 if (only_one_window()) /* quit last window */
6108#endif
6109 getout(0);
6110#ifdef FEAT_WINDOWS
6111# ifdef FEAT_GUI
6112 need_mouse_correct = TRUE;
6113# endif
6114 /* close window; may free buffer */
6115 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6116#endif
6117 }
6118}
6119
6120/*
6121 * ":cquit".
6122 */
6123/*ARGSUSED*/
6124 static void
6125ex_cquit(eap)
6126 exarg_T *eap;
6127{
6128 getout(1); /* this does not always pass on the exit code to the Manx
6129 compiler. why? */
6130}
6131
6132/*
6133 * ":qall": try to quit all windows
6134 */
6135 static void
6136ex_quit_all(eap)
6137 exarg_T *eap;
6138{
6139# ifdef FEAT_CMDWIN
6140 if (cmdwin_type != 0)
6141 {
6142 if (eap->forceit)
6143 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6144 else
6145 cmdwin_result = K_XF2;
6146 return;
6147 }
6148# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006149
6150 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006151 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006152 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006153 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006154 return;
6155 }
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 exiting = TRUE;
6158 if (eap->forceit || !check_changed_any(FALSE))
6159 getout(0);
6160 not_exiting();
6161}
6162
Bram Moolenaard9967712006-03-11 21:18:15 +00006163#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164/*
6165 * ":close": close current window, unless it is the last one
6166 */
6167 static void
6168ex_close(eap)
6169 exarg_T *eap;
6170{
6171# ifdef FEAT_CMDWIN
6172 if (cmdwin_type != 0)
6173 cmdwin_result = K_IGNORE;
6174 else
6175# endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006176 if (!text_locked())
Bram Moolenaarf740b292006-02-16 22:11:02 +00006177 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178}
6179
Bram Moolenaard9967712006-03-11 21:18:15 +00006180# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006181/*
6182 * ":pclose": Close any preview window.
6183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006185ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006187{
6188 win_T *win;
6189
6190 for (win = firstwin; win != NULL; win = win->w_next)
6191 if (win->w_p_pvw)
6192 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006193 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006194 break;
6195 }
6196}
Bram Moolenaard9967712006-03-11 21:18:15 +00006197# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198
Bram Moolenaarf740b292006-02-16 22:11:02 +00006199/*
6200 * Close window "win" and take care of handling closing the last window for a
6201 * modified buffer.
6202 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006203 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006204ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006205 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006207 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
6209 int need_hide;
6210 buf_T *buf = win->w_buffer;
6211
6212 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006215# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if ((p_confirm || cmdmod.confirm) && p_write)
6217 {
6218 dialog_changed(buf, FALSE);
6219 if (buf_valid(buf) && bufIsChanged(buf))
6220 return;
6221 need_hide = FALSE;
6222 }
6223 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 {
6226 EMSG(_(e_nowrtmsg));
6227 return;
6228 }
6229 }
6230
Bram Moolenaard9967712006-03-11 21:18:15 +00006231# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006233# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006234
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006236 if (tp == NULL)
6237 win_close(win, !need_hide && !P_HID(buf));
6238 else
6239 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240}
6241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006243 * ":tabclose": close current tab page, unless it is the last one.
6244 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 */
6246 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006247ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 exarg_T *eap;
6249{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006250 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006251 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006252
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006253# ifdef FEAT_CMDWIN
6254 if (cmdwin_type != 0)
6255 cmdwin_result = K_IGNORE;
6256 else
6257# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006258 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006259 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006262 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006263 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006264 tp = find_tabpage((int)eap->line2);
6265 if (tp == NULL)
6266 {
6267 beep_flush();
6268 return;
6269 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006270 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006271 {
6272 tabpage_close_other(tp, eap->forceit);
6273 return;
6274 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006275 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00006276 if (!text_locked())
6277 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006279
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006280 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006281 shell_new_rows();
6282}
6283
6284/*
6285 * ":tabonly": close all tab pages except the current one
6286 */
6287 static void
6288ex_tabonly(eap)
6289 exarg_T *eap;
6290{
6291 tabpage_T *tp;
6292 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006293 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006294
6295# ifdef FEAT_CMDWIN
6296 if (cmdwin_type != 0)
6297 cmdwin_result = K_IGNORE;
6298 else
6299# endif
6300 if (first_tabpage->tp_next == NULL)
6301 MSG(_("Already only one tab page"));
6302 else
6303 {
6304 /* Repeat this up to a 1000 times, because autocommands may mess
6305 * up the lists. */
6306 for (done = 0; done < 1000; ++done)
6307 {
6308 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6309 if (tp->tp_topframe != topframe)
6310 {
6311 tabpage_close_other(tp, eap->forceit);
6312 /* if we failed to close it quit */
6313 if (valid_tabpage(tp))
6314 done = 1000;
6315 /* start over, "tp" is now invalid */
6316 break;
6317 }
6318 if (first_tabpage->tp_next == NULL)
6319 break;
6320 }
6321 }
6322
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006323 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006324 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
6327/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006328 * Close the current tab page.
6329 */
6330 void
6331tabpage_close(forceit)
6332 int forceit;
6333{
6334 /* First close all the windows but the current one. If that worked then
6335 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006336 if (lastwin != firstwin)
6337 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006338 if (lastwin == firstwin)
6339 ex_win_close(forceit, curwin, NULL);
6340# ifdef FEAT_GUI
6341 need_mouse_correct = TRUE;
6342# endif
6343}
6344
6345/*
6346 * Close tab page "tp", which is not the current tab page.
6347 * Note that autocommands may make "tp" invalid.
6348 */
6349 void
6350tabpage_close_other(tp, forceit)
6351 tabpage_T *tp;
6352 int forceit;
6353{
6354 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006355 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006356
6357 /* Limit to 1000 windows, autocommands may add a window while we close
6358 * one. OK, so I'm paranoid... */
6359 while (++done < 1000)
6360 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006361 wp = tp->tp_firstwin;
6362 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006363
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006364 /* Autocommands may delete the tab page under our fingers and we may
6365 * fail to close a window with a modified buffer. */
6366 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006367 break;
6368 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006369 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006370}
6371
6372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 * ":only".
6374 */
6375 static void
6376ex_only(eap)
6377 exarg_T *eap;
6378{
6379# ifdef FEAT_GUI
6380 need_mouse_correct = TRUE;
6381# endif
6382 close_others(TRUE, eap->forceit);
6383}
6384
6385/*
6386 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006387 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006389 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390ex_all(eap)
6391 exarg_T *eap;
6392{
6393 if (eap->addr_count == 0)
6394 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006395 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396}
6397#endif /* FEAT_WINDOWS */
6398
6399 static void
6400ex_hide(eap)
6401 exarg_T *eap;
6402{
6403 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6404 eap->errmsg = e_invarg;
6405 else
6406 {
6407 /* ":hide" or ":hide | cmd": hide current window */
6408 eap->nextcmd = check_nextcmd(eap->arg);
6409#ifdef FEAT_WINDOWS
6410 if (!eap->skip)
6411 {
6412# ifdef FEAT_GUI
6413 need_mouse_correct = TRUE;
6414# endif
6415 win_close(curwin, FALSE); /* don't free buffer */
6416 }
6417#endif
6418 }
6419}
6420
6421/*
6422 * ":stop" and ":suspend": Suspend Vim.
6423 */
6424 static void
6425ex_stop(eap)
6426 exarg_T *eap;
6427{
6428 /*
6429 * Disallow suspending for "rvim".
6430 */
6431 if (!check_restricted()
6432#ifdef WIN3264
6433 /*
6434 * Check if external commands are allowed now.
6435 */
6436 && can_end_termcap_mode(TRUE)
6437#endif
6438 )
6439 {
6440 if (!eap->forceit)
6441 autowrite_all();
6442 windgoto((int)Rows - 1, 0);
6443 out_char('\n');
6444 out_flush();
6445 stoptermcap();
6446 out_flush(); /* needed for SUN to restore xterm buffer */
6447#ifdef FEAT_TITLE
6448 mch_restore_title(3); /* restore window titles */
6449#endif
6450 ui_suspend(); /* call machine specific function */
6451#ifdef FEAT_TITLE
6452 maketitle();
6453 resettitle(); /* force updating the title */
6454#endif
6455 starttermcap();
6456 scroll_start(); /* scroll screen before redrawing */
6457 redraw_later_clear();
6458 shell_resized(); /* may have resized window */
6459 }
6460}
6461
6462/*
6463 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6464 */
6465 static void
6466ex_exit(eap)
6467 exarg_T *eap;
6468{
6469#ifdef FEAT_CMDWIN
6470 if (cmdwin_type != 0)
6471 {
6472 cmdwin_result = Ctrl_C;
6473 return;
6474 }
6475#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006476 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006477 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006478 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006479 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006480 return;
6481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
6483 /*
6484 * if more files or windows we won't exit
6485 */
6486 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6487 exiting = TRUE;
6488 if ( ((eap->cmdidx == CMD_wq
6489 || curbufIsChanged())
6490 && do_write(eap) == FAIL)
6491 || check_more(TRUE, eap->forceit) == FAIL
6492 || (only_one_window() && check_changed_any(eap->forceit)))
6493 {
6494 not_exiting();
6495 }
6496 else
6497 {
6498#ifdef FEAT_WINDOWS
6499 if (only_one_window()) /* quit last window, exit Vim */
6500#endif
6501 getout(0);
6502#ifdef FEAT_WINDOWS
6503# ifdef FEAT_GUI
6504 need_mouse_correct = TRUE;
6505# endif
6506 /* quit current window, may free buffer */
6507 win_close(curwin, !P_HID(curwin->w_buffer));
6508#endif
6509 }
6510}
6511
6512/*
6513 * ":print", ":list", ":number".
6514 */
6515 static void
6516ex_print(eap)
6517 exarg_T *eap;
6518{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006519 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6520 EMSG(_(e_emptybuf));
6521 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006523 for ( ;!got_int; ui_breakcheck())
6524 {
6525 print_line(eap->line1,
6526 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6527 || (eap->flags & EXFLAG_NR)),
6528 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6529 if (++eap->line1 > eap->line2)
6530 break;
6531 out_flush(); /* show one line at a time */
6532 }
6533 setpcmark();
6534 /* put cursor at last line */
6535 curwin->w_cursor.lnum = eap->line2;
6536 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 }
6538
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540}
6541
6542#ifdef FEAT_BYTEOFF
6543 static void
6544ex_goto(eap)
6545 exarg_T *eap;
6546{
6547 goto_byte(eap->line2);
6548}
6549#endif
6550
6551/*
6552 * ":shell".
6553 */
6554/*ARGSUSED*/
6555 static void
6556ex_shell(eap)
6557 exarg_T *eap;
6558{
6559 do_shell(NULL, 0);
6560}
6561
6562#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6563 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006564 || defined(FEAT_GUI_MSWIN) \
6565 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 || defined(PROTO)
6567
6568/*
6569 * Handle a file drop. The code is here because a drop is *nearly* like an
6570 * :args command, but not quite (we have a list of exact filenames, so we
6571 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6572 * code is very similar to :args and hence needs access to a lot of the static
6573 * functions in this file.
6574 *
6575 * The list should be allocated using alloc(), as should each item in the
6576 * list. This function takes over responsibility for freeing the list.
6577 *
6578 * XXX The list is made into the arggument list. This is freed using
6579 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6580 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6581 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6582 * file functionality is (currently) not in EMX this is not presently a
6583 * problem.
6584 */
6585 void
6586handle_drop(filec, filev, split)
6587 int filec; /* the number of files dropped */
6588 char_u **filev; /* the list of files dropped */
6589 int split; /* force splitting the window */
6590{
6591 exarg_T ea;
6592 int save_msg_scroll = msg_scroll;
6593
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006594 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006595 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
6598 /* Check whether the current buffer is changed. If so, we will need
6599 * to split the current window or data could be lost.
6600 * We don't need to check if the 'hidden' option is set, as in this
6601 * case the buffer won't be lost.
6602 */
6603 if (!P_HID(curbuf) && !split)
6604 {
6605 ++emsg_off;
6606 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6607 --emsg_off;
6608 }
6609 if (split)
6610 {
6611# ifdef FEAT_WINDOWS
6612 if (win_split(0, 0) == FAIL)
6613 return;
6614# ifdef FEAT_SCROLLBIND
6615 curwin->w_p_scb = FALSE;
6616# endif
6617
6618 /* When splitting the window, create a new alist. Otherwise the
6619 * existing one is overwritten. */
6620 alist_unlink(curwin->w_alist);
6621 alist_new();
6622# else
6623 return; /* can't split, always fail */
6624# endif
6625 }
6626
6627 /*
6628 * Set up the new argument list.
6629 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006630 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631
6632 /*
6633 * Move to the first file.
6634 */
6635 /* Fake up a minimal "next" command for do_argfile() */
6636 vim_memset(&ea, 0, sizeof(ea));
6637 ea.cmd = (char_u *)"next";
6638 do_argfile(&ea, 0);
6639
6640 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6641 * mode that is not needed here. */
6642 need_start_insertmode = FALSE;
6643
6644 /* Restore msg_scroll, otherwise a following command may cause scrolling
6645 * unexpectedly. The screen will be redrawn by the caller, thus
6646 * msg_scroll being set by displaying a message is irrelevant. */
6647 msg_scroll = save_msg_scroll;
6648}
6649#endif
6650
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651/*
6652 * Clear an argument list: free all file names and reset it to zero entries.
6653 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006654 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655alist_clear(al)
6656 alist_T *al;
6657{
6658 while (--al->al_ga.ga_len >= 0)
6659 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6660 ga_clear(&al->al_ga);
6661}
6662
6663/*
6664 * Init an argument list.
6665 */
6666 void
6667alist_init(al)
6668 alist_T *al;
6669{
6670 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6671}
6672
6673#if defined(FEAT_WINDOWS) || defined(PROTO)
6674
6675/*
6676 * Remove a reference from an argument list.
6677 * Ignored when the argument list is the global one.
6678 * If the argument list is no longer used by any window, free it.
6679 */
6680 void
6681alist_unlink(al)
6682 alist_T *al;
6683{
6684 if (al != &global_alist && --al->al_refcount <= 0)
6685 {
6686 alist_clear(al);
6687 vim_free(al);
6688 }
6689}
6690
6691# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6692/*
6693 * Create a new argument list and use it for the current window.
6694 */
6695 void
6696alist_new()
6697{
6698 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6699 if (curwin->w_alist == NULL)
6700 {
6701 curwin->w_alist = &global_alist;
6702 ++global_alist.al_refcount;
6703 }
6704 else
6705 {
6706 curwin->w_alist->al_refcount = 1;
6707 alist_init(curwin->w_alist);
6708 }
6709}
6710# endif
6711#endif
6712
6713#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6714/*
6715 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006716 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6717 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 */
6719 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006720alist_expand(fnum_list, fnum_len)
6721 int *fnum_list;
6722 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
6724 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006725 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 char_u **new_arg_files;
6727 int new_arg_file_count;
6728 char_u *save_p_su = p_su;
6729 int i;
6730
6731 /* Don't use 'suffixes' here. This should work like the shell did the
6732 * expansion. Also, the vimrc file isn't read yet, thus the user
6733 * can't set the options. */
6734 p_su = empty_option;
6735 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6736 if (old_arg_files != NULL)
6737 {
6738 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006739 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6740 old_arg_count = GARGCOUNT;
6741 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 &new_arg_file_count, &new_arg_files,
6743 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6744 && new_arg_file_count > 0)
6745 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006746 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6747 TRUE, fnum_list, fnum_len);
6748 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 }
6751 p_su = save_p_su;
6752}
6753#endif
6754
6755/*
6756 * Set the argument list for the current window.
6757 * Takes over the allocated files[] and the allocated fnames in it.
6758 */
6759 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006760alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 alist_T *al;
6762 int count;
6763 char_u **files;
6764 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006765 int *fnum_list;
6766 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
6768 int i;
6769
6770 alist_clear(al);
6771 if (ga_grow(&al->al_ga, count) == OK)
6772 {
6773 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006774 {
6775 if (got_int)
6776 {
6777 /* When adding many buffers this can take a long time. Allow
6778 * interrupting here. */
6779 while (i < count)
6780 vim_free(files[i++]);
6781 break;
6782 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006783
6784 /* May set buffer name of a buffer previously used for the
6785 * argument list, so that it's re-used by alist_add. */
6786 if (fnum_list != NULL && i < fnum_len)
6787 buf_set_name(fnum_list[i], files[i]);
6788
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006790 ui_breakcheck();
6791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 vim_free(files);
6793 }
6794 else
6795 FreeWild(count, files);
6796#ifdef FEAT_WINDOWS
6797 if (al == &global_alist)
6798#endif
6799 arg_had_last = FALSE;
6800}
6801
6802/*
6803 * Add file "fname" to argument list "al".
6804 * "fname" must have been allocated and "al" must have been checked for room.
6805 */
6806 void
6807alist_add(al, fname, set_fnum)
6808 alist_T *al;
6809 char_u *fname;
6810 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6811{
6812 if (fname == NULL) /* don't add NULL file names */
6813 return;
6814#ifdef BACKSLASH_IN_FILENAME
6815 slash_adjust(fname);
6816#endif
6817 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6818 if (set_fnum > 0)
6819 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6820 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6821 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822}
6823
6824#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6825/*
6826 * Adjust slashes in file names. Called after 'shellslash' was set.
6827 */
6828 void
6829alist_slash_adjust()
6830{
6831 int i;
6832# ifdef FEAT_WINDOWS
6833 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006834 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835# endif
6836
6837 for (i = 0; i < GARGCOUNT; ++i)
6838 if (GARGLIST[i].ae_fname != NULL)
6839 slash_adjust(GARGLIST[i].ae_fname);
6840# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006841 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 if (wp->w_alist != &global_alist)
6843 for (i = 0; i < WARGCOUNT(wp); ++i)
6844 if (WARGLIST(wp)[i].ae_fname != NULL)
6845 slash_adjust(WARGLIST(wp)[i].ae_fname);
6846# endif
6847}
6848#endif
6849
6850/*
6851 * ":preserve".
6852 */
6853/*ARGSUSED*/
6854 static void
6855ex_preserve(eap)
6856 exarg_T *eap;
6857{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006858 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 ml_preserve(curbuf, TRUE);
6860}
6861
6862/*
6863 * ":recover".
6864 */
6865 static void
6866ex_recover(eap)
6867 exarg_T *eap;
6868{
6869 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6870 recoverymode = TRUE;
6871 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6872 && (*eap->arg == NUL
6873 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6874 ml_recover();
6875 recoverymode = FALSE;
6876}
6877
6878/*
6879 * Command modifier used in a wrong way.
6880 */
6881 static void
6882ex_wrongmodifier(eap)
6883 exarg_T *eap;
6884{
6885 eap->errmsg = e_invcmd;
6886}
6887
6888#ifdef FEAT_WINDOWS
6889/*
6890 * :sview [+command] file split window with new file, read-only
6891 * :split [[+command] file] split window with current or new file
6892 * :vsplit [[+command] file] split window vertically with current or new file
6893 * :new [[+command] file] split window with no or new file
6894 * :vnew [[+command] file] split vertically window with no or new file
6895 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006896 *
6897 * :tabedit open new Tab page with empty window
6898 * :tabedit [+command] file open new Tab page and edit "file"
6899 * :tabnew [[+command] file] just like :tabedit
6900 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 */
6902 void
6903ex_splitview(eap)
6904 exarg_T *eap;
6905{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006906 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006907# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006909# endif
6910# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006914# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6916 {
6917 ex_ni(eap);
6918 return;
6919 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006922# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006926# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 /* A ":split" in the quickfix window works like ":new". Don't want two
6928 * quickfix windows. */
6929 if (bt_quickfix(curbuf))
6930 {
6931 if (eap->cmdidx == CMD_split)
6932 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006933# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 if (eap->cmdidx == CMD_vsplit)
6935 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006940# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006941 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {
6943 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6944 FNAME_MESS, TRUE, curbuf->b_ffname);
6945 if (fname == NULL)
6946 goto theend;
6947 eap->arg = fname;
6948 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006949# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006953# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006955# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 && eap->cmdidx != CMD_new)
6959 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006960 if (
6961# ifdef FEAT_GUI
6962 !gui.in_use &&
6963# endif
6964 au_has_group((char_u *)"FileExplorer"))
6965 {
6966 /* No browsing supported but we do have the file explorer:
6967 * Edit the directory. */
6968 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6969 eap->arg = (char_u *)".";
6970 }
6971 else
6972 {
6973 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006975 if (fname == NULL)
6976 goto theend;
6977 eap->arg = fname;
6978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006983 /*
6984 * Either open new tab page or split the window.
6985 */
6986 if (eap->cmdidx == CMD_tabedit
6987 || eap->cmdidx == CMD_tabfind
6988 || eap->cmdidx == CMD_tabnew)
6989 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006990 if (win_new_tabpage(cmdmod.tab) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006991 {
6992 do_exedit(eap, NULL);
6993
6994 /* set the alternate buffer for the window we came from */
6995 if (curwin != old_curwin
6996 && win_valid(old_curwin)
6997 && old_curwin->w_buffer != curbuf
6998 && !cmdmod.keepalt)
6999 old_curwin->w_alt_fnum = curbuf->b_fnum;
7000 }
7001 }
7002 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7004 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007005# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 /* Reset 'scrollbind' when editing another file, but keep it when
7007 * doing ":split" without arguments. */
7008 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007009# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 )
7013 curwin->w_p_scb = FALSE;
7014 else
7015 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 do_exedit(eap, old_curwin);
7018 }
7019
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007020# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007024# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025theend:
7026 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007029
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007030#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007031/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007032 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007033 */
7034 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007035tabpage_new()
7036{
7037 exarg_T ea;
7038
7039 vim_memset(&ea, 0, sizeof(ea));
7040 ea.cmdidx = CMD_tabnew;
7041 ea.cmd = (char_u *)"tabn";
7042 ea.arg = (char_u *)"";
7043 ex_splitview(&ea);
7044}
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007045#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007046
7047/*
7048 * :tabnext command
7049 */
7050 static void
7051ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007052 exarg_T *eap;
7053{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007054 switch (eap->cmdidx)
7055 {
7056 case CMD_tabfirst:
7057 case CMD_tabrewind:
7058 goto_tabpage(1);
7059 break;
7060 case CMD_tablast:
7061 goto_tabpage(9999);
7062 break;
7063 case CMD_tabprevious:
7064 case CMD_tabNext:
7065 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7066 break;
7067 default: /* CMD_tabnext */
7068 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7069 break;
7070 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007071}
7072
7073/*
7074 * :tabmove command
7075 */
7076 static void
7077ex_tabmove(eap)
7078 exarg_T *eap;
7079{
7080 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007081}
7082
7083/*
7084 * :tabs command: List tabs and their contents.
7085 */
7086/*ARGSUSED*/
7087 static void
7088ex_tabs(eap)
7089 exarg_T *eap;
7090{
7091 tabpage_T *tp;
7092 win_T *wp;
7093 int tabcount = 1;
7094
7095 msg_start();
7096 msg_scroll = TRUE;
7097 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7098 {
7099 msg_putchar('\n');
7100 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7101 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7102 out_flush(); /* output one line at a time */
7103 ui_breakcheck();
7104
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007105 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007106 wp = firstwin;
7107 else
7108 wp = tp->tp_firstwin;
7109 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7110 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007111 msg_putchar('\n');
7112 msg_putchar(wp == curwin ? '>' : ' ');
7113 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007114 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7115 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007116 if (buf_spname(wp->w_buffer) != NULL)
7117 STRCPY(IObuff, buf_spname(wp->w_buffer));
7118 else
7119 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7120 IObuff, IOSIZE, TRUE);
7121 msg_outtrans(IObuff);
7122 out_flush(); /* output one line at a time */
7123 ui_breakcheck();
7124 }
7125 }
7126}
7127
7128#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129
7130/*
7131 * ":mode": Set screen mode.
7132 * If no argument given, just get the screen size and redraw.
7133 */
7134 static void
7135ex_mode(eap)
7136 exarg_T *eap;
7137{
7138 if (*eap->arg == NUL)
7139 shell_resized();
7140 else
7141 mch_screenmode(eap->arg);
7142}
7143
7144#ifdef FEAT_WINDOWS
7145/*
7146 * ":resize".
7147 * set, increment or decrement current window height
7148 */
7149 static void
7150ex_resize(eap)
7151 exarg_T *eap;
7152{
7153 int n;
7154 win_T *wp = curwin;
7155
7156 if (eap->addr_count > 0)
7157 {
7158 n = eap->line2;
7159 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7160 ;
7161 }
7162
7163#ifdef FEAT_GUI
7164 need_mouse_correct = TRUE;
7165#endif
7166 n = atol((char *)eap->arg);
7167#ifdef FEAT_VERTSPLIT
7168 if (cmdmod.split & WSP_VERT)
7169 {
7170 if (*eap->arg == '-' || *eap->arg == '+')
7171 n += W_WIDTH(curwin);
7172 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7173 n = 9999;
7174 win_setwidth_win((int)n, wp);
7175 }
7176 else
7177#endif
7178 {
7179 if (*eap->arg == '-' || *eap->arg == '+')
7180 n += curwin->w_height;
7181 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7182 n = 9999;
7183 win_setheight_win((int)n, wp);
7184 }
7185}
7186#endif
7187
7188/*
7189 * ":find [+command] <file>" command.
7190 */
7191 static void
7192ex_find(eap)
7193 exarg_T *eap;
7194{
7195#ifdef FEAT_SEARCHPATH
7196 char_u *fname;
7197 int count;
7198
7199 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7200 TRUE, curbuf->b_ffname);
7201 if (eap->addr_count > 0)
7202 {
7203 /* Repeat finding the file "count" times. This matters when it
7204 * appears several times in the path. */
7205 count = eap->line2;
7206 while (fname != NULL && --count > 0)
7207 {
7208 vim_free(fname);
7209 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7210 FALSE, curbuf->b_ffname);
7211 }
7212 }
7213
7214 if (fname != NULL)
7215 {
7216 eap->arg = fname;
7217#endif
7218 do_exedit(eap, NULL);
7219#ifdef FEAT_SEARCHPATH
7220 vim_free(fname);
7221 }
7222#endif
7223}
7224
7225/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007226 * ":open" simulation: for now just work like ":visual".
7227 */
7228 static void
7229ex_open(eap)
7230 exarg_T *eap;
7231{
7232 regmatch_T regmatch;
7233 char_u *p;
7234
7235 curwin->w_cursor.lnum = eap->line2;
7236 beginline(BL_SOL | BL_FIX);
7237 if (*eap->arg == '/')
7238 {
7239 /* ":open /pattern/": put cursor in column found with pattern */
7240 ++eap->arg;
7241 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7242 *p = NUL;
7243 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7244 if (regmatch.regprog != NULL)
7245 {
7246 regmatch.rm_ic = p_ic;
7247 p = ml_get_curline();
7248 if (vim_regexec(&regmatch, p, (colnr_T)0))
7249 curwin->w_cursor.col = regmatch.startp[0] - p;
7250 else
7251 EMSG(_(e_nomatch));
7252 vim_free(regmatch.regprog);
7253 }
7254 /* Move to the NUL, ignore any other arguments. */
7255 eap->arg += STRLEN(eap->arg);
7256 }
7257 check_cursor();
7258
7259 eap->cmdidx = CMD_visual;
7260 do_exedit(eap, NULL);
7261}
7262
7263/*
7264 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 */
7266 static void
7267ex_edit(eap)
7268 exarg_T *eap;
7269{
7270 do_exedit(eap, NULL);
7271}
7272
7273/*
7274 * ":edit <file>" command and alikes.
7275 */
7276/*ARGSUSED*/
7277 void
7278do_exedit(eap, old_curwin)
7279 exarg_T *eap;
7280 win_T *old_curwin; /* curwin before doing a split or NULL */
7281{
7282 int n;
7283#ifdef FEAT_WINDOWS
7284 int need_hide;
7285#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007286 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287
7288 /*
7289 * ":vi" command ends Ex mode.
7290 */
7291 if (exmode_active && (eap->cmdidx == CMD_visual
7292 || eap->cmdidx == CMD_view))
7293 {
7294 exmode_active = FALSE;
7295 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007296 {
7297 /* Special case: ":global/pat/visual\NLvi-commands" */
7298 if (global_busy)
7299 {
7300 int rd = RedrawingDisabled;
7301 int nwr = no_wait_return;
7302 int ms = msg_scroll;
7303#ifdef FEAT_GUI
7304 int he = hold_gui_events;
7305#endif
7306
7307 if (eap->nextcmd != NULL)
7308 {
7309 stuffReadbuff(eap->nextcmd);
7310 eap->nextcmd = NULL;
7311 }
7312
7313 if (exmode_was != EXMODE_VIM)
7314 settmode(TMODE_RAW);
7315 RedrawingDisabled = 0;
7316 no_wait_return = 0;
7317 need_wait_return = FALSE;
7318 msg_scroll = 0;
7319#ifdef FEAT_GUI
7320 hold_gui_events = 0;
7321#endif
7322 must_redraw = CLEAR;
7323
7324 main_loop(FALSE, TRUE);
7325
7326 RedrawingDisabled = rd;
7327 no_wait_return = nwr;
7328 msg_scroll = ms;
7329#ifdef FEAT_GUI
7330 hold_gui_events = he;
7331#endif
7332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 }
7336
7337 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007338 || eap->cmdidx == CMD_tabnew
7339 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340#ifdef FEAT_VERTSPLIT
7341 || eap->cmdidx == CMD_vnew
7342#endif
7343 ) && *eap->arg == NUL)
7344 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007345 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 setpcmark();
7347 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7348 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7349 }
7350 else if ((eap->cmdidx != CMD_split
7351#ifdef FEAT_VERTSPLIT
7352 && eap->cmdidx != CMD_vsplit
7353#endif
7354 )
7355 || *eap->arg != NUL
7356#ifdef FEAT_BROWSE
7357 || cmdmod.browse
7358#endif
7359 )
7360 {
7361 n = readonlymode;
7362 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7363 readonlymode = TRUE;
7364 else if (eap->cmdidx == CMD_enew)
7365 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7366 empty buffer */
7367 setpcmark();
7368 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7369 NULL, eap,
7370 /* ":edit" goes to first line if Vi compatible */
7371 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7372 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7373 ? ECMD_ONE : eap->do_ecmd_lnum,
7374 (P_HID(curbuf) ? ECMD_HIDE : 0)
7375 + (eap->forceit ? ECMD_FORCEIT : 0)
7376#ifdef FEAT_LISTCMDS
7377 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7378#endif
7379 ) == FAIL)
7380 {
7381 /* Editing the file failed. If the window was split, close it. */
7382#ifdef FEAT_WINDOWS
7383 if (old_curwin != NULL)
7384 {
7385 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7386 if (!need_hide || P_HID(curbuf))
7387 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007388# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7389 cleanup_T cs;
7390
7391 /* Reset the error/interrupt/exception state here so that
7392 * aborting() returns FALSE when closing a window. */
7393 enter_cleanup(&cs);
7394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395# ifdef FEAT_GUI
7396 need_mouse_correct = TRUE;
7397# endif
7398 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007399
7400# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7401 /* Restore the error/interrupt/exception state if not
7402 * discarded by a new aborting error, interrupt, or
7403 * uncaught exception. */
7404 leave_cleanup(&cs);
7405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 }
7407 }
7408#endif
7409 }
7410 else if (readonlymode && curbuf->b_nwindows == 1)
7411 {
7412 /* When editing an already visited buffer, 'readonly' won't be set
7413 * but the previous value is kept. With ":view" and ":sview" we
7414 * want the file to be readonly, except when another window is
7415 * editing the same buffer. */
7416 curbuf->b_p_ro = TRUE;
7417 }
7418 readonlymode = n;
7419 }
7420 else
7421 {
7422 if (eap->do_ecmd_cmd != NULL)
7423 do_cmdline_cmd(eap->do_ecmd_cmd);
7424#ifdef FEAT_TITLE
7425 n = curwin->w_arg_idx_invalid;
7426#endif
7427 check_arg_idx(curwin);
7428#ifdef FEAT_TITLE
7429 if (n != curwin->w_arg_idx_invalid)
7430 maketitle();
7431#endif
7432 }
7433
7434#ifdef FEAT_WINDOWS
7435 /*
7436 * if ":split file" worked, set alternate file name in old window to new
7437 * file
7438 */
7439 if (old_curwin != NULL
7440 && *eap->arg != NUL
7441 && curwin != old_curwin
7442 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007443 && old_curwin->w_buffer != curbuf
7444 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 old_curwin->w_alt_fnum = curbuf->b_fnum;
7446#endif
7447
7448 ex_no_reprint = TRUE;
7449}
7450
7451#ifndef FEAT_GUI
7452/*
7453 * ":gui" and ":gvim" when there is no GUI.
7454 */
7455 static void
7456ex_nogui(eap)
7457 exarg_T *eap;
7458{
7459 eap->errmsg = e_nogvim;
7460}
7461#endif
7462
7463#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7464 static void
7465ex_tearoff(eap)
7466 exarg_T *eap;
7467{
7468 gui_make_tearoff(eap->arg);
7469}
7470#endif
7471
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007472#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 static void
7474ex_popup(eap)
7475 exarg_T *eap;
7476{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007477 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478}
7479#endif
7480
7481/*ARGSUSED*/
7482 static void
7483ex_swapname(eap)
7484 exarg_T *eap;
7485{
7486 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7487 MSG(_("No swap file"));
7488 else
7489 msg(curbuf->b_ml.ml_mfp->mf_fname);
7490}
7491
7492/*
7493 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7494 * offset.
7495 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7496 */
7497/*ARGSUSED*/
7498 static void
7499ex_syncbind(eap)
7500 exarg_T *eap;
7501{
7502#ifdef FEAT_SCROLLBIND
7503 win_T *wp;
7504 long topline;
7505 long y;
7506 linenr_T old_linenr = curwin->w_cursor.lnum;
7507
7508 setpcmark();
7509
7510 /*
7511 * determine max topline
7512 */
7513 if (curwin->w_p_scb)
7514 {
7515 topline = curwin->w_topline;
7516 for (wp = firstwin; wp; wp = wp->w_next)
7517 {
7518 if (wp->w_p_scb && wp->w_buffer)
7519 {
7520 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7521 if (topline > y)
7522 topline = y;
7523 }
7524 }
7525 if (topline < 1)
7526 topline = 1;
7527 }
7528 else
7529 {
7530 topline = 1;
7531 }
7532
7533
7534 /*
7535 * set all scrollbind windows to the same topline
7536 */
7537 wp = curwin;
7538 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7539 {
7540 if (curwin->w_p_scb)
7541 {
7542 y = topline - curwin->w_topline;
7543 if (y > 0)
7544 scrollup(y, TRUE);
7545 else
7546 scrolldown(-y, TRUE);
7547 curwin->w_scbind_pos = topline;
7548 redraw_later(VALID);
7549 cursor_correct();
7550#ifdef FEAT_WINDOWS
7551 curwin->w_redr_status = TRUE;
7552#endif
7553 }
7554 }
7555 curwin = wp;
7556 if (curwin->w_p_scb)
7557 {
7558 did_syncbind = TRUE;
7559 checkpcmark();
7560 if (old_linenr != curwin->w_cursor.lnum)
7561 {
7562 char_u ctrl_o[2];
7563
7564 ctrl_o[0] = Ctrl_O;
7565 ctrl_o[1] = 0;
7566 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7567 }
7568 }
7569#endif
7570}
7571
7572
7573 static void
7574ex_read(eap)
7575 exarg_T *eap;
7576{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007577 int i;
7578 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7579 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580
7581 if (eap->usefilter) /* :r!cmd */
7582 do_bang(1, eap, FALSE, FALSE, TRUE);
7583 else
7584 {
7585 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7586 return;
7587
7588#ifdef FEAT_BROWSE
7589 if (cmdmod.browse)
7590 {
7591 char_u *browseFile;
7592
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007593 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 NULL, NULL, NULL, curbuf);
7595 if (browseFile != NULL)
7596 {
7597 i = readfile(browseFile, NULL,
7598 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7599 vim_free(browseFile);
7600 }
7601 else
7602 i = OK;
7603 }
7604 else
7605#endif
7606 if (*eap->arg == NUL)
7607 {
7608 if (check_fname() == FAIL) /* check for no file name */
7609 return;
7610 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7611 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7612 }
7613 else
7614 {
7615 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7616 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7617 i = readfile(eap->arg, NULL,
7618 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7619
7620 }
7621 if (i == FAIL)
7622 {
7623#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7624 if (!aborting())
7625#endif
7626 EMSG2(_(e_notopen), eap->arg);
7627 }
7628 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007629 {
7630 if (empty && exmode_active)
7631 {
7632 /* Delete the empty line that remains. Historically ex does
7633 * this but vi doesn't. */
7634 if (eap->line2 == 0)
7635 lnum = curbuf->b_ml.ml_line_count;
7636 else
7637 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007638 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007639 {
7640 ml_delete(lnum, FALSE);
7641 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007642 if (curwin->w_cursor.lnum > 1
7643 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644 --curwin->w_cursor.lnum;
7645 }
7646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650}
7651
Bram Moolenaarea408852005-06-25 22:49:46 +00007652static char_u *prev_dir = NULL;
7653
7654#if defined(EXITFREE) || defined(PROTO)
7655 void
7656free_cd_dir()
7657{
7658 vim_free(prev_dir);
7659}
7660#endif
7661
7662
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663/*
7664 * ":cd", ":lcd", ":chdir" and ":lchdir".
7665 */
7666 static void
7667ex_cd(eap)
7668 exarg_T *eap;
7669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 char_u *new_dir;
7671 char_u *tofree;
7672
7673 new_dir = eap->arg;
7674#if !defined(UNIX) && !defined(VMS)
7675 /* for non-UNIX ":cd" means: print current directory */
7676 if (*new_dir == NUL)
7677 ex_pwd(NULL);
7678 else
7679#endif
7680 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007681 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7682 && !eap->forceit)
7683 {
7684 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7685 return;
7686 }
7687
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 /* ":cd -": Change to previous directory */
7689 if (STRCMP(new_dir, "-") == 0)
7690 {
7691 if (prev_dir == NULL)
7692 {
7693 EMSG(_("E186: No previous directory"));
7694 return;
7695 }
7696 new_dir = prev_dir;
7697 }
7698
7699 /* Save current directory for next ":cd -" */
7700 tofree = prev_dir;
7701 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7702 prev_dir = vim_strsave(NameBuff);
7703 else
7704 prev_dir = NULL;
7705
7706#if defined(UNIX) || defined(VMS)
7707 /* for UNIX ":cd" means: go to home directory */
7708 if (*new_dir == NUL)
7709 {
7710 /* use NameBuff for home directory name */
7711# ifdef VMS
7712 char_u *p;
7713
7714 p = mch_getenv((char_u *)"SYS$LOGIN");
7715 if (p == NULL || *p == NUL) /* empty is the same as not set */
7716 NameBuff[0] = NUL;
7717 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007718 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719# else
7720 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7721# endif
7722 new_dir = NameBuff;
7723 }
7724#endif
7725 if (new_dir == NULL || vim_chdir(new_dir))
7726 EMSG(_(e_failed));
7727 else
7728 {
7729 vim_free(curwin->w_localdir);
7730 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7731 {
7732 /* If still in global directory, need to remember current
7733 * directory as global directory. */
7734 if (globaldir == NULL && prev_dir != NULL)
7735 globaldir = vim_strsave(prev_dir);
7736 /* Remember this local directory for the window. */
7737 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7738 curwin->w_localdir = vim_strsave(NameBuff);
7739 }
7740 else
7741 {
7742 /* We are now in the global directory, no need to remember its
7743 * name. */
7744 vim_free(globaldir);
7745 globaldir = NULL;
7746 curwin->w_localdir = NULL;
7747 }
7748
7749 shorten_fnames(TRUE);
7750
7751 /* Echo the new current directory if the command was typed. */
7752 if (KeyTyped)
7753 ex_pwd(eap);
7754 }
7755 vim_free(tofree);
7756 }
7757}
7758
7759/*
7760 * ":pwd".
7761 */
7762/*ARGSUSED*/
7763 static void
7764ex_pwd(eap)
7765 exarg_T *eap;
7766{
7767 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7768 {
7769#ifdef BACKSLASH_IN_FILENAME
7770 slash_adjust(NameBuff);
7771#endif
7772 msg(NameBuff);
7773 }
7774 else
7775 EMSG(_("E187: Unknown"));
7776}
7777
7778/*
7779 * ":=".
7780 */
7781 static void
7782ex_equal(eap)
7783 exarg_T *eap;
7784{
7785 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007786 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787}
7788
7789 static void
7790ex_sleep(eap)
7791 exarg_T *eap;
7792{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007793 int n;
7794 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795
7796 if (cursor_valid())
7797 {
7798 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7799 if (n >= 0)
7800 windgoto((int)n, curwin->w_wcol);
7801 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007802
7803 len = eap->line2;
7804 switch (*eap->arg)
7805 {
7806 case 'm': break;
7807 case NUL: len *= 1000L; break;
7808 default: EMSG2(_(e_invarg2), eap->arg); return;
7809 }
7810 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811}
7812
7813/*
7814 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7815 */
7816 void
7817do_sleep(msec)
7818 long msec;
7819{
7820 long done;
7821
7822 cursor_on();
7823 out_flush();
7824 for (done = 0; !got_int && done < msec; done += 1000L)
7825 {
7826 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7827 ui_breakcheck();
7828 }
7829}
7830
7831 static void
7832do_exmap(eap, isabbrev)
7833 exarg_T *eap;
7834 int isabbrev;
7835{
7836 int mode;
7837 char_u *cmdp;
7838
7839 cmdp = eap->cmd;
7840 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7841
7842 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7843 eap->arg, mode, isabbrev))
7844 {
7845 case 1: EMSG(_(e_invarg));
7846 break;
7847 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7848 break;
7849 }
7850}
7851
7852/*
7853 * ":winsize" command (obsolete).
7854 */
7855 static void
7856ex_winsize(eap)
7857 exarg_T *eap;
7858{
7859 int w, h;
7860 char_u *arg = eap->arg;
7861 char_u *p;
7862
7863 w = getdigits(&arg);
7864 arg = skipwhite(arg);
7865 p = arg;
7866 h = getdigits(&arg);
7867 if (*p != NUL && *arg == NUL)
7868 set_shellsize(w, h, TRUE);
7869 else
7870 EMSG(_("E465: :winsize requires two number arguments"));
7871}
7872
7873#ifdef FEAT_WINDOWS
7874 static void
7875ex_wincmd(eap)
7876 exarg_T *eap;
7877{
7878 int xchar = NUL;
7879 char_u *p;
7880
7881 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7882 {
7883 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7884 if (eap->arg[1] == NUL)
7885 {
7886 EMSG(_(e_invarg));
7887 return;
7888 }
7889 xchar = eap->arg[1];
7890 p = eap->arg + 2;
7891 }
7892 else
7893 p = eap->arg + 1;
7894
7895 eap->nextcmd = check_nextcmd(p);
7896 p = skipwhite(p);
7897 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7898 EMSG(_(e_invarg));
7899 else
7900 {
7901 /* Pass flags on for ":vertical wincmd ]". */
7902 postponed_split_flags = cmdmod.split;
7903 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7904 postponed_split_flags = 0;
7905 }
7906}
7907#endif
7908
Bram Moolenaar843ee412004-06-30 16:16:41 +00007909#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910/*
7911 * ":winpos".
7912 */
7913 static void
7914ex_winpos(eap)
7915 exarg_T *eap;
7916{
7917 int x, y;
7918 char_u *arg = eap->arg;
7919 char_u *p;
7920
7921 if (*arg == NUL)
7922 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007923# if defined(FEAT_GUI) || defined(MSWIN)
7924# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007926# else
7927 if (mch_get_winpos(&x, &y) != FAIL)
7928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {
7930 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7931 msg(IObuff);
7932 }
7933 else
7934# endif
7935 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7936 }
7937 else
7938 {
7939 x = getdigits(&arg);
7940 arg = skipwhite(arg);
7941 p = arg;
7942 y = getdigits(&arg);
7943 if (*p == NUL || *arg != NUL)
7944 {
7945 EMSG(_("E466: :winpos requires two number arguments"));
7946 return;
7947 }
7948# ifdef FEAT_GUI
7949 if (gui.in_use)
7950 gui_mch_set_winpos(x, y);
7951 else if (gui.starting)
7952 {
7953 /* Remember the coordinates for when the window is opened. */
7954 gui_win_x = x;
7955 gui_win_y = y;
7956 }
7957# ifdef HAVE_TGETENT
7958 else
7959# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007960# else
7961# ifdef MSWIN
7962 mch_set_winpos(x, y);
7963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964# endif
7965# ifdef HAVE_TGETENT
7966 if (*T_CWP)
7967 term_set_winpos(x, y);
7968# endif
7969 }
7970}
7971#endif
7972
7973/*
7974 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7975 */
7976 static void
7977ex_operators(eap)
7978 exarg_T *eap;
7979{
7980 oparg_T oa;
7981
7982 clear_oparg(&oa);
7983 oa.regname = eap->regname;
7984 oa.start.lnum = eap->line1;
7985 oa.end.lnum = eap->line2;
7986 oa.line_count = eap->line2 - eap->line1 + 1;
7987 oa.motion_type = MLINE;
7988#ifdef FEAT_VIRTUALEDIT
7989 virtual_op = FALSE;
7990#endif
7991 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7992 {
7993 setpcmark();
7994 curwin->w_cursor.lnum = eap->line1;
7995 beginline(BL_SOL | BL_FIX);
7996 }
7997
7998 switch (eap->cmdidx)
7999 {
8000 case CMD_delete:
8001 oa.op_type = OP_DELETE;
8002 op_delete(&oa);
8003 break;
8004
8005 case CMD_yank:
8006 oa.op_type = OP_YANK;
8007 (void)op_yank(&oa, FALSE, TRUE);
8008 break;
8009
8010 default: /* CMD_rshift or CMD_lshift */
8011 if ((eap->cmdidx == CMD_rshift)
8012#ifdef FEAT_RIGHTLEFT
8013 ^ curwin->w_p_rl
8014#endif
8015 )
8016 oa.op_type = OP_RSHIFT;
8017 else
8018 oa.op_type = OP_LSHIFT;
8019 op_shift(&oa, FALSE, eap->amount);
8020 break;
8021 }
8022#ifdef FEAT_VIRTUALEDIT
8023 virtual_op = MAYBE;
8024#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008025 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026}
8027
8028/*
8029 * ":put".
8030 */
8031 static void
8032ex_put(eap)
8033 exarg_T *eap;
8034{
8035 /* ":0put" works like ":1put!". */
8036 if (eap->line2 == 0)
8037 {
8038 eap->line2 = 1;
8039 eap->forceit = TRUE;
8040 }
8041 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008042 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8043 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044}
8045
8046/*
8047 * Handle ":copy" and ":move".
8048 */
8049 static void
8050ex_copymove(eap)
8051 exarg_T *eap;
8052{
8053 long n;
8054
8055 n = get_address(&eap->arg, FALSE, FALSE);
8056 if (eap->arg == NULL) /* error detected */
8057 {
8058 eap->nextcmd = NULL;
8059 return;
8060 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008061 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
8063 /*
8064 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8065 */
8066 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8067 {
8068 EMSG(_(e_invaddr));
8069 return;
8070 }
8071
8072 if (eap->cmdidx == CMD_move)
8073 {
8074 if (do_move(eap->line1, eap->line2, n) == FAIL)
8075 return;
8076 }
8077 else
8078 ex_copy(eap->line1, eap->line2, n);
8079 u_clearline();
8080 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008081 ex_may_print(eap);
8082}
8083
8084/*
8085 * Print the current line if flags were given to the Ex command.
8086 */
8087 static void
8088ex_may_print(eap)
8089 exarg_T *eap;
8090{
8091 if (eap->flags != 0)
8092 {
8093 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8094 (eap->flags & EXFLAG_LIST));
8095 ex_no_reprint = TRUE;
8096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097}
8098
8099/*
8100 * ":smagic" and ":snomagic".
8101 */
8102 static void
8103ex_submagic(eap)
8104 exarg_T *eap;
8105{
8106 int magic_save = p_magic;
8107
8108 p_magic = (eap->cmdidx == CMD_smagic);
8109 do_sub(eap);
8110 p_magic = magic_save;
8111}
8112
8113/*
8114 * ":join".
8115 */
8116 static void
8117ex_join(eap)
8118 exarg_T *eap;
8119{
8120 curwin->w_cursor.lnum = eap->line1;
8121 if (eap->line1 == eap->line2)
8122 {
8123 if (eap->addr_count >= 2) /* :2,2join does nothing */
8124 return;
8125 if (eap->line2 == curbuf->b_ml.ml_line_count)
8126 {
8127 beep_flush();
8128 return;
8129 }
8130 ++eap->line2;
8131 }
8132 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8133 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008134 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135}
8136
8137/*
8138 * ":[addr]@r" or ":[addr]*r": execute register
8139 */
8140 static void
8141ex_at(eap)
8142 exarg_T *eap;
8143{
8144 int c;
8145
8146 curwin->w_cursor.lnum = eap->line2;
8147
8148#ifdef USE_ON_FLY_SCROLL
8149 dont_scroll = TRUE; /* disallow scrolling here */
8150#endif
8151
8152 /* get the register name. No name means to use the previous one */
8153 c = *eap->arg;
8154 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8155 c = '@';
8156 /* put the register in mapbuf */
8157 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 else
8162 {
8163 int save_efr = exec_from_reg;
8164
8165 exec_from_reg = TRUE;
8166
8167 /*
8168 * Execute from the typeahead buffer.
8169 * Originally this didn't check for the typeahead buffer to be empty,
8170 * thus could read more Ex commands from stdin. It's not clear why,
8171 * it is certainly unexpected.
8172 */
8173 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8174 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8175
8176 exec_from_reg = save_efr;
8177 }
8178}
8179
8180/*
8181 * ":!".
8182 */
8183 static void
8184ex_bang(eap)
8185 exarg_T *eap;
8186{
8187 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8188}
8189
8190/*
8191 * ":undo".
8192 */
8193/*ARGSUSED*/
8194 static void
8195ex_undo(eap)
8196 exarg_T *eap;
8197{
8198 u_undo(1);
8199}
8200
8201/*
8202 * ":redo".
8203 */
8204/*ARGSUSED*/
8205 static void
8206ex_redo(eap)
8207 exarg_T *eap;
8208{
8209 u_redo(1);
8210}
8211
8212/*
8213 * ":redir": start/stop redirection.
8214 */
8215 static void
8216ex_redir(eap)
8217 exarg_T *eap;
8218{
8219 char *mode;
8220 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008221 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
8223 if (STRICMP(eap->arg, "END") == 0)
8224 close_redir();
8225 else
8226 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008227 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008229 ++arg;
8230 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008232 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 mode = "a";
8234 }
8235 else
8236 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008237 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
8239 close_redir();
8240
8241 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008242 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (fname == NULL)
8244 return;
8245#ifdef FEAT_BROWSE
8246 if (cmdmod.browse)
8247 {
8248 char_u *browseFile;
8249
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008250 browseFile = do_browse(BROWSE_SAVE,
8251 (char_u *)_("Save Redirection"),
8252 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 if (browseFile == NULL)
8254 return; /* operation cancelled */
8255 vim_free(fname);
8256 fname = browseFile;
8257 eap->forceit = TRUE; /* since dialog already asked */
8258 }
8259#endif
8260
8261 redir_fd = open_exfile(fname, eap->forceit, mode);
8262 vim_free(fname);
8263 }
8264#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008265 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {
8267 /* redirect to a register a-z (resp. A-Z for appending) */
8268 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008269 ++arg;
8270 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008272 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008274 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008276 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008277 if (*arg == '>' && arg[1] == '>')
8278 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008279 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8280 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008282 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008284 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008286 if (*arg == '>')
8287 arg++;
8288
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 /* make register empty */
8290 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8291 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008292 }
8293 if (*arg != NUL)
8294 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008295 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008296 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008297 }
8298 }
8299 else if (*arg == '=' && arg[1] == '>')
8300 {
8301 int append;
8302
8303 /* redirect to a variable */
8304 close_redir();
8305 arg += 2;
8306
8307 if (*arg == '>')
8308 {
8309 ++arg;
8310 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 }
8312 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008313 append = FALSE;
8314
8315 if (var_redir_start(skipwhite(arg), append) == OK)
8316 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 }
8318#endif
8319
8320 /* TODO: redirect to a buffer */
8321
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008323 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325}
8326
8327/*
8328 * ":redraw": force redraw
8329 */
8330 static void
8331ex_redraw(eap)
8332 exarg_T *eap;
8333{
8334 int r = RedrawingDisabled;
8335 int p = p_lz;
8336
8337 RedrawingDisabled = 0;
8338 p_lz = FALSE;
8339 update_topline();
8340 update_screen(eap->forceit ? CLEAR :
8341#ifdef FEAT_VISUAL
8342 VIsual_active ? INVERTED :
8343#endif
8344 0);
8345#ifdef FEAT_TITLE
8346 if (need_maketitle)
8347 maketitle();
8348#endif
8349 RedrawingDisabled = r;
8350 p_lz = p;
8351
8352 /* Reset msg_didout, so that a message that's there is overwritten. */
8353 msg_didout = FALSE;
8354 msg_col = 0;
8355
8356 out_flush();
8357}
8358
8359/*
8360 * ":redrawstatus": force redraw of status line(s)
8361 */
8362/*ARGSUSED*/
8363 static void
8364ex_redrawstatus(eap)
8365 exarg_T *eap;
8366{
8367#if defined(FEAT_WINDOWS)
8368 int r = RedrawingDisabled;
8369 int p = p_lz;
8370
8371 RedrawingDisabled = 0;
8372 p_lz = FALSE;
8373 if (eap->forceit)
8374 status_redraw_all();
8375 else
8376 status_redraw_curbuf();
8377 update_screen(
8378# ifdef FEAT_VISUAL
8379 VIsual_active ? INVERTED :
8380# endif
8381 0);
8382 RedrawingDisabled = r;
8383 p_lz = p;
8384 out_flush();
8385#endif
8386}
8387
8388 static void
8389close_redir()
8390{
8391 if (redir_fd != NULL)
8392 {
8393 fclose(redir_fd);
8394 redir_fd = NULL;
8395 }
8396#ifdef FEAT_EVAL
8397 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008398 if (redir_vname)
8399 {
8400 var_redir_stop();
8401 redir_vname = 0;
8402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403#endif
8404}
8405
8406#if defined(FEAT_SESSION) && defined(USE_CRNL)
8407# define MKSESSION_NL
8408static int mksession_nl = FALSE; /* use NL only in put_eol() */
8409#endif
8410
8411/*
8412 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8413 */
8414 static void
8415ex_mkrc(eap)
8416 exarg_T *eap;
8417{
8418 FILE *fd;
8419 int failed = FALSE;
8420 char_u *fname;
8421#ifdef FEAT_BROWSE
8422 char_u *browseFile = NULL;
8423#endif
8424#ifdef FEAT_SESSION
8425 int view_session = FALSE;
8426 int using_vdir = FALSE; /* using 'viewdir'? */
8427 char_u *viewFile = NULL;
8428 unsigned *flagp;
8429#endif
8430
8431 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8432 {
8433#ifdef FEAT_SESSION
8434 view_session = TRUE;
8435#else
8436 ex_ni(eap);
8437 return;
8438#endif
8439 }
8440
8441#ifdef FEAT_SESSION
8442 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8443 if (eap->cmdidx == CMD_mkview
8444 && (*eap->arg == NUL
8445 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8446 {
8447 eap->forceit = TRUE;
8448 fname = get_view_file(*eap->arg);
8449 if (fname == NULL)
8450 return;
8451 viewFile = fname;
8452 using_vdir = TRUE;
8453 }
8454 else
8455#endif
8456 if (*eap->arg != NUL)
8457 fname = eap->arg;
8458 else if (eap->cmdidx == CMD_mkvimrc)
8459 fname = (char_u *)VIMRC_FILE;
8460#ifdef FEAT_SESSION
8461 else if (eap->cmdidx == CMD_mksession)
8462 fname = (char_u *)SESSION_FILE;
8463#endif
8464 else
8465 fname = (char_u *)EXRC_FILE;
8466
8467#ifdef FEAT_BROWSE
8468 if (cmdmod.browse)
8469 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008470 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471# ifdef FEAT_SESSION
8472 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8473 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8474# endif
8475 (char_u *)_("Save Setup"),
8476 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8477 if (browseFile == NULL)
8478 goto theend;
8479 fname = browseFile;
8480 eap->forceit = TRUE; /* since dialog already asked */
8481 }
8482#endif
8483
8484#if defined(FEAT_SESSION) && defined(vim_mkdir)
8485 /* When using 'viewdir' may have to create the directory. */
8486 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008487 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488#endif
8489
8490 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8491 if (fd != NULL)
8492 {
8493#ifdef FEAT_SESSION
8494 if (eap->cmdidx == CMD_mkview)
8495 flagp = &vop_flags;
8496 else
8497 flagp = &ssop_flags;
8498#endif
8499
8500#ifdef MKSESSION_NL
8501 /* "unix" in 'sessionoptions': use NL line separator */
8502 if (view_session && (*flagp & SSOP_UNIX))
8503 mksession_nl = TRUE;
8504#endif
8505
8506 /* Write the version command for :mkvimrc */
8507 if (eap->cmdidx == CMD_mkvimrc)
8508 (void)put_line(fd, "version 6.0");
8509
8510#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008511 if (eap->cmdidx == CMD_mksession)
8512 {
8513 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8514 failed = TRUE;
8515 }
8516
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 if (eap->cmdidx != CMD_mkview)
8518#endif
8519 {
8520 /* Write setting 'compatible' first, because it has side effects.
8521 * For that same reason only do it when needed. */
8522 if (p_cp)
8523 (void)put_line(fd, "if !&cp | set cp | endif");
8524 else
8525 (void)put_line(fd, "if &cp | set nocp | endif");
8526 }
8527
8528#ifdef FEAT_SESSION
8529 if (!view_session
8530 || (eap->cmdidx == CMD_mksession
8531 && (*flagp & SSOP_OPTIONS)))
8532#endif
8533 failed |= (makemap(fd, NULL) == FAIL
8534 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8535
8536#ifdef FEAT_SESSION
8537 if (!failed && view_session)
8538 {
8539 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8540 failed = TRUE;
8541 if (eap->cmdidx == CMD_mksession)
8542 {
8543 char_u dirnow[MAXPATHL]; /* current directory */
8544
8545 /*
8546 * Change to session file's dir.
8547 */
8548 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8549 || mch_chdir((char *)dirnow) != 0)
8550 *dirnow = NUL;
8551 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8552 {
8553 if (vim_chdirfile(fname) == OK)
8554 shorten_fnames(TRUE);
8555 }
8556 else if (*dirnow != NUL
8557 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8558 {
8559 (void)mch_chdir((char *)globaldir);
8560 shorten_fnames(TRUE);
8561 }
8562
8563 failed |= (makeopens(fd, dirnow) == FAIL);
8564
8565 /* restore original dir */
8566 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8567 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8568 {
8569 if (mch_chdir((char *)dirnow) != 0)
8570 EMSG(_(e_prev_dir));
8571 shorten_fnames(TRUE);
8572 }
8573 }
8574 else
8575 {
8576 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8577 }
8578 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8579 == FAIL)
8580 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008581 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8582 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008583 if (eap->cmdidx == CMD_mksession)
8584 {
8585 if (put_line(fd, "unlet SessionLoad") == FAIL)
8586 failed = TRUE;
8587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008590 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8591 failed = TRUE;
8592
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 failed |= fclose(fd);
8594
8595 if (failed)
8596 EMSG(_(e_write));
8597#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8598 else if (eap->cmdidx == CMD_mksession)
8599 {
8600 /* successful session write - set this_session var */
8601 char_u tbuf[MAXPATHL];
8602
8603 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8604 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8605 }
8606#endif
8607#ifdef MKSESSION_NL
8608 mksession_nl = FALSE;
8609#endif
8610 }
8611
8612#ifdef FEAT_BROWSE
8613theend:
8614 vim_free(browseFile);
8615#endif
8616#ifdef FEAT_SESSION
8617 vim_free(viewFile);
8618#endif
8619}
8620
Bram Moolenaardf177f62005-02-22 08:39:57 +00008621#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8622 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008623/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008624 int
8625vim_mkdir_emsg(name, prot)
8626 char_u *name;
8627 int prot;
8628{
8629 if (vim_mkdir(name, prot) != 0)
8630 {
8631 EMSG2(_("E739: Cannot create directory: %s"), name);
8632 return FAIL;
8633 }
8634 return OK;
8635}
8636#endif
8637
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638/*
8639 * Open a file for writing for an Ex command, with some checks.
8640 * Return file descriptor, or NULL on failure.
8641 */
8642 FILE *
8643open_exfile(fname, forceit, mode)
8644 char_u *fname;
8645 int forceit;
8646 char *mode; /* "w" for create new file or "a" for append */
8647{
8648 FILE *fd;
8649
8650#ifdef UNIX
8651 /* with Unix it is possible to open a directory */
8652 if (mch_isdir(fname))
8653 {
8654 EMSG2(_(e_isadir2), fname);
8655 return NULL;
8656 }
8657#endif
8658 if (!forceit && *mode != 'a' && vim_fexists(fname))
8659 {
8660 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8661 return NULL;
8662 }
8663
8664 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8665 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8666
8667 return fd;
8668}
8669
8670/*
8671 * ":mark" and ":k".
8672 */
8673 static void
8674ex_mark(eap)
8675 exarg_T *eap;
8676{
8677 pos_T pos;
8678
8679 if (*eap->arg == NUL) /* No argument? */
8680 EMSG(_(e_argreq));
8681 else if (eap->arg[1] != NUL) /* more than one character? */
8682 EMSG(_(e_trailing));
8683 else
8684 {
8685 pos = curwin->w_cursor; /* save curwin->w_cursor */
8686 curwin->w_cursor.lnum = eap->line2;
8687 beginline(BL_WHITE | BL_FIX);
8688 if (setmark(*eap->arg) == FAIL) /* set mark */
8689 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8690 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8691 }
8692}
8693
8694/*
8695 * Update w_topline, w_leftcol and the cursor position.
8696 */
8697 void
8698update_topline_cursor()
8699{
8700 check_cursor(); /* put cursor on valid line */
8701 update_topline();
8702 if (!curwin->w_p_wrap)
8703 validate_cursor();
8704 update_curswant();
8705}
8706
8707#ifdef FEAT_EX_EXTRA
8708/*
8709 * ":normal[!] {commands}": Execute normal mode commands.
8710 */
8711 static void
8712ex_normal(eap)
8713 exarg_T *eap;
8714{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 int save_msg_scroll = msg_scroll;
8716 int save_restart_edit = restart_edit;
8717 int save_msg_didout = msg_didout;
8718 int save_State = State;
8719 tasave_T tabuf;
8720 int save_insertmode = p_im;
8721 int save_finish_op = finish_op;
8722#ifdef FEAT_MBYTE
8723 char_u *arg = NULL;
8724 int l;
8725 char_u *p;
8726#endif
8727
8728 if (ex_normal_busy >= p_mmd)
8729 {
8730 EMSG(_("E192: Recursive use of :normal too deep"));
8731 return;
8732 }
8733 ++ex_normal_busy;
8734
8735 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8736 restart_edit = 0; /* don't go to Insert mode */
8737 p_im = FALSE; /* don't use 'insertmode' */
8738
8739#ifdef FEAT_MBYTE
8740 /*
8741 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8742 * this for the K_SPECIAL leading byte, otherwise special keys will not
8743 * work.
8744 */
8745 if (has_mbyte)
8746 {
8747 int len = 0;
8748
8749 /* Count the number of characters to be escaped. */
8750 for (p = eap->arg; *p != NUL; ++p)
8751 {
8752# ifdef FEAT_GUI
8753 if (*p == CSI) /* leadbyte CSI */
8754 len += 2;
8755# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008756 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8758# ifdef FEAT_GUI
8759 || *p == CSI
8760# endif
8761 )
8762 len += 2;
8763 }
8764 if (len > 0)
8765 {
8766 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8767 if (arg != NULL)
8768 {
8769 len = 0;
8770 for (p = eap->arg; *p != NUL; ++p)
8771 {
8772 arg[len++] = *p;
8773# ifdef FEAT_GUI
8774 if (*p == CSI)
8775 {
8776 arg[len++] = KS_EXTRA;
8777 arg[len++] = (int)KE_CSI;
8778 }
8779# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008780 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {
8782 arg[len++] = *++p;
8783 if (*p == K_SPECIAL)
8784 {
8785 arg[len++] = KS_SPECIAL;
8786 arg[len++] = KE_FILLER;
8787 }
8788# ifdef FEAT_GUI
8789 else if (*p == CSI)
8790 {
8791 arg[len++] = KS_EXTRA;
8792 arg[len++] = (int)KE_CSI;
8793 }
8794# endif
8795 }
8796 arg[len] = NUL;
8797 }
8798 }
8799 }
8800 }
8801#endif
8802
8803 /*
8804 * Save the current typeahead. This is required to allow using ":normal"
8805 * from an event handler and makes sure we don't hang when the argument
8806 * ends with half a command.
8807 */
8808 save_typeahead(&tabuf);
8809 if (tabuf.typebuf_valid)
8810 {
8811 /*
8812 * Repeat the :normal command for each line in the range. When no
8813 * range given, execute it just once, without positioning the cursor
8814 * first.
8815 */
8816 do
8817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 if (eap->addr_count != 0)
8819 {
8820 curwin->w_cursor.lnum = eap->line1++;
8821 curwin->w_cursor.col = 0;
8822 }
8823
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008824 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825#ifdef FEAT_MBYTE
8826 arg != NULL ? arg :
8827#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008828 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 }
8830 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8831 }
8832
8833 /* Might not return to the main loop when in an event handler. */
8834 update_topline_cursor();
8835
8836 /* Restore the previous typeahead. */
8837 restore_typeahead(&tabuf);
8838
8839 --ex_normal_busy;
8840 msg_scroll = save_msg_scroll;
8841 restart_edit = save_restart_edit;
8842 p_im = save_insertmode;
8843 finish_op = save_finish_op;
8844 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8845
8846 /* Restore the state (needed when called from a function executed for
8847 * 'indentexpr'). */
8848 State = save_State;
8849#ifdef FEAT_MBYTE
8850 vim_free(arg);
8851#endif
8852}
8853
8854/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008855 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 */
8857 static void
8858ex_startinsert(eap)
8859 exarg_T *eap;
8860{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008861 if (eap->forceit)
8862 {
8863 coladvance((colnr_T)MAXCOL);
8864 curwin->w_curswant = MAXCOL;
8865 curwin->w_set_curswant = FALSE;
8866 }
8867
Bram Moolenaara40c5002005-01-09 21:16:21 +00008868 /* Ignore the command when already in Insert mode. Inserting an
8869 * expression register that invokes a function can do this. */
8870 if (State & INSERT)
8871 return;
8872
Bram Moolenaar64969662005-12-14 21:59:55 +00008873 if (eap->cmdidx == CMD_startinsert)
8874 restart_edit = 'a';
8875 else if (eap->cmdidx == CMD_startreplace)
8876 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008878 restart_edit = 'V';
8879
8880 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008882 if (eap->cmdidx == CMD_startinsert)
8883 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 curwin->w_curswant = 0; /* avoid MAXCOL */
8885 }
8886}
8887
8888/*
8889 * ":stopinsert"
8890 */
8891/*ARGSUSED*/
8892 static void
8893ex_stopinsert(eap)
8894 exarg_T *eap;
8895{
8896 restart_edit = 0;
8897 stop_insert_mode = TRUE;
8898}
8899#endif
8900
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008901#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8902/*
8903 * Execute normal mode command "cmd".
8904 * "remap" can be REMAP_NONE or REMAP_YES.
8905 */
8906 void
8907exec_normal_cmd(cmd, remap, silent)
8908 char_u *cmd;
8909 int remap;
8910 int silent;
8911{
8912 oparg_T oa;
8913
8914 /*
8915 * Stuff the argument into the typeahead buffer.
8916 * Execute normal_cmd() until there is no typeahead left.
8917 */
8918 clear_oparg(&oa);
8919 finish_op = FALSE;
8920 ins_typebuf(cmd, remap, 0, TRUE, silent);
8921 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8922 && !got_int)
8923 {
8924 update_topline_cursor();
8925 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8926 }
8927}
8928#endif
8929
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930#ifdef FEAT_FIND_ID
8931 static void
8932ex_checkpath(eap)
8933 exarg_T *eap;
8934{
8935 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8936 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8937 (linenr_T)1, (linenr_T)MAXLNUM);
8938}
8939
8940#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8941/*
8942 * ":psearch"
8943 */
8944 static void
8945ex_psearch(eap)
8946 exarg_T *eap;
8947{
8948 g_do_tagpreview = p_pvh;
8949 ex_findpat(eap);
8950 g_do_tagpreview = 0;
8951}
8952#endif
8953
8954 static void
8955ex_findpat(eap)
8956 exarg_T *eap;
8957{
8958 int whole = TRUE;
8959 long n;
8960 char_u *p;
8961 int action;
8962
8963 switch (cmdnames[eap->cmdidx].cmd_name[2])
8964 {
8965 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8966 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8967 action = ACTION_GOTO;
8968 else
8969 action = ACTION_SHOW;
8970 break;
8971 case 'i': /* ":ilist" and ":dlist" */
8972 action = ACTION_SHOW_ALL;
8973 break;
8974 case 'u': /* ":ijump" and ":djump" */
8975 action = ACTION_GOTO;
8976 break;
8977 default: /* ":isplit" and ":dsplit" */
8978 action = ACTION_SPLIT;
8979 break;
8980 }
8981
8982 n = 1;
8983 if (vim_isdigit(*eap->arg)) /* get count */
8984 {
8985 n = getdigits(&eap->arg);
8986 eap->arg = skipwhite(eap->arg);
8987 }
8988 if (*eap->arg == '/') /* Match regexp, not just whole words */
8989 {
8990 whole = FALSE;
8991 ++eap->arg;
8992 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8993 if (*p)
8994 {
8995 *p++ = NUL;
8996 p = skipwhite(p);
8997
8998 /* Check for trailing illegal characters */
8999 if (!ends_excmd(*p))
9000 eap->errmsg = e_trailing;
9001 else
9002 eap->nextcmd = check_nextcmd(p);
9003 }
9004 }
9005 if (!eap->skip)
9006 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9007 whole, !eap->forceit,
9008 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9009 n, action, eap->line1, eap->line2);
9010}
9011#endif
9012
9013#ifdef FEAT_WINDOWS
9014
9015# ifdef FEAT_QUICKFIX
9016/*
9017 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9018 */
9019 static void
9020ex_ptag(eap)
9021 exarg_T *eap;
9022{
9023 g_do_tagpreview = p_pvh;
9024 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9025}
9026
9027/*
9028 * ":pedit"
9029 */
9030 static void
9031ex_pedit(eap)
9032 exarg_T *eap;
9033{
9034 win_T *curwin_save = curwin;
9035
9036 g_do_tagpreview = p_pvh;
9037 prepare_tagpreview();
9038 keep_help_flag = curwin_save->w_buffer->b_help;
9039 do_exedit(eap, NULL);
9040 keep_help_flag = FALSE;
9041 if (curwin != curwin_save && win_valid(curwin_save))
9042 {
9043 /* Return cursor to where we were */
9044 validate_cursor();
9045 redraw_later(VALID);
9046 win_enter(curwin_save, TRUE);
9047 }
9048 g_do_tagpreview = 0;
9049}
9050# endif
9051
9052/*
9053 * ":stag", ":stselect" and ":stjump".
9054 */
9055 static void
9056ex_stag(eap)
9057 exarg_T *eap;
9058{
9059 postponed_split = -1;
9060 postponed_split_flags = cmdmod.split;
9061 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9062 postponed_split_flags = 0;
9063}
9064#endif
9065
9066/*
9067 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9068 */
9069 static void
9070ex_tag(eap)
9071 exarg_T *eap;
9072{
9073 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9074}
9075
9076 static void
9077ex_tag_cmd(eap, name)
9078 exarg_T *eap;
9079 char_u *name;
9080{
9081 int cmd;
9082
9083 switch (name[1])
9084 {
9085 case 'j': cmd = DT_JUMP; /* ":tjump" */
9086 break;
9087 case 's': cmd = DT_SELECT; /* ":tselect" */
9088 break;
9089 case 'p': cmd = DT_PREV; /* ":tprevious" */
9090 break;
9091 case 'N': cmd = DT_PREV; /* ":tNext" */
9092 break;
9093 case 'n': cmd = DT_NEXT; /* ":tnext" */
9094 break;
9095 case 'o': cmd = DT_POP; /* ":pop" */
9096 break;
9097 case 'f': /* ":tfirst" */
9098 case 'r': cmd = DT_FIRST; /* ":trewind" */
9099 break;
9100 case 'l': cmd = DT_LAST; /* ":tlast" */
9101 break;
9102 default: /* ":tag" */
9103#ifdef FEAT_CSCOPE
9104 if (p_cst)
9105 {
9106 do_cstag(eap);
9107 return;
9108 }
9109#endif
9110 cmd = DT_TAG;
9111 break;
9112 }
9113
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009114 if (name[0] == 'l')
9115 {
9116#ifndef FEAT_QUICKFIX
9117 ex_ni(eap);
9118 return;
9119#else
9120 cmd = DT_LTAG;
9121#endif
9122 }
9123
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9125 eap->forceit, TRUE);
9126}
9127
9128/*
9129 * Evaluate cmdline variables.
9130 *
9131 * change '%' to curbuf->b_ffname
9132 * '#' to curwin->w_altfile
9133 * '<cword>' to word under the cursor
9134 * '<cWORD>' to WORD under the cursor
9135 * '<cfile>' to path name under the cursor
9136 * '<sfile>' to sourced file name
9137 * '<afile>' to file name for autocommand
9138 * '<abuf>' to buffer number for autocommand
9139 * '<amatch>' to matching name for autocommand
9140 *
9141 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9142 * "" for error without a message) and NULL is returned.
9143 * Returns an allocated string if a valid match was found.
9144 * Returns NULL if no match was found. "usedlen" then still contains the
9145 * number of characters to skip.
9146 */
9147 char_u *
9148eval_vars(src, usedlen, lnump, errormsg, srcstart)
9149 char_u *src; /* pointer into commandline */
9150 int *usedlen; /* characters after src that are used */
9151 linenr_T *lnump; /* line number for :e command, or NULL */
9152 char_u **errormsg; /* pointer to error message */
9153 char_u *srcstart; /* beginning of valid memory for src */
9154{
9155 int i;
9156 char_u *s;
9157 char_u *result;
9158 char_u *resultbuf = NULL;
9159 int resultlen;
9160 buf_T *buf;
9161 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9162 int spec_idx;
9163#ifdef FEAT_MODIFY_FNAME
9164 int skip_mod = FALSE;
9165#endif
9166 static char *(spec_str[]) =
9167 {
9168 "%",
9169#define SPEC_PERC 0
9170 "#",
9171#define SPEC_HASH 1
9172 "<cword>", /* cursor word */
9173#define SPEC_CWORD 2
9174 "<cWORD>", /* cursor WORD */
9175#define SPEC_CCWORD 3
9176 "<cfile>", /* cursor path name */
9177#define SPEC_CFILE 4
9178 "<sfile>", /* ":so" file name */
9179#define SPEC_SFILE 5
9180#ifdef FEAT_AUTOCMD
9181 "<afile>", /* autocommand file name */
9182# define SPEC_AFILE 6
9183 "<abuf>", /* autocommand buffer number */
9184# define SPEC_ABUF 7
9185 "<amatch>", /* autocommand match name */
9186# define SPEC_AMATCH 8
9187#endif
9188#ifdef FEAT_CLIENTSERVER
9189 "<client>"
9190# define SPEC_CLIENT 9
9191#endif
9192 };
9193#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9194
9195#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9196 char_u strbuf[30];
9197#endif
9198
9199 *errormsg = NULL;
9200
9201 /*
9202 * Check if there is something to do.
9203 */
9204 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9205 {
9206 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9207 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9208 break;
9209 }
9210 if (spec_idx == SPEC_COUNT) /* no match */
9211 {
9212 *usedlen = 1;
9213 return NULL;
9214 }
9215
9216 /*
9217 * Skip when preceded with a backslash "\%" and "\#".
9218 * Note: In "\\%" the % is also not recognized!
9219 */
9220 if (src > srcstart && src[-1] == '\\')
9221 {
9222 *usedlen = 0;
9223 STRCPY(src - 1, src); /* remove backslash */
9224 return NULL;
9225 }
9226
9227 /*
9228 * word or WORD under cursor
9229 */
9230 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9231 {
9232 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9233 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9234 if (resultlen == 0)
9235 {
9236 *errormsg = (char_u *)"";
9237 return NULL;
9238 }
9239 }
9240
9241 /*
9242 * '#': Alternate file name
9243 * '%': Current file name
9244 * File name under the cursor
9245 * File name for autocommand
9246 * and following modifiers
9247 */
9248 else
9249 {
9250 switch (spec_idx)
9251 {
9252 case SPEC_PERC: /* '%': current file */
9253 if (curbuf->b_fname == NULL)
9254 {
9255 result = (char_u *)"";
9256 valid = 0; /* Must have ":p:h" to be valid */
9257 }
9258 else
9259#ifdef RISCOS
9260 /* Always use the full path for RISC OS if possible. */
9261 result = curbuf->b_ffname;
9262 if (result == NULL)
9263 result = curbuf->b_fname;
9264#else
9265 result = curbuf->b_fname;
9266#endif
9267 break;
9268
9269 case SPEC_HASH: /* '#' or "#99": alternate file */
9270 if (src[1] == '#') /* "##": the argument list */
9271 {
9272 result = arg_all();
9273 resultbuf = result;
9274 *usedlen = 2;
9275#ifdef FEAT_MODIFY_FNAME
9276 skip_mod = TRUE;
9277#endif
9278 break;
9279 }
9280 s = src + 1;
9281 i = (int)getdigits(&s);
9282 *usedlen = (int)(s - src); /* length of what we expand */
9283
9284 buf = buflist_findnr(i);
9285 if (buf == NULL)
9286 {
9287 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9288 return NULL;
9289 }
9290 if (lnump != NULL)
9291 *lnump = ECMD_LAST;
9292 if (buf->b_fname == NULL)
9293 {
9294 result = (char_u *)"";
9295 valid = 0; /* Must have ":p:h" to be valid */
9296 }
9297 else
9298 result = buf->b_fname;
9299 break;
9300
9301#ifdef FEAT_SEARCHPATH
9302 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009303 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 if (result == NULL)
9305 {
9306 *errormsg = (char_u *)"";
9307 return NULL;
9308 }
9309 resultbuf = result; /* remember allocated string */
9310 break;
9311#endif
9312
9313#ifdef FEAT_AUTOCMD
9314 case SPEC_AFILE: /* file name for autocommand */
9315 result = autocmd_fname;
9316 if (result == NULL)
9317 {
9318 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9319 return NULL;
9320 }
9321 break;
9322
9323 case SPEC_ABUF: /* buffer number for autocommand */
9324 if (autocmd_bufnr <= 0)
9325 {
9326 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9327 return NULL;
9328 }
9329 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9330 result = strbuf;
9331 break;
9332
9333 case SPEC_AMATCH: /* match name for autocommand */
9334 result = autocmd_match;
9335 if (result == NULL)
9336 {
9337 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9338 return NULL;
9339 }
9340 break;
9341
9342#endif
9343 case SPEC_SFILE: /* file name for ":so" command */
9344 result = sourcing_name;
9345 if (result == NULL)
9346 {
9347 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9348 return NULL;
9349 }
9350 break;
9351#if defined(FEAT_CLIENTSERVER)
9352 case SPEC_CLIENT: /* Source of last submitted input */
9353 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9354 result = strbuf;
9355 break;
9356#endif
9357 }
9358
9359 resultlen = (int)STRLEN(result); /* length of new string */
9360 if (src[*usedlen] == '<') /* remove the file name extension */
9361 {
9362 ++*usedlen;
9363#ifdef RISCOS
9364 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9365#else
9366 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9367#endif
9368 resultlen = (int)(s - result);
9369 }
9370#ifdef FEAT_MODIFY_FNAME
9371 else if (!skip_mod)
9372 {
9373 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9374 &resultlen);
9375 if (result == NULL)
9376 {
9377 *errormsg = (char_u *)"";
9378 return NULL;
9379 }
9380 }
9381#endif
9382 }
9383
9384 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9385 {
9386 if (valid != VALID_HEAD + VALID_PATH)
9387 /* xgettext:no-c-format */
9388 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9389 else
9390 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9391 result = NULL;
9392 }
9393 else
9394 result = vim_strnsave(result, resultlen);
9395 vim_free(resultbuf);
9396 return result;
9397}
9398
9399/*
9400 * Concatenate all files in the argument list, separated by spaces, and return
9401 * it in one allocated string.
9402 * Spaces and backslashes in the file names are escaped with a backslash.
9403 * Returns NULL when out of memory.
9404 */
9405 static char_u *
9406arg_all()
9407{
9408 int len;
9409 int idx;
9410 char_u *retval = NULL;
9411 char_u *p;
9412
9413 /*
9414 * Do this loop two times:
9415 * first time: compute the total length
9416 * second time: concatenate the names
9417 */
9418 for (;;)
9419 {
9420 len = 0;
9421 for (idx = 0; idx < ARGCOUNT; ++idx)
9422 {
9423 p = alist_name(&ARGLIST[idx]);
9424 if (p != NULL)
9425 {
9426 if (len > 0)
9427 {
9428 /* insert a space in between names */
9429 if (retval != NULL)
9430 retval[len] = ' ';
9431 ++len;
9432 }
9433 for ( ; *p != NUL; ++p)
9434 {
9435 if (*p == ' ' || *p == '\\')
9436 {
9437 /* insert a backslash */
9438 if (retval != NULL)
9439 retval[len] = '\\';
9440 ++len;
9441 }
9442 if (retval != NULL)
9443 retval[len] = *p;
9444 ++len;
9445 }
9446 }
9447 }
9448
9449 /* second time: break here */
9450 if (retval != NULL)
9451 {
9452 retval[len] = NUL;
9453 break;
9454 }
9455
9456 /* allocate memory */
9457 retval = alloc(len + 1);
9458 if (retval == NULL)
9459 break;
9460 }
9461
9462 return retval;
9463}
9464
9465#if defined(FEAT_AUTOCMD) || defined(PROTO)
9466/*
9467 * Expand the <sfile> string in "arg".
9468 *
9469 * Returns an allocated string, or NULL for any error.
9470 */
9471 char_u *
9472expand_sfile(arg)
9473 char_u *arg;
9474{
9475 char_u *errormsg;
9476 int len;
9477 char_u *result;
9478 char_u *newres;
9479 char_u *repl;
9480 int srclen;
9481 char_u *p;
9482
9483 result = vim_strsave(arg);
9484 if (result == NULL)
9485 return NULL;
9486
9487 for (p = result; *p; )
9488 {
9489 if (STRNCMP(p, "<sfile>", 7) != 0)
9490 ++p;
9491 else
9492 {
9493 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9494 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9495 if (errormsg != NULL)
9496 {
9497 if (*errormsg)
9498 emsg(errormsg);
9499 vim_free(result);
9500 return NULL;
9501 }
9502 if (repl == NULL) /* no match (cannot happen) */
9503 {
9504 p += srclen;
9505 continue;
9506 }
9507 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9508 newres = alloc(len);
9509 if (newres == NULL)
9510 {
9511 vim_free(repl);
9512 vim_free(result);
9513 return NULL;
9514 }
9515 mch_memmove(newres, result, (size_t)(p - result));
9516 STRCPY(newres + (p - result), repl);
9517 len = (int)STRLEN(newres);
9518 STRCAT(newres, p + srclen);
9519 vim_free(repl);
9520 vim_free(result);
9521 result = newres;
9522 p = newres + len; /* continue after the match */
9523 }
9524 }
9525
9526 return result;
9527}
9528#endif
9529
9530#ifdef FEAT_SESSION
9531static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9532static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9533static frame_T *ses_skipframe __ARGS((frame_T *fr));
9534static int ses_do_frame __ARGS((frame_T *fr));
9535static int ses_do_win __ARGS((win_T *wp));
9536static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9537static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9538static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9539
9540/*
9541 * Write openfile commands for the current buffers to an .exrc file.
9542 * Return FAIL on error, OK otherwise.
9543 */
9544 static int
9545makeopens(fd, dirnow)
9546 FILE *fd;
9547 char_u *dirnow; /* Current directory name */
9548{
9549 buf_T *buf;
9550 int only_save_windows = TRUE;
9551 int nr;
9552 int cnr = 1;
9553 int restore_size = TRUE;
9554 win_T *wp;
9555 char_u *sname;
9556 win_T *edited_win = NULL;
9557
9558 if (ssop_flags & SSOP_BUFFERS)
9559 only_save_windows = FALSE; /* Save ALL buffers */
9560
9561 /*
9562 * Begin by setting the this_session variable, and then other
9563 * sessionable variables.
9564 */
9565#ifdef FEAT_EVAL
9566 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9567 return FAIL;
9568 if (ssop_flags & SSOP_GLOBALS)
9569 if (store_session_globals(fd) == FAIL)
9570 return FAIL;
9571#endif
9572
9573 /*
9574 * Close all windows but one.
9575 */
9576 if (put_line(fd, "silent only") == FAIL)
9577 return FAIL;
9578
9579 /*
9580 * Now a :cd command to the session directory or the current directory
9581 */
9582 if (ssop_flags & SSOP_SESDIR)
9583 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009584 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9585 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 return FAIL;
9587 }
9588 else if (ssop_flags & SSOP_CURDIR)
9589 {
9590 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9591 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009592 || fputs("cd ", fd) < 0
9593 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9594 || put_eol(fd) == FAIL)
9595 {
9596 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 vim_free(sname);
9600 }
9601
9602 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009603 * If there is an empty, unnamed buffer we will wipe it out later.
9604 * Remember the buffer number.
9605 */
9606 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9607 return FAIL;
9608 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9609 return FAIL;
9610 if (put_line(fd, "endif") == FAIL)
9611 return FAIL;
9612
9613 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 * Now save the current files, current buffer first.
9615 */
9616 if (put_line(fd, "set shortmess=aoO") == FAIL)
9617 return FAIL;
9618
9619 /* Now put the other buffers into the buffer list */
9620 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9621 {
9622 if (!(only_save_windows && buf->b_nwindows == 0)
9623 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9624 && buf->b_fname != NULL
9625 && buf->b_p_bl)
9626 {
9627 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9628 : buf->b_wininfo->wi_fpos.lnum) < 0
9629 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9630 return FAIL;
9631 }
9632 }
9633
9634 /* the global argument list */
9635 if (ses_arglist(fd, "args", &global_alist.al_ga,
9636 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9637 return FAIL;
9638
9639 if (ssop_flags & SSOP_RESIZE)
9640 {
9641 /* Note: after the restore we still check it worked!*/
9642 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9643 || put_eol(fd) == FAIL)
9644 return FAIL;
9645 }
9646
9647#ifdef FEAT_GUI
9648 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9649 {
9650 int x, y;
9651
9652 if (gui_mch_get_winpos(&x, &y) == OK)
9653 {
9654 /* Note: after the restore we still check it worked!*/
9655 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9656 return FAIL;
9657 }
9658 }
9659#endif
9660
9661 /*
9662 * Before creating the window layout, try loading one file. If this is
9663 * aborted we don't end up with a number of useless windows.
9664 * This may have side effects! (e.g., compressed or network file).
9665 */
9666 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9667 {
9668 if (ses_do_win(wp)
9669 && wp->w_buffer->b_ffname != NULL
9670 && !wp->w_buffer->b_help
9671#ifdef FEAT_QUICKFIX
9672 && !bt_nofile(wp->w_buffer)
9673#endif
9674 )
9675 {
9676 if (fputs("edit ", fd) < 0
9677 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9678 return FAIL;
9679 if (!wp->w_arg_idx_invalid)
9680 edited_win = wp;
9681 break;
9682 }
9683 }
9684
9685 /*
9686 * Save current window layout.
9687 */
9688 if (put_line(fd, "set splitbelow splitright") == FAIL)
9689 return FAIL;
9690 if (ses_win_rec(fd, topframe) == FAIL)
9691 return FAIL;
9692 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9693 return FAIL;
9694 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9695 return FAIL;
9696
9697 /*
9698 * Check if window sizes can be restored (no windows omitted).
9699 * Remember the window number of the current window after restoring.
9700 */
9701 nr = 0;
9702 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9703 {
9704 if (ses_do_win(wp))
9705 ++nr;
9706 else
9707 restore_size = FALSE;
9708 if (curwin == wp)
9709 cnr = nr;
9710 }
9711
9712 /* Go to the first window. */
9713 if (put_line(fd, "wincmd t") == FAIL)
9714 return FAIL;
9715
9716 /*
9717 * If more than one window, see if sizes can be restored.
9718 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9719 * resized when moving between windows.
9720 * Do this before restoring the view, so that the topline and the cursor
9721 * can be set. This is done again below.
9722 */
9723 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9724 return FAIL;
9725 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9726 return FAIL;
9727
9728 /*
9729 * Restore the view of the window (options, file, cursor, etc.).
9730 */
9731 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9732 {
9733 if (!ses_do_win(wp))
9734 continue;
9735 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9736 return FAIL;
9737 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9738 return FAIL;
9739 }
9740
9741 /*
9742 * Restore cursor to the current window if it's not the first one.
9743 */
9744 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9745 return FAIL;
9746
9747 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009748 * Wipe out an empty unnamed buffer we started in.
9749 */
9750 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9751 return FAIL;
9752 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9753 return FAIL;
9754 if (put_line(fd, "endif") == FAIL)
9755 return FAIL;
9756 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9757 return FAIL;
9758
9759 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 * Restore window sizes again after jumping around in windows, because the
9761 * current window has a minimum size while others may not.
9762 */
9763 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9764 return FAIL;
9765
9766 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9767 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9768 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9769 return FAIL;
9770
9771 /*
9772 * Lastly, execute the x.vim file if it exists.
9773 */
9774 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9775 || put_line(fd, "if file_readable(s:sx)") == FAIL
9776 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9777 || put_line(fd, "endif") == FAIL)
9778 return FAIL;
9779
9780 return OK;
9781}
9782
9783 static int
9784ses_winsizes(fd, restore_size)
9785 FILE *fd;
9786 int restore_size;
9787{
9788 int n = 0;
9789 win_T *wp;
9790
9791 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9792 {
9793 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9794 {
9795 if (!ses_do_win(wp))
9796 continue;
9797 ++n;
9798
9799 /* restore height when not full height */
9800 if (wp->w_height + wp->w_status_height < topframe->fr_height
9801 && (fprintf(fd,
9802 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9803 n, (long)wp->w_height, Rows / 2, Rows) < 0
9804 || put_eol(fd) == FAIL))
9805 return FAIL;
9806
9807 /* restore width when not full width */
9808 if (wp->w_width < Columns && (fprintf(fd,
9809 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9810 n, (long)wp->w_width, Columns / 2, Columns) < 0
9811 || put_eol(fd) == FAIL))
9812 return FAIL;
9813 }
9814 }
9815 else
9816 {
9817 /* Just equalise window sizes */
9818 if (put_line(fd, "wincmd =") == FAIL)
9819 return FAIL;
9820 }
9821 return OK;
9822}
9823
9824/*
9825 * Write commands to "fd" to recursively create windows for frame "fr",
9826 * horizontally and vertically split.
9827 * After the commands the last window in the frame is the current window.
9828 * Returns FAIL when writing the commands to "fd" fails.
9829 */
9830 static int
9831ses_win_rec(fd, fr)
9832 FILE *fd;
9833 frame_T *fr;
9834{
9835 frame_T *frc;
9836 int count = 0;
9837
9838 if (fr->fr_layout != FR_LEAF)
9839 {
9840 /* Find first frame that's not skipped and then create a window for
9841 * each following one (first frame is already there). */
9842 frc = ses_skipframe(fr->fr_child);
9843 if (frc != NULL)
9844 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9845 {
9846 /* Make window as big as possible so that we have lots of room
9847 * to split. */
9848 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9849 || put_line(fd, fr->fr_layout == FR_COL
9850 ? "split" : "vsplit") == FAIL)
9851 return FAIL;
9852 ++count;
9853 }
9854
9855 /* Go back to the first window. */
9856 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9857 ? "%dwincmd k" : "%dwincmd h", count) < 0
9858 || put_eol(fd) == FAIL))
9859 return FAIL;
9860
9861 /* Recursively create frames/windows in each window of this column or
9862 * row. */
9863 frc = ses_skipframe(fr->fr_child);
9864 while (frc != NULL)
9865 {
9866 ses_win_rec(fd, frc);
9867 frc = ses_skipframe(frc->fr_next);
9868 /* Go to next window. */
9869 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9870 return FAIL;
9871 }
9872 }
9873 return OK;
9874}
9875
9876/*
9877 * Skip frames that don't contain windows we want to save in the Session.
9878 * Returns NULL when there none.
9879 */
9880 static frame_T *
9881ses_skipframe(fr)
9882 frame_T *fr;
9883{
9884 frame_T *frc;
9885
9886 for (frc = fr; frc != NULL; frc = frc->fr_next)
9887 if (ses_do_frame(frc))
9888 break;
9889 return frc;
9890}
9891
9892/*
9893 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9894 * the Session.
9895 */
9896 static int
9897ses_do_frame(fr)
9898 frame_T *fr;
9899{
9900 frame_T *frc;
9901
9902 if (fr->fr_layout == FR_LEAF)
9903 return ses_do_win(fr->fr_win);
9904 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9905 if (ses_do_frame(frc))
9906 return TRUE;
9907 return FALSE;
9908}
9909
9910/*
9911 * Return non-zero if window "wp" is to be stored in the Session.
9912 */
9913 static int
9914ses_do_win(wp)
9915 win_T *wp;
9916{
9917 if (wp->w_buffer->b_fname == NULL
9918#ifdef FEAT_QUICKFIX
9919 /* When 'buftype' is "nofile" can't restore the window contents. */
9920 || bt_nofile(wp->w_buffer)
9921#endif
9922 )
9923 return (ssop_flags & SSOP_BLANK);
9924 if (wp->w_buffer->b_help)
9925 return (ssop_flags & SSOP_HELP);
9926 return TRUE;
9927}
9928
9929/*
9930 * Write commands to "fd" to restore the view of a window.
9931 * Caller must make sure 'scrolloff' is zero.
9932 */
9933 static int
9934put_view(fd, wp, add_edit, flagp)
9935 FILE *fd;
9936 win_T *wp;
9937 int add_edit; /* add ":edit" command to view */
9938 unsigned *flagp; /* vop_flags or ssop_flags */
9939{
9940 win_T *save_curwin;
9941 int f;
9942 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009943 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944
9945 /* Always restore cursor position for ":mksession". For ":mkview" only
9946 * when 'viewoptions' contains "cursor". */
9947 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9948
9949 /*
9950 * Local argument list.
9951 */
9952 if (wp->w_alist == &global_alist)
9953 {
9954 if (put_line(fd, "argglobal") == FAIL)
9955 return FAIL;
9956 }
9957 else
9958 {
9959 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9960 flagp == &vop_flags
9961 || !(*flagp & SSOP_CURDIR)
9962 || wp->w_localdir != NULL, flagp) == FAIL)
9963 return FAIL;
9964 }
9965
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009966 /* Only when part of a session: restore the argument index. Some
9967 * arguments may have been deleted, check if the index is valid. */
9968 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9969 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 {
9971 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9972 || put_eol(fd) == FAIL)
9973 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009974 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 }
9976
9977 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009978 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 {
9980 /*
9981 * Load the file.
9982 */
9983 if (wp->w_buffer->b_ffname != NULL
9984#ifdef FEAT_QUICKFIX
9985 && !bt_nofile(wp->w_buffer)
9986#endif
9987 )
9988 {
9989 /*
9990 * Editing a file in this buffer: use ":edit file".
9991 * This may have side effects! (e.g., compressed or network file).
9992 */
9993 if (fputs("edit ", fd) < 0
9994 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9995 return FAIL;
9996 }
9997 else
9998 {
9999 /* No file in this buffer, just make it empty. */
10000 if (put_line(fd, "enew") == FAIL)
10001 return FAIL;
10002#ifdef FEAT_QUICKFIX
10003 if (wp->w_buffer->b_ffname != NULL)
10004 {
10005 /* The buffer does have a name, but it's not a file name. */
10006 if (fputs("file ", fd) < 0
10007 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10008 return FAIL;
10009 }
10010#endif
10011 do_cursor = FALSE;
10012 }
10013 }
10014
10015 /*
10016 * Local mappings and abbreviations.
10017 */
10018 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10019 && makemap(fd, wp->w_buffer) == FAIL)
10020 return FAIL;
10021
10022 /*
10023 * Local options. Need to go to the window temporarily.
10024 * Store only local values when using ":mkview" and when ":mksession" is
10025 * used and 'sessionoptions' doesn't include "options".
10026 * Some folding options are always stored when "folds" is included,
10027 * otherwise the folds would not be restored correctly.
10028 */
10029 save_curwin = curwin;
10030 curwin = wp;
10031 curbuf = curwin->w_buffer;
10032 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10033 f = makeset(fd, OPT_LOCAL,
10034 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10035#ifdef FEAT_FOLDING
10036 else if (*flagp & SSOP_FOLDS)
10037 f = makefoldset(fd);
10038#endif
10039 else
10040 f = OK;
10041 curwin = save_curwin;
10042 curbuf = curwin->w_buffer;
10043 if (f == FAIL)
10044 return FAIL;
10045
10046#ifdef FEAT_FOLDING
10047 /*
10048 * Save Folds when 'buftype' is empty and for help files.
10049 */
10050 if ((*flagp & SSOP_FOLDS)
10051 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010052# ifdef FEAT_QUICKFIX
10053 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10054# endif
10055 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 {
10057 if (put_folds(fd, wp) == FAIL)
10058 return FAIL;
10059 }
10060#endif
10061
10062 /*
10063 * Set the cursor after creating folds, since that moves the cursor.
10064 */
10065 if (do_cursor)
10066 {
10067
10068 /* Restore the cursor line in the file and relatively in the
10069 * window. Don't use "G", it changes the jumplist. */
10070 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10071 (long)wp->w_cursor.lnum,
10072 (long)(wp->w_cursor.lnum - wp->w_topline),
10073 (long)wp->w_height / 2, (long)wp->w_height) < 0
10074 || put_eol(fd) == FAIL
10075 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10076 || put_line(fd, "exe s:l") == FAIL
10077 || put_line(fd, "normal! zt") == FAIL
10078 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10079 || put_eol(fd) == FAIL)
10080 return FAIL;
10081 /* Restore the cursor column and left offset when not wrapping. */
10082 if (wp->w_cursor.col == 0)
10083 {
10084 if (put_line(fd, "normal! 0") == FAIL)
10085 return FAIL;
10086 }
10087 else
10088 {
10089 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10090 {
10091 if (fprintf(fd,
10092 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10093 (long)wp->w_cursor.col,
10094 (long)(wp->w_cursor.col - wp->w_leftcol),
10095 (long)wp->w_width / 2, (long)wp->w_width) < 0
10096 || put_eol(fd) == FAIL
10097 || put_line(fd, "if s:c > 0") == FAIL
10098 || fprintf(fd,
10099 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10100 (long)wp->w_cursor.col) < 0
10101 || put_eol(fd) == FAIL
10102 || put_line(fd, "else") == FAIL
10103 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10104 || put_eol(fd) == FAIL
10105 || put_line(fd, "endif") == FAIL)
10106 return FAIL;
10107 }
10108 else
10109 {
10110 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10111 || put_eol(fd) == FAIL)
10112 return FAIL;
10113 }
10114 }
10115 }
10116
10117 /*
10118 * Local directory.
10119 */
10120 if (wp->w_localdir != NULL)
10121 {
10122 if (fputs("lcd ", fd) < 0
10123 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10124 || put_eol(fd) == FAIL)
10125 return FAIL;
10126 }
10127
10128 return OK;
10129}
10130
10131/*
10132 * Write an argument list to the session file.
10133 * Returns FAIL if writing fails.
10134 */
10135 static int
10136ses_arglist(fd, cmd, gap, fullname, flagp)
10137 FILE *fd;
10138 char *cmd;
10139 garray_T *gap;
10140 int fullname; /* TRUE: use full path name */
10141 unsigned *flagp;
10142{
10143 int i;
10144 char_u buf[MAXPATHL];
10145 char_u *s;
10146
10147 if (gap->ga_len == 0)
10148 return put_line(fd, "silent! argdel *");
10149 if (fputs(cmd, fd) < 0)
10150 return FAIL;
10151 for (i = 0; i < gap->ga_len; ++i)
10152 {
10153 /* NULL file names are skipped (only happens when out of memory). */
10154 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10155 if (s != NULL)
10156 {
10157 if (fullname)
10158 {
10159 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10160 s = buf;
10161 }
10162 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10163 return FAIL;
10164 }
10165 }
10166 return put_eol(fd);
10167}
10168
10169/*
10170 * Write a buffer name to the session file.
10171 * Also ends the line.
10172 * Returns FAIL if writing fails.
10173 */
10174 static int
10175ses_fname(fd, buf, flagp)
10176 FILE *fd;
10177 buf_T *buf;
10178 unsigned *flagp;
10179{
10180 char_u *name;
10181
10182 /* Use the short file name if the current directory is known at the time
10183 * the session file will be sourced. Don't do this for ":mkview", we
10184 * don't know the current directory. */
10185 if (buf->b_sfname != NULL
10186 && flagp == &ssop_flags
10187 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10188 name = buf->b_sfname;
10189 else
10190 name = buf->b_ffname;
10191 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10192 return FAIL;
10193 return OK;
10194}
10195
10196/*
10197 * Write a file name to the session file.
10198 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10199 * characters.
10200 * Returns FAIL if writing fails.
10201 */
10202 static int
10203ses_put_fname(fd, name, flagp)
10204 FILE *fd;
10205 char_u *name;
10206 unsigned *flagp;
10207{
10208 char_u *sname;
10209 int retval = OK;
10210 int c;
10211
10212 sname = home_replace_save(NULL, name);
10213 if (sname != NULL)
10214 name = sname;
10215 while (*name != NUL)
10216 {
10217#ifdef FEAT_MBYTE
10218 {
10219 int l;
10220
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010221 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 {
10223 /* copy a multibyte char */
10224 while (--l >= 0)
10225 {
10226 if (putc(*name, fd) != *name)
10227 retval = FAIL;
10228 ++name;
10229 }
10230 continue;
10231 }
10232 }
10233#endif
10234 c = *name++;
10235 if (c == '\\' && (*flagp & SSOP_SLASH))
10236 /* change a backslash to a forward slash */
10237 c = '/';
10238 else if ((vim_strchr(escape_chars, c) != NULL
10239#ifdef BACKSLASH_IN_FILENAME
10240 && c != '\\'
10241#endif
10242 ) || c == '#' || c == '%')
10243 {
10244 /* escape a special character with a backslash */
10245 if (putc('\\', fd) != '\\')
10246 retval = FAIL;
10247 }
10248 if (putc(c, fd) != c)
10249 retval = FAIL;
10250 }
10251 vim_free(sname);
10252 return retval;
10253}
10254
10255/*
10256 * ":loadview [nr]"
10257 */
10258 static void
10259ex_loadview(eap)
10260 exarg_T *eap;
10261{
10262 char_u *fname;
10263
10264 fname = get_view_file(*eap->arg);
10265 if (fname != NULL)
10266 {
10267 do_source(fname, FALSE, FALSE);
10268 vim_free(fname);
10269 }
10270}
10271
10272/*
10273 * Get the name of the view file for the current buffer.
10274 */
10275 static char_u *
10276get_view_file(c)
10277 int c;
10278{
10279 int len = 0;
10280 char_u *p, *s;
10281 char_u *retval;
10282 char_u *sname;
10283
10284 if (curbuf->b_ffname == NULL)
10285 {
10286 EMSG(_(e_noname));
10287 return NULL;
10288 }
10289 sname = home_replace_save(NULL, curbuf->b_ffname);
10290 if (sname == NULL)
10291 return NULL;
10292
10293 /*
10294 * We want a file name without separators, because we're not going to make
10295 * a directory.
10296 * "normal" path separator -> "=+"
10297 * "=" -> "=="
10298 * ":" path separator -> "=-"
10299 */
10300 for (p = sname; *p; ++p)
10301 if (*p == '=' || vim_ispathsep(*p))
10302 ++len;
10303 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10304 if (retval != NULL)
10305 {
10306 STRCPY(retval, p_vdir);
10307 add_pathsep(retval);
10308 s = retval + STRLEN(retval);
10309 for (p = sname; *p; ++p)
10310 {
10311 if (*p == '=')
10312 {
10313 *s++ = '=';
10314 *s++ = '=';
10315 }
10316 else if (vim_ispathsep(*p))
10317 {
10318 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010319#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 || defined(VMS)
10321 if (*p == ':')
10322 *s++ = '-';
10323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010325 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 }
10327 else
10328 *s++ = *p;
10329 }
10330 *s++ = '=';
10331 *s++ = c;
10332 STRCPY(s, ".vim");
10333 }
10334
10335 vim_free(sname);
10336 return retval;
10337}
10338
10339#endif /* FEAT_SESSION */
10340
10341/*
10342 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10343 * Return FAIL for a write error.
10344 */
10345 int
10346put_eol(fd)
10347 FILE *fd;
10348{
10349 if (
10350#ifdef USE_CRNL
10351 (
10352# ifdef MKSESSION_NL
10353 !mksession_nl &&
10354# endif
10355 (putc('\r', fd) < 0)) ||
10356#endif
10357 (putc('\n', fd) < 0))
10358 return FAIL;
10359 return OK;
10360}
10361
10362/*
10363 * Write a line to "fd".
10364 * Return FAIL for a write error.
10365 */
10366 int
10367put_line(fd, s)
10368 FILE *fd;
10369 char *s;
10370{
10371 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10372 return FAIL;
10373 return OK;
10374}
10375
10376#ifdef FEAT_VIMINFO
10377/*
10378 * ":rviminfo" and ":wviminfo".
10379 */
10380 static void
10381ex_viminfo(eap)
10382 exarg_T *eap;
10383{
10384 char_u *save_viminfo;
10385
10386 save_viminfo = p_viminfo;
10387 if (*p_viminfo == NUL)
10388 p_viminfo = (char_u *)"'100";
10389 if (eap->cmdidx == CMD_rviminfo)
10390 {
10391 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10392 EMSG(_("E195: Cannot open viminfo file for reading"));
10393 }
10394 else
10395 write_viminfo(eap->arg, eap->forceit);
10396 p_viminfo = save_viminfo;
10397}
10398#endif
10399
10400#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010401/*
10402 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010403 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 void
10406dialog_msg(buff, format, fname)
10407 char_u *buff;
10408 char *format;
10409 char_u *fname;
10410{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 if (fname == NULL)
10412 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010413 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414}
10415#endif
10416
10417/*
10418 * ":behave {mswin,xterm}"
10419 */
10420 static void
10421ex_behave(eap)
10422 exarg_T *eap;
10423{
10424 if (STRCMP(eap->arg, "mswin") == 0)
10425 {
10426 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10427 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10428 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10429 set_option_value((char_u *)"keymodel", 0L,
10430 (char_u *)"startsel,stopsel", 0);
10431 }
10432 else if (STRCMP(eap->arg, "xterm") == 0)
10433 {
10434 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10435 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10436 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10437 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10438 }
10439 else
10440 EMSG2(_(e_invarg2), eap->arg);
10441}
10442
10443#ifdef FEAT_AUTOCMD
10444static int filetype_detect = FALSE;
10445static int filetype_plugin = FALSE;
10446static int filetype_indent = FALSE;
10447
10448/*
10449 * ":filetype [plugin] [indent] {on,off,detect}"
10450 * on: Load the filetype.vim file to install autocommands for file types.
10451 * off: Load the ftoff.vim file to remove all autocommands for file types.
10452 * plugin on: load filetype.vim and ftplugin.vim
10453 * plugin off: load ftplugof.vim
10454 * indent on: load filetype.vim and indent.vim
10455 * indent off: load indoff.vim
10456 */
10457 static void
10458ex_filetype(eap)
10459 exarg_T *eap;
10460{
10461 char_u *arg = eap->arg;
10462 int plugin = FALSE;
10463 int indent = FALSE;
10464
10465 if (*eap->arg == NUL)
10466 {
10467 /* Print current status. */
10468 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10469 filetype_detect ? "ON" : "OFF",
10470 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10471 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10472 return;
10473 }
10474
10475 /* Accept "plugin" and "indent" in any order. */
10476 for (;;)
10477 {
10478 if (STRNCMP(arg, "plugin", 6) == 0)
10479 {
10480 plugin = TRUE;
10481 arg = skipwhite(arg + 6);
10482 continue;
10483 }
10484 if (STRNCMP(arg, "indent", 6) == 0)
10485 {
10486 indent = TRUE;
10487 arg = skipwhite(arg + 6);
10488 continue;
10489 }
10490 break;
10491 }
10492 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10493 {
10494 if (*arg == 'o' || !filetype_detect)
10495 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010496 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 filetype_detect = TRUE;
10498 if (plugin)
10499 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010500 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 filetype_plugin = TRUE;
10502 }
10503 if (indent)
10504 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010505 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 filetype_indent = TRUE;
10507 }
10508 }
10509 if (*arg == 'd')
10510 {
10511 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010512 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 }
10514 }
10515 else if (STRCMP(arg, "off") == 0)
10516 {
10517 if (plugin || indent)
10518 {
10519 if (plugin)
10520 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010521 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 filetype_plugin = FALSE;
10523 }
10524 if (indent)
10525 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010526 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 filetype_indent = FALSE;
10528 }
10529 }
10530 else
10531 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010532 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 filetype_detect = FALSE;
10534 }
10535 }
10536 else
10537 EMSG2(_(e_invarg2), arg);
10538}
10539
10540/*
10541 * ":setfiletype {name}"
10542 */
10543 static void
10544ex_setfiletype(eap)
10545 exarg_T *eap;
10546{
10547 if (!did_filetype)
10548 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10549}
10550#endif
10551
10552/*ARGSUSED*/
10553 static void
10554ex_digraphs(eap)
10555 exarg_T *eap;
10556{
10557#ifdef FEAT_DIGRAPHS
10558 if (*eap->arg != NUL)
10559 putdigraph(eap->arg);
10560 else
10561 listdigraphs();
10562#else
10563 EMSG(_("E196: No digraphs in this version"));
10564#endif
10565}
10566
10567 static void
10568ex_set(eap)
10569 exarg_T *eap;
10570{
10571 int flags = 0;
10572
10573 if (eap->cmdidx == CMD_setlocal)
10574 flags = OPT_LOCAL;
10575 else if (eap->cmdidx == CMD_setglobal)
10576 flags = OPT_GLOBAL;
10577#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10578 if (cmdmod.browse && flags == 0)
10579 ex_options(eap);
10580 else
10581#endif
10582 (void)do_set(eap->arg, flags);
10583}
10584
10585#ifdef FEAT_SEARCH_EXTRA
10586/*
10587 * ":nohlsearch"
10588 */
10589/*ARGSUSED*/
10590 static void
10591ex_nohlsearch(eap)
10592 exarg_T *eap;
10593{
10594 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010595 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596}
10597
10598/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010599 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 * Sets nextcmd to the start of the next command, if any. Also called when
10601 * skipping commands to find the next command.
10602 */
10603 static void
10604ex_match(eap)
10605 exarg_T *eap;
10606{
10607 char_u *p;
10608 char_u *end;
10609 int c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010610 int mi;
10611
10612 if (eap->line2 <= 3)
10613 mi = eap->line2 - 1;
10614 else
10615 {
10616 EMSG(e_invcmd);
10617 return;
10618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619
10620 /* First clear any old pattern. */
10621 if (!eap->skip)
10622 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010623 vim_free(curwin->w_match[mi].regprog);
10624 curwin->w_match[mi].regprog = NULL;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010625 redraw_later(SOME_VALID); /* always need a redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 }
10627
10628 if (ends_excmd(*eap->arg))
10629 end = eap->arg;
10630 else if ((STRNICMP(eap->arg, "none", 4) == 0
10631 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10632 end = eap->arg + 4;
10633 else
10634 {
10635 p = skiptowhite(eap->arg);
10636 if (!eap->skip)
10637 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010638 curwin->w_match_id[mi] = syn_namen2id(eap->arg,
10639 (int)(p - eap->arg));
10640 if (curwin->w_match_id[mi] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 {
10642 EMSG2(_(e_nogroup), eap->arg);
10643 return;
10644 }
10645 }
10646 p = skipwhite(p);
10647 if (*p == NUL)
10648 {
10649 /* There must be two arguments. */
10650 EMSG2(_(e_invarg2), eap->arg);
10651 return;
10652 }
10653 end = skip_regexp(p + 1, *p, TRUE, NULL);
10654 if (!eap->skip)
10655 {
10656 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10657 {
10658 eap->errmsg = e_trailing;
10659 return;
10660 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010661 if (*end != *p)
10662 {
10663 EMSG2(_(e_invarg2), p);
10664 return;
10665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666
10667 c = *end;
10668 *end = NUL;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010669 curwin->w_match[mi].regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 *end = c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010671 if (curwin->w_match[mi].regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 {
10673 EMSG2(_(e_invarg2), p);
10674 return;
10675 }
10676 }
10677 }
10678 eap->nextcmd = find_nextcmd(end);
10679}
10680#endif
10681
10682#ifdef FEAT_CRYPT
10683/*
10684 * ":X": Get crypt key
10685 */
10686/*ARGSUSED*/
10687 static void
10688ex_X(eap)
10689 exarg_T *eap;
10690{
10691 (void)get_crypt_key(TRUE, TRUE);
10692}
10693#endif
10694
10695#ifdef FEAT_FOLDING
10696 static void
10697ex_fold(eap)
10698 exarg_T *eap;
10699{
10700 if (foldManualAllowed(TRUE))
10701 foldCreate(eap->line1, eap->line2);
10702}
10703
10704 static void
10705ex_foldopen(eap)
10706 exarg_T *eap;
10707{
10708 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10709 eap->forceit, FALSE);
10710}
10711
10712 static void
10713ex_folddo(eap)
10714 exarg_T *eap;
10715{
10716 linenr_T lnum;
10717
10718 /* First set the marks for all lines closed/open. */
10719 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10720 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10721 ml_setmarked(lnum);
10722
10723 /* Execute the command on the marked lines. */
10724 global_exe(eap->arg);
10725 ml_clearmarked(); /* clear rest of the marks */
10726}
10727#endif