blob: e78f762440e6a55dc9ce346df79c1576288753d9 [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 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000836 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000837 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 initial_trylevel = trylevel;
840
841 /*
842 * "did_throw" will be set to TRUE when an exception is being thrown.
843 */
844 did_throw = FALSE;
845#endif
846 /*
847 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000848 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 * If force_abort is set, we cancel everything.
850 */
851 did_emsg = FALSE;
852
853 /*
854 * KeyTyped is only set when calling vgetc(). Reset it here when not
855 * calling vgetc() (sourced command lines).
856 */
857 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
858 KeyTyped = FALSE;
859
860 /*
861 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * - for multiple commands on one line, separated with '|'
864 * - when repeating until there are no more lines (for ":source")
865 */
866 next_cmdline = cmdline;
867 do
868 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869#ifdef FEAT_EVAL
870 getline_is_func = getline_equal(getline, cookie, get_func_line);
871#endif
872
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000873 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (next_cmdline == NULL
875#ifdef FEAT_EVAL
876 && !force_abort
877 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000878 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
880 )
881 did_emsg = FALSE;
882
883 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000884 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * 2. If no line given: Get an allocated line with getline().
886 * 3. If a line is given: Make a copy, so we can mess with it.
887 */
888
889#ifdef FEAT_EVAL
890 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000891 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
893 /* Each '|' separated command is stored separately in lines_ga, to
894 * be able to jump to it. Don't use next_cmdline now. */
895 vim_free(cmdline_copy);
896 cmdline_copy = NULL;
897
898 /* Check if a function has returned or, unless it has an unclosed
899 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000903 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904 func_line_end(real_cookie);
905# endif
906 if (func_has_ended(real_cookie))
907 {
908 retval = FAIL;
909 break;
910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000912#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000913 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914 && getline_equal(getline, cookie, getsourceline))
915 script_line_end();
916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 /* Check if a sourced file hit a ":finish" command. */
919 if (source_finished(getline, cookie))
920 {
921 retval = FAIL;
922 break;
923 }
924
925 /* If breakpoints have been added/deleted need to check for it. */
926 if (breakpoint != NULL && dbg_tick != NULL
927 && *dbg_tick != debug_tick)
928 {
929 *breakpoint = dbg_find_breakpoint(
930 getline_equal(getline, cookie, getsourceline),
931 fname, sourcing_lnum);
932 *dbg_tick = debug_tick;
933 }
934
935 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
936 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
937
938 /* Did we encounter a breakpoint? */
939 if (breakpoint != NULL && *breakpoint != 0
940 && *breakpoint <= sourcing_lnum)
941 {
942 dbg_breakpoint(fname, sourcing_lnum);
943 /* Find next breakpoint. */
944 *breakpoint = dbg_find_breakpoint(
945 getline_equal(getline, cookie, getsourceline),
946 fname, sourcing_lnum);
947 *dbg_tick = debug_tick;
948 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000950 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000951 {
952 if (getline_is_func)
953 func_line_start(real_cookie);
954 else if (getline_equal(getline, cookie, getsourceline))
955 script_line_start();
956 }
957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000960 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000962 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 * again. Pass a different "getline" function to do_one_cmd()
964 * below, so that it stores lines in or reads them from
965 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000966 * while/for loop. */
967 cmd_getline = get_loop_line;
968 cmd_cookie = (void *)&cmd_loop_cookie;
969 cmd_loop_cookie.lines_gap = &lines_ga;
970 cmd_loop_cookie.current_line = current_line;
971 cmd_loop_cookie.getline = getline;
972 cmd_loop_cookie.cookie = cookie;
973 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 }
975 else
976 {
977 cmd_getline = getline;
978 cmd_cookie = cookie;
979 }
980#endif
981
982 /* 2. If no line given, get an allocated line with getline(). */
983 if (next_cmdline == NULL)
984 {
985 /*
986 * Need to set msg_didout for the first line after an ":if",
987 * otherwise the ":if" will be overwritten.
988 */
989 if (count == 1 && getline_equal(getline, cookie, getexline))
990 msg_didout = TRUE;
991 if (getline == NULL || (next_cmdline = getline(':', cookie,
992#ifdef FEAT_EVAL
993 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
994#else
995 0
996#endif
997 )) == NULL)
998 {
999 /* Don't call wait_return for aborted command line. The NULL
1000 * returned for the end of a sourced file or executed function
1001 * doesn't do this. */
1002 if (KeyTyped && !(flags & DOCMD_REPEAT))
1003 need_wait_return = FALSE;
1004 retval = FAIL;
1005 break;
1006 }
1007 used_getline = TRUE;
1008
1009 /*
1010 * Keep the first typed line. Clear it when more lines are typed.
1011 */
1012 if (flags & DOCMD_KEEPLINE)
1013 {
1014 vim_free(repeat_cmdline);
1015 if (count == 0)
1016 repeat_cmdline = vim_strsave(next_cmdline);
1017 else
1018 repeat_cmdline = NULL;
1019 }
1020 }
1021
1022 /* 3. Make a copy of the command so we can mess with it. */
1023 else if (cmdline_copy == NULL)
1024 {
1025 next_cmdline = vim_strsave(next_cmdline);
1026 if (next_cmdline == NULL)
1027 {
1028 EMSG(_(e_outofmem));
1029 retval = FAIL;
1030 break;
1031 }
1032 }
1033 cmdline_copy = next_cmdline;
1034
1035#ifdef FEAT_EVAL
1036 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 * Save the current line when inside a ":while" or ":for", and when
1038 * the command looks like a ":while" or ":for", because we may need it
1039 * later. When there is a '|' and another command, it is stored
1040 * separately, because we need to be able to jump back to it from an
1041 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (current_line == lines_ga.ga_len
1044 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 {
1048 retval = FAIL;
1049 break;
1050 }
1051 }
1052 did_endif = FALSE;
1053#endif
1054
1055 if (count++ == 0)
1056 {
1057 /*
1058 * All output from the commands is put below each other, without
1059 * waiting for a return. Don't do this when executing commands
1060 * from a script or when being called recursive (e.g. for ":e
1061 * +command file").
1062 */
1063 if (!(flags & DOCMD_NOWAIT) && !recursive)
1064 {
1065 msg_didout_before_start = msg_didout;
1066 msg_didany = FALSE; /* no output yet */
1067 msg_start();
1068 msg_scroll = TRUE; /* put messages below each other */
1069 ++no_wait_return; /* dont wait for return until finished */
1070 ++RedrawingDisabled;
1071 did_inc = TRUE;
1072 }
1073 }
1074
1075 if (p_verbose >= 15 && sourcing_name != NULL)
1076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001078 verbose_enter_scroll();
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 smsg((char_u *)_("line %ld: %s"),
1081 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001082 if (msg_silent == 0)
1083 msg_puts((char_u *)"\n"); /* don't overwrite this */
1084
1085 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 --no_wait_return;
1087 }
1088
1089 /*
1090 * 2. Execute one '|' separated command.
1091 * do_one_cmd() will return NULL if there is no trailing '|'.
1092 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1093 */
1094 ++recursive;
1095 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1096#ifdef FEAT_EVAL
1097 &cstack,
1098#endif
1099 cmd_getline, cmd_cookie);
1100 --recursive;
1101
1102#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 if (cmd_cookie == (void *)&cmd_loop_cookie)
1104 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107#endif
1108
1109 if (next_cmdline == NULL)
1110 {
1111 vim_free(cmdline_copy);
1112 cmdline_copy = NULL;
1113#ifdef FEAT_CMDHIST
1114 /*
1115 * If the command was typed, remember it for the ':' register.
1116 * Do this AFTER executing the command to make :@: work.
1117 */
1118 if (getline_equal(getline, cookie, getexline)
1119 && new_last_cmdline != NULL)
1120 {
1121 vim_free(last_cmdline);
1122 last_cmdline = new_last_cmdline;
1123 new_last_cmdline = NULL;
1124 }
1125#endif
1126 }
1127 else
1128 {
1129 /* need to copy the command after the '|' to cmdline_copy, for the
1130 * next do_one_cmd() */
1131 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1132 next_cmdline = cmdline_copy;
1133 }
1134
1135
1136#ifdef FEAT_EVAL
1137 /* reset did_emsg for a function that is not aborted by an error */
1138 if (did_emsg && !force_abort
1139 && getline_equal(getline, cookie, get_func_line)
1140 && !func_has_abort(real_cookie))
1141 did_emsg = FALSE;
1142
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 ++current_line;
1146
1147 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 * An ":endwhile", ":endfor" and ":continue" is handled here.
1149 * If we were executing commands, jump back to the ":while" or
1150 * ":for".
1151 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 /* Jump back to the matching ":while" or ":for". Be careful
1158 * not to use a cs_line[] from an entry that isn't a ":while"
1159 * or ":for": It would make "current_line" invalid and can
1160 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (!did_emsg && !got_int && !did_throw
1162 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 && (cstack.cs_flags[cstack.cs_idx]
1164 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 && cstack.cs_line[cstack.cs_idx] >= 0
1166 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1167 {
1168 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 /* remember we jumped there */
1170 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 line_breakcheck(); /* check if CTRL-C typed */
1172
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 /* Check for the next breakpoint at or after the ":while"
1174 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (breakpoint != NULL)
1176 {
1177 *breakpoint = dbg_find_breakpoint(
1178 getline_equal(getline, cookie, getsourceline),
1179 fname,
1180 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1181 *dbg_tick = debug_tick;
1182 }
1183 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001188 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1189 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 }
1192
1193 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1200 }
1201 }
1202
1203 /*
1204 * When not inside any ":while" loop, clear remembered lines.
1205 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
1208 if (lines_ga.ga_len > 0)
1209 {
1210 sourcing_lnum =
1211 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1212 free_cmdlines(&lines_ga);
1213 }
1214 current_line = 0;
1215 }
1216
1217 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1219 * being restored at the ":endtry". Reset them here and set the
1220 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1221 * This includes the case where a missing ":endif", ":endwhile" or
1222 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 cstack.cs_lflags &= ~CSL_HAD_FINA;
1227 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1228 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 did_throw ? (void *)current_exception : NULL);
1230 did_emsg = got_int = did_throw = FALSE;
1231 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1232 }
1233
1234 /* Update global "trylevel" for recursive calls to do_cmdline() from
1235 * within this loop. */
1236 trylevel = initial_trylevel + cstack.cs_trylevel;
1237
1238 /*
1239 * If the outermost try conditional (accross function calls and sourced
1240 * files) is aborted because of an error, an interrupt, or an uncaught
1241 * exception, cancel everything. If it is left normally, reset
1242 * force_abort to get the non-EH compatible abortion behavior for
1243 * the rest of the script.
1244 */
1245 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1246 force_abort = FALSE;
1247
1248 /* Convert an interrupt to an exception if appropriate. */
1249 (void)do_intthrow(&cstack);
1250#endif /* FEAT_EVAL */
1251
1252 }
1253 /*
1254 * Continue executing command lines when:
1255 * - no CTRL-C typed, no aborting error, no exception thrown or try
1256 * conditionals need to be checked for executing finally clauses or
1257 * catching an interrupt exception
1258 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001259 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 * looping for ":source" command or function call.
1261 */
1262 while (!((got_int
1263#ifdef FEAT_EVAL
1264 || (did_emsg && force_abort) || did_throw
1265#endif
1266 )
1267#ifdef FEAT_EVAL
1268 && cstack.cs_trylevel == 0
1269#endif
1270 )
1271 && !(did_emsg && used_getline
1272 && (getline_equal(getline, cookie, getexmodeline)
1273 || getline_equal(getline, cookie, getexline)))
1274 && (next_cmdline != NULL
1275#ifdef FEAT_EVAL
1276 || cstack.cs_idx >= 0
1277#endif
1278 || (flags & DOCMD_REPEAT)));
1279
1280 vim_free(cmdline_copy);
1281#ifdef FEAT_EVAL
1282 free_cmdlines(&lines_ga);
1283 ga_clear(&lines_ga);
1284
1285 if (cstack.cs_idx >= 0)
1286 {
1287 /*
1288 * If a sourced file or executed function ran to its end, report the
1289 * unclosed conditional.
1290 */
1291 if (!got_int && !did_throw
1292 && ((getline_equal(getline, cookie, getsourceline)
1293 && !source_finished(getline, cookie))
1294 || (getline_equal(getline, cookie, get_func_line)
1295 && !func_has_ended(real_cookie))))
1296 {
1297 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1298 EMSG(_(e_endtry));
1299 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1300 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001301 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1302 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 else
1304 EMSG(_(e_endif));
1305 }
1306
1307 /*
1308 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1309 * ":endtry" in a sourced file or executed function. If the try
1310 * conditional is in its finally clause, ignore anything pending.
1311 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001312 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 */
1314 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001315 {
1316 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1317
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001318 if (idx >= 0)
1319 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001320 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1321 &cstack.cs_looplevel);
1322 }
1323 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 trylevel = initial_trylevel;
1325 }
1326
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001327 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1328 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * exception, do this now after rewinding the cstack. */
1330 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1331 ? (char_u *)"endfunction" : (char_u *)NULL);
1332
1333 if (trylevel == 0)
1334 {
1335 /*
1336 * When an exception is being thrown out of the outermost try
1337 * conditional, discard the uncaught exception, disable the conversion
1338 * of interrupts or errors to exceptions, and ensure that no more
1339 * commands are executed.
1340 */
1341 if (did_throw)
1342 {
1343 void *p = NULL;
1344 char_u *saved_sourcing_name;
1345 int saved_sourcing_lnum;
1346 struct msglist *messages = NULL, *next;
1347
1348 /*
1349 * If the uncaught exception is a user exception, report it as an
1350 * error. If it is an error exception, display the saved error
1351 * message now. For an interrupt exception, do nothing; the
1352 * interrupt message is given elsewhere.
1353 */
1354 switch (current_exception->type)
1355 {
1356 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001357 vim_snprintf((char *)IObuff, IOSIZE,
1358 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 current_exception->value);
1360 p = vim_strsave(IObuff);
1361 break;
1362 case ET_ERROR:
1363 messages = current_exception->messages;
1364 current_exception->messages = NULL;
1365 break;
1366 case ET_INTERRUPT:
1367 break;
1368 default:
1369 p = vim_strsave((char_u *)_(e_internal));
1370 }
1371
1372 saved_sourcing_name = sourcing_name;
1373 saved_sourcing_lnum = sourcing_lnum;
1374 sourcing_name = current_exception->throw_name;
1375 sourcing_lnum = current_exception->throw_lnum;
1376 current_exception->throw_name = NULL;
1377
1378 discard_current_exception(); /* uses IObuff if 'verbose' */
1379 suppress_errthrow = TRUE;
1380 force_abort = TRUE;
1381
1382 if (messages != NULL)
1383 {
1384 do
1385 {
1386 next = messages->next;
1387 emsg(messages->msg);
1388 vim_free(messages->msg);
1389 vim_free(messages);
1390 messages = next;
1391 }
1392 while (messages != NULL);
1393 }
1394 else if (p != NULL)
1395 {
1396 emsg(p);
1397 vim_free(p);
1398 }
1399 vim_free(sourcing_name);
1400 sourcing_name = saved_sourcing_name;
1401 sourcing_lnum = saved_sourcing_lnum;
1402 }
1403
1404 /*
1405 * On an interrupt or an aborting error not converted to an exception,
1406 * disable the conversion of errors to exceptions. (Interrupts are not
1407 * converted any more, here.) This enables also the interrupt message
1408 * when force_abort is set and did_emsg unset in case of an interrupt
1409 * from a finally clause after an error.
1410 */
1411 else if (got_int || (did_emsg && force_abort))
1412 suppress_errthrow = TRUE;
1413 }
1414
1415 /*
1416 * The current cstack will be freed when do_cmdline() returns. An uncaught
1417 * exception will have to be rethrown in the previous cstack. If a function
1418 * has just returned or a script file was just finished and the previous
1419 * cstack belongs to the same function or, respectively, script file, it
1420 * will have to be checked for finally clauses to be executed due to the
1421 * ":return" or ":finish". This is done in do_one_cmd().
1422 */
1423 if (did_throw)
1424 need_rethrow = TRUE;
1425 if ((getline_equal(getline, cookie, getsourceline)
1426 && ex_nesting_level > source_level(real_cookie))
1427 || (getline_equal(getline, cookie, get_func_line)
1428 && ex_nesting_level > func_level(real_cookie) + 1))
1429 {
1430 if (!did_throw)
1431 check_cstack = TRUE;
1432 }
1433 else
1434 {
1435 /* When leaving a function, reduce nesting level. */
1436 if (getline_equal(getline, cookie, get_func_line))
1437 --ex_nesting_level;
1438 /*
1439 * Go to debug mode when returning from a function in which we are
1440 * single-stepping.
1441 */
1442 if ((getline_equal(getline, cookie, getsourceline)
1443 || getline_equal(getline, cookie, get_func_line))
1444 && ex_nesting_level + 1 <= debug_break_level)
1445 do_debug(getline_equal(getline, cookie, getsourceline)
1446 ? (char_u *)_("End of sourced file")
1447 : (char_u *)_("End of function"));
1448 }
1449
1450 /*
1451 * Restore the exception environment (done after returning from the
1452 * debugger).
1453 */
1454 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001455 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 msg_list = saved_msg_list;
1458#endif /* FEAT_EVAL */
1459
1460 /*
1461 * If there was too much output to fit on the command line, ask the user to
1462 * hit return before redrawing the screen. With the ":global" command we do
1463 * this only once after the command is finished.
1464 */
1465 if (did_inc)
1466 {
1467 --RedrawingDisabled;
1468 --no_wait_return;
1469 msg_scroll = FALSE;
1470
1471 /*
1472 * When just finished an ":if"-":else" which was typed, no need to
1473 * wait for hit-return. Also for an error situation.
1474 */
1475 if (retval == FAIL
1476#ifdef FEAT_EVAL
1477 || (did_endif && KeyTyped && !did_emsg)
1478#endif
1479 )
1480 {
1481 need_wait_return = FALSE;
1482 msg_didany = FALSE; /* don't wait when restarting edit */
1483 }
1484 else if (need_wait_return)
1485 {
1486 /*
1487 * The msg_start() above clears msg_didout. The wait_return we do
1488 * here should not overwrite the command that may be shown before
1489 * doing that.
1490 */
1491 msg_didout |= msg_didout_before_start;
1492 wait_return(FALSE);
1493 }
1494 }
1495
1496#ifndef FEAT_EVAL
1497 /*
1498 * Reset if_level, in case a sourced script file contains more ":if" than
1499 * ":endif" (could be ":if x | foo | endif").
1500 */
1501 if_level = 0;
1502#endif
1503
1504 --call_depth;
1505 return retval;
1506}
1507
1508#ifdef FEAT_EVAL
1509/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 */
1512 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001513get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 int c;
1515 void *cookie;
1516 int indent;
1517{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001518 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 wcmd_T *wp;
1520 char_u *line;
1521
1522 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1523 {
1524 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (cp->getline == NULL)
1529 line = getcmdline(c, 0L, indent);
1530 else
1531 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001532 if (line != NULL && 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
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001713 /* "#!anything" is handled like a comment. */
1714 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1715 goto doend;
1716
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 /*
1718 * Repeat until no more command modifiers are found.
1719 */
1720 ea.cmd = *cmdlinep;
1721 for (;;)
1722 {
1723/*
1724 * 1. skip comment lines and leading white space and colons
1725 */
1726 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1727 ++ea.cmd;
1728
1729 /* in ex mode, an empty line works like :+ */
1730 if (*ea.cmd == NUL && exmode_active
1731 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001732 || getline_equal(getline, cookie, getexline))
1733 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
1735 ea.cmd = (char_u *)"+";
1736 ex_pressedreturn = TRUE;
1737 }
1738
1739 /* ignore comment and empty lines */
1740 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001741 {
1742 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746/*
1747 * 2. handle command modifiers.
1748 */
1749 p = ea.cmd;
1750 if (VIM_ISDIGIT(*ea.cmd))
1751 p = skipwhite(skipdigits(ea.cmd));
1752 switch (*p)
1753 {
1754 /* When adding an entry, also modify cmd_exists(). */
1755 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1756 break;
1757#ifdef FEAT_WINDOWS
1758 cmdmod.split |= WSP_ABOVE;
1759#endif
1760 continue;
1761
1762 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1763 {
1764#ifdef FEAT_WINDOWS
1765 cmdmod.split |= WSP_BELOW;
1766#endif
1767 continue;
1768 }
1769 if (checkforcmd(&ea.cmd, "browse", 3))
1770 {
1771#ifdef FEAT_BROWSE
1772 cmdmod.browse = TRUE;
1773#endif
1774 continue;
1775 }
1776 if (!checkforcmd(&ea.cmd, "botright", 2))
1777 break;
1778#ifdef FEAT_WINDOWS
1779 cmdmod.split |= WSP_BOT;
1780#endif
1781 continue;
1782
1783 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1784 break;
1785#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1786 cmdmod.confirm = TRUE;
1787#endif
1788 continue;
1789
1790 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1791 {
1792 cmdmod.keepmarks = TRUE;
1793 continue;
1794 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001795 if (checkforcmd(&ea.cmd, "keepalt", 5))
1796 {
1797 cmdmod.keepalt = TRUE;
1798 continue;
1799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1801 break;
1802 cmdmod.keepjumps = TRUE;
1803 continue;
1804
1805 /* ":hide" and ":hide | cmd" are not modifiers */
1806 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1807 || *p == NUL || ends_excmd(*p))
1808 break;
1809 ea.cmd = p;
1810 cmdmod.hide = TRUE;
1811 continue;
1812
1813 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1814 {
1815 cmdmod.lockmarks = TRUE;
1816 continue;
1817 }
1818
1819 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1820 break;
1821#ifdef FEAT_WINDOWS
1822 cmdmod.split |= WSP_ABOVE;
1823#endif
1824 continue;
1825
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001826 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1827 break;
1828#ifdef FEAT_AUTOCMD
1829 if (cmdmod.save_ei == NULL)
1830 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001831 /* Set 'eventignore' to "all". Restore the
1832 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001833 cmdmod.save_ei = vim_strsave(p_ei);
1834 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001835 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001836 }
1837#endif
1838 continue;
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1841 break;
1842#ifdef FEAT_WINDOWS
1843 cmdmod.split |= WSP_BELOW;
1844#endif
1845 continue;
1846
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001847 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1848 {
1849#ifdef HAVE_SANDBOX
1850 if (!did_sandbox)
1851 ++sandbox;
1852 did_sandbox = TRUE;
1853#endif
1854 continue;
1855 }
1856 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 break;
1858 ++did_silent;
1859 ++msg_silent;
1860 save_msg_scroll = msg_scroll;
1861 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1862 {
1863 /* ":silent!", but not "silent !cmd" */
1864 ea.cmd = skipwhite(ea.cmd + 1);
1865 ++emsg_silent;
1866 ++did_esilent;
1867 }
1868 continue;
1869
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001870 case 't': if (checkforcmd(&p, "tab", 3))
1871 {
1872#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001873 if (vim_isdigit(*ea.cmd))
1874 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1875 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001876 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001877 ea.cmd = p;
1878#endif
1879 continue;
1880 }
1881 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 break;
1883#ifdef FEAT_WINDOWS
1884 cmdmod.split |= WSP_TOP;
1885#endif
1886 continue;
1887
1888 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1889 {
1890#ifdef FEAT_VERTSPLIT
1891 cmdmod.split |= WSP_VERT;
1892#endif
1893 continue;
1894 }
1895 if (!checkforcmd(&p, "verbose", 4))
1896 break;
1897 if (verbose_save < 0)
1898 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001899 if (vim_isdigit(*ea.cmd))
1900 p_verbose = atoi((char *)ea.cmd);
1901 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 p_verbose = 1;
1903 ea.cmd = p;
1904 continue;
1905 }
1906 break;
1907 }
1908
1909#ifdef FEAT_EVAL
1910 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1911 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1912#else
1913 ea.skip = (if_level > 0);
1914#endif
1915
1916#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001917# ifdef FEAT_PROFILE
1918 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001919 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001920 {
1921 if (getline_equal(getline, cookie, get_func_line))
1922 func_line_exec(getline_cookie(getline, cookie));
1923 else if (getline_equal(getline, cookie, getsourceline))
1924 script_line_exec();
1925 }
1926#endif
1927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 /* May go to debug mode. If this happens and the ">quit" debug command is
1929 * used, throw an interrupt exception and skip the next command. */
1930 dbg_check_breakpoint(&ea);
1931 if (!ea.skip && got_int)
1932 {
1933 ea.skip = TRUE;
1934 (void)do_intthrow(cstack);
1935 }
1936#endif
1937
1938/*
1939 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1940 *
1941 * where 'addr' is:
1942 *
1943 * % (entire file)
1944 * $ [+-NUM]
1945 * 'x [+-NUM] (where x denotes a currently defined mark)
1946 * . [+-NUM]
1947 * [+-NUM]..
1948 * NUM
1949 *
1950 * The ea.cmd pointer is updated to point to the first character following the
1951 * range spec. If an initial address is found, but no second, the upper bound
1952 * is equal to the lower.
1953 */
1954
1955 /* repeat for all ',' or ';' separated addresses */
1956 for (;;)
1957 {
1958 ea.line1 = ea.line2;
1959 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1960 ea.cmd = skipwhite(ea.cmd);
1961 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1962 if (ea.cmd == NULL) /* error detected */
1963 goto doend;
1964 if (lnum == MAXLNUM)
1965 {
1966 if (*ea.cmd == '%') /* '%' - all lines */
1967 {
1968 ++ea.cmd;
1969 ea.line1 = 1;
1970 ea.line2 = curbuf->b_ml.ml_line_count;
1971 ++ea.addr_count;
1972 }
1973 /* '*' - visual area */
1974 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1975 {
1976 pos_T *fp;
1977
1978 ++ea.cmd;
1979 if (!ea.skip)
1980 {
1981 fp = getmark('<', FALSE);
1982 if (check_mark(fp) == FAIL)
1983 goto doend;
1984 ea.line1 = fp->lnum;
1985 fp = getmark('>', FALSE);
1986 if (check_mark(fp) == FAIL)
1987 goto doend;
1988 ea.line2 = fp->lnum;
1989 ++ea.addr_count;
1990 }
1991 }
1992 }
1993 else
1994 ea.line2 = lnum;
1995 ea.addr_count++;
1996
1997 if (*ea.cmd == ';')
1998 {
1999 if (!ea.skip)
2000 curwin->w_cursor.lnum = ea.line2;
2001 }
2002 else if (*ea.cmd != ',')
2003 break;
2004 ++ea.cmd;
2005 }
2006
2007 /* One address given: set start and end lines */
2008 if (ea.addr_count == 1)
2009 {
2010 ea.line1 = ea.line2;
2011 /* ... but only implicit: really no address given */
2012 if (lnum == MAXLNUM)
2013 ea.addr_count = 0;
2014 }
2015
2016 /* Don't leave the cursor on an illegal line (caused by ';') */
2017 check_cursor_lnum();
2018
2019/*
2020 * 4. parse command
2021 */
2022
2023 /*
2024 * Skip ':' and any white space
2025 */
2026 ea.cmd = skipwhite(ea.cmd);
2027 while (*ea.cmd == ':')
2028 ea.cmd = skipwhite(ea.cmd + 1);
2029
2030 /*
2031 * If we got a line, but no command, then go to the line.
2032 * If we find a '|' or '\n' we set ea.nextcmd.
2033 */
2034 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2035 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2036 {
2037 /*
2038 * strange vi behaviour:
2039 * ":3" jumps to line 3
2040 * ":3|..." prints line 3
2041 * ":|" prints current line
2042 */
2043 if (ea.skip) /* skip this if inside :if */
2044 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002045 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
2047 ea.cmdidx = CMD_print;
2048 ea.argt = RANGE+COUNT+TRLBAR;
2049 if ((errormsg = invalid_range(&ea)) == NULL)
2050 {
2051 correct_range(&ea);
2052 ex_print(&ea);
2053 }
2054 }
2055 else if (ea.addr_count != 0)
2056 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002057 if (ea.line2 > curbuf->b_ml.ml_line_count)
2058 {
2059 /* With '-' in 'cpoptions' a line number past the file is an
2060 * error, otherwise put it at the end of the file. */
2061 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2062 ea.line2 = -1;
2063 else
2064 ea.line2 = curbuf->b_ml.ml_line_count;
2065 }
2066
2067 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002068 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
2070 {
2071 if (ea.line2 == 0)
2072 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 else
2074 curwin->w_cursor.lnum = ea.line2;
2075 beginline(BL_SOL | BL_FIX);
2076 }
2077 }
2078 goto doend;
2079 }
2080
2081 /* Find the command and let "p" point to after it. */
2082 p = find_command(&ea, NULL);
2083
2084#ifdef FEAT_USR_CMDS
2085 if (p == NULL)
2086 {
2087 if (!ea.skip)
2088 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2089 goto doend;
2090 }
2091 /* Check for wrong commands. */
2092 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2093 {
2094 errormsg = uc_fun_cmd();
2095 goto doend;
2096 }
2097#endif
2098 if (ea.cmdidx == CMD_SIZE)
2099 {
2100 if (!ea.skip)
2101 {
2102 STRCPY(IObuff, _("E492: Not an editor command"));
2103 if (!sourcing)
2104 {
2105 STRCAT(IObuff, ": ");
2106 STRNCAT(IObuff, *cmdlinep, 40);
2107 }
2108 errormsg = IObuff;
2109 }
2110 goto doend;
2111 }
2112
2113 ni = (
2114#ifdef FEAT_USR_CMDS
2115 !USER_CMDIDX(ea.cmdidx) &&
2116#endif
2117 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2118
2119#ifndef FEAT_EVAL
2120 /*
2121 * When the expression evaluation is disabled, recognize the ":if" and
2122 * ":endif" commands and ignore everything in between it.
2123 */
2124 if (ea.cmdidx == CMD_if)
2125 ++if_level;
2126 if (if_level)
2127 {
2128 if (ea.cmdidx == CMD_endif)
2129 --if_level;
2130 goto doend;
2131 }
2132
2133#endif
2134
2135 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2136 {
2137 ++p;
2138 ea.forceit = TRUE;
2139 }
2140 else
2141 ea.forceit = FALSE;
2142
2143/*
2144 * 5. parse arguments
2145 */
2146#ifdef FEAT_USR_CMDS
2147 if (!USER_CMDIDX(ea.cmdidx))
2148#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002149 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
2151 if (!ea.skip)
2152 {
2153#ifdef HAVE_SANDBOX
2154 if (sandbox != 0 && !(ea.argt & SBOXOK))
2155 {
2156 /* Command not allowed in sandbox. */
2157 errormsg = (char_u *)_(e_sandbox);
2158 goto doend;
2159 }
2160#endif
2161 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2162 {
2163 /* Command not allowed in non-'modifiable' buffer */
2164 errormsg = (char_u *)_(e_modifiable);
2165 goto doend;
2166 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002167
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002168 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169# ifdef FEAT_USR_CMDS
2170 && !USER_CMDIDX(ea.cmdidx)
2171# endif
2172 )
2173 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002174 /* Command not allowed when editing the command line. */
2175#ifdef FEAT_CMDWIN
2176 if (cmdwin_type != 0)
2177 errormsg = (char_u *)_(e_cmdwin);
2178 else
2179#endif
2180 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 goto doend;
2182 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002183#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002184 /* Disallow editing another buffer when "curbuf_lock" is set.
2185 * Do allow ":edit" (check for argument later).
2186 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002187 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002188 && ea.cmdidx != CMD_edit
2189 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002190# ifdef FEAT_USR_CMDS
2191 && !USER_CMDIDX(ea.cmdidx)
2192# endif
2193 && curbuf_locked())
2194 goto doend;
2195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2198 {
2199 /* no range allowed */
2200 errormsg = (char_u *)_(e_norange);
2201 goto doend;
2202 }
2203 }
2204
2205 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2206 {
2207 errormsg = (char_u *)_(e_nobang);
2208 goto doend;
2209 }
2210
2211 /*
2212 * Don't complain about the range if it is not used
2213 * (could happen if line_count is accidentally set to 0).
2214 */
2215 if (!ea.skip && !ni)
2216 {
2217 /*
2218 * If the range is backwards, ask for confirmation and, if given, swap
2219 * ea.line1 & ea.line2 so it's forwards again.
2220 * When global command is busy, don't ask, will fail below.
2221 */
2222 if (!global_busy && ea.line1 > ea.line2)
2223 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002224 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002226 if (sourcing || exmode_active)
2227 {
2228 errormsg = (char_u *)_("E493: Backwards range given");
2229 goto doend;
2230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 if (ask_yesno((char_u *)
2232 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002233 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
2235 lnum = ea.line1;
2236 ea.line1 = ea.line2;
2237 ea.line2 = lnum;
2238 }
2239 if ((errormsg = invalid_range(&ea)) != NULL)
2240 goto doend;
2241 }
2242
2243 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2244 ea.line2 = 1;
2245
2246 correct_range(&ea);
2247
2248#ifdef FEAT_FOLDING
2249 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2250 {
2251 /* Put the first line at the start of a closed fold, put the last line
2252 * at the end of a closed fold. */
2253 (void)hasFolding(ea.line1, &ea.line1, NULL);
2254 (void)hasFolding(ea.line2, NULL, &ea.line2);
2255 }
2256#endif
2257
2258#ifdef FEAT_QUICKFIX
2259 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002260 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 * option here, so things like % get expanded.
2262 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002263 p = replace_makeprg(&ea, p, cmdlinep);
2264 if (p == NULL)
2265 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266#endif
2267
2268 /*
2269 * Skip to start of argument.
2270 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2271 */
2272 if (ea.cmdidx == CMD_bang)
2273 ea.arg = p;
2274 else
2275 ea.arg = skipwhite(p);
2276
2277 /*
2278 * Check for "++opt=val" argument.
2279 * Must be first, allow ":w ++enc=utf8 !cmd"
2280 */
2281 if (ea.argt & ARGOPT)
2282 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2283 if (getargopt(&ea) == FAIL && !ni)
2284 {
2285 errormsg = (char_u *)_(e_invarg);
2286 goto doend;
2287 }
2288
2289 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2290 {
2291 if (*ea.arg == '>') /* append */
2292 {
2293 if (*++ea.arg != '>') /* typed wrong */
2294 {
2295 errormsg = (char_u *)_("E494: Use w or w>>");
2296 goto doend;
2297 }
2298 ea.arg = skipwhite(ea.arg + 1);
2299 ea.append = TRUE;
2300 }
2301 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2302 {
2303 ++ea.arg;
2304 ea.usefilter = TRUE;
2305 }
2306 }
2307
2308 if (ea.cmdidx == CMD_read)
2309 {
2310 if (ea.forceit)
2311 {
2312 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2313 ea.forceit = FALSE;
2314 }
2315 else if (*ea.arg == '!') /* :r !filter */
2316 {
2317 ++ea.arg;
2318 ea.usefilter = TRUE;
2319 }
2320 }
2321
2322 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2323 {
2324 ea.amount = 1;
2325 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2326 {
2327 ++ea.arg;
2328 ++ea.amount;
2329 }
2330 ea.arg = skipwhite(ea.arg);
2331 }
2332
2333 /*
2334 * Check for "+command" argument, before checking for next command.
2335 * Don't do this for ":read !cmd" and ":write !cmd".
2336 */
2337 if ((ea.argt & EDITCMD) && !ea.usefilter)
2338 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2339
2340 /*
2341 * Check for '|' to separate commands and '"' to start comments.
2342 * Don't do this for ":read !cmd" and ":write !cmd".
2343 */
2344 if ((ea.argt & TRLBAR) && !ea.usefilter)
2345 separate_nextcmd(&ea);
2346
2347 /*
2348 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002349 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2350 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002352 else if (ea.cmdidx == CMD_bang
2353 || ea.cmdidx == CMD_global
2354 || ea.cmdidx == CMD_vglobal
2355 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
2357 for (p = ea.arg; *p; ++p)
2358 {
2359 /* Remove one backslash before a newline, so that it's possible to
2360 * pass a newline to the shell and also a newline that is preceded
2361 * with a backslash. This makes it impossible to end a shell
2362 * command in a backslash, but that doesn't appear useful.
2363 * Halving the number of backslashes is incompatible with previous
2364 * versions. */
2365 if (*p == '\\' && p[1] == '\n')
2366 mch_memmove(p, p + 1, STRLEN(p));
2367 else if (*p == '\n')
2368 {
2369 ea.nextcmd = p + 1;
2370 *p = NUL;
2371 break;
2372 }
2373 }
2374 }
2375
2376 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2377 {
2378 ea.line1 = 1;
2379 ea.line2 = curbuf->b_ml.ml_line_count;
2380 }
2381
2382 /* accept numbered register only when no count allowed (:put) */
2383 if ( (ea.argt & REGSTR)
2384 && *ea.arg != NUL
2385#ifdef FEAT_USR_CMDS
2386 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2387 && USER_CMDIDX(ea.cmdidx)))
2388 /* Do not allow register = for user commands */
2389 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2390#else
2391 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2392#endif
2393 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2394 {
2395 ea.regname = *ea.arg++;
2396#ifdef FEAT_EVAL
2397 /* for '=' register: accept the rest of the line as an expression */
2398 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2399 {
2400 set_expr_line(vim_strsave(ea.arg));
2401 ea.arg += STRLEN(ea.arg);
2402 }
2403#endif
2404 ea.arg = skipwhite(ea.arg);
2405 }
2406
2407 /*
2408 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2409 * count, it's a buffer name.
2410 */
2411 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2412 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2413 || vim_iswhite(*p)))
2414 {
2415 n = getdigits(&ea.arg);
2416 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002417 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
2419 errormsg = (char_u *)_(e_zerocount);
2420 goto doend;
2421 }
2422 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2423 {
2424 ea.line2 = n;
2425 if (ea.addr_count == 0)
2426 ea.addr_count = 1;
2427 }
2428 else
2429 {
2430 ea.line1 = ea.line2;
2431 ea.line2 += n - 1;
2432 ++ea.addr_count;
2433 /*
2434 * Be vi compatible: no error message for out of range.
2435 */
2436 if (ea.line2 > curbuf->b_ml.ml_line_count)
2437 ea.line2 = curbuf->b_ml.ml_line_count;
2438 }
2439 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002440
2441 /*
2442 * Check for flags: 'l', 'p' and '#'.
2443 */
2444 if (ea.argt & EXFLAGS)
2445 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002447 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002448 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 errormsg = (char_u *)_(e_trailing);
2451 goto doend;
2452 }
2453
2454 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2455 {
2456 errormsg = (char_u *)_(e_argreq);
2457 goto doend;
2458 }
2459
2460#ifdef FEAT_EVAL
2461 /*
2462 * Skip the command when it's not going to be executed.
2463 * The commands like :if, :endif, etc. always need to be executed.
2464 * Also make an exception for commands that handle a trailing command
2465 * themselves.
2466 */
2467 if (ea.skip)
2468 {
2469 switch (ea.cmdidx)
2470 {
2471 /* commands that need evaluation */
2472 case CMD_while:
2473 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002474 case CMD_for:
2475 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 case CMD_if:
2477 case CMD_elseif:
2478 case CMD_else:
2479 case CMD_endif:
2480 case CMD_try:
2481 case CMD_catch:
2482 case CMD_finally:
2483 case CMD_endtry:
2484 case CMD_function:
2485 break;
2486
2487 /* Commands that handle '|' themselves. Check: A command should
2488 * either have the TRLBAR flag, appear in this list or appear in
2489 * the list at ":help :bar". */
2490 case CMD_aboveleft:
2491 case CMD_and:
2492 case CMD_belowright:
2493 case CMD_botright:
2494 case CMD_browse:
2495 case CMD_call:
2496 case CMD_confirm:
2497 case CMD_delfunction:
2498 case CMD_djump:
2499 case CMD_dlist:
2500 case CMD_dsearch:
2501 case CMD_dsplit:
2502 case CMD_echo:
2503 case CMD_echoerr:
2504 case CMD_echomsg:
2505 case CMD_echon:
2506 case CMD_execute:
2507 case CMD_help:
2508 case CMD_hide:
2509 case CMD_ijump:
2510 case CMD_ilist:
2511 case CMD_isearch:
2512 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002513 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 case CMD_keepjumps:
2515 case CMD_keepmarks:
2516 case CMD_leftabove:
2517 case CMD_let:
2518 case CMD_lockmarks:
2519 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002520 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 case CMD_perl:
2522 case CMD_psearch:
2523 case CMD_python:
2524 case CMD_return:
2525 case CMD_rightbelow:
2526 case CMD_ruby:
2527 case CMD_silent:
2528 case CMD_smagic:
2529 case CMD_snomagic:
2530 case CMD_substitute:
2531 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002532 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 case CMD_tcl:
2534 case CMD_throw:
2535 case CMD_tilde:
2536 case CMD_topleft:
2537 case CMD_unlet:
2538 case CMD_verbose:
2539 case CMD_vertical:
2540 break;
2541
2542 default: goto doend;
2543 }
2544 }
2545#endif
2546
2547 if (ea.argt & XFILE)
2548 {
2549 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2550 goto doend;
2551 }
2552
2553#ifdef FEAT_LISTCMDS
2554 /*
2555 * Accept buffer name. Cannot be used at the same time with a buffer
2556 * number. Don't do this for a user command.
2557 */
2558 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2559# ifdef FEAT_USR_CMDS
2560 && !USER_CMDIDX(ea.cmdidx)
2561# endif
2562 )
2563 {
2564 /*
2565 * :bdelete, :bwipeout and :bunload take several arguments, separated
2566 * by spaces: find next space (skipping over escaped characters).
2567 * The others take one argument: ignore trailing spaces.
2568 */
2569 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2570 || ea.cmdidx == CMD_bunload)
2571 p = skiptowhite_esc(ea.arg);
2572 else
2573 {
2574 p = ea.arg + STRLEN(ea.arg);
2575 while (p > ea.arg && vim_iswhite(p[-1]))
2576 --p;
2577 }
2578 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2579 if (ea.line2 < 0) /* failed */
2580 goto doend;
2581 ea.addr_count = 1;
2582 ea.arg = skipwhite(p);
2583 }
2584#endif
2585
2586/*
2587 * 6. switch on command name
2588 *
2589 * The "ea" structure holds the arguments that can be used.
2590 */
2591 ea.cmdlinep = cmdlinep;
2592 ea.getline = getline;
2593 ea.cookie = cookie;
2594#ifdef FEAT_EVAL
2595 ea.cstack = cstack;
2596#endif
2597
2598#ifdef FEAT_USR_CMDS
2599 if (USER_CMDIDX(ea.cmdidx))
2600 {
2601 /*
2602 * Execute a user-defined command.
2603 */
2604 do_ucmd(&ea);
2605 }
2606 else
2607#endif
2608 {
2609 /*
2610 * Call the function to execute the command.
2611 */
2612 ea.errmsg = NULL;
2613 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2614 if (ea.errmsg != NULL)
2615 errormsg = (char_u *)_(ea.errmsg);
2616 }
2617
2618#ifdef FEAT_EVAL
2619 /*
2620 * If the command just executed called do_cmdline(), any throw or ":return"
2621 * or ":finish" encountered there must also check the cstack of the still
2622 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2623 * exception, or reanimate a returned function or finished script file and
2624 * return or finish it again.
2625 */
2626 if (need_rethrow)
2627 do_throw(cstack);
2628 else if (check_cstack)
2629 {
2630 if (source_finished(getline, cookie))
2631 do_finish(&ea, TRUE);
2632 else if (getline_equal(getline, cookie, get_func_line)
2633 && current_func_returned())
2634 do_return(&ea, TRUE, FALSE, NULL);
2635 }
2636 need_rethrow = check_cstack = FALSE;
2637#endif
2638
2639doend:
2640 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2641 curwin->w_cursor.lnum = 1;
2642
2643 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2644 {
2645 if (sourcing)
2646 {
2647 if (errormsg != IObuff)
2648 {
2649 STRCPY(IObuff, errormsg);
2650 errormsg = IObuff;
2651 }
2652 STRCAT(errormsg, ": ");
2653 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2654 }
2655 emsg(errormsg);
2656 }
2657#ifdef FEAT_EVAL
2658 do_errthrow(cstack,
2659 (ea.cmdidx != CMD_SIZE
2660# ifdef FEAT_USR_CMDS
2661 && !USER_CMDIDX(ea.cmdidx)
2662# endif
2663 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2664#endif
2665
2666 if (verbose_save >= 0)
2667 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002668#ifdef FEAT_AUTOCMD
2669 if (cmdmod.save_ei != NULL)
2670 {
2671 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002672 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2673 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002674 free_string_option(cmdmod.save_ei);
2675 }
2676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
2678 cmdmod = save_cmdmod;
2679
2680 if (did_silent > 0)
2681 {
2682 /* messages could be enabled for a serious error, need to check if the
2683 * counters don't become negative */
2684 msg_silent -= did_silent;
2685 if (msg_silent < 0)
2686 msg_silent = 0;
2687 emsg_silent -= did_esilent;
2688 if (emsg_silent < 0)
2689 emsg_silent = 0;
2690 /* Restore msg_scroll, it's set by file I/O commands, even when no
2691 * message is actually displayed. */
2692 msg_scroll = save_msg_scroll;
2693 }
2694
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002695#ifdef HAVE_SANDBOX
2696 if (did_sandbox)
2697 --sandbox;
2698#endif
2699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2701 ea.nextcmd = NULL;
2702
2703#ifdef FEAT_EVAL
2704 --ex_nesting_level;
2705#endif
2706
2707 return ea.nextcmd;
2708}
2709#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002710 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711#endif
2712
2713/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002714 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 * If there is a match advance "pp" to the argument and return TRUE.
2716 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002717 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002719 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 char *cmd; /* name of command */
2721 int len; /* required length */
2722{
2723 int i;
2724
2725 for (i = 0; cmd[i] != NUL; ++i)
2726 if (cmd[i] != (*pp)[i])
2727 break;
2728 if (i >= len && !isalpha((*pp)[i]))
2729 {
2730 *pp = skipwhite(*pp + i);
2731 return TRUE;
2732 }
2733 return FALSE;
2734}
2735
2736/*
2737 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002738 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002740 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 * Returns NULL for an ambiguous user command.
2742 */
2743/*ARGSUSED*/
2744 static char_u *
2745find_command(eap, full)
2746 exarg_T *eap;
2747 int *full;
2748{
2749 int len;
2750 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002751 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753 /*
2754 * Isolate the command and search for it in the command table.
2755 * Exeptions:
2756 * - the 'k' command can directly be followed by any character.
2757 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2758 * but :sre[wind] is another command, as are :scrip[tnames],
2759 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002760 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 */
2762 p = eap->cmd;
2763 if (*p == 'k')
2764 {
2765 eap->cmdidx = CMD_k;
2766 ++p;
2767 }
2768 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002769 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2770 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 || p[1] == 'g'
2772 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2773 || p[1] == 'I'
2774 || (p[1] == 'r' && p[2] != 'e')))
2775 {
2776 eap->cmdidx = CMD_substitute;
2777 ++p;
2778 }
2779 else
2780 {
2781 while (ASCII_ISALPHA(*p))
2782 ++p;
2783 /* check for non-alpha command */
2784 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2785 ++p;
2786 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002787 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2788 {
2789 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2790 * :delete with the 'l' flag. Same for 'p'. */
2791 for (i = 0; i < len; ++i)
2792 if (eap->cmd[i] != "delete"[i])
2793 break;
2794 if (i == len - 1)
2795 {
2796 --len;
2797 if (p[-1] == 'l')
2798 eap->flags |= EXFLAG_LIST;
2799 else
2800 eap->flags |= EXFLAG_PRINT;
2801 }
2802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
2804 if (ASCII_ISLOWER(*eap->cmd))
2805 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2806 else
2807 eap->cmdidx = cmdidxs[26];
2808
2809 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2810 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2811 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2812 (size_t)len) == 0)
2813 {
2814#ifdef FEAT_EVAL
2815 if (full != NULL
2816 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2817 *full = TRUE;
2818#endif
2819 break;
2820 }
2821
2822#ifdef FEAT_USR_CMDS
2823 /* Look for a user defined command as a last resort */
2824 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2825 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002826 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 while (ASCII_ISALNUM(*p))
2828 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002829 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 }
2831#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002832 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 eap->cmdidx = CMD_SIZE;
2834 }
2835
2836 return p;
2837}
2838
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002839#ifdef FEAT_USR_CMDS
2840/*
2841 * Search for a user command that matches "eap->cmd".
2842 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2843 * Return a pointer to just after the command.
2844 * Return NULL if there is no matching command.
2845 */
2846 static char_u *
2847find_ucmd(eap, p, full, xp, compl)
2848 exarg_T *eap;
2849 char_u *p; /* end of the command (possibly including count) */
2850 int *full; /* set to TRUE for a full match */
2851 expand_T *xp; /* used for completion, NULL otherwise */
2852 int *compl; /* completion flags or NULL */
2853{
2854 int len = (int)(p - eap->cmd);
2855 int j, k, matchlen = 0;
2856 ucmd_T *uc;
2857 int found = FALSE;
2858 int possible = FALSE;
2859 char_u *cp, *np; /* Point into typed cmd and test name */
2860 garray_T *gap;
2861 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2862 only full match global is accepted. */
2863
2864 /*
2865 * Look for buffer-local user commands first, then global ones.
2866 */
2867 gap = &curbuf->b_ucmds;
2868 for (;;)
2869 {
2870 for (j = 0; j < gap->ga_len; ++j)
2871 {
2872 uc = USER_CMD_GA(gap, j);
2873 cp = eap->cmd;
2874 np = uc->uc_name;
2875 k = 0;
2876 while (k < len && *np != NUL && *cp++ == *np++)
2877 k++;
2878 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2879 {
2880 /* If finding a second match, the command is ambiguous. But
2881 * not if a buffer-local command wasn't a full match and a
2882 * global command is a full match. */
2883 if (k == len && found && *np != NUL)
2884 {
2885 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002886 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002887 amb_local = TRUE;
2888 }
2889
2890 if (!found || (k == len && *np == NUL))
2891 {
2892 /* If we matched up to a digit, then there could
2893 * be another command including the digit that we
2894 * should use instead.
2895 */
2896 if (k == len)
2897 found = TRUE;
2898 else
2899 possible = TRUE;
2900
2901 if (gap == &ucmds)
2902 eap->cmdidx = CMD_USER;
2903 else
2904 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002905 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002906 eap->useridx = j;
2907
2908# ifdef FEAT_CMDL_COMPL
2909 if (compl != NULL)
2910 *compl = uc->uc_compl;
2911# ifdef FEAT_EVAL
2912 if (xp != NULL)
2913 {
2914 xp->xp_arg = uc->uc_compl_arg;
2915 xp->xp_scriptID = uc->uc_scriptID;
2916 }
2917# endif
2918# endif
2919 /* Do not search for further abbreviations
2920 * if this is an exact match. */
2921 matchlen = k;
2922 if (k == len && *np == NUL)
2923 {
2924 if (full != NULL)
2925 *full = TRUE;
2926 amb_local = FALSE;
2927 break;
2928 }
2929 }
2930 }
2931 }
2932
2933 /* Stop if we found a full match or searched all. */
2934 if (j < gap->ga_len || gap == &ucmds)
2935 break;
2936 gap = &ucmds;
2937 }
2938
2939 /* Only found ambiguous matches. */
2940 if (amb_local)
2941 {
2942 if (xp != NULL)
2943 xp->xp_context = EXPAND_UNSUCCESSFUL;
2944 return NULL;
2945 }
2946
2947 /* The match we found may be followed immediately by a number. Move "p"
2948 * back to point to it. */
2949 if (found || possible)
2950 return p + (matchlen - len);
2951 return p;
2952}
2953#endif
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955#if defined(FEAT_EVAL) || defined(PROTO)
2956/*
2957 * Return > 0 if an Ex command "name" exists.
2958 * Return 2 if there is an exact match.
2959 * Return 3 if there is an ambiguous match.
2960 */
2961 int
2962cmd_exists(name)
2963 char_u *name;
2964{
2965 exarg_T ea;
2966 int full = FALSE;
2967 int i;
2968 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00002969 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 static struct cmdmod
2971 {
2972 char *name;
2973 int minlen;
2974 } cmdmods[] = {
2975 {"aboveleft", 3},
2976 {"belowright", 3},
2977 {"botright", 2},
2978 {"browse", 3},
2979 {"confirm", 4},
2980 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002981 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {"keepjumps", 5},
2983 {"keepmarks", 3},
2984 {"leftabove", 5},
2985 {"lockmarks", 3},
2986 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002987 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {"silent", 3},
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002989 {"tab", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 {"topleft", 2},
2991 {"verbose", 4},
2992 {"vertical", 4},
2993 };
2994
2995 /* Check command modifiers. */
2996 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2997 {
2998 for (j = 0; name[j] != NUL; ++j)
2999 if (name[j] != cmdmods[i].name[j])
3000 break;
3001 if (name[j] == NUL && j >= cmdmods[i].minlen)
3002 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3003 }
3004
Bram Moolenaara9587612006-05-04 21:47:50 +00003005 /* Check built-in commands and user defined commands.
3006 * For ":2match" and ":3match" we need to skip the number. */
3007 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003009 p = find_command(&ea, &full);
3010 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003012 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3013 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003014 if (*skipwhite(p) != NUL)
3015 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3017}
3018#endif
3019
3020/*
3021 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3022 * we don't need/want deleted. Maybe this could be done better if we didn't
3023 * repeat all this stuff. The only problem is that they may not stay
3024 * perfectly compatible with each other, but then the command line syntax
3025 * probably won't change that much -- webb.
3026 */
3027 char_u *
3028set_one_cmd_context(xp, buff)
3029 expand_T *xp;
3030 char_u *buff; /* buffer for command string */
3031{
3032 char_u *p;
3033 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003034 int len = 0;
3035 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3037 int compl = EXPAND_NOTHING;
3038#endif
3039#ifdef FEAT_CMDL_COMPL
3040 int delim;
3041#endif
3042 int forceit = FALSE;
3043 int usefilter = FALSE; /* filter instead of file name */
3044
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003045 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 xp->xp_pattern = buff;
3047 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003048 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
3050/*
3051 * 2. skip comment lines and leading space, colons or bars
3052 */
3053 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3054 ;
3055 xp->xp_pattern = cmd;
3056
3057 if (*cmd == NUL)
3058 return NULL;
3059 if (*cmd == '"') /* ignore comment lines */
3060 {
3061 xp->xp_context = EXPAND_NOTHING;
3062 return NULL;
3063 }
3064
3065/*
3066 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3067 */
3068 cmd = skip_range(cmd, &xp->xp_context);
3069
3070/*
3071 * 4. parse command
3072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 xp->xp_pattern = cmd;
3074 if (*cmd == NUL)
3075 return NULL;
3076 if (*cmd == '"')
3077 {
3078 xp->xp_context = EXPAND_NOTHING;
3079 return NULL;
3080 }
3081
3082 if (*cmd == '|' || *cmd == '\n')
3083 return cmd + 1; /* There's another command */
3084
3085 /*
3086 * Isolate the command and search for it in the command table.
3087 * Exceptions:
3088 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003089 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3091 */
3092 if (*cmd == 'k' && cmd[1] != 'e')
3093 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003094 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 p = cmd + 1;
3096 }
3097 else
3098 {
3099 p = cmd;
3100 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3101 ++p;
3102 /* check for non-alpha command */
3103 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3104 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003105 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003107 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
3109 xp->xp_context = EXPAND_UNSUCCESSFUL;
3110 return NULL;
3111 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003112 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3113 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3114 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 break;
3116
3117#ifdef FEAT_USR_CMDS
3118 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3119 {
3120 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3121 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003122 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
3124#endif
3125 }
3126
3127 /*
3128 * If the cursor is touching the command, and it ends in an alpha-numeric
3129 * character, complete the command name.
3130 */
3131 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3132 return NULL;
3133
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003134 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3137 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003138 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 p = cmd + 1;
3140 }
3141#ifdef FEAT_USR_CMDS
3142 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3143 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003144 ea.cmd = cmd;
3145 p = find_ucmd(&ea, p, NULL, xp,
3146# if defined(FEAT_CMDL_COMPL)
3147 &compl
3148# else
3149 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003151 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003152 if (p == NULL)
3153 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
3155#endif
3156 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003157 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
3159 /* Not still touching the command and it was an illegal one */
3160 xp->xp_context = EXPAND_UNSUCCESSFUL;
3161 return NULL;
3162 }
3163
3164 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3165
3166 if (*p == '!') /* forced commands */
3167 {
3168 forceit = TRUE;
3169 ++p;
3170 }
3171
3172/*
3173 * 5. parse arguments
3174 */
3175#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003176 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003178 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
3180 arg = skipwhite(p);
3181
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003182 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 if (*arg == '>') /* append */
3185 {
3186 if (*++arg == '>')
3187 ++arg;
3188 arg = skipwhite(arg);
3189 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
3192 ++arg;
3193 usefilter = TRUE;
3194 }
3195 }
3196
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003197 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
3199 usefilter = forceit; /* :r! filter if forced */
3200 if (*arg == '!') /* :r !filter */
3201 {
3202 ++arg;
3203 usefilter = TRUE;
3204 }
3205 }
3206
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 {
3209 while (*arg == *cmd) /* allow any number of '>' or '<' */
3210 ++arg;
3211 arg = skipwhite(arg);
3212 }
3213
3214 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003215 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
3217 /* Check if we're in the +command */
3218 p = arg + 1;
3219 arg = skip_cmd_arg(arg, FALSE);
3220
3221 /* Still touching the command after '+'? */
3222 if (*arg == NUL)
3223 return p;
3224
3225 /* Skip space(s) after +command to get to the real argument */
3226 arg = skipwhite(arg);
3227 }
3228
3229 /*
3230 * Check for '|' to separate commands and '"' to start comments.
3231 * Don't do this for ":read !cmd" and ":write !cmd".
3232 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
3235 p = arg;
3236 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 p += 2;
3239 while (*p)
3240 {
3241 if (*p == Ctrl_V)
3242 {
3243 if (p[1] != NUL)
3244 ++p;
3245 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003246 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 || *p == '|' || *p == '\n')
3248 {
3249 if (*(p - 1) != '\\')
3250 {
3251 if (*p == '|' || *p == '\n')
3252 return p + 1;
3253 return NULL; /* It's a comment */
3254 }
3255 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003256 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258 }
3259
3260 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003261 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 vim_strchr((char_u *)"|\"", *arg) == NULL)
3263 return NULL;
3264
3265 /* Find start of last argument (argument just before cursor): */
3266 p = buff + STRLEN(buff);
3267 while (p != arg && *p != ' ' && *p != TAB)
3268 p--;
3269 if (*p == ' ' || *p == TAB)
3270 p++;
3271 xp->xp_pattern = p;
3272
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003273 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 {
3275 int in_quote = FALSE;
3276 char_u *bow = NULL; /* Beginning of word */
3277
3278 /*
3279 * Allow spaces within back-quotes to count as part of the argument
3280 * being expanded.
3281 */
3282 xp->xp_pattern = skipwhite(arg);
3283 for (p = xp->xp_pattern; *p; )
3284 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003285 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 ++p;
3287#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003288 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289#else
3290 else if (vim_iswhite(*p))
3291#endif
3292 {
3293 p = skipwhite(p);
3294 if (in_quote)
3295 bow = p;
3296 else
3297 xp->xp_pattern = p;
3298 --p;
3299 }
3300 else if (*p == '`')
3301 {
3302 if (!in_quote)
3303 {
3304 xp->xp_pattern = p;
3305 bow = p + 1;
3306 }
3307 in_quote = !in_quote;
3308 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003309 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
3311
3312 /*
3313 * If we are still inside the quotes, and we passed a space, just
3314 * expand from there.
3315 */
3316 if (bow != NULL && in_quote)
3317 xp->xp_pattern = bow;
3318 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003319
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003320#ifndef BACKSLASH_IN_FILENAME
3321 /* For a shell command more chars need to be escaped. */
3322 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003323 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003324 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003325
3326 /* When still after the command name expand executables. */
3327 if (xp->xp_pattern == skipwhite(arg))
3328 xp->xp_context = EXPAND_SHELLCMD;
3329 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
3332 /* Check for environment variable */
3333 if (*xp->xp_pattern == '$'
3334#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3335 || *xp->xp_pattern == '%'
3336#endif
3337 )
3338 {
3339 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3340 if (!vim_isIDc(*p))
3341 break;
3342 if (*p == NUL)
3343 {
3344 xp->xp_context = EXPAND_ENV_VARS;
3345 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003346#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3347 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003348 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003349 compl = EXPAND_ENV_VARS;
3350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 }
3352 }
3353 }
3354
3355/*
3356 * 6. switch on command name
3357 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003358 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 case CMD_cd:
3361 case CMD_chdir:
3362 case CMD_lcd:
3363 case CMD_lchdir:
3364 if (xp->xp_context == EXPAND_FILES)
3365 xp->xp_context = EXPAND_DIRECTORIES;
3366 break;
3367 case CMD_help:
3368 xp->xp_context = EXPAND_HELP;
3369 xp->xp_pattern = arg;
3370 break;
3371
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003372 /* Command modifiers: return the argument.
3373 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003375 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 case CMD_belowright:
3377 case CMD_botright:
3378 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003379 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003381 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 case CMD_folddoclosed:
3383 case CMD_folddoopen:
3384 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003385 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 case CMD_keepjumps:
3387 case CMD_keepmarks:
3388 case CMD_leftabove:
3389 case CMD_lockmarks:
3390 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003391 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003393 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 case CMD_topleft:
3395 case CMD_verbose:
3396 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003397 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 return arg;
3399
3400#ifdef FEAT_SEARCH_EXTRA
3401 case CMD_match:
3402 if (*arg == NUL || !ends_excmd(*arg))
3403 {
3404 /* Dummy call to clear variables. */
3405 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3406 xp->xp_context = EXPAND_HIGHLIGHT;
3407 xp->xp_pattern = arg;
3408 arg = skipwhite(skiptowhite(arg));
3409 if (*arg != NUL)
3410 {
3411 xp->xp_context = EXPAND_NOTHING;
3412 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3413 }
3414 }
3415 return find_nextcmd(arg);
3416#endif
3417
3418#ifdef FEAT_CMDL_COMPL
3419/*
3420 * All completion for the +cmdline_compl feature goes here.
3421 */
3422
3423# ifdef FEAT_USR_CMDS
3424 case CMD_command:
3425 /* Check for attributes */
3426 while (*arg == '-')
3427 {
3428 arg++; /* Skip "-" */
3429 p = skiptowhite(arg);
3430 if (*p == NUL)
3431 {
3432 /* Cursor is still in the attribute */
3433 p = vim_strchr(arg, '=');
3434 if (p == NULL)
3435 {
3436 /* No "=", so complete attribute names */
3437 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3438 xp->xp_pattern = arg;
3439 return NULL;
3440 }
3441
3442 /* For the -complete and -nargs attributes, we complete
3443 * their arguments as well.
3444 */
3445 if (STRNICMP(arg, "complete", p - arg) == 0)
3446 {
3447 xp->xp_context = EXPAND_USER_COMPLETE;
3448 xp->xp_pattern = p + 1;
3449 return NULL;
3450 }
3451 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3452 {
3453 xp->xp_context = EXPAND_USER_NARGS;
3454 xp->xp_pattern = p + 1;
3455 return NULL;
3456 }
3457 return NULL;
3458 }
3459 arg = skipwhite(p);
3460 }
3461
3462 /* After the attributes comes the new command name */
3463 p = skiptowhite(arg);
3464 if (*p == NUL)
3465 {
3466 xp->xp_context = EXPAND_USER_COMMANDS;
3467 xp->xp_pattern = arg;
3468 break;
3469 }
3470
3471 /* And finally comes a normal command */
3472 return skipwhite(p);
3473
3474 case CMD_delcommand:
3475 xp->xp_context = EXPAND_USER_COMMANDS;
3476 xp->xp_pattern = arg;
3477 break;
3478# endif
3479
3480 case CMD_global:
3481 case CMD_vglobal:
3482 delim = *arg; /* get the delimiter */
3483 if (delim)
3484 ++arg; /* skip delimiter if there is one */
3485
3486 while (arg[0] != NUL && arg[0] != delim)
3487 {
3488 if (arg[0] == '\\' && arg[1] != NUL)
3489 ++arg;
3490 ++arg;
3491 }
3492 if (arg[0] != NUL)
3493 return arg + 1;
3494 break;
3495 case CMD_and:
3496 case CMD_substitute:
3497 delim = *arg;
3498 if (delim)
3499 {
3500 /* skip "from" part */
3501 ++arg;
3502 arg = skip_regexp(arg, delim, p_magic, NULL);
3503 }
3504 /* skip "to" part */
3505 while (arg[0] != NUL && arg[0] != delim)
3506 {
3507 if (arg[0] == '\\' && arg[1] != NUL)
3508 ++arg;
3509 ++arg;
3510 }
3511 if (arg[0] != NUL) /* skip delimiter */
3512 ++arg;
3513 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3514 ++arg;
3515 if (arg[0] != NUL)
3516 return arg;
3517 break;
3518 case CMD_isearch:
3519 case CMD_dsearch:
3520 case CMD_ilist:
3521 case CMD_dlist:
3522 case CMD_ijump:
3523 case CMD_psearch:
3524 case CMD_djump:
3525 case CMD_isplit:
3526 case CMD_dsplit:
3527 arg = skipwhite(skipdigits(arg)); /* skip count */
3528 if (*arg == '/') /* Match regexp, not just whole words */
3529 {
3530 for (++arg; *arg && *arg != '/'; arg++)
3531 if (*arg == '\\' && arg[1] != NUL)
3532 arg++;
3533 if (*arg)
3534 {
3535 arg = skipwhite(arg + 1);
3536
3537 /* Check for trailing illegal characters */
3538 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3539 xp->xp_context = EXPAND_NOTHING;
3540 else
3541 return arg;
3542 }
3543 }
3544 break;
3545#ifdef FEAT_AUTOCMD
3546 case CMD_autocmd:
3547 return set_context_in_autocmd(xp, arg, FALSE);
3548
3549 case CMD_doautocmd:
3550 return set_context_in_autocmd(xp, arg, TRUE);
3551#endif
3552 case CMD_set:
3553 set_context_in_set_cmd(xp, arg, 0);
3554 break;
3555 case CMD_setglobal:
3556 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3557 break;
3558 case CMD_setlocal:
3559 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3560 break;
3561 case CMD_tag:
3562 case CMD_stag:
3563 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003564 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 case CMD_tselect:
3566 case CMD_stselect:
3567 case CMD_ptselect:
3568 case CMD_tjump:
3569 case CMD_stjump:
3570 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003571 if (*p_wop != NUL)
3572 xp->xp_context = EXPAND_TAGS_LISTFILES;
3573 else
3574 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 xp->xp_pattern = arg;
3576 break;
3577 case CMD_augroup:
3578 xp->xp_context = EXPAND_AUGROUP;
3579 xp->xp_pattern = arg;
3580 break;
3581#ifdef FEAT_SYN_HL
3582 case CMD_syntax:
3583 set_context_in_syntax_cmd(xp, arg);
3584 break;
3585#endif
3586#ifdef FEAT_EVAL
3587 case CMD_let:
3588 case CMD_if:
3589 case CMD_elseif:
3590 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003591 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 case CMD_echo:
3593 case CMD_echon:
3594 case CMD_execute:
3595 case CMD_echomsg:
3596 case CMD_echoerr:
3597 case CMD_call:
3598 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 break;
3601
3602 case CMD_unlet:
3603 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3604 arg = xp->xp_pattern + 1;
3605 xp->xp_context = EXPAND_USER_VARS;
3606 xp->xp_pattern = arg;
3607 break;
3608
3609 case CMD_function:
3610 case CMD_delfunction:
3611 xp->xp_context = EXPAND_USER_FUNC;
3612 xp->xp_pattern = arg;
3613 break;
3614
3615 case CMD_echohl:
3616 xp->xp_context = EXPAND_HIGHLIGHT;
3617 xp->xp_pattern = arg;
3618 break;
3619#endif
3620 case CMD_highlight:
3621 set_context_in_highlight_cmd(xp, arg);
3622 break;
3623#ifdef FEAT_LISTCMDS
3624 case CMD_bdelete:
3625 case CMD_bwipeout:
3626 case CMD_bunload:
3627 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3628 arg = xp->xp_pattern + 1;
3629 /*FALLTHROUGH*/
3630 case CMD_buffer:
3631 case CMD_sbuffer:
3632 case CMD_checktime:
3633 xp->xp_context = EXPAND_BUFFERS;
3634 xp->xp_pattern = arg;
3635 break;
3636#endif
3637#ifdef FEAT_USR_CMDS
3638 case CMD_USER:
3639 case CMD_USER_BUF:
3640 if (compl != EXPAND_NOTHING)
3641 {
3642 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645# ifdef FEAT_MENU
3646 if (compl == EXPAND_MENUS)
3647 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3648# endif
3649 if (compl == EXPAND_COMMANDS)
3650 return arg;
3651 if (compl == EXPAND_MAPPINGS)
3652 return set_context_in_map_cmd(xp, (char_u *)"map",
3653 arg, forceit, FALSE, FALSE, CMD_map);
3654 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3655 arg = xp->xp_pattern + 1;
3656 xp->xp_pattern = arg;
3657 }
3658 xp->xp_context = compl;
3659 }
3660 break;
3661#endif
3662 case CMD_map: case CMD_noremap:
3663 case CMD_nmap: case CMD_nnoremap:
3664 case CMD_vmap: case CMD_vnoremap:
3665 case CMD_omap: case CMD_onoremap:
3666 case CMD_imap: case CMD_inoremap:
3667 case CMD_cmap: case CMD_cnoremap:
3668 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003669 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 case CMD_unmap:
3671 case CMD_nunmap:
3672 case CMD_vunmap:
3673 case CMD_ounmap:
3674 case CMD_iunmap:
3675 case CMD_cunmap:
3676 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003677 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 case CMD_abbreviate: case CMD_noreabbrev:
3679 case CMD_cabbrev: case CMD_cnoreabbrev:
3680 case CMD_iabbrev: case CMD_inoreabbrev:
3681 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003682 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 case CMD_unabbreviate:
3684 case CMD_cunabbrev:
3685 case CMD_iunabbrev:
3686 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003687 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688#ifdef FEAT_MENU
3689 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3690 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3691 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3692 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3693 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3694 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3695 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3696 case CMD_tmenu: case CMD_tunmenu:
3697 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3698 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3699#endif
3700
3701 case CMD_colorscheme:
3702 xp->xp_context = EXPAND_COLORS;
3703 xp->xp_pattern = arg;
3704 break;
3705
3706 case CMD_compiler:
3707 xp->xp_context = EXPAND_COMPILER;
3708 xp->xp_pattern = arg;
3709 break;
3710
3711#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3712 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3713 case CMD_language:
3714 if (*skiptowhite(arg) == NUL)
3715 {
3716 xp->xp_context = EXPAND_LANGUAGE;
3717 xp->xp_pattern = arg;
3718 }
3719 else
3720 xp->xp_context = EXPAND_NOTHING;
3721 break;
3722#endif
3723
3724#endif /* FEAT_CMDL_COMPL */
3725
3726 default:
3727 break;
3728 }
3729 return NULL;
3730}
3731
3732/*
3733 * skip a range specifier of the form: addr [,addr] [;addr] ..
3734 *
3735 * Backslashed delimiters after / or ? will be skipped, and commands will
3736 * not be expanded between /'s and ?'s or after "'".
3737 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003738 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 * Returns the "cmd" pointer advanced to beyond the range.
3740 */
3741 char_u *
3742skip_range(cmd, ctx)
3743 char_u *cmd;
3744 int *ctx; /* pointer to xp_context or NULL */
3745{
3746 int delim;
3747
Bram Moolenaardf177f62005-02-22 08:39:57 +00003748 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
3750 if (*cmd == '\'')
3751 {
3752 if (*++cmd == NUL && ctx != NULL)
3753 *ctx = EXPAND_NOTHING;
3754 }
3755 else if (*cmd == '/' || *cmd == '?')
3756 {
3757 delim = *cmd++;
3758 while (*cmd != NUL && *cmd != delim)
3759 if (*cmd++ == '\\' && *cmd != NUL)
3760 ++cmd;
3761 if (*cmd == NUL && ctx != NULL)
3762 *ctx = EXPAND_NOTHING;
3763 }
3764 if (*cmd != NUL)
3765 ++cmd;
3766 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003767
3768 /* Skip ":" and white space. */
3769 while (*cmd == ':')
3770 cmd = skipwhite(cmd + 1);
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return cmd;
3773}
3774
3775/*
3776 * get a single EX address
3777 *
3778 * Set ptr to the next character after the part that was interpreted.
3779 * Set ptr to NULL when an error is encountered.
3780 *
3781 * Return MAXLNUM when no Ex address was found.
3782 */
3783 static linenr_T
3784get_address(ptr, skip, to_other_file)
3785 char_u **ptr;
3786 int skip; /* only skip the address, don't use it */
3787 int to_other_file; /* flag: may jump to other file */
3788{
3789 int c;
3790 int i;
3791 long n;
3792 char_u *cmd;
3793 pos_T pos;
3794 pos_T *fp;
3795 linenr_T lnum;
3796
3797 cmd = skipwhite(*ptr);
3798 lnum = MAXLNUM;
3799 do
3800 {
3801 switch (*cmd)
3802 {
3803 case '.': /* '.' - Cursor position */
3804 ++cmd;
3805 lnum = curwin->w_cursor.lnum;
3806 break;
3807
3808 case '$': /* '$' - last line */
3809 ++cmd;
3810 lnum = curbuf->b_ml.ml_line_count;
3811 break;
3812
3813 case '\'': /* ''' - mark */
3814 if (*++cmd == NUL)
3815 {
3816 cmd = NULL;
3817 goto error;
3818 }
3819 if (skip)
3820 ++cmd;
3821 else
3822 {
3823 /* Only accept a mark in another file when it is
3824 * used by itself: ":'M". */
3825 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3826 ++cmd;
3827 if (fp == (pos_T *)-1)
3828 /* Jumped to another file. */
3829 lnum = curwin->w_cursor.lnum;
3830 else
3831 {
3832 if (check_mark(fp) == FAIL)
3833 {
3834 cmd = NULL;
3835 goto error;
3836 }
3837 lnum = fp->lnum;
3838 }
3839 }
3840 break;
3841
3842 case '/':
3843 case '?': /* '/' or '?' - search */
3844 c = *cmd++;
3845 if (skip) /* skip "/pat/" */
3846 {
3847 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3848 if (*cmd == c)
3849 ++cmd;
3850 }
3851 else
3852 {
3853 pos = curwin->w_cursor; /* save curwin->w_cursor */
3854 /*
3855 * When '/' or '?' follows another address, start
3856 * from there.
3857 */
3858 if (lnum != MAXLNUM)
3859 curwin->w_cursor.lnum = lnum;
3860 /*
3861 * Start a forward search at the end of the line.
3862 * Start a backward search at the start of the line.
3863 * This makes sure we never match in the current
3864 * line, and can match anywhere in the
3865 * next/previous line.
3866 */
3867 if (c == '/')
3868 curwin->w_cursor.col = MAXCOL;
3869 else
3870 curwin->w_cursor.col = 0;
3871 searchcmdlen = 0;
3872 if (!do_search(NULL, c, cmd, 1L,
3873 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3874 {
3875 curwin->w_cursor = pos;
3876 cmd = NULL;
3877 goto error;
3878 }
3879 lnum = curwin->w_cursor.lnum;
3880 curwin->w_cursor = pos;
3881 /* adjust command string pointer */
3882 cmd += searchcmdlen;
3883 }
3884 break;
3885
3886 case '\\': /* "\?", "\/" or "\&", repeat search */
3887 ++cmd;
3888 if (*cmd == '&')
3889 i = RE_SUBST;
3890 else if (*cmd == '?' || *cmd == '/')
3891 i = RE_SEARCH;
3892 else
3893 {
3894 EMSG(_(e_backslash));
3895 cmd = NULL;
3896 goto error;
3897 }
3898
3899 if (!skip)
3900 {
3901 /*
3902 * When search follows another address, start from
3903 * there.
3904 */
3905 if (lnum != MAXLNUM)
3906 pos.lnum = lnum;
3907 else
3908 pos.lnum = curwin->w_cursor.lnum;
3909
3910 /*
3911 * Start the search just like for the above
3912 * do_search().
3913 */
3914 if (*cmd != '?')
3915 pos.col = MAXCOL;
3916 else
3917 pos.col = 0;
3918 if (searchit(curwin, curbuf, &pos,
3919 *cmd == '?' ? BACKWARD : FORWARD,
3920 (char_u *)"", 1L,
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003921 SEARCH_MSG + SEARCH_START,
3922 i, (linenr_T)0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 lnum = pos.lnum;
3924 else
3925 {
3926 cmd = NULL;
3927 goto error;
3928 }
3929 }
3930 ++cmd;
3931 break;
3932
3933 default:
3934 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3935 lnum = getdigits(&cmd);
3936 }
3937
3938 for (;;)
3939 {
3940 cmd = skipwhite(cmd);
3941 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3942 break;
3943
3944 if (lnum == MAXLNUM)
3945 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3946 if (VIM_ISDIGIT(*cmd))
3947 i = '+'; /* "number" is same as "+number" */
3948 else
3949 i = *cmd++;
3950 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3951 n = 1;
3952 else
3953 n = getdigits(&cmd);
3954 if (i == '-')
3955 lnum -= n;
3956 else
3957 lnum += n;
3958 }
3959 } while (*cmd == '/' || *cmd == '?');
3960
3961error:
3962 *ptr = cmd;
3963 return lnum;
3964}
3965
3966/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003967 * Get flags from an Ex command argument.
3968 */
3969 static void
3970get_flags(eap)
3971 exarg_T *eap;
3972{
3973 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3974 {
3975 if (*eap->arg == 'l')
3976 eap->flags |= EXFLAG_LIST;
3977 else if (*eap->arg == 'p')
3978 eap->flags |= EXFLAG_PRINT;
3979 else
3980 eap->flags |= EXFLAG_NR;
3981 eap->arg = skipwhite(eap->arg + 1);
3982 }
3983}
3984
3985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 * Function called for command which is Not Implemented. NI!
3987 */
3988 void
3989ex_ni(eap)
3990 exarg_T *eap;
3991{
3992 if (!eap->skip)
3993 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3994}
3995
3996#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003997 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998/*
3999 * Function called for script command which is Not Implemented. NI!
4000 * Skips over ":perl <<EOF" constructs.
4001 */
4002 static void
4003ex_script_ni(eap)
4004 exarg_T *eap;
4005{
4006 if (!eap->skip)
4007 ex_ni(eap);
4008 else
4009 vim_free(script_get(eap, eap->arg));
4010}
4011#endif
4012
4013/*
4014 * Check range in Ex command for validity.
4015 * Return NULL when valid, error message when invalid.
4016 */
4017 static char_u *
4018invalid_range(eap)
4019 exarg_T *eap;
4020{
4021 if ( eap->line1 < 0
4022 || eap->line2 < 0
4023 || eap->line1 > eap->line2
4024 || ((eap->argt & RANGE)
4025 && !(eap->argt & NOTADR)
4026 && eap->line2 > curbuf->b_ml.ml_line_count
4027#ifdef FEAT_DIFF
4028 + (eap->cmdidx == CMD_diffget)
4029#endif
4030 ))
4031 return (char_u *)_(e_invrange);
4032 return NULL;
4033}
4034
4035/*
4036 * Correct the range for zero line number, if required.
4037 */
4038 static void
4039correct_range(eap)
4040 exarg_T *eap;
4041{
4042 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4043 {
4044 if (eap->line1 == 0)
4045 eap->line1 = 1;
4046 if (eap->line2 == 0)
4047 eap->line2 = 1;
4048 }
4049}
4050
Bram Moolenaar748bf032005-02-02 23:04:36 +00004051#ifdef FEAT_QUICKFIX
4052static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4053
4054/*
4055 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4056 * pattern. Otherwise return eap->arg.
4057 */
4058 static char_u *
4059skip_grep_pat(eap)
4060 exarg_T *eap;
4061{
4062 char_u *p = eap->arg;
4063
Bram Moolenaara37420f2006-02-04 22:37:47 +00004064 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4065 || eap->cmdidx == CMD_vimgrepadd
4066 || eap->cmdidx == CMD_lvimgrepadd
4067 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004068 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004069 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004070 if (p == NULL)
4071 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004072 }
4073 return p;
4074}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004075
4076/*
4077 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4078 * in the command line, so that things like % get expanded.
4079 */
4080 static char_u *
4081replace_makeprg(eap, p, cmdlinep)
4082 exarg_T *eap;
4083 char_u *p;
4084 char_u **cmdlinep;
4085{
4086 char_u *new_cmdline;
4087 char_u *program;
4088 char_u *pos;
4089 char_u *ptr;
4090 int len;
4091 int i;
4092
4093 /*
4094 * Don't do it when ":vimgrep" is used for ":grep".
4095 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004096 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4097 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4098 || eap->cmdidx == CMD_grepadd
4099 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004100 && !grep_internal(eap->cmdidx))
4101 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004102 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4103 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004104 {
4105 if (*curbuf->b_p_gp == NUL)
4106 program = p_gp;
4107 else
4108 program = curbuf->b_p_gp;
4109 }
4110 else
4111 {
4112 if (*curbuf->b_p_mp == NUL)
4113 program = p_mp;
4114 else
4115 program = curbuf->b_p_mp;
4116 }
4117
4118 p = skipwhite(p);
4119
4120 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4121 {
4122 /* replace $* by given arguments */
4123 i = 1;
4124 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4125 ++i;
4126 len = (int)STRLEN(p);
4127 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4128 if (new_cmdline == NULL)
4129 return NULL; /* out of memory */
4130 ptr = new_cmdline;
4131 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4132 {
4133 i = (int)(pos - program);
4134 STRNCPY(ptr, program, i);
4135 STRCPY(ptr += i, p);
4136 ptr += len;
4137 program = pos + 2;
4138 }
4139 STRCPY(ptr, program);
4140 }
4141 else
4142 {
4143 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4144 if (new_cmdline == NULL)
4145 return NULL; /* out of memory */
4146 STRCPY(new_cmdline, program);
4147 STRCAT(new_cmdline, " ");
4148 STRCAT(new_cmdline, p);
4149 }
4150 msg_make(p);
4151
4152 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4153 vim_free(*cmdlinep);
4154 *cmdlinep = new_cmdline;
4155 p = new_cmdline;
4156 }
4157 return p;
4158}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004159#endif
4160
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161/*
4162 * Expand file name in Ex command argument.
4163 * Return FAIL for failure, OK otherwise.
4164 */
4165 int
4166expand_filename(eap, cmdlinep, errormsgp)
4167 exarg_T *eap;
4168 char_u **cmdlinep;
4169 char_u **errormsgp;
4170{
4171 int has_wildcards; /* need to expand wildcards */
4172 char_u *repl;
4173 int srclen;
4174 char_u *p;
4175 int n;
4176
Bram Moolenaar748bf032005-02-02 23:04:36 +00004177#ifdef FEAT_QUICKFIX
4178 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4179 p = skip_grep_pat(eap);
4180#else
4181 p = eap->arg;
4182#endif
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 /*
4185 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4186 * the file name contains a wildcard it should not cause expanding.
4187 * (it will be expanded anyway if there is a wildcard before replacing).
4188 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004189 has_wildcards = mch_has_wildcard(p);
4190 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004192#ifdef FEAT_EVAL
4193 /* Skip over `=expr`, wildcards in it are not expanded. */
4194 if (p[0] == '`' && p[1] == '=')
4195 {
4196 p += 2;
4197 (void)skip_expr(&p);
4198 if (*p == '`')
4199 ++p;
4200 continue;
4201 }
4202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 /*
4204 * Quick check if this cannot be the start of a special string.
4205 * Also removes backslash before '%', '#' and '<'.
4206 */
4207 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4208 {
4209 ++p;
4210 continue;
4211 }
4212
4213 /*
4214 * Try to find a match at this position.
4215 */
4216 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4217 if (*errormsgp != NULL) /* error detected */
4218 return FAIL;
4219 if (repl == NULL) /* no match found */
4220 {
4221 p += srclen;
4222 continue;
4223 }
4224
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004225 /* Wildcards won't be expanded below, the replacement is taken
4226 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4227 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4228 {
4229 char_u *l = repl;
4230
4231 repl = expand_env_save(repl);
4232 vim_free(l);
4233 }
4234
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 /* Need to escape white space et al. with a backslash. Don't do this
4236 * for shell commands (may have to use quotes instead). Don't do this
4237 * for non-unix systems when there is a single argument (spaces don't
4238 * separate arguments then). */
4239 if (!eap->usefilter
4240 && eap->cmdidx != CMD_bang
4241 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004242 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004244 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004246 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247#ifndef UNIX
4248 && !(eap->argt & NOSPC)
4249#endif
4250 )
4251 {
4252 char_u *l;
4253#ifdef BACKSLASH_IN_FILENAME
4254 /* Don't escape a backslash here, because rem_backslash() doesn't
4255 * remove it later. */
4256 static char_u *nobslash = (char_u *)" \t\"|";
4257# define ESCAPE_CHARS nobslash
4258#else
4259# define ESCAPE_CHARS escape_chars
4260#endif
4261
4262 for (l = repl; *l; ++l)
4263 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4264 {
4265 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4266 if (l != NULL)
4267 {
4268 vim_free(repl);
4269 repl = l;
4270 }
4271 break;
4272 }
4273 }
4274
4275 /* For a shell command a '!' must be escaped. */
4276 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004277 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
4279 char_u *l;
4280
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004281 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 if (l != NULL)
4283 {
4284 vim_free(repl);
4285 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004286 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (strstr((char *)p_sh, "sh") != NULL)
4288 {
4289 l = vim_strsave_escaped(repl, (char_u *)"!");
4290 if (l != NULL)
4291 {
4292 vim_free(repl);
4293 repl = l;
4294 }
4295 }
4296 }
4297 }
4298
4299 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4300 vim_free(repl);
4301 if (p == NULL)
4302 return FAIL;
4303 }
4304
4305 /*
4306 * One file argument: Expand wildcards.
4307 * Don't do this with ":r !command" or ":w !command".
4308 */
4309 if ((eap->argt & NOSPC) && !eap->usefilter)
4310 {
4311 /*
4312 * May do this twice:
4313 * 1. Replace environment variables.
4314 * 2. Replace any other wildcards, remove backslashes.
4315 */
4316 for (n = 1; n <= 2; ++n)
4317 {
4318 if (n == 2)
4319 {
4320#ifdef UNIX
4321 /*
4322 * Only for Unix we check for more than one file name.
4323 * For other systems spaces are considered to be part
4324 * of the file name.
4325 * Only check here if there is no wildcard, otherwise
4326 * ExpandOne() will check for errors. This allows
4327 * ":e `ls ve*.c`" on Unix.
4328 */
4329 if (!has_wildcards)
4330 for (p = eap->arg; *p; ++p)
4331 {
4332 /* skip escaped characters */
4333 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4334 ++p;
4335 else if (vim_iswhite(*p))
4336 {
4337 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4338 return FAIL;
4339 }
4340 }
4341#endif
4342
4343 /*
4344 * Halve the number of backslashes (this is Vi compatible).
4345 * For Unix and OS/2, when wildcards are expanded, this is
4346 * done by ExpandOne() below.
4347 */
4348#if defined(UNIX) || defined(OS2)
4349 if (!has_wildcards)
4350#endif
4351 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353
4354 if (has_wildcards)
4355 {
4356 if (n == 1)
4357 {
4358 /*
4359 * First loop: May expand environment variables. This
4360 * can be done much faster with expand_env() than with
4361 * something else (e.g., calling a shell).
4362 * After expanding environment variables, check again
4363 * if there are still wildcards present.
4364 */
4365 if (vim_strchr(eap->arg, '$') != NULL
4366 || vim_strchr(eap->arg, '~') != NULL)
4367 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004368 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4369 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 has_wildcards = mch_has_wildcard(NameBuff);
4371 p = NameBuff;
4372 }
4373 else
4374 p = NULL;
4375 }
4376 else /* n == 2 */
4377 {
4378 expand_T xpc;
4379
4380 ExpandInit(&xpc);
4381 xpc.xp_context = EXPAND_FILES;
4382 p = ExpandOne(&xpc, eap->arg, NULL,
4383 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4384 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (p == NULL)
4386 return FAIL;
4387 }
4388 if (p != NULL)
4389 {
4390 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4391 p, cmdlinep);
4392 if (n == 2) /* p came from ExpandOne() */
4393 vim_free(p);
4394 }
4395 }
4396 }
4397 }
4398 return OK;
4399}
4400
4401/*
4402 * Replace part of the command line, keeping eap->cmd, eap->arg and
4403 * eap->nextcmd correct.
4404 * "src" points to the part that is to be replaced, of length "srclen".
4405 * "repl" is the replacement string.
4406 * Returns a pointer to the character after the replaced string.
4407 * Returns NULL for failure.
4408 */
4409 static char_u *
4410repl_cmdline(eap, src, srclen, repl, cmdlinep)
4411 exarg_T *eap;
4412 char_u *src;
4413 int srclen;
4414 char_u *repl;
4415 char_u **cmdlinep;
4416{
4417 int len;
4418 int i;
4419 char_u *new_cmdline;
4420
4421 /*
4422 * The new command line is build in new_cmdline[].
4423 * First allocate it.
4424 * Careful: a "+cmd" argument may have been NUL terminated.
4425 */
4426 len = (int)STRLEN(repl);
4427 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004428 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4430 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4431 return NULL; /* out of memory! */
4432
4433 /*
4434 * Copy the stuff before the expanded part.
4435 * Copy the expanded stuff.
4436 * Copy what came after the expanded part.
4437 * Copy the next commands, if there are any.
4438 */
4439 i = (int)(src - *cmdlinep); /* length of part before match */
4440 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 mch_memmove(new_cmdline + i, repl, (size_t)len);
4443 i += len; /* remember the end of the string */
4444 STRCPY(new_cmdline + i, src + srclen);
4445 src = new_cmdline + i; /* remember where to continue */
4446
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004447 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
4449 i = (int)STRLEN(new_cmdline) + 1;
4450 STRCPY(new_cmdline + i, eap->nextcmd);
4451 eap->nextcmd = new_cmdline + i;
4452 }
4453 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4454 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4455 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4456 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4457 vim_free(*cmdlinep);
4458 *cmdlinep = new_cmdline;
4459
4460 return src;
4461}
4462
4463/*
4464 * Check for '|' to separate commands and '"' to start comments.
4465 */
4466 void
4467separate_nextcmd(eap)
4468 exarg_T *eap;
4469{
4470 char_u *p;
4471
Bram Moolenaar86b68352004-12-27 21:59:20 +00004472#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004473 p = skip_grep_pat(eap);
4474#else
4475 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004476#endif
4477
4478 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 if (*p == Ctrl_V)
4481 {
4482 if (eap->argt & (USECTRLV | XFILE))
4483 ++p; /* skip CTRL-V and next char */
4484 else
4485 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4486 if (*p == NUL) /* stop at NUL after CTRL-V */
4487 break;
4488 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004489
4490#ifdef FEAT_EVAL
4491 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004492 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004493 {
4494 p += 2;
4495 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004496 }
4497#endif
4498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 /* Check for '"': start of comment or '|': next command */
4500 /* :@" and :*" do not start a comment!
4501 * :redir @" doesn't either. */
4502 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4503 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4504 || p != eap->arg)
4505 && (eap->cmdidx != CMD_redir
4506 || p != eap->arg + 1 || p[-1] != '@'))
4507 || *p == '|' || *p == '\n')
4508 {
4509 /*
4510 * We remove the '\' before the '|', unless USECTRLV is used
4511 * AND 'b' is present in 'cpoptions'.
4512 */
4513 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4514 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4515 {
4516 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4517 --p;
4518 }
4519 else
4520 {
4521 eap->nextcmd = check_nextcmd(p);
4522 *p = NUL;
4523 break;
4524 }
4525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004527
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4529 del_trailing_spaces(eap->arg);
4530}
4531
4532/*
4533 * get + command from ex argument
4534 */
4535 static char_u *
4536getargcmd(argp)
4537 char_u **argp;
4538{
4539 char_u *arg = *argp;
4540 char_u *command = NULL;
4541
4542 if (*arg == '+') /* +[command] */
4543 {
4544 ++arg;
4545 if (vim_isspace(*arg))
4546 command = dollar_command;
4547 else
4548 {
4549 command = arg;
4550 arg = skip_cmd_arg(command, TRUE);
4551 if (*arg != NUL)
4552 *arg++ = NUL; /* terminate command with NUL */
4553 }
4554
4555 arg = skipwhite(arg); /* skip over spaces */
4556 *argp = arg;
4557 }
4558 return command;
4559}
4560
4561/*
4562 * Find end of "+command" argument. Skip over "\ " and "\\".
4563 */
4564 static char_u *
4565skip_cmd_arg(p, rembs)
4566 char_u *p;
4567 int rembs; /* TRUE to halve the number of backslashes */
4568{
4569 while (*p && !vim_isspace(*p))
4570 {
4571 if (*p == '\\' && p[1] != NUL)
4572 {
4573 if (rembs)
4574 mch_memmove(p, p + 1, STRLEN(p));
4575 else
4576 ++p;
4577 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004578 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580 return p;
4581}
4582
4583/*
4584 * Get "++opt=arg" argument.
4585 * Return FAIL or OK.
4586 */
4587 static int
4588getargopt(eap)
4589 exarg_T *eap;
4590{
4591 char_u *arg = eap->arg + 2;
4592 int *pp = NULL;
4593#ifdef FEAT_MBYTE
4594 char_u *p;
4595#endif
4596
4597 /* ":edit ++[no]bin[ary] file" */
4598 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4599 {
4600 if (*arg == 'n')
4601 {
4602 arg += 2;
4603 eap->force_bin = FORCE_NOBIN;
4604 }
4605 else
4606 eap->force_bin = FORCE_BIN;
4607 if (!checkforcmd(&arg, "binary", 3))
4608 return FAIL;
4609 eap->arg = skipwhite(arg);
4610 return OK;
4611 }
4612
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004613 /* ":read ++edit file" */
4614 if (STRNCMP(arg, "edit", 4) == 0)
4615 {
4616 eap->read_edit = TRUE;
4617 eap->arg = skipwhite(arg + 4);
4618 return OK;
4619 }
4620
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (STRNCMP(arg, "ff", 2) == 0)
4622 {
4623 arg += 2;
4624 pp = &eap->force_ff;
4625 }
4626 else if (STRNCMP(arg, "fileformat", 10) == 0)
4627 {
4628 arg += 10;
4629 pp = &eap->force_ff;
4630 }
4631#ifdef FEAT_MBYTE
4632 else if (STRNCMP(arg, "enc", 3) == 0)
4633 {
4634 arg += 3;
4635 pp = &eap->force_enc;
4636 }
4637 else if (STRNCMP(arg, "encoding", 8) == 0)
4638 {
4639 arg += 8;
4640 pp = &eap->force_enc;
4641 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004642 else if (STRNCMP(arg, "bad", 3) == 0)
4643 {
4644 arg += 3;
4645 pp = &eap->bad_char;
4646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647#endif
4648
4649 if (pp == NULL || *arg != '=')
4650 return FAIL;
4651
4652 ++arg;
4653 *pp = (int)(arg - eap->cmd);
4654 arg = skip_cmd_arg(arg, FALSE);
4655 eap->arg = skipwhite(arg);
4656 *arg = NUL;
4657
4658#ifdef FEAT_MBYTE
4659 if (pp == &eap->force_ff)
4660 {
4661#endif
4662 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4663 return FAIL;
4664#ifdef FEAT_MBYTE
4665 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004666 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
4668 /* Make 'fileencoding' lower case. */
4669 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4670 *p = TOLOWER_ASC(*p);
4671 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004672 else
4673 {
4674 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4675 * "drop". */
4676 p = eap->cmd + eap->bad_char;
4677 if (STRICMP(p, "keep") == 0)
4678 eap->bad_char = BAD_KEEP;
4679 else if (STRICMP(p, "drop") == 0)
4680 eap->bad_char = BAD_DROP;
4681 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4682 eap->bad_char = *p;
4683 else
4684 return FAIL;
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686#endif
4687
4688 return OK;
4689}
4690
4691/*
4692 * ":abbreviate" and friends.
4693 */
4694 static void
4695ex_abbreviate(eap)
4696 exarg_T *eap;
4697{
4698 do_exmap(eap, TRUE); /* almost the same as mapping */
4699}
4700
4701/*
4702 * ":map" and friends.
4703 */
4704 static void
4705ex_map(eap)
4706 exarg_T *eap;
4707{
4708 /*
4709 * If we are sourcing .exrc or .vimrc in current directory we
4710 * print the mappings for security reasons.
4711 */
4712 if (secure)
4713 {
4714 secure = 2;
4715 msg_outtrans(eap->cmd);
4716 msg_putchar('\n');
4717 }
4718 do_exmap(eap, FALSE);
4719}
4720
4721/*
4722 * ":unmap" and friends.
4723 */
4724 static void
4725ex_unmap(eap)
4726 exarg_T *eap;
4727{
4728 do_exmap(eap, FALSE);
4729}
4730
4731/*
4732 * ":mapclear" and friends.
4733 */
4734 static void
4735ex_mapclear(eap)
4736 exarg_T *eap;
4737{
4738 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4739}
4740
4741/*
4742 * ":abclear" and friends.
4743 */
4744 static void
4745ex_abclear(eap)
4746 exarg_T *eap;
4747{
4748 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4749}
4750
4751#ifdef FEAT_AUTOCMD
4752 static void
4753ex_autocmd(eap)
4754 exarg_T *eap;
4755{
4756 /*
4757 * Disallow auto commands from .exrc and .vimrc in current
4758 * directory for security reasons.
4759 */
4760 if (secure)
4761 {
4762 secure = 2;
4763 eap->errmsg = e_curdir;
4764 }
4765 else if (eap->cmdidx == CMD_autocmd)
4766 do_autocmd(eap->arg, eap->forceit);
4767 else
4768 do_augroup(eap->arg, eap->forceit);
4769}
4770
4771/*
4772 * ":doautocmd": Apply the automatic commands to the current buffer.
4773 */
4774 static void
4775ex_doautocmd(eap)
4776 exarg_T *eap;
4777{
4778 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004779 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780}
4781#endif
4782
4783#ifdef FEAT_LISTCMDS
4784/*
4785 * :[N]bunload[!] [N] [bufname] unload buffer
4786 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4787 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4788 */
4789 static void
4790ex_bunload(eap)
4791 exarg_T *eap;
4792{
4793 eap->errmsg = do_bufdel(
4794 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4795 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4796 : DOBUF_UNLOAD, eap->arg,
4797 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4798}
4799
4800/*
4801 * :[N]buffer [N] to buffer N
4802 * :[N]sbuffer [N] to buffer N
4803 */
4804 static void
4805ex_buffer(eap)
4806 exarg_T *eap;
4807{
4808 if (*eap->arg)
4809 eap->errmsg = e_trailing;
4810 else
4811 {
4812 if (eap->addr_count == 0) /* default is current buffer */
4813 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4814 else
4815 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4816 }
4817}
4818
4819/*
4820 * :[N]bmodified [N] to next mod. buffer
4821 * :[N]sbmodified [N] to next mod. buffer
4822 */
4823 static void
4824ex_bmodified(eap)
4825 exarg_T *eap;
4826{
4827 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4828}
4829
4830/*
4831 * :[N]bnext [N] to next buffer
4832 * :[N]sbnext [N] split and to next buffer
4833 */
4834 static void
4835ex_bnext(eap)
4836 exarg_T *eap;
4837{
4838 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4839}
4840
4841/*
4842 * :[N]bNext [N] to previous buffer
4843 * :[N]bprevious [N] to previous buffer
4844 * :[N]sbNext [N] split and to previous buffer
4845 * :[N]sbprevious [N] split and to previous buffer
4846 */
4847 static void
4848ex_bprevious(eap)
4849 exarg_T *eap;
4850{
4851 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4852}
4853
4854/*
4855 * :brewind to first buffer
4856 * :bfirst to first buffer
4857 * :sbrewind split and to first buffer
4858 * :sbfirst split and to first buffer
4859 */
4860 static void
4861ex_brewind(eap)
4862 exarg_T *eap;
4863{
4864 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4865}
4866
4867/*
4868 * :blast to last buffer
4869 * :sblast split and to last buffer
4870 */
4871 static void
4872ex_blast(eap)
4873 exarg_T *eap;
4874{
4875 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4876}
4877#endif
4878
4879 int
4880ends_excmd(c)
4881 int c;
4882{
4883 return (c == NUL || c == '|' || c == '"' || c == '\n');
4884}
4885
4886#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4887 || defined(PROTO)
4888/*
4889 * Return the next command, after the first '|' or '\n'.
4890 * Return NULL if not found.
4891 */
4892 char_u *
4893find_nextcmd(p)
4894 char_u *p;
4895{
4896 while (*p != '|' && *p != '\n')
4897 {
4898 if (*p == NUL)
4899 return NULL;
4900 ++p;
4901 }
4902 return (p + 1);
4903}
4904#endif
4905
4906/*
4907 * Check if *p is a separator between Ex commands.
4908 * Return NULL if it isn't, (p + 1) if it is.
4909 */
4910 char_u *
4911check_nextcmd(p)
4912 char_u *p;
4913{
4914 p = skipwhite(p);
4915 if (*p == '|' || *p == '\n')
4916 return (p + 1);
4917 else
4918 return NULL;
4919}
4920
4921/*
4922 * - if there are more files to edit
4923 * - and this is the last window
4924 * - and forceit not used
4925 * - and not repeated twice on a row
4926 * return FAIL and give error message if 'message' TRUE
4927 * return OK otherwise
4928 */
4929 static int
4930check_more(message, forceit)
4931 int message; /* when FALSE check only, no messages */
4932 int forceit;
4933{
4934 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4935
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004936 if (!forceit && only_one_window()
4937 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
4939 if (message)
4940 {
4941#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4942 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4943 {
4944 char_u buff[IOSIZE];
4945
4946 if (n == 1)
4947 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4948 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004949 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 _("%d more files to edit. Quit anyway?"), n);
4951 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4952 return OK;
4953 return FAIL;
4954 }
4955#endif
4956 if (n == 1)
4957 EMSG(_("E173: 1 more file to edit"));
4958 else
4959 EMSGN(_("E173: %ld more files to edit"), n);
4960 quitmore = 2; /* next try to quit is allowed */
4961 }
4962 return FAIL;
4963 }
4964 return OK;
4965}
4966
4967#ifdef FEAT_CMDL_COMPL
4968/*
4969 * Function given to ExpandGeneric() to obtain the list of command names.
4970 */
4971/*ARGSUSED*/
4972 char_u *
4973get_command_name(xp, idx)
4974 expand_T *xp;
4975 int idx;
4976{
4977 if (idx >= (int)CMD_SIZE)
4978# ifdef FEAT_USR_CMDS
4979 return get_user_command_name(idx);
4980# else
4981 return NULL;
4982# endif
4983 return cmdnames[idx].cmd_name;
4984}
4985#endif
4986
4987#if defined(FEAT_USR_CMDS) || defined(PROTO)
4988static 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));
4989static void uc_list __ARGS((char_u *name, size_t name_len));
4990static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4991static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4992static 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));
4993
4994 static int
4995uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4996 char_u *name;
4997 size_t name_len;
4998 char_u *rep;
4999 long argt;
5000 long def;
5001 int flags;
5002 int compl;
5003 char_u *compl_arg;
5004 int force;
5005{
5006 ucmd_T *cmd = NULL;
5007 char_u *p;
5008 int i;
5009 int cmp = 1;
5010 char_u *rep_buf = NULL;
5011 garray_T *gap;
5012
Bram Moolenaar9c102382006-05-03 21:26:49 +00005013 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 if (rep_buf == NULL)
5015 {
5016 /* Can't replace termcodes - try using the string as is */
5017 rep_buf = vim_strsave(rep);
5018
5019 /* Give up if out of memory */
5020 if (rep_buf == NULL)
5021 return FAIL;
5022 }
5023
5024 /* get address of growarray: global or in curbuf */
5025 if (flags & UC_BUFFER)
5026 {
5027 gap = &curbuf->b_ucmds;
5028 if (gap->ga_itemsize == 0)
5029 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5030 }
5031 else
5032 gap = &ucmds;
5033
5034 /* Search for the command in the already defined commands. */
5035 for (i = 0; i < gap->ga_len; ++i)
5036 {
5037 size_t len;
5038
5039 cmd = USER_CMD_GA(gap, i);
5040 len = STRLEN(cmd->uc_name);
5041 cmp = STRNCMP(name, cmd->uc_name, name_len);
5042 if (cmp == 0)
5043 {
5044 if (name_len < len)
5045 cmp = -1;
5046 else if (name_len > len)
5047 cmp = 1;
5048 }
5049
5050 if (cmp == 0)
5051 {
5052 if (!force)
5053 {
5054 EMSG(_("E174: Command already exists: add ! to replace it"));
5055 goto fail;
5056 }
5057
5058 vim_free(cmd->uc_rep);
5059 cmd->uc_rep = 0;
5060 break;
5061 }
5062
5063 /* Stop as soon as we pass the name to add */
5064 if (cmp < 0)
5065 break;
5066 }
5067
5068 /* Extend the array unless we're replacing an existing command */
5069 if (cmp != 0)
5070 {
5071 if (ga_grow(gap, 1) != OK)
5072 goto fail;
5073 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5074 goto fail;
5075
5076 cmd = USER_CMD_GA(gap, i);
5077 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5078
5079 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081 cmd->uc_name = p;
5082 }
5083
5084 cmd->uc_rep = rep_buf;
5085 cmd->uc_argt = argt;
5086 cmd->uc_def = def;
5087 cmd->uc_compl = compl;
5088#ifdef FEAT_EVAL
5089 cmd->uc_scriptID = current_SID;
5090# ifdef FEAT_CMDL_COMPL
5091 cmd->uc_compl_arg = compl_arg;
5092# endif
5093#endif
5094
5095 return OK;
5096
5097fail:
5098 vim_free(rep_buf);
5099#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5100 vim_free(compl_arg);
5101#endif
5102 return FAIL;
5103}
5104
5105/*
5106 * List of names for completion for ":command" with the EXPAND_ flag.
5107 * Must be alphabetical for completion.
5108 */
5109static struct
5110{
5111 int expand;
5112 char *name;
5113} command_complete[] =
5114{
5115 {EXPAND_AUGROUP, "augroup"},
5116 {EXPAND_BUFFERS, "buffer"},
5117 {EXPAND_COMMANDS, "command"},
5118#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5119 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005120 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121#endif
5122 {EXPAND_DIRECTORIES, "dir"},
5123 {EXPAND_ENV_VARS, "environment"},
5124 {EXPAND_EVENTS, "event"},
5125 {EXPAND_EXPRESSION, "expression"},
5126 {EXPAND_FILES, "file"},
5127 {EXPAND_FUNCTIONS, "function"},
5128 {EXPAND_HELP, "help"},
5129 {EXPAND_HIGHLIGHT, "highlight"},
5130 {EXPAND_MAPPINGS, "mapping"},
5131 {EXPAND_MENUS, "menu"},
5132 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005133 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {EXPAND_TAGS, "tag"},
5135 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5136 {EXPAND_USER_VARS, "var"},
5137 {0, NULL}
5138};
5139
5140 static void
5141uc_list(name, name_len)
5142 char_u *name;
5143 size_t name_len;
5144{
5145 int i, j;
5146 int found = FALSE;
5147 ucmd_T *cmd;
5148 int len;
5149 long a;
5150 garray_T *gap;
5151
5152 gap = &curbuf->b_ucmds;
5153 for (;;)
5154 {
5155 for (i = 0; i < gap->ga_len; ++i)
5156 {
5157 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005158 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
5160 /* Skip commands which don't match the requested prefix */
5161 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5162 continue;
5163
5164 /* Put out the title first time */
5165 if (!found)
5166 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5167 found = TRUE;
5168 msg_putchar('\n');
5169 if (got_int)
5170 break;
5171
5172 /* Special cases */
5173 msg_putchar(a & BANG ? '!' : ' ');
5174 msg_putchar(a & REGSTR ? '"' : ' ');
5175 msg_putchar(gap != &ucmds ? 'b' : ' ');
5176 msg_putchar(' ');
5177
5178 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5179 len = (int)STRLEN(cmd->uc_name) + 4;
5180
5181 do {
5182 msg_putchar(' ');
5183 ++len;
5184 } while (len < 16);
5185
5186 len = 0;
5187
5188 /* Arguments */
5189 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5190 {
5191 case 0: IObuff[len++] = '0'; break;
5192 case (EXTRA): IObuff[len++] = '*'; break;
5193 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5194 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5195 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5196 }
5197
5198 do {
5199 IObuff[len++] = ' ';
5200 } while (len < 5);
5201
5202 /* Range */
5203 if (a & (RANGE|COUNT))
5204 {
5205 if (a & COUNT)
5206 {
5207 /* -count=N */
5208 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5209 len += (int)STRLEN(IObuff + len);
5210 }
5211 else if (a & DFLALL)
5212 IObuff[len++] = '%';
5213 else if (cmd->uc_def >= 0)
5214 {
5215 /* -range=N */
5216 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5217 len += (int)STRLEN(IObuff + len);
5218 }
5219 else
5220 IObuff[len++] = '.';
5221 }
5222
5223 do {
5224 IObuff[len++] = ' ';
5225 } while (len < 11);
5226
5227 /* Completion */
5228 for (j = 0; command_complete[j].expand != 0; ++j)
5229 if (command_complete[j].expand == cmd->uc_compl)
5230 {
5231 STRCPY(IObuff + len, command_complete[j].name);
5232 len += (int)STRLEN(IObuff + len);
5233 break;
5234 }
5235
5236 do {
5237 IObuff[len++] = ' ';
5238 } while (len < 21);
5239
5240 IObuff[len] = '\0';
5241 msg_outtrans(IObuff);
5242
5243 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005244#ifdef FEAT_EVAL
5245 if (p_verbose > 0)
5246 last_set_msg(cmd->uc_scriptID);
5247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 out_flush();
5249 ui_breakcheck();
5250 if (got_int)
5251 break;
5252 }
5253 if (gap == &ucmds || i < gap->ga_len)
5254 break;
5255 gap = &ucmds;
5256 }
5257
5258 if (!found)
5259 MSG(_("No user-defined commands found"));
5260}
5261
5262 static char_u *
5263uc_fun_cmd()
5264{
5265 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5266 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5267 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5268 0xb9, 0x7f, 0};
5269 int i;
5270
5271 for (i = 0; fcmd[i]; ++i)
5272 IObuff[i] = fcmd[i] - 0x40;
5273 IObuff[i] = 0;
5274 return IObuff;
5275}
5276
5277 static int
5278uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5279 char_u *attr;
5280 size_t len;
5281 long *argt;
5282 long *def;
5283 int *flags;
5284 int *compl;
5285 char_u **compl_arg;
5286{
5287 char_u *p;
5288
5289 if (len == 0)
5290 {
5291 EMSG(_("E175: No attribute specified"));
5292 return FAIL;
5293 }
5294
5295 /* First, try the simple attributes (no arguments) */
5296 if (STRNICMP(attr, "bang", len) == 0)
5297 *argt |= BANG;
5298 else if (STRNICMP(attr, "buffer", len) == 0)
5299 *flags |= UC_BUFFER;
5300 else if (STRNICMP(attr, "register", len) == 0)
5301 *argt |= REGSTR;
5302 else if (STRNICMP(attr, "bar", len) == 0)
5303 *argt |= TRLBAR;
5304 else
5305 {
5306 int i;
5307 char_u *val = NULL;
5308 size_t vallen = 0;
5309 size_t attrlen = len;
5310
5311 /* Look for the attribute name - which is the part before any '=' */
5312 for (i = 0; i < (int)len; ++i)
5313 {
5314 if (attr[i] == '=')
5315 {
5316 val = &attr[i + 1];
5317 vallen = len - i - 1;
5318 attrlen = i;
5319 break;
5320 }
5321 }
5322
5323 if (STRNICMP(attr, "nargs", attrlen) == 0)
5324 {
5325 if (vallen == 1)
5326 {
5327 if (*val == '0')
5328 /* Do nothing - this is the default */;
5329 else if (*val == '1')
5330 *argt |= (EXTRA | NOSPC | NEEDARG);
5331 else if (*val == '*')
5332 *argt |= EXTRA;
5333 else if (*val == '?')
5334 *argt |= (EXTRA | NOSPC);
5335 else if (*val == '+')
5336 *argt |= (EXTRA | NEEDARG);
5337 else
5338 goto wrong_nargs;
5339 }
5340 else
5341 {
5342wrong_nargs:
5343 EMSG(_("E176: Invalid number of arguments"));
5344 return FAIL;
5345 }
5346 }
5347 else if (STRNICMP(attr, "range", attrlen) == 0)
5348 {
5349 *argt |= RANGE;
5350 if (vallen == 1 && *val == '%')
5351 *argt |= DFLALL;
5352 else if (val != NULL)
5353 {
5354 p = val;
5355 if (*def >= 0)
5356 {
5357two_count:
5358 EMSG(_("E177: Count cannot be specified twice"));
5359 return FAIL;
5360 }
5361
5362 *def = getdigits(&p);
5363 *argt |= (ZEROR | NOTADR);
5364
5365 if (p != val + vallen || vallen == 0)
5366 {
5367invalid_count:
5368 EMSG(_("E178: Invalid default value for count"));
5369 return FAIL;
5370 }
5371 }
5372 }
5373 else if (STRNICMP(attr, "count", attrlen) == 0)
5374 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005375 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377 if (val != NULL)
5378 {
5379 p = val;
5380 if (*def >= 0)
5381 goto two_count;
5382
5383 *def = getdigits(&p);
5384
5385 if (p != val + vallen)
5386 goto invalid_count;
5387 }
5388
5389 if (*def < 0)
5390 *def = 0;
5391 }
5392 else if (STRNICMP(attr, "complete", attrlen) == 0)
5393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 if (val == NULL)
5395 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005396 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 return FAIL;
5398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005400 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5401 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404 else
5405 {
5406 char_u ch = attr[len];
5407 attr[len] = '\0';
5408 EMSG2(_("E181: Invalid attribute: %s"), attr);
5409 attr[len] = ch;
5410 return FAIL;
5411 }
5412 }
5413
5414 return OK;
5415}
5416
5417 static void
5418ex_command(eap)
5419 exarg_T *eap;
5420{
5421 char_u *name;
5422 char_u *end;
5423 char_u *p;
5424 long argt = 0;
5425 long def = -1;
5426 int flags = 0;
5427 int compl = EXPAND_NOTHING;
5428 char_u *compl_arg = NULL;
5429 int has_attr = (eap->arg[0] == '-');
5430
5431 p = eap->arg;
5432
5433 /* Check for attributes */
5434 while (*p == '-')
5435 {
5436 ++p;
5437 end = skiptowhite(p);
5438 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5439 == FAIL)
5440 return;
5441 p = skipwhite(end);
5442 }
5443
5444 /* Get the name (if any) and skip to the following argument */
5445 name = p;
5446 if (ASCII_ISALPHA(*p))
5447 while (ASCII_ISALNUM(*p))
5448 ++p;
5449 if (!ends_excmd(*p) && !vim_iswhite(*p))
5450 {
5451 EMSG(_("E182: Invalid command name"));
5452 return;
5453 }
5454 end = p;
5455
5456 /* If there is nothing after the name, and no attributes were specified,
5457 * we are listing commands
5458 */
5459 p = skipwhite(end);
5460 if (!has_attr && ends_excmd(*p))
5461 {
5462 uc_list(name, end - name);
5463 }
5464 else if (!ASCII_ISUPPER(*name))
5465 {
5466 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5467 return;
5468 }
5469 else
5470 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5471 eap->forceit);
5472}
5473
5474/*
5475 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 * Clear all user commands, global and for current buffer.
5477 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005478/*ARGSUSED*/
5479 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480ex_comclear(eap)
5481 exarg_T *eap;
5482{
5483 uc_clear(&ucmds);
5484 uc_clear(&curbuf->b_ucmds);
5485}
5486
5487/*
5488 * Clear all user commands for "gap".
5489 */
5490 void
5491uc_clear(gap)
5492 garray_T *gap;
5493{
5494 int i;
5495 ucmd_T *cmd;
5496
5497 for (i = 0; i < gap->ga_len; ++i)
5498 {
5499 cmd = USER_CMD_GA(gap, i);
5500 vim_free(cmd->uc_name);
5501 vim_free(cmd->uc_rep);
5502# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5503 vim_free(cmd->uc_compl_arg);
5504# endif
5505 }
5506 ga_clear(gap);
5507}
5508
5509 static void
5510ex_delcommand(eap)
5511 exarg_T *eap;
5512{
5513 int i = 0;
5514 ucmd_T *cmd = NULL;
5515 int cmp = -1;
5516 garray_T *gap;
5517
5518 gap = &curbuf->b_ucmds;
5519 for (;;)
5520 {
5521 for (i = 0; i < gap->ga_len; ++i)
5522 {
5523 cmd = USER_CMD_GA(gap, i);
5524 cmp = STRCMP(eap->arg, cmd->uc_name);
5525 if (cmp <= 0)
5526 break;
5527 }
5528 if (gap == &ucmds || cmp == 0)
5529 break;
5530 gap = &ucmds;
5531 }
5532
5533 if (cmp != 0)
5534 {
5535 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5536 return;
5537 }
5538
5539 vim_free(cmd->uc_name);
5540 vim_free(cmd->uc_rep);
5541# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5542 vim_free(cmd->uc_compl_arg);
5543# endif
5544
5545 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547 if (i < gap->ga_len)
5548 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5549}
5550
5551 static char_u *
5552uc_split_args(arg, lenp)
5553 char_u *arg;
5554 size_t *lenp;
5555{
5556 char_u *buf;
5557 char_u *p;
5558 char_u *q;
5559 int len;
5560
5561 /* Precalculate length */
5562 p = arg;
5563 len = 2; /* Initial and final quotes */
5564
5565 while (*p)
5566 {
5567 if (p[0] == '\\' && vim_iswhite(p[1]))
5568 {
5569 len += 1;
5570 p += 2;
5571 }
5572 else if (*p == '\\' || *p == '"')
5573 {
5574 len += 2;
5575 p += 1;
5576 }
5577 else if (vim_iswhite(*p))
5578 {
5579 p = skipwhite(p);
5580 if (*p == NUL)
5581 break;
5582 len += 3; /* "," */
5583 }
5584 else
5585 {
5586 ++len;
5587 ++p;
5588 }
5589 }
5590
5591 buf = alloc(len + 1);
5592 if (buf == NULL)
5593 {
5594 *lenp = 0;
5595 return buf;
5596 }
5597
5598 p = arg;
5599 q = buf;
5600 *q++ = '"';
5601 while (*p)
5602 {
5603 if (p[0] == '\\' && vim_iswhite(p[1]))
5604 {
5605 *q++ = p[1];
5606 p += 2;
5607 }
5608 else if (*p == '\\' || *p == '"')
5609 {
5610 *q++ = '\\';
5611 *q++ = *p++;
5612 }
5613 else if (vim_iswhite(*p))
5614 {
5615 p = skipwhite(p);
5616 if (*p == NUL)
5617 break;
5618 *q++ = '"';
5619 *q++ = ',';
5620 *q++ = '"';
5621 }
5622 else
5623 {
5624 *q++ = *p++;
5625 }
5626 }
5627 *q++ = '"';
5628 *q = 0;
5629
5630 *lenp = len;
5631 return buf;
5632}
5633
5634/*
5635 * Check for a <> code in a user command.
5636 * "code" points to the '<'. "len" the length of the <> (inclusive).
5637 * "buf" is where the result is to be added.
5638 * "split_buf" points to a buffer used for splitting, caller should free it.
5639 * "split_len" is the length of what "split_buf" contains.
5640 * Returns the length of the replacement, which has been added to "buf".
5641 * Returns -1 if there was no match, and only the "<" has been copied.
5642 */
5643 static size_t
5644uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5645 char_u *code;
5646 size_t len;
5647 char_u *buf;
5648 ucmd_T *cmd; /* the user command we're expanding */
5649 exarg_T *eap; /* ex arguments */
5650 char_u **split_buf;
5651 size_t *split_len;
5652{
5653 size_t result = 0;
5654 char_u *p = code + 1;
5655 size_t l = len - 2;
5656 int quote = 0;
5657 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5658 ct_LT, ct_NONE } type = ct_NONE;
5659
5660 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5661 {
5662 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5663 p += 2;
5664 l -= 2;
5665 }
5666
Bram Moolenaar371d5402006-03-20 21:47:49 +00005667 ++l;
5668 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005670 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005672 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005674 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005676 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005678 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005680 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005682 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 type = ct_REGISTER;
5684
5685 switch (type)
5686 {
5687 case ct_ARGS:
5688 /* Simple case first */
5689 if (*eap->arg == NUL)
5690 {
5691 if (quote == 1)
5692 {
5693 result = 2;
5694 if (buf != NULL)
5695 STRCPY(buf, "''");
5696 }
5697 else
5698 result = 0;
5699 break;
5700 }
5701
5702 /* When specified there is a single argument don't split it.
5703 * Works for ":Cmd %" when % is "a b c". */
5704 if ((eap->argt & NOSPC) && quote == 2)
5705 quote = 1;
5706
5707 switch (quote)
5708 {
5709 case 0: /* No quoting, no splitting */
5710 result = STRLEN(eap->arg);
5711 if (buf != NULL)
5712 STRCPY(buf, eap->arg);
5713 break;
5714 case 1: /* Quote, but don't split */
5715 result = STRLEN(eap->arg) + 2;
5716 for (p = eap->arg; *p; ++p)
5717 {
5718 if (*p == '\\' || *p == '"')
5719 ++result;
5720 }
5721
5722 if (buf != NULL)
5723 {
5724 *buf++ = '"';
5725 for (p = eap->arg; *p; ++p)
5726 {
5727 if (*p == '\\' || *p == '"')
5728 *buf++ = '\\';
5729 *buf++ = *p;
5730 }
5731 *buf = '"';
5732 }
5733
5734 break;
5735 case 2: /* Quote and split */
5736 /* This is hard, so only do it once, and cache the result */
5737 if (*split_buf == NULL)
5738 *split_buf = uc_split_args(eap->arg, split_len);
5739
5740 result = *split_len;
5741 if (buf != NULL && result != 0)
5742 STRCPY(buf, *split_buf);
5743
5744 break;
5745 }
5746 break;
5747
5748 case ct_BANG:
5749 result = eap->forceit ? 1 : 0;
5750 if (quote)
5751 result += 2;
5752 if (buf != NULL)
5753 {
5754 if (quote)
5755 *buf++ = '"';
5756 if (eap->forceit)
5757 *buf++ = '!';
5758 if (quote)
5759 *buf = '"';
5760 }
5761 break;
5762
5763 case ct_LINE1:
5764 case ct_LINE2:
5765 case ct_COUNT:
5766 {
5767 char num_buf[20];
5768 long num = (type == ct_LINE1) ? eap->line1 :
5769 (type == ct_LINE2) ? eap->line2 :
5770 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5771 size_t num_len;
5772
5773 sprintf(num_buf, "%ld", num);
5774 num_len = STRLEN(num_buf);
5775 result = num_len;
5776
5777 if (quote)
5778 result += 2;
5779
5780 if (buf != NULL)
5781 {
5782 if (quote)
5783 *buf++ = '"';
5784 STRCPY(buf, num_buf);
5785 buf += num_len;
5786 if (quote)
5787 *buf = '"';
5788 }
5789
5790 break;
5791 }
5792
5793 case ct_REGISTER:
5794 result = eap->regname ? 1 : 0;
5795 if (quote)
5796 result += 2;
5797 if (buf != NULL)
5798 {
5799 if (quote)
5800 *buf++ = '\'';
5801 if (eap->regname)
5802 *buf++ = eap->regname;
5803 if (quote)
5804 *buf = '\'';
5805 }
5806 break;
5807
5808 case ct_LT:
5809 result = 1;
5810 if (buf != NULL)
5811 *buf = '<';
5812 break;
5813
5814 default:
5815 /* Not recognized: just copy the '<' and return -1. */
5816 result = (size_t)-1;
5817 if (buf != NULL)
5818 *buf = '<';
5819 break;
5820 }
5821
5822 return result;
5823}
5824
5825 static void
5826do_ucmd(eap)
5827 exarg_T *eap;
5828{
5829 char_u *buf;
5830 char_u *p;
5831 char_u *q;
5832
5833 char_u *start;
5834 char_u *end;
5835 size_t len, totlen;
5836
5837 size_t split_len = 0;
5838 char_u *split_buf = NULL;
5839 ucmd_T *cmd;
5840#ifdef FEAT_EVAL
5841 scid_T save_current_SID = current_SID;
5842#endif
5843
5844 if (eap->cmdidx == CMD_USER)
5845 cmd = USER_CMD(eap->useridx);
5846 else
5847 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5848
5849 /*
5850 * Replace <> in the command by the arguments.
5851 */
5852 buf = NULL;
5853 for (;;)
5854 {
5855 p = cmd->uc_rep;
5856 q = buf;
5857 totlen = 0;
5858 while ((start = vim_strchr(p, '<')) != NULL
5859 && (end = vim_strchr(start + 1, '>')) != NULL)
5860 {
5861 /* Include the '>' */
5862 ++end;
5863
5864 /* Take everything up to the '<' */
5865 len = start - p;
5866 if (buf == NULL)
5867 totlen += len;
5868 else
5869 {
5870 mch_memmove(q, p, len);
5871 q += len;
5872 }
5873
5874 len = uc_check_code(start, end - start, q, cmd, eap,
5875 &split_buf, &split_len);
5876 if (len == (size_t)-1)
5877 {
5878 /* no match, continue after '<' */
5879 p = start + 1;
5880 len = 1;
5881 }
5882 else
5883 p = end;
5884 if (buf == NULL)
5885 totlen += len;
5886 else
5887 q += len;
5888 }
5889 if (buf != NULL) /* second time here, finished */
5890 {
5891 STRCPY(q, p);
5892 break;
5893 }
5894
5895 totlen += STRLEN(p); /* Add on the trailing characters */
5896 buf = alloc((unsigned)(totlen + 1));
5897 if (buf == NULL)
5898 {
5899 vim_free(split_buf);
5900 return;
5901 }
5902 }
5903
5904#ifdef FEAT_EVAL
5905 current_SID = cmd->uc_scriptID;
5906#endif
5907 (void)do_cmdline(buf, eap->getline, eap->cookie,
5908 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5909#ifdef FEAT_EVAL
5910 current_SID = save_current_SID;
5911#endif
5912 vim_free(buf);
5913 vim_free(split_buf);
5914}
5915
5916# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5917 static char_u *
5918get_user_command_name(idx)
5919 int idx;
5920{
5921 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5922}
5923
5924/*
5925 * Function given to ExpandGeneric() to obtain the list of user command names.
5926 */
5927/*ARGSUSED*/
5928 char_u *
5929get_user_commands(xp, idx)
5930 expand_T *xp;
5931 int idx;
5932{
5933 if (idx < curbuf->b_ucmds.ga_len)
5934 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5935 idx -= curbuf->b_ucmds.ga_len;
5936 if (idx < ucmds.ga_len)
5937 return USER_CMD(idx)->uc_name;
5938 return NULL;
5939}
5940
5941/*
5942 * Function given to ExpandGeneric() to obtain the list of user command
5943 * attributes.
5944 */
5945/*ARGSUSED*/
5946 char_u *
5947get_user_cmd_flags(xp, idx)
5948 expand_T *xp;
5949 int idx;
5950{
5951 static char *user_cmd_flags[] =
5952 {"bang", "bar", "buffer", "complete", "count",
5953 "nargs", "range", "register"};
5954
5955 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5956 return NULL;
5957 return (char_u *)user_cmd_flags[idx];
5958}
5959
5960/*
5961 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5962 */
5963/*ARGSUSED*/
5964 char_u *
5965get_user_cmd_nargs(xp, idx)
5966 expand_T *xp;
5967 int idx;
5968{
5969 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5970
5971 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5972 return NULL;
5973 return (char_u *)user_cmd_nargs[idx];
5974}
5975
5976/*
5977 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5978 */
5979/*ARGSUSED*/
5980 char_u *
5981get_user_cmd_complete(xp, idx)
5982 expand_T *xp;
5983 int idx;
5984{
5985 return (char_u *)command_complete[idx].name;
5986}
5987# endif /* FEAT_CMDL_COMPL */
5988
5989#endif /* FEAT_USR_CMDS */
5990
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005991#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5992/*
5993 * Parse a completion argument "value[vallen]".
5994 * The detected completion goes in "*complp", argument type in "*argt".
5995 * When there is an argument, for function and user defined completion, it's
5996 * copied to allocated memory and stored in "*compl_arg".
5997 * Returns FAIL if something is wrong.
5998 */
5999 int
6000parse_compl_arg(value, vallen, complp, argt, compl_arg)
6001 char_u *value;
6002 int vallen;
6003 int *complp;
6004 long *argt;
6005 char_u **compl_arg;
6006{
6007 char_u *arg = NULL;
6008 size_t arglen = 0;
6009 int i;
6010 int valend = vallen;
6011
6012 /* Look for any argument part - which is the part after any ',' */
6013 for (i = 0; i < vallen; ++i)
6014 {
6015 if (value[i] == ',')
6016 {
6017 arg = &value[i + 1];
6018 arglen = vallen - i - 1;
6019 valend = i;
6020 break;
6021 }
6022 }
6023
6024 for (i = 0; command_complete[i].expand != 0; ++i)
6025 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006026 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006027 && STRNCMP(value, command_complete[i].name, valend) == 0)
6028 {
6029 *complp = command_complete[i].expand;
6030 if (command_complete[i].expand == EXPAND_BUFFERS)
6031 *argt |= BUFNAME;
6032 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6033 || command_complete[i].expand == EXPAND_FILES)
6034 *argt |= XFILE;
6035 break;
6036 }
6037 }
6038
6039 if (command_complete[i].expand == 0)
6040 {
6041 EMSG2(_("E180: Invalid complete value: %s"), value);
6042 return FAIL;
6043 }
6044
6045# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6046 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6047 && arg != NULL)
6048# else
6049 if (arg != NULL)
6050# endif
6051 {
6052 EMSG(_("E468: Completion argument only allowed for custom completion"));
6053 return FAIL;
6054 }
6055
6056# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6057 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6058 && arg == NULL)
6059 {
6060 EMSG(_("E467: Custom completion requires a function argument"));
6061 return FAIL;
6062 }
6063
6064 if (arg != NULL)
6065 *compl_arg = vim_strnsave(arg, (int)arglen);
6066# endif
6067 return OK;
6068}
6069#endif
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 static void
6072ex_colorscheme(eap)
6073 exarg_T *eap;
6074{
6075 if (load_colors(eap->arg) == FAIL)
6076 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6077}
6078
6079 static void
6080ex_highlight(eap)
6081 exarg_T *eap;
6082{
6083 if (*eap->arg == NUL && eap->cmd[2] == '!')
6084 MSG(_("Greetings, Vim user!"));
6085 do_highlight(eap->arg, eap->forceit, FALSE);
6086}
6087
6088
6089/*
6090 * Call this function if we thought we were going to exit, but we won't
6091 * (because of an error). May need to restore the terminal mode.
6092 */
6093 void
6094not_exiting()
6095{
6096 exiting = FALSE;
6097 settmode(TMODE_RAW);
6098}
6099
6100/*
6101 * ":quit": quit current window, quit Vim if closed the last window.
6102 */
6103 static void
6104ex_quit(eap)
6105 exarg_T *eap;
6106{
6107#ifdef FEAT_CMDWIN
6108 if (cmdwin_type != 0)
6109 {
6110 cmdwin_result = Ctrl_C;
6111 return;
6112 }
6113#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006114 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006115 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006116 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006117 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006118 return;
6119 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006120#ifdef FEAT_AUTOCMD
6121 if (curbuf_locked())
6122 return;
6123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124
6125#ifdef FEAT_NETBEANS_INTG
6126 netbeansForcedQuit = eap->forceit;
6127#endif
6128
6129 /*
6130 * If there are more files or windows we won't exit.
6131 */
6132 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6133 exiting = TRUE;
6134 if ((!P_HID(curbuf)
6135 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6136 || check_more(TRUE, eap->forceit) == FAIL
6137 || (only_one_window() && check_changed_any(eap->forceit)))
6138 {
6139 not_exiting();
6140 }
6141 else
6142 {
6143#ifdef FEAT_WINDOWS
6144 if (only_one_window()) /* quit last window */
6145#endif
6146 getout(0);
6147#ifdef FEAT_WINDOWS
6148# ifdef FEAT_GUI
6149 need_mouse_correct = TRUE;
6150# endif
6151 /* close window; may free buffer */
6152 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6153#endif
6154 }
6155}
6156
6157/*
6158 * ":cquit".
6159 */
6160/*ARGSUSED*/
6161 static void
6162ex_cquit(eap)
6163 exarg_T *eap;
6164{
6165 getout(1); /* this does not always pass on the exit code to the Manx
6166 compiler. why? */
6167}
6168
6169/*
6170 * ":qall": try to quit all windows
6171 */
6172 static void
6173ex_quit_all(eap)
6174 exarg_T *eap;
6175{
6176# ifdef FEAT_CMDWIN
6177 if (cmdwin_type != 0)
6178 {
6179 if (eap->forceit)
6180 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6181 else
6182 cmdwin_result = K_XF2;
6183 return;
6184 }
6185# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006186
6187 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006188 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006189 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006190 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006191 return;
6192 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006193#ifdef FEAT_AUTOCMD
6194 if (curbuf_locked())
6195 return;
6196#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006197
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 exiting = TRUE;
6199 if (eap->forceit || !check_changed_any(FALSE))
6200 getout(0);
6201 not_exiting();
6202}
6203
Bram Moolenaard9967712006-03-11 21:18:15 +00006204#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205/*
6206 * ":close": close current window, unless it is the last one
6207 */
6208 static void
6209ex_close(eap)
6210 exarg_T *eap;
6211{
6212# ifdef FEAT_CMDWIN
6213 if (cmdwin_type != 0)
6214 cmdwin_result = K_IGNORE;
6215 else
6216# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006217 if (!text_locked()
6218#ifdef FEAT_AUTOCMD
6219 && !curbuf_locked()
6220#endif
6221 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006222 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223}
6224
Bram Moolenaard9967712006-03-11 21:18:15 +00006225# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226/*
6227 * ":pclose": Close any preview window.
6228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006230ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232{
6233 win_T *win;
6234
6235 for (win = firstwin; win != NULL; win = win->w_next)
6236 if (win->w_p_pvw)
6237 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006238 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006239 break;
6240 }
6241}
Bram Moolenaard9967712006-03-11 21:18:15 +00006242# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243
Bram Moolenaarf740b292006-02-16 22:11:02 +00006244/*
6245 * Close window "win" and take care of handling closing the last window for a
6246 * modified buffer.
6247 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006249ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006250 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006252 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 int need_hide;
6255 buf_T *buf = win->w_buffer;
6256
6257 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006258 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006260# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 if ((p_confirm || cmdmod.confirm) && p_write)
6262 {
6263 dialog_changed(buf, FALSE);
6264 if (buf_valid(buf) && bufIsChanged(buf))
6265 return;
6266 need_hide = FALSE;
6267 }
6268 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 EMSG(_(e_nowrtmsg));
6272 return;
6273 }
6274 }
6275
Bram Moolenaard9967712006-03-11 21:18:15 +00006276# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006278# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006279
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006281 if (tp == NULL)
6282 win_close(win, !need_hide && !P_HID(buf));
6283 else
6284 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285}
6286
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006288 * ":tabclose": close current tab page, unless it is the last one.
6289 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 */
6291 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006292ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 exarg_T *eap;
6294{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006295 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006296 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006297
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006298# ifdef FEAT_CMDWIN
6299 if (cmdwin_type != 0)
6300 cmdwin_result = K_IGNORE;
6301 else
6302# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006303 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006304 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006305 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006307 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006308 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006309 tp = find_tabpage((int)eap->line2);
6310 if (tp == NULL)
6311 {
6312 beep_flush();
6313 return;
6314 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006315 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006316 {
6317 tabpage_close_other(tp, eap->forceit);
6318 return;
6319 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006320 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006321 if (!text_locked()
6322#ifdef FEAT_AUTOCMD
6323 && !curbuf_locked()
6324#endif
6325 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006326 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006328
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006329 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006330 shell_new_rows();
6331}
6332
6333/*
6334 * ":tabonly": close all tab pages except the current one
6335 */
6336 static void
6337ex_tabonly(eap)
6338 exarg_T *eap;
6339{
6340 tabpage_T *tp;
6341 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006342 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006343
6344# ifdef FEAT_CMDWIN
6345 if (cmdwin_type != 0)
6346 cmdwin_result = K_IGNORE;
6347 else
6348# endif
6349 if (first_tabpage->tp_next == NULL)
6350 MSG(_("Already only one tab page"));
6351 else
6352 {
6353 /* Repeat this up to a 1000 times, because autocommands may mess
6354 * up the lists. */
6355 for (done = 0; done < 1000; ++done)
6356 {
6357 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6358 if (tp->tp_topframe != topframe)
6359 {
6360 tabpage_close_other(tp, eap->forceit);
6361 /* if we failed to close it quit */
6362 if (valid_tabpage(tp))
6363 done = 1000;
6364 /* start over, "tp" is now invalid */
6365 break;
6366 }
6367 if (first_tabpage->tp_next == NULL)
6368 break;
6369 }
6370 }
6371
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006372 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006373 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006377 * Close the current tab page.
6378 */
6379 void
6380tabpage_close(forceit)
6381 int forceit;
6382{
6383 /* First close all the windows but the current one. If that worked then
6384 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006385 if (lastwin != firstwin)
6386 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006387 if (lastwin == firstwin)
6388 ex_win_close(forceit, curwin, NULL);
6389# ifdef FEAT_GUI
6390 need_mouse_correct = TRUE;
6391# endif
6392}
6393
6394/*
6395 * Close tab page "tp", which is not the current tab page.
6396 * Note that autocommands may make "tp" invalid.
6397 */
6398 void
6399tabpage_close_other(tp, forceit)
6400 tabpage_T *tp;
6401 int forceit;
6402{
6403 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006404 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006405
6406 /* Limit to 1000 windows, autocommands may add a window while we close
6407 * one. OK, so I'm paranoid... */
6408 while (++done < 1000)
6409 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006410 wp = tp->tp_firstwin;
6411 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006412
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006413 /* Autocommands may delete the tab page under our fingers and we may
6414 * fail to close a window with a modified buffer. */
6415 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006416 break;
6417 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006418 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006419}
6420
6421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 * ":only".
6423 */
6424 static void
6425ex_only(eap)
6426 exarg_T *eap;
6427{
6428# ifdef FEAT_GUI
6429 need_mouse_correct = TRUE;
6430# endif
6431 close_others(TRUE, eap->forceit);
6432}
6433
6434/*
6435 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006436 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006438 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439ex_all(eap)
6440 exarg_T *eap;
6441{
6442 if (eap->addr_count == 0)
6443 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006444 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445}
6446#endif /* FEAT_WINDOWS */
6447
6448 static void
6449ex_hide(eap)
6450 exarg_T *eap;
6451{
6452 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6453 eap->errmsg = e_invarg;
6454 else
6455 {
6456 /* ":hide" or ":hide | cmd": hide current window */
6457 eap->nextcmd = check_nextcmd(eap->arg);
6458#ifdef FEAT_WINDOWS
6459 if (!eap->skip)
6460 {
6461# ifdef FEAT_GUI
6462 need_mouse_correct = TRUE;
6463# endif
6464 win_close(curwin, FALSE); /* don't free buffer */
6465 }
6466#endif
6467 }
6468}
6469
6470/*
6471 * ":stop" and ":suspend": Suspend Vim.
6472 */
6473 static void
6474ex_stop(eap)
6475 exarg_T *eap;
6476{
6477 /*
6478 * Disallow suspending for "rvim".
6479 */
6480 if (!check_restricted()
6481#ifdef WIN3264
6482 /*
6483 * Check if external commands are allowed now.
6484 */
6485 && can_end_termcap_mode(TRUE)
6486#endif
6487 )
6488 {
6489 if (!eap->forceit)
6490 autowrite_all();
6491 windgoto((int)Rows - 1, 0);
6492 out_char('\n');
6493 out_flush();
6494 stoptermcap();
6495 out_flush(); /* needed for SUN to restore xterm buffer */
6496#ifdef FEAT_TITLE
6497 mch_restore_title(3); /* restore window titles */
6498#endif
6499 ui_suspend(); /* call machine specific function */
6500#ifdef FEAT_TITLE
6501 maketitle();
6502 resettitle(); /* force updating the title */
6503#endif
6504 starttermcap();
6505 scroll_start(); /* scroll screen before redrawing */
6506 redraw_later_clear();
6507 shell_resized(); /* may have resized window */
6508 }
6509}
6510
6511/*
6512 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6513 */
6514 static void
6515ex_exit(eap)
6516 exarg_T *eap;
6517{
6518#ifdef FEAT_CMDWIN
6519 if (cmdwin_type != 0)
6520 {
6521 cmdwin_result = Ctrl_C;
6522 return;
6523 }
6524#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006525 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006526 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006527 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006528 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006529 return;
6530 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006531#ifdef FEAT_AUTOCMD
6532 if (curbuf_locked())
6533 return;
6534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
6536 /*
6537 * if more files or windows we won't exit
6538 */
6539 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6540 exiting = TRUE;
6541 if ( ((eap->cmdidx == CMD_wq
6542 || curbufIsChanged())
6543 && do_write(eap) == FAIL)
6544 || check_more(TRUE, eap->forceit) == FAIL
6545 || (only_one_window() && check_changed_any(eap->forceit)))
6546 {
6547 not_exiting();
6548 }
6549 else
6550 {
6551#ifdef FEAT_WINDOWS
6552 if (only_one_window()) /* quit last window, exit Vim */
6553#endif
6554 getout(0);
6555#ifdef FEAT_WINDOWS
6556# ifdef FEAT_GUI
6557 need_mouse_correct = TRUE;
6558# endif
6559 /* quit current window, may free buffer */
6560 win_close(curwin, !P_HID(curwin->w_buffer));
6561#endif
6562 }
6563}
6564
6565/*
6566 * ":print", ":list", ":number".
6567 */
6568 static void
6569ex_print(eap)
6570 exarg_T *eap;
6571{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006572 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6573 EMSG(_(e_emptybuf));
6574 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006576 for ( ;!got_int; ui_breakcheck())
6577 {
6578 print_line(eap->line1,
6579 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6580 || (eap->flags & EXFLAG_NR)),
6581 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6582 if (++eap->line1 > eap->line2)
6583 break;
6584 out_flush(); /* show one line at a time */
6585 }
6586 setpcmark();
6587 /* put cursor at last line */
6588 curwin->w_cursor.lnum = eap->line2;
6589 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 }
6591
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593}
6594
6595#ifdef FEAT_BYTEOFF
6596 static void
6597ex_goto(eap)
6598 exarg_T *eap;
6599{
6600 goto_byte(eap->line2);
6601}
6602#endif
6603
6604/*
6605 * ":shell".
6606 */
6607/*ARGSUSED*/
6608 static void
6609ex_shell(eap)
6610 exarg_T *eap;
6611{
6612 do_shell(NULL, 0);
6613}
6614
6615#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6616 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006617 || defined(FEAT_GUI_MSWIN) \
6618 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 || defined(PROTO)
6620
6621/*
6622 * Handle a file drop. The code is here because a drop is *nearly* like an
6623 * :args command, but not quite (we have a list of exact filenames, so we
6624 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6625 * code is very similar to :args and hence needs access to a lot of the static
6626 * functions in this file.
6627 *
6628 * The list should be allocated using alloc(), as should each item in the
6629 * list. This function takes over responsibility for freeing the list.
6630 *
6631 * XXX The list is made into the arggument list. This is freed using
6632 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6633 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6634 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6635 * file functionality is (currently) not in EMX this is not presently a
6636 * problem.
6637 */
6638 void
6639handle_drop(filec, filev, split)
6640 int filec; /* the number of files dropped */
6641 char_u **filev; /* the list of files dropped */
6642 int split; /* force splitting the window */
6643{
6644 exarg_T ea;
6645 int save_msg_scroll = msg_scroll;
6646
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006647 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006648 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006650#ifdef FEAT_AUTOCMD
6651 if (curbuf_locked())
6652 return;
6653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654
6655 /* Check whether the current buffer is changed. If so, we will need
6656 * to split the current window or data could be lost.
6657 * We don't need to check if the 'hidden' option is set, as in this
6658 * case the buffer won't be lost.
6659 */
6660 if (!P_HID(curbuf) && !split)
6661 {
6662 ++emsg_off;
6663 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6664 --emsg_off;
6665 }
6666 if (split)
6667 {
6668# ifdef FEAT_WINDOWS
6669 if (win_split(0, 0) == FAIL)
6670 return;
6671# ifdef FEAT_SCROLLBIND
6672 curwin->w_p_scb = FALSE;
6673# endif
6674
6675 /* When splitting the window, create a new alist. Otherwise the
6676 * existing one is overwritten. */
6677 alist_unlink(curwin->w_alist);
6678 alist_new();
6679# else
6680 return; /* can't split, always fail */
6681# endif
6682 }
6683
6684 /*
6685 * Set up the new argument list.
6686 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006687 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
6689 /*
6690 * Move to the first file.
6691 */
6692 /* Fake up a minimal "next" command for do_argfile() */
6693 vim_memset(&ea, 0, sizeof(ea));
6694 ea.cmd = (char_u *)"next";
6695 do_argfile(&ea, 0);
6696
6697 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6698 * mode that is not needed here. */
6699 need_start_insertmode = FALSE;
6700
6701 /* Restore msg_scroll, otherwise a following command may cause scrolling
6702 * unexpectedly. The screen will be redrawn by the caller, thus
6703 * msg_scroll being set by displaying a message is irrelevant. */
6704 msg_scroll = save_msg_scroll;
6705}
6706#endif
6707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708/*
6709 * Clear an argument list: free all file names and reset it to zero entries.
6710 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006711 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712alist_clear(al)
6713 alist_T *al;
6714{
6715 while (--al->al_ga.ga_len >= 0)
6716 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6717 ga_clear(&al->al_ga);
6718}
6719
6720/*
6721 * Init an argument list.
6722 */
6723 void
6724alist_init(al)
6725 alist_T *al;
6726{
6727 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6728}
6729
6730#if defined(FEAT_WINDOWS) || defined(PROTO)
6731
6732/*
6733 * Remove a reference from an argument list.
6734 * Ignored when the argument list is the global one.
6735 * If the argument list is no longer used by any window, free it.
6736 */
6737 void
6738alist_unlink(al)
6739 alist_T *al;
6740{
6741 if (al != &global_alist && --al->al_refcount <= 0)
6742 {
6743 alist_clear(al);
6744 vim_free(al);
6745 }
6746}
6747
6748# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6749/*
6750 * Create a new argument list and use it for the current window.
6751 */
6752 void
6753alist_new()
6754{
6755 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6756 if (curwin->w_alist == NULL)
6757 {
6758 curwin->w_alist = &global_alist;
6759 ++global_alist.al_refcount;
6760 }
6761 else
6762 {
6763 curwin->w_alist->al_refcount = 1;
6764 alist_init(curwin->w_alist);
6765 }
6766}
6767# endif
6768#endif
6769
6770#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6771/*
6772 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006773 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6774 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 */
6776 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006777alist_expand(fnum_list, fnum_len)
6778 int *fnum_list;
6779 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006782 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 char_u **new_arg_files;
6784 int new_arg_file_count;
6785 char_u *save_p_su = p_su;
6786 int i;
6787
6788 /* Don't use 'suffixes' here. This should work like the shell did the
6789 * expansion. Also, the vimrc file isn't read yet, thus the user
6790 * can't set the options. */
6791 p_su = empty_option;
6792 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6793 if (old_arg_files != NULL)
6794 {
6795 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006796 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6797 old_arg_count = GARGCOUNT;
6798 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 &new_arg_file_count, &new_arg_files,
6800 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6801 && new_arg_file_count > 0)
6802 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006803 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6804 TRUE, fnum_list, fnum_len);
6805 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808 p_su = save_p_su;
6809}
6810#endif
6811
6812/*
6813 * Set the argument list for the current window.
6814 * Takes over the allocated files[] and the allocated fnames in it.
6815 */
6816 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006817alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 alist_T *al;
6819 int count;
6820 char_u **files;
6821 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006822 int *fnum_list;
6823 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
6825 int i;
6826
6827 alist_clear(al);
6828 if (ga_grow(&al->al_ga, count) == OK)
6829 {
6830 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006831 {
6832 if (got_int)
6833 {
6834 /* When adding many buffers this can take a long time. Allow
6835 * interrupting here. */
6836 while (i < count)
6837 vim_free(files[i++]);
6838 break;
6839 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006840
6841 /* May set buffer name of a buffer previously used for the
6842 * argument list, so that it's re-used by alist_add. */
6843 if (fnum_list != NULL && i < fnum_len)
6844 buf_set_name(fnum_list[i], files[i]);
6845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006847 ui_breakcheck();
6848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 vim_free(files);
6850 }
6851 else
6852 FreeWild(count, files);
6853#ifdef FEAT_WINDOWS
6854 if (al == &global_alist)
6855#endif
6856 arg_had_last = FALSE;
6857}
6858
6859/*
6860 * Add file "fname" to argument list "al".
6861 * "fname" must have been allocated and "al" must have been checked for room.
6862 */
6863 void
6864alist_add(al, fname, set_fnum)
6865 alist_T *al;
6866 char_u *fname;
6867 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6868{
6869 if (fname == NULL) /* don't add NULL file names */
6870 return;
6871#ifdef BACKSLASH_IN_FILENAME
6872 slash_adjust(fname);
6873#endif
6874 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6875 if (set_fnum > 0)
6876 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6877 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6878 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879}
6880
6881#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6882/*
6883 * Adjust slashes in file names. Called after 'shellslash' was set.
6884 */
6885 void
6886alist_slash_adjust()
6887{
6888 int i;
6889# ifdef FEAT_WINDOWS
6890 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006891 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892# endif
6893
6894 for (i = 0; i < GARGCOUNT; ++i)
6895 if (GARGLIST[i].ae_fname != NULL)
6896 slash_adjust(GARGLIST[i].ae_fname);
6897# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006898 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 if (wp->w_alist != &global_alist)
6900 for (i = 0; i < WARGCOUNT(wp); ++i)
6901 if (WARGLIST(wp)[i].ae_fname != NULL)
6902 slash_adjust(WARGLIST(wp)[i].ae_fname);
6903# endif
6904}
6905#endif
6906
6907/*
6908 * ":preserve".
6909 */
6910/*ARGSUSED*/
6911 static void
6912ex_preserve(eap)
6913 exarg_T *eap;
6914{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006915 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 ml_preserve(curbuf, TRUE);
6917}
6918
6919/*
6920 * ":recover".
6921 */
6922 static void
6923ex_recover(eap)
6924 exarg_T *eap;
6925{
6926 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6927 recoverymode = TRUE;
6928 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6929 && (*eap->arg == NUL
6930 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6931 ml_recover();
6932 recoverymode = FALSE;
6933}
6934
6935/*
6936 * Command modifier used in a wrong way.
6937 */
6938 static void
6939ex_wrongmodifier(eap)
6940 exarg_T *eap;
6941{
6942 eap->errmsg = e_invcmd;
6943}
6944
6945#ifdef FEAT_WINDOWS
6946/*
6947 * :sview [+command] file split window with new file, read-only
6948 * :split [[+command] file] split window with current or new file
6949 * :vsplit [[+command] file] split window vertically with current or new file
6950 * :new [[+command] file] split window with no or new file
6951 * :vnew [[+command] file] split vertically window with no or new file
6952 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006953 *
6954 * :tabedit open new Tab page with empty window
6955 * :tabedit [+command] file open new Tab page and edit "file"
6956 * :tabnew [[+command] file] just like :tabedit
6957 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 */
6959 void
6960ex_splitview(eap)
6961 exarg_T *eap;
6962{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006963 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006964# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006966# endif
6967# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006971# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6973 {
6974 ex_ni(eap);
6975 return;
6976 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006979# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006983# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 /* A ":split" in the quickfix window works like ":new". Don't want two
6985 * quickfix windows. */
6986 if (bt_quickfix(curbuf))
6987 {
6988 if (eap->cmdidx == CMD_split)
6989 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006990# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (eap->cmdidx == CMD_vsplit)
6992 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006997# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006998 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {
7000 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7001 FNAME_MESS, TRUE, curbuf->b_ffname);
7002 if (fname == NULL)
7003 goto theend;
7004 eap->arg = fname;
7005 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007006# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007008# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007010# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007012# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 && eap->cmdidx != CMD_new)
7016 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007017 if (
7018# ifdef FEAT_GUI
7019 !gui.in_use &&
7020# endif
7021 au_has_group((char_u *)"FileExplorer"))
7022 {
7023 /* No browsing supported but we do have the file explorer:
7024 * Edit the directory. */
7025 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7026 eap->arg = (char_u *)".";
7027 }
7028 else
7029 {
7030 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007032 if (fname == NULL)
7033 goto theend;
7034 eap->arg = fname;
7035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007040 /*
7041 * Either open new tab page or split the window.
7042 */
7043 if (eap->cmdidx == CMD_tabedit
7044 || eap->cmdidx == CMD_tabfind
7045 || eap->cmdidx == CMD_tabnew)
7046 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007047 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7048 : eap->addr_count == 0 ? 0
7049 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007050 {
7051 do_exedit(eap, NULL);
7052
7053 /* set the alternate buffer for the window we came from */
7054 if (curwin != old_curwin
7055 && win_valid(old_curwin)
7056 && old_curwin->w_buffer != curbuf
7057 && !cmdmod.keepalt)
7058 old_curwin->w_alt_fnum = curbuf->b_fnum;
7059 }
7060 }
7061 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7063 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007064# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 /* Reset 'scrollbind' when editing another file, but keep it when
7066 * doing ":split" without arguments. */
7067 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007068# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 )
7072 curwin->w_p_scb = FALSE;
7073 else
7074 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 do_exedit(eap, old_curwin);
7077 }
7078
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007079# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007083# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084theend:
7085 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007088
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007089#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007090/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007091 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007092 */
7093 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007094tabpage_new()
7095{
7096 exarg_T ea;
7097
7098 vim_memset(&ea, 0, sizeof(ea));
7099 ea.cmdidx = CMD_tabnew;
7100 ea.cmd = (char_u *)"tabn";
7101 ea.arg = (char_u *)"";
7102 ex_splitview(&ea);
7103}
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007104#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007105
7106/*
7107 * :tabnext command
7108 */
7109 static void
7110ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007111 exarg_T *eap;
7112{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007113 switch (eap->cmdidx)
7114 {
7115 case CMD_tabfirst:
7116 case CMD_tabrewind:
7117 goto_tabpage(1);
7118 break;
7119 case CMD_tablast:
7120 goto_tabpage(9999);
7121 break;
7122 case CMD_tabprevious:
7123 case CMD_tabNext:
7124 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7125 break;
7126 default: /* CMD_tabnext */
7127 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7128 break;
7129 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007130}
7131
7132/*
7133 * :tabmove command
7134 */
7135 static void
7136ex_tabmove(eap)
7137 exarg_T *eap;
7138{
7139 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007140}
7141
7142/*
7143 * :tabs command: List tabs and their contents.
7144 */
7145/*ARGSUSED*/
7146 static void
7147ex_tabs(eap)
7148 exarg_T *eap;
7149{
7150 tabpage_T *tp;
7151 win_T *wp;
7152 int tabcount = 1;
7153
7154 msg_start();
7155 msg_scroll = TRUE;
7156 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7157 {
7158 msg_putchar('\n');
7159 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7160 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7161 out_flush(); /* output one line at a time */
7162 ui_breakcheck();
7163
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007164 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007165 wp = firstwin;
7166 else
7167 wp = tp->tp_firstwin;
7168 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7169 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007170 msg_putchar('\n');
7171 msg_putchar(wp == curwin ? '>' : ' ');
7172 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007173 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7174 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007175 if (buf_spname(wp->w_buffer) != NULL)
7176 STRCPY(IObuff, buf_spname(wp->w_buffer));
7177 else
7178 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7179 IObuff, IOSIZE, TRUE);
7180 msg_outtrans(IObuff);
7181 out_flush(); /* output one line at a time */
7182 ui_breakcheck();
7183 }
7184 }
7185}
7186
7187#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
7189/*
7190 * ":mode": Set screen mode.
7191 * If no argument given, just get the screen size and redraw.
7192 */
7193 static void
7194ex_mode(eap)
7195 exarg_T *eap;
7196{
7197 if (*eap->arg == NUL)
7198 shell_resized();
7199 else
7200 mch_screenmode(eap->arg);
7201}
7202
7203#ifdef FEAT_WINDOWS
7204/*
7205 * ":resize".
7206 * set, increment or decrement current window height
7207 */
7208 static void
7209ex_resize(eap)
7210 exarg_T *eap;
7211{
7212 int n;
7213 win_T *wp = curwin;
7214
7215 if (eap->addr_count > 0)
7216 {
7217 n = eap->line2;
7218 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7219 ;
7220 }
7221
7222#ifdef FEAT_GUI
7223 need_mouse_correct = TRUE;
7224#endif
7225 n = atol((char *)eap->arg);
7226#ifdef FEAT_VERTSPLIT
7227 if (cmdmod.split & WSP_VERT)
7228 {
7229 if (*eap->arg == '-' || *eap->arg == '+')
7230 n += W_WIDTH(curwin);
7231 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7232 n = 9999;
7233 win_setwidth_win((int)n, wp);
7234 }
7235 else
7236#endif
7237 {
7238 if (*eap->arg == '-' || *eap->arg == '+')
7239 n += curwin->w_height;
7240 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7241 n = 9999;
7242 win_setheight_win((int)n, wp);
7243 }
7244}
7245#endif
7246
7247/*
7248 * ":find [+command] <file>" command.
7249 */
7250 static void
7251ex_find(eap)
7252 exarg_T *eap;
7253{
7254#ifdef FEAT_SEARCHPATH
7255 char_u *fname;
7256 int count;
7257
7258 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7259 TRUE, curbuf->b_ffname);
7260 if (eap->addr_count > 0)
7261 {
7262 /* Repeat finding the file "count" times. This matters when it
7263 * appears several times in the path. */
7264 count = eap->line2;
7265 while (fname != NULL && --count > 0)
7266 {
7267 vim_free(fname);
7268 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7269 FALSE, curbuf->b_ffname);
7270 }
7271 }
7272
7273 if (fname != NULL)
7274 {
7275 eap->arg = fname;
7276#endif
7277 do_exedit(eap, NULL);
7278#ifdef FEAT_SEARCHPATH
7279 vim_free(fname);
7280 }
7281#endif
7282}
7283
7284/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007285 * ":open" simulation: for now just work like ":visual".
7286 */
7287 static void
7288ex_open(eap)
7289 exarg_T *eap;
7290{
7291 regmatch_T regmatch;
7292 char_u *p;
7293
7294 curwin->w_cursor.lnum = eap->line2;
7295 beginline(BL_SOL | BL_FIX);
7296 if (*eap->arg == '/')
7297 {
7298 /* ":open /pattern/": put cursor in column found with pattern */
7299 ++eap->arg;
7300 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7301 *p = NUL;
7302 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7303 if (regmatch.regprog != NULL)
7304 {
7305 regmatch.rm_ic = p_ic;
7306 p = ml_get_curline();
7307 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007308 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007309 else
7310 EMSG(_(e_nomatch));
7311 vim_free(regmatch.regprog);
7312 }
7313 /* Move to the NUL, ignore any other arguments. */
7314 eap->arg += STRLEN(eap->arg);
7315 }
7316 check_cursor();
7317
7318 eap->cmdidx = CMD_visual;
7319 do_exedit(eap, NULL);
7320}
7321
7322/*
7323 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 */
7325 static void
7326ex_edit(eap)
7327 exarg_T *eap;
7328{
7329 do_exedit(eap, NULL);
7330}
7331
7332/*
7333 * ":edit <file>" command and alikes.
7334 */
7335/*ARGSUSED*/
7336 void
7337do_exedit(eap, old_curwin)
7338 exarg_T *eap;
7339 win_T *old_curwin; /* curwin before doing a split or NULL */
7340{
7341 int n;
7342#ifdef FEAT_WINDOWS
7343 int need_hide;
7344#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007345 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346
7347 /*
7348 * ":vi" command ends Ex mode.
7349 */
7350 if (exmode_active && (eap->cmdidx == CMD_visual
7351 || eap->cmdidx == CMD_view))
7352 {
7353 exmode_active = FALSE;
7354 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007355 {
7356 /* Special case: ":global/pat/visual\NLvi-commands" */
7357 if (global_busy)
7358 {
7359 int rd = RedrawingDisabled;
7360 int nwr = no_wait_return;
7361 int ms = msg_scroll;
7362#ifdef FEAT_GUI
7363 int he = hold_gui_events;
7364#endif
7365
7366 if (eap->nextcmd != NULL)
7367 {
7368 stuffReadbuff(eap->nextcmd);
7369 eap->nextcmd = NULL;
7370 }
7371
7372 if (exmode_was != EXMODE_VIM)
7373 settmode(TMODE_RAW);
7374 RedrawingDisabled = 0;
7375 no_wait_return = 0;
7376 need_wait_return = FALSE;
7377 msg_scroll = 0;
7378#ifdef FEAT_GUI
7379 hold_gui_events = 0;
7380#endif
7381 must_redraw = CLEAR;
7382
7383 main_loop(FALSE, TRUE);
7384
7385 RedrawingDisabled = rd;
7386 no_wait_return = nwr;
7387 msg_scroll = ms;
7388#ifdef FEAT_GUI
7389 hold_gui_events = he;
7390#endif
7391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 }
7395
7396 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007397 || eap->cmdidx == CMD_tabnew
7398 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399#ifdef FEAT_VERTSPLIT
7400 || eap->cmdidx == CMD_vnew
7401#endif
7402 ) && *eap->arg == NUL)
7403 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007404 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 setpcmark();
7406 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7407 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7408 }
7409 else if ((eap->cmdidx != CMD_split
7410#ifdef FEAT_VERTSPLIT
7411 && eap->cmdidx != CMD_vsplit
7412#endif
7413 )
7414 || *eap->arg != NUL
7415#ifdef FEAT_BROWSE
7416 || cmdmod.browse
7417#endif
7418 )
7419 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007420#ifdef FEAT_AUTOCMD
7421 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7422 * can bring us here, others are stopped earlier. */
7423 if (*eap->arg != NUL && curbuf_locked())
7424 return;
7425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 n = readonlymode;
7427 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7428 readonlymode = TRUE;
7429 else if (eap->cmdidx == CMD_enew)
7430 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7431 empty buffer */
7432 setpcmark();
7433 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7434 NULL, eap,
7435 /* ":edit" goes to first line if Vi compatible */
7436 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7437 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7438 ? ECMD_ONE : eap->do_ecmd_lnum,
7439 (P_HID(curbuf) ? ECMD_HIDE : 0)
7440 + (eap->forceit ? ECMD_FORCEIT : 0)
7441#ifdef FEAT_LISTCMDS
7442 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7443#endif
7444 ) == FAIL)
7445 {
7446 /* Editing the file failed. If the window was split, close it. */
7447#ifdef FEAT_WINDOWS
7448 if (old_curwin != NULL)
7449 {
7450 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7451 if (!need_hide || P_HID(curbuf))
7452 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007453# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7454 cleanup_T cs;
7455
7456 /* Reset the error/interrupt/exception state here so that
7457 * aborting() returns FALSE when closing a window. */
7458 enter_cleanup(&cs);
7459# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460# ifdef FEAT_GUI
7461 need_mouse_correct = TRUE;
7462# endif
7463 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007464
7465# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7466 /* Restore the error/interrupt/exception state if not
7467 * discarded by a new aborting error, interrupt, or
7468 * uncaught exception. */
7469 leave_cleanup(&cs);
7470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 }
7472 }
7473#endif
7474 }
7475 else if (readonlymode && curbuf->b_nwindows == 1)
7476 {
7477 /* When editing an already visited buffer, 'readonly' won't be set
7478 * but the previous value is kept. With ":view" and ":sview" we
7479 * want the file to be readonly, except when another window is
7480 * editing the same buffer. */
7481 curbuf->b_p_ro = TRUE;
7482 }
7483 readonlymode = n;
7484 }
7485 else
7486 {
7487 if (eap->do_ecmd_cmd != NULL)
7488 do_cmdline_cmd(eap->do_ecmd_cmd);
7489#ifdef FEAT_TITLE
7490 n = curwin->w_arg_idx_invalid;
7491#endif
7492 check_arg_idx(curwin);
7493#ifdef FEAT_TITLE
7494 if (n != curwin->w_arg_idx_invalid)
7495 maketitle();
7496#endif
7497 }
7498
7499#ifdef FEAT_WINDOWS
7500 /*
7501 * if ":split file" worked, set alternate file name in old window to new
7502 * file
7503 */
7504 if (old_curwin != NULL
7505 && *eap->arg != NUL
7506 && curwin != old_curwin
7507 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007508 && old_curwin->w_buffer != curbuf
7509 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 old_curwin->w_alt_fnum = curbuf->b_fnum;
7511#endif
7512
7513 ex_no_reprint = TRUE;
7514}
7515
7516#ifndef FEAT_GUI
7517/*
7518 * ":gui" and ":gvim" when there is no GUI.
7519 */
7520 static void
7521ex_nogui(eap)
7522 exarg_T *eap;
7523{
7524 eap->errmsg = e_nogvim;
7525}
7526#endif
7527
7528#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7529 static void
7530ex_tearoff(eap)
7531 exarg_T *eap;
7532{
7533 gui_make_tearoff(eap->arg);
7534}
7535#endif
7536
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007537#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 static void
7539ex_popup(eap)
7540 exarg_T *eap;
7541{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007542 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543}
7544#endif
7545
7546/*ARGSUSED*/
7547 static void
7548ex_swapname(eap)
7549 exarg_T *eap;
7550{
7551 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7552 MSG(_("No swap file"));
7553 else
7554 msg(curbuf->b_ml.ml_mfp->mf_fname);
7555}
7556
7557/*
7558 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7559 * offset.
7560 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7561 */
7562/*ARGSUSED*/
7563 static void
7564ex_syncbind(eap)
7565 exarg_T *eap;
7566{
7567#ifdef FEAT_SCROLLBIND
7568 win_T *wp;
7569 long topline;
7570 long y;
7571 linenr_T old_linenr = curwin->w_cursor.lnum;
7572
7573 setpcmark();
7574
7575 /*
7576 * determine max topline
7577 */
7578 if (curwin->w_p_scb)
7579 {
7580 topline = curwin->w_topline;
7581 for (wp = firstwin; wp; wp = wp->w_next)
7582 {
7583 if (wp->w_p_scb && wp->w_buffer)
7584 {
7585 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7586 if (topline > y)
7587 topline = y;
7588 }
7589 }
7590 if (topline < 1)
7591 topline = 1;
7592 }
7593 else
7594 {
7595 topline = 1;
7596 }
7597
7598
7599 /*
7600 * set all scrollbind windows to the same topline
7601 */
7602 wp = curwin;
7603 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7604 {
7605 if (curwin->w_p_scb)
7606 {
7607 y = topline - curwin->w_topline;
7608 if (y > 0)
7609 scrollup(y, TRUE);
7610 else
7611 scrolldown(-y, TRUE);
7612 curwin->w_scbind_pos = topline;
7613 redraw_later(VALID);
7614 cursor_correct();
7615#ifdef FEAT_WINDOWS
7616 curwin->w_redr_status = TRUE;
7617#endif
7618 }
7619 }
7620 curwin = wp;
7621 if (curwin->w_p_scb)
7622 {
7623 did_syncbind = TRUE;
7624 checkpcmark();
7625 if (old_linenr != curwin->w_cursor.lnum)
7626 {
7627 char_u ctrl_o[2];
7628
7629 ctrl_o[0] = Ctrl_O;
7630 ctrl_o[1] = 0;
7631 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7632 }
7633 }
7634#endif
7635}
7636
7637
7638 static void
7639ex_read(eap)
7640 exarg_T *eap;
7641{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007642 int i;
7643 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7644 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645
7646 if (eap->usefilter) /* :r!cmd */
7647 do_bang(1, eap, FALSE, FALSE, TRUE);
7648 else
7649 {
7650 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7651 return;
7652
7653#ifdef FEAT_BROWSE
7654 if (cmdmod.browse)
7655 {
7656 char_u *browseFile;
7657
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007658 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 NULL, NULL, NULL, curbuf);
7660 if (browseFile != NULL)
7661 {
7662 i = readfile(browseFile, NULL,
7663 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7664 vim_free(browseFile);
7665 }
7666 else
7667 i = OK;
7668 }
7669 else
7670#endif
7671 if (*eap->arg == NUL)
7672 {
7673 if (check_fname() == FAIL) /* check for no file name */
7674 return;
7675 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7676 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7677 }
7678 else
7679 {
7680 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7681 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7682 i = readfile(eap->arg, NULL,
7683 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7684
7685 }
7686 if (i == FAIL)
7687 {
7688#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7689 if (!aborting())
7690#endif
7691 EMSG2(_(e_notopen), eap->arg);
7692 }
7693 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007694 {
7695 if (empty && exmode_active)
7696 {
7697 /* Delete the empty line that remains. Historically ex does
7698 * this but vi doesn't. */
7699 if (eap->line2 == 0)
7700 lnum = curbuf->b_ml.ml_line_count;
7701 else
7702 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007703 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007704 {
7705 ml_delete(lnum, FALSE);
7706 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007707 if (curwin->w_cursor.lnum > 1
7708 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007709 --curwin->w_cursor.lnum;
7710 }
7711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715}
7716
Bram Moolenaarea408852005-06-25 22:49:46 +00007717static char_u *prev_dir = NULL;
7718
7719#if defined(EXITFREE) || defined(PROTO)
7720 void
7721free_cd_dir()
7722{
7723 vim_free(prev_dir);
7724}
7725#endif
7726
7727
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728/*
7729 * ":cd", ":lcd", ":chdir" and ":lchdir".
7730 */
7731 static void
7732ex_cd(eap)
7733 exarg_T *eap;
7734{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 char_u *new_dir;
7736 char_u *tofree;
7737
7738 new_dir = eap->arg;
7739#if !defined(UNIX) && !defined(VMS)
7740 /* for non-UNIX ":cd" means: print current directory */
7741 if (*new_dir == NUL)
7742 ex_pwd(NULL);
7743 else
7744#endif
7745 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007746 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7747 && !eap->forceit)
7748 {
7749 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7750 return;
7751 }
7752
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 /* ":cd -": Change to previous directory */
7754 if (STRCMP(new_dir, "-") == 0)
7755 {
7756 if (prev_dir == NULL)
7757 {
7758 EMSG(_("E186: No previous directory"));
7759 return;
7760 }
7761 new_dir = prev_dir;
7762 }
7763
7764 /* Save current directory for next ":cd -" */
7765 tofree = prev_dir;
7766 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7767 prev_dir = vim_strsave(NameBuff);
7768 else
7769 prev_dir = NULL;
7770
7771#if defined(UNIX) || defined(VMS)
7772 /* for UNIX ":cd" means: go to home directory */
7773 if (*new_dir == NUL)
7774 {
7775 /* use NameBuff for home directory name */
7776# ifdef VMS
7777 char_u *p;
7778
7779 p = mch_getenv((char_u *)"SYS$LOGIN");
7780 if (p == NULL || *p == NUL) /* empty is the same as not set */
7781 NameBuff[0] = NUL;
7782 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007783 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784# else
7785 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7786# endif
7787 new_dir = NameBuff;
7788 }
7789#endif
7790 if (new_dir == NULL || vim_chdir(new_dir))
7791 EMSG(_(e_failed));
7792 else
7793 {
7794 vim_free(curwin->w_localdir);
7795 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7796 {
7797 /* If still in global directory, need to remember current
7798 * directory as global directory. */
7799 if (globaldir == NULL && prev_dir != NULL)
7800 globaldir = vim_strsave(prev_dir);
7801 /* Remember this local directory for the window. */
7802 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7803 curwin->w_localdir = vim_strsave(NameBuff);
7804 }
7805 else
7806 {
7807 /* We are now in the global directory, no need to remember its
7808 * name. */
7809 vim_free(globaldir);
7810 globaldir = NULL;
7811 curwin->w_localdir = NULL;
7812 }
7813
7814 shorten_fnames(TRUE);
7815
7816 /* Echo the new current directory if the command was typed. */
7817 if (KeyTyped)
7818 ex_pwd(eap);
7819 }
7820 vim_free(tofree);
7821 }
7822}
7823
7824/*
7825 * ":pwd".
7826 */
7827/*ARGSUSED*/
7828 static void
7829ex_pwd(eap)
7830 exarg_T *eap;
7831{
7832 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7833 {
7834#ifdef BACKSLASH_IN_FILENAME
7835 slash_adjust(NameBuff);
7836#endif
7837 msg(NameBuff);
7838 }
7839 else
7840 EMSG(_("E187: Unknown"));
7841}
7842
7843/*
7844 * ":=".
7845 */
7846 static void
7847ex_equal(eap)
7848 exarg_T *eap;
7849{
7850 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007851 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852}
7853
7854 static void
7855ex_sleep(eap)
7856 exarg_T *eap;
7857{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007858 int n;
7859 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860
7861 if (cursor_valid())
7862 {
7863 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7864 if (n >= 0)
7865 windgoto((int)n, curwin->w_wcol);
7866 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007867
7868 len = eap->line2;
7869 switch (*eap->arg)
7870 {
7871 case 'm': break;
7872 case NUL: len *= 1000L; break;
7873 default: EMSG2(_(e_invarg2), eap->arg); return;
7874 }
7875 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876}
7877
7878/*
7879 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7880 */
7881 void
7882do_sleep(msec)
7883 long msec;
7884{
7885 long done;
7886
7887 cursor_on();
7888 out_flush();
7889 for (done = 0; !got_int && done < msec; done += 1000L)
7890 {
7891 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7892 ui_breakcheck();
7893 }
7894}
7895
7896 static void
7897do_exmap(eap, isabbrev)
7898 exarg_T *eap;
7899 int isabbrev;
7900{
7901 int mode;
7902 char_u *cmdp;
7903
7904 cmdp = eap->cmd;
7905 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7906
7907 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7908 eap->arg, mode, isabbrev))
7909 {
7910 case 1: EMSG(_(e_invarg));
7911 break;
7912 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7913 break;
7914 }
7915}
7916
7917/*
7918 * ":winsize" command (obsolete).
7919 */
7920 static void
7921ex_winsize(eap)
7922 exarg_T *eap;
7923{
7924 int w, h;
7925 char_u *arg = eap->arg;
7926 char_u *p;
7927
7928 w = getdigits(&arg);
7929 arg = skipwhite(arg);
7930 p = arg;
7931 h = getdigits(&arg);
7932 if (*p != NUL && *arg == NUL)
7933 set_shellsize(w, h, TRUE);
7934 else
7935 EMSG(_("E465: :winsize requires two number arguments"));
7936}
7937
7938#ifdef FEAT_WINDOWS
7939 static void
7940ex_wincmd(eap)
7941 exarg_T *eap;
7942{
7943 int xchar = NUL;
7944 char_u *p;
7945
7946 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7947 {
7948 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7949 if (eap->arg[1] == NUL)
7950 {
7951 EMSG(_(e_invarg));
7952 return;
7953 }
7954 xchar = eap->arg[1];
7955 p = eap->arg + 2;
7956 }
7957 else
7958 p = eap->arg + 1;
7959
7960 eap->nextcmd = check_nextcmd(p);
7961 p = skipwhite(p);
7962 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7963 EMSG(_(e_invarg));
7964 else
7965 {
7966 /* Pass flags on for ":vertical wincmd ]". */
7967 postponed_split_flags = cmdmod.split;
7968 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7969 postponed_split_flags = 0;
7970 }
7971}
7972#endif
7973
Bram Moolenaar843ee412004-06-30 16:16:41 +00007974#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975/*
7976 * ":winpos".
7977 */
7978 static void
7979ex_winpos(eap)
7980 exarg_T *eap;
7981{
7982 int x, y;
7983 char_u *arg = eap->arg;
7984 char_u *p;
7985
7986 if (*arg == NUL)
7987 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007988# if defined(FEAT_GUI) || defined(MSWIN)
7989# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007991# else
7992 if (mch_get_winpos(&x, &y) != FAIL)
7993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {
7995 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7996 msg(IObuff);
7997 }
7998 else
7999# endif
8000 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8001 }
8002 else
8003 {
8004 x = getdigits(&arg);
8005 arg = skipwhite(arg);
8006 p = arg;
8007 y = getdigits(&arg);
8008 if (*p == NUL || *arg != NUL)
8009 {
8010 EMSG(_("E466: :winpos requires two number arguments"));
8011 return;
8012 }
8013# ifdef FEAT_GUI
8014 if (gui.in_use)
8015 gui_mch_set_winpos(x, y);
8016 else if (gui.starting)
8017 {
8018 /* Remember the coordinates for when the window is opened. */
8019 gui_win_x = x;
8020 gui_win_y = y;
8021 }
8022# ifdef HAVE_TGETENT
8023 else
8024# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008025# else
8026# ifdef MSWIN
8027 mch_set_winpos(x, y);
8028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029# endif
8030# ifdef HAVE_TGETENT
8031 if (*T_CWP)
8032 term_set_winpos(x, y);
8033# endif
8034 }
8035}
8036#endif
8037
8038/*
8039 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8040 */
8041 static void
8042ex_operators(eap)
8043 exarg_T *eap;
8044{
8045 oparg_T oa;
8046
8047 clear_oparg(&oa);
8048 oa.regname = eap->regname;
8049 oa.start.lnum = eap->line1;
8050 oa.end.lnum = eap->line2;
8051 oa.line_count = eap->line2 - eap->line1 + 1;
8052 oa.motion_type = MLINE;
8053#ifdef FEAT_VIRTUALEDIT
8054 virtual_op = FALSE;
8055#endif
8056 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8057 {
8058 setpcmark();
8059 curwin->w_cursor.lnum = eap->line1;
8060 beginline(BL_SOL | BL_FIX);
8061 }
8062
8063 switch (eap->cmdidx)
8064 {
8065 case CMD_delete:
8066 oa.op_type = OP_DELETE;
8067 op_delete(&oa);
8068 break;
8069
8070 case CMD_yank:
8071 oa.op_type = OP_YANK;
8072 (void)op_yank(&oa, FALSE, TRUE);
8073 break;
8074
8075 default: /* CMD_rshift or CMD_lshift */
8076 if ((eap->cmdidx == CMD_rshift)
8077#ifdef FEAT_RIGHTLEFT
8078 ^ curwin->w_p_rl
8079#endif
8080 )
8081 oa.op_type = OP_RSHIFT;
8082 else
8083 oa.op_type = OP_LSHIFT;
8084 op_shift(&oa, FALSE, eap->amount);
8085 break;
8086 }
8087#ifdef FEAT_VIRTUALEDIT
8088 virtual_op = MAYBE;
8089#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008090 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091}
8092
8093/*
8094 * ":put".
8095 */
8096 static void
8097ex_put(eap)
8098 exarg_T *eap;
8099{
8100 /* ":0put" works like ":1put!". */
8101 if (eap->line2 == 0)
8102 {
8103 eap->line2 = 1;
8104 eap->forceit = TRUE;
8105 }
8106 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008107 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8108 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109}
8110
8111/*
8112 * Handle ":copy" and ":move".
8113 */
8114 static void
8115ex_copymove(eap)
8116 exarg_T *eap;
8117{
8118 long n;
8119
8120 n = get_address(&eap->arg, FALSE, FALSE);
8121 if (eap->arg == NULL) /* error detected */
8122 {
8123 eap->nextcmd = NULL;
8124 return;
8125 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008126 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127
8128 /*
8129 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8130 */
8131 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8132 {
8133 EMSG(_(e_invaddr));
8134 return;
8135 }
8136
8137 if (eap->cmdidx == CMD_move)
8138 {
8139 if (do_move(eap->line1, eap->line2, n) == FAIL)
8140 return;
8141 }
8142 else
8143 ex_copy(eap->line1, eap->line2, n);
8144 u_clearline();
8145 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008146 ex_may_print(eap);
8147}
8148
8149/*
8150 * Print the current line if flags were given to the Ex command.
8151 */
8152 static void
8153ex_may_print(eap)
8154 exarg_T *eap;
8155{
8156 if (eap->flags != 0)
8157 {
8158 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8159 (eap->flags & EXFLAG_LIST));
8160 ex_no_reprint = TRUE;
8161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162}
8163
8164/*
8165 * ":smagic" and ":snomagic".
8166 */
8167 static void
8168ex_submagic(eap)
8169 exarg_T *eap;
8170{
8171 int magic_save = p_magic;
8172
8173 p_magic = (eap->cmdidx == CMD_smagic);
8174 do_sub(eap);
8175 p_magic = magic_save;
8176}
8177
8178/*
8179 * ":join".
8180 */
8181 static void
8182ex_join(eap)
8183 exarg_T *eap;
8184{
8185 curwin->w_cursor.lnum = eap->line1;
8186 if (eap->line1 == eap->line2)
8187 {
8188 if (eap->addr_count >= 2) /* :2,2join does nothing */
8189 return;
8190 if (eap->line2 == curbuf->b_ml.ml_line_count)
8191 {
8192 beep_flush();
8193 return;
8194 }
8195 ++eap->line2;
8196 }
8197 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8198 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008199 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200}
8201
8202/*
8203 * ":[addr]@r" or ":[addr]*r": execute register
8204 */
8205 static void
8206ex_at(eap)
8207 exarg_T *eap;
8208{
8209 int c;
8210
8211 curwin->w_cursor.lnum = eap->line2;
8212
8213#ifdef USE_ON_FLY_SCROLL
8214 dont_scroll = TRUE; /* disallow scrolling here */
8215#endif
8216
8217 /* get the register name. No name means to use the previous one */
8218 c = *eap->arg;
8219 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8220 c = '@';
8221 /* put the register in mapbuf */
8222 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 else
8227 {
8228 int save_efr = exec_from_reg;
8229
8230 exec_from_reg = TRUE;
8231
8232 /*
8233 * Execute from the typeahead buffer.
8234 * Originally this didn't check for the typeahead buffer to be empty,
8235 * thus could read more Ex commands from stdin. It's not clear why,
8236 * it is certainly unexpected.
8237 */
8238 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8239 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8240
8241 exec_from_reg = save_efr;
8242 }
8243}
8244
8245/*
8246 * ":!".
8247 */
8248 static void
8249ex_bang(eap)
8250 exarg_T *eap;
8251{
8252 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8253}
8254
8255/*
8256 * ":undo".
8257 */
8258/*ARGSUSED*/
8259 static void
8260ex_undo(eap)
8261 exarg_T *eap;
8262{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008263 if (eap->addr_count == 1) /* :undo 123 */
8264 undo_time(eap->line2, FALSE, TRUE);
8265 else
8266 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267}
8268
8269/*
8270 * ":redo".
8271 */
8272/*ARGSUSED*/
8273 static void
8274ex_redo(eap)
8275 exarg_T *eap;
8276{
8277 u_redo(1);
8278}
8279
8280/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008281 * ":earlier" and ":later".
8282 */
8283/*ARGSUSED*/
8284 static void
8285ex_later(eap)
8286 exarg_T *eap;
8287{
8288 long count = 0;
8289 int sec = FALSE;
8290 char_u *p = eap->arg;
8291
8292 if (*p == NUL)
8293 count = 1;
8294 else if (isdigit(*p))
8295 {
8296 count = getdigits(&p);
8297 switch (*p)
8298 {
8299 case 's': ++p; sec = TRUE; break;
8300 case 'm': ++p; sec = TRUE; count *= 60; break;
8301 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
8302 }
8303 }
8304
8305 if (*p != NUL)
8306 EMSG2(_(e_invarg2), eap->arg);
8307 else
Bram Moolenaard3667a22006-03-16 21:35:52 +00008308 undo_time(eap->cmdidx == CMD_earlier ? -count : count, sec, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008309}
8310
8311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 * ":redir": start/stop redirection.
8313 */
8314 static void
8315ex_redir(eap)
8316 exarg_T *eap;
8317{
8318 char *mode;
8319 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008320 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
8322 if (STRICMP(eap->arg, "END") == 0)
8323 close_redir();
8324 else
8325 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008326 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008328 ++arg;
8329 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008331 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 mode = "a";
8333 }
8334 else
8335 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008336 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337
8338 close_redir();
8339
8340 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008341 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 if (fname == NULL)
8343 return;
8344#ifdef FEAT_BROWSE
8345 if (cmdmod.browse)
8346 {
8347 char_u *browseFile;
8348
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008349 browseFile = do_browse(BROWSE_SAVE,
8350 (char_u *)_("Save Redirection"),
8351 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 if (browseFile == NULL)
8353 return; /* operation cancelled */
8354 vim_free(fname);
8355 fname = browseFile;
8356 eap->forceit = TRUE; /* since dialog already asked */
8357 }
8358#endif
8359
8360 redir_fd = open_exfile(fname, eap->forceit, mode);
8361 vim_free(fname);
8362 }
8363#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008364 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
8366 /* redirect to a register a-z (resp. A-Z for appending) */
8367 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008368 ++arg;
8369 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008371 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008373 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008375 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008376 if (*arg == '>' && arg[1] == '>')
8377 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008378 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8379 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008381 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008383 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008385 if (*arg == '>')
8386 arg++;
8387
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 /* make register empty */
8389 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8390 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008391 }
8392 if (*arg != NUL)
8393 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008394 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008395 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008396 }
8397 }
8398 else if (*arg == '=' && arg[1] == '>')
8399 {
8400 int append;
8401
8402 /* redirect to a variable */
8403 close_redir();
8404 arg += 2;
8405
8406 if (*arg == '>')
8407 {
8408 ++arg;
8409 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 }
8411 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008412 append = FALSE;
8413
8414 if (var_redir_start(skipwhite(arg), append) == OK)
8415 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 }
8417#endif
8418
8419 /* TODO: redirect to a buffer */
8420
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008422 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 }
8424}
8425
8426/*
8427 * ":redraw": force redraw
8428 */
8429 static void
8430ex_redraw(eap)
8431 exarg_T *eap;
8432{
8433 int r = RedrawingDisabled;
8434 int p = p_lz;
8435
8436 RedrawingDisabled = 0;
8437 p_lz = FALSE;
8438 update_topline();
8439 update_screen(eap->forceit ? CLEAR :
8440#ifdef FEAT_VISUAL
8441 VIsual_active ? INVERTED :
8442#endif
8443 0);
8444#ifdef FEAT_TITLE
8445 if (need_maketitle)
8446 maketitle();
8447#endif
8448 RedrawingDisabled = r;
8449 p_lz = p;
8450
8451 /* Reset msg_didout, so that a message that's there is overwritten. */
8452 msg_didout = FALSE;
8453 msg_col = 0;
8454
8455 out_flush();
8456}
8457
8458/*
8459 * ":redrawstatus": force redraw of status line(s)
8460 */
8461/*ARGSUSED*/
8462 static void
8463ex_redrawstatus(eap)
8464 exarg_T *eap;
8465{
8466#if defined(FEAT_WINDOWS)
8467 int r = RedrawingDisabled;
8468 int p = p_lz;
8469
8470 RedrawingDisabled = 0;
8471 p_lz = FALSE;
8472 if (eap->forceit)
8473 status_redraw_all();
8474 else
8475 status_redraw_curbuf();
8476 update_screen(
8477# ifdef FEAT_VISUAL
8478 VIsual_active ? INVERTED :
8479# endif
8480 0);
8481 RedrawingDisabled = r;
8482 p_lz = p;
8483 out_flush();
8484#endif
8485}
8486
8487 static void
8488close_redir()
8489{
8490 if (redir_fd != NULL)
8491 {
8492 fclose(redir_fd);
8493 redir_fd = NULL;
8494 }
8495#ifdef FEAT_EVAL
8496 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008497 if (redir_vname)
8498 {
8499 var_redir_stop();
8500 redir_vname = 0;
8501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502#endif
8503}
8504
8505#if defined(FEAT_SESSION) && defined(USE_CRNL)
8506# define MKSESSION_NL
8507static int mksession_nl = FALSE; /* use NL only in put_eol() */
8508#endif
8509
8510/*
8511 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8512 */
8513 static void
8514ex_mkrc(eap)
8515 exarg_T *eap;
8516{
8517 FILE *fd;
8518 int failed = FALSE;
8519 char_u *fname;
8520#ifdef FEAT_BROWSE
8521 char_u *browseFile = NULL;
8522#endif
8523#ifdef FEAT_SESSION
8524 int view_session = FALSE;
8525 int using_vdir = FALSE; /* using 'viewdir'? */
8526 char_u *viewFile = NULL;
8527 unsigned *flagp;
8528#endif
8529
8530 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8531 {
8532#ifdef FEAT_SESSION
8533 view_session = TRUE;
8534#else
8535 ex_ni(eap);
8536 return;
8537#endif
8538 }
8539
8540#ifdef FEAT_SESSION
8541 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8542 if (eap->cmdidx == CMD_mkview
8543 && (*eap->arg == NUL
8544 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8545 {
8546 eap->forceit = TRUE;
8547 fname = get_view_file(*eap->arg);
8548 if (fname == NULL)
8549 return;
8550 viewFile = fname;
8551 using_vdir = TRUE;
8552 }
8553 else
8554#endif
8555 if (*eap->arg != NUL)
8556 fname = eap->arg;
8557 else if (eap->cmdidx == CMD_mkvimrc)
8558 fname = (char_u *)VIMRC_FILE;
8559#ifdef FEAT_SESSION
8560 else if (eap->cmdidx == CMD_mksession)
8561 fname = (char_u *)SESSION_FILE;
8562#endif
8563 else
8564 fname = (char_u *)EXRC_FILE;
8565
8566#ifdef FEAT_BROWSE
8567 if (cmdmod.browse)
8568 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008569 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570# ifdef FEAT_SESSION
8571 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8572 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8573# endif
8574 (char_u *)_("Save Setup"),
8575 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8576 if (browseFile == NULL)
8577 goto theend;
8578 fname = browseFile;
8579 eap->forceit = TRUE; /* since dialog already asked */
8580 }
8581#endif
8582
8583#if defined(FEAT_SESSION) && defined(vim_mkdir)
8584 /* When using 'viewdir' may have to create the directory. */
8585 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008586 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587#endif
8588
8589 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8590 if (fd != NULL)
8591 {
8592#ifdef FEAT_SESSION
8593 if (eap->cmdidx == CMD_mkview)
8594 flagp = &vop_flags;
8595 else
8596 flagp = &ssop_flags;
8597#endif
8598
8599#ifdef MKSESSION_NL
8600 /* "unix" in 'sessionoptions': use NL line separator */
8601 if (view_session && (*flagp & SSOP_UNIX))
8602 mksession_nl = TRUE;
8603#endif
8604
8605 /* Write the version command for :mkvimrc */
8606 if (eap->cmdidx == CMD_mkvimrc)
8607 (void)put_line(fd, "version 6.0");
8608
8609#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008610 if (eap->cmdidx == CMD_mksession)
8611 {
8612 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8613 failed = TRUE;
8614 }
8615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 if (eap->cmdidx != CMD_mkview)
8617#endif
8618 {
8619 /* Write setting 'compatible' first, because it has side effects.
8620 * For that same reason only do it when needed. */
8621 if (p_cp)
8622 (void)put_line(fd, "if !&cp | set cp | endif");
8623 else
8624 (void)put_line(fd, "if &cp | set nocp | endif");
8625 }
8626
8627#ifdef FEAT_SESSION
8628 if (!view_session
8629 || (eap->cmdidx == CMD_mksession
8630 && (*flagp & SSOP_OPTIONS)))
8631#endif
8632 failed |= (makemap(fd, NULL) == FAIL
8633 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8634
8635#ifdef FEAT_SESSION
8636 if (!failed && view_session)
8637 {
8638 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8639 failed = TRUE;
8640 if (eap->cmdidx == CMD_mksession)
8641 {
8642 char_u dirnow[MAXPATHL]; /* current directory */
8643
8644 /*
8645 * Change to session file's dir.
8646 */
8647 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8648 || mch_chdir((char *)dirnow) != 0)
8649 *dirnow = NUL;
8650 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8651 {
8652 if (vim_chdirfile(fname) == OK)
8653 shorten_fnames(TRUE);
8654 }
8655 else if (*dirnow != NUL
8656 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8657 {
8658 (void)mch_chdir((char *)globaldir);
8659 shorten_fnames(TRUE);
8660 }
8661
8662 failed |= (makeopens(fd, dirnow) == FAIL);
8663
8664 /* restore original dir */
8665 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8666 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8667 {
8668 if (mch_chdir((char *)dirnow) != 0)
8669 EMSG(_(e_prev_dir));
8670 shorten_fnames(TRUE);
8671 }
8672 }
8673 else
8674 {
8675 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8676 }
8677 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8678 == FAIL)
8679 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008680 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8681 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008682 if (eap->cmdidx == CMD_mksession)
8683 {
8684 if (put_line(fd, "unlet SessionLoad") == FAIL)
8685 failed = TRUE;
8686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 }
8688#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008689 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8690 failed = TRUE;
8691
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 failed |= fclose(fd);
8693
8694 if (failed)
8695 EMSG(_(e_write));
8696#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8697 else if (eap->cmdidx == CMD_mksession)
8698 {
8699 /* successful session write - set this_session var */
8700 char_u tbuf[MAXPATHL];
8701
8702 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8703 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8704 }
8705#endif
8706#ifdef MKSESSION_NL
8707 mksession_nl = FALSE;
8708#endif
8709 }
8710
8711#ifdef FEAT_BROWSE
8712theend:
8713 vim_free(browseFile);
8714#endif
8715#ifdef FEAT_SESSION
8716 vim_free(viewFile);
8717#endif
8718}
8719
Bram Moolenaardf177f62005-02-22 08:39:57 +00008720#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8721 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008723 int
8724vim_mkdir_emsg(name, prot)
8725 char_u *name;
8726 int prot;
8727{
8728 if (vim_mkdir(name, prot) != 0)
8729 {
8730 EMSG2(_("E739: Cannot create directory: %s"), name);
8731 return FAIL;
8732 }
8733 return OK;
8734}
8735#endif
8736
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737/*
8738 * Open a file for writing for an Ex command, with some checks.
8739 * Return file descriptor, or NULL on failure.
8740 */
8741 FILE *
8742open_exfile(fname, forceit, mode)
8743 char_u *fname;
8744 int forceit;
8745 char *mode; /* "w" for create new file or "a" for append */
8746{
8747 FILE *fd;
8748
8749#ifdef UNIX
8750 /* with Unix it is possible to open a directory */
8751 if (mch_isdir(fname))
8752 {
8753 EMSG2(_(e_isadir2), fname);
8754 return NULL;
8755 }
8756#endif
8757 if (!forceit && *mode != 'a' && vim_fexists(fname))
8758 {
8759 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8760 return NULL;
8761 }
8762
8763 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8764 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8765
8766 return fd;
8767}
8768
8769/*
8770 * ":mark" and ":k".
8771 */
8772 static void
8773ex_mark(eap)
8774 exarg_T *eap;
8775{
8776 pos_T pos;
8777
8778 if (*eap->arg == NUL) /* No argument? */
8779 EMSG(_(e_argreq));
8780 else if (eap->arg[1] != NUL) /* more than one character? */
8781 EMSG(_(e_trailing));
8782 else
8783 {
8784 pos = curwin->w_cursor; /* save curwin->w_cursor */
8785 curwin->w_cursor.lnum = eap->line2;
8786 beginline(BL_WHITE | BL_FIX);
8787 if (setmark(*eap->arg) == FAIL) /* set mark */
8788 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8789 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8790 }
8791}
8792
8793/*
8794 * Update w_topline, w_leftcol and the cursor position.
8795 */
8796 void
8797update_topline_cursor()
8798{
8799 check_cursor(); /* put cursor on valid line */
8800 update_topline();
8801 if (!curwin->w_p_wrap)
8802 validate_cursor();
8803 update_curswant();
8804}
8805
8806#ifdef FEAT_EX_EXTRA
8807/*
8808 * ":normal[!] {commands}": Execute normal mode commands.
8809 */
8810 static void
8811ex_normal(eap)
8812 exarg_T *eap;
8813{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 int save_msg_scroll = msg_scroll;
8815 int save_restart_edit = restart_edit;
8816 int save_msg_didout = msg_didout;
8817 int save_State = State;
8818 tasave_T tabuf;
8819 int save_insertmode = p_im;
8820 int save_finish_op = finish_op;
8821#ifdef FEAT_MBYTE
8822 char_u *arg = NULL;
8823 int l;
8824 char_u *p;
8825#endif
8826
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008827 if (ex_normal_lock > 0)
8828 {
8829 EMSG(_(e_secure));
8830 return;
8831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 if (ex_normal_busy >= p_mmd)
8833 {
8834 EMSG(_("E192: Recursive use of :normal too deep"));
8835 return;
8836 }
8837 ++ex_normal_busy;
8838
8839 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8840 restart_edit = 0; /* don't go to Insert mode */
8841 p_im = FALSE; /* don't use 'insertmode' */
8842
8843#ifdef FEAT_MBYTE
8844 /*
8845 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8846 * this for the K_SPECIAL leading byte, otherwise special keys will not
8847 * work.
8848 */
8849 if (has_mbyte)
8850 {
8851 int len = 0;
8852
8853 /* Count the number of characters to be escaped. */
8854 for (p = eap->arg; *p != NUL; ++p)
8855 {
8856# ifdef FEAT_GUI
8857 if (*p == CSI) /* leadbyte CSI */
8858 len += 2;
8859# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008860 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8862# ifdef FEAT_GUI
8863 || *p == CSI
8864# endif
8865 )
8866 len += 2;
8867 }
8868 if (len > 0)
8869 {
8870 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8871 if (arg != NULL)
8872 {
8873 len = 0;
8874 for (p = eap->arg; *p != NUL; ++p)
8875 {
8876 arg[len++] = *p;
8877# ifdef FEAT_GUI
8878 if (*p == CSI)
8879 {
8880 arg[len++] = KS_EXTRA;
8881 arg[len++] = (int)KE_CSI;
8882 }
8883# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008884 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
8886 arg[len++] = *++p;
8887 if (*p == K_SPECIAL)
8888 {
8889 arg[len++] = KS_SPECIAL;
8890 arg[len++] = KE_FILLER;
8891 }
8892# ifdef FEAT_GUI
8893 else if (*p == CSI)
8894 {
8895 arg[len++] = KS_EXTRA;
8896 arg[len++] = (int)KE_CSI;
8897 }
8898# endif
8899 }
8900 arg[len] = NUL;
8901 }
8902 }
8903 }
8904 }
8905#endif
8906
8907 /*
8908 * Save the current typeahead. This is required to allow using ":normal"
8909 * from an event handler and makes sure we don't hang when the argument
8910 * ends with half a command.
8911 */
8912 save_typeahead(&tabuf);
8913 if (tabuf.typebuf_valid)
8914 {
8915 /*
8916 * Repeat the :normal command for each line in the range. When no
8917 * range given, execute it just once, without positioning the cursor
8918 * first.
8919 */
8920 do
8921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 if (eap->addr_count != 0)
8923 {
8924 curwin->w_cursor.lnum = eap->line1++;
8925 curwin->w_cursor.col = 0;
8926 }
8927
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008928 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929#ifdef FEAT_MBYTE
8930 arg != NULL ? arg :
8931#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008932 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 }
8934 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8935 }
8936
8937 /* Might not return to the main loop when in an event handler. */
8938 update_topline_cursor();
8939
8940 /* Restore the previous typeahead. */
8941 restore_typeahead(&tabuf);
8942
8943 --ex_normal_busy;
8944 msg_scroll = save_msg_scroll;
8945 restart_edit = save_restart_edit;
8946 p_im = save_insertmode;
8947 finish_op = save_finish_op;
8948 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8949
8950 /* Restore the state (needed when called from a function executed for
8951 * 'indentexpr'). */
8952 State = save_State;
8953#ifdef FEAT_MBYTE
8954 vim_free(arg);
8955#endif
8956}
8957
8958/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008959 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 */
8961 static void
8962ex_startinsert(eap)
8963 exarg_T *eap;
8964{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008965 if (eap->forceit)
8966 {
8967 coladvance((colnr_T)MAXCOL);
8968 curwin->w_curswant = MAXCOL;
8969 curwin->w_set_curswant = FALSE;
8970 }
8971
Bram Moolenaara40c5002005-01-09 21:16:21 +00008972 /* Ignore the command when already in Insert mode. Inserting an
8973 * expression register that invokes a function can do this. */
8974 if (State & INSERT)
8975 return;
8976
Bram Moolenaar64969662005-12-14 21:59:55 +00008977 if (eap->cmdidx == CMD_startinsert)
8978 restart_edit = 'a';
8979 else if (eap->cmdidx == CMD_startreplace)
8980 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008982 restart_edit = 'V';
8983
8984 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008986 if (eap->cmdidx == CMD_startinsert)
8987 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 curwin->w_curswant = 0; /* avoid MAXCOL */
8989 }
8990}
8991
8992/*
8993 * ":stopinsert"
8994 */
8995/*ARGSUSED*/
8996 static void
8997ex_stopinsert(eap)
8998 exarg_T *eap;
8999{
9000 restart_edit = 0;
9001 stop_insert_mode = TRUE;
9002}
9003#endif
9004
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009005#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9006/*
9007 * Execute normal mode command "cmd".
9008 * "remap" can be REMAP_NONE or REMAP_YES.
9009 */
9010 void
9011exec_normal_cmd(cmd, remap, silent)
9012 char_u *cmd;
9013 int remap;
9014 int silent;
9015{
9016 oparg_T oa;
9017
9018 /*
9019 * Stuff the argument into the typeahead buffer.
9020 * Execute normal_cmd() until there is no typeahead left.
9021 */
9022 clear_oparg(&oa);
9023 finish_op = FALSE;
9024 ins_typebuf(cmd, remap, 0, TRUE, silent);
9025 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9026 && !got_int)
9027 {
9028 update_topline_cursor();
9029 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9030 }
9031}
9032#endif
9033
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034#ifdef FEAT_FIND_ID
9035 static void
9036ex_checkpath(eap)
9037 exarg_T *eap;
9038{
9039 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9040 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9041 (linenr_T)1, (linenr_T)MAXLNUM);
9042}
9043
9044#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9045/*
9046 * ":psearch"
9047 */
9048 static void
9049ex_psearch(eap)
9050 exarg_T *eap;
9051{
9052 g_do_tagpreview = p_pvh;
9053 ex_findpat(eap);
9054 g_do_tagpreview = 0;
9055}
9056#endif
9057
9058 static void
9059ex_findpat(eap)
9060 exarg_T *eap;
9061{
9062 int whole = TRUE;
9063 long n;
9064 char_u *p;
9065 int action;
9066
9067 switch (cmdnames[eap->cmdidx].cmd_name[2])
9068 {
9069 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9070 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9071 action = ACTION_GOTO;
9072 else
9073 action = ACTION_SHOW;
9074 break;
9075 case 'i': /* ":ilist" and ":dlist" */
9076 action = ACTION_SHOW_ALL;
9077 break;
9078 case 'u': /* ":ijump" and ":djump" */
9079 action = ACTION_GOTO;
9080 break;
9081 default: /* ":isplit" and ":dsplit" */
9082 action = ACTION_SPLIT;
9083 break;
9084 }
9085
9086 n = 1;
9087 if (vim_isdigit(*eap->arg)) /* get count */
9088 {
9089 n = getdigits(&eap->arg);
9090 eap->arg = skipwhite(eap->arg);
9091 }
9092 if (*eap->arg == '/') /* Match regexp, not just whole words */
9093 {
9094 whole = FALSE;
9095 ++eap->arg;
9096 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9097 if (*p)
9098 {
9099 *p++ = NUL;
9100 p = skipwhite(p);
9101
9102 /* Check for trailing illegal characters */
9103 if (!ends_excmd(*p))
9104 eap->errmsg = e_trailing;
9105 else
9106 eap->nextcmd = check_nextcmd(p);
9107 }
9108 }
9109 if (!eap->skip)
9110 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9111 whole, !eap->forceit,
9112 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9113 n, action, eap->line1, eap->line2);
9114}
9115#endif
9116
9117#ifdef FEAT_WINDOWS
9118
9119# ifdef FEAT_QUICKFIX
9120/*
9121 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9122 */
9123 static void
9124ex_ptag(eap)
9125 exarg_T *eap;
9126{
9127 g_do_tagpreview = p_pvh;
9128 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9129}
9130
9131/*
9132 * ":pedit"
9133 */
9134 static void
9135ex_pedit(eap)
9136 exarg_T *eap;
9137{
9138 win_T *curwin_save = curwin;
9139
9140 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009141 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 keep_help_flag = curwin_save->w_buffer->b_help;
9143 do_exedit(eap, NULL);
9144 keep_help_flag = FALSE;
9145 if (curwin != curwin_save && win_valid(curwin_save))
9146 {
9147 /* Return cursor to where we were */
9148 validate_cursor();
9149 redraw_later(VALID);
9150 win_enter(curwin_save, TRUE);
9151 }
9152 g_do_tagpreview = 0;
9153}
9154# endif
9155
9156/*
9157 * ":stag", ":stselect" and ":stjump".
9158 */
9159 static void
9160ex_stag(eap)
9161 exarg_T *eap;
9162{
9163 postponed_split = -1;
9164 postponed_split_flags = cmdmod.split;
9165 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9166 postponed_split_flags = 0;
9167}
9168#endif
9169
9170/*
9171 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9172 */
9173 static void
9174ex_tag(eap)
9175 exarg_T *eap;
9176{
9177 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9178}
9179
9180 static void
9181ex_tag_cmd(eap, name)
9182 exarg_T *eap;
9183 char_u *name;
9184{
9185 int cmd;
9186
9187 switch (name[1])
9188 {
9189 case 'j': cmd = DT_JUMP; /* ":tjump" */
9190 break;
9191 case 's': cmd = DT_SELECT; /* ":tselect" */
9192 break;
9193 case 'p': cmd = DT_PREV; /* ":tprevious" */
9194 break;
9195 case 'N': cmd = DT_PREV; /* ":tNext" */
9196 break;
9197 case 'n': cmd = DT_NEXT; /* ":tnext" */
9198 break;
9199 case 'o': cmd = DT_POP; /* ":pop" */
9200 break;
9201 case 'f': /* ":tfirst" */
9202 case 'r': cmd = DT_FIRST; /* ":trewind" */
9203 break;
9204 case 'l': cmd = DT_LAST; /* ":tlast" */
9205 break;
9206 default: /* ":tag" */
9207#ifdef FEAT_CSCOPE
9208 if (p_cst)
9209 {
9210 do_cstag(eap);
9211 return;
9212 }
9213#endif
9214 cmd = DT_TAG;
9215 break;
9216 }
9217
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009218 if (name[0] == 'l')
9219 {
9220#ifndef FEAT_QUICKFIX
9221 ex_ni(eap);
9222 return;
9223#else
9224 cmd = DT_LTAG;
9225#endif
9226 }
9227
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9229 eap->forceit, TRUE);
9230}
9231
9232/*
9233 * Evaluate cmdline variables.
9234 *
9235 * change '%' to curbuf->b_ffname
9236 * '#' to curwin->w_altfile
9237 * '<cword>' to word under the cursor
9238 * '<cWORD>' to WORD under the cursor
9239 * '<cfile>' to path name under the cursor
9240 * '<sfile>' to sourced file name
9241 * '<afile>' to file name for autocommand
9242 * '<abuf>' to buffer number for autocommand
9243 * '<amatch>' to matching name for autocommand
9244 *
9245 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9246 * "" for error without a message) and NULL is returned.
9247 * Returns an allocated string if a valid match was found.
9248 * Returns NULL if no match was found. "usedlen" then still contains the
9249 * number of characters to skip.
9250 */
9251 char_u *
9252eval_vars(src, usedlen, lnump, errormsg, srcstart)
9253 char_u *src; /* pointer into commandline */
9254 int *usedlen; /* characters after src that are used */
9255 linenr_T *lnump; /* line number for :e command, or NULL */
9256 char_u **errormsg; /* pointer to error message */
9257 char_u *srcstart; /* beginning of valid memory for src */
9258{
9259 int i;
9260 char_u *s;
9261 char_u *result;
9262 char_u *resultbuf = NULL;
9263 int resultlen;
9264 buf_T *buf;
9265 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9266 int spec_idx;
9267#ifdef FEAT_MODIFY_FNAME
9268 int skip_mod = FALSE;
9269#endif
9270 static char *(spec_str[]) =
9271 {
9272 "%",
9273#define SPEC_PERC 0
9274 "#",
9275#define SPEC_HASH 1
9276 "<cword>", /* cursor word */
9277#define SPEC_CWORD 2
9278 "<cWORD>", /* cursor WORD */
9279#define SPEC_CCWORD 3
9280 "<cfile>", /* cursor path name */
9281#define SPEC_CFILE 4
9282 "<sfile>", /* ":so" file name */
9283#define SPEC_SFILE 5
9284#ifdef FEAT_AUTOCMD
9285 "<afile>", /* autocommand file name */
9286# define SPEC_AFILE 6
9287 "<abuf>", /* autocommand buffer number */
9288# define SPEC_ABUF 7
9289 "<amatch>", /* autocommand match name */
9290# define SPEC_AMATCH 8
9291#endif
9292#ifdef FEAT_CLIENTSERVER
9293 "<client>"
9294# define SPEC_CLIENT 9
9295#endif
9296 };
9297#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9298
9299#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9300 char_u strbuf[30];
9301#endif
9302
9303 *errormsg = NULL;
9304
9305 /*
9306 * Check if there is something to do.
9307 */
9308 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9309 {
9310 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9311 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9312 break;
9313 }
9314 if (spec_idx == SPEC_COUNT) /* no match */
9315 {
9316 *usedlen = 1;
9317 return NULL;
9318 }
9319
9320 /*
9321 * Skip when preceded with a backslash "\%" and "\#".
9322 * Note: In "\\%" the % is also not recognized!
9323 */
9324 if (src > srcstart && src[-1] == '\\')
9325 {
9326 *usedlen = 0;
9327 STRCPY(src - 1, src); /* remove backslash */
9328 return NULL;
9329 }
9330
9331 /*
9332 * word or WORD under cursor
9333 */
9334 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9335 {
9336 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9337 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9338 if (resultlen == 0)
9339 {
9340 *errormsg = (char_u *)"";
9341 return NULL;
9342 }
9343 }
9344
9345 /*
9346 * '#': Alternate file name
9347 * '%': Current file name
9348 * File name under the cursor
9349 * File name for autocommand
9350 * and following modifiers
9351 */
9352 else
9353 {
9354 switch (spec_idx)
9355 {
9356 case SPEC_PERC: /* '%': current file */
9357 if (curbuf->b_fname == NULL)
9358 {
9359 result = (char_u *)"";
9360 valid = 0; /* Must have ":p:h" to be valid */
9361 }
9362 else
9363#ifdef RISCOS
9364 /* Always use the full path for RISC OS if possible. */
9365 result = curbuf->b_ffname;
9366 if (result == NULL)
9367 result = curbuf->b_fname;
9368#else
9369 result = curbuf->b_fname;
9370#endif
9371 break;
9372
9373 case SPEC_HASH: /* '#' or "#99": alternate file */
9374 if (src[1] == '#') /* "##": the argument list */
9375 {
9376 result = arg_all();
9377 resultbuf = result;
9378 *usedlen = 2;
9379#ifdef FEAT_MODIFY_FNAME
9380 skip_mod = TRUE;
9381#endif
9382 break;
9383 }
9384 s = src + 1;
9385 i = (int)getdigits(&s);
9386 *usedlen = (int)(s - src); /* length of what we expand */
9387
9388 buf = buflist_findnr(i);
9389 if (buf == NULL)
9390 {
9391 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9392 return NULL;
9393 }
9394 if (lnump != NULL)
9395 *lnump = ECMD_LAST;
9396 if (buf->b_fname == NULL)
9397 {
9398 result = (char_u *)"";
9399 valid = 0; /* Must have ":p:h" to be valid */
9400 }
9401 else
9402 result = buf->b_fname;
9403 break;
9404
9405#ifdef FEAT_SEARCHPATH
9406 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009407 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 if (result == NULL)
9409 {
9410 *errormsg = (char_u *)"";
9411 return NULL;
9412 }
9413 resultbuf = result; /* remember allocated string */
9414 break;
9415#endif
9416
9417#ifdef FEAT_AUTOCMD
9418 case SPEC_AFILE: /* file name for autocommand */
9419 result = autocmd_fname;
9420 if (result == NULL)
9421 {
9422 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9423 return NULL;
9424 }
9425 break;
9426
9427 case SPEC_ABUF: /* buffer number for autocommand */
9428 if (autocmd_bufnr <= 0)
9429 {
9430 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9431 return NULL;
9432 }
9433 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9434 result = strbuf;
9435 break;
9436
9437 case SPEC_AMATCH: /* match name for autocommand */
9438 result = autocmd_match;
9439 if (result == NULL)
9440 {
9441 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9442 return NULL;
9443 }
9444 break;
9445
9446#endif
9447 case SPEC_SFILE: /* file name for ":so" command */
9448 result = sourcing_name;
9449 if (result == NULL)
9450 {
9451 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9452 return NULL;
9453 }
9454 break;
9455#if defined(FEAT_CLIENTSERVER)
9456 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009457 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9458 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 result = strbuf;
9460 break;
9461#endif
9462 }
9463
9464 resultlen = (int)STRLEN(result); /* length of new string */
9465 if (src[*usedlen] == '<') /* remove the file name extension */
9466 {
9467 ++*usedlen;
9468#ifdef RISCOS
9469 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9470#else
9471 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9472#endif
9473 resultlen = (int)(s - result);
9474 }
9475#ifdef FEAT_MODIFY_FNAME
9476 else if (!skip_mod)
9477 {
9478 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9479 &resultlen);
9480 if (result == NULL)
9481 {
9482 *errormsg = (char_u *)"";
9483 return NULL;
9484 }
9485 }
9486#endif
9487 }
9488
9489 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9490 {
9491 if (valid != VALID_HEAD + VALID_PATH)
9492 /* xgettext:no-c-format */
9493 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9494 else
9495 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9496 result = NULL;
9497 }
9498 else
9499 result = vim_strnsave(result, resultlen);
9500 vim_free(resultbuf);
9501 return result;
9502}
9503
9504/*
9505 * Concatenate all files in the argument list, separated by spaces, and return
9506 * it in one allocated string.
9507 * Spaces and backslashes in the file names are escaped with a backslash.
9508 * Returns NULL when out of memory.
9509 */
9510 static char_u *
9511arg_all()
9512{
9513 int len;
9514 int idx;
9515 char_u *retval = NULL;
9516 char_u *p;
9517
9518 /*
9519 * Do this loop two times:
9520 * first time: compute the total length
9521 * second time: concatenate the names
9522 */
9523 for (;;)
9524 {
9525 len = 0;
9526 for (idx = 0; idx < ARGCOUNT; ++idx)
9527 {
9528 p = alist_name(&ARGLIST[idx]);
9529 if (p != NULL)
9530 {
9531 if (len > 0)
9532 {
9533 /* insert a space in between names */
9534 if (retval != NULL)
9535 retval[len] = ' ';
9536 ++len;
9537 }
9538 for ( ; *p != NUL; ++p)
9539 {
9540 if (*p == ' ' || *p == '\\')
9541 {
9542 /* insert a backslash */
9543 if (retval != NULL)
9544 retval[len] = '\\';
9545 ++len;
9546 }
9547 if (retval != NULL)
9548 retval[len] = *p;
9549 ++len;
9550 }
9551 }
9552 }
9553
9554 /* second time: break here */
9555 if (retval != NULL)
9556 {
9557 retval[len] = NUL;
9558 break;
9559 }
9560
9561 /* allocate memory */
9562 retval = alloc(len + 1);
9563 if (retval == NULL)
9564 break;
9565 }
9566
9567 return retval;
9568}
9569
9570#if defined(FEAT_AUTOCMD) || defined(PROTO)
9571/*
9572 * Expand the <sfile> string in "arg".
9573 *
9574 * Returns an allocated string, or NULL for any error.
9575 */
9576 char_u *
9577expand_sfile(arg)
9578 char_u *arg;
9579{
9580 char_u *errormsg;
9581 int len;
9582 char_u *result;
9583 char_u *newres;
9584 char_u *repl;
9585 int srclen;
9586 char_u *p;
9587
9588 result = vim_strsave(arg);
9589 if (result == NULL)
9590 return NULL;
9591
9592 for (p = result; *p; )
9593 {
9594 if (STRNCMP(p, "<sfile>", 7) != 0)
9595 ++p;
9596 else
9597 {
9598 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9599 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9600 if (errormsg != NULL)
9601 {
9602 if (*errormsg)
9603 emsg(errormsg);
9604 vim_free(result);
9605 return NULL;
9606 }
9607 if (repl == NULL) /* no match (cannot happen) */
9608 {
9609 p += srclen;
9610 continue;
9611 }
9612 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9613 newres = alloc(len);
9614 if (newres == NULL)
9615 {
9616 vim_free(repl);
9617 vim_free(result);
9618 return NULL;
9619 }
9620 mch_memmove(newres, result, (size_t)(p - result));
9621 STRCPY(newres + (p - result), repl);
9622 len = (int)STRLEN(newres);
9623 STRCAT(newres, p + srclen);
9624 vim_free(repl);
9625 vim_free(result);
9626 result = newres;
9627 p = newres + len; /* continue after the match */
9628 }
9629 }
9630
9631 return result;
9632}
9633#endif
9634
9635#ifdef FEAT_SESSION
9636static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9637static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9638static frame_T *ses_skipframe __ARGS((frame_T *fr));
9639static int ses_do_frame __ARGS((frame_T *fr));
9640static int ses_do_win __ARGS((win_T *wp));
9641static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9642static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9643static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9644
9645/*
9646 * Write openfile commands for the current buffers to an .exrc file.
9647 * Return FAIL on error, OK otherwise.
9648 */
9649 static int
9650makeopens(fd, dirnow)
9651 FILE *fd;
9652 char_u *dirnow; /* Current directory name */
9653{
9654 buf_T *buf;
9655 int only_save_windows = TRUE;
9656 int nr;
9657 int cnr = 1;
9658 int restore_size = TRUE;
9659 win_T *wp;
9660 char_u *sname;
9661 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009662 tabpage_T *old_curtab = curtab;
9663 int tabnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664
9665 if (ssop_flags & SSOP_BUFFERS)
9666 only_save_windows = FALSE; /* Save ALL buffers */
9667
9668 /*
9669 * Begin by setting the this_session variable, and then other
9670 * sessionable variables.
9671 */
9672#ifdef FEAT_EVAL
9673 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9674 return FAIL;
9675 if (ssop_flags & SSOP_GLOBALS)
9676 if (store_session_globals(fd) == FAIL)
9677 return FAIL;
9678#endif
9679
9680 /*
9681 * Close all windows but one.
9682 */
9683 if (put_line(fd, "silent only") == FAIL)
9684 return FAIL;
9685
9686 /*
9687 * Now a :cd command to the session directory or the current directory
9688 */
9689 if (ssop_flags & SSOP_SESDIR)
9690 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009691 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9692 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 return FAIL;
9694 }
9695 else if (ssop_flags & SSOP_CURDIR)
9696 {
9697 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9698 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009699 || fputs("cd ", fd) < 0
9700 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9701 || put_eol(fd) == FAIL)
9702 {
9703 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 vim_free(sname);
9707 }
9708
9709 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009710 * If there is an empty, unnamed buffer we will wipe it out later.
9711 * Remember the buffer number.
9712 */
9713 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9714 return FAIL;
9715 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9716 return FAIL;
9717 if (put_line(fd, "endif") == FAIL)
9718 return FAIL;
9719
9720 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 * Now save the current files, current buffer first.
9722 */
9723 if (put_line(fd, "set shortmess=aoO") == FAIL)
9724 return FAIL;
9725
9726 /* Now put the other buffers into the buffer list */
9727 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9728 {
9729 if (!(only_save_windows && buf->b_nwindows == 0)
9730 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9731 && buf->b_fname != NULL
9732 && buf->b_p_bl)
9733 {
9734 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9735 : buf->b_wininfo->wi_fpos.lnum) < 0
9736 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9737 return FAIL;
9738 }
9739 }
9740
9741 /* the global argument list */
9742 if (ses_arglist(fd, "args", &global_alist.al_ga,
9743 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9744 return FAIL;
9745
9746 if (ssop_flags & SSOP_RESIZE)
9747 {
9748 /* Note: after the restore we still check it worked!*/
9749 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9750 || put_eol(fd) == FAIL)
9751 return FAIL;
9752 }
9753
9754#ifdef FEAT_GUI
9755 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9756 {
9757 int x, y;
9758
9759 if (gui_mch_get_winpos(&x, &y) == OK)
9760 {
9761 /* Note: after the restore we still check it worked!*/
9762 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9763 return FAIL;
9764 }
9765 }
9766#endif
9767
9768 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009769 * May repeat putting Windows for each tab, when "tabpages" is in
9770 * 'sessionoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009772 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009774 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009776 goto_tabpage(tabnr);
9777 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780
Bram Moolenaar18144c82006-04-12 21:52:12 +00009781 /*
9782 * Before creating the window layout, try loading one file. If this
9783 * is aborted we don't end up with a number of useless windows.
9784 * This may have side effects! (e.g., compressed or network file).
9785 */
9786 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9787 {
9788 if (ses_do_win(wp)
9789 && wp->w_buffer->b_ffname != NULL
9790 && !wp->w_buffer->b_help
9791#ifdef FEAT_QUICKFIX
9792 && !bt_nofile(wp->w_buffer)
9793#endif
9794 )
9795 {
9796 if (fputs("edit ", fd) < 0
9797 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9798 return FAIL;
9799 if (!wp->w_arg_idx_invalid)
9800 edited_win = wp;
9801 break;
9802 }
9803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804
Bram Moolenaar18144c82006-04-12 21:52:12 +00009805 /*
9806 * Save current window layout.
9807 */
9808 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009810 if (ses_win_rec(fd, topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009812 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9813 return FAIL;
9814 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9815 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816
Bram Moolenaar18144c82006-04-12 21:52:12 +00009817 /*
9818 * Check if window sizes can be restored (no windows omitted).
9819 * Remember the window number of the current window after restoring.
9820 */
9821 nr = 0;
9822 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9823 {
9824 if (ses_do_win(wp))
9825 ++nr;
9826 else
9827 restore_size = FALSE;
9828 if (curwin == wp)
9829 cnr = nr;
9830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831
Bram Moolenaar18144c82006-04-12 21:52:12 +00009832 /* Go to the first window. */
9833 if (put_line(fd, "wincmd t") == FAIL)
9834 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009835
Bram Moolenaar18144c82006-04-12 21:52:12 +00009836 /*
9837 * If more than one window, see if sizes can be restored.
9838 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9839 * resized when moving between windows.
9840 * Do this before restoring the view, so that the topline and the
9841 * cursor can be set. This is done again below.
9842 */
9843 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9844 return FAIL;
9845 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9846 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847
Bram Moolenaar18144c82006-04-12 21:52:12 +00009848 /*
9849 * Restore the view of the window (options, file, cursor, etc.).
9850 */
9851 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9852 {
9853 if (!ses_do_win(wp))
9854 continue;
9855 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9856 return FAIL;
9857 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9858 return FAIL;
9859 }
9860
9861 /*
9862 * Restore cursor to the current window if it's not the first one.
9863 */
9864 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
9865 || put_eol(fd) == FAIL))
9866 return FAIL;
9867
9868 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009869 * Restore window sizes again after jumping around in windows, because
9870 * the current window has a minimum size while others may not.
9871 */
9872 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9873 return FAIL;
9874
Bram Moolenaar18144c82006-04-12 21:52:12 +00009875 /* Don't continue in another tab page when doing only the current one
9876 * or when at the last tab page. */
9877 if (!(ssop_flags & SSOP_TABPAGES) || curtab->tp_next == NULL)
9878 break;
9879 }
9880
9881 if (ssop_flags & SSOP_TABPAGES)
9882 {
9883 if (valid_tabpage(old_curtab))
9884 goto_tabpage_tp(old_curtab);
9885 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
9886 || put_eol(fd) == FAIL)
9887 return FAIL;
9888 }
9889
Bram Moolenaar9c102382006-05-03 21:26:49 +00009890 /*
9891 * Wipe out an empty unnamed buffer we started in.
9892 */
9893 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9894 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00009895 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009896 return FAIL;
9897 if (put_line(fd, "endif") == FAIL)
9898 return FAIL;
9899 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9900 return FAIL;
9901
9902 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9903 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9904 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9905 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906
9907 /*
9908 * Lastly, execute the x.vim file if it exists.
9909 */
9910 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9911 || put_line(fd, "if file_readable(s:sx)") == FAIL
9912 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9913 || put_line(fd, "endif") == FAIL)
9914 return FAIL;
9915
9916 return OK;
9917}
9918
9919 static int
9920ses_winsizes(fd, restore_size)
9921 FILE *fd;
9922 int restore_size;
9923{
9924 int n = 0;
9925 win_T *wp;
9926
9927 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9928 {
9929 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9930 {
9931 if (!ses_do_win(wp))
9932 continue;
9933 ++n;
9934
9935 /* restore height when not full height */
9936 if (wp->w_height + wp->w_status_height < topframe->fr_height
9937 && (fprintf(fd,
9938 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9939 n, (long)wp->w_height, Rows / 2, Rows) < 0
9940 || put_eol(fd) == FAIL))
9941 return FAIL;
9942
9943 /* restore width when not full width */
9944 if (wp->w_width < Columns && (fprintf(fd,
9945 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9946 n, (long)wp->w_width, Columns / 2, Columns) < 0
9947 || put_eol(fd) == FAIL))
9948 return FAIL;
9949 }
9950 }
9951 else
9952 {
9953 /* Just equalise window sizes */
9954 if (put_line(fd, "wincmd =") == FAIL)
9955 return FAIL;
9956 }
9957 return OK;
9958}
9959
9960/*
9961 * Write commands to "fd" to recursively create windows for frame "fr",
9962 * horizontally and vertically split.
9963 * After the commands the last window in the frame is the current window.
9964 * Returns FAIL when writing the commands to "fd" fails.
9965 */
9966 static int
9967ses_win_rec(fd, fr)
9968 FILE *fd;
9969 frame_T *fr;
9970{
9971 frame_T *frc;
9972 int count = 0;
9973
9974 if (fr->fr_layout != FR_LEAF)
9975 {
9976 /* Find first frame that's not skipped and then create a window for
9977 * each following one (first frame is already there). */
9978 frc = ses_skipframe(fr->fr_child);
9979 if (frc != NULL)
9980 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9981 {
9982 /* Make window as big as possible so that we have lots of room
9983 * to split. */
9984 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9985 || put_line(fd, fr->fr_layout == FR_COL
9986 ? "split" : "vsplit") == FAIL)
9987 return FAIL;
9988 ++count;
9989 }
9990
9991 /* Go back to the first window. */
9992 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9993 ? "%dwincmd k" : "%dwincmd h", count) < 0
9994 || put_eol(fd) == FAIL))
9995 return FAIL;
9996
9997 /* Recursively create frames/windows in each window of this column or
9998 * row. */
9999 frc = ses_skipframe(fr->fr_child);
10000 while (frc != NULL)
10001 {
10002 ses_win_rec(fd, frc);
10003 frc = ses_skipframe(frc->fr_next);
10004 /* Go to next window. */
10005 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10006 return FAIL;
10007 }
10008 }
10009 return OK;
10010}
10011
10012/*
10013 * Skip frames that don't contain windows we want to save in the Session.
10014 * Returns NULL when there none.
10015 */
10016 static frame_T *
10017ses_skipframe(fr)
10018 frame_T *fr;
10019{
10020 frame_T *frc;
10021
10022 for (frc = fr; frc != NULL; frc = frc->fr_next)
10023 if (ses_do_frame(frc))
10024 break;
10025 return frc;
10026}
10027
10028/*
10029 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10030 * the Session.
10031 */
10032 static int
10033ses_do_frame(fr)
10034 frame_T *fr;
10035{
10036 frame_T *frc;
10037
10038 if (fr->fr_layout == FR_LEAF)
10039 return ses_do_win(fr->fr_win);
10040 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10041 if (ses_do_frame(frc))
10042 return TRUE;
10043 return FALSE;
10044}
10045
10046/*
10047 * Return non-zero if window "wp" is to be stored in the Session.
10048 */
10049 static int
10050ses_do_win(wp)
10051 win_T *wp;
10052{
10053 if (wp->w_buffer->b_fname == NULL
10054#ifdef FEAT_QUICKFIX
10055 /* When 'buftype' is "nofile" can't restore the window contents. */
10056 || bt_nofile(wp->w_buffer)
10057#endif
10058 )
10059 return (ssop_flags & SSOP_BLANK);
10060 if (wp->w_buffer->b_help)
10061 return (ssop_flags & SSOP_HELP);
10062 return TRUE;
10063}
10064
10065/*
10066 * Write commands to "fd" to restore the view of a window.
10067 * Caller must make sure 'scrolloff' is zero.
10068 */
10069 static int
10070put_view(fd, wp, add_edit, flagp)
10071 FILE *fd;
10072 win_T *wp;
10073 int add_edit; /* add ":edit" command to view */
10074 unsigned *flagp; /* vop_flags or ssop_flags */
10075{
10076 win_T *save_curwin;
10077 int f;
10078 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010079 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080
10081 /* Always restore cursor position for ":mksession". For ":mkview" only
10082 * when 'viewoptions' contains "cursor". */
10083 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10084
10085 /*
10086 * Local argument list.
10087 */
10088 if (wp->w_alist == &global_alist)
10089 {
10090 if (put_line(fd, "argglobal") == FAIL)
10091 return FAIL;
10092 }
10093 else
10094 {
10095 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10096 flagp == &vop_flags
10097 || !(*flagp & SSOP_CURDIR)
10098 || wp->w_localdir != NULL, flagp) == FAIL)
10099 return FAIL;
10100 }
10101
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010102 /* Only when part of a session: restore the argument index. Some
10103 * arguments may have been deleted, check if the index is valid. */
10104 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
10105 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 {
10107 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
10108 || put_eol(fd) == FAIL)
10109 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010110 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 }
10112
10113 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010114 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 {
10116 /*
10117 * Load the file.
10118 */
10119 if (wp->w_buffer->b_ffname != NULL
10120#ifdef FEAT_QUICKFIX
10121 && !bt_nofile(wp->w_buffer)
10122#endif
10123 )
10124 {
10125 /*
10126 * Editing a file in this buffer: use ":edit file".
10127 * This may have side effects! (e.g., compressed or network file).
10128 */
10129 if (fputs("edit ", fd) < 0
10130 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10131 return FAIL;
10132 }
10133 else
10134 {
10135 /* No file in this buffer, just make it empty. */
10136 if (put_line(fd, "enew") == FAIL)
10137 return FAIL;
10138#ifdef FEAT_QUICKFIX
10139 if (wp->w_buffer->b_ffname != NULL)
10140 {
10141 /* The buffer does have a name, but it's not a file name. */
10142 if (fputs("file ", fd) < 0
10143 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10144 return FAIL;
10145 }
10146#endif
10147 do_cursor = FALSE;
10148 }
10149 }
10150
10151 /*
10152 * Local mappings and abbreviations.
10153 */
10154 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10155 && makemap(fd, wp->w_buffer) == FAIL)
10156 return FAIL;
10157
10158 /*
10159 * Local options. Need to go to the window temporarily.
10160 * Store only local values when using ":mkview" and when ":mksession" is
10161 * used and 'sessionoptions' doesn't include "options".
10162 * Some folding options are always stored when "folds" is included,
10163 * otherwise the folds would not be restored correctly.
10164 */
10165 save_curwin = curwin;
10166 curwin = wp;
10167 curbuf = curwin->w_buffer;
10168 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10169 f = makeset(fd, OPT_LOCAL,
10170 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10171#ifdef FEAT_FOLDING
10172 else if (*flagp & SSOP_FOLDS)
10173 f = makefoldset(fd);
10174#endif
10175 else
10176 f = OK;
10177 curwin = save_curwin;
10178 curbuf = curwin->w_buffer;
10179 if (f == FAIL)
10180 return FAIL;
10181
10182#ifdef FEAT_FOLDING
10183 /*
10184 * Save Folds when 'buftype' is empty and for help files.
10185 */
10186 if ((*flagp & SSOP_FOLDS)
10187 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010188# ifdef FEAT_QUICKFIX
10189 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10190# endif
10191 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 {
10193 if (put_folds(fd, wp) == FAIL)
10194 return FAIL;
10195 }
10196#endif
10197
10198 /*
10199 * Set the cursor after creating folds, since that moves the cursor.
10200 */
10201 if (do_cursor)
10202 {
10203
10204 /* Restore the cursor line in the file and relatively in the
10205 * window. Don't use "G", it changes the jumplist. */
10206 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10207 (long)wp->w_cursor.lnum,
10208 (long)(wp->w_cursor.lnum - wp->w_topline),
10209 (long)wp->w_height / 2, (long)wp->w_height) < 0
10210 || put_eol(fd) == FAIL
10211 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10212 || put_line(fd, "exe s:l") == FAIL
10213 || put_line(fd, "normal! zt") == FAIL
10214 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10215 || put_eol(fd) == FAIL)
10216 return FAIL;
10217 /* Restore the cursor column and left offset when not wrapping. */
10218 if (wp->w_cursor.col == 0)
10219 {
10220 if (put_line(fd, "normal! 0") == FAIL)
10221 return FAIL;
10222 }
10223 else
10224 {
10225 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10226 {
10227 if (fprintf(fd,
10228 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10229 (long)wp->w_cursor.col,
10230 (long)(wp->w_cursor.col - wp->w_leftcol),
10231 (long)wp->w_width / 2, (long)wp->w_width) < 0
10232 || put_eol(fd) == FAIL
10233 || put_line(fd, "if s:c > 0") == FAIL
10234 || fprintf(fd,
10235 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10236 (long)wp->w_cursor.col) < 0
10237 || put_eol(fd) == FAIL
10238 || put_line(fd, "else") == FAIL
10239 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10240 || put_eol(fd) == FAIL
10241 || put_line(fd, "endif") == FAIL)
10242 return FAIL;
10243 }
10244 else
10245 {
10246 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10247 || put_eol(fd) == FAIL)
10248 return FAIL;
10249 }
10250 }
10251 }
10252
10253 /*
10254 * Local directory.
10255 */
10256 if (wp->w_localdir != NULL)
10257 {
10258 if (fputs("lcd ", fd) < 0
10259 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10260 || put_eol(fd) == FAIL)
10261 return FAIL;
10262 }
10263
10264 return OK;
10265}
10266
10267/*
10268 * Write an argument list to the session file.
10269 * Returns FAIL if writing fails.
10270 */
10271 static int
10272ses_arglist(fd, cmd, gap, fullname, flagp)
10273 FILE *fd;
10274 char *cmd;
10275 garray_T *gap;
10276 int fullname; /* TRUE: use full path name */
10277 unsigned *flagp;
10278{
10279 int i;
10280 char_u buf[MAXPATHL];
10281 char_u *s;
10282
10283 if (gap->ga_len == 0)
10284 return put_line(fd, "silent! argdel *");
10285 if (fputs(cmd, fd) < 0)
10286 return FAIL;
10287 for (i = 0; i < gap->ga_len; ++i)
10288 {
10289 /* NULL file names are skipped (only happens when out of memory). */
10290 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10291 if (s != NULL)
10292 {
10293 if (fullname)
10294 {
10295 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10296 s = buf;
10297 }
10298 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10299 return FAIL;
10300 }
10301 }
10302 return put_eol(fd);
10303}
10304
10305/*
10306 * Write a buffer name to the session file.
10307 * Also ends the line.
10308 * Returns FAIL if writing fails.
10309 */
10310 static int
10311ses_fname(fd, buf, flagp)
10312 FILE *fd;
10313 buf_T *buf;
10314 unsigned *flagp;
10315{
10316 char_u *name;
10317
10318 /* Use the short file name if the current directory is known at the time
10319 * the session file will be sourced. Don't do this for ":mkview", we
10320 * don't know the current directory. */
10321 if (buf->b_sfname != NULL
10322 && flagp == &ssop_flags
10323 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10324 name = buf->b_sfname;
10325 else
10326 name = buf->b_ffname;
10327 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10328 return FAIL;
10329 return OK;
10330}
10331
10332/*
10333 * Write a file name to the session file.
10334 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10335 * characters.
10336 * Returns FAIL if writing fails.
10337 */
10338 static int
10339ses_put_fname(fd, name, flagp)
10340 FILE *fd;
10341 char_u *name;
10342 unsigned *flagp;
10343{
10344 char_u *sname;
10345 int retval = OK;
10346 int c;
10347
10348 sname = home_replace_save(NULL, name);
10349 if (sname != NULL)
10350 name = sname;
10351 while (*name != NUL)
10352 {
10353#ifdef FEAT_MBYTE
10354 {
10355 int l;
10356
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010357 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 {
10359 /* copy a multibyte char */
10360 while (--l >= 0)
10361 {
10362 if (putc(*name, fd) != *name)
10363 retval = FAIL;
10364 ++name;
10365 }
10366 continue;
10367 }
10368 }
10369#endif
10370 c = *name++;
10371 if (c == '\\' && (*flagp & SSOP_SLASH))
10372 /* change a backslash to a forward slash */
10373 c = '/';
10374 else if ((vim_strchr(escape_chars, c) != NULL
10375#ifdef BACKSLASH_IN_FILENAME
10376 && c != '\\'
10377#endif
10378 ) || c == '#' || c == '%')
10379 {
10380 /* escape a special character with a backslash */
10381 if (putc('\\', fd) != '\\')
10382 retval = FAIL;
10383 }
10384 if (putc(c, fd) != c)
10385 retval = FAIL;
10386 }
10387 vim_free(sname);
10388 return retval;
10389}
10390
10391/*
10392 * ":loadview [nr]"
10393 */
10394 static void
10395ex_loadview(eap)
10396 exarg_T *eap;
10397{
10398 char_u *fname;
10399
10400 fname = get_view_file(*eap->arg);
10401 if (fname != NULL)
10402 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010403 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 vim_free(fname);
10405 }
10406}
10407
10408/*
10409 * Get the name of the view file for the current buffer.
10410 */
10411 static char_u *
10412get_view_file(c)
10413 int c;
10414{
10415 int len = 0;
10416 char_u *p, *s;
10417 char_u *retval;
10418 char_u *sname;
10419
10420 if (curbuf->b_ffname == NULL)
10421 {
10422 EMSG(_(e_noname));
10423 return NULL;
10424 }
10425 sname = home_replace_save(NULL, curbuf->b_ffname);
10426 if (sname == NULL)
10427 return NULL;
10428
10429 /*
10430 * We want a file name without separators, because we're not going to make
10431 * a directory.
10432 * "normal" path separator -> "=+"
10433 * "=" -> "=="
10434 * ":" path separator -> "=-"
10435 */
10436 for (p = sname; *p; ++p)
10437 if (*p == '=' || vim_ispathsep(*p))
10438 ++len;
10439 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10440 if (retval != NULL)
10441 {
10442 STRCPY(retval, p_vdir);
10443 add_pathsep(retval);
10444 s = retval + STRLEN(retval);
10445 for (p = sname; *p; ++p)
10446 {
10447 if (*p == '=')
10448 {
10449 *s++ = '=';
10450 *s++ = '=';
10451 }
10452 else if (vim_ispathsep(*p))
10453 {
10454 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010455#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 || defined(VMS)
10457 if (*p == ':')
10458 *s++ = '-';
10459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010461 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 }
10463 else
10464 *s++ = *p;
10465 }
10466 *s++ = '=';
10467 *s++ = c;
10468 STRCPY(s, ".vim");
10469 }
10470
10471 vim_free(sname);
10472 return retval;
10473}
10474
10475#endif /* FEAT_SESSION */
10476
10477/*
10478 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10479 * Return FAIL for a write error.
10480 */
10481 int
10482put_eol(fd)
10483 FILE *fd;
10484{
10485 if (
10486#ifdef USE_CRNL
10487 (
10488# ifdef MKSESSION_NL
10489 !mksession_nl &&
10490# endif
10491 (putc('\r', fd) < 0)) ||
10492#endif
10493 (putc('\n', fd) < 0))
10494 return FAIL;
10495 return OK;
10496}
10497
10498/*
10499 * Write a line to "fd".
10500 * Return FAIL for a write error.
10501 */
10502 int
10503put_line(fd, s)
10504 FILE *fd;
10505 char *s;
10506{
10507 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10508 return FAIL;
10509 return OK;
10510}
10511
10512#ifdef FEAT_VIMINFO
10513/*
10514 * ":rviminfo" and ":wviminfo".
10515 */
10516 static void
10517ex_viminfo(eap)
10518 exarg_T *eap;
10519{
10520 char_u *save_viminfo;
10521
10522 save_viminfo = p_viminfo;
10523 if (*p_viminfo == NUL)
10524 p_viminfo = (char_u *)"'100";
10525 if (eap->cmdidx == CMD_rviminfo)
10526 {
10527 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10528 EMSG(_("E195: Cannot open viminfo file for reading"));
10529 }
10530 else
10531 write_viminfo(eap->arg, eap->forceit);
10532 p_viminfo = save_viminfo;
10533}
10534#endif
10535
10536#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010537/*
10538 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010539 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 void
10542dialog_msg(buff, format, fname)
10543 char_u *buff;
10544 char *format;
10545 char_u *fname;
10546{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 if (fname == NULL)
10548 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010549 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550}
10551#endif
10552
10553/*
10554 * ":behave {mswin,xterm}"
10555 */
10556 static void
10557ex_behave(eap)
10558 exarg_T *eap;
10559{
10560 if (STRCMP(eap->arg, "mswin") == 0)
10561 {
10562 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10563 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10564 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10565 set_option_value((char_u *)"keymodel", 0L,
10566 (char_u *)"startsel,stopsel", 0);
10567 }
10568 else if (STRCMP(eap->arg, "xterm") == 0)
10569 {
10570 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10571 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10572 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10573 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10574 }
10575 else
10576 EMSG2(_(e_invarg2), eap->arg);
10577}
10578
10579#ifdef FEAT_AUTOCMD
10580static int filetype_detect = FALSE;
10581static int filetype_plugin = FALSE;
10582static int filetype_indent = FALSE;
10583
10584/*
10585 * ":filetype [plugin] [indent] {on,off,detect}"
10586 * on: Load the filetype.vim file to install autocommands for file types.
10587 * off: Load the ftoff.vim file to remove all autocommands for file types.
10588 * plugin on: load filetype.vim and ftplugin.vim
10589 * plugin off: load ftplugof.vim
10590 * indent on: load filetype.vim and indent.vim
10591 * indent off: load indoff.vim
10592 */
10593 static void
10594ex_filetype(eap)
10595 exarg_T *eap;
10596{
10597 char_u *arg = eap->arg;
10598 int plugin = FALSE;
10599 int indent = FALSE;
10600
10601 if (*eap->arg == NUL)
10602 {
10603 /* Print current status. */
10604 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10605 filetype_detect ? "ON" : "OFF",
10606 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10607 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10608 return;
10609 }
10610
10611 /* Accept "plugin" and "indent" in any order. */
10612 for (;;)
10613 {
10614 if (STRNCMP(arg, "plugin", 6) == 0)
10615 {
10616 plugin = TRUE;
10617 arg = skipwhite(arg + 6);
10618 continue;
10619 }
10620 if (STRNCMP(arg, "indent", 6) == 0)
10621 {
10622 indent = TRUE;
10623 arg = skipwhite(arg + 6);
10624 continue;
10625 }
10626 break;
10627 }
10628 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10629 {
10630 if (*arg == 'o' || !filetype_detect)
10631 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010632 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 filetype_detect = TRUE;
10634 if (plugin)
10635 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010636 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 filetype_plugin = TRUE;
10638 }
10639 if (indent)
10640 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010641 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 filetype_indent = TRUE;
10643 }
10644 }
10645 if (*arg == 'd')
10646 {
10647 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010648 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 }
10650 }
10651 else if (STRCMP(arg, "off") == 0)
10652 {
10653 if (plugin || indent)
10654 {
10655 if (plugin)
10656 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010657 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 filetype_plugin = FALSE;
10659 }
10660 if (indent)
10661 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010662 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 filetype_indent = FALSE;
10664 }
10665 }
10666 else
10667 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010668 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 filetype_detect = FALSE;
10670 }
10671 }
10672 else
10673 EMSG2(_(e_invarg2), arg);
10674}
10675
10676/*
10677 * ":setfiletype {name}"
10678 */
10679 static void
10680ex_setfiletype(eap)
10681 exarg_T *eap;
10682{
10683 if (!did_filetype)
10684 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10685}
10686#endif
10687
10688/*ARGSUSED*/
10689 static void
10690ex_digraphs(eap)
10691 exarg_T *eap;
10692{
10693#ifdef FEAT_DIGRAPHS
10694 if (*eap->arg != NUL)
10695 putdigraph(eap->arg);
10696 else
10697 listdigraphs();
10698#else
10699 EMSG(_("E196: No digraphs in this version"));
10700#endif
10701}
10702
10703 static void
10704ex_set(eap)
10705 exarg_T *eap;
10706{
10707 int flags = 0;
10708
10709 if (eap->cmdidx == CMD_setlocal)
10710 flags = OPT_LOCAL;
10711 else if (eap->cmdidx == CMD_setglobal)
10712 flags = OPT_GLOBAL;
10713#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10714 if (cmdmod.browse && flags == 0)
10715 ex_options(eap);
10716 else
10717#endif
10718 (void)do_set(eap->arg, flags);
10719}
10720
10721#ifdef FEAT_SEARCH_EXTRA
10722/*
10723 * ":nohlsearch"
10724 */
10725/*ARGSUSED*/
10726 static void
10727ex_nohlsearch(eap)
10728 exarg_T *eap;
10729{
10730 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010731 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732}
10733
10734/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010735 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 * Sets nextcmd to the start of the next command, if any. Also called when
10737 * skipping commands to find the next command.
10738 */
10739 static void
10740ex_match(eap)
10741 exarg_T *eap;
10742{
10743 char_u *p;
10744 char_u *end;
10745 int c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010746 int mi;
10747
10748 if (eap->line2 <= 3)
10749 mi = eap->line2 - 1;
10750 else
10751 {
10752 EMSG(e_invcmd);
10753 return;
10754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
10756 /* First clear any old pattern. */
10757 if (!eap->skip)
10758 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010759 vim_free(curwin->w_match[mi].regprog);
10760 curwin->w_match[mi].regprog = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010761 vim_free(curwin->w_match_pat[mi]);
10762 curwin->w_match_pat[mi] = NULL;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010763 redraw_later(SOME_VALID); /* always need a redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 }
10765
10766 if (ends_excmd(*eap->arg))
10767 end = eap->arg;
10768 else if ((STRNICMP(eap->arg, "none", 4) == 0
10769 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10770 end = eap->arg + 4;
10771 else
10772 {
10773 p = skiptowhite(eap->arg);
10774 if (!eap->skip)
10775 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010776 curwin->w_match_id[mi] = syn_namen2id(eap->arg,
10777 (int)(p - eap->arg));
10778 if (curwin->w_match_id[mi] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 {
10780 EMSG2(_(e_nogroup), eap->arg);
10781 return;
10782 }
10783 }
10784 p = skipwhite(p);
10785 if (*p == NUL)
10786 {
10787 /* There must be two arguments. */
10788 EMSG2(_(e_invarg2), eap->arg);
10789 return;
10790 }
10791 end = skip_regexp(p + 1, *p, TRUE, NULL);
10792 if (!eap->skip)
10793 {
10794 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10795 {
10796 eap->errmsg = e_trailing;
10797 return;
10798 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010799 if (*end != *p)
10800 {
10801 EMSG2(_(e_invarg2), p);
10802 return;
10803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804
10805 c = *end;
10806 *end = NUL;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010807 curwin->w_match[mi].regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010808 if (curwin->w_match[mi].regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 {
10810 EMSG2(_(e_invarg2), p);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010811 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 return;
10813 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010814 curwin->w_match_pat[mi] = vim_strsave(p + 1);
10815 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 }
10817 }
10818 eap->nextcmd = find_nextcmd(end);
10819}
10820#endif
10821
10822#ifdef FEAT_CRYPT
10823/*
10824 * ":X": Get crypt key
10825 */
10826/*ARGSUSED*/
10827 static void
10828ex_X(eap)
10829 exarg_T *eap;
10830{
10831 (void)get_crypt_key(TRUE, TRUE);
10832}
10833#endif
10834
10835#ifdef FEAT_FOLDING
10836 static void
10837ex_fold(eap)
10838 exarg_T *eap;
10839{
10840 if (foldManualAllowed(TRUE))
10841 foldCreate(eap->line1, eap->line2);
10842}
10843
10844 static void
10845ex_foldopen(eap)
10846 exarg_T *eap;
10847{
10848 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10849 eap->forceit, FALSE);
10850}
10851
10852 static void
10853ex_folddo(eap)
10854 exarg_T *eap;
10855{
10856 linenr_T lnum;
10857
10858 /* First set the marks for all lines closed/open. */
10859 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10860 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10861 ml_setmarked(lnum);
10862
10863 /* Execute the command on the marked lines. */
10864 global_exe(eap->arg);
10865 ml_clearmarked(); /* clear rest of the marks */
10866}
10867#endif