blob: 74998d93bab7978319b31ff232042e03b3053eb3 [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));
153static void ex_all __ARGS((exarg_T *eap));
154static void ex_resize __ARGS((exarg_T *eap));
155static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000156static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000157static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000158static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000159static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000160static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#else
162# define ex_close ex_ni
163# define ex_only ex_ni
164# define ex_all ex_ni
165# define ex_resize ex_ni
166# define ex_splitview ex_ni
167# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000168# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000169# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000170# define ex_tabs ex_ni
171# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000172# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif
174#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
175static void ex_pclose __ARGS((exarg_T *eap));
176static void ex_ptag __ARGS((exarg_T *eap));
177static void ex_pedit __ARGS((exarg_T *eap));
178#else
179# define ex_pclose ex_ni
180# define ex_ptag ex_ni
181# define ex_pedit ex_ni
182#endif
183static void ex_hide __ARGS((exarg_T *eap));
184static void ex_stop __ARGS((exarg_T *eap));
185static void ex_exit __ARGS((exarg_T *eap));
186static void ex_print __ARGS((exarg_T *eap));
187#ifdef FEAT_BYTEOFF
188static void ex_goto __ARGS((exarg_T *eap));
189#else
190# define ex_goto ex_ni
191#endif
192static void ex_shell __ARGS((exarg_T *eap));
193static void ex_preserve __ARGS((exarg_T *eap));
194static void ex_recover __ARGS((exarg_T *eap));
195#ifndef FEAT_LISTCMDS
196# define ex_argedit ex_ni
197# define ex_argadd ex_ni
198# define ex_argdelete ex_ni
199# define ex_listdo ex_ni
200#endif
201static void ex_mode __ARGS((exarg_T *eap));
202static void ex_wrongmodifier __ARGS((exarg_T *eap));
203static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000204static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205static void ex_edit __ARGS((exarg_T *eap));
206#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
207# define ex_drop ex_ni
208#endif
209#ifndef FEAT_GUI
210# define ex_gui ex_nogui
211static void ex_nogui __ARGS((exarg_T *eap));
212#endif
213#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
214static void ex_tearoff __ARGS((exarg_T *eap));
215#else
216# define ex_tearoff ex_ni
217#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000218#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219static void ex_popup __ARGS((exarg_T *eap));
220#else
221# define ex_popup ex_ni
222#endif
223#ifndef FEAT_GUI_MSWIN
224# define ex_simalt ex_ni
225#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000226#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227# define gui_mch_find_dialog ex_ni
228# define gui_mch_replace_dialog ex_ni
229#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000230#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231# define ex_helpfind ex_ni
232#endif
233#ifndef FEAT_CSCOPE
234# define do_cscope ex_ni
235# define do_scscope ex_ni
236# define do_cstag ex_ni
237#endif
238#ifndef FEAT_SYN_HL
239# define ex_syntax ex_ni
Bram Moolenaarb765d632005-06-07 21:00:02 +0000240# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000241# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000242# define ex_spelldump ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000243# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000244#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000245#ifndef FEAT_MZSCHEME
246# define ex_mzscheme ex_script_ni
247# define ex_mzfile ex_ni
248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifndef FEAT_PERL
250# define ex_perl ex_script_ni
251# define ex_perldo ex_ni
252#endif
253#ifndef FEAT_PYTHON
254# define ex_python ex_script_ni
255# define ex_pyfile ex_ni
256#endif
257#ifndef FEAT_TCL
258# define ex_tcl ex_script_ni
259# define ex_tcldo ex_ni
260# define ex_tclfile ex_ni
261#endif
262#ifndef FEAT_RUBY
263# define ex_ruby ex_script_ni
264# define ex_rubydo ex_ni
265# define ex_rubyfile ex_ni
266#endif
267#ifndef FEAT_SNIFF
268# define ex_sniff ex_ni
269#endif
270#ifndef FEAT_KEYMAP
271# define ex_loadkeymap ex_ni
272#endif
273static void ex_swapname __ARGS((exarg_T *eap));
274static void ex_syncbind __ARGS((exarg_T *eap));
275static void ex_read __ARGS((exarg_T *eap));
276static void ex_cd __ARGS((exarg_T *eap));
277static void ex_pwd __ARGS((exarg_T *eap));
278static void ex_equal __ARGS((exarg_T *eap));
279static void ex_sleep __ARGS((exarg_T *eap));
280static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
281static void ex_winsize __ARGS((exarg_T *eap));
282#ifdef FEAT_WINDOWS
283static void ex_wincmd __ARGS((exarg_T *eap));
284#else
285# define ex_wincmd ex_ni
286#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000287#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288static void ex_winpos __ARGS((exarg_T *eap));
289#else
290# define ex_winpos ex_ni
291#endif
292static void ex_operators __ARGS((exarg_T *eap));
293static void ex_put __ARGS((exarg_T *eap));
294static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000295static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static void ex_submagic __ARGS((exarg_T *eap));
297static void ex_join __ARGS((exarg_T *eap));
298static void ex_at __ARGS((exarg_T *eap));
299static void ex_bang __ARGS((exarg_T *eap));
300static void ex_undo __ARGS((exarg_T *eap));
301static void ex_redo __ARGS((exarg_T *eap));
302static void ex_redir __ARGS((exarg_T *eap));
303static void ex_redraw __ARGS((exarg_T *eap));
304static void ex_redrawstatus __ARGS((exarg_T *eap));
305static void close_redir __ARGS((void));
306static void ex_mkrc __ARGS((exarg_T *eap));
307static void ex_mark __ARGS((exarg_T *eap));
308#ifdef FEAT_USR_CMDS
309static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000310static 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 +0000311#endif
312#ifdef FEAT_EX_EXTRA
313static void ex_normal __ARGS((exarg_T *eap));
314static void ex_startinsert __ARGS((exarg_T *eap));
315static void ex_stopinsert __ARGS((exarg_T *eap));
316#else
317# define ex_normal ex_ni
318# define ex_align ex_ni
319# define ex_retab ex_ni
320# define ex_startinsert ex_ni
321# define ex_stopinsert ex_ni
322# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000323# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#endif
325#ifdef FEAT_FIND_ID
326static void ex_checkpath __ARGS((exarg_T *eap));
327static void ex_findpat __ARGS((exarg_T *eap));
328#else
329# define ex_findpat ex_ni
330# define ex_checkpath ex_ni
331#endif
332#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
333static void ex_psearch __ARGS((exarg_T *eap));
334#else
335# define ex_psearch ex_ni
336#endif
337static void ex_tag __ARGS((exarg_T *eap));
338static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
339#ifndef FEAT_EVAL
340# define ex_scriptnames ex_ni
341# define ex_finish ex_ni
342# define ex_echo ex_ni
343# define ex_echohl ex_ni
344# define ex_execute ex_ni
345# define ex_call ex_ni
346# define ex_if ex_ni
347# define ex_endif ex_ni
348# define ex_else ex_ni
349# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000350# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351# define ex_continue ex_ni
352# define ex_break ex_ni
353# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000354# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355# define ex_throw ex_ni
356# define ex_try ex_ni
357# define ex_catch ex_ni
358# define ex_finally ex_ni
359# define ex_endtry ex_ni
360# define ex_endfunction ex_ni
361# define ex_let ex_ni
362# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000363# define ex_lockvar ex_ni
364# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365# define ex_function ex_ni
366# define ex_delfunction ex_ni
367# define ex_return ex_ni
368#endif
369static char_u *arg_all __ARGS((void));
370#ifdef FEAT_SESSION
371static int makeopens __ARGS((FILE *fd, char_u *dirnow));
372static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
373static void ex_loadview __ARGS((exarg_T *eap));
374static char_u *get_view_file __ARGS((int c));
375#else
376# define ex_loadview ex_ni
377#endif
378#ifndef FEAT_EVAL
379# define ex_compiler ex_ni
380#endif
381#ifdef FEAT_VIMINFO
382static void ex_viminfo __ARGS((exarg_T *eap));
383#else
384# define ex_viminfo ex_ni
385#endif
386static void ex_behave __ARGS((exarg_T *eap));
387#ifdef FEAT_AUTOCMD
388static void ex_filetype __ARGS((exarg_T *eap));
389static void ex_setfiletype __ARGS((exarg_T *eap));
390#else
391# define ex_filetype ex_ni
392# define ex_setfiletype ex_ni
393#endif
394#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000395# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396# define ex_diffpatch ex_ni
397# define ex_diffgetput ex_ni
398# define ex_diffsplit ex_ni
399# define ex_diffthis ex_ni
400# define ex_diffupdate ex_ni
401#endif
402static void ex_digraphs __ARGS((exarg_T *eap));
403static void ex_set __ARGS((exarg_T *eap));
404#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
405# define ex_options ex_ni
406#endif
407#ifdef FEAT_SEARCH_EXTRA
408static void ex_nohlsearch __ARGS((exarg_T *eap));
409static void ex_match __ARGS((exarg_T *eap));
410#else
411# define ex_nohlsearch ex_ni
412# define ex_match ex_ni
413#endif
414#ifdef FEAT_CRYPT
415static void ex_X __ARGS((exarg_T *eap));
416#else
417# define ex_X ex_ni
418#endif
419#ifdef FEAT_FOLDING
420static void ex_fold __ARGS((exarg_T *eap));
421static void ex_foldopen __ARGS((exarg_T *eap));
422static void ex_folddo __ARGS((exarg_T *eap));
423#else
424# define ex_fold ex_ni
425# define ex_foldopen ex_ni
426# define ex_folddo ex_ni
427#endif
428#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
429 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
430# define ex_language ex_ni
431#endif
432#ifndef FEAT_SIGNS
433# define ex_sign ex_ni
434#endif
435#ifndef FEAT_SUN_WORKSHOP
436# define ex_wsverb ex_ni
437#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000438#ifndef FEAT_NETBEANS_INTG
439# define ex_nbkey ex_ni
440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442#ifndef FEAT_EVAL
443# define ex_debug ex_ni
444# define ex_breakadd ex_ni
445# define ex_debuggreedy ex_ni
446# define ex_breakdel ex_ni
447# define ex_breaklist ex_ni
448#endif
449
450#ifndef FEAT_CMDHIST
451# define ex_history ex_ni
452#endif
453#ifndef FEAT_JUMPLIST
454# define ex_jumps ex_ni
455# define ex_changes ex_ni
456#endif
457
Bram Moolenaar05159a02005-02-26 23:04:13 +0000458#ifndef FEAT_PROFILE
459# define ex_profile ex_ni
460#endif
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462/*
463 * Declare cmdnames[].
464 */
465#define DO_DECLARE_EXCMD
466#include "ex_cmds.h"
467
468/*
469 * Table used to quickly search for a command, based on its first character.
470 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000471static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472{
473 CMD_append,
474 CMD_buffer,
475 CMD_change,
476 CMD_delete,
477 CMD_edit,
478 CMD_file,
479 CMD_global,
480 CMD_help,
481 CMD_insert,
482 CMD_join,
483 CMD_k,
484 CMD_list,
485 CMD_move,
486 CMD_next,
487 CMD_open,
488 CMD_print,
489 CMD_quit,
490 CMD_read,
491 CMD_substitute,
492 CMD_t,
493 CMD_undo,
494 CMD_vglobal,
495 CMD_write,
496 CMD_xit,
497 CMD_yank,
498 CMD_z,
499 CMD_bang
500};
501
502static char_u dollar_command[2] = {'$', 0};
503
504
505#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000506/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507typedef struct
508{
509 char_u *line; /* command line */
510 linenr_T lnum; /* sourcing_lnum of the line */
511} wcmd_T;
512
513/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000514 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000516 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000518struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
520 garray_T *lines_gap; /* growarray with line info */
521 int current_line; /* last read line from growarray */
522 int repeating; /* TRUE when looping a second time */
523 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
524 char_u *(*getline) __ARGS((int, void *, int));
525 void *cookie;
526};
527
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000528static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
529static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000531
532/* Struct to save a few things while debugging. Used in do_cmdline() only. */
533struct dbg_stuff
534{
535 int trylevel;
536 int force_abort;
537 except_T *caught_stack;
538 char_u *vv_exception;
539 char_u *vv_throwpoint;
540 int did_emsg;
541 int got_int;
542 int did_throw;
543 int need_rethrow;
544 int check_cstack;
545 except_T *current_exception;
546};
547
548static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
549static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
550
551 static void
552save_dbg_stuff(dsp)
553 struct dbg_stuff *dsp;
554{
555 dsp->trylevel = trylevel; trylevel = 0;
556 dsp->force_abort = force_abort; force_abort = FALSE;
557 dsp->caught_stack = caught_stack; caught_stack = NULL;
558 dsp->vv_exception = v_exception(NULL);
559 dsp->vv_throwpoint = v_throwpoint(NULL);
560
561 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
562 dsp->did_emsg = did_emsg; did_emsg = FALSE;
563 dsp->got_int = got_int; got_int = FALSE;
564 dsp->did_throw = did_throw; did_throw = FALSE;
565 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
566 dsp->check_cstack = check_cstack; check_cstack = FALSE;
567 dsp->current_exception = current_exception; current_exception = NULL;
568}
569
570 static void
571restore_dbg_stuff(dsp)
572 struct dbg_stuff *dsp;
573{
574 suppress_errthrow = FALSE;
575 trylevel = dsp->trylevel;
576 force_abort = dsp->force_abort;
577 caught_stack = dsp->caught_stack;
578 (void)v_exception(dsp->vv_exception);
579 (void)v_throwpoint(dsp->vv_throwpoint);
580 did_emsg = dsp->did_emsg;
581 got_int = dsp->got_int;
582 did_throw = dsp->did_throw;
583 need_rethrow = dsp->need_rethrow;
584 check_cstack = dsp->check_cstack;
585 current_exception = dsp->current_exception;
586}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587#endif
588
589
590/*
591 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
592 * command is given.
593 */
594 void
595do_exmode(improved)
596 int improved; /* TRUE for "improved Ex" mode */
597{
598 int save_msg_scroll;
599 int prev_msg_row;
600 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000601 int changedtick;
602
603 if (improved)
604 exmode_active = EXMODE_VIM;
605 else
606 exmode_active = EXMODE_NORMAL;
607 State = NORMAL;
608
609 /* When using ":global /pat/ visual" and then "Q" we return to continue
610 * the :global command. */
611 if (global_busy)
612 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614 save_msg_scroll = msg_scroll;
615 ++RedrawingDisabled; /* don't redisplay the window */
616 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#ifdef FEAT_GUI
618 /* Ignore scrollbar and mouse events in Ex mode */
619 ++hold_gui_events;
620#endif
621#ifdef FEAT_SNIFF
622 want_sniff_request = 0; /* No K_SNIFF wanted */
623#endif
624
625 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
626 while (exmode_active)
627 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000628#ifdef FEAT_EX_EXTRA
629 /* Check for a ":normal" command and no more characters left. */
630 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
631 {
632 exmode_active = FALSE;
633 break;
634 }
635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 msg_scroll = TRUE;
637 need_wait_return = FALSE;
638 ex_pressedreturn = FALSE;
639 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000640 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 prev_msg_row = msg_row;
642 prev_line = curwin->w_cursor.lnum;
643#ifdef FEAT_SNIFF
644 ProcessSniffRequests();
645#endif
646 if (improved)
647 {
648 cmdline_row = msg_row;
649 do_cmdline(NULL, getexline, NULL, 0);
650 }
651 else
652 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
653 lines_left = Rows - 1;
654
Bram Moolenaardf177f62005-02-22 08:39:57 +0000655 if ((prev_line != curwin->w_cursor.lnum
656 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000658 if (curbuf->b_ml.ml_flags & ML_EMPTY)
659 EMSG(_(e_emptybuf));
660 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000662 if (ex_pressedreturn)
663 {
664 /* go up one line, to overwrite the ":<CR>" line, so the
665 * output doensn't contain empty lines. */
666 msg_row = prev_msg_row;
667 if (prev_msg_row == Rows - 1)
668 msg_row--;
669 }
670 msg_col = 0;
671 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
672 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
676 {
677 if (curbuf->b_ml.ml_flags & ML_EMPTY)
678 EMSG(_(e_emptybuf));
679 else
680 EMSG(_("E501: At end-of-file"));
681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 }
683
684#ifdef FEAT_GUI
685 --hold_gui_events;
686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 --RedrawingDisabled;
688 --no_wait_return;
689 update_screen(CLEAR);
690 need_wait_return = FALSE;
691 msg_scroll = save_msg_scroll;
692}
693
694/*
695 * Execute a simple command line. Used for translated commands like "*".
696 */
697 int
698do_cmdline_cmd(cmd)
699 char_u *cmd;
700{
701 return do_cmdline(cmd, NULL, NULL,
702 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
703}
704
705/*
706 * do_cmdline(): execute one Ex command line
707 *
708 * 1. Execute "cmdline" when it is not NULL.
709 * If "cmdline" is NULL, or more lines are needed, getline() is used.
710 * 2. Split up in parts separated with '|'.
711 *
712 * This function can be called recursively!
713 *
714 * flags:
715 * DOCMD_VERBOSE - The command will be included in the error message.
716 * DOCMD_NOWAIT - Don't call wait_return() and friends.
717 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
718 * DOCMD_KEYTYPED - Don't reset KeyTyped.
719 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
720 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
721 *
722 * return FAIL if cmdline could not be executed, OK otherwise
723 */
724 int
725do_cmdline(cmdline, getline, cookie, flags)
726 char_u *cmdline;
727 char_u *(*getline) __ARGS((int, void *, int));
728 void *cookie; /* argument for getline() */
729 int flags;
730{
731 char_u *next_cmdline; /* next cmd to execute */
732 char_u *cmdline_copy = NULL; /* copy of cmd line */
733 int used_getline = FALSE; /* used "getline" to obtain command */
734 static int recursive = 0; /* recursive depth */
735 int msg_didout_before_start = 0;
736 int count = 0; /* line number count */
737 int did_inc = FALSE; /* incremented RedrawingDisabled */
738 int retval = OK;
739#ifdef FEAT_EVAL
740 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000741 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 int current_line = 0; /* active line in lines_ga */
743 char_u *fname = NULL; /* function or script name */
744 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
745 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000746 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 int initial_trylevel;
748 struct msglist **saved_msg_list = NULL;
749 struct msglist *private_msg_list;
750
751 /* "getline" and "cookie" passed to do_one_cmd() */
752 char_u *(*cmd_getline) __ARGS((int, void *, int));
753 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000754 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000756 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#else
758# define cmd_getline getline
759# define cmd_cookie cookie
760#endif
761 static int call_depth = 0; /* recursiveness */
762
763#ifdef FEAT_EVAL
764 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
765 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000766 * This ensures that the do_errthrow() call in do_one_cmd() does not
767 * combine the messages stored by an earlier invocation of do_one_cmd()
768 * with the command name of the later one. This would happen when
769 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 saved_msg_list = msg_list;
771 msg_list = &private_msg_list;
772 private_msg_list = NULL;
773#endif
774
775 /* It's possible to create an endless loop with ":execute", catch that
776 * here. The value of 200 allows nested function calls, ":source", etc. */
777 if (call_depth == 200)
778 {
779 EMSG(_("E169: Command too recursive"));
780#ifdef FEAT_EVAL
781 /* When converting to an exception, we do not include the command name
782 * since this is not an error of the specific command. */
783 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
784 msg_list = saved_msg_list;
785#endif
786 return FAIL;
787 }
788 ++call_depth;
789
790#ifdef FEAT_EVAL
791 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000792 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 cstack.cs_trylevel = 0;
794 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000795 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
797
798 real_cookie = getline_cookie(getline, cookie);
799
800 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000801 getline_is_func = getline_equal(getline, cookie, get_func_line);
802 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 ++ex_nesting_level;
804
805 /* Get the function or script name and the address where the next breakpoint
806 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
809 fname = func_name(real_cookie);
810 breakpoint = func_breakpoint(real_cookie);
811 dbg_tick = func_dbg_tick(real_cookie);
812 }
813 else if (getline_equal(getline, cookie, getsourceline))
814 {
815 fname = sourcing_name;
816 breakpoint = source_breakpoint(real_cookie);
817 dbg_tick = source_dbg_tick(real_cookie);
818 }
819
820 /*
821 * Initialize "force_abort" and "suppress_errthrow" at the top level.
822 */
823 if (!recursive)
824 {
825 force_abort = FALSE;
826 suppress_errthrow = FALSE;
827 }
828
829 /*
830 * If requested, store and reset the global values controlling the
831 * exception handling (used when debugging).
832 */
833 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000834 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
836 initial_trylevel = trylevel;
837
838 /*
839 * "did_throw" will be set to TRUE when an exception is being thrown.
840 */
841 did_throw = FALSE;
842#endif
843 /*
844 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000845 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 * If force_abort is set, we cancel everything.
847 */
848 did_emsg = FALSE;
849
850 /*
851 * KeyTyped is only set when calling vgetc(). Reset it here when not
852 * calling vgetc() (sourced command lines).
853 */
854 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
855 KeyTyped = FALSE;
856
857 /*
858 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000859 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 * - for multiple commands on one line, separated with '|'
861 * - when repeating until there are no more lines (for ":source")
862 */
863 next_cmdline = cmdline;
864 do
865 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000866#ifdef FEAT_EVAL
867 getline_is_func = getline_equal(getline, cookie, get_func_line);
868#endif
869
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000870 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 if (next_cmdline == NULL
872#ifdef FEAT_EVAL
873 && !force_abort
874 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000875 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#endif
877 )
878 did_emsg = FALSE;
879
880 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000881 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 * 2. If no line given: Get an allocated line with getline().
883 * 3. If a line is given: Make a copy, so we can mess with it.
884 */
885
886#ifdef FEAT_EVAL
887 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000888 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
890 /* Each '|' separated command is stored separately in lines_ga, to
891 * be able to jump to it. Don't use next_cmdline now. */
892 vim_free(cmdline_copy);
893 cmdline_copy = NULL;
894
895 /* Check if a function has returned or, unless it has an unclosed
896 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000899# ifdef FEAT_PROFILE
900 if (do_profiling)
901 func_line_end(real_cookie);
902# endif
903 if (func_has_ended(real_cookie))
904 {
905 retval = FAIL;
906 break;
907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909#ifdef FEAT_PROFILE
910 else if (do_profiling
911 && getline_equal(getline, cookie, getsourceline))
912 script_line_end();
913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
915 /* Check if a sourced file hit a ":finish" command. */
916 if (source_finished(getline, cookie))
917 {
918 retval = FAIL;
919 break;
920 }
921
922 /* If breakpoints have been added/deleted need to check for it. */
923 if (breakpoint != NULL && dbg_tick != NULL
924 && *dbg_tick != debug_tick)
925 {
926 *breakpoint = dbg_find_breakpoint(
927 getline_equal(getline, cookie, getsourceline),
928 fname, sourcing_lnum);
929 *dbg_tick = debug_tick;
930 }
931
932 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
933 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
934
935 /* Did we encounter a breakpoint? */
936 if (breakpoint != NULL && *breakpoint != 0
937 && *breakpoint <= sourcing_lnum)
938 {
939 dbg_breakpoint(fname, sourcing_lnum);
940 /* Find next breakpoint. */
941 *breakpoint = dbg_find_breakpoint(
942 getline_equal(getline, cookie, getsourceline),
943 fname, sourcing_lnum);
944 *dbg_tick = debug_tick;
945 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000946# ifdef FEAT_PROFILE
947 if (do_profiling)
948 {
949 if (getline_is_func)
950 func_line_start(real_cookie);
951 else if (getline_equal(getline, cookie, getsourceline))
952 script_line_start();
953 }
954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000957 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000959 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 * again. Pass a different "getline" function to do_one_cmd()
961 * below, so that it stores lines in or reads them from
962 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000963 * while/for loop. */
964 cmd_getline = get_loop_line;
965 cmd_cookie = (void *)&cmd_loop_cookie;
966 cmd_loop_cookie.lines_gap = &lines_ga;
967 cmd_loop_cookie.current_line = current_line;
968 cmd_loop_cookie.getline = getline;
969 cmd_loop_cookie.cookie = cookie;
970 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972 else
973 {
974 cmd_getline = getline;
975 cmd_cookie = cookie;
976 }
977#endif
978
979 /* 2. If no line given, get an allocated line with getline(). */
980 if (next_cmdline == NULL)
981 {
982 /*
983 * Need to set msg_didout for the first line after an ":if",
984 * otherwise the ":if" will be overwritten.
985 */
986 if (count == 1 && getline_equal(getline, cookie, getexline))
987 msg_didout = TRUE;
988 if (getline == NULL || (next_cmdline = getline(':', cookie,
989#ifdef FEAT_EVAL
990 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
991#else
992 0
993#endif
994 )) == NULL)
995 {
996 /* Don't call wait_return for aborted command line. The NULL
997 * returned for the end of a sourced file or executed function
998 * doesn't do this. */
999 if (KeyTyped && !(flags & DOCMD_REPEAT))
1000 need_wait_return = FALSE;
1001 retval = FAIL;
1002 break;
1003 }
1004 used_getline = TRUE;
1005
1006 /*
1007 * Keep the first typed line. Clear it when more lines are typed.
1008 */
1009 if (flags & DOCMD_KEEPLINE)
1010 {
1011 vim_free(repeat_cmdline);
1012 if (count == 0)
1013 repeat_cmdline = vim_strsave(next_cmdline);
1014 else
1015 repeat_cmdline = NULL;
1016 }
1017 }
1018
1019 /* 3. Make a copy of the command so we can mess with it. */
1020 else if (cmdline_copy == NULL)
1021 {
1022 next_cmdline = vim_strsave(next_cmdline);
1023 if (next_cmdline == NULL)
1024 {
1025 EMSG(_(e_outofmem));
1026 retval = FAIL;
1027 break;
1028 }
1029 }
1030 cmdline_copy = next_cmdline;
1031
1032#ifdef FEAT_EVAL
1033 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 * Save the current line when inside a ":while" or ":for", and when
1035 * the command looks like a ":while" or ":for", because we may need it
1036 * later. When there is a '|' and another command, it is stored
1037 * separately, because we need to be able to jump back to it from an
1038 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 if (current_line == lines_ga.ga_len
1041 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
1045 retval = FAIL;
1046 break;
1047 }
1048 }
1049 did_endif = FALSE;
1050#endif
1051
1052 if (count++ == 0)
1053 {
1054 /*
1055 * All output from the commands is put below each other, without
1056 * waiting for a return. Don't do this when executing commands
1057 * from a script or when being called recursive (e.g. for ":e
1058 * +command file").
1059 */
1060 if (!(flags & DOCMD_NOWAIT) && !recursive)
1061 {
1062 msg_didout_before_start = msg_didout;
1063 msg_didany = FALSE; /* no output yet */
1064 msg_start();
1065 msg_scroll = TRUE; /* put messages below each other */
1066 ++no_wait_return; /* dont wait for return until finished */
1067 ++RedrawingDisabled;
1068 did_inc = TRUE;
1069 }
1070 }
1071
1072 if (p_verbose >= 15 && sourcing_name != NULL)
1073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001075 verbose_enter_scroll();
1076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 smsg((char_u *)_("line %ld: %s"),
1078 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001079 if (msg_silent == 0)
1080 msg_puts((char_u *)"\n"); /* don't overwrite this */
1081
1082 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 --no_wait_return;
1084 }
1085
1086 /*
1087 * 2. Execute one '|' separated command.
1088 * do_one_cmd() will return NULL if there is no trailing '|'.
1089 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1090 */
1091 ++recursive;
1092 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1093#ifdef FEAT_EVAL
1094 &cstack,
1095#endif
1096 cmd_getline, cmd_cookie);
1097 --recursive;
1098
1099#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 if (cmd_cookie == (void *)&cmd_loop_cookie)
1101 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#endif
1105
1106 if (next_cmdline == NULL)
1107 {
1108 vim_free(cmdline_copy);
1109 cmdline_copy = NULL;
1110#ifdef FEAT_CMDHIST
1111 /*
1112 * If the command was typed, remember it for the ':' register.
1113 * Do this AFTER executing the command to make :@: work.
1114 */
1115 if (getline_equal(getline, cookie, getexline)
1116 && new_last_cmdline != NULL)
1117 {
1118 vim_free(last_cmdline);
1119 last_cmdline = new_last_cmdline;
1120 new_last_cmdline = NULL;
1121 }
1122#endif
1123 }
1124 else
1125 {
1126 /* need to copy the command after the '|' to cmdline_copy, for the
1127 * next do_one_cmd() */
1128 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1129 next_cmdline = cmdline_copy;
1130 }
1131
1132
1133#ifdef FEAT_EVAL
1134 /* reset did_emsg for a function that is not aborted by an error */
1135 if (did_emsg && !force_abort
1136 && getline_equal(getline, cookie, get_func_line)
1137 && !func_has_abort(real_cookie))
1138 did_emsg = FALSE;
1139
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
1142 ++current_line;
1143
1144 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001145 * An ":endwhile", ":endfor" and ":continue" is handled here.
1146 * If we were executing commands, jump back to the ":while" or
1147 * ":for".
1148 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001152 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001154 /* Jump back to the matching ":while" or ":for". Be careful
1155 * not to use a cs_line[] from an entry that isn't a ":while"
1156 * or ":for": It would make "current_line" invalid and can
1157 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (!did_emsg && !got_int && !did_throw
1159 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 && (cstack.cs_flags[cstack.cs_idx]
1161 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 && cstack.cs_line[cstack.cs_idx] >= 0
1163 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1164 {
1165 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 /* remember we jumped there */
1167 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 line_breakcheck(); /* check if CTRL-C typed */
1169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 /* Check for the next breakpoint at or after the ":while"
1171 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (breakpoint != NULL)
1173 {
1174 *breakpoint = dbg_find_breakpoint(
1175 getline_equal(getline, cookie, getsourceline),
1176 fname,
1177 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1178 *dbg_tick = debug_tick;
1179 }
1180 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001185 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1186 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 }
1188 }
1189
1190 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001193 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1197 }
1198 }
1199
1200 /*
1201 * When not inside any ":while" loop, clear remembered lines.
1202 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001203 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 {
1205 if (lines_ga.ga_len > 0)
1206 {
1207 sourcing_lnum =
1208 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1209 free_cmdlines(&lines_ga);
1210 }
1211 current_line = 0;
1212 }
1213
1214 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1216 * being restored at the ":endtry". Reset them here and set the
1217 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1218 * This includes the case where a missing ":endif", ":endwhile" or
1219 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 cstack.cs_lflags &= ~CSL_HAD_FINA;
1224 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1225 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 did_throw ? (void *)current_exception : NULL);
1227 did_emsg = got_int = did_throw = FALSE;
1228 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1229 }
1230
1231 /* Update global "trylevel" for recursive calls to do_cmdline() from
1232 * within this loop. */
1233 trylevel = initial_trylevel + cstack.cs_trylevel;
1234
1235 /*
1236 * If the outermost try conditional (accross function calls and sourced
1237 * files) is aborted because of an error, an interrupt, or an uncaught
1238 * exception, cancel everything. If it is left normally, reset
1239 * force_abort to get the non-EH compatible abortion behavior for
1240 * the rest of the script.
1241 */
1242 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1243 force_abort = FALSE;
1244
1245 /* Convert an interrupt to an exception if appropriate. */
1246 (void)do_intthrow(&cstack);
1247#endif /* FEAT_EVAL */
1248
1249 }
1250 /*
1251 * Continue executing command lines when:
1252 * - no CTRL-C typed, no aborting error, no exception thrown or try
1253 * conditionals need to be checked for executing finally clauses or
1254 * catching an interrupt exception
1255 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001256 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 * looping for ":source" command or function call.
1258 */
1259 while (!((got_int
1260#ifdef FEAT_EVAL
1261 || (did_emsg && force_abort) || did_throw
1262#endif
1263 )
1264#ifdef FEAT_EVAL
1265 && cstack.cs_trylevel == 0
1266#endif
1267 )
1268 && !(did_emsg && used_getline
1269 && (getline_equal(getline, cookie, getexmodeline)
1270 || getline_equal(getline, cookie, getexline)))
1271 && (next_cmdline != NULL
1272#ifdef FEAT_EVAL
1273 || cstack.cs_idx >= 0
1274#endif
1275 || (flags & DOCMD_REPEAT)));
1276
1277 vim_free(cmdline_copy);
1278#ifdef FEAT_EVAL
1279 free_cmdlines(&lines_ga);
1280 ga_clear(&lines_ga);
1281
1282 if (cstack.cs_idx >= 0)
1283 {
1284 /*
1285 * If a sourced file or executed function ran to its end, report the
1286 * unclosed conditional.
1287 */
1288 if (!got_int && !did_throw
1289 && ((getline_equal(getline, cookie, getsourceline)
1290 && !source_finished(getline, cookie))
1291 || (getline_equal(getline, cookie, get_func_line)
1292 && !func_has_ended(real_cookie))))
1293 {
1294 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1295 EMSG(_(e_endtry));
1296 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1297 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001298 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1299 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 else
1301 EMSG(_(e_endif));
1302 }
1303
1304 /*
1305 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1306 * ":endtry" in a sourced file or executed function. If the try
1307 * conditional is in its finally clause, ignore anything pending.
1308 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001309 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 */
1311 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001312 {
1313 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1314
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001315 if (idx >= 0)
1316 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001317 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1318 &cstack.cs_looplevel);
1319 }
1320 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 trylevel = initial_trylevel;
1322 }
1323
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001324 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1325 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 * exception, do this now after rewinding the cstack. */
1327 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1328 ? (char_u *)"endfunction" : (char_u *)NULL);
1329
1330 if (trylevel == 0)
1331 {
1332 /*
1333 * When an exception is being thrown out of the outermost try
1334 * conditional, discard the uncaught exception, disable the conversion
1335 * of interrupts or errors to exceptions, and ensure that no more
1336 * commands are executed.
1337 */
1338 if (did_throw)
1339 {
1340 void *p = NULL;
1341 char_u *saved_sourcing_name;
1342 int saved_sourcing_lnum;
1343 struct msglist *messages = NULL, *next;
1344
1345 /*
1346 * If the uncaught exception is a user exception, report it as an
1347 * error. If it is an error exception, display the saved error
1348 * message now. For an interrupt exception, do nothing; the
1349 * interrupt message is given elsewhere.
1350 */
1351 switch (current_exception->type)
1352 {
1353 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001354 vim_snprintf((char *)IObuff, IOSIZE,
1355 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 current_exception->value);
1357 p = vim_strsave(IObuff);
1358 break;
1359 case ET_ERROR:
1360 messages = current_exception->messages;
1361 current_exception->messages = NULL;
1362 break;
1363 case ET_INTERRUPT:
1364 break;
1365 default:
1366 p = vim_strsave((char_u *)_(e_internal));
1367 }
1368
1369 saved_sourcing_name = sourcing_name;
1370 saved_sourcing_lnum = sourcing_lnum;
1371 sourcing_name = current_exception->throw_name;
1372 sourcing_lnum = current_exception->throw_lnum;
1373 current_exception->throw_name = NULL;
1374
1375 discard_current_exception(); /* uses IObuff if 'verbose' */
1376 suppress_errthrow = TRUE;
1377 force_abort = TRUE;
1378
1379 if (messages != NULL)
1380 {
1381 do
1382 {
1383 next = messages->next;
1384 emsg(messages->msg);
1385 vim_free(messages->msg);
1386 vim_free(messages);
1387 messages = next;
1388 }
1389 while (messages != NULL);
1390 }
1391 else if (p != NULL)
1392 {
1393 emsg(p);
1394 vim_free(p);
1395 }
1396 vim_free(sourcing_name);
1397 sourcing_name = saved_sourcing_name;
1398 sourcing_lnum = saved_sourcing_lnum;
1399 }
1400
1401 /*
1402 * On an interrupt or an aborting error not converted to an exception,
1403 * disable the conversion of errors to exceptions. (Interrupts are not
1404 * converted any more, here.) This enables also the interrupt message
1405 * when force_abort is set and did_emsg unset in case of an interrupt
1406 * from a finally clause after an error.
1407 */
1408 else if (got_int || (did_emsg && force_abort))
1409 suppress_errthrow = TRUE;
1410 }
1411
1412 /*
1413 * The current cstack will be freed when do_cmdline() returns. An uncaught
1414 * exception will have to be rethrown in the previous cstack. If a function
1415 * has just returned or a script file was just finished and the previous
1416 * cstack belongs to the same function or, respectively, script file, it
1417 * will have to be checked for finally clauses to be executed due to the
1418 * ":return" or ":finish". This is done in do_one_cmd().
1419 */
1420 if (did_throw)
1421 need_rethrow = TRUE;
1422 if ((getline_equal(getline, cookie, getsourceline)
1423 && ex_nesting_level > source_level(real_cookie))
1424 || (getline_equal(getline, cookie, get_func_line)
1425 && ex_nesting_level > func_level(real_cookie) + 1))
1426 {
1427 if (!did_throw)
1428 check_cstack = TRUE;
1429 }
1430 else
1431 {
1432 /* When leaving a function, reduce nesting level. */
1433 if (getline_equal(getline, cookie, get_func_line))
1434 --ex_nesting_level;
1435 /*
1436 * Go to debug mode when returning from a function in which we are
1437 * single-stepping.
1438 */
1439 if ((getline_equal(getline, cookie, getsourceline)
1440 || getline_equal(getline, cookie, get_func_line))
1441 && ex_nesting_level + 1 <= debug_break_level)
1442 do_debug(getline_equal(getline, cookie, getsourceline)
1443 ? (char_u *)_("End of sourced file")
1444 : (char_u *)_("End of function"));
1445 }
1446
1447 /*
1448 * Restore the exception environment (done after returning from the
1449 * debugger).
1450 */
1451 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001452 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453
1454 msg_list = saved_msg_list;
1455#endif /* FEAT_EVAL */
1456
1457 /*
1458 * If there was too much output to fit on the command line, ask the user to
1459 * hit return before redrawing the screen. With the ":global" command we do
1460 * this only once after the command is finished.
1461 */
1462 if (did_inc)
1463 {
1464 --RedrawingDisabled;
1465 --no_wait_return;
1466 msg_scroll = FALSE;
1467
1468 /*
1469 * When just finished an ":if"-":else" which was typed, no need to
1470 * wait for hit-return. Also for an error situation.
1471 */
1472 if (retval == FAIL
1473#ifdef FEAT_EVAL
1474 || (did_endif && KeyTyped && !did_emsg)
1475#endif
1476 )
1477 {
1478 need_wait_return = FALSE;
1479 msg_didany = FALSE; /* don't wait when restarting edit */
1480 }
1481 else if (need_wait_return)
1482 {
1483 /*
1484 * The msg_start() above clears msg_didout. The wait_return we do
1485 * here should not overwrite the command that may be shown before
1486 * doing that.
1487 */
1488 msg_didout |= msg_didout_before_start;
1489 wait_return(FALSE);
1490 }
1491 }
1492
1493#ifndef FEAT_EVAL
1494 /*
1495 * Reset if_level, in case a sourced script file contains more ":if" than
1496 * ":endif" (could be ":if x | foo | endif").
1497 */
1498 if_level = 0;
1499#endif
1500
1501 --call_depth;
1502 return retval;
1503}
1504
1505#ifdef FEAT_EVAL
1506/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001507 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 */
1509 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 int c;
1512 void *cookie;
1513 int indent;
1514{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001515 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 wcmd_T *wp;
1517 char_u *line;
1518
1519 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1520 {
1521 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001522 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (cp->getline == NULL)
1526 line = getcmdline(c, 0L, indent);
1527 else
1528 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001529 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 ++cp->current_line;
1531
1532 return line;
1533 }
1534
1535 KeyTyped = FALSE;
1536 ++cp->current_line;
1537 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1538 sourcing_lnum = wp->lnum;
1539 return vim_strsave(wp->line);
1540}
1541
1542/*
1543 * Store a line in "gap" so that a ":while" loop can execute it again.
1544 */
1545 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 garray_T *gap;
1548 char_u *line;
1549{
1550 if (ga_grow(gap, 1) == FAIL)
1551 return FAIL;
1552 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1553 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1554 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 return OK;
1556}
1557
1558/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
1561 static void
1562free_cmdlines(gap)
1563 garray_T *gap;
1564{
1565 while (gap->ga_len > 0)
1566 {
1567 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1568 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
1570}
1571#endif
1572
1573/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001574 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 * "func". * Otherwise return TRUE when "getline" equals "func".
1576 */
1577/*ARGSUSED*/
1578 int
1579getline_equal(getline, cookie, func)
1580 char_u *(*getline) __ARGS((int, void *, int));
1581 void *cookie; /* argument for getline() */
1582 char_u *(*func) __ARGS((int, void *, int));
1583{
1584#ifdef FEAT_EVAL
1585 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001586 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001588 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 * function that's orignally used to obtain the lines. This may be nested
1590 * several levels. */
1591 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001592 cp = (struct loop_cookie *)cookie;
1593 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 gp = cp->getline;
1596 cp = cp->cookie;
1597 }
1598 return gp == func;
1599#else
1600 return getline == func;
1601#endif
1602}
1603
1604#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1605/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001606 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 * getline function. Otherwise return "cookie".
1608 */
1609/*ARGSUSED*/
1610 void *
1611getline_cookie(getline, cookie)
1612 char_u *(*getline) __ARGS((int, void *, int));
1613 void *cookie; /* argument for getline() */
1614{
1615# ifdef FEAT_EVAL
1616 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001617 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001619 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 * cookie that's orignally used to obtain the lines. This may be nested
1621 * several levels. */
1622 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001623 cp = (struct loop_cookie *)cookie;
1624 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
1626 gp = cp->getline;
1627 cp = cp->cookie;
1628 }
1629 return cp;
1630# else
1631 return cookie;
1632# endif
1633}
1634#endif
1635
1636/*
1637 * Execute one Ex command.
1638 *
1639 * If 'sourcing' is TRUE, the command will be included in the error message.
1640 *
1641 * 1. skip comment lines and leading space
1642 * 2. handle command modifiers
1643 * 3. parse range
1644 * 4. parse command
1645 * 5. parse arguments
1646 * 6. switch on command name
1647 *
1648 * Note: "getline" can be NULL.
1649 *
1650 * This function may be called recursively!
1651 */
1652#if (_MSC_VER == 1200)
1653/*
Bram Moolenaared203462004-06-16 11:19:22 +00001654 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001656 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657#endif
1658 static char_u *
1659do_one_cmd(cmdlinep, sourcing,
1660#ifdef FEAT_EVAL
1661 cstack,
1662#endif
1663 getline, cookie)
1664 char_u **cmdlinep;
1665 int sourcing;
1666#ifdef FEAT_EVAL
1667 struct condstack *cstack;
1668#endif
1669 char_u *(*getline) __ARGS((int, void *, int));
1670 void *cookie; /* argument for getline() */
1671{
1672 char_u *p;
1673 linenr_T lnum;
1674 long n;
1675 char_u *errormsg = NULL; /* error message */
1676 exarg_T ea; /* Ex command arguments */
1677 long verbose_save = -1;
1678 int save_msg_scroll = 0;
1679 int did_silent = 0;
1680 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001681#ifdef HAVE_SANDBOX
1682 int did_sandbox = FALSE;
1683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 cmdmod_T save_cmdmod;
1685 int ni; /* set when Not Implemented */
1686
1687 vim_memset(&ea, 0, sizeof(ea));
1688 ea.line1 = 1;
1689 ea.line2 = 1;
1690#ifdef FEAT_EVAL
1691 ++ex_nesting_level;
1692#endif
1693
1694 /* when not editing the last file :q has to be typed twice */
1695 if (quitmore
1696#ifdef FEAT_EVAL
1697 /* avoid that a function call in 'statusline' does this */
1698 && !getline_equal(getline, cookie, get_func_line)
1699#endif
1700 )
1701 --quitmore;
1702
1703 /*
1704 * Reset browse, confirm, etc.. They are restored when returning, for
1705 * recursive calls.
1706 */
1707 save_cmdmod = cmdmod;
1708 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1709
1710 /*
1711 * Repeat until no more command modifiers are found.
1712 */
1713 ea.cmd = *cmdlinep;
1714 for (;;)
1715 {
1716/*
1717 * 1. skip comment lines and leading white space and colons
1718 */
1719 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1720 ++ea.cmd;
1721
1722 /* in ex mode, an empty line works like :+ */
1723 if (*ea.cmd == NUL && exmode_active
1724 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001725 || getline_equal(getline, cookie, getexline))
1726 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 {
1728 ea.cmd = (char_u *)"+";
1729 ex_pressedreturn = TRUE;
1730 }
1731
1732 /* ignore comment and empty lines */
1733 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001734 {
1735 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
1739/*
1740 * 2. handle command modifiers.
1741 */
1742 p = ea.cmd;
1743 if (VIM_ISDIGIT(*ea.cmd))
1744 p = skipwhite(skipdigits(ea.cmd));
1745 switch (*p)
1746 {
1747 /* When adding an entry, also modify cmd_exists(). */
1748 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1749 break;
1750#ifdef FEAT_WINDOWS
1751 cmdmod.split |= WSP_ABOVE;
1752#endif
1753 continue;
1754
1755 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1756 {
1757#ifdef FEAT_WINDOWS
1758 cmdmod.split |= WSP_BELOW;
1759#endif
1760 continue;
1761 }
1762 if (checkforcmd(&ea.cmd, "browse", 3))
1763 {
1764#ifdef FEAT_BROWSE
1765 cmdmod.browse = TRUE;
1766#endif
1767 continue;
1768 }
1769 if (!checkforcmd(&ea.cmd, "botright", 2))
1770 break;
1771#ifdef FEAT_WINDOWS
1772 cmdmod.split |= WSP_BOT;
1773#endif
1774 continue;
1775
1776 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1777 break;
1778#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1779 cmdmod.confirm = TRUE;
1780#endif
1781 continue;
1782
1783 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1784 {
1785 cmdmod.keepmarks = TRUE;
1786 continue;
1787 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001788 if (checkforcmd(&ea.cmd, "keepalt", 5))
1789 {
1790 cmdmod.keepalt = TRUE;
1791 continue;
1792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1794 break;
1795 cmdmod.keepjumps = TRUE;
1796 continue;
1797
1798 /* ":hide" and ":hide | cmd" are not modifiers */
1799 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1800 || *p == NUL || ends_excmd(*p))
1801 break;
1802 ea.cmd = p;
1803 cmdmod.hide = TRUE;
1804 continue;
1805
1806 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1807 {
1808 cmdmod.lockmarks = TRUE;
1809 continue;
1810 }
1811
1812 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1813 break;
1814#ifdef FEAT_WINDOWS
1815 cmdmod.split |= WSP_ABOVE;
1816#endif
1817 continue;
1818
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001819 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1820 break;
1821#ifdef FEAT_AUTOCMD
1822 if (cmdmod.save_ei == NULL)
1823 {
1824 /* Set 'eventignore' to "all". Don't free the
1825 * existing option value, we restore it later. */
1826 cmdmod.save_ei = vim_strsave(p_ei);
1827 set_string_option_direct((char_u *)"ei", -1,
1828 (char_u *)"all", OPT_FREE);
1829 }
1830#endif
1831 continue;
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1834 break;
1835#ifdef FEAT_WINDOWS
1836 cmdmod.split |= WSP_BELOW;
1837#endif
1838 continue;
1839
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001840 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1841 {
1842#ifdef HAVE_SANDBOX
1843 if (!did_sandbox)
1844 ++sandbox;
1845 did_sandbox = TRUE;
1846#endif
1847 continue;
1848 }
1849 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 break;
1851 ++did_silent;
1852 ++msg_silent;
1853 save_msg_scroll = msg_scroll;
1854 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1855 {
1856 /* ":silent!", but not "silent !cmd" */
1857 ea.cmd = skipwhite(ea.cmd + 1);
1858 ++emsg_silent;
1859 ++did_esilent;
1860 }
1861 continue;
1862
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001863 case 't': if (checkforcmd(&p, "tab", 3))
1864 {
1865#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001866 if (vim_isdigit(*ea.cmd))
1867 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1868 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001869 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001870 ea.cmd = p;
1871#endif
1872 continue;
1873 }
1874 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 break;
1876#ifdef FEAT_WINDOWS
1877 cmdmod.split |= WSP_TOP;
1878#endif
1879 continue;
1880
1881 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1882 {
1883#ifdef FEAT_VERTSPLIT
1884 cmdmod.split |= WSP_VERT;
1885#endif
1886 continue;
1887 }
1888 if (!checkforcmd(&p, "verbose", 4))
1889 break;
1890 if (verbose_save < 0)
1891 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001892 if (vim_isdigit(*ea.cmd))
1893 p_verbose = atoi((char *)ea.cmd);
1894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 p_verbose = 1;
1896 ea.cmd = p;
1897 continue;
1898 }
1899 break;
1900 }
1901
1902#ifdef FEAT_EVAL
1903 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1904 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1905#else
1906 ea.skip = (if_level > 0);
1907#endif
1908
1909#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001910# ifdef FEAT_PROFILE
1911 /* Count this line for profiling if ea.skip is FALSE. */
1912 if (do_profiling && !ea.skip)
1913 {
1914 if (getline_equal(getline, cookie, get_func_line))
1915 func_line_exec(getline_cookie(getline, cookie));
1916 else if (getline_equal(getline, cookie, getsourceline))
1917 script_line_exec();
1918 }
1919#endif
1920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 /* May go to debug mode. If this happens and the ">quit" debug command is
1922 * used, throw an interrupt exception and skip the next command. */
1923 dbg_check_breakpoint(&ea);
1924 if (!ea.skip && got_int)
1925 {
1926 ea.skip = TRUE;
1927 (void)do_intthrow(cstack);
1928 }
1929#endif
1930
1931/*
1932 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1933 *
1934 * where 'addr' is:
1935 *
1936 * % (entire file)
1937 * $ [+-NUM]
1938 * 'x [+-NUM] (where x denotes a currently defined mark)
1939 * . [+-NUM]
1940 * [+-NUM]..
1941 * NUM
1942 *
1943 * The ea.cmd pointer is updated to point to the first character following the
1944 * range spec. If an initial address is found, but no second, the upper bound
1945 * is equal to the lower.
1946 */
1947
1948 /* repeat for all ',' or ';' separated addresses */
1949 for (;;)
1950 {
1951 ea.line1 = ea.line2;
1952 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1953 ea.cmd = skipwhite(ea.cmd);
1954 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1955 if (ea.cmd == NULL) /* error detected */
1956 goto doend;
1957 if (lnum == MAXLNUM)
1958 {
1959 if (*ea.cmd == '%') /* '%' - all lines */
1960 {
1961 ++ea.cmd;
1962 ea.line1 = 1;
1963 ea.line2 = curbuf->b_ml.ml_line_count;
1964 ++ea.addr_count;
1965 }
1966 /* '*' - visual area */
1967 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1968 {
1969 pos_T *fp;
1970
1971 ++ea.cmd;
1972 if (!ea.skip)
1973 {
1974 fp = getmark('<', FALSE);
1975 if (check_mark(fp) == FAIL)
1976 goto doend;
1977 ea.line1 = fp->lnum;
1978 fp = getmark('>', FALSE);
1979 if (check_mark(fp) == FAIL)
1980 goto doend;
1981 ea.line2 = fp->lnum;
1982 ++ea.addr_count;
1983 }
1984 }
1985 }
1986 else
1987 ea.line2 = lnum;
1988 ea.addr_count++;
1989
1990 if (*ea.cmd == ';')
1991 {
1992 if (!ea.skip)
1993 curwin->w_cursor.lnum = ea.line2;
1994 }
1995 else if (*ea.cmd != ',')
1996 break;
1997 ++ea.cmd;
1998 }
1999
2000 /* One address given: set start and end lines */
2001 if (ea.addr_count == 1)
2002 {
2003 ea.line1 = ea.line2;
2004 /* ... but only implicit: really no address given */
2005 if (lnum == MAXLNUM)
2006 ea.addr_count = 0;
2007 }
2008
2009 /* Don't leave the cursor on an illegal line (caused by ';') */
2010 check_cursor_lnum();
2011
2012/*
2013 * 4. parse command
2014 */
2015
2016 /*
2017 * Skip ':' and any white space
2018 */
2019 ea.cmd = skipwhite(ea.cmd);
2020 while (*ea.cmd == ':')
2021 ea.cmd = skipwhite(ea.cmd + 1);
2022
2023 /*
2024 * If we got a line, but no command, then go to the line.
2025 * If we find a '|' or '\n' we set ea.nextcmd.
2026 */
2027 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2028 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2029 {
2030 /*
2031 * strange vi behaviour:
2032 * ":3" jumps to line 3
2033 * ":3|..." prints line 3
2034 * ":|" prints current line
2035 */
2036 if (ea.skip) /* skip this if inside :if */
2037 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002038 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
2040 ea.cmdidx = CMD_print;
2041 ea.argt = RANGE+COUNT+TRLBAR;
2042 if ((errormsg = invalid_range(&ea)) == NULL)
2043 {
2044 correct_range(&ea);
2045 ex_print(&ea);
2046 }
2047 }
2048 else if (ea.addr_count != 0)
2049 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002050 if (ea.line2 > curbuf->b_ml.ml_line_count)
2051 {
2052 /* With '-' in 'cpoptions' a line number past the file is an
2053 * error, otherwise put it at the end of the file. */
2054 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2055 ea.line2 = -1;
2056 else
2057 ea.line2 = curbuf->b_ml.ml_line_count;
2058 }
2059
2060 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002061 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 else
2063 {
2064 if (ea.line2 == 0)
2065 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 else
2067 curwin->w_cursor.lnum = ea.line2;
2068 beginline(BL_SOL | BL_FIX);
2069 }
2070 }
2071 goto doend;
2072 }
2073
2074 /* Find the command and let "p" point to after it. */
2075 p = find_command(&ea, NULL);
2076
2077#ifdef FEAT_USR_CMDS
2078 if (p == NULL)
2079 {
2080 if (!ea.skip)
2081 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2082 goto doend;
2083 }
2084 /* Check for wrong commands. */
2085 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2086 {
2087 errormsg = uc_fun_cmd();
2088 goto doend;
2089 }
2090#endif
2091 if (ea.cmdidx == CMD_SIZE)
2092 {
2093 if (!ea.skip)
2094 {
2095 STRCPY(IObuff, _("E492: Not an editor command"));
2096 if (!sourcing)
2097 {
2098 STRCAT(IObuff, ": ");
2099 STRNCAT(IObuff, *cmdlinep, 40);
2100 }
2101 errormsg = IObuff;
2102 }
2103 goto doend;
2104 }
2105
2106 ni = (
2107#ifdef FEAT_USR_CMDS
2108 !USER_CMDIDX(ea.cmdidx) &&
2109#endif
2110 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2111
2112#ifndef FEAT_EVAL
2113 /*
2114 * When the expression evaluation is disabled, recognize the ":if" and
2115 * ":endif" commands and ignore everything in between it.
2116 */
2117 if (ea.cmdidx == CMD_if)
2118 ++if_level;
2119 if (if_level)
2120 {
2121 if (ea.cmdidx == CMD_endif)
2122 --if_level;
2123 goto doend;
2124 }
2125
2126#endif
2127
2128 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2129 {
2130 ++p;
2131 ea.forceit = TRUE;
2132 }
2133 else
2134 ea.forceit = FALSE;
2135
2136/*
2137 * 5. parse arguments
2138 */
2139#ifdef FEAT_USR_CMDS
2140 if (!USER_CMDIDX(ea.cmdidx))
2141#endif
2142 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2143
2144 if (!ea.skip)
2145 {
2146#ifdef HAVE_SANDBOX
2147 if (sandbox != 0 && !(ea.argt & SBOXOK))
2148 {
2149 /* Command not allowed in sandbox. */
2150 errormsg = (char_u *)_(e_sandbox);
2151 goto doend;
2152 }
2153#endif
2154 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2155 {
2156 /* Command not allowed in non-'modifiable' buffer */
2157 errormsg = (char_u *)_(e_modifiable);
2158 goto doend;
2159 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002160
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002161 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162# ifdef FEAT_USR_CMDS
2163 && !USER_CMDIDX(ea.cmdidx)
2164# endif
2165 )
2166 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002167 /* Command not allowed when editing the command line. */
2168#ifdef FEAT_CMDWIN
2169 if (cmdwin_type != 0)
2170 errormsg = (char_u *)_(e_cmdwin);
2171 else
2172#endif
2173 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 goto doend;
2175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2178 {
2179 /* no range allowed */
2180 errormsg = (char_u *)_(e_norange);
2181 goto doend;
2182 }
2183 }
2184
2185 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2186 {
2187 errormsg = (char_u *)_(e_nobang);
2188 goto doend;
2189 }
2190
2191 /*
2192 * Don't complain about the range if it is not used
2193 * (could happen if line_count is accidentally set to 0).
2194 */
2195 if (!ea.skip && !ni)
2196 {
2197 /*
2198 * If the range is backwards, ask for confirmation and, if given, swap
2199 * ea.line1 & ea.line2 so it's forwards again.
2200 * When global command is busy, don't ask, will fail below.
2201 */
2202 if (!global_busy && ea.line1 > ea.line2)
2203 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002204 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002206 if (sourcing || exmode_active)
2207 {
2208 errormsg = (char_u *)_("E493: Backwards range given");
2209 goto doend;
2210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (ask_yesno((char_u *)
2212 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002213 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 }
2215 lnum = ea.line1;
2216 ea.line1 = ea.line2;
2217 ea.line2 = lnum;
2218 }
2219 if ((errormsg = invalid_range(&ea)) != NULL)
2220 goto doend;
2221 }
2222
2223 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2224 ea.line2 = 1;
2225
2226 correct_range(&ea);
2227
2228#ifdef FEAT_FOLDING
2229 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2230 {
2231 /* Put the first line at the start of a closed fold, put the last line
2232 * at the end of a closed fold. */
2233 (void)hasFolding(ea.line1, &ea.line1, NULL);
2234 (void)hasFolding(ea.line2, NULL, &ea.line2);
2235 }
2236#endif
2237
2238#ifdef FEAT_QUICKFIX
2239 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002240 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 * option here, so things like % get expanded.
2242 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002243 p = replace_makeprg(&ea, p, cmdlinep);
2244 if (p == NULL)
2245 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246#endif
2247
2248 /*
2249 * Skip to start of argument.
2250 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2251 */
2252 if (ea.cmdidx == CMD_bang)
2253 ea.arg = p;
2254 else
2255 ea.arg = skipwhite(p);
2256
2257 /*
2258 * Check for "++opt=val" argument.
2259 * Must be first, allow ":w ++enc=utf8 !cmd"
2260 */
2261 if (ea.argt & ARGOPT)
2262 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2263 if (getargopt(&ea) == FAIL && !ni)
2264 {
2265 errormsg = (char_u *)_(e_invarg);
2266 goto doend;
2267 }
2268
2269 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2270 {
2271 if (*ea.arg == '>') /* append */
2272 {
2273 if (*++ea.arg != '>') /* typed wrong */
2274 {
2275 errormsg = (char_u *)_("E494: Use w or w>>");
2276 goto doend;
2277 }
2278 ea.arg = skipwhite(ea.arg + 1);
2279 ea.append = TRUE;
2280 }
2281 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2282 {
2283 ++ea.arg;
2284 ea.usefilter = TRUE;
2285 }
2286 }
2287
2288 if (ea.cmdidx == CMD_read)
2289 {
2290 if (ea.forceit)
2291 {
2292 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2293 ea.forceit = FALSE;
2294 }
2295 else if (*ea.arg == '!') /* :r !filter */
2296 {
2297 ++ea.arg;
2298 ea.usefilter = TRUE;
2299 }
2300 }
2301
2302 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2303 {
2304 ea.amount = 1;
2305 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2306 {
2307 ++ea.arg;
2308 ++ea.amount;
2309 }
2310 ea.arg = skipwhite(ea.arg);
2311 }
2312
2313 /*
2314 * Check for "+command" argument, before checking for next command.
2315 * Don't do this for ":read !cmd" and ":write !cmd".
2316 */
2317 if ((ea.argt & EDITCMD) && !ea.usefilter)
2318 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2319
2320 /*
2321 * Check for '|' to separate commands and '"' to start comments.
2322 * Don't do this for ":read !cmd" and ":write !cmd".
2323 */
2324 if ((ea.argt & TRLBAR) && !ea.usefilter)
2325 separate_nextcmd(&ea);
2326
2327 /*
2328 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002329 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2330 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002332 else if (ea.cmdidx == CMD_bang
2333 || ea.cmdidx == CMD_global
2334 || ea.cmdidx == CMD_vglobal
2335 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 for (p = ea.arg; *p; ++p)
2338 {
2339 /* Remove one backslash before a newline, so that it's possible to
2340 * pass a newline to the shell and also a newline that is preceded
2341 * with a backslash. This makes it impossible to end a shell
2342 * command in a backslash, but that doesn't appear useful.
2343 * Halving the number of backslashes is incompatible with previous
2344 * versions. */
2345 if (*p == '\\' && p[1] == '\n')
2346 mch_memmove(p, p + 1, STRLEN(p));
2347 else if (*p == '\n')
2348 {
2349 ea.nextcmd = p + 1;
2350 *p = NUL;
2351 break;
2352 }
2353 }
2354 }
2355
2356 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2357 {
2358 ea.line1 = 1;
2359 ea.line2 = curbuf->b_ml.ml_line_count;
2360 }
2361
2362 /* accept numbered register only when no count allowed (:put) */
2363 if ( (ea.argt & REGSTR)
2364 && *ea.arg != NUL
2365#ifdef FEAT_USR_CMDS
2366 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2367 && USER_CMDIDX(ea.cmdidx)))
2368 /* Do not allow register = for user commands */
2369 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2370#else
2371 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2372#endif
2373 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2374 {
2375 ea.regname = *ea.arg++;
2376#ifdef FEAT_EVAL
2377 /* for '=' register: accept the rest of the line as an expression */
2378 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2379 {
2380 set_expr_line(vim_strsave(ea.arg));
2381 ea.arg += STRLEN(ea.arg);
2382 }
2383#endif
2384 ea.arg = skipwhite(ea.arg);
2385 }
2386
2387 /*
2388 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2389 * count, it's a buffer name.
2390 */
2391 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2392 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2393 || vim_iswhite(*p)))
2394 {
2395 n = getdigits(&ea.arg);
2396 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002397 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
2399 errormsg = (char_u *)_(e_zerocount);
2400 goto doend;
2401 }
2402 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2403 {
2404 ea.line2 = n;
2405 if (ea.addr_count == 0)
2406 ea.addr_count = 1;
2407 }
2408 else
2409 {
2410 ea.line1 = ea.line2;
2411 ea.line2 += n - 1;
2412 ++ea.addr_count;
2413 /*
2414 * Be vi compatible: no error message for out of range.
2415 */
2416 if (ea.line2 > curbuf->b_ml.ml_line_count)
2417 ea.line2 = curbuf->b_ml.ml_line_count;
2418 }
2419 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002420
2421 /*
2422 * Check for flags: 'l', 'p' and '#'.
2423 */
2424 if (ea.argt & EXFLAGS)
2425 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002427 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
2428 && vim_strchr((char_u *)"|\"", *ea.arg) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 errormsg = (char_u *)_(e_trailing);
2431 goto doend;
2432 }
2433
2434 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2435 {
2436 errormsg = (char_u *)_(e_argreq);
2437 goto doend;
2438 }
2439
2440#ifdef FEAT_EVAL
2441 /*
2442 * Skip the command when it's not going to be executed.
2443 * The commands like :if, :endif, etc. always need to be executed.
2444 * Also make an exception for commands that handle a trailing command
2445 * themselves.
2446 */
2447 if (ea.skip)
2448 {
2449 switch (ea.cmdidx)
2450 {
2451 /* commands that need evaluation */
2452 case CMD_while:
2453 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002454 case CMD_for:
2455 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 case CMD_if:
2457 case CMD_elseif:
2458 case CMD_else:
2459 case CMD_endif:
2460 case CMD_try:
2461 case CMD_catch:
2462 case CMD_finally:
2463 case CMD_endtry:
2464 case CMD_function:
2465 break;
2466
2467 /* Commands that handle '|' themselves. Check: A command should
2468 * either have the TRLBAR flag, appear in this list or appear in
2469 * the list at ":help :bar". */
2470 case CMD_aboveleft:
2471 case CMD_and:
2472 case CMD_belowright:
2473 case CMD_botright:
2474 case CMD_browse:
2475 case CMD_call:
2476 case CMD_confirm:
2477 case CMD_delfunction:
2478 case CMD_djump:
2479 case CMD_dlist:
2480 case CMD_dsearch:
2481 case CMD_dsplit:
2482 case CMD_echo:
2483 case CMD_echoerr:
2484 case CMD_echomsg:
2485 case CMD_echon:
2486 case CMD_execute:
2487 case CMD_help:
2488 case CMD_hide:
2489 case CMD_ijump:
2490 case CMD_ilist:
2491 case CMD_isearch:
2492 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002493 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_keepjumps:
2495 case CMD_keepmarks:
2496 case CMD_leftabove:
2497 case CMD_let:
2498 case CMD_lockmarks:
2499 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002500 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 case CMD_perl:
2502 case CMD_psearch:
2503 case CMD_python:
2504 case CMD_return:
2505 case CMD_rightbelow:
2506 case CMD_ruby:
2507 case CMD_silent:
2508 case CMD_smagic:
2509 case CMD_snomagic:
2510 case CMD_substitute:
2511 case CMD_syntax:
2512 case CMD_tcl:
2513 case CMD_throw:
2514 case CMD_tilde:
2515 case CMD_topleft:
2516 case CMD_unlet:
2517 case CMD_verbose:
2518 case CMD_vertical:
2519 break;
2520
2521 default: goto doend;
2522 }
2523 }
2524#endif
2525
2526 if (ea.argt & XFILE)
2527 {
2528 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2529 goto doend;
2530 }
2531
2532#ifdef FEAT_LISTCMDS
2533 /*
2534 * Accept buffer name. Cannot be used at the same time with a buffer
2535 * number. Don't do this for a user command.
2536 */
2537 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2538# ifdef FEAT_USR_CMDS
2539 && !USER_CMDIDX(ea.cmdidx)
2540# endif
2541 )
2542 {
2543 /*
2544 * :bdelete, :bwipeout and :bunload take several arguments, separated
2545 * by spaces: find next space (skipping over escaped characters).
2546 * The others take one argument: ignore trailing spaces.
2547 */
2548 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2549 || ea.cmdidx == CMD_bunload)
2550 p = skiptowhite_esc(ea.arg);
2551 else
2552 {
2553 p = ea.arg + STRLEN(ea.arg);
2554 while (p > ea.arg && vim_iswhite(p[-1]))
2555 --p;
2556 }
2557 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2558 if (ea.line2 < 0) /* failed */
2559 goto doend;
2560 ea.addr_count = 1;
2561 ea.arg = skipwhite(p);
2562 }
2563#endif
2564
2565/*
2566 * 6. switch on command name
2567 *
2568 * The "ea" structure holds the arguments that can be used.
2569 */
2570 ea.cmdlinep = cmdlinep;
2571 ea.getline = getline;
2572 ea.cookie = cookie;
2573#ifdef FEAT_EVAL
2574 ea.cstack = cstack;
2575#endif
2576
2577#ifdef FEAT_USR_CMDS
2578 if (USER_CMDIDX(ea.cmdidx))
2579 {
2580 /*
2581 * Execute a user-defined command.
2582 */
2583 do_ucmd(&ea);
2584 }
2585 else
2586#endif
2587 {
2588 /*
2589 * Call the function to execute the command.
2590 */
2591 ea.errmsg = NULL;
2592 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2593 if (ea.errmsg != NULL)
2594 errormsg = (char_u *)_(ea.errmsg);
2595 }
2596
2597#ifdef FEAT_EVAL
2598 /*
2599 * If the command just executed called do_cmdline(), any throw or ":return"
2600 * or ":finish" encountered there must also check the cstack of the still
2601 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2602 * exception, or reanimate a returned function or finished script file and
2603 * return or finish it again.
2604 */
2605 if (need_rethrow)
2606 do_throw(cstack);
2607 else if (check_cstack)
2608 {
2609 if (source_finished(getline, cookie))
2610 do_finish(&ea, TRUE);
2611 else if (getline_equal(getline, cookie, get_func_line)
2612 && current_func_returned())
2613 do_return(&ea, TRUE, FALSE, NULL);
2614 }
2615 need_rethrow = check_cstack = FALSE;
2616#endif
2617
2618doend:
2619 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2620 curwin->w_cursor.lnum = 1;
2621
2622 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2623 {
2624 if (sourcing)
2625 {
2626 if (errormsg != IObuff)
2627 {
2628 STRCPY(IObuff, errormsg);
2629 errormsg = IObuff;
2630 }
2631 STRCAT(errormsg, ": ");
2632 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2633 }
2634 emsg(errormsg);
2635 }
2636#ifdef FEAT_EVAL
2637 do_errthrow(cstack,
2638 (ea.cmdidx != CMD_SIZE
2639# ifdef FEAT_USR_CMDS
2640 && !USER_CMDIDX(ea.cmdidx)
2641# endif
2642 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2643#endif
2644
2645 if (verbose_save >= 0)
2646 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002647#ifdef FEAT_AUTOCMD
2648 if (cmdmod.save_ei != NULL)
2649 {
2650 /* Restore 'eventignore' to the value before ":noautocmd". */
2651 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei, OPT_FREE);
2652 free_string_option(cmdmod.save_ei);
2653 }
2654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 cmdmod = save_cmdmod;
2657
2658 if (did_silent > 0)
2659 {
2660 /* messages could be enabled for a serious error, need to check if the
2661 * counters don't become negative */
2662 msg_silent -= did_silent;
2663 if (msg_silent < 0)
2664 msg_silent = 0;
2665 emsg_silent -= did_esilent;
2666 if (emsg_silent < 0)
2667 emsg_silent = 0;
2668 /* Restore msg_scroll, it's set by file I/O commands, even when no
2669 * message is actually displayed. */
2670 msg_scroll = save_msg_scroll;
2671 }
2672
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002673#ifdef HAVE_SANDBOX
2674 if (did_sandbox)
2675 --sandbox;
2676#endif
2677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2679 ea.nextcmd = NULL;
2680
2681#ifdef FEAT_EVAL
2682 --ex_nesting_level;
2683#endif
2684
2685 return ea.nextcmd;
2686}
2687#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002688 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689#endif
2690
2691/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002692 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 * If there is a match advance "pp" to the argument and return TRUE.
2694 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002695 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002697 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 char *cmd; /* name of command */
2699 int len; /* required length */
2700{
2701 int i;
2702
2703 for (i = 0; cmd[i] != NUL; ++i)
2704 if (cmd[i] != (*pp)[i])
2705 break;
2706 if (i >= len && !isalpha((*pp)[i]))
2707 {
2708 *pp = skipwhite(*pp + i);
2709 return TRUE;
2710 }
2711 return FALSE;
2712}
2713
2714/*
2715 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002716 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002718 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 * Returns NULL for an ambiguous user command.
2720 */
2721/*ARGSUSED*/
2722 static char_u *
2723find_command(eap, full)
2724 exarg_T *eap;
2725 int *full;
2726{
2727 int len;
2728 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002729 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 /*
2732 * Isolate the command and search for it in the command table.
2733 * Exeptions:
2734 * - the 'k' command can directly be followed by any character.
2735 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2736 * but :sre[wind] is another command, as are :scrip[tnames],
2737 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002738 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 */
2740 p = eap->cmd;
2741 if (*p == 'k')
2742 {
2743 eap->cmdidx = CMD_k;
2744 ++p;
2745 }
2746 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002747 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2748 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 || p[1] == 'g'
2750 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2751 || p[1] == 'I'
2752 || (p[1] == 'r' && p[2] != 'e')))
2753 {
2754 eap->cmdidx = CMD_substitute;
2755 ++p;
2756 }
2757 else
2758 {
2759 while (ASCII_ISALPHA(*p))
2760 ++p;
2761 /* check for non-alpha command */
2762 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2763 ++p;
2764 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002765 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2766 {
2767 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2768 * :delete with the 'l' flag. Same for 'p'. */
2769 for (i = 0; i < len; ++i)
2770 if (eap->cmd[i] != "delete"[i])
2771 break;
2772 if (i == len - 1)
2773 {
2774 --len;
2775 if (p[-1] == 'l')
2776 eap->flags |= EXFLAG_LIST;
2777 else
2778 eap->flags |= EXFLAG_PRINT;
2779 }
2780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
2782 if (ASCII_ISLOWER(*eap->cmd))
2783 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2784 else
2785 eap->cmdidx = cmdidxs[26];
2786
2787 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2788 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2789 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2790 (size_t)len) == 0)
2791 {
2792#ifdef FEAT_EVAL
2793 if (full != NULL
2794 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2795 *full = TRUE;
2796#endif
2797 break;
2798 }
2799
2800#ifdef FEAT_USR_CMDS
2801 /* Look for a user defined command as a last resort */
2802 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2803 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002804 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 while (ASCII_ISALNUM(*p))
2806 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002807 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
2809#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002810 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 eap->cmdidx = CMD_SIZE;
2812 }
2813
2814 return p;
2815}
2816
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002817#ifdef FEAT_USR_CMDS
2818/*
2819 * Search for a user command that matches "eap->cmd".
2820 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2821 * Return a pointer to just after the command.
2822 * Return NULL if there is no matching command.
2823 */
2824 static char_u *
2825find_ucmd(eap, p, full, xp, compl)
2826 exarg_T *eap;
2827 char_u *p; /* end of the command (possibly including count) */
2828 int *full; /* set to TRUE for a full match */
2829 expand_T *xp; /* used for completion, NULL otherwise */
2830 int *compl; /* completion flags or NULL */
2831{
2832 int len = (int)(p - eap->cmd);
2833 int j, k, matchlen = 0;
2834 ucmd_T *uc;
2835 int found = FALSE;
2836 int possible = FALSE;
2837 char_u *cp, *np; /* Point into typed cmd and test name */
2838 garray_T *gap;
2839 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2840 only full match global is accepted. */
2841
2842 /*
2843 * Look for buffer-local user commands first, then global ones.
2844 */
2845 gap = &curbuf->b_ucmds;
2846 for (;;)
2847 {
2848 for (j = 0; j < gap->ga_len; ++j)
2849 {
2850 uc = USER_CMD_GA(gap, j);
2851 cp = eap->cmd;
2852 np = uc->uc_name;
2853 k = 0;
2854 while (k < len && *np != NUL && *cp++ == *np++)
2855 k++;
2856 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2857 {
2858 /* If finding a second match, the command is ambiguous. But
2859 * not if a buffer-local command wasn't a full match and a
2860 * global command is a full match. */
2861 if (k == len && found && *np != NUL)
2862 {
2863 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002864 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002865 amb_local = TRUE;
2866 }
2867
2868 if (!found || (k == len && *np == NUL))
2869 {
2870 /* If we matched up to a digit, then there could
2871 * be another command including the digit that we
2872 * should use instead.
2873 */
2874 if (k == len)
2875 found = TRUE;
2876 else
2877 possible = TRUE;
2878
2879 if (gap == &ucmds)
2880 eap->cmdidx = CMD_USER;
2881 else
2882 eap->cmdidx = CMD_USER_BUF;
2883 eap->argt = uc->uc_argt;
2884 eap->useridx = j;
2885
2886# ifdef FEAT_CMDL_COMPL
2887 if (compl != NULL)
2888 *compl = uc->uc_compl;
2889# ifdef FEAT_EVAL
2890 if (xp != NULL)
2891 {
2892 xp->xp_arg = uc->uc_compl_arg;
2893 xp->xp_scriptID = uc->uc_scriptID;
2894 }
2895# endif
2896# endif
2897 /* Do not search for further abbreviations
2898 * if this is an exact match. */
2899 matchlen = k;
2900 if (k == len && *np == NUL)
2901 {
2902 if (full != NULL)
2903 *full = TRUE;
2904 amb_local = FALSE;
2905 break;
2906 }
2907 }
2908 }
2909 }
2910
2911 /* Stop if we found a full match or searched all. */
2912 if (j < gap->ga_len || gap == &ucmds)
2913 break;
2914 gap = &ucmds;
2915 }
2916
2917 /* Only found ambiguous matches. */
2918 if (amb_local)
2919 {
2920 if (xp != NULL)
2921 xp->xp_context = EXPAND_UNSUCCESSFUL;
2922 return NULL;
2923 }
2924
2925 /* The match we found may be followed immediately by a number. Move "p"
2926 * back to point to it. */
2927 if (found || possible)
2928 return p + (matchlen - len);
2929 return p;
2930}
2931#endif
2932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933#if defined(FEAT_EVAL) || defined(PROTO)
2934/*
2935 * Return > 0 if an Ex command "name" exists.
2936 * Return 2 if there is an exact match.
2937 * Return 3 if there is an ambiguous match.
2938 */
2939 int
2940cmd_exists(name)
2941 char_u *name;
2942{
2943 exarg_T ea;
2944 int full = FALSE;
2945 int i;
2946 int j;
2947 static struct cmdmod
2948 {
2949 char *name;
2950 int minlen;
2951 } cmdmods[] = {
2952 {"aboveleft", 3},
2953 {"belowright", 3},
2954 {"botright", 2},
2955 {"browse", 3},
2956 {"confirm", 4},
2957 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002958 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {"keepjumps", 5},
2960 {"keepmarks", 3},
2961 {"leftabove", 5},
2962 {"lockmarks", 3},
2963 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002964 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 {"silent", 3},
2966 {"topleft", 2},
2967 {"verbose", 4},
2968 {"vertical", 4},
2969 };
2970
2971 /* Check command modifiers. */
2972 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2973 {
2974 for (j = 0; name[j] != NUL; ++j)
2975 if (name[j] != cmdmods[i].name[j])
2976 break;
2977 if (name[j] == NUL && j >= cmdmods[i].minlen)
2978 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2979 }
2980
2981 /* Check built-in commands and user defined commands. */
2982 ea.cmd = name;
2983 ea.cmdidx = (cmdidx_T)0;
2984 if (find_command(&ea, &full) == NULL)
2985 return 3;
2986 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2987}
2988#endif
2989
2990/*
2991 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2992 * we don't need/want deleted. Maybe this could be done better if we didn't
2993 * repeat all this stuff. The only problem is that they may not stay
2994 * perfectly compatible with each other, but then the command line syntax
2995 * probably won't change that much -- webb.
2996 */
2997 char_u *
2998set_one_cmd_context(xp, buff)
2999 expand_T *xp;
3000 char_u *buff; /* buffer for command string */
3001{
3002 char_u *p;
3003 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003004 int len = 0;
3005 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3007 int compl = EXPAND_NOTHING;
3008#endif
3009#ifdef FEAT_CMDL_COMPL
3010 int delim;
3011#endif
3012 int forceit = FALSE;
3013 int usefilter = FALSE; /* filter instead of file name */
3014
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003015 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 xp->xp_pattern = buff;
3017 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003018 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
3020/*
3021 * 2. skip comment lines and leading space, colons or bars
3022 */
3023 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3024 ;
3025 xp->xp_pattern = cmd;
3026
3027 if (*cmd == NUL)
3028 return NULL;
3029 if (*cmd == '"') /* ignore comment lines */
3030 {
3031 xp->xp_context = EXPAND_NOTHING;
3032 return NULL;
3033 }
3034
3035/*
3036 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3037 */
3038 cmd = skip_range(cmd, &xp->xp_context);
3039
3040/*
3041 * 4. parse command
3042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 xp->xp_pattern = cmd;
3044 if (*cmd == NUL)
3045 return NULL;
3046 if (*cmd == '"')
3047 {
3048 xp->xp_context = EXPAND_NOTHING;
3049 return NULL;
3050 }
3051
3052 if (*cmd == '|' || *cmd == '\n')
3053 return cmd + 1; /* There's another command */
3054
3055 /*
3056 * Isolate the command and search for it in the command table.
3057 * Exceptions:
3058 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003059 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3061 */
3062 if (*cmd == 'k' && cmd[1] != 'e')
3063 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003064 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 p = cmd + 1;
3066 }
3067 else
3068 {
3069 p = cmd;
3070 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3071 ++p;
3072 /* check for non-alpha command */
3073 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3074 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003075 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003077 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 {
3079 xp->xp_context = EXPAND_UNSUCCESSFUL;
3080 return NULL;
3081 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003082 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3083 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3084 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 break;
3086
3087#ifdef FEAT_USR_CMDS
3088 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3089 {
3090 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3091 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003092 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
3094#endif
3095 }
3096
3097 /*
3098 * If the cursor is touching the command, and it ends in an alpha-numeric
3099 * character, complete the command name.
3100 */
3101 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3102 return NULL;
3103
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003104 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
3106 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3107 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003108 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 p = cmd + 1;
3110 }
3111#ifdef FEAT_USR_CMDS
3112 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3113 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003114 ea.cmd = cmd;
3115 p = find_ucmd(&ea, p, NULL, xp,
3116# if defined(FEAT_CMDL_COMPL)
3117 &compl
3118# else
3119 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003121 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003122 if (p == NULL)
3123 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 }
3125#endif
3126 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003127 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
3129 /* Not still touching the command and it was an illegal one */
3130 xp->xp_context = EXPAND_UNSUCCESSFUL;
3131 return NULL;
3132 }
3133
3134 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3135
3136 if (*p == '!') /* forced commands */
3137 {
3138 forceit = TRUE;
3139 ++p;
3140 }
3141
3142/*
3143 * 5. parse arguments
3144 */
3145#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003148 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 arg = skipwhite(p);
3151
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003152 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 if (*arg == '>') /* append */
3155 {
3156 if (*++arg == '>')
3157 ++arg;
3158 arg = skipwhite(arg);
3159 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003160 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
3162 ++arg;
3163 usefilter = TRUE;
3164 }
3165 }
3166
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003167 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 {
3169 usefilter = forceit; /* :r! filter if forced */
3170 if (*arg == '!') /* :r !filter */
3171 {
3172 ++arg;
3173 usefilter = TRUE;
3174 }
3175 }
3176
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
3179 while (*arg == *cmd) /* allow any number of '>' or '<' */
3180 ++arg;
3181 arg = skipwhite(arg);
3182 }
3183
3184 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003185 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
3187 /* Check if we're in the +command */
3188 p = arg + 1;
3189 arg = skip_cmd_arg(arg, FALSE);
3190
3191 /* Still touching the command after '+'? */
3192 if (*arg == NUL)
3193 return p;
3194
3195 /* Skip space(s) after +command to get to the real argument */
3196 arg = skipwhite(arg);
3197 }
3198
3199 /*
3200 * Check for '|' to separate commands and '"' to start comments.
3201 * Don't do this for ":read !cmd" and ":write !cmd".
3202 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003203 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205 p = arg;
3206 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 p += 2;
3209 while (*p)
3210 {
3211 if (*p == Ctrl_V)
3212 {
3213 if (p[1] != NUL)
3214 ++p;
3215 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003216 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 || *p == '|' || *p == '\n')
3218 {
3219 if (*(p - 1) != '\\')
3220 {
3221 if (*p == '|' || *p == '\n')
3222 return p + 1;
3223 return NULL; /* It's a comment */
3224 }
3225 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003226 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228 }
3229
3230 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003231 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 vim_strchr((char_u *)"|\"", *arg) == NULL)
3233 return NULL;
3234
3235 /* Find start of last argument (argument just before cursor): */
3236 p = buff + STRLEN(buff);
3237 while (p != arg && *p != ' ' && *p != TAB)
3238 p--;
3239 if (*p == ' ' || *p == TAB)
3240 p++;
3241 xp->xp_pattern = p;
3242
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 {
3245 int in_quote = FALSE;
3246 char_u *bow = NULL; /* Beginning of word */
3247
3248 /*
3249 * Allow spaces within back-quotes to count as part of the argument
3250 * being expanded.
3251 */
3252 xp->xp_pattern = skipwhite(arg);
3253 for (p = xp->xp_pattern; *p; )
3254 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003255 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 ++p;
3257#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003258 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259#else
3260 else if (vim_iswhite(*p))
3261#endif
3262 {
3263 p = skipwhite(p);
3264 if (in_quote)
3265 bow = p;
3266 else
3267 xp->xp_pattern = p;
3268 --p;
3269 }
3270 else if (*p == '`')
3271 {
3272 if (!in_quote)
3273 {
3274 xp->xp_pattern = p;
3275 bow = p + 1;
3276 }
3277 in_quote = !in_quote;
3278 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003279 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281
3282 /*
3283 * If we are still inside the quotes, and we passed a space, just
3284 * expand from there.
3285 */
3286 if (bow != NULL && in_quote)
3287 xp->xp_pattern = bow;
3288 xp->xp_context = EXPAND_FILES;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003289#ifndef BACKSLASH_IN_FILENAME
3290 /* For a shell command more chars need to be escaped. */
3291 if (usefilter || ea.cmdidx == CMD_bang)
3292 xp->xp_shell = TRUE;
3293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
3295 /* Check for environment variable */
3296 if (*xp->xp_pattern == '$'
3297#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3298 || *xp->xp_pattern == '%'
3299#endif
3300 )
3301 {
3302 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3303 if (!vim_isIDc(*p))
3304 break;
3305 if (*p == NUL)
3306 {
3307 xp->xp_context = EXPAND_ENV_VARS;
3308 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003309#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3310 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003311 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003312 compl = EXPAND_ENV_VARS;
3313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315 }
3316 }
3317
3318/*
3319 * 6. switch on command name
3320 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003321 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
3323 case CMD_cd:
3324 case CMD_chdir:
3325 case CMD_lcd:
3326 case CMD_lchdir:
3327 if (xp->xp_context == EXPAND_FILES)
3328 xp->xp_context = EXPAND_DIRECTORIES;
3329 break;
3330 case CMD_help:
3331 xp->xp_context = EXPAND_HELP;
3332 xp->xp_pattern = arg;
3333 break;
3334
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003335 /* Command modifiers: return the argument.
3336 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003338 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 case CMD_belowright:
3340 case CMD_botright:
3341 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003342 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003344 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 case CMD_folddoclosed:
3346 case CMD_folddoopen:
3347 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003348 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 case CMD_keepjumps:
3350 case CMD_keepmarks:
3351 case CMD_leftabove:
3352 case CMD_lockmarks:
3353 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003354 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 case CMD_silent:
3356 case CMD_topleft:
3357 case CMD_verbose:
3358 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003359 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 return arg;
3361
3362#ifdef FEAT_SEARCH_EXTRA
3363 case CMD_match:
3364 if (*arg == NUL || !ends_excmd(*arg))
3365 {
3366 /* Dummy call to clear variables. */
3367 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3368 xp->xp_context = EXPAND_HIGHLIGHT;
3369 xp->xp_pattern = arg;
3370 arg = skipwhite(skiptowhite(arg));
3371 if (*arg != NUL)
3372 {
3373 xp->xp_context = EXPAND_NOTHING;
3374 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3375 }
3376 }
3377 return find_nextcmd(arg);
3378#endif
3379
3380#ifdef FEAT_CMDL_COMPL
3381/*
3382 * All completion for the +cmdline_compl feature goes here.
3383 */
3384
3385# ifdef FEAT_USR_CMDS
3386 case CMD_command:
3387 /* Check for attributes */
3388 while (*arg == '-')
3389 {
3390 arg++; /* Skip "-" */
3391 p = skiptowhite(arg);
3392 if (*p == NUL)
3393 {
3394 /* Cursor is still in the attribute */
3395 p = vim_strchr(arg, '=');
3396 if (p == NULL)
3397 {
3398 /* No "=", so complete attribute names */
3399 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3400 xp->xp_pattern = arg;
3401 return NULL;
3402 }
3403
3404 /* For the -complete and -nargs attributes, we complete
3405 * their arguments as well.
3406 */
3407 if (STRNICMP(arg, "complete", p - arg) == 0)
3408 {
3409 xp->xp_context = EXPAND_USER_COMPLETE;
3410 xp->xp_pattern = p + 1;
3411 return NULL;
3412 }
3413 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3414 {
3415 xp->xp_context = EXPAND_USER_NARGS;
3416 xp->xp_pattern = p + 1;
3417 return NULL;
3418 }
3419 return NULL;
3420 }
3421 arg = skipwhite(p);
3422 }
3423
3424 /* After the attributes comes the new command name */
3425 p = skiptowhite(arg);
3426 if (*p == NUL)
3427 {
3428 xp->xp_context = EXPAND_USER_COMMANDS;
3429 xp->xp_pattern = arg;
3430 break;
3431 }
3432
3433 /* And finally comes a normal command */
3434 return skipwhite(p);
3435
3436 case CMD_delcommand:
3437 xp->xp_context = EXPAND_USER_COMMANDS;
3438 xp->xp_pattern = arg;
3439 break;
3440# endif
3441
3442 case CMD_global:
3443 case CMD_vglobal:
3444 delim = *arg; /* get the delimiter */
3445 if (delim)
3446 ++arg; /* skip delimiter if there is one */
3447
3448 while (arg[0] != NUL && arg[0] != delim)
3449 {
3450 if (arg[0] == '\\' && arg[1] != NUL)
3451 ++arg;
3452 ++arg;
3453 }
3454 if (arg[0] != NUL)
3455 return arg + 1;
3456 break;
3457 case CMD_and:
3458 case CMD_substitute:
3459 delim = *arg;
3460 if (delim)
3461 {
3462 /* skip "from" part */
3463 ++arg;
3464 arg = skip_regexp(arg, delim, p_magic, NULL);
3465 }
3466 /* skip "to" part */
3467 while (arg[0] != NUL && arg[0] != delim)
3468 {
3469 if (arg[0] == '\\' && arg[1] != NUL)
3470 ++arg;
3471 ++arg;
3472 }
3473 if (arg[0] != NUL) /* skip delimiter */
3474 ++arg;
3475 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3476 ++arg;
3477 if (arg[0] != NUL)
3478 return arg;
3479 break;
3480 case CMD_isearch:
3481 case CMD_dsearch:
3482 case CMD_ilist:
3483 case CMD_dlist:
3484 case CMD_ijump:
3485 case CMD_psearch:
3486 case CMD_djump:
3487 case CMD_isplit:
3488 case CMD_dsplit:
3489 arg = skipwhite(skipdigits(arg)); /* skip count */
3490 if (*arg == '/') /* Match regexp, not just whole words */
3491 {
3492 for (++arg; *arg && *arg != '/'; arg++)
3493 if (*arg == '\\' && arg[1] != NUL)
3494 arg++;
3495 if (*arg)
3496 {
3497 arg = skipwhite(arg + 1);
3498
3499 /* Check for trailing illegal characters */
3500 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3501 xp->xp_context = EXPAND_NOTHING;
3502 else
3503 return arg;
3504 }
3505 }
3506 break;
3507#ifdef FEAT_AUTOCMD
3508 case CMD_autocmd:
3509 return set_context_in_autocmd(xp, arg, FALSE);
3510
3511 case CMD_doautocmd:
3512 return set_context_in_autocmd(xp, arg, TRUE);
3513#endif
3514 case CMD_set:
3515 set_context_in_set_cmd(xp, arg, 0);
3516 break;
3517 case CMD_setglobal:
3518 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3519 break;
3520 case CMD_setlocal:
3521 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3522 break;
3523 case CMD_tag:
3524 case CMD_stag:
3525 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003526 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 case CMD_tselect:
3528 case CMD_stselect:
3529 case CMD_ptselect:
3530 case CMD_tjump:
3531 case CMD_stjump:
3532 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003533 if (*p_wop != NUL)
3534 xp->xp_context = EXPAND_TAGS_LISTFILES;
3535 else
3536 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 xp->xp_pattern = arg;
3538 break;
3539 case CMD_augroup:
3540 xp->xp_context = EXPAND_AUGROUP;
3541 xp->xp_pattern = arg;
3542 break;
3543#ifdef FEAT_SYN_HL
3544 case CMD_syntax:
3545 set_context_in_syntax_cmd(xp, arg);
3546 break;
3547#endif
3548#ifdef FEAT_EVAL
3549 case CMD_let:
3550 case CMD_if:
3551 case CMD_elseif:
3552 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003553 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 case CMD_echo:
3555 case CMD_echon:
3556 case CMD_execute:
3557 case CMD_echomsg:
3558 case CMD_echoerr:
3559 case CMD_call:
3560 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003561 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 break;
3563
3564 case CMD_unlet:
3565 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3566 arg = xp->xp_pattern + 1;
3567 xp->xp_context = EXPAND_USER_VARS;
3568 xp->xp_pattern = arg;
3569 break;
3570
3571 case CMD_function:
3572 case CMD_delfunction:
3573 xp->xp_context = EXPAND_USER_FUNC;
3574 xp->xp_pattern = arg;
3575 break;
3576
3577 case CMD_echohl:
3578 xp->xp_context = EXPAND_HIGHLIGHT;
3579 xp->xp_pattern = arg;
3580 break;
3581#endif
3582 case CMD_highlight:
3583 set_context_in_highlight_cmd(xp, arg);
3584 break;
3585#ifdef FEAT_LISTCMDS
3586 case CMD_bdelete:
3587 case CMD_bwipeout:
3588 case CMD_bunload:
3589 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3590 arg = xp->xp_pattern + 1;
3591 /*FALLTHROUGH*/
3592 case CMD_buffer:
3593 case CMD_sbuffer:
3594 case CMD_checktime:
3595 xp->xp_context = EXPAND_BUFFERS;
3596 xp->xp_pattern = arg;
3597 break;
3598#endif
3599#ifdef FEAT_USR_CMDS
3600 case CMD_USER:
3601 case CMD_USER_BUF:
3602 if (compl != EXPAND_NOTHING)
3603 {
3604 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003605 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607# ifdef FEAT_MENU
3608 if (compl == EXPAND_MENUS)
3609 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3610# endif
3611 if (compl == EXPAND_COMMANDS)
3612 return arg;
3613 if (compl == EXPAND_MAPPINGS)
3614 return set_context_in_map_cmd(xp, (char_u *)"map",
3615 arg, forceit, FALSE, FALSE, CMD_map);
3616 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3617 arg = xp->xp_pattern + 1;
3618 xp->xp_pattern = arg;
3619 }
3620 xp->xp_context = compl;
3621 }
3622 break;
3623#endif
3624 case CMD_map: case CMD_noremap:
3625 case CMD_nmap: case CMD_nnoremap:
3626 case CMD_vmap: case CMD_vnoremap:
3627 case CMD_omap: case CMD_onoremap:
3628 case CMD_imap: case CMD_inoremap:
3629 case CMD_cmap: case CMD_cnoremap:
3630 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003631 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 case CMD_unmap:
3633 case CMD_nunmap:
3634 case CMD_vunmap:
3635 case CMD_ounmap:
3636 case CMD_iunmap:
3637 case CMD_cunmap:
3638 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003639 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 case CMD_abbreviate: case CMD_noreabbrev:
3641 case CMD_cabbrev: case CMD_cnoreabbrev:
3642 case CMD_iabbrev: case CMD_inoreabbrev:
3643 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003644 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 case CMD_unabbreviate:
3646 case CMD_cunabbrev:
3647 case CMD_iunabbrev:
3648 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003649 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650#ifdef FEAT_MENU
3651 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3652 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3653 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3654 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3655 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3656 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3657 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3658 case CMD_tmenu: case CMD_tunmenu:
3659 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3660 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3661#endif
3662
3663 case CMD_colorscheme:
3664 xp->xp_context = EXPAND_COLORS;
3665 xp->xp_pattern = arg;
3666 break;
3667
3668 case CMD_compiler:
3669 xp->xp_context = EXPAND_COMPILER;
3670 xp->xp_pattern = arg;
3671 break;
3672
3673#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3674 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3675 case CMD_language:
3676 if (*skiptowhite(arg) == NUL)
3677 {
3678 xp->xp_context = EXPAND_LANGUAGE;
3679 xp->xp_pattern = arg;
3680 }
3681 else
3682 xp->xp_context = EXPAND_NOTHING;
3683 break;
3684#endif
3685
3686#endif /* FEAT_CMDL_COMPL */
3687
3688 default:
3689 break;
3690 }
3691 return NULL;
3692}
3693
3694/*
3695 * skip a range specifier of the form: addr [,addr] [;addr] ..
3696 *
3697 * Backslashed delimiters after / or ? will be skipped, and commands will
3698 * not be expanded between /'s and ?'s or after "'".
3699 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003700 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 * Returns the "cmd" pointer advanced to beyond the range.
3702 */
3703 char_u *
3704skip_range(cmd, ctx)
3705 char_u *cmd;
3706 int *ctx; /* pointer to xp_context or NULL */
3707{
3708 int delim;
3709
Bram Moolenaardf177f62005-02-22 08:39:57 +00003710 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
3712 if (*cmd == '\'')
3713 {
3714 if (*++cmd == NUL && ctx != NULL)
3715 *ctx = EXPAND_NOTHING;
3716 }
3717 else if (*cmd == '/' || *cmd == '?')
3718 {
3719 delim = *cmd++;
3720 while (*cmd != NUL && *cmd != delim)
3721 if (*cmd++ == '\\' && *cmd != NUL)
3722 ++cmd;
3723 if (*cmd == NUL && ctx != NULL)
3724 *ctx = EXPAND_NOTHING;
3725 }
3726 if (*cmd != NUL)
3727 ++cmd;
3728 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003729
3730 /* Skip ":" and white space. */
3731 while (*cmd == ':')
3732 cmd = skipwhite(cmd + 1);
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 return cmd;
3735}
3736
3737/*
3738 * get a single EX address
3739 *
3740 * Set ptr to the next character after the part that was interpreted.
3741 * Set ptr to NULL when an error is encountered.
3742 *
3743 * Return MAXLNUM when no Ex address was found.
3744 */
3745 static linenr_T
3746get_address(ptr, skip, to_other_file)
3747 char_u **ptr;
3748 int skip; /* only skip the address, don't use it */
3749 int to_other_file; /* flag: may jump to other file */
3750{
3751 int c;
3752 int i;
3753 long n;
3754 char_u *cmd;
3755 pos_T pos;
3756 pos_T *fp;
3757 linenr_T lnum;
3758
3759 cmd = skipwhite(*ptr);
3760 lnum = MAXLNUM;
3761 do
3762 {
3763 switch (*cmd)
3764 {
3765 case '.': /* '.' - Cursor position */
3766 ++cmd;
3767 lnum = curwin->w_cursor.lnum;
3768 break;
3769
3770 case '$': /* '$' - last line */
3771 ++cmd;
3772 lnum = curbuf->b_ml.ml_line_count;
3773 break;
3774
3775 case '\'': /* ''' - mark */
3776 if (*++cmd == NUL)
3777 {
3778 cmd = NULL;
3779 goto error;
3780 }
3781 if (skip)
3782 ++cmd;
3783 else
3784 {
3785 /* Only accept a mark in another file when it is
3786 * used by itself: ":'M". */
3787 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3788 ++cmd;
3789 if (fp == (pos_T *)-1)
3790 /* Jumped to another file. */
3791 lnum = curwin->w_cursor.lnum;
3792 else
3793 {
3794 if (check_mark(fp) == FAIL)
3795 {
3796 cmd = NULL;
3797 goto error;
3798 }
3799 lnum = fp->lnum;
3800 }
3801 }
3802 break;
3803
3804 case '/':
3805 case '?': /* '/' or '?' - search */
3806 c = *cmd++;
3807 if (skip) /* skip "/pat/" */
3808 {
3809 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3810 if (*cmd == c)
3811 ++cmd;
3812 }
3813 else
3814 {
3815 pos = curwin->w_cursor; /* save curwin->w_cursor */
3816 /*
3817 * When '/' or '?' follows another address, start
3818 * from there.
3819 */
3820 if (lnum != MAXLNUM)
3821 curwin->w_cursor.lnum = lnum;
3822 /*
3823 * Start a forward search at the end of the line.
3824 * Start a backward search at the start of the line.
3825 * This makes sure we never match in the current
3826 * line, and can match anywhere in the
3827 * next/previous line.
3828 */
3829 if (c == '/')
3830 curwin->w_cursor.col = MAXCOL;
3831 else
3832 curwin->w_cursor.col = 0;
3833 searchcmdlen = 0;
3834 if (!do_search(NULL, c, cmd, 1L,
3835 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3836 {
3837 curwin->w_cursor = pos;
3838 cmd = NULL;
3839 goto error;
3840 }
3841 lnum = curwin->w_cursor.lnum;
3842 curwin->w_cursor = pos;
3843 /* adjust command string pointer */
3844 cmd += searchcmdlen;
3845 }
3846 break;
3847
3848 case '\\': /* "\?", "\/" or "\&", repeat search */
3849 ++cmd;
3850 if (*cmd == '&')
3851 i = RE_SUBST;
3852 else if (*cmd == '?' || *cmd == '/')
3853 i = RE_SEARCH;
3854 else
3855 {
3856 EMSG(_(e_backslash));
3857 cmd = NULL;
3858 goto error;
3859 }
3860
3861 if (!skip)
3862 {
3863 /*
3864 * When search follows another address, start from
3865 * there.
3866 */
3867 if (lnum != MAXLNUM)
3868 pos.lnum = lnum;
3869 else
3870 pos.lnum = curwin->w_cursor.lnum;
3871
3872 /*
3873 * Start the search just like for the above
3874 * do_search().
3875 */
3876 if (*cmd != '?')
3877 pos.col = MAXCOL;
3878 else
3879 pos.col = 0;
3880 if (searchit(curwin, curbuf, &pos,
3881 *cmd == '?' ? BACKWARD : FORWARD,
3882 (char_u *)"", 1L,
3883 SEARCH_MSG + SEARCH_START, i) != FAIL)
3884 lnum = pos.lnum;
3885 else
3886 {
3887 cmd = NULL;
3888 goto error;
3889 }
3890 }
3891 ++cmd;
3892 break;
3893
3894 default:
3895 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3896 lnum = getdigits(&cmd);
3897 }
3898
3899 for (;;)
3900 {
3901 cmd = skipwhite(cmd);
3902 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3903 break;
3904
3905 if (lnum == MAXLNUM)
3906 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3907 if (VIM_ISDIGIT(*cmd))
3908 i = '+'; /* "number" is same as "+number" */
3909 else
3910 i = *cmd++;
3911 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3912 n = 1;
3913 else
3914 n = getdigits(&cmd);
3915 if (i == '-')
3916 lnum -= n;
3917 else
3918 lnum += n;
3919 }
3920 } while (*cmd == '/' || *cmd == '?');
3921
3922error:
3923 *ptr = cmd;
3924 return lnum;
3925}
3926
3927/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003928 * Get flags from an Ex command argument.
3929 */
3930 static void
3931get_flags(eap)
3932 exarg_T *eap;
3933{
3934 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3935 {
3936 if (*eap->arg == 'l')
3937 eap->flags |= EXFLAG_LIST;
3938 else if (*eap->arg == 'p')
3939 eap->flags |= EXFLAG_PRINT;
3940 else
3941 eap->flags |= EXFLAG_NR;
3942 eap->arg = skipwhite(eap->arg + 1);
3943 }
3944}
3945
3946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 * Function called for command which is Not Implemented. NI!
3948 */
3949 void
3950ex_ni(eap)
3951 exarg_T *eap;
3952{
3953 if (!eap->skip)
3954 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3955}
3956
3957#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003958 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959/*
3960 * Function called for script command which is Not Implemented. NI!
3961 * Skips over ":perl <<EOF" constructs.
3962 */
3963 static void
3964ex_script_ni(eap)
3965 exarg_T *eap;
3966{
3967 if (!eap->skip)
3968 ex_ni(eap);
3969 else
3970 vim_free(script_get(eap, eap->arg));
3971}
3972#endif
3973
3974/*
3975 * Check range in Ex command for validity.
3976 * Return NULL when valid, error message when invalid.
3977 */
3978 static char_u *
3979invalid_range(eap)
3980 exarg_T *eap;
3981{
3982 if ( eap->line1 < 0
3983 || eap->line2 < 0
3984 || eap->line1 > eap->line2
3985 || ((eap->argt & RANGE)
3986 && !(eap->argt & NOTADR)
3987 && eap->line2 > curbuf->b_ml.ml_line_count
3988#ifdef FEAT_DIFF
3989 + (eap->cmdidx == CMD_diffget)
3990#endif
3991 ))
3992 return (char_u *)_(e_invrange);
3993 return NULL;
3994}
3995
3996/*
3997 * Correct the range for zero line number, if required.
3998 */
3999 static void
4000correct_range(eap)
4001 exarg_T *eap;
4002{
4003 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4004 {
4005 if (eap->line1 == 0)
4006 eap->line1 = 1;
4007 if (eap->line2 == 0)
4008 eap->line2 = 1;
4009 }
4010}
4011
Bram Moolenaar748bf032005-02-02 23:04:36 +00004012#ifdef FEAT_QUICKFIX
4013static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4014
4015/*
4016 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4017 * pattern. Otherwise return eap->arg.
4018 */
4019 static char_u *
4020skip_grep_pat(eap)
4021 exarg_T *eap;
4022{
4023 char_u *p = eap->arg;
4024
Bram Moolenaara37420f2006-02-04 22:37:47 +00004025 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4026 || eap->cmdidx == CMD_vimgrepadd
4027 || eap->cmdidx == CMD_lvimgrepadd
4028 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004029 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004030 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004031 if (p == NULL)
4032 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004033 }
4034 return p;
4035}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004036
4037/*
4038 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4039 * in the command line, so that things like % get expanded.
4040 */
4041 static char_u *
4042replace_makeprg(eap, p, cmdlinep)
4043 exarg_T *eap;
4044 char_u *p;
4045 char_u **cmdlinep;
4046{
4047 char_u *new_cmdline;
4048 char_u *program;
4049 char_u *pos;
4050 char_u *ptr;
4051 int len;
4052 int i;
4053
4054 /*
4055 * Don't do it when ":vimgrep" is used for ":grep".
4056 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004057 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4058 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4059 || eap->cmdidx == CMD_grepadd
4060 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004061 && !grep_internal(eap->cmdidx))
4062 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004063 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4064 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004065 {
4066 if (*curbuf->b_p_gp == NUL)
4067 program = p_gp;
4068 else
4069 program = curbuf->b_p_gp;
4070 }
4071 else
4072 {
4073 if (*curbuf->b_p_mp == NUL)
4074 program = p_mp;
4075 else
4076 program = curbuf->b_p_mp;
4077 }
4078
4079 p = skipwhite(p);
4080
4081 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4082 {
4083 /* replace $* by given arguments */
4084 i = 1;
4085 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4086 ++i;
4087 len = (int)STRLEN(p);
4088 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4089 if (new_cmdline == NULL)
4090 return NULL; /* out of memory */
4091 ptr = new_cmdline;
4092 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4093 {
4094 i = (int)(pos - program);
4095 STRNCPY(ptr, program, i);
4096 STRCPY(ptr += i, p);
4097 ptr += len;
4098 program = pos + 2;
4099 }
4100 STRCPY(ptr, program);
4101 }
4102 else
4103 {
4104 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4105 if (new_cmdline == NULL)
4106 return NULL; /* out of memory */
4107 STRCPY(new_cmdline, program);
4108 STRCAT(new_cmdline, " ");
4109 STRCAT(new_cmdline, p);
4110 }
4111 msg_make(p);
4112
4113 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4114 vim_free(*cmdlinep);
4115 *cmdlinep = new_cmdline;
4116 p = new_cmdline;
4117 }
4118 return p;
4119}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004120#endif
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122/*
4123 * Expand file name in Ex command argument.
4124 * Return FAIL for failure, OK otherwise.
4125 */
4126 int
4127expand_filename(eap, cmdlinep, errormsgp)
4128 exarg_T *eap;
4129 char_u **cmdlinep;
4130 char_u **errormsgp;
4131{
4132 int has_wildcards; /* need to expand wildcards */
4133 char_u *repl;
4134 int srclen;
4135 char_u *p;
4136 int n;
4137
Bram Moolenaar748bf032005-02-02 23:04:36 +00004138#ifdef FEAT_QUICKFIX
4139 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4140 p = skip_grep_pat(eap);
4141#else
4142 p = eap->arg;
4143#endif
4144
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 /*
4146 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4147 * the file name contains a wildcard it should not cause expanding.
4148 * (it will be expanded anyway if there is a wildcard before replacing).
4149 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004150 has_wildcards = mch_has_wildcard(p);
4151 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004153#ifdef FEAT_EVAL
4154 /* Skip over `=expr`, wildcards in it are not expanded. */
4155 if (p[0] == '`' && p[1] == '=')
4156 {
4157 p += 2;
4158 (void)skip_expr(&p);
4159 if (*p == '`')
4160 ++p;
4161 continue;
4162 }
4163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 /*
4165 * Quick check if this cannot be the start of a special string.
4166 * Also removes backslash before '%', '#' and '<'.
4167 */
4168 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4169 {
4170 ++p;
4171 continue;
4172 }
4173
4174 /*
4175 * Try to find a match at this position.
4176 */
4177 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4178 if (*errormsgp != NULL) /* error detected */
4179 return FAIL;
4180 if (repl == NULL) /* no match found */
4181 {
4182 p += srclen;
4183 continue;
4184 }
4185
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004186 /* Wildcards won't be expanded below, the replacement is taken
4187 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4188 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4189 {
4190 char_u *l = repl;
4191
4192 repl = expand_env_save(repl);
4193 vim_free(l);
4194 }
4195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 /* Need to escape white space et al. with a backslash. Don't do this
4197 * for shell commands (may have to use quotes instead). Don't do this
4198 * for non-unix systems when there is a single argument (spaces don't
4199 * separate arguments then). */
4200 if (!eap->usefilter
4201 && eap->cmdidx != CMD_bang
4202 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004203 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004205 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004207 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#ifndef UNIX
4209 && !(eap->argt & NOSPC)
4210#endif
4211 )
4212 {
4213 char_u *l;
4214#ifdef BACKSLASH_IN_FILENAME
4215 /* Don't escape a backslash here, because rem_backslash() doesn't
4216 * remove it later. */
4217 static char_u *nobslash = (char_u *)" \t\"|";
4218# define ESCAPE_CHARS nobslash
4219#else
4220# define ESCAPE_CHARS escape_chars
4221#endif
4222
4223 for (l = repl; *l; ++l)
4224 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4225 {
4226 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4227 if (l != NULL)
4228 {
4229 vim_free(repl);
4230 repl = l;
4231 }
4232 break;
4233 }
4234 }
4235
4236 /* For a shell command a '!' must be escaped. */
4237 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004238 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
4240 char_u *l;
4241
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004242 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 if (l != NULL)
4244 {
4245 vim_free(repl);
4246 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004247 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (strstr((char *)p_sh, "sh") != NULL)
4249 {
4250 l = vim_strsave_escaped(repl, (char_u *)"!");
4251 if (l != NULL)
4252 {
4253 vim_free(repl);
4254 repl = l;
4255 }
4256 }
4257 }
4258 }
4259
4260 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4261 vim_free(repl);
4262 if (p == NULL)
4263 return FAIL;
4264 }
4265
4266 /*
4267 * One file argument: Expand wildcards.
4268 * Don't do this with ":r !command" or ":w !command".
4269 */
4270 if ((eap->argt & NOSPC) && !eap->usefilter)
4271 {
4272 /*
4273 * May do this twice:
4274 * 1. Replace environment variables.
4275 * 2. Replace any other wildcards, remove backslashes.
4276 */
4277 for (n = 1; n <= 2; ++n)
4278 {
4279 if (n == 2)
4280 {
4281#ifdef UNIX
4282 /*
4283 * Only for Unix we check for more than one file name.
4284 * For other systems spaces are considered to be part
4285 * of the file name.
4286 * Only check here if there is no wildcard, otherwise
4287 * ExpandOne() will check for errors. This allows
4288 * ":e `ls ve*.c`" on Unix.
4289 */
4290 if (!has_wildcards)
4291 for (p = eap->arg; *p; ++p)
4292 {
4293 /* skip escaped characters */
4294 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4295 ++p;
4296 else if (vim_iswhite(*p))
4297 {
4298 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4299 return FAIL;
4300 }
4301 }
4302#endif
4303
4304 /*
4305 * Halve the number of backslashes (this is Vi compatible).
4306 * For Unix and OS/2, when wildcards are expanded, this is
4307 * done by ExpandOne() below.
4308 */
4309#if defined(UNIX) || defined(OS2)
4310 if (!has_wildcards)
4311#endif
4312 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314
4315 if (has_wildcards)
4316 {
4317 if (n == 1)
4318 {
4319 /*
4320 * First loop: May expand environment variables. This
4321 * can be done much faster with expand_env() than with
4322 * something else (e.g., calling a shell).
4323 * After expanding environment variables, check again
4324 * if there are still wildcards present.
4325 */
4326 if (vim_strchr(eap->arg, '$') != NULL
4327 || vim_strchr(eap->arg, '~') != NULL)
4328 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004329 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4330 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 has_wildcards = mch_has_wildcard(NameBuff);
4332 p = NameBuff;
4333 }
4334 else
4335 p = NULL;
4336 }
4337 else /* n == 2 */
4338 {
4339 expand_T xpc;
4340
4341 ExpandInit(&xpc);
4342 xpc.xp_context = EXPAND_FILES;
4343 p = ExpandOne(&xpc, eap->arg, NULL,
4344 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4345 WILD_EXPAND_FREE);
4346 ExpandCleanup(&xpc);
4347 if (p == NULL)
4348 return FAIL;
4349 }
4350 if (p != NULL)
4351 {
4352 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4353 p, cmdlinep);
4354 if (n == 2) /* p came from ExpandOne() */
4355 vim_free(p);
4356 }
4357 }
4358 }
4359 }
4360 return OK;
4361}
4362
4363/*
4364 * Replace part of the command line, keeping eap->cmd, eap->arg and
4365 * eap->nextcmd correct.
4366 * "src" points to the part that is to be replaced, of length "srclen".
4367 * "repl" is the replacement string.
4368 * Returns a pointer to the character after the replaced string.
4369 * Returns NULL for failure.
4370 */
4371 static char_u *
4372repl_cmdline(eap, src, srclen, repl, cmdlinep)
4373 exarg_T *eap;
4374 char_u *src;
4375 int srclen;
4376 char_u *repl;
4377 char_u **cmdlinep;
4378{
4379 int len;
4380 int i;
4381 char_u *new_cmdline;
4382
4383 /*
4384 * The new command line is build in new_cmdline[].
4385 * First allocate it.
4386 * Careful: a "+cmd" argument may have been NUL terminated.
4387 */
4388 len = (int)STRLEN(repl);
4389 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4390 if (eap->nextcmd)
4391 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4392 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4393 return NULL; /* out of memory! */
4394
4395 /*
4396 * Copy the stuff before the expanded part.
4397 * Copy the expanded stuff.
4398 * Copy what came after the expanded part.
4399 * Copy the next commands, if there are any.
4400 */
4401 i = (int)(src - *cmdlinep); /* length of part before match */
4402 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004403
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 mch_memmove(new_cmdline + i, repl, (size_t)len);
4405 i += len; /* remember the end of the string */
4406 STRCPY(new_cmdline + i, src + srclen);
4407 src = new_cmdline + i; /* remember where to continue */
4408
4409 if (eap->nextcmd) /* append next command */
4410 {
4411 i = (int)STRLEN(new_cmdline) + 1;
4412 STRCPY(new_cmdline + i, eap->nextcmd);
4413 eap->nextcmd = new_cmdline + i;
4414 }
4415 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4416 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4417 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4418 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4419 vim_free(*cmdlinep);
4420 *cmdlinep = new_cmdline;
4421
4422 return src;
4423}
4424
4425/*
4426 * Check for '|' to separate commands and '"' to start comments.
4427 */
4428 void
4429separate_nextcmd(eap)
4430 exarg_T *eap;
4431{
4432 char_u *p;
4433
Bram Moolenaar86b68352004-12-27 21:59:20 +00004434#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004435 p = skip_grep_pat(eap);
4436#else
4437 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004438#endif
4439
4440 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
4442 if (*p == Ctrl_V)
4443 {
4444 if (eap->argt & (USECTRLV | XFILE))
4445 ++p; /* skip CTRL-V and next char */
4446 else
4447 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4448 if (*p == NUL) /* stop at NUL after CTRL-V */
4449 break;
4450 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004451
4452#ifdef FEAT_EVAL
4453 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004454 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004455 {
4456 p += 2;
4457 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004458 }
4459#endif
4460
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 /* Check for '"': start of comment or '|': next command */
4462 /* :@" and :*" do not start a comment!
4463 * :redir @" doesn't either. */
4464 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4465 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4466 || p != eap->arg)
4467 && (eap->cmdidx != CMD_redir
4468 || p != eap->arg + 1 || p[-1] != '@'))
4469 || *p == '|' || *p == '\n')
4470 {
4471 /*
4472 * We remove the '\' before the '|', unless USECTRLV is used
4473 * AND 'b' is present in 'cpoptions'.
4474 */
4475 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4476 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4477 {
4478 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4479 --p;
4480 }
4481 else
4482 {
4483 eap->nextcmd = check_nextcmd(p);
4484 *p = NUL;
4485 break;
4486 }
4487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4491 del_trailing_spaces(eap->arg);
4492}
4493
4494/*
4495 * get + command from ex argument
4496 */
4497 static char_u *
4498getargcmd(argp)
4499 char_u **argp;
4500{
4501 char_u *arg = *argp;
4502 char_u *command = NULL;
4503
4504 if (*arg == '+') /* +[command] */
4505 {
4506 ++arg;
4507 if (vim_isspace(*arg))
4508 command = dollar_command;
4509 else
4510 {
4511 command = arg;
4512 arg = skip_cmd_arg(command, TRUE);
4513 if (*arg != NUL)
4514 *arg++ = NUL; /* terminate command with NUL */
4515 }
4516
4517 arg = skipwhite(arg); /* skip over spaces */
4518 *argp = arg;
4519 }
4520 return command;
4521}
4522
4523/*
4524 * Find end of "+command" argument. Skip over "\ " and "\\".
4525 */
4526 static char_u *
4527skip_cmd_arg(p, rembs)
4528 char_u *p;
4529 int rembs; /* TRUE to halve the number of backslashes */
4530{
4531 while (*p && !vim_isspace(*p))
4532 {
4533 if (*p == '\\' && p[1] != NUL)
4534 {
4535 if (rembs)
4536 mch_memmove(p, p + 1, STRLEN(p));
4537 else
4538 ++p;
4539 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004540 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 }
4542 return p;
4543}
4544
4545/*
4546 * Get "++opt=arg" argument.
4547 * Return FAIL or OK.
4548 */
4549 static int
4550getargopt(eap)
4551 exarg_T *eap;
4552{
4553 char_u *arg = eap->arg + 2;
4554 int *pp = NULL;
4555#ifdef FEAT_MBYTE
4556 char_u *p;
4557#endif
4558
4559 /* ":edit ++[no]bin[ary] file" */
4560 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4561 {
4562 if (*arg == 'n')
4563 {
4564 arg += 2;
4565 eap->force_bin = FORCE_NOBIN;
4566 }
4567 else
4568 eap->force_bin = FORCE_BIN;
4569 if (!checkforcmd(&arg, "binary", 3))
4570 return FAIL;
4571 eap->arg = skipwhite(arg);
4572 return OK;
4573 }
4574
4575 if (STRNCMP(arg, "ff", 2) == 0)
4576 {
4577 arg += 2;
4578 pp = &eap->force_ff;
4579 }
4580 else if (STRNCMP(arg, "fileformat", 10) == 0)
4581 {
4582 arg += 10;
4583 pp = &eap->force_ff;
4584 }
4585#ifdef FEAT_MBYTE
4586 else if (STRNCMP(arg, "enc", 3) == 0)
4587 {
4588 arg += 3;
4589 pp = &eap->force_enc;
4590 }
4591 else if (STRNCMP(arg, "encoding", 8) == 0)
4592 {
4593 arg += 8;
4594 pp = &eap->force_enc;
4595 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004596 else if (STRNCMP(arg, "bad", 3) == 0)
4597 {
4598 arg += 3;
4599 pp = &eap->bad_char;
4600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601#endif
4602
4603 if (pp == NULL || *arg != '=')
4604 return FAIL;
4605
4606 ++arg;
4607 *pp = (int)(arg - eap->cmd);
4608 arg = skip_cmd_arg(arg, FALSE);
4609 eap->arg = skipwhite(arg);
4610 *arg = NUL;
4611
4612#ifdef FEAT_MBYTE
4613 if (pp == &eap->force_ff)
4614 {
4615#endif
4616 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4617 return FAIL;
4618#ifdef FEAT_MBYTE
4619 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004620 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
4622 /* Make 'fileencoding' lower case. */
4623 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4624 *p = TOLOWER_ASC(*p);
4625 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004626 else
4627 {
4628 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4629 * "drop". */
4630 p = eap->cmd + eap->bad_char;
4631 if (STRICMP(p, "keep") == 0)
4632 eap->bad_char = BAD_KEEP;
4633 else if (STRICMP(p, "drop") == 0)
4634 eap->bad_char = BAD_DROP;
4635 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4636 eap->bad_char = *p;
4637 else
4638 return FAIL;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#endif
4641
4642 return OK;
4643}
4644
4645/*
4646 * ":abbreviate" and friends.
4647 */
4648 static void
4649ex_abbreviate(eap)
4650 exarg_T *eap;
4651{
4652 do_exmap(eap, TRUE); /* almost the same as mapping */
4653}
4654
4655/*
4656 * ":map" and friends.
4657 */
4658 static void
4659ex_map(eap)
4660 exarg_T *eap;
4661{
4662 /*
4663 * If we are sourcing .exrc or .vimrc in current directory we
4664 * print the mappings for security reasons.
4665 */
4666 if (secure)
4667 {
4668 secure = 2;
4669 msg_outtrans(eap->cmd);
4670 msg_putchar('\n');
4671 }
4672 do_exmap(eap, FALSE);
4673}
4674
4675/*
4676 * ":unmap" and friends.
4677 */
4678 static void
4679ex_unmap(eap)
4680 exarg_T *eap;
4681{
4682 do_exmap(eap, FALSE);
4683}
4684
4685/*
4686 * ":mapclear" and friends.
4687 */
4688 static void
4689ex_mapclear(eap)
4690 exarg_T *eap;
4691{
4692 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4693}
4694
4695/*
4696 * ":abclear" and friends.
4697 */
4698 static void
4699ex_abclear(eap)
4700 exarg_T *eap;
4701{
4702 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4703}
4704
4705#ifdef FEAT_AUTOCMD
4706 static void
4707ex_autocmd(eap)
4708 exarg_T *eap;
4709{
4710 /*
4711 * Disallow auto commands from .exrc and .vimrc in current
4712 * directory for security reasons.
4713 */
4714 if (secure)
4715 {
4716 secure = 2;
4717 eap->errmsg = e_curdir;
4718 }
4719 else if (eap->cmdidx == CMD_autocmd)
4720 do_autocmd(eap->arg, eap->forceit);
4721 else
4722 do_augroup(eap->arg, eap->forceit);
4723}
4724
4725/*
4726 * ":doautocmd": Apply the automatic commands to the current buffer.
4727 */
4728 static void
4729ex_doautocmd(eap)
4730 exarg_T *eap;
4731{
4732 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004733 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734}
4735#endif
4736
4737#ifdef FEAT_LISTCMDS
4738/*
4739 * :[N]bunload[!] [N] [bufname] unload buffer
4740 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4741 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4742 */
4743 static void
4744ex_bunload(eap)
4745 exarg_T *eap;
4746{
4747 eap->errmsg = do_bufdel(
4748 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4749 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4750 : DOBUF_UNLOAD, eap->arg,
4751 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4752}
4753
4754/*
4755 * :[N]buffer [N] to buffer N
4756 * :[N]sbuffer [N] to buffer N
4757 */
4758 static void
4759ex_buffer(eap)
4760 exarg_T *eap;
4761{
4762 if (*eap->arg)
4763 eap->errmsg = e_trailing;
4764 else
4765 {
4766 if (eap->addr_count == 0) /* default is current buffer */
4767 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4768 else
4769 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4770 }
4771}
4772
4773/*
4774 * :[N]bmodified [N] to next mod. buffer
4775 * :[N]sbmodified [N] to next mod. buffer
4776 */
4777 static void
4778ex_bmodified(eap)
4779 exarg_T *eap;
4780{
4781 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4782}
4783
4784/*
4785 * :[N]bnext [N] to next buffer
4786 * :[N]sbnext [N] split and to next buffer
4787 */
4788 static void
4789ex_bnext(eap)
4790 exarg_T *eap;
4791{
4792 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4793}
4794
4795/*
4796 * :[N]bNext [N] to previous buffer
4797 * :[N]bprevious [N] to previous buffer
4798 * :[N]sbNext [N] split and to previous buffer
4799 * :[N]sbprevious [N] split and to previous buffer
4800 */
4801 static void
4802ex_bprevious(eap)
4803 exarg_T *eap;
4804{
4805 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4806}
4807
4808/*
4809 * :brewind to first buffer
4810 * :bfirst to first buffer
4811 * :sbrewind split and to first buffer
4812 * :sbfirst split and to first buffer
4813 */
4814 static void
4815ex_brewind(eap)
4816 exarg_T *eap;
4817{
4818 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4819}
4820
4821/*
4822 * :blast to last buffer
4823 * :sblast split and to last buffer
4824 */
4825 static void
4826ex_blast(eap)
4827 exarg_T *eap;
4828{
4829 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4830}
4831#endif
4832
4833 int
4834ends_excmd(c)
4835 int c;
4836{
4837 return (c == NUL || c == '|' || c == '"' || c == '\n');
4838}
4839
4840#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4841 || defined(PROTO)
4842/*
4843 * Return the next command, after the first '|' or '\n'.
4844 * Return NULL if not found.
4845 */
4846 char_u *
4847find_nextcmd(p)
4848 char_u *p;
4849{
4850 while (*p != '|' && *p != '\n')
4851 {
4852 if (*p == NUL)
4853 return NULL;
4854 ++p;
4855 }
4856 return (p + 1);
4857}
4858#endif
4859
4860/*
4861 * Check if *p is a separator between Ex commands.
4862 * Return NULL if it isn't, (p + 1) if it is.
4863 */
4864 char_u *
4865check_nextcmd(p)
4866 char_u *p;
4867{
4868 p = skipwhite(p);
4869 if (*p == '|' || *p == '\n')
4870 return (p + 1);
4871 else
4872 return NULL;
4873}
4874
4875/*
4876 * - if there are more files to edit
4877 * - and this is the last window
4878 * - and forceit not used
4879 * - and not repeated twice on a row
4880 * return FAIL and give error message if 'message' TRUE
4881 * return OK otherwise
4882 */
4883 static int
4884check_more(message, forceit)
4885 int message; /* when FALSE check only, no messages */
4886 int forceit;
4887{
4888 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4889
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004890 if (!forceit && only_one_window()
4891 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
4893 if (message)
4894 {
4895#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4896 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4897 {
4898 char_u buff[IOSIZE];
4899
4900 if (n == 1)
4901 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4902 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004903 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 _("%d more files to edit. Quit anyway?"), n);
4905 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4906 return OK;
4907 return FAIL;
4908 }
4909#endif
4910 if (n == 1)
4911 EMSG(_("E173: 1 more file to edit"));
4912 else
4913 EMSGN(_("E173: %ld more files to edit"), n);
4914 quitmore = 2; /* next try to quit is allowed */
4915 }
4916 return FAIL;
4917 }
4918 return OK;
4919}
4920
4921#ifdef FEAT_CMDL_COMPL
4922/*
4923 * Function given to ExpandGeneric() to obtain the list of command names.
4924 */
4925/*ARGSUSED*/
4926 char_u *
4927get_command_name(xp, idx)
4928 expand_T *xp;
4929 int idx;
4930{
4931 if (idx >= (int)CMD_SIZE)
4932# ifdef FEAT_USR_CMDS
4933 return get_user_command_name(idx);
4934# else
4935 return NULL;
4936# endif
4937 return cmdnames[idx].cmd_name;
4938}
4939#endif
4940
4941#if defined(FEAT_USR_CMDS) || defined(PROTO)
4942static 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));
4943static void uc_list __ARGS((char_u *name, size_t name_len));
4944static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4945static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4946static 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));
4947
4948 static int
4949uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4950 char_u *name;
4951 size_t name_len;
4952 char_u *rep;
4953 long argt;
4954 long def;
4955 int flags;
4956 int compl;
4957 char_u *compl_arg;
4958 int force;
4959{
4960 ucmd_T *cmd = NULL;
4961 char_u *p;
4962 int i;
4963 int cmp = 1;
4964 char_u *rep_buf = NULL;
4965 garray_T *gap;
4966
4967 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4968 if (rep_buf == NULL)
4969 {
4970 /* Can't replace termcodes - try using the string as is */
4971 rep_buf = vim_strsave(rep);
4972
4973 /* Give up if out of memory */
4974 if (rep_buf == NULL)
4975 return FAIL;
4976 }
4977
4978 /* get address of growarray: global or in curbuf */
4979 if (flags & UC_BUFFER)
4980 {
4981 gap = &curbuf->b_ucmds;
4982 if (gap->ga_itemsize == 0)
4983 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4984 }
4985 else
4986 gap = &ucmds;
4987
4988 /* Search for the command in the already defined commands. */
4989 for (i = 0; i < gap->ga_len; ++i)
4990 {
4991 size_t len;
4992
4993 cmd = USER_CMD_GA(gap, i);
4994 len = STRLEN(cmd->uc_name);
4995 cmp = STRNCMP(name, cmd->uc_name, name_len);
4996 if (cmp == 0)
4997 {
4998 if (name_len < len)
4999 cmp = -1;
5000 else if (name_len > len)
5001 cmp = 1;
5002 }
5003
5004 if (cmp == 0)
5005 {
5006 if (!force)
5007 {
5008 EMSG(_("E174: Command already exists: add ! to replace it"));
5009 goto fail;
5010 }
5011
5012 vim_free(cmd->uc_rep);
5013 cmd->uc_rep = 0;
5014 break;
5015 }
5016
5017 /* Stop as soon as we pass the name to add */
5018 if (cmp < 0)
5019 break;
5020 }
5021
5022 /* Extend the array unless we're replacing an existing command */
5023 if (cmp != 0)
5024 {
5025 if (ga_grow(gap, 1) != OK)
5026 goto fail;
5027 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5028 goto fail;
5029
5030 cmd = USER_CMD_GA(gap, i);
5031 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5032
5033 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
5035 cmd->uc_name = p;
5036 }
5037
5038 cmd->uc_rep = rep_buf;
5039 cmd->uc_argt = argt;
5040 cmd->uc_def = def;
5041 cmd->uc_compl = compl;
5042#ifdef FEAT_EVAL
5043 cmd->uc_scriptID = current_SID;
5044# ifdef FEAT_CMDL_COMPL
5045 cmd->uc_compl_arg = compl_arg;
5046# endif
5047#endif
5048
5049 return OK;
5050
5051fail:
5052 vim_free(rep_buf);
5053#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5054 vim_free(compl_arg);
5055#endif
5056 return FAIL;
5057}
5058
5059/*
5060 * List of names for completion for ":command" with the EXPAND_ flag.
5061 * Must be alphabetical for completion.
5062 */
5063static struct
5064{
5065 int expand;
5066 char *name;
5067} command_complete[] =
5068{
5069 {EXPAND_AUGROUP, "augroup"},
5070 {EXPAND_BUFFERS, "buffer"},
5071 {EXPAND_COMMANDS, "command"},
5072#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5073 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005074 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075#endif
5076 {EXPAND_DIRECTORIES, "dir"},
5077 {EXPAND_ENV_VARS, "environment"},
5078 {EXPAND_EVENTS, "event"},
5079 {EXPAND_EXPRESSION, "expression"},
5080 {EXPAND_FILES, "file"},
5081 {EXPAND_FUNCTIONS, "function"},
5082 {EXPAND_HELP, "help"},
5083 {EXPAND_HIGHLIGHT, "highlight"},
5084 {EXPAND_MAPPINGS, "mapping"},
5085 {EXPAND_MENUS, "menu"},
5086 {EXPAND_SETTINGS, "option"},
5087 {EXPAND_TAGS, "tag"},
5088 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5089 {EXPAND_USER_VARS, "var"},
5090 {0, NULL}
5091};
5092
5093 static void
5094uc_list(name, name_len)
5095 char_u *name;
5096 size_t name_len;
5097{
5098 int i, j;
5099 int found = FALSE;
5100 ucmd_T *cmd;
5101 int len;
5102 long a;
5103 garray_T *gap;
5104
5105 gap = &curbuf->b_ucmds;
5106 for (;;)
5107 {
5108 for (i = 0; i < gap->ga_len; ++i)
5109 {
5110 cmd = USER_CMD_GA(gap, i);
5111 a = cmd->uc_argt;
5112
5113 /* Skip commands which don't match the requested prefix */
5114 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5115 continue;
5116
5117 /* Put out the title first time */
5118 if (!found)
5119 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5120 found = TRUE;
5121 msg_putchar('\n');
5122 if (got_int)
5123 break;
5124
5125 /* Special cases */
5126 msg_putchar(a & BANG ? '!' : ' ');
5127 msg_putchar(a & REGSTR ? '"' : ' ');
5128 msg_putchar(gap != &ucmds ? 'b' : ' ');
5129 msg_putchar(' ');
5130
5131 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5132 len = (int)STRLEN(cmd->uc_name) + 4;
5133
5134 do {
5135 msg_putchar(' ');
5136 ++len;
5137 } while (len < 16);
5138
5139 len = 0;
5140
5141 /* Arguments */
5142 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5143 {
5144 case 0: IObuff[len++] = '0'; break;
5145 case (EXTRA): IObuff[len++] = '*'; break;
5146 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5147 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5148 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5149 }
5150
5151 do {
5152 IObuff[len++] = ' ';
5153 } while (len < 5);
5154
5155 /* Range */
5156 if (a & (RANGE|COUNT))
5157 {
5158 if (a & COUNT)
5159 {
5160 /* -count=N */
5161 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5162 len += (int)STRLEN(IObuff + len);
5163 }
5164 else if (a & DFLALL)
5165 IObuff[len++] = '%';
5166 else if (cmd->uc_def >= 0)
5167 {
5168 /* -range=N */
5169 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5170 len += (int)STRLEN(IObuff + len);
5171 }
5172 else
5173 IObuff[len++] = '.';
5174 }
5175
5176 do {
5177 IObuff[len++] = ' ';
5178 } while (len < 11);
5179
5180 /* Completion */
5181 for (j = 0; command_complete[j].expand != 0; ++j)
5182 if (command_complete[j].expand == cmd->uc_compl)
5183 {
5184 STRCPY(IObuff + len, command_complete[j].name);
5185 len += (int)STRLEN(IObuff + len);
5186 break;
5187 }
5188
5189 do {
5190 IObuff[len++] = ' ';
5191 } while (len < 21);
5192
5193 IObuff[len] = '\0';
5194 msg_outtrans(IObuff);
5195
5196 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005197#ifdef FEAT_EVAL
5198 if (p_verbose > 0)
5199 last_set_msg(cmd->uc_scriptID);
5200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 out_flush();
5202 ui_breakcheck();
5203 if (got_int)
5204 break;
5205 }
5206 if (gap == &ucmds || i < gap->ga_len)
5207 break;
5208 gap = &ucmds;
5209 }
5210
5211 if (!found)
5212 MSG(_("No user-defined commands found"));
5213}
5214
5215 static char_u *
5216uc_fun_cmd()
5217{
5218 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5219 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5220 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5221 0xb9, 0x7f, 0};
5222 int i;
5223
5224 for (i = 0; fcmd[i]; ++i)
5225 IObuff[i] = fcmd[i] - 0x40;
5226 IObuff[i] = 0;
5227 return IObuff;
5228}
5229
5230 static int
5231uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5232 char_u *attr;
5233 size_t len;
5234 long *argt;
5235 long *def;
5236 int *flags;
5237 int *compl;
5238 char_u **compl_arg;
5239{
5240 char_u *p;
5241
5242 if (len == 0)
5243 {
5244 EMSG(_("E175: No attribute specified"));
5245 return FAIL;
5246 }
5247
5248 /* First, try the simple attributes (no arguments) */
5249 if (STRNICMP(attr, "bang", len) == 0)
5250 *argt |= BANG;
5251 else if (STRNICMP(attr, "buffer", len) == 0)
5252 *flags |= UC_BUFFER;
5253 else if (STRNICMP(attr, "register", len) == 0)
5254 *argt |= REGSTR;
5255 else if (STRNICMP(attr, "bar", len) == 0)
5256 *argt |= TRLBAR;
5257 else
5258 {
5259 int i;
5260 char_u *val = NULL;
5261 size_t vallen = 0;
5262 size_t attrlen = len;
5263
5264 /* Look for the attribute name - which is the part before any '=' */
5265 for (i = 0; i < (int)len; ++i)
5266 {
5267 if (attr[i] == '=')
5268 {
5269 val = &attr[i + 1];
5270 vallen = len - i - 1;
5271 attrlen = i;
5272 break;
5273 }
5274 }
5275
5276 if (STRNICMP(attr, "nargs", attrlen) == 0)
5277 {
5278 if (vallen == 1)
5279 {
5280 if (*val == '0')
5281 /* Do nothing - this is the default */;
5282 else if (*val == '1')
5283 *argt |= (EXTRA | NOSPC | NEEDARG);
5284 else if (*val == '*')
5285 *argt |= EXTRA;
5286 else if (*val == '?')
5287 *argt |= (EXTRA | NOSPC);
5288 else if (*val == '+')
5289 *argt |= (EXTRA | NEEDARG);
5290 else
5291 goto wrong_nargs;
5292 }
5293 else
5294 {
5295wrong_nargs:
5296 EMSG(_("E176: Invalid number of arguments"));
5297 return FAIL;
5298 }
5299 }
5300 else if (STRNICMP(attr, "range", attrlen) == 0)
5301 {
5302 *argt |= RANGE;
5303 if (vallen == 1 && *val == '%')
5304 *argt |= DFLALL;
5305 else if (val != NULL)
5306 {
5307 p = val;
5308 if (*def >= 0)
5309 {
5310two_count:
5311 EMSG(_("E177: Count cannot be specified twice"));
5312 return FAIL;
5313 }
5314
5315 *def = getdigits(&p);
5316 *argt |= (ZEROR | NOTADR);
5317
5318 if (p != val + vallen || vallen == 0)
5319 {
5320invalid_count:
5321 EMSG(_("E178: Invalid default value for count"));
5322 return FAIL;
5323 }
5324 }
5325 }
5326 else if (STRNICMP(attr, "count", attrlen) == 0)
5327 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005328 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 if (val != NULL)
5331 {
5332 p = val;
5333 if (*def >= 0)
5334 goto two_count;
5335
5336 *def = getdigits(&p);
5337
5338 if (p != val + vallen)
5339 goto invalid_count;
5340 }
5341
5342 if (*def < 0)
5343 *def = 0;
5344 }
5345 else if (STRNICMP(attr, "complete", attrlen) == 0)
5346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 if (val == NULL)
5348 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005349 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 return FAIL;
5351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005353 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5354 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357 else
5358 {
5359 char_u ch = attr[len];
5360 attr[len] = '\0';
5361 EMSG2(_("E181: Invalid attribute: %s"), attr);
5362 attr[len] = ch;
5363 return FAIL;
5364 }
5365 }
5366
5367 return OK;
5368}
5369
5370 static void
5371ex_command(eap)
5372 exarg_T *eap;
5373{
5374 char_u *name;
5375 char_u *end;
5376 char_u *p;
5377 long argt = 0;
5378 long def = -1;
5379 int flags = 0;
5380 int compl = EXPAND_NOTHING;
5381 char_u *compl_arg = NULL;
5382 int has_attr = (eap->arg[0] == '-');
5383
5384 p = eap->arg;
5385
5386 /* Check for attributes */
5387 while (*p == '-')
5388 {
5389 ++p;
5390 end = skiptowhite(p);
5391 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5392 == FAIL)
5393 return;
5394 p = skipwhite(end);
5395 }
5396
5397 /* Get the name (if any) and skip to the following argument */
5398 name = p;
5399 if (ASCII_ISALPHA(*p))
5400 while (ASCII_ISALNUM(*p))
5401 ++p;
5402 if (!ends_excmd(*p) && !vim_iswhite(*p))
5403 {
5404 EMSG(_("E182: Invalid command name"));
5405 return;
5406 }
5407 end = p;
5408
5409 /* If there is nothing after the name, and no attributes were specified,
5410 * we are listing commands
5411 */
5412 p = skipwhite(end);
5413 if (!has_attr && ends_excmd(*p))
5414 {
5415 uc_list(name, end - name);
5416 }
5417 else if (!ASCII_ISUPPER(*name))
5418 {
5419 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5420 return;
5421 }
5422 else
5423 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5424 eap->forceit);
5425}
5426
5427/*
5428 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 * Clear all user commands, global and for current buffer.
5430 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005431/*ARGSUSED*/
5432 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433ex_comclear(eap)
5434 exarg_T *eap;
5435{
5436 uc_clear(&ucmds);
5437 uc_clear(&curbuf->b_ucmds);
5438}
5439
5440/*
5441 * Clear all user commands for "gap".
5442 */
5443 void
5444uc_clear(gap)
5445 garray_T *gap;
5446{
5447 int i;
5448 ucmd_T *cmd;
5449
5450 for (i = 0; i < gap->ga_len; ++i)
5451 {
5452 cmd = USER_CMD_GA(gap, i);
5453 vim_free(cmd->uc_name);
5454 vim_free(cmd->uc_rep);
5455# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5456 vim_free(cmd->uc_compl_arg);
5457# endif
5458 }
5459 ga_clear(gap);
5460}
5461
5462 static void
5463ex_delcommand(eap)
5464 exarg_T *eap;
5465{
5466 int i = 0;
5467 ucmd_T *cmd = NULL;
5468 int cmp = -1;
5469 garray_T *gap;
5470
5471 gap = &curbuf->b_ucmds;
5472 for (;;)
5473 {
5474 for (i = 0; i < gap->ga_len; ++i)
5475 {
5476 cmd = USER_CMD_GA(gap, i);
5477 cmp = STRCMP(eap->arg, cmd->uc_name);
5478 if (cmp <= 0)
5479 break;
5480 }
5481 if (gap == &ucmds || cmp == 0)
5482 break;
5483 gap = &ucmds;
5484 }
5485
5486 if (cmp != 0)
5487 {
5488 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5489 return;
5490 }
5491
5492 vim_free(cmd->uc_name);
5493 vim_free(cmd->uc_rep);
5494# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5495 vim_free(cmd->uc_compl_arg);
5496# endif
5497
5498 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
5500 if (i < gap->ga_len)
5501 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5502}
5503
5504 static char_u *
5505uc_split_args(arg, lenp)
5506 char_u *arg;
5507 size_t *lenp;
5508{
5509 char_u *buf;
5510 char_u *p;
5511 char_u *q;
5512 int len;
5513
5514 /* Precalculate length */
5515 p = arg;
5516 len = 2; /* Initial and final quotes */
5517
5518 while (*p)
5519 {
5520 if (p[0] == '\\' && vim_iswhite(p[1]))
5521 {
5522 len += 1;
5523 p += 2;
5524 }
5525 else if (*p == '\\' || *p == '"')
5526 {
5527 len += 2;
5528 p += 1;
5529 }
5530 else if (vim_iswhite(*p))
5531 {
5532 p = skipwhite(p);
5533 if (*p == NUL)
5534 break;
5535 len += 3; /* "," */
5536 }
5537 else
5538 {
5539 ++len;
5540 ++p;
5541 }
5542 }
5543
5544 buf = alloc(len + 1);
5545 if (buf == NULL)
5546 {
5547 *lenp = 0;
5548 return buf;
5549 }
5550
5551 p = arg;
5552 q = buf;
5553 *q++ = '"';
5554 while (*p)
5555 {
5556 if (p[0] == '\\' && vim_iswhite(p[1]))
5557 {
5558 *q++ = p[1];
5559 p += 2;
5560 }
5561 else if (*p == '\\' || *p == '"')
5562 {
5563 *q++ = '\\';
5564 *q++ = *p++;
5565 }
5566 else if (vim_iswhite(*p))
5567 {
5568 p = skipwhite(p);
5569 if (*p == NUL)
5570 break;
5571 *q++ = '"';
5572 *q++ = ',';
5573 *q++ = '"';
5574 }
5575 else
5576 {
5577 *q++ = *p++;
5578 }
5579 }
5580 *q++ = '"';
5581 *q = 0;
5582
5583 *lenp = len;
5584 return buf;
5585}
5586
5587/*
5588 * Check for a <> code in a user command.
5589 * "code" points to the '<'. "len" the length of the <> (inclusive).
5590 * "buf" is where the result is to be added.
5591 * "split_buf" points to a buffer used for splitting, caller should free it.
5592 * "split_len" is the length of what "split_buf" contains.
5593 * Returns the length of the replacement, which has been added to "buf".
5594 * Returns -1 if there was no match, and only the "<" has been copied.
5595 */
5596 static size_t
5597uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5598 char_u *code;
5599 size_t len;
5600 char_u *buf;
5601 ucmd_T *cmd; /* the user command we're expanding */
5602 exarg_T *eap; /* ex arguments */
5603 char_u **split_buf;
5604 size_t *split_len;
5605{
5606 size_t result = 0;
5607 char_u *p = code + 1;
5608 size_t l = len - 2;
5609 int quote = 0;
5610 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5611 ct_LT, ct_NONE } type = ct_NONE;
5612
5613 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5614 {
5615 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5616 p += 2;
5617 l -= 2;
5618 }
5619
5620 if (l < 1)
5621 type = ct_NONE;
5622 else if (STRNICMP(p, "args", l) == 0)
5623 type = ct_ARGS;
5624 else if (STRNICMP(p, "bang", l) == 0)
5625 type = ct_BANG;
5626 else if (STRNICMP(p, "count", l) == 0)
5627 type = ct_COUNT;
5628 else if (STRNICMP(p, "line1", l) == 0)
5629 type = ct_LINE1;
5630 else if (STRNICMP(p, "line2", l) == 0)
5631 type = ct_LINE2;
5632 else if (STRNICMP(p, "lt", l) == 0)
5633 type = ct_LT;
5634 else if (STRNICMP(p, "register", l) == 0)
5635 type = ct_REGISTER;
5636
5637 switch (type)
5638 {
5639 case ct_ARGS:
5640 /* Simple case first */
5641 if (*eap->arg == NUL)
5642 {
5643 if (quote == 1)
5644 {
5645 result = 2;
5646 if (buf != NULL)
5647 STRCPY(buf, "''");
5648 }
5649 else
5650 result = 0;
5651 break;
5652 }
5653
5654 /* When specified there is a single argument don't split it.
5655 * Works for ":Cmd %" when % is "a b c". */
5656 if ((eap->argt & NOSPC) && quote == 2)
5657 quote = 1;
5658
5659 switch (quote)
5660 {
5661 case 0: /* No quoting, no splitting */
5662 result = STRLEN(eap->arg);
5663 if (buf != NULL)
5664 STRCPY(buf, eap->arg);
5665 break;
5666 case 1: /* Quote, but don't split */
5667 result = STRLEN(eap->arg) + 2;
5668 for (p = eap->arg; *p; ++p)
5669 {
5670 if (*p == '\\' || *p == '"')
5671 ++result;
5672 }
5673
5674 if (buf != NULL)
5675 {
5676 *buf++ = '"';
5677 for (p = eap->arg; *p; ++p)
5678 {
5679 if (*p == '\\' || *p == '"')
5680 *buf++ = '\\';
5681 *buf++ = *p;
5682 }
5683 *buf = '"';
5684 }
5685
5686 break;
5687 case 2: /* Quote and split */
5688 /* This is hard, so only do it once, and cache the result */
5689 if (*split_buf == NULL)
5690 *split_buf = uc_split_args(eap->arg, split_len);
5691
5692 result = *split_len;
5693 if (buf != NULL && result != 0)
5694 STRCPY(buf, *split_buf);
5695
5696 break;
5697 }
5698 break;
5699
5700 case ct_BANG:
5701 result = eap->forceit ? 1 : 0;
5702 if (quote)
5703 result += 2;
5704 if (buf != NULL)
5705 {
5706 if (quote)
5707 *buf++ = '"';
5708 if (eap->forceit)
5709 *buf++ = '!';
5710 if (quote)
5711 *buf = '"';
5712 }
5713 break;
5714
5715 case ct_LINE1:
5716 case ct_LINE2:
5717 case ct_COUNT:
5718 {
5719 char num_buf[20];
5720 long num = (type == ct_LINE1) ? eap->line1 :
5721 (type == ct_LINE2) ? eap->line2 :
5722 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5723 size_t num_len;
5724
5725 sprintf(num_buf, "%ld", num);
5726 num_len = STRLEN(num_buf);
5727 result = num_len;
5728
5729 if (quote)
5730 result += 2;
5731
5732 if (buf != NULL)
5733 {
5734 if (quote)
5735 *buf++ = '"';
5736 STRCPY(buf, num_buf);
5737 buf += num_len;
5738 if (quote)
5739 *buf = '"';
5740 }
5741
5742 break;
5743 }
5744
5745 case ct_REGISTER:
5746 result = eap->regname ? 1 : 0;
5747 if (quote)
5748 result += 2;
5749 if (buf != NULL)
5750 {
5751 if (quote)
5752 *buf++ = '\'';
5753 if (eap->regname)
5754 *buf++ = eap->regname;
5755 if (quote)
5756 *buf = '\'';
5757 }
5758 break;
5759
5760 case ct_LT:
5761 result = 1;
5762 if (buf != NULL)
5763 *buf = '<';
5764 break;
5765
5766 default:
5767 /* Not recognized: just copy the '<' and return -1. */
5768 result = (size_t)-1;
5769 if (buf != NULL)
5770 *buf = '<';
5771 break;
5772 }
5773
5774 return result;
5775}
5776
5777 static void
5778do_ucmd(eap)
5779 exarg_T *eap;
5780{
5781 char_u *buf;
5782 char_u *p;
5783 char_u *q;
5784
5785 char_u *start;
5786 char_u *end;
5787 size_t len, totlen;
5788
5789 size_t split_len = 0;
5790 char_u *split_buf = NULL;
5791 ucmd_T *cmd;
5792#ifdef FEAT_EVAL
5793 scid_T save_current_SID = current_SID;
5794#endif
5795
5796 if (eap->cmdidx == CMD_USER)
5797 cmd = USER_CMD(eap->useridx);
5798 else
5799 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5800
5801 /*
5802 * Replace <> in the command by the arguments.
5803 */
5804 buf = NULL;
5805 for (;;)
5806 {
5807 p = cmd->uc_rep;
5808 q = buf;
5809 totlen = 0;
5810 while ((start = vim_strchr(p, '<')) != NULL
5811 && (end = vim_strchr(start + 1, '>')) != NULL)
5812 {
5813 /* Include the '>' */
5814 ++end;
5815
5816 /* Take everything up to the '<' */
5817 len = start - p;
5818 if (buf == NULL)
5819 totlen += len;
5820 else
5821 {
5822 mch_memmove(q, p, len);
5823 q += len;
5824 }
5825
5826 len = uc_check_code(start, end - start, q, cmd, eap,
5827 &split_buf, &split_len);
5828 if (len == (size_t)-1)
5829 {
5830 /* no match, continue after '<' */
5831 p = start + 1;
5832 len = 1;
5833 }
5834 else
5835 p = end;
5836 if (buf == NULL)
5837 totlen += len;
5838 else
5839 q += len;
5840 }
5841 if (buf != NULL) /* second time here, finished */
5842 {
5843 STRCPY(q, p);
5844 break;
5845 }
5846
5847 totlen += STRLEN(p); /* Add on the trailing characters */
5848 buf = alloc((unsigned)(totlen + 1));
5849 if (buf == NULL)
5850 {
5851 vim_free(split_buf);
5852 return;
5853 }
5854 }
5855
5856#ifdef FEAT_EVAL
5857 current_SID = cmd->uc_scriptID;
5858#endif
5859 (void)do_cmdline(buf, eap->getline, eap->cookie,
5860 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5861#ifdef FEAT_EVAL
5862 current_SID = save_current_SID;
5863#endif
5864 vim_free(buf);
5865 vim_free(split_buf);
5866}
5867
5868# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5869 static char_u *
5870get_user_command_name(idx)
5871 int idx;
5872{
5873 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5874}
5875
5876/*
5877 * Function given to ExpandGeneric() to obtain the list of user command names.
5878 */
5879/*ARGSUSED*/
5880 char_u *
5881get_user_commands(xp, idx)
5882 expand_T *xp;
5883 int idx;
5884{
5885 if (idx < curbuf->b_ucmds.ga_len)
5886 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5887 idx -= curbuf->b_ucmds.ga_len;
5888 if (idx < ucmds.ga_len)
5889 return USER_CMD(idx)->uc_name;
5890 return NULL;
5891}
5892
5893/*
5894 * Function given to ExpandGeneric() to obtain the list of user command
5895 * attributes.
5896 */
5897/*ARGSUSED*/
5898 char_u *
5899get_user_cmd_flags(xp, idx)
5900 expand_T *xp;
5901 int idx;
5902{
5903 static char *user_cmd_flags[] =
5904 {"bang", "bar", "buffer", "complete", "count",
5905 "nargs", "range", "register"};
5906
5907 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5908 return NULL;
5909 return (char_u *)user_cmd_flags[idx];
5910}
5911
5912/*
5913 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5914 */
5915/*ARGSUSED*/
5916 char_u *
5917get_user_cmd_nargs(xp, idx)
5918 expand_T *xp;
5919 int idx;
5920{
5921 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5922
5923 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5924 return NULL;
5925 return (char_u *)user_cmd_nargs[idx];
5926}
5927
5928/*
5929 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5930 */
5931/*ARGSUSED*/
5932 char_u *
5933get_user_cmd_complete(xp, idx)
5934 expand_T *xp;
5935 int idx;
5936{
5937 return (char_u *)command_complete[idx].name;
5938}
5939# endif /* FEAT_CMDL_COMPL */
5940
5941#endif /* FEAT_USR_CMDS */
5942
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005943#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5944/*
5945 * Parse a completion argument "value[vallen]".
5946 * The detected completion goes in "*complp", argument type in "*argt".
5947 * When there is an argument, for function and user defined completion, it's
5948 * copied to allocated memory and stored in "*compl_arg".
5949 * Returns FAIL if something is wrong.
5950 */
5951 int
5952parse_compl_arg(value, vallen, complp, argt, compl_arg)
5953 char_u *value;
5954 int vallen;
5955 int *complp;
5956 long *argt;
5957 char_u **compl_arg;
5958{
5959 char_u *arg = NULL;
5960 size_t arglen = 0;
5961 int i;
5962 int valend = vallen;
5963
5964 /* Look for any argument part - which is the part after any ',' */
5965 for (i = 0; i < vallen; ++i)
5966 {
5967 if (value[i] == ',')
5968 {
5969 arg = &value[i + 1];
5970 arglen = vallen - i - 1;
5971 valend = i;
5972 break;
5973 }
5974 }
5975
5976 for (i = 0; command_complete[i].expand != 0; ++i)
5977 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005978 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005979 && STRNCMP(value, command_complete[i].name, valend) == 0)
5980 {
5981 *complp = command_complete[i].expand;
5982 if (command_complete[i].expand == EXPAND_BUFFERS)
5983 *argt |= BUFNAME;
5984 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5985 || command_complete[i].expand == EXPAND_FILES)
5986 *argt |= XFILE;
5987 break;
5988 }
5989 }
5990
5991 if (command_complete[i].expand == 0)
5992 {
5993 EMSG2(_("E180: Invalid complete value: %s"), value);
5994 return FAIL;
5995 }
5996
5997# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5998 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
5999 && arg != NULL)
6000# else
6001 if (arg != NULL)
6002# endif
6003 {
6004 EMSG(_("E468: Completion argument only allowed for custom completion"));
6005 return FAIL;
6006 }
6007
6008# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6009 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6010 && arg == NULL)
6011 {
6012 EMSG(_("E467: Custom completion requires a function argument"));
6013 return FAIL;
6014 }
6015
6016 if (arg != NULL)
6017 *compl_arg = vim_strnsave(arg, (int)arglen);
6018# endif
6019 return OK;
6020}
6021#endif
6022
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 static void
6024ex_colorscheme(eap)
6025 exarg_T *eap;
6026{
6027 if (load_colors(eap->arg) == FAIL)
6028 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6029}
6030
6031 static void
6032ex_highlight(eap)
6033 exarg_T *eap;
6034{
6035 if (*eap->arg == NUL && eap->cmd[2] == '!')
6036 MSG(_("Greetings, Vim user!"));
6037 do_highlight(eap->arg, eap->forceit, FALSE);
6038}
6039
6040
6041/*
6042 * Call this function if we thought we were going to exit, but we won't
6043 * (because of an error). May need to restore the terminal mode.
6044 */
6045 void
6046not_exiting()
6047{
6048 exiting = FALSE;
6049 settmode(TMODE_RAW);
6050}
6051
6052/*
6053 * ":quit": quit current window, quit Vim if closed the last window.
6054 */
6055 static void
6056ex_quit(eap)
6057 exarg_T *eap;
6058{
6059#ifdef FEAT_CMDWIN
6060 if (cmdwin_type != 0)
6061 {
6062 cmdwin_result = Ctrl_C;
6063 return;
6064 }
6065#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006066 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006067 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006068 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006069 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006070 return;
6071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
6073#ifdef FEAT_NETBEANS_INTG
6074 netbeansForcedQuit = eap->forceit;
6075#endif
6076
6077 /*
6078 * If there are more files or windows we won't exit.
6079 */
6080 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6081 exiting = TRUE;
6082 if ((!P_HID(curbuf)
6083 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6084 || check_more(TRUE, eap->forceit) == FAIL
6085 || (only_one_window() && check_changed_any(eap->forceit)))
6086 {
6087 not_exiting();
6088 }
6089 else
6090 {
6091#ifdef FEAT_WINDOWS
6092 if (only_one_window()) /* quit last window */
6093#endif
6094 getout(0);
6095#ifdef FEAT_WINDOWS
6096# ifdef FEAT_GUI
6097 need_mouse_correct = TRUE;
6098# endif
6099 /* close window; may free buffer */
6100 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6101#endif
6102 }
6103}
6104
6105/*
6106 * ":cquit".
6107 */
6108/*ARGSUSED*/
6109 static void
6110ex_cquit(eap)
6111 exarg_T *eap;
6112{
6113 getout(1); /* this does not always pass on the exit code to the Manx
6114 compiler. why? */
6115}
6116
6117/*
6118 * ":qall": try to quit all windows
6119 */
6120 static void
6121ex_quit_all(eap)
6122 exarg_T *eap;
6123{
6124# ifdef FEAT_CMDWIN
6125 if (cmdwin_type != 0)
6126 {
6127 if (eap->forceit)
6128 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6129 else
6130 cmdwin_result = K_XF2;
6131 return;
6132 }
6133# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006134
6135 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006136 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006137 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006138 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006139 return;
6140 }
6141
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 exiting = TRUE;
6143 if (eap->forceit || !check_changed_any(FALSE))
6144 getout(0);
6145 not_exiting();
6146}
6147
6148#ifdef FEAT_WINDOWS
6149/*
6150 * ":close": close current window, unless it is the last one
6151 */
6152 static void
6153ex_close(eap)
6154 exarg_T *eap;
6155{
6156# ifdef FEAT_CMDWIN
6157 if (cmdwin_type != 0)
6158 cmdwin_result = K_IGNORE;
6159 else
6160# endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006161 if (!text_locked())
Bram Moolenaarf740b292006-02-16 22:11:02 +00006162 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163}
6164
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006165#ifdef FEAT_QUICKFIX
6166/*
6167 * ":pclose": Close any preview window.
6168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006170ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006172{
6173 win_T *win;
6174
6175 for (win = firstwin; win != NULL; win = win->w_next)
6176 if (win->w_p_pvw)
6177 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006178 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006179 break;
6180 }
6181}
6182#endif
6183
Bram Moolenaarf740b292006-02-16 22:11:02 +00006184/*
6185 * Close window "win" and take care of handling closing the last window for a
6186 * modified buffer.
6187 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006188 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006189ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006190 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006192 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194 int need_hide;
6195 buf_T *buf = win->w_buffer;
6196
6197 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 {
6200#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6201 if ((p_confirm || cmdmod.confirm) && p_write)
6202 {
6203 dialog_changed(buf, FALSE);
6204 if (buf_valid(buf) && bufIsChanged(buf))
6205 return;
6206 need_hide = FALSE;
6207 }
6208 else
6209#endif
6210 {
6211 EMSG(_(e_nowrtmsg));
6212 return;
6213 }
6214 }
6215
6216#ifdef FEAT_GUI
6217 need_mouse_correct = TRUE;
6218#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006219
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006221 if (tp == NULL)
6222 win_close(win, !need_hide && !P_HID(buf));
6223 else
6224 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225}
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006228 * ":tabclose": close current tab page, unless it is the last one.
6229 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 */
6231 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 exarg_T *eap;
6234{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006235 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006236 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006237
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006238# ifdef FEAT_CMDWIN
6239 if (cmdwin_type != 0)
6240 cmdwin_result = K_IGNORE;
6241 else
6242# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006243 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006244 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006247 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006249 tp = find_tabpage((int)eap->line2);
6250 if (tp == NULL)
6251 {
6252 beep_flush();
6253 return;
6254 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006255 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006256 {
6257 tabpage_close_other(tp, eap->forceit);
6258 return;
6259 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006260 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00006261 if (!text_locked())
6262 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006264
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006265 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006266 shell_new_rows();
6267}
6268
6269/*
6270 * ":tabonly": close all tab pages except the current one
6271 */
6272 static void
6273ex_tabonly(eap)
6274 exarg_T *eap;
6275{
6276 tabpage_T *tp;
6277 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006278 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006279
6280# ifdef FEAT_CMDWIN
6281 if (cmdwin_type != 0)
6282 cmdwin_result = K_IGNORE;
6283 else
6284# endif
6285 if (first_tabpage->tp_next == NULL)
6286 MSG(_("Already only one tab page"));
6287 else
6288 {
6289 /* Repeat this up to a 1000 times, because autocommands may mess
6290 * up the lists. */
6291 for (done = 0; done < 1000; ++done)
6292 {
6293 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6294 if (tp->tp_topframe != topframe)
6295 {
6296 tabpage_close_other(tp, eap->forceit);
6297 /* if we failed to close it quit */
6298 if (valid_tabpage(tp))
6299 done = 1000;
6300 /* start over, "tp" is now invalid */
6301 break;
6302 }
6303 if (first_tabpage->tp_next == NULL)
6304 break;
6305 }
6306 }
6307
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006308 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006309 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311
6312/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006313 * Close the current tab page.
6314 */
6315 void
6316tabpage_close(forceit)
6317 int forceit;
6318{
6319 /* First close all the windows but the current one. If that worked then
6320 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006321 if (lastwin != firstwin)
6322 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006323 if (lastwin == firstwin)
6324 ex_win_close(forceit, curwin, NULL);
6325# ifdef FEAT_GUI
6326 need_mouse_correct = TRUE;
6327# endif
6328}
6329
6330/*
6331 * Close tab page "tp", which is not the current tab page.
6332 * Note that autocommands may make "tp" invalid.
6333 */
6334 void
6335tabpage_close_other(tp, forceit)
6336 tabpage_T *tp;
6337 int forceit;
6338{
6339 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006340 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006341
6342 /* Limit to 1000 windows, autocommands may add a window while we close
6343 * one. OK, so I'm paranoid... */
6344 while (++done < 1000)
6345 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006346 wp = tp->tp_firstwin;
6347 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006348
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006349 /* Autocommands may delete the tab page under our fingers and we may
6350 * fail to close a window with a modified buffer. */
6351 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006352 break;
6353 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006354 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006355}
6356
6357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 * ":only".
6359 */
6360 static void
6361ex_only(eap)
6362 exarg_T *eap;
6363{
6364# ifdef FEAT_GUI
6365 need_mouse_correct = TRUE;
6366# endif
6367 close_others(TRUE, eap->forceit);
6368}
6369
6370/*
6371 * ":all" and ":sall".
6372 */
6373 static void
6374ex_all(eap)
6375 exarg_T *eap;
6376{
6377 if (eap->addr_count == 0)
6378 eap->line2 = 9999;
6379 do_arg_all((int)eap->line2, eap->forceit);
6380}
6381#endif /* FEAT_WINDOWS */
6382
6383 static void
6384ex_hide(eap)
6385 exarg_T *eap;
6386{
6387 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6388 eap->errmsg = e_invarg;
6389 else
6390 {
6391 /* ":hide" or ":hide | cmd": hide current window */
6392 eap->nextcmd = check_nextcmd(eap->arg);
6393#ifdef FEAT_WINDOWS
6394 if (!eap->skip)
6395 {
6396# ifdef FEAT_GUI
6397 need_mouse_correct = TRUE;
6398# endif
6399 win_close(curwin, FALSE); /* don't free buffer */
6400 }
6401#endif
6402 }
6403}
6404
6405/*
6406 * ":stop" and ":suspend": Suspend Vim.
6407 */
6408 static void
6409ex_stop(eap)
6410 exarg_T *eap;
6411{
6412 /*
6413 * Disallow suspending for "rvim".
6414 */
6415 if (!check_restricted()
6416#ifdef WIN3264
6417 /*
6418 * Check if external commands are allowed now.
6419 */
6420 && can_end_termcap_mode(TRUE)
6421#endif
6422 )
6423 {
6424 if (!eap->forceit)
6425 autowrite_all();
6426 windgoto((int)Rows - 1, 0);
6427 out_char('\n');
6428 out_flush();
6429 stoptermcap();
6430 out_flush(); /* needed for SUN to restore xterm buffer */
6431#ifdef FEAT_TITLE
6432 mch_restore_title(3); /* restore window titles */
6433#endif
6434 ui_suspend(); /* call machine specific function */
6435#ifdef FEAT_TITLE
6436 maketitle();
6437 resettitle(); /* force updating the title */
6438#endif
6439 starttermcap();
6440 scroll_start(); /* scroll screen before redrawing */
6441 redraw_later_clear();
6442 shell_resized(); /* may have resized window */
6443 }
6444}
6445
6446/*
6447 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6448 */
6449 static void
6450ex_exit(eap)
6451 exarg_T *eap;
6452{
6453#ifdef FEAT_CMDWIN
6454 if (cmdwin_type != 0)
6455 {
6456 cmdwin_result = Ctrl_C;
6457 return;
6458 }
6459#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006460 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006461 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006462 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006463 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006464 return;
6465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
6467 /*
6468 * if more files or windows we won't exit
6469 */
6470 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6471 exiting = TRUE;
6472 if ( ((eap->cmdidx == CMD_wq
6473 || curbufIsChanged())
6474 && do_write(eap) == FAIL)
6475 || check_more(TRUE, eap->forceit) == FAIL
6476 || (only_one_window() && check_changed_any(eap->forceit)))
6477 {
6478 not_exiting();
6479 }
6480 else
6481 {
6482#ifdef FEAT_WINDOWS
6483 if (only_one_window()) /* quit last window, exit Vim */
6484#endif
6485 getout(0);
6486#ifdef FEAT_WINDOWS
6487# ifdef FEAT_GUI
6488 need_mouse_correct = TRUE;
6489# endif
6490 /* quit current window, may free buffer */
6491 win_close(curwin, !P_HID(curwin->w_buffer));
6492#endif
6493 }
6494}
6495
6496/*
6497 * ":print", ":list", ":number".
6498 */
6499 static void
6500ex_print(eap)
6501 exarg_T *eap;
6502{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006503 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6504 EMSG(_(e_emptybuf));
6505 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006507 for ( ;!got_int; ui_breakcheck())
6508 {
6509 print_line(eap->line1,
6510 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6511 || (eap->flags & EXFLAG_NR)),
6512 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6513 if (++eap->line1 > eap->line2)
6514 break;
6515 out_flush(); /* show one line at a time */
6516 }
6517 setpcmark();
6518 /* put cursor at last line */
6519 curwin->w_cursor.lnum = eap->line2;
6520 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
6522
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524}
6525
6526#ifdef FEAT_BYTEOFF
6527 static void
6528ex_goto(eap)
6529 exarg_T *eap;
6530{
6531 goto_byte(eap->line2);
6532}
6533#endif
6534
6535/*
6536 * ":shell".
6537 */
6538/*ARGSUSED*/
6539 static void
6540ex_shell(eap)
6541 exarg_T *eap;
6542{
6543 do_shell(NULL, 0);
6544}
6545
6546#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6547 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006548 || defined(FEAT_GUI_MSWIN) \
6549 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 || defined(PROTO)
6551
6552/*
6553 * Handle a file drop. The code is here because a drop is *nearly* like an
6554 * :args command, but not quite (we have a list of exact filenames, so we
6555 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6556 * code is very similar to :args and hence needs access to a lot of the static
6557 * functions in this file.
6558 *
6559 * The list should be allocated using alloc(), as should each item in the
6560 * list. This function takes over responsibility for freeing the list.
6561 *
6562 * XXX The list is made into the arggument list. This is freed using
6563 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6564 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6565 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6566 * file functionality is (currently) not in EMX this is not presently a
6567 * problem.
6568 */
6569 void
6570handle_drop(filec, filev, split)
6571 int filec; /* the number of files dropped */
6572 char_u **filev; /* the list of files dropped */
6573 int split; /* force splitting the window */
6574{
6575 exarg_T ea;
6576 int save_msg_scroll = msg_scroll;
6577
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006578 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006579 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582 /* Check whether the current buffer is changed. If so, we will need
6583 * to split the current window or data could be lost.
6584 * We don't need to check if the 'hidden' option is set, as in this
6585 * case the buffer won't be lost.
6586 */
6587 if (!P_HID(curbuf) && !split)
6588 {
6589 ++emsg_off;
6590 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6591 --emsg_off;
6592 }
6593 if (split)
6594 {
6595# ifdef FEAT_WINDOWS
6596 if (win_split(0, 0) == FAIL)
6597 return;
6598# ifdef FEAT_SCROLLBIND
6599 curwin->w_p_scb = FALSE;
6600# endif
6601
6602 /* When splitting the window, create a new alist. Otherwise the
6603 * existing one is overwritten. */
6604 alist_unlink(curwin->w_alist);
6605 alist_new();
6606# else
6607 return; /* can't split, always fail */
6608# endif
6609 }
6610
6611 /*
6612 * Set up the new argument list.
6613 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006614 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
6616 /*
6617 * Move to the first file.
6618 */
6619 /* Fake up a minimal "next" command for do_argfile() */
6620 vim_memset(&ea, 0, sizeof(ea));
6621 ea.cmd = (char_u *)"next";
6622 do_argfile(&ea, 0);
6623
6624 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6625 * mode that is not needed here. */
6626 need_start_insertmode = FALSE;
6627
6628 /* Restore msg_scroll, otherwise a following command may cause scrolling
6629 * unexpectedly. The screen will be redrawn by the caller, thus
6630 * msg_scroll being set by displaying a message is irrelevant. */
6631 msg_scroll = save_msg_scroll;
6632}
6633#endif
6634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635/*
6636 * Clear an argument list: free all file names and reset it to zero entries.
6637 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006638 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639alist_clear(al)
6640 alist_T *al;
6641{
6642 while (--al->al_ga.ga_len >= 0)
6643 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6644 ga_clear(&al->al_ga);
6645}
6646
6647/*
6648 * Init an argument list.
6649 */
6650 void
6651alist_init(al)
6652 alist_T *al;
6653{
6654 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6655}
6656
6657#if defined(FEAT_WINDOWS) || defined(PROTO)
6658
6659/*
6660 * Remove a reference from an argument list.
6661 * Ignored when the argument list is the global one.
6662 * If the argument list is no longer used by any window, free it.
6663 */
6664 void
6665alist_unlink(al)
6666 alist_T *al;
6667{
6668 if (al != &global_alist && --al->al_refcount <= 0)
6669 {
6670 alist_clear(al);
6671 vim_free(al);
6672 }
6673}
6674
6675# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6676/*
6677 * Create a new argument list and use it for the current window.
6678 */
6679 void
6680alist_new()
6681{
6682 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6683 if (curwin->w_alist == NULL)
6684 {
6685 curwin->w_alist = &global_alist;
6686 ++global_alist.al_refcount;
6687 }
6688 else
6689 {
6690 curwin->w_alist->al_refcount = 1;
6691 alist_init(curwin->w_alist);
6692 }
6693}
6694# endif
6695#endif
6696
6697#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6698/*
6699 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006700 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6701 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 */
6703 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006704alist_expand(fnum_list, fnum_len)
6705 int *fnum_list;
6706 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006709 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 char_u **new_arg_files;
6711 int new_arg_file_count;
6712 char_u *save_p_su = p_su;
6713 int i;
6714
6715 /* Don't use 'suffixes' here. This should work like the shell did the
6716 * expansion. Also, the vimrc file isn't read yet, thus the user
6717 * can't set the options. */
6718 p_su = empty_option;
6719 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6720 if (old_arg_files != NULL)
6721 {
6722 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006723 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6724 old_arg_count = GARGCOUNT;
6725 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 &new_arg_file_count, &new_arg_files,
6727 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6728 && new_arg_file_count > 0)
6729 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006730 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6731 TRUE, fnum_list, fnum_len);
6732 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 }
6735 p_su = save_p_su;
6736}
6737#endif
6738
6739/*
6740 * Set the argument list for the current window.
6741 * Takes over the allocated files[] and the allocated fnames in it.
6742 */
6743 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006744alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 alist_T *al;
6746 int count;
6747 char_u **files;
6748 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006749 int *fnum_list;
6750 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752 int i;
6753
6754 alist_clear(al);
6755 if (ga_grow(&al->al_ga, count) == OK)
6756 {
6757 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006758 {
6759 if (got_int)
6760 {
6761 /* When adding many buffers this can take a long time. Allow
6762 * interrupting here. */
6763 while (i < count)
6764 vim_free(files[i++]);
6765 break;
6766 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006767
6768 /* May set buffer name of a buffer previously used for the
6769 * argument list, so that it's re-used by alist_add. */
6770 if (fnum_list != NULL && i < fnum_len)
6771 buf_set_name(fnum_list[i], files[i]);
6772
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006774 ui_breakcheck();
6775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 vim_free(files);
6777 }
6778 else
6779 FreeWild(count, files);
6780#ifdef FEAT_WINDOWS
6781 if (al == &global_alist)
6782#endif
6783 arg_had_last = FALSE;
6784}
6785
6786/*
6787 * Add file "fname" to argument list "al".
6788 * "fname" must have been allocated and "al" must have been checked for room.
6789 */
6790 void
6791alist_add(al, fname, set_fnum)
6792 alist_T *al;
6793 char_u *fname;
6794 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6795{
6796 if (fname == NULL) /* don't add NULL file names */
6797 return;
6798#ifdef BACKSLASH_IN_FILENAME
6799 slash_adjust(fname);
6800#endif
6801 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6802 if (set_fnum > 0)
6803 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6804 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6805 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806}
6807
6808#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6809/*
6810 * Adjust slashes in file names. Called after 'shellslash' was set.
6811 */
6812 void
6813alist_slash_adjust()
6814{
6815 int i;
6816# ifdef FEAT_WINDOWS
6817 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006818 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819# endif
6820
6821 for (i = 0; i < GARGCOUNT; ++i)
6822 if (GARGLIST[i].ae_fname != NULL)
6823 slash_adjust(GARGLIST[i].ae_fname);
6824# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006825 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 if (wp->w_alist != &global_alist)
6827 for (i = 0; i < WARGCOUNT(wp); ++i)
6828 if (WARGLIST(wp)[i].ae_fname != NULL)
6829 slash_adjust(WARGLIST(wp)[i].ae_fname);
6830# endif
6831}
6832#endif
6833
6834/*
6835 * ":preserve".
6836 */
6837/*ARGSUSED*/
6838 static void
6839ex_preserve(eap)
6840 exarg_T *eap;
6841{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006842 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 ml_preserve(curbuf, TRUE);
6844}
6845
6846/*
6847 * ":recover".
6848 */
6849 static void
6850ex_recover(eap)
6851 exarg_T *eap;
6852{
6853 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6854 recoverymode = TRUE;
6855 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6856 && (*eap->arg == NUL
6857 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6858 ml_recover();
6859 recoverymode = FALSE;
6860}
6861
6862/*
6863 * Command modifier used in a wrong way.
6864 */
6865 static void
6866ex_wrongmodifier(eap)
6867 exarg_T *eap;
6868{
6869 eap->errmsg = e_invcmd;
6870}
6871
6872#ifdef FEAT_WINDOWS
6873/*
6874 * :sview [+command] file split window with new file, read-only
6875 * :split [[+command] file] split window with current or new file
6876 * :vsplit [[+command] file] split window vertically with current or new file
6877 * :new [[+command] file] split window with no or new file
6878 * :vnew [[+command] file] split vertically window with no or new file
6879 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006880 *
6881 * :tabedit open new Tab page with empty window
6882 * :tabedit [+command] file open new Tab page and edit "file"
6883 * :tabnew [[+command] file] just like :tabedit
6884 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 */
6886 void
6887ex_splitview(eap)
6888 exarg_T *eap;
6889{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006890 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006891# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006893# endif
6894# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006898# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6900 {
6901 ex_ni(eap);
6902 return;
6903 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006906# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 /* A ":split" in the quickfix window works like ":new". Don't want two
6912 * quickfix windows. */
6913 if (bt_quickfix(curbuf))
6914 {
6915 if (eap->cmdidx == CMD_split)
6916 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006917# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (eap->cmdidx == CMD_vsplit)
6919 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006924# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006925 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
6927 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6928 FNAME_MESS, TRUE, curbuf->b_ffname);
6929 if (fname == NULL)
6930 goto theend;
6931 eap->arg = fname;
6932 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006933# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006937# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006939# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 && eap->cmdidx != CMD_new)
6943 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006944 if (
6945# ifdef FEAT_GUI
6946 !gui.in_use &&
6947# endif
6948 au_has_group((char_u *)"FileExplorer"))
6949 {
6950 /* No browsing supported but we do have the file explorer:
6951 * Edit the directory. */
6952 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6953 eap->arg = (char_u *)".";
6954 }
6955 else
6956 {
6957 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006959 if (fname == NULL)
6960 goto theend;
6961 eap->arg = fname;
6962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 }
6964 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006967 /*
6968 * Either open new tab page or split the window.
6969 */
6970 if (eap->cmdidx == CMD_tabedit
6971 || eap->cmdidx == CMD_tabfind
6972 || eap->cmdidx == CMD_tabnew)
6973 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006974 if (win_new_tabpage(cmdmod.tab) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006975 {
6976 do_exedit(eap, NULL);
6977
6978 /* set the alternate buffer for the window we came from */
6979 if (curwin != old_curwin
6980 && win_valid(old_curwin)
6981 && old_curwin->w_buffer != curbuf
6982 && !cmdmod.keepalt)
6983 old_curwin->w_alt_fnum = curbuf->b_fnum;
6984 }
6985 }
6986 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6988 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006989# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 /* Reset 'scrollbind' when editing another file, but keep it when
6991 * doing ":split" without arguments. */
6992 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006993# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 )
6997 curwin->w_p_scb = FALSE;
6998 else
6999 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 do_exedit(eap, old_curwin);
7002 }
7003
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007004# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007008# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009theend:
7010 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007013
7014/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007015 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007016 */
7017 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007018tabpage_new()
7019{
7020 exarg_T ea;
7021
7022 vim_memset(&ea, 0, sizeof(ea));
7023 ea.cmdidx = CMD_tabnew;
7024 ea.cmd = (char_u *)"tabn";
7025 ea.arg = (char_u *)"";
7026 ex_splitview(&ea);
7027}
7028
7029/*
7030 * :tabnext command
7031 */
7032 static void
7033ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007034 exarg_T *eap;
7035{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007036 switch (eap->cmdidx)
7037 {
7038 case CMD_tabfirst:
7039 case CMD_tabrewind:
7040 goto_tabpage(1);
7041 break;
7042 case CMD_tablast:
7043 goto_tabpage(9999);
7044 break;
7045 case CMD_tabprevious:
7046 case CMD_tabNext:
7047 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7048 break;
7049 default: /* CMD_tabnext */
7050 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7051 break;
7052 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007053}
7054
7055/*
7056 * :tabmove command
7057 */
7058 static void
7059ex_tabmove(eap)
7060 exarg_T *eap;
7061{
7062 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007063}
7064
7065/*
7066 * :tabs command: List tabs and their contents.
7067 */
7068/*ARGSUSED*/
7069 static void
7070ex_tabs(eap)
7071 exarg_T *eap;
7072{
7073 tabpage_T *tp;
7074 win_T *wp;
7075 int tabcount = 1;
7076
7077 msg_start();
7078 msg_scroll = TRUE;
7079 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7080 {
7081 msg_putchar('\n');
7082 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7083 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7084 out_flush(); /* output one line at a time */
7085 ui_breakcheck();
7086
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007087 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007088 wp = firstwin;
7089 else
7090 wp = tp->tp_firstwin;
7091 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7092 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007093 msg_putchar('\n');
7094 msg_putchar(wp == curwin ? '>' : ' ');
7095 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007096 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7097 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007098 if (buf_spname(wp->w_buffer) != NULL)
7099 STRCPY(IObuff, buf_spname(wp->w_buffer));
7100 else
7101 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7102 IObuff, IOSIZE, TRUE);
7103 msg_outtrans(IObuff);
7104 out_flush(); /* output one line at a time */
7105 ui_breakcheck();
7106 }
7107 }
7108}
7109
7110#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111
7112/*
7113 * ":mode": Set screen mode.
7114 * If no argument given, just get the screen size and redraw.
7115 */
7116 static void
7117ex_mode(eap)
7118 exarg_T *eap;
7119{
7120 if (*eap->arg == NUL)
7121 shell_resized();
7122 else
7123 mch_screenmode(eap->arg);
7124}
7125
7126#ifdef FEAT_WINDOWS
7127/*
7128 * ":resize".
7129 * set, increment or decrement current window height
7130 */
7131 static void
7132ex_resize(eap)
7133 exarg_T *eap;
7134{
7135 int n;
7136 win_T *wp = curwin;
7137
7138 if (eap->addr_count > 0)
7139 {
7140 n = eap->line2;
7141 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7142 ;
7143 }
7144
7145#ifdef FEAT_GUI
7146 need_mouse_correct = TRUE;
7147#endif
7148 n = atol((char *)eap->arg);
7149#ifdef FEAT_VERTSPLIT
7150 if (cmdmod.split & WSP_VERT)
7151 {
7152 if (*eap->arg == '-' || *eap->arg == '+')
7153 n += W_WIDTH(curwin);
7154 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7155 n = 9999;
7156 win_setwidth_win((int)n, wp);
7157 }
7158 else
7159#endif
7160 {
7161 if (*eap->arg == '-' || *eap->arg == '+')
7162 n += curwin->w_height;
7163 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7164 n = 9999;
7165 win_setheight_win((int)n, wp);
7166 }
7167}
7168#endif
7169
7170/*
7171 * ":find [+command] <file>" command.
7172 */
7173 static void
7174ex_find(eap)
7175 exarg_T *eap;
7176{
7177#ifdef FEAT_SEARCHPATH
7178 char_u *fname;
7179 int count;
7180
7181 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7182 TRUE, curbuf->b_ffname);
7183 if (eap->addr_count > 0)
7184 {
7185 /* Repeat finding the file "count" times. This matters when it
7186 * appears several times in the path. */
7187 count = eap->line2;
7188 while (fname != NULL && --count > 0)
7189 {
7190 vim_free(fname);
7191 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7192 FALSE, curbuf->b_ffname);
7193 }
7194 }
7195
7196 if (fname != NULL)
7197 {
7198 eap->arg = fname;
7199#endif
7200 do_exedit(eap, NULL);
7201#ifdef FEAT_SEARCHPATH
7202 vim_free(fname);
7203 }
7204#endif
7205}
7206
7207/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007208 * ":open" simulation: for now just work like ":visual".
7209 */
7210 static void
7211ex_open(eap)
7212 exarg_T *eap;
7213{
7214 regmatch_T regmatch;
7215 char_u *p;
7216
7217 curwin->w_cursor.lnum = eap->line2;
7218 beginline(BL_SOL | BL_FIX);
7219 if (*eap->arg == '/')
7220 {
7221 /* ":open /pattern/": put cursor in column found with pattern */
7222 ++eap->arg;
7223 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7224 *p = NUL;
7225 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7226 if (regmatch.regprog != NULL)
7227 {
7228 regmatch.rm_ic = p_ic;
7229 p = ml_get_curline();
7230 if (vim_regexec(&regmatch, p, (colnr_T)0))
7231 curwin->w_cursor.col = regmatch.startp[0] - p;
7232 else
7233 EMSG(_(e_nomatch));
7234 vim_free(regmatch.regprog);
7235 }
7236 /* Move to the NUL, ignore any other arguments. */
7237 eap->arg += STRLEN(eap->arg);
7238 }
7239 check_cursor();
7240
7241 eap->cmdidx = CMD_visual;
7242 do_exedit(eap, NULL);
7243}
7244
7245/*
7246 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 */
7248 static void
7249ex_edit(eap)
7250 exarg_T *eap;
7251{
7252 do_exedit(eap, NULL);
7253}
7254
7255/*
7256 * ":edit <file>" command and alikes.
7257 */
7258/*ARGSUSED*/
7259 void
7260do_exedit(eap, old_curwin)
7261 exarg_T *eap;
7262 win_T *old_curwin; /* curwin before doing a split or NULL */
7263{
7264 int n;
7265#ifdef FEAT_WINDOWS
7266 int need_hide;
7267#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007268 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
7270 /*
7271 * ":vi" command ends Ex mode.
7272 */
7273 if (exmode_active && (eap->cmdidx == CMD_visual
7274 || eap->cmdidx == CMD_view))
7275 {
7276 exmode_active = FALSE;
7277 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007278 {
7279 /* Special case: ":global/pat/visual\NLvi-commands" */
7280 if (global_busy)
7281 {
7282 int rd = RedrawingDisabled;
7283 int nwr = no_wait_return;
7284 int ms = msg_scroll;
7285#ifdef FEAT_GUI
7286 int he = hold_gui_events;
7287#endif
7288
7289 if (eap->nextcmd != NULL)
7290 {
7291 stuffReadbuff(eap->nextcmd);
7292 eap->nextcmd = NULL;
7293 }
7294
7295 if (exmode_was != EXMODE_VIM)
7296 settmode(TMODE_RAW);
7297 RedrawingDisabled = 0;
7298 no_wait_return = 0;
7299 need_wait_return = FALSE;
7300 msg_scroll = 0;
7301#ifdef FEAT_GUI
7302 hold_gui_events = 0;
7303#endif
7304 must_redraw = CLEAR;
7305
7306 main_loop(FALSE, TRUE);
7307
7308 RedrawingDisabled = rd;
7309 no_wait_return = nwr;
7310 msg_scroll = ms;
7311#ifdef FEAT_GUI
7312 hold_gui_events = he;
7313#endif
7314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 }
7318
7319 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007320 || eap->cmdidx == CMD_tabnew
7321 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322#ifdef FEAT_VERTSPLIT
7323 || eap->cmdidx == CMD_vnew
7324#endif
7325 ) && *eap->arg == NUL)
7326 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007327 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 setpcmark();
7329 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7330 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7331 }
7332 else if ((eap->cmdidx != CMD_split
7333#ifdef FEAT_VERTSPLIT
7334 && eap->cmdidx != CMD_vsplit
7335#endif
7336 )
7337 || *eap->arg != NUL
7338#ifdef FEAT_BROWSE
7339 || cmdmod.browse
7340#endif
7341 )
7342 {
7343 n = readonlymode;
7344 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7345 readonlymode = TRUE;
7346 else if (eap->cmdidx == CMD_enew)
7347 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7348 empty buffer */
7349 setpcmark();
7350 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7351 NULL, eap,
7352 /* ":edit" goes to first line if Vi compatible */
7353 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7354 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7355 ? ECMD_ONE : eap->do_ecmd_lnum,
7356 (P_HID(curbuf) ? ECMD_HIDE : 0)
7357 + (eap->forceit ? ECMD_FORCEIT : 0)
7358#ifdef FEAT_LISTCMDS
7359 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7360#endif
7361 ) == FAIL)
7362 {
7363 /* Editing the file failed. If the window was split, close it. */
7364#ifdef FEAT_WINDOWS
7365 if (old_curwin != NULL)
7366 {
7367 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7368 if (!need_hide || P_HID(curbuf))
7369 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007370# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7371 cleanup_T cs;
7372
7373 /* Reset the error/interrupt/exception state here so that
7374 * aborting() returns FALSE when closing a window. */
7375 enter_cleanup(&cs);
7376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377# ifdef FEAT_GUI
7378 need_mouse_correct = TRUE;
7379# endif
7380 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007381
7382# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7383 /* Restore the error/interrupt/exception state if not
7384 * discarded by a new aborting error, interrupt, or
7385 * uncaught exception. */
7386 leave_cleanup(&cs);
7387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 }
7389 }
7390#endif
7391 }
7392 else if (readonlymode && curbuf->b_nwindows == 1)
7393 {
7394 /* When editing an already visited buffer, 'readonly' won't be set
7395 * but the previous value is kept. With ":view" and ":sview" we
7396 * want the file to be readonly, except when another window is
7397 * editing the same buffer. */
7398 curbuf->b_p_ro = TRUE;
7399 }
7400 readonlymode = n;
7401 }
7402 else
7403 {
7404 if (eap->do_ecmd_cmd != NULL)
7405 do_cmdline_cmd(eap->do_ecmd_cmd);
7406#ifdef FEAT_TITLE
7407 n = curwin->w_arg_idx_invalid;
7408#endif
7409 check_arg_idx(curwin);
7410#ifdef FEAT_TITLE
7411 if (n != curwin->w_arg_idx_invalid)
7412 maketitle();
7413#endif
7414 }
7415
7416#ifdef FEAT_WINDOWS
7417 /*
7418 * if ":split file" worked, set alternate file name in old window to new
7419 * file
7420 */
7421 if (old_curwin != NULL
7422 && *eap->arg != NUL
7423 && curwin != old_curwin
7424 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007425 && old_curwin->w_buffer != curbuf
7426 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 old_curwin->w_alt_fnum = curbuf->b_fnum;
7428#endif
7429
7430 ex_no_reprint = TRUE;
7431}
7432
7433#ifndef FEAT_GUI
7434/*
7435 * ":gui" and ":gvim" when there is no GUI.
7436 */
7437 static void
7438ex_nogui(eap)
7439 exarg_T *eap;
7440{
7441 eap->errmsg = e_nogvim;
7442}
7443#endif
7444
7445#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7446 static void
7447ex_tearoff(eap)
7448 exarg_T *eap;
7449{
7450 gui_make_tearoff(eap->arg);
7451}
7452#endif
7453
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007454#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 static void
7456ex_popup(eap)
7457 exarg_T *eap;
7458{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007459 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460}
7461#endif
7462
7463/*ARGSUSED*/
7464 static void
7465ex_swapname(eap)
7466 exarg_T *eap;
7467{
7468 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7469 MSG(_("No swap file"));
7470 else
7471 msg(curbuf->b_ml.ml_mfp->mf_fname);
7472}
7473
7474/*
7475 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7476 * offset.
7477 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7478 */
7479/*ARGSUSED*/
7480 static void
7481ex_syncbind(eap)
7482 exarg_T *eap;
7483{
7484#ifdef FEAT_SCROLLBIND
7485 win_T *wp;
7486 long topline;
7487 long y;
7488 linenr_T old_linenr = curwin->w_cursor.lnum;
7489
7490 setpcmark();
7491
7492 /*
7493 * determine max topline
7494 */
7495 if (curwin->w_p_scb)
7496 {
7497 topline = curwin->w_topline;
7498 for (wp = firstwin; wp; wp = wp->w_next)
7499 {
7500 if (wp->w_p_scb && wp->w_buffer)
7501 {
7502 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7503 if (topline > y)
7504 topline = y;
7505 }
7506 }
7507 if (topline < 1)
7508 topline = 1;
7509 }
7510 else
7511 {
7512 topline = 1;
7513 }
7514
7515
7516 /*
7517 * set all scrollbind windows to the same topline
7518 */
7519 wp = curwin;
7520 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7521 {
7522 if (curwin->w_p_scb)
7523 {
7524 y = topline - curwin->w_topline;
7525 if (y > 0)
7526 scrollup(y, TRUE);
7527 else
7528 scrolldown(-y, TRUE);
7529 curwin->w_scbind_pos = topline;
7530 redraw_later(VALID);
7531 cursor_correct();
7532#ifdef FEAT_WINDOWS
7533 curwin->w_redr_status = TRUE;
7534#endif
7535 }
7536 }
7537 curwin = wp;
7538 if (curwin->w_p_scb)
7539 {
7540 did_syncbind = TRUE;
7541 checkpcmark();
7542 if (old_linenr != curwin->w_cursor.lnum)
7543 {
7544 char_u ctrl_o[2];
7545
7546 ctrl_o[0] = Ctrl_O;
7547 ctrl_o[1] = 0;
7548 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7549 }
7550 }
7551#endif
7552}
7553
7554
7555 static void
7556ex_read(eap)
7557 exarg_T *eap;
7558{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007559 int i;
7560 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7561 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
7563 if (eap->usefilter) /* :r!cmd */
7564 do_bang(1, eap, FALSE, FALSE, TRUE);
7565 else
7566 {
7567 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7568 return;
7569
7570#ifdef FEAT_BROWSE
7571 if (cmdmod.browse)
7572 {
7573 char_u *browseFile;
7574
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007575 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 NULL, NULL, NULL, curbuf);
7577 if (browseFile != NULL)
7578 {
7579 i = readfile(browseFile, NULL,
7580 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7581 vim_free(browseFile);
7582 }
7583 else
7584 i = OK;
7585 }
7586 else
7587#endif
7588 if (*eap->arg == NUL)
7589 {
7590 if (check_fname() == FAIL) /* check for no file name */
7591 return;
7592 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7593 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7594 }
7595 else
7596 {
7597 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7598 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7599 i = readfile(eap->arg, NULL,
7600 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7601
7602 }
7603 if (i == FAIL)
7604 {
7605#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7606 if (!aborting())
7607#endif
7608 EMSG2(_(e_notopen), eap->arg);
7609 }
7610 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007611 {
7612 if (empty && exmode_active)
7613 {
7614 /* Delete the empty line that remains. Historically ex does
7615 * this but vi doesn't. */
7616 if (eap->line2 == 0)
7617 lnum = curbuf->b_ml.ml_line_count;
7618 else
7619 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007620 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007621 {
7622 ml_delete(lnum, FALSE);
7623 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007624 if (curwin->w_cursor.lnum > 1
7625 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007626 --curwin->w_cursor.lnum;
7627 }
7628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632}
7633
Bram Moolenaarea408852005-06-25 22:49:46 +00007634static char_u *prev_dir = NULL;
7635
7636#if defined(EXITFREE) || defined(PROTO)
7637 void
7638free_cd_dir()
7639{
7640 vim_free(prev_dir);
7641}
7642#endif
7643
7644
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645/*
7646 * ":cd", ":lcd", ":chdir" and ":lchdir".
7647 */
7648 static void
7649ex_cd(eap)
7650 exarg_T *eap;
7651{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 char_u *new_dir;
7653 char_u *tofree;
7654
7655 new_dir = eap->arg;
7656#if !defined(UNIX) && !defined(VMS)
7657 /* for non-UNIX ":cd" means: print current directory */
7658 if (*new_dir == NUL)
7659 ex_pwd(NULL);
7660 else
7661#endif
7662 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007663 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7664 && !eap->forceit)
7665 {
7666 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7667 return;
7668 }
7669
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 /* ":cd -": Change to previous directory */
7671 if (STRCMP(new_dir, "-") == 0)
7672 {
7673 if (prev_dir == NULL)
7674 {
7675 EMSG(_("E186: No previous directory"));
7676 return;
7677 }
7678 new_dir = prev_dir;
7679 }
7680
7681 /* Save current directory for next ":cd -" */
7682 tofree = prev_dir;
7683 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7684 prev_dir = vim_strsave(NameBuff);
7685 else
7686 prev_dir = NULL;
7687
7688#if defined(UNIX) || defined(VMS)
7689 /* for UNIX ":cd" means: go to home directory */
7690 if (*new_dir == NUL)
7691 {
7692 /* use NameBuff for home directory name */
7693# ifdef VMS
7694 char_u *p;
7695
7696 p = mch_getenv((char_u *)"SYS$LOGIN");
7697 if (p == NULL || *p == NUL) /* empty is the same as not set */
7698 NameBuff[0] = NUL;
7699 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007700 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701# else
7702 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7703# endif
7704 new_dir = NameBuff;
7705 }
7706#endif
7707 if (new_dir == NULL || vim_chdir(new_dir))
7708 EMSG(_(e_failed));
7709 else
7710 {
7711 vim_free(curwin->w_localdir);
7712 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7713 {
7714 /* If still in global directory, need to remember current
7715 * directory as global directory. */
7716 if (globaldir == NULL && prev_dir != NULL)
7717 globaldir = vim_strsave(prev_dir);
7718 /* Remember this local directory for the window. */
7719 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7720 curwin->w_localdir = vim_strsave(NameBuff);
7721 }
7722 else
7723 {
7724 /* We are now in the global directory, no need to remember its
7725 * name. */
7726 vim_free(globaldir);
7727 globaldir = NULL;
7728 curwin->w_localdir = NULL;
7729 }
7730
7731 shorten_fnames(TRUE);
7732
7733 /* Echo the new current directory if the command was typed. */
7734 if (KeyTyped)
7735 ex_pwd(eap);
7736 }
7737 vim_free(tofree);
7738 }
7739}
7740
7741/*
7742 * ":pwd".
7743 */
7744/*ARGSUSED*/
7745 static void
7746ex_pwd(eap)
7747 exarg_T *eap;
7748{
7749 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7750 {
7751#ifdef BACKSLASH_IN_FILENAME
7752 slash_adjust(NameBuff);
7753#endif
7754 msg(NameBuff);
7755 }
7756 else
7757 EMSG(_("E187: Unknown"));
7758}
7759
7760/*
7761 * ":=".
7762 */
7763 static void
7764ex_equal(eap)
7765 exarg_T *eap;
7766{
7767 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007768 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769}
7770
7771 static void
7772ex_sleep(eap)
7773 exarg_T *eap;
7774{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007775 int n;
7776 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777
7778 if (cursor_valid())
7779 {
7780 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7781 if (n >= 0)
7782 windgoto((int)n, curwin->w_wcol);
7783 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007784
7785 len = eap->line2;
7786 switch (*eap->arg)
7787 {
7788 case 'm': break;
7789 case NUL: len *= 1000L; break;
7790 default: EMSG2(_(e_invarg2), eap->arg); return;
7791 }
7792 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793}
7794
7795/*
7796 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7797 */
7798 void
7799do_sleep(msec)
7800 long msec;
7801{
7802 long done;
7803
7804 cursor_on();
7805 out_flush();
7806 for (done = 0; !got_int && done < msec; done += 1000L)
7807 {
7808 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7809 ui_breakcheck();
7810 }
7811}
7812
7813 static void
7814do_exmap(eap, isabbrev)
7815 exarg_T *eap;
7816 int isabbrev;
7817{
7818 int mode;
7819 char_u *cmdp;
7820
7821 cmdp = eap->cmd;
7822 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7823
7824 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7825 eap->arg, mode, isabbrev))
7826 {
7827 case 1: EMSG(_(e_invarg));
7828 break;
7829 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7830 break;
7831 }
7832}
7833
7834/*
7835 * ":winsize" command (obsolete).
7836 */
7837 static void
7838ex_winsize(eap)
7839 exarg_T *eap;
7840{
7841 int w, h;
7842 char_u *arg = eap->arg;
7843 char_u *p;
7844
7845 w = getdigits(&arg);
7846 arg = skipwhite(arg);
7847 p = arg;
7848 h = getdigits(&arg);
7849 if (*p != NUL && *arg == NUL)
7850 set_shellsize(w, h, TRUE);
7851 else
7852 EMSG(_("E465: :winsize requires two number arguments"));
7853}
7854
7855#ifdef FEAT_WINDOWS
7856 static void
7857ex_wincmd(eap)
7858 exarg_T *eap;
7859{
7860 int xchar = NUL;
7861 char_u *p;
7862
7863 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7864 {
7865 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7866 if (eap->arg[1] == NUL)
7867 {
7868 EMSG(_(e_invarg));
7869 return;
7870 }
7871 xchar = eap->arg[1];
7872 p = eap->arg + 2;
7873 }
7874 else
7875 p = eap->arg + 1;
7876
7877 eap->nextcmd = check_nextcmd(p);
7878 p = skipwhite(p);
7879 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7880 EMSG(_(e_invarg));
7881 else
7882 {
7883 /* Pass flags on for ":vertical wincmd ]". */
7884 postponed_split_flags = cmdmod.split;
7885 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7886 postponed_split_flags = 0;
7887 }
7888}
7889#endif
7890
Bram Moolenaar843ee412004-06-30 16:16:41 +00007891#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892/*
7893 * ":winpos".
7894 */
7895 static void
7896ex_winpos(eap)
7897 exarg_T *eap;
7898{
7899 int x, y;
7900 char_u *arg = eap->arg;
7901 char_u *p;
7902
7903 if (*arg == NUL)
7904 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007905# if defined(FEAT_GUI) || defined(MSWIN)
7906# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007908# else
7909 if (mch_get_winpos(&x, &y) != FAIL)
7910# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
7912 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7913 msg(IObuff);
7914 }
7915 else
7916# endif
7917 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7918 }
7919 else
7920 {
7921 x = getdigits(&arg);
7922 arg = skipwhite(arg);
7923 p = arg;
7924 y = getdigits(&arg);
7925 if (*p == NUL || *arg != NUL)
7926 {
7927 EMSG(_("E466: :winpos requires two number arguments"));
7928 return;
7929 }
7930# ifdef FEAT_GUI
7931 if (gui.in_use)
7932 gui_mch_set_winpos(x, y);
7933 else if (gui.starting)
7934 {
7935 /* Remember the coordinates for when the window is opened. */
7936 gui_win_x = x;
7937 gui_win_y = y;
7938 }
7939# ifdef HAVE_TGETENT
7940 else
7941# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007942# else
7943# ifdef MSWIN
7944 mch_set_winpos(x, y);
7945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946# endif
7947# ifdef HAVE_TGETENT
7948 if (*T_CWP)
7949 term_set_winpos(x, y);
7950# endif
7951 }
7952}
7953#endif
7954
7955/*
7956 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7957 */
7958 static void
7959ex_operators(eap)
7960 exarg_T *eap;
7961{
7962 oparg_T oa;
7963
7964 clear_oparg(&oa);
7965 oa.regname = eap->regname;
7966 oa.start.lnum = eap->line1;
7967 oa.end.lnum = eap->line2;
7968 oa.line_count = eap->line2 - eap->line1 + 1;
7969 oa.motion_type = MLINE;
7970#ifdef FEAT_VIRTUALEDIT
7971 virtual_op = FALSE;
7972#endif
7973 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7974 {
7975 setpcmark();
7976 curwin->w_cursor.lnum = eap->line1;
7977 beginline(BL_SOL | BL_FIX);
7978 }
7979
7980 switch (eap->cmdidx)
7981 {
7982 case CMD_delete:
7983 oa.op_type = OP_DELETE;
7984 op_delete(&oa);
7985 break;
7986
7987 case CMD_yank:
7988 oa.op_type = OP_YANK;
7989 (void)op_yank(&oa, FALSE, TRUE);
7990 break;
7991
7992 default: /* CMD_rshift or CMD_lshift */
7993 if ((eap->cmdidx == CMD_rshift)
7994#ifdef FEAT_RIGHTLEFT
7995 ^ curwin->w_p_rl
7996#endif
7997 )
7998 oa.op_type = OP_RSHIFT;
7999 else
8000 oa.op_type = OP_LSHIFT;
8001 op_shift(&oa, FALSE, eap->amount);
8002 break;
8003 }
8004#ifdef FEAT_VIRTUALEDIT
8005 virtual_op = MAYBE;
8006#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008007 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008}
8009
8010/*
8011 * ":put".
8012 */
8013 static void
8014ex_put(eap)
8015 exarg_T *eap;
8016{
8017 /* ":0put" works like ":1put!". */
8018 if (eap->line2 == 0)
8019 {
8020 eap->line2 = 1;
8021 eap->forceit = TRUE;
8022 }
8023 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008024 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8025 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026}
8027
8028/*
8029 * Handle ":copy" and ":move".
8030 */
8031 static void
8032ex_copymove(eap)
8033 exarg_T *eap;
8034{
8035 long n;
8036
8037 n = get_address(&eap->arg, FALSE, FALSE);
8038 if (eap->arg == NULL) /* error detected */
8039 {
8040 eap->nextcmd = NULL;
8041 return;
8042 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008043 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
8045 /*
8046 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8047 */
8048 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8049 {
8050 EMSG(_(e_invaddr));
8051 return;
8052 }
8053
8054 if (eap->cmdidx == CMD_move)
8055 {
8056 if (do_move(eap->line1, eap->line2, n) == FAIL)
8057 return;
8058 }
8059 else
8060 ex_copy(eap->line1, eap->line2, n);
8061 u_clearline();
8062 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008063 ex_may_print(eap);
8064}
8065
8066/*
8067 * Print the current line if flags were given to the Ex command.
8068 */
8069 static void
8070ex_may_print(eap)
8071 exarg_T *eap;
8072{
8073 if (eap->flags != 0)
8074 {
8075 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8076 (eap->flags & EXFLAG_LIST));
8077 ex_no_reprint = TRUE;
8078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079}
8080
8081/*
8082 * ":smagic" and ":snomagic".
8083 */
8084 static void
8085ex_submagic(eap)
8086 exarg_T *eap;
8087{
8088 int magic_save = p_magic;
8089
8090 p_magic = (eap->cmdidx == CMD_smagic);
8091 do_sub(eap);
8092 p_magic = magic_save;
8093}
8094
8095/*
8096 * ":join".
8097 */
8098 static void
8099ex_join(eap)
8100 exarg_T *eap;
8101{
8102 curwin->w_cursor.lnum = eap->line1;
8103 if (eap->line1 == eap->line2)
8104 {
8105 if (eap->addr_count >= 2) /* :2,2join does nothing */
8106 return;
8107 if (eap->line2 == curbuf->b_ml.ml_line_count)
8108 {
8109 beep_flush();
8110 return;
8111 }
8112 ++eap->line2;
8113 }
8114 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8115 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008116 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117}
8118
8119/*
8120 * ":[addr]@r" or ":[addr]*r": execute register
8121 */
8122 static void
8123ex_at(eap)
8124 exarg_T *eap;
8125{
8126 int c;
8127
8128 curwin->w_cursor.lnum = eap->line2;
8129
8130#ifdef USE_ON_FLY_SCROLL
8131 dont_scroll = TRUE; /* disallow scrolling here */
8132#endif
8133
8134 /* get the register name. No name means to use the previous one */
8135 c = *eap->arg;
8136 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8137 c = '@';
8138 /* put the register in mapbuf */
8139 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 else
8144 {
8145 int save_efr = exec_from_reg;
8146
8147 exec_from_reg = TRUE;
8148
8149 /*
8150 * Execute from the typeahead buffer.
8151 * Originally this didn't check for the typeahead buffer to be empty,
8152 * thus could read more Ex commands from stdin. It's not clear why,
8153 * it is certainly unexpected.
8154 */
8155 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8156 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8157
8158 exec_from_reg = save_efr;
8159 }
8160}
8161
8162/*
8163 * ":!".
8164 */
8165 static void
8166ex_bang(eap)
8167 exarg_T *eap;
8168{
8169 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8170}
8171
8172/*
8173 * ":undo".
8174 */
8175/*ARGSUSED*/
8176 static void
8177ex_undo(eap)
8178 exarg_T *eap;
8179{
8180 u_undo(1);
8181}
8182
8183/*
8184 * ":redo".
8185 */
8186/*ARGSUSED*/
8187 static void
8188ex_redo(eap)
8189 exarg_T *eap;
8190{
8191 u_redo(1);
8192}
8193
8194/*
8195 * ":redir": start/stop redirection.
8196 */
8197 static void
8198ex_redir(eap)
8199 exarg_T *eap;
8200{
8201 char *mode;
8202 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008203 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
8205 if (STRICMP(eap->arg, "END") == 0)
8206 close_redir();
8207 else
8208 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008209 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008211 ++arg;
8212 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008214 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 mode = "a";
8216 }
8217 else
8218 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008219 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220
8221 close_redir();
8222
8223 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008224 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 if (fname == NULL)
8226 return;
8227#ifdef FEAT_BROWSE
8228 if (cmdmod.browse)
8229 {
8230 char_u *browseFile;
8231
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008232 browseFile = do_browse(BROWSE_SAVE,
8233 (char_u *)_("Save Redirection"),
8234 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 if (browseFile == NULL)
8236 return; /* operation cancelled */
8237 vim_free(fname);
8238 fname = browseFile;
8239 eap->forceit = TRUE; /* since dialog already asked */
8240 }
8241#endif
8242
8243 redir_fd = open_exfile(fname, eap->forceit, mode);
8244 vim_free(fname);
8245 }
8246#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008247 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
8249 /* redirect to a register a-z (resp. A-Z for appending) */
8250 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008251 ++arg;
8252 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008254 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008256 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008258 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008259 if (*arg == '>' && arg[1] == '>')
8260 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008261 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8262 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008264 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008266 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008268 if (*arg == '>')
8269 arg++;
8270
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 /* make register empty */
8272 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8273 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008274 }
8275 if (*arg != NUL)
8276 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008277 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008278 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008279 }
8280 }
8281 else if (*arg == '=' && arg[1] == '>')
8282 {
8283 int append;
8284
8285 /* redirect to a variable */
8286 close_redir();
8287 arg += 2;
8288
8289 if (*arg == '>')
8290 {
8291 ++arg;
8292 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 }
8294 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008295 append = FALSE;
8296
8297 if (var_redir_start(skipwhite(arg), append) == OK)
8298 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 }
8300#endif
8301
8302 /* TODO: redirect to a buffer */
8303
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008305 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307}
8308
8309/*
8310 * ":redraw": force redraw
8311 */
8312 static void
8313ex_redraw(eap)
8314 exarg_T *eap;
8315{
8316 int r = RedrawingDisabled;
8317 int p = p_lz;
8318
8319 RedrawingDisabled = 0;
8320 p_lz = FALSE;
8321 update_topline();
8322 update_screen(eap->forceit ? CLEAR :
8323#ifdef FEAT_VISUAL
8324 VIsual_active ? INVERTED :
8325#endif
8326 0);
8327#ifdef FEAT_TITLE
8328 if (need_maketitle)
8329 maketitle();
8330#endif
8331 RedrawingDisabled = r;
8332 p_lz = p;
8333
8334 /* Reset msg_didout, so that a message that's there is overwritten. */
8335 msg_didout = FALSE;
8336 msg_col = 0;
8337
8338 out_flush();
8339}
8340
8341/*
8342 * ":redrawstatus": force redraw of status line(s)
8343 */
8344/*ARGSUSED*/
8345 static void
8346ex_redrawstatus(eap)
8347 exarg_T *eap;
8348{
8349#if defined(FEAT_WINDOWS)
8350 int r = RedrawingDisabled;
8351 int p = p_lz;
8352
8353 RedrawingDisabled = 0;
8354 p_lz = FALSE;
8355 if (eap->forceit)
8356 status_redraw_all();
8357 else
8358 status_redraw_curbuf();
8359 update_screen(
8360# ifdef FEAT_VISUAL
8361 VIsual_active ? INVERTED :
8362# endif
8363 0);
8364 RedrawingDisabled = r;
8365 p_lz = p;
8366 out_flush();
8367#endif
8368}
8369
8370 static void
8371close_redir()
8372{
8373 if (redir_fd != NULL)
8374 {
8375 fclose(redir_fd);
8376 redir_fd = NULL;
8377 }
8378#ifdef FEAT_EVAL
8379 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008380 if (redir_vname)
8381 {
8382 var_redir_stop();
8383 redir_vname = 0;
8384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385#endif
8386}
8387
8388#if defined(FEAT_SESSION) && defined(USE_CRNL)
8389# define MKSESSION_NL
8390static int mksession_nl = FALSE; /* use NL only in put_eol() */
8391#endif
8392
8393/*
8394 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8395 */
8396 static void
8397ex_mkrc(eap)
8398 exarg_T *eap;
8399{
8400 FILE *fd;
8401 int failed = FALSE;
8402 char_u *fname;
8403#ifdef FEAT_BROWSE
8404 char_u *browseFile = NULL;
8405#endif
8406#ifdef FEAT_SESSION
8407 int view_session = FALSE;
8408 int using_vdir = FALSE; /* using 'viewdir'? */
8409 char_u *viewFile = NULL;
8410 unsigned *flagp;
8411#endif
8412
8413 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8414 {
8415#ifdef FEAT_SESSION
8416 view_session = TRUE;
8417#else
8418 ex_ni(eap);
8419 return;
8420#endif
8421 }
8422
8423#ifdef FEAT_SESSION
8424 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8425 if (eap->cmdidx == CMD_mkview
8426 && (*eap->arg == NUL
8427 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8428 {
8429 eap->forceit = TRUE;
8430 fname = get_view_file(*eap->arg);
8431 if (fname == NULL)
8432 return;
8433 viewFile = fname;
8434 using_vdir = TRUE;
8435 }
8436 else
8437#endif
8438 if (*eap->arg != NUL)
8439 fname = eap->arg;
8440 else if (eap->cmdidx == CMD_mkvimrc)
8441 fname = (char_u *)VIMRC_FILE;
8442#ifdef FEAT_SESSION
8443 else if (eap->cmdidx == CMD_mksession)
8444 fname = (char_u *)SESSION_FILE;
8445#endif
8446 else
8447 fname = (char_u *)EXRC_FILE;
8448
8449#ifdef FEAT_BROWSE
8450 if (cmdmod.browse)
8451 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008452 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453# ifdef FEAT_SESSION
8454 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8455 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8456# endif
8457 (char_u *)_("Save Setup"),
8458 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8459 if (browseFile == NULL)
8460 goto theend;
8461 fname = browseFile;
8462 eap->forceit = TRUE; /* since dialog already asked */
8463 }
8464#endif
8465
8466#if defined(FEAT_SESSION) && defined(vim_mkdir)
8467 /* When using 'viewdir' may have to create the directory. */
8468 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008469 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470#endif
8471
8472 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8473 if (fd != NULL)
8474 {
8475#ifdef FEAT_SESSION
8476 if (eap->cmdidx == CMD_mkview)
8477 flagp = &vop_flags;
8478 else
8479 flagp = &ssop_flags;
8480#endif
8481
8482#ifdef MKSESSION_NL
8483 /* "unix" in 'sessionoptions': use NL line separator */
8484 if (view_session && (*flagp & SSOP_UNIX))
8485 mksession_nl = TRUE;
8486#endif
8487
8488 /* Write the version command for :mkvimrc */
8489 if (eap->cmdidx == CMD_mkvimrc)
8490 (void)put_line(fd, "version 6.0");
8491
8492#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008493 if (eap->cmdidx == CMD_mksession)
8494 {
8495 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8496 failed = TRUE;
8497 }
8498
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (eap->cmdidx != CMD_mkview)
8500#endif
8501 {
8502 /* Write setting 'compatible' first, because it has side effects.
8503 * For that same reason only do it when needed. */
8504 if (p_cp)
8505 (void)put_line(fd, "if !&cp | set cp | endif");
8506 else
8507 (void)put_line(fd, "if &cp | set nocp | endif");
8508 }
8509
8510#ifdef FEAT_SESSION
8511 if (!view_session
8512 || (eap->cmdidx == CMD_mksession
8513 && (*flagp & SSOP_OPTIONS)))
8514#endif
8515 failed |= (makemap(fd, NULL) == FAIL
8516 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8517
8518#ifdef FEAT_SESSION
8519 if (!failed && view_session)
8520 {
8521 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8522 failed = TRUE;
8523 if (eap->cmdidx == CMD_mksession)
8524 {
8525 char_u dirnow[MAXPATHL]; /* current directory */
8526
8527 /*
8528 * Change to session file's dir.
8529 */
8530 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8531 || mch_chdir((char *)dirnow) != 0)
8532 *dirnow = NUL;
8533 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8534 {
8535 if (vim_chdirfile(fname) == OK)
8536 shorten_fnames(TRUE);
8537 }
8538 else if (*dirnow != NUL
8539 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8540 {
8541 (void)mch_chdir((char *)globaldir);
8542 shorten_fnames(TRUE);
8543 }
8544
8545 failed |= (makeopens(fd, dirnow) == FAIL);
8546
8547 /* restore original dir */
8548 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8549 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8550 {
8551 if (mch_chdir((char *)dirnow) != 0)
8552 EMSG(_(e_prev_dir));
8553 shorten_fnames(TRUE);
8554 }
8555 }
8556 else
8557 {
8558 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8559 }
8560 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8561 == FAIL)
8562 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008563 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8564 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008565 if (eap->cmdidx == CMD_mksession)
8566 {
8567 if (put_line(fd, "unlet SessionLoad") == FAIL)
8568 failed = TRUE;
8569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 }
8571#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008572 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8573 failed = TRUE;
8574
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 failed |= fclose(fd);
8576
8577 if (failed)
8578 EMSG(_(e_write));
8579#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8580 else if (eap->cmdidx == CMD_mksession)
8581 {
8582 /* successful session write - set this_session var */
8583 char_u tbuf[MAXPATHL];
8584
8585 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8586 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8587 }
8588#endif
8589#ifdef MKSESSION_NL
8590 mksession_nl = FALSE;
8591#endif
8592 }
8593
8594#ifdef FEAT_BROWSE
8595theend:
8596 vim_free(browseFile);
8597#endif
8598#ifdef FEAT_SESSION
8599 vim_free(viewFile);
8600#endif
8601}
8602
Bram Moolenaardf177f62005-02-22 08:39:57 +00008603#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8604 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008605/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008606 int
8607vim_mkdir_emsg(name, prot)
8608 char_u *name;
8609 int prot;
8610{
8611 if (vim_mkdir(name, prot) != 0)
8612 {
8613 EMSG2(_("E739: Cannot create directory: %s"), name);
8614 return FAIL;
8615 }
8616 return OK;
8617}
8618#endif
8619
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620/*
8621 * Open a file for writing for an Ex command, with some checks.
8622 * Return file descriptor, or NULL on failure.
8623 */
8624 FILE *
8625open_exfile(fname, forceit, mode)
8626 char_u *fname;
8627 int forceit;
8628 char *mode; /* "w" for create new file or "a" for append */
8629{
8630 FILE *fd;
8631
8632#ifdef UNIX
8633 /* with Unix it is possible to open a directory */
8634 if (mch_isdir(fname))
8635 {
8636 EMSG2(_(e_isadir2), fname);
8637 return NULL;
8638 }
8639#endif
8640 if (!forceit && *mode != 'a' && vim_fexists(fname))
8641 {
8642 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8643 return NULL;
8644 }
8645
8646 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8647 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8648
8649 return fd;
8650}
8651
8652/*
8653 * ":mark" and ":k".
8654 */
8655 static void
8656ex_mark(eap)
8657 exarg_T *eap;
8658{
8659 pos_T pos;
8660
8661 if (*eap->arg == NUL) /* No argument? */
8662 EMSG(_(e_argreq));
8663 else if (eap->arg[1] != NUL) /* more than one character? */
8664 EMSG(_(e_trailing));
8665 else
8666 {
8667 pos = curwin->w_cursor; /* save curwin->w_cursor */
8668 curwin->w_cursor.lnum = eap->line2;
8669 beginline(BL_WHITE | BL_FIX);
8670 if (setmark(*eap->arg) == FAIL) /* set mark */
8671 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8672 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8673 }
8674}
8675
8676/*
8677 * Update w_topline, w_leftcol and the cursor position.
8678 */
8679 void
8680update_topline_cursor()
8681{
8682 check_cursor(); /* put cursor on valid line */
8683 update_topline();
8684 if (!curwin->w_p_wrap)
8685 validate_cursor();
8686 update_curswant();
8687}
8688
8689#ifdef FEAT_EX_EXTRA
8690/*
8691 * ":normal[!] {commands}": Execute normal mode commands.
8692 */
8693 static void
8694ex_normal(eap)
8695 exarg_T *eap;
8696{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 int save_msg_scroll = msg_scroll;
8698 int save_restart_edit = restart_edit;
8699 int save_msg_didout = msg_didout;
8700 int save_State = State;
8701 tasave_T tabuf;
8702 int save_insertmode = p_im;
8703 int save_finish_op = finish_op;
8704#ifdef FEAT_MBYTE
8705 char_u *arg = NULL;
8706 int l;
8707 char_u *p;
8708#endif
8709
8710 if (ex_normal_busy >= p_mmd)
8711 {
8712 EMSG(_("E192: Recursive use of :normal too deep"));
8713 return;
8714 }
8715 ++ex_normal_busy;
8716
8717 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8718 restart_edit = 0; /* don't go to Insert mode */
8719 p_im = FALSE; /* don't use 'insertmode' */
8720
8721#ifdef FEAT_MBYTE
8722 /*
8723 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8724 * this for the K_SPECIAL leading byte, otherwise special keys will not
8725 * work.
8726 */
8727 if (has_mbyte)
8728 {
8729 int len = 0;
8730
8731 /* Count the number of characters to be escaped. */
8732 for (p = eap->arg; *p != NUL; ++p)
8733 {
8734# ifdef FEAT_GUI
8735 if (*p == CSI) /* leadbyte CSI */
8736 len += 2;
8737# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008738 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8740# ifdef FEAT_GUI
8741 || *p == CSI
8742# endif
8743 )
8744 len += 2;
8745 }
8746 if (len > 0)
8747 {
8748 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8749 if (arg != NULL)
8750 {
8751 len = 0;
8752 for (p = eap->arg; *p != NUL; ++p)
8753 {
8754 arg[len++] = *p;
8755# ifdef FEAT_GUI
8756 if (*p == CSI)
8757 {
8758 arg[len++] = KS_EXTRA;
8759 arg[len++] = (int)KE_CSI;
8760 }
8761# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008762 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 {
8764 arg[len++] = *++p;
8765 if (*p == K_SPECIAL)
8766 {
8767 arg[len++] = KS_SPECIAL;
8768 arg[len++] = KE_FILLER;
8769 }
8770# ifdef FEAT_GUI
8771 else if (*p == CSI)
8772 {
8773 arg[len++] = KS_EXTRA;
8774 arg[len++] = (int)KE_CSI;
8775 }
8776# endif
8777 }
8778 arg[len] = NUL;
8779 }
8780 }
8781 }
8782 }
8783#endif
8784
8785 /*
8786 * Save the current typeahead. This is required to allow using ":normal"
8787 * from an event handler and makes sure we don't hang when the argument
8788 * ends with half a command.
8789 */
8790 save_typeahead(&tabuf);
8791 if (tabuf.typebuf_valid)
8792 {
8793 /*
8794 * Repeat the :normal command for each line in the range. When no
8795 * range given, execute it just once, without positioning the cursor
8796 * first.
8797 */
8798 do
8799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 if (eap->addr_count != 0)
8801 {
8802 curwin->w_cursor.lnum = eap->line1++;
8803 curwin->w_cursor.col = 0;
8804 }
8805
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008806 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807#ifdef FEAT_MBYTE
8808 arg != NULL ? arg :
8809#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008810 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 }
8812 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8813 }
8814
8815 /* Might not return to the main loop when in an event handler. */
8816 update_topline_cursor();
8817
8818 /* Restore the previous typeahead. */
8819 restore_typeahead(&tabuf);
8820
8821 --ex_normal_busy;
8822 msg_scroll = save_msg_scroll;
8823 restart_edit = save_restart_edit;
8824 p_im = save_insertmode;
8825 finish_op = save_finish_op;
8826 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8827
8828 /* Restore the state (needed when called from a function executed for
8829 * 'indentexpr'). */
8830 State = save_State;
8831#ifdef FEAT_MBYTE
8832 vim_free(arg);
8833#endif
8834}
8835
8836/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008837 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 */
8839 static void
8840ex_startinsert(eap)
8841 exarg_T *eap;
8842{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008843 if (eap->forceit)
8844 {
8845 coladvance((colnr_T)MAXCOL);
8846 curwin->w_curswant = MAXCOL;
8847 curwin->w_set_curswant = FALSE;
8848 }
8849
Bram Moolenaara40c5002005-01-09 21:16:21 +00008850 /* Ignore the command when already in Insert mode. Inserting an
8851 * expression register that invokes a function can do this. */
8852 if (State & INSERT)
8853 return;
8854
Bram Moolenaar64969662005-12-14 21:59:55 +00008855 if (eap->cmdidx == CMD_startinsert)
8856 restart_edit = 'a';
8857 else if (eap->cmdidx == CMD_startreplace)
8858 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008860 restart_edit = 'V';
8861
8862 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008864 if (eap->cmdidx == CMD_startinsert)
8865 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 curwin->w_curswant = 0; /* avoid MAXCOL */
8867 }
8868}
8869
8870/*
8871 * ":stopinsert"
8872 */
8873/*ARGSUSED*/
8874 static void
8875ex_stopinsert(eap)
8876 exarg_T *eap;
8877{
8878 restart_edit = 0;
8879 stop_insert_mode = TRUE;
8880}
8881#endif
8882
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008883#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8884/*
8885 * Execute normal mode command "cmd".
8886 * "remap" can be REMAP_NONE or REMAP_YES.
8887 */
8888 void
8889exec_normal_cmd(cmd, remap, silent)
8890 char_u *cmd;
8891 int remap;
8892 int silent;
8893{
8894 oparg_T oa;
8895
8896 /*
8897 * Stuff the argument into the typeahead buffer.
8898 * Execute normal_cmd() until there is no typeahead left.
8899 */
8900 clear_oparg(&oa);
8901 finish_op = FALSE;
8902 ins_typebuf(cmd, remap, 0, TRUE, silent);
8903 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8904 && !got_int)
8905 {
8906 update_topline_cursor();
8907 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8908 }
8909}
8910#endif
8911
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912#ifdef FEAT_FIND_ID
8913 static void
8914ex_checkpath(eap)
8915 exarg_T *eap;
8916{
8917 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8918 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8919 (linenr_T)1, (linenr_T)MAXLNUM);
8920}
8921
8922#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8923/*
8924 * ":psearch"
8925 */
8926 static void
8927ex_psearch(eap)
8928 exarg_T *eap;
8929{
8930 g_do_tagpreview = p_pvh;
8931 ex_findpat(eap);
8932 g_do_tagpreview = 0;
8933}
8934#endif
8935
8936 static void
8937ex_findpat(eap)
8938 exarg_T *eap;
8939{
8940 int whole = TRUE;
8941 long n;
8942 char_u *p;
8943 int action;
8944
8945 switch (cmdnames[eap->cmdidx].cmd_name[2])
8946 {
8947 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8948 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8949 action = ACTION_GOTO;
8950 else
8951 action = ACTION_SHOW;
8952 break;
8953 case 'i': /* ":ilist" and ":dlist" */
8954 action = ACTION_SHOW_ALL;
8955 break;
8956 case 'u': /* ":ijump" and ":djump" */
8957 action = ACTION_GOTO;
8958 break;
8959 default: /* ":isplit" and ":dsplit" */
8960 action = ACTION_SPLIT;
8961 break;
8962 }
8963
8964 n = 1;
8965 if (vim_isdigit(*eap->arg)) /* get count */
8966 {
8967 n = getdigits(&eap->arg);
8968 eap->arg = skipwhite(eap->arg);
8969 }
8970 if (*eap->arg == '/') /* Match regexp, not just whole words */
8971 {
8972 whole = FALSE;
8973 ++eap->arg;
8974 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8975 if (*p)
8976 {
8977 *p++ = NUL;
8978 p = skipwhite(p);
8979
8980 /* Check for trailing illegal characters */
8981 if (!ends_excmd(*p))
8982 eap->errmsg = e_trailing;
8983 else
8984 eap->nextcmd = check_nextcmd(p);
8985 }
8986 }
8987 if (!eap->skip)
8988 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8989 whole, !eap->forceit,
8990 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8991 n, action, eap->line1, eap->line2);
8992}
8993#endif
8994
8995#ifdef FEAT_WINDOWS
8996
8997# ifdef FEAT_QUICKFIX
8998/*
8999 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9000 */
9001 static void
9002ex_ptag(eap)
9003 exarg_T *eap;
9004{
9005 g_do_tagpreview = p_pvh;
9006 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9007}
9008
9009/*
9010 * ":pedit"
9011 */
9012 static void
9013ex_pedit(eap)
9014 exarg_T *eap;
9015{
9016 win_T *curwin_save = curwin;
9017
9018 g_do_tagpreview = p_pvh;
9019 prepare_tagpreview();
9020 keep_help_flag = curwin_save->w_buffer->b_help;
9021 do_exedit(eap, NULL);
9022 keep_help_flag = FALSE;
9023 if (curwin != curwin_save && win_valid(curwin_save))
9024 {
9025 /* Return cursor to where we were */
9026 validate_cursor();
9027 redraw_later(VALID);
9028 win_enter(curwin_save, TRUE);
9029 }
9030 g_do_tagpreview = 0;
9031}
9032# endif
9033
9034/*
9035 * ":stag", ":stselect" and ":stjump".
9036 */
9037 static void
9038ex_stag(eap)
9039 exarg_T *eap;
9040{
9041 postponed_split = -1;
9042 postponed_split_flags = cmdmod.split;
9043 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9044 postponed_split_flags = 0;
9045}
9046#endif
9047
9048/*
9049 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9050 */
9051 static void
9052ex_tag(eap)
9053 exarg_T *eap;
9054{
9055 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9056}
9057
9058 static void
9059ex_tag_cmd(eap, name)
9060 exarg_T *eap;
9061 char_u *name;
9062{
9063 int cmd;
9064
9065 switch (name[1])
9066 {
9067 case 'j': cmd = DT_JUMP; /* ":tjump" */
9068 break;
9069 case 's': cmd = DT_SELECT; /* ":tselect" */
9070 break;
9071 case 'p': cmd = DT_PREV; /* ":tprevious" */
9072 break;
9073 case 'N': cmd = DT_PREV; /* ":tNext" */
9074 break;
9075 case 'n': cmd = DT_NEXT; /* ":tnext" */
9076 break;
9077 case 'o': cmd = DT_POP; /* ":pop" */
9078 break;
9079 case 'f': /* ":tfirst" */
9080 case 'r': cmd = DT_FIRST; /* ":trewind" */
9081 break;
9082 case 'l': cmd = DT_LAST; /* ":tlast" */
9083 break;
9084 default: /* ":tag" */
9085#ifdef FEAT_CSCOPE
9086 if (p_cst)
9087 {
9088 do_cstag(eap);
9089 return;
9090 }
9091#endif
9092 cmd = DT_TAG;
9093 break;
9094 }
9095
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009096 if (name[0] == 'l')
9097 {
9098#ifndef FEAT_QUICKFIX
9099 ex_ni(eap);
9100 return;
9101#else
9102 cmd = DT_LTAG;
9103#endif
9104 }
9105
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9107 eap->forceit, TRUE);
9108}
9109
9110/*
9111 * Evaluate cmdline variables.
9112 *
9113 * change '%' to curbuf->b_ffname
9114 * '#' to curwin->w_altfile
9115 * '<cword>' to word under the cursor
9116 * '<cWORD>' to WORD under the cursor
9117 * '<cfile>' to path name under the cursor
9118 * '<sfile>' to sourced file name
9119 * '<afile>' to file name for autocommand
9120 * '<abuf>' to buffer number for autocommand
9121 * '<amatch>' to matching name for autocommand
9122 *
9123 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9124 * "" for error without a message) and NULL is returned.
9125 * Returns an allocated string if a valid match was found.
9126 * Returns NULL if no match was found. "usedlen" then still contains the
9127 * number of characters to skip.
9128 */
9129 char_u *
9130eval_vars(src, usedlen, lnump, errormsg, srcstart)
9131 char_u *src; /* pointer into commandline */
9132 int *usedlen; /* characters after src that are used */
9133 linenr_T *lnump; /* line number for :e command, or NULL */
9134 char_u **errormsg; /* pointer to error message */
9135 char_u *srcstart; /* beginning of valid memory for src */
9136{
9137 int i;
9138 char_u *s;
9139 char_u *result;
9140 char_u *resultbuf = NULL;
9141 int resultlen;
9142 buf_T *buf;
9143 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9144 int spec_idx;
9145#ifdef FEAT_MODIFY_FNAME
9146 int skip_mod = FALSE;
9147#endif
9148 static char *(spec_str[]) =
9149 {
9150 "%",
9151#define SPEC_PERC 0
9152 "#",
9153#define SPEC_HASH 1
9154 "<cword>", /* cursor word */
9155#define SPEC_CWORD 2
9156 "<cWORD>", /* cursor WORD */
9157#define SPEC_CCWORD 3
9158 "<cfile>", /* cursor path name */
9159#define SPEC_CFILE 4
9160 "<sfile>", /* ":so" file name */
9161#define SPEC_SFILE 5
9162#ifdef FEAT_AUTOCMD
9163 "<afile>", /* autocommand file name */
9164# define SPEC_AFILE 6
9165 "<abuf>", /* autocommand buffer number */
9166# define SPEC_ABUF 7
9167 "<amatch>", /* autocommand match name */
9168# define SPEC_AMATCH 8
9169#endif
9170#ifdef FEAT_CLIENTSERVER
9171 "<client>"
9172# define SPEC_CLIENT 9
9173#endif
9174 };
9175#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9176
9177#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9178 char_u strbuf[30];
9179#endif
9180
9181 *errormsg = NULL;
9182
9183 /*
9184 * Check if there is something to do.
9185 */
9186 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9187 {
9188 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9189 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9190 break;
9191 }
9192 if (spec_idx == SPEC_COUNT) /* no match */
9193 {
9194 *usedlen = 1;
9195 return NULL;
9196 }
9197
9198 /*
9199 * Skip when preceded with a backslash "\%" and "\#".
9200 * Note: In "\\%" the % is also not recognized!
9201 */
9202 if (src > srcstart && src[-1] == '\\')
9203 {
9204 *usedlen = 0;
9205 STRCPY(src - 1, src); /* remove backslash */
9206 return NULL;
9207 }
9208
9209 /*
9210 * word or WORD under cursor
9211 */
9212 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9213 {
9214 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9215 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9216 if (resultlen == 0)
9217 {
9218 *errormsg = (char_u *)"";
9219 return NULL;
9220 }
9221 }
9222
9223 /*
9224 * '#': Alternate file name
9225 * '%': Current file name
9226 * File name under the cursor
9227 * File name for autocommand
9228 * and following modifiers
9229 */
9230 else
9231 {
9232 switch (spec_idx)
9233 {
9234 case SPEC_PERC: /* '%': current file */
9235 if (curbuf->b_fname == NULL)
9236 {
9237 result = (char_u *)"";
9238 valid = 0; /* Must have ":p:h" to be valid */
9239 }
9240 else
9241#ifdef RISCOS
9242 /* Always use the full path for RISC OS if possible. */
9243 result = curbuf->b_ffname;
9244 if (result == NULL)
9245 result = curbuf->b_fname;
9246#else
9247 result = curbuf->b_fname;
9248#endif
9249 break;
9250
9251 case SPEC_HASH: /* '#' or "#99": alternate file */
9252 if (src[1] == '#') /* "##": the argument list */
9253 {
9254 result = arg_all();
9255 resultbuf = result;
9256 *usedlen = 2;
9257#ifdef FEAT_MODIFY_FNAME
9258 skip_mod = TRUE;
9259#endif
9260 break;
9261 }
9262 s = src + 1;
9263 i = (int)getdigits(&s);
9264 *usedlen = (int)(s - src); /* length of what we expand */
9265
9266 buf = buflist_findnr(i);
9267 if (buf == NULL)
9268 {
9269 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9270 return NULL;
9271 }
9272 if (lnump != NULL)
9273 *lnump = ECMD_LAST;
9274 if (buf->b_fname == NULL)
9275 {
9276 result = (char_u *)"";
9277 valid = 0; /* Must have ":p:h" to be valid */
9278 }
9279 else
9280 result = buf->b_fname;
9281 break;
9282
9283#ifdef FEAT_SEARCHPATH
9284 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009285 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 if (result == NULL)
9287 {
9288 *errormsg = (char_u *)"";
9289 return NULL;
9290 }
9291 resultbuf = result; /* remember allocated string */
9292 break;
9293#endif
9294
9295#ifdef FEAT_AUTOCMD
9296 case SPEC_AFILE: /* file name for autocommand */
9297 result = autocmd_fname;
9298 if (result == NULL)
9299 {
9300 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9301 return NULL;
9302 }
9303 break;
9304
9305 case SPEC_ABUF: /* buffer number for autocommand */
9306 if (autocmd_bufnr <= 0)
9307 {
9308 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9309 return NULL;
9310 }
9311 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9312 result = strbuf;
9313 break;
9314
9315 case SPEC_AMATCH: /* match name for autocommand */
9316 result = autocmd_match;
9317 if (result == NULL)
9318 {
9319 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9320 return NULL;
9321 }
9322 break;
9323
9324#endif
9325 case SPEC_SFILE: /* file name for ":so" command */
9326 result = sourcing_name;
9327 if (result == NULL)
9328 {
9329 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9330 return NULL;
9331 }
9332 break;
9333#if defined(FEAT_CLIENTSERVER)
9334 case SPEC_CLIENT: /* Source of last submitted input */
9335 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9336 result = strbuf;
9337 break;
9338#endif
9339 }
9340
9341 resultlen = (int)STRLEN(result); /* length of new string */
9342 if (src[*usedlen] == '<') /* remove the file name extension */
9343 {
9344 ++*usedlen;
9345#ifdef RISCOS
9346 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9347#else
9348 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9349#endif
9350 resultlen = (int)(s - result);
9351 }
9352#ifdef FEAT_MODIFY_FNAME
9353 else if (!skip_mod)
9354 {
9355 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9356 &resultlen);
9357 if (result == NULL)
9358 {
9359 *errormsg = (char_u *)"";
9360 return NULL;
9361 }
9362 }
9363#endif
9364 }
9365
9366 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9367 {
9368 if (valid != VALID_HEAD + VALID_PATH)
9369 /* xgettext:no-c-format */
9370 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9371 else
9372 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9373 result = NULL;
9374 }
9375 else
9376 result = vim_strnsave(result, resultlen);
9377 vim_free(resultbuf);
9378 return result;
9379}
9380
9381/*
9382 * Concatenate all files in the argument list, separated by spaces, and return
9383 * it in one allocated string.
9384 * Spaces and backslashes in the file names are escaped with a backslash.
9385 * Returns NULL when out of memory.
9386 */
9387 static char_u *
9388arg_all()
9389{
9390 int len;
9391 int idx;
9392 char_u *retval = NULL;
9393 char_u *p;
9394
9395 /*
9396 * Do this loop two times:
9397 * first time: compute the total length
9398 * second time: concatenate the names
9399 */
9400 for (;;)
9401 {
9402 len = 0;
9403 for (idx = 0; idx < ARGCOUNT; ++idx)
9404 {
9405 p = alist_name(&ARGLIST[idx]);
9406 if (p != NULL)
9407 {
9408 if (len > 0)
9409 {
9410 /* insert a space in between names */
9411 if (retval != NULL)
9412 retval[len] = ' ';
9413 ++len;
9414 }
9415 for ( ; *p != NUL; ++p)
9416 {
9417 if (*p == ' ' || *p == '\\')
9418 {
9419 /* insert a backslash */
9420 if (retval != NULL)
9421 retval[len] = '\\';
9422 ++len;
9423 }
9424 if (retval != NULL)
9425 retval[len] = *p;
9426 ++len;
9427 }
9428 }
9429 }
9430
9431 /* second time: break here */
9432 if (retval != NULL)
9433 {
9434 retval[len] = NUL;
9435 break;
9436 }
9437
9438 /* allocate memory */
9439 retval = alloc(len + 1);
9440 if (retval == NULL)
9441 break;
9442 }
9443
9444 return retval;
9445}
9446
9447#if defined(FEAT_AUTOCMD) || defined(PROTO)
9448/*
9449 * Expand the <sfile> string in "arg".
9450 *
9451 * Returns an allocated string, or NULL for any error.
9452 */
9453 char_u *
9454expand_sfile(arg)
9455 char_u *arg;
9456{
9457 char_u *errormsg;
9458 int len;
9459 char_u *result;
9460 char_u *newres;
9461 char_u *repl;
9462 int srclen;
9463 char_u *p;
9464
9465 result = vim_strsave(arg);
9466 if (result == NULL)
9467 return NULL;
9468
9469 for (p = result; *p; )
9470 {
9471 if (STRNCMP(p, "<sfile>", 7) != 0)
9472 ++p;
9473 else
9474 {
9475 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9476 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9477 if (errormsg != NULL)
9478 {
9479 if (*errormsg)
9480 emsg(errormsg);
9481 vim_free(result);
9482 return NULL;
9483 }
9484 if (repl == NULL) /* no match (cannot happen) */
9485 {
9486 p += srclen;
9487 continue;
9488 }
9489 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9490 newres = alloc(len);
9491 if (newres == NULL)
9492 {
9493 vim_free(repl);
9494 vim_free(result);
9495 return NULL;
9496 }
9497 mch_memmove(newres, result, (size_t)(p - result));
9498 STRCPY(newres + (p - result), repl);
9499 len = (int)STRLEN(newres);
9500 STRCAT(newres, p + srclen);
9501 vim_free(repl);
9502 vim_free(result);
9503 result = newres;
9504 p = newres + len; /* continue after the match */
9505 }
9506 }
9507
9508 return result;
9509}
9510#endif
9511
9512#ifdef FEAT_SESSION
9513static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9514static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9515static frame_T *ses_skipframe __ARGS((frame_T *fr));
9516static int ses_do_frame __ARGS((frame_T *fr));
9517static int ses_do_win __ARGS((win_T *wp));
9518static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9519static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9520static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9521
9522/*
9523 * Write openfile commands for the current buffers to an .exrc file.
9524 * Return FAIL on error, OK otherwise.
9525 */
9526 static int
9527makeopens(fd, dirnow)
9528 FILE *fd;
9529 char_u *dirnow; /* Current directory name */
9530{
9531 buf_T *buf;
9532 int only_save_windows = TRUE;
9533 int nr;
9534 int cnr = 1;
9535 int restore_size = TRUE;
9536 win_T *wp;
9537 char_u *sname;
9538 win_T *edited_win = NULL;
9539
9540 if (ssop_flags & SSOP_BUFFERS)
9541 only_save_windows = FALSE; /* Save ALL buffers */
9542
9543 /*
9544 * Begin by setting the this_session variable, and then other
9545 * sessionable variables.
9546 */
9547#ifdef FEAT_EVAL
9548 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9549 return FAIL;
9550 if (ssop_flags & SSOP_GLOBALS)
9551 if (store_session_globals(fd) == FAIL)
9552 return FAIL;
9553#endif
9554
9555 /*
9556 * Close all windows but one.
9557 */
9558 if (put_line(fd, "silent only") == FAIL)
9559 return FAIL;
9560
9561 /*
9562 * Now a :cd command to the session directory or the current directory
9563 */
9564 if (ssop_flags & SSOP_SESDIR)
9565 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009566 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9567 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 return FAIL;
9569 }
9570 else if (ssop_flags & SSOP_CURDIR)
9571 {
9572 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9573 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009574 || fputs("cd ", fd) < 0
9575 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9576 || put_eol(fd) == FAIL)
9577 {
9578 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 vim_free(sname);
9582 }
9583
9584 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009585 * If there is an empty, unnamed buffer we will wipe it out later.
9586 * Remember the buffer number.
9587 */
9588 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9589 return FAIL;
9590 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9591 return FAIL;
9592 if (put_line(fd, "endif") == FAIL)
9593 return FAIL;
9594
9595 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 * Now save the current files, current buffer first.
9597 */
9598 if (put_line(fd, "set shortmess=aoO") == FAIL)
9599 return FAIL;
9600
9601 /* Now put the other buffers into the buffer list */
9602 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9603 {
9604 if (!(only_save_windows && buf->b_nwindows == 0)
9605 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9606 && buf->b_fname != NULL
9607 && buf->b_p_bl)
9608 {
9609 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9610 : buf->b_wininfo->wi_fpos.lnum) < 0
9611 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9612 return FAIL;
9613 }
9614 }
9615
9616 /* the global argument list */
9617 if (ses_arglist(fd, "args", &global_alist.al_ga,
9618 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9619 return FAIL;
9620
9621 if (ssop_flags & SSOP_RESIZE)
9622 {
9623 /* Note: after the restore we still check it worked!*/
9624 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9625 || put_eol(fd) == FAIL)
9626 return FAIL;
9627 }
9628
9629#ifdef FEAT_GUI
9630 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9631 {
9632 int x, y;
9633
9634 if (gui_mch_get_winpos(&x, &y) == OK)
9635 {
9636 /* Note: after the restore we still check it worked!*/
9637 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9638 return FAIL;
9639 }
9640 }
9641#endif
9642
9643 /*
9644 * Before creating the window layout, try loading one file. If this is
9645 * aborted we don't end up with a number of useless windows.
9646 * This may have side effects! (e.g., compressed or network file).
9647 */
9648 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9649 {
9650 if (ses_do_win(wp)
9651 && wp->w_buffer->b_ffname != NULL
9652 && !wp->w_buffer->b_help
9653#ifdef FEAT_QUICKFIX
9654 && !bt_nofile(wp->w_buffer)
9655#endif
9656 )
9657 {
9658 if (fputs("edit ", fd) < 0
9659 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9660 return FAIL;
9661 if (!wp->w_arg_idx_invalid)
9662 edited_win = wp;
9663 break;
9664 }
9665 }
9666
9667 /*
9668 * Save current window layout.
9669 */
9670 if (put_line(fd, "set splitbelow splitright") == FAIL)
9671 return FAIL;
9672 if (ses_win_rec(fd, topframe) == FAIL)
9673 return FAIL;
9674 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9675 return FAIL;
9676 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9677 return FAIL;
9678
9679 /*
9680 * Check if window sizes can be restored (no windows omitted).
9681 * Remember the window number of the current window after restoring.
9682 */
9683 nr = 0;
9684 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9685 {
9686 if (ses_do_win(wp))
9687 ++nr;
9688 else
9689 restore_size = FALSE;
9690 if (curwin == wp)
9691 cnr = nr;
9692 }
9693
9694 /* Go to the first window. */
9695 if (put_line(fd, "wincmd t") == FAIL)
9696 return FAIL;
9697
9698 /*
9699 * If more than one window, see if sizes can be restored.
9700 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9701 * resized when moving between windows.
9702 * Do this before restoring the view, so that the topline and the cursor
9703 * can be set. This is done again below.
9704 */
9705 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9706 return FAIL;
9707 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9708 return FAIL;
9709
9710 /*
9711 * Restore the view of the window (options, file, cursor, etc.).
9712 */
9713 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9714 {
9715 if (!ses_do_win(wp))
9716 continue;
9717 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9718 return FAIL;
9719 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9720 return FAIL;
9721 }
9722
9723 /*
9724 * Restore cursor to the current window if it's not the first one.
9725 */
9726 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9727 return FAIL;
9728
9729 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009730 * Wipe out an empty unnamed buffer we started in.
9731 */
9732 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9733 return FAIL;
9734 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9735 return FAIL;
9736 if (put_line(fd, "endif") == FAIL)
9737 return FAIL;
9738 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9739 return FAIL;
9740
9741 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 * Restore window sizes again after jumping around in windows, because the
9743 * current window has a minimum size while others may not.
9744 */
9745 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9746 return FAIL;
9747
9748 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9749 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9750 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9751 return FAIL;
9752
9753 /*
9754 * Lastly, execute the x.vim file if it exists.
9755 */
9756 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9757 || put_line(fd, "if file_readable(s:sx)") == FAIL
9758 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9759 || put_line(fd, "endif") == FAIL)
9760 return FAIL;
9761
9762 return OK;
9763}
9764
9765 static int
9766ses_winsizes(fd, restore_size)
9767 FILE *fd;
9768 int restore_size;
9769{
9770 int n = 0;
9771 win_T *wp;
9772
9773 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9774 {
9775 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9776 {
9777 if (!ses_do_win(wp))
9778 continue;
9779 ++n;
9780
9781 /* restore height when not full height */
9782 if (wp->w_height + wp->w_status_height < topframe->fr_height
9783 && (fprintf(fd,
9784 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9785 n, (long)wp->w_height, Rows / 2, Rows) < 0
9786 || put_eol(fd) == FAIL))
9787 return FAIL;
9788
9789 /* restore width when not full width */
9790 if (wp->w_width < Columns && (fprintf(fd,
9791 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9792 n, (long)wp->w_width, Columns / 2, Columns) < 0
9793 || put_eol(fd) == FAIL))
9794 return FAIL;
9795 }
9796 }
9797 else
9798 {
9799 /* Just equalise window sizes */
9800 if (put_line(fd, "wincmd =") == FAIL)
9801 return FAIL;
9802 }
9803 return OK;
9804}
9805
9806/*
9807 * Write commands to "fd" to recursively create windows for frame "fr",
9808 * horizontally and vertically split.
9809 * After the commands the last window in the frame is the current window.
9810 * Returns FAIL when writing the commands to "fd" fails.
9811 */
9812 static int
9813ses_win_rec(fd, fr)
9814 FILE *fd;
9815 frame_T *fr;
9816{
9817 frame_T *frc;
9818 int count = 0;
9819
9820 if (fr->fr_layout != FR_LEAF)
9821 {
9822 /* Find first frame that's not skipped and then create a window for
9823 * each following one (first frame is already there). */
9824 frc = ses_skipframe(fr->fr_child);
9825 if (frc != NULL)
9826 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9827 {
9828 /* Make window as big as possible so that we have lots of room
9829 * to split. */
9830 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9831 || put_line(fd, fr->fr_layout == FR_COL
9832 ? "split" : "vsplit") == FAIL)
9833 return FAIL;
9834 ++count;
9835 }
9836
9837 /* Go back to the first window. */
9838 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9839 ? "%dwincmd k" : "%dwincmd h", count) < 0
9840 || put_eol(fd) == FAIL))
9841 return FAIL;
9842
9843 /* Recursively create frames/windows in each window of this column or
9844 * row. */
9845 frc = ses_skipframe(fr->fr_child);
9846 while (frc != NULL)
9847 {
9848 ses_win_rec(fd, frc);
9849 frc = ses_skipframe(frc->fr_next);
9850 /* Go to next window. */
9851 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9852 return FAIL;
9853 }
9854 }
9855 return OK;
9856}
9857
9858/*
9859 * Skip frames that don't contain windows we want to save in the Session.
9860 * Returns NULL when there none.
9861 */
9862 static frame_T *
9863ses_skipframe(fr)
9864 frame_T *fr;
9865{
9866 frame_T *frc;
9867
9868 for (frc = fr; frc != NULL; frc = frc->fr_next)
9869 if (ses_do_frame(frc))
9870 break;
9871 return frc;
9872}
9873
9874/*
9875 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9876 * the Session.
9877 */
9878 static int
9879ses_do_frame(fr)
9880 frame_T *fr;
9881{
9882 frame_T *frc;
9883
9884 if (fr->fr_layout == FR_LEAF)
9885 return ses_do_win(fr->fr_win);
9886 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9887 if (ses_do_frame(frc))
9888 return TRUE;
9889 return FALSE;
9890}
9891
9892/*
9893 * Return non-zero if window "wp" is to be stored in the Session.
9894 */
9895 static int
9896ses_do_win(wp)
9897 win_T *wp;
9898{
9899 if (wp->w_buffer->b_fname == NULL
9900#ifdef FEAT_QUICKFIX
9901 /* When 'buftype' is "nofile" can't restore the window contents. */
9902 || bt_nofile(wp->w_buffer)
9903#endif
9904 )
9905 return (ssop_flags & SSOP_BLANK);
9906 if (wp->w_buffer->b_help)
9907 return (ssop_flags & SSOP_HELP);
9908 return TRUE;
9909}
9910
9911/*
9912 * Write commands to "fd" to restore the view of a window.
9913 * Caller must make sure 'scrolloff' is zero.
9914 */
9915 static int
9916put_view(fd, wp, add_edit, flagp)
9917 FILE *fd;
9918 win_T *wp;
9919 int add_edit; /* add ":edit" command to view */
9920 unsigned *flagp; /* vop_flags or ssop_flags */
9921{
9922 win_T *save_curwin;
9923 int f;
9924 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009925 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
9927 /* Always restore cursor position for ":mksession". For ":mkview" only
9928 * when 'viewoptions' contains "cursor". */
9929 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9930
9931 /*
9932 * Local argument list.
9933 */
9934 if (wp->w_alist == &global_alist)
9935 {
9936 if (put_line(fd, "argglobal") == FAIL)
9937 return FAIL;
9938 }
9939 else
9940 {
9941 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9942 flagp == &vop_flags
9943 || !(*flagp & SSOP_CURDIR)
9944 || wp->w_localdir != NULL, flagp) == FAIL)
9945 return FAIL;
9946 }
9947
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009948 /* Only when part of a session: restore the argument index. Some
9949 * arguments may have been deleted, check if the index is valid. */
9950 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9951 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 {
9953 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9954 || put_eol(fd) == FAIL)
9955 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009956 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 }
9958
9959 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009960 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 {
9962 /*
9963 * Load the file.
9964 */
9965 if (wp->w_buffer->b_ffname != NULL
9966#ifdef FEAT_QUICKFIX
9967 && !bt_nofile(wp->w_buffer)
9968#endif
9969 )
9970 {
9971 /*
9972 * Editing a file in this buffer: use ":edit file".
9973 * This may have side effects! (e.g., compressed or network file).
9974 */
9975 if (fputs("edit ", fd) < 0
9976 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9977 return FAIL;
9978 }
9979 else
9980 {
9981 /* No file in this buffer, just make it empty. */
9982 if (put_line(fd, "enew") == FAIL)
9983 return FAIL;
9984#ifdef FEAT_QUICKFIX
9985 if (wp->w_buffer->b_ffname != NULL)
9986 {
9987 /* The buffer does have a name, but it's not a file name. */
9988 if (fputs("file ", fd) < 0
9989 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9990 return FAIL;
9991 }
9992#endif
9993 do_cursor = FALSE;
9994 }
9995 }
9996
9997 /*
9998 * Local mappings and abbreviations.
9999 */
10000 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10001 && makemap(fd, wp->w_buffer) == FAIL)
10002 return FAIL;
10003
10004 /*
10005 * Local options. Need to go to the window temporarily.
10006 * Store only local values when using ":mkview" and when ":mksession" is
10007 * used and 'sessionoptions' doesn't include "options".
10008 * Some folding options are always stored when "folds" is included,
10009 * otherwise the folds would not be restored correctly.
10010 */
10011 save_curwin = curwin;
10012 curwin = wp;
10013 curbuf = curwin->w_buffer;
10014 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10015 f = makeset(fd, OPT_LOCAL,
10016 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10017#ifdef FEAT_FOLDING
10018 else if (*flagp & SSOP_FOLDS)
10019 f = makefoldset(fd);
10020#endif
10021 else
10022 f = OK;
10023 curwin = save_curwin;
10024 curbuf = curwin->w_buffer;
10025 if (f == FAIL)
10026 return FAIL;
10027
10028#ifdef FEAT_FOLDING
10029 /*
10030 * Save Folds when 'buftype' is empty and for help files.
10031 */
10032 if ((*flagp & SSOP_FOLDS)
10033 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010034# ifdef FEAT_QUICKFIX
10035 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10036# endif
10037 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 {
10039 if (put_folds(fd, wp) == FAIL)
10040 return FAIL;
10041 }
10042#endif
10043
10044 /*
10045 * Set the cursor after creating folds, since that moves the cursor.
10046 */
10047 if (do_cursor)
10048 {
10049
10050 /* Restore the cursor line in the file and relatively in the
10051 * window. Don't use "G", it changes the jumplist. */
10052 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10053 (long)wp->w_cursor.lnum,
10054 (long)(wp->w_cursor.lnum - wp->w_topline),
10055 (long)wp->w_height / 2, (long)wp->w_height) < 0
10056 || put_eol(fd) == FAIL
10057 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10058 || put_line(fd, "exe s:l") == FAIL
10059 || put_line(fd, "normal! zt") == FAIL
10060 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10061 || put_eol(fd) == FAIL)
10062 return FAIL;
10063 /* Restore the cursor column and left offset when not wrapping. */
10064 if (wp->w_cursor.col == 0)
10065 {
10066 if (put_line(fd, "normal! 0") == FAIL)
10067 return FAIL;
10068 }
10069 else
10070 {
10071 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10072 {
10073 if (fprintf(fd,
10074 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10075 (long)wp->w_cursor.col,
10076 (long)(wp->w_cursor.col - wp->w_leftcol),
10077 (long)wp->w_width / 2, (long)wp->w_width) < 0
10078 || put_eol(fd) == FAIL
10079 || put_line(fd, "if s:c > 0") == FAIL
10080 || fprintf(fd,
10081 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10082 (long)wp->w_cursor.col) < 0
10083 || put_eol(fd) == FAIL
10084 || put_line(fd, "else") == FAIL
10085 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10086 || put_eol(fd) == FAIL
10087 || put_line(fd, "endif") == FAIL)
10088 return FAIL;
10089 }
10090 else
10091 {
10092 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10093 || put_eol(fd) == FAIL)
10094 return FAIL;
10095 }
10096 }
10097 }
10098
10099 /*
10100 * Local directory.
10101 */
10102 if (wp->w_localdir != NULL)
10103 {
10104 if (fputs("lcd ", fd) < 0
10105 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10106 || put_eol(fd) == FAIL)
10107 return FAIL;
10108 }
10109
10110 return OK;
10111}
10112
10113/*
10114 * Write an argument list to the session file.
10115 * Returns FAIL if writing fails.
10116 */
10117 static int
10118ses_arglist(fd, cmd, gap, fullname, flagp)
10119 FILE *fd;
10120 char *cmd;
10121 garray_T *gap;
10122 int fullname; /* TRUE: use full path name */
10123 unsigned *flagp;
10124{
10125 int i;
10126 char_u buf[MAXPATHL];
10127 char_u *s;
10128
10129 if (gap->ga_len == 0)
10130 return put_line(fd, "silent! argdel *");
10131 if (fputs(cmd, fd) < 0)
10132 return FAIL;
10133 for (i = 0; i < gap->ga_len; ++i)
10134 {
10135 /* NULL file names are skipped (only happens when out of memory). */
10136 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10137 if (s != NULL)
10138 {
10139 if (fullname)
10140 {
10141 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10142 s = buf;
10143 }
10144 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10145 return FAIL;
10146 }
10147 }
10148 return put_eol(fd);
10149}
10150
10151/*
10152 * Write a buffer name to the session file.
10153 * Also ends the line.
10154 * Returns FAIL if writing fails.
10155 */
10156 static int
10157ses_fname(fd, buf, flagp)
10158 FILE *fd;
10159 buf_T *buf;
10160 unsigned *flagp;
10161{
10162 char_u *name;
10163
10164 /* Use the short file name if the current directory is known at the time
10165 * the session file will be sourced. Don't do this for ":mkview", we
10166 * don't know the current directory. */
10167 if (buf->b_sfname != NULL
10168 && flagp == &ssop_flags
10169 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10170 name = buf->b_sfname;
10171 else
10172 name = buf->b_ffname;
10173 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10174 return FAIL;
10175 return OK;
10176}
10177
10178/*
10179 * Write a file name to the session file.
10180 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10181 * characters.
10182 * Returns FAIL if writing fails.
10183 */
10184 static int
10185ses_put_fname(fd, name, flagp)
10186 FILE *fd;
10187 char_u *name;
10188 unsigned *flagp;
10189{
10190 char_u *sname;
10191 int retval = OK;
10192 int c;
10193
10194 sname = home_replace_save(NULL, name);
10195 if (sname != NULL)
10196 name = sname;
10197 while (*name != NUL)
10198 {
10199#ifdef FEAT_MBYTE
10200 {
10201 int l;
10202
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010203 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 {
10205 /* copy a multibyte char */
10206 while (--l >= 0)
10207 {
10208 if (putc(*name, fd) != *name)
10209 retval = FAIL;
10210 ++name;
10211 }
10212 continue;
10213 }
10214 }
10215#endif
10216 c = *name++;
10217 if (c == '\\' && (*flagp & SSOP_SLASH))
10218 /* change a backslash to a forward slash */
10219 c = '/';
10220 else if ((vim_strchr(escape_chars, c) != NULL
10221#ifdef BACKSLASH_IN_FILENAME
10222 && c != '\\'
10223#endif
10224 ) || c == '#' || c == '%')
10225 {
10226 /* escape a special character with a backslash */
10227 if (putc('\\', fd) != '\\')
10228 retval = FAIL;
10229 }
10230 if (putc(c, fd) != c)
10231 retval = FAIL;
10232 }
10233 vim_free(sname);
10234 return retval;
10235}
10236
10237/*
10238 * ":loadview [nr]"
10239 */
10240 static void
10241ex_loadview(eap)
10242 exarg_T *eap;
10243{
10244 char_u *fname;
10245
10246 fname = get_view_file(*eap->arg);
10247 if (fname != NULL)
10248 {
10249 do_source(fname, FALSE, FALSE);
10250 vim_free(fname);
10251 }
10252}
10253
10254/*
10255 * Get the name of the view file for the current buffer.
10256 */
10257 static char_u *
10258get_view_file(c)
10259 int c;
10260{
10261 int len = 0;
10262 char_u *p, *s;
10263 char_u *retval;
10264 char_u *sname;
10265
10266 if (curbuf->b_ffname == NULL)
10267 {
10268 EMSG(_(e_noname));
10269 return NULL;
10270 }
10271 sname = home_replace_save(NULL, curbuf->b_ffname);
10272 if (sname == NULL)
10273 return NULL;
10274
10275 /*
10276 * We want a file name without separators, because we're not going to make
10277 * a directory.
10278 * "normal" path separator -> "=+"
10279 * "=" -> "=="
10280 * ":" path separator -> "=-"
10281 */
10282 for (p = sname; *p; ++p)
10283 if (*p == '=' || vim_ispathsep(*p))
10284 ++len;
10285 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10286 if (retval != NULL)
10287 {
10288 STRCPY(retval, p_vdir);
10289 add_pathsep(retval);
10290 s = retval + STRLEN(retval);
10291 for (p = sname; *p; ++p)
10292 {
10293 if (*p == '=')
10294 {
10295 *s++ = '=';
10296 *s++ = '=';
10297 }
10298 else if (vim_ispathsep(*p))
10299 {
10300 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010301#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 || defined(VMS)
10303 if (*p == ':')
10304 *s++ = '-';
10305 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010307 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 }
10309 else
10310 *s++ = *p;
10311 }
10312 *s++ = '=';
10313 *s++ = c;
10314 STRCPY(s, ".vim");
10315 }
10316
10317 vim_free(sname);
10318 return retval;
10319}
10320
10321#endif /* FEAT_SESSION */
10322
10323/*
10324 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10325 * Return FAIL for a write error.
10326 */
10327 int
10328put_eol(fd)
10329 FILE *fd;
10330{
10331 if (
10332#ifdef USE_CRNL
10333 (
10334# ifdef MKSESSION_NL
10335 !mksession_nl &&
10336# endif
10337 (putc('\r', fd) < 0)) ||
10338#endif
10339 (putc('\n', fd) < 0))
10340 return FAIL;
10341 return OK;
10342}
10343
10344/*
10345 * Write a line to "fd".
10346 * Return FAIL for a write error.
10347 */
10348 int
10349put_line(fd, s)
10350 FILE *fd;
10351 char *s;
10352{
10353 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10354 return FAIL;
10355 return OK;
10356}
10357
10358#ifdef FEAT_VIMINFO
10359/*
10360 * ":rviminfo" and ":wviminfo".
10361 */
10362 static void
10363ex_viminfo(eap)
10364 exarg_T *eap;
10365{
10366 char_u *save_viminfo;
10367
10368 save_viminfo = p_viminfo;
10369 if (*p_viminfo == NUL)
10370 p_viminfo = (char_u *)"'100";
10371 if (eap->cmdidx == CMD_rviminfo)
10372 {
10373 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10374 EMSG(_("E195: Cannot open viminfo file for reading"));
10375 }
10376 else
10377 write_viminfo(eap->arg, eap->forceit);
10378 p_viminfo = save_viminfo;
10379}
10380#endif
10381
10382#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010383/*
10384 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010385 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 void
10388dialog_msg(buff, format, fname)
10389 char_u *buff;
10390 char *format;
10391 char_u *fname;
10392{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 if (fname == NULL)
10394 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010395 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396}
10397#endif
10398
10399/*
10400 * ":behave {mswin,xterm}"
10401 */
10402 static void
10403ex_behave(eap)
10404 exarg_T *eap;
10405{
10406 if (STRCMP(eap->arg, "mswin") == 0)
10407 {
10408 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10409 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10410 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10411 set_option_value((char_u *)"keymodel", 0L,
10412 (char_u *)"startsel,stopsel", 0);
10413 }
10414 else if (STRCMP(eap->arg, "xterm") == 0)
10415 {
10416 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10417 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10418 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10419 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10420 }
10421 else
10422 EMSG2(_(e_invarg2), eap->arg);
10423}
10424
10425#ifdef FEAT_AUTOCMD
10426static int filetype_detect = FALSE;
10427static int filetype_plugin = FALSE;
10428static int filetype_indent = FALSE;
10429
10430/*
10431 * ":filetype [plugin] [indent] {on,off,detect}"
10432 * on: Load the filetype.vim file to install autocommands for file types.
10433 * off: Load the ftoff.vim file to remove all autocommands for file types.
10434 * plugin on: load filetype.vim and ftplugin.vim
10435 * plugin off: load ftplugof.vim
10436 * indent on: load filetype.vim and indent.vim
10437 * indent off: load indoff.vim
10438 */
10439 static void
10440ex_filetype(eap)
10441 exarg_T *eap;
10442{
10443 char_u *arg = eap->arg;
10444 int plugin = FALSE;
10445 int indent = FALSE;
10446
10447 if (*eap->arg == NUL)
10448 {
10449 /* Print current status. */
10450 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10451 filetype_detect ? "ON" : "OFF",
10452 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10453 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10454 return;
10455 }
10456
10457 /* Accept "plugin" and "indent" in any order. */
10458 for (;;)
10459 {
10460 if (STRNCMP(arg, "plugin", 6) == 0)
10461 {
10462 plugin = TRUE;
10463 arg = skipwhite(arg + 6);
10464 continue;
10465 }
10466 if (STRNCMP(arg, "indent", 6) == 0)
10467 {
10468 indent = TRUE;
10469 arg = skipwhite(arg + 6);
10470 continue;
10471 }
10472 break;
10473 }
10474 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10475 {
10476 if (*arg == 'o' || !filetype_detect)
10477 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010478 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 filetype_detect = TRUE;
10480 if (plugin)
10481 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010482 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 filetype_plugin = TRUE;
10484 }
10485 if (indent)
10486 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010487 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 filetype_indent = TRUE;
10489 }
10490 }
10491 if (*arg == 'd')
10492 {
10493 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010494 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 }
10496 }
10497 else if (STRCMP(arg, "off") == 0)
10498 {
10499 if (plugin || indent)
10500 {
10501 if (plugin)
10502 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010503 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 filetype_plugin = FALSE;
10505 }
10506 if (indent)
10507 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010508 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 filetype_indent = FALSE;
10510 }
10511 }
10512 else
10513 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010514 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 filetype_detect = FALSE;
10516 }
10517 }
10518 else
10519 EMSG2(_(e_invarg2), arg);
10520}
10521
10522/*
10523 * ":setfiletype {name}"
10524 */
10525 static void
10526ex_setfiletype(eap)
10527 exarg_T *eap;
10528{
10529 if (!did_filetype)
10530 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10531}
10532#endif
10533
10534/*ARGSUSED*/
10535 static void
10536ex_digraphs(eap)
10537 exarg_T *eap;
10538{
10539#ifdef FEAT_DIGRAPHS
10540 if (*eap->arg != NUL)
10541 putdigraph(eap->arg);
10542 else
10543 listdigraphs();
10544#else
10545 EMSG(_("E196: No digraphs in this version"));
10546#endif
10547}
10548
10549 static void
10550ex_set(eap)
10551 exarg_T *eap;
10552{
10553 int flags = 0;
10554
10555 if (eap->cmdidx == CMD_setlocal)
10556 flags = OPT_LOCAL;
10557 else if (eap->cmdidx == CMD_setglobal)
10558 flags = OPT_GLOBAL;
10559#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10560 if (cmdmod.browse && flags == 0)
10561 ex_options(eap);
10562 else
10563#endif
10564 (void)do_set(eap->arg, flags);
10565}
10566
10567#ifdef FEAT_SEARCH_EXTRA
10568/*
10569 * ":nohlsearch"
10570 */
10571/*ARGSUSED*/
10572 static void
10573ex_nohlsearch(eap)
10574 exarg_T *eap;
10575{
10576 no_hlsearch = TRUE;
10577 redraw_all_later(NOT_VALID);
10578}
10579
10580/*
10581 * ":match {group} {pattern}"
10582 * Sets nextcmd to the start of the next command, if any. Also called when
10583 * skipping commands to find the next command.
10584 */
10585 static void
10586ex_match(eap)
10587 exarg_T *eap;
10588{
10589 char_u *p;
10590 char_u *end;
10591 int c;
10592
10593 /* First clear any old pattern. */
10594 if (!eap->skip)
10595 {
10596 vim_free(curwin->w_match.regprog);
10597 curwin->w_match.regprog = NULL;
10598 redraw_later(NOT_VALID); /* always need a redraw */
10599 }
10600
10601 if (ends_excmd(*eap->arg))
10602 end = eap->arg;
10603 else if ((STRNICMP(eap->arg, "none", 4) == 0
10604 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10605 end = eap->arg + 4;
10606 else
10607 {
10608 p = skiptowhite(eap->arg);
10609 if (!eap->skip)
10610 {
10611 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10612 if (curwin->w_match_id == 0)
10613 {
10614 EMSG2(_(e_nogroup), eap->arg);
10615 return;
10616 }
10617 }
10618 p = skipwhite(p);
10619 if (*p == NUL)
10620 {
10621 /* There must be two arguments. */
10622 EMSG2(_(e_invarg2), eap->arg);
10623 return;
10624 }
10625 end = skip_regexp(p + 1, *p, TRUE, NULL);
10626 if (!eap->skip)
10627 {
10628 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10629 {
10630 eap->errmsg = e_trailing;
10631 return;
10632 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010633 if (*end != *p)
10634 {
10635 EMSG2(_(e_invarg2), p);
10636 return;
10637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638
10639 c = *end;
10640 *end = NUL;
10641 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10642 *end = c;
10643 if (curwin->w_match.regprog == NULL)
10644 {
10645 EMSG2(_(e_invarg2), p);
10646 return;
10647 }
10648 }
10649 }
10650 eap->nextcmd = find_nextcmd(end);
10651}
10652#endif
10653
10654#ifdef FEAT_CRYPT
10655/*
10656 * ":X": Get crypt key
10657 */
10658/*ARGSUSED*/
10659 static void
10660ex_X(eap)
10661 exarg_T *eap;
10662{
10663 (void)get_crypt_key(TRUE, TRUE);
10664}
10665#endif
10666
10667#ifdef FEAT_FOLDING
10668 static void
10669ex_fold(eap)
10670 exarg_T *eap;
10671{
10672 if (foldManualAllowed(TRUE))
10673 foldCreate(eap->line1, eap->line2);
10674}
10675
10676 static void
10677ex_foldopen(eap)
10678 exarg_T *eap;
10679{
10680 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10681 eap->forceit, FALSE);
10682}
10683
10684 static void
10685ex_folddo(eap)
10686 exarg_T *eap;
10687{
10688 linenr_T lnum;
10689
10690 /* First set the marks for all lines closed/open. */
10691 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10692 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10693 ml_setmarked(lnum);
10694
10695 /* Execute the command on the marked lines. */
10696 global_exe(eap->arg);
10697 ml_clearmarked(); /* clear rest of the marks */
10698}
10699#endif