blob: 3ba0d37ef5f91fb9fdf5826b851879bdcc774112 [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
903 if (do_profiling)
904 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
913 else if (do_profiling
914 && 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
950 if (do_profiling)
951 {
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. */
1915 if (do_profiling && !ea.skip)
1916 {
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 Moolenaar071d4272004-06-13 20:20:40 +00002179
2180 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2181 {
2182 /* no range allowed */
2183 errormsg = (char_u *)_(e_norange);
2184 goto doend;
2185 }
2186 }
2187
2188 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2189 {
2190 errormsg = (char_u *)_(e_nobang);
2191 goto doend;
2192 }
2193
2194 /*
2195 * Don't complain about the range if it is not used
2196 * (could happen if line_count is accidentally set to 0).
2197 */
2198 if (!ea.skip && !ni)
2199 {
2200 /*
2201 * If the range is backwards, ask for confirmation and, if given, swap
2202 * ea.line1 & ea.line2 so it's forwards again.
2203 * When global command is busy, don't ask, will fail below.
2204 */
2205 if (!global_busy && ea.line1 > ea.line2)
2206 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002207 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002209 if (sourcing || exmode_active)
2210 {
2211 errormsg = (char_u *)_("E493: Backwards range given");
2212 goto doend;
2213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (ask_yesno((char_u *)
2215 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002216 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
2218 lnum = ea.line1;
2219 ea.line1 = ea.line2;
2220 ea.line2 = lnum;
2221 }
2222 if ((errormsg = invalid_range(&ea)) != NULL)
2223 goto doend;
2224 }
2225
2226 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2227 ea.line2 = 1;
2228
2229 correct_range(&ea);
2230
2231#ifdef FEAT_FOLDING
2232 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2233 {
2234 /* Put the first line at the start of a closed fold, put the last line
2235 * at the end of a closed fold. */
2236 (void)hasFolding(ea.line1, &ea.line1, NULL);
2237 (void)hasFolding(ea.line2, NULL, &ea.line2);
2238 }
2239#endif
2240
2241#ifdef FEAT_QUICKFIX
2242 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002243 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 * option here, so things like % get expanded.
2245 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002246 p = replace_makeprg(&ea, p, cmdlinep);
2247 if (p == NULL)
2248 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#endif
2250
2251 /*
2252 * Skip to start of argument.
2253 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2254 */
2255 if (ea.cmdidx == CMD_bang)
2256 ea.arg = p;
2257 else
2258 ea.arg = skipwhite(p);
2259
2260 /*
2261 * Check for "++opt=val" argument.
2262 * Must be first, allow ":w ++enc=utf8 !cmd"
2263 */
2264 if (ea.argt & ARGOPT)
2265 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2266 if (getargopt(&ea) == FAIL && !ni)
2267 {
2268 errormsg = (char_u *)_(e_invarg);
2269 goto doend;
2270 }
2271
2272 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2273 {
2274 if (*ea.arg == '>') /* append */
2275 {
2276 if (*++ea.arg != '>') /* typed wrong */
2277 {
2278 errormsg = (char_u *)_("E494: Use w or w>>");
2279 goto doend;
2280 }
2281 ea.arg = skipwhite(ea.arg + 1);
2282 ea.append = TRUE;
2283 }
2284 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2285 {
2286 ++ea.arg;
2287 ea.usefilter = TRUE;
2288 }
2289 }
2290
2291 if (ea.cmdidx == CMD_read)
2292 {
2293 if (ea.forceit)
2294 {
2295 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2296 ea.forceit = FALSE;
2297 }
2298 else if (*ea.arg == '!') /* :r !filter */
2299 {
2300 ++ea.arg;
2301 ea.usefilter = TRUE;
2302 }
2303 }
2304
2305 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2306 {
2307 ea.amount = 1;
2308 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2309 {
2310 ++ea.arg;
2311 ++ea.amount;
2312 }
2313 ea.arg = skipwhite(ea.arg);
2314 }
2315
2316 /*
2317 * Check for "+command" argument, before checking for next command.
2318 * Don't do this for ":read !cmd" and ":write !cmd".
2319 */
2320 if ((ea.argt & EDITCMD) && !ea.usefilter)
2321 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2322
2323 /*
2324 * Check for '|' to separate commands and '"' to start comments.
2325 * Don't do this for ":read !cmd" and ":write !cmd".
2326 */
2327 if ((ea.argt & TRLBAR) && !ea.usefilter)
2328 separate_nextcmd(&ea);
2329
2330 /*
2331 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002332 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2333 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002335 else if (ea.cmdidx == CMD_bang
2336 || ea.cmdidx == CMD_global
2337 || ea.cmdidx == CMD_vglobal
2338 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 for (p = ea.arg; *p; ++p)
2341 {
2342 /* Remove one backslash before a newline, so that it's possible to
2343 * pass a newline to the shell and also a newline that is preceded
2344 * with a backslash. This makes it impossible to end a shell
2345 * command in a backslash, but that doesn't appear useful.
2346 * Halving the number of backslashes is incompatible with previous
2347 * versions. */
2348 if (*p == '\\' && p[1] == '\n')
2349 mch_memmove(p, p + 1, STRLEN(p));
2350 else if (*p == '\n')
2351 {
2352 ea.nextcmd = p + 1;
2353 *p = NUL;
2354 break;
2355 }
2356 }
2357 }
2358
2359 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2360 {
2361 ea.line1 = 1;
2362 ea.line2 = curbuf->b_ml.ml_line_count;
2363 }
2364
2365 /* accept numbered register only when no count allowed (:put) */
2366 if ( (ea.argt & REGSTR)
2367 && *ea.arg != NUL
2368#ifdef FEAT_USR_CMDS
2369 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2370 && USER_CMDIDX(ea.cmdidx)))
2371 /* Do not allow register = for user commands */
2372 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2373#else
2374 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2375#endif
2376 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2377 {
2378 ea.regname = *ea.arg++;
2379#ifdef FEAT_EVAL
2380 /* for '=' register: accept the rest of the line as an expression */
2381 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2382 {
2383 set_expr_line(vim_strsave(ea.arg));
2384 ea.arg += STRLEN(ea.arg);
2385 }
2386#endif
2387 ea.arg = skipwhite(ea.arg);
2388 }
2389
2390 /*
2391 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2392 * count, it's a buffer name.
2393 */
2394 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2395 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2396 || vim_iswhite(*p)))
2397 {
2398 n = getdigits(&ea.arg);
2399 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002400 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
2402 errormsg = (char_u *)_(e_zerocount);
2403 goto doend;
2404 }
2405 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2406 {
2407 ea.line2 = n;
2408 if (ea.addr_count == 0)
2409 ea.addr_count = 1;
2410 }
2411 else
2412 {
2413 ea.line1 = ea.line2;
2414 ea.line2 += n - 1;
2415 ++ea.addr_count;
2416 /*
2417 * Be vi compatible: no error message for out of range.
2418 */
2419 if (ea.line2 > curbuf->b_ml.ml_line_count)
2420 ea.line2 = curbuf->b_ml.ml_line_count;
2421 }
2422 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002423
2424 /*
2425 * Check for flags: 'l', 'p' and '#'.
2426 */
2427 if (ea.argt & EXFLAGS)
2428 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002430 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002431 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
2433 errormsg = (char_u *)_(e_trailing);
2434 goto doend;
2435 }
2436
2437 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2438 {
2439 errormsg = (char_u *)_(e_argreq);
2440 goto doend;
2441 }
2442
2443#ifdef FEAT_EVAL
2444 /*
2445 * Skip the command when it's not going to be executed.
2446 * The commands like :if, :endif, etc. always need to be executed.
2447 * Also make an exception for commands that handle a trailing command
2448 * themselves.
2449 */
2450 if (ea.skip)
2451 {
2452 switch (ea.cmdidx)
2453 {
2454 /* commands that need evaluation */
2455 case CMD_while:
2456 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002457 case CMD_for:
2458 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 case CMD_if:
2460 case CMD_elseif:
2461 case CMD_else:
2462 case CMD_endif:
2463 case CMD_try:
2464 case CMD_catch:
2465 case CMD_finally:
2466 case CMD_endtry:
2467 case CMD_function:
2468 break;
2469
2470 /* Commands that handle '|' themselves. Check: A command should
2471 * either have the TRLBAR flag, appear in this list or appear in
2472 * the list at ":help :bar". */
2473 case CMD_aboveleft:
2474 case CMD_and:
2475 case CMD_belowright:
2476 case CMD_botright:
2477 case CMD_browse:
2478 case CMD_call:
2479 case CMD_confirm:
2480 case CMD_delfunction:
2481 case CMD_djump:
2482 case CMD_dlist:
2483 case CMD_dsearch:
2484 case CMD_dsplit:
2485 case CMD_echo:
2486 case CMD_echoerr:
2487 case CMD_echomsg:
2488 case CMD_echon:
2489 case CMD_execute:
2490 case CMD_help:
2491 case CMD_hide:
2492 case CMD_ijump:
2493 case CMD_ilist:
2494 case CMD_isearch:
2495 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002496 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 case CMD_keepjumps:
2498 case CMD_keepmarks:
2499 case CMD_leftabove:
2500 case CMD_let:
2501 case CMD_lockmarks:
2502 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002503 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 case CMD_perl:
2505 case CMD_psearch:
2506 case CMD_python:
2507 case CMD_return:
2508 case CMD_rightbelow:
2509 case CMD_ruby:
2510 case CMD_silent:
2511 case CMD_smagic:
2512 case CMD_snomagic:
2513 case CMD_substitute:
2514 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002515 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 case CMD_tcl:
2517 case CMD_throw:
2518 case CMD_tilde:
2519 case CMD_topleft:
2520 case CMD_unlet:
2521 case CMD_verbose:
2522 case CMD_vertical:
2523 break;
2524
2525 default: goto doend;
2526 }
2527 }
2528#endif
2529
2530 if (ea.argt & XFILE)
2531 {
2532 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2533 goto doend;
2534 }
2535
2536#ifdef FEAT_LISTCMDS
2537 /*
2538 * Accept buffer name. Cannot be used at the same time with a buffer
2539 * number. Don't do this for a user command.
2540 */
2541 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2542# ifdef FEAT_USR_CMDS
2543 && !USER_CMDIDX(ea.cmdidx)
2544# endif
2545 )
2546 {
2547 /*
2548 * :bdelete, :bwipeout and :bunload take several arguments, separated
2549 * by spaces: find next space (skipping over escaped characters).
2550 * The others take one argument: ignore trailing spaces.
2551 */
2552 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2553 || ea.cmdidx == CMD_bunload)
2554 p = skiptowhite_esc(ea.arg);
2555 else
2556 {
2557 p = ea.arg + STRLEN(ea.arg);
2558 while (p > ea.arg && vim_iswhite(p[-1]))
2559 --p;
2560 }
2561 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2562 if (ea.line2 < 0) /* failed */
2563 goto doend;
2564 ea.addr_count = 1;
2565 ea.arg = skipwhite(p);
2566 }
2567#endif
2568
2569/*
2570 * 6. switch on command name
2571 *
2572 * The "ea" structure holds the arguments that can be used.
2573 */
2574 ea.cmdlinep = cmdlinep;
2575 ea.getline = getline;
2576 ea.cookie = cookie;
2577#ifdef FEAT_EVAL
2578 ea.cstack = cstack;
2579#endif
2580
2581#ifdef FEAT_USR_CMDS
2582 if (USER_CMDIDX(ea.cmdidx))
2583 {
2584 /*
2585 * Execute a user-defined command.
2586 */
2587 do_ucmd(&ea);
2588 }
2589 else
2590#endif
2591 {
2592 /*
2593 * Call the function to execute the command.
2594 */
2595 ea.errmsg = NULL;
2596 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2597 if (ea.errmsg != NULL)
2598 errormsg = (char_u *)_(ea.errmsg);
2599 }
2600
2601#ifdef FEAT_EVAL
2602 /*
2603 * If the command just executed called do_cmdline(), any throw or ":return"
2604 * or ":finish" encountered there must also check the cstack of the still
2605 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2606 * exception, or reanimate a returned function or finished script file and
2607 * return or finish it again.
2608 */
2609 if (need_rethrow)
2610 do_throw(cstack);
2611 else if (check_cstack)
2612 {
2613 if (source_finished(getline, cookie))
2614 do_finish(&ea, TRUE);
2615 else if (getline_equal(getline, cookie, get_func_line)
2616 && current_func_returned())
2617 do_return(&ea, TRUE, FALSE, NULL);
2618 }
2619 need_rethrow = check_cstack = FALSE;
2620#endif
2621
2622doend:
2623 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2624 curwin->w_cursor.lnum = 1;
2625
2626 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2627 {
2628 if (sourcing)
2629 {
2630 if (errormsg != IObuff)
2631 {
2632 STRCPY(IObuff, errormsg);
2633 errormsg = IObuff;
2634 }
2635 STRCAT(errormsg, ": ");
2636 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2637 }
2638 emsg(errormsg);
2639 }
2640#ifdef FEAT_EVAL
2641 do_errthrow(cstack,
2642 (ea.cmdidx != CMD_SIZE
2643# ifdef FEAT_USR_CMDS
2644 && !USER_CMDIDX(ea.cmdidx)
2645# endif
2646 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2647#endif
2648
2649 if (verbose_save >= 0)
2650 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002651#ifdef FEAT_AUTOCMD
2652 if (cmdmod.save_ei != NULL)
2653 {
2654 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002655 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2656 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002657 free_string_option(cmdmod.save_ei);
2658 }
2659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 cmdmod = save_cmdmod;
2662
2663 if (did_silent > 0)
2664 {
2665 /* messages could be enabled for a serious error, need to check if the
2666 * counters don't become negative */
2667 msg_silent -= did_silent;
2668 if (msg_silent < 0)
2669 msg_silent = 0;
2670 emsg_silent -= did_esilent;
2671 if (emsg_silent < 0)
2672 emsg_silent = 0;
2673 /* Restore msg_scroll, it's set by file I/O commands, even when no
2674 * message is actually displayed. */
2675 msg_scroll = save_msg_scroll;
2676 }
2677
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002678#ifdef HAVE_SANDBOX
2679 if (did_sandbox)
2680 --sandbox;
2681#endif
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2684 ea.nextcmd = NULL;
2685
2686#ifdef FEAT_EVAL
2687 --ex_nesting_level;
2688#endif
2689
2690 return ea.nextcmd;
2691}
2692#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002693 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#endif
2695
2696/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002697 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 * If there is a match advance "pp" to the argument and return TRUE.
2699 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002700 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002702 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 char *cmd; /* name of command */
2704 int len; /* required length */
2705{
2706 int i;
2707
2708 for (i = 0; cmd[i] != NUL; ++i)
2709 if (cmd[i] != (*pp)[i])
2710 break;
2711 if (i >= len && !isalpha((*pp)[i]))
2712 {
2713 *pp = skipwhite(*pp + i);
2714 return TRUE;
2715 }
2716 return FALSE;
2717}
2718
2719/*
2720 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002721 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002723 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 * Returns NULL for an ambiguous user command.
2725 */
2726/*ARGSUSED*/
2727 static char_u *
2728find_command(eap, full)
2729 exarg_T *eap;
2730 int *full;
2731{
2732 int len;
2733 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002734 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 /*
2737 * Isolate the command and search for it in the command table.
2738 * Exeptions:
2739 * - the 'k' command can directly be followed by any character.
2740 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2741 * but :sre[wind] is another command, as are :scrip[tnames],
2742 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002743 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 */
2745 p = eap->cmd;
2746 if (*p == 'k')
2747 {
2748 eap->cmdidx = CMD_k;
2749 ++p;
2750 }
2751 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002752 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2753 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 || p[1] == 'g'
2755 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2756 || p[1] == 'I'
2757 || (p[1] == 'r' && p[2] != 'e')))
2758 {
2759 eap->cmdidx = CMD_substitute;
2760 ++p;
2761 }
2762 else
2763 {
2764 while (ASCII_ISALPHA(*p))
2765 ++p;
2766 /* check for non-alpha command */
2767 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2768 ++p;
2769 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002770 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2771 {
2772 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2773 * :delete with the 'l' flag. Same for 'p'. */
2774 for (i = 0; i < len; ++i)
2775 if (eap->cmd[i] != "delete"[i])
2776 break;
2777 if (i == len - 1)
2778 {
2779 --len;
2780 if (p[-1] == 'l')
2781 eap->flags |= EXFLAG_LIST;
2782 else
2783 eap->flags |= EXFLAG_PRINT;
2784 }
2785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 if (ASCII_ISLOWER(*eap->cmd))
2788 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2789 else
2790 eap->cmdidx = cmdidxs[26];
2791
2792 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2793 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2794 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2795 (size_t)len) == 0)
2796 {
2797#ifdef FEAT_EVAL
2798 if (full != NULL
2799 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2800 *full = TRUE;
2801#endif
2802 break;
2803 }
2804
2805#ifdef FEAT_USR_CMDS
2806 /* Look for a user defined command as a last resort */
2807 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2808 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002809 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 while (ASCII_ISALNUM(*p))
2811 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002812 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
2814#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002815 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 eap->cmdidx = CMD_SIZE;
2817 }
2818
2819 return p;
2820}
2821
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002822#ifdef FEAT_USR_CMDS
2823/*
2824 * Search for a user command that matches "eap->cmd".
2825 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2826 * Return a pointer to just after the command.
2827 * Return NULL if there is no matching command.
2828 */
2829 static char_u *
2830find_ucmd(eap, p, full, xp, compl)
2831 exarg_T *eap;
2832 char_u *p; /* end of the command (possibly including count) */
2833 int *full; /* set to TRUE for a full match */
2834 expand_T *xp; /* used for completion, NULL otherwise */
2835 int *compl; /* completion flags or NULL */
2836{
2837 int len = (int)(p - eap->cmd);
2838 int j, k, matchlen = 0;
2839 ucmd_T *uc;
2840 int found = FALSE;
2841 int possible = FALSE;
2842 char_u *cp, *np; /* Point into typed cmd and test name */
2843 garray_T *gap;
2844 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2845 only full match global is accepted. */
2846
2847 /*
2848 * Look for buffer-local user commands first, then global ones.
2849 */
2850 gap = &curbuf->b_ucmds;
2851 for (;;)
2852 {
2853 for (j = 0; j < gap->ga_len; ++j)
2854 {
2855 uc = USER_CMD_GA(gap, j);
2856 cp = eap->cmd;
2857 np = uc->uc_name;
2858 k = 0;
2859 while (k < len && *np != NUL && *cp++ == *np++)
2860 k++;
2861 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2862 {
2863 /* If finding a second match, the command is ambiguous. But
2864 * not if a buffer-local command wasn't a full match and a
2865 * global command is a full match. */
2866 if (k == len && found && *np != NUL)
2867 {
2868 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002869 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002870 amb_local = TRUE;
2871 }
2872
2873 if (!found || (k == len && *np == NUL))
2874 {
2875 /* If we matched up to a digit, then there could
2876 * be another command including the digit that we
2877 * should use instead.
2878 */
2879 if (k == len)
2880 found = TRUE;
2881 else
2882 possible = TRUE;
2883
2884 if (gap == &ucmds)
2885 eap->cmdidx = CMD_USER;
2886 else
2887 eap->cmdidx = CMD_USER_BUF;
2888 eap->argt = uc->uc_argt;
2889 eap->useridx = j;
2890
2891# ifdef FEAT_CMDL_COMPL
2892 if (compl != NULL)
2893 *compl = uc->uc_compl;
2894# ifdef FEAT_EVAL
2895 if (xp != NULL)
2896 {
2897 xp->xp_arg = uc->uc_compl_arg;
2898 xp->xp_scriptID = uc->uc_scriptID;
2899 }
2900# endif
2901# endif
2902 /* Do not search for further abbreviations
2903 * if this is an exact match. */
2904 matchlen = k;
2905 if (k == len && *np == NUL)
2906 {
2907 if (full != NULL)
2908 *full = TRUE;
2909 amb_local = FALSE;
2910 break;
2911 }
2912 }
2913 }
2914 }
2915
2916 /* Stop if we found a full match or searched all. */
2917 if (j < gap->ga_len || gap == &ucmds)
2918 break;
2919 gap = &ucmds;
2920 }
2921
2922 /* Only found ambiguous matches. */
2923 if (amb_local)
2924 {
2925 if (xp != NULL)
2926 xp->xp_context = EXPAND_UNSUCCESSFUL;
2927 return NULL;
2928 }
2929
2930 /* The match we found may be followed immediately by a number. Move "p"
2931 * back to point to it. */
2932 if (found || possible)
2933 return p + (matchlen - len);
2934 return p;
2935}
2936#endif
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938#if defined(FEAT_EVAL) || defined(PROTO)
2939/*
2940 * Return > 0 if an Ex command "name" exists.
2941 * Return 2 if there is an exact match.
2942 * Return 3 if there is an ambiguous match.
2943 */
2944 int
2945cmd_exists(name)
2946 char_u *name;
2947{
2948 exarg_T ea;
2949 int full = FALSE;
2950 int i;
2951 int j;
2952 static struct cmdmod
2953 {
2954 char *name;
2955 int minlen;
2956 } cmdmods[] = {
2957 {"aboveleft", 3},
2958 {"belowright", 3},
2959 {"botright", 2},
2960 {"browse", 3},
2961 {"confirm", 4},
2962 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002963 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {"keepjumps", 5},
2965 {"keepmarks", 3},
2966 {"leftabove", 5},
2967 {"lockmarks", 3},
2968 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002969 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 {"silent", 3},
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002971 {"tab", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 {"topleft", 2},
2973 {"verbose", 4},
2974 {"vertical", 4},
2975 };
2976
2977 /* Check command modifiers. */
2978 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2979 {
2980 for (j = 0; name[j] != NUL; ++j)
2981 if (name[j] != cmdmods[i].name[j])
2982 break;
2983 if (name[j] == NUL && j >= cmdmods[i].minlen)
2984 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2985 }
2986
2987 /* Check built-in commands and user defined commands. */
2988 ea.cmd = name;
2989 ea.cmdidx = (cmdidx_T)0;
2990 if (find_command(&ea, &full) == NULL)
2991 return 3;
2992 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2993}
2994#endif
2995
2996/*
2997 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2998 * we don't need/want deleted. Maybe this could be done better if we didn't
2999 * repeat all this stuff. The only problem is that they may not stay
3000 * perfectly compatible with each other, but then the command line syntax
3001 * probably won't change that much -- webb.
3002 */
3003 char_u *
3004set_one_cmd_context(xp, buff)
3005 expand_T *xp;
3006 char_u *buff; /* buffer for command string */
3007{
3008 char_u *p;
3009 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003010 int len = 0;
3011 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3013 int compl = EXPAND_NOTHING;
3014#endif
3015#ifdef FEAT_CMDL_COMPL
3016 int delim;
3017#endif
3018 int forceit = FALSE;
3019 int usefilter = FALSE; /* filter instead of file name */
3020
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003021 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 xp->xp_pattern = buff;
3023 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003024 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026/*
3027 * 2. skip comment lines and leading space, colons or bars
3028 */
3029 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3030 ;
3031 xp->xp_pattern = cmd;
3032
3033 if (*cmd == NUL)
3034 return NULL;
3035 if (*cmd == '"') /* ignore comment lines */
3036 {
3037 xp->xp_context = EXPAND_NOTHING;
3038 return NULL;
3039 }
3040
3041/*
3042 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3043 */
3044 cmd = skip_range(cmd, &xp->xp_context);
3045
3046/*
3047 * 4. parse command
3048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 xp->xp_pattern = cmd;
3050 if (*cmd == NUL)
3051 return NULL;
3052 if (*cmd == '"')
3053 {
3054 xp->xp_context = EXPAND_NOTHING;
3055 return NULL;
3056 }
3057
3058 if (*cmd == '|' || *cmd == '\n')
3059 return cmd + 1; /* There's another command */
3060
3061 /*
3062 * Isolate the command and search for it in the command table.
3063 * Exceptions:
3064 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003065 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3067 */
3068 if (*cmd == 'k' && cmd[1] != 'e')
3069 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003070 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 p = cmd + 1;
3072 }
3073 else
3074 {
3075 p = cmd;
3076 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3077 ++p;
3078 /* check for non-alpha command */
3079 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3080 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003081 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003083 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 {
3085 xp->xp_context = EXPAND_UNSUCCESSFUL;
3086 return NULL;
3087 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003088 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3089 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3090 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 break;
3092
3093#ifdef FEAT_USR_CMDS
3094 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3095 {
3096 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3097 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003098 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
3100#endif
3101 }
3102
3103 /*
3104 * If the cursor is touching the command, and it ends in an alpha-numeric
3105 * character, complete the command name.
3106 */
3107 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3108 return NULL;
3109
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003110 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3113 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003114 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 p = cmd + 1;
3116 }
3117#ifdef FEAT_USR_CMDS
3118 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3119 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003120 ea.cmd = cmd;
3121 p = find_ucmd(&ea, p, NULL, xp,
3122# if defined(FEAT_CMDL_COMPL)
3123 &compl
3124# else
3125 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003127 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003128 if (p == NULL)
3129 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
3131#endif
3132 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003133 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 {
3135 /* Not still touching the command and it was an illegal one */
3136 xp->xp_context = EXPAND_UNSUCCESSFUL;
3137 return NULL;
3138 }
3139
3140 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3141
3142 if (*p == '!') /* forced commands */
3143 {
3144 forceit = TRUE;
3145 ++p;
3146 }
3147
3148/*
3149 * 5. parse arguments
3150 */
3151#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003152 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003154 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156 arg = skipwhite(p);
3157
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003158 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
3160 if (*arg == '>') /* append */
3161 {
3162 if (*++arg == '>')
3163 ++arg;
3164 arg = skipwhite(arg);
3165 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003166 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 ++arg;
3169 usefilter = TRUE;
3170 }
3171 }
3172
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003173 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
3175 usefilter = forceit; /* :r! filter if forced */
3176 if (*arg == '!') /* :r !filter */
3177 {
3178 ++arg;
3179 usefilter = TRUE;
3180 }
3181 }
3182
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
3185 while (*arg == *cmd) /* allow any number of '>' or '<' */
3186 ++arg;
3187 arg = skipwhite(arg);
3188 }
3189
3190 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 /* Check if we're in the +command */
3194 p = arg + 1;
3195 arg = skip_cmd_arg(arg, FALSE);
3196
3197 /* Still touching the command after '+'? */
3198 if (*arg == NUL)
3199 return p;
3200
3201 /* Skip space(s) after +command to get to the real argument */
3202 arg = skipwhite(arg);
3203 }
3204
3205 /*
3206 * Check for '|' to separate commands and '"' to start comments.
3207 * Don't do this for ":read !cmd" and ":write !cmd".
3208 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003209 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
3211 p = arg;
3212 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003213 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 p += 2;
3215 while (*p)
3216 {
3217 if (*p == Ctrl_V)
3218 {
3219 if (p[1] != NUL)
3220 ++p;
3221 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003222 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 || *p == '|' || *p == '\n')
3224 {
3225 if (*(p - 1) != '\\')
3226 {
3227 if (*p == '|' || *p == '\n')
3228 return p + 1;
3229 return NULL; /* It's a comment */
3230 }
3231 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003232 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234 }
3235
3236 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 vim_strchr((char_u *)"|\"", *arg) == NULL)
3239 return NULL;
3240
3241 /* Find start of last argument (argument just before cursor): */
3242 p = buff + STRLEN(buff);
3243 while (p != arg && *p != ' ' && *p != TAB)
3244 p--;
3245 if (*p == ' ' || *p == TAB)
3246 p++;
3247 xp->xp_pattern = p;
3248
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003249 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 {
3251 int in_quote = FALSE;
3252 char_u *bow = NULL; /* Beginning of word */
3253
3254 /*
3255 * Allow spaces within back-quotes to count as part of the argument
3256 * being expanded.
3257 */
3258 xp->xp_pattern = skipwhite(arg);
3259 for (p = xp->xp_pattern; *p; )
3260 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003261 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 ++p;
3263#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003264 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#else
3266 else if (vim_iswhite(*p))
3267#endif
3268 {
3269 p = skipwhite(p);
3270 if (in_quote)
3271 bow = p;
3272 else
3273 xp->xp_pattern = p;
3274 --p;
3275 }
3276 else if (*p == '`')
3277 {
3278 if (!in_quote)
3279 {
3280 xp->xp_pattern = p;
3281 bow = p + 1;
3282 }
3283 in_quote = !in_quote;
3284 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003285 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
3287
3288 /*
3289 * If we are still inside the quotes, and we passed a space, just
3290 * expand from there.
3291 */
3292 if (bow != NULL && in_quote)
3293 xp->xp_pattern = bow;
3294 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003295
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003296#ifndef BACKSLASH_IN_FILENAME
3297 /* For a shell command more chars need to be escaped. */
3298 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003299 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003300 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003301
3302 /* When still after the command name expand executables. */
3303 if (xp->xp_pattern == skipwhite(arg))
3304 xp->xp_context = EXPAND_SHELLCMD;
3305 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
3308 /* Check for environment variable */
3309 if (*xp->xp_pattern == '$'
3310#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3311 || *xp->xp_pattern == '%'
3312#endif
3313 )
3314 {
3315 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3316 if (!vim_isIDc(*p))
3317 break;
3318 if (*p == NUL)
3319 {
3320 xp->xp_context = EXPAND_ENV_VARS;
3321 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003322#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3323 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003324 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003325 compl = EXPAND_ENV_VARS;
3326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 }
3329 }
3330
3331/*
3332 * 6. switch on command name
3333 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003334 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
3336 case CMD_cd:
3337 case CMD_chdir:
3338 case CMD_lcd:
3339 case CMD_lchdir:
3340 if (xp->xp_context == EXPAND_FILES)
3341 xp->xp_context = EXPAND_DIRECTORIES;
3342 break;
3343 case CMD_help:
3344 xp->xp_context = EXPAND_HELP;
3345 xp->xp_pattern = arg;
3346 break;
3347
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003348 /* Command modifiers: return the argument.
3349 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003351 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 case CMD_belowright:
3353 case CMD_botright:
3354 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003355 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003357 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 case CMD_folddoclosed:
3359 case CMD_folddoopen:
3360 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003361 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 case CMD_keepjumps:
3363 case CMD_keepmarks:
3364 case CMD_leftabove:
3365 case CMD_lockmarks:
3366 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003367 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003369 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 case CMD_topleft:
3371 case CMD_verbose:
3372 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003373 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 return arg;
3375
3376#ifdef FEAT_SEARCH_EXTRA
3377 case CMD_match:
3378 if (*arg == NUL || !ends_excmd(*arg))
3379 {
3380 /* Dummy call to clear variables. */
3381 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3382 xp->xp_context = EXPAND_HIGHLIGHT;
3383 xp->xp_pattern = arg;
3384 arg = skipwhite(skiptowhite(arg));
3385 if (*arg != NUL)
3386 {
3387 xp->xp_context = EXPAND_NOTHING;
3388 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3389 }
3390 }
3391 return find_nextcmd(arg);
3392#endif
3393
3394#ifdef FEAT_CMDL_COMPL
3395/*
3396 * All completion for the +cmdline_compl feature goes here.
3397 */
3398
3399# ifdef FEAT_USR_CMDS
3400 case CMD_command:
3401 /* Check for attributes */
3402 while (*arg == '-')
3403 {
3404 arg++; /* Skip "-" */
3405 p = skiptowhite(arg);
3406 if (*p == NUL)
3407 {
3408 /* Cursor is still in the attribute */
3409 p = vim_strchr(arg, '=');
3410 if (p == NULL)
3411 {
3412 /* No "=", so complete attribute names */
3413 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3414 xp->xp_pattern = arg;
3415 return NULL;
3416 }
3417
3418 /* For the -complete and -nargs attributes, we complete
3419 * their arguments as well.
3420 */
3421 if (STRNICMP(arg, "complete", p - arg) == 0)
3422 {
3423 xp->xp_context = EXPAND_USER_COMPLETE;
3424 xp->xp_pattern = p + 1;
3425 return NULL;
3426 }
3427 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3428 {
3429 xp->xp_context = EXPAND_USER_NARGS;
3430 xp->xp_pattern = p + 1;
3431 return NULL;
3432 }
3433 return NULL;
3434 }
3435 arg = skipwhite(p);
3436 }
3437
3438 /* After the attributes comes the new command name */
3439 p = skiptowhite(arg);
3440 if (*p == NUL)
3441 {
3442 xp->xp_context = EXPAND_USER_COMMANDS;
3443 xp->xp_pattern = arg;
3444 break;
3445 }
3446
3447 /* And finally comes a normal command */
3448 return skipwhite(p);
3449
3450 case CMD_delcommand:
3451 xp->xp_context = EXPAND_USER_COMMANDS;
3452 xp->xp_pattern = arg;
3453 break;
3454# endif
3455
3456 case CMD_global:
3457 case CMD_vglobal:
3458 delim = *arg; /* get the delimiter */
3459 if (delim)
3460 ++arg; /* skip delimiter if there is one */
3461
3462 while (arg[0] != NUL && arg[0] != delim)
3463 {
3464 if (arg[0] == '\\' && arg[1] != NUL)
3465 ++arg;
3466 ++arg;
3467 }
3468 if (arg[0] != NUL)
3469 return arg + 1;
3470 break;
3471 case CMD_and:
3472 case CMD_substitute:
3473 delim = *arg;
3474 if (delim)
3475 {
3476 /* skip "from" part */
3477 ++arg;
3478 arg = skip_regexp(arg, delim, p_magic, NULL);
3479 }
3480 /* skip "to" part */
3481 while (arg[0] != NUL && arg[0] != delim)
3482 {
3483 if (arg[0] == '\\' && arg[1] != NUL)
3484 ++arg;
3485 ++arg;
3486 }
3487 if (arg[0] != NUL) /* skip delimiter */
3488 ++arg;
3489 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3490 ++arg;
3491 if (arg[0] != NUL)
3492 return arg;
3493 break;
3494 case CMD_isearch:
3495 case CMD_dsearch:
3496 case CMD_ilist:
3497 case CMD_dlist:
3498 case CMD_ijump:
3499 case CMD_psearch:
3500 case CMD_djump:
3501 case CMD_isplit:
3502 case CMD_dsplit:
3503 arg = skipwhite(skipdigits(arg)); /* skip count */
3504 if (*arg == '/') /* Match regexp, not just whole words */
3505 {
3506 for (++arg; *arg && *arg != '/'; arg++)
3507 if (*arg == '\\' && arg[1] != NUL)
3508 arg++;
3509 if (*arg)
3510 {
3511 arg = skipwhite(arg + 1);
3512
3513 /* Check for trailing illegal characters */
3514 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3515 xp->xp_context = EXPAND_NOTHING;
3516 else
3517 return arg;
3518 }
3519 }
3520 break;
3521#ifdef FEAT_AUTOCMD
3522 case CMD_autocmd:
3523 return set_context_in_autocmd(xp, arg, FALSE);
3524
3525 case CMD_doautocmd:
3526 return set_context_in_autocmd(xp, arg, TRUE);
3527#endif
3528 case CMD_set:
3529 set_context_in_set_cmd(xp, arg, 0);
3530 break;
3531 case CMD_setglobal:
3532 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3533 break;
3534 case CMD_setlocal:
3535 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3536 break;
3537 case CMD_tag:
3538 case CMD_stag:
3539 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003540 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 case CMD_tselect:
3542 case CMD_stselect:
3543 case CMD_ptselect:
3544 case CMD_tjump:
3545 case CMD_stjump:
3546 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003547 if (*p_wop != NUL)
3548 xp->xp_context = EXPAND_TAGS_LISTFILES;
3549 else
3550 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 xp->xp_pattern = arg;
3552 break;
3553 case CMD_augroup:
3554 xp->xp_context = EXPAND_AUGROUP;
3555 xp->xp_pattern = arg;
3556 break;
3557#ifdef FEAT_SYN_HL
3558 case CMD_syntax:
3559 set_context_in_syntax_cmd(xp, arg);
3560 break;
3561#endif
3562#ifdef FEAT_EVAL
3563 case CMD_let:
3564 case CMD_if:
3565 case CMD_elseif:
3566 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003567 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 case CMD_echo:
3569 case CMD_echon:
3570 case CMD_execute:
3571 case CMD_echomsg:
3572 case CMD_echoerr:
3573 case CMD_call:
3574 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003575 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 break;
3577
3578 case CMD_unlet:
3579 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3580 arg = xp->xp_pattern + 1;
3581 xp->xp_context = EXPAND_USER_VARS;
3582 xp->xp_pattern = arg;
3583 break;
3584
3585 case CMD_function:
3586 case CMD_delfunction:
3587 xp->xp_context = EXPAND_USER_FUNC;
3588 xp->xp_pattern = arg;
3589 break;
3590
3591 case CMD_echohl:
3592 xp->xp_context = EXPAND_HIGHLIGHT;
3593 xp->xp_pattern = arg;
3594 break;
3595#endif
3596 case CMD_highlight:
3597 set_context_in_highlight_cmd(xp, arg);
3598 break;
3599#ifdef FEAT_LISTCMDS
3600 case CMD_bdelete:
3601 case CMD_bwipeout:
3602 case CMD_bunload:
3603 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3604 arg = xp->xp_pattern + 1;
3605 /*FALLTHROUGH*/
3606 case CMD_buffer:
3607 case CMD_sbuffer:
3608 case CMD_checktime:
3609 xp->xp_context = EXPAND_BUFFERS;
3610 xp->xp_pattern = arg;
3611 break;
3612#endif
3613#ifdef FEAT_USR_CMDS
3614 case CMD_USER:
3615 case CMD_USER_BUF:
3616 if (compl != EXPAND_NOTHING)
3617 {
3618 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003619 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621# ifdef FEAT_MENU
3622 if (compl == EXPAND_MENUS)
3623 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3624# endif
3625 if (compl == EXPAND_COMMANDS)
3626 return arg;
3627 if (compl == EXPAND_MAPPINGS)
3628 return set_context_in_map_cmd(xp, (char_u *)"map",
3629 arg, forceit, FALSE, FALSE, CMD_map);
3630 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3631 arg = xp->xp_pattern + 1;
3632 xp->xp_pattern = arg;
3633 }
3634 xp->xp_context = compl;
3635 }
3636 break;
3637#endif
3638 case CMD_map: case CMD_noremap:
3639 case CMD_nmap: case CMD_nnoremap:
3640 case CMD_vmap: case CMD_vnoremap:
3641 case CMD_omap: case CMD_onoremap:
3642 case CMD_imap: case CMD_inoremap:
3643 case CMD_cmap: case CMD_cnoremap:
3644 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003645 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 case CMD_unmap:
3647 case CMD_nunmap:
3648 case CMD_vunmap:
3649 case CMD_ounmap:
3650 case CMD_iunmap:
3651 case CMD_cunmap:
3652 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003653 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 case CMD_abbreviate: case CMD_noreabbrev:
3655 case CMD_cabbrev: case CMD_cnoreabbrev:
3656 case CMD_iabbrev: case CMD_inoreabbrev:
3657 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003658 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 case CMD_unabbreviate:
3660 case CMD_cunabbrev:
3661 case CMD_iunabbrev:
3662 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003663 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664#ifdef FEAT_MENU
3665 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3666 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3667 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3668 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3669 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3670 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3671 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3672 case CMD_tmenu: case CMD_tunmenu:
3673 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3674 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3675#endif
3676
3677 case CMD_colorscheme:
3678 xp->xp_context = EXPAND_COLORS;
3679 xp->xp_pattern = arg;
3680 break;
3681
3682 case CMD_compiler:
3683 xp->xp_context = EXPAND_COMPILER;
3684 xp->xp_pattern = arg;
3685 break;
3686
3687#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3688 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3689 case CMD_language:
3690 if (*skiptowhite(arg) == NUL)
3691 {
3692 xp->xp_context = EXPAND_LANGUAGE;
3693 xp->xp_pattern = arg;
3694 }
3695 else
3696 xp->xp_context = EXPAND_NOTHING;
3697 break;
3698#endif
3699
3700#endif /* FEAT_CMDL_COMPL */
3701
3702 default:
3703 break;
3704 }
3705 return NULL;
3706}
3707
3708/*
3709 * skip a range specifier of the form: addr [,addr] [;addr] ..
3710 *
3711 * Backslashed delimiters after / or ? will be skipped, and commands will
3712 * not be expanded between /'s and ?'s or after "'".
3713 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003714 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 * Returns the "cmd" pointer advanced to beyond the range.
3716 */
3717 char_u *
3718skip_range(cmd, ctx)
3719 char_u *cmd;
3720 int *ctx; /* pointer to xp_context or NULL */
3721{
3722 int delim;
3723
Bram Moolenaardf177f62005-02-22 08:39:57 +00003724 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
3726 if (*cmd == '\'')
3727 {
3728 if (*++cmd == NUL && ctx != NULL)
3729 *ctx = EXPAND_NOTHING;
3730 }
3731 else if (*cmd == '/' || *cmd == '?')
3732 {
3733 delim = *cmd++;
3734 while (*cmd != NUL && *cmd != delim)
3735 if (*cmd++ == '\\' && *cmd != NUL)
3736 ++cmd;
3737 if (*cmd == NUL && ctx != NULL)
3738 *ctx = EXPAND_NOTHING;
3739 }
3740 if (*cmd != NUL)
3741 ++cmd;
3742 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003743
3744 /* Skip ":" and white space. */
3745 while (*cmd == ':')
3746 cmd = skipwhite(cmd + 1);
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 return cmd;
3749}
3750
3751/*
3752 * get a single EX address
3753 *
3754 * Set ptr to the next character after the part that was interpreted.
3755 * Set ptr to NULL when an error is encountered.
3756 *
3757 * Return MAXLNUM when no Ex address was found.
3758 */
3759 static linenr_T
3760get_address(ptr, skip, to_other_file)
3761 char_u **ptr;
3762 int skip; /* only skip the address, don't use it */
3763 int to_other_file; /* flag: may jump to other file */
3764{
3765 int c;
3766 int i;
3767 long n;
3768 char_u *cmd;
3769 pos_T pos;
3770 pos_T *fp;
3771 linenr_T lnum;
3772
3773 cmd = skipwhite(*ptr);
3774 lnum = MAXLNUM;
3775 do
3776 {
3777 switch (*cmd)
3778 {
3779 case '.': /* '.' - Cursor position */
3780 ++cmd;
3781 lnum = curwin->w_cursor.lnum;
3782 break;
3783
3784 case '$': /* '$' - last line */
3785 ++cmd;
3786 lnum = curbuf->b_ml.ml_line_count;
3787 break;
3788
3789 case '\'': /* ''' - mark */
3790 if (*++cmd == NUL)
3791 {
3792 cmd = NULL;
3793 goto error;
3794 }
3795 if (skip)
3796 ++cmd;
3797 else
3798 {
3799 /* Only accept a mark in another file when it is
3800 * used by itself: ":'M". */
3801 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3802 ++cmd;
3803 if (fp == (pos_T *)-1)
3804 /* Jumped to another file. */
3805 lnum = curwin->w_cursor.lnum;
3806 else
3807 {
3808 if (check_mark(fp) == FAIL)
3809 {
3810 cmd = NULL;
3811 goto error;
3812 }
3813 lnum = fp->lnum;
3814 }
3815 }
3816 break;
3817
3818 case '/':
3819 case '?': /* '/' or '?' - search */
3820 c = *cmd++;
3821 if (skip) /* skip "/pat/" */
3822 {
3823 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3824 if (*cmd == c)
3825 ++cmd;
3826 }
3827 else
3828 {
3829 pos = curwin->w_cursor; /* save curwin->w_cursor */
3830 /*
3831 * When '/' or '?' follows another address, start
3832 * from there.
3833 */
3834 if (lnum != MAXLNUM)
3835 curwin->w_cursor.lnum = lnum;
3836 /*
3837 * Start a forward search at the end of the line.
3838 * Start a backward search at the start of the line.
3839 * This makes sure we never match in the current
3840 * line, and can match anywhere in the
3841 * next/previous line.
3842 */
3843 if (c == '/')
3844 curwin->w_cursor.col = MAXCOL;
3845 else
3846 curwin->w_cursor.col = 0;
3847 searchcmdlen = 0;
3848 if (!do_search(NULL, c, cmd, 1L,
3849 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3850 {
3851 curwin->w_cursor = pos;
3852 cmd = NULL;
3853 goto error;
3854 }
3855 lnum = curwin->w_cursor.lnum;
3856 curwin->w_cursor = pos;
3857 /* adjust command string pointer */
3858 cmd += searchcmdlen;
3859 }
3860 break;
3861
3862 case '\\': /* "\?", "\/" or "\&", repeat search */
3863 ++cmd;
3864 if (*cmd == '&')
3865 i = RE_SUBST;
3866 else if (*cmd == '?' || *cmd == '/')
3867 i = RE_SEARCH;
3868 else
3869 {
3870 EMSG(_(e_backslash));
3871 cmd = NULL;
3872 goto error;
3873 }
3874
3875 if (!skip)
3876 {
3877 /*
3878 * When search follows another address, start from
3879 * there.
3880 */
3881 if (lnum != MAXLNUM)
3882 pos.lnum = lnum;
3883 else
3884 pos.lnum = curwin->w_cursor.lnum;
3885
3886 /*
3887 * Start the search just like for the above
3888 * do_search().
3889 */
3890 if (*cmd != '?')
3891 pos.col = MAXCOL;
3892 else
3893 pos.col = 0;
3894 if (searchit(curwin, curbuf, &pos,
3895 *cmd == '?' ? BACKWARD : FORWARD,
3896 (char_u *)"", 1L,
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003897 SEARCH_MSG + SEARCH_START,
3898 i, (linenr_T)0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 lnum = pos.lnum;
3900 else
3901 {
3902 cmd = NULL;
3903 goto error;
3904 }
3905 }
3906 ++cmd;
3907 break;
3908
3909 default:
3910 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3911 lnum = getdigits(&cmd);
3912 }
3913
3914 for (;;)
3915 {
3916 cmd = skipwhite(cmd);
3917 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3918 break;
3919
3920 if (lnum == MAXLNUM)
3921 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3922 if (VIM_ISDIGIT(*cmd))
3923 i = '+'; /* "number" is same as "+number" */
3924 else
3925 i = *cmd++;
3926 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3927 n = 1;
3928 else
3929 n = getdigits(&cmd);
3930 if (i == '-')
3931 lnum -= n;
3932 else
3933 lnum += n;
3934 }
3935 } while (*cmd == '/' || *cmd == '?');
3936
3937error:
3938 *ptr = cmd;
3939 return lnum;
3940}
3941
3942/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003943 * Get flags from an Ex command argument.
3944 */
3945 static void
3946get_flags(eap)
3947 exarg_T *eap;
3948{
3949 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3950 {
3951 if (*eap->arg == 'l')
3952 eap->flags |= EXFLAG_LIST;
3953 else if (*eap->arg == 'p')
3954 eap->flags |= EXFLAG_PRINT;
3955 else
3956 eap->flags |= EXFLAG_NR;
3957 eap->arg = skipwhite(eap->arg + 1);
3958 }
3959}
3960
3961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 * Function called for command which is Not Implemented. NI!
3963 */
3964 void
3965ex_ni(eap)
3966 exarg_T *eap;
3967{
3968 if (!eap->skip)
3969 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3970}
3971
3972#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003973 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974/*
3975 * Function called for script command which is Not Implemented. NI!
3976 * Skips over ":perl <<EOF" constructs.
3977 */
3978 static void
3979ex_script_ni(eap)
3980 exarg_T *eap;
3981{
3982 if (!eap->skip)
3983 ex_ni(eap);
3984 else
3985 vim_free(script_get(eap, eap->arg));
3986}
3987#endif
3988
3989/*
3990 * Check range in Ex command for validity.
3991 * Return NULL when valid, error message when invalid.
3992 */
3993 static char_u *
3994invalid_range(eap)
3995 exarg_T *eap;
3996{
3997 if ( eap->line1 < 0
3998 || eap->line2 < 0
3999 || eap->line1 > eap->line2
4000 || ((eap->argt & RANGE)
4001 && !(eap->argt & NOTADR)
4002 && eap->line2 > curbuf->b_ml.ml_line_count
4003#ifdef FEAT_DIFF
4004 + (eap->cmdidx == CMD_diffget)
4005#endif
4006 ))
4007 return (char_u *)_(e_invrange);
4008 return NULL;
4009}
4010
4011/*
4012 * Correct the range for zero line number, if required.
4013 */
4014 static void
4015correct_range(eap)
4016 exarg_T *eap;
4017{
4018 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4019 {
4020 if (eap->line1 == 0)
4021 eap->line1 = 1;
4022 if (eap->line2 == 0)
4023 eap->line2 = 1;
4024 }
4025}
4026
Bram Moolenaar748bf032005-02-02 23:04:36 +00004027#ifdef FEAT_QUICKFIX
4028static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4029
4030/*
4031 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4032 * pattern. Otherwise return eap->arg.
4033 */
4034 static char_u *
4035skip_grep_pat(eap)
4036 exarg_T *eap;
4037{
4038 char_u *p = eap->arg;
4039
Bram Moolenaara37420f2006-02-04 22:37:47 +00004040 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4041 || eap->cmdidx == CMD_vimgrepadd
4042 || eap->cmdidx == CMD_lvimgrepadd
4043 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004044 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004045 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004046 if (p == NULL)
4047 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004048 }
4049 return p;
4050}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004051
4052/*
4053 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4054 * in the command line, so that things like % get expanded.
4055 */
4056 static char_u *
4057replace_makeprg(eap, p, cmdlinep)
4058 exarg_T *eap;
4059 char_u *p;
4060 char_u **cmdlinep;
4061{
4062 char_u *new_cmdline;
4063 char_u *program;
4064 char_u *pos;
4065 char_u *ptr;
4066 int len;
4067 int i;
4068
4069 /*
4070 * Don't do it when ":vimgrep" is used for ":grep".
4071 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004072 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4073 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4074 || eap->cmdidx == CMD_grepadd
4075 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004076 && !grep_internal(eap->cmdidx))
4077 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004078 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4079 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004080 {
4081 if (*curbuf->b_p_gp == NUL)
4082 program = p_gp;
4083 else
4084 program = curbuf->b_p_gp;
4085 }
4086 else
4087 {
4088 if (*curbuf->b_p_mp == NUL)
4089 program = p_mp;
4090 else
4091 program = curbuf->b_p_mp;
4092 }
4093
4094 p = skipwhite(p);
4095
4096 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4097 {
4098 /* replace $* by given arguments */
4099 i = 1;
4100 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4101 ++i;
4102 len = (int)STRLEN(p);
4103 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4104 if (new_cmdline == NULL)
4105 return NULL; /* out of memory */
4106 ptr = new_cmdline;
4107 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4108 {
4109 i = (int)(pos - program);
4110 STRNCPY(ptr, program, i);
4111 STRCPY(ptr += i, p);
4112 ptr += len;
4113 program = pos + 2;
4114 }
4115 STRCPY(ptr, program);
4116 }
4117 else
4118 {
4119 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4120 if (new_cmdline == NULL)
4121 return NULL; /* out of memory */
4122 STRCPY(new_cmdline, program);
4123 STRCAT(new_cmdline, " ");
4124 STRCAT(new_cmdline, p);
4125 }
4126 msg_make(p);
4127
4128 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4129 vim_free(*cmdlinep);
4130 *cmdlinep = new_cmdline;
4131 p = new_cmdline;
4132 }
4133 return p;
4134}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004135#endif
4136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137/*
4138 * Expand file name in Ex command argument.
4139 * Return FAIL for failure, OK otherwise.
4140 */
4141 int
4142expand_filename(eap, cmdlinep, errormsgp)
4143 exarg_T *eap;
4144 char_u **cmdlinep;
4145 char_u **errormsgp;
4146{
4147 int has_wildcards; /* need to expand wildcards */
4148 char_u *repl;
4149 int srclen;
4150 char_u *p;
4151 int n;
4152
Bram Moolenaar748bf032005-02-02 23:04:36 +00004153#ifdef FEAT_QUICKFIX
4154 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4155 p = skip_grep_pat(eap);
4156#else
4157 p = eap->arg;
4158#endif
4159
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 /*
4161 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4162 * the file name contains a wildcard it should not cause expanding.
4163 * (it will be expanded anyway if there is a wildcard before replacing).
4164 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004165 has_wildcards = mch_has_wildcard(p);
4166 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004168#ifdef FEAT_EVAL
4169 /* Skip over `=expr`, wildcards in it are not expanded. */
4170 if (p[0] == '`' && p[1] == '=')
4171 {
4172 p += 2;
4173 (void)skip_expr(&p);
4174 if (*p == '`')
4175 ++p;
4176 continue;
4177 }
4178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 /*
4180 * Quick check if this cannot be the start of a special string.
4181 * Also removes backslash before '%', '#' and '<'.
4182 */
4183 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4184 {
4185 ++p;
4186 continue;
4187 }
4188
4189 /*
4190 * Try to find a match at this position.
4191 */
4192 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4193 if (*errormsgp != NULL) /* error detected */
4194 return FAIL;
4195 if (repl == NULL) /* no match found */
4196 {
4197 p += srclen;
4198 continue;
4199 }
4200
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004201 /* Wildcards won't be expanded below, the replacement is taken
4202 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4203 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4204 {
4205 char_u *l = repl;
4206
4207 repl = expand_env_save(repl);
4208 vim_free(l);
4209 }
4210
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 /* Need to escape white space et al. with a backslash. Don't do this
4212 * for shell commands (may have to use quotes instead). Don't do this
4213 * for non-unix systems when there is a single argument (spaces don't
4214 * separate arguments then). */
4215 if (!eap->usefilter
4216 && eap->cmdidx != CMD_bang
4217 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004218 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004220 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004222 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#ifndef UNIX
4224 && !(eap->argt & NOSPC)
4225#endif
4226 )
4227 {
4228 char_u *l;
4229#ifdef BACKSLASH_IN_FILENAME
4230 /* Don't escape a backslash here, because rem_backslash() doesn't
4231 * remove it later. */
4232 static char_u *nobslash = (char_u *)" \t\"|";
4233# define ESCAPE_CHARS nobslash
4234#else
4235# define ESCAPE_CHARS escape_chars
4236#endif
4237
4238 for (l = repl; *l; ++l)
4239 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4240 {
4241 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4242 if (l != NULL)
4243 {
4244 vim_free(repl);
4245 repl = l;
4246 }
4247 break;
4248 }
4249 }
4250
4251 /* For a shell command a '!' must be escaped. */
4252 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004253 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 char_u *l;
4256
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004257 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 if (l != NULL)
4259 {
4260 vim_free(repl);
4261 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004262 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (strstr((char *)p_sh, "sh") != NULL)
4264 {
4265 l = vim_strsave_escaped(repl, (char_u *)"!");
4266 if (l != NULL)
4267 {
4268 vim_free(repl);
4269 repl = l;
4270 }
4271 }
4272 }
4273 }
4274
4275 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4276 vim_free(repl);
4277 if (p == NULL)
4278 return FAIL;
4279 }
4280
4281 /*
4282 * One file argument: Expand wildcards.
4283 * Don't do this with ":r !command" or ":w !command".
4284 */
4285 if ((eap->argt & NOSPC) && !eap->usefilter)
4286 {
4287 /*
4288 * May do this twice:
4289 * 1. Replace environment variables.
4290 * 2. Replace any other wildcards, remove backslashes.
4291 */
4292 for (n = 1; n <= 2; ++n)
4293 {
4294 if (n == 2)
4295 {
4296#ifdef UNIX
4297 /*
4298 * Only for Unix we check for more than one file name.
4299 * For other systems spaces are considered to be part
4300 * of the file name.
4301 * Only check here if there is no wildcard, otherwise
4302 * ExpandOne() will check for errors. This allows
4303 * ":e `ls ve*.c`" on Unix.
4304 */
4305 if (!has_wildcards)
4306 for (p = eap->arg; *p; ++p)
4307 {
4308 /* skip escaped characters */
4309 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4310 ++p;
4311 else if (vim_iswhite(*p))
4312 {
4313 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4314 return FAIL;
4315 }
4316 }
4317#endif
4318
4319 /*
4320 * Halve the number of backslashes (this is Vi compatible).
4321 * For Unix and OS/2, when wildcards are expanded, this is
4322 * done by ExpandOne() below.
4323 */
4324#if defined(UNIX) || defined(OS2)
4325 if (!has_wildcards)
4326#endif
4327 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
4329
4330 if (has_wildcards)
4331 {
4332 if (n == 1)
4333 {
4334 /*
4335 * First loop: May expand environment variables. This
4336 * can be done much faster with expand_env() than with
4337 * something else (e.g., calling a shell).
4338 * After expanding environment variables, check again
4339 * if there are still wildcards present.
4340 */
4341 if (vim_strchr(eap->arg, '$') != NULL
4342 || vim_strchr(eap->arg, '~') != NULL)
4343 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004344 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4345 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 has_wildcards = mch_has_wildcard(NameBuff);
4347 p = NameBuff;
4348 }
4349 else
4350 p = NULL;
4351 }
4352 else /* n == 2 */
4353 {
4354 expand_T xpc;
4355
4356 ExpandInit(&xpc);
4357 xpc.xp_context = EXPAND_FILES;
4358 p = ExpandOne(&xpc, eap->arg, NULL,
4359 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4360 WILD_EXPAND_FREE);
4361 ExpandCleanup(&xpc);
4362 if (p == NULL)
4363 return FAIL;
4364 }
4365 if (p != NULL)
4366 {
4367 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4368 p, cmdlinep);
4369 if (n == 2) /* p came from ExpandOne() */
4370 vim_free(p);
4371 }
4372 }
4373 }
4374 }
4375 return OK;
4376}
4377
4378/*
4379 * Replace part of the command line, keeping eap->cmd, eap->arg and
4380 * eap->nextcmd correct.
4381 * "src" points to the part that is to be replaced, of length "srclen".
4382 * "repl" is the replacement string.
4383 * Returns a pointer to the character after the replaced string.
4384 * Returns NULL for failure.
4385 */
4386 static char_u *
4387repl_cmdline(eap, src, srclen, repl, cmdlinep)
4388 exarg_T *eap;
4389 char_u *src;
4390 int srclen;
4391 char_u *repl;
4392 char_u **cmdlinep;
4393{
4394 int len;
4395 int i;
4396 char_u *new_cmdline;
4397
4398 /*
4399 * The new command line is build in new_cmdline[].
4400 * First allocate it.
4401 * Careful: a "+cmd" argument may have been NUL terminated.
4402 */
4403 len = (int)STRLEN(repl);
4404 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004405 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4407 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4408 return NULL; /* out of memory! */
4409
4410 /*
4411 * Copy the stuff before the expanded part.
4412 * Copy the expanded stuff.
4413 * Copy what came after the expanded part.
4414 * Copy the next commands, if there are any.
4415 */
4416 i = (int)(src - *cmdlinep); /* length of part before match */
4417 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004418
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 mch_memmove(new_cmdline + i, repl, (size_t)len);
4420 i += len; /* remember the end of the string */
4421 STRCPY(new_cmdline + i, src + srclen);
4422 src = new_cmdline + i; /* remember where to continue */
4423
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004424 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 i = (int)STRLEN(new_cmdline) + 1;
4427 STRCPY(new_cmdline + i, eap->nextcmd);
4428 eap->nextcmd = new_cmdline + i;
4429 }
4430 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4431 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4432 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4433 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4434 vim_free(*cmdlinep);
4435 *cmdlinep = new_cmdline;
4436
4437 return src;
4438}
4439
4440/*
4441 * Check for '|' to separate commands and '"' to start comments.
4442 */
4443 void
4444separate_nextcmd(eap)
4445 exarg_T *eap;
4446{
4447 char_u *p;
4448
Bram Moolenaar86b68352004-12-27 21:59:20 +00004449#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004450 p = skip_grep_pat(eap);
4451#else
4452 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004453#endif
4454
4455 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
4457 if (*p == Ctrl_V)
4458 {
4459 if (eap->argt & (USECTRLV | XFILE))
4460 ++p; /* skip CTRL-V and next char */
4461 else
4462 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4463 if (*p == NUL) /* stop at NUL after CTRL-V */
4464 break;
4465 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004466
4467#ifdef FEAT_EVAL
4468 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004469 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004470 {
4471 p += 2;
4472 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004473 }
4474#endif
4475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 /* Check for '"': start of comment or '|': next command */
4477 /* :@" and :*" do not start a comment!
4478 * :redir @" doesn't either. */
4479 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4480 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4481 || p != eap->arg)
4482 && (eap->cmdidx != CMD_redir
4483 || p != eap->arg + 1 || p[-1] != '@'))
4484 || *p == '|' || *p == '\n')
4485 {
4486 /*
4487 * We remove the '\' before the '|', unless USECTRLV is used
4488 * AND 'b' is present in 'cpoptions'.
4489 */
4490 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4491 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4492 {
4493 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4494 --p;
4495 }
4496 else
4497 {
4498 eap->nextcmd = check_nextcmd(p);
4499 *p = NUL;
4500 break;
4501 }
4502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4506 del_trailing_spaces(eap->arg);
4507}
4508
4509/*
4510 * get + command from ex argument
4511 */
4512 static char_u *
4513getargcmd(argp)
4514 char_u **argp;
4515{
4516 char_u *arg = *argp;
4517 char_u *command = NULL;
4518
4519 if (*arg == '+') /* +[command] */
4520 {
4521 ++arg;
4522 if (vim_isspace(*arg))
4523 command = dollar_command;
4524 else
4525 {
4526 command = arg;
4527 arg = skip_cmd_arg(command, TRUE);
4528 if (*arg != NUL)
4529 *arg++ = NUL; /* terminate command with NUL */
4530 }
4531
4532 arg = skipwhite(arg); /* skip over spaces */
4533 *argp = arg;
4534 }
4535 return command;
4536}
4537
4538/*
4539 * Find end of "+command" argument. Skip over "\ " and "\\".
4540 */
4541 static char_u *
4542skip_cmd_arg(p, rembs)
4543 char_u *p;
4544 int rembs; /* TRUE to halve the number of backslashes */
4545{
4546 while (*p && !vim_isspace(*p))
4547 {
4548 if (*p == '\\' && p[1] != NUL)
4549 {
4550 if (rembs)
4551 mch_memmove(p, p + 1, STRLEN(p));
4552 else
4553 ++p;
4554 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004555 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557 return p;
4558}
4559
4560/*
4561 * Get "++opt=arg" argument.
4562 * Return FAIL or OK.
4563 */
4564 static int
4565getargopt(eap)
4566 exarg_T *eap;
4567{
4568 char_u *arg = eap->arg + 2;
4569 int *pp = NULL;
4570#ifdef FEAT_MBYTE
4571 char_u *p;
4572#endif
4573
4574 /* ":edit ++[no]bin[ary] file" */
4575 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4576 {
4577 if (*arg == 'n')
4578 {
4579 arg += 2;
4580 eap->force_bin = FORCE_NOBIN;
4581 }
4582 else
4583 eap->force_bin = FORCE_BIN;
4584 if (!checkforcmd(&arg, "binary", 3))
4585 return FAIL;
4586 eap->arg = skipwhite(arg);
4587 return OK;
4588 }
4589
4590 if (STRNCMP(arg, "ff", 2) == 0)
4591 {
4592 arg += 2;
4593 pp = &eap->force_ff;
4594 }
4595 else if (STRNCMP(arg, "fileformat", 10) == 0)
4596 {
4597 arg += 10;
4598 pp = &eap->force_ff;
4599 }
4600#ifdef FEAT_MBYTE
4601 else if (STRNCMP(arg, "enc", 3) == 0)
4602 {
4603 arg += 3;
4604 pp = &eap->force_enc;
4605 }
4606 else if (STRNCMP(arg, "encoding", 8) == 0)
4607 {
4608 arg += 8;
4609 pp = &eap->force_enc;
4610 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004611 else if (STRNCMP(arg, "bad", 3) == 0)
4612 {
4613 arg += 3;
4614 pp = &eap->bad_char;
4615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616#endif
4617
4618 if (pp == NULL || *arg != '=')
4619 return FAIL;
4620
4621 ++arg;
4622 *pp = (int)(arg - eap->cmd);
4623 arg = skip_cmd_arg(arg, FALSE);
4624 eap->arg = skipwhite(arg);
4625 *arg = NUL;
4626
4627#ifdef FEAT_MBYTE
4628 if (pp == &eap->force_ff)
4629 {
4630#endif
4631 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4632 return FAIL;
4633#ifdef FEAT_MBYTE
4634 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004635 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
4637 /* Make 'fileencoding' lower case. */
4638 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4639 *p = TOLOWER_ASC(*p);
4640 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004641 else
4642 {
4643 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4644 * "drop". */
4645 p = eap->cmd + eap->bad_char;
4646 if (STRICMP(p, "keep") == 0)
4647 eap->bad_char = BAD_KEEP;
4648 else if (STRICMP(p, "drop") == 0)
4649 eap->bad_char = BAD_DROP;
4650 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4651 eap->bad_char = *p;
4652 else
4653 return FAIL;
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655#endif
4656
4657 return OK;
4658}
4659
4660/*
4661 * ":abbreviate" and friends.
4662 */
4663 static void
4664ex_abbreviate(eap)
4665 exarg_T *eap;
4666{
4667 do_exmap(eap, TRUE); /* almost the same as mapping */
4668}
4669
4670/*
4671 * ":map" and friends.
4672 */
4673 static void
4674ex_map(eap)
4675 exarg_T *eap;
4676{
4677 /*
4678 * If we are sourcing .exrc or .vimrc in current directory we
4679 * print the mappings for security reasons.
4680 */
4681 if (secure)
4682 {
4683 secure = 2;
4684 msg_outtrans(eap->cmd);
4685 msg_putchar('\n');
4686 }
4687 do_exmap(eap, FALSE);
4688}
4689
4690/*
4691 * ":unmap" and friends.
4692 */
4693 static void
4694ex_unmap(eap)
4695 exarg_T *eap;
4696{
4697 do_exmap(eap, FALSE);
4698}
4699
4700/*
4701 * ":mapclear" and friends.
4702 */
4703 static void
4704ex_mapclear(eap)
4705 exarg_T *eap;
4706{
4707 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4708}
4709
4710/*
4711 * ":abclear" and friends.
4712 */
4713 static void
4714ex_abclear(eap)
4715 exarg_T *eap;
4716{
4717 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4718}
4719
4720#ifdef FEAT_AUTOCMD
4721 static void
4722ex_autocmd(eap)
4723 exarg_T *eap;
4724{
4725 /*
4726 * Disallow auto commands from .exrc and .vimrc in current
4727 * directory for security reasons.
4728 */
4729 if (secure)
4730 {
4731 secure = 2;
4732 eap->errmsg = e_curdir;
4733 }
4734 else if (eap->cmdidx == CMD_autocmd)
4735 do_autocmd(eap->arg, eap->forceit);
4736 else
4737 do_augroup(eap->arg, eap->forceit);
4738}
4739
4740/*
4741 * ":doautocmd": Apply the automatic commands to the current buffer.
4742 */
4743 static void
4744ex_doautocmd(eap)
4745 exarg_T *eap;
4746{
4747 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004748 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749}
4750#endif
4751
4752#ifdef FEAT_LISTCMDS
4753/*
4754 * :[N]bunload[!] [N] [bufname] unload buffer
4755 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4756 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4757 */
4758 static void
4759ex_bunload(eap)
4760 exarg_T *eap;
4761{
4762 eap->errmsg = do_bufdel(
4763 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4764 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4765 : DOBUF_UNLOAD, eap->arg,
4766 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4767}
4768
4769/*
4770 * :[N]buffer [N] to buffer N
4771 * :[N]sbuffer [N] to buffer N
4772 */
4773 static void
4774ex_buffer(eap)
4775 exarg_T *eap;
4776{
4777 if (*eap->arg)
4778 eap->errmsg = e_trailing;
4779 else
4780 {
4781 if (eap->addr_count == 0) /* default is current buffer */
4782 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4783 else
4784 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4785 }
4786}
4787
4788/*
4789 * :[N]bmodified [N] to next mod. buffer
4790 * :[N]sbmodified [N] to next mod. buffer
4791 */
4792 static void
4793ex_bmodified(eap)
4794 exarg_T *eap;
4795{
4796 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4797}
4798
4799/*
4800 * :[N]bnext [N] to next buffer
4801 * :[N]sbnext [N] split and to next buffer
4802 */
4803 static void
4804ex_bnext(eap)
4805 exarg_T *eap;
4806{
4807 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4808}
4809
4810/*
4811 * :[N]bNext [N] to previous buffer
4812 * :[N]bprevious [N] to previous buffer
4813 * :[N]sbNext [N] split and to previous buffer
4814 * :[N]sbprevious [N] split and to previous buffer
4815 */
4816 static void
4817ex_bprevious(eap)
4818 exarg_T *eap;
4819{
4820 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4821}
4822
4823/*
4824 * :brewind to first buffer
4825 * :bfirst to first buffer
4826 * :sbrewind split and to first buffer
4827 * :sbfirst split and to first buffer
4828 */
4829 static void
4830ex_brewind(eap)
4831 exarg_T *eap;
4832{
4833 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4834}
4835
4836/*
4837 * :blast to last buffer
4838 * :sblast split and to last buffer
4839 */
4840 static void
4841ex_blast(eap)
4842 exarg_T *eap;
4843{
4844 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4845}
4846#endif
4847
4848 int
4849ends_excmd(c)
4850 int c;
4851{
4852 return (c == NUL || c == '|' || c == '"' || c == '\n');
4853}
4854
4855#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4856 || defined(PROTO)
4857/*
4858 * Return the next command, after the first '|' or '\n'.
4859 * Return NULL if not found.
4860 */
4861 char_u *
4862find_nextcmd(p)
4863 char_u *p;
4864{
4865 while (*p != '|' && *p != '\n')
4866 {
4867 if (*p == NUL)
4868 return NULL;
4869 ++p;
4870 }
4871 return (p + 1);
4872}
4873#endif
4874
4875/*
4876 * Check if *p is a separator between Ex commands.
4877 * Return NULL if it isn't, (p + 1) if it is.
4878 */
4879 char_u *
4880check_nextcmd(p)
4881 char_u *p;
4882{
4883 p = skipwhite(p);
4884 if (*p == '|' || *p == '\n')
4885 return (p + 1);
4886 else
4887 return NULL;
4888}
4889
4890/*
4891 * - if there are more files to edit
4892 * - and this is the last window
4893 * - and forceit not used
4894 * - and not repeated twice on a row
4895 * return FAIL and give error message if 'message' TRUE
4896 * return OK otherwise
4897 */
4898 static int
4899check_more(message, forceit)
4900 int message; /* when FALSE check only, no messages */
4901 int forceit;
4902{
4903 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4904
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004905 if (!forceit && only_one_window()
4906 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
4908 if (message)
4909 {
4910#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4911 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4912 {
4913 char_u buff[IOSIZE];
4914
4915 if (n == 1)
4916 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4917 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004918 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 _("%d more files to edit. Quit anyway?"), n);
4920 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4921 return OK;
4922 return FAIL;
4923 }
4924#endif
4925 if (n == 1)
4926 EMSG(_("E173: 1 more file to edit"));
4927 else
4928 EMSGN(_("E173: %ld more files to edit"), n);
4929 quitmore = 2; /* next try to quit is allowed */
4930 }
4931 return FAIL;
4932 }
4933 return OK;
4934}
4935
4936#ifdef FEAT_CMDL_COMPL
4937/*
4938 * Function given to ExpandGeneric() to obtain the list of command names.
4939 */
4940/*ARGSUSED*/
4941 char_u *
4942get_command_name(xp, idx)
4943 expand_T *xp;
4944 int idx;
4945{
4946 if (idx >= (int)CMD_SIZE)
4947# ifdef FEAT_USR_CMDS
4948 return get_user_command_name(idx);
4949# else
4950 return NULL;
4951# endif
4952 return cmdnames[idx].cmd_name;
4953}
4954#endif
4955
4956#if defined(FEAT_USR_CMDS) || defined(PROTO)
4957static 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));
4958static void uc_list __ARGS((char_u *name, size_t name_len));
4959static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4960static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4961static 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));
4962
4963 static int
4964uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4965 char_u *name;
4966 size_t name_len;
4967 char_u *rep;
4968 long argt;
4969 long def;
4970 int flags;
4971 int compl;
4972 char_u *compl_arg;
4973 int force;
4974{
4975 ucmd_T *cmd = NULL;
4976 char_u *p;
4977 int i;
4978 int cmp = 1;
4979 char_u *rep_buf = NULL;
4980 garray_T *gap;
4981
4982 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4983 if (rep_buf == NULL)
4984 {
4985 /* Can't replace termcodes - try using the string as is */
4986 rep_buf = vim_strsave(rep);
4987
4988 /* Give up if out of memory */
4989 if (rep_buf == NULL)
4990 return FAIL;
4991 }
4992
4993 /* get address of growarray: global or in curbuf */
4994 if (flags & UC_BUFFER)
4995 {
4996 gap = &curbuf->b_ucmds;
4997 if (gap->ga_itemsize == 0)
4998 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4999 }
5000 else
5001 gap = &ucmds;
5002
5003 /* Search for the command in the already defined commands. */
5004 for (i = 0; i < gap->ga_len; ++i)
5005 {
5006 size_t len;
5007
5008 cmd = USER_CMD_GA(gap, i);
5009 len = STRLEN(cmd->uc_name);
5010 cmp = STRNCMP(name, cmd->uc_name, name_len);
5011 if (cmp == 0)
5012 {
5013 if (name_len < len)
5014 cmp = -1;
5015 else if (name_len > len)
5016 cmp = 1;
5017 }
5018
5019 if (cmp == 0)
5020 {
5021 if (!force)
5022 {
5023 EMSG(_("E174: Command already exists: add ! to replace it"));
5024 goto fail;
5025 }
5026
5027 vim_free(cmd->uc_rep);
5028 cmd->uc_rep = 0;
5029 break;
5030 }
5031
5032 /* Stop as soon as we pass the name to add */
5033 if (cmp < 0)
5034 break;
5035 }
5036
5037 /* Extend the array unless we're replacing an existing command */
5038 if (cmp != 0)
5039 {
5040 if (ga_grow(gap, 1) != OK)
5041 goto fail;
5042 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5043 goto fail;
5044
5045 cmd = USER_CMD_GA(gap, i);
5046 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5047
5048 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
5050 cmd->uc_name = p;
5051 }
5052
5053 cmd->uc_rep = rep_buf;
5054 cmd->uc_argt = argt;
5055 cmd->uc_def = def;
5056 cmd->uc_compl = compl;
5057#ifdef FEAT_EVAL
5058 cmd->uc_scriptID = current_SID;
5059# ifdef FEAT_CMDL_COMPL
5060 cmd->uc_compl_arg = compl_arg;
5061# endif
5062#endif
5063
5064 return OK;
5065
5066fail:
5067 vim_free(rep_buf);
5068#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5069 vim_free(compl_arg);
5070#endif
5071 return FAIL;
5072}
5073
5074/*
5075 * List of names for completion for ":command" with the EXPAND_ flag.
5076 * Must be alphabetical for completion.
5077 */
5078static struct
5079{
5080 int expand;
5081 char *name;
5082} command_complete[] =
5083{
5084 {EXPAND_AUGROUP, "augroup"},
5085 {EXPAND_BUFFERS, "buffer"},
5086 {EXPAND_COMMANDS, "command"},
5087#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5088 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005089 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090#endif
5091 {EXPAND_DIRECTORIES, "dir"},
5092 {EXPAND_ENV_VARS, "environment"},
5093 {EXPAND_EVENTS, "event"},
5094 {EXPAND_EXPRESSION, "expression"},
5095 {EXPAND_FILES, "file"},
5096 {EXPAND_FUNCTIONS, "function"},
5097 {EXPAND_HELP, "help"},
5098 {EXPAND_HIGHLIGHT, "highlight"},
5099 {EXPAND_MAPPINGS, "mapping"},
5100 {EXPAND_MENUS, "menu"},
5101 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005102 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {EXPAND_TAGS, "tag"},
5104 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5105 {EXPAND_USER_VARS, "var"},
5106 {0, NULL}
5107};
5108
5109 static void
5110uc_list(name, name_len)
5111 char_u *name;
5112 size_t name_len;
5113{
5114 int i, j;
5115 int found = FALSE;
5116 ucmd_T *cmd;
5117 int len;
5118 long a;
5119 garray_T *gap;
5120
5121 gap = &curbuf->b_ucmds;
5122 for (;;)
5123 {
5124 for (i = 0; i < gap->ga_len; ++i)
5125 {
5126 cmd = USER_CMD_GA(gap, i);
5127 a = cmd->uc_argt;
5128
5129 /* Skip commands which don't match the requested prefix */
5130 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5131 continue;
5132
5133 /* Put out the title first time */
5134 if (!found)
5135 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5136 found = TRUE;
5137 msg_putchar('\n');
5138 if (got_int)
5139 break;
5140
5141 /* Special cases */
5142 msg_putchar(a & BANG ? '!' : ' ');
5143 msg_putchar(a & REGSTR ? '"' : ' ');
5144 msg_putchar(gap != &ucmds ? 'b' : ' ');
5145 msg_putchar(' ');
5146
5147 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5148 len = (int)STRLEN(cmd->uc_name) + 4;
5149
5150 do {
5151 msg_putchar(' ');
5152 ++len;
5153 } while (len < 16);
5154
5155 len = 0;
5156
5157 /* Arguments */
5158 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5159 {
5160 case 0: IObuff[len++] = '0'; break;
5161 case (EXTRA): IObuff[len++] = '*'; break;
5162 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5163 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5164 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5165 }
5166
5167 do {
5168 IObuff[len++] = ' ';
5169 } while (len < 5);
5170
5171 /* Range */
5172 if (a & (RANGE|COUNT))
5173 {
5174 if (a & COUNT)
5175 {
5176 /* -count=N */
5177 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5178 len += (int)STRLEN(IObuff + len);
5179 }
5180 else if (a & DFLALL)
5181 IObuff[len++] = '%';
5182 else if (cmd->uc_def >= 0)
5183 {
5184 /* -range=N */
5185 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5186 len += (int)STRLEN(IObuff + len);
5187 }
5188 else
5189 IObuff[len++] = '.';
5190 }
5191
5192 do {
5193 IObuff[len++] = ' ';
5194 } while (len < 11);
5195
5196 /* Completion */
5197 for (j = 0; command_complete[j].expand != 0; ++j)
5198 if (command_complete[j].expand == cmd->uc_compl)
5199 {
5200 STRCPY(IObuff + len, command_complete[j].name);
5201 len += (int)STRLEN(IObuff + len);
5202 break;
5203 }
5204
5205 do {
5206 IObuff[len++] = ' ';
5207 } while (len < 21);
5208
5209 IObuff[len] = '\0';
5210 msg_outtrans(IObuff);
5211
5212 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005213#ifdef FEAT_EVAL
5214 if (p_verbose > 0)
5215 last_set_msg(cmd->uc_scriptID);
5216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 out_flush();
5218 ui_breakcheck();
5219 if (got_int)
5220 break;
5221 }
5222 if (gap == &ucmds || i < gap->ga_len)
5223 break;
5224 gap = &ucmds;
5225 }
5226
5227 if (!found)
5228 MSG(_("No user-defined commands found"));
5229}
5230
5231 static char_u *
5232uc_fun_cmd()
5233{
5234 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5235 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5236 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5237 0xb9, 0x7f, 0};
5238 int i;
5239
5240 for (i = 0; fcmd[i]; ++i)
5241 IObuff[i] = fcmd[i] - 0x40;
5242 IObuff[i] = 0;
5243 return IObuff;
5244}
5245
5246 static int
5247uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5248 char_u *attr;
5249 size_t len;
5250 long *argt;
5251 long *def;
5252 int *flags;
5253 int *compl;
5254 char_u **compl_arg;
5255{
5256 char_u *p;
5257
5258 if (len == 0)
5259 {
5260 EMSG(_("E175: No attribute specified"));
5261 return FAIL;
5262 }
5263
5264 /* First, try the simple attributes (no arguments) */
5265 if (STRNICMP(attr, "bang", len) == 0)
5266 *argt |= BANG;
5267 else if (STRNICMP(attr, "buffer", len) == 0)
5268 *flags |= UC_BUFFER;
5269 else if (STRNICMP(attr, "register", len) == 0)
5270 *argt |= REGSTR;
5271 else if (STRNICMP(attr, "bar", len) == 0)
5272 *argt |= TRLBAR;
5273 else
5274 {
5275 int i;
5276 char_u *val = NULL;
5277 size_t vallen = 0;
5278 size_t attrlen = len;
5279
5280 /* Look for the attribute name - which is the part before any '=' */
5281 for (i = 0; i < (int)len; ++i)
5282 {
5283 if (attr[i] == '=')
5284 {
5285 val = &attr[i + 1];
5286 vallen = len - i - 1;
5287 attrlen = i;
5288 break;
5289 }
5290 }
5291
5292 if (STRNICMP(attr, "nargs", attrlen) == 0)
5293 {
5294 if (vallen == 1)
5295 {
5296 if (*val == '0')
5297 /* Do nothing - this is the default */;
5298 else if (*val == '1')
5299 *argt |= (EXTRA | NOSPC | NEEDARG);
5300 else if (*val == '*')
5301 *argt |= EXTRA;
5302 else if (*val == '?')
5303 *argt |= (EXTRA | NOSPC);
5304 else if (*val == '+')
5305 *argt |= (EXTRA | NEEDARG);
5306 else
5307 goto wrong_nargs;
5308 }
5309 else
5310 {
5311wrong_nargs:
5312 EMSG(_("E176: Invalid number of arguments"));
5313 return FAIL;
5314 }
5315 }
5316 else if (STRNICMP(attr, "range", attrlen) == 0)
5317 {
5318 *argt |= RANGE;
5319 if (vallen == 1 && *val == '%')
5320 *argt |= DFLALL;
5321 else if (val != NULL)
5322 {
5323 p = val;
5324 if (*def >= 0)
5325 {
5326two_count:
5327 EMSG(_("E177: Count cannot be specified twice"));
5328 return FAIL;
5329 }
5330
5331 *def = getdigits(&p);
5332 *argt |= (ZEROR | NOTADR);
5333
5334 if (p != val + vallen || vallen == 0)
5335 {
5336invalid_count:
5337 EMSG(_("E178: Invalid default value for count"));
5338 return FAIL;
5339 }
5340 }
5341 }
5342 else if (STRNICMP(attr, "count", attrlen) == 0)
5343 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005344 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
5346 if (val != NULL)
5347 {
5348 p = val;
5349 if (*def >= 0)
5350 goto two_count;
5351
5352 *def = getdigits(&p);
5353
5354 if (p != val + vallen)
5355 goto invalid_count;
5356 }
5357
5358 if (*def < 0)
5359 *def = 0;
5360 }
5361 else if (STRNICMP(attr, "complete", attrlen) == 0)
5362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 if (val == NULL)
5364 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005365 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 return FAIL;
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005369 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5370 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373 else
5374 {
5375 char_u ch = attr[len];
5376 attr[len] = '\0';
5377 EMSG2(_("E181: Invalid attribute: %s"), attr);
5378 attr[len] = ch;
5379 return FAIL;
5380 }
5381 }
5382
5383 return OK;
5384}
5385
5386 static void
5387ex_command(eap)
5388 exarg_T *eap;
5389{
5390 char_u *name;
5391 char_u *end;
5392 char_u *p;
5393 long argt = 0;
5394 long def = -1;
5395 int flags = 0;
5396 int compl = EXPAND_NOTHING;
5397 char_u *compl_arg = NULL;
5398 int has_attr = (eap->arg[0] == '-');
5399
5400 p = eap->arg;
5401
5402 /* Check for attributes */
5403 while (*p == '-')
5404 {
5405 ++p;
5406 end = skiptowhite(p);
5407 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5408 == FAIL)
5409 return;
5410 p = skipwhite(end);
5411 }
5412
5413 /* Get the name (if any) and skip to the following argument */
5414 name = p;
5415 if (ASCII_ISALPHA(*p))
5416 while (ASCII_ISALNUM(*p))
5417 ++p;
5418 if (!ends_excmd(*p) && !vim_iswhite(*p))
5419 {
5420 EMSG(_("E182: Invalid command name"));
5421 return;
5422 }
5423 end = p;
5424
5425 /* If there is nothing after the name, and no attributes were specified,
5426 * we are listing commands
5427 */
5428 p = skipwhite(end);
5429 if (!has_attr && ends_excmd(*p))
5430 {
5431 uc_list(name, end - name);
5432 }
5433 else if (!ASCII_ISUPPER(*name))
5434 {
5435 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5436 return;
5437 }
5438 else
5439 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5440 eap->forceit);
5441}
5442
5443/*
5444 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 * Clear all user commands, global and for current buffer.
5446 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005447/*ARGSUSED*/
5448 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449ex_comclear(eap)
5450 exarg_T *eap;
5451{
5452 uc_clear(&ucmds);
5453 uc_clear(&curbuf->b_ucmds);
5454}
5455
5456/*
5457 * Clear all user commands for "gap".
5458 */
5459 void
5460uc_clear(gap)
5461 garray_T *gap;
5462{
5463 int i;
5464 ucmd_T *cmd;
5465
5466 for (i = 0; i < gap->ga_len; ++i)
5467 {
5468 cmd = USER_CMD_GA(gap, i);
5469 vim_free(cmd->uc_name);
5470 vim_free(cmd->uc_rep);
5471# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5472 vim_free(cmd->uc_compl_arg);
5473# endif
5474 }
5475 ga_clear(gap);
5476}
5477
5478 static void
5479ex_delcommand(eap)
5480 exarg_T *eap;
5481{
5482 int i = 0;
5483 ucmd_T *cmd = NULL;
5484 int cmp = -1;
5485 garray_T *gap;
5486
5487 gap = &curbuf->b_ucmds;
5488 for (;;)
5489 {
5490 for (i = 0; i < gap->ga_len; ++i)
5491 {
5492 cmd = USER_CMD_GA(gap, i);
5493 cmp = STRCMP(eap->arg, cmd->uc_name);
5494 if (cmp <= 0)
5495 break;
5496 }
5497 if (gap == &ucmds || cmp == 0)
5498 break;
5499 gap = &ucmds;
5500 }
5501
5502 if (cmp != 0)
5503 {
5504 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5505 return;
5506 }
5507
5508 vim_free(cmd->uc_name);
5509 vim_free(cmd->uc_rep);
5510# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5511 vim_free(cmd->uc_compl_arg);
5512# endif
5513
5514 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
5516 if (i < gap->ga_len)
5517 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5518}
5519
5520 static char_u *
5521uc_split_args(arg, lenp)
5522 char_u *arg;
5523 size_t *lenp;
5524{
5525 char_u *buf;
5526 char_u *p;
5527 char_u *q;
5528 int len;
5529
5530 /* Precalculate length */
5531 p = arg;
5532 len = 2; /* Initial and final quotes */
5533
5534 while (*p)
5535 {
5536 if (p[0] == '\\' && vim_iswhite(p[1]))
5537 {
5538 len += 1;
5539 p += 2;
5540 }
5541 else if (*p == '\\' || *p == '"')
5542 {
5543 len += 2;
5544 p += 1;
5545 }
5546 else if (vim_iswhite(*p))
5547 {
5548 p = skipwhite(p);
5549 if (*p == NUL)
5550 break;
5551 len += 3; /* "," */
5552 }
5553 else
5554 {
5555 ++len;
5556 ++p;
5557 }
5558 }
5559
5560 buf = alloc(len + 1);
5561 if (buf == NULL)
5562 {
5563 *lenp = 0;
5564 return buf;
5565 }
5566
5567 p = arg;
5568 q = buf;
5569 *q++ = '"';
5570 while (*p)
5571 {
5572 if (p[0] == '\\' && vim_iswhite(p[1]))
5573 {
5574 *q++ = p[1];
5575 p += 2;
5576 }
5577 else if (*p == '\\' || *p == '"')
5578 {
5579 *q++ = '\\';
5580 *q++ = *p++;
5581 }
5582 else if (vim_iswhite(*p))
5583 {
5584 p = skipwhite(p);
5585 if (*p == NUL)
5586 break;
5587 *q++ = '"';
5588 *q++ = ',';
5589 *q++ = '"';
5590 }
5591 else
5592 {
5593 *q++ = *p++;
5594 }
5595 }
5596 *q++ = '"';
5597 *q = 0;
5598
5599 *lenp = len;
5600 return buf;
5601}
5602
5603/*
5604 * Check for a <> code in a user command.
5605 * "code" points to the '<'. "len" the length of the <> (inclusive).
5606 * "buf" is where the result is to be added.
5607 * "split_buf" points to a buffer used for splitting, caller should free it.
5608 * "split_len" is the length of what "split_buf" contains.
5609 * Returns the length of the replacement, which has been added to "buf".
5610 * Returns -1 if there was no match, and only the "<" has been copied.
5611 */
5612 static size_t
5613uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5614 char_u *code;
5615 size_t len;
5616 char_u *buf;
5617 ucmd_T *cmd; /* the user command we're expanding */
5618 exarg_T *eap; /* ex arguments */
5619 char_u **split_buf;
5620 size_t *split_len;
5621{
5622 size_t result = 0;
5623 char_u *p = code + 1;
5624 size_t l = len - 2;
5625 int quote = 0;
5626 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5627 ct_LT, ct_NONE } type = ct_NONE;
5628
5629 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5630 {
5631 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5632 p += 2;
5633 l -= 2;
5634 }
5635
5636 if (l < 1)
5637 type = ct_NONE;
5638 else if (STRNICMP(p, "args", l) == 0)
5639 type = ct_ARGS;
5640 else if (STRNICMP(p, "bang", l) == 0)
5641 type = ct_BANG;
5642 else if (STRNICMP(p, "count", l) == 0)
5643 type = ct_COUNT;
5644 else if (STRNICMP(p, "line1", l) == 0)
5645 type = ct_LINE1;
5646 else if (STRNICMP(p, "line2", l) == 0)
5647 type = ct_LINE2;
5648 else if (STRNICMP(p, "lt", l) == 0)
5649 type = ct_LT;
5650 else if (STRNICMP(p, "register", l) == 0)
5651 type = ct_REGISTER;
5652
5653 switch (type)
5654 {
5655 case ct_ARGS:
5656 /* Simple case first */
5657 if (*eap->arg == NUL)
5658 {
5659 if (quote == 1)
5660 {
5661 result = 2;
5662 if (buf != NULL)
5663 STRCPY(buf, "''");
5664 }
5665 else
5666 result = 0;
5667 break;
5668 }
5669
5670 /* When specified there is a single argument don't split it.
5671 * Works for ":Cmd %" when % is "a b c". */
5672 if ((eap->argt & NOSPC) && quote == 2)
5673 quote = 1;
5674
5675 switch (quote)
5676 {
5677 case 0: /* No quoting, no splitting */
5678 result = STRLEN(eap->arg);
5679 if (buf != NULL)
5680 STRCPY(buf, eap->arg);
5681 break;
5682 case 1: /* Quote, but don't split */
5683 result = STRLEN(eap->arg) + 2;
5684 for (p = eap->arg; *p; ++p)
5685 {
5686 if (*p == '\\' || *p == '"')
5687 ++result;
5688 }
5689
5690 if (buf != NULL)
5691 {
5692 *buf++ = '"';
5693 for (p = eap->arg; *p; ++p)
5694 {
5695 if (*p == '\\' || *p == '"')
5696 *buf++ = '\\';
5697 *buf++ = *p;
5698 }
5699 *buf = '"';
5700 }
5701
5702 break;
5703 case 2: /* Quote and split */
5704 /* This is hard, so only do it once, and cache the result */
5705 if (*split_buf == NULL)
5706 *split_buf = uc_split_args(eap->arg, split_len);
5707
5708 result = *split_len;
5709 if (buf != NULL && result != 0)
5710 STRCPY(buf, *split_buf);
5711
5712 break;
5713 }
5714 break;
5715
5716 case ct_BANG:
5717 result = eap->forceit ? 1 : 0;
5718 if (quote)
5719 result += 2;
5720 if (buf != NULL)
5721 {
5722 if (quote)
5723 *buf++ = '"';
5724 if (eap->forceit)
5725 *buf++ = '!';
5726 if (quote)
5727 *buf = '"';
5728 }
5729 break;
5730
5731 case ct_LINE1:
5732 case ct_LINE2:
5733 case ct_COUNT:
5734 {
5735 char num_buf[20];
5736 long num = (type == ct_LINE1) ? eap->line1 :
5737 (type == ct_LINE2) ? eap->line2 :
5738 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5739 size_t num_len;
5740
5741 sprintf(num_buf, "%ld", num);
5742 num_len = STRLEN(num_buf);
5743 result = num_len;
5744
5745 if (quote)
5746 result += 2;
5747
5748 if (buf != NULL)
5749 {
5750 if (quote)
5751 *buf++ = '"';
5752 STRCPY(buf, num_buf);
5753 buf += num_len;
5754 if (quote)
5755 *buf = '"';
5756 }
5757
5758 break;
5759 }
5760
5761 case ct_REGISTER:
5762 result = eap->regname ? 1 : 0;
5763 if (quote)
5764 result += 2;
5765 if (buf != NULL)
5766 {
5767 if (quote)
5768 *buf++ = '\'';
5769 if (eap->regname)
5770 *buf++ = eap->regname;
5771 if (quote)
5772 *buf = '\'';
5773 }
5774 break;
5775
5776 case ct_LT:
5777 result = 1;
5778 if (buf != NULL)
5779 *buf = '<';
5780 break;
5781
5782 default:
5783 /* Not recognized: just copy the '<' and return -1. */
5784 result = (size_t)-1;
5785 if (buf != NULL)
5786 *buf = '<';
5787 break;
5788 }
5789
5790 return result;
5791}
5792
5793 static void
5794do_ucmd(eap)
5795 exarg_T *eap;
5796{
5797 char_u *buf;
5798 char_u *p;
5799 char_u *q;
5800
5801 char_u *start;
5802 char_u *end;
5803 size_t len, totlen;
5804
5805 size_t split_len = 0;
5806 char_u *split_buf = NULL;
5807 ucmd_T *cmd;
5808#ifdef FEAT_EVAL
5809 scid_T save_current_SID = current_SID;
5810#endif
5811
5812 if (eap->cmdidx == CMD_USER)
5813 cmd = USER_CMD(eap->useridx);
5814 else
5815 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5816
5817 /*
5818 * Replace <> in the command by the arguments.
5819 */
5820 buf = NULL;
5821 for (;;)
5822 {
5823 p = cmd->uc_rep;
5824 q = buf;
5825 totlen = 0;
5826 while ((start = vim_strchr(p, '<')) != NULL
5827 && (end = vim_strchr(start + 1, '>')) != NULL)
5828 {
5829 /* Include the '>' */
5830 ++end;
5831
5832 /* Take everything up to the '<' */
5833 len = start - p;
5834 if (buf == NULL)
5835 totlen += len;
5836 else
5837 {
5838 mch_memmove(q, p, len);
5839 q += len;
5840 }
5841
5842 len = uc_check_code(start, end - start, q, cmd, eap,
5843 &split_buf, &split_len);
5844 if (len == (size_t)-1)
5845 {
5846 /* no match, continue after '<' */
5847 p = start + 1;
5848 len = 1;
5849 }
5850 else
5851 p = end;
5852 if (buf == NULL)
5853 totlen += len;
5854 else
5855 q += len;
5856 }
5857 if (buf != NULL) /* second time here, finished */
5858 {
5859 STRCPY(q, p);
5860 break;
5861 }
5862
5863 totlen += STRLEN(p); /* Add on the trailing characters */
5864 buf = alloc((unsigned)(totlen + 1));
5865 if (buf == NULL)
5866 {
5867 vim_free(split_buf);
5868 return;
5869 }
5870 }
5871
5872#ifdef FEAT_EVAL
5873 current_SID = cmd->uc_scriptID;
5874#endif
5875 (void)do_cmdline(buf, eap->getline, eap->cookie,
5876 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5877#ifdef FEAT_EVAL
5878 current_SID = save_current_SID;
5879#endif
5880 vim_free(buf);
5881 vim_free(split_buf);
5882}
5883
5884# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5885 static char_u *
5886get_user_command_name(idx)
5887 int idx;
5888{
5889 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5890}
5891
5892/*
5893 * Function given to ExpandGeneric() to obtain the list of user command names.
5894 */
5895/*ARGSUSED*/
5896 char_u *
5897get_user_commands(xp, idx)
5898 expand_T *xp;
5899 int idx;
5900{
5901 if (idx < curbuf->b_ucmds.ga_len)
5902 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5903 idx -= curbuf->b_ucmds.ga_len;
5904 if (idx < ucmds.ga_len)
5905 return USER_CMD(idx)->uc_name;
5906 return NULL;
5907}
5908
5909/*
5910 * Function given to ExpandGeneric() to obtain the list of user command
5911 * attributes.
5912 */
5913/*ARGSUSED*/
5914 char_u *
5915get_user_cmd_flags(xp, idx)
5916 expand_T *xp;
5917 int idx;
5918{
5919 static char *user_cmd_flags[] =
5920 {"bang", "bar", "buffer", "complete", "count",
5921 "nargs", "range", "register"};
5922
5923 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5924 return NULL;
5925 return (char_u *)user_cmd_flags[idx];
5926}
5927
5928/*
5929 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5930 */
5931/*ARGSUSED*/
5932 char_u *
5933get_user_cmd_nargs(xp, idx)
5934 expand_T *xp;
5935 int idx;
5936{
5937 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5938
5939 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5940 return NULL;
5941 return (char_u *)user_cmd_nargs[idx];
5942}
5943
5944/*
5945 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5946 */
5947/*ARGSUSED*/
5948 char_u *
5949get_user_cmd_complete(xp, idx)
5950 expand_T *xp;
5951 int idx;
5952{
5953 return (char_u *)command_complete[idx].name;
5954}
5955# endif /* FEAT_CMDL_COMPL */
5956
5957#endif /* FEAT_USR_CMDS */
5958
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005959#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5960/*
5961 * Parse a completion argument "value[vallen]".
5962 * The detected completion goes in "*complp", argument type in "*argt".
5963 * When there is an argument, for function and user defined completion, it's
5964 * copied to allocated memory and stored in "*compl_arg".
5965 * Returns FAIL if something is wrong.
5966 */
5967 int
5968parse_compl_arg(value, vallen, complp, argt, compl_arg)
5969 char_u *value;
5970 int vallen;
5971 int *complp;
5972 long *argt;
5973 char_u **compl_arg;
5974{
5975 char_u *arg = NULL;
5976 size_t arglen = 0;
5977 int i;
5978 int valend = vallen;
5979
5980 /* Look for any argument part - which is the part after any ',' */
5981 for (i = 0; i < vallen; ++i)
5982 {
5983 if (value[i] == ',')
5984 {
5985 arg = &value[i + 1];
5986 arglen = vallen - i - 1;
5987 valend = i;
5988 break;
5989 }
5990 }
5991
5992 for (i = 0; command_complete[i].expand != 0; ++i)
5993 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005994 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005995 && STRNCMP(value, command_complete[i].name, valend) == 0)
5996 {
5997 *complp = command_complete[i].expand;
5998 if (command_complete[i].expand == EXPAND_BUFFERS)
5999 *argt |= BUFNAME;
6000 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6001 || command_complete[i].expand == EXPAND_FILES)
6002 *argt |= XFILE;
6003 break;
6004 }
6005 }
6006
6007 if (command_complete[i].expand == 0)
6008 {
6009 EMSG2(_("E180: Invalid complete value: %s"), value);
6010 return FAIL;
6011 }
6012
6013# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6014 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6015 && arg != NULL)
6016# else
6017 if (arg != NULL)
6018# endif
6019 {
6020 EMSG(_("E468: Completion argument only allowed for custom completion"));
6021 return FAIL;
6022 }
6023
6024# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6025 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6026 && arg == NULL)
6027 {
6028 EMSG(_("E467: Custom completion requires a function argument"));
6029 return FAIL;
6030 }
6031
6032 if (arg != NULL)
6033 *compl_arg = vim_strnsave(arg, (int)arglen);
6034# endif
6035 return OK;
6036}
6037#endif
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 static void
6040ex_colorscheme(eap)
6041 exarg_T *eap;
6042{
6043 if (load_colors(eap->arg) == FAIL)
6044 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6045}
6046
6047 static void
6048ex_highlight(eap)
6049 exarg_T *eap;
6050{
6051 if (*eap->arg == NUL && eap->cmd[2] == '!')
6052 MSG(_("Greetings, Vim user!"));
6053 do_highlight(eap->arg, eap->forceit, FALSE);
6054}
6055
6056
6057/*
6058 * Call this function if we thought we were going to exit, but we won't
6059 * (because of an error). May need to restore the terminal mode.
6060 */
6061 void
6062not_exiting()
6063{
6064 exiting = FALSE;
6065 settmode(TMODE_RAW);
6066}
6067
6068/*
6069 * ":quit": quit current window, quit Vim if closed the last window.
6070 */
6071 static void
6072ex_quit(eap)
6073 exarg_T *eap;
6074{
6075#ifdef FEAT_CMDWIN
6076 if (cmdwin_type != 0)
6077 {
6078 cmdwin_result = Ctrl_C;
6079 return;
6080 }
6081#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006082 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006083 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006084 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006085 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006086 return;
6087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088
6089#ifdef FEAT_NETBEANS_INTG
6090 netbeansForcedQuit = eap->forceit;
6091#endif
6092
6093 /*
6094 * If there are more files or windows we won't exit.
6095 */
6096 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6097 exiting = TRUE;
6098 if ((!P_HID(curbuf)
6099 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6100 || check_more(TRUE, eap->forceit) == FAIL
6101 || (only_one_window() && check_changed_any(eap->forceit)))
6102 {
6103 not_exiting();
6104 }
6105 else
6106 {
6107#ifdef FEAT_WINDOWS
6108 if (only_one_window()) /* quit last window */
6109#endif
6110 getout(0);
6111#ifdef FEAT_WINDOWS
6112# ifdef FEAT_GUI
6113 need_mouse_correct = TRUE;
6114# endif
6115 /* close window; may free buffer */
6116 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6117#endif
6118 }
6119}
6120
6121/*
6122 * ":cquit".
6123 */
6124/*ARGSUSED*/
6125 static void
6126ex_cquit(eap)
6127 exarg_T *eap;
6128{
6129 getout(1); /* this does not always pass on the exit code to the Manx
6130 compiler. why? */
6131}
6132
6133/*
6134 * ":qall": try to quit all windows
6135 */
6136 static void
6137ex_quit_all(eap)
6138 exarg_T *eap;
6139{
6140# ifdef FEAT_CMDWIN
6141 if (cmdwin_type != 0)
6142 {
6143 if (eap->forceit)
6144 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6145 else
6146 cmdwin_result = K_XF2;
6147 return;
6148 }
6149# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006150
6151 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006152 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006153 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006154 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006155 return;
6156 }
6157
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 exiting = TRUE;
6159 if (eap->forceit || !check_changed_any(FALSE))
6160 getout(0);
6161 not_exiting();
6162}
6163
Bram Moolenaard9967712006-03-11 21:18:15 +00006164#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165/*
6166 * ":close": close current window, unless it is the last one
6167 */
6168 static void
6169ex_close(eap)
6170 exarg_T *eap;
6171{
6172# ifdef FEAT_CMDWIN
6173 if (cmdwin_type != 0)
6174 cmdwin_result = K_IGNORE;
6175 else
6176# endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006177 if (!text_locked())
Bram Moolenaarf740b292006-02-16 22:11:02 +00006178 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179}
6180
Bram Moolenaard9967712006-03-11 21:18:15 +00006181# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006182/*
6183 * ":pclose": Close any preview window.
6184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006188{
6189 win_T *win;
6190
6191 for (win = firstwin; win != NULL; win = win->w_next)
6192 if (win->w_p_pvw)
6193 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006194 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006195 break;
6196 }
6197}
Bram Moolenaard9967712006-03-11 21:18:15 +00006198# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006199
Bram Moolenaarf740b292006-02-16 22:11:02 +00006200/*
6201 * Close window "win" and take care of handling closing the last window for a
6202 * modified buffer.
6203 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006204 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006205ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006208 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
6210 int need_hide;
6211 buf_T *buf = win->w_buffer;
6212
6213 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006216# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if ((p_confirm || cmdmod.confirm) && p_write)
6218 {
6219 dialog_changed(buf, FALSE);
6220 if (buf_valid(buf) && bufIsChanged(buf))
6221 return;
6222 need_hide = FALSE;
6223 }
6224 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
6227 EMSG(_(e_nowrtmsg));
6228 return;
6229 }
6230 }
6231
Bram Moolenaard9967712006-03-11 21:18:15 +00006232# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006234# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006237 if (tp == NULL)
6238 win_close(win, !need_hide && !P_HID(buf));
6239 else
6240 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241}
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006244 * ":tabclose": close current tab page, unless it is the last one.
6245 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 */
6247 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 exarg_T *eap;
6250{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006251 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006252 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006253
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254# ifdef FEAT_CMDWIN
6255 if (cmdwin_type != 0)
6256 cmdwin_result = K_IGNORE;
6257 else
6258# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006259 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006260 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006263 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006264 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006265 tp = find_tabpage((int)eap->line2);
6266 if (tp == NULL)
6267 {
6268 beep_flush();
6269 return;
6270 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006271 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006272 {
6273 tabpage_close_other(tp, eap->forceit);
6274 return;
6275 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006276 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00006277 if (!text_locked())
6278 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006280
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006281 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006282 shell_new_rows();
6283}
6284
6285/*
6286 * ":tabonly": close all tab pages except the current one
6287 */
6288 static void
6289ex_tabonly(eap)
6290 exarg_T *eap;
6291{
6292 tabpage_T *tp;
6293 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006294 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006295
6296# ifdef FEAT_CMDWIN
6297 if (cmdwin_type != 0)
6298 cmdwin_result = K_IGNORE;
6299 else
6300# endif
6301 if (first_tabpage->tp_next == NULL)
6302 MSG(_("Already only one tab page"));
6303 else
6304 {
6305 /* Repeat this up to a 1000 times, because autocommands may mess
6306 * up the lists. */
6307 for (done = 0; done < 1000; ++done)
6308 {
6309 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6310 if (tp->tp_topframe != topframe)
6311 {
6312 tabpage_close_other(tp, eap->forceit);
6313 /* if we failed to close it quit */
6314 if (valid_tabpage(tp))
6315 done = 1000;
6316 /* start over, "tp" is now invalid */
6317 break;
6318 }
6319 if (first_tabpage->tp_next == NULL)
6320 break;
6321 }
6322 }
6323
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006324 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006325 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
6328/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006329 * Close the current tab page.
6330 */
6331 void
6332tabpage_close(forceit)
6333 int forceit;
6334{
6335 /* First close all the windows but the current one. If that worked then
6336 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006337 if (lastwin != firstwin)
6338 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006339 if (lastwin == firstwin)
6340 ex_win_close(forceit, curwin, NULL);
6341# ifdef FEAT_GUI
6342 need_mouse_correct = TRUE;
6343# endif
6344}
6345
6346/*
6347 * Close tab page "tp", which is not the current tab page.
6348 * Note that autocommands may make "tp" invalid.
6349 */
6350 void
6351tabpage_close_other(tp, forceit)
6352 tabpage_T *tp;
6353 int forceit;
6354{
6355 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006356 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006357
6358 /* Limit to 1000 windows, autocommands may add a window while we close
6359 * one. OK, so I'm paranoid... */
6360 while (++done < 1000)
6361 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006362 wp = tp->tp_firstwin;
6363 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006364
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006365 /* Autocommands may delete the tab page under our fingers and we may
6366 * fail to close a window with a modified buffer. */
6367 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006368 break;
6369 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006370 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006371}
6372
6373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 * ":only".
6375 */
6376 static void
6377ex_only(eap)
6378 exarg_T *eap;
6379{
6380# ifdef FEAT_GUI
6381 need_mouse_correct = TRUE;
6382# endif
6383 close_others(TRUE, eap->forceit);
6384}
6385
6386/*
6387 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006388 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006390 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391ex_all(eap)
6392 exarg_T *eap;
6393{
6394 if (eap->addr_count == 0)
6395 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006396 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397}
6398#endif /* FEAT_WINDOWS */
6399
6400 static void
6401ex_hide(eap)
6402 exarg_T *eap;
6403{
6404 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6405 eap->errmsg = e_invarg;
6406 else
6407 {
6408 /* ":hide" or ":hide | cmd": hide current window */
6409 eap->nextcmd = check_nextcmd(eap->arg);
6410#ifdef FEAT_WINDOWS
6411 if (!eap->skip)
6412 {
6413# ifdef FEAT_GUI
6414 need_mouse_correct = TRUE;
6415# endif
6416 win_close(curwin, FALSE); /* don't free buffer */
6417 }
6418#endif
6419 }
6420}
6421
6422/*
6423 * ":stop" and ":suspend": Suspend Vim.
6424 */
6425 static void
6426ex_stop(eap)
6427 exarg_T *eap;
6428{
6429 /*
6430 * Disallow suspending for "rvim".
6431 */
6432 if (!check_restricted()
6433#ifdef WIN3264
6434 /*
6435 * Check if external commands are allowed now.
6436 */
6437 && can_end_termcap_mode(TRUE)
6438#endif
6439 )
6440 {
6441 if (!eap->forceit)
6442 autowrite_all();
6443 windgoto((int)Rows - 1, 0);
6444 out_char('\n');
6445 out_flush();
6446 stoptermcap();
6447 out_flush(); /* needed for SUN to restore xterm buffer */
6448#ifdef FEAT_TITLE
6449 mch_restore_title(3); /* restore window titles */
6450#endif
6451 ui_suspend(); /* call machine specific function */
6452#ifdef FEAT_TITLE
6453 maketitle();
6454 resettitle(); /* force updating the title */
6455#endif
6456 starttermcap();
6457 scroll_start(); /* scroll screen before redrawing */
6458 redraw_later_clear();
6459 shell_resized(); /* may have resized window */
6460 }
6461}
6462
6463/*
6464 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6465 */
6466 static void
6467ex_exit(eap)
6468 exarg_T *eap;
6469{
6470#ifdef FEAT_CMDWIN
6471 if (cmdwin_type != 0)
6472 {
6473 cmdwin_result = Ctrl_C;
6474 return;
6475 }
6476#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006477 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006478 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006479 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006480 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006481 return;
6482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
6484 /*
6485 * if more files or windows we won't exit
6486 */
6487 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6488 exiting = TRUE;
6489 if ( ((eap->cmdidx == CMD_wq
6490 || curbufIsChanged())
6491 && do_write(eap) == FAIL)
6492 || check_more(TRUE, eap->forceit) == FAIL
6493 || (only_one_window() && check_changed_any(eap->forceit)))
6494 {
6495 not_exiting();
6496 }
6497 else
6498 {
6499#ifdef FEAT_WINDOWS
6500 if (only_one_window()) /* quit last window, exit Vim */
6501#endif
6502 getout(0);
6503#ifdef FEAT_WINDOWS
6504# ifdef FEAT_GUI
6505 need_mouse_correct = TRUE;
6506# endif
6507 /* quit current window, may free buffer */
6508 win_close(curwin, !P_HID(curwin->w_buffer));
6509#endif
6510 }
6511}
6512
6513/*
6514 * ":print", ":list", ":number".
6515 */
6516 static void
6517ex_print(eap)
6518 exarg_T *eap;
6519{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006520 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6521 EMSG(_(e_emptybuf));
6522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006524 for ( ;!got_int; ui_breakcheck())
6525 {
6526 print_line(eap->line1,
6527 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6528 || (eap->flags & EXFLAG_NR)),
6529 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6530 if (++eap->line1 > eap->line2)
6531 break;
6532 out_flush(); /* show one line at a time */
6533 }
6534 setpcmark();
6535 /* put cursor at last line */
6536 curwin->w_cursor.lnum = eap->line2;
6537 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 }
6539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541}
6542
6543#ifdef FEAT_BYTEOFF
6544 static void
6545ex_goto(eap)
6546 exarg_T *eap;
6547{
6548 goto_byte(eap->line2);
6549}
6550#endif
6551
6552/*
6553 * ":shell".
6554 */
6555/*ARGSUSED*/
6556 static void
6557ex_shell(eap)
6558 exarg_T *eap;
6559{
6560 do_shell(NULL, 0);
6561}
6562
6563#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6564 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006565 || defined(FEAT_GUI_MSWIN) \
6566 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 || defined(PROTO)
6568
6569/*
6570 * Handle a file drop. The code is here because a drop is *nearly* like an
6571 * :args command, but not quite (we have a list of exact filenames, so we
6572 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6573 * code is very similar to :args and hence needs access to a lot of the static
6574 * functions in this file.
6575 *
6576 * The list should be allocated using alloc(), as should each item in the
6577 * list. This function takes over responsibility for freeing the list.
6578 *
6579 * XXX The list is made into the arggument list. This is freed using
6580 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6581 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6582 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6583 * file functionality is (currently) not in EMX this is not presently a
6584 * problem.
6585 */
6586 void
6587handle_drop(filec, filev, split)
6588 int filec; /* the number of files dropped */
6589 char_u **filev; /* the list of files dropped */
6590 int split; /* force splitting the window */
6591{
6592 exarg_T ea;
6593 int save_msg_scroll = msg_scroll;
6594
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006595 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006596 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598
6599 /* Check whether the current buffer is changed. If so, we will need
6600 * to split the current window or data could be lost.
6601 * We don't need to check if the 'hidden' option is set, as in this
6602 * case the buffer won't be lost.
6603 */
6604 if (!P_HID(curbuf) && !split)
6605 {
6606 ++emsg_off;
6607 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6608 --emsg_off;
6609 }
6610 if (split)
6611 {
6612# ifdef FEAT_WINDOWS
6613 if (win_split(0, 0) == FAIL)
6614 return;
6615# ifdef FEAT_SCROLLBIND
6616 curwin->w_p_scb = FALSE;
6617# endif
6618
6619 /* When splitting the window, create a new alist. Otherwise the
6620 * existing one is overwritten. */
6621 alist_unlink(curwin->w_alist);
6622 alist_new();
6623# else
6624 return; /* can't split, always fail */
6625# endif
6626 }
6627
6628 /*
6629 * Set up the new argument list.
6630 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006631 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
6633 /*
6634 * Move to the first file.
6635 */
6636 /* Fake up a minimal "next" command for do_argfile() */
6637 vim_memset(&ea, 0, sizeof(ea));
6638 ea.cmd = (char_u *)"next";
6639 do_argfile(&ea, 0);
6640
6641 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6642 * mode that is not needed here. */
6643 need_start_insertmode = FALSE;
6644
6645 /* Restore msg_scroll, otherwise a following command may cause scrolling
6646 * unexpectedly. The screen will be redrawn by the caller, thus
6647 * msg_scroll being set by displaying a message is irrelevant. */
6648 msg_scroll = save_msg_scroll;
6649}
6650#endif
6651
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652/*
6653 * Clear an argument list: free all file names and reset it to zero entries.
6654 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006655 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656alist_clear(al)
6657 alist_T *al;
6658{
6659 while (--al->al_ga.ga_len >= 0)
6660 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6661 ga_clear(&al->al_ga);
6662}
6663
6664/*
6665 * Init an argument list.
6666 */
6667 void
6668alist_init(al)
6669 alist_T *al;
6670{
6671 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6672}
6673
6674#if defined(FEAT_WINDOWS) || defined(PROTO)
6675
6676/*
6677 * Remove a reference from an argument list.
6678 * Ignored when the argument list is the global one.
6679 * If the argument list is no longer used by any window, free it.
6680 */
6681 void
6682alist_unlink(al)
6683 alist_T *al;
6684{
6685 if (al != &global_alist && --al->al_refcount <= 0)
6686 {
6687 alist_clear(al);
6688 vim_free(al);
6689 }
6690}
6691
6692# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6693/*
6694 * Create a new argument list and use it for the current window.
6695 */
6696 void
6697alist_new()
6698{
6699 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6700 if (curwin->w_alist == NULL)
6701 {
6702 curwin->w_alist = &global_alist;
6703 ++global_alist.al_refcount;
6704 }
6705 else
6706 {
6707 curwin->w_alist->al_refcount = 1;
6708 alist_init(curwin->w_alist);
6709 }
6710}
6711# endif
6712#endif
6713
6714#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6715/*
6716 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006717 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6718 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 */
6720 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006721alist_expand(fnum_list, fnum_len)
6722 int *fnum_list;
6723 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006726 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 char_u **new_arg_files;
6728 int new_arg_file_count;
6729 char_u *save_p_su = p_su;
6730 int i;
6731
6732 /* Don't use 'suffixes' here. This should work like the shell did the
6733 * expansion. Also, the vimrc file isn't read yet, thus the user
6734 * can't set the options. */
6735 p_su = empty_option;
6736 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6737 if (old_arg_files != NULL)
6738 {
6739 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006740 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6741 old_arg_count = GARGCOUNT;
6742 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 &new_arg_file_count, &new_arg_files,
6744 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6745 && new_arg_file_count > 0)
6746 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006747 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6748 TRUE, fnum_list, fnum_len);
6749 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 }
6752 p_su = save_p_su;
6753}
6754#endif
6755
6756/*
6757 * Set the argument list for the current window.
6758 * Takes over the allocated files[] and the allocated fnames in it.
6759 */
6760 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006761alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 alist_T *al;
6763 int count;
6764 char_u **files;
6765 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006766 int *fnum_list;
6767 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 int i;
6770
6771 alist_clear(al);
6772 if (ga_grow(&al->al_ga, count) == OK)
6773 {
6774 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006775 {
6776 if (got_int)
6777 {
6778 /* When adding many buffers this can take a long time. Allow
6779 * interrupting here. */
6780 while (i < count)
6781 vim_free(files[i++]);
6782 break;
6783 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006784
6785 /* May set buffer name of a buffer previously used for the
6786 * argument list, so that it's re-used by alist_add. */
6787 if (fnum_list != NULL && i < fnum_len)
6788 buf_set_name(fnum_list[i], files[i]);
6789
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006791 ui_breakcheck();
6792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 vim_free(files);
6794 }
6795 else
6796 FreeWild(count, files);
6797#ifdef FEAT_WINDOWS
6798 if (al == &global_alist)
6799#endif
6800 arg_had_last = FALSE;
6801}
6802
6803/*
6804 * Add file "fname" to argument list "al".
6805 * "fname" must have been allocated and "al" must have been checked for room.
6806 */
6807 void
6808alist_add(al, fname, set_fnum)
6809 alist_T *al;
6810 char_u *fname;
6811 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6812{
6813 if (fname == NULL) /* don't add NULL file names */
6814 return;
6815#ifdef BACKSLASH_IN_FILENAME
6816 slash_adjust(fname);
6817#endif
6818 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6819 if (set_fnum > 0)
6820 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6821 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6822 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823}
6824
6825#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6826/*
6827 * Adjust slashes in file names. Called after 'shellslash' was set.
6828 */
6829 void
6830alist_slash_adjust()
6831{
6832 int i;
6833# ifdef FEAT_WINDOWS
6834 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006835 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836# endif
6837
6838 for (i = 0; i < GARGCOUNT; ++i)
6839 if (GARGLIST[i].ae_fname != NULL)
6840 slash_adjust(GARGLIST[i].ae_fname);
6841# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006842 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 if (wp->w_alist != &global_alist)
6844 for (i = 0; i < WARGCOUNT(wp); ++i)
6845 if (WARGLIST(wp)[i].ae_fname != NULL)
6846 slash_adjust(WARGLIST(wp)[i].ae_fname);
6847# endif
6848}
6849#endif
6850
6851/*
6852 * ":preserve".
6853 */
6854/*ARGSUSED*/
6855 static void
6856ex_preserve(eap)
6857 exarg_T *eap;
6858{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006859 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 ml_preserve(curbuf, TRUE);
6861}
6862
6863/*
6864 * ":recover".
6865 */
6866 static void
6867ex_recover(eap)
6868 exarg_T *eap;
6869{
6870 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6871 recoverymode = TRUE;
6872 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6873 && (*eap->arg == NUL
6874 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6875 ml_recover();
6876 recoverymode = FALSE;
6877}
6878
6879/*
6880 * Command modifier used in a wrong way.
6881 */
6882 static void
6883ex_wrongmodifier(eap)
6884 exarg_T *eap;
6885{
6886 eap->errmsg = e_invcmd;
6887}
6888
6889#ifdef FEAT_WINDOWS
6890/*
6891 * :sview [+command] file split window with new file, read-only
6892 * :split [[+command] file] split window with current or new file
6893 * :vsplit [[+command] file] split window vertically with current or new file
6894 * :new [[+command] file] split window with no or new file
6895 * :vnew [[+command] file] split vertically window with no or new file
6896 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006897 *
6898 * :tabedit open new Tab page with empty window
6899 * :tabedit [+command] file open new Tab page and edit "file"
6900 * :tabnew [[+command] file] just like :tabedit
6901 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 */
6903 void
6904ex_splitview(eap)
6905 exarg_T *eap;
6906{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006907 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006908# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910# endif
6911# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006915# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6917 {
6918 ex_ni(eap);
6919 return;
6920 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006923# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006927# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 /* A ":split" in the quickfix window works like ":new". Don't want two
6929 * quickfix windows. */
6930 if (bt_quickfix(curbuf))
6931 {
6932 if (eap->cmdidx == CMD_split)
6933 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006934# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 if (eap->cmdidx == CMD_vsplit)
6936 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006941# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006942 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 {
6944 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6945 FNAME_MESS, TRUE, curbuf->b_ffname);
6946 if (fname == NULL)
6947 goto theend;
6948 eap->arg = fname;
6949 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006950# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006954# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006956# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 && eap->cmdidx != CMD_new)
6960 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006961 if (
6962# ifdef FEAT_GUI
6963 !gui.in_use &&
6964# endif
6965 au_has_group((char_u *)"FileExplorer"))
6966 {
6967 /* No browsing supported but we do have the file explorer:
6968 * Edit the directory. */
6969 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6970 eap->arg = (char_u *)".";
6971 }
6972 else
6973 {
6974 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006976 if (fname == NULL)
6977 goto theend;
6978 eap->arg = fname;
6979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 }
6981 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006984 /*
6985 * Either open new tab page or split the window.
6986 */
6987 if (eap->cmdidx == CMD_tabedit
6988 || eap->cmdidx == CMD_tabfind
6989 || eap->cmdidx == CMD_tabnew)
6990 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006991 if (win_new_tabpage(cmdmod.tab) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006992 {
6993 do_exedit(eap, NULL);
6994
6995 /* set the alternate buffer for the window we came from */
6996 if (curwin != old_curwin
6997 && win_valid(old_curwin)
6998 && old_curwin->w_buffer != curbuf
6999 && !cmdmod.keepalt)
7000 old_curwin->w_alt_fnum = curbuf->b_fnum;
7001 }
7002 }
7003 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7005 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007006# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 /* Reset 'scrollbind' when editing another file, but keep it when
7008 * doing ":split" without arguments. */
7009 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007010# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 )
7014 curwin->w_p_scb = FALSE;
7015 else
7016 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007017# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 do_exedit(eap, old_curwin);
7019 }
7020
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007021# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007025# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026theend:
7027 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007030
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007031#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007032/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007033 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007034 */
7035 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007036tabpage_new()
7037{
7038 exarg_T ea;
7039
7040 vim_memset(&ea, 0, sizeof(ea));
7041 ea.cmdidx = CMD_tabnew;
7042 ea.cmd = (char_u *)"tabn";
7043 ea.arg = (char_u *)"";
7044 ex_splitview(&ea);
7045}
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007046#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007047
7048/*
7049 * :tabnext command
7050 */
7051 static void
7052ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007053 exarg_T *eap;
7054{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007055 switch (eap->cmdidx)
7056 {
7057 case CMD_tabfirst:
7058 case CMD_tabrewind:
7059 goto_tabpage(1);
7060 break;
7061 case CMD_tablast:
7062 goto_tabpage(9999);
7063 break;
7064 case CMD_tabprevious:
7065 case CMD_tabNext:
7066 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7067 break;
7068 default: /* CMD_tabnext */
7069 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7070 break;
7071 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007072}
7073
7074/*
7075 * :tabmove command
7076 */
7077 static void
7078ex_tabmove(eap)
7079 exarg_T *eap;
7080{
7081 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007082}
7083
7084/*
7085 * :tabs command: List tabs and their contents.
7086 */
7087/*ARGSUSED*/
7088 static void
7089ex_tabs(eap)
7090 exarg_T *eap;
7091{
7092 tabpage_T *tp;
7093 win_T *wp;
7094 int tabcount = 1;
7095
7096 msg_start();
7097 msg_scroll = TRUE;
7098 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7099 {
7100 msg_putchar('\n');
7101 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7102 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7103 out_flush(); /* output one line at a time */
7104 ui_breakcheck();
7105
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007106 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007107 wp = firstwin;
7108 else
7109 wp = tp->tp_firstwin;
7110 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7111 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007112 msg_putchar('\n');
7113 msg_putchar(wp == curwin ? '>' : ' ');
7114 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007115 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7116 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007117 if (buf_spname(wp->w_buffer) != NULL)
7118 STRCPY(IObuff, buf_spname(wp->w_buffer));
7119 else
7120 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7121 IObuff, IOSIZE, TRUE);
7122 msg_outtrans(IObuff);
7123 out_flush(); /* output one line at a time */
7124 ui_breakcheck();
7125 }
7126 }
7127}
7128
7129#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
7131/*
7132 * ":mode": Set screen mode.
7133 * If no argument given, just get the screen size and redraw.
7134 */
7135 static void
7136ex_mode(eap)
7137 exarg_T *eap;
7138{
7139 if (*eap->arg == NUL)
7140 shell_resized();
7141 else
7142 mch_screenmode(eap->arg);
7143}
7144
7145#ifdef FEAT_WINDOWS
7146/*
7147 * ":resize".
7148 * set, increment or decrement current window height
7149 */
7150 static void
7151ex_resize(eap)
7152 exarg_T *eap;
7153{
7154 int n;
7155 win_T *wp = curwin;
7156
7157 if (eap->addr_count > 0)
7158 {
7159 n = eap->line2;
7160 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7161 ;
7162 }
7163
7164#ifdef FEAT_GUI
7165 need_mouse_correct = TRUE;
7166#endif
7167 n = atol((char *)eap->arg);
7168#ifdef FEAT_VERTSPLIT
7169 if (cmdmod.split & WSP_VERT)
7170 {
7171 if (*eap->arg == '-' || *eap->arg == '+')
7172 n += W_WIDTH(curwin);
7173 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7174 n = 9999;
7175 win_setwidth_win((int)n, wp);
7176 }
7177 else
7178#endif
7179 {
7180 if (*eap->arg == '-' || *eap->arg == '+')
7181 n += curwin->w_height;
7182 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7183 n = 9999;
7184 win_setheight_win((int)n, wp);
7185 }
7186}
7187#endif
7188
7189/*
7190 * ":find [+command] <file>" command.
7191 */
7192 static void
7193ex_find(eap)
7194 exarg_T *eap;
7195{
7196#ifdef FEAT_SEARCHPATH
7197 char_u *fname;
7198 int count;
7199
7200 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7201 TRUE, curbuf->b_ffname);
7202 if (eap->addr_count > 0)
7203 {
7204 /* Repeat finding the file "count" times. This matters when it
7205 * appears several times in the path. */
7206 count = eap->line2;
7207 while (fname != NULL && --count > 0)
7208 {
7209 vim_free(fname);
7210 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7211 FALSE, curbuf->b_ffname);
7212 }
7213 }
7214
7215 if (fname != NULL)
7216 {
7217 eap->arg = fname;
7218#endif
7219 do_exedit(eap, NULL);
7220#ifdef FEAT_SEARCHPATH
7221 vim_free(fname);
7222 }
7223#endif
7224}
7225
7226/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007227 * ":open" simulation: for now just work like ":visual".
7228 */
7229 static void
7230ex_open(eap)
7231 exarg_T *eap;
7232{
7233 regmatch_T regmatch;
7234 char_u *p;
7235
7236 curwin->w_cursor.lnum = eap->line2;
7237 beginline(BL_SOL | BL_FIX);
7238 if (*eap->arg == '/')
7239 {
7240 /* ":open /pattern/": put cursor in column found with pattern */
7241 ++eap->arg;
7242 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7243 *p = NUL;
7244 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7245 if (regmatch.regprog != NULL)
7246 {
7247 regmatch.rm_ic = p_ic;
7248 p = ml_get_curline();
7249 if (vim_regexec(&regmatch, p, (colnr_T)0))
7250 curwin->w_cursor.col = regmatch.startp[0] - p;
7251 else
7252 EMSG(_(e_nomatch));
7253 vim_free(regmatch.regprog);
7254 }
7255 /* Move to the NUL, ignore any other arguments. */
7256 eap->arg += STRLEN(eap->arg);
7257 }
7258 check_cursor();
7259
7260 eap->cmdidx = CMD_visual;
7261 do_exedit(eap, NULL);
7262}
7263
7264/*
7265 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 */
7267 static void
7268ex_edit(eap)
7269 exarg_T *eap;
7270{
7271 do_exedit(eap, NULL);
7272}
7273
7274/*
7275 * ":edit <file>" command and alikes.
7276 */
7277/*ARGSUSED*/
7278 void
7279do_exedit(eap, old_curwin)
7280 exarg_T *eap;
7281 win_T *old_curwin; /* curwin before doing a split or NULL */
7282{
7283 int n;
7284#ifdef FEAT_WINDOWS
7285 int need_hide;
7286#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007287 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288
7289 /*
7290 * ":vi" command ends Ex mode.
7291 */
7292 if (exmode_active && (eap->cmdidx == CMD_visual
7293 || eap->cmdidx == CMD_view))
7294 {
7295 exmode_active = FALSE;
7296 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007297 {
7298 /* Special case: ":global/pat/visual\NLvi-commands" */
7299 if (global_busy)
7300 {
7301 int rd = RedrawingDisabled;
7302 int nwr = no_wait_return;
7303 int ms = msg_scroll;
7304#ifdef FEAT_GUI
7305 int he = hold_gui_events;
7306#endif
7307
7308 if (eap->nextcmd != NULL)
7309 {
7310 stuffReadbuff(eap->nextcmd);
7311 eap->nextcmd = NULL;
7312 }
7313
7314 if (exmode_was != EXMODE_VIM)
7315 settmode(TMODE_RAW);
7316 RedrawingDisabled = 0;
7317 no_wait_return = 0;
7318 need_wait_return = FALSE;
7319 msg_scroll = 0;
7320#ifdef FEAT_GUI
7321 hold_gui_events = 0;
7322#endif
7323 must_redraw = CLEAR;
7324
7325 main_loop(FALSE, TRUE);
7326
7327 RedrawingDisabled = rd;
7328 no_wait_return = nwr;
7329 msg_scroll = ms;
7330#ifdef FEAT_GUI
7331 hold_gui_events = he;
7332#endif
7333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
7337
7338 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007339 || eap->cmdidx == CMD_tabnew
7340 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341#ifdef FEAT_VERTSPLIT
7342 || eap->cmdidx == CMD_vnew
7343#endif
7344 ) && *eap->arg == NUL)
7345 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007346 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 setpcmark();
7348 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7349 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7350 }
7351 else if ((eap->cmdidx != CMD_split
7352#ifdef FEAT_VERTSPLIT
7353 && eap->cmdidx != CMD_vsplit
7354#endif
7355 )
7356 || *eap->arg != NUL
7357#ifdef FEAT_BROWSE
7358 || cmdmod.browse
7359#endif
7360 )
7361 {
7362 n = readonlymode;
7363 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7364 readonlymode = TRUE;
7365 else if (eap->cmdidx == CMD_enew)
7366 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7367 empty buffer */
7368 setpcmark();
7369 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7370 NULL, eap,
7371 /* ":edit" goes to first line if Vi compatible */
7372 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7373 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7374 ? ECMD_ONE : eap->do_ecmd_lnum,
7375 (P_HID(curbuf) ? ECMD_HIDE : 0)
7376 + (eap->forceit ? ECMD_FORCEIT : 0)
7377#ifdef FEAT_LISTCMDS
7378 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7379#endif
7380 ) == FAIL)
7381 {
7382 /* Editing the file failed. If the window was split, close it. */
7383#ifdef FEAT_WINDOWS
7384 if (old_curwin != NULL)
7385 {
7386 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7387 if (!need_hide || P_HID(curbuf))
7388 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007389# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7390 cleanup_T cs;
7391
7392 /* Reset the error/interrupt/exception state here so that
7393 * aborting() returns FALSE when closing a window. */
7394 enter_cleanup(&cs);
7395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396# ifdef FEAT_GUI
7397 need_mouse_correct = TRUE;
7398# endif
7399 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007400
7401# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7402 /* Restore the error/interrupt/exception state if not
7403 * discarded by a new aborting error, interrupt, or
7404 * uncaught exception. */
7405 leave_cleanup(&cs);
7406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 }
7408 }
7409#endif
7410 }
7411 else if (readonlymode && curbuf->b_nwindows == 1)
7412 {
7413 /* When editing an already visited buffer, 'readonly' won't be set
7414 * but the previous value is kept. With ":view" and ":sview" we
7415 * want the file to be readonly, except when another window is
7416 * editing the same buffer. */
7417 curbuf->b_p_ro = TRUE;
7418 }
7419 readonlymode = n;
7420 }
7421 else
7422 {
7423 if (eap->do_ecmd_cmd != NULL)
7424 do_cmdline_cmd(eap->do_ecmd_cmd);
7425#ifdef FEAT_TITLE
7426 n = curwin->w_arg_idx_invalid;
7427#endif
7428 check_arg_idx(curwin);
7429#ifdef FEAT_TITLE
7430 if (n != curwin->w_arg_idx_invalid)
7431 maketitle();
7432#endif
7433 }
7434
7435#ifdef FEAT_WINDOWS
7436 /*
7437 * if ":split file" worked, set alternate file name in old window to new
7438 * file
7439 */
7440 if (old_curwin != NULL
7441 && *eap->arg != NUL
7442 && curwin != old_curwin
7443 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007444 && old_curwin->w_buffer != curbuf
7445 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 old_curwin->w_alt_fnum = curbuf->b_fnum;
7447#endif
7448
7449 ex_no_reprint = TRUE;
7450}
7451
7452#ifndef FEAT_GUI
7453/*
7454 * ":gui" and ":gvim" when there is no GUI.
7455 */
7456 static void
7457ex_nogui(eap)
7458 exarg_T *eap;
7459{
7460 eap->errmsg = e_nogvim;
7461}
7462#endif
7463
7464#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7465 static void
7466ex_tearoff(eap)
7467 exarg_T *eap;
7468{
7469 gui_make_tearoff(eap->arg);
7470}
7471#endif
7472
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007473#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 static void
7475ex_popup(eap)
7476 exarg_T *eap;
7477{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007478 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479}
7480#endif
7481
7482/*ARGSUSED*/
7483 static void
7484ex_swapname(eap)
7485 exarg_T *eap;
7486{
7487 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7488 MSG(_("No swap file"));
7489 else
7490 msg(curbuf->b_ml.ml_mfp->mf_fname);
7491}
7492
7493/*
7494 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7495 * offset.
7496 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7497 */
7498/*ARGSUSED*/
7499 static void
7500ex_syncbind(eap)
7501 exarg_T *eap;
7502{
7503#ifdef FEAT_SCROLLBIND
7504 win_T *wp;
7505 long topline;
7506 long y;
7507 linenr_T old_linenr = curwin->w_cursor.lnum;
7508
7509 setpcmark();
7510
7511 /*
7512 * determine max topline
7513 */
7514 if (curwin->w_p_scb)
7515 {
7516 topline = curwin->w_topline;
7517 for (wp = firstwin; wp; wp = wp->w_next)
7518 {
7519 if (wp->w_p_scb && wp->w_buffer)
7520 {
7521 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7522 if (topline > y)
7523 topline = y;
7524 }
7525 }
7526 if (topline < 1)
7527 topline = 1;
7528 }
7529 else
7530 {
7531 topline = 1;
7532 }
7533
7534
7535 /*
7536 * set all scrollbind windows to the same topline
7537 */
7538 wp = curwin;
7539 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7540 {
7541 if (curwin->w_p_scb)
7542 {
7543 y = topline - curwin->w_topline;
7544 if (y > 0)
7545 scrollup(y, TRUE);
7546 else
7547 scrolldown(-y, TRUE);
7548 curwin->w_scbind_pos = topline;
7549 redraw_later(VALID);
7550 cursor_correct();
7551#ifdef FEAT_WINDOWS
7552 curwin->w_redr_status = TRUE;
7553#endif
7554 }
7555 }
7556 curwin = wp;
7557 if (curwin->w_p_scb)
7558 {
7559 did_syncbind = TRUE;
7560 checkpcmark();
7561 if (old_linenr != curwin->w_cursor.lnum)
7562 {
7563 char_u ctrl_o[2];
7564
7565 ctrl_o[0] = Ctrl_O;
7566 ctrl_o[1] = 0;
7567 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7568 }
7569 }
7570#endif
7571}
7572
7573
7574 static void
7575ex_read(eap)
7576 exarg_T *eap;
7577{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007578 int i;
7579 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7580 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581
7582 if (eap->usefilter) /* :r!cmd */
7583 do_bang(1, eap, FALSE, FALSE, TRUE);
7584 else
7585 {
7586 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7587 return;
7588
7589#ifdef FEAT_BROWSE
7590 if (cmdmod.browse)
7591 {
7592 char_u *browseFile;
7593
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007594 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 NULL, NULL, NULL, curbuf);
7596 if (browseFile != NULL)
7597 {
7598 i = readfile(browseFile, NULL,
7599 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7600 vim_free(browseFile);
7601 }
7602 else
7603 i = OK;
7604 }
7605 else
7606#endif
7607 if (*eap->arg == NUL)
7608 {
7609 if (check_fname() == FAIL) /* check for no file name */
7610 return;
7611 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7612 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7613 }
7614 else
7615 {
7616 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7617 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7618 i = readfile(eap->arg, NULL,
7619 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7620
7621 }
7622 if (i == FAIL)
7623 {
7624#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7625 if (!aborting())
7626#endif
7627 EMSG2(_(e_notopen), eap->arg);
7628 }
7629 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007630 {
7631 if (empty && exmode_active)
7632 {
7633 /* Delete the empty line that remains. Historically ex does
7634 * this but vi doesn't. */
7635 if (eap->line2 == 0)
7636 lnum = curbuf->b_ml.ml_line_count;
7637 else
7638 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007639 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007640 {
7641 ml_delete(lnum, FALSE);
7642 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007643 if (curwin->w_cursor.lnum > 1
7644 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007645 --curwin->w_cursor.lnum;
7646 }
7647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
7651}
7652
Bram Moolenaarea408852005-06-25 22:49:46 +00007653static char_u *prev_dir = NULL;
7654
7655#if defined(EXITFREE) || defined(PROTO)
7656 void
7657free_cd_dir()
7658{
7659 vim_free(prev_dir);
7660}
7661#endif
7662
7663
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664/*
7665 * ":cd", ":lcd", ":chdir" and ":lchdir".
7666 */
7667 static void
7668ex_cd(eap)
7669 exarg_T *eap;
7670{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 char_u *new_dir;
7672 char_u *tofree;
7673
7674 new_dir = eap->arg;
7675#if !defined(UNIX) && !defined(VMS)
7676 /* for non-UNIX ":cd" means: print current directory */
7677 if (*new_dir == NUL)
7678 ex_pwd(NULL);
7679 else
7680#endif
7681 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007682 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7683 && !eap->forceit)
7684 {
7685 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7686 return;
7687 }
7688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 /* ":cd -": Change to previous directory */
7690 if (STRCMP(new_dir, "-") == 0)
7691 {
7692 if (prev_dir == NULL)
7693 {
7694 EMSG(_("E186: No previous directory"));
7695 return;
7696 }
7697 new_dir = prev_dir;
7698 }
7699
7700 /* Save current directory for next ":cd -" */
7701 tofree = prev_dir;
7702 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7703 prev_dir = vim_strsave(NameBuff);
7704 else
7705 prev_dir = NULL;
7706
7707#if defined(UNIX) || defined(VMS)
7708 /* for UNIX ":cd" means: go to home directory */
7709 if (*new_dir == NUL)
7710 {
7711 /* use NameBuff for home directory name */
7712# ifdef VMS
7713 char_u *p;
7714
7715 p = mch_getenv((char_u *)"SYS$LOGIN");
7716 if (p == NULL || *p == NUL) /* empty is the same as not set */
7717 NameBuff[0] = NUL;
7718 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007719 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720# else
7721 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7722# endif
7723 new_dir = NameBuff;
7724 }
7725#endif
7726 if (new_dir == NULL || vim_chdir(new_dir))
7727 EMSG(_(e_failed));
7728 else
7729 {
7730 vim_free(curwin->w_localdir);
7731 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7732 {
7733 /* If still in global directory, need to remember current
7734 * directory as global directory. */
7735 if (globaldir == NULL && prev_dir != NULL)
7736 globaldir = vim_strsave(prev_dir);
7737 /* Remember this local directory for the window. */
7738 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7739 curwin->w_localdir = vim_strsave(NameBuff);
7740 }
7741 else
7742 {
7743 /* We are now in the global directory, no need to remember its
7744 * name. */
7745 vim_free(globaldir);
7746 globaldir = NULL;
7747 curwin->w_localdir = NULL;
7748 }
7749
7750 shorten_fnames(TRUE);
7751
7752 /* Echo the new current directory if the command was typed. */
7753 if (KeyTyped)
7754 ex_pwd(eap);
7755 }
7756 vim_free(tofree);
7757 }
7758}
7759
7760/*
7761 * ":pwd".
7762 */
7763/*ARGSUSED*/
7764 static void
7765ex_pwd(eap)
7766 exarg_T *eap;
7767{
7768 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7769 {
7770#ifdef BACKSLASH_IN_FILENAME
7771 slash_adjust(NameBuff);
7772#endif
7773 msg(NameBuff);
7774 }
7775 else
7776 EMSG(_("E187: Unknown"));
7777}
7778
7779/*
7780 * ":=".
7781 */
7782 static void
7783ex_equal(eap)
7784 exarg_T *eap;
7785{
7786 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007787 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788}
7789
7790 static void
7791ex_sleep(eap)
7792 exarg_T *eap;
7793{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007794 int n;
7795 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796
7797 if (cursor_valid())
7798 {
7799 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7800 if (n >= 0)
7801 windgoto((int)n, curwin->w_wcol);
7802 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007803
7804 len = eap->line2;
7805 switch (*eap->arg)
7806 {
7807 case 'm': break;
7808 case NUL: len *= 1000L; break;
7809 default: EMSG2(_(e_invarg2), eap->arg); return;
7810 }
7811 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812}
7813
7814/*
7815 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7816 */
7817 void
7818do_sleep(msec)
7819 long msec;
7820{
7821 long done;
7822
7823 cursor_on();
7824 out_flush();
7825 for (done = 0; !got_int && done < msec; done += 1000L)
7826 {
7827 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7828 ui_breakcheck();
7829 }
7830}
7831
7832 static void
7833do_exmap(eap, isabbrev)
7834 exarg_T *eap;
7835 int isabbrev;
7836{
7837 int mode;
7838 char_u *cmdp;
7839
7840 cmdp = eap->cmd;
7841 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7842
7843 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7844 eap->arg, mode, isabbrev))
7845 {
7846 case 1: EMSG(_(e_invarg));
7847 break;
7848 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7849 break;
7850 }
7851}
7852
7853/*
7854 * ":winsize" command (obsolete).
7855 */
7856 static void
7857ex_winsize(eap)
7858 exarg_T *eap;
7859{
7860 int w, h;
7861 char_u *arg = eap->arg;
7862 char_u *p;
7863
7864 w = getdigits(&arg);
7865 arg = skipwhite(arg);
7866 p = arg;
7867 h = getdigits(&arg);
7868 if (*p != NUL && *arg == NUL)
7869 set_shellsize(w, h, TRUE);
7870 else
7871 EMSG(_("E465: :winsize requires two number arguments"));
7872}
7873
7874#ifdef FEAT_WINDOWS
7875 static void
7876ex_wincmd(eap)
7877 exarg_T *eap;
7878{
7879 int xchar = NUL;
7880 char_u *p;
7881
7882 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7883 {
7884 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7885 if (eap->arg[1] == NUL)
7886 {
7887 EMSG(_(e_invarg));
7888 return;
7889 }
7890 xchar = eap->arg[1];
7891 p = eap->arg + 2;
7892 }
7893 else
7894 p = eap->arg + 1;
7895
7896 eap->nextcmd = check_nextcmd(p);
7897 p = skipwhite(p);
7898 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7899 EMSG(_(e_invarg));
7900 else
7901 {
7902 /* Pass flags on for ":vertical wincmd ]". */
7903 postponed_split_flags = cmdmod.split;
7904 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7905 postponed_split_flags = 0;
7906 }
7907}
7908#endif
7909
Bram Moolenaar843ee412004-06-30 16:16:41 +00007910#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911/*
7912 * ":winpos".
7913 */
7914 static void
7915ex_winpos(eap)
7916 exarg_T *eap;
7917{
7918 int x, y;
7919 char_u *arg = eap->arg;
7920 char_u *p;
7921
7922 if (*arg == NUL)
7923 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007924# if defined(FEAT_GUI) || defined(MSWIN)
7925# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007927# else
7928 if (mch_get_winpos(&x, &y) != FAIL)
7929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {
7931 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7932 msg(IObuff);
7933 }
7934 else
7935# endif
7936 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7937 }
7938 else
7939 {
7940 x = getdigits(&arg);
7941 arg = skipwhite(arg);
7942 p = arg;
7943 y = getdigits(&arg);
7944 if (*p == NUL || *arg != NUL)
7945 {
7946 EMSG(_("E466: :winpos requires two number arguments"));
7947 return;
7948 }
7949# ifdef FEAT_GUI
7950 if (gui.in_use)
7951 gui_mch_set_winpos(x, y);
7952 else if (gui.starting)
7953 {
7954 /* Remember the coordinates for when the window is opened. */
7955 gui_win_x = x;
7956 gui_win_y = y;
7957 }
7958# ifdef HAVE_TGETENT
7959 else
7960# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007961# else
7962# ifdef MSWIN
7963 mch_set_winpos(x, y);
7964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965# endif
7966# ifdef HAVE_TGETENT
7967 if (*T_CWP)
7968 term_set_winpos(x, y);
7969# endif
7970 }
7971}
7972#endif
7973
7974/*
7975 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7976 */
7977 static void
7978ex_operators(eap)
7979 exarg_T *eap;
7980{
7981 oparg_T oa;
7982
7983 clear_oparg(&oa);
7984 oa.regname = eap->regname;
7985 oa.start.lnum = eap->line1;
7986 oa.end.lnum = eap->line2;
7987 oa.line_count = eap->line2 - eap->line1 + 1;
7988 oa.motion_type = MLINE;
7989#ifdef FEAT_VIRTUALEDIT
7990 virtual_op = FALSE;
7991#endif
7992 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7993 {
7994 setpcmark();
7995 curwin->w_cursor.lnum = eap->line1;
7996 beginline(BL_SOL | BL_FIX);
7997 }
7998
7999 switch (eap->cmdidx)
8000 {
8001 case CMD_delete:
8002 oa.op_type = OP_DELETE;
8003 op_delete(&oa);
8004 break;
8005
8006 case CMD_yank:
8007 oa.op_type = OP_YANK;
8008 (void)op_yank(&oa, FALSE, TRUE);
8009 break;
8010
8011 default: /* CMD_rshift or CMD_lshift */
8012 if ((eap->cmdidx == CMD_rshift)
8013#ifdef FEAT_RIGHTLEFT
8014 ^ curwin->w_p_rl
8015#endif
8016 )
8017 oa.op_type = OP_RSHIFT;
8018 else
8019 oa.op_type = OP_LSHIFT;
8020 op_shift(&oa, FALSE, eap->amount);
8021 break;
8022 }
8023#ifdef FEAT_VIRTUALEDIT
8024 virtual_op = MAYBE;
8025#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008026 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027}
8028
8029/*
8030 * ":put".
8031 */
8032 static void
8033ex_put(eap)
8034 exarg_T *eap;
8035{
8036 /* ":0put" works like ":1put!". */
8037 if (eap->line2 == 0)
8038 {
8039 eap->line2 = 1;
8040 eap->forceit = TRUE;
8041 }
8042 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008043 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8044 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045}
8046
8047/*
8048 * Handle ":copy" and ":move".
8049 */
8050 static void
8051ex_copymove(eap)
8052 exarg_T *eap;
8053{
8054 long n;
8055
8056 n = get_address(&eap->arg, FALSE, FALSE);
8057 if (eap->arg == NULL) /* error detected */
8058 {
8059 eap->nextcmd = NULL;
8060 return;
8061 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008062 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064 /*
8065 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8066 */
8067 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8068 {
8069 EMSG(_(e_invaddr));
8070 return;
8071 }
8072
8073 if (eap->cmdidx == CMD_move)
8074 {
8075 if (do_move(eap->line1, eap->line2, n) == FAIL)
8076 return;
8077 }
8078 else
8079 ex_copy(eap->line1, eap->line2, n);
8080 u_clearline();
8081 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008082 ex_may_print(eap);
8083}
8084
8085/*
8086 * Print the current line if flags were given to the Ex command.
8087 */
8088 static void
8089ex_may_print(eap)
8090 exarg_T *eap;
8091{
8092 if (eap->flags != 0)
8093 {
8094 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8095 (eap->flags & EXFLAG_LIST));
8096 ex_no_reprint = TRUE;
8097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098}
8099
8100/*
8101 * ":smagic" and ":snomagic".
8102 */
8103 static void
8104ex_submagic(eap)
8105 exarg_T *eap;
8106{
8107 int magic_save = p_magic;
8108
8109 p_magic = (eap->cmdidx == CMD_smagic);
8110 do_sub(eap);
8111 p_magic = magic_save;
8112}
8113
8114/*
8115 * ":join".
8116 */
8117 static void
8118ex_join(eap)
8119 exarg_T *eap;
8120{
8121 curwin->w_cursor.lnum = eap->line1;
8122 if (eap->line1 == eap->line2)
8123 {
8124 if (eap->addr_count >= 2) /* :2,2join does nothing */
8125 return;
8126 if (eap->line2 == curbuf->b_ml.ml_line_count)
8127 {
8128 beep_flush();
8129 return;
8130 }
8131 ++eap->line2;
8132 }
8133 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8134 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008135 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136}
8137
8138/*
8139 * ":[addr]@r" or ":[addr]*r": execute register
8140 */
8141 static void
8142ex_at(eap)
8143 exarg_T *eap;
8144{
8145 int c;
8146
8147 curwin->w_cursor.lnum = eap->line2;
8148
8149#ifdef USE_ON_FLY_SCROLL
8150 dont_scroll = TRUE; /* disallow scrolling here */
8151#endif
8152
8153 /* get the register name. No name means to use the previous one */
8154 c = *eap->arg;
8155 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8156 c = '@';
8157 /* put the register in mapbuf */
8158 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 else
8163 {
8164 int save_efr = exec_from_reg;
8165
8166 exec_from_reg = TRUE;
8167
8168 /*
8169 * Execute from the typeahead buffer.
8170 * Originally this didn't check for the typeahead buffer to be empty,
8171 * thus could read more Ex commands from stdin. It's not clear why,
8172 * it is certainly unexpected.
8173 */
8174 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8175 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8176
8177 exec_from_reg = save_efr;
8178 }
8179}
8180
8181/*
8182 * ":!".
8183 */
8184 static void
8185ex_bang(eap)
8186 exarg_T *eap;
8187{
8188 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8189}
8190
8191/*
8192 * ":undo".
8193 */
8194/*ARGSUSED*/
8195 static void
8196ex_undo(eap)
8197 exarg_T *eap;
8198{
8199 u_undo(1);
8200}
8201
8202/*
8203 * ":redo".
8204 */
8205/*ARGSUSED*/
8206 static void
8207ex_redo(eap)
8208 exarg_T *eap;
8209{
8210 u_redo(1);
8211}
8212
8213/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008214 * ":earlier" and ":later".
8215 */
8216/*ARGSUSED*/
8217 static void
8218ex_later(eap)
8219 exarg_T *eap;
8220{
8221 long count = 0;
8222 int sec = FALSE;
8223 char_u *p = eap->arg;
8224
8225 if (*p == NUL)
8226 count = 1;
8227 else if (isdigit(*p))
8228 {
8229 count = getdigits(&p);
8230 switch (*p)
8231 {
8232 case 's': ++p; sec = TRUE; break;
8233 case 'm': ++p; sec = TRUE; count *= 60; break;
8234 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
8235 }
8236 }
8237
8238 if (*p != NUL)
8239 EMSG2(_(e_invarg2), eap->arg);
8240 else
8241 undo_time(eap->cmdidx == CMD_earlier ? -count : count, sec);
8242}
8243
8244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 * ":redir": start/stop redirection.
8246 */
8247 static void
8248ex_redir(eap)
8249 exarg_T *eap;
8250{
8251 char *mode;
8252 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008253 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254
8255 if (STRICMP(eap->arg, "END") == 0)
8256 close_redir();
8257 else
8258 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008259 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008261 ++arg;
8262 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008264 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 mode = "a";
8266 }
8267 else
8268 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008269 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270
8271 close_redir();
8272
8273 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008274 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 if (fname == NULL)
8276 return;
8277#ifdef FEAT_BROWSE
8278 if (cmdmod.browse)
8279 {
8280 char_u *browseFile;
8281
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008282 browseFile = do_browse(BROWSE_SAVE,
8283 (char_u *)_("Save Redirection"),
8284 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 if (browseFile == NULL)
8286 return; /* operation cancelled */
8287 vim_free(fname);
8288 fname = browseFile;
8289 eap->forceit = TRUE; /* since dialog already asked */
8290 }
8291#endif
8292
8293 redir_fd = open_exfile(fname, eap->forceit, mode);
8294 vim_free(fname);
8295 }
8296#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008297 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {
8299 /* redirect to a register a-z (resp. A-Z for appending) */
8300 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008301 ++arg;
8302 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008304 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008306 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008308 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008309 if (*arg == '>' && arg[1] == '>')
8310 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008311 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8312 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008314 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008316 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008318 if (*arg == '>')
8319 arg++;
8320
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 /* make register empty */
8322 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8323 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008324 }
8325 if (*arg != NUL)
8326 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008327 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008328 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008329 }
8330 }
8331 else if (*arg == '=' && arg[1] == '>')
8332 {
8333 int append;
8334
8335 /* redirect to a variable */
8336 close_redir();
8337 arg += 2;
8338
8339 if (*arg == '>')
8340 {
8341 ++arg;
8342 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 }
8344 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008345 append = FALSE;
8346
8347 if (var_redir_start(skipwhite(arg), append) == OK)
8348 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 }
8350#endif
8351
8352 /* TODO: redirect to a buffer */
8353
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008355 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 }
8357}
8358
8359/*
8360 * ":redraw": force redraw
8361 */
8362 static void
8363ex_redraw(eap)
8364 exarg_T *eap;
8365{
8366 int r = RedrawingDisabled;
8367 int p = p_lz;
8368
8369 RedrawingDisabled = 0;
8370 p_lz = FALSE;
8371 update_topline();
8372 update_screen(eap->forceit ? CLEAR :
8373#ifdef FEAT_VISUAL
8374 VIsual_active ? INVERTED :
8375#endif
8376 0);
8377#ifdef FEAT_TITLE
8378 if (need_maketitle)
8379 maketitle();
8380#endif
8381 RedrawingDisabled = r;
8382 p_lz = p;
8383
8384 /* Reset msg_didout, so that a message that's there is overwritten. */
8385 msg_didout = FALSE;
8386 msg_col = 0;
8387
8388 out_flush();
8389}
8390
8391/*
8392 * ":redrawstatus": force redraw of status line(s)
8393 */
8394/*ARGSUSED*/
8395 static void
8396ex_redrawstatus(eap)
8397 exarg_T *eap;
8398{
8399#if defined(FEAT_WINDOWS)
8400 int r = RedrawingDisabled;
8401 int p = p_lz;
8402
8403 RedrawingDisabled = 0;
8404 p_lz = FALSE;
8405 if (eap->forceit)
8406 status_redraw_all();
8407 else
8408 status_redraw_curbuf();
8409 update_screen(
8410# ifdef FEAT_VISUAL
8411 VIsual_active ? INVERTED :
8412# endif
8413 0);
8414 RedrawingDisabled = r;
8415 p_lz = p;
8416 out_flush();
8417#endif
8418}
8419
8420 static void
8421close_redir()
8422{
8423 if (redir_fd != NULL)
8424 {
8425 fclose(redir_fd);
8426 redir_fd = NULL;
8427 }
8428#ifdef FEAT_EVAL
8429 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008430 if (redir_vname)
8431 {
8432 var_redir_stop();
8433 redir_vname = 0;
8434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435#endif
8436}
8437
8438#if defined(FEAT_SESSION) && defined(USE_CRNL)
8439# define MKSESSION_NL
8440static int mksession_nl = FALSE; /* use NL only in put_eol() */
8441#endif
8442
8443/*
8444 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8445 */
8446 static void
8447ex_mkrc(eap)
8448 exarg_T *eap;
8449{
8450 FILE *fd;
8451 int failed = FALSE;
8452 char_u *fname;
8453#ifdef FEAT_BROWSE
8454 char_u *browseFile = NULL;
8455#endif
8456#ifdef FEAT_SESSION
8457 int view_session = FALSE;
8458 int using_vdir = FALSE; /* using 'viewdir'? */
8459 char_u *viewFile = NULL;
8460 unsigned *flagp;
8461#endif
8462
8463 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8464 {
8465#ifdef FEAT_SESSION
8466 view_session = TRUE;
8467#else
8468 ex_ni(eap);
8469 return;
8470#endif
8471 }
8472
8473#ifdef FEAT_SESSION
8474 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8475 if (eap->cmdidx == CMD_mkview
8476 && (*eap->arg == NUL
8477 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8478 {
8479 eap->forceit = TRUE;
8480 fname = get_view_file(*eap->arg);
8481 if (fname == NULL)
8482 return;
8483 viewFile = fname;
8484 using_vdir = TRUE;
8485 }
8486 else
8487#endif
8488 if (*eap->arg != NUL)
8489 fname = eap->arg;
8490 else if (eap->cmdidx == CMD_mkvimrc)
8491 fname = (char_u *)VIMRC_FILE;
8492#ifdef FEAT_SESSION
8493 else if (eap->cmdidx == CMD_mksession)
8494 fname = (char_u *)SESSION_FILE;
8495#endif
8496 else
8497 fname = (char_u *)EXRC_FILE;
8498
8499#ifdef FEAT_BROWSE
8500 if (cmdmod.browse)
8501 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008502 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503# ifdef FEAT_SESSION
8504 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8505 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8506# endif
8507 (char_u *)_("Save Setup"),
8508 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8509 if (browseFile == NULL)
8510 goto theend;
8511 fname = browseFile;
8512 eap->forceit = TRUE; /* since dialog already asked */
8513 }
8514#endif
8515
8516#if defined(FEAT_SESSION) && defined(vim_mkdir)
8517 /* When using 'viewdir' may have to create the directory. */
8518 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008519 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520#endif
8521
8522 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8523 if (fd != NULL)
8524 {
8525#ifdef FEAT_SESSION
8526 if (eap->cmdidx == CMD_mkview)
8527 flagp = &vop_flags;
8528 else
8529 flagp = &ssop_flags;
8530#endif
8531
8532#ifdef MKSESSION_NL
8533 /* "unix" in 'sessionoptions': use NL line separator */
8534 if (view_session && (*flagp & SSOP_UNIX))
8535 mksession_nl = TRUE;
8536#endif
8537
8538 /* Write the version command for :mkvimrc */
8539 if (eap->cmdidx == CMD_mkvimrc)
8540 (void)put_line(fd, "version 6.0");
8541
8542#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008543 if (eap->cmdidx == CMD_mksession)
8544 {
8545 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8546 failed = TRUE;
8547 }
8548
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 if (eap->cmdidx != CMD_mkview)
8550#endif
8551 {
8552 /* Write setting 'compatible' first, because it has side effects.
8553 * For that same reason only do it when needed. */
8554 if (p_cp)
8555 (void)put_line(fd, "if !&cp | set cp | endif");
8556 else
8557 (void)put_line(fd, "if &cp | set nocp | endif");
8558 }
8559
8560#ifdef FEAT_SESSION
8561 if (!view_session
8562 || (eap->cmdidx == CMD_mksession
8563 && (*flagp & SSOP_OPTIONS)))
8564#endif
8565 failed |= (makemap(fd, NULL) == FAIL
8566 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8567
8568#ifdef FEAT_SESSION
8569 if (!failed && view_session)
8570 {
8571 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8572 failed = TRUE;
8573 if (eap->cmdidx == CMD_mksession)
8574 {
8575 char_u dirnow[MAXPATHL]; /* current directory */
8576
8577 /*
8578 * Change to session file's dir.
8579 */
8580 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8581 || mch_chdir((char *)dirnow) != 0)
8582 *dirnow = NUL;
8583 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8584 {
8585 if (vim_chdirfile(fname) == OK)
8586 shorten_fnames(TRUE);
8587 }
8588 else if (*dirnow != NUL
8589 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8590 {
8591 (void)mch_chdir((char *)globaldir);
8592 shorten_fnames(TRUE);
8593 }
8594
8595 failed |= (makeopens(fd, dirnow) == FAIL);
8596
8597 /* restore original dir */
8598 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8599 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8600 {
8601 if (mch_chdir((char *)dirnow) != 0)
8602 EMSG(_(e_prev_dir));
8603 shorten_fnames(TRUE);
8604 }
8605 }
8606 else
8607 {
8608 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8609 }
8610 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8611 == FAIL)
8612 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008613 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8614 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008615 if (eap->cmdidx == CMD_mksession)
8616 {
8617 if (put_line(fd, "unlet SessionLoad") == FAIL)
8618 failed = TRUE;
8619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 }
8621#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008622 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8623 failed = TRUE;
8624
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 failed |= fclose(fd);
8626
8627 if (failed)
8628 EMSG(_(e_write));
8629#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8630 else if (eap->cmdidx == CMD_mksession)
8631 {
8632 /* successful session write - set this_session var */
8633 char_u tbuf[MAXPATHL];
8634
8635 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8636 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8637 }
8638#endif
8639#ifdef MKSESSION_NL
8640 mksession_nl = FALSE;
8641#endif
8642 }
8643
8644#ifdef FEAT_BROWSE
8645theend:
8646 vim_free(browseFile);
8647#endif
8648#ifdef FEAT_SESSION
8649 vim_free(viewFile);
8650#endif
8651}
8652
Bram Moolenaardf177f62005-02-22 08:39:57 +00008653#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8654 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008655/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008656 int
8657vim_mkdir_emsg(name, prot)
8658 char_u *name;
8659 int prot;
8660{
8661 if (vim_mkdir(name, prot) != 0)
8662 {
8663 EMSG2(_("E739: Cannot create directory: %s"), name);
8664 return FAIL;
8665 }
8666 return OK;
8667}
8668#endif
8669
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670/*
8671 * Open a file for writing for an Ex command, with some checks.
8672 * Return file descriptor, or NULL on failure.
8673 */
8674 FILE *
8675open_exfile(fname, forceit, mode)
8676 char_u *fname;
8677 int forceit;
8678 char *mode; /* "w" for create new file or "a" for append */
8679{
8680 FILE *fd;
8681
8682#ifdef UNIX
8683 /* with Unix it is possible to open a directory */
8684 if (mch_isdir(fname))
8685 {
8686 EMSG2(_(e_isadir2), fname);
8687 return NULL;
8688 }
8689#endif
8690 if (!forceit && *mode != 'a' && vim_fexists(fname))
8691 {
8692 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8693 return NULL;
8694 }
8695
8696 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8697 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8698
8699 return fd;
8700}
8701
8702/*
8703 * ":mark" and ":k".
8704 */
8705 static void
8706ex_mark(eap)
8707 exarg_T *eap;
8708{
8709 pos_T pos;
8710
8711 if (*eap->arg == NUL) /* No argument? */
8712 EMSG(_(e_argreq));
8713 else if (eap->arg[1] != NUL) /* more than one character? */
8714 EMSG(_(e_trailing));
8715 else
8716 {
8717 pos = curwin->w_cursor; /* save curwin->w_cursor */
8718 curwin->w_cursor.lnum = eap->line2;
8719 beginline(BL_WHITE | BL_FIX);
8720 if (setmark(*eap->arg) == FAIL) /* set mark */
8721 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8722 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8723 }
8724}
8725
8726/*
8727 * Update w_topline, w_leftcol and the cursor position.
8728 */
8729 void
8730update_topline_cursor()
8731{
8732 check_cursor(); /* put cursor on valid line */
8733 update_topline();
8734 if (!curwin->w_p_wrap)
8735 validate_cursor();
8736 update_curswant();
8737}
8738
8739#ifdef FEAT_EX_EXTRA
8740/*
8741 * ":normal[!] {commands}": Execute normal mode commands.
8742 */
8743 static void
8744ex_normal(eap)
8745 exarg_T *eap;
8746{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 int save_msg_scroll = msg_scroll;
8748 int save_restart_edit = restart_edit;
8749 int save_msg_didout = msg_didout;
8750 int save_State = State;
8751 tasave_T tabuf;
8752 int save_insertmode = p_im;
8753 int save_finish_op = finish_op;
8754#ifdef FEAT_MBYTE
8755 char_u *arg = NULL;
8756 int l;
8757 char_u *p;
8758#endif
8759
8760 if (ex_normal_busy >= p_mmd)
8761 {
8762 EMSG(_("E192: Recursive use of :normal too deep"));
8763 return;
8764 }
8765 ++ex_normal_busy;
8766
8767 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8768 restart_edit = 0; /* don't go to Insert mode */
8769 p_im = FALSE; /* don't use 'insertmode' */
8770
8771#ifdef FEAT_MBYTE
8772 /*
8773 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8774 * this for the K_SPECIAL leading byte, otherwise special keys will not
8775 * work.
8776 */
8777 if (has_mbyte)
8778 {
8779 int len = 0;
8780
8781 /* Count the number of characters to be escaped. */
8782 for (p = eap->arg; *p != NUL; ++p)
8783 {
8784# ifdef FEAT_GUI
8785 if (*p == CSI) /* leadbyte CSI */
8786 len += 2;
8787# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008788 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8790# ifdef FEAT_GUI
8791 || *p == CSI
8792# endif
8793 )
8794 len += 2;
8795 }
8796 if (len > 0)
8797 {
8798 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8799 if (arg != NULL)
8800 {
8801 len = 0;
8802 for (p = eap->arg; *p != NUL; ++p)
8803 {
8804 arg[len++] = *p;
8805# ifdef FEAT_GUI
8806 if (*p == CSI)
8807 {
8808 arg[len++] = KS_EXTRA;
8809 arg[len++] = (int)KE_CSI;
8810 }
8811# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008812 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 {
8814 arg[len++] = *++p;
8815 if (*p == K_SPECIAL)
8816 {
8817 arg[len++] = KS_SPECIAL;
8818 arg[len++] = KE_FILLER;
8819 }
8820# ifdef FEAT_GUI
8821 else if (*p == CSI)
8822 {
8823 arg[len++] = KS_EXTRA;
8824 arg[len++] = (int)KE_CSI;
8825 }
8826# endif
8827 }
8828 arg[len] = NUL;
8829 }
8830 }
8831 }
8832 }
8833#endif
8834
8835 /*
8836 * Save the current typeahead. This is required to allow using ":normal"
8837 * from an event handler and makes sure we don't hang when the argument
8838 * ends with half a command.
8839 */
8840 save_typeahead(&tabuf);
8841 if (tabuf.typebuf_valid)
8842 {
8843 /*
8844 * Repeat the :normal command for each line in the range. When no
8845 * range given, execute it just once, without positioning the cursor
8846 * first.
8847 */
8848 do
8849 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 if (eap->addr_count != 0)
8851 {
8852 curwin->w_cursor.lnum = eap->line1++;
8853 curwin->w_cursor.col = 0;
8854 }
8855
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008856 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857#ifdef FEAT_MBYTE
8858 arg != NULL ? arg :
8859#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008860 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 }
8862 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8863 }
8864
8865 /* Might not return to the main loop when in an event handler. */
8866 update_topline_cursor();
8867
8868 /* Restore the previous typeahead. */
8869 restore_typeahead(&tabuf);
8870
8871 --ex_normal_busy;
8872 msg_scroll = save_msg_scroll;
8873 restart_edit = save_restart_edit;
8874 p_im = save_insertmode;
8875 finish_op = save_finish_op;
8876 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8877
8878 /* Restore the state (needed when called from a function executed for
8879 * 'indentexpr'). */
8880 State = save_State;
8881#ifdef FEAT_MBYTE
8882 vim_free(arg);
8883#endif
8884}
8885
8886/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008887 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 */
8889 static void
8890ex_startinsert(eap)
8891 exarg_T *eap;
8892{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008893 if (eap->forceit)
8894 {
8895 coladvance((colnr_T)MAXCOL);
8896 curwin->w_curswant = MAXCOL;
8897 curwin->w_set_curswant = FALSE;
8898 }
8899
Bram Moolenaara40c5002005-01-09 21:16:21 +00008900 /* Ignore the command when already in Insert mode. Inserting an
8901 * expression register that invokes a function can do this. */
8902 if (State & INSERT)
8903 return;
8904
Bram Moolenaar64969662005-12-14 21:59:55 +00008905 if (eap->cmdidx == CMD_startinsert)
8906 restart_edit = 'a';
8907 else if (eap->cmdidx == CMD_startreplace)
8908 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008910 restart_edit = 'V';
8911
8912 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008914 if (eap->cmdidx == CMD_startinsert)
8915 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 curwin->w_curswant = 0; /* avoid MAXCOL */
8917 }
8918}
8919
8920/*
8921 * ":stopinsert"
8922 */
8923/*ARGSUSED*/
8924 static void
8925ex_stopinsert(eap)
8926 exarg_T *eap;
8927{
8928 restart_edit = 0;
8929 stop_insert_mode = TRUE;
8930}
8931#endif
8932
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008933#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8934/*
8935 * Execute normal mode command "cmd".
8936 * "remap" can be REMAP_NONE or REMAP_YES.
8937 */
8938 void
8939exec_normal_cmd(cmd, remap, silent)
8940 char_u *cmd;
8941 int remap;
8942 int silent;
8943{
8944 oparg_T oa;
8945
8946 /*
8947 * Stuff the argument into the typeahead buffer.
8948 * Execute normal_cmd() until there is no typeahead left.
8949 */
8950 clear_oparg(&oa);
8951 finish_op = FALSE;
8952 ins_typebuf(cmd, remap, 0, TRUE, silent);
8953 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8954 && !got_int)
8955 {
8956 update_topline_cursor();
8957 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8958 }
8959}
8960#endif
8961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962#ifdef FEAT_FIND_ID
8963 static void
8964ex_checkpath(eap)
8965 exarg_T *eap;
8966{
8967 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8968 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8969 (linenr_T)1, (linenr_T)MAXLNUM);
8970}
8971
8972#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8973/*
8974 * ":psearch"
8975 */
8976 static void
8977ex_psearch(eap)
8978 exarg_T *eap;
8979{
8980 g_do_tagpreview = p_pvh;
8981 ex_findpat(eap);
8982 g_do_tagpreview = 0;
8983}
8984#endif
8985
8986 static void
8987ex_findpat(eap)
8988 exarg_T *eap;
8989{
8990 int whole = TRUE;
8991 long n;
8992 char_u *p;
8993 int action;
8994
8995 switch (cmdnames[eap->cmdidx].cmd_name[2])
8996 {
8997 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8998 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8999 action = ACTION_GOTO;
9000 else
9001 action = ACTION_SHOW;
9002 break;
9003 case 'i': /* ":ilist" and ":dlist" */
9004 action = ACTION_SHOW_ALL;
9005 break;
9006 case 'u': /* ":ijump" and ":djump" */
9007 action = ACTION_GOTO;
9008 break;
9009 default: /* ":isplit" and ":dsplit" */
9010 action = ACTION_SPLIT;
9011 break;
9012 }
9013
9014 n = 1;
9015 if (vim_isdigit(*eap->arg)) /* get count */
9016 {
9017 n = getdigits(&eap->arg);
9018 eap->arg = skipwhite(eap->arg);
9019 }
9020 if (*eap->arg == '/') /* Match regexp, not just whole words */
9021 {
9022 whole = FALSE;
9023 ++eap->arg;
9024 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9025 if (*p)
9026 {
9027 *p++ = NUL;
9028 p = skipwhite(p);
9029
9030 /* Check for trailing illegal characters */
9031 if (!ends_excmd(*p))
9032 eap->errmsg = e_trailing;
9033 else
9034 eap->nextcmd = check_nextcmd(p);
9035 }
9036 }
9037 if (!eap->skip)
9038 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9039 whole, !eap->forceit,
9040 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9041 n, action, eap->line1, eap->line2);
9042}
9043#endif
9044
9045#ifdef FEAT_WINDOWS
9046
9047# ifdef FEAT_QUICKFIX
9048/*
9049 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9050 */
9051 static void
9052ex_ptag(eap)
9053 exarg_T *eap;
9054{
9055 g_do_tagpreview = p_pvh;
9056 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9057}
9058
9059/*
9060 * ":pedit"
9061 */
9062 static void
9063ex_pedit(eap)
9064 exarg_T *eap;
9065{
9066 win_T *curwin_save = curwin;
9067
9068 g_do_tagpreview = p_pvh;
9069 prepare_tagpreview();
9070 keep_help_flag = curwin_save->w_buffer->b_help;
9071 do_exedit(eap, NULL);
9072 keep_help_flag = FALSE;
9073 if (curwin != curwin_save && win_valid(curwin_save))
9074 {
9075 /* Return cursor to where we were */
9076 validate_cursor();
9077 redraw_later(VALID);
9078 win_enter(curwin_save, TRUE);
9079 }
9080 g_do_tagpreview = 0;
9081}
9082# endif
9083
9084/*
9085 * ":stag", ":stselect" and ":stjump".
9086 */
9087 static void
9088ex_stag(eap)
9089 exarg_T *eap;
9090{
9091 postponed_split = -1;
9092 postponed_split_flags = cmdmod.split;
9093 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9094 postponed_split_flags = 0;
9095}
9096#endif
9097
9098/*
9099 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9100 */
9101 static void
9102ex_tag(eap)
9103 exarg_T *eap;
9104{
9105 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9106}
9107
9108 static void
9109ex_tag_cmd(eap, name)
9110 exarg_T *eap;
9111 char_u *name;
9112{
9113 int cmd;
9114
9115 switch (name[1])
9116 {
9117 case 'j': cmd = DT_JUMP; /* ":tjump" */
9118 break;
9119 case 's': cmd = DT_SELECT; /* ":tselect" */
9120 break;
9121 case 'p': cmd = DT_PREV; /* ":tprevious" */
9122 break;
9123 case 'N': cmd = DT_PREV; /* ":tNext" */
9124 break;
9125 case 'n': cmd = DT_NEXT; /* ":tnext" */
9126 break;
9127 case 'o': cmd = DT_POP; /* ":pop" */
9128 break;
9129 case 'f': /* ":tfirst" */
9130 case 'r': cmd = DT_FIRST; /* ":trewind" */
9131 break;
9132 case 'l': cmd = DT_LAST; /* ":tlast" */
9133 break;
9134 default: /* ":tag" */
9135#ifdef FEAT_CSCOPE
9136 if (p_cst)
9137 {
9138 do_cstag(eap);
9139 return;
9140 }
9141#endif
9142 cmd = DT_TAG;
9143 break;
9144 }
9145
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009146 if (name[0] == 'l')
9147 {
9148#ifndef FEAT_QUICKFIX
9149 ex_ni(eap);
9150 return;
9151#else
9152 cmd = DT_LTAG;
9153#endif
9154 }
9155
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9157 eap->forceit, TRUE);
9158}
9159
9160/*
9161 * Evaluate cmdline variables.
9162 *
9163 * change '%' to curbuf->b_ffname
9164 * '#' to curwin->w_altfile
9165 * '<cword>' to word under the cursor
9166 * '<cWORD>' to WORD under the cursor
9167 * '<cfile>' to path name under the cursor
9168 * '<sfile>' to sourced file name
9169 * '<afile>' to file name for autocommand
9170 * '<abuf>' to buffer number for autocommand
9171 * '<amatch>' to matching name for autocommand
9172 *
9173 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9174 * "" for error without a message) and NULL is returned.
9175 * Returns an allocated string if a valid match was found.
9176 * Returns NULL if no match was found. "usedlen" then still contains the
9177 * number of characters to skip.
9178 */
9179 char_u *
9180eval_vars(src, usedlen, lnump, errormsg, srcstart)
9181 char_u *src; /* pointer into commandline */
9182 int *usedlen; /* characters after src that are used */
9183 linenr_T *lnump; /* line number for :e command, or NULL */
9184 char_u **errormsg; /* pointer to error message */
9185 char_u *srcstart; /* beginning of valid memory for src */
9186{
9187 int i;
9188 char_u *s;
9189 char_u *result;
9190 char_u *resultbuf = NULL;
9191 int resultlen;
9192 buf_T *buf;
9193 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9194 int spec_idx;
9195#ifdef FEAT_MODIFY_FNAME
9196 int skip_mod = FALSE;
9197#endif
9198 static char *(spec_str[]) =
9199 {
9200 "%",
9201#define SPEC_PERC 0
9202 "#",
9203#define SPEC_HASH 1
9204 "<cword>", /* cursor word */
9205#define SPEC_CWORD 2
9206 "<cWORD>", /* cursor WORD */
9207#define SPEC_CCWORD 3
9208 "<cfile>", /* cursor path name */
9209#define SPEC_CFILE 4
9210 "<sfile>", /* ":so" file name */
9211#define SPEC_SFILE 5
9212#ifdef FEAT_AUTOCMD
9213 "<afile>", /* autocommand file name */
9214# define SPEC_AFILE 6
9215 "<abuf>", /* autocommand buffer number */
9216# define SPEC_ABUF 7
9217 "<amatch>", /* autocommand match name */
9218# define SPEC_AMATCH 8
9219#endif
9220#ifdef FEAT_CLIENTSERVER
9221 "<client>"
9222# define SPEC_CLIENT 9
9223#endif
9224 };
9225#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9226
9227#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9228 char_u strbuf[30];
9229#endif
9230
9231 *errormsg = NULL;
9232
9233 /*
9234 * Check if there is something to do.
9235 */
9236 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9237 {
9238 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9239 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9240 break;
9241 }
9242 if (spec_idx == SPEC_COUNT) /* no match */
9243 {
9244 *usedlen = 1;
9245 return NULL;
9246 }
9247
9248 /*
9249 * Skip when preceded with a backslash "\%" and "\#".
9250 * Note: In "\\%" the % is also not recognized!
9251 */
9252 if (src > srcstart && src[-1] == '\\')
9253 {
9254 *usedlen = 0;
9255 STRCPY(src - 1, src); /* remove backslash */
9256 return NULL;
9257 }
9258
9259 /*
9260 * word or WORD under cursor
9261 */
9262 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9263 {
9264 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9265 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9266 if (resultlen == 0)
9267 {
9268 *errormsg = (char_u *)"";
9269 return NULL;
9270 }
9271 }
9272
9273 /*
9274 * '#': Alternate file name
9275 * '%': Current file name
9276 * File name under the cursor
9277 * File name for autocommand
9278 * and following modifiers
9279 */
9280 else
9281 {
9282 switch (spec_idx)
9283 {
9284 case SPEC_PERC: /* '%': current file */
9285 if (curbuf->b_fname == NULL)
9286 {
9287 result = (char_u *)"";
9288 valid = 0; /* Must have ":p:h" to be valid */
9289 }
9290 else
9291#ifdef RISCOS
9292 /* Always use the full path for RISC OS if possible. */
9293 result = curbuf->b_ffname;
9294 if (result == NULL)
9295 result = curbuf->b_fname;
9296#else
9297 result = curbuf->b_fname;
9298#endif
9299 break;
9300
9301 case SPEC_HASH: /* '#' or "#99": alternate file */
9302 if (src[1] == '#') /* "##": the argument list */
9303 {
9304 result = arg_all();
9305 resultbuf = result;
9306 *usedlen = 2;
9307#ifdef FEAT_MODIFY_FNAME
9308 skip_mod = TRUE;
9309#endif
9310 break;
9311 }
9312 s = src + 1;
9313 i = (int)getdigits(&s);
9314 *usedlen = (int)(s - src); /* length of what we expand */
9315
9316 buf = buflist_findnr(i);
9317 if (buf == NULL)
9318 {
9319 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9320 return NULL;
9321 }
9322 if (lnump != NULL)
9323 *lnump = ECMD_LAST;
9324 if (buf->b_fname == NULL)
9325 {
9326 result = (char_u *)"";
9327 valid = 0; /* Must have ":p:h" to be valid */
9328 }
9329 else
9330 result = buf->b_fname;
9331 break;
9332
9333#ifdef FEAT_SEARCHPATH
9334 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009335 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 if (result == NULL)
9337 {
9338 *errormsg = (char_u *)"";
9339 return NULL;
9340 }
9341 resultbuf = result; /* remember allocated string */
9342 break;
9343#endif
9344
9345#ifdef FEAT_AUTOCMD
9346 case SPEC_AFILE: /* file name for autocommand */
9347 result = autocmd_fname;
9348 if (result == NULL)
9349 {
9350 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9351 return NULL;
9352 }
9353 break;
9354
9355 case SPEC_ABUF: /* buffer number for autocommand */
9356 if (autocmd_bufnr <= 0)
9357 {
9358 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9359 return NULL;
9360 }
9361 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9362 result = strbuf;
9363 break;
9364
9365 case SPEC_AMATCH: /* match name for autocommand */
9366 result = autocmd_match;
9367 if (result == NULL)
9368 {
9369 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9370 return NULL;
9371 }
9372 break;
9373
9374#endif
9375 case SPEC_SFILE: /* file name for ":so" command */
9376 result = sourcing_name;
9377 if (result == NULL)
9378 {
9379 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9380 return NULL;
9381 }
9382 break;
9383#if defined(FEAT_CLIENTSERVER)
9384 case SPEC_CLIENT: /* Source of last submitted input */
9385 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9386 result = strbuf;
9387 break;
9388#endif
9389 }
9390
9391 resultlen = (int)STRLEN(result); /* length of new string */
9392 if (src[*usedlen] == '<') /* remove the file name extension */
9393 {
9394 ++*usedlen;
9395#ifdef RISCOS
9396 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9397#else
9398 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9399#endif
9400 resultlen = (int)(s - result);
9401 }
9402#ifdef FEAT_MODIFY_FNAME
9403 else if (!skip_mod)
9404 {
9405 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9406 &resultlen);
9407 if (result == NULL)
9408 {
9409 *errormsg = (char_u *)"";
9410 return NULL;
9411 }
9412 }
9413#endif
9414 }
9415
9416 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9417 {
9418 if (valid != VALID_HEAD + VALID_PATH)
9419 /* xgettext:no-c-format */
9420 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9421 else
9422 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9423 result = NULL;
9424 }
9425 else
9426 result = vim_strnsave(result, resultlen);
9427 vim_free(resultbuf);
9428 return result;
9429}
9430
9431/*
9432 * Concatenate all files in the argument list, separated by spaces, and return
9433 * it in one allocated string.
9434 * Spaces and backslashes in the file names are escaped with a backslash.
9435 * Returns NULL when out of memory.
9436 */
9437 static char_u *
9438arg_all()
9439{
9440 int len;
9441 int idx;
9442 char_u *retval = NULL;
9443 char_u *p;
9444
9445 /*
9446 * Do this loop two times:
9447 * first time: compute the total length
9448 * second time: concatenate the names
9449 */
9450 for (;;)
9451 {
9452 len = 0;
9453 for (idx = 0; idx < ARGCOUNT; ++idx)
9454 {
9455 p = alist_name(&ARGLIST[idx]);
9456 if (p != NULL)
9457 {
9458 if (len > 0)
9459 {
9460 /* insert a space in between names */
9461 if (retval != NULL)
9462 retval[len] = ' ';
9463 ++len;
9464 }
9465 for ( ; *p != NUL; ++p)
9466 {
9467 if (*p == ' ' || *p == '\\')
9468 {
9469 /* insert a backslash */
9470 if (retval != NULL)
9471 retval[len] = '\\';
9472 ++len;
9473 }
9474 if (retval != NULL)
9475 retval[len] = *p;
9476 ++len;
9477 }
9478 }
9479 }
9480
9481 /* second time: break here */
9482 if (retval != NULL)
9483 {
9484 retval[len] = NUL;
9485 break;
9486 }
9487
9488 /* allocate memory */
9489 retval = alloc(len + 1);
9490 if (retval == NULL)
9491 break;
9492 }
9493
9494 return retval;
9495}
9496
9497#if defined(FEAT_AUTOCMD) || defined(PROTO)
9498/*
9499 * Expand the <sfile> string in "arg".
9500 *
9501 * Returns an allocated string, or NULL for any error.
9502 */
9503 char_u *
9504expand_sfile(arg)
9505 char_u *arg;
9506{
9507 char_u *errormsg;
9508 int len;
9509 char_u *result;
9510 char_u *newres;
9511 char_u *repl;
9512 int srclen;
9513 char_u *p;
9514
9515 result = vim_strsave(arg);
9516 if (result == NULL)
9517 return NULL;
9518
9519 for (p = result; *p; )
9520 {
9521 if (STRNCMP(p, "<sfile>", 7) != 0)
9522 ++p;
9523 else
9524 {
9525 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9526 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9527 if (errormsg != NULL)
9528 {
9529 if (*errormsg)
9530 emsg(errormsg);
9531 vim_free(result);
9532 return NULL;
9533 }
9534 if (repl == NULL) /* no match (cannot happen) */
9535 {
9536 p += srclen;
9537 continue;
9538 }
9539 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9540 newres = alloc(len);
9541 if (newres == NULL)
9542 {
9543 vim_free(repl);
9544 vim_free(result);
9545 return NULL;
9546 }
9547 mch_memmove(newres, result, (size_t)(p - result));
9548 STRCPY(newres + (p - result), repl);
9549 len = (int)STRLEN(newres);
9550 STRCAT(newres, p + srclen);
9551 vim_free(repl);
9552 vim_free(result);
9553 result = newres;
9554 p = newres + len; /* continue after the match */
9555 }
9556 }
9557
9558 return result;
9559}
9560#endif
9561
9562#ifdef FEAT_SESSION
9563static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9564static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9565static frame_T *ses_skipframe __ARGS((frame_T *fr));
9566static int ses_do_frame __ARGS((frame_T *fr));
9567static int ses_do_win __ARGS((win_T *wp));
9568static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9569static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9570static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9571
9572/*
9573 * Write openfile commands for the current buffers to an .exrc file.
9574 * Return FAIL on error, OK otherwise.
9575 */
9576 static int
9577makeopens(fd, dirnow)
9578 FILE *fd;
9579 char_u *dirnow; /* Current directory name */
9580{
9581 buf_T *buf;
9582 int only_save_windows = TRUE;
9583 int nr;
9584 int cnr = 1;
9585 int restore_size = TRUE;
9586 win_T *wp;
9587 char_u *sname;
9588 win_T *edited_win = NULL;
9589
9590 if (ssop_flags & SSOP_BUFFERS)
9591 only_save_windows = FALSE; /* Save ALL buffers */
9592
9593 /*
9594 * Begin by setting the this_session variable, and then other
9595 * sessionable variables.
9596 */
9597#ifdef FEAT_EVAL
9598 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9599 return FAIL;
9600 if (ssop_flags & SSOP_GLOBALS)
9601 if (store_session_globals(fd) == FAIL)
9602 return FAIL;
9603#endif
9604
9605 /*
9606 * Close all windows but one.
9607 */
9608 if (put_line(fd, "silent only") == FAIL)
9609 return FAIL;
9610
9611 /*
9612 * Now a :cd command to the session directory or the current directory
9613 */
9614 if (ssop_flags & SSOP_SESDIR)
9615 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009616 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9617 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 return FAIL;
9619 }
9620 else if (ssop_flags & SSOP_CURDIR)
9621 {
9622 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9623 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009624 || fputs("cd ", fd) < 0
9625 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9626 || put_eol(fd) == FAIL)
9627 {
9628 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 vim_free(sname);
9632 }
9633
9634 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009635 * If there is an empty, unnamed buffer we will wipe it out later.
9636 * Remember the buffer number.
9637 */
9638 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9639 return FAIL;
9640 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9641 return FAIL;
9642 if (put_line(fd, "endif") == FAIL)
9643 return FAIL;
9644
9645 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 * Now save the current files, current buffer first.
9647 */
9648 if (put_line(fd, "set shortmess=aoO") == FAIL)
9649 return FAIL;
9650
9651 /* Now put the other buffers into the buffer list */
9652 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9653 {
9654 if (!(only_save_windows && buf->b_nwindows == 0)
9655 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9656 && buf->b_fname != NULL
9657 && buf->b_p_bl)
9658 {
9659 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9660 : buf->b_wininfo->wi_fpos.lnum) < 0
9661 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9662 return FAIL;
9663 }
9664 }
9665
9666 /* the global argument list */
9667 if (ses_arglist(fd, "args", &global_alist.al_ga,
9668 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9669 return FAIL;
9670
9671 if (ssop_flags & SSOP_RESIZE)
9672 {
9673 /* Note: after the restore we still check it worked!*/
9674 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9675 || put_eol(fd) == FAIL)
9676 return FAIL;
9677 }
9678
9679#ifdef FEAT_GUI
9680 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9681 {
9682 int x, y;
9683
9684 if (gui_mch_get_winpos(&x, &y) == OK)
9685 {
9686 /* Note: after the restore we still check it worked!*/
9687 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9688 return FAIL;
9689 }
9690 }
9691#endif
9692
9693 /*
9694 * Before creating the window layout, try loading one file. If this is
9695 * aborted we don't end up with a number of useless windows.
9696 * This may have side effects! (e.g., compressed or network file).
9697 */
9698 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9699 {
9700 if (ses_do_win(wp)
9701 && wp->w_buffer->b_ffname != NULL
9702 && !wp->w_buffer->b_help
9703#ifdef FEAT_QUICKFIX
9704 && !bt_nofile(wp->w_buffer)
9705#endif
9706 )
9707 {
9708 if (fputs("edit ", fd) < 0
9709 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9710 return FAIL;
9711 if (!wp->w_arg_idx_invalid)
9712 edited_win = wp;
9713 break;
9714 }
9715 }
9716
9717 /*
9718 * Save current window layout.
9719 */
9720 if (put_line(fd, "set splitbelow splitright") == FAIL)
9721 return FAIL;
9722 if (ses_win_rec(fd, topframe) == FAIL)
9723 return FAIL;
9724 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9725 return FAIL;
9726 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9727 return FAIL;
9728
9729 /*
9730 * Check if window sizes can be restored (no windows omitted).
9731 * Remember the window number of the current window after restoring.
9732 */
9733 nr = 0;
9734 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9735 {
9736 if (ses_do_win(wp))
9737 ++nr;
9738 else
9739 restore_size = FALSE;
9740 if (curwin == wp)
9741 cnr = nr;
9742 }
9743
9744 /* Go to the first window. */
9745 if (put_line(fd, "wincmd t") == FAIL)
9746 return FAIL;
9747
9748 /*
9749 * If more than one window, see if sizes can be restored.
9750 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9751 * resized when moving between windows.
9752 * Do this before restoring the view, so that the topline and the cursor
9753 * can be set. This is done again below.
9754 */
9755 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9756 return FAIL;
9757 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9758 return FAIL;
9759
9760 /*
9761 * Restore the view of the window (options, file, cursor, etc.).
9762 */
9763 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9764 {
9765 if (!ses_do_win(wp))
9766 continue;
9767 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9768 return FAIL;
9769 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9770 return FAIL;
9771 }
9772
9773 /*
9774 * Restore cursor to the current window if it's not the first one.
9775 */
9776 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9777 return FAIL;
9778
9779 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009780 * Wipe out an empty unnamed buffer we started in.
9781 */
9782 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9783 return FAIL;
9784 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9785 return FAIL;
9786 if (put_line(fd, "endif") == FAIL)
9787 return FAIL;
9788 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9789 return FAIL;
9790
9791 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 * Restore window sizes again after jumping around in windows, because the
9793 * current window has a minimum size while others may not.
9794 */
9795 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9796 return FAIL;
9797
9798 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9799 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9800 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9801 return FAIL;
9802
9803 /*
9804 * Lastly, execute the x.vim file if it exists.
9805 */
9806 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9807 || put_line(fd, "if file_readable(s:sx)") == FAIL
9808 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9809 || put_line(fd, "endif") == FAIL)
9810 return FAIL;
9811
9812 return OK;
9813}
9814
9815 static int
9816ses_winsizes(fd, restore_size)
9817 FILE *fd;
9818 int restore_size;
9819{
9820 int n = 0;
9821 win_T *wp;
9822
9823 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9824 {
9825 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9826 {
9827 if (!ses_do_win(wp))
9828 continue;
9829 ++n;
9830
9831 /* restore height when not full height */
9832 if (wp->w_height + wp->w_status_height < topframe->fr_height
9833 && (fprintf(fd,
9834 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9835 n, (long)wp->w_height, Rows / 2, Rows) < 0
9836 || put_eol(fd) == FAIL))
9837 return FAIL;
9838
9839 /* restore width when not full width */
9840 if (wp->w_width < Columns && (fprintf(fd,
9841 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9842 n, (long)wp->w_width, Columns / 2, Columns) < 0
9843 || put_eol(fd) == FAIL))
9844 return FAIL;
9845 }
9846 }
9847 else
9848 {
9849 /* Just equalise window sizes */
9850 if (put_line(fd, "wincmd =") == FAIL)
9851 return FAIL;
9852 }
9853 return OK;
9854}
9855
9856/*
9857 * Write commands to "fd" to recursively create windows for frame "fr",
9858 * horizontally and vertically split.
9859 * After the commands the last window in the frame is the current window.
9860 * Returns FAIL when writing the commands to "fd" fails.
9861 */
9862 static int
9863ses_win_rec(fd, fr)
9864 FILE *fd;
9865 frame_T *fr;
9866{
9867 frame_T *frc;
9868 int count = 0;
9869
9870 if (fr->fr_layout != FR_LEAF)
9871 {
9872 /* Find first frame that's not skipped and then create a window for
9873 * each following one (first frame is already there). */
9874 frc = ses_skipframe(fr->fr_child);
9875 if (frc != NULL)
9876 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9877 {
9878 /* Make window as big as possible so that we have lots of room
9879 * to split. */
9880 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9881 || put_line(fd, fr->fr_layout == FR_COL
9882 ? "split" : "vsplit") == FAIL)
9883 return FAIL;
9884 ++count;
9885 }
9886
9887 /* Go back to the first window. */
9888 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9889 ? "%dwincmd k" : "%dwincmd h", count) < 0
9890 || put_eol(fd) == FAIL))
9891 return FAIL;
9892
9893 /* Recursively create frames/windows in each window of this column or
9894 * row. */
9895 frc = ses_skipframe(fr->fr_child);
9896 while (frc != NULL)
9897 {
9898 ses_win_rec(fd, frc);
9899 frc = ses_skipframe(frc->fr_next);
9900 /* Go to next window. */
9901 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9902 return FAIL;
9903 }
9904 }
9905 return OK;
9906}
9907
9908/*
9909 * Skip frames that don't contain windows we want to save in the Session.
9910 * Returns NULL when there none.
9911 */
9912 static frame_T *
9913ses_skipframe(fr)
9914 frame_T *fr;
9915{
9916 frame_T *frc;
9917
9918 for (frc = fr; frc != NULL; frc = frc->fr_next)
9919 if (ses_do_frame(frc))
9920 break;
9921 return frc;
9922}
9923
9924/*
9925 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9926 * the Session.
9927 */
9928 static int
9929ses_do_frame(fr)
9930 frame_T *fr;
9931{
9932 frame_T *frc;
9933
9934 if (fr->fr_layout == FR_LEAF)
9935 return ses_do_win(fr->fr_win);
9936 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9937 if (ses_do_frame(frc))
9938 return TRUE;
9939 return FALSE;
9940}
9941
9942/*
9943 * Return non-zero if window "wp" is to be stored in the Session.
9944 */
9945 static int
9946ses_do_win(wp)
9947 win_T *wp;
9948{
9949 if (wp->w_buffer->b_fname == NULL
9950#ifdef FEAT_QUICKFIX
9951 /* When 'buftype' is "nofile" can't restore the window contents. */
9952 || bt_nofile(wp->w_buffer)
9953#endif
9954 )
9955 return (ssop_flags & SSOP_BLANK);
9956 if (wp->w_buffer->b_help)
9957 return (ssop_flags & SSOP_HELP);
9958 return TRUE;
9959}
9960
9961/*
9962 * Write commands to "fd" to restore the view of a window.
9963 * Caller must make sure 'scrolloff' is zero.
9964 */
9965 static int
9966put_view(fd, wp, add_edit, flagp)
9967 FILE *fd;
9968 win_T *wp;
9969 int add_edit; /* add ":edit" command to view */
9970 unsigned *flagp; /* vop_flags or ssop_flags */
9971{
9972 win_T *save_curwin;
9973 int f;
9974 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009975 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976
9977 /* Always restore cursor position for ":mksession". For ":mkview" only
9978 * when 'viewoptions' contains "cursor". */
9979 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9980
9981 /*
9982 * Local argument list.
9983 */
9984 if (wp->w_alist == &global_alist)
9985 {
9986 if (put_line(fd, "argglobal") == FAIL)
9987 return FAIL;
9988 }
9989 else
9990 {
9991 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9992 flagp == &vop_flags
9993 || !(*flagp & SSOP_CURDIR)
9994 || wp->w_localdir != NULL, flagp) == FAIL)
9995 return FAIL;
9996 }
9997
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009998 /* Only when part of a session: restore the argument index. Some
9999 * arguments may have been deleted, check if the index is valid. */
10000 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
10001 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 {
10003 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
10004 || put_eol(fd) == FAIL)
10005 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010006 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 }
10008
10009 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010010 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 {
10012 /*
10013 * Load the file.
10014 */
10015 if (wp->w_buffer->b_ffname != NULL
10016#ifdef FEAT_QUICKFIX
10017 && !bt_nofile(wp->w_buffer)
10018#endif
10019 )
10020 {
10021 /*
10022 * Editing a file in this buffer: use ":edit file".
10023 * This may have side effects! (e.g., compressed or network file).
10024 */
10025 if (fputs("edit ", fd) < 0
10026 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10027 return FAIL;
10028 }
10029 else
10030 {
10031 /* No file in this buffer, just make it empty. */
10032 if (put_line(fd, "enew") == FAIL)
10033 return FAIL;
10034#ifdef FEAT_QUICKFIX
10035 if (wp->w_buffer->b_ffname != NULL)
10036 {
10037 /* The buffer does have a name, but it's not a file name. */
10038 if (fputs("file ", fd) < 0
10039 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10040 return FAIL;
10041 }
10042#endif
10043 do_cursor = FALSE;
10044 }
10045 }
10046
10047 /*
10048 * Local mappings and abbreviations.
10049 */
10050 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10051 && makemap(fd, wp->w_buffer) == FAIL)
10052 return FAIL;
10053
10054 /*
10055 * Local options. Need to go to the window temporarily.
10056 * Store only local values when using ":mkview" and when ":mksession" is
10057 * used and 'sessionoptions' doesn't include "options".
10058 * Some folding options are always stored when "folds" is included,
10059 * otherwise the folds would not be restored correctly.
10060 */
10061 save_curwin = curwin;
10062 curwin = wp;
10063 curbuf = curwin->w_buffer;
10064 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10065 f = makeset(fd, OPT_LOCAL,
10066 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10067#ifdef FEAT_FOLDING
10068 else if (*flagp & SSOP_FOLDS)
10069 f = makefoldset(fd);
10070#endif
10071 else
10072 f = OK;
10073 curwin = save_curwin;
10074 curbuf = curwin->w_buffer;
10075 if (f == FAIL)
10076 return FAIL;
10077
10078#ifdef FEAT_FOLDING
10079 /*
10080 * Save Folds when 'buftype' is empty and for help files.
10081 */
10082 if ((*flagp & SSOP_FOLDS)
10083 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010084# ifdef FEAT_QUICKFIX
10085 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10086# endif
10087 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 {
10089 if (put_folds(fd, wp) == FAIL)
10090 return FAIL;
10091 }
10092#endif
10093
10094 /*
10095 * Set the cursor after creating folds, since that moves the cursor.
10096 */
10097 if (do_cursor)
10098 {
10099
10100 /* Restore the cursor line in the file and relatively in the
10101 * window. Don't use "G", it changes the jumplist. */
10102 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10103 (long)wp->w_cursor.lnum,
10104 (long)(wp->w_cursor.lnum - wp->w_topline),
10105 (long)wp->w_height / 2, (long)wp->w_height) < 0
10106 || put_eol(fd) == FAIL
10107 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10108 || put_line(fd, "exe s:l") == FAIL
10109 || put_line(fd, "normal! zt") == FAIL
10110 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10111 || put_eol(fd) == FAIL)
10112 return FAIL;
10113 /* Restore the cursor column and left offset when not wrapping. */
10114 if (wp->w_cursor.col == 0)
10115 {
10116 if (put_line(fd, "normal! 0") == FAIL)
10117 return FAIL;
10118 }
10119 else
10120 {
10121 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10122 {
10123 if (fprintf(fd,
10124 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10125 (long)wp->w_cursor.col,
10126 (long)(wp->w_cursor.col - wp->w_leftcol),
10127 (long)wp->w_width / 2, (long)wp->w_width) < 0
10128 || put_eol(fd) == FAIL
10129 || put_line(fd, "if s:c > 0") == FAIL
10130 || fprintf(fd,
10131 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10132 (long)wp->w_cursor.col) < 0
10133 || put_eol(fd) == FAIL
10134 || put_line(fd, "else") == FAIL
10135 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10136 || put_eol(fd) == FAIL
10137 || put_line(fd, "endif") == FAIL)
10138 return FAIL;
10139 }
10140 else
10141 {
10142 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10143 || put_eol(fd) == FAIL)
10144 return FAIL;
10145 }
10146 }
10147 }
10148
10149 /*
10150 * Local directory.
10151 */
10152 if (wp->w_localdir != NULL)
10153 {
10154 if (fputs("lcd ", fd) < 0
10155 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10156 || put_eol(fd) == FAIL)
10157 return FAIL;
10158 }
10159
10160 return OK;
10161}
10162
10163/*
10164 * Write an argument list to the session file.
10165 * Returns FAIL if writing fails.
10166 */
10167 static int
10168ses_arglist(fd, cmd, gap, fullname, flagp)
10169 FILE *fd;
10170 char *cmd;
10171 garray_T *gap;
10172 int fullname; /* TRUE: use full path name */
10173 unsigned *flagp;
10174{
10175 int i;
10176 char_u buf[MAXPATHL];
10177 char_u *s;
10178
10179 if (gap->ga_len == 0)
10180 return put_line(fd, "silent! argdel *");
10181 if (fputs(cmd, fd) < 0)
10182 return FAIL;
10183 for (i = 0; i < gap->ga_len; ++i)
10184 {
10185 /* NULL file names are skipped (only happens when out of memory). */
10186 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10187 if (s != NULL)
10188 {
10189 if (fullname)
10190 {
10191 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10192 s = buf;
10193 }
10194 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10195 return FAIL;
10196 }
10197 }
10198 return put_eol(fd);
10199}
10200
10201/*
10202 * Write a buffer name to the session file.
10203 * Also ends the line.
10204 * Returns FAIL if writing fails.
10205 */
10206 static int
10207ses_fname(fd, buf, flagp)
10208 FILE *fd;
10209 buf_T *buf;
10210 unsigned *flagp;
10211{
10212 char_u *name;
10213
10214 /* Use the short file name if the current directory is known at the time
10215 * the session file will be sourced. Don't do this for ":mkview", we
10216 * don't know the current directory. */
10217 if (buf->b_sfname != NULL
10218 && flagp == &ssop_flags
10219 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10220 name = buf->b_sfname;
10221 else
10222 name = buf->b_ffname;
10223 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10224 return FAIL;
10225 return OK;
10226}
10227
10228/*
10229 * Write a file name to the session file.
10230 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10231 * characters.
10232 * Returns FAIL if writing fails.
10233 */
10234 static int
10235ses_put_fname(fd, name, flagp)
10236 FILE *fd;
10237 char_u *name;
10238 unsigned *flagp;
10239{
10240 char_u *sname;
10241 int retval = OK;
10242 int c;
10243
10244 sname = home_replace_save(NULL, name);
10245 if (sname != NULL)
10246 name = sname;
10247 while (*name != NUL)
10248 {
10249#ifdef FEAT_MBYTE
10250 {
10251 int l;
10252
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010253 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 {
10255 /* copy a multibyte char */
10256 while (--l >= 0)
10257 {
10258 if (putc(*name, fd) != *name)
10259 retval = FAIL;
10260 ++name;
10261 }
10262 continue;
10263 }
10264 }
10265#endif
10266 c = *name++;
10267 if (c == '\\' && (*flagp & SSOP_SLASH))
10268 /* change a backslash to a forward slash */
10269 c = '/';
10270 else if ((vim_strchr(escape_chars, c) != NULL
10271#ifdef BACKSLASH_IN_FILENAME
10272 && c != '\\'
10273#endif
10274 ) || c == '#' || c == '%')
10275 {
10276 /* escape a special character with a backslash */
10277 if (putc('\\', fd) != '\\')
10278 retval = FAIL;
10279 }
10280 if (putc(c, fd) != c)
10281 retval = FAIL;
10282 }
10283 vim_free(sname);
10284 return retval;
10285}
10286
10287/*
10288 * ":loadview [nr]"
10289 */
10290 static void
10291ex_loadview(eap)
10292 exarg_T *eap;
10293{
10294 char_u *fname;
10295
10296 fname = get_view_file(*eap->arg);
10297 if (fname != NULL)
10298 {
10299 do_source(fname, FALSE, FALSE);
10300 vim_free(fname);
10301 }
10302}
10303
10304/*
10305 * Get the name of the view file for the current buffer.
10306 */
10307 static char_u *
10308get_view_file(c)
10309 int c;
10310{
10311 int len = 0;
10312 char_u *p, *s;
10313 char_u *retval;
10314 char_u *sname;
10315
10316 if (curbuf->b_ffname == NULL)
10317 {
10318 EMSG(_(e_noname));
10319 return NULL;
10320 }
10321 sname = home_replace_save(NULL, curbuf->b_ffname);
10322 if (sname == NULL)
10323 return NULL;
10324
10325 /*
10326 * We want a file name without separators, because we're not going to make
10327 * a directory.
10328 * "normal" path separator -> "=+"
10329 * "=" -> "=="
10330 * ":" path separator -> "=-"
10331 */
10332 for (p = sname; *p; ++p)
10333 if (*p == '=' || vim_ispathsep(*p))
10334 ++len;
10335 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10336 if (retval != NULL)
10337 {
10338 STRCPY(retval, p_vdir);
10339 add_pathsep(retval);
10340 s = retval + STRLEN(retval);
10341 for (p = sname; *p; ++p)
10342 {
10343 if (*p == '=')
10344 {
10345 *s++ = '=';
10346 *s++ = '=';
10347 }
10348 else if (vim_ispathsep(*p))
10349 {
10350 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010351#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 || defined(VMS)
10353 if (*p == ':')
10354 *s++ = '-';
10355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010357 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 }
10359 else
10360 *s++ = *p;
10361 }
10362 *s++ = '=';
10363 *s++ = c;
10364 STRCPY(s, ".vim");
10365 }
10366
10367 vim_free(sname);
10368 return retval;
10369}
10370
10371#endif /* FEAT_SESSION */
10372
10373/*
10374 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10375 * Return FAIL for a write error.
10376 */
10377 int
10378put_eol(fd)
10379 FILE *fd;
10380{
10381 if (
10382#ifdef USE_CRNL
10383 (
10384# ifdef MKSESSION_NL
10385 !mksession_nl &&
10386# endif
10387 (putc('\r', fd) < 0)) ||
10388#endif
10389 (putc('\n', fd) < 0))
10390 return FAIL;
10391 return OK;
10392}
10393
10394/*
10395 * Write a line to "fd".
10396 * Return FAIL for a write error.
10397 */
10398 int
10399put_line(fd, s)
10400 FILE *fd;
10401 char *s;
10402{
10403 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10404 return FAIL;
10405 return OK;
10406}
10407
10408#ifdef FEAT_VIMINFO
10409/*
10410 * ":rviminfo" and ":wviminfo".
10411 */
10412 static void
10413ex_viminfo(eap)
10414 exarg_T *eap;
10415{
10416 char_u *save_viminfo;
10417
10418 save_viminfo = p_viminfo;
10419 if (*p_viminfo == NUL)
10420 p_viminfo = (char_u *)"'100";
10421 if (eap->cmdidx == CMD_rviminfo)
10422 {
10423 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10424 EMSG(_("E195: Cannot open viminfo file for reading"));
10425 }
10426 else
10427 write_viminfo(eap->arg, eap->forceit);
10428 p_viminfo = save_viminfo;
10429}
10430#endif
10431
10432#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010433/*
10434 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010435 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 void
10438dialog_msg(buff, format, fname)
10439 char_u *buff;
10440 char *format;
10441 char_u *fname;
10442{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 if (fname == NULL)
10444 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010445 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446}
10447#endif
10448
10449/*
10450 * ":behave {mswin,xterm}"
10451 */
10452 static void
10453ex_behave(eap)
10454 exarg_T *eap;
10455{
10456 if (STRCMP(eap->arg, "mswin") == 0)
10457 {
10458 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10459 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10460 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10461 set_option_value((char_u *)"keymodel", 0L,
10462 (char_u *)"startsel,stopsel", 0);
10463 }
10464 else if (STRCMP(eap->arg, "xterm") == 0)
10465 {
10466 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10467 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10468 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10469 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10470 }
10471 else
10472 EMSG2(_(e_invarg2), eap->arg);
10473}
10474
10475#ifdef FEAT_AUTOCMD
10476static int filetype_detect = FALSE;
10477static int filetype_plugin = FALSE;
10478static int filetype_indent = FALSE;
10479
10480/*
10481 * ":filetype [plugin] [indent] {on,off,detect}"
10482 * on: Load the filetype.vim file to install autocommands for file types.
10483 * off: Load the ftoff.vim file to remove all autocommands for file types.
10484 * plugin on: load filetype.vim and ftplugin.vim
10485 * plugin off: load ftplugof.vim
10486 * indent on: load filetype.vim and indent.vim
10487 * indent off: load indoff.vim
10488 */
10489 static void
10490ex_filetype(eap)
10491 exarg_T *eap;
10492{
10493 char_u *arg = eap->arg;
10494 int plugin = FALSE;
10495 int indent = FALSE;
10496
10497 if (*eap->arg == NUL)
10498 {
10499 /* Print current status. */
10500 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10501 filetype_detect ? "ON" : "OFF",
10502 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10503 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10504 return;
10505 }
10506
10507 /* Accept "plugin" and "indent" in any order. */
10508 for (;;)
10509 {
10510 if (STRNCMP(arg, "plugin", 6) == 0)
10511 {
10512 plugin = TRUE;
10513 arg = skipwhite(arg + 6);
10514 continue;
10515 }
10516 if (STRNCMP(arg, "indent", 6) == 0)
10517 {
10518 indent = TRUE;
10519 arg = skipwhite(arg + 6);
10520 continue;
10521 }
10522 break;
10523 }
10524 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10525 {
10526 if (*arg == 'o' || !filetype_detect)
10527 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010528 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 filetype_detect = TRUE;
10530 if (plugin)
10531 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010532 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 filetype_plugin = TRUE;
10534 }
10535 if (indent)
10536 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010537 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 filetype_indent = TRUE;
10539 }
10540 }
10541 if (*arg == 'd')
10542 {
10543 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010544 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 }
10546 }
10547 else if (STRCMP(arg, "off") == 0)
10548 {
10549 if (plugin || indent)
10550 {
10551 if (plugin)
10552 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010553 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 filetype_plugin = FALSE;
10555 }
10556 if (indent)
10557 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010558 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 filetype_indent = FALSE;
10560 }
10561 }
10562 else
10563 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010564 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 filetype_detect = FALSE;
10566 }
10567 }
10568 else
10569 EMSG2(_(e_invarg2), arg);
10570}
10571
10572/*
10573 * ":setfiletype {name}"
10574 */
10575 static void
10576ex_setfiletype(eap)
10577 exarg_T *eap;
10578{
10579 if (!did_filetype)
10580 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10581}
10582#endif
10583
10584/*ARGSUSED*/
10585 static void
10586ex_digraphs(eap)
10587 exarg_T *eap;
10588{
10589#ifdef FEAT_DIGRAPHS
10590 if (*eap->arg != NUL)
10591 putdigraph(eap->arg);
10592 else
10593 listdigraphs();
10594#else
10595 EMSG(_("E196: No digraphs in this version"));
10596#endif
10597}
10598
10599 static void
10600ex_set(eap)
10601 exarg_T *eap;
10602{
10603 int flags = 0;
10604
10605 if (eap->cmdidx == CMD_setlocal)
10606 flags = OPT_LOCAL;
10607 else if (eap->cmdidx == CMD_setglobal)
10608 flags = OPT_GLOBAL;
10609#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10610 if (cmdmod.browse && flags == 0)
10611 ex_options(eap);
10612 else
10613#endif
10614 (void)do_set(eap->arg, flags);
10615}
10616
10617#ifdef FEAT_SEARCH_EXTRA
10618/*
10619 * ":nohlsearch"
10620 */
10621/*ARGSUSED*/
10622 static void
10623ex_nohlsearch(eap)
10624 exarg_T *eap;
10625{
10626 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010627 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628}
10629
10630/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010631 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 * Sets nextcmd to the start of the next command, if any. Also called when
10633 * skipping commands to find the next command.
10634 */
10635 static void
10636ex_match(eap)
10637 exarg_T *eap;
10638{
10639 char_u *p;
10640 char_u *end;
10641 int c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010642 int mi;
10643
10644 if (eap->line2 <= 3)
10645 mi = eap->line2 - 1;
10646 else
10647 {
10648 EMSG(e_invcmd);
10649 return;
10650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651
10652 /* First clear any old pattern. */
10653 if (!eap->skip)
10654 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010655 vim_free(curwin->w_match[mi].regprog);
10656 curwin->w_match[mi].regprog = NULL;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010657 redraw_later(SOME_VALID); /* always need a redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 }
10659
10660 if (ends_excmd(*eap->arg))
10661 end = eap->arg;
10662 else if ((STRNICMP(eap->arg, "none", 4) == 0
10663 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10664 end = eap->arg + 4;
10665 else
10666 {
10667 p = skiptowhite(eap->arg);
10668 if (!eap->skip)
10669 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010670 curwin->w_match_id[mi] = syn_namen2id(eap->arg,
10671 (int)(p - eap->arg));
10672 if (curwin->w_match_id[mi] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 {
10674 EMSG2(_(e_nogroup), eap->arg);
10675 return;
10676 }
10677 }
10678 p = skipwhite(p);
10679 if (*p == NUL)
10680 {
10681 /* There must be two arguments. */
10682 EMSG2(_(e_invarg2), eap->arg);
10683 return;
10684 }
10685 end = skip_regexp(p + 1, *p, TRUE, NULL);
10686 if (!eap->skip)
10687 {
10688 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10689 {
10690 eap->errmsg = e_trailing;
10691 return;
10692 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010693 if (*end != *p)
10694 {
10695 EMSG2(_(e_invarg2), p);
10696 return;
10697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698
10699 c = *end;
10700 *end = NUL;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010701 curwin->w_match[mi].regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 *end = c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010703 if (curwin->w_match[mi].regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 {
10705 EMSG2(_(e_invarg2), p);
10706 return;
10707 }
10708 }
10709 }
10710 eap->nextcmd = find_nextcmd(end);
10711}
10712#endif
10713
10714#ifdef FEAT_CRYPT
10715/*
10716 * ":X": Get crypt key
10717 */
10718/*ARGSUSED*/
10719 static void
10720ex_X(eap)
10721 exarg_T *eap;
10722{
10723 (void)get_crypt_key(TRUE, TRUE);
10724}
10725#endif
10726
10727#ifdef FEAT_FOLDING
10728 static void
10729ex_fold(eap)
10730 exarg_T *eap;
10731{
10732 if (foldManualAllowed(TRUE))
10733 foldCreate(eap->line1, eap->line2);
10734}
10735
10736 static void
10737ex_foldopen(eap)
10738 exarg_T *eap;
10739{
10740 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10741 eap->forceit, FALSE);
10742}
10743
10744 static void
10745ex_folddo(eap)
10746 exarg_T *eap;
10747{
10748 linenr_T lnum;
10749
10750 /* First set the marks for all lines closed/open. */
10751 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10752 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10753 ml_setmarked(lnum);
10754
10755 /* Execute the command on the marked lines. */
10756 global_exe(eap->arg);
10757 ml_clearmarked(); /* clear rest of the marks */
10758}
10759#endif