blob: cff6d4ccb7ab8cab7755a62915f0af188eb388b4 [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));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000304static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305static void ex_redir __ARGS((exarg_T *eap));
306static void ex_redraw __ARGS((exarg_T *eap));
307static void ex_redrawstatus __ARGS((exarg_T *eap));
308static void close_redir __ARGS((void));
309static void ex_mkrc __ARGS((exarg_T *eap));
310static void ex_mark __ARGS((exarg_T *eap));
311#ifdef FEAT_USR_CMDS
312static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000313static 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 +0000314#endif
315#ifdef FEAT_EX_EXTRA
316static void ex_normal __ARGS((exarg_T *eap));
317static void ex_startinsert __ARGS((exarg_T *eap));
318static void ex_stopinsert __ARGS((exarg_T *eap));
319#else
320# define ex_normal ex_ni
321# define ex_align ex_ni
322# define ex_retab ex_ni
323# define ex_startinsert ex_ni
324# define ex_stopinsert ex_ni
325# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000326# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#endif
328#ifdef FEAT_FIND_ID
329static void ex_checkpath __ARGS((exarg_T *eap));
330static void ex_findpat __ARGS((exarg_T *eap));
331#else
332# define ex_findpat ex_ni
333# define ex_checkpath ex_ni
334#endif
335#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
336static void ex_psearch __ARGS((exarg_T *eap));
337#else
338# define ex_psearch ex_ni
339#endif
340static void ex_tag __ARGS((exarg_T *eap));
341static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
342#ifndef FEAT_EVAL
343# define ex_scriptnames ex_ni
344# define ex_finish ex_ni
345# define ex_echo ex_ni
346# define ex_echohl ex_ni
347# define ex_execute ex_ni
348# define ex_call ex_ni
349# define ex_if ex_ni
350# define ex_endif ex_ni
351# define ex_else ex_ni
352# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000353# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354# define ex_continue ex_ni
355# define ex_break ex_ni
356# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000357# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358# define ex_throw ex_ni
359# define ex_try ex_ni
360# define ex_catch ex_ni
361# define ex_finally ex_ni
362# define ex_endtry ex_ni
363# define ex_endfunction ex_ni
364# define ex_let ex_ni
365# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000366# define ex_lockvar ex_ni
367# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368# define ex_function ex_ni
369# define ex_delfunction ex_ni
370# define ex_return ex_ni
371#endif
372static char_u *arg_all __ARGS((void));
373#ifdef FEAT_SESSION
374static int makeopens __ARGS((FILE *fd, char_u *dirnow));
375static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
376static void ex_loadview __ARGS((exarg_T *eap));
377static char_u *get_view_file __ARGS((int c));
378#else
379# define ex_loadview ex_ni
380#endif
381#ifndef FEAT_EVAL
382# define ex_compiler ex_ni
383#endif
384#ifdef FEAT_VIMINFO
385static void ex_viminfo __ARGS((exarg_T *eap));
386#else
387# define ex_viminfo ex_ni
388#endif
389static void ex_behave __ARGS((exarg_T *eap));
390#ifdef FEAT_AUTOCMD
391static void ex_filetype __ARGS((exarg_T *eap));
392static void ex_setfiletype __ARGS((exarg_T *eap));
393#else
394# define ex_filetype ex_ni
395# define ex_setfiletype ex_ni
396#endif
397#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000398# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399# define ex_diffpatch ex_ni
400# define ex_diffgetput ex_ni
401# define ex_diffsplit ex_ni
402# define ex_diffthis ex_ni
403# define ex_diffupdate ex_ni
404#endif
405static void ex_digraphs __ARGS((exarg_T *eap));
406static void ex_set __ARGS((exarg_T *eap));
407#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
408# define ex_options ex_ni
409#endif
410#ifdef FEAT_SEARCH_EXTRA
411static void ex_nohlsearch __ARGS((exarg_T *eap));
412static void ex_match __ARGS((exarg_T *eap));
413#else
414# define ex_nohlsearch ex_ni
415# define ex_match ex_ni
416#endif
417#ifdef FEAT_CRYPT
418static void ex_X __ARGS((exarg_T *eap));
419#else
420# define ex_X ex_ni
421#endif
422#ifdef FEAT_FOLDING
423static void ex_fold __ARGS((exarg_T *eap));
424static void ex_foldopen __ARGS((exarg_T *eap));
425static void ex_folddo __ARGS((exarg_T *eap));
426#else
427# define ex_fold ex_ni
428# define ex_foldopen ex_ni
429# define ex_folddo ex_ni
430#endif
431#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
432 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
433# define ex_language ex_ni
434#endif
435#ifndef FEAT_SIGNS
436# define ex_sign ex_ni
437#endif
438#ifndef FEAT_SUN_WORKSHOP
439# define ex_wsverb ex_ni
440#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000441#ifndef FEAT_NETBEANS_INTG
442# define ex_nbkey ex_ni
443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445#ifndef FEAT_EVAL
446# define ex_debug ex_ni
447# define ex_breakadd ex_ni
448# define ex_debuggreedy ex_ni
449# define ex_breakdel ex_ni
450# define ex_breaklist ex_ni
451#endif
452
453#ifndef FEAT_CMDHIST
454# define ex_history ex_ni
455#endif
456#ifndef FEAT_JUMPLIST
457# define ex_jumps ex_ni
458# define ex_changes ex_ni
459#endif
460
Bram Moolenaar05159a02005-02-26 23:04:13 +0000461#ifndef FEAT_PROFILE
462# define ex_profile ex_ni
463#endif
464
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465/*
466 * Declare cmdnames[].
467 */
468#define DO_DECLARE_EXCMD
469#include "ex_cmds.h"
470
471/*
472 * Table used to quickly search for a command, based on its first character.
473 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000474static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 CMD_append,
477 CMD_buffer,
478 CMD_change,
479 CMD_delete,
480 CMD_edit,
481 CMD_file,
482 CMD_global,
483 CMD_help,
484 CMD_insert,
485 CMD_join,
486 CMD_k,
487 CMD_list,
488 CMD_move,
489 CMD_next,
490 CMD_open,
491 CMD_print,
492 CMD_quit,
493 CMD_read,
494 CMD_substitute,
495 CMD_t,
496 CMD_undo,
497 CMD_vglobal,
498 CMD_write,
499 CMD_xit,
500 CMD_yank,
501 CMD_z,
502 CMD_bang
503};
504
505static char_u dollar_command[2] = {'$', 0};
506
507
508#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000509/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510typedef struct
511{
512 char_u *line; /* command line */
513 linenr_T lnum; /* sourcing_lnum of the line */
514} wcmd_T;
515
516/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000517 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000519 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000521struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 garray_T *lines_gap; /* growarray with line info */
524 int current_line; /* last read line from growarray */
525 int repeating; /* TRUE when looping a second time */
526 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
527 char_u *(*getline) __ARGS((int, void *, int));
528 void *cookie;
529};
530
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000531static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
532static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000534
535/* Struct to save a few things while debugging. Used in do_cmdline() only. */
536struct dbg_stuff
537{
538 int trylevel;
539 int force_abort;
540 except_T *caught_stack;
541 char_u *vv_exception;
542 char_u *vv_throwpoint;
543 int did_emsg;
544 int got_int;
545 int did_throw;
546 int need_rethrow;
547 int check_cstack;
548 except_T *current_exception;
549};
550
551static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
552static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
553
554 static void
555save_dbg_stuff(dsp)
556 struct dbg_stuff *dsp;
557{
558 dsp->trylevel = trylevel; trylevel = 0;
559 dsp->force_abort = force_abort; force_abort = FALSE;
560 dsp->caught_stack = caught_stack; caught_stack = NULL;
561 dsp->vv_exception = v_exception(NULL);
562 dsp->vv_throwpoint = v_throwpoint(NULL);
563
564 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
565 dsp->did_emsg = did_emsg; did_emsg = FALSE;
566 dsp->got_int = got_int; got_int = FALSE;
567 dsp->did_throw = did_throw; did_throw = FALSE;
568 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
569 dsp->check_cstack = check_cstack; check_cstack = FALSE;
570 dsp->current_exception = current_exception; current_exception = NULL;
571}
572
573 static void
574restore_dbg_stuff(dsp)
575 struct dbg_stuff *dsp;
576{
577 suppress_errthrow = FALSE;
578 trylevel = dsp->trylevel;
579 force_abort = dsp->force_abort;
580 caught_stack = dsp->caught_stack;
581 (void)v_exception(dsp->vv_exception);
582 (void)v_throwpoint(dsp->vv_throwpoint);
583 did_emsg = dsp->did_emsg;
584 got_int = dsp->got_int;
585 did_throw = dsp->did_throw;
586 need_rethrow = dsp->need_rethrow;
587 check_cstack = dsp->check_cstack;
588 current_exception = dsp->current_exception;
589}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#endif
591
592
593/*
594 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
595 * command is given.
596 */
597 void
598do_exmode(improved)
599 int improved; /* TRUE for "improved Ex" mode */
600{
601 int save_msg_scroll;
602 int prev_msg_row;
603 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000604 int changedtick;
605
606 if (improved)
607 exmode_active = EXMODE_VIM;
608 else
609 exmode_active = EXMODE_NORMAL;
610 State = NORMAL;
611
612 /* When using ":global /pat/ visual" and then "Q" we return to continue
613 * the :global command. */
614 if (global_busy)
615 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 save_msg_scroll = msg_scroll;
618 ++RedrawingDisabled; /* don't redisplay the window */
619 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#ifdef FEAT_GUI
621 /* Ignore scrollbar and mouse events in Ex mode */
622 ++hold_gui_events;
623#endif
624#ifdef FEAT_SNIFF
625 want_sniff_request = 0; /* No K_SNIFF wanted */
626#endif
627
628 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
629 while (exmode_active)
630 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000631#ifdef FEAT_EX_EXTRA
632 /* Check for a ":normal" command and no more characters left. */
633 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
634 {
635 exmode_active = FALSE;
636 break;
637 }
638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 msg_scroll = TRUE;
640 need_wait_return = FALSE;
641 ex_pressedreturn = FALSE;
642 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000643 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 prev_msg_row = msg_row;
645 prev_line = curwin->w_cursor.lnum;
646#ifdef FEAT_SNIFF
647 ProcessSniffRequests();
648#endif
649 if (improved)
650 {
651 cmdline_row = msg_row;
652 do_cmdline(NULL, getexline, NULL, 0);
653 }
654 else
655 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
656 lines_left = Rows - 1;
657
Bram Moolenaardf177f62005-02-22 08:39:57 +0000658 if ((prev_line != curwin->w_cursor.lnum
659 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000661 if (curbuf->b_ml.ml_flags & ML_EMPTY)
662 EMSG(_(e_emptybuf));
663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000665 if (ex_pressedreturn)
666 {
667 /* go up one line, to overwrite the ":<CR>" line, so the
668 * output doensn't contain empty lines. */
669 msg_row = prev_msg_row;
670 if (prev_msg_row == Rows - 1)
671 msg_row--;
672 }
673 msg_col = 0;
674 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
675 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000678 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
679 {
680 if (curbuf->b_ml.ml_flags & ML_EMPTY)
681 EMSG(_(e_emptybuf));
682 else
683 EMSG(_("E501: At end-of-file"));
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
686
687#ifdef FEAT_GUI
688 --hold_gui_events;
689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 --RedrawingDisabled;
691 --no_wait_return;
692 update_screen(CLEAR);
693 need_wait_return = FALSE;
694 msg_scroll = save_msg_scroll;
695}
696
697/*
698 * Execute a simple command line. Used for translated commands like "*".
699 */
700 int
701do_cmdline_cmd(cmd)
702 char_u *cmd;
703{
704 return do_cmdline(cmd, NULL, NULL,
705 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
706}
707
708/*
709 * do_cmdline(): execute one Ex command line
710 *
711 * 1. Execute "cmdline" when it is not NULL.
712 * If "cmdline" is NULL, or more lines are needed, getline() is used.
713 * 2. Split up in parts separated with '|'.
714 *
715 * This function can be called recursively!
716 *
717 * flags:
718 * DOCMD_VERBOSE - The command will be included in the error message.
719 * DOCMD_NOWAIT - Don't call wait_return() and friends.
720 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
721 * DOCMD_KEYTYPED - Don't reset KeyTyped.
722 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
723 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
724 *
725 * return FAIL if cmdline could not be executed, OK otherwise
726 */
727 int
728do_cmdline(cmdline, getline, cookie, flags)
729 char_u *cmdline;
730 char_u *(*getline) __ARGS((int, void *, int));
731 void *cookie; /* argument for getline() */
732 int flags;
733{
734 char_u *next_cmdline; /* next cmd to execute */
735 char_u *cmdline_copy = NULL; /* copy of cmd line */
736 int used_getline = FALSE; /* used "getline" to obtain command */
737 static int recursive = 0; /* recursive depth */
738 int msg_didout_before_start = 0;
739 int count = 0; /* line number count */
740 int did_inc = FALSE; /* incremented RedrawingDisabled */
741 int retval = OK;
742#ifdef FEAT_EVAL
743 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000744 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 int current_line = 0; /* active line in lines_ga */
746 char_u *fname = NULL; /* function or script name */
747 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
748 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000749 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 int initial_trylevel;
751 struct msglist **saved_msg_list = NULL;
752 struct msglist *private_msg_list;
753
754 /* "getline" and "cookie" passed to do_one_cmd() */
755 char_u *(*cmd_getline) __ARGS((int, void *, int));
756 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000757 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000759 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760#else
761# define cmd_getline getline
762# define cmd_cookie cookie
763#endif
764 static int call_depth = 0; /* recursiveness */
765
766#ifdef FEAT_EVAL
767 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
768 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000769 * This ensures that the do_errthrow() call in do_one_cmd() does not
770 * combine the messages stored by an earlier invocation of do_one_cmd()
771 * with the command name of the later one. This would happen when
772 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 saved_msg_list = msg_list;
774 msg_list = &private_msg_list;
775 private_msg_list = NULL;
776#endif
777
778 /* It's possible to create an endless loop with ":execute", catch that
779 * here. The value of 200 allows nested function calls, ":source", etc. */
780 if (call_depth == 200)
781 {
782 EMSG(_("E169: Command too recursive"));
783#ifdef FEAT_EVAL
784 /* When converting to an exception, we do not include the command name
785 * since this is not an error of the specific command. */
786 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
787 msg_list = saved_msg_list;
788#endif
789 return FAIL;
790 }
791 ++call_depth;
792
793#ifdef FEAT_EVAL
794 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000795 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 cstack.cs_trylevel = 0;
797 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
800
801 real_cookie = getline_cookie(getline, cookie);
802
803 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804 getline_is_func = getline_equal(getline, cookie, get_func_line);
805 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 ++ex_nesting_level;
807
808 /* Get the function or script name and the address where the next breakpoint
809 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {
812 fname = func_name(real_cookie);
813 breakpoint = func_breakpoint(real_cookie);
814 dbg_tick = func_dbg_tick(real_cookie);
815 }
816 else if (getline_equal(getline, cookie, getsourceline))
817 {
818 fname = sourcing_name;
819 breakpoint = source_breakpoint(real_cookie);
820 dbg_tick = source_dbg_tick(real_cookie);
821 }
822
823 /*
824 * Initialize "force_abort" and "suppress_errthrow" at the top level.
825 */
826 if (!recursive)
827 {
828 force_abort = FALSE;
829 suppress_errthrow = FALSE;
830 }
831
832 /*
833 * If requested, store and reset the global values controlling the
834 * exception handling (used when debugging).
835 */
836 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000837 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 initial_trylevel = trylevel;
840
841 /*
842 * "did_throw" will be set to TRUE when an exception is being thrown.
843 */
844 did_throw = FALSE;
845#endif
846 /*
847 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000848 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 * If force_abort is set, we cancel everything.
850 */
851 did_emsg = FALSE;
852
853 /*
854 * KeyTyped is only set when calling vgetc(). Reset it here when not
855 * calling vgetc() (sourced command lines).
856 */
857 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
858 KeyTyped = FALSE;
859
860 /*
861 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * - for multiple commands on one line, separated with '|'
864 * - when repeating until there are no more lines (for ":source")
865 */
866 next_cmdline = cmdline;
867 do
868 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869#ifdef FEAT_EVAL
870 getline_is_func = getline_equal(getline, cookie, get_func_line);
871#endif
872
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000873 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (next_cmdline == NULL
875#ifdef FEAT_EVAL
876 && !force_abort
877 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000878 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
880 )
881 did_emsg = FALSE;
882
883 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000884 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * 2. If no line given: Get an allocated line with getline().
886 * 3. If a line is given: Make a copy, so we can mess with it.
887 */
888
889#ifdef FEAT_EVAL
890 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000891 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
893 /* Each '|' separated command is stored separately in lines_ga, to
894 * be able to jump to it. Don't use next_cmdline now. */
895 vim_free(cmdline_copy);
896 cmdline_copy = NULL;
897
898 /* Check if a function has returned or, unless it has an unclosed
899 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000903 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904 func_line_end(real_cookie);
905# endif
906 if (func_has_ended(real_cookie))
907 {
908 retval = FAIL;
909 break;
910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000912#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000913 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914 && getline_equal(getline, cookie, getsourceline))
915 script_line_end();
916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 /* Check if a sourced file hit a ":finish" command. */
919 if (source_finished(getline, cookie))
920 {
921 retval = FAIL;
922 break;
923 }
924
925 /* If breakpoints have been added/deleted need to check for it. */
926 if (breakpoint != NULL && dbg_tick != NULL
927 && *dbg_tick != debug_tick)
928 {
929 *breakpoint = dbg_find_breakpoint(
930 getline_equal(getline, cookie, getsourceline),
931 fname, sourcing_lnum);
932 *dbg_tick = debug_tick;
933 }
934
935 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
936 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
937
938 /* Did we encounter a breakpoint? */
939 if (breakpoint != NULL && *breakpoint != 0
940 && *breakpoint <= sourcing_lnum)
941 {
942 dbg_breakpoint(fname, sourcing_lnum);
943 /* Find next breakpoint. */
944 *breakpoint = dbg_find_breakpoint(
945 getline_equal(getline, cookie, getsourceline),
946 fname, sourcing_lnum);
947 *dbg_tick = debug_tick;
948 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000950 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000951 {
952 if (getline_is_func)
953 func_line_start(real_cookie);
954 else if (getline_equal(getline, cookie, getsourceline))
955 script_line_start();
956 }
957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000960 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000962 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 * again. Pass a different "getline" function to do_one_cmd()
964 * below, so that it stores lines in or reads them from
965 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000966 * while/for loop. */
967 cmd_getline = get_loop_line;
968 cmd_cookie = (void *)&cmd_loop_cookie;
969 cmd_loop_cookie.lines_gap = &lines_ga;
970 cmd_loop_cookie.current_line = current_line;
971 cmd_loop_cookie.getline = getline;
972 cmd_loop_cookie.cookie = cookie;
973 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 }
975 else
976 {
977 cmd_getline = getline;
978 cmd_cookie = cookie;
979 }
980#endif
981
982 /* 2. If no line given, get an allocated line with getline(). */
983 if (next_cmdline == NULL)
984 {
985 /*
986 * Need to set msg_didout for the first line after an ":if",
987 * otherwise the ":if" will be overwritten.
988 */
989 if (count == 1 && getline_equal(getline, cookie, getexline))
990 msg_didout = TRUE;
991 if (getline == NULL || (next_cmdline = getline(':', cookie,
992#ifdef FEAT_EVAL
993 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
994#else
995 0
996#endif
997 )) == NULL)
998 {
999 /* Don't call wait_return for aborted command line. The NULL
1000 * returned for the end of a sourced file or executed function
1001 * doesn't do this. */
1002 if (KeyTyped && !(flags & DOCMD_REPEAT))
1003 need_wait_return = FALSE;
1004 retval = FAIL;
1005 break;
1006 }
1007 used_getline = TRUE;
1008
1009 /*
1010 * Keep the first typed line. Clear it when more lines are typed.
1011 */
1012 if (flags & DOCMD_KEEPLINE)
1013 {
1014 vim_free(repeat_cmdline);
1015 if (count == 0)
1016 repeat_cmdline = vim_strsave(next_cmdline);
1017 else
1018 repeat_cmdline = NULL;
1019 }
1020 }
1021
1022 /* 3. Make a copy of the command so we can mess with it. */
1023 else if (cmdline_copy == NULL)
1024 {
1025 next_cmdline = vim_strsave(next_cmdline);
1026 if (next_cmdline == NULL)
1027 {
1028 EMSG(_(e_outofmem));
1029 retval = FAIL;
1030 break;
1031 }
1032 }
1033 cmdline_copy = next_cmdline;
1034
1035#ifdef FEAT_EVAL
1036 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 * Save the current line when inside a ":while" or ":for", and when
1038 * the command looks like a ":while" or ":for", because we may need it
1039 * later. When there is a '|' and another command, it is stored
1040 * separately, because we need to be able to jump back to it from an
1041 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (current_line == lines_ga.ga_len
1044 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 {
1048 retval = FAIL;
1049 break;
1050 }
1051 }
1052 did_endif = FALSE;
1053#endif
1054
1055 if (count++ == 0)
1056 {
1057 /*
1058 * All output from the commands is put below each other, without
1059 * waiting for a return. Don't do this when executing commands
1060 * from a script or when being called recursive (e.g. for ":e
1061 * +command file").
1062 */
1063 if (!(flags & DOCMD_NOWAIT) && !recursive)
1064 {
1065 msg_didout_before_start = msg_didout;
1066 msg_didany = FALSE; /* no output yet */
1067 msg_start();
1068 msg_scroll = TRUE; /* put messages below each other */
1069 ++no_wait_return; /* dont wait for return until finished */
1070 ++RedrawingDisabled;
1071 did_inc = TRUE;
1072 }
1073 }
1074
1075 if (p_verbose >= 15 && sourcing_name != NULL)
1076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001078 verbose_enter_scroll();
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 smsg((char_u *)_("line %ld: %s"),
1081 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001082 if (msg_silent == 0)
1083 msg_puts((char_u *)"\n"); /* don't overwrite this */
1084
1085 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 --no_wait_return;
1087 }
1088
1089 /*
1090 * 2. Execute one '|' separated command.
1091 * do_one_cmd() will return NULL if there is no trailing '|'.
1092 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1093 */
1094 ++recursive;
1095 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1096#ifdef FEAT_EVAL
1097 &cstack,
1098#endif
1099 cmd_getline, cmd_cookie);
1100 --recursive;
1101
1102#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 if (cmd_cookie == (void *)&cmd_loop_cookie)
1104 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107#endif
1108
1109 if (next_cmdline == NULL)
1110 {
1111 vim_free(cmdline_copy);
1112 cmdline_copy = NULL;
1113#ifdef FEAT_CMDHIST
1114 /*
1115 * If the command was typed, remember it for the ':' register.
1116 * Do this AFTER executing the command to make :@: work.
1117 */
1118 if (getline_equal(getline, cookie, getexline)
1119 && new_last_cmdline != NULL)
1120 {
1121 vim_free(last_cmdline);
1122 last_cmdline = new_last_cmdline;
1123 new_last_cmdline = NULL;
1124 }
1125#endif
1126 }
1127 else
1128 {
1129 /* need to copy the command after the '|' to cmdline_copy, for the
1130 * next do_one_cmd() */
1131 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1132 next_cmdline = cmdline_copy;
1133 }
1134
1135
1136#ifdef FEAT_EVAL
1137 /* reset did_emsg for a function that is not aborted by an error */
1138 if (did_emsg && !force_abort
1139 && getline_equal(getline, cookie, get_func_line)
1140 && !func_has_abort(real_cookie))
1141 did_emsg = FALSE;
1142
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 ++current_line;
1146
1147 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 * An ":endwhile", ":endfor" and ":continue" is handled here.
1149 * If we were executing commands, jump back to the ":while" or
1150 * ":for".
1151 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 /* Jump back to the matching ":while" or ":for". Be careful
1158 * not to use a cs_line[] from an entry that isn't a ":while"
1159 * or ":for": It would make "current_line" invalid and can
1160 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (!did_emsg && !got_int && !did_throw
1162 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 && (cstack.cs_flags[cstack.cs_idx]
1164 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 && cstack.cs_line[cstack.cs_idx] >= 0
1166 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1167 {
1168 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 /* remember we jumped there */
1170 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 line_breakcheck(); /* check if CTRL-C typed */
1172
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 /* Check for the next breakpoint at or after the ":while"
1174 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (breakpoint != NULL)
1176 {
1177 *breakpoint = dbg_find_breakpoint(
1178 getline_equal(getline, cookie, getsourceline),
1179 fname,
1180 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1181 *dbg_tick = debug_tick;
1182 }
1183 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001188 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1189 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 }
1192
1193 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1200 }
1201 }
1202
1203 /*
1204 * When not inside any ":while" loop, clear remembered lines.
1205 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
1208 if (lines_ga.ga_len > 0)
1209 {
1210 sourcing_lnum =
1211 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1212 free_cmdlines(&lines_ga);
1213 }
1214 current_line = 0;
1215 }
1216
1217 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1219 * being restored at the ":endtry". Reset them here and set the
1220 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1221 * This includes the case where a missing ":endif", ":endwhile" or
1222 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 cstack.cs_lflags &= ~CSL_HAD_FINA;
1227 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1228 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 did_throw ? (void *)current_exception : NULL);
1230 did_emsg = got_int = did_throw = FALSE;
1231 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1232 }
1233
1234 /* Update global "trylevel" for recursive calls to do_cmdline() from
1235 * within this loop. */
1236 trylevel = initial_trylevel + cstack.cs_trylevel;
1237
1238 /*
1239 * If the outermost try conditional (accross function calls and sourced
1240 * files) is aborted because of an error, an interrupt, or an uncaught
1241 * exception, cancel everything. If it is left normally, reset
1242 * force_abort to get the non-EH compatible abortion behavior for
1243 * the rest of the script.
1244 */
1245 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1246 force_abort = FALSE;
1247
1248 /* Convert an interrupt to an exception if appropriate. */
1249 (void)do_intthrow(&cstack);
1250#endif /* FEAT_EVAL */
1251
1252 }
1253 /*
1254 * Continue executing command lines when:
1255 * - no CTRL-C typed, no aborting error, no exception thrown or try
1256 * conditionals need to be checked for executing finally clauses or
1257 * catching an interrupt exception
1258 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001259 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 * looping for ":source" command or function call.
1261 */
1262 while (!((got_int
1263#ifdef FEAT_EVAL
1264 || (did_emsg && force_abort) || did_throw
1265#endif
1266 )
1267#ifdef FEAT_EVAL
1268 && cstack.cs_trylevel == 0
1269#endif
1270 )
1271 && !(did_emsg && used_getline
1272 && (getline_equal(getline, cookie, getexmodeline)
1273 || getline_equal(getline, cookie, getexline)))
1274 && (next_cmdline != NULL
1275#ifdef FEAT_EVAL
1276 || cstack.cs_idx >= 0
1277#endif
1278 || (flags & DOCMD_REPEAT)));
1279
1280 vim_free(cmdline_copy);
1281#ifdef FEAT_EVAL
1282 free_cmdlines(&lines_ga);
1283 ga_clear(&lines_ga);
1284
1285 if (cstack.cs_idx >= 0)
1286 {
1287 /*
1288 * If a sourced file or executed function ran to its end, report the
1289 * unclosed conditional.
1290 */
1291 if (!got_int && !did_throw
1292 && ((getline_equal(getline, cookie, getsourceline)
1293 && !source_finished(getline, cookie))
1294 || (getline_equal(getline, cookie, get_func_line)
1295 && !func_has_ended(real_cookie))))
1296 {
1297 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1298 EMSG(_(e_endtry));
1299 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1300 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001301 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1302 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 else
1304 EMSG(_(e_endif));
1305 }
1306
1307 /*
1308 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1309 * ":endtry" in a sourced file or executed function. If the try
1310 * conditional is in its finally clause, ignore anything pending.
1311 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001312 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 */
1314 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001315 {
1316 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1317
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001318 if (idx >= 0)
1319 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001320 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1321 &cstack.cs_looplevel);
1322 }
1323 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 trylevel = initial_trylevel;
1325 }
1326
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001327 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1328 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * exception, do this now after rewinding the cstack. */
1330 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1331 ? (char_u *)"endfunction" : (char_u *)NULL);
1332
1333 if (trylevel == 0)
1334 {
1335 /*
1336 * When an exception is being thrown out of the outermost try
1337 * conditional, discard the uncaught exception, disable the conversion
1338 * of interrupts or errors to exceptions, and ensure that no more
1339 * commands are executed.
1340 */
1341 if (did_throw)
1342 {
1343 void *p = NULL;
1344 char_u *saved_sourcing_name;
1345 int saved_sourcing_lnum;
1346 struct msglist *messages = NULL, *next;
1347
1348 /*
1349 * If the uncaught exception is a user exception, report it as an
1350 * error. If it is an error exception, display the saved error
1351 * message now. For an interrupt exception, do nothing; the
1352 * interrupt message is given elsewhere.
1353 */
1354 switch (current_exception->type)
1355 {
1356 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001357 vim_snprintf((char *)IObuff, IOSIZE,
1358 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 current_exception->value);
1360 p = vim_strsave(IObuff);
1361 break;
1362 case ET_ERROR:
1363 messages = current_exception->messages;
1364 current_exception->messages = NULL;
1365 break;
1366 case ET_INTERRUPT:
1367 break;
1368 default:
1369 p = vim_strsave((char_u *)_(e_internal));
1370 }
1371
1372 saved_sourcing_name = sourcing_name;
1373 saved_sourcing_lnum = sourcing_lnum;
1374 sourcing_name = current_exception->throw_name;
1375 sourcing_lnum = current_exception->throw_lnum;
1376 current_exception->throw_name = NULL;
1377
1378 discard_current_exception(); /* uses IObuff if 'verbose' */
1379 suppress_errthrow = TRUE;
1380 force_abort = TRUE;
1381
1382 if (messages != NULL)
1383 {
1384 do
1385 {
1386 next = messages->next;
1387 emsg(messages->msg);
1388 vim_free(messages->msg);
1389 vim_free(messages);
1390 messages = next;
1391 }
1392 while (messages != NULL);
1393 }
1394 else if (p != NULL)
1395 {
1396 emsg(p);
1397 vim_free(p);
1398 }
1399 vim_free(sourcing_name);
1400 sourcing_name = saved_sourcing_name;
1401 sourcing_lnum = saved_sourcing_lnum;
1402 }
1403
1404 /*
1405 * On an interrupt or an aborting error not converted to an exception,
1406 * disable the conversion of errors to exceptions. (Interrupts are not
1407 * converted any more, here.) This enables also the interrupt message
1408 * when force_abort is set and did_emsg unset in case of an interrupt
1409 * from a finally clause after an error.
1410 */
1411 else if (got_int || (did_emsg && force_abort))
1412 suppress_errthrow = TRUE;
1413 }
1414
1415 /*
1416 * The current cstack will be freed when do_cmdline() returns. An uncaught
1417 * exception will have to be rethrown in the previous cstack. If a function
1418 * has just returned or a script file was just finished and the previous
1419 * cstack belongs to the same function or, respectively, script file, it
1420 * will have to be checked for finally clauses to be executed due to the
1421 * ":return" or ":finish". This is done in do_one_cmd().
1422 */
1423 if (did_throw)
1424 need_rethrow = TRUE;
1425 if ((getline_equal(getline, cookie, getsourceline)
1426 && ex_nesting_level > source_level(real_cookie))
1427 || (getline_equal(getline, cookie, get_func_line)
1428 && ex_nesting_level > func_level(real_cookie) + 1))
1429 {
1430 if (!did_throw)
1431 check_cstack = TRUE;
1432 }
1433 else
1434 {
1435 /* When leaving a function, reduce nesting level. */
1436 if (getline_equal(getline, cookie, get_func_line))
1437 --ex_nesting_level;
1438 /*
1439 * Go to debug mode when returning from a function in which we are
1440 * single-stepping.
1441 */
1442 if ((getline_equal(getline, cookie, getsourceline)
1443 || getline_equal(getline, cookie, get_func_line))
1444 && ex_nesting_level + 1 <= debug_break_level)
1445 do_debug(getline_equal(getline, cookie, getsourceline)
1446 ? (char_u *)_("End of sourced file")
1447 : (char_u *)_("End of function"));
1448 }
1449
1450 /*
1451 * Restore the exception environment (done after returning from the
1452 * debugger).
1453 */
1454 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001455 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 msg_list = saved_msg_list;
1458#endif /* FEAT_EVAL */
1459
1460 /*
1461 * If there was too much output to fit on the command line, ask the user to
1462 * hit return before redrawing the screen. With the ":global" command we do
1463 * this only once after the command is finished.
1464 */
1465 if (did_inc)
1466 {
1467 --RedrawingDisabled;
1468 --no_wait_return;
1469 msg_scroll = FALSE;
1470
1471 /*
1472 * When just finished an ":if"-":else" which was typed, no need to
1473 * wait for hit-return. Also for an error situation.
1474 */
1475 if (retval == FAIL
1476#ifdef FEAT_EVAL
1477 || (did_endif && KeyTyped && !did_emsg)
1478#endif
1479 )
1480 {
1481 need_wait_return = FALSE;
1482 msg_didany = FALSE; /* don't wait when restarting edit */
1483 }
1484 else if (need_wait_return)
1485 {
1486 /*
1487 * The msg_start() above clears msg_didout. The wait_return we do
1488 * here should not overwrite the command that may be shown before
1489 * doing that.
1490 */
1491 msg_didout |= msg_didout_before_start;
1492 wait_return(FALSE);
1493 }
1494 }
1495
1496#ifndef FEAT_EVAL
1497 /*
1498 * Reset if_level, in case a sourced script file contains more ":if" than
1499 * ":endif" (could be ":if x | foo | endif").
1500 */
1501 if_level = 0;
1502#endif
1503
1504 --call_depth;
1505 return retval;
1506}
1507
1508#ifdef FEAT_EVAL
1509/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 */
1512 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001513get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 int c;
1515 void *cookie;
1516 int indent;
1517{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001518 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 wcmd_T *wp;
1520 char_u *line;
1521
1522 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1523 {
1524 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (cp->getline == NULL)
1529 line = getcmdline(c, 0L, indent);
1530 else
1531 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 ++cp->current_line;
1534
1535 return line;
1536 }
1537
1538 KeyTyped = FALSE;
1539 ++cp->current_line;
1540 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1541 sourcing_lnum = wp->lnum;
1542 return vim_strsave(wp->line);
1543}
1544
1545/*
1546 * Store a line in "gap" so that a ":while" loop can execute it again.
1547 */
1548 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001549store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 garray_T *gap;
1551 char_u *line;
1552{
1553 if (ga_grow(gap, 1) == FAIL)
1554 return FAIL;
1555 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1556 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1557 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 return OK;
1559}
1560
1561/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 */
1564 static void
1565free_cmdlines(gap)
1566 garray_T *gap;
1567{
1568 while (gap->ga_len > 0)
1569 {
1570 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1571 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
1573}
1574#endif
1575
1576/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 * "func". * Otherwise return TRUE when "getline" equals "func".
1579 */
1580/*ARGSUSED*/
1581 int
1582getline_equal(getline, cookie, func)
1583 char_u *(*getline) __ARGS((int, void *, int));
1584 void *cookie; /* argument for getline() */
1585 char_u *(*func) __ARGS((int, void *, int));
1586{
1587#ifdef FEAT_EVAL
1588 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001591 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 * function that's orignally used to obtain the lines. This may be nested
1593 * several levels. */
1594 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001595 cp = (struct loop_cookie *)cookie;
1596 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
1598 gp = cp->getline;
1599 cp = cp->cookie;
1600 }
1601 return gp == func;
1602#else
1603 return getline == func;
1604#endif
1605}
1606
1607#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1608/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001609 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 * getline function. Otherwise return "cookie".
1611 */
1612/*ARGSUSED*/
1613 void *
1614getline_cookie(getline, cookie)
1615 char_u *(*getline) __ARGS((int, void *, int));
1616 void *cookie; /* argument for getline() */
1617{
1618# ifdef FEAT_EVAL
1619 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001620 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001622 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 * cookie that's orignally used to obtain the lines. This may be nested
1624 * several levels. */
1625 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001626 cp = (struct loop_cookie *)cookie;
1627 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629 gp = cp->getline;
1630 cp = cp->cookie;
1631 }
1632 return cp;
1633# else
1634 return cookie;
1635# endif
1636}
1637#endif
1638
1639/*
1640 * Execute one Ex command.
1641 *
1642 * If 'sourcing' is TRUE, the command will be included in the error message.
1643 *
1644 * 1. skip comment lines and leading space
1645 * 2. handle command modifiers
1646 * 3. parse range
1647 * 4. parse command
1648 * 5. parse arguments
1649 * 6. switch on command name
1650 *
1651 * Note: "getline" can be NULL.
1652 *
1653 * This function may be called recursively!
1654 */
1655#if (_MSC_VER == 1200)
1656/*
Bram Moolenaared203462004-06-16 11:19:22 +00001657 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001659 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660#endif
1661 static char_u *
1662do_one_cmd(cmdlinep, sourcing,
1663#ifdef FEAT_EVAL
1664 cstack,
1665#endif
1666 getline, cookie)
1667 char_u **cmdlinep;
1668 int sourcing;
1669#ifdef FEAT_EVAL
1670 struct condstack *cstack;
1671#endif
1672 char_u *(*getline) __ARGS((int, void *, int));
1673 void *cookie; /* argument for getline() */
1674{
1675 char_u *p;
1676 linenr_T lnum;
1677 long n;
1678 char_u *errormsg = NULL; /* error message */
1679 exarg_T ea; /* Ex command arguments */
1680 long verbose_save = -1;
1681 int save_msg_scroll = 0;
1682 int did_silent = 0;
1683 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001684#ifdef HAVE_SANDBOX
1685 int did_sandbox = FALSE;
1686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 cmdmod_T save_cmdmod;
1688 int ni; /* set when Not Implemented */
1689
1690 vim_memset(&ea, 0, sizeof(ea));
1691 ea.line1 = 1;
1692 ea.line2 = 1;
1693#ifdef FEAT_EVAL
1694 ++ex_nesting_level;
1695#endif
1696
1697 /* when not editing the last file :q has to be typed twice */
1698 if (quitmore
1699#ifdef FEAT_EVAL
1700 /* avoid that a function call in 'statusline' does this */
1701 && !getline_equal(getline, cookie, get_func_line)
1702#endif
1703 )
1704 --quitmore;
1705
1706 /*
1707 * Reset browse, confirm, etc.. They are restored when returning, for
1708 * recursive calls.
1709 */
1710 save_cmdmod = cmdmod;
1711 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1712
1713 /*
1714 * Repeat until no more command modifiers are found.
1715 */
1716 ea.cmd = *cmdlinep;
1717 for (;;)
1718 {
1719/*
1720 * 1. skip comment lines and leading white space and colons
1721 */
1722 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1723 ++ea.cmd;
1724
1725 /* in ex mode, an empty line works like :+ */
1726 if (*ea.cmd == NUL && exmode_active
1727 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001728 || getline_equal(getline, cookie, getexline))
1729 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
1731 ea.cmd = (char_u *)"+";
1732 ex_pressedreturn = TRUE;
1733 }
1734
1735 /* ignore comment and empty lines */
1736 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001737 {
1738 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742/*
1743 * 2. handle command modifiers.
1744 */
1745 p = ea.cmd;
1746 if (VIM_ISDIGIT(*ea.cmd))
1747 p = skipwhite(skipdigits(ea.cmd));
1748 switch (*p)
1749 {
1750 /* When adding an entry, also modify cmd_exists(). */
1751 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1752 break;
1753#ifdef FEAT_WINDOWS
1754 cmdmod.split |= WSP_ABOVE;
1755#endif
1756 continue;
1757
1758 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1759 {
1760#ifdef FEAT_WINDOWS
1761 cmdmod.split |= WSP_BELOW;
1762#endif
1763 continue;
1764 }
1765 if (checkforcmd(&ea.cmd, "browse", 3))
1766 {
1767#ifdef FEAT_BROWSE
1768 cmdmod.browse = TRUE;
1769#endif
1770 continue;
1771 }
1772 if (!checkforcmd(&ea.cmd, "botright", 2))
1773 break;
1774#ifdef FEAT_WINDOWS
1775 cmdmod.split |= WSP_BOT;
1776#endif
1777 continue;
1778
1779 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1780 break;
1781#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1782 cmdmod.confirm = TRUE;
1783#endif
1784 continue;
1785
1786 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1787 {
1788 cmdmod.keepmarks = TRUE;
1789 continue;
1790 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001791 if (checkforcmd(&ea.cmd, "keepalt", 5))
1792 {
1793 cmdmod.keepalt = TRUE;
1794 continue;
1795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1797 break;
1798 cmdmod.keepjumps = TRUE;
1799 continue;
1800
1801 /* ":hide" and ":hide | cmd" are not modifiers */
1802 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1803 || *p == NUL || ends_excmd(*p))
1804 break;
1805 ea.cmd = p;
1806 cmdmod.hide = TRUE;
1807 continue;
1808
1809 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1810 {
1811 cmdmod.lockmarks = TRUE;
1812 continue;
1813 }
1814
1815 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1816 break;
1817#ifdef FEAT_WINDOWS
1818 cmdmod.split |= WSP_ABOVE;
1819#endif
1820 continue;
1821
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001822 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1823 break;
1824#ifdef FEAT_AUTOCMD
1825 if (cmdmod.save_ei == NULL)
1826 {
1827 /* Set 'eventignore' to "all". Don't free the
1828 * existing option value, we restore it later. */
1829 cmdmod.save_ei = vim_strsave(p_ei);
1830 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001831 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001832 }
1833#endif
1834 continue;
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1837 break;
1838#ifdef FEAT_WINDOWS
1839 cmdmod.split |= WSP_BELOW;
1840#endif
1841 continue;
1842
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001843 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1844 {
1845#ifdef HAVE_SANDBOX
1846 if (!did_sandbox)
1847 ++sandbox;
1848 did_sandbox = TRUE;
1849#endif
1850 continue;
1851 }
1852 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 break;
1854 ++did_silent;
1855 ++msg_silent;
1856 save_msg_scroll = msg_scroll;
1857 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1858 {
1859 /* ":silent!", but not "silent !cmd" */
1860 ea.cmd = skipwhite(ea.cmd + 1);
1861 ++emsg_silent;
1862 ++did_esilent;
1863 }
1864 continue;
1865
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001866 case 't': if (checkforcmd(&p, "tab", 3))
1867 {
1868#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001869 if (vim_isdigit(*ea.cmd))
1870 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1871 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001872 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001873 ea.cmd = p;
1874#endif
1875 continue;
1876 }
1877 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 break;
1879#ifdef FEAT_WINDOWS
1880 cmdmod.split |= WSP_TOP;
1881#endif
1882 continue;
1883
1884 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1885 {
1886#ifdef FEAT_VERTSPLIT
1887 cmdmod.split |= WSP_VERT;
1888#endif
1889 continue;
1890 }
1891 if (!checkforcmd(&p, "verbose", 4))
1892 break;
1893 if (verbose_save < 0)
1894 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001895 if (vim_isdigit(*ea.cmd))
1896 p_verbose = atoi((char *)ea.cmd);
1897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 p_verbose = 1;
1899 ea.cmd = p;
1900 continue;
1901 }
1902 break;
1903 }
1904
1905#ifdef FEAT_EVAL
1906 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1907 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1908#else
1909 ea.skip = (if_level > 0);
1910#endif
1911
1912#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001913# ifdef FEAT_PROFILE
1914 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001915 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001916 {
1917 if (getline_equal(getline, cookie, get_func_line))
1918 func_line_exec(getline_cookie(getline, cookie));
1919 else if (getline_equal(getline, cookie, getsourceline))
1920 script_line_exec();
1921 }
1922#endif
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 /* May go to debug mode. If this happens and the ">quit" debug command is
1925 * used, throw an interrupt exception and skip the next command. */
1926 dbg_check_breakpoint(&ea);
1927 if (!ea.skip && got_int)
1928 {
1929 ea.skip = TRUE;
1930 (void)do_intthrow(cstack);
1931 }
1932#endif
1933
1934/*
1935 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1936 *
1937 * where 'addr' is:
1938 *
1939 * % (entire file)
1940 * $ [+-NUM]
1941 * 'x [+-NUM] (where x denotes a currently defined mark)
1942 * . [+-NUM]
1943 * [+-NUM]..
1944 * NUM
1945 *
1946 * The ea.cmd pointer is updated to point to the first character following the
1947 * range spec. If an initial address is found, but no second, the upper bound
1948 * is equal to the lower.
1949 */
1950
1951 /* repeat for all ',' or ';' separated addresses */
1952 for (;;)
1953 {
1954 ea.line1 = ea.line2;
1955 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1956 ea.cmd = skipwhite(ea.cmd);
1957 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1958 if (ea.cmd == NULL) /* error detected */
1959 goto doend;
1960 if (lnum == MAXLNUM)
1961 {
1962 if (*ea.cmd == '%') /* '%' - all lines */
1963 {
1964 ++ea.cmd;
1965 ea.line1 = 1;
1966 ea.line2 = curbuf->b_ml.ml_line_count;
1967 ++ea.addr_count;
1968 }
1969 /* '*' - visual area */
1970 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1971 {
1972 pos_T *fp;
1973
1974 ++ea.cmd;
1975 if (!ea.skip)
1976 {
1977 fp = getmark('<', FALSE);
1978 if (check_mark(fp) == FAIL)
1979 goto doend;
1980 ea.line1 = fp->lnum;
1981 fp = getmark('>', FALSE);
1982 if (check_mark(fp) == FAIL)
1983 goto doend;
1984 ea.line2 = fp->lnum;
1985 ++ea.addr_count;
1986 }
1987 }
1988 }
1989 else
1990 ea.line2 = lnum;
1991 ea.addr_count++;
1992
1993 if (*ea.cmd == ';')
1994 {
1995 if (!ea.skip)
1996 curwin->w_cursor.lnum = ea.line2;
1997 }
1998 else if (*ea.cmd != ',')
1999 break;
2000 ++ea.cmd;
2001 }
2002
2003 /* One address given: set start and end lines */
2004 if (ea.addr_count == 1)
2005 {
2006 ea.line1 = ea.line2;
2007 /* ... but only implicit: really no address given */
2008 if (lnum == MAXLNUM)
2009 ea.addr_count = 0;
2010 }
2011
2012 /* Don't leave the cursor on an illegal line (caused by ';') */
2013 check_cursor_lnum();
2014
2015/*
2016 * 4. parse command
2017 */
2018
2019 /*
2020 * Skip ':' and any white space
2021 */
2022 ea.cmd = skipwhite(ea.cmd);
2023 while (*ea.cmd == ':')
2024 ea.cmd = skipwhite(ea.cmd + 1);
2025
2026 /*
2027 * If we got a line, but no command, then go to the line.
2028 * If we find a '|' or '\n' we set ea.nextcmd.
2029 */
2030 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2031 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2032 {
2033 /*
2034 * strange vi behaviour:
2035 * ":3" jumps to line 3
2036 * ":3|..." prints line 3
2037 * ":|" prints current line
2038 */
2039 if (ea.skip) /* skip this if inside :if */
2040 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002041 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
2043 ea.cmdidx = CMD_print;
2044 ea.argt = RANGE+COUNT+TRLBAR;
2045 if ((errormsg = invalid_range(&ea)) == NULL)
2046 {
2047 correct_range(&ea);
2048 ex_print(&ea);
2049 }
2050 }
2051 else if (ea.addr_count != 0)
2052 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002053 if (ea.line2 > curbuf->b_ml.ml_line_count)
2054 {
2055 /* With '-' in 'cpoptions' a line number past the file is an
2056 * error, otherwise put it at the end of the file. */
2057 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2058 ea.line2 = -1;
2059 else
2060 ea.line2 = curbuf->b_ml.ml_line_count;
2061 }
2062
2063 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002064 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 else
2066 {
2067 if (ea.line2 == 0)
2068 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
2070 curwin->w_cursor.lnum = ea.line2;
2071 beginline(BL_SOL | BL_FIX);
2072 }
2073 }
2074 goto doend;
2075 }
2076
2077 /* Find the command and let "p" point to after it. */
2078 p = find_command(&ea, NULL);
2079
2080#ifdef FEAT_USR_CMDS
2081 if (p == NULL)
2082 {
2083 if (!ea.skip)
2084 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2085 goto doend;
2086 }
2087 /* Check for wrong commands. */
2088 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2089 {
2090 errormsg = uc_fun_cmd();
2091 goto doend;
2092 }
2093#endif
2094 if (ea.cmdidx == CMD_SIZE)
2095 {
2096 if (!ea.skip)
2097 {
2098 STRCPY(IObuff, _("E492: Not an editor command"));
2099 if (!sourcing)
2100 {
2101 STRCAT(IObuff, ": ");
2102 STRNCAT(IObuff, *cmdlinep, 40);
2103 }
2104 errormsg = IObuff;
2105 }
2106 goto doend;
2107 }
2108
2109 ni = (
2110#ifdef FEAT_USR_CMDS
2111 !USER_CMDIDX(ea.cmdidx) &&
2112#endif
2113 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2114
2115#ifndef FEAT_EVAL
2116 /*
2117 * When the expression evaluation is disabled, recognize the ":if" and
2118 * ":endif" commands and ignore everything in between it.
2119 */
2120 if (ea.cmdidx == CMD_if)
2121 ++if_level;
2122 if (if_level)
2123 {
2124 if (ea.cmdidx == CMD_endif)
2125 --if_level;
2126 goto doend;
2127 }
2128
2129#endif
2130
2131 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2132 {
2133 ++p;
2134 ea.forceit = TRUE;
2135 }
2136 else
2137 ea.forceit = FALSE;
2138
2139/*
2140 * 5. parse arguments
2141 */
2142#ifdef FEAT_USR_CMDS
2143 if (!USER_CMDIDX(ea.cmdidx))
2144#endif
2145 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2146
2147 if (!ea.skip)
2148 {
2149#ifdef HAVE_SANDBOX
2150 if (sandbox != 0 && !(ea.argt & SBOXOK))
2151 {
2152 /* Command not allowed in sandbox. */
2153 errormsg = (char_u *)_(e_sandbox);
2154 goto doend;
2155 }
2156#endif
2157 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2158 {
2159 /* Command not allowed in non-'modifiable' buffer */
2160 errormsg = (char_u *)_(e_modifiable);
2161 goto doend;
2162 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002163
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002164 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165# ifdef FEAT_USR_CMDS
2166 && !USER_CMDIDX(ea.cmdidx)
2167# endif
2168 )
2169 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002170 /* Command not allowed when editing the command line. */
2171#ifdef FEAT_CMDWIN
2172 if (cmdwin_type != 0)
2173 errormsg = (char_u *)_(e_cmdwin);
2174 else
2175#endif
2176 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 goto doend;
2178 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179#ifdef FEAT_AUTOCMD
2180 if (!(ea.argt & CMDWIN)
2181# ifdef FEAT_USR_CMDS
2182 && !USER_CMDIDX(ea.cmdidx)
2183# endif
2184 && curbuf_locked())
2185 goto doend;
2186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2189 {
2190 /* no range allowed */
2191 errormsg = (char_u *)_(e_norange);
2192 goto doend;
2193 }
2194 }
2195
2196 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2197 {
2198 errormsg = (char_u *)_(e_nobang);
2199 goto doend;
2200 }
2201
2202 /*
2203 * Don't complain about the range if it is not used
2204 * (could happen if line_count is accidentally set to 0).
2205 */
2206 if (!ea.skip && !ni)
2207 {
2208 /*
2209 * If the range is backwards, ask for confirmation and, if given, swap
2210 * ea.line1 & ea.line2 so it's forwards again.
2211 * When global command is busy, don't ask, will fail below.
2212 */
2213 if (!global_busy && ea.line1 > ea.line2)
2214 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002215 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002217 if (sourcing || exmode_active)
2218 {
2219 errormsg = (char_u *)_("E493: Backwards range given");
2220 goto doend;
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 if (ask_yesno((char_u *)
2223 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002224 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 }
2226 lnum = ea.line1;
2227 ea.line1 = ea.line2;
2228 ea.line2 = lnum;
2229 }
2230 if ((errormsg = invalid_range(&ea)) != NULL)
2231 goto doend;
2232 }
2233
2234 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2235 ea.line2 = 1;
2236
2237 correct_range(&ea);
2238
2239#ifdef FEAT_FOLDING
2240 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2241 {
2242 /* Put the first line at the start of a closed fold, put the last line
2243 * at the end of a closed fold. */
2244 (void)hasFolding(ea.line1, &ea.line1, NULL);
2245 (void)hasFolding(ea.line2, NULL, &ea.line2);
2246 }
2247#endif
2248
2249#ifdef FEAT_QUICKFIX
2250 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002251 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 * option here, so things like % get expanded.
2253 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002254 p = replace_makeprg(&ea, p, cmdlinep);
2255 if (p == NULL)
2256 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257#endif
2258
2259 /*
2260 * Skip to start of argument.
2261 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2262 */
2263 if (ea.cmdidx == CMD_bang)
2264 ea.arg = p;
2265 else
2266 ea.arg = skipwhite(p);
2267
2268 /*
2269 * Check for "++opt=val" argument.
2270 * Must be first, allow ":w ++enc=utf8 !cmd"
2271 */
2272 if (ea.argt & ARGOPT)
2273 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2274 if (getargopt(&ea) == FAIL && !ni)
2275 {
2276 errormsg = (char_u *)_(e_invarg);
2277 goto doend;
2278 }
2279
2280 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2281 {
2282 if (*ea.arg == '>') /* append */
2283 {
2284 if (*++ea.arg != '>') /* typed wrong */
2285 {
2286 errormsg = (char_u *)_("E494: Use w or w>>");
2287 goto doend;
2288 }
2289 ea.arg = skipwhite(ea.arg + 1);
2290 ea.append = TRUE;
2291 }
2292 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2293 {
2294 ++ea.arg;
2295 ea.usefilter = TRUE;
2296 }
2297 }
2298
2299 if (ea.cmdidx == CMD_read)
2300 {
2301 if (ea.forceit)
2302 {
2303 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2304 ea.forceit = FALSE;
2305 }
2306 else if (*ea.arg == '!') /* :r !filter */
2307 {
2308 ++ea.arg;
2309 ea.usefilter = TRUE;
2310 }
2311 }
2312
2313 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2314 {
2315 ea.amount = 1;
2316 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2317 {
2318 ++ea.arg;
2319 ++ea.amount;
2320 }
2321 ea.arg = skipwhite(ea.arg);
2322 }
2323
2324 /*
2325 * Check for "+command" argument, before checking for next command.
2326 * Don't do this for ":read !cmd" and ":write !cmd".
2327 */
2328 if ((ea.argt & EDITCMD) && !ea.usefilter)
2329 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2330
2331 /*
2332 * Check for '|' to separate commands and '"' to start comments.
2333 * Don't do this for ":read !cmd" and ":write !cmd".
2334 */
2335 if ((ea.argt & TRLBAR) && !ea.usefilter)
2336 separate_nextcmd(&ea);
2337
2338 /*
2339 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002340 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2341 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002343 else if (ea.cmdidx == CMD_bang
2344 || ea.cmdidx == CMD_global
2345 || ea.cmdidx == CMD_vglobal
2346 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 for (p = ea.arg; *p; ++p)
2349 {
2350 /* Remove one backslash before a newline, so that it's possible to
2351 * pass a newline to the shell and also a newline that is preceded
2352 * with a backslash. This makes it impossible to end a shell
2353 * command in a backslash, but that doesn't appear useful.
2354 * Halving the number of backslashes is incompatible with previous
2355 * versions. */
2356 if (*p == '\\' && p[1] == '\n')
2357 mch_memmove(p, p + 1, STRLEN(p));
2358 else if (*p == '\n')
2359 {
2360 ea.nextcmd = p + 1;
2361 *p = NUL;
2362 break;
2363 }
2364 }
2365 }
2366
2367 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2368 {
2369 ea.line1 = 1;
2370 ea.line2 = curbuf->b_ml.ml_line_count;
2371 }
2372
2373 /* accept numbered register only when no count allowed (:put) */
2374 if ( (ea.argt & REGSTR)
2375 && *ea.arg != NUL
2376#ifdef FEAT_USR_CMDS
2377 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2378 && USER_CMDIDX(ea.cmdidx)))
2379 /* Do not allow register = for user commands */
2380 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2381#else
2382 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2383#endif
2384 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2385 {
2386 ea.regname = *ea.arg++;
2387#ifdef FEAT_EVAL
2388 /* for '=' register: accept the rest of the line as an expression */
2389 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2390 {
2391 set_expr_line(vim_strsave(ea.arg));
2392 ea.arg += STRLEN(ea.arg);
2393 }
2394#endif
2395 ea.arg = skipwhite(ea.arg);
2396 }
2397
2398 /*
2399 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2400 * count, it's a buffer name.
2401 */
2402 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2403 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2404 || vim_iswhite(*p)))
2405 {
2406 n = getdigits(&ea.arg);
2407 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002408 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 errormsg = (char_u *)_(e_zerocount);
2411 goto doend;
2412 }
2413 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2414 {
2415 ea.line2 = n;
2416 if (ea.addr_count == 0)
2417 ea.addr_count = 1;
2418 }
2419 else
2420 {
2421 ea.line1 = ea.line2;
2422 ea.line2 += n - 1;
2423 ++ea.addr_count;
2424 /*
2425 * Be vi compatible: no error message for out of range.
2426 */
2427 if (ea.line2 > curbuf->b_ml.ml_line_count)
2428 ea.line2 = curbuf->b_ml.ml_line_count;
2429 }
2430 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002431
2432 /*
2433 * Check for flags: 'l', 'p' and '#'.
2434 */
2435 if (ea.argt & EXFLAGS)
2436 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002438 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002439 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
2441 errormsg = (char_u *)_(e_trailing);
2442 goto doend;
2443 }
2444
2445 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2446 {
2447 errormsg = (char_u *)_(e_argreq);
2448 goto doend;
2449 }
2450
2451#ifdef FEAT_EVAL
2452 /*
2453 * Skip the command when it's not going to be executed.
2454 * The commands like :if, :endif, etc. always need to be executed.
2455 * Also make an exception for commands that handle a trailing command
2456 * themselves.
2457 */
2458 if (ea.skip)
2459 {
2460 switch (ea.cmdidx)
2461 {
2462 /* commands that need evaluation */
2463 case CMD_while:
2464 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002465 case CMD_for:
2466 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 case CMD_if:
2468 case CMD_elseif:
2469 case CMD_else:
2470 case CMD_endif:
2471 case CMD_try:
2472 case CMD_catch:
2473 case CMD_finally:
2474 case CMD_endtry:
2475 case CMD_function:
2476 break;
2477
2478 /* Commands that handle '|' themselves. Check: A command should
2479 * either have the TRLBAR flag, appear in this list or appear in
2480 * the list at ":help :bar". */
2481 case CMD_aboveleft:
2482 case CMD_and:
2483 case CMD_belowright:
2484 case CMD_botright:
2485 case CMD_browse:
2486 case CMD_call:
2487 case CMD_confirm:
2488 case CMD_delfunction:
2489 case CMD_djump:
2490 case CMD_dlist:
2491 case CMD_dsearch:
2492 case CMD_dsplit:
2493 case CMD_echo:
2494 case CMD_echoerr:
2495 case CMD_echomsg:
2496 case CMD_echon:
2497 case CMD_execute:
2498 case CMD_help:
2499 case CMD_hide:
2500 case CMD_ijump:
2501 case CMD_ilist:
2502 case CMD_isearch:
2503 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002504 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 case CMD_keepjumps:
2506 case CMD_keepmarks:
2507 case CMD_leftabove:
2508 case CMD_let:
2509 case CMD_lockmarks:
2510 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002511 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 case CMD_perl:
2513 case CMD_psearch:
2514 case CMD_python:
2515 case CMD_return:
2516 case CMD_rightbelow:
2517 case CMD_ruby:
2518 case CMD_silent:
2519 case CMD_smagic:
2520 case CMD_snomagic:
2521 case CMD_substitute:
2522 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002523 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 case CMD_tcl:
2525 case CMD_throw:
2526 case CMD_tilde:
2527 case CMD_topleft:
2528 case CMD_unlet:
2529 case CMD_verbose:
2530 case CMD_vertical:
2531 break;
2532
2533 default: goto doend;
2534 }
2535 }
2536#endif
2537
2538 if (ea.argt & XFILE)
2539 {
2540 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2541 goto doend;
2542 }
2543
2544#ifdef FEAT_LISTCMDS
2545 /*
2546 * Accept buffer name. Cannot be used at the same time with a buffer
2547 * number. Don't do this for a user command.
2548 */
2549 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2550# ifdef FEAT_USR_CMDS
2551 && !USER_CMDIDX(ea.cmdidx)
2552# endif
2553 )
2554 {
2555 /*
2556 * :bdelete, :bwipeout and :bunload take several arguments, separated
2557 * by spaces: find next space (skipping over escaped characters).
2558 * The others take one argument: ignore trailing spaces.
2559 */
2560 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2561 || ea.cmdidx == CMD_bunload)
2562 p = skiptowhite_esc(ea.arg);
2563 else
2564 {
2565 p = ea.arg + STRLEN(ea.arg);
2566 while (p > ea.arg && vim_iswhite(p[-1]))
2567 --p;
2568 }
2569 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2570 if (ea.line2 < 0) /* failed */
2571 goto doend;
2572 ea.addr_count = 1;
2573 ea.arg = skipwhite(p);
2574 }
2575#endif
2576
2577/*
2578 * 6. switch on command name
2579 *
2580 * The "ea" structure holds the arguments that can be used.
2581 */
2582 ea.cmdlinep = cmdlinep;
2583 ea.getline = getline;
2584 ea.cookie = cookie;
2585#ifdef FEAT_EVAL
2586 ea.cstack = cstack;
2587#endif
2588
2589#ifdef FEAT_USR_CMDS
2590 if (USER_CMDIDX(ea.cmdidx))
2591 {
2592 /*
2593 * Execute a user-defined command.
2594 */
2595 do_ucmd(&ea);
2596 }
2597 else
2598#endif
2599 {
2600 /*
2601 * Call the function to execute the command.
2602 */
2603 ea.errmsg = NULL;
2604 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2605 if (ea.errmsg != NULL)
2606 errormsg = (char_u *)_(ea.errmsg);
2607 }
2608
2609#ifdef FEAT_EVAL
2610 /*
2611 * If the command just executed called do_cmdline(), any throw or ":return"
2612 * or ":finish" encountered there must also check the cstack of the still
2613 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2614 * exception, or reanimate a returned function or finished script file and
2615 * return or finish it again.
2616 */
2617 if (need_rethrow)
2618 do_throw(cstack);
2619 else if (check_cstack)
2620 {
2621 if (source_finished(getline, cookie))
2622 do_finish(&ea, TRUE);
2623 else if (getline_equal(getline, cookie, get_func_line)
2624 && current_func_returned())
2625 do_return(&ea, TRUE, FALSE, NULL);
2626 }
2627 need_rethrow = check_cstack = FALSE;
2628#endif
2629
2630doend:
2631 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2632 curwin->w_cursor.lnum = 1;
2633
2634 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2635 {
2636 if (sourcing)
2637 {
2638 if (errormsg != IObuff)
2639 {
2640 STRCPY(IObuff, errormsg);
2641 errormsg = IObuff;
2642 }
2643 STRCAT(errormsg, ": ");
2644 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2645 }
2646 emsg(errormsg);
2647 }
2648#ifdef FEAT_EVAL
2649 do_errthrow(cstack,
2650 (ea.cmdidx != CMD_SIZE
2651# ifdef FEAT_USR_CMDS
2652 && !USER_CMDIDX(ea.cmdidx)
2653# endif
2654 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2655#endif
2656
2657 if (verbose_save >= 0)
2658 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002659#ifdef FEAT_AUTOCMD
2660 if (cmdmod.save_ei != NULL)
2661 {
2662 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002663 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2664 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002665 free_string_option(cmdmod.save_ei);
2666 }
2667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
2669 cmdmod = save_cmdmod;
2670
2671 if (did_silent > 0)
2672 {
2673 /* messages could be enabled for a serious error, need to check if the
2674 * counters don't become negative */
2675 msg_silent -= did_silent;
2676 if (msg_silent < 0)
2677 msg_silent = 0;
2678 emsg_silent -= did_esilent;
2679 if (emsg_silent < 0)
2680 emsg_silent = 0;
2681 /* Restore msg_scroll, it's set by file I/O commands, even when no
2682 * message is actually displayed. */
2683 msg_scroll = save_msg_scroll;
2684 }
2685
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002686#ifdef HAVE_SANDBOX
2687 if (did_sandbox)
2688 --sandbox;
2689#endif
2690
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2692 ea.nextcmd = NULL;
2693
2694#ifdef FEAT_EVAL
2695 --ex_nesting_level;
2696#endif
2697
2698 return ea.nextcmd;
2699}
2700#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002701 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#endif
2703
2704/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002705 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 * If there is a match advance "pp" to the argument and return TRUE.
2707 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002708 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002710 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 char *cmd; /* name of command */
2712 int len; /* required length */
2713{
2714 int i;
2715
2716 for (i = 0; cmd[i] != NUL; ++i)
2717 if (cmd[i] != (*pp)[i])
2718 break;
2719 if (i >= len && !isalpha((*pp)[i]))
2720 {
2721 *pp = skipwhite(*pp + i);
2722 return TRUE;
2723 }
2724 return FALSE;
2725}
2726
2727/*
2728 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002729 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002731 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 * Returns NULL for an ambiguous user command.
2733 */
2734/*ARGSUSED*/
2735 static char_u *
2736find_command(eap, full)
2737 exarg_T *eap;
2738 int *full;
2739{
2740 int len;
2741 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002742 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 /*
2745 * Isolate the command and search for it in the command table.
2746 * Exeptions:
2747 * - the 'k' command can directly be followed by any character.
2748 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2749 * but :sre[wind] is another command, as are :scrip[tnames],
2750 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002751 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
2753 p = eap->cmd;
2754 if (*p == 'k')
2755 {
2756 eap->cmdidx = CMD_k;
2757 ++p;
2758 }
2759 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002760 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2761 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 || p[1] == 'g'
2763 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2764 || p[1] == 'I'
2765 || (p[1] == 'r' && p[2] != 'e')))
2766 {
2767 eap->cmdidx = CMD_substitute;
2768 ++p;
2769 }
2770 else
2771 {
2772 while (ASCII_ISALPHA(*p))
2773 ++p;
2774 /* check for non-alpha command */
2775 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2776 ++p;
2777 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002778 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2779 {
2780 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2781 * :delete with the 'l' flag. Same for 'p'. */
2782 for (i = 0; i < len; ++i)
2783 if (eap->cmd[i] != "delete"[i])
2784 break;
2785 if (i == len - 1)
2786 {
2787 --len;
2788 if (p[-1] == 'l')
2789 eap->flags |= EXFLAG_LIST;
2790 else
2791 eap->flags |= EXFLAG_PRINT;
2792 }
2793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 if (ASCII_ISLOWER(*eap->cmd))
2796 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2797 else
2798 eap->cmdidx = cmdidxs[26];
2799
2800 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2801 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2802 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2803 (size_t)len) == 0)
2804 {
2805#ifdef FEAT_EVAL
2806 if (full != NULL
2807 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2808 *full = TRUE;
2809#endif
2810 break;
2811 }
2812
2813#ifdef FEAT_USR_CMDS
2814 /* Look for a user defined command as a last resort */
2815 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2816 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002817 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 while (ASCII_ISALNUM(*p))
2819 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002820 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002823 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 eap->cmdidx = CMD_SIZE;
2825 }
2826
2827 return p;
2828}
2829
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002830#ifdef FEAT_USR_CMDS
2831/*
2832 * Search for a user command that matches "eap->cmd".
2833 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2834 * Return a pointer to just after the command.
2835 * Return NULL if there is no matching command.
2836 */
2837 static char_u *
2838find_ucmd(eap, p, full, xp, compl)
2839 exarg_T *eap;
2840 char_u *p; /* end of the command (possibly including count) */
2841 int *full; /* set to TRUE for a full match */
2842 expand_T *xp; /* used for completion, NULL otherwise */
2843 int *compl; /* completion flags or NULL */
2844{
2845 int len = (int)(p - eap->cmd);
2846 int j, k, matchlen = 0;
2847 ucmd_T *uc;
2848 int found = FALSE;
2849 int possible = FALSE;
2850 char_u *cp, *np; /* Point into typed cmd and test name */
2851 garray_T *gap;
2852 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2853 only full match global is accepted. */
2854
2855 /*
2856 * Look for buffer-local user commands first, then global ones.
2857 */
2858 gap = &curbuf->b_ucmds;
2859 for (;;)
2860 {
2861 for (j = 0; j < gap->ga_len; ++j)
2862 {
2863 uc = USER_CMD_GA(gap, j);
2864 cp = eap->cmd;
2865 np = uc->uc_name;
2866 k = 0;
2867 while (k < len && *np != NUL && *cp++ == *np++)
2868 k++;
2869 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2870 {
2871 /* If finding a second match, the command is ambiguous. But
2872 * not if a buffer-local command wasn't a full match and a
2873 * global command is a full match. */
2874 if (k == len && found && *np != NUL)
2875 {
2876 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002877 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002878 amb_local = TRUE;
2879 }
2880
2881 if (!found || (k == len && *np == NUL))
2882 {
2883 /* If we matched up to a digit, then there could
2884 * be another command including the digit that we
2885 * should use instead.
2886 */
2887 if (k == len)
2888 found = TRUE;
2889 else
2890 possible = TRUE;
2891
2892 if (gap == &ucmds)
2893 eap->cmdidx = CMD_USER;
2894 else
2895 eap->cmdidx = CMD_USER_BUF;
2896 eap->argt = uc->uc_argt;
2897 eap->useridx = j;
2898
2899# ifdef FEAT_CMDL_COMPL
2900 if (compl != NULL)
2901 *compl = uc->uc_compl;
2902# ifdef FEAT_EVAL
2903 if (xp != NULL)
2904 {
2905 xp->xp_arg = uc->uc_compl_arg;
2906 xp->xp_scriptID = uc->uc_scriptID;
2907 }
2908# endif
2909# endif
2910 /* Do not search for further abbreviations
2911 * if this is an exact match. */
2912 matchlen = k;
2913 if (k == len && *np == NUL)
2914 {
2915 if (full != NULL)
2916 *full = TRUE;
2917 amb_local = FALSE;
2918 break;
2919 }
2920 }
2921 }
2922 }
2923
2924 /* Stop if we found a full match or searched all. */
2925 if (j < gap->ga_len || gap == &ucmds)
2926 break;
2927 gap = &ucmds;
2928 }
2929
2930 /* Only found ambiguous matches. */
2931 if (amb_local)
2932 {
2933 if (xp != NULL)
2934 xp->xp_context = EXPAND_UNSUCCESSFUL;
2935 return NULL;
2936 }
2937
2938 /* The match we found may be followed immediately by a number. Move "p"
2939 * back to point to it. */
2940 if (found || possible)
2941 return p + (matchlen - len);
2942 return p;
2943}
2944#endif
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946#if defined(FEAT_EVAL) || defined(PROTO)
2947/*
2948 * Return > 0 if an Ex command "name" exists.
2949 * Return 2 if there is an exact match.
2950 * Return 3 if there is an ambiguous match.
2951 */
2952 int
2953cmd_exists(name)
2954 char_u *name;
2955{
2956 exarg_T ea;
2957 int full = FALSE;
2958 int i;
2959 int j;
2960 static struct cmdmod
2961 {
2962 char *name;
2963 int minlen;
2964 } cmdmods[] = {
2965 {"aboveleft", 3},
2966 {"belowright", 3},
2967 {"botright", 2},
2968 {"browse", 3},
2969 {"confirm", 4},
2970 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002971 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 {"keepjumps", 5},
2973 {"keepmarks", 3},
2974 {"leftabove", 5},
2975 {"lockmarks", 3},
2976 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002977 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {"silent", 3},
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002979 {"tab", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {"topleft", 2},
2981 {"verbose", 4},
2982 {"vertical", 4},
2983 };
2984
2985 /* Check command modifiers. */
2986 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2987 {
2988 for (j = 0; name[j] != NUL; ++j)
2989 if (name[j] != cmdmods[i].name[j])
2990 break;
2991 if (name[j] == NUL && j >= cmdmods[i].minlen)
2992 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2993 }
2994
2995 /* Check built-in commands and user defined commands. */
2996 ea.cmd = name;
2997 ea.cmdidx = (cmdidx_T)0;
2998 if (find_command(&ea, &full) == NULL)
2999 return 3;
3000 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3001}
3002#endif
3003
3004/*
3005 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3006 * we don't need/want deleted. Maybe this could be done better if we didn't
3007 * repeat all this stuff. The only problem is that they may not stay
3008 * perfectly compatible with each other, but then the command line syntax
3009 * probably won't change that much -- webb.
3010 */
3011 char_u *
3012set_one_cmd_context(xp, buff)
3013 expand_T *xp;
3014 char_u *buff; /* buffer for command string */
3015{
3016 char_u *p;
3017 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003018 int len = 0;
3019 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3021 int compl = EXPAND_NOTHING;
3022#endif
3023#ifdef FEAT_CMDL_COMPL
3024 int delim;
3025#endif
3026 int forceit = FALSE;
3027 int usefilter = FALSE; /* filter instead of file name */
3028
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003029 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 xp->xp_pattern = buff;
3031 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003032 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034/*
3035 * 2. skip comment lines and leading space, colons or bars
3036 */
3037 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3038 ;
3039 xp->xp_pattern = cmd;
3040
3041 if (*cmd == NUL)
3042 return NULL;
3043 if (*cmd == '"') /* ignore comment lines */
3044 {
3045 xp->xp_context = EXPAND_NOTHING;
3046 return NULL;
3047 }
3048
3049/*
3050 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3051 */
3052 cmd = skip_range(cmd, &xp->xp_context);
3053
3054/*
3055 * 4. parse command
3056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 xp->xp_pattern = cmd;
3058 if (*cmd == NUL)
3059 return NULL;
3060 if (*cmd == '"')
3061 {
3062 xp->xp_context = EXPAND_NOTHING;
3063 return NULL;
3064 }
3065
3066 if (*cmd == '|' || *cmd == '\n')
3067 return cmd + 1; /* There's another command */
3068
3069 /*
3070 * Isolate the command and search for it in the command table.
3071 * Exceptions:
3072 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003073 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3075 */
3076 if (*cmd == 'k' && cmd[1] != 'e')
3077 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003078 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 p = cmd + 1;
3080 }
3081 else
3082 {
3083 p = cmd;
3084 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3085 ++p;
3086 /* check for non-alpha command */
3087 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3088 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003089 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003091 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
3093 xp->xp_context = EXPAND_UNSUCCESSFUL;
3094 return NULL;
3095 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003096 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3097 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3098 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 break;
3100
3101#ifdef FEAT_USR_CMDS
3102 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3103 {
3104 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3105 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003106 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
3108#endif
3109 }
3110
3111 /*
3112 * If the cursor is touching the command, and it ends in an alpha-numeric
3113 * character, complete the command name.
3114 */
3115 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3116 return NULL;
3117
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003118 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
3120 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3121 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003122 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 p = cmd + 1;
3124 }
3125#ifdef FEAT_USR_CMDS
3126 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3127 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003128 ea.cmd = cmd;
3129 p = find_ucmd(&ea, p, NULL, xp,
3130# if defined(FEAT_CMDL_COMPL)
3131 &compl
3132# else
3133 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003135 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003136 if (p == NULL)
3137 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 }
3139#endif
3140 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003141 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
3143 /* Not still touching the command and it was an illegal one */
3144 xp->xp_context = EXPAND_UNSUCCESSFUL;
3145 return NULL;
3146 }
3147
3148 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3149
3150 if (*p == '!') /* forced commands */
3151 {
3152 forceit = TRUE;
3153 ++p;
3154 }
3155
3156/*
3157 * 5. parse arguments
3158 */
3159#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003160 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003162 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 arg = skipwhite(p);
3165
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003166 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 if (*arg == '>') /* append */
3169 {
3170 if (*++arg == '>')
3171 ++arg;
3172 arg = skipwhite(arg);
3173 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003174 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 ++arg;
3177 usefilter = TRUE;
3178 }
3179 }
3180
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003181 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
3183 usefilter = forceit; /* :r! filter if forced */
3184 if (*arg == '!') /* :r !filter */
3185 {
3186 ++arg;
3187 usefilter = TRUE;
3188 }
3189 }
3190
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 while (*arg == *cmd) /* allow any number of '>' or '<' */
3194 ++arg;
3195 arg = skipwhite(arg);
3196 }
3197
3198 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003199 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 /* Check if we're in the +command */
3202 p = arg + 1;
3203 arg = skip_cmd_arg(arg, FALSE);
3204
3205 /* Still touching the command after '+'? */
3206 if (*arg == NUL)
3207 return p;
3208
3209 /* Skip space(s) after +command to get to the real argument */
3210 arg = skipwhite(arg);
3211 }
3212
3213 /*
3214 * Check for '|' to separate commands and '"' to start comments.
3215 * Don't do this for ":read !cmd" and ":write !cmd".
3216 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003217 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 p = arg;
3220 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003221 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 p += 2;
3223 while (*p)
3224 {
3225 if (*p == Ctrl_V)
3226 {
3227 if (p[1] != NUL)
3228 ++p;
3229 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003230 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 || *p == '|' || *p == '\n')
3232 {
3233 if (*(p - 1) != '\\')
3234 {
3235 if (*p == '|' || *p == '\n')
3236 return p + 1;
3237 return NULL; /* It's a comment */
3238 }
3239 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003240 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242 }
3243
3244 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 vim_strchr((char_u *)"|\"", *arg) == NULL)
3247 return NULL;
3248
3249 /* Find start of last argument (argument just before cursor): */
3250 p = buff + STRLEN(buff);
3251 while (p != arg && *p != ' ' && *p != TAB)
3252 p--;
3253 if (*p == ' ' || *p == TAB)
3254 p++;
3255 xp->xp_pattern = p;
3256
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
3259 int in_quote = FALSE;
3260 char_u *bow = NULL; /* Beginning of word */
3261
3262 /*
3263 * Allow spaces within back-quotes to count as part of the argument
3264 * being expanded.
3265 */
3266 xp->xp_pattern = skipwhite(arg);
3267 for (p = xp->xp_pattern; *p; )
3268 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003269 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 ++p;
3271#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#else
3274 else if (vim_iswhite(*p))
3275#endif
3276 {
3277 p = skipwhite(p);
3278 if (in_quote)
3279 bow = p;
3280 else
3281 xp->xp_pattern = p;
3282 --p;
3283 }
3284 else if (*p == '`')
3285 {
3286 if (!in_quote)
3287 {
3288 xp->xp_pattern = p;
3289 bow = p + 1;
3290 }
3291 in_quote = !in_quote;
3292 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003293 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295
3296 /*
3297 * If we are still inside the quotes, and we passed a space, just
3298 * expand from there.
3299 */
3300 if (bow != NULL && in_quote)
3301 xp->xp_pattern = bow;
3302 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003303
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003304#ifndef BACKSLASH_IN_FILENAME
3305 /* For a shell command more chars need to be escaped. */
3306 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003307 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003308 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003309
3310 /* When still after the command name expand executables. */
3311 if (xp->xp_pattern == skipwhite(arg))
3312 xp->xp_context = EXPAND_SHELLCMD;
3313 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
3316 /* Check for environment variable */
3317 if (*xp->xp_pattern == '$'
3318#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3319 || *xp->xp_pattern == '%'
3320#endif
3321 )
3322 {
3323 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3324 if (!vim_isIDc(*p))
3325 break;
3326 if (*p == NUL)
3327 {
3328 xp->xp_context = EXPAND_ENV_VARS;
3329 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003330#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3331 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003332 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003333 compl = EXPAND_ENV_VARS;
3334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336 }
3337 }
3338
3339/*
3340 * 6. switch on command name
3341 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
3344 case CMD_cd:
3345 case CMD_chdir:
3346 case CMD_lcd:
3347 case CMD_lchdir:
3348 if (xp->xp_context == EXPAND_FILES)
3349 xp->xp_context = EXPAND_DIRECTORIES;
3350 break;
3351 case CMD_help:
3352 xp->xp_context = EXPAND_HELP;
3353 xp->xp_pattern = arg;
3354 break;
3355
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003356 /* Command modifiers: return the argument.
3357 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003359 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 case CMD_belowright:
3361 case CMD_botright:
3362 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003363 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003365 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 case CMD_folddoclosed:
3367 case CMD_folddoopen:
3368 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003369 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 case CMD_keepjumps:
3371 case CMD_keepmarks:
3372 case CMD_leftabove:
3373 case CMD_lockmarks:
3374 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003375 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003377 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 case CMD_topleft:
3379 case CMD_verbose:
3380 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003381 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 return arg;
3383
3384#ifdef FEAT_SEARCH_EXTRA
3385 case CMD_match:
3386 if (*arg == NUL || !ends_excmd(*arg))
3387 {
3388 /* Dummy call to clear variables. */
3389 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3390 xp->xp_context = EXPAND_HIGHLIGHT;
3391 xp->xp_pattern = arg;
3392 arg = skipwhite(skiptowhite(arg));
3393 if (*arg != NUL)
3394 {
3395 xp->xp_context = EXPAND_NOTHING;
3396 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3397 }
3398 }
3399 return find_nextcmd(arg);
3400#endif
3401
3402#ifdef FEAT_CMDL_COMPL
3403/*
3404 * All completion for the +cmdline_compl feature goes here.
3405 */
3406
3407# ifdef FEAT_USR_CMDS
3408 case CMD_command:
3409 /* Check for attributes */
3410 while (*arg == '-')
3411 {
3412 arg++; /* Skip "-" */
3413 p = skiptowhite(arg);
3414 if (*p == NUL)
3415 {
3416 /* Cursor is still in the attribute */
3417 p = vim_strchr(arg, '=');
3418 if (p == NULL)
3419 {
3420 /* No "=", so complete attribute names */
3421 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3422 xp->xp_pattern = arg;
3423 return NULL;
3424 }
3425
3426 /* For the -complete and -nargs attributes, we complete
3427 * their arguments as well.
3428 */
3429 if (STRNICMP(arg, "complete", p - arg) == 0)
3430 {
3431 xp->xp_context = EXPAND_USER_COMPLETE;
3432 xp->xp_pattern = p + 1;
3433 return NULL;
3434 }
3435 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3436 {
3437 xp->xp_context = EXPAND_USER_NARGS;
3438 xp->xp_pattern = p + 1;
3439 return NULL;
3440 }
3441 return NULL;
3442 }
3443 arg = skipwhite(p);
3444 }
3445
3446 /* After the attributes comes the new command name */
3447 p = skiptowhite(arg);
3448 if (*p == NUL)
3449 {
3450 xp->xp_context = EXPAND_USER_COMMANDS;
3451 xp->xp_pattern = arg;
3452 break;
3453 }
3454
3455 /* And finally comes a normal command */
3456 return skipwhite(p);
3457
3458 case CMD_delcommand:
3459 xp->xp_context = EXPAND_USER_COMMANDS;
3460 xp->xp_pattern = arg;
3461 break;
3462# endif
3463
3464 case CMD_global:
3465 case CMD_vglobal:
3466 delim = *arg; /* get the delimiter */
3467 if (delim)
3468 ++arg; /* skip delimiter if there is one */
3469
3470 while (arg[0] != NUL && arg[0] != delim)
3471 {
3472 if (arg[0] == '\\' && arg[1] != NUL)
3473 ++arg;
3474 ++arg;
3475 }
3476 if (arg[0] != NUL)
3477 return arg + 1;
3478 break;
3479 case CMD_and:
3480 case CMD_substitute:
3481 delim = *arg;
3482 if (delim)
3483 {
3484 /* skip "from" part */
3485 ++arg;
3486 arg = skip_regexp(arg, delim, p_magic, NULL);
3487 }
3488 /* skip "to" part */
3489 while (arg[0] != NUL && arg[0] != delim)
3490 {
3491 if (arg[0] == '\\' && arg[1] != NUL)
3492 ++arg;
3493 ++arg;
3494 }
3495 if (arg[0] != NUL) /* skip delimiter */
3496 ++arg;
3497 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3498 ++arg;
3499 if (arg[0] != NUL)
3500 return arg;
3501 break;
3502 case CMD_isearch:
3503 case CMD_dsearch:
3504 case CMD_ilist:
3505 case CMD_dlist:
3506 case CMD_ijump:
3507 case CMD_psearch:
3508 case CMD_djump:
3509 case CMD_isplit:
3510 case CMD_dsplit:
3511 arg = skipwhite(skipdigits(arg)); /* skip count */
3512 if (*arg == '/') /* Match regexp, not just whole words */
3513 {
3514 for (++arg; *arg && *arg != '/'; arg++)
3515 if (*arg == '\\' && arg[1] != NUL)
3516 arg++;
3517 if (*arg)
3518 {
3519 arg = skipwhite(arg + 1);
3520
3521 /* Check for trailing illegal characters */
3522 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3523 xp->xp_context = EXPAND_NOTHING;
3524 else
3525 return arg;
3526 }
3527 }
3528 break;
3529#ifdef FEAT_AUTOCMD
3530 case CMD_autocmd:
3531 return set_context_in_autocmd(xp, arg, FALSE);
3532
3533 case CMD_doautocmd:
3534 return set_context_in_autocmd(xp, arg, TRUE);
3535#endif
3536 case CMD_set:
3537 set_context_in_set_cmd(xp, arg, 0);
3538 break;
3539 case CMD_setglobal:
3540 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3541 break;
3542 case CMD_setlocal:
3543 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3544 break;
3545 case CMD_tag:
3546 case CMD_stag:
3547 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003548 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 case CMD_tselect:
3550 case CMD_stselect:
3551 case CMD_ptselect:
3552 case CMD_tjump:
3553 case CMD_stjump:
3554 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003555 if (*p_wop != NUL)
3556 xp->xp_context = EXPAND_TAGS_LISTFILES;
3557 else
3558 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 xp->xp_pattern = arg;
3560 break;
3561 case CMD_augroup:
3562 xp->xp_context = EXPAND_AUGROUP;
3563 xp->xp_pattern = arg;
3564 break;
3565#ifdef FEAT_SYN_HL
3566 case CMD_syntax:
3567 set_context_in_syntax_cmd(xp, arg);
3568 break;
3569#endif
3570#ifdef FEAT_EVAL
3571 case CMD_let:
3572 case CMD_if:
3573 case CMD_elseif:
3574 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003575 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 case CMD_echo:
3577 case CMD_echon:
3578 case CMD_execute:
3579 case CMD_echomsg:
3580 case CMD_echoerr:
3581 case CMD_call:
3582 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 break;
3585
3586 case CMD_unlet:
3587 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3588 arg = xp->xp_pattern + 1;
3589 xp->xp_context = EXPAND_USER_VARS;
3590 xp->xp_pattern = arg;
3591 break;
3592
3593 case CMD_function:
3594 case CMD_delfunction:
3595 xp->xp_context = EXPAND_USER_FUNC;
3596 xp->xp_pattern = arg;
3597 break;
3598
3599 case CMD_echohl:
3600 xp->xp_context = EXPAND_HIGHLIGHT;
3601 xp->xp_pattern = arg;
3602 break;
3603#endif
3604 case CMD_highlight:
3605 set_context_in_highlight_cmd(xp, arg);
3606 break;
3607#ifdef FEAT_LISTCMDS
3608 case CMD_bdelete:
3609 case CMD_bwipeout:
3610 case CMD_bunload:
3611 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3612 arg = xp->xp_pattern + 1;
3613 /*FALLTHROUGH*/
3614 case CMD_buffer:
3615 case CMD_sbuffer:
3616 case CMD_checktime:
3617 xp->xp_context = EXPAND_BUFFERS;
3618 xp->xp_pattern = arg;
3619 break;
3620#endif
3621#ifdef FEAT_USR_CMDS
3622 case CMD_USER:
3623 case CMD_USER_BUF:
3624 if (compl != EXPAND_NOTHING)
3625 {
3626 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003627 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
3629# ifdef FEAT_MENU
3630 if (compl == EXPAND_MENUS)
3631 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3632# endif
3633 if (compl == EXPAND_COMMANDS)
3634 return arg;
3635 if (compl == EXPAND_MAPPINGS)
3636 return set_context_in_map_cmd(xp, (char_u *)"map",
3637 arg, forceit, FALSE, FALSE, CMD_map);
3638 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3639 arg = xp->xp_pattern + 1;
3640 xp->xp_pattern = arg;
3641 }
3642 xp->xp_context = compl;
3643 }
3644 break;
3645#endif
3646 case CMD_map: case CMD_noremap:
3647 case CMD_nmap: case CMD_nnoremap:
3648 case CMD_vmap: case CMD_vnoremap:
3649 case CMD_omap: case CMD_onoremap:
3650 case CMD_imap: case CMD_inoremap:
3651 case CMD_cmap: case CMD_cnoremap:
3652 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003653 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 case CMD_unmap:
3655 case CMD_nunmap:
3656 case CMD_vunmap:
3657 case CMD_ounmap:
3658 case CMD_iunmap:
3659 case CMD_cunmap:
3660 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003661 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 case CMD_abbreviate: case CMD_noreabbrev:
3663 case CMD_cabbrev: case CMD_cnoreabbrev:
3664 case CMD_iabbrev: case CMD_inoreabbrev:
3665 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003666 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 case CMD_unabbreviate:
3668 case CMD_cunabbrev:
3669 case CMD_iunabbrev:
3670 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003671 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672#ifdef FEAT_MENU
3673 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3674 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3675 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3676 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3677 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3678 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3679 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3680 case CMD_tmenu: case CMD_tunmenu:
3681 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3682 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3683#endif
3684
3685 case CMD_colorscheme:
3686 xp->xp_context = EXPAND_COLORS;
3687 xp->xp_pattern = arg;
3688 break;
3689
3690 case CMD_compiler:
3691 xp->xp_context = EXPAND_COMPILER;
3692 xp->xp_pattern = arg;
3693 break;
3694
3695#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3696 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3697 case CMD_language:
3698 if (*skiptowhite(arg) == NUL)
3699 {
3700 xp->xp_context = EXPAND_LANGUAGE;
3701 xp->xp_pattern = arg;
3702 }
3703 else
3704 xp->xp_context = EXPAND_NOTHING;
3705 break;
3706#endif
3707
3708#endif /* FEAT_CMDL_COMPL */
3709
3710 default:
3711 break;
3712 }
3713 return NULL;
3714}
3715
3716/*
3717 * skip a range specifier of the form: addr [,addr] [;addr] ..
3718 *
3719 * Backslashed delimiters after / or ? will be skipped, and commands will
3720 * not be expanded between /'s and ?'s or after "'".
3721 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003722 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 * Returns the "cmd" pointer advanced to beyond the range.
3724 */
3725 char_u *
3726skip_range(cmd, ctx)
3727 char_u *cmd;
3728 int *ctx; /* pointer to xp_context or NULL */
3729{
3730 int delim;
3731
Bram Moolenaardf177f62005-02-22 08:39:57 +00003732 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
3734 if (*cmd == '\'')
3735 {
3736 if (*++cmd == NUL && ctx != NULL)
3737 *ctx = EXPAND_NOTHING;
3738 }
3739 else if (*cmd == '/' || *cmd == '?')
3740 {
3741 delim = *cmd++;
3742 while (*cmd != NUL && *cmd != delim)
3743 if (*cmd++ == '\\' && *cmd != NUL)
3744 ++cmd;
3745 if (*cmd == NUL && ctx != NULL)
3746 *ctx = EXPAND_NOTHING;
3747 }
3748 if (*cmd != NUL)
3749 ++cmd;
3750 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003751
3752 /* Skip ":" and white space. */
3753 while (*cmd == ':')
3754 cmd = skipwhite(cmd + 1);
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 return cmd;
3757}
3758
3759/*
3760 * get a single EX address
3761 *
3762 * Set ptr to the next character after the part that was interpreted.
3763 * Set ptr to NULL when an error is encountered.
3764 *
3765 * Return MAXLNUM when no Ex address was found.
3766 */
3767 static linenr_T
3768get_address(ptr, skip, to_other_file)
3769 char_u **ptr;
3770 int skip; /* only skip the address, don't use it */
3771 int to_other_file; /* flag: may jump to other file */
3772{
3773 int c;
3774 int i;
3775 long n;
3776 char_u *cmd;
3777 pos_T pos;
3778 pos_T *fp;
3779 linenr_T lnum;
3780
3781 cmd = skipwhite(*ptr);
3782 lnum = MAXLNUM;
3783 do
3784 {
3785 switch (*cmd)
3786 {
3787 case '.': /* '.' - Cursor position */
3788 ++cmd;
3789 lnum = curwin->w_cursor.lnum;
3790 break;
3791
3792 case '$': /* '$' - last line */
3793 ++cmd;
3794 lnum = curbuf->b_ml.ml_line_count;
3795 break;
3796
3797 case '\'': /* ''' - mark */
3798 if (*++cmd == NUL)
3799 {
3800 cmd = NULL;
3801 goto error;
3802 }
3803 if (skip)
3804 ++cmd;
3805 else
3806 {
3807 /* Only accept a mark in another file when it is
3808 * used by itself: ":'M". */
3809 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3810 ++cmd;
3811 if (fp == (pos_T *)-1)
3812 /* Jumped to another file. */
3813 lnum = curwin->w_cursor.lnum;
3814 else
3815 {
3816 if (check_mark(fp) == FAIL)
3817 {
3818 cmd = NULL;
3819 goto error;
3820 }
3821 lnum = fp->lnum;
3822 }
3823 }
3824 break;
3825
3826 case '/':
3827 case '?': /* '/' or '?' - search */
3828 c = *cmd++;
3829 if (skip) /* skip "/pat/" */
3830 {
3831 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3832 if (*cmd == c)
3833 ++cmd;
3834 }
3835 else
3836 {
3837 pos = curwin->w_cursor; /* save curwin->w_cursor */
3838 /*
3839 * When '/' or '?' follows another address, start
3840 * from there.
3841 */
3842 if (lnum != MAXLNUM)
3843 curwin->w_cursor.lnum = lnum;
3844 /*
3845 * Start a forward search at the end of the line.
3846 * Start a backward search at the start of the line.
3847 * This makes sure we never match in the current
3848 * line, and can match anywhere in the
3849 * next/previous line.
3850 */
3851 if (c == '/')
3852 curwin->w_cursor.col = MAXCOL;
3853 else
3854 curwin->w_cursor.col = 0;
3855 searchcmdlen = 0;
3856 if (!do_search(NULL, c, cmd, 1L,
3857 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3858 {
3859 curwin->w_cursor = pos;
3860 cmd = NULL;
3861 goto error;
3862 }
3863 lnum = curwin->w_cursor.lnum;
3864 curwin->w_cursor = pos;
3865 /* adjust command string pointer */
3866 cmd += searchcmdlen;
3867 }
3868 break;
3869
3870 case '\\': /* "\?", "\/" or "\&", repeat search */
3871 ++cmd;
3872 if (*cmd == '&')
3873 i = RE_SUBST;
3874 else if (*cmd == '?' || *cmd == '/')
3875 i = RE_SEARCH;
3876 else
3877 {
3878 EMSG(_(e_backslash));
3879 cmd = NULL;
3880 goto error;
3881 }
3882
3883 if (!skip)
3884 {
3885 /*
3886 * When search follows another address, start from
3887 * there.
3888 */
3889 if (lnum != MAXLNUM)
3890 pos.lnum = lnum;
3891 else
3892 pos.lnum = curwin->w_cursor.lnum;
3893
3894 /*
3895 * Start the search just like for the above
3896 * do_search().
3897 */
3898 if (*cmd != '?')
3899 pos.col = MAXCOL;
3900 else
3901 pos.col = 0;
3902 if (searchit(curwin, curbuf, &pos,
3903 *cmd == '?' ? BACKWARD : FORWARD,
3904 (char_u *)"", 1L,
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003905 SEARCH_MSG + SEARCH_START,
3906 i, (linenr_T)0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 lnum = pos.lnum;
3908 else
3909 {
3910 cmd = NULL;
3911 goto error;
3912 }
3913 }
3914 ++cmd;
3915 break;
3916
3917 default:
3918 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3919 lnum = getdigits(&cmd);
3920 }
3921
3922 for (;;)
3923 {
3924 cmd = skipwhite(cmd);
3925 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3926 break;
3927
3928 if (lnum == MAXLNUM)
3929 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3930 if (VIM_ISDIGIT(*cmd))
3931 i = '+'; /* "number" is same as "+number" */
3932 else
3933 i = *cmd++;
3934 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3935 n = 1;
3936 else
3937 n = getdigits(&cmd);
3938 if (i == '-')
3939 lnum -= n;
3940 else
3941 lnum += n;
3942 }
3943 } while (*cmd == '/' || *cmd == '?');
3944
3945error:
3946 *ptr = cmd;
3947 return lnum;
3948}
3949
3950/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003951 * Get flags from an Ex command argument.
3952 */
3953 static void
3954get_flags(eap)
3955 exarg_T *eap;
3956{
3957 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3958 {
3959 if (*eap->arg == 'l')
3960 eap->flags |= EXFLAG_LIST;
3961 else if (*eap->arg == 'p')
3962 eap->flags |= EXFLAG_PRINT;
3963 else
3964 eap->flags |= EXFLAG_NR;
3965 eap->arg = skipwhite(eap->arg + 1);
3966 }
3967}
3968
3969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 * Function called for command which is Not Implemented. NI!
3971 */
3972 void
3973ex_ni(eap)
3974 exarg_T *eap;
3975{
3976 if (!eap->skip)
3977 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3978}
3979
3980#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003981 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982/*
3983 * Function called for script command which is Not Implemented. NI!
3984 * Skips over ":perl <<EOF" constructs.
3985 */
3986 static void
3987ex_script_ni(eap)
3988 exarg_T *eap;
3989{
3990 if (!eap->skip)
3991 ex_ni(eap);
3992 else
3993 vim_free(script_get(eap, eap->arg));
3994}
3995#endif
3996
3997/*
3998 * Check range in Ex command for validity.
3999 * Return NULL when valid, error message when invalid.
4000 */
4001 static char_u *
4002invalid_range(eap)
4003 exarg_T *eap;
4004{
4005 if ( eap->line1 < 0
4006 || eap->line2 < 0
4007 || eap->line1 > eap->line2
4008 || ((eap->argt & RANGE)
4009 && !(eap->argt & NOTADR)
4010 && eap->line2 > curbuf->b_ml.ml_line_count
4011#ifdef FEAT_DIFF
4012 + (eap->cmdidx == CMD_diffget)
4013#endif
4014 ))
4015 return (char_u *)_(e_invrange);
4016 return NULL;
4017}
4018
4019/*
4020 * Correct the range for zero line number, if required.
4021 */
4022 static void
4023correct_range(eap)
4024 exarg_T *eap;
4025{
4026 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4027 {
4028 if (eap->line1 == 0)
4029 eap->line1 = 1;
4030 if (eap->line2 == 0)
4031 eap->line2 = 1;
4032 }
4033}
4034
Bram Moolenaar748bf032005-02-02 23:04:36 +00004035#ifdef FEAT_QUICKFIX
4036static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4037
4038/*
4039 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4040 * pattern. Otherwise return eap->arg.
4041 */
4042 static char_u *
4043skip_grep_pat(eap)
4044 exarg_T *eap;
4045{
4046 char_u *p = eap->arg;
4047
Bram Moolenaara37420f2006-02-04 22:37:47 +00004048 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4049 || eap->cmdidx == CMD_vimgrepadd
4050 || eap->cmdidx == CMD_lvimgrepadd
4051 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004052 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004053 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004054 if (p == NULL)
4055 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004056 }
4057 return p;
4058}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004059
4060/*
4061 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4062 * in the command line, so that things like % get expanded.
4063 */
4064 static char_u *
4065replace_makeprg(eap, p, cmdlinep)
4066 exarg_T *eap;
4067 char_u *p;
4068 char_u **cmdlinep;
4069{
4070 char_u *new_cmdline;
4071 char_u *program;
4072 char_u *pos;
4073 char_u *ptr;
4074 int len;
4075 int i;
4076
4077 /*
4078 * Don't do it when ":vimgrep" is used for ":grep".
4079 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004080 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4081 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4082 || eap->cmdidx == CMD_grepadd
4083 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004084 && !grep_internal(eap->cmdidx))
4085 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004086 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4087 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004088 {
4089 if (*curbuf->b_p_gp == NUL)
4090 program = p_gp;
4091 else
4092 program = curbuf->b_p_gp;
4093 }
4094 else
4095 {
4096 if (*curbuf->b_p_mp == NUL)
4097 program = p_mp;
4098 else
4099 program = curbuf->b_p_mp;
4100 }
4101
4102 p = skipwhite(p);
4103
4104 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4105 {
4106 /* replace $* by given arguments */
4107 i = 1;
4108 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4109 ++i;
4110 len = (int)STRLEN(p);
4111 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4112 if (new_cmdline == NULL)
4113 return NULL; /* out of memory */
4114 ptr = new_cmdline;
4115 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4116 {
4117 i = (int)(pos - program);
4118 STRNCPY(ptr, program, i);
4119 STRCPY(ptr += i, p);
4120 ptr += len;
4121 program = pos + 2;
4122 }
4123 STRCPY(ptr, program);
4124 }
4125 else
4126 {
4127 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4128 if (new_cmdline == NULL)
4129 return NULL; /* out of memory */
4130 STRCPY(new_cmdline, program);
4131 STRCAT(new_cmdline, " ");
4132 STRCAT(new_cmdline, p);
4133 }
4134 msg_make(p);
4135
4136 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4137 vim_free(*cmdlinep);
4138 *cmdlinep = new_cmdline;
4139 p = new_cmdline;
4140 }
4141 return p;
4142}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004143#endif
4144
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145/*
4146 * Expand file name in Ex command argument.
4147 * Return FAIL for failure, OK otherwise.
4148 */
4149 int
4150expand_filename(eap, cmdlinep, errormsgp)
4151 exarg_T *eap;
4152 char_u **cmdlinep;
4153 char_u **errormsgp;
4154{
4155 int has_wildcards; /* need to expand wildcards */
4156 char_u *repl;
4157 int srclen;
4158 char_u *p;
4159 int n;
4160
Bram Moolenaar748bf032005-02-02 23:04:36 +00004161#ifdef FEAT_QUICKFIX
4162 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4163 p = skip_grep_pat(eap);
4164#else
4165 p = eap->arg;
4166#endif
4167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 /*
4169 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4170 * the file name contains a wildcard it should not cause expanding.
4171 * (it will be expanded anyway if there is a wildcard before replacing).
4172 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004173 has_wildcards = mch_has_wildcard(p);
4174 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004176#ifdef FEAT_EVAL
4177 /* Skip over `=expr`, wildcards in it are not expanded. */
4178 if (p[0] == '`' && p[1] == '=')
4179 {
4180 p += 2;
4181 (void)skip_expr(&p);
4182 if (*p == '`')
4183 ++p;
4184 continue;
4185 }
4186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 /*
4188 * Quick check if this cannot be the start of a special string.
4189 * Also removes backslash before '%', '#' and '<'.
4190 */
4191 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4192 {
4193 ++p;
4194 continue;
4195 }
4196
4197 /*
4198 * Try to find a match at this position.
4199 */
4200 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4201 if (*errormsgp != NULL) /* error detected */
4202 return FAIL;
4203 if (repl == NULL) /* no match found */
4204 {
4205 p += srclen;
4206 continue;
4207 }
4208
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004209 /* Wildcards won't be expanded below, the replacement is taken
4210 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4211 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4212 {
4213 char_u *l = repl;
4214
4215 repl = expand_env_save(repl);
4216 vim_free(l);
4217 }
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 /* Need to escape white space et al. with a backslash. Don't do this
4220 * for shell commands (may have to use quotes instead). Don't do this
4221 * for non-unix systems when there is a single argument (spaces don't
4222 * separate arguments then). */
4223 if (!eap->usefilter
4224 && eap->cmdidx != CMD_bang
4225 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004226 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004228 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004230 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231#ifndef UNIX
4232 && !(eap->argt & NOSPC)
4233#endif
4234 )
4235 {
4236 char_u *l;
4237#ifdef BACKSLASH_IN_FILENAME
4238 /* Don't escape a backslash here, because rem_backslash() doesn't
4239 * remove it later. */
4240 static char_u *nobslash = (char_u *)" \t\"|";
4241# define ESCAPE_CHARS nobslash
4242#else
4243# define ESCAPE_CHARS escape_chars
4244#endif
4245
4246 for (l = repl; *l; ++l)
4247 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4248 {
4249 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4250 if (l != NULL)
4251 {
4252 vim_free(repl);
4253 repl = l;
4254 }
4255 break;
4256 }
4257 }
4258
4259 /* For a shell command a '!' must be escaped. */
4260 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004261 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
4263 char_u *l;
4264
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004265 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (l != NULL)
4267 {
4268 vim_free(repl);
4269 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004270 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (strstr((char *)p_sh, "sh") != NULL)
4272 {
4273 l = vim_strsave_escaped(repl, (char_u *)"!");
4274 if (l != NULL)
4275 {
4276 vim_free(repl);
4277 repl = l;
4278 }
4279 }
4280 }
4281 }
4282
4283 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4284 vim_free(repl);
4285 if (p == NULL)
4286 return FAIL;
4287 }
4288
4289 /*
4290 * One file argument: Expand wildcards.
4291 * Don't do this with ":r !command" or ":w !command".
4292 */
4293 if ((eap->argt & NOSPC) && !eap->usefilter)
4294 {
4295 /*
4296 * May do this twice:
4297 * 1. Replace environment variables.
4298 * 2. Replace any other wildcards, remove backslashes.
4299 */
4300 for (n = 1; n <= 2; ++n)
4301 {
4302 if (n == 2)
4303 {
4304#ifdef UNIX
4305 /*
4306 * Only for Unix we check for more than one file name.
4307 * For other systems spaces are considered to be part
4308 * of the file name.
4309 * Only check here if there is no wildcard, otherwise
4310 * ExpandOne() will check for errors. This allows
4311 * ":e `ls ve*.c`" on Unix.
4312 */
4313 if (!has_wildcards)
4314 for (p = eap->arg; *p; ++p)
4315 {
4316 /* skip escaped characters */
4317 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4318 ++p;
4319 else if (vim_iswhite(*p))
4320 {
4321 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4322 return FAIL;
4323 }
4324 }
4325#endif
4326
4327 /*
4328 * Halve the number of backslashes (this is Vi compatible).
4329 * For Unix and OS/2, when wildcards are expanded, this is
4330 * done by ExpandOne() below.
4331 */
4332#if defined(UNIX) || defined(OS2)
4333 if (!has_wildcards)
4334#endif
4335 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337
4338 if (has_wildcards)
4339 {
4340 if (n == 1)
4341 {
4342 /*
4343 * First loop: May expand environment variables. This
4344 * can be done much faster with expand_env() than with
4345 * something else (e.g., calling a shell).
4346 * After expanding environment variables, check again
4347 * if there are still wildcards present.
4348 */
4349 if (vim_strchr(eap->arg, '$') != NULL
4350 || vim_strchr(eap->arg, '~') != NULL)
4351 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004352 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4353 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 has_wildcards = mch_has_wildcard(NameBuff);
4355 p = NameBuff;
4356 }
4357 else
4358 p = NULL;
4359 }
4360 else /* n == 2 */
4361 {
4362 expand_T xpc;
4363
4364 ExpandInit(&xpc);
4365 xpc.xp_context = EXPAND_FILES;
4366 p = ExpandOne(&xpc, eap->arg, NULL,
4367 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4368 WILD_EXPAND_FREE);
4369 ExpandCleanup(&xpc);
4370 if (p == NULL)
4371 return FAIL;
4372 }
4373 if (p != NULL)
4374 {
4375 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4376 p, cmdlinep);
4377 if (n == 2) /* p came from ExpandOne() */
4378 vim_free(p);
4379 }
4380 }
4381 }
4382 }
4383 return OK;
4384}
4385
4386/*
4387 * Replace part of the command line, keeping eap->cmd, eap->arg and
4388 * eap->nextcmd correct.
4389 * "src" points to the part that is to be replaced, of length "srclen".
4390 * "repl" is the replacement string.
4391 * Returns a pointer to the character after the replaced string.
4392 * Returns NULL for failure.
4393 */
4394 static char_u *
4395repl_cmdline(eap, src, srclen, repl, cmdlinep)
4396 exarg_T *eap;
4397 char_u *src;
4398 int srclen;
4399 char_u *repl;
4400 char_u **cmdlinep;
4401{
4402 int len;
4403 int i;
4404 char_u *new_cmdline;
4405
4406 /*
4407 * The new command line is build in new_cmdline[].
4408 * First allocate it.
4409 * Careful: a "+cmd" argument may have been NUL terminated.
4410 */
4411 len = (int)STRLEN(repl);
4412 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004413 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4415 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4416 return NULL; /* out of memory! */
4417
4418 /*
4419 * Copy the stuff before the expanded part.
4420 * Copy the expanded stuff.
4421 * Copy what came after the expanded part.
4422 * Copy the next commands, if there are any.
4423 */
4424 i = (int)(src - *cmdlinep); /* length of part before match */
4425 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 mch_memmove(new_cmdline + i, repl, (size_t)len);
4428 i += len; /* remember the end of the string */
4429 STRCPY(new_cmdline + i, src + srclen);
4430 src = new_cmdline + i; /* remember where to continue */
4431
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004432 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {
4434 i = (int)STRLEN(new_cmdline) + 1;
4435 STRCPY(new_cmdline + i, eap->nextcmd);
4436 eap->nextcmd = new_cmdline + i;
4437 }
4438 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4439 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4440 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4441 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4442 vim_free(*cmdlinep);
4443 *cmdlinep = new_cmdline;
4444
4445 return src;
4446}
4447
4448/*
4449 * Check for '|' to separate commands and '"' to start comments.
4450 */
4451 void
4452separate_nextcmd(eap)
4453 exarg_T *eap;
4454{
4455 char_u *p;
4456
Bram Moolenaar86b68352004-12-27 21:59:20 +00004457#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004458 p = skip_grep_pat(eap);
4459#else
4460 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004461#endif
4462
4463 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
4465 if (*p == Ctrl_V)
4466 {
4467 if (eap->argt & (USECTRLV | XFILE))
4468 ++p; /* skip CTRL-V and next char */
4469 else
4470 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4471 if (*p == NUL) /* stop at NUL after CTRL-V */
4472 break;
4473 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004474
4475#ifdef FEAT_EVAL
4476 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004477 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004478 {
4479 p += 2;
4480 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004481 }
4482#endif
4483
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 /* Check for '"': start of comment or '|': next command */
4485 /* :@" and :*" do not start a comment!
4486 * :redir @" doesn't either. */
4487 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4488 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4489 || p != eap->arg)
4490 && (eap->cmdidx != CMD_redir
4491 || p != eap->arg + 1 || p[-1] != '@'))
4492 || *p == '|' || *p == '\n')
4493 {
4494 /*
4495 * We remove the '\' before the '|', unless USECTRLV is used
4496 * AND 'b' is present in 'cpoptions'.
4497 */
4498 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4499 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4500 {
4501 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4502 --p;
4503 }
4504 else
4505 {
4506 eap->nextcmd = check_nextcmd(p);
4507 *p = NUL;
4508 break;
4509 }
4510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4514 del_trailing_spaces(eap->arg);
4515}
4516
4517/*
4518 * get + command from ex argument
4519 */
4520 static char_u *
4521getargcmd(argp)
4522 char_u **argp;
4523{
4524 char_u *arg = *argp;
4525 char_u *command = NULL;
4526
4527 if (*arg == '+') /* +[command] */
4528 {
4529 ++arg;
4530 if (vim_isspace(*arg))
4531 command = dollar_command;
4532 else
4533 {
4534 command = arg;
4535 arg = skip_cmd_arg(command, TRUE);
4536 if (*arg != NUL)
4537 *arg++ = NUL; /* terminate command with NUL */
4538 }
4539
4540 arg = skipwhite(arg); /* skip over spaces */
4541 *argp = arg;
4542 }
4543 return command;
4544}
4545
4546/*
4547 * Find end of "+command" argument. Skip over "\ " and "\\".
4548 */
4549 static char_u *
4550skip_cmd_arg(p, rembs)
4551 char_u *p;
4552 int rembs; /* TRUE to halve the number of backslashes */
4553{
4554 while (*p && !vim_isspace(*p))
4555 {
4556 if (*p == '\\' && p[1] != NUL)
4557 {
4558 if (rembs)
4559 mch_memmove(p, p + 1, STRLEN(p));
4560 else
4561 ++p;
4562 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004563 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565 return p;
4566}
4567
4568/*
4569 * Get "++opt=arg" argument.
4570 * Return FAIL or OK.
4571 */
4572 static int
4573getargopt(eap)
4574 exarg_T *eap;
4575{
4576 char_u *arg = eap->arg + 2;
4577 int *pp = NULL;
4578#ifdef FEAT_MBYTE
4579 char_u *p;
4580#endif
4581
4582 /* ":edit ++[no]bin[ary] file" */
4583 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4584 {
4585 if (*arg == 'n')
4586 {
4587 arg += 2;
4588 eap->force_bin = FORCE_NOBIN;
4589 }
4590 else
4591 eap->force_bin = FORCE_BIN;
4592 if (!checkforcmd(&arg, "binary", 3))
4593 return FAIL;
4594 eap->arg = skipwhite(arg);
4595 return OK;
4596 }
4597
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004598 /* ":read ++edit file" */
4599 if (STRNCMP(arg, "edit", 4) == 0)
4600 {
4601 eap->read_edit = TRUE;
4602 eap->arg = skipwhite(arg + 4);
4603 return OK;
4604 }
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if (STRNCMP(arg, "ff", 2) == 0)
4607 {
4608 arg += 2;
4609 pp = &eap->force_ff;
4610 }
4611 else if (STRNCMP(arg, "fileformat", 10) == 0)
4612 {
4613 arg += 10;
4614 pp = &eap->force_ff;
4615 }
4616#ifdef FEAT_MBYTE
4617 else if (STRNCMP(arg, "enc", 3) == 0)
4618 {
4619 arg += 3;
4620 pp = &eap->force_enc;
4621 }
4622 else if (STRNCMP(arg, "encoding", 8) == 0)
4623 {
4624 arg += 8;
4625 pp = &eap->force_enc;
4626 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004627 else if (STRNCMP(arg, "bad", 3) == 0)
4628 {
4629 arg += 3;
4630 pp = &eap->bad_char;
4631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#endif
4633
4634 if (pp == NULL || *arg != '=')
4635 return FAIL;
4636
4637 ++arg;
4638 *pp = (int)(arg - eap->cmd);
4639 arg = skip_cmd_arg(arg, FALSE);
4640 eap->arg = skipwhite(arg);
4641 *arg = NUL;
4642
4643#ifdef FEAT_MBYTE
4644 if (pp == &eap->force_ff)
4645 {
4646#endif
4647 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4648 return FAIL;
4649#ifdef FEAT_MBYTE
4650 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004651 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
4653 /* Make 'fileencoding' lower case. */
4654 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4655 *p = TOLOWER_ASC(*p);
4656 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004657 else
4658 {
4659 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4660 * "drop". */
4661 p = eap->cmd + eap->bad_char;
4662 if (STRICMP(p, "keep") == 0)
4663 eap->bad_char = BAD_KEEP;
4664 else if (STRICMP(p, "drop") == 0)
4665 eap->bad_char = BAD_DROP;
4666 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4667 eap->bad_char = *p;
4668 else
4669 return FAIL;
4670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671#endif
4672
4673 return OK;
4674}
4675
4676/*
4677 * ":abbreviate" and friends.
4678 */
4679 static void
4680ex_abbreviate(eap)
4681 exarg_T *eap;
4682{
4683 do_exmap(eap, TRUE); /* almost the same as mapping */
4684}
4685
4686/*
4687 * ":map" and friends.
4688 */
4689 static void
4690ex_map(eap)
4691 exarg_T *eap;
4692{
4693 /*
4694 * If we are sourcing .exrc or .vimrc in current directory we
4695 * print the mappings for security reasons.
4696 */
4697 if (secure)
4698 {
4699 secure = 2;
4700 msg_outtrans(eap->cmd);
4701 msg_putchar('\n');
4702 }
4703 do_exmap(eap, FALSE);
4704}
4705
4706/*
4707 * ":unmap" and friends.
4708 */
4709 static void
4710ex_unmap(eap)
4711 exarg_T *eap;
4712{
4713 do_exmap(eap, FALSE);
4714}
4715
4716/*
4717 * ":mapclear" and friends.
4718 */
4719 static void
4720ex_mapclear(eap)
4721 exarg_T *eap;
4722{
4723 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4724}
4725
4726/*
4727 * ":abclear" and friends.
4728 */
4729 static void
4730ex_abclear(eap)
4731 exarg_T *eap;
4732{
4733 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4734}
4735
4736#ifdef FEAT_AUTOCMD
4737 static void
4738ex_autocmd(eap)
4739 exarg_T *eap;
4740{
4741 /*
4742 * Disallow auto commands from .exrc and .vimrc in current
4743 * directory for security reasons.
4744 */
4745 if (secure)
4746 {
4747 secure = 2;
4748 eap->errmsg = e_curdir;
4749 }
4750 else if (eap->cmdidx == CMD_autocmd)
4751 do_autocmd(eap->arg, eap->forceit);
4752 else
4753 do_augroup(eap->arg, eap->forceit);
4754}
4755
4756/*
4757 * ":doautocmd": Apply the automatic commands to the current buffer.
4758 */
4759 static void
4760ex_doautocmd(eap)
4761 exarg_T *eap;
4762{
4763 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004764 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765}
4766#endif
4767
4768#ifdef FEAT_LISTCMDS
4769/*
4770 * :[N]bunload[!] [N] [bufname] unload buffer
4771 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4772 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4773 */
4774 static void
4775ex_bunload(eap)
4776 exarg_T *eap;
4777{
4778 eap->errmsg = do_bufdel(
4779 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4780 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4781 : DOBUF_UNLOAD, eap->arg,
4782 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4783}
4784
4785/*
4786 * :[N]buffer [N] to buffer N
4787 * :[N]sbuffer [N] to buffer N
4788 */
4789 static void
4790ex_buffer(eap)
4791 exarg_T *eap;
4792{
4793 if (*eap->arg)
4794 eap->errmsg = e_trailing;
4795 else
4796 {
4797 if (eap->addr_count == 0) /* default is current buffer */
4798 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4799 else
4800 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4801 }
4802}
4803
4804/*
4805 * :[N]bmodified [N] to next mod. buffer
4806 * :[N]sbmodified [N] to next mod. buffer
4807 */
4808 static void
4809ex_bmodified(eap)
4810 exarg_T *eap;
4811{
4812 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4813}
4814
4815/*
4816 * :[N]bnext [N] to next buffer
4817 * :[N]sbnext [N] split and to next buffer
4818 */
4819 static void
4820ex_bnext(eap)
4821 exarg_T *eap;
4822{
4823 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4824}
4825
4826/*
4827 * :[N]bNext [N] to previous buffer
4828 * :[N]bprevious [N] to previous buffer
4829 * :[N]sbNext [N] split and to previous buffer
4830 * :[N]sbprevious [N] split and to previous buffer
4831 */
4832 static void
4833ex_bprevious(eap)
4834 exarg_T *eap;
4835{
4836 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4837}
4838
4839/*
4840 * :brewind to first buffer
4841 * :bfirst to first buffer
4842 * :sbrewind split and to first buffer
4843 * :sbfirst split and to first buffer
4844 */
4845 static void
4846ex_brewind(eap)
4847 exarg_T *eap;
4848{
4849 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4850}
4851
4852/*
4853 * :blast to last buffer
4854 * :sblast split and to last buffer
4855 */
4856 static void
4857ex_blast(eap)
4858 exarg_T *eap;
4859{
4860 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4861}
4862#endif
4863
4864 int
4865ends_excmd(c)
4866 int c;
4867{
4868 return (c == NUL || c == '|' || c == '"' || c == '\n');
4869}
4870
4871#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4872 || defined(PROTO)
4873/*
4874 * Return the next command, after the first '|' or '\n'.
4875 * Return NULL if not found.
4876 */
4877 char_u *
4878find_nextcmd(p)
4879 char_u *p;
4880{
4881 while (*p != '|' && *p != '\n')
4882 {
4883 if (*p == NUL)
4884 return NULL;
4885 ++p;
4886 }
4887 return (p + 1);
4888}
4889#endif
4890
4891/*
4892 * Check if *p is a separator between Ex commands.
4893 * Return NULL if it isn't, (p + 1) if it is.
4894 */
4895 char_u *
4896check_nextcmd(p)
4897 char_u *p;
4898{
4899 p = skipwhite(p);
4900 if (*p == '|' || *p == '\n')
4901 return (p + 1);
4902 else
4903 return NULL;
4904}
4905
4906/*
4907 * - if there are more files to edit
4908 * - and this is the last window
4909 * - and forceit not used
4910 * - and not repeated twice on a row
4911 * return FAIL and give error message if 'message' TRUE
4912 * return OK otherwise
4913 */
4914 static int
4915check_more(message, forceit)
4916 int message; /* when FALSE check only, no messages */
4917 int forceit;
4918{
4919 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4920
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004921 if (!forceit && only_one_window()
4922 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
4924 if (message)
4925 {
4926#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4927 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4928 {
4929 char_u buff[IOSIZE];
4930
4931 if (n == 1)
4932 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4933 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004934 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 _("%d more files to edit. Quit anyway?"), n);
4936 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4937 return OK;
4938 return FAIL;
4939 }
4940#endif
4941 if (n == 1)
4942 EMSG(_("E173: 1 more file to edit"));
4943 else
4944 EMSGN(_("E173: %ld more files to edit"), n);
4945 quitmore = 2; /* next try to quit is allowed */
4946 }
4947 return FAIL;
4948 }
4949 return OK;
4950}
4951
4952#ifdef FEAT_CMDL_COMPL
4953/*
4954 * Function given to ExpandGeneric() to obtain the list of command names.
4955 */
4956/*ARGSUSED*/
4957 char_u *
4958get_command_name(xp, idx)
4959 expand_T *xp;
4960 int idx;
4961{
4962 if (idx >= (int)CMD_SIZE)
4963# ifdef FEAT_USR_CMDS
4964 return get_user_command_name(idx);
4965# else
4966 return NULL;
4967# endif
4968 return cmdnames[idx].cmd_name;
4969}
4970#endif
4971
4972#if defined(FEAT_USR_CMDS) || defined(PROTO)
4973static 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));
4974static void uc_list __ARGS((char_u *name, size_t name_len));
4975static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4976static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4977static 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));
4978
4979 static int
4980uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4981 char_u *name;
4982 size_t name_len;
4983 char_u *rep;
4984 long argt;
4985 long def;
4986 int flags;
4987 int compl;
4988 char_u *compl_arg;
4989 int force;
4990{
4991 ucmd_T *cmd = NULL;
4992 char_u *p;
4993 int i;
4994 int cmp = 1;
4995 char_u *rep_buf = NULL;
4996 garray_T *gap;
4997
4998 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4999 if (rep_buf == NULL)
5000 {
5001 /* Can't replace termcodes - try using the string as is */
5002 rep_buf = vim_strsave(rep);
5003
5004 /* Give up if out of memory */
5005 if (rep_buf == NULL)
5006 return FAIL;
5007 }
5008
5009 /* get address of growarray: global or in curbuf */
5010 if (flags & UC_BUFFER)
5011 {
5012 gap = &curbuf->b_ucmds;
5013 if (gap->ga_itemsize == 0)
5014 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5015 }
5016 else
5017 gap = &ucmds;
5018
5019 /* Search for the command in the already defined commands. */
5020 for (i = 0; i < gap->ga_len; ++i)
5021 {
5022 size_t len;
5023
5024 cmd = USER_CMD_GA(gap, i);
5025 len = STRLEN(cmd->uc_name);
5026 cmp = STRNCMP(name, cmd->uc_name, name_len);
5027 if (cmp == 0)
5028 {
5029 if (name_len < len)
5030 cmp = -1;
5031 else if (name_len > len)
5032 cmp = 1;
5033 }
5034
5035 if (cmp == 0)
5036 {
5037 if (!force)
5038 {
5039 EMSG(_("E174: Command already exists: add ! to replace it"));
5040 goto fail;
5041 }
5042
5043 vim_free(cmd->uc_rep);
5044 cmd->uc_rep = 0;
5045 break;
5046 }
5047
5048 /* Stop as soon as we pass the name to add */
5049 if (cmp < 0)
5050 break;
5051 }
5052
5053 /* Extend the array unless we're replacing an existing command */
5054 if (cmp != 0)
5055 {
5056 if (ga_grow(gap, 1) != OK)
5057 goto fail;
5058 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5059 goto fail;
5060
5061 cmd = USER_CMD_GA(gap, i);
5062 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5063
5064 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
5066 cmd->uc_name = p;
5067 }
5068
5069 cmd->uc_rep = rep_buf;
5070 cmd->uc_argt = argt;
5071 cmd->uc_def = def;
5072 cmd->uc_compl = compl;
5073#ifdef FEAT_EVAL
5074 cmd->uc_scriptID = current_SID;
5075# ifdef FEAT_CMDL_COMPL
5076 cmd->uc_compl_arg = compl_arg;
5077# endif
5078#endif
5079
5080 return OK;
5081
5082fail:
5083 vim_free(rep_buf);
5084#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5085 vim_free(compl_arg);
5086#endif
5087 return FAIL;
5088}
5089
5090/*
5091 * List of names for completion for ":command" with the EXPAND_ flag.
5092 * Must be alphabetical for completion.
5093 */
5094static struct
5095{
5096 int expand;
5097 char *name;
5098} command_complete[] =
5099{
5100 {EXPAND_AUGROUP, "augroup"},
5101 {EXPAND_BUFFERS, "buffer"},
5102 {EXPAND_COMMANDS, "command"},
5103#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5104 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005105 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106#endif
5107 {EXPAND_DIRECTORIES, "dir"},
5108 {EXPAND_ENV_VARS, "environment"},
5109 {EXPAND_EVENTS, "event"},
5110 {EXPAND_EXPRESSION, "expression"},
5111 {EXPAND_FILES, "file"},
5112 {EXPAND_FUNCTIONS, "function"},
5113 {EXPAND_HELP, "help"},
5114 {EXPAND_HIGHLIGHT, "highlight"},
5115 {EXPAND_MAPPINGS, "mapping"},
5116 {EXPAND_MENUS, "menu"},
5117 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005118 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {EXPAND_TAGS, "tag"},
5120 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5121 {EXPAND_USER_VARS, "var"},
5122 {0, NULL}
5123};
5124
5125 static void
5126uc_list(name, name_len)
5127 char_u *name;
5128 size_t name_len;
5129{
5130 int i, j;
5131 int found = FALSE;
5132 ucmd_T *cmd;
5133 int len;
5134 long a;
5135 garray_T *gap;
5136
5137 gap = &curbuf->b_ucmds;
5138 for (;;)
5139 {
5140 for (i = 0; i < gap->ga_len; ++i)
5141 {
5142 cmd = USER_CMD_GA(gap, i);
5143 a = cmd->uc_argt;
5144
5145 /* Skip commands which don't match the requested prefix */
5146 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5147 continue;
5148
5149 /* Put out the title first time */
5150 if (!found)
5151 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5152 found = TRUE;
5153 msg_putchar('\n');
5154 if (got_int)
5155 break;
5156
5157 /* Special cases */
5158 msg_putchar(a & BANG ? '!' : ' ');
5159 msg_putchar(a & REGSTR ? '"' : ' ');
5160 msg_putchar(gap != &ucmds ? 'b' : ' ');
5161 msg_putchar(' ');
5162
5163 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5164 len = (int)STRLEN(cmd->uc_name) + 4;
5165
5166 do {
5167 msg_putchar(' ');
5168 ++len;
5169 } while (len < 16);
5170
5171 len = 0;
5172
5173 /* Arguments */
5174 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5175 {
5176 case 0: IObuff[len++] = '0'; break;
5177 case (EXTRA): IObuff[len++] = '*'; break;
5178 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5179 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5180 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5181 }
5182
5183 do {
5184 IObuff[len++] = ' ';
5185 } while (len < 5);
5186
5187 /* Range */
5188 if (a & (RANGE|COUNT))
5189 {
5190 if (a & COUNT)
5191 {
5192 /* -count=N */
5193 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5194 len += (int)STRLEN(IObuff + len);
5195 }
5196 else if (a & DFLALL)
5197 IObuff[len++] = '%';
5198 else if (cmd->uc_def >= 0)
5199 {
5200 /* -range=N */
5201 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5202 len += (int)STRLEN(IObuff + len);
5203 }
5204 else
5205 IObuff[len++] = '.';
5206 }
5207
5208 do {
5209 IObuff[len++] = ' ';
5210 } while (len < 11);
5211
5212 /* Completion */
5213 for (j = 0; command_complete[j].expand != 0; ++j)
5214 if (command_complete[j].expand == cmd->uc_compl)
5215 {
5216 STRCPY(IObuff + len, command_complete[j].name);
5217 len += (int)STRLEN(IObuff + len);
5218 break;
5219 }
5220
5221 do {
5222 IObuff[len++] = ' ';
5223 } while (len < 21);
5224
5225 IObuff[len] = '\0';
5226 msg_outtrans(IObuff);
5227
5228 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005229#ifdef FEAT_EVAL
5230 if (p_verbose > 0)
5231 last_set_msg(cmd->uc_scriptID);
5232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 out_flush();
5234 ui_breakcheck();
5235 if (got_int)
5236 break;
5237 }
5238 if (gap == &ucmds || i < gap->ga_len)
5239 break;
5240 gap = &ucmds;
5241 }
5242
5243 if (!found)
5244 MSG(_("No user-defined commands found"));
5245}
5246
5247 static char_u *
5248uc_fun_cmd()
5249{
5250 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5251 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5252 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5253 0xb9, 0x7f, 0};
5254 int i;
5255
5256 for (i = 0; fcmd[i]; ++i)
5257 IObuff[i] = fcmd[i] - 0x40;
5258 IObuff[i] = 0;
5259 return IObuff;
5260}
5261
5262 static int
5263uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5264 char_u *attr;
5265 size_t len;
5266 long *argt;
5267 long *def;
5268 int *flags;
5269 int *compl;
5270 char_u **compl_arg;
5271{
5272 char_u *p;
5273
5274 if (len == 0)
5275 {
5276 EMSG(_("E175: No attribute specified"));
5277 return FAIL;
5278 }
5279
5280 /* First, try the simple attributes (no arguments) */
5281 if (STRNICMP(attr, "bang", len) == 0)
5282 *argt |= BANG;
5283 else if (STRNICMP(attr, "buffer", len) == 0)
5284 *flags |= UC_BUFFER;
5285 else if (STRNICMP(attr, "register", len) == 0)
5286 *argt |= REGSTR;
5287 else if (STRNICMP(attr, "bar", len) == 0)
5288 *argt |= TRLBAR;
5289 else
5290 {
5291 int i;
5292 char_u *val = NULL;
5293 size_t vallen = 0;
5294 size_t attrlen = len;
5295
5296 /* Look for the attribute name - which is the part before any '=' */
5297 for (i = 0; i < (int)len; ++i)
5298 {
5299 if (attr[i] == '=')
5300 {
5301 val = &attr[i + 1];
5302 vallen = len - i - 1;
5303 attrlen = i;
5304 break;
5305 }
5306 }
5307
5308 if (STRNICMP(attr, "nargs", attrlen) == 0)
5309 {
5310 if (vallen == 1)
5311 {
5312 if (*val == '0')
5313 /* Do nothing - this is the default */;
5314 else if (*val == '1')
5315 *argt |= (EXTRA | NOSPC | NEEDARG);
5316 else if (*val == '*')
5317 *argt |= EXTRA;
5318 else if (*val == '?')
5319 *argt |= (EXTRA | NOSPC);
5320 else if (*val == '+')
5321 *argt |= (EXTRA | NEEDARG);
5322 else
5323 goto wrong_nargs;
5324 }
5325 else
5326 {
5327wrong_nargs:
5328 EMSG(_("E176: Invalid number of arguments"));
5329 return FAIL;
5330 }
5331 }
5332 else if (STRNICMP(attr, "range", attrlen) == 0)
5333 {
5334 *argt |= RANGE;
5335 if (vallen == 1 && *val == '%')
5336 *argt |= DFLALL;
5337 else if (val != NULL)
5338 {
5339 p = val;
5340 if (*def >= 0)
5341 {
5342two_count:
5343 EMSG(_("E177: Count cannot be specified twice"));
5344 return FAIL;
5345 }
5346
5347 *def = getdigits(&p);
5348 *argt |= (ZEROR | NOTADR);
5349
5350 if (p != val + vallen || vallen == 0)
5351 {
5352invalid_count:
5353 EMSG(_("E178: Invalid default value for count"));
5354 return FAIL;
5355 }
5356 }
5357 }
5358 else if (STRNICMP(attr, "count", attrlen) == 0)
5359 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005360 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361
5362 if (val != NULL)
5363 {
5364 p = val;
5365 if (*def >= 0)
5366 goto two_count;
5367
5368 *def = getdigits(&p);
5369
5370 if (p != val + vallen)
5371 goto invalid_count;
5372 }
5373
5374 if (*def < 0)
5375 *def = 0;
5376 }
5377 else if (STRNICMP(attr, "complete", attrlen) == 0)
5378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 if (val == NULL)
5380 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005381 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 return FAIL;
5383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005385 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5386 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 else
5390 {
5391 char_u ch = attr[len];
5392 attr[len] = '\0';
5393 EMSG2(_("E181: Invalid attribute: %s"), attr);
5394 attr[len] = ch;
5395 return FAIL;
5396 }
5397 }
5398
5399 return OK;
5400}
5401
5402 static void
5403ex_command(eap)
5404 exarg_T *eap;
5405{
5406 char_u *name;
5407 char_u *end;
5408 char_u *p;
5409 long argt = 0;
5410 long def = -1;
5411 int flags = 0;
5412 int compl = EXPAND_NOTHING;
5413 char_u *compl_arg = NULL;
5414 int has_attr = (eap->arg[0] == '-');
5415
5416 p = eap->arg;
5417
5418 /* Check for attributes */
5419 while (*p == '-')
5420 {
5421 ++p;
5422 end = skiptowhite(p);
5423 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5424 == FAIL)
5425 return;
5426 p = skipwhite(end);
5427 }
5428
5429 /* Get the name (if any) and skip to the following argument */
5430 name = p;
5431 if (ASCII_ISALPHA(*p))
5432 while (ASCII_ISALNUM(*p))
5433 ++p;
5434 if (!ends_excmd(*p) && !vim_iswhite(*p))
5435 {
5436 EMSG(_("E182: Invalid command name"));
5437 return;
5438 }
5439 end = p;
5440
5441 /* If there is nothing after the name, and no attributes were specified,
5442 * we are listing commands
5443 */
5444 p = skipwhite(end);
5445 if (!has_attr && ends_excmd(*p))
5446 {
5447 uc_list(name, end - name);
5448 }
5449 else if (!ASCII_ISUPPER(*name))
5450 {
5451 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5452 return;
5453 }
5454 else
5455 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5456 eap->forceit);
5457}
5458
5459/*
5460 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 * Clear all user commands, global and for current buffer.
5462 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005463/*ARGSUSED*/
5464 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465ex_comclear(eap)
5466 exarg_T *eap;
5467{
5468 uc_clear(&ucmds);
5469 uc_clear(&curbuf->b_ucmds);
5470}
5471
5472/*
5473 * Clear all user commands for "gap".
5474 */
5475 void
5476uc_clear(gap)
5477 garray_T *gap;
5478{
5479 int i;
5480 ucmd_T *cmd;
5481
5482 for (i = 0; i < gap->ga_len; ++i)
5483 {
5484 cmd = USER_CMD_GA(gap, i);
5485 vim_free(cmd->uc_name);
5486 vim_free(cmd->uc_rep);
5487# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5488 vim_free(cmd->uc_compl_arg);
5489# endif
5490 }
5491 ga_clear(gap);
5492}
5493
5494 static void
5495ex_delcommand(eap)
5496 exarg_T *eap;
5497{
5498 int i = 0;
5499 ucmd_T *cmd = NULL;
5500 int cmp = -1;
5501 garray_T *gap;
5502
5503 gap = &curbuf->b_ucmds;
5504 for (;;)
5505 {
5506 for (i = 0; i < gap->ga_len; ++i)
5507 {
5508 cmd = USER_CMD_GA(gap, i);
5509 cmp = STRCMP(eap->arg, cmd->uc_name);
5510 if (cmp <= 0)
5511 break;
5512 }
5513 if (gap == &ucmds || cmp == 0)
5514 break;
5515 gap = &ucmds;
5516 }
5517
5518 if (cmp != 0)
5519 {
5520 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5521 return;
5522 }
5523
5524 vim_free(cmd->uc_name);
5525 vim_free(cmd->uc_rep);
5526# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5527 vim_free(cmd->uc_compl_arg);
5528# endif
5529
5530 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
5532 if (i < gap->ga_len)
5533 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5534}
5535
5536 static char_u *
5537uc_split_args(arg, lenp)
5538 char_u *arg;
5539 size_t *lenp;
5540{
5541 char_u *buf;
5542 char_u *p;
5543 char_u *q;
5544 int len;
5545
5546 /* Precalculate length */
5547 p = arg;
5548 len = 2; /* Initial and final quotes */
5549
5550 while (*p)
5551 {
5552 if (p[0] == '\\' && vim_iswhite(p[1]))
5553 {
5554 len += 1;
5555 p += 2;
5556 }
5557 else if (*p == '\\' || *p == '"')
5558 {
5559 len += 2;
5560 p += 1;
5561 }
5562 else if (vim_iswhite(*p))
5563 {
5564 p = skipwhite(p);
5565 if (*p == NUL)
5566 break;
5567 len += 3; /* "," */
5568 }
5569 else
5570 {
5571 ++len;
5572 ++p;
5573 }
5574 }
5575
5576 buf = alloc(len + 1);
5577 if (buf == NULL)
5578 {
5579 *lenp = 0;
5580 return buf;
5581 }
5582
5583 p = arg;
5584 q = buf;
5585 *q++ = '"';
5586 while (*p)
5587 {
5588 if (p[0] == '\\' && vim_iswhite(p[1]))
5589 {
5590 *q++ = p[1];
5591 p += 2;
5592 }
5593 else if (*p == '\\' || *p == '"')
5594 {
5595 *q++ = '\\';
5596 *q++ = *p++;
5597 }
5598 else if (vim_iswhite(*p))
5599 {
5600 p = skipwhite(p);
5601 if (*p == NUL)
5602 break;
5603 *q++ = '"';
5604 *q++ = ',';
5605 *q++ = '"';
5606 }
5607 else
5608 {
5609 *q++ = *p++;
5610 }
5611 }
5612 *q++ = '"';
5613 *q = 0;
5614
5615 *lenp = len;
5616 return buf;
5617}
5618
5619/*
5620 * Check for a <> code in a user command.
5621 * "code" points to the '<'. "len" the length of the <> (inclusive).
5622 * "buf" is where the result is to be added.
5623 * "split_buf" points to a buffer used for splitting, caller should free it.
5624 * "split_len" is the length of what "split_buf" contains.
5625 * Returns the length of the replacement, which has been added to "buf".
5626 * Returns -1 if there was no match, and only the "<" has been copied.
5627 */
5628 static size_t
5629uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5630 char_u *code;
5631 size_t len;
5632 char_u *buf;
5633 ucmd_T *cmd; /* the user command we're expanding */
5634 exarg_T *eap; /* ex arguments */
5635 char_u **split_buf;
5636 size_t *split_len;
5637{
5638 size_t result = 0;
5639 char_u *p = code + 1;
5640 size_t l = len - 2;
5641 int quote = 0;
5642 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5643 ct_LT, ct_NONE } type = ct_NONE;
5644
5645 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5646 {
5647 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5648 p += 2;
5649 l -= 2;
5650 }
5651
Bram Moolenaar371d5402006-03-20 21:47:49 +00005652 ++l;
5653 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005655 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005657 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005659 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005661 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005663 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005665 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005667 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 type = ct_REGISTER;
5669
5670 switch (type)
5671 {
5672 case ct_ARGS:
5673 /* Simple case first */
5674 if (*eap->arg == NUL)
5675 {
5676 if (quote == 1)
5677 {
5678 result = 2;
5679 if (buf != NULL)
5680 STRCPY(buf, "''");
5681 }
5682 else
5683 result = 0;
5684 break;
5685 }
5686
5687 /* When specified there is a single argument don't split it.
5688 * Works for ":Cmd %" when % is "a b c". */
5689 if ((eap->argt & NOSPC) && quote == 2)
5690 quote = 1;
5691
5692 switch (quote)
5693 {
5694 case 0: /* No quoting, no splitting */
5695 result = STRLEN(eap->arg);
5696 if (buf != NULL)
5697 STRCPY(buf, eap->arg);
5698 break;
5699 case 1: /* Quote, but don't split */
5700 result = STRLEN(eap->arg) + 2;
5701 for (p = eap->arg; *p; ++p)
5702 {
5703 if (*p == '\\' || *p == '"')
5704 ++result;
5705 }
5706
5707 if (buf != NULL)
5708 {
5709 *buf++ = '"';
5710 for (p = eap->arg; *p; ++p)
5711 {
5712 if (*p == '\\' || *p == '"')
5713 *buf++ = '\\';
5714 *buf++ = *p;
5715 }
5716 *buf = '"';
5717 }
5718
5719 break;
5720 case 2: /* Quote and split */
5721 /* This is hard, so only do it once, and cache the result */
5722 if (*split_buf == NULL)
5723 *split_buf = uc_split_args(eap->arg, split_len);
5724
5725 result = *split_len;
5726 if (buf != NULL && result != 0)
5727 STRCPY(buf, *split_buf);
5728
5729 break;
5730 }
5731 break;
5732
5733 case ct_BANG:
5734 result = eap->forceit ? 1 : 0;
5735 if (quote)
5736 result += 2;
5737 if (buf != NULL)
5738 {
5739 if (quote)
5740 *buf++ = '"';
5741 if (eap->forceit)
5742 *buf++ = '!';
5743 if (quote)
5744 *buf = '"';
5745 }
5746 break;
5747
5748 case ct_LINE1:
5749 case ct_LINE2:
5750 case ct_COUNT:
5751 {
5752 char num_buf[20];
5753 long num = (type == ct_LINE1) ? eap->line1 :
5754 (type == ct_LINE2) ? eap->line2 :
5755 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5756 size_t num_len;
5757
5758 sprintf(num_buf, "%ld", num);
5759 num_len = STRLEN(num_buf);
5760 result = num_len;
5761
5762 if (quote)
5763 result += 2;
5764
5765 if (buf != NULL)
5766 {
5767 if (quote)
5768 *buf++ = '"';
5769 STRCPY(buf, num_buf);
5770 buf += num_len;
5771 if (quote)
5772 *buf = '"';
5773 }
5774
5775 break;
5776 }
5777
5778 case ct_REGISTER:
5779 result = eap->regname ? 1 : 0;
5780 if (quote)
5781 result += 2;
5782 if (buf != NULL)
5783 {
5784 if (quote)
5785 *buf++ = '\'';
5786 if (eap->regname)
5787 *buf++ = eap->regname;
5788 if (quote)
5789 *buf = '\'';
5790 }
5791 break;
5792
5793 case ct_LT:
5794 result = 1;
5795 if (buf != NULL)
5796 *buf = '<';
5797 break;
5798
5799 default:
5800 /* Not recognized: just copy the '<' and return -1. */
5801 result = (size_t)-1;
5802 if (buf != NULL)
5803 *buf = '<';
5804 break;
5805 }
5806
5807 return result;
5808}
5809
5810 static void
5811do_ucmd(eap)
5812 exarg_T *eap;
5813{
5814 char_u *buf;
5815 char_u *p;
5816 char_u *q;
5817
5818 char_u *start;
5819 char_u *end;
5820 size_t len, totlen;
5821
5822 size_t split_len = 0;
5823 char_u *split_buf = NULL;
5824 ucmd_T *cmd;
5825#ifdef FEAT_EVAL
5826 scid_T save_current_SID = current_SID;
5827#endif
5828
5829 if (eap->cmdidx == CMD_USER)
5830 cmd = USER_CMD(eap->useridx);
5831 else
5832 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5833
5834 /*
5835 * Replace <> in the command by the arguments.
5836 */
5837 buf = NULL;
5838 for (;;)
5839 {
5840 p = cmd->uc_rep;
5841 q = buf;
5842 totlen = 0;
5843 while ((start = vim_strchr(p, '<')) != NULL
5844 && (end = vim_strchr(start + 1, '>')) != NULL)
5845 {
5846 /* Include the '>' */
5847 ++end;
5848
5849 /* Take everything up to the '<' */
5850 len = start - p;
5851 if (buf == NULL)
5852 totlen += len;
5853 else
5854 {
5855 mch_memmove(q, p, len);
5856 q += len;
5857 }
5858
5859 len = uc_check_code(start, end - start, q, cmd, eap,
5860 &split_buf, &split_len);
5861 if (len == (size_t)-1)
5862 {
5863 /* no match, continue after '<' */
5864 p = start + 1;
5865 len = 1;
5866 }
5867 else
5868 p = end;
5869 if (buf == NULL)
5870 totlen += len;
5871 else
5872 q += len;
5873 }
5874 if (buf != NULL) /* second time here, finished */
5875 {
5876 STRCPY(q, p);
5877 break;
5878 }
5879
5880 totlen += STRLEN(p); /* Add on the trailing characters */
5881 buf = alloc((unsigned)(totlen + 1));
5882 if (buf == NULL)
5883 {
5884 vim_free(split_buf);
5885 return;
5886 }
5887 }
5888
5889#ifdef FEAT_EVAL
5890 current_SID = cmd->uc_scriptID;
5891#endif
5892 (void)do_cmdline(buf, eap->getline, eap->cookie,
5893 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5894#ifdef FEAT_EVAL
5895 current_SID = save_current_SID;
5896#endif
5897 vim_free(buf);
5898 vim_free(split_buf);
5899}
5900
5901# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5902 static char_u *
5903get_user_command_name(idx)
5904 int idx;
5905{
5906 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5907}
5908
5909/*
5910 * Function given to ExpandGeneric() to obtain the list of user command names.
5911 */
5912/*ARGSUSED*/
5913 char_u *
5914get_user_commands(xp, idx)
5915 expand_T *xp;
5916 int idx;
5917{
5918 if (idx < curbuf->b_ucmds.ga_len)
5919 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5920 idx -= curbuf->b_ucmds.ga_len;
5921 if (idx < ucmds.ga_len)
5922 return USER_CMD(idx)->uc_name;
5923 return NULL;
5924}
5925
5926/*
5927 * Function given to ExpandGeneric() to obtain the list of user command
5928 * attributes.
5929 */
5930/*ARGSUSED*/
5931 char_u *
5932get_user_cmd_flags(xp, idx)
5933 expand_T *xp;
5934 int idx;
5935{
5936 static char *user_cmd_flags[] =
5937 {"bang", "bar", "buffer", "complete", "count",
5938 "nargs", "range", "register"};
5939
5940 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5941 return NULL;
5942 return (char_u *)user_cmd_flags[idx];
5943}
5944
5945/*
5946 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5947 */
5948/*ARGSUSED*/
5949 char_u *
5950get_user_cmd_nargs(xp, idx)
5951 expand_T *xp;
5952 int idx;
5953{
5954 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5955
5956 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5957 return NULL;
5958 return (char_u *)user_cmd_nargs[idx];
5959}
5960
5961/*
5962 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5963 */
5964/*ARGSUSED*/
5965 char_u *
5966get_user_cmd_complete(xp, idx)
5967 expand_T *xp;
5968 int idx;
5969{
5970 return (char_u *)command_complete[idx].name;
5971}
5972# endif /* FEAT_CMDL_COMPL */
5973
5974#endif /* FEAT_USR_CMDS */
5975
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005976#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5977/*
5978 * Parse a completion argument "value[vallen]".
5979 * The detected completion goes in "*complp", argument type in "*argt".
5980 * When there is an argument, for function and user defined completion, it's
5981 * copied to allocated memory and stored in "*compl_arg".
5982 * Returns FAIL if something is wrong.
5983 */
5984 int
5985parse_compl_arg(value, vallen, complp, argt, compl_arg)
5986 char_u *value;
5987 int vallen;
5988 int *complp;
5989 long *argt;
5990 char_u **compl_arg;
5991{
5992 char_u *arg = NULL;
5993 size_t arglen = 0;
5994 int i;
5995 int valend = vallen;
5996
5997 /* Look for any argument part - which is the part after any ',' */
5998 for (i = 0; i < vallen; ++i)
5999 {
6000 if (value[i] == ',')
6001 {
6002 arg = &value[i + 1];
6003 arglen = vallen - i - 1;
6004 valend = i;
6005 break;
6006 }
6007 }
6008
6009 for (i = 0; command_complete[i].expand != 0; ++i)
6010 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006011 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006012 && STRNCMP(value, command_complete[i].name, valend) == 0)
6013 {
6014 *complp = command_complete[i].expand;
6015 if (command_complete[i].expand == EXPAND_BUFFERS)
6016 *argt |= BUFNAME;
6017 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6018 || command_complete[i].expand == EXPAND_FILES)
6019 *argt |= XFILE;
6020 break;
6021 }
6022 }
6023
6024 if (command_complete[i].expand == 0)
6025 {
6026 EMSG2(_("E180: Invalid complete value: %s"), value);
6027 return FAIL;
6028 }
6029
6030# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6031 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6032 && arg != NULL)
6033# else
6034 if (arg != NULL)
6035# endif
6036 {
6037 EMSG(_("E468: Completion argument only allowed for custom completion"));
6038 return FAIL;
6039 }
6040
6041# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6042 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6043 && arg == NULL)
6044 {
6045 EMSG(_("E467: Custom completion requires a function argument"));
6046 return FAIL;
6047 }
6048
6049 if (arg != NULL)
6050 *compl_arg = vim_strnsave(arg, (int)arglen);
6051# endif
6052 return OK;
6053}
6054#endif
6055
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 static void
6057ex_colorscheme(eap)
6058 exarg_T *eap;
6059{
6060 if (load_colors(eap->arg) == FAIL)
6061 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6062}
6063
6064 static void
6065ex_highlight(eap)
6066 exarg_T *eap;
6067{
6068 if (*eap->arg == NUL && eap->cmd[2] == '!')
6069 MSG(_("Greetings, Vim user!"));
6070 do_highlight(eap->arg, eap->forceit, FALSE);
6071}
6072
6073
6074/*
6075 * Call this function if we thought we were going to exit, but we won't
6076 * (because of an error). May need to restore the terminal mode.
6077 */
6078 void
6079not_exiting()
6080{
6081 exiting = FALSE;
6082 settmode(TMODE_RAW);
6083}
6084
6085/*
6086 * ":quit": quit current window, quit Vim if closed the last window.
6087 */
6088 static void
6089ex_quit(eap)
6090 exarg_T *eap;
6091{
6092#ifdef FEAT_CMDWIN
6093 if (cmdwin_type != 0)
6094 {
6095 cmdwin_result = Ctrl_C;
6096 return;
6097 }
6098#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006099 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006100 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006101 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006102 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006103 return;
6104 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006105#ifdef FEAT_AUTOCMD
6106 if (curbuf_locked())
6107 return;
6108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
6110#ifdef FEAT_NETBEANS_INTG
6111 netbeansForcedQuit = eap->forceit;
6112#endif
6113
6114 /*
6115 * If there are more files or windows we won't exit.
6116 */
6117 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6118 exiting = TRUE;
6119 if ((!P_HID(curbuf)
6120 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6121 || check_more(TRUE, eap->forceit) == FAIL
6122 || (only_one_window() && check_changed_any(eap->forceit)))
6123 {
6124 not_exiting();
6125 }
6126 else
6127 {
6128#ifdef FEAT_WINDOWS
6129 if (only_one_window()) /* quit last window */
6130#endif
6131 getout(0);
6132#ifdef FEAT_WINDOWS
6133# ifdef FEAT_GUI
6134 need_mouse_correct = TRUE;
6135# endif
6136 /* close window; may free buffer */
6137 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6138#endif
6139 }
6140}
6141
6142/*
6143 * ":cquit".
6144 */
6145/*ARGSUSED*/
6146 static void
6147ex_cquit(eap)
6148 exarg_T *eap;
6149{
6150 getout(1); /* this does not always pass on the exit code to the Manx
6151 compiler. why? */
6152}
6153
6154/*
6155 * ":qall": try to quit all windows
6156 */
6157 static void
6158ex_quit_all(eap)
6159 exarg_T *eap;
6160{
6161# ifdef FEAT_CMDWIN
6162 if (cmdwin_type != 0)
6163 {
6164 if (eap->forceit)
6165 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6166 else
6167 cmdwin_result = K_XF2;
6168 return;
6169 }
6170# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006171
6172 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006173 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006174 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006175 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006176 return;
6177 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006178#ifdef FEAT_AUTOCMD
6179 if (curbuf_locked())
6180 return;
6181#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006182
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 exiting = TRUE;
6184 if (eap->forceit || !check_changed_any(FALSE))
6185 getout(0);
6186 not_exiting();
6187}
6188
Bram Moolenaard9967712006-03-11 21:18:15 +00006189#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190/*
6191 * ":close": close current window, unless it is the last one
6192 */
6193 static void
6194ex_close(eap)
6195 exarg_T *eap;
6196{
6197# ifdef FEAT_CMDWIN
6198 if (cmdwin_type != 0)
6199 cmdwin_result = K_IGNORE;
6200 else
6201# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006202 if (!text_locked()
6203#ifdef FEAT_AUTOCMD
6204 && !curbuf_locked()
6205#endif
6206 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006207 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208}
6209
Bram Moolenaard9967712006-03-11 21:18:15 +00006210# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211/*
6212 * ":pclose": Close any preview window.
6213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006215ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006217{
6218 win_T *win;
6219
6220 for (win = firstwin; win != NULL; win = win->w_next)
6221 if (win->w_p_pvw)
6222 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006223 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224 break;
6225 }
6226}
Bram Moolenaard9967712006-03-11 21:18:15 +00006227# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006228
Bram Moolenaarf740b292006-02-16 22:11:02 +00006229/*
6230 * Close window "win" and take care of handling closing the last window for a
6231 * modified buffer.
6232 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006233 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006234ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006235 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006237 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
6239 int need_hide;
6240 buf_T *buf = win->w_buffer;
6241
6242 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006245# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 if ((p_confirm || cmdmod.confirm) && p_write)
6247 {
6248 dialog_changed(buf, FALSE);
6249 if (buf_valid(buf) && bufIsChanged(buf))
6250 return;
6251 need_hide = FALSE;
6252 }
6253 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
6256 EMSG(_(e_nowrtmsg));
6257 return;
6258 }
6259 }
6260
Bram Moolenaard9967712006-03-11 21:18:15 +00006261# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006263# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006264
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006266 if (tp == NULL)
6267 win_close(win, !need_hide && !P_HID(buf));
6268 else
6269 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270}
6271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006273 * ":tabclose": close current tab page, unless it is the last one.
6274 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 */
6276 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006277ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 exarg_T *eap;
6279{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006280 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006281 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006282
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006283# ifdef FEAT_CMDWIN
6284 if (cmdwin_type != 0)
6285 cmdwin_result = K_IGNORE;
6286 else
6287# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006288 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006289 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006290 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006292 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006293 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006294 tp = find_tabpage((int)eap->line2);
6295 if (tp == NULL)
6296 {
6297 beep_flush();
6298 return;
6299 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006300 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006301 {
6302 tabpage_close_other(tp, eap->forceit);
6303 return;
6304 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006305 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006306 if (!text_locked()
6307#ifdef FEAT_AUTOCMD
6308 && !curbuf_locked()
6309#endif
6310 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006311 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006313
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006314 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006315 shell_new_rows();
6316}
6317
6318/*
6319 * ":tabonly": close all tab pages except the current one
6320 */
6321 static void
6322ex_tabonly(eap)
6323 exarg_T *eap;
6324{
6325 tabpage_T *tp;
6326 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006327 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006328
6329# ifdef FEAT_CMDWIN
6330 if (cmdwin_type != 0)
6331 cmdwin_result = K_IGNORE;
6332 else
6333# endif
6334 if (first_tabpage->tp_next == NULL)
6335 MSG(_("Already only one tab page"));
6336 else
6337 {
6338 /* Repeat this up to a 1000 times, because autocommands may mess
6339 * up the lists. */
6340 for (done = 0; done < 1000; ++done)
6341 {
6342 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6343 if (tp->tp_topframe != topframe)
6344 {
6345 tabpage_close_other(tp, eap->forceit);
6346 /* if we failed to close it quit */
6347 if (valid_tabpage(tp))
6348 done = 1000;
6349 /* start over, "tp" is now invalid */
6350 break;
6351 }
6352 if (first_tabpage->tp_next == NULL)
6353 break;
6354 }
6355 }
6356
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006357 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006358 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
6361/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006362 * Close the current tab page.
6363 */
6364 void
6365tabpage_close(forceit)
6366 int forceit;
6367{
6368 /* First close all the windows but the current one. If that worked then
6369 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006370 if (lastwin != firstwin)
6371 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006372 if (lastwin == firstwin)
6373 ex_win_close(forceit, curwin, NULL);
6374# ifdef FEAT_GUI
6375 need_mouse_correct = TRUE;
6376# endif
6377}
6378
6379/*
6380 * Close tab page "tp", which is not the current tab page.
6381 * Note that autocommands may make "tp" invalid.
6382 */
6383 void
6384tabpage_close_other(tp, forceit)
6385 tabpage_T *tp;
6386 int forceit;
6387{
6388 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006389 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006390
6391 /* Limit to 1000 windows, autocommands may add a window while we close
6392 * one. OK, so I'm paranoid... */
6393 while (++done < 1000)
6394 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006395 wp = tp->tp_firstwin;
6396 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006397
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006398 /* Autocommands may delete the tab page under our fingers and we may
6399 * fail to close a window with a modified buffer. */
6400 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006401 break;
6402 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006403 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006404}
6405
6406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 * ":only".
6408 */
6409 static void
6410ex_only(eap)
6411 exarg_T *eap;
6412{
6413# ifdef FEAT_GUI
6414 need_mouse_correct = TRUE;
6415# endif
6416 close_others(TRUE, eap->forceit);
6417}
6418
6419/*
6420 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006421 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006423 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424ex_all(eap)
6425 exarg_T *eap;
6426{
6427 if (eap->addr_count == 0)
6428 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006429 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430}
6431#endif /* FEAT_WINDOWS */
6432
6433 static void
6434ex_hide(eap)
6435 exarg_T *eap;
6436{
6437 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6438 eap->errmsg = e_invarg;
6439 else
6440 {
6441 /* ":hide" or ":hide | cmd": hide current window */
6442 eap->nextcmd = check_nextcmd(eap->arg);
6443#ifdef FEAT_WINDOWS
6444 if (!eap->skip)
6445 {
6446# ifdef FEAT_GUI
6447 need_mouse_correct = TRUE;
6448# endif
6449 win_close(curwin, FALSE); /* don't free buffer */
6450 }
6451#endif
6452 }
6453}
6454
6455/*
6456 * ":stop" and ":suspend": Suspend Vim.
6457 */
6458 static void
6459ex_stop(eap)
6460 exarg_T *eap;
6461{
6462 /*
6463 * Disallow suspending for "rvim".
6464 */
6465 if (!check_restricted()
6466#ifdef WIN3264
6467 /*
6468 * Check if external commands are allowed now.
6469 */
6470 && can_end_termcap_mode(TRUE)
6471#endif
6472 )
6473 {
6474 if (!eap->forceit)
6475 autowrite_all();
6476 windgoto((int)Rows - 1, 0);
6477 out_char('\n');
6478 out_flush();
6479 stoptermcap();
6480 out_flush(); /* needed for SUN to restore xterm buffer */
6481#ifdef FEAT_TITLE
6482 mch_restore_title(3); /* restore window titles */
6483#endif
6484 ui_suspend(); /* call machine specific function */
6485#ifdef FEAT_TITLE
6486 maketitle();
6487 resettitle(); /* force updating the title */
6488#endif
6489 starttermcap();
6490 scroll_start(); /* scroll screen before redrawing */
6491 redraw_later_clear();
6492 shell_resized(); /* may have resized window */
6493 }
6494}
6495
6496/*
6497 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6498 */
6499 static void
6500ex_exit(eap)
6501 exarg_T *eap;
6502{
6503#ifdef FEAT_CMDWIN
6504 if (cmdwin_type != 0)
6505 {
6506 cmdwin_result = Ctrl_C;
6507 return;
6508 }
6509#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006510 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006511 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006512 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006513 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006514 return;
6515 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006516#ifdef FEAT_AUTOCMD
6517 if (curbuf_locked())
6518 return;
6519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
6521 /*
6522 * if more files or windows we won't exit
6523 */
6524 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6525 exiting = TRUE;
6526 if ( ((eap->cmdidx == CMD_wq
6527 || curbufIsChanged())
6528 && do_write(eap) == FAIL)
6529 || check_more(TRUE, eap->forceit) == FAIL
6530 || (only_one_window() && check_changed_any(eap->forceit)))
6531 {
6532 not_exiting();
6533 }
6534 else
6535 {
6536#ifdef FEAT_WINDOWS
6537 if (only_one_window()) /* quit last window, exit Vim */
6538#endif
6539 getout(0);
6540#ifdef FEAT_WINDOWS
6541# ifdef FEAT_GUI
6542 need_mouse_correct = TRUE;
6543# endif
6544 /* quit current window, may free buffer */
6545 win_close(curwin, !P_HID(curwin->w_buffer));
6546#endif
6547 }
6548}
6549
6550/*
6551 * ":print", ":list", ":number".
6552 */
6553 static void
6554ex_print(eap)
6555 exarg_T *eap;
6556{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006557 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6558 EMSG(_(e_emptybuf));
6559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006561 for ( ;!got_int; ui_breakcheck())
6562 {
6563 print_line(eap->line1,
6564 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6565 || (eap->flags & EXFLAG_NR)),
6566 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6567 if (++eap->line1 > eap->line2)
6568 break;
6569 out_flush(); /* show one line at a time */
6570 }
6571 setpcmark();
6572 /* put cursor at last line */
6573 curwin->w_cursor.lnum = eap->line2;
6574 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 }
6576
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578}
6579
6580#ifdef FEAT_BYTEOFF
6581 static void
6582ex_goto(eap)
6583 exarg_T *eap;
6584{
6585 goto_byte(eap->line2);
6586}
6587#endif
6588
6589/*
6590 * ":shell".
6591 */
6592/*ARGSUSED*/
6593 static void
6594ex_shell(eap)
6595 exarg_T *eap;
6596{
6597 do_shell(NULL, 0);
6598}
6599
6600#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6601 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006602 || defined(FEAT_GUI_MSWIN) \
6603 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 || defined(PROTO)
6605
6606/*
6607 * Handle a file drop. The code is here because a drop is *nearly* like an
6608 * :args command, but not quite (we have a list of exact filenames, so we
6609 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6610 * code is very similar to :args and hence needs access to a lot of the static
6611 * functions in this file.
6612 *
6613 * The list should be allocated using alloc(), as should each item in the
6614 * list. This function takes over responsibility for freeing the list.
6615 *
6616 * XXX The list is made into the arggument list. This is freed using
6617 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6618 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6619 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6620 * file functionality is (currently) not in EMX this is not presently a
6621 * problem.
6622 */
6623 void
6624handle_drop(filec, filev, split)
6625 int filec; /* the number of files dropped */
6626 char_u **filev; /* the list of files dropped */
6627 int split; /* force splitting the window */
6628{
6629 exarg_T ea;
6630 int save_msg_scroll = msg_scroll;
6631
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006632 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006633 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006635#ifdef FEAT_AUTOCMD
6636 if (curbuf_locked())
6637 return;
6638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639
6640 /* Check whether the current buffer is changed. If so, we will need
6641 * to split the current window or data could be lost.
6642 * We don't need to check if the 'hidden' option is set, as in this
6643 * case the buffer won't be lost.
6644 */
6645 if (!P_HID(curbuf) && !split)
6646 {
6647 ++emsg_off;
6648 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6649 --emsg_off;
6650 }
6651 if (split)
6652 {
6653# ifdef FEAT_WINDOWS
6654 if (win_split(0, 0) == FAIL)
6655 return;
6656# ifdef FEAT_SCROLLBIND
6657 curwin->w_p_scb = FALSE;
6658# endif
6659
6660 /* When splitting the window, create a new alist. Otherwise the
6661 * existing one is overwritten. */
6662 alist_unlink(curwin->w_alist);
6663 alist_new();
6664# else
6665 return; /* can't split, always fail */
6666# endif
6667 }
6668
6669 /*
6670 * Set up the new argument list.
6671 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006672 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674 /*
6675 * Move to the first file.
6676 */
6677 /* Fake up a minimal "next" command for do_argfile() */
6678 vim_memset(&ea, 0, sizeof(ea));
6679 ea.cmd = (char_u *)"next";
6680 do_argfile(&ea, 0);
6681
6682 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6683 * mode that is not needed here. */
6684 need_start_insertmode = FALSE;
6685
6686 /* Restore msg_scroll, otherwise a following command may cause scrolling
6687 * unexpectedly. The screen will be redrawn by the caller, thus
6688 * msg_scroll being set by displaying a message is irrelevant. */
6689 msg_scroll = save_msg_scroll;
6690}
6691#endif
6692
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693/*
6694 * Clear an argument list: free all file names and reset it to zero entries.
6695 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006696 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697alist_clear(al)
6698 alist_T *al;
6699{
6700 while (--al->al_ga.ga_len >= 0)
6701 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6702 ga_clear(&al->al_ga);
6703}
6704
6705/*
6706 * Init an argument list.
6707 */
6708 void
6709alist_init(al)
6710 alist_T *al;
6711{
6712 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6713}
6714
6715#if defined(FEAT_WINDOWS) || defined(PROTO)
6716
6717/*
6718 * Remove a reference from an argument list.
6719 * Ignored when the argument list is the global one.
6720 * If the argument list is no longer used by any window, free it.
6721 */
6722 void
6723alist_unlink(al)
6724 alist_T *al;
6725{
6726 if (al != &global_alist && --al->al_refcount <= 0)
6727 {
6728 alist_clear(al);
6729 vim_free(al);
6730 }
6731}
6732
6733# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6734/*
6735 * Create a new argument list and use it for the current window.
6736 */
6737 void
6738alist_new()
6739{
6740 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6741 if (curwin->w_alist == NULL)
6742 {
6743 curwin->w_alist = &global_alist;
6744 ++global_alist.al_refcount;
6745 }
6746 else
6747 {
6748 curwin->w_alist->al_refcount = 1;
6749 alist_init(curwin->w_alist);
6750 }
6751}
6752# endif
6753#endif
6754
6755#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6756/*
6757 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006758 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6759 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 */
6761 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006762alist_expand(fnum_list, fnum_len)
6763 int *fnum_list;
6764 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006767 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 char_u **new_arg_files;
6769 int new_arg_file_count;
6770 char_u *save_p_su = p_su;
6771 int i;
6772
6773 /* Don't use 'suffixes' here. This should work like the shell did the
6774 * expansion. Also, the vimrc file isn't read yet, thus the user
6775 * can't set the options. */
6776 p_su = empty_option;
6777 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6778 if (old_arg_files != NULL)
6779 {
6780 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006781 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6782 old_arg_count = GARGCOUNT;
6783 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 &new_arg_file_count, &new_arg_files,
6785 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6786 && new_arg_file_count > 0)
6787 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006788 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6789 TRUE, fnum_list, fnum_len);
6790 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 }
6793 p_su = save_p_su;
6794}
6795#endif
6796
6797/*
6798 * Set the argument list for the current window.
6799 * Takes over the allocated files[] and the allocated fnames in it.
6800 */
6801 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006802alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 alist_T *al;
6804 int count;
6805 char_u **files;
6806 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006807 int *fnum_list;
6808 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809{
6810 int i;
6811
6812 alist_clear(al);
6813 if (ga_grow(&al->al_ga, count) == OK)
6814 {
6815 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006816 {
6817 if (got_int)
6818 {
6819 /* When adding many buffers this can take a long time. Allow
6820 * interrupting here. */
6821 while (i < count)
6822 vim_free(files[i++]);
6823 break;
6824 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006825
6826 /* May set buffer name of a buffer previously used for the
6827 * argument list, so that it's re-used by alist_add. */
6828 if (fnum_list != NULL && i < fnum_len)
6829 buf_set_name(fnum_list[i], files[i]);
6830
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006832 ui_breakcheck();
6833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 vim_free(files);
6835 }
6836 else
6837 FreeWild(count, files);
6838#ifdef FEAT_WINDOWS
6839 if (al == &global_alist)
6840#endif
6841 arg_had_last = FALSE;
6842}
6843
6844/*
6845 * Add file "fname" to argument list "al".
6846 * "fname" must have been allocated and "al" must have been checked for room.
6847 */
6848 void
6849alist_add(al, fname, set_fnum)
6850 alist_T *al;
6851 char_u *fname;
6852 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6853{
6854 if (fname == NULL) /* don't add NULL file names */
6855 return;
6856#ifdef BACKSLASH_IN_FILENAME
6857 slash_adjust(fname);
6858#endif
6859 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6860 if (set_fnum > 0)
6861 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6862 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6863 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864}
6865
6866#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6867/*
6868 * Adjust slashes in file names. Called after 'shellslash' was set.
6869 */
6870 void
6871alist_slash_adjust()
6872{
6873 int i;
6874# ifdef FEAT_WINDOWS
6875 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006876 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877# endif
6878
6879 for (i = 0; i < GARGCOUNT; ++i)
6880 if (GARGLIST[i].ae_fname != NULL)
6881 slash_adjust(GARGLIST[i].ae_fname);
6882# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006883 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 if (wp->w_alist != &global_alist)
6885 for (i = 0; i < WARGCOUNT(wp); ++i)
6886 if (WARGLIST(wp)[i].ae_fname != NULL)
6887 slash_adjust(WARGLIST(wp)[i].ae_fname);
6888# endif
6889}
6890#endif
6891
6892/*
6893 * ":preserve".
6894 */
6895/*ARGSUSED*/
6896 static void
6897ex_preserve(eap)
6898 exarg_T *eap;
6899{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006900 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 ml_preserve(curbuf, TRUE);
6902}
6903
6904/*
6905 * ":recover".
6906 */
6907 static void
6908ex_recover(eap)
6909 exarg_T *eap;
6910{
6911 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6912 recoverymode = TRUE;
6913 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6914 && (*eap->arg == NUL
6915 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6916 ml_recover();
6917 recoverymode = FALSE;
6918}
6919
6920/*
6921 * Command modifier used in a wrong way.
6922 */
6923 static void
6924ex_wrongmodifier(eap)
6925 exarg_T *eap;
6926{
6927 eap->errmsg = e_invcmd;
6928}
6929
6930#ifdef FEAT_WINDOWS
6931/*
6932 * :sview [+command] file split window with new file, read-only
6933 * :split [[+command] file] split window with current or new file
6934 * :vsplit [[+command] file] split window vertically with current or new file
6935 * :new [[+command] file] split window with no or new file
6936 * :vnew [[+command] file] split vertically window with no or new file
6937 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006938 *
6939 * :tabedit open new Tab page with empty window
6940 * :tabedit [+command] file open new Tab page and edit "file"
6941 * :tabnew [[+command] file] just like :tabedit
6942 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 */
6944 void
6945ex_splitview(eap)
6946 exarg_T *eap;
6947{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006948 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006949# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006951# endif
6952# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006956# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6958 {
6959 ex_ni(eap);
6960 return;
6961 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006962# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006964# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006968# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 /* A ":split" in the quickfix window works like ":new". Don't want two
6970 * quickfix windows. */
6971 if (bt_quickfix(curbuf))
6972 {
6973 if (eap->cmdidx == CMD_split)
6974 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006975# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if (eap->cmdidx == CMD_vsplit)
6977 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006982# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006983 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {
6985 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6986 FNAME_MESS, TRUE, curbuf->b_ffname);
6987 if (fname == NULL)
6988 goto theend;
6989 eap->arg = fname;
6990 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006991# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006995# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006997# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 && eap->cmdidx != CMD_new)
7001 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007002 if (
7003# ifdef FEAT_GUI
7004 !gui.in_use &&
7005# endif
7006 au_has_group((char_u *)"FileExplorer"))
7007 {
7008 /* No browsing supported but we do have the file explorer:
7009 * Edit the directory. */
7010 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7011 eap->arg = (char_u *)".";
7012 }
7013 else
7014 {
7015 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007017 if (fname == NULL)
7018 goto theend;
7019 eap->arg = fname;
7020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
7022 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007025 /*
7026 * Either open new tab page or split the window.
7027 */
7028 if (eap->cmdidx == CMD_tabedit
7029 || eap->cmdidx == CMD_tabfind
7030 || eap->cmdidx == CMD_tabnew)
7031 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007032 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7033 : eap->addr_count == 0 ? 0
7034 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007035 {
7036 do_exedit(eap, NULL);
7037
7038 /* set the alternate buffer for the window we came from */
7039 if (curwin != old_curwin
7040 && win_valid(old_curwin)
7041 && old_curwin->w_buffer != curbuf
7042 && !cmdmod.keepalt)
7043 old_curwin->w_alt_fnum = curbuf->b_fnum;
7044 }
7045 }
7046 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7048 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007049# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 /* Reset 'scrollbind' when editing another file, but keep it when
7051 * doing ":split" without arguments. */
7052 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007053# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 )
7057 curwin->w_p_scb = FALSE;
7058 else
7059 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 do_exedit(eap, old_curwin);
7062 }
7063
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007064# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007068# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069theend:
7070 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007073
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007074#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007075/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007076 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007077 */
7078 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007079tabpage_new()
7080{
7081 exarg_T ea;
7082
7083 vim_memset(&ea, 0, sizeof(ea));
7084 ea.cmdidx = CMD_tabnew;
7085 ea.cmd = (char_u *)"tabn";
7086 ea.arg = (char_u *)"";
7087 ex_splitview(&ea);
7088}
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007089#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007090
7091/*
7092 * :tabnext command
7093 */
7094 static void
7095ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007096 exarg_T *eap;
7097{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007098 switch (eap->cmdidx)
7099 {
7100 case CMD_tabfirst:
7101 case CMD_tabrewind:
7102 goto_tabpage(1);
7103 break;
7104 case CMD_tablast:
7105 goto_tabpage(9999);
7106 break;
7107 case CMD_tabprevious:
7108 case CMD_tabNext:
7109 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7110 break;
7111 default: /* CMD_tabnext */
7112 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7113 break;
7114 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007115}
7116
7117/*
7118 * :tabmove command
7119 */
7120 static void
7121ex_tabmove(eap)
7122 exarg_T *eap;
7123{
7124 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007125}
7126
7127/*
7128 * :tabs command: List tabs and their contents.
7129 */
7130/*ARGSUSED*/
7131 static void
7132ex_tabs(eap)
7133 exarg_T *eap;
7134{
7135 tabpage_T *tp;
7136 win_T *wp;
7137 int tabcount = 1;
7138
7139 msg_start();
7140 msg_scroll = TRUE;
7141 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7142 {
7143 msg_putchar('\n');
7144 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7145 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7146 out_flush(); /* output one line at a time */
7147 ui_breakcheck();
7148
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007149 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007150 wp = firstwin;
7151 else
7152 wp = tp->tp_firstwin;
7153 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7154 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007155 msg_putchar('\n');
7156 msg_putchar(wp == curwin ? '>' : ' ');
7157 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007158 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7159 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007160 if (buf_spname(wp->w_buffer) != NULL)
7161 STRCPY(IObuff, buf_spname(wp->w_buffer));
7162 else
7163 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7164 IObuff, IOSIZE, TRUE);
7165 msg_outtrans(IObuff);
7166 out_flush(); /* output one line at a time */
7167 ui_breakcheck();
7168 }
7169 }
7170}
7171
7172#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
7174/*
7175 * ":mode": Set screen mode.
7176 * If no argument given, just get the screen size and redraw.
7177 */
7178 static void
7179ex_mode(eap)
7180 exarg_T *eap;
7181{
7182 if (*eap->arg == NUL)
7183 shell_resized();
7184 else
7185 mch_screenmode(eap->arg);
7186}
7187
7188#ifdef FEAT_WINDOWS
7189/*
7190 * ":resize".
7191 * set, increment or decrement current window height
7192 */
7193 static void
7194ex_resize(eap)
7195 exarg_T *eap;
7196{
7197 int n;
7198 win_T *wp = curwin;
7199
7200 if (eap->addr_count > 0)
7201 {
7202 n = eap->line2;
7203 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7204 ;
7205 }
7206
7207#ifdef FEAT_GUI
7208 need_mouse_correct = TRUE;
7209#endif
7210 n = atol((char *)eap->arg);
7211#ifdef FEAT_VERTSPLIT
7212 if (cmdmod.split & WSP_VERT)
7213 {
7214 if (*eap->arg == '-' || *eap->arg == '+')
7215 n += W_WIDTH(curwin);
7216 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7217 n = 9999;
7218 win_setwidth_win((int)n, wp);
7219 }
7220 else
7221#endif
7222 {
7223 if (*eap->arg == '-' || *eap->arg == '+')
7224 n += curwin->w_height;
7225 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7226 n = 9999;
7227 win_setheight_win((int)n, wp);
7228 }
7229}
7230#endif
7231
7232/*
7233 * ":find [+command] <file>" command.
7234 */
7235 static void
7236ex_find(eap)
7237 exarg_T *eap;
7238{
7239#ifdef FEAT_SEARCHPATH
7240 char_u *fname;
7241 int count;
7242
7243 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7244 TRUE, curbuf->b_ffname);
7245 if (eap->addr_count > 0)
7246 {
7247 /* Repeat finding the file "count" times. This matters when it
7248 * appears several times in the path. */
7249 count = eap->line2;
7250 while (fname != NULL && --count > 0)
7251 {
7252 vim_free(fname);
7253 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7254 FALSE, curbuf->b_ffname);
7255 }
7256 }
7257
7258 if (fname != NULL)
7259 {
7260 eap->arg = fname;
7261#endif
7262 do_exedit(eap, NULL);
7263#ifdef FEAT_SEARCHPATH
7264 vim_free(fname);
7265 }
7266#endif
7267}
7268
7269/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007270 * ":open" simulation: for now just work like ":visual".
7271 */
7272 static void
7273ex_open(eap)
7274 exarg_T *eap;
7275{
7276 regmatch_T regmatch;
7277 char_u *p;
7278
7279 curwin->w_cursor.lnum = eap->line2;
7280 beginline(BL_SOL | BL_FIX);
7281 if (*eap->arg == '/')
7282 {
7283 /* ":open /pattern/": put cursor in column found with pattern */
7284 ++eap->arg;
7285 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7286 *p = NUL;
7287 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7288 if (regmatch.regprog != NULL)
7289 {
7290 regmatch.rm_ic = p_ic;
7291 p = ml_get_curline();
7292 if (vim_regexec(&regmatch, p, (colnr_T)0))
7293 curwin->w_cursor.col = regmatch.startp[0] - p;
7294 else
7295 EMSG(_(e_nomatch));
7296 vim_free(regmatch.regprog);
7297 }
7298 /* Move to the NUL, ignore any other arguments. */
7299 eap->arg += STRLEN(eap->arg);
7300 }
7301 check_cursor();
7302
7303 eap->cmdidx = CMD_visual;
7304 do_exedit(eap, NULL);
7305}
7306
7307/*
7308 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 */
7310 static void
7311ex_edit(eap)
7312 exarg_T *eap;
7313{
7314 do_exedit(eap, NULL);
7315}
7316
7317/*
7318 * ":edit <file>" command and alikes.
7319 */
7320/*ARGSUSED*/
7321 void
7322do_exedit(eap, old_curwin)
7323 exarg_T *eap;
7324 win_T *old_curwin; /* curwin before doing a split or NULL */
7325{
7326 int n;
7327#ifdef FEAT_WINDOWS
7328 int need_hide;
7329#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007330 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
7332 /*
7333 * ":vi" command ends Ex mode.
7334 */
7335 if (exmode_active && (eap->cmdidx == CMD_visual
7336 || eap->cmdidx == CMD_view))
7337 {
7338 exmode_active = FALSE;
7339 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007340 {
7341 /* Special case: ":global/pat/visual\NLvi-commands" */
7342 if (global_busy)
7343 {
7344 int rd = RedrawingDisabled;
7345 int nwr = no_wait_return;
7346 int ms = msg_scroll;
7347#ifdef FEAT_GUI
7348 int he = hold_gui_events;
7349#endif
7350
7351 if (eap->nextcmd != NULL)
7352 {
7353 stuffReadbuff(eap->nextcmd);
7354 eap->nextcmd = NULL;
7355 }
7356
7357 if (exmode_was != EXMODE_VIM)
7358 settmode(TMODE_RAW);
7359 RedrawingDisabled = 0;
7360 no_wait_return = 0;
7361 need_wait_return = FALSE;
7362 msg_scroll = 0;
7363#ifdef FEAT_GUI
7364 hold_gui_events = 0;
7365#endif
7366 must_redraw = CLEAR;
7367
7368 main_loop(FALSE, TRUE);
7369
7370 RedrawingDisabled = rd;
7371 no_wait_return = nwr;
7372 msg_scroll = ms;
7373#ifdef FEAT_GUI
7374 hold_gui_events = he;
7375#endif
7376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 }
7380
7381 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007382 || eap->cmdidx == CMD_tabnew
7383 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384#ifdef FEAT_VERTSPLIT
7385 || eap->cmdidx == CMD_vnew
7386#endif
7387 ) && *eap->arg == NUL)
7388 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007389 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 setpcmark();
7391 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7392 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7393 }
7394 else if ((eap->cmdidx != CMD_split
7395#ifdef FEAT_VERTSPLIT
7396 && eap->cmdidx != CMD_vsplit
7397#endif
7398 )
7399 || *eap->arg != NUL
7400#ifdef FEAT_BROWSE
7401 || cmdmod.browse
7402#endif
7403 )
7404 {
7405 n = readonlymode;
7406 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7407 readonlymode = TRUE;
7408 else if (eap->cmdidx == CMD_enew)
7409 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7410 empty buffer */
7411 setpcmark();
7412 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7413 NULL, eap,
7414 /* ":edit" goes to first line if Vi compatible */
7415 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7416 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7417 ? ECMD_ONE : eap->do_ecmd_lnum,
7418 (P_HID(curbuf) ? ECMD_HIDE : 0)
7419 + (eap->forceit ? ECMD_FORCEIT : 0)
7420#ifdef FEAT_LISTCMDS
7421 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7422#endif
7423 ) == FAIL)
7424 {
7425 /* Editing the file failed. If the window was split, close it. */
7426#ifdef FEAT_WINDOWS
7427 if (old_curwin != NULL)
7428 {
7429 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7430 if (!need_hide || P_HID(curbuf))
7431 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007432# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7433 cleanup_T cs;
7434
7435 /* Reset the error/interrupt/exception state here so that
7436 * aborting() returns FALSE when closing a window. */
7437 enter_cleanup(&cs);
7438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439# ifdef FEAT_GUI
7440 need_mouse_correct = TRUE;
7441# endif
7442 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007443
7444# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7445 /* Restore the error/interrupt/exception state if not
7446 * discarded by a new aborting error, interrupt, or
7447 * uncaught exception. */
7448 leave_cleanup(&cs);
7449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451 }
7452#endif
7453 }
7454 else if (readonlymode && curbuf->b_nwindows == 1)
7455 {
7456 /* When editing an already visited buffer, 'readonly' won't be set
7457 * but the previous value is kept. With ":view" and ":sview" we
7458 * want the file to be readonly, except when another window is
7459 * editing the same buffer. */
7460 curbuf->b_p_ro = TRUE;
7461 }
7462 readonlymode = n;
7463 }
7464 else
7465 {
7466 if (eap->do_ecmd_cmd != NULL)
7467 do_cmdline_cmd(eap->do_ecmd_cmd);
7468#ifdef FEAT_TITLE
7469 n = curwin->w_arg_idx_invalid;
7470#endif
7471 check_arg_idx(curwin);
7472#ifdef FEAT_TITLE
7473 if (n != curwin->w_arg_idx_invalid)
7474 maketitle();
7475#endif
7476 }
7477
7478#ifdef FEAT_WINDOWS
7479 /*
7480 * if ":split file" worked, set alternate file name in old window to new
7481 * file
7482 */
7483 if (old_curwin != NULL
7484 && *eap->arg != NUL
7485 && curwin != old_curwin
7486 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007487 && old_curwin->w_buffer != curbuf
7488 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 old_curwin->w_alt_fnum = curbuf->b_fnum;
7490#endif
7491
7492 ex_no_reprint = TRUE;
7493}
7494
7495#ifndef FEAT_GUI
7496/*
7497 * ":gui" and ":gvim" when there is no GUI.
7498 */
7499 static void
7500ex_nogui(eap)
7501 exarg_T *eap;
7502{
7503 eap->errmsg = e_nogvim;
7504}
7505#endif
7506
7507#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7508 static void
7509ex_tearoff(eap)
7510 exarg_T *eap;
7511{
7512 gui_make_tearoff(eap->arg);
7513}
7514#endif
7515
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007516#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 static void
7518ex_popup(eap)
7519 exarg_T *eap;
7520{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007521 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522}
7523#endif
7524
7525/*ARGSUSED*/
7526 static void
7527ex_swapname(eap)
7528 exarg_T *eap;
7529{
7530 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7531 MSG(_("No swap file"));
7532 else
7533 msg(curbuf->b_ml.ml_mfp->mf_fname);
7534}
7535
7536/*
7537 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7538 * offset.
7539 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7540 */
7541/*ARGSUSED*/
7542 static void
7543ex_syncbind(eap)
7544 exarg_T *eap;
7545{
7546#ifdef FEAT_SCROLLBIND
7547 win_T *wp;
7548 long topline;
7549 long y;
7550 linenr_T old_linenr = curwin->w_cursor.lnum;
7551
7552 setpcmark();
7553
7554 /*
7555 * determine max topline
7556 */
7557 if (curwin->w_p_scb)
7558 {
7559 topline = curwin->w_topline;
7560 for (wp = firstwin; wp; wp = wp->w_next)
7561 {
7562 if (wp->w_p_scb && wp->w_buffer)
7563 {
7564 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7565 if (topline > y)
7566 topline = y;
7567 }
7568 }
7569 if (topline < 1)
7570 topline = 1;
7571 }
7572 else
7573 {
7574 topline = 1;
7575 }
7576
7577
7578 /*
7579 * set all scrollbind windows to the same topline
7580 */
7581 wp = curwin;
7582 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7583 {
7584 if (curwin->w_p_scb)
7585 {
7586 y = topline - curwin->w_topline;
7587 if (y > 0)
7588 scrollup(y, TRUE);
7589 else
7590 scrolldown(-y, TRUE);
7591 curwin->w_scbind_pos = topline;
7592 redraw_later(VALID);
7593 cursor_correct();
7594#ifdef FEAT_WINDOWS
7595 curwin->w_redr_status = TRUE;
7596#endif
7597 }
7598 }
7599 curwin = wp;
7600 if (curwin->w_p_scb)
7601 {
7602 did_syncbind = TRUE;
7603 checkpcmark();
7604 if (old_linenr != curwin->w_cursor.lnum)
7605 {
7606 char_u ctrl_o[2];
7607
7608 ctrl_o[0] = Ctrl_O;
7609 ctrl_o[1] = 0;
7610 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7611 }
7612 }
7613#endif
7614}
7615
7616
7617 static void
7618ex_read(eap)
7619 exarg_T *eap;
7620{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007621 int i;
7622 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7623 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
7625 if (eap->usefilter) /* :r!cmd */
7626 do_bang(1, eap, FALSE, FALSE, TRUE);
7627 else
7628 {
7629 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7630 return;
7631
7632#ifdef FEAT_BROWSE
7633 if (cmdmod.browse)
7634 {
7635 char_u *browseFile;
7636
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007637 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 NULL, NULL, NULL, curbuf);
7639 if (browseFile != NULL)
7640 {
7641 i = readfile(browseFile, NULL,
7642 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7643 vim_free(browseFile);
7644 }
7645 else
7646 i = OK;
7647 }
7648 else
7649#endif
7650 if (*eap->arg == NUL)
7651 {
7652 if (check_fname() == FAIL) /* check for no file name */
7653 return;
7654 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7655 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7656 }
7657 else
7658 {
7659 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7660 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7661 i = readfile(eap->arg, NULL,
7662 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7663
7664 }
7665 if (i == FAIL)
7666 {
7667#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7668 if (!aborting())
7669#endif
7670 EMSG2(_(e_notopen), eap->arg);
7671 }
7672 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007673 {
7674 if (empty && exmode_active)
7675 {
7676 /* Delete the empty line that remains. Historically ex does
7677 * this but vi doesn't. */
7678 if (eap->line2 == 0)
7679 lnum = curbuf->b_ml.ml_line_count;
7680 else
7681 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007682 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007683 {
7684 ml_delete(lnum, FALSE);
7685 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007686 if (curwin->w_cursor.lnum > 1
7687 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007688 --curwin->w_cursor.lnum;
7689 }
7690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 }
7694}
7695
Bram Moolenaarea408852005-06-25 22:49:46 +00007696static char_u *prev_dir = NULL;
7697
7698#if defined(EXITFREE) || defined(PROTO)
7699 void
7700free_cd_dir()
7701{
7702 vim_free(prev_dir);
7703}
7704#endif
7705
7706
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707/*
7708 * ":cd", ":lcd", ":chdir" and ":lchdir".
7709 */
7710 static void
7711ex_cd(eap)
7712 exarg_T *eap;
7713{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 char_u *new_dir;
7715 char_u *tofree;
7716
7717 new_dir = eap->arg;
7718#if !defined(UNIX) && !defined(VMS)
7719 /* for non-UNIX ":cd" means: print current directory */
7720 if (*new_dir == NUL)
7721 ex_pwd(NULL);
7722 else
7723#endif
7724 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007725 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7726 && !eap->forceit)
7727 {
7728 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7729 return;
7730 }
7731
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 /* ":cd -": Change to previous directory */
7733 if (STRCMP(new_dir, "-") == 0)
7734 {
7735 if (prev_dir == NULL)
7736 {
7737 EMSG(_("E186: No previous directory"));
7738 return;
7739 }
7740 new_dir = prev_dir;
7741 }
7742
7743 /* Save current directory for next ":cd -" */
7744 tofree = prev_dir;
7745 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7746 prev_dir = vim_strsave(NameBuff);
7747 else
7748 prev_dir = NULL;
7749
7750#if defined(UNIX) || defined(VMS)
7751 /* for UNIX ":cd" means: go to home directory */
7752 if (*new_dir == NUL)
7753 {
7754 /* use NameBuff for home directory name */
7755# ifdef VMS
7756 char_u *p;
7757
7758 p = mch_getenv((char_u *)"SYS$LOGIN");
7759 if (p == NULL || *p == NUL) /* empty is the same as not set */
7760 NameBuff[0] = NUL;
7761 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007762 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763# else
7764 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7765# endif
7766 new_dir = NameBuff;
7767 }
7768#endif
7769 if (new_dir == NULL || vim_chdir(new_dir))
7770 EMSG(_(e_failed));
7771 else
7772 {
7773 vim_free(curwin->w_localdir);
7774 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7775 {
7776 /* If still in global directory, need to remember current
7777 * directory as global directory. */
7778 if (globaldir == NULL && prev_dir != NULL)
7779 globaldir = vim_strsave(prev_dir);
7780 /* Remember this local directory for the window. */
7781 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7782 curwin->w_localdir = vim_strsave(NameBuff);
7783 }
7784 else
7785 {
7786 /* We are now in the global directory, no need to remember its
7787 * name. */
7788 vim_free(globaldir);
7789 globaldir = NULL;
7790 curwin->w_localdir = NULL;
7791 }
7792
7793 shorten_fnames(TRUE);
7794
7795 /* Echo the new current directory if the command was typed. */
7796 if (KeyTyped)
7797 ex_pwd(eap);
7798 }
7799 vim_free(tofree);
7800 }
7801}
7802
7803/*
7804 * ":pwd".
7805 */
7806/*ARGSUSED*/
7807 static void
7808ex_pwd(eap)
7809 exarg_T *eap;
7810{
7811 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7812 {
7813#ifdef BACKSLASH_IN_FILENAME
7814 slash_adjust(NameBuff);
7815#endif
7816 msg(NameBuff);
7817 }
7818 else
7819 EMSG(_("E187: Unknown"));
7820}
7821
7822/*
7823 * ":=".
7824 */
7825 static void
7826ex_equal(eap)
7827 exarg_T *eap;
7828{
7829 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007830 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831}
7832
7833 static void
7834ex_sleep(eap)
7835 exarg_T *eap;
7836{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007837 int n;
7838 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839
7840 if (cursor_valid())
7841 {
7842 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7843 if (n >= 0)
7844 windgoto((int)n, curwin->w_wcol);
7845 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007846
7847 len = eap->line2;
7848 switch (*eap->arg)
7849 {
7850 case 'm': break;
7851 case NUL: len *= 1000L; break;
7852 default: EMSG2(_(e_invarg2), eap->arg); return;
7853 }
7854 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855}
7856
7857/*
7858 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7859 */
7860 void
7861do_sleep(msec)
7862 long msec;
7863{
7864 long done;
7865
7866 cursor_on();
7867 out_flush();
7868 for (done = 0; !got_int && done < msec; done += 1000L)
7869 {
7870 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7871 ui_breakcheck();
7872 }
7873}
7874
7875 static void
7876do_exmap(eap, isabbrev)
7877 exarg_T *eap;
7878 int isabbrev;
7879{
7880 int mode;
7881 char_u *cmdp;
7882
7883 cmdp = eap->cmd;
7884 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7885
7886 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7887 eap->arg, mode, isabbrev))
7888 {
7889 case 1: EMSG(_(e_invarg));
7890 break;
7891 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7892 break;
7893 }
7894}
7895
7896/*
7897 * ":winsize" command (obsolete).
7898 */
7899 static void
7900ex_winsize(eap)
7901 exarg_T *eap;
7902{
7903 int w, h;
7904 char_u *arg = eap->arg;
7905 char_u *p;
7906
7907 w = getdigits(&arg);
7908 arg = skipwhite(arg);
7909 p = arg;
7910 h = getdigits(&arg);
7911 if (*p != NUL && *arg == NUL)
7912 set_shellsize(w, h, TRUE);
7913 else
7914 EMSG(_("E465: :winsize requires two number arguments"));
7915}
7916
7917#ifdef FEAT_WINDOWS
7918 static void
7919ex_wincmd(eap)
7920 exarg_T *eap;
7921{
7922 int xchar = NUL;
7923 char_u *p;
7924
7925 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7926 {
7927 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7928 if (eap->arg[1] == NUL)
7929 {
7930 EMSG(_(e_invarg));
7931 return;
7932 }
7933 xchar = eap->arg[1];
7934 p = eap->arg + 2;
7935 }
7936 else
7937 p = eap->arg + 1;
7938
7939 eap->nextcmd = check_nextcmd(p);
7940 p = skipwhite(p);
7941 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7942 EMSG(_(e_invarg));
7943 else
7944 {
7945 /* Pass flags on for ":vertical wincmd ]". */
7946 postponed_split_flags = cmdmod.split;
7947 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7948 postponed_split_flags = 0;
7949 }
7950}
7951#endif
7952
Bram Moolenaar843ee412004-06-30 16:16:41 +00007953#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954/*
7955 * ":winpos".
7956 */
7957 static void
7958ex_winpos(eap)
7959 exarg_T *eap;
7960{
7961 int x, y;
7962 char_u *arg = eap->arg;
7963 char_u *p;
7964
7965 if (*arg == NUL)
7966 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007967# if defined(FEAT_GUI) || defined(MSWIN)
7968# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007970# else
7971 if (mch_get_winpos(&x, &y) != FAIL)
7972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {
7974 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7975 msg(IObuff);
7976 }
7977 else
7978# endif
7979 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7980 }
7981 else
7982 {
7983 x = getdigits(&arg);
7984 arg = skipwhite(arg);
7985 p = arg;
7986 y = getdigits(&arg);
7987 if (*p == NUL || *arg != NUL)
7988 {
7989 EMSG(_("E466: :winpos requires two number arguments"));
7990 return;
7991 }
7992# ifdef FEAT_GUI
7993 if (gui.in_use)
7994 gui_mch_set_winpos(x, y);
7995 else if (gui.starting)
7996 {
7997 /* Remember the coordinates for when the window is opened. */
7998 gui_win_x = x;
7999 gui_win_y = y;
8000 }
8001# ifdef HAVE_TGETENT
8002 else
8003# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008004# else
8005# ifdef MSWIN
8006 mch_set_winpos(x, y);
8007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008# endif
8009# ifdef HAVE_TGETENT
8010 if (*T_CWP)
8011 term_set_winpos(x, y);
8012# endif
8013 }
8014}
8015#endif
8016
8017/*
8018 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8019 */
8020 static void
8021ex_operators(eap)
8022 exarg_T *eap;
8023{
8024 oparg_T oa;
8025
8026 clear_oparg(&oa);
8027 oa.regname = eap->regname;
8028 oa.start.lnum = eap->line1;
8029 oa.end.lnum = eap->line2;
8030 oa.line_count = eap->line2 - eap->line1 + 1;
8031 oa.motion_type = MLINE;
8032#ifdef FEAT_VIRTUALEDIT
8033 virtual_op = FALSE;
8034#endif
8035 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8036 {
8037 setpcmark();
8038 curwin->w_cursor.lnum = eap->line1;
8039 beginline(BL_SOL | BL_FIX);
8040 }
8041
8042 switch (eap->cmdidx)
8043 {
8044 case CMD_delete:
8045 oa.op_type = OP_DELETE;
8046 op_delete(&oa);
8047 break;
8048
8049 case CMD_yank:
8050 oa.op_type = OP_YANK;
8051 (void)op_yank(&oa, FALSE, TRUE);
8052 break;
8053
8054 default: /* CMD_rshift or CMD_lshift */
8055 if ((eap->cmdidx == CMD_rshift)
8056#ifdef FEAT_RIGHTLEFT
8057 ^ curwin->w_p_rl
8058#endif
8059 )
8060 oa.op_type = OP_RSHIFT;
8061 else
8062 oa.op_type = OP_LSHIFT;
8063 op_shift(&oa, FALSE, eap->amount);
8064 break;
8065 }
8066#ifdef FEAT_VIRTUALEDIT
8067 virtual_op = MAYBE;
8068#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008069 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070}
8071
8072/*
8073 * ":put".
8074 */
8075 static void
8076ex_put(eap)
8077 exarg_T *eap;
8078{
8079 /* ":0put" works like ":1put!". */
8080 if (eap->line2 == 0)
8081 {
8082 eap->line2 = 1;
8083 eap->forceit = TRUE;
8084 }
8085 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008086 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8087 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088}
8089
8090/*
8091 * Handle ":copy" and ":move".
8092 */
8093 static void
8094ex_copymove(eap)
8095 exarg_T *eap;
8096{
8097 long n;
8098
8099 n = get_address(&eap->arg, FALSE, FALSE);
8100 if (eap->arg == NULL) /* error detected */
8101 {
8102 eap->nextcmd = NULL;
8103 return;
8104 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008105 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106
8107 /*
8108 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8109 */
8110 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8111 {
8112 EMSG(_(e_invaddr));
8113 return;
8114 }
8115
8116 if (eap->cmdidx == CMD_move)
8117 {
8118 if (do_move(eap->line1, eap->line2, n) == FAIL)
8119 return;
8120 }
8121 else
8122 ex_copy(eap->line1, eap->line2, n);
8123 u_clearline();
8124 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008125 ex_may_print(eap);
8126}
8127
8128/*
8129 * Print the current line if flags were given to the Ex command.
8130 */
8131 static void
8132ex_may_print(eap)
8133 exarg_T *eap;
8134{
8135 if (eap->flags != 0)
8136 {
8137 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8138 (eap->flags & EXFLAG_LIST));
8139 ex_no_reprint = TRUE;
8140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141}
8142
8143/*
8144 * ":smagic" and ":snomagic".
8145 */
8146 static void
8147ex_submagic(eap)
8148 exarg_T *eap;
8149{
8150 int magic_save = p_magic;
8151
8152 p_magic = (eap->cmdidx == CMD_smagic);
8153 do_sub(eap);
8154 p_magic = magic_save;
8155}
8156
8157/*
8158 * ":join".
8159 */
8160 static void
8161ex_join(eap)
8162 exarg_T *eap;
8163{
8164 curwin->w_cursor.lnum = eap->line1;
8165 if (eap->line1 == eap->line2)
8166 {
8167 if (eap->addr_count >= 2) /* :2,2join does nothing */
8168 return;
8169 if (eap->line2 == curbuf->b_ml.ml_line_count)
8170 {
8171 beep_flush();
8172 return;
8173 }
8174 ++eap->line2;
8175 }
8176 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8177 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008178 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179}
8180
8181/*
8182 * ":[addr]@r" or ":[addr]*r": execute register
8183 */
8184 static void
8185ex_at(eap)
8186 exarg_T *eap;
8187{
8188 int c;
8189
8190 curwin->w_cursor.lnum = eap->line2;
8191
8192#ifdef USE_ON_FLY_SCROLL
8193 dont_scroll = TRUE; /* disallow scrolling here */
8194#endif
8195
8196 /* get the register name. No name means to use the previous one */
8197 c = *eap->arg;
8198 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8199 c = '@';
8200 /* put the register in mapbuf */
8201 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 else
8206 {
8207 int save_efr = exec_from_reg;
8208
8209 exec_from_reg = TRUE;
8210
8211 /*
8212 * Execute from the typeahead buffer.
8213 * Originally this didn't check for the typeahead buffer to be empty,
8214 * thus could read more Ex commands from stdin. It's not clear why,
8215 * it is certainly unexpected.
8216 */
8217 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8218 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8219
8220 exec_from_reg = save_efr;
8221 }
8222}
8223
8224/*
8225 * ":!".
8226 */
8227 static void
8228ex_bang(eap)
8229 exarg_T *eap;
8230{
8231 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8232}
8233
8234/*
8235 * ":undo".
8236 */
8237/*ARGSUSED*/
8238 static void
8239ex_undo(eap)
8240 exarg_T *eap;
8241{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008242 if (eap->addr_count == 1) /* :undo 123 */
8243 undo_time(eap->line2, FALSE, TRUE);
8244 else
8245 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246}
8247
8248/*
8249 * ":redo".
8250 */
8251/*ARGSUSED*/
8252 static void
8253ex_redo(eap)
8254 exarg_T *eap;
8255{
8256 u_redo(1);
8257}
8258
8259/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008260 * ":earlier" and ":later".
8261 */
8262/*ARGSUSED*/
8263 static void
8264ex_later(eap)
8265 exarg_T *eap;
8266{
8267 long count = 0;
8268 int sec = FALSE;
8269 char_u *p = eap->arg;
8270
8271 if (*p == NUL)
8272 count = 1;
8273 else if (isdigit(*p))
8274 {
8275 count = getdigits(&p);
8276 switch (*p)
8277 {
8278 case 's': ++p; sec = TRUE; break;
8279 case 'm': ++p; sec = TRUE; count *= 60; break;
8280 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
8281 }
8282 }
8283
8284 if (*p != NUL)
8285 EMSG2(_(e_invarg2), eap->arg);
8286 else
Bram Moolenaard3667a22006-03-16 21:35:52 +00008287 undo_time(eap->cmdidx == CMD_earlier ? -count : count, sec, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008288}
8289
8290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 * ":redir": start/stop redirection.
8292 */
8293 static void
8294ex_redir(eap)
8295 exarg_T *eap;
8296{
8297 char *mode;
8298 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008299 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300
8301 if (STRICMP(eap->arg, "END") == 0)
8302 close_redir();
8303 else
8304 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008305 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008307 ++arg;
8308 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008310 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 mode = "a";
8312 }
8313 else
8314 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008315 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
8317 close_redir();
8318
8319 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008320 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (fname == NULL)
8322 return;
8323#ifdef FEAT_BROWSE
8324 if (cmdmod.browse)
8325 {
8326 char_u *browseFile;
8327
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008328 browseFile = do_browse(BROWSE_SAVE,
8329 (char_u *)_("Save Redirection"),
8330 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 if (browseFile == NULL)
8332 return; /* operation cancelled */
8333 vim_free(fname);
8334 fname = browseFile;
8335 eap->forceit = TRUE; /* since dialog already asked */
8336 }
8337#endif
8338
8339 redir_fd = open_exfile(fname, eap->forceit, mode);
8340 vim_free(fname);
8341 }
8342#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008343 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {
8345 /* redirect to a register a-z (resp. A-Z for appending) */
8346 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008347 ++arg;
8348 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008350 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008352 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008354 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008355 if (*arg == '>' && arg[1] == '>')
8356 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008357 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8358 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008360 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008362 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008364 if (*arg == '>')
8365 arg++;
8366
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 /* make register empty */
8368 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8369 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008370 }
8371 if (*arg != NUL)
8372 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008373 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008374 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008375 }
8376 }
8377 else if (*arg == '=' && arg[1] == '>')
8378 {
8379 int append;
8380
8381 /* redirect to a variable */
8382 close_redir();
8383 arg += 2;
8384
8385 if (*arg == '>')
8386 {
8387 ++arg;
8388 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 }
8390 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008391 append = FALSE;
8392
8393 if (var_redir_start(skipwhite(arg), append) == OK)
8394 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 }
8396#endif
8397
8398 /* TODO: redirect to a buffer */
8399
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008401 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 }
8403}
8404
8405/*
8406 * ":redraw": force redraw
8407 */
8408 static void
8409ex_redraw(eap)
8410 exarg_T *eap;
8411{
8412 int r = RedrawingDisabled;
8413 int p = p_lz;
8414
8415 RedrawingDisabled = 0;
8416 p_lz = FALSE;
8417 update_topline();
8418 update_screen(eap->forceit ? CLEAR :
8419#ifdef FEAT_VISUAL
8420 VIsual_active ? INVERTED :
8421#endif
8422 0);
8423#ifdef FEAT_TITLE
8424 if (need_maketitle)
8425 maketitle();
8426#endif
8427 RedrawingDisabled = r;
8428 p_lz = p;
8429
8430 /* Reset msg_didout, so that a message that's there is overwritten. */
8431 msg_didout = FALSE;
8432 msg_col = 0;
8433
8434 out_flush();
8435}
8436
8437/*
8438 * ":redrawstatus": force redraw of status line(s)
8439 */
8440/*ARGSUSED*/
8441 static void
8442ex_redrawstatus(eap)
8443 exarg_T *eap;
8444{
8445#if defined(FEAT_WINDOWS)
8446 int r = RedrawingDisabled;
8447 int p = p_lz;
8448
8449 RedrawingDisabled = 0;
8450 p_lz = FALSE;
8451 if (eap->forceit)
8452 status_redraw_all();
8453 else
8454 status_redraw_curbuf();
8455 update_screen(
8456# ifdef FEAT_VISUAL
8457 VIsual_active ? INVERTED :
8458# endif
8459 0);
8460 RedrawingDisabled = r;
8461 p_lz = p;
8462 out_flush();
8463#endif
8464}
8465
8466 static void
8467close_redir()
8468{
8469 if (redir_fd != NULL)
8470 {
8471 fclose(redir_fd);
8472 redir_fd = NULL;
8473 }
8474#ifdef FEAT_EVAL
8475 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008476 if (redir_vname)
8477 {
8478 var_redir_stop();
8479 redir_vname = 0;
8480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481#endif
8482}
8483
8484#if defined(FEAT_SESSION) && defined(USE_CRNL)
8485# define MKSESSION_NL
8486static int mksession_nl = FALSE; /* use NL only in put_eol() */
8487#endif
8488
8489/*
8490 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8491 */
8492 static void
8493ex_mkrc(eap)
8494 exarg_T *eap;
8495{
8496 FILE *fd;
8497 int failed = FALSE;
8498 char_u *fname;
8499#ifdef FEAT_BROWSE
8500 char_u *browseFile = NULL;
8501#endif
8502#ifdef FEAT_SESSION
8503 int view_session = FALSE;
8504 int using_vdir = FALSE; /* using 'viewdir'? */
8505 char_u *viewFile = NULL;
8506 unsigned *flagp;
8507#endif
8508
8509 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8510 {
8511#ifdef FEAT_SESSION
8512 view_session = TRUE;
8513#else
8514 ex_ni(eap);
8515 return;
8516#endif
8517 }
8518
8519#ifdef FEAT_SESSION
8520 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8521 if (eap->cmdidx == CMD_mkview
8522 && (*eap->arg == NUL
8523 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8524 {
8525 eap->forceit = TRUE;
8526 fname = get_view_file(*eap->arg);
8527 if (fname == NULL)
8528 return;
8529 viewFile = fname;
8530 using_vdir = TRUE;
8531 }
8532 else
8533#endif
8534 if (*eap->arg != NUL)
8535 fname = eap->arg;
8536 else if (eap->cmdidx == CMD_mkvimrc)
8537 fname = (char_u *)VIMRC_FILE;
8538#ifdef FEAT_SESSION
8539 else if (eap->cmdidx == CMD_mksession)
8540 fname = (char_u *)SESSION_FILE;
8541#endif
8542 else
8543 fname = (char_u *)EXRC_FILE;
8544
8545#ifdef FEAT_BROWSE
8546 if (cmdmod.browse)
8547 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008548 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549# ifdef FEAT_SESSION
8550 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8551 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8552# endif
8553 (char_u *)_("Save Setup"),
8554 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8555 if (browseFile == NULL)
8556 goto theend;
8557 fname = browseFile;
8558 eap->forceit = TRUE; /* since dialog already asked */
8559 }
8560#endif
8561
8562#if defined(FEAT_SESSION) && defined(vim_mkdir)
8563 /* When using 'viewdir' may have to create the directory. */
8564 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008565 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566#endif
8567
8568 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8569 if (fd != NULL)
8570 {
8571#ifdef FEAT_SESSION
8572 if (eap->cmdidx == CMD_mkview)
8573 flagp = &vop_flags;
8574 else
8575 flagp = &ssop_flags;
8576#endif
8577
8578#ifdef MKSESSION_NL
8579 /* "unix" in 'sessionoptions': use NL line separator */
8580 if (view_session && (*flagp & SSOP_UNIX))
8581 mksession_nl = TRUE;
8582#endif
8583
8584 /* Write the version command for :mkvimrc */
8585 if (eap->cmdidx == CMD_mkvimrc)
8586 (void)put_line(fd, "version 6.0");
8587
8588#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008589 if (eap->cmdidx == CMD_mksession)
8590 {
8591 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8592 failed = TRUE;
8593 }
8594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 if (eap->cmdidx != CMD_mkview)
8596#endif
8597 {
8598 /* Write setting 'compatible' first, because it has side effects.
8599 * For that same reason only do it when needed. */
8600 if (p_cp)
8601 (void)put_line(fd, "if !&cp | set cp | endif");
8602 else
8603 (void)put_line(fd, "if &cp | set nocp | endif");
8604 }
8605
8606#ifdef FEAT_SESSION
8607 if (!view_session
8608 || (eap->cmdidx == CMD_mksession
8609 && (*flagp & SSOP_OPTIONS)))
8610#endif
8611 failed |= (makemap(fd, NULL) == FAIL
8612 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8613
8614#ifdef FEAT_SESSION
8615 if (!failed && view_session)
8616 {
8617 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8618 failed = TRUE;
8619 if (eap->cmdidx == CMD_mksession)
8620 {
8621 char_u dirnow[MAXPATHL]; /* current directory */
8622
8623 /*
8624 * Change to session file's dir.
8625 */
8626 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8627 || mch_chdir((char *)dirnow) != 0)
8628 *dirnow = NUL;
8629 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8630 {
8631 if (vim_chdirfile(fname) == OK)
8632 shorten_fnames(TRUE);
8633 }
8634 else if (*dirnow != NUL
8635 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8636 {
8637 (void)mch_chdir((char *)globaldir);
8638 shorten_fnames(TRUE);
8639 }
8640
8641 failed |= (makeopens(fd, dirnow) == FAIL);
8642
8643 /* restore original dir */
8644 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8645 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8646 {
8647 if (mch_chdir((char *)dirnow) != 0)
8648 EMSG(_(e_prev_dir));
8649 shorten_fnames(TRUE);
8650 }
8651 }
8652 else
8653 {
8654 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8655 }
8656 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8657 == FAIL)
8658 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008659 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8660 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008661 if (eap->cmdidx == CMD_mksession)
8662 {
8663 if (put_line(fd, "unlet SessionLoad") == FAIL)
8664 failed = TRUE;
8665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 }
8667#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008668 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8669 failed = TRUE;
8670
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 failed |= fclose(fd);
8672
8673 if (failed)
8674 EMSG(_(e_write));
8675#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8676 else if (eap->cmdidx == CMD_mksession)
8677 {
8678 /* successful session write - set this_session var */
8679 char_u tbuf[MAXPATHL];
8680
8681 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8682 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8683 }
8684#endif
8685#ifdef MKSESSION_NL
8686 mksession_nl = FALSE;
8687#endif
8688 }
8689
8690#ifdef FEAT_BROWSE
8691theend:
8692 vim_free(browseFile);
8693#endif
8694#ifdef FEAT_SESSION
8695 vim_free(viewFile);
8696#endif
8697}
8698
Bram Moolenaardf177f62005-02-22 08:39:57 +00008699#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8700 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008701/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008702 int
8703vim_mkdir_emsg(name, prot)
8704 char_u *name;
8705 int prot;
8706{
8707 if (vim_mkdir(name, prot) != 0)
8708 {
8709 EMSG2(_("E739: Cannot create directory: %s"), name);
8710 return FAIL;
8711 }
8712 return OK;
8713}
8714#endif
8715
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716/*
8717 * Open a file for writing for an Ex command, with some checks.
8718 * Return file descriptor, or NULL on failure.
8719 */
8720 FILE *
8721open_exfile(fname, forceit, mode)
8722 char_u *fname;
8723 int forceit;
8724 char *mode; /* "w" for create new file or "a" for append */
8725{
8726 FILE *fd;
8727
8728#ifdef UNIX
8729 /* with Unix it is possible to open a directory */
8730 if (mch_isdir(fname))
8731 {
8732 EMSG2(_(e_isadir2), fname);
8733 return NULL;
8734 }
8735#endif
8736 if (!forceit && *mode != 'a' && vim_fexists(fname))
8737 {
8738 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8739 return NULL;
8740 }
8741
8742 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8743 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8744
8745 return fd;
8746}
8747
8748/*
8749 * ":mark" and ":k".
8750 */
8751 static void
8752ex_mark(eap)
8753 exarg_T *eap;
8754{
8755 pos_T pos;
8756
8757 if (*eap->arg == NUL) /* No argument? */
8758 EMSG(_(e_argreq));
8759 else if (eap->arg[1] != NUL) /* more than one character? */
8760 EMSG(_(e_trailing));
8761 else
8762 {
8763 pos = curwin->w_cursor; /* save curwin->w_cursor */
8764 curwin->w_cursor.lnum = eap->line2;
8765 beginline(BL_WHITE | BL_FIX);
8766 if (setmark(*eap->arg) == FAIL) /* set mark */
8767 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8768 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8769 }
8770}
8771
8772/*
8773 * Update w_topline, w_leftcol and the cursor position.
8774 */
8775 void
8776update_topline_cursor()
8777{
8778 check_cursor(); /* put cursor on valid line */
8779 update_topline();
8780 if (!curwin->w_p_wrap)
8781 validate_cursor();
8782 update_curswant();
8783}
8784
8785#ifdef FEAT_EX_EXTRA
8786/*
8787 * ":normal[!] {commands}": Execute normal mode commands.
8788 */
8789 static void
8790ex_normal(eap)
8791 exarg_T *eap;
8792{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 int save_msg_scroll = msg_scroll;
8794 int save_restart_edit = restart_edit;
8795 int save_msg_didout = msg_didout;
8796 int save_State = State;
8797 tasave_T tabuf;
8798 int save_insertmode = p_im;
8799 int save_finish_op = finish_op;
8800#ifdef FEAT_MBYTE
8801 char_u *arg = NULL;
8802 int l;
8803 char_u *p;
8804#endif
8805
8806 if (ex_normal_busy >= p_mmd)
8807 {
8808 EMSG(_("E192: Recursive use of :normal too deep"));
8809 return;
8810 }
8811 ++ex_normal_busy;
8812
8813 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8814 restart_edit = 0; /* don't go to Insert mode */
8815 p_im = FALSE; /* don't use 'insertmode' */
8816
8817#ifdef FEAT_MBYTE
8818 /*
8819 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8820 * this for the K_SPECIAL leading byte, otherwise special keys will not
8821 * work.
8822 */
8823 if (has_mbyte)
8824 {
8825 int len = 0;
8826
8827 /* Count the number of characters to be escaped. */
8828 for (p = eap->arg; *p != NUL; ++p)
8829 {
8830# ifdef FEAT_GUI
8831 if (*p == CSI) /* leadbyte CSI */
8832 len += 2;
8833# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008834 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8836# ifdef FEAT_GUI
8837 || *p == CSI
8838# endif
8839 )
8840 len += 2;
8841 }
8842 if (len > 0)
8843 {
8844 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8845 if (arg != NULL)
8846 {
8847 len = 0;
8848 for (p = eap->arg; *p != NUL; ++p)
8849 {
8850 arg[len++] = *p;
8851# ifdef FEAT_GUI
8852 if (*p == CSI)
8853 {
8854 arg[len++] = KS_EXTRA;
8855 arg[len++] = (int)KE_CSI;
8856 }
8857# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008858 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 {
8860 arg[len++] = *++p;
8861 if (*p == K_SPECIAL)
8862 {
8863 arg[len++] = KS_SPECIAL;
8864 arg[len++] = KE_FILLER;
8865 }
8866# ifdef FEAT_GUI
8867 else if (*p == CSI)
8868 {
8869 arg[len++] = KS_EXTRA;
8870 arg[len++] = (int)KE_CSI;
8871 }
8872# endif
8873 }
8874 arg[len] = NUL;
8875 }
8876 }
8877 }
8878 }
8879#endif
8880
8881 /*
8882 * Save the current typeahead. This is required to allow using ":normal"
8883 * from an event handler and makes sure we don't hang when the argument
8884 * ends with half a command.
8885 */
8886 save_typeahead(&tabuf);
8887 if (tabuf.typebuf_valid)
8888 {
8889 /*
8890 * Repeat the :normal command for each line in the range. When no
8891 * range given, execute it just once, without positioning the cursor
8892 * first.
8893 */
8894 do
8895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 if (eap->addr_count != 0)
8897 {
8898 curwin->w_cursor.lnum = eap->line1++;
8899 curwin->w_cursor.col = 0;
8900 }
8901
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008902 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903#ifdef FEAT_MBYTE
8904 arg != NULL ? arg :
8905#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008906 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 }
8908 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8909 }
8910
8911 /* Might not return to the main loop when in an event handler. */
8912 update_topline_cursor();
8913
8914 /* Restore the previous typeahead. */
8915 restore_typeahead(&tabuf);
8916
8917 --ex_normal_busy;
8918 msg_scroll = save_msg_scroll;
8919 restart_edit = save_restart_edit;
8920 p_im = save_insertmode;
8921 finish_op = save_finish_op;
8922 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8923
8924 /* Restore the state (needed when called from a function executed for
8925 * 'indentexpr'). */
8926 State = save_State;
8927#ifdef FEAT_MBYTE
8928 vim_free(arg);
8929#endif
8930}
8931
8932/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008933 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 */
8935 static void
8936ex_startinsert(eap)
8937 exarg_T *eap;
8938{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008939 if (eap->forceit)
8940 {
8941 coladvance((colnr_T)MAXCOL);
8942 curwin->w_curswant = MAXCOL;
8943 curwin->w_set_curswant = FALSE;
8944 }
8945
Bram Moolenaara40c5002005-01-09 21:16:21 +00008946 /* Ignore the command when already in Insert mode. Inserting an
8947 * expression register that invokes a function can do this. */
8948 if (State & INSERT)
8949 return;
8950
Bram Moolenaar64969662005-12-14 21:59:55 +00008951 if (eap->cmdidx == CMD_startinsert)
8952 restart_edit = 'a';
8953 else if (eap->cmdidx == CMD_startreplace)
8954 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008956 restart_edit = 'V';
8957
8958 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008960 if (eap->cmdidx == CMD_startinsert)
8961 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 curwin->w_curswant = 0; /* avoid MAXCOL */
8963 }
8964}
8965
8966/*
8967 * ":stopinsert"
8968 */
8969/*ARGSUSED*/
8970 static void
8971ex_stopinsert(eap)
8972 exarg_T *eap;
8973{
8974 restart_edit = 0;
8975 stop_insert_mode = TRUE;
8976}
8977#endif
8978
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008979#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8980/*
8981 * Execute normal mode command "cmd".
8982 * "remap" can be REMAP_NONE or REMAP_YES.
8983 */
8984 void
8985exec_normal_cmd(cmd, remap, silent)
8986 char_u *cmd;
8987 int remap;
8988 int silent;
8989{
8990 oparg_T oa;
8991
8992 /*
8993 * Stuff the argument into the typeahead buffer.
8994 * Execute normal_cmd() until there is no typeahead left.
8995 */
8996 clear_oparg(&oa);
8997 finish_op = FALSE;
8998 ins_typebuf(cmd, remap, 0, TRUE, silent);
8999 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9000 && !got_int)
9001 {
9002 update_topline_cursor();
9003 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9004 }
9005}
9006#endif
9007
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008#ifdef FEAT_FIND_ID
9009 static void
9010ex_checkpath(eap)
9011 exarg_T *eap;
9012{
9013 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9014 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9015 (linenr_T)1, (linenr_T)MAXLNUM);
9016}
9017
9018#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9019/*
9020 * ":psearch"
9021 */
9022 static void
9023ex_psearch(eap)
9024 exarg_T *eap;
9025{
9026 g_do_tagpreview = p_pvh;
9027 ex_findpat(eap);
9028 g_do_tagpreview = 0;
9029}
9030#endif
9031
9032 static void
9033ex_findpat(eap)
9034 exarg_T *eap;
9035{
9036 int whole = TRUE;
9037 long n;
9038 char_u *p;
9039 int action;
9040
9041 switch (cmdnames[eap->cmdidx].cmd_name[2])
9042 {
9043 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9044 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9045 action = ACTION_GOTO;
9046 else
9047 action = ACTION_SHOW;
9048 break;
9049 case 'i': /* ":ilist" and ":dlist" */
9050 action = ACTION_SHOW_ALL;
9051 break;
9052 case 'u': /* ":ijump" and ":djump" */
9053 action = ACTION_GOTO;
9054 break;
9055 default: /* ":isplit" and ":dsplit" */
9056 action = ACTION_SPLIT;
9057 break;
9058 }
9059
9060 n = 1;
9061 if (vim_isdigit(*eap->arg)) /* get count */
9062 {
9063 n = getdigits(&eap->arg);
9064 eap->arg = skipwhite(eap->arg);
9065 }
9066 if (*eap->arg == '/') /* Match regexp, not just whole words */
9067 {
9068 whole = FALSE;
9069 ++eap->arg;
9070 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9071 if (*p)
9072 {
9073 *p++ = NUL;
9074 p = skipwhite(p);
9075
9076 /* Check for trailing illegal characters */
9077 if (!ends_excmd(*p))
9078 eap->errmsg = e_trailing;
9079 else
9080 eap->nextcmd = check_nextcmd(p);
9081 }
9082 }
9083 if (!eap->skip)
9084 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9085 whole, !eap->forceit,
9086 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9087 n, action, eap->line1, eap->line2);
9088}
9089#endif
9090
9091#ifdef FEAT_WINDOWS
9092
9093# ifdef FEAT_QUICKFIX
9094/*
9095 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9096 */
9097 static void
9098ex_ptag(eap)
9099 exarg_T *eap;
9100{
9101 g_do_tagpreview = p_pvh;
9102 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9103}
9104
9105/*
9106 * ":pedit"
9107 */
9108 static void
9109ex_pedit(eap)
9110 exarg_T *eap;
9111{
9112 win_T *curwin_save = curwin;
9113
9114 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009115 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 keep_help_flag = curwin_save->w_buffer->b_help;
9117 do_exedit(eap, NULL);
9118 keep_help_flag = FALSE;
9119 if (curwin != curwin_save && win_valid(curwin_save))
9120 {
9121 /* Return cursor to where we were */
9122 validate_cursor();
9123 redraw_later(VALID);
9124 win_enter(curwin_save, TRUE);
9125 }
9126 g_do_tagpreview = 0;
9127}
9128# endif
9129
9130/*
9131 * ":stag", ":stselect" and ":stjump".
9132 */
9133 static void
9134ex_stag(eap)
9135 exarg_T *eap;
9136{
9137 postponed_split = -1;
9138 postponed_split_flags = cmdmod.split;
9139 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9140 postponed_split_flags = 0;
9141}
9142#endif
9143
9144/*
9145 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9146 */
9147 static void
9148ex_tag(eap)
9149 exarg_T *eap;
9150{
9151 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9152}
9153
9154 static void
9155ex_tag_cmd(eap, name)
9156 exarg_T *eap;
9157 char_u *name;
9158{
9159 int cmd;
9160
9161 switch (name[1])
9162 {
9163 case 'j': cmd = DT_JUMP; /* ":tjump" */
9164 break;
9165 case 's': cmd = DT_SELECT; /* ":tselect" */
9166 break;
9167 case 'p': cmd = DT_PREV; /* ":tprevious" */
9168 break;
9169 case 'N': cmd = DT_PREV; /* ":tNext" */
9170 break;
9171 case 'n': cmd = DT_NEXT; /* ":tnext" */
9172 break;
9173 case 'o': cmd = DT_POP; /* ":pop" */
9174 break;
9175 case 'f': /* ":tfirst" */
9176 case 'r': cmd = DT_FIRST; /* ":trewind" */
9177 break;
9178 case 'l': cmd = DT_LAST; /* ":tlast" */
9179 break;
9180 default: /* ":tag" */
9181#ifdef FEAT_CSCOPE
9182 if (p_cst)
9183 {
9184 do_cstag(eap);
9185 return;
9186 }
9187#endif
9188 cmd = DT_TAG;
9189 break;
9190 }
9191
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009192 if (name[0] == 'l')
9193 {
9194#ifndef FEAT_QUICKFIX
9195 ex_ni(eap);
9196 return;
9197#else
9198 cmd = DT_LTAG;
9199#endif
9200 }
9201
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9203 eap->forceit, TRUE);
9204}
9205
9206/*
9207 * Evaluate cmdline variables.
9208 *
9209 * change '%' to curbuf->b_ffname
9210 * '#' to curwin->w_altfile
9211 * '<cword>' to word under the cursor
9212 * '<cWORD>' to WORD under the cursor
9213 * '<cfile>' to path name under the cursor
9214 * '<sfile>' to sourced file name
9215 * '<afile>' to file name for autocommand
9216 * '<abuf>' to buffer number for autocommand
9217 * '<amatch>' to matching name for autocommand
9218 *
9219 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9220 * "" for error without a message) and NULL is returned.
9221 * Returns an allocated string if a valid match was found.
9222 * Returns NULL if no match was found. "usedlen" then still contains the
9223 * number of characters to skip.
9224 */
9225 char_u *
9226eval_vars(src, usedlen, lnump, errormsg, srcstart)
9227 char_u *src; /* pointer into commandline */
9228 int *usedlen; /* characters after src that are used */
9229 linenr_T *lnump; /* line number for :e command, or NULL */
9230 char_u **errormsg; /* pointer to error message */
9231 char_u *srcstart; /* beginning of valid memory for src */
9232{
9233 int i;
9234 char_u *s;
9235 char_u *result;
9236 char_u *resultbuf = NULL;
9237 int resultlen;
9238 buf_T *buf;
9239 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9240 int spec_idx;
9241#ifdef FEAT_MODIFY_FNAME
9242 int skip_mod = FALSE;
9243#endif
9244 static char *(spec_str[]) =
9245 {
9246 "%",
9247#define SPEC_PERC 0
9248 "#",
9249#define SPEC_HASH 1
9250 "<cword>", /* cursor word */
9251#define SPEC_CWORD 2
9252 "<cWORD>", /* cursor WORD */
9253#define SPEC_CCWORD 3
9254 "<cfile>", /* cursor path name */
9255#define SPEC_CFILE 4
9256 "<sfile>", /* ":so" file name */
9257#define SPEC_SFILE 5
9258#ifdef FEAT_AUTOCMD
9259 "<afile>", /* autocommand file name */
9260# define SPEC_AFILE 6
9261 "<abuf>", /* autocommand buffer number */
9262# define SPEC_ABUF 7
9263 "<amatch>", /* autocommand match name */
9264# define SPEC_AMATCH 8
9265#endif
9266#ifdef FEAT_CLIENTSERVER
9267 "<client>"
9268# define SPEC_CLIENT 9
9269#endif
9270 };
9271#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9272
9273#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9274 char_u strbuf[30];
9275#endif
9276
9277 *errormsg = NULL;
9278
9279 /*
9280 * Check if there is something to do.
9281 */
9282 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9283 {
9284 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9285 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9286 break;
9287 }
9288 if (spec_idx == SPEC_COUNT) /* no match */
9289 {
9290 *usedlen = 1;
9291 return NULL;
9292 }
9293
9294 /*
9295 * Skip when preceded with a backslash "\%" and "\#".
9296 * Note: In "\\%" the % is also not recognized!
9297 */
9298 if (src > srcstart && src[-1] == '\\')
9299 {
9300 *usedlen = 0;
9301 STRCPY(src - 1, src); /* remove backslash */
9302 return NULL;
9303 }
9304
9305 /*
9306 * word or WORD under cursor
9307 */
9308 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9309 {
9310 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9311 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9312 if (resultlen == 0)
9313 {
9314 *errormsg = (char_u *)"";
9315 return NULL;
9316 }
9317 }
9318
9319 /*
9320 * '#': Alternate file name
9321 * '%': Current file name
9322 * File name under the cursor
9323 * File name for autocommand
9324 * and following modifiers
9325 */
9326 else
9327 {
9328 switch (spec_idx)
9329 {
9330 case SPEC_PERC: /* '%': current file */
9331 if (curbuf->b_fname == NULL)
9332 {
9333 result = (char_u *)"";
9334 valid = 0; /* Must have ":p:h" to be valid */
9335 }
9336 else
9337#ifdef RISCOS
9338 /* Always use the full path for RISC OS if possible. */
9339 result = curbuf->b_ffname;
9340 if (result == NULL)
9341 result = curbuf->b_fname;
9342#else
9343 result = curbuf->b_fname;
9344#endif
9345 break;
9346
9347 case SPEC_HASH: /* '#' or "#99": alternate file */
9348 if (src[1] == '#') /* "##": the argument list */
9349 {
9350 result = arg_all();
9351 resultbuf = result;
9352 *usedlen = 2;
9353#ifdef FEAT_MODIFY_FNAME
9354 skip_mod = TRUE;
9355#endif
9356 break;
9357 }
9358 s = src + 1;
9359 i = (int)getdigits(&s);
9360 *usedlen = (int)(s - src); /* length of what we expand */
9361
9362 buf = buflist_findnr(i);
9363 if (buf == NULL)
9364 {
9365 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9366 return NULL;
9367 }
9368 if (lnump != NULL)
9369 *lnump = ECMD_LAST;
9370 if (buf->b_fname == NULL)
9371 {
9372 result = (char_u *)"";
9373 valid = 0; /* Must have ":p:h" to be valid */
9374 }
9375 else
9376 result = buf->b_fname;
9377 break;
9378
9379#ifdef FEAT_SEARCHPATH
9380 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009381 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 if (result == NULL)
9383 {
9384 *errormsg = (char_u *)"";
9385 return NULL;
9386 }
9387 resultbuf = result; /* remember allocated string */
9388 break;
9389#endif
9390
9391#ifdef FEAT_AUTOCMD
9392 case SPEC_AFILE: /* file name for autocommand */
9393 result = autocmd_fname;
9394 if (result == NULL)
9395 {
9396 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9397 return NULL;
9398 }
9399 break;
9400
9401 case SPEC_ABUF: /* buffer number for autocommand */
9402 if (autocmd_bufnr <= 0)
9403 {
9404 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9405 return NULL;
9406 }
9407 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9408 result = strbuf;
9409 break;
9410
9411 case SPEC_AMATCH: /* match name for autocommand */
9412 result = autocmd_match;
9413 if (result == NULL)
9414 {
9415 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9416 return NULL;
9417 }
9418 break;
9419
9420#endif
9421 case SPEC_SFILE: /* file name for ":so" command */
9422 result = sourcing_name;
9423 if (result == NULL)
9424 {
9425 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9426 return NULL;
9427 }
9428 break;
9429#if defined(FEAT_CLIENTSERVER)
9430 case SPEC_CLIENT: /* Source of last submitted input */
9431 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9432 result = strbuf;
9433 break;
9434#endif
9435 }
9436
9437 resultlen = (int)STRLEN(result); /* length of new string */
9438 if (src[*usedlen] == '<') /* remove the file name extension */
9439 {
9440 ++*usedlen;
9441#ifdef RISCOS
9442 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9443#else
9444 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9445#endif
9446 resultlen = (int)(s - result);
9447 }
9448#ifdef FEAT_MODIFY_FNAME
9449 else if (!skip_mod)
9450 {
9451 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9452 &resultlen);
9453 if (result == NULL)
9454 {
9455 *errormsg = (char_u *)"";
9456 return NULL;
9457 }
9458 }
9459#endif
9460 }
9461
9462 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9463 {
9464 if (valid != VALID_HEAD + VALID_PATH)
9465 /* xgettext:no-c-format */
9466 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9467 else
9468 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9469 result = NULL;
9470 }
9471 else
9472 result = vim_strnsave(result, resultlen);
9473 vim_free(resultbuf);
9474 return result;
9475}
9476
9477/*
9478 * Concatenate all files in the argument list, separated by spaces, and return
9479 * it in one allocated string.
9480 * Spaces and backslashes in the file names are escaped with a backslash.
9481 * Returns NULL when out of memory.
9482 */
9483 static char_u *
9484arg_all()
9485{
9486 int len;
9487 int idx;
9488 char_u *retval = NULL;
9489 char_u *p;
9490
9491 /*
9492 * Do this loop two times:
9493 * first time: compute the total length
9494 * second time: concatenate the names
9495 */
9496 for (;;)
9497 {
9498 len = 0;
9499 for (idx = 0; idx < ARGCOUNT; ++idx)
9500 {
9501 p = alist_name(&ARGLIST[idx]);
9502 if (p != NULL)
9503 {
9504 if (len > 0)
9505 {
9506 /* insert a space in between names */
9507 if (retval != NULL)
9508 retval[len] = ' ';
9509 ++len;
9510 }
9511 for ( ; *p != NUL; ++p)
9512 {
9513 if (*p == ' ' || *p == '\\')
9514 {
9515 /* insert a backslash */
9516 if (retval != NULL)
9517 retval[len] = '\\';
9518 ++len;
9519 }
9520 if (retval != NULL)
9521 retval[len] = *p;
9522 ++len;
9523 }
9524 }
9525 }
9526
9527 /* second time: break here */
9528 if (retval != NULL)
9529 {
9530 retval[len] = NUL;
9531 break;
9532 }
9533
9534 /* allocate memory */
9535 retval = alloc(len + 1);
9536 if (retval == NULL)
9537 break;
9538 }
9539
9540 return retval;
9541}
9542
9543#if defined(FEAT_AUTOCMD) || defined(PROTO)
9544/*
9545 * Expand the <sfile> string in "arg".
9546 *
9547 * Returns an allocated string, or NULL for any error.
9548 */
9549 char_u *
9550expand_sfile(arg)
9551 char_u *arg;
9552{
9553 char_u *errormsg;
9554 int len;
9555 char_u *result;
9556 char_u *newres;
9557 char_u *repl;
9558 int srclen;
9559 char_u *p;
9560
9561 result = vim_strsave(arg);
9562 if (result == NULL)
9563 return NULL;
9564
9565 for (p = result; *p; )
9566 {
9567 if (STRNCMP(p, "<sfile>", 7) != 0)
9568 ++p;
9569 else
9570 {
9571 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9572 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9573 if (errormsg != NULL)
9574 {
9575 if (*errormsg)
9576 emsg(errormsg);
9577 vim_free(result);
9578 return NULL;
9579 }
9580 if (repl == NULL) /* no match (cannot happen) */
9581 {
9582 p += srclen;
9583 continue;
9584 }
9585 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9586 newres = alloc(len);
9587 if (newres == NULL)
9588 {
9589 vim_free(repl);
9590 vim_free(result);
9591 return NULL;
9592 }
9593 mch_memmove(newres, result, (size_t)(p - result));
9594 STRCPY(newres + (p - result), repl);
9595 len = (int)STRLEN(newres);
9596 STRCAT(newres, p + srclen);
9597 vim_free(repl);
9598 vim_free(result);
9599 result = newres;
9600 p = newres + len; /* continue after the match */
9601 }
9602 }
9603
9604 return result;
9605}
9606#endif
9607
9608#ifdef FEAT_SESSION
9609static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9610static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9611static frame_T *ses_skipframe __ARGS((frame_T *fr));
9612static int ses_do_frame __ARGS((frame_T *fr));
9613static int ses_do_win __ARGS((win_T *wp));
9614static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9615static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9616static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9617
9618/*
9619 * Write openfile commands for the current buffers to an .exrc file.
9620 * Return FAIL on error, OK otherwise.
9621 */
9622 static int
9623makeopens(fd, dirnow)
9624 FILE *fd;
9625 char_u *dirnow; /* Current directory name */
9626{
9627 buf_T *buf;
9628 int only_save_windows = TRUE;
9629 int nr;
9630 int cnr = 1;
9631 int restore_size = TRUE;
9632 win_T *wp;
9633 char_u *sname;
9634 win_T *edited_win = NULL;
9635
9636 if (ssop_flags & SSOP_BUFFERS)
9637 only_save_windows = FALSE; /* Save ALL buffers */
9638
9639 /*
9640 * Begin by setting the this_session variable, and then other
9641 * sessionable variables.
9642 */
9643#ifdef FEAT_EVAL
9644 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9645 return FAIL;
9646 if (ssop_flags & SSOP_GLOBALS)
9647 if (store_session_globals(fd) == FAIL)
9648 return FAIL;
9649#endif
9650
9651 /*
9652 * Close all windows but one.
9653 */
9654 if (put_line(fd, "silent only") == FAIL)
9655 return FAIL;
9656
9657 /*
9658 * Now a :cd command to the session directory or the current directory
9659 */
9660 if (ssop_flags & SSOP_SESDIR)
9661 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009662 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9663 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 return FAIL;
9665 }
9666 else if (ssop_flags & SSOP_CURDIR)
9667 {
9668 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9669 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009670 || fputs("cd ", fd) < 0
9671 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9672 || put_eol(fd) == FAIL)
9673 {
9674 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 vim_free(sname);
9678 }
9679
9680 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009681 * If there is an empty, unnamed buffer we will wipe it out later.
9682 * Remember the buffer number.
9683 */
9684 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9685 return FAIL;
9686 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9687 return FAIL;
9688 if (put_line(fd, "endif") == FAIL)
9689 return FAIL;
9690
9691 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 * Now save the current files, current buffer first.
9693 */
9694 if (put_line(fd, "set shortmess=aoO") == FAIL)
9695 return FAIL;
9696
9697 /* Now put the other buffers into the buffer list */
9698 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9699 {
9700 if (!(only_save_windows && buf->b_nwindows == 0)
9701 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9702 && buf->b_fname != NULL
9703 && buf->b_p_bl)
9704 {
9705 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9706 : buf->b_wininfo->wi_fpos.lnum) < 0
9707 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9708 return FAIL;
9709 }
9710 }
9711
9712 /* the global argument list */
9713 if (ses_arglist(fd, "args", &global_alist.al_ga,
9714 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9715 return FAIL;
9716
9717 if (ssop_flags & SSOP_RESIZE)
9718 {
9719 /* Note: after the restore we still check it worked!*/
9720 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9721 || put_eol(fd) == FAIL)
9722 return FAIL;
9723 }
9724
9725#ifdef FEAT_GUI
9726 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9727 {
9728 int x, y;
9729
9730 if (gui_mch_get_winpos(&x, &y) == OK)
9731 {
9732 /* Note: after the restore we still check it worked!*/
9733 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9734 return FAIL;
9735 }
9736 }
9737#endif
9738
9739 /*
9740 * Before creating the window layout, try loading one file. If this is
9741 * aborted we don't end up with a number of useless windows.
9742 * This may have side effects! (e.g., compressed or network file).
9743 */
9744 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9745 {
9746 if (ses_do_win(wp)
9747 && wp->w_buffer->b_ffname != NULL
9748 && !wp->w_buffer->b_help
9749#ifdef FEAT_QUICKFIX
9750 && !bt_nofile(wp->w_buffer)
9751#endif
9752 )
9753 {
9754 if (fputs("edit ", fd) < 0
9755 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9756 return FAIL;
9757 if (!wp->w_arg_idx_invalid)
9758 edited_win = wp;
9759 break;
9760 }
9761 }
9762
9763 /*
9764 * Save current window layout.
9765 */
9766 if (put_line(fd, "set splitbelow splitright") == FAIL)
9767 return FAIL;
9768 if (ses_win_rec(fd, topframe) == FAIL)
9769 return FAIL;
9770 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9771 return FAIL;
9772 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9773 return FAIL;
9774
9775 /*
9776 * Check if window sizes can be restored (no windows omitted).
9777 * Remember the window number of the current window after restoring.
9778 */
9779 nr = 0;
9780 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9781 {
9782 if (ses_do_win(wp))
9783 ++nr;
9784 else
9785 restore_size = FALSE;
9786 if (curwin == wp)
9787 cnr = nr;
9788 }
9789
9790 /* Go to the first window. */
9791 if (put_line(fd, "wincmd t") == FAIL)
9792 return FAIL;
9793
9794 /*
9795 * If more than one window, see if sizes can be restored.
9796 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9797 * resized when moving between windows.
9798 * Do this before restoring the view, so that the topline and the cursor
9799 * can be set. This is done again below.
9800 */
9801 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9802 return FAIL;
9803 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9804 return FAIL;
9805
9806 /*
9807 * Restore the view of the window (options, file, cursor, etc.).
9808 */
9809 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9810 {
9811 if (!ses_do_win(wp))
9812 continue;
9813 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9814 return FAIL;
9815 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9816 return FAIL;
9817 }
9818
9819 /*
9820 * Restore cursor to the current window if it's not the first one.
9821 */
9822 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9823 return FAIL;
9824
9825 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009826 * Wipe out an empty unnamed buffer we started in.
9827 */
9828 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9829 return FAIL;
9830 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9831 return FAIL;
9832 if (put_line(fd, "endif") == FAIL)
9833 return FAIL;
9834 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9835 return FAIL;
9836
9837 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 * Restore window sizes again after jumping around in windows, because the
9839 * current window has a minimum size while others may not.
9840 */
9841 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9842 return FAIL;
9843
9844 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9845 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9846 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9847 return FAIL;
9848
9849 /*
9850 * Lastly, execute the x.vim file if it exists.
9851 */
9852 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9853 || put_line(fd, "if file_readable(s:sx)") == FAIL
9854 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9855 || put_line(fd, "endif") == FAIL)
9856 return FAIL;
9857
9858 return OK;
9859}
9860
9861 static int
9862ses_winsizes(fd, restore_size)
9863 FILE *fd;
9864 int restore_size;
9865{
9866 int n = 0;
9867 win_T *wp;
9868
9869 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9870 {
9871 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9872 {
9873 if (!ses_do_win(wp))
9874 continue;
9875 ++n;
9876
9877 /* restore height when not full height */
9878 if (wp->w_height + wp->w_status_height < topframe->fr_height
9879 && (fprintf(fd,
9880 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9881 n, (long)wp->w_height, Rows / 2, Rows) < 0
9882 || put_eol(fd) == FAIL))
9883 return FAIL;
9884
9885 /* restore width when not full width */
9886 if (wp->w_width < Columns && (fprintf(fd,
9887 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9888 n, (long)wp->w_width, Columns / 2, Columns) < 0
9889 || put_eol(fd) == FAIL))
9890 return FAIL;
9891 }
9892 }
9893 else
9894 {
9895 /* Just equalise window sizes */
9896 if (put_line(fd, "wincmd =") == FAIL)
9897 return FAIL;
9898 }
9899 return OK;
9900}
9901
9902/*
9903 * Write commands to "fd" to recursively create windows for frame "fr",
9904 * horizontally and vertically split.
9905 * After the commands the last window in the frame is the current window.
9906 * Returns FAIL when writing the commands to "fd" fails.
9907 */
9908 static int
9909ses_win_rec(fd, fr)
9910 FILE *fd;
9911 frame_T *fr;
9912{
9913 frame_T *frc;
9914 int count = 0;
9915
9916 if (fr->fr_layout != FR_LEAF)
9917 {
9918 /* Find first frame that's not skipped and then create a window for
9919 * each following one (first frame is already there). */
9920 frc = ses_skipframe(fr->fr_child);
9921 if (frc != NULL)
9922 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9923 {
9924 /* Make window as big as possible so that we have lots of room
9925 * to split. */
9926 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9927 || put_line(fd, fr->fr_layout == FR_COL
9928 ? "split" : "vsplit") == FAIL)
9929 return FAIL;
9930 ++count;
9931 }
9932
9933 /* Go back to the first window. */
9934 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9935 ? "%dwincmd k" : "%dwincmd h", count) < 0
9936 || put_eol(fd) == FAIL))
9937 return FAIL;
9938
9939 /* Recursively create frames/windows in each window of this column or
9940 * row. */
9941 frc = ses_skipframe(fr->fr_child);
9942 while (frc != NULL)
9943 {
9944 ses_win_rec(fd, frc);
9945 frc = ses_skipframe(frc->fr_next);
9946 /* Go to next window. */
9947 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9948 return FAIL;
9949 }
9950 }
9951 return OK;
9952}
9953
9954/*
9955 * Skip frames that don't contain windows we want to save in the Session.
9956 * Returns NULL when there none.
9957 */
9958 static frame_T *
9959ses_skipframe(fr)
9960 frame_T *fr;
9961{
9962 frame_T *frc;
9963
9964 for (frc = fr; frc != NULL; frc = frc->fr_next)
9965 if (ses_do_frame(frc))
9966 break;
9967 return frc;
9968}
9969
9970/*
9971 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9972 * the Session.
9973 */
9974 static int
9975ses_do_frame(fr)
9976 frame_T *fr;
9977{
9978 frame_T *frc;
9979
9980 if (fr->fr_layout == FR_LEAF)
9981 return ses_do_win(fr->fr_win);
9982 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9983 if (ses_do_frame(frc))
9984 return TRUE;
9985 return FALSE;
9986}
9987
9988/*
9989 * Return non-zero if window "wp" is to be stored in the Session.
9990 */
9991 static int
9992ses_do_win(wp)
9993 win_T *wp;
9994{
9995 if (wp->w_buffer->b_fname == NULL
9996#ifdef FEAT_QUICKFIX
9997 /* When 'buftype' is "nofile" can't restore the window contents. */
9998 || bt_nofile(wp->w_buffer)
9999#endif
10000 )
10001 return (ssop_flags & SSOP_BLANK);
10002 if (wp->w_buffer->b_help)
10003 return (ssop_flags & SSOP_HELP);
10004 return TRUE;
10005}
10006
10007/*
10008 * Write commands to "fd" to restore the view of a window.
10009 * Caller must make sure 'scrolloff' is zero.
10010 */
10011 static int
10012put_view(fd, wp, add_edit, flagp)
10013 FILE *fd;
10014 win_T *wp;
10015 int add_edit; /* add ":edit" command to view */
10016 unsigned *flagp; /* vop_flags or ssop_flags */
10017{
10018 win_T *save_curwin;
10019 int f;
10020 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010021 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022
10023 /* Always restore cursor position for ":mksession". For ":mkview" only
10024 * when 'viewoptions' contains "cursor". */
10025 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10026
10027 /*
10028 * Local argument list.
10029 */
10030 if (wp->w_alist == &global_alist)
10031 {
10032 if (put_line(fd, "argglobal") == FAIL)
10033 return FAIL;
10034 }
10035 else
10036 {
10037 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10038 flagp == &vop_flags
10039 || !(*flagp & SSOP_CURDIR)
10040 || wp->w_localdir != NULL, flagp) == FAIL)
10041 return FAIL;
10042 }
10043
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010044 /* Only when part of a session: restore the argument index. Some
10045 * arguments may have been deleted, check if the index is valid. */
10046 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
10047 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 {
10049 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
10050 || put_eol(fd) == FAIL)
10051 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010052 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 }
10054
10055 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010056 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 {
10058 /*
10059 * Load the file.
10060 */
10061 if (wp->w_buffer->b_ffname != NULL
10062#ifdef FEAT_QUICKFIX
10063 && !bt_nofile(wp->w_buffer)
10064#endif
10065 )
10066 {
10067 /*
10068 * Editing a file in this buffer: use ":edit file".
10069 * This may have side effects! (e.g., compressed or network file).
10070 */
10071 if (fputs("edit ", fd) < 0
10072 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10073 return FAIL;
10074 }
10075 else
10076 {
10077 /* No file in this buffer, just make it empty. */
10078 if (put_line(fd, "enew") == FAIL)
10079 return FAIL;
10080#ifdef FEAT_QUICKFIX
10081 if (wp->w_buffer->b_ffname != NULL)
10082 {
10083 /* The buffer does have a name, but it's not a file name. */
10084 if (fputs("file ", fd) < 0
10085 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10086 return FAIL;
10087 }
10088#endif
10089 do_cursor = FALSE;
10090 }
10091 }
10092
10093 /*
10094 * Local mappings and abbreviations.
10095 */
10096 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10097 && makemap(fd, wp->w_buffer) == FAIL)
10098 return FAIL;
10099
10100 /*
10101 * Local options. Need to go to the window temporarily.
10102 * Store only local values when using ":mkview" and when ":mksession" is
10103 * used and 'sessionoptions' doesn't include "options".
10104 * Some folding options are always stored when "folds" is included,
10105 * otherwise the folds would not be restored correctly.
10106 */
10107 save_curwin = curwin;
10108 curwin = wp;
10109 curbuf = curwin->w_buffer;
10110 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10111 f = makeset(fd, OPT_LOCAL,
10112 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10113#ifdef FEAT_FOLDING
10114 else if (*flagp & SSOP_FOLDS)
10115 f = makefoldset(fd);
10116#endif
10117 else
10118 f = OK;
10119 curwin = save_curwin;
10120 curbuf = curwin->w_buffer;
10121 if (f == FAIL)
10122 return FAIL;
10123
10124#ifdef FEAT_FOLDING
10125 /*
10126 * Save Folds when 'buftype' is empty and for help files.
10127 */
10128 if ((*flagp & SSOP_FOLDS)
10129 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010130# ifdef FEAT_QUICKFIX
10131 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10132# endif
10133 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 {
10135 if (put_folds(fd, wp) == FAIL)
10136 return FAIL;
10137 }
10138#endif
10139
10140 /*
10141 * Set the cursor after creating folds, since that moves the cursor.
10142 */
10143 if (do_cursor)
10144 {
10145
10146 /* Restore the cursor line in the file and relatively in the
10147 * window. Don't use "G", it changes the jumplist. */
10148 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10149 (long)wp->w_cursor.lnum,
10150 (long)(wp->w_cursor.lnum - wp->w_topline),
10151 (long)wp->w_height / 2, (long)wp->w_height) < 0
10152 || put_eol(fd) == FAIL
10153 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10154 || put_line(fd, "exe s:l") == FAIL
10155 || put_line(fd, "normal! zt") == FAIL
10156 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10157 || put_eol(fd) == FAIL)
10158 return FAIL;
10159 /* Restore the cursor column and left offset when not wrapping. */
10160 if (wp->w_cursor.col == 0)
10161 {
10162 if (put_line(fd, "normal! 0") == FAIL)
10163 return FAIL;
10164 }
10165 else
10166 {
10167 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10168 {
10169 if (fprintf(fd,
10170 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10171 (long)wp->w_cursor.col,
10172 (long)(wp->w_cursor.col - wp->w_leftcol),
10173 (long)wp->w_width / 2, (long)wp->w_width) < 0
10174 || put_eol(fd) == FAIL
10175 || put_line(fd, "if s:c > 0") == FAIL
10176 || fprintf(fd,
10177 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10178 (long)wp->w_cursor.col) < 0
10179 || put_eol(fd) == FAIL
10180 || put_line(fd, "else") == FAIL
10181 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10182 || put_eol(fd) == FAIL
10183 || put_line(fd, "endif") == FAIL)
10184 return FAIL;
10185 }
10186 else
10187 {
10188 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10189 || put_eol(fd) == FAIL)
10190 return FAIL;
10191 }
10192 }
10193 }
10194
10195 /*
10196 * Local directory.
10197 */
10198 if (wp->w_localdir != NULL)
10199 {
10200 if (fputs("lcd ", fd) < 0
10201 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10202 || put_eol(fd) == FAIL)
10203 return FAIL;
10204 }
10205
10206 return OK;
10207}
10208
10209/*
10210 * Write an argument list to the session file.
10211 * Returns FAIL if writing fails.
10212 */
10213 static int
10214ses_arglist(fd, cmd, gap, fullname, flagp)
10215 FILE *fd;
10216 char *cmd;
10217 garray_T *gap;
10218 int fullname; /* TRUE: use full path name */
10219 unsigned *flagp;
10220{
10221 int i;
10222 char_u buf[MAXPATHL];
10223 char_u *s;
10224
10225 if (gap->ga_len == 0)
10226 return put_line(fd, "silent! argdel *");
10227 if (fputs(cmd, fd) < 0)
10228 return FAIL;
10229 for (i = 0; i < gap->ga_len; ++i)
10230 {
10231 /* NULL file names are skipped (only happens when out of memory). */
10232 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10233 if (s != NULL)
10234 {
10235 if (fullname)
10236 {
10237 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10238 s = buf;
10239 }
10240 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10241 return FAIL;
10242 }
10243 }
10244 return put_eol(fd);
10245}
10246
10247/*
10248 * Write a buffer name to the session file.
10249 * Also ends the line.
10250 * Returns FAIL if writing fails.
10251 */
10252 static int
10253ses_fname(fd, buf, flagp)
10254 FILE *fd;
10255 buf_T *buf;
10256 unsigned *flagp;
10257{
10258 char_u *name;
10259
10260 /* Use the short file name if the current directory is known at the time
10261 * the session file will be sourced. Don't do this for ":mkview", we
10262 * don't know the current directory. */
10263 if (buf->b_sfname != NULL
10264 && flagp == &ssop_flags
10265 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10266 name = buf->b_sfname;
10267 else
10268 name = buf->b_ffname;
10269 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10270 return FAIL;
10271 return OK;
10272}
10273
10274/*
10275 * Write a file name to the session file.
10276 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10277 * characters.
10278 * Returns FAIL if writing fails.
10279 */
10280 static int
10281ses_put_fname(fd, name, flagp)
10282 FILE *fd;
10283 char_u *name;
10284 unsigned *flagp;
10285{
10286 char_u *sname;
10287 int retval = OK;
10288 int c;
10289
10290 sname = home_replace_save(NULL, name);
10291 if (sname != NULL)
10292 name = sname;
10293 while (*name != NUL)
10294 {
10295#ifdef FEAT_MBYTE
10296 {
10297 int l;
10298
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010299 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 {
10301 /* copy a multibyte char */
10302 while (--l >= 0)
10303 {
10304 if (putc(*name, fd) != *name)
10305 retval = FAIL;
10306 ++name;
10307 }
10308 continue;
10309 }
10310 }
10311#endif
10312 c = *name++;
10313 if (c == '\\' && (*flagp & SSOP_SLASH))
10314 /* change a backslash to a forward slash */
10315 c = '/';
10316 else if ((vim_strchr(escape_chars, c) != NULL
10317#ifdef BACKSLASH_IN_FILENAME
10318 && c != '\\'
10319#endif
10320 ) || c == '#' || c == '%')
10321 {
10322 /* escape a special character with a backslash */
10323 if (putc('\\', fd) != '\\')
10324 retval = FAIL;
10325 }
10326 if (putc(c, fd) != c)
10327 retval = FAIL;
10328 }
10329 vim_free(sname);
10330 return retval;
10331}
10332
10333/*
10334 * ":loadview [nr]"
10335 */
10336 static void
10337ex_loadview(eap)
10338 exarg_T *eap;
10339{
10340 char_u *fname;
10341
10342 fname = get_view_file(*eap->arg);
10343 if (fname != NULL)
10344 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010345 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 vim_free(fname);
10347 }
10348}
10349
10350/*
10351 * Get the name of the view file for the current buffer.
10352 */
10353 static char_u *
10354get_view_file(c)
10355 int c;
10356{
10357 int len = 0;
10358 char_u *p, *s;
10359 char_u *retval;
10360 char_u *sname;
10361
10362 if (curbuf->b_ffname == NULL)
10363 {
10364 EMSG(_(e_noname));
10365 return NULL;
10366 }
10367 sname = home_replace_save(NULL, curbuf->b_ffname);
10368 if (sname == NULL)
10369 return NULL;
10370
10371 /*
10372 * We want a file name without separators, because we're not going to make
10373 * a directory.
10374 * "normal" path separator -> "=+"
10375 * "=" -> "=="
10376 * ":" path separator -> "=-"
10377 */
10378 for (p = sname; *p; ++p)
10379 if (*p == '=' || vim_ispathsep(*p))
10380 ++len;
10381 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10382 if (retval != NULL)
10383 {
10384 STRCPY(retval, p_vdir);
10385 add_pathsep(retval);
10386 s = retval + STRLEN(retval);
10387 for (p = sname; *p; ++p)
10388 {
10389 if (*p == '=')
10390 {
10391 *s++ = '=';
10392 *s++ = '=';
10393 }
10394 else if (vim_ispathsep(*p))
10395 {
10396 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010397#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 || defined(VMS)
10399 if (*p == ':')
10400 *s++ = '-';
10401 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010403 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 }
10405 else
10406 *s++ = *p;
10407 }
10408 *s++ = '=';
10409 *s++ = c;
10410 STRCPY(s, ".vim");
10411 }
10412
10413 vim_free(sname);
10414 return retval;
10415}
10416
10417#endif /* FEAT_SESSION */
10418
10419/*
10420 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10421 * Return FAIL for a write error.
10422 */
10423 int
10424put_eol(fd)
10425 FILE *fd;
10426{
10427 if (
10428#ifdef USE_CRNL
10429 (
10430# ifdef MKSESSION_NL
10431 !mksession_nl &&
10432# endif
10433 (putc('\r', fd) < 0)) ||
10434#endif
10435 (putc('\n', fd) < 0))
10436 return FAIL;
10437 return OK;
10438}
10439
10440/*
10441 * Write a line to "fd".
10442 * Return FAIL for a write error.
10443 */
10444 int
10445put_line(fd, s)
10446 FILE *fd;
10447 char *s;
10448{
10449 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10450 return FAIL;
10451 return OK;
10452}
10453
10454#ifdef FEAT_VIMINFO
10455/*
10456 * ":rviminfo" and ":wviminfo".
10457 */
10458 static void
10459ex_viminfo(eap)
10460 exarg_T *eap;
10461{
10462 char_u *save_viminfo;
10463
10464 save_viminfo = p_viminfo;
10465 if (*p_viminfo == NUL)
10466 p_viminfo = (char_u *)"'100";
10467 if (eap->cmdidx == CMD_rviminfo)
10468 {
10469 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10470 EMSG(_("E195: Cannot open viminfo file for reading"));
10471 }
10472 else
10473 write_viminfo(eap->arg, eap->forceit);
10474 p_viminfo = save_viminfo;
10475}
10476#endif
10477
10478#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010479/*
10480 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010481 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 void
10484dialog_msg(buff, format, fname)
10485 char_u *buff;
10486 char *format;
10487 char_u *fname;
10488{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 if (fname == NULL)
10490 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010491 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492}
10493#endif
10494
10495/*
10496 * ":behave {mswin,xterm}"
10497 */
10498 static void
10499ex_behave(eap)
10500 exarg_T *eap;
10501{
10502 if (STRCMP(eap->arg, "mswin") == 0)
10503 {
10504 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10505 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10506 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10507 set_option_value((char_u *)"keymodel", 0L,
10508 (char_u *)"startsel,stopsel", 0);
10509 }
10510 else if (STRCMP(eap->arg, "xterm") == 0)
10511 {
10512 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10513 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10514 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10515 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10516 }
10517 else
10518 EMSG2(_(e_invarg2), eap->arg);
10519}
10520
10521#ifdef FEAT_AUTOCMD
10522static int filetype_detect = FALSE;
10523static int filetype_plugin = FALSE;
10524static int filetype_indent = FALSE;
10525
10526/*
10527 * ":filetype [plugin] [indent] {on,off,detect}"
10528 * on: Load the filetype.vim file to install autocommands for file types.
10529 * off: Load the ftoff.vim file to remove all autocommands for file types.
10530 * plugin on: load filetype.vim and ftplugin.vim
10531 * plugin off: load ftplugof.vim
10532 * indent on: load filetype.vim and indent.vim
10533 * indent off: load indoff.vim
10534 */
10535 static void
10536ex_filetype(eap)
10537 exarg_T *eap;
10538{
10539 char_u *arg = eap->arg;
10540 int plugin = FALSE;
10541 int indent = FALSE;
10542
10543 if (*eap->arg == NUL)
10544 {
10545 /* Print current status. */
10546 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10547 filetype_detect ? "ON" : "OFF",
10548 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10549 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10550 return;
10551 }
10552
10553 /* Accept "plugin" and "indent" in any order. */
10554 for (;;)
10555 {
10556 if (STRNCMP(arg, "plugin", 6) == 0)
10557 {
10558 plugin = TRUE;
10559 arg = skipwhite(arg + 6);
10560 continue;
10561 }
10562 if (STRNCMP(arg, "indent", 6) == 0)
10563 {
10564 indent = TRUE;
10565 arg = skipwhite(arg + 6);
10566 continue;
10567 }
10568 break;
10569 }
10570 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10571 {
10572 if (*arg == 'o' || !filetype_detect)
10573 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010574 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 filetype_detect = TRUE;
10576 if (plugin)
10577 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010578 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 filetype_plugin = TRUE;
10580 }
10581 if (indent)
10582 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010583 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 filetype_indent = TRUE;
10585 }
10586 }
10587 if (*arg == 'd')
10588 {
10589 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010590 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 }
10592 }
10593 else if (STRCMP(arg, "off") == 0)
10594 {
10595 if (plugin || indent)
10596 {
10597 if (plugin)
10598 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010599 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 filetype_plugin = FALSE;
10601 }
10602 if (indent)
10603 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010604 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 filetype_indent = FALSE;
10606 }
10607 }
10608 else
10609 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010610 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 filetype_detect = FALSE;
10612 }
10613 }
10614 else
10615 EMSG2(_(e_invarg2), arg);
10616}
10617
10618/*
10619 * ":setfiletype {name}"
10620 */
10621 static void
10622ex_setfiletype(eap)
10623 exarg_T *eap;
10624{
10625 if (!did_filetype)
10626 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10627}
10628#endif
10629
10630/*ARGSUSED*/
10631 static void
10632ex_digraphs(eap)
10633 exarg_T *eap;
10634{
10635#ifdef FEAT_DIGRAPHS
10636 if (*eap->arg != NUL)
10637 putdigraph(eap->arg);
10638 else
10639 listdigraphs();
10640#else
10641 EMSG(_("E196: No digraphs in this version"));
10642#endif
10643}
10644
10645 static void
10646ex_set(eap)
10647 exarg_T *eap;
10648{
10649 int flags = 0;
10650
10651 if (eap->cmdidx == CMD_setlocal)
10652 flags = OPT_LOCAL;
10653 else if (eap->cmdidx == CMD_setglobal)
10654 flags = OPT_GLOBAL;
10655#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10656 if (cmdmod.browse && flags == 0)
10657 ex_options(eap);
10658 else
10659#endif
10660 (void)do_set(eap->arg, flags);
10661}
10662
10663#ifdef FEAT_SEARCH_EXTRA
10664/*
10665 * ":nohlsearch"
10666 */
10667/*ARGSUSED*/
10668 static void
10669ex_nohlsearch(eap)
10670 exarg_T *eap;
10671{
10672 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010673 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674}
10675
10676/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010677 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 * Sets nextcmd to the start of the next command, if any. Also called when
10679 * skipping commands to find the next command.
10680 */
10681 static void
10682ex_match(eap)
10683 exarg_T *eap;
10684{
10685 char_u *p;
10686 char_u *end;
10687 int c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010688 int mi;
10689
10690 if (eap->line2 <= 3)
10691 mi = eap->line2 - 1;
10692 else
10693 {
10694 EMSG(e_invcmd);
10695 return;
10696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697
10698 /* First clear any old pattern. */
10699 if (!eap->skip)
10700 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010701 vim_free(curwin->w_match[mi].regprog);
10702 curwin->w_match[mi].regprog = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010703 vim_free(curwin->w_match_pat[mi]);
10704 curwin->w_match_pat[mi] = NULL;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010705 redraw_later(SOME_VALID); /* always need a redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 }
10707
10708 if (ends_excmd(*eap->arg))
10709 end = eap->arg;
10710 else if ((STRNICMP(eap->arg, "none", 4) == 0
10711 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10712 end = eap->arg + 4;
10713 else
10714 {
10715 p = skiptowhite(eap->arg);
10716 if (!eap->skip)
10717 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010718 curwin->w_match_id[mi] = syn_namen2id(eap->arg,
10719 (int)(p - eap->arg));
10720 if (curwin->w_match_id[mi] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 {
10722 EMSG2(_(e_nogroup), eap->arg);
10723 return;
10724 }
10725 }
10726 p = skipwhite(p);
10727 if (*p == NUL)
10728 {
10729 /* There must be two arguments. */
10730 EMSG2(_(e_invarg2), eap->arg);
10731 return;
10732 }
10733 end = skip_regexp(p + 1, *p, TRUE, NULL);
10734 if (!eap->skip)
10735 {
10736 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10737 {
10738 eap->errmsg = e_trailing;
10739 return;
10740 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010741 if (*end != *p)
10742 {
10743 EMSG2(_(e_invarg2), p);
10744 return;
10745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746
10747 c = *end;
10748 *end = NUL;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010749 curwin->w_match[mi].regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010750 if (curwin->w_match[mi].regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 {
10752 EMSG2(_(e_invarg2), p);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010753 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 return;
10755 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010756 curwin->w_match_pat[mi] = vim_strsave(p + 1);
10757 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 }
10759 }
10760 eap->nextcmd = find_nextcmd(end);
10761}
10762#endif
10763
10764#ifdef FEAT_CRYPT
10765/*
10766 * ":X": Get crypt key
10767 */
10768/*ARGSUSED*/
10769 static void
10770ex_X(eap)
10771 exarg_T *eap;
10772{
10773 (void)get_crypt_key(TRUE, TRUE);
10774}
10775#endif
10776
10777#ifdef FEAT_FOLDING
10778 static void
10779ex_fold(eap)
10780 exarg_T *eap;
10781{
10782 if (foldManualAllowed(TRUE))
10783 foldCreate(eap->line1, eap->line2);
10784}
10785
10786 static void
10787ex_foldopen(eap)
10788 exarg_T *eap;
10789{
10790 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10791 eap->forceit, FALSE);
10792}
10793
10794 static void
10795ex_folddo(eap)
10796 exarg_T *eap;
10797{
10798 linenr_T lnum;
10799
10800 /* First set the marks for all lines closed/open. */
10801 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10802 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10803 ml_setmarked(lnum);
10804
10805 /* Execute the command on the marked lines. */
10806 global_exe(eap->arg);
10807 ml_clearmarked(); /* clear rest of the marks */
10808}
10809#endif