blob: 5798250d722884384fd63aa0df58a3b30ce8d0c4 [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 Moolenaar1d2ba7f2006-02-14 22:29:30 +0000158static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#else
160# define ex_close ex_ni
161# define ex_only ex_ni
162# define ex_all ex_ni
163# define ex_resize ex_ni
164# define ex_splitview ex_ni
165# define ex_stag ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166# define ex_tab ex_ni
167# define ex_tabs ex_ni
168# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000169# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#endif
171#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
172static void ex_pclose __ARGS((exarg_T *eap));
173static void ex_ptag __ARGS((exarg_T *eap));
174static void ex_pedit __ARGS((exarg_T *eap));
175#else
176# define ex_pclose ex_ni
177# define ex_ptag ex_ni
178# define ex_pedit ex_ni
179#endif
180static void ex_hide __ARGS((exarg_T *eap));
181static void ex_stop __ARGS((exarg_T *eap));
182static void ex_exit __ARGS((exarg_T *eap));
183static void ex_print __ARGS((exarg_T *eap));
184#ifdef FEAT_BYTEOFF
185static void ex_goto __ARGS((exarg_T *eap));
186#else
187# define ex_goto ex_ni
188#endif
189static void ex_shell __ARGS((exarg_T *eap));
190static void ex_preserve __ARGS((exarg_T *eap));
191static void ex_recover __ARGS((exarg_T *eap));
192#ifndef FEAT_LISTCMDS
193# define ex_argedit ex_ni
194# define ex_argadd ex_ni
195# define ex_argdelete ex_ni
196# define ex_listdo ex_ni
197#endif
198static void ex_mode __ARGS((exarg_T *eap));
199static void ex_wrongmodifier __ARGS((exarg_T *eap));
200static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000201static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202static void ex_edit __ARGS((exarg_T *eap));
203#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
204# define ex_drop ex_ni
205#endif
206#ifndef FEAT_GUI
207# define ex_gui ex_nogui
208static void ex_nogui __ARGS((exarg_T *eap));
209#endif
210#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
211static void ex_tearoff __ARGS((exarg_T *eap));
212#else
213# define ex_tearoff ex_ni
214#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000215#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216static void ex_popup __ARGS((exarg_T *eap));
217#else
218# define ex_popup ex_ni
219#endif
220#ifndef FEAT_GUI_MSWIN
221# define ex_simalt ex_ni
222#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000223#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224# define gui_mch_find_dialog ex_ni
225# define gui_mch_replace_dialog ex_ni
226#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000227#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228# define ex_helpfind ex_ni
229#endif
230#ifndef FEAT_CSCOPE
231# define do_cscope ex_ni
232# define do_scscope ex_ni
233# define do_cstag ex_ni
234#endif
235#ifndef FEAT_SYN_HL
236# define ex_syntax ex_ni
Bram Moolenaarb765d632005-06-07 21:00:02 +0000237# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000238# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000239# define ex_spelldump ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000240# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000241#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000242#ifndef FEAT_MZSCHEME
243# define ex_mzscheme ex_script_ni
244# define ex_mzfile ex_ni
245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#ifndef FEAT_PERL
247# define ex_perl ex_script_ni
248# define ex_perldo ex_ni
249#endif
250#ifndef FEAT_PYTHON
251# define ex_python ex_script_ni
252# define ex_pyfile ex_ni
253#endif
254#ifndef FEAT_TCL
255# define ex_tcl ex_script_ni
256# define ex_tcldo ex_ni
257# define ex_tclfile ex_ni
258#endif
259#ifndef FEAT_RUBY
260# define ex_ruby ex_script_ni
261# define ex_rubydo ex_ni
262# define ex_rubyfile ex_ni
263#endif
264#ifndef FEAT_SNIFF
265# define ex_sniff ex_ni
266#endif
267#ifndef FEAT_KEYMAP
268# define ex_loadkeymap ex_ni
269#endif
270static void ex_swapname __ARGS((exarg_T *eap));
271static void ex_syncbind __ARGS((exarg_T *eap));
272static void ex_read __ARGS((exarg_T *eap));
273static void ex_cd __ARGS((exarg_T *eap));
274static void ex_pwd __ARGS((exarg_T *eap));
275static void ex_equal __ARGS((exarg_T *eap));
276static void ex_sleep __ARGS((exarg_T *eap));
277static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
278static void ex_winsize __ARGS((exarg_T *eap));
279#ifdef FEAT_WINDOWS
280static void ex_wincmd __ARGS((exarg_T *eap));
281#else
282# define ex_wincmd ex_ni
283#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000284#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285static void ex_winpos __ARGS((exarg_T *eap));
286#else
287# define ex_winpos ex_ni
288#endif
289static void ex_operators __ARGS((exarg_T *eap));
290static void ex_put __ARGS((exarg_T *eap));
291static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000292static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static void ex_submagic __ARGS((exarg_T *eap));
294static void ex_join __ARGS((exarg_T *eap));
295static void ex_at __ARGS((exarg_T *eap));
296static void ex_bang __ARGS((exarg_T *eap));
297static void ex_undo __ARGS((exarg_T *eap));
298static void ex_redo __ARGS((exarg_T *eap));
299static void ex_redir __ARGS((exarg_T *eap));
300static void ex_redraw __ARGS((exarg_T *eap));
301static void ex_redrawstatus __ARGS((exarg_T *eap));
302static void close_redir __ARGS((void));
303static void ex_mkrc __ARGS((exarg_T *eap));
304static void ex_mark __ARGS((exarg_T *eap));
305#ifdef FEAT_USR_CMDS
306static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000307static 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 +0000308#endif
309#ifdef FEAT_EX_EXTRA
310static void ex_normal __ARGS((exarg_T *eap));
311static void ex_startinsert __ARGS((exarg_T *eap));
312static void ex_stopinsert __ARGS((exarg_T *eap));
313#else
314# define ex_normal ex_ni
315# define ex_align ex_ni
316# define ex_retab ex_ni
317# define ex_startinsert ex_ni
318# define ex_stopinsert ex_ni
319# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000320# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#endif
322#ifdef FEAT_FIND_ID
323static void ex_checkpath __ARGS((exarg_T *eap));
324static void ex_findpat __ARGS((exarg_T *eap));
325#else
326# define ex_findpat ex_ni
327# define ex_checkpath ex_ni
328#endif
329#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
330static void ex_psearch __ARGS((exarg_T *eap));
331#else
332# define ex_psearch ex_ni
333#endif
334static void ex_tag __ARGS((exarg_T *eap));
335static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
336#ifndef FEAT_EVAL
337# define ex_scriptnames ex_ni
338# define ex_finish ex_ni
339# define ex_echo ex_ni
340# define ex_echohl ex_ni
341# define ex_execute ex_ni
342# define ex_call ex_ni
343# define ex_if ex_ni
344# define ex_endif ex_ni
345# define ex_else ex_ni
346# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000347# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348# define ex_continue ex_ni
349# define ex_break ex_ni
350# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000351# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352# define ex_throw ex_ni
353# define ex_try ex_ni
354# define ex_catch ex_ni
355# define ex_finally ex_ni
356# define ex_endtry ex_ni
357# define ex_endfunction ex_ni
358# define ex_let ex_ni
359# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000360# define ex_lockvar ex_ni
361# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362# define ex_function ex_ni
363# define ex_delfunction ex_ni
364# define ex_return ex_ni
365#endif
366static char_u *arg_all __ARGS((void));
367#ifdef FEAT_SESSION
368static int makeopens __ARGS((FILE *fd, char_u *dirnow));
369static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
370static void ex_loadview __ARGS((exarg_T *eap));
371static char_u *get_view_file __ARGS((int c));
372#else
373# define ex_loadview ex_ni
374#endif
375#ifndef FEAT_EVAL
376# define ex_compiler ex_ni
377#endif
378#ifdef FEAT_VIMINFO
379static void ex_viminfo __ARGS((exarg_T *eap));
380#else
381# define ex_viminfo ex_ni
382#endif
383static void ex_behave __ARGS((exarg_T *eap));
384#ifdef FEAT_AUTOCMD
385static void ex_filetype __ARGS((exarg_T *eap));
386static void ex_setfiletype __ARGS((exarg_T *eap));
387#else
388# define ex_filetype ex_ni
389# define ex_setfiletype ex_ni
390#endif
391#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000392# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393# define ex_diffpatch ex_ni
394# define ex_diffgetput ex_ni
395# define ex_diffsplit ex_ni
396# define ex_diffthis ex_ni
397# define ex_diffupdate ex_ni
398#endif
399static void ex_digraphs __ARGS((exarg_T *eap));
400static void ex_set __ARGS((exarg_T *eap));
401#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
402# define ex_options ex_ni
403#endif
404#ifdef FEAT_SEARCH_EXTRA
405static void ex_nohlsearch __ARGS((exarg_T *eap));
406static void ex_match __ARGS((exarg_T *eap));
407#else
408# define ex_nohlsearch ex_ni
409# define ex_match ex_ni
410#endif
411#ifdef FEAT_CRYPT
412static void ex_X __ARGS((exarg_T *eap));
413#else
414# define ex_X ex_ni
415#endif
416#ifdef FEAT_FOLDING
417static void ex_fold __ARGS((exarg_T *eap));
418static void ex_foldopen __ARGS((exarg_T *eap));
419static void ex_folddo __ARGS((exarg_T *eap));
420#else
421# define ex_fold ex_ni
422# define ex_foldopen ex_ni
423# define ex_folddo ex_ni
424#endif
425#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
426 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
427# define ex_language ex_ni
428#endif
429#ifndef FEAT_SIGNS
430# define ex_sign ex_ni
431#endif
432#ifndef FEAT_SUN_WORKSHOP
433# define ex_wsverb ex_ni
434#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000435#ifndef FEAT_NETBEANS_INTG
436# define ex_nbkey ex_ni
437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438
439#ifndef FEAT_EVAL
440# define ex_debug ex_ni
441# define ex_breakadd ex_ni
442# define ex_debuggreedy ex_ni
443# define ex_breakdel ex_ni
444# define ex_breaklist ex_ni
445#endif
446
447#ifndef FEAT_CMDHIST
448# define ex_history ex_ni
449#endif
450#ifndef FEAT_JUMPLIST
451# define ex_jumps ex_ni
452# define ex_changes ex_ni
453#endif
454
Bram Moolenaar05159a02005-02-26 23:04:13 +0000455#ifndef FEAT_PROFILE
456# define ex_profile ex_ni
457#endif
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459/*
460 * Declare cmdnames[].
461 */
462#define DO_DECLARE_EXCMD
463#include "ex_cmds.h"
464
465/*
466 * Table used to quickly search for a command, based on its first character.
467 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000468static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469{
470 CMD_append,
471 CMD_buffer,
472 CMD_change,
473 CMD_delete,
474 CMD_edit,
475 CMD_file,
476 CMD_global,
477 CMD_help,
478 CMD_insert,
479 CMD_join,
480 CMD_k,
481 CMD_list,
482 CMD_move,
483 CMD_next,
484 CMD_open,
485 CMD_print,
486 CMD_quit,
487 CMD_read,
488 CMD_substitute,
489 CMD_t,
490 CMD_undo,
491 CMD_vglobal,
492 CMD_write,
493 CMD_xit,
494 CMD_yank,
495 CMD_z,
496 CMD_bang
497};
498
499static char_u dollar_command[2] = {'$', 0};
500
501
502#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000503/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504typedef struct
505{
506 char_u *line; /* command line */
507 linenr_T lnum; /* sourcing_lnum of the line */
508} wcmd_T;
509
510/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000511 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000513 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000515struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 garray_T *lines_gap; /* growarray with line info */
518 int current_line; /* last read line from growarray */
519 int repeating; /* TRUE when looping a second time */
520 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
521 char_u *(*getline) __ARGS((int, void *, int));
522 void *cookie;
523};
524
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000525static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
526static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000528
529/* Struct to save a few things while debugging. Used in do_cmdline() only. */
530struct dbg_stuff
531{
532 int trylevel;
533 int force_abort;
534 except_T *caught_stack;
535 char_u *vv_exception;
536 char_u *vv_throwpoint;
537 int did_emsg;
538 int got_int;
539 int did_throw;
540 int need_rethrow;
541 int check_cstack;
542 except_T *current_exception;
543};
544
545static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
546static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
547
548 static void
549save_dbg_stuff(dsp)
550 struct dbg_stuff *dsp;
551{
552 dsp->trylevel = trylevel; trylevel = 0;
553 dsp->force_abort = force_abort; force_abort = FALSE;
554 dsp->caught_stack = caught_stack; caught_stack = NULL;
555 dsp->vv_exception = v_exception(NULL);
556 dsp->vv_throwpoint = v_throwpoint(NULL);
557
558 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
559 dsp->did_emsg = did_emsg; did_emsg = FALSE;
560 dsp->got_int = got_int; got_int = FALSE;
561 dsp->did_throw = did_throw; did_throw = FALSE;
562 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
563 dsp->check_cstack = check_cstack; check_cstack = FALSE;
564 dsp->current_exception = current_exception; current_exception = NULL;
565}
566
567 static void
568restore_dbg_stuff(dsp)
569 struct dbg_stuff *dsp;
570{
571 suppress_errthrow = FALSE;
572 trylevel = dsp->trylevel;
573 force_abort = dsp->force_abort;
574 caught_stack = dsp->caught_stack;
575 (void)v_exception(dsp->vv_exception);
576 (void)v_throwpoint(dsp->vv_throwpoint);
577 did_emsg = dsp->did_emsg;
578 got_int = dsp->got_int;
579 did_throw = dsp->did_throw;
580 need_rethrow = dsp->need_rethrow;
581 check_cstack = dsp->check_cstack;
582 current_exception = dsp->current_exception;
583}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584#endif
585
586
587/*
588 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
589 * command is given.
590 */
591 void
592do_exmode(improved)
593 int improved; /* TRUE for "improved Ex" mode */
594{
595 int save_msg_scroll;
596 int prev_msg_row;
597 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000598 int changedtick;
599
600 if (improved)
601 exmode_active = EXMODE_VIM;
602 else
603 exmode_active = EXMODE_NORMAL;
604 State = NORMAL;
605
606 /* When using ":global /pat/ visual" and then "Q" we return to continue
607 * the :global command. */
608 if (global_busy)
609 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 save_msg_scroll = msg_scroll;
612 ++RedrawingDisabled; /* don't redisplay the window */
613 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef FEAT_GUI
615 /* Ignore scrollbar and mouse events in Ex mode */
616 ++hold_gui_events;
617#endif
618#ifdef FEAT_SNIFF
619 want_sniff_request = 0; /* No K_SNIFF wanted */
620#endif
621
622 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
623 while (exmode_active)
624 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000625#ifdef FEAT_EX_EXTRA
626 /* Check for a ":normal" command and no more characters left. */
627 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
628 {
629 exmode_active = FALSE;
630 break;
631 }
632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 msg_scroll = TRUE;
634 need_wait_return = FALSE;
635 ex_pressedreturn = FALSE;
636 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000637 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 prev_msg_row = msg_row;
639 prev_line = curwin->w_cursor.lnum;
640#ifdef FEAT_SNIFF
641 ProcessSniffRequests();
642#endif
643 if (improved)
644 {
645 cmdline_row = msg_row;
646 do_cmdline(NULL, getexline, NULL, 0);
647 }
648 else
649 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
650 lines_left = Rows - 1;
651
Bram Moolenaardf177f62005-02-22 08:39:57 +0000652 if ((prev_line != curwin->w_cursor.lnum
653 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000655 if (curbuf->b_ml.ml_flags & ML_EMPTY)
656 EMSG(_(e_emptybuf));
657 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000659 if (ex_pressedreturn)
660 {
661 /* go up one line, to overwrite the ":<CR>" line, so the
662 * output doensn't contain empty lines. */
663 msg_row = prev_msg_row;
664 if (prev_msg_row == Rows - 1)
665 msg_row--;
666 }
667 msg_col = 0;
668 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
669 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000672 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
673 {
674 if (curbuf->b_ml.ml_flags & ML_EMPTY)
675 EMSG(_(e_emptybuf));
676 else
677 EMSG(_("E501: At end-of-file"));
678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680
681#ifdef FEAT_GUI
682 --hold_gui_events;
683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 --RedrawingDisabled;
685 --no_wait_return;
686 update_screen(CLEAR);
687 need_wait_return = FALSE;
688 msg_scroll = save_msg_scroll;
689}
690
691/*
692 * Execute a simple command line. Used for translated commands like "*".
693 */
694 int
695do_cmdline_cmd(cmd)
696 char_u *cmd;
697{
698 return do_cmdline(cmd, NULL, NULL,
699 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
700}
701
702/*
703 * do_cmdline(): execute one Ex command line
704 *
705 * 1. Execute "cmdline" when it is not NULL.
706 * If "cmdline" is NULL, or more lines are needed, getline() is used.
707 * 2. Split up in parts separated with '|'.
708 *
709 * This function can be called recursively!
710 *
711 * flags:
712 * DOCMD_VERBOSE - The command will be included in the error message.
713 * DOCMD_NOWAIT - Don't call wait_return() and friends.
714 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
715 * DOCMD_KEYTYPED - Don't reset KeyTyped.
716 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
717 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
718 *
719 * return FAIL if cmdline could not be executed, OK otherwise
720 */
721 int
722do_cmdline(cmdline, getline, cookie, flags)
723 char_u *cmdline;
724 char_u *(*getline) __ARGS((int, void *, int));
725 void *cookie; /* argument for getline() */
726 int flags;
727{
728 char_u *next_cmdline; /* next cmd to execute */
729 char_u *cmdline_copy = NULL; /* copy of cmd line */
730 int used_getline = FALSE; /* used "getline" to obtain command */
731 static int recursive = 0; /* recursive depth */
732 int msg_didout_before_start = 0;
733 int count = 0; /* line number count */
734 int did_inc = FALSE; /* incremented RedrawingDisabled */
735 int retval = OK;
736#ifdef FEAT_EVAL
737 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000738 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 int current_line = 0; /* active line in lines_ga */
740 char_u *fname = NULL; /* function or script name */
741 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
742 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000743 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 int initial_trylevel;
745 struct msglist **saved_msg_list = NULL;
746 struct msglist *private_msg_list;
747
748 /* "getline" and "cookie" passed to do_one_cmd() */
749 char_u *(*cmd_getline) __ARGS((int, void *, int));
750 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000751 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000753 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754#else
755# define cmd_getline getline
756# define cmd_cookie cookie
757#endif
758 static int call_depth = 0; /* recursiveness */
759
760#ifdef FEAT_EVAL
761 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
762 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000763 * This ensures that the do_errthrow() call in do_one_cmd() does not
764 * combine the messages stored by an earlier invocation of do_one_cmd()
765 * with the command name of the later one. This would happen when
766 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 saved_msg_list = msg_list;
768 msg_list = &private_msg_list;
769 private_msg_list = NULL;
770#endif
771
772 /* It's possible to create an endless loop with ":execute", catch that
773 * here. The value of 200 allows nested function calls, ":source", etc. */
774 if (call_depth == 200)
775 {
776 EMSG(_("E169: Command too recursive"));
777#ifdef FEAT_EVAL
778 /* When converting to an exception, we do not include the command name
779 * since this is not an error of the specific command. */
780 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
781 msg_list = saved_msg_list;
782#endif
783 return FAIL;
784 }
785 ++call_depth;
786
787#ifdef FEAT_EVAL
788 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 cstack.cs_trylevel = 0;
791 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000792 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
794
795 real_cookie = getline_cookie(getline, cookie);
796
797 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798 getline_is_func = getline_equal(getline, cookie, get_func_line);
799 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 ++ex_nesting_level;
801
802 /* Get the function or script name and the address where the next breakpoint
803 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
806 fname = func_name(real_cookie);
807 breakpoint = func_breakpoint(real_cookie);
808 dbg_tick = func_dbg_tick(real_cookie);
809 }
810 else if (getline_equal(getline, cookie, getsourceline))
811 {
812 fname = sourcing_name;
813 breakpoint = source_breakpoint(real_cookie);
814 dbg_tick = source_dbg_tick(real_cookie);
815 }
816
817 /*
818 * Initialize "force_abort" and "suppress_errthrow" at the top level.
819 */
820 if (!recursive)
821 {
822 force_abort = FALSE;
823 suppress_errthrow = FALSE;
824 }
825
826 /*
827 * If requested, store and reset the global values controlling the
828 * exception handling (used when debugging).
829 */
830 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000831 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 initial_trylevel = trylevel;
834
835 /*
836 * "did_throw" will be set to TRUE when an exception is being thrown.
837 */
838 did_throw = FALSE;
839#endif
840 /*
841 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000842 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 * If force_abort is set, we cancel everything.
844 */
845 did_emsg = FALSE;
846
847 /*
848 * KeyTyped is only set when calling vgetc(). Reset it here when not
849 * calling vgetc() (sourced command lines).
850 */
851 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
852 KeyTyped = FALSE;
853
854 /*
855 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000856 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 * - for multiple commands on one line, separated with '|'
858 * - when repeating until there are no more lines (for ":source")
859 */
860 next_cmdline = cmdline;
861 do
862 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863#ifdef FEAT_EVAL
864 getline_is_func = getline_equal(getline, cookie, get_func_line);
865#endif
866
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (next_cmdline == NULL
869#ifdef FEAT_EVAL
870 && !force_abort
871 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000872 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif
874 )
875 did_emsg = FALSE;
876
877 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000878 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 * 2. If no line given: Get an allocated line with getline().
880 * 3. If a line is given: Make a copy, so we can mess with it.
881 */
882
883#ifdef FEAT_EVAL
884 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000885 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {
887 /* Each '|' separated command is stored separately in lines_ga, to
888 * be able to jump to it. Don't use next_cmdline now. */
889 vim_free(cmdline_copy);
890 cmdline_copy = NULL;
891
892 /* Check if a function has returned or, unless it has an unclosed
893 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896# ifdef FEAT_PROFILE
897 if (do_profiling)
898 func_line_end(real_cookie);
899# endif
900 if (func_has_ended(real_cookie))
901 {
902 retval = FAIL;
903 break;
904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906#ifdef FEAT_PROFILE
907 else if (do_profiling
908 && getline_equal(getline, cookie, getsourceline))
909 script_line_end();
910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
912 /* Check if a sourced file hit a ":finish" command. */
913 if (source_finished(getline, cookie))
914 {
915 retval = FAIL;
916 break;
917 }
918
919 /* If breakpoints have been added/deleted need to check for it. */
920 if (breakpoint != NULL && dbg_tick != NULL
921 && *dbg_tick != debug_tick)
922 {
923 *breakpoint = dbg_find_breakpoint(
924 getline_equal(getline, cookie, getsourceline),
925 fname, sourcing_lnum);
926 *dbg_tick = debug_tick;
927 }
928
929 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
930 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
931
932 /* Did we encounter a breakpoint? */
933 if (breakpoint != NULL && *breakpoint != 0
934 && *breakpoint <= sourcing_lnum)
935 {
936 dbg_breakpoint(fname, sourcing_lnum);
937 /* Find next breakpoint. */
938 *breakpoint = dbg_find_breakpoint(
939 getline_equal(getline, cookie, getsourceline),
940 fname, sourcing_lnum);
941 *dbg_tick = debug_tick;
942 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943# ifdef FEAT_PROFILE
944 if (do_profiling)
945 {
946 if (getline_is_func)
947 func_line_start(real_cookie);
948 else if (getline_equal(getline, cookie, getsourceline))
949 script_line_start();
950 }
951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000954 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000956 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 * again. Pass a different "getline" function to do_one_cmd()
958 * below, so that it stores lines in or reads them from
959 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000960 * while/for loop. */
961 cmd_getline = get_loop_line;
962 cmd_cookie = (void *)&cmd_loop_cookie;
963 cmd_loop_cookie.lines_gap = &lines_ga;
964 cmd_loop_cookie.current_line = current_line;
965 cmd_loop_cookie.getline = getline;
966 cmd_loop_cookie.cookie = cookie;
967 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 }
969 else
970 {
971 cmd_getline = getline;
972 cmd_cookie = cookie;
973 }
974#endif
975
976 /* 2. If no line given, get an allocated line with getline(). */
977 if (next_cmdline == NULL)
978 {
979 /*
980 * Need to set msg_didout for the first line after an ":if",
981 * otherwise the ":if" will be overwritten.
982 */
983 if (count == 1 && getline_equal(getline, cookie, getexline))
984 msg_didout = TRUE;
985 if (getline == NULL || (next_cmdline = getline(':', cookie,
986#ifdef FEAT_EVAL
987 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
988#else
989 0
990#endif
991 )) == NULL)
992 {
993 /* Don't call wait_return for aborted command line. The NULL
994 * returned for the end of a sourced file or executed function
995 * doesn't do this. */
996 if (KeyTyped && !(flags & DOCMD_REPEAT))
997 need_wait_return = FALSE;
998 retval = FAIL;
999 break;
1000 }
1001 used_getline = TRUE;
1002
1003 /*
1004 * Keep the first typed line. Clear it when more lines are typed.
1005 */
1006 if (flags & DOCMD_KEEPLINE)
1007 {
1008 vim_free(repeat_cmdline);
1009 if (count == 0)
1010 repeat_cmdline = vim_strsave(next_cmdline);
1011 else
1012 repeat_cmdline = NULL;
1013 }
1014 }
1015
1016 /* 3. Make a copy of the command so we can mess with it. */
1017 else if (cmdline_copy == NULL)
1018 {
1019 next_cmdline = vim_strsave(next_cmdline);
1020 if (next_cmdline == NULL)
1021 {
1022 EMSG(_(e_outofmem));
1023 retval = FAIL;
1024 break;
1025 }
1026 }
1027 cmdline_copy = next_cmdline;
1028
1029#ifdef FEAT_EVAL
1030 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 * Save the current line when inside a ":while" or ":for", and when
1032 * the command looks like a ":while" or ":for", because we may need it
1033 * later. When there is a '|' and another command, it is stored
1034 * separately, because we need to be able to jump back to it from an
1035 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (current_line == lines_ga.ga_len
1038 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 retval = FAIL;
1043 break;
1044 }
1045 }
1046 did_endif = FALSE;
1047#endif
1048
1049 if (count++ == 0)
1050 {
1051 /*
1052 * All output from the commands is put below each other, without
1053 * waiting for a return. Don't do this when executing commands
1054 * from a script or when being called recursive (e.g. for ":e
1055 * +command file").
1056 */
1057 if (!(flags & DOCMD_NOWAIT) && !recursive)
1058 {
1059 msg_didout_before_start = msg_didout;
1060 msg_didany = FALSE; /* no output yet */
1061 msg_start();
1062 msg_scroll = TRUE; /* put messages below each other */
1063 ++no_wait_return; /* dont wait for return until finished */
1064 ++RedrawingDisabled;
1065 did_inc = TRUE;
1066 }
1067 }
1068
1069 if (p_verbose >= 15 && sourcing_name != NULL)
1070 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001072 verbose_enter_scroll();
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 smsg((char_u *)_("line %ld: %s"),
1075 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001076 if (msg_silent == 0)
1077 msg_puts((char_u *)"\n"); /* don't overwrite this */
1078
1079 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 --no_wait_return;
1081 }
1082
1083 /*
1084 * 2. Execute one '|' separated command.
1085 * do_one_cmd() will return NULL if there is no trailing '|'.
1086 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1087 */
1088 ++recursive;
1089 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1090#ifdef FEAT_EVAL
1091 &cstack,
1092#endif
1093 cmd_getline, cmd_cookie);
1094 --recursive;
1095
1096#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cmd_cookie == (void *)&cmd_loop_cookie)
1098 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#endif
1102
1103 if (next_cmdline == NULL)
1104 {
1105 vim_free(cmdline_copy);
1106 cmdline_copy = NULL;
1107#ifdef FEAT_CMDHIST
1108 /*
1109 * If the command was typed, remember it for the ':' register.
1110 * Do this AFTER executing the command to make :@: work.
1111 */
1112 if (getline_equal(getline, cookie, getexline)
1113 && new_last_cmdline != NULL)
1114 {
1115 vim_free(last_cmdline);
1116 last_cmdline = new_last_cmdline;
1117 new_last_cmdline = NULL;
1118 }
1119#endif
1120 }
1121 else
1122 {
1123 /* need to copy the command after the '|' to cmdline_copy, for the
1124 * next do_one_cmd() */
1125 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1126 next_cmdline = cmdline_copy;
1127 }
1128
1129
1130#ifdef FEAT_EVAL
1131 /* reset did_emsg for a function that is not aborted by an error */
1132 if (did_emsg && !force_abort
1133 && getline_equal(getline, cookie, get_func_line)
1134 && !func_has_abort(real_cookie))
1135 did_emsg = FALSE;
1136
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
1139 ++current_line;
1140
1141 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001142 * An ":endwhile", ":endfor" and ":continue" is handled here.
1143 * If we were executing commands, jump back to the ":while" or
1144 * ":for".
1145 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001149 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001151 /* Jump back to the matching ":while" or ":for". Be careful
1152 * not to use a cs_line[] from an entry that isn't a ":while"
1153 * or ":for": It would make "current_line" invalid and can
1154 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (!did_emsg && !got_int && !did_throw
1156 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 && (cstack.cs_flags[cstack.cs_idx]
1158 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 && cstack.cs_line[cstack.cs_idx] >= 0
1160 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1161 {
1162 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 /* remember we jumped there */
1164 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 line_breakcheck(); /* check if CTRL-C typed */
1166
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 /* Check for the next breakpoint at or after the ":while"
1168 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (breakpoint != NULL)
1170 {
1171 *breakpoint = dbg_find_breakpoint(
1172 getline_equal(getline, cookie, getsourceline),
1173 fname,
1174 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1175 *dbg_tick = debug_tick;
1176 }
1177 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001182 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1183 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 }
1186
1187 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1194 }
1195 }
1196
1197 /*
1198 * When not inside any ":while" loop, clear remembered lines.
1199 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
1202 if (lines_ga.ga_len > 0)
1203 {
1204 sourcing_lnum =
1205 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1206 free_cmdlines(&lines_ga);
1207 }
1208 current_line = 0;
1209 }
1210
1211 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1213 * being restored at the ":endtry". Reset them here and set the
1214 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1215 * This includes the case where a missing ":endif", ":endwhile" or
1216 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 cstack.cs_lflags &= ~CSL_HAD_FINA;
1221 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1222 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 did_throw ? (void *)current_exception : NULL);
1224 did_emsg = got_int = did_throw = FALSE;
1225 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1226 }
1227
1228 /* Update global "trylevel" for recursive calls to do_cmdline() from
1229 * within this loop. */
1230 trylevel = initial_trylevel + cstack.cs_trylevel;
1231
1232 /*
1233 * If the outermost try conditional (accross function calls and sourced
1234 * files) is aborted because of an error, an interrupt, or an uncaught
1235 * exception, cancel everything. If it is left normally, reset
1236 * force_abort to get the non-EH compatible abortion behavior for
1237 * the rest of the script.
1238 */
1239 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1240 force_abort = FALSE;
1241
1242 /* Convert an interrupt to an exception if appropriate. */
1243 (void)do_intthrow(&cstack);
1244#endif /* FEAT_EVAL */
1245
1246 }
1247 /*
1248 * Continue executing command lines when:
1249 * - no CTRL-C typed, no aborting error, no exception thrown or try
1250 * conditionals need to be checked for executing finally clauses or
1251 * catching an interrupt exception
1252 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * looping for ":source" command or function call.
1255 */
1256 while (!((got_int
1257#ifdef FEAT_EVAL
1258 || (did_emsg && force_abort) || did_throw
1259#endif
1260 )
1261#ifdef FEAT_EVAL
1262 && cstack.cs_trylevel == 0
1263#endif
1264 )
1265 && !(did_emsg && used_getline
1266 && (getline_equal(getline, cookie, getexmodeline)
1267 || getline_equal(getline, cookie, getexline)))
1268 && (next_cmdline != NULL
1269#ifdef FEAT_EVAL
1270 || cstack.cs_idx >= 0
1271#endif
1272 || (flags & DOCMD_REPEAT)));
1273
1274 vim_free(cmdline_copy);
1275#ifdef FEAT_EVAL
1276 free_cmdlines(&lines_ga);
1277 ga_clear(&lines_ga);
1278
1279 if (cstack.cs_idx >= 0)
1280 {
1281 /*
1282 * If a sourced file or executed function ran to its end, report the
1283 * unclosed conditional.
1284 */
1285 if (!got_int && !did_throw
1286 && ((getline_equal(getline, cookie, getsourceline)
1287 && !source_finished(getline, cookie))
1288 || (getline_equal(getline, cookie, get_func_line)
1289 && !func_has_ended(real_cookie))))
1290 {
1291 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1292 EMSG(_(e_endtry));
1293 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1294 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001295 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1296 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 else
1298 EMSG(_(e_endif));
1299 }
1300
1301 /*
1302 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1303 * ":endtry" in a sourced file or executed function. If the try
1304 * conditional is in its finally clause, ignore anything pending.
1305 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001306 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 */
1308 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001309 {
1310 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1311
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001312 if (idx >= 0)
1313 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001314 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1315 &cstack.cs_looplevel);
1316 }
1317 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 trylevel = initial_trylevel;
1319 }
1320
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001321 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1322 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 * exception, do this now after rewinding the cstack. */
1324 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1325 ? (char_u *)"endfunction" : (char_u *)NULL);
1326
1327 if (trylevel == 0)
1328 {
1329 /*
1330 * When an exception is being thrown out of the outermost try
1331 * conditional, discard the uncaught exception, disable the conversion
1332 * of interrupts or errors to exceptions, and ensure that no more
1333 * commands are executed.
1334 */
1335 if (did_throw)
1336 {
1337 void *p = NULL;
1338 char_u *saved_sourcing_name;
1339 int saved_sourcing_lnum;
1340 struct msglist *messages = NULL, *next;
1341
1342 /*
1343 * If the uncaught exception is a user exception, report it as an
1344 * error. If it is an error exception, display the saved error
1345 * message now. For an interrupt exception, do nothing; the
1346 * interrupt message is given elsewhere.
1347 */
1348 switch (current_exception->type)
1349 {
1350 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001351 vim_snprintf((char *)IObuff, IOSIZE,
1352 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 current_exception->value);
1354 p = vim_strsave(IObuff);
1355 break;
1356 case ET_ERROR:
1357 messages = current_exception->messages;
1358 current_exception->messages = NULL;
1359 break;
1360 case ET_INTERRUPT:
1361 break;
1362 default:
1363 p = vim_strsave((char_u *)_(e_internal));
1364 }
1365
1366 saved_sourcing_name = sourcing_name;
1367 saved_sourcing_lnum = sourcing_lnum;
1368 sourcing_name = current_exception->throw_name;
1369 sourcing_lnum = current_exception->throw_lnum;
1370 current_exception->throw_name = NULL;
1371
1372 discard_current_exception(); /* uses IObuff if 'verbose' */
1373 suppress_errthrow = TRUE;
1374 force_abort = TRUE;
1375
1376 if (messages != NULL)
1377 {
1378 do
1379 {
1380 next = messages->next;
1381 emsg(messages->msg);
1382 vim_free(messages->msg);
1383 vim_free(messages);
1384 messages = next;
1385 }
1386 while (messages != NULL);
1387 }
1388 else if (p != NULL)
1389 {
1390 emsg(p);
1391 vim_free(p);
1392 }
1393 vim_free(sourcing_name);
1394 sourcing_name = saved_sourcing_name;
1395 sourcing_lnum = saved_sourcing_lnum;
1396 }
1397
1398 /*
1399 * On an interrupt or an aborting error not converted to an exception,
1400 * disable the conversion of errors to exceptions. (Interrupts are not
1401 * converted any more, here.) This enables also the interrupt message
1402 * when force_abort is set and did_emsg unset in case of an interrupt
1403 * from a finally clause after an error.
1404 */
1405 else if (got_int || (did_emsg && force_abort))
1406 suppress_errthrow = TRUE;
1407 }
1408
1409 /*
1410 * The current cstack will be freed when do_cmdline() returns. An uncaught
1411 * exception will have to be rethrown in the previous cstack. If a function
1412 * has just returned or a script file was just finished and the previous
1413 * cstack belongs to the same function or, respectively, script file, it
1414 * will have to be checked for finally clauses to be executed due to the
1415 * ":return" or ":finish". This is done in do_one_cmd().
1416 */
1417 if (did_throw)
1418 need_rethrow = TRUE;
1419 if ((getline_equal(getline, cookie, getsourceline)
1420 && ex_nesting_level > source_level(real_cookie))
1421 || (getline_equal(getline, cookie, get_func_line)
1422 && ex_nesting_level > func_level(real_cookie) + 1))
1423 {
1424 if (!did_throw)
1425 check_cstack = TRUE;
1426 }
1427 else
1428 {
1429 /* When leaving a function, reduce nesting level. */
1430 if (getline_equal(getline, cookie, get_func_line))
1431 --ex_nesting_level;
1432 /*
1433 * Go to debug mode when returning from a function in which we are
1434 * single-stepping.
1435 */
1436 if ((getline_equal(getline, cookie, getsourceline)
1437 || getline_equal(getline, cookie, get_func_line))
1438 && ex_nesting_level + 1 <= debug_break_level)
1439 do_debug(getline_equal(getline, cookie, getsourceline)
1440 ? (char_u *)_("End of sourced file")
1441 : (char_u *)_("End of function"));
1442 }
1443
1444 /*
1445 * Restore the exception environment (done after returning from the
1446 * debugger).
1447 */
1448 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001449 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451 msg_list = saved_msg_list;
1452#endif /* FEAT_EVAL */
1453
1454 /*
1455 * If there was too much output to fit on the command line, ask the user to
1456 * hit return before redrawing the screen. With the ":global" command we do
1457 * this only once after the command is finished.
1458 */
1459 if (did_inc)
1460 {
1461 --RedrawingDisabled;
1462 --no_wait_return;
1463 msg_scroll = FALSE;
1464
1465 /*
1466 * When just finished an ":if"-":else" which was typed, no need to
1467 * wait for hit-return. Also for an error situation.
1468 */
1469 if (retval == FAIL
1470#ifdef FEAT_EVAL
1471 || (did_endif && KeyTyped && !did_emsg)
1472#endif
1473 )
1474 {
1475 need_wait_return = FALSE;
1476 msg_didany = FALSE; /* don't wait when restarting edit */
1477 }
1478 else if (need_wait_return)
1479 {
1480 /*
1481 * The msg_start() above clears msg_didout. The wait_return we do
1482 * here should not overwrite the command that may be shown before
1483 * doing that.
1484 */
1485 msg_didout |= msg_didout_before_start;
1486 wait_return(FALSE);
1487 }
1488 }
1489
1490#ifndef FEAT_EVAL
1491 /*
1492 * Reset if_level, in case a sourced script file contains more ":if" than
1493 * ":endif" (could be ":if x | foo | endif").
1494 */
1495 if_level = 0;
1496#endif
1497
1498 --call_depth;
1499 return retval;
1500}
1501
1502#ifdef FEAT_EVAL
1503/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 */
1506 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001507get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 int c;
1509 void *cookie;
1510 int indent;
1511{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001512 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 wcmd_T *wp;
1514 char_u *line;
1515
1516 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1517 {
1518 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001519 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001521 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (cp->getline == NULL)
1523 line = getcmdline(c, 0L, indent);
1524 else
1525 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 ++cp->current_line;
1528
1529 return line;
1530 }
1531
1532 KeyTyped = FALSE;
1533 ++cp->current_line;
1534 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1535 sourcing_lnum = wp->lnum;
1536 return vim_strsave(wp->line);
1537}
1538
1539/*
1540 * Store a line in "gap" so that a ":while" loop can execute it again.
1541 */
1542 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001543store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 garray_T *gap;
1545 char_u *line;
1546{
1547 if (ga_grow(gap, 1) == FAIL)
1548 return FAIL;
1549 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1550 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1551 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 return OK;
1553}
1554
1555/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001556 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
1558 static void
1559free_cmdlines(gap)
1560 garray_T *gap;
1561{
1562 while (gap->ga_len > 0)
1563 {
1564 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1565 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567}
1568#endif
1569
1570/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001571 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 * "func". * Otherwise return TRUE when "getline" equals "func".
1573 */
1574/*ARGSUSED*/
1575 int
1576getline_equal(getline, cookie, func)
1577 char_u *(*getline) __ARGS((int, void *, int));
1578 void *cookie; /* argument for getline() */
1579 char_u *(*func) __ARGS((int, void *, int));
1580{
1581#ifdef FEAT_EVAL
1582 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001583 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001585 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 * function that's orignally used to obtain the lines. This may be nested
1587 * several levels. */
1588 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 cp = (struct loop_cookie *)cookie;
1590 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {
1592 gp = cp->getline;
1593 cp = cp->cookie;
1594 }
1595 return gp == func;
1596#else
1597 return getline == func;
1598#endif
1599}
1600
1601#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1602/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001603 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 * getline function. Otherwise return "cookie".
1605 */
1606/*ARGSUSED*/
1607 void *
1608getline_cookie(getline, cookie)
1609 char_u *(*getline) __ARGS((int, void *, int));
1610 void *cookie; /* argument for getline() */
1611{
1612# ifdef FEAT_EVAL
1613 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001614 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001616 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 * cookie that's orignally used to obtain the lines. This may be nested
1618 * several levels. */
1619 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001620 cp = (struct loop_cookie *)cookie;
1621 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 gp = cp->getline;
1624 cp = cp->cookie;
1625 }
1626 return cp;
1627# else
1628 return cookie;
1629# endif
1630}
1631#endif
1632
1633/*
1634 * Execute one Ex command.
1635 *
1636 * If 'sourcing' is TRUE, the command will be included in the error message.
1637 *
1638 * 1. skip comment lines and leading space
1639 * 2. handle command modifiers
1640 * 3. parse range
1641 * 4. parse command
1642 * 5. parse arguments
1643 * 6. switch on command name
1644 *
1645 * Note: "getline" can be NULL.
1646 *
1647 * This function may be called recursively!
1648 */
1649#if (_MSC_VER == 1200)
1650/*
Bram Moolenaared203462004-06-16 11:19:22 +00001651 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001653 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
1655 static char_u *
1656do_one_cmd(cmdlinep, sourcing,
1657#ifdef FEAT_EVAL
1658 cstack,
1659#endif
1660 getline, cookie)
1661 char_u **cmdlinep;
1662 int sourcing;
1663#ifdef FEAT_EVAL
1664 struct condstack *cstack;
1665#endif
1666 char_u *(*getline) __ARGS((int, void *, int));
1667 void *cookie; /* argument for getline() */
1668{
1669 char_u *p;
1670 linenr_T lnum;
1671 long n;
1672 char_u *errormsg = NULL; /* error message */
1673 exarg_T ea; /* Ex command arguments */
1674 long verbose_save = -1;
1675 int save_msg_scroll = 0;
1676 int did_silent = 0;
1677 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001678#ifdef HAVE_SANDBOX
1679 int did_sandbox = FALSE;
1680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 cmdmod_T save_cmdmod;
1682 int ni; /* set when Not Implemented */
1683
1684 vim_memset(&ea, 0, sizeof(ea));
1685 ea.line1 = 1;
1686 ea.line2 = 1;
1687#ifdef FEAT_EVAL
1688 ++ex_nesting_level;
1689#endif
1690
1691 /* when not editing the last file :q has to be typed twice */
1692 if (quitmore
1693#ifdef FEAT_EVAL
1694 /* avoid that a function call in 'statusline' does this */
1695 && !getline_equal(getline, cookie, get_func_line)
1696#endif
1697 )
1698 --quitmore;
1699
1700 /*
1701 * Reset browse, confirm, etc.. They are restored when returning, for
1702 * recursive calls.
1703 */
1704 save_cmdmod = cmdmod;
1705 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1706
1707 /*
1708 * Repeat until no more command modifiers are found.
1709 */
1710 ea.cmd = *cmdlinep;
1711 for (;;)
1712 {
1713/*
1714 * 1. skip comment lines and leading white space and colons
1715 */
1716 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1717 ++ea.cmd;
1718
1719 /* in ex mode, an empty line works like :+ */
1720 if (*ea.cmd == NUL && exmode_active
1721 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001722 || getline_equal(getline, cookie, getexline))
1723 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
1725 ea.cmd = (char_u *)"+";
1726 ex_pressedreturn = TRUE;
1727 }
1728
1729 /* ignore comment and empty lines */
1730 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001731 {
1732 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
1736/*
1737 * 2. handle command modifiers.
1738 */
1739 p = ea.cmd;
1740 if (VIM_ISDIGIT(*ea.cmd))
1741 p = skipwhite(skipdigits(ea.cmd));
1742 switch (*p)
1743 {
1744 /* When adding an entry, also modify cmd_exists(). */
1745 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1746 break;
1747#ifdef FEAT_WINDOWS
1748 cmdmod.split |= WSP_ABOVE;
1749#endif
1750 continue;
1751
1752 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1753 {
1754#ifdef FEAT_WINDOWS
1755 cmdmod.split |= WSP_BELOW;
1756#endif
1757 continue;
1758 }
1759 if (checkforcmd(&ea.cmd, "browse", 3))
1760 {
1761#ifdef FEAT_BROWSE
1762 cmdmod.browse = TRUE;
1763#endif
1764 continue;
1765 }
1766 if (!checkforcmd(&ea.cmd, "botright", 2))
1767 break;
1768#ifdef FEAT_WINDOWS
1769 cmdmod.split |= WSP_BOT;
1770#endif
1771 continue;
1772
1773 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1774 break;
1775#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1776 cmdmod.confirm = TRUE;
1777#endif
1778 continue;
1779
1780 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1781 {
1782 cmdmod.keepmarks = TRUE;
1783 continue;
1784 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001785 if (checkforcmd(&ea.cmd, "keepalt", 5))
1786 {
1787 cmdmod.keepalt = TRUE;
1788 continue;
1789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1791 break;
1792 cmdmod.keepjumps = TRUE;
1793 continue;
1794
1795 /* ":hide" and ":hide | cmd" are not modifiers */
1796 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1797 || *p == NUL || ends_excmd(*p))
1798 break;
1799 ea.cmd = p;
1800 cmdmod.hide = TRUE;
1801 continue;
1802
1803 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1804 {
1805 cmdmod.lockmarks = TRUE;
1806 continue;
1807 }
1808
1809 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1810 break;
1811#ifdef FEAT_WINDOWS
1812 cmdmod.split |= WSP_ABOVE;
1813#endif
1814 continue;
1815
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001816 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1817 break;
1818#ifdef FEAT_AUTOCMD
1819 if (cmdmod.save_ei == NULL)
1820 {
1821 /* Set 'eventignore' to "all". Don't free the
1822 * existing option value, we restore it later. */
1823 cmdmod.save_ei = vim_strsave(p_ei);
1824 set_string_option_direct((char_u *)"ei", -1,
1825 (char_u *)"all", OPT_FREE);
1826 }
1827#endif
1828 continue;
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1831 break;
1832#ifdef FEAT_WINDOWS
1833 cmdmod.split |= WSP_BELOW;
1834#endif
1835 continue;
1836
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001837 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1838 {
1839#ifdef HAVE_SANDBOX
1840 if (!did_sandbox)
1841 ++sandbox;
1842 did_sandbox = TRUE;
1843#endif
1844 continue;
1845 }
1846 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 break;
1848 ++did_silent;
1849 ++msg_silent;
1850 save_msg_scroll = msg_scroll;
1851 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1852 {
1853 /* ":silent!", but not "silent !cmd" */
1854 ea.cmd = skipwhite(ea.cmd + 1);
1855 ++emsg_silent;
1856 ++did_esilent;
1857 }
1858 continue;
1859
1860 case 't': if (!checkforcmd(&ea.cmd, "topleft", 2))
1861 break;
1862#ifdef FEAT_WINDOWS
1863 cmdmod.split |= WSP_TOP;
1864#endif
1865 continue;
1866
1867 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1868 {
1869#ifdef FEAT_VERTSPLIT
1870 cmdmod.split |= WSP_VERT;
1871#endif
1872 continue;
1873 }
1874 if (!checkforcmd(&p, "verbose", 4))
1875 break;
1876 if (verbose_save < 0)
1877 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001878 if (vim_isdigit(*ea.cmd))
1879 p_verbose = atoi((char *)ea.cmd);
1880 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 p_verbose = 1;
1882 ea.cmd = p;
1883 continue;
1884 }
1885 break;
1886 }
1887
1888#ifdef FEAT_EVAL
1889 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1890 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1891#else
1892 ea.skip = (if_level > 0);
1893#endif
1894
1895#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001896# ifdef FEAT_PROFILE
1897 /* Count this line for profiling if ea.skip is FALSE. */
1898 if (do_profiling && !ea.skip)
1899 {
1900 if (getline_equal(getline, cookie, get_func_line))
1901 func_line_exec(getline_cookie(getline, cookie));
1902 else if (getline_equal(getline, cookie, getsourceline))
1903 script_line_exec();
1904 }
1905#endif
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 /* May go to debug mode. If this happens and the ">quit" debug command is
1908 * used, throw an interrupt exception and skip the next command. */
1909 dbg_check_breakpoint(&ea);
1910 if (!ea.skip && got_int)
1911 {
1912 ea.skip = TRUE;
1913 (void)do_intthrow(cstack);
1914 }
1915#endif
1916
1917/*
1918 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1919 *
1920 * where 'addr' is:
1921 *
1922 * % (entire file)
1923 * $ [+-NUM]
1924 * 'x [+-NUM] (where x denotes a currently defined mark)
1925 * . [+-NUM]
1926 * [+-NUM]..
1927 * NUM
1928 *
1929 * The ea.cmd pointer is updated to point to the first character following the
1930 * range spec. If an initial address is found, but no second, the upper bound
1931 * is equal to the lower.
1932 */
1933
1934 /* repeat for all ',' or ';' separated addresses */
1935 for (;;)
1936 {
1937 ea.line1 = ea.line2;
1938 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1939 ea.cmd = skipwhite(ea.cmd);
1940 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1941 if (ea.cmd == NULL) /* error detected */
1942 goto doend;
1943 if (lnum == MAXLNUM)
1944 {
1945 if (*ea.cmd == '%') /* '%' - all lines */
1946 {
1947 ++ea.cmd;
1948 ea.line1 = 1;
1949 ea.line2 = curbuf->b_ml.ml_line_count;
1950 ++ea.addr_count;
1951 }
1952 /* '*' - visual area */
1953 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1954 {
1955 pos_T *fp;
1956
1957 ++ea.cmd;
1958 if (!ea.skip)
1959 {
1960 fp = getmark('<', FALSE);
1961 if (check_mark(fp) == FAIL)
1962 goto doend;
1963 ea.line1 = fp->lnum;
1964 fp = getmark('>', FALSE);
1965 if (check_mark(fp) == FAIL)
1966 goto doend;
1967 ea.line2 = fp->lnum;
1968 ++ea.addr_count;
1969 }
1970 }
1971 }
1972 else
1973 ea.line2 = lnum;
1974 ea.addr_count++;
1975
1976 if (*ea.cmd == ';')
1977 {
1978 if (!ea.skip)
1979 curwin->w_cursor.lnum = ea.line2;
1980 }
1981 else if (*ea.cmd != ',')
1982 break;
1983 ++ea.cmd;
1984 }
1985
1986 /* One address given: set start and end lines */
1987 if (ea.addr_count == 1)
1988 {
1989 ea.line1 = ea.line2;
1990 /* ... but only implicit: really no address given */
1991 if (lnum == MAXLNUM)
1992 ea.addr_count = 0;
1993 }
1994
1995 /* Don't leave the cursor on an illegal line (caused by ';') */
1996 check_cursor_lnum();
1997
1998/*
1999 * 4. parse command
2000 */
2001
2002 /*
2003 * Skip ':' and any white space
2004 */
2005 ea.cmd = skipwhite(ea.cmd);
2006 while (*ea.cmd == ':')
2007 ea.cmd = skipwhite(ea.cmd + 1);
2008
2009 /*
2010 * If we got a line, but no command, then go to the line.
2011 * If we find a '|' or '\n' we set ea.nextcmd.
2012 */
2013 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2014 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2015 {
2016 /*
2017 * strange vi behaviour:
2018 * ":3" jumps to line 3
2019 * ":3|..." prints line 3
2020 * ":|" prints current line
2021 */
2022 if (ea.skip) /* skip this if inside :if */
2023 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002024 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026 ea.cmdidx = CMD_print;
2027 ea.argt = RANGE+COUNT+TRLBAR;
2028 if ((errormsg = invalid_range(&ea)) == NULL)
2029 {
2030 correct_range(&ea);
2031 ex_print(&ea);
2032 }
2033 }
2034 else if (ea.addr_count != 0)
2035 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002036 if (ea.line2 > curbuf->b_ml.ml_line_count)
2037 {
2038 /* With '-' in 'cpoptions' a line number past the file is an
2039 * error, otherwise put it at the end of the file. */
2040 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2041 ea.line2 = -1;
2042 else
2043 ea.line2 = curbuf->b_ml.ml_line_count;
2044 }
2045
2046 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002047 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 else
2049 {
2050 if (ea.line2 == 0)
2051 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 else
2053 curwin->w_cursor.lnum = ea.line2;
2054 beginline(BL_SOL | BL_FIX);
2055 }
2056 }
2057 goto doend;
2058 }
2059
2060 /* Find the command and let "p" point to after it. */
2061 p = find_command(&ea, NULL);
2062
2063#ifdef FEAT_USR_CMDS
2064 if (p == NULL)
2065 {
2066 if (!ea.skip)
2067 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2068 goto doend;
2069 }
2070 /* Check for wrong commands. */
2071 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2072 {
2073 errormsg = uc_fun_cmd();
2074 goto doend;
2075 }
2076#endif
2077 if (ea.cmdidx == CMD_SIZE)
2078 {
2079 if (!ea.skip)
2080 {
2081 STRCPY(IObuff, _("E492: Not an editor command"));
2082 if (!sourcing)
2083 {
2084 STRCAT(IObuff, ": ");
2085 STRNCAT(IObuff, *cmdlinep, 40);
2086 }
2087 errormsg = IObuff;
2088 }
2089 goto doend;
2090 }
2091
2092 ni = (
2093#ifdef FEAT_USR_CMDS
2094 !USER_CMDIDX(ea.cmdidx) &&
2095#endif
2096 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2097
2098#ifndef FEAT_EVAL
2099 /*
2100 * When the expression evaluation is disabled, recognize the ":if" and
2101 * ":endif" commands and ignore everything in between it.
2102 */
2103 if (ea.cmdidx == CMD_if)
2104 ++if_level;
2105 if (if_level)
2106 {
2107 if (ea.cmdidx == CMD_endif)
2108 --if_level;
2109 goto doend;
2110 }
2111
2112#endif
2113
2114 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2115 {
2116 ++p;
2117 ea.forceit = TRUE;
2118 }
2119 else
2120 ea.forceit = FALSE;
2121
2122/*
2123 * 5. parse arguments
2124 */
2125#ifdef FEAT_USR_CMDS
2126 if (!USER_CMDIDX(ea.cmdidx))
2127#endif
2128 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2129
2130 if (!ea.skip)
2131 {
2132#ifdef HAVE_SANDBOX
2133 if (sandbox != 0 && !(ea.argt & SBOXOK))
2134 {
2135 /* Command not allowed in sandbox. */
2136 errormsg = (char_u *)_(e_sandbox);
2137 goto doend;
2138 }
2139#endif
2140 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2141 {
2142 /* Command not allowed in non-'modifiable' buffer */
2143 errormsg = (char_u *)_(e_modifiable);
2144 goto doend;
2145 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002146
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002147 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148# ifdef FEAT_USR_CMDS
2149 && !USER_CMDIDX(ea.cmdidx)
2150# endif
2151 )
2152 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002153 /* Command not allowed when editing the command line. */
2154#ifdef FEAT_CMDWIN
2155 if (cmdwin_type != 0)
2156 errormsg = (char_u *)_(e_cmdwin);
2157 else
2158#endif
2159 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 goto doend;
2161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2164 {
2165 /* no range allowed */
2166 errormsg = (char_u *)_(e_norange);
2167 goto doend;
2168 }
2169 }
2170
2171 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2172 {
2173 errormsg = (char_u *)_(e_nobang);
2174 goto doend;
2175 }
2176
2177 /*
2178 * Don't complain about the range if it is not used
2179 * (could happen if line_count is accidentally set to 0).
2180 */
2181 if (!ea.skip && !ni)
2182 {
2183 /*
2184 * If the range is backwards, ask for confirmation and, if given, swap
2185 * ea.line1 & ea.line2 so it's forwards again.
2186 * When global command is busy, don't ask, will fail below.
2187 */
2188 if (!global_busy && ea.line1 > ea.line2)
2189 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002190 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002192 if (sourcing || exmode_active)
2193 {
2194 errormsg = (char_u *)_("E493: Backwards range given");
2195 goto doend;
2196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (ask_yesno((char_u *)
2198 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002199 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
2201 lnum = ea.line1;
2202 ea.line1 = ea.line2;
2203 ea.line2 = lnum;
2204 }
2205 if ((errormsg = invalid_range(&ea)) != NULL)
2206 goto doend;
2207 }
2208
2209 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2210 ea.line2 = 1;
2211
2212 correct_range(&ea);
2213
2214#ifdef FEAT_FOLDING
2215 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2216 {
2217 /* Put the first line at the start of a closed fold, put the last line
2218 * at the end of a closed fold. */
2219 (void)hasFolding(ea.line1, &ea.line1, NULL);
2220 (void)hasFolding(ea.line2, NULL, &ea.line2);
2221 }
2222#endif
2223
2224#ifdef FEAT_QUICKFIX
2225 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002226 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * option here, so things like % get expanded.
2228 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002229 p = replace_makeprg(&ea, p, cmdlinep);
2230 if (p == NULL)
2231 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#endif
2233
2234 /*
2235 * Skip to start of argument.
2236 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2237 */
2238 if (ea.cmdidx == CMD_bang)
2239 ea.arg = p;
2240 else
2241 ea.arg = skipwhite(p);
2242
2243 /*
2244 * Check for "++opt=val" argument.
2245 * Must be first, allow ":w ++enc=utf8 !cmd"
2246 */
2247 if (ea.argt & ARGOPT)
2248 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2249 if (getargopt(&ea) == FAIL && !ni)
2250 {
2251 errormsg = (char_u *)_(e_invarg);
2252 goto doend;
2253 }
2254
2255 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2256 {
2257 if (*ea.arg == '>') /* append */
2258 {
2259 if (*++ea.arg != '>') /* typed wrong */
2260 {
2261 errormsg = (char_u *)_("E494: Use w or w>>");
2262 goto doend;
2263 }
2264 ea.arg = skipwhite(ea.arg + 1);
2265 ea.append = TRUE;
2266 }
2267 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2268 {
2269 ++ea.arg;
2270 ea.usefilter = TRUE;
2271 }
2272 }
2273
2274 if (ea.cmdidx == CMD_read)
2275 {
2276 if (ea.forceit)
2277 {
2278 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2279 ea.forceit = FALSE;
2280 }
2281 else if (*ea.arg == '!') /* :r !filter */
2282 {
2283 ++ea.arg;
2284 ea.usefilter = TRUE;
2285 }
2286 }
2287
2288 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2289 {
2290 ea.amount = 1;
2291 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2292 {
2293 ++ea.arg;
2294 ++ea.amount;
2295 }
2296 ea.arg = skipwhite(ea.arg);
2297 }
2298
2299 /*
2300 * Check for "+command" argument, before checking for next command.
2301 * Don't do this for ":read !cmd" and ":write !cmd".
2302 */
2303 if ((ea.argt & EDITCMD) && !ea.usefilter)
2304 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2305
2306 /*
2307 * Check for '|' to separate commands and '"' to start comments.
2308 * Don't do this for ":read !cmd" and ":write !cmd".
2309 */
2310 if ((ea.argt & TRLBAR) && !ea.usefilter)
2311 separate_nextcmd(&ea);
2312
2313 /*
2314 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002315 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2316 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002318 else if (ea.cmdidx == CMD_bang
2319 || ea.cmdidx == CMD_global
2320 || ea.cmdidx == CMD_vglobal
2321 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
2323 for (p = ea.arg; *p; ++p)
2324 {
2325 /* Remove one backslash before a newline, so that it's possible to
2326 * pass a newline to the shell and also a newline that is preceded
2327 * with a backslash. This makes it impossible to end a shell
2328 * command in a backslash, but that doesn't appear useful.
2329 * Halving the number of backslashes is incompatible with previous
2330 * versions. */
2331 if (*p == '\\' && p[1] == '\n')
2332 mch_memmove(p, p + 1, STRLEN(p));
2333 else if (*p == '\n')
2334 {
2335 ea.nextcmd = p + 1;
2336 *p = NUL;
2337 break;
2338 }
2339 }
2340 }
2341
2342 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2343 {
2344 ea.line1 = 1;
2345 ea.line2 = curbuf->b_ml.ml_line_count;
2346 }
2347
2348 /* accept numbered register only when no count allowed (:put) */
2349 if ( (ea.argt & REGSTR)
2350 && *ea.arg != NUL
2351#ifdef FEAT_USR_CMDS
2352 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2353 && USER_CMDIDX(ea.cmdidx)))
2354 /* Do not allow register = for user commands */
2355 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2356#else
2357 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2358#endif
2359 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2360 {
2361 ea.regname = *ea.arg++;
2362#ifdef FEAT_EVAL
2363 /* for '=' register: accept the rest of the line as an expression */
2364 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2365 {
2366 set_expr_line(vim_strsave(ea.arg));
2367 ea.arg += STRLEN(ea.arg);
2368 }
2369#endif
2370 ea.arg = skipwhite(ea.arg);
2371 }
2372
2373 /*
2374 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2375 * count, it's a buffer name.
2376 */
2377 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2378 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2379 || vim_iswhite(*p)))
2380 {
2381 n = getdigits(&ea.arg);
2382 ea.arg = skipwhite(ea.arg);
2383 if (n <= 0 && !ni)
2384 {
2385 errormsg = (char_u *)_(e_zerocount);
2386 goto doend;
2387 }
2388 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2389 {
2390 ea.line2 = n;
2391 if (ea.addr_count == 0)
2392 ea.addr_count = 1;
2393 }
2394 else
2395 {
2396 ea.line1 = ea.line2;
2397 ea.line2 += n - 1;
2398 ++ea.addr_count;
2399 /*
2400 * Be vi compatible: no error message for out of range.
2401 */
2402 if (ea.line2 > curbuf->b_ml.ml_line_count)
2403 ea.line2 = curbuf->b_ml.ml_line_count;
2404 }
2405 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002406
2407 /*
2408 * Check for flags: 'l', 'p' and '#'.
2409 */
2410 if (ea.argt & EXFLAGS)
2411 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002413 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
2414 && vim_strchr((char_u *)"|\"", *ea.arg) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
2416 errormsg = (char_u *)_(e_trailing);
2417 goto doend;
2418 }
2419
2420 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2421 {
2422 errormsg = (char_u *)_(e_argreq);
2423 goto doend;
2424 }
2425
2426#ifdef FEAT_EVAL
2427 /*
2428 * Skip the command when it's not going to be executed.
2429 * The commands like :if, :endif, etc. always need to be executed.
2430 * Also make an exception for commands that handle a trailing command
2431 * themselves.
2432 */
2433 if (ea.skip)
2434 {
2435 switch (ea.cmdidx)
2436 {
2437 /* commands that need evaluation */
2438 case CMD_while:
2439 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002440 case CMD_for:
2441 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 case CMD_if:
2443 case CMD_elseif:
2444 case CMD_else:
2445 case CMD_endif:
2446 case CMD_try:
2447 case CMD_catch:
2448 case CMD_finally:
2449 case CMD_endtry:
2450 case CMD_function:
2451 break;
2452
2453 /* Commands that handle '|' themselves. Check: A command should
2454 * either have the TRLBAR flag, appear in this list or appear in
2455 * the list at ":help :bar". */
2456 case CMD_aboveleft:
2457 case CMD_and:
2458 case CMD_belowright:
2459 case CMD_botright:
2460 case CMD_browse:
2461 case CMD_call:
2462 case CMD_confirm:
2463 case CMD_delfunction:
2464 case CMD_djump:
2465 case CMD_dlist:
2466 case CMD_dsearch:
2467 case CMD_dsplit:
2468 case CMD_echo:
2469 case CMD_echoerr:
2470 case CMD_echomsg:
2471 case CMD_echon:
2472 case CMD_execute:
2473 case CMD_help:
2474 case CMD_hide:
2475 case CMD_ijump:
2476 case CMD_ilist:
2477 case CMD_isearch:
2478 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002479 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 case CMD_keepjumps:
2481 case CMD_keepmarks:
2482 case CMD_leftabove:
2483 case CMD_let:
2484 case CMD_lockmarks:
2485 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002486 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 case CMD_perl:
2488 case CMD_psearch:
2489 case CMD_python:
2490 case CMD_return:
2491 case CMD_rightbelow:
2492 case CMD_ruby:
2493 case CMD_silent:
2494 case CMD_smagic:
2495 case CMD_snomagic:
2496 case CMD_substitute:
2497 case CMD_syntax:
2498 case CMD_tcl:
2499 case CMD_throw:
2500 case CMD_tilde:
2501 case CMD_topleft:
2502 case CMD_unlet:
2503 case CMD_verbose:
2504 case CMD_vertical:
2505 break;
2506
2507 default: goto doend;
2508 }
2509 }
2510#endif
2511
2512 if (ea.argt & XFILE)
2513 {
2514 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2515 goto doend;
2516 }
2517
2518#ifdef FEAT_LISTCMDS
2519 /*
2520 * Accept buffer name. Cannot be used at the same time with a buffer
2521 * number. Don't do this for a user command.
2522 */
2523 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2524# ifdef FEAT_USR_CMDS
2525 && !USER_CMDIDX(ea.cmdidx)
2526# endif
2527 )
2528 {
2529 /*
2530 * :bdelete, :bwipeout and :bunload take several arguments, separated
2531 * by spaces: find next space (skipping over escaped characters).
2532 * The others take one argument: ignore trailing spaces.
2533 */
2534 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2535 || ea.cmdidx == CMD_bunload)
2536 p = skiptowhite_esc(ea.arg);
2537 else
2538 {
2539 p = ea.arg + STRLEN(ea.arg);
2540 while (p > ea.arg && vim_iswhite(p[-1]))
2541 --p;
2542 }
2543 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2544 if (ea.line2 < 0) /* failed */
2545 goto doend;
2546 ea.addr_count = 1;
2547 ea.arg = skipwhite(p);
2548 }
2549#endif
2550
2551/*
2552 * 6. switch on command name
2553 *
2554 * The "ea" structure holds the arguments that can be used.
2555 */
2556 ea.cmdlinep = cmdlinep;
2557 ea.getline = getline;
2558 ea.cookie = cookie;
2559#ifdef FEAT_EVAL
2560 ea.cstack = cstack;
2561#endif
2562
2563#ifdef FEAT_USR_CMDS
2564 if (USER_CMDIDX(ea.cmdidx))
2565 {
2566 /*
2567 * Execute a user-defined command.
2568 */
2569 do_ucmd(&ea);
2570 }
2571 else
2572#endif
2573 {
2574 /*
2575 * Call the function to execute the command.
2576 */
2577 ea.errmsg = NULL;
2578 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2579 if (ea.errmsg != NULL)
2580 errormsg = (char_u *)_(ea.errmsg);
2581 }
2582
2583#ifdef FEAT_EVAL
2584 /*
2585 * If the command just executed called do_cmdline(), any throw or ":return"
2586 * or ":finish" encountered there must also check the cstack of the still
2587 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2588 * exception, or reanimate a returned function or finished script file and
2589 * return or finish it again.
2590 */
2591 if (need_rethrow)
2592 do_throw(cstack);
2593 else if (check_cstack)
2594 {
2595 if (source_finished(getline, cookie))
2596 do_finish(&ea, TRUE);
2597 else if (getline_equal(getline, cookie, get_func_line)
2598 && current_func_returned())
2599 do_return(&ea, TRUE, FALSE, NULL);
2600 }
2601 need_rethrow = check_cstack = FALSE;
2602#endif
2603
2604doend:
2605 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2606 curwin->w_cursor.lnum = 1;
2607
2608 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2609 {
2610 if (sourcing)
2611 {
2612 if (errormsg != IObuff)
2613 {
2614 STRCPY(IObuff, errormsg);
2615 errormsg = IObuff;
2616 }
2617 STRCAT(errormsg, ": ");
2618 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2619 }
2620 emsg(errormsg);
2621 }
2622#ifdef FEAT_EVAL
2623 do_errthrow(cstack,
2624 (ea.cmdidx != CMD_SIZE
2625# ifdef FEAT_USR_CMDS
2626 && !USER_CMDIDX(ea.cmdidx)
2627# endif
2628 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2629#endif
2630
2631 if (verbose_save >= 0)
2632 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002633#ifdef FEAT_AUTOCMD
2634 if (cmdmod.save_ei != NULL)
2635 {
2636 /* Restore 'eventignore' to the value before ":noautocmd". */
2637 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei, OPT_FREE);
2638 free_string_option(cmdmod.save_ei);
2639 }
2640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 cmdmod = save_cmdmod;
2643
2644 if (did_silent > 0)
2645 {
2646 /* messages could be enabled for a serious error, need to check if the
2647 * counters don't become negative */
2648 msg_silent -= did_silent;
2649 if (msg_silent < 0)
2650 msg_silent = 0;
2651 emsg_silent -= did_esilent;
2652 if (emsg_silent < 0)
2653 emsg_silent = 0;
2654 /* Restore msg_scroll, it's set by file I/O commands, even when no
2655 * message is actually displayed. */
2656 msg_scroll = save_msg_scroll;
2657 }
2658
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002659#ifdef HAVE_SANDBOX
2660 if (did_sandbox)
2661 --sandbox;
2662#endif
2663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2665 ea.nextcmd = NULL;
2666
2667#ifdef FEAT_EVAL
2668 --ex_nesting_level;
2669#endif
2670
2671 return ea.nextcmd;
2672}
2673#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002674 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675#endif
2676
2677/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002678 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 * If there is a match advance "pp" to the argument and return TRUE.
2680 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002681 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002683 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 char *cmd; /* name of command */
2685 int len; /* required length */
2686{
2687 int i;
2688
2689 for (i = 0; cmd[i] != NUL; ++i)
2690 if (cmd[i] != (*pp)[i])
2691 break;
2692 if (i >= len && !isalpha((*pp)[i]))
2693 {
2694 *pp = skipwhite(*pp + i);
2695 return TRUE;
2696 }
2697 return FALSE;
2698}
2699
2700/*
2701 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002702 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002704 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 * Returns NULL for an ambiguous user command.
2706 */
2707/*ARGSUSED*/
2708 static char_u *
2709find_command(eap, full)
2710 exarg_T *eap;
2711 int *full;
2712{
2713 int len;
2714 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002715 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 /*
2718 * Isolate the command and search for it in the command table.
2719 * Exeptions:
2720 * - the 'k' command can directly be followed by any character.
2721 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2722 * but :sre[wind] is another command, as are :scrip[tnames],
2723 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002724 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 */
2726 p = eap->cmd;
2727 if (*p == 'k')
2728 {
2729 eap->cmdidx = CMD_k;
2730 ++p;
2731 }
2732 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002733 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2734 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 || p[1] == 'g'
2736 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2737 || p[1] == 'I'
2738 || (p[1] == 'r' && p[2] != 'e')))
2739 {
2740 eap->cmdidx = CMD_substitute;
2741 ++p;
2742 }
2743 else
2744 {
2745 while (ASCII_ISALPHA(*p))
2746 ++p;
2747 /* check for non-alpha command */
2748 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2749 ++p;
2750 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002751 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2752 {
2753 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2754 * :delete with the 'l' flag. Same for 'p'. */
2755 for (i = 0; i < len; ++i)
2756 if (eap->cmd[i] != "delete"[i])
2757 break;
2758 if (i == len - 1)
2759 {
2760 --len;
2761 if (p[-1] == 'l')
2762 eap->flags |= EXFLAG_LIST;
2763 else
2764 eap->flags |= EXFLAG_PRINT;
2765 }
2766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
2768 if (ASCII_ISLOWER(*eap->cmd))
2769 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2770 else
2771 eap->cmdidx = cmdidxs[26];
2772
2773 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2774 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2775 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2776 (size_t)len) == 0)
2777 {
2778#ifdef FEAT_EVAL
2779 if (full != NULL
2780 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2781 *full = TRUE;
2782#endif
2783 break;
2784 }
2785
2786#ifdef FEAT_USR_CMDS
2787 /* Look for a user defined command as a last resort */
2788 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2789 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002790 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 while (ASCII_ISALNUM(*p))
2792 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002793 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 }
2795#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002796 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 eap->cmdidx = CMD_SIZE;
2798 }
2799
2800 return p;
2801}
2802
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002803#ifdef FEAT_USR_CMDS
2804/*
2805 * Search for a user command that matches "eap->cmd".
2806 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2807 * Return a pointer to just after the command.
2808 * Return NULL if there is no matching command.
2809 */
2810 static char_u *
2811find_ucmd(eap, p, full, xp, compl)
2812 exarg_T *eap;
2813 char_u *p; /* end of the command (possibly including count) */
2814 int *full; /* set to TRUE for a full match */
2815 expand_T *xp; /* used for completion, NULL otherwise */
2816 int *compl; /* completion flags or NULL */
2817{
2818 int len = (int)(p - eap->cmd);
2819 int j, k, matchlen = 0;
2820 ucmd_T *uc;
2821 int found = FALSE;
2822 int possible = FALSE;
2823 char_u *cp, *np; /* Point into typed cmd and test name */
2824 garray_T *gap;
2825 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2826 only full match global is accepted. */
2827
2828 /*
2829 * Look for buffer-local user commands first, then global ones.
2830 */
2831 gap = &curbuf->b_ucmds;
2832 for (;;)
2833 {
2834 for (j = 0; j < gap->ga_len; ++j)
2835 {
2836 uc = USER_CMD_GA(gap, j);
2837 cp = eap->cmd;
2838 np = uc->uc_name;
2839 k = 0;
2840 while (k < len && *np != NUL && *cp++ == *np++)
2841 k++;
2842 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2843 {
2844 /* If finding a second match, the command is ambiguous. But
2845 * not if a buffer-local command wasn't a full match and a
2846 * global command is a full match. */
2847 if (k == len && found && *np != NUL)
2848 {
2849 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002850 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002851 amb_local = TRUE;
2852 }
2853
2854 if (!found || (k == len && *np == NUL))
2855 {
2856 /* If we matched up to a digit, then there could
2857 * be another command including the digit that we
2858 * should use instead.
2859 */
2860 if (k == len)
2861 found = TRUE;
2862 else
2863 possible = TRUE;
2864
2865 if (gap == &ucmds)
2866 eap->cmdidx = CMD_USER;
2867 else
2868 eap->cmdidx = CMD_USER_BUF;
2869 eap->argt = uc->uc_argt;
2870 eap->useridx = j;
2871
2872# ifdef FEAT_CMDL_COMPL
2873 if (compl != NULL)
2874 *compl = uc->uc_compl;
2875# ifdef FEAT_EVAL
2876 if (xp != NULL)
2877 {
2878 xp->xp_arg = uc->uc_compl_arg;
2879 xp->xp_scriptID = uc->uc_scriptID;
2880 }
2881# endif
2882# endif
2883 /* Do not search for further abbreviations
2884 * if this is an exact match. */
2885 matchlen = k;
2886 if (k == len && *np == NUL)
2887 {
2888 if (full != NULL)
2889 *full = TRUE;
2890 amb_local = FALSE;
2891 break;
2892 }
2893 }
2894 }
2895 }
2896
2897 /* Stop if we found a full match or searched all. */
2898 if (j < gap->ga_len || gap == &ucmds)
2899 break;
2900 gap = &ucmds;
2901 }
2902
2903 /* Only found ambiguous matches. */
2904 if (amb_local)
2905 {
2906 if (xp != NULL)
2907 xp->xp_context = EXPAND_UNSUCCESSFUL;
2908 return NULL;
2909 }
2910
2911 /* The match we found may be followed immediately by a number. Move "p"
2912 * back to point to it. */
2913 if (found || possible)
2914 return p + (matchlen - len);
2915 return p;
2916}
2917#endif
2918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919#if defined(FEAT_EVAL) || defined(PROTO)
2920/*
2921 * Return > 0 if an Ex command "name" exists.
2922 * Return 2 if there is an exact match.
2923 * Return 3 if there is an ambiguous match.
2924 */
2925 int
2926cmd_exists(name)
2927 char_u *name;
2928{
2929 exarg_T ea;
2930 int full = FALSE;
2931 int i;
2932 int j;
2933 static struct cmdmod
2934 {
2935 char *name;
2936 int minlen;
2937 } cmdmods[] = {
2938 {"aboveleft", 3},
2939 {"belowright", 3},
2940 {"botright", 2},
2941 {"browse", 3},
2942 {"confirm", 4},
2943 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002944 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 {"keepjumps", 5},
2946 {"keepmarks", 3},
2947 {"leftabove", 5},
2948 {"lockmarks", 3},
2949 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002950 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {"silent", 3},
2952 {"topleft", 2},
2953 {"verbose", 4},
2954 {"vertical", 4},
2955 };
2956
2957 /* Check command modifiers. */
2958 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2959 {
2960 for (j = 0; name[j] != NUL; ++j)
2961 if (name[j] != cmdmods[i].name[j])
2962 break;
2963 if (name[j] == NUL && j >= cmdmods[i].minlen)
2964 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2965 }
2966
2967 /* Check built-in commands and user defined commands. */
2968 ea.cmd = name;
2969 ea.cmdidx = (cmdidx_T)0;
2970 if (find_command(&ea, &full) == NULL)
2971 return 3;
2972 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2973}
2974#endif
2975
2976/*
2977 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2978 * we don't need/want deleted. Maybe this could be done better if we didn't
2979 * repeat all this stuff. The only problem is that they may not stay
2980 * perfectly compatible with each other, but then the command line syntax
2981 * probably won't change that much -- webb.
2982 */
2983 char_u *
2984set_one_cmd_context(xp, buff)
2985 expand_T *xp;
2986 char_u *buff; /* buffer for command string */
2987{
2988 char_u *p;
2989 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002990 int len = 0;
2991 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
2993 int compl = EXPAND_NOTHING;
2994#endif
2995#ifdef FEAT_CMDL_COMPL
2996 int delim;
2997#endif
2998 int forceit = FALSE;
2999 int usefilter = FALSE; /* filter instead of file name */
3000
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003001 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 xp->xp_pattern = buff;
3003 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003004 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005
3006/*
3007 * 2. skip comment lines and leading space, colons or bars
3008 */
3009 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3010 ;
3011 xp->xp_pattern = cmd;
3012
3013 if (*cmd == NUL)
3014 return NULL;
3015 if (*cmd == '"') /* ignore comment lines */
3016 {
3017 xp->xp_context = EXPAND_NOTHING;
3018 return NULL;
3019 }
3020
3021/*
3022 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3023 */
3024 cmd = skip_range(cmd, &xp->xp_context);
3025
3026/*
3027 * 4. parse command
3028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 xp->xp_pattern = cmd;
3030 if (*cmd == NUL)
3031 return NULL;
3032 if (*cmd == '"')
3033 {
3034 xp->xp_context = EXPAND_NOTHING;
3035 return NULL;
3036 }
3037
3038 if (*cmd == '|' || *cmd == '\n')
3039 return cmd + 1; /* There's another command */
3040
3041 /*
3042 * Isolate the command and search for it in the command table.
3043 * Exceptions:
3044 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003045 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3047 */
3048 if (*cmd == 'k' && cmd[1] != 'e')
3049 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003050 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 p = cmd + 1;
3052 }
3053 else
3054 {
3055 p = cmd;
3056 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3057 ++p;
3058 /* check for non-alpha command */
3059 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3060 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003061 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003063 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 {
3065 xp->xp_context = EXPAND_UNSUCCESSFUL;
3066 return NULL;
3067 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003068 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3069 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3070 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 break;
3072
3073#ifdef FEAT_USR_CMDS
3074 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3075 {
3076 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3077 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003078 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 }
3080#endif
3081 }
3082
3083 /*
3084 * If the cursor is touching the command, and it ends in an alpha-numeric
3085 * character, complete the command name.
3086 */
3087 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3088 return NULL;
3089
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003090 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 {
3092 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3093 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003094 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 p = cmd + 1;
3096 }
3097#ifdef FEAT_USR_CMDS
3098 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3099 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003100 ea.cmd = cmd;
3101 p = find_ucmd(&ea, p, NULL, xp,
3102# if defined(FEAT_CMDL_COMPL)
3103 &compl
3104# else
3105 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003107 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003108 if (p == NULL)
3109 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
3111#endif
3112 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003113 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
3115 /* Not still touching the command and it was an illegal one */
3116 xp->xp_context = EXPAND_UNSUCCESSFUL;
3117 return NULL;
3118 }
3119
3120 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3121
3122 if (*p == '!') /* forced commands */
3123 {
3124 forceit = TRUE;
3125 ++p;
3126 }
3127
3128/*
3129 * 5. parse arguments
3130 */
3131#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003132 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003134 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 arg = skipwhite(p);
3137
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003138 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 if (*arg == '>') /* append */
3141 {
3142 if (*++arg == '>')
3143 ++arg;
3144 arg = skipwhite(arg);
3145 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 ++arg;
3149 usefilter = TRUE;
3150 }
3151 }
3152
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003153 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155 usefilter = forceit; /* :r! filter if forced */
3156 if (*arg == '!') /* :r !filter */
3157 {
3158 ++arg;
3159 usefilter = TRUE;
3160 }
3161 }
3162
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003163 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
3165 while (*arg == *cmd) /* allow any number of '>' or '<' */
3166 ++arg;
3167 arg = skipwhite(arg);
3168 }
3169
3170 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003171 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
3173 /* Check if we're in the +command */
3174 p = arg + 1;
3175 arg = skip_cmd_arg(arg, FALSE);
3176
3177 /* Still touching the command after '+'? */
3178 if (*arg == NUL)
3179 return p;
3180
3181 /* Skip space(s) after +command to get to the real argument */
3182 arg = skipwhite(arg);
3183 }
3184
3185 /*
3186 * Check for '|' to separate commands and '"' to start comments.
3187 * Don't do this for ":read !cmd" and ":write !cmd".
3188 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003189 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 p = arg;
3192 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 p += 2;
3195 while (*p)
3196 {
3197 if (*p == Ctrl_V)
3198 {
3199 if (p[1] != NUL)
3200 ++p;
3201 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003202 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 || *p == '|' || *p == '\n')
3204 {
3205 if (*(p - 1) != '\\')
3206 {
3207 if (*p == '|' || *p == '\n')
3208 return p + 1;
3209 return NULL; /* It's a comment */
3210 }
3211 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003212 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 }
3214 }
3215
3216 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003217 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 vim_strchr((char_u *)"|\"", *arg) == NULL)
3219 return NULL;
3220
3221 /* Find start of last argument (argument just before cursor): */
3222 p = buff + STRLEN(buff);
3223 while (p != arg && *p != ' ' && *p != TAB)
3224 p--;
3225 if (*p == ' ' || *p == TAB)
3226 p++;
3227 xp->xp_pattern = p;
3228
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003229 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 int in_quote = FALSE;
3232 char_u *bow = NULL; /* Beginning of word */
3233
3234 /*
3235 * Allow spaces within back-quotes to count as part of the argument
3236 * being expanded.
3237 */
3238 xp->xp_pattern = skipwhite(arg);
3239 for (p = xp->xp_pattern; *p; )
3240 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003241 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 ++p;
3243#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003244 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245#else
3246 else if (vim_iswhite(*p))
3247#endif
3248 {
3249 p = skipwhite(p);
3250 if (in_quote)
3251 bow = p;
3252 else
3253 xp->xp_pattern = p;
3254 --p;
3255 }
3256 else if (*p == '`')
3257 {
3258 if (!in_quote)
3259 {
3260 xp->xp_pattern = p;
3261 bow = p + 1;
3262 }
3263 in_quote = !in_quote;
3264 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003265 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267
3268 /*
3269 * If we are still inside the quotes, and we passed a space, just
3270 * expand from there.
3271 */
3272 if (bow != NULL && in_quote)
3273 xp->xp_pattern = bow;
3274 xp->xp_context = EXPAND_FILES;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003275#ifndef BACKSLASH_IN_FILENAME
3276 /* For a shell command more chars need to be escaped. */
3277 if (usefilter || ea.cmdidx == CMD_bang)
3278 xp->xp_shell = TRUE;
3279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281 /* Check for environment variable */
3282 if (*xp->xp_pattern == '$'
3283#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3284 || *xp->xp_pattern == '%'
3285#endif
3286 )
3287 {
3288 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3289 if (!vim_isIDc(*p))
3290 break;
3291 if (*p == NUL)
3292 {
3293 xp->xp_context = EXPAND_ENV_VARS;
3294 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003295#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3296 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003297 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003298 compl = EXPAND_ENV_VARS;
3299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 }
3302 }
3303
3304/*
3305 * 6. switch on command name
3306 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003307 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 case CMD_cd:
3310 case CMD_chdir:
3311 case CMD_lcd:
3312 case CMD_lchdir:
3313 if (xp->xp_context == EXPAND_FILES)
3314 xp->xp_context = EXPAND_DIRECTORIES;
3315 break;
3316 case CMD_help:
3317 xp->xp_context = EXPAND_HELP;
3318 xp->xp_pattern = arg;
3319 break;
3320
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003321 /* Command modifiers: return the argument.
3322 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003324 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 case CMD_belowright:
3326 case CMD_botright:
3327 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003328 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003330 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 case CMD_folddoclosed:
3332 case CMD_folddoopen:
3333 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003334 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 case CMD_keepjumps:
3336 case CMD_keepmarks:
3337 case CMD_leftabove:
3338 case CMD_lockmarks:
3339 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003340 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 case CMD_silent:
3342 case CMD_topleft:
3343 case CMD_verbose:
3344 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003345 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 return arg;
3347
3348#ifdef FEAT_SEARCH_EXTRA
3349 case CMD_match:
3350 if (*arg == NUL || !ends_excmd(*arg))
3351 {
3352 /* Dummy call to clear variables. */
3353 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3354 xp->xp_context = EXPAND_HIGHLIGHT;
3355 xp->xp_pattern = arg;
3356 arg = skipwhite(skiptowhite(arg));
3357 if (*arg != NUL)
3358 {
3359 xp->xp_context = EXPAND_NOTHING;
3360 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3361 }
3362 }
3363 return find_nextcmd(arg);
3364#endif
3365
3366#ifdef FEAT_CMDL_COMPL
3367/*
3368 * All completion for the +cmdline_compl feature goes here.
3369 */
3370
3371# ifdef FEAT_USR_CMDS
3372 case CMD_command:
3373 /* Check for attributes */
3374 while (*arg == '-')
3375 {
3376 arg++; /* Skip "-" */
3377 p = skiptowhite(arg);
3378 if (*p == NUL)
3379 {
3380 /* Cursor is still in the attribute */
3381 p = vim_strchr(arg, '=');
3382 if (p == NULL)
3383 {
3384 /* No "=", so complete attribute names */
3385 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3386 xp->xp_pattern = arg;
3387 return NULL;
3388 }
3389
3390 /* For the -complete and -nargs attributes, we complete
3391 * their arguments as well.
3392 */
3393 if (STRNICMP(arg, "complete", p - arg) == 0)
3394 {
3395 xp->xp_context = EXPAND_USER_COMPLETE;
3396 xp->xp_pattern = p + 1;
3397 return NULL;
3398 }
3399 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3400 {
3401 xp->xp_context = EXPAND_USER_NARGS;
3402 xp->xp_pattern = p + 1;
3403 return NULL;
3404 }
3405 return NULL;
3406 }
3407 arg = skipwhite(p);
3408 }
3409
3410 /* After the attributes comes the new command name */
3411 p = skiptowhite(arg);
3412 if (*p == NUL)
3413 {
3414 xp->xp_context = EXPAND_USER_COMMANDS;
3415 xp->xp_pattern = arg;
3416 break;
3417 }
3418
3419 /* And finally comes a normal command */
3420 return skipwhite(p);
3421
3422 case CMD_delcommand:
3423 xp->xp_context = EXPAND_USER_COMMANDS;
3424 xp->xp_pattern = arg;
3425 break;
3426# endif
3427
3428 case CMD_global:
3429 case CMD_vglobal:
3430 delim = *arg; /* get the delimiter */
3431 if (delim)
3432 ++arg; /* skip delimiter if there is one */
3433
3434 while (arg[0] != NUL && arg[0] != delim)
3435 {
3436 if (arg[0] == '\\' && arg[1] != NUL)
3437 ++arg;
3438 ++arg;
3439 }
3440 if (arg[0] != NUL)
3441 return arg + 1;
3442 break;
3443 case CMD_and:
3444 case CMD_substitute:
3445 delim = *arg;
3446 if (delim)
3447 {
3448 /* skip "from" part */
3449 ++arg;
3450 arg = skip_regexp(arg, delim, p_magic, NULL);
3451 }
3452 /* skip "to" part */
3453 while (arg[0] != NUL && arg[0] != delim)
3454 {
3455 if (arg[0] == '\\' && arg[1] != NUL)
3456 ++arg;
3457 ++arg;
3458 }
3459 if (arg[0] != NUL) /* skip delimiter */
3460 ++arg;
3461 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3462 ++arg;
3463 if (arg[0] != NUL)
3464 return arg;
3465 break;
3466 case CMD_isearch:
3467 case CMD_dsearch:
3468 case CMD_ilist:
3469 case CMD_dlist:
3470 case CMD_ijump:
3471 case CMD_psearch:
3472 case CMD_djump:
3473 case CMD_isplit:
3474 case CMD_dsplit:
3475 arg = skipwhite(skipdigits(arg)); /* skip count */
3476 if (*arg == '/') /* Match regexp, not just whole words */
3477 {
3478 for (++arg; *arg && *arg != '/'; arg++)
3479 if (*arg == '\\' && arg[1] != NUL)
3480 arg++;
3481 if (*arg)
3482 {
3483 arg = skipwhite(arg + 1);
3484
3485 /* Check for trailing illegal characters */
3486 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3487 xp->xp_context = EXPAND_NOTHING;
3488 else
3489 return arg;
3490 }
3491 }
3492 break;
3493#ifdef FEAT_AUTOCMD
3494 case CMD_autocmd:
3495 return set_context_in_autocmd(xp, arg, FALSE);
3496
3497 case CMD_doautocmd:
3498 return set_context_in_autocmd(xp, arg, TRUE);
3499#endif
3500 case CMD_set:
3501 set_context_in_set_cmd(xp, arg, 0);
3502 break;
3503 case CMD_setglobal:
3504 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3505 break;
3506 case CMD_setlocal:
3507 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3508 break;
3509 case CMD_tag:
3510 case CMD_stag:
3511 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003512 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 case CMD_tselect:
3514 case CMD_stselect:
3515 case CMD_ptselect:
3516 case CMD_tjump:
3517 case CMD_stjump:
3518 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003519 if (*p_wop != NUL)
3520 xp->xp_context = EXPAND_TAGS_LISTFILES;
3521 else
3522 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 xp->xp_pattern = arg;
3524 break;
3525 case CMD_augroup:
3526 xp->xp_context = EXPAND_AUGROUP;
3527 xp->xp_pattern = arg;
3528 break;
3529#ifdef FEAT_SYN_HL
3530 case CMD_syntax:
3531 set_context_in_syntax_cmd(xp, arg);
3532 break;
3533#endif
3534#ifdef FEAT_EVAL
3535 case CMD_let:
3536 case CMD_if:
3537 case CMD_elseif:
3538 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003539 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 case CMD_echo:
3541 case CMD_echon:
3542 case CMD_execute:
3543 case CMD_echomsg:
3544 case CMD_echoerr:
3545 case CMD_call:
3546 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003547 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 break;
3549
3550 case CMD_unlet:
3551 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3552 arg = xp->xp_pattern + 1;
3553 xp->xp_context = EXPAND_USER_VARS;
3554 xp->xp_pattern = arg;
3555 break;
3556
3557 case CMD_function:
3558 case CMD_delfunction:
3559 xp->xp_context = EXPAND_USER_FUNC;
3560 xp->xp_pattern = arg;
3561 break;
3562
3563 case CMD_echohl:
3564 xp->xp_context = EXPAND_HIGHLIGHT;
3565 xp->xp_pattern = arg;
3566 break;
3567#endif
3568 case CMD_highlight:
3569 set_context_in_highlight_cmd(xp, arg);
3570 break;
3571#ifdef FEAT_LISTCMDS
3572 case CMD_bdelete:
3573 case CMD_bwipeout:
3574 case CMD_bunload:
3575 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3576 arg = xp->xp_pattern + 1;
3577 /*FALLTHROUGH*/
3578 case CMD_buffer:
3579 case CMD_sbuffer:
3580 case CMD_checktime:
3581 xp->xp_context = EXPAND_BUFFERS;
3582 xp->xp_pattern = arg;
3583 break;
3584#endif
3585#ifdef FEAT_USR_CMDS
3586 case CMD_USER:
3587 case CMD_USER_BUF:
3588 if (compl != EXPAND_NOTHING)
3589 {
3590 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003591 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
3593# ifdef FEAT_MENU
3594 if (compl == EXPAND_MENUS)
3595 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3596# endif
3597 if (compl == EXPAND_COMMANDS)
3598 return arg;
3599 if (compl == EXPAND_MAPPINGS)
3600 return set_context_in_map_cmd(xp, (char_u *)"map",
3601 arg, forceit, FALSE, FALSE, CMD_map);
3602 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3603 arg = xp->xp_pattern + 1;
3604 xp->xp_pattern = arg;
3605 }
3606 xp->xp_context = compl;
3607 }
3608 break;
3609#endif
3610 case CMD_map: case CMD_noremap:
3611 case CMD_nmap: case CMD_nnoremap:
3612 case CMD_vmap: case CMD_vnoremap:
3613 case CMD_omap: case CMD_onoremap:
3614 case CMD_imap: case CMD_inoremap:
3615 case CMD_cmap: case CMD_cnoremap:
3616 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003617 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 case CMD_unmap:
3619 case CMD_nunmap:
3620 case CMD_vunmap:
3621 case CMD_ounmap:
3622 case CMD_iunmap:
3623 case CMD_cunmap:
3624 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003625 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 case CMD_abbreviate: case CMD_noreabbrev:
3627 case CMD_cabbrev: case CMD_cnoreabbrev:
3628 case CMD_iabbrev: case CMD_inoreabbrev:
3629 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003630 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 case CMD_unabbreviate:
3632 case CMD_cunabbrev:
3633 case CMD_iunabbrev:
3634 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636#ifdef FEAT_MENU
3637 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3638 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3639 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3640 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3641 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3642 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3643 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3644 case CMD_tmenu: case CMD_tunmenu:
3645 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3646 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3647#endif
3648
3649 case CMD_colorscheme:
3650 xp->xp_context = EXPAND_COLORS;
3651 xp->xp_pattern = arg;
3652 break;
3653
3654 case CMD_compiler:
3655 xp->xp_context = EXPAND_COMPILER;
3656 xp->xp_pattern = arg;
3657 break;
3658
3659#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3660 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3661 case CMD_language:
3662 if (*skiptowhite(arg) == NUL)
3663 {
3664 xp->xp_context = EXPAND_LANGUAGE;
3665 xp->xp_pattern = arg;
3666 }
3667 else
3668 xp->xp_context = EXPAND_NOTHING;
3669 break;
3670#endif
3671
3672#endif /* FEAT_CMDL_COMPL */
3673
3674 default:
3675 break;
3676 }
3677 return NULL;
3678}
3679
3680/*
3681 * skip a range specifier of the form: addr [,addr] [;addr] ..
3682 *
3683 * Backslashed delimiters after / or ? will be skipped, and commands will
3684 * not be expanded between /'s and ?'s or after "'".
3685 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003686 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 * Returns the "cmd" pointer advanced to beyond the range.
3688 */
3689 char_u *
3690skip_range(cmd, ctx)
3691 char_u *cmd;
3692 int *ctx; /* pointer to xp_context or NULL */
3693{
3694 int delim;
3695
Bram Moolenaardf177f62005-02-22 08:39:57 +00003696 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
3698 if (*cmd == '\'')
3699 {
3700 if (*++cmd == NUL && ctx != NULL)
3701 *ctx = EXPAND_NOTHING;
3702 }
3703 else if (*cmd == '/' || *cmd == '?')
3704 {
3705 delim = *cmd++;
3706 while (*cmd != NUL && *cmd != delim)
3707 if (*cmd++ == '\\' && *cmd != NUL)
3708 ++cmd;
3709 if (*cmd == NUL && ctx != NULL)
3710 *ctx = EXPAND_NOTHING;
3711 }
3712 if (*cmd != NUL)
3713 ++cmd;
3714 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003715
3716 /* Skip ":" and white space. */
3717 while (*cmd == ':')
3718 cmd = skipwhite(cmd + 1);
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 return cmd;
3721}
3722
3723/*
3724 * get a single EX address
3725 *
3726 * Set ptr to the next character after the part that was interpreted.
3727 * Set ptr to NULL when an error is encountered.
3728 *
3729 * Return MAXLNUM when no Ex address was found.
3730 */
3731 static linenr_T
3732get_address(ptr, skip, to_other_file)
3733 char_u **ptr;
3734 int skip; /* only skip the address, don't use it */
3735 int to_other_file; /* flag: may jump to other file */
3736{
3737 int c;
3738 int i;
3739 long n;
3740 char_u *cmd;
3741 pos_T pos;
3742 pos_T *fp;
3743 linenr_T lnum;
3744
3745 cmd = skipwhite(*ptr);
3746 lnum = MAXLNUM;
3747 do
3748 {
3749 switch (*cmd)
3750 {
3751 case '.': /* '.' - Cursor position */
3752 ++cmd;
3753 lnum = curwin->w_cursor.lnum;
3754 break;
3755
3756 case '$': /* '$' - last line */
3757 ++cmd;
3758 lnum = curbuf->b_ml.ml_line_count;
3759 break;
3760
3761 case '\'': /* ''' - mark */
3762 if (*++cmd == NUL)
3763 {
3764 cmd = NULL;
3765 goto error;
3766 }
3767 if (skip)
3768 ++cmd;
3769 else
3770 {
3771 /* Only accept a mark in another file when it is
3772 * used by itself: ":'M". */
3773 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3774 ++cmd;
3775 if (fp == (pos_T *)-1)
3776 /* Jumped to another file. */
3777 lnum = curwin->w_cursor.lnum;
3778 else
3779 {
3780 if (check_mark(fp) == FAIL)
3781 {
3782 cmd = NULL;
3783 goto error;
3784 }
3785 lnum = fp->lnum;
3786 }
3787 }
3788 break;
3789
3790 case '/':
3791 case '?': /* '/' or '?' - search */
3792 c = *cmd++;
3793 if (skip) /* skip "/pat/" */
3794 {
3795 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3796 if (*cmd == c)
3797 ++cmd;
3798 }
3799 else
3800 {
3801 pos = curwin->w_cursor; /* save curwin->w_cursor */
3802 /*
3803 * When '/' or '?' follows another address, start
3804 * from there.
3805 */
3806 if (lnum != MAXLNUM)
3807 curwin->w_cursor.lnum = lnum;
3808 /*
3809 * Start a forward search at the end of the line.
3810 * Start a backward search at the start of the line.
3811 * This makes sure we never match in the current
3812 * line, and can match anywhere in the
3813 * next/previous line.
3814 */
3815 if (c == '/')
3816 curwin->w_cursor.col = MAXCOL;
3817 else
3818 curwin->w_cursor.col = 0;
3819 searchcmdlen = 0;
3820 if (!do_search(NULL, c, cmd, 1L,
3821 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3822 {
3823 curwin->w_cursor = pos;
3824 cmd = NULL;
3825 goto error;
3826 }
3827 lnum = curwin->w_cursor.lnum;
3828 curwin->w_cursor = pos;
3829 /* adjust command string pointer */
3830 cmd += searchcmdlen;
3831 }
3832 break;
3833
3834 case '\\': /* "\?", "\/" or "\&", repeat search */
3835 ++cmd;
3836 if (*cmd == '&')
3837 i = RE_SUBST;
3838 else if (*cmd == '?' || *cmd == '/')
3839 i = RE_SEARCH;
3840 else
3841 {
3842 EMSG(_(e_backslash));
3843 cmd = NULL;
3844 goto error;
3845 }
3846
3847 if (!skip)
3848 {
3849 /*
3850 * When search follows another address, start from
3851 * there.
3852 */
3853 if (lnum != MAXLNUM)
3854 pos.lnum = lnum;
3855 else
3856 pos.lnum = curwin->w_cursor.lnum;
3857
3858 /*
3859 * Start the search just like for the above
3860 * do_search().
3861 */
3862 if (*cmd != '?')
3863 pos.col = MAXCOL;
3864 else
3865 pos.col = 0;
3866 if (searchit(curwin, curbuf, &pos,
3867 *cmd == '?' ? BACKWARD : FORWARD,
3868 (char_u *)"", 1L,
3869 SEARCH_MSG + SEARCH_START, i) != FAIL)
3870 lnum = pos.lnum;
3871 else
3872 {
3873 cmd = NULL;
3874 goto error;
3875 }
3876 }
3877 ++cmd;
3878 break;
3879
3880 default:
3881 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3882 lnum = getdigits(&cmd);
3883 }
3884
3885 for (;;)
3886 {
3887 cmd = skipwhite(cmd);
3888 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3889 break;
3890
3891 if (lnum == MAXLNUM)
3892 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3893 if (VIM_ISDIGIT(*cmd))
3894 i = '+'; /* "number" is same as "+number" */
3895 else
3896 i = *cmd++;
3897 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3898 n = 1;
3899 else
3900 n = getdigits(&cmd);
3901 if (i == '-')
3902 lnum -= n;
3903 else
3904 lnum += n;
3905 }
3906 } while (*cmd == '/' || *cmd == '?');
3907
3908error:
3909 *ptr = cmd;
3910 return lnum;
3911}
3912
3913/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003914 * Get flags from an Ex command argument.
3915 */
3916 static void
3917get_flags(eap)
3918 exarg_T *eap;
3919{
3920 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3921 {
3922 if (*eap->arg == 'l')
3923 eap->flags |= EXFLAG_LIST;
3924 else if (*eap->arg == 'p')
3925 eap->flags |= EXFLAG_PRINT;
3926 else
3927 eap->flags |= EXFLAG_NR;
3928 eap->arg = skipwhite(eap->arg + 1);
3929 }
3930}
3931
3932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 * Function called for command which is Not Implemented. NI!
3934 */
3935 void
3936ex_ni(eap)
3937 exarg_T *eap;
3938{
3939 if (!eap->skip)
3940 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3941}
3942
3943#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003944 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945/*
3946 * Function called for script command which is Not Implemented. NI!
3947 * Skips over ":perl <<EOF" constructs.
3948 */
3949 static void
3950ex_script_ni(eap)
3951 exarg_T *eap;
3952{
3953 if (!eap->skip)
3954 ex_ni(eap);
3955 else
3956 vim_free(script_get(eap, eap->arg));
3957}
3958#endif
3959
3960/*
3961 * Check range in Ex command for validity.
3962 * Return NULL when valid, error message when invalid.
3963 */
3964 static char_u *
3965invalid_range(eap)
3966 exarg_T *eap;
3967{
3968 if ( eap->line1 < 0
3969 || eap->line2 < 0
3970 || eap->line1 > eap->line2
3971 || ((eap->argt & RANGE)
3972 && !(eap->argt & NOTADR)
3973 && eap->line2 > curbuf->b_ml.ml_line_count
3974#ifdef FEAT_DIFF
3975 + (eap->cmdidx == CMD_diffget)
3976#endif
3977 ))
3978 return (char_u *)_(e_invrange);
3979 return NULL;
3980}
3981
3982/*
3983 * Correct the range for zero line number, if required.
3984 */
3985 static void
3986correct_range(eap)
3987 exarg_T *eap;
3988{
3989 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
3990 {
3991 if (eap->line1 == 0)
3992 eap->line1 = 1;
3993 if (eap->line2 == 0)
3994 eap->line2 = 1;
3995 }
3996}
3997
Bram Moolenaar748bf032005-02-02 23:04:36 +00003998#ifdef FEAT_QUICKFIX
3999static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4000
4001/*
4002 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4003 * pattern. Otherwise return eap->arg.
4004 */
4005 static char_u *
4006skip_grep_pat(eap)
4007 exarg_T *eap;
4008{
4009 char_u *p = eap->arg;
4010
Bram Moolenaara37420f2006-02-04 22:37:47 +00004011 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4012 || eap->cmdidx == CMD_vimgrepadd
4013 || eap->cmdidx == CMD_lvimgrepadd
4014 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004015 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004016 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004017 if (p == NULL)
4018 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004019 }
4020 return p;
4021}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004022
4023/*
4024 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4025 * in the command line, so that things like % get expanded.
4026 */
4027 static char_u *
4028replace_makeprg(eap, p, cmdlinep)
4029 exarg_T *eap;
4030 char_u *p;
4031 char_u **cmdlinep;
4032{
4033 char_u *new_cmdline;
4034 char_u *program;
4035 char_u *pos;
4036 char_u *ptr;
4037 int len;
4038 int i;
4039
4040 /*
4041 * Don't do it when ":vimgrep" is used for ":grep".
4042 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004043 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4044 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4045 || eap->cmdidx == CMD_grepadd
4046 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004047 && !grep_internal(eap->cmdidx))
4048 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004049 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4050 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004051 {
4052 if (*curbuf->b_p_gp == NUL)
4053 program = p_gp;
4054 else
4055 program = curbuf->b_p_gp;
4056 }
4057 else
4058 {
4059 if (*curbuf->b_p_mp == NUL)
4060 program = p_mp;
4061 else
4062 program = curbuf->b_p_mp;
4063 }
4064
4065 p = skipwhite(p);
4066
4067 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4068 {
4069 /* replace $* by given arguments */
4070 i = 1;
4071 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4072 ++i;
4073 len = (int)STRLEN(p);
4074 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4075 if (new_cmdline == NULL)
4076 return NULL; /* out of memory */
4077 ptr = new_cmdline;
4078 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4079 {
4080 i = (int)(pos - program);
4081 STRNCPY(ptr, program, i);
4082 STRCPY(ptr += i, p);
4083 ptr += len;
4084 program = pos + 2;
4085 }
4086 STRCPY(ptr, program);
4087 }
4088 else
4089 {
4090 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4091 if (new_cmdline == NULL)
4092 return NULL; /* out of memory */
4093 STRCPY(new_cmdline, program);
4094 STRCAT(new_cmdline, " ");
4095 STRCAT(new_cmdline, p);
4096 }
4097 msg_make(p);
4098
4099 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4100 vim_free(*cmdlinep);
4101 *cmdlinep = new_cmdline;
4102 p = new_cmdline;
4103 }
4104 return p;
4105}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004106#endif
4107
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108/*
4109 * Expand file name in Ex command argument.
4110 * Return FAIL for failure, OK otherwise.
4111 */
4112 int
4113expand_filename(eap, cmdlinep, errormsgp)
4114 exarg_T *eap;
4115 char_u **cmdlinep;
4116 char_u **errormsgp;
4117{
4118 int has_wildcards; /* need to expand wildcards */
4119 char_u *repl;
4120 int srclen;
4121 char_u *p;
4122 int n;
4123
Bram Moolenaar748bf032005-02-02 23:04:36 +00004124#ifdef FEAT_QUICKFIX
4125 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4126 p = skip_grep_pat(eap);
4127#else
4128 p = eap->arg;
4129#endif
4130
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 /*
4132 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4133 * the file name contains a wildcard it should not cause expanding.
4134 * (it will be expanded anyway if there is a wildcard before replacing).
4135 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004136 has_wildcards = mch_has_wildcard(p);
4137 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004139#ifdef FEAT_EVAL
4140 /* Skip over `=expr`, wildcards in it are not expanded. */
4141 if (p[0] == '`' && p[1] == '=')
4142 {
4143 p += 2;
4144 (void)skip_expr(&p);
4145 if (*p == '`')
4146 ++p;
4147 continue;
4148 }
4149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 /*
4151 * Quick check if this cannot be the start of a special string.
4152 * Also removes backslash before '%', '#' and '<'.
4153 */
4154 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4155 {
4156 ++p;
4157 continue;
4158 }
4159
4160 /*
4161 * Try to find a match at this position.
4162 */
4163 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4164 if (*errormsgp != NULL) /* error detected */
4165 return FAIL;
4166 if (repl == NULL) /* no match found */
4167 {
4168 p += srclen;
4169 continue;
4170 }
4171
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004172 /* Wildcards won't be expanded below, the replacement is taken
4173 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4174 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4175 {
4176 char_u *l = repl;
4177
4178 repl = expand_env_save(repl);
4179 vim_free(l);
4180 }
4181
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 /* Need to escape white space et al. with a backslash. Don't do this
4183 * for shell commands (may have to use quotes instead). Don't do this
4184 * for non-unix systems when there is a single argument (spaces don't
4185 * separate arguments then). */
4186 if (!eap->usefilter
4187 && eap->cmdidx != CMD_bang
4188 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004189 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004191 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004193 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194#ifndef UNIX
4195 && !(eap->argt & NOSPC)
4196#endif
4197 )
4198 {
4199 char_u *l;
4200#ifdef BACKSLASH_IN_FILENAME
4201 /* Don't escape a backslash here, because rem_backslash() doesn't
4202 * remove it later. */
4203 static char_u *nobslash = (char_u *)" \t\"|";
4204# define ESCAPE_CHARS nobslash
4205#else
4206# define ESCAPE_CHARS escape_chars
4207#endif
4208
4209 for (l = repl; *l; ++l)
4210 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4211 {
4212 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4213 if (l != NULL)
4214 {
4215 vim_free(repl);
4216 repl = l;
4217 }
4218 break;
4219 }
4220 }
4221
4222 /* For a shell command a '!' must be escaped. */
4223 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004224 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
4226 char_u *l;
4227
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004228 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (l != NULL)
4230 {
4231 vim_free(repl);
4232 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004233 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 if (strstr((char *)p_sh, "sh") != NULL)
4235 {
4236 l = vim_strsave_escaped(repl, (char_u *)"!");
4237 if (l != NULL)
4238 {
4239 vim_free(repl);
4240 repl = l;
4241 }
4242 }
4243 }
4244 }
4245
4246 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4247 vim_free(repl);
4248 if (p == NULL)
4249 return FAIL;
4250 }
4251
4252 /*
4253 * One file argument: Expand wildcards.
4254 * Don't do this with ":r !command" or ":w !command".
4255 */
4256 if ((eap->argt & NOSPC) && !eap->usefilter)
4257 {
4258 /*
4259 * May do this twice:
4260 * 1. Replace environment variables.
4261 * 2. Replace any other wildcards, remove backslashes.
4262 */
4263 for (n = 1; n <= 2; ++n)
4264 {
4265 if (n == 2)
4266 {
4267#ifdef UNIX
4268 /*
4269 * Only for Unix we check for more than one file name.
4270 * For other systems spaces are considered to be part
4271 * of the file name.
4272 * Only check here if there is no wildcard, otherwise
4273 * ExpandOne() will check for errors. This allows
4274 * ":e `ls ve*.c`" on Unix.
4275 */
4276 if (!has_wildcards)
4277 for (p = eap->arg; *p; ++p)
4278 {
4279 /* skip escaped characters */
4280 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4281 ++p;
4282 else if (vim_iswhite(*p))
4283 {
4284 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4285 return FAIL;
4286 }
4287 }
4288#endif
4289
4290 /*
4291 * Halve the number of backslashes (this is Vi compatible).
4292 * For Unix and OS/2, when wildcards are expanded, this is
4293 * done by ExpandOne() below.
4294 */
4295#if defined(UNIX) || defined(OS2)
4296 if (!has_wildcards)
4297#endif
4298 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300
4301 if (has_wildcards)
4302 {
4303 if (n == 1)
4304 {
4305 /*
4306 * First loop: May expand environment variables. This
4307 * can be done much faster with expand_env() than with
4308 * something else (e.g., calling a shell).
4309 * After expanding environment variables, check again
4310 * if there are still wildcards present.
4311 */
4312 if (vim_strchr(eap->arg, '$') != NULL
4313 || vim_strchr(eap->arg, '~') != NULL)
4314 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004315 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4316 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 has_wildcards = mch_has_wildcard(NameBuff);
4318 p = NameBuff;
4319 }
4320 else
4321 p = NULL;
4322 }
4323 else /* n == 2 */
4324 {
4325 expand_T xpc;
4326
4327 ExpandInit(&xpc);
4328 xpc.xp_context = EXPAND_FILES;
4329 p = ExpandOne(&xpc, eap->arg, NULL,
4330 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4331 WILD_EXPAND_FREE);
4332 ExpandCleanup(&xpc);
4333 if (p == NULL)
4334 return FAIL;
4335 }
4336 if (p != NULL)
4337 {
4338 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4339 p, cmdlinep);
4340 if (n == 2) /* p came from ExpandOne() */
4341 vim_free(p);
4342 }
4343 }
4344 }
4345 }
4346 return OK;
4347}
4348
4349/*
4350 * Replace part of the command line, keeping eap->cmd, eap->arg and
4351 * eap->nextcmd correct.
4352 * "src" points to the part that is to be replaced, of length "srclen".
4353 * "repl" is the replacement string.
4354 * Returns a pointer to the character after the replaced string.
4355 * Returns NULL for failure.
4356 */
4357 static char_u *
4358repl_cmdline(eap, src, srclen, repl, cmdlinep)
4359 exarg_T *eap;
4360 char_u *src;
4361 int srclen;
4362 char_u *repl;
4363 char_u **cmdlinep;
4364{
4365 int len;
4366 int i;
4367 char_u *new_cmdline;
4368
4369 /*
4370 * The new command line is build in new_cmdline[].
4371 * First allocate it.
4372 * Careful: a "+cmd" argument may have been NUL terminated.
4373 */
4374 len = (int)STRLEN(repl);
4375 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4376 if (eap->nextcmd)
4377 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4378 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4379 return NULL; /* out of memory! */
4380
4381 /*
4382 * Copy the stuff before the expanded part.
4383 * Copy the expanded stuff.
4384 * Copy what came after the expanded part.
4385 * Copy the next commands, if there are any.
4386 */
4387 i = (int)(src - *cmdlinep); /* length of part before match */
4388 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 mch_memmove(new_cmdline + i, repl, (size_t)len);
4391 i += len; /* remember the end of the string */
4392 STRCPY(new_cmdline + i, src + srclen);
4393 src = new_cmdline + i; /* remember where to continue */
4394
4395 if (eap->nextcmd) /* append next command */
4396 {
4397 i = (int)STRLEN(new_cmdline) + 1;
4398 STRCPY(new_cmdline + i, eap->nextcmd);
4399 eap->nextcmd = new_cmdline + i;
4400 }
4401 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4402 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4403 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4404 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4405 vim_free(*cmdlinep);
4406 *cmdlinep = new_cmdline;
4407
4408 return src;
4409}
4410
4411/*
4412 * Check for '|' to separate commands and '"' to start comments.
4413 */
4414 void
4415separate_nextcmd(eap)
4416 exarg_T *eap;
4417{
4418 char_u *p;
4419
Bram Moolenaar86b68352004-12-27 21:59:20 +00004420#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004421 p = skip_grep_pat(eap);
4422#else
4423 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004424#endif
4425
4426 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
4428 if (*p == Ctrl_V)
4429 {
4430 if (eap->argt & (USECTRLV | XFILE))
4431 ++p; /* skip CTRL-V and next char */
4432 else
4433 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4434 if (*p == NUL) /* stop at NUL after CTRL-V */
4435 break;
4436 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004437
4438#ifdef FEAT_EVAL
4439 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004440 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004441 {
4442 p += 2;
4443 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004444 }
4445#endif
4446
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 /* Check for '"': start of comment or '|': next command */
4448 /* :@" and :*" do not start a comment!
4449 * :redir @" doesn't either. */
4450 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4451 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4452 || p != eap->arg)
4453 && (eap->cmdidx != CMD_redir
4454 || p != eap->arg + 1 || p[-1] != '@'))
4455 || *p == '|' || *p == '\n')
4456 {
4457 /*
4458 * We remove the '\' before the '|', unless USECTRLV is used
4459 * AND 'b' is present in 'cpoptions'.
4460 */
4461 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4462 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4463 {
4464 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4465 --p;
4466 }
4467 else
4468 {
4469 eap->nextcmd = check_nextcmd(p);
4470 *p = NUL;
4471 break;
4472 }
4473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4477 del_trailing_spaces(eap->arg);
4478}
4479
4480/*
4481 * get + command from ex argument
4482 */
4483 static char_u *
4484getargcmd(argp)
4485 char_u **argp;
4486{
4487 char_u *arg = *argp;
4488 char_u *command = NULL;
4489
4490 if (*arg == '+') /* +[command] */
4491 {
4492 ++arg;
4493 if (vim_isspace(*arg))
4494 command = dollar_command;
4495 else
4496 {
4497 command = arg;
4498 arg = skip_cmd_arg(command, TRUE);
4499 if (*arg != NUL)
4500 *arg++ = NUL; /* terminate command with NUL */
4501 }
4502
4503 arg = skipwhite(arg); /* skip over spaces */
4504 *argp = arg;
4505 }
4506 return command;
4507}
4508
4509/*
4510 * Find end of "+command" argument. Skip over "\ " and "\\".
4511 */
4512 static char_u *
4513skip_cmd_arg(p, rembs)
4514 char_u *p;
4515 int rembs; /* TRUE to halve the number of backslashes */
4516{
4517 while (*p && !vim_isspace(*p))
4518 {
4519 if (*p == '\\' && p[1] != NUL)
4520 {
4521 if (rembs)
4522 mch_memmove(p, p + 1, STRLEN(p));
4523 else
4524 ++p;
4525 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004526 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 return p;
4529}
4530
4531/*
4532 * Get "++opt=arg" argument.
4533 * Return FAIL or OK.
4534 */
4535 static int
4536getargopt(eap)
4537 exarg_T *eap;
4538{
4539 char_u *arg = eap->arg + 2;
4540 int *pp = NULL;
4541#ifdef FEAT_MBYTE
4542 char_u *p;
4543#endif
4544
4545 /* ":edit ++[no]bin[ary] file" */
4546 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4547 {
4548 if (*arg == 'n')
4549 {
4550 arg += 2;
4551 eap->force_bin = FORCE_NOBIN;
4552 }
4553 else
4554 eap->force_bin = FORCE_BIN;
4555 if (!checkforcmd(&arg, "binary", 3))
4556 return FAIL;
4557 eap->arg = skipwhite(arg);
4558 return OK;
4559 }
4560
4561 if (STRNCMP(arg, "ff", 2) == 0)
4562 {
4563 arg += 2;
4564 pp = &eap->force_ff;
4565 }
4566 else if (STRNCMP(arg, "fileformat", 10) == 0)
4567 {
4568 arg += 10;
4569 pp = &eap->force_ff;
4570 }
4571#ifdef FEAT_MBYTE
4572 else if (STRNCMP(arg, "enc", 3) == 0)
4573 {
4574 arg += 3;
4575 pp = &eap->force_enc;
4576 }
4577 else if (STRNCMP(arg, "encoding", 8) == 0)
4578 {
4579 arg += 8;
4580 pp = &eap->force_enc;
4581 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004582 else if (STRNCMP(arg, "bad", 3) == 0)
4583 {
4584 arg += 3;
4585 pp = &eap->bad_char;
4586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#endif
4588
4589 if (pp == NULL || *arg != '=')
4590 return FAIL;
4591
4592 ++arg;
4593 *pp = (int)(arg - eap->cmd);
4594 arg = skip_cmd_arg(arg, FALSE);
4595 eap->arg = skipwhite(arg);
4596 *arg = NUL;
4597
4598#ifdef FEAT_MBYTE
4599 if (pp == &eap->force_ff)
4600 {
4601#endif
4602 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4603 return FAIL;
4604#ifdef FEAT_MBYTE
4605 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004606 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 /* Make 'fileencoding' lower case. */
4609 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4610 *p = TOLOWER_ASC(*p);
4611 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004612 else
4613 {
4614 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4615 * "drop". */
4616 p = eap->cmd + eap->bad_char;
4617 if (STRICMP(p, "keep") == 0)
4618 eap->bad_char = BAD_KEEP;
4619 else if (STRICMP(p, "drop") == 0)
4620 eap->bad_char = BAD_DROP;
4621 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4622 eap->bad_char = *p;
4623 else
4624 return FAIL;
4625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626#endif
4627
4628 return OK;
4629}
4630
4631/*
4632 * ":abbreviate" and friends.
4633 */
4634 static void
4635ex_abbreviate(eap)
4636 exarg_T *eap;
4637{
4638 do_exmap(eap, TRUE); /* almost the same as mapping */
4639}
4640
4641/*
4642 * ":map" and friends.
4643 */
4644 static void
4645ex_map(eap)
4646 exarg_T *eap;
4647{
4648 /*
4649 * If we are sourcing .exrc or .vimrc in current directory we
4650 * print the mappings for security reasons.
4651 */
4652 if (secure)
4653 {
4654 secure = 2;
4655 msg_outtrans(eap->cmd);
4656 msg_putchar('\n');
4657 }
4658 do_exmap(eap, FALSE);
4659}
4660
4661/*
4662 * ":unmap" and friends.
4663 */
4664 static void
4665ex_unmap(eap)
4666 exarg_T *eap;
4667{
4668 do_exmap(eap, FALSE);
4669}
4670
4671/*
4672 * ":mapclear" and friends.
4673 */
4674 static void
4675ex_mapclear(eap)
4676 exarg_T *eap;
4677{
4678 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4679}
4680
4681/*
4682 * ":abclear" and friends.
4683 */
4684 static void
4685ex_abclear(eap)
4686 exarg_T *eap;
4687{
4688 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4689}
4690
4691#ifdef FEAT_AUTOCMD
4692 static void
4693ex_autocmd(eap)
4694 exarg_T *eap;
4695{
4696 /*
4697 * Disallow auto commands from .exrc and .vimrc in current
4698 * directory for security reasons.
4699 */
4700 if (secure)
4701 {
4702 secure = 2;
4703 eap->errmsg = e_curdir;
4704 }
4705 else if (eap->cmdidx == CMD_autocmd)
4706 do_autocmd(eap->arg, eap->forceit);
4707 else
4708 do_augroup(eap->arg, eap->forceit);
4709}
4710
4711/*
4712 * ":doautocmd": Apply the automatic commands to the current buffer.
4713 */
4714 static void
4715ex_doautocmd(eap)
4716 exarg_T *eap;
4717{
4718 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004719 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721#endif
4722
4723#ifdef FEAT_LISTCMDS
4724/*
4725 * :[N]bunload[!] [N] [bufname] unload buffer
4726 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4727 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4728 */
4729 static void
4730ex_bunload(eap)
4731 exarg_T *eap;
4732{
4733 eap->errmsg = do_bufdel(
4734 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4735 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4736 : DOBUF_UNLOAD, eap->arg,
4737 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4738}
4739
4740/*
4741 * :[N]buffer [N] to buffer N
4742 * :[N]sbuffer [N] to buffer N
4743 */
4744 static void
4745ex_buffer(eap)
4746 exarg_T *eap;
4747{
4748 if (*eap->arg)
4749 eap->errmsg = e_trailing;
4750 else
4751 {
4752 if (eap->addr_count == 0) /* default is current buffer */
4753 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4754 else
4755 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4756 }
4757}
4758
4759/*
4760 * :[N]bmodified [N] to next mod. buffer
4761 * :[N]sbmodified [N] to next mod. buffer
4762 */
4763 static void
4764ex_bmodified(eap)
4765 exarg_T *eap;
4766{
4767 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4768}
4769
4770/*
4771 * :[N]bnext [N] to next buffer
4772 * :[N]sbnext [N] split and to next buffer
4773 */
4774 static void
4775ex_bnext(eap)
4776 exarg_T *eap;
4777{
4778 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4779}
4780
4781/*
4782 * :[N]bNext [N] to previous buffer
4783 * :[N]bprevious [N] to previous buffer
4784 * :[N]sbNext [N] split and to previous buffer
4785 * :[N]sbprevious [N] split and to previous buffer
4786 */
4787 static void
4788ex_bprevious(eap)
4789 exarg_T *eap;
4790{
4791 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4792}
4793
4794/*
4795 * :brewind to first buffer
4796 * :bfirst to first buffer
4797 * :sbrewind split and to first buffer
4798 * :sbfirst split and to first buffer
4799 */
4800 static void
4801ex_brewind(eap)
4802 exarg_T *eap;
4803{
4804 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4805}
4806
4807/*
4808 * :blast to last buffer
4809 * :sblast split and to last buffer
4810 */
4811 static void
4812ex_blast(eap)
4813 exarg_T *eap;
4814{
4815 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4816}
4817#endif
4818
4819 int
4820ends_excmd(c)
4821 int c;
4822{
4823 return (c == NUL || c == '|' || c == '"' || c == '\n');
4824}
4825
4826#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4827 || defined(PROTO)
4828/*
4829 * Return the next command, after the first '|' or '\n'.
4830 * Return NULL if not found.
4831 */
4832 char_u *
4833find_nextcmd(p)
4834 char_u *p;
4835{
4836 while (*p != '|' && *p != '\n')
4837 {
4838 if (*p == NUL)
4839 return NULL;
4840 ++p;
4841 }
4842 return (p + 1);
4843}
4844#endif
4845
4846/*
4847 * Check if *p is a separator between Ex commands.
4848 * Return NULL if it isn't, (p + 1) if it is.
4849 */
4850 char_u *
4851check_nextcmd(p)
4852 char_u *p;
4853{
4854 p = skipwhite(p);
4855 if (*p == '|' || *p == '\n')
4856 return (p + 1);
4857 else
4858 return NULL;
4859}
4860
4861/*
4862 * - if there are more files to edit
4863 * - and this is the last window
4864 * - and forceit not used
4865 * - and not repeated twice on a row
4866 * return FAIL and give error message if 'message' TRUE
4867 * return OK otherwise
4868 */
4869 static int
4870check_more(message, forceit)
4871 int message; /* when FALSE check only, no messages */
4872 int forceit;
4873{
4874 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4875
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004876 if (!forceit && only_one_window()
4877 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
4879 if (message)
4880 {
4881#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4882 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4883 {
4884 char_u buff[IOSIZE];
4885
4886 if (n == 1)
4887 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4888 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004889 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 _("%d more files to edit. Quit anyway?"), n);
4891 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4892 return OK;
4893 return FAIL;
4894 }
4895#endif
4896 if (n == 1)
4897 EMSG(_("E173: 1 more file to edit"));
4898 else
4899 EMSGN(_("E173: %ld more files to edit"), n);
4900 quitmore = 2; /* next try to quit is allowed */
4901 }
4902 return FAIL;
4903 }
4904 return OK;
4905}
4906
4907#ifdef FEAT_CMDL_COMPL
4908/*
4909 * Function given to ExpandGeneric() to obtain the list of command names.
4910 */
4911/*ARGSUSED*/
4912 char_u *
4913get_command_name(xp, idx)
4914 expand_T *xp;
4915 int idx;
4916{
4917 if (idx >= (int)CMD_SIZE)
4918# ifdef FEAT_USR_CMDS
4919 return get_user_command_name(idx);
4920# else
4921 return NULL;
4922# endif
4923 return cmdnames[idx].cmd_name;
4924}
4925#endif
4926
4927#if defined(FEAT_USR_CMDS) || defined(PROTO)
4928static 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));
4929static void uc_list __ARGS((char_u *name, size_t name_len));
4930static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4931static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4932static 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));
4933
4934 static int
4935uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4936 char_u *name;
4937 size_t name_len;
4938 char_u *rep;
4939 long argt;
4940 long def;
4941 int flags;
4942 int compl;
4943 char_u *compl_arg;
4944 int force;
4945{
4946 ucmd_T *cmd = NULL;
4947 char_u *p;
4948 int i;
4949 int cmp = 1;
4950 char_u *rep_buf = NULL;
4951 garray_T *gap;
4952
4953 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4954 if (rep_buf == NULL)
4955 {
4956 /* Can't replace termcodes - try using the string as is */
4957 rep_buf = vim_strsave(rep);
4958
4959 /* Give up if out of memory */
4960 if (rep_buf == NULL)
4961 return FAIL;
4962 }
4963
4964 /* get address of growarray: global or in curbuf */
4965 if (flags & UC_BUFFER)
4966 {
4967 gap = &curbuf->b_ucmds;
4968 if (gap->ga_itemsize == 0)
4969 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4970 }
4971 else
4972 gap = &ucmds;
4973
4974 /* Search for the command in the already defined commands. */
4975 for (i = 0; i < gap->ga_len; ++i)
4976 {
4977 size_t len;
4978
4979 cmd = USER_CMD_GA(gap, i);
4980 len = STRLEN(cmd->uc_name);
4981 cmp = STRNCMP(name, cmd->uc_name, name_len);
4982 if (cmp == 0)
4983 {
4984 if (name_len < len)
4985 cmp = -1;
4986 else if (name_len > len)
4987 cmp = 1;
4988 }
4989
4990 if (cmp == 0)
4991 {
4992 if (!force)
4993 {
4994 EMSG(_("E174: Command already exists: add ! to replace it"));
4995 goto fail;
4996 }
4997
4998 vim_free(cmd->uc_rep);
4999 cmd->uc_rep = 0;
5000 break;
5001 }
5002
5003 /* Stop as soon as we pass the name to add */
5004 if (cmp < 0)
5005 break;
5006 }
5007
5008 /* Extend the array unless we're replacing an existing command */
5009 if (cmp != 0)
5010 {
5011 if (ga_grow(gap, 1) != OK)
5012 goto fail;
5013 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5014 goto fail;
5015
5016 cmd = USER_CMD_GA(gap, i);
5017 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5018
5019 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
5021 cmd->uc_name = p;
5022 }
5023
5024 cmd->uc_rep = rep_buf;
5025 cmd->uc_argt = argt;
5026 cmd->uc_def = def;
5027 cmd->uc_compl = compl;
5028#ifdef FEAT_EVAL
5029 cmd->uc_scriptID = current_SID;
5030# ifdef FEAT_CMDL_COMPL
5031 cmd->uc_compl_arg = compl_arg;
5032# endif
5033#endif
5034
5035 return OK;
5036
5037fail:
5038 vim_free(rep_buf);
5039#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5040 vim_free(compl_arg);
5041#endif
5042 return FAIL;
5043}
5044
5045/*
5046 * List of names for completion for ":command" with the EXPAND_ flag.
5047 * Must be alphabetical for completion.
5048 */
5049static struct
5050{
5051 int expand;
5052 char *name;
5053} command_complete[] =
5054{
5055 {EXPAND_AUGROUP, "augroup"},
5056 {EXPAND_BUFFERS, "buffer"},
5057 {EXPAND_COMMANDS, "command"},
5058#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5059 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005060 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061#endif
5062 {EXPAND_DIRECTORIES, "dir"},
5063 {EXPAND_ENV_VARS, "environment"},
5064 {EXPAND_EVENTS, "event"},
5065 {EXPAND_EXPRESSION, "expression"},
5066 {EXPAND_FILES, "file"},
5067 {EXPAND_FUNCTIONS, "function"},
5068 {EXPAND_HELP, "help"},
5069 {EXPAND_HIGHLIGHT, "highlight"},
5070 {EXPAND_MAPPINGS, "mapping"},
5071 {EXPAND_MENUS, "menu"},
5072 {EXPAND_SETTINGS, "option"},
5073 {EXPAND_TAGS, "tag"},
5074 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5075 {EXPAND_USER_VARS, "var"},
5076 {0, NULL}
5077};
5078
5079 static void
5080uc_list(name, name_len)
5081 char_u *name;
5082 size_t name_len;
5083{
5084 int i, j;
5085 int found = FALSE;
5086 ucmd_T *cmd;
5087 int len;
5088 long a;
5089 garray_T *gap;
5090
5091 gap = &curbuf->b_ucmds;
5092 for (;;)
5093 {
5094 for (i = 0; i < gap->ga_len; ++i)
5095 {
5096 cmd = USER_CMD_GA(gap, i);
5097 a = cmd->uc_argt;
5098
5099 /* Skip commands which don't match the requested prefix */
5100 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5101 continue;
5102
5103 /* Put out the title first time */
5104 if (!found)
5105 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5106 found = TRUE;
5107 msg_putchar('\n');
5108 if (got_int)
5109 break;
5110
5111 /* Special cases */
5112 msg_putchar(a & BANG ? '!' : ' ');
5113 msg_putchar(a & REGSTR ? '"' : ' ');
5114 msg_putchar(gap != &ucmds ? 'b' : ' ');
5115 msg_putchar(' ');
5116
5117 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5118 len = (int)STRLEN(cmd->uc_name) + 4;
5119
5120 do {
5121 msg_putchar(' ');
5122 ++len;
5123 } while (len < 16);
5124
5125 len = 0;
5126
5127 /* Arguments */
5128 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5129 {
5130 case 0: IObuff[len++] = '0'; break;
5131 case (EXTRA): IObuff[len++] = '*'; break;
5132 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5133 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5134 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5135 }
5136
5137 do {
5138 IObuff[len++] = ' ';
5139 } while (len < 5);
5140
5141 /* Range */
5142 if (a & (RANGE|COUNT))
5143 {
5144 if (a & COUNT)
5145 {
5146 /* -count=N */
5147 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5148 len += (int)STRLEN(IObuff + len);
5149 }
5150 else if (a & DFLALL)
5151 IObuff[len++] = '%';
5152 else if (cmd->uc_def >= 0)
5153 {
5154 /* -range=N */
5155 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5156 len += (int)STRLEN(IObuff + len);
5157 }
5158 else
5159 IObuff[len++] = '.';
5160 }
5161
5162 do {
5163 IObuff[len++] = ' ';
5164 } while (len < 11);
5165
5166 /* Completion */
5167 for (j = 0; command_complete[j].expand != 0; ++j)
5168 if (command_complete[j].expand == cmd->uc_compl)
5169 {
5170 STRCPY(IObuff + len, command_complete[j].name);
5171 len += (int)STRLEN(IObuff + len);
5172 break;
5173 }
5174
5175 do {
5176 IObuff[len++] = ' ';
5177 } while (len < 21);
5178
5179 IObuff[len] = '\0';
5180 msg_outtrans(IObuff);
5181
5182 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005183#ifdef FEAT_EVAL
5184 if (p_verbose > 0)
5185 last_set_msg(cmd->uc_scriptID);
5186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 out_flush();
5188 ui_breakcheck();
5189 if (got_int)
5190 break;
5191 }
5192 if (gap == &ucmds || i < gap->ga_len)
5193 break;
5194 gap = &ucmds;
5195 }
5196
5197 if (!found)
5198 MSG(_("No user-defined commands found"));
5199}
5200
5201 static char_u *
5202uc_fun_cmd()
5203{
5204 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5205 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5206 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5207 0xb9, 0x7f, 0};
5208 int i;
5209
5210 for (i = 0; fcmd[i]; ++i)
5211 IObuff[i] = fcmd[i] - 0x40;
5212 IObuff[i] = 0;
5213 return IObuff;
5214}
5215
5216 static int
5217uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5218 char_u *attr;
5219 size_t len;
5220 long *argt;
5221 long *def;
5222 int *flags;
5223 int *compl;
5224 char_u **compl_arg;
5225{
5226 char_u *p;
5227
5228 if (len == 0)
5229 {
5230 EMSG(_("E175: No attribute specified"));
5231 return FAIL;
5232 }
5233
5234 /* First, try the simple attributes (no arguments) */
5235 if (STRNICMP(attr, "bang", len) == 0)
5236 *argt |= BANG;
5237 else if (STRNICMP(attr, "buffer", len) == 0)
5238 *flags |= UC_BUFFER;
5239 else if (STRNICMP(attr, "register", len) == 0)
5240 *argt |= REGSTR;
5241 else if (STRNICMP(attr, "bar", len) == 0)
5242 *argt |= TRLBAR;
5243 else
5244 {
5245 int i;
5246 char_u *val = NULL;
5247 size_t vallen = 0;
5248 size_t attrlen = len;
5249
5250 /* Look for the attribute name - which is the part before any '=' */
5251 for (i = 0; i < (int)len; ++i)
5252 {
5253 if (attr[i] == '=')
5254 {
5255 val = &attr[i + 1];
5256 vallen = len - i - 1;
5257 attrlen = i;
5258 break;
5259 }
5260 }
5261
5262 if (STRNICMP(attr, "nargs", attrlen) == 0)
5263 {
5264 if (vallen == 1)
5265 {
5266 if (*val == '0')
5267 /* Do nothing - this is the default */;
5268 else if (*val == '1')
5269 *argt |= (EXTRA | NOSPC | NEEDARG);
5270 else if (*val == '*')
5271 *argt |= EXTRA;
5272 else if (*val == '?')
5273 *argt |= (EXTRA | NOSPC);
5274 else if (*val == '+')
5275 *argt |= (EXTRA | NEEDARG);
5276 else
5277 goto wrong_nargs;
5278 }
5279 else
5280 {
5281wrong_nargs:
5282 EMSG(_("E176: Invalid number of arguments"));
5283 return FAIL;
5284 }
5285 }
5286 else if (STRNICMP(attr, "range", attrlen) == 0)
5287 {
5288 *argt |= RANGE;
5289 if (vallen == 1 && *val == '%')
5290 *argt |= DFLALL;
5291 else if (val != NULL)
5292 {
5293 p = val;
5294 if (*def >= 0)
5295 {
5296two_count:
5297 EMSG(_("E177: Count cannot be specified twice"));
5298 return FAIL;
5299 }
5300
5301 *def = getdigits(&p);
5302 *argt |= (ZEROR | NOTADR);
5303
5304 if (p != val + vallen || vallen == 0)
5305 {
5306invalid_count:
5307 EMSG(_("E178: Invalid default value for count"));
5308 return FAIL;
5309 }
5310 }
5311 }
5312 else if (STRNICMP(attr, "count", attrlen) == 0)
5313 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005314 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316 if (val != NULL)
5317 {
5318 p = val;
5319 if (*def >= 0)
5320 goto two_count;
5321
5322 *def = getdigits(&p);
5323
5324 if (p != val + vallen)
5325 goto invalid_count;
5326 }
5327
5328 if (*def < 0)
5329 *def = 0;
5330 }
5331 else if (STRNICMP(attr, "complete", attrlen) == 0)
5332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (val == NULL)
5334 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005335 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 return FAIL;
5337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005339 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5340 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
5343 else
5344 {
5345 char_u ch = attr[len];
5346 attr[len] = '\0';
5347 EMSG2(_("E181: Invalid attribute: %s"), attr);
5348 attr[len] = ch;
5349 return FAIL;
5350 }
5351 }
5352
5353 return OK;
5354}
5355
5356 static void
5357ex_command(eap)
5358 exarg_T *eap;
5359{
5360 char_u *name;
5361 char_u *end;
5362 char_u *p;
5363 long argt = 0;
5364 long def = -1;
5365 int flags = 0;
5366 int compl = EXPAND_NOTHING;
5367 char_u *compl_arg = NULL;
5368 int has_attr = (eap->arg[0] == '-');
5369
5370 p = eap->arg;
5371
5372 /* Check for attributes */
5373 while (*p == '-')
5374 {
5375 ++p;
5376 end = skiptowhite(p);
5377 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5378 == FAIL)
5379 return;
5380 p = skipwhite(end);
5381 }
5382
5383 /* Get the name (if any) and skip to the following argument */
5384 name = p;
5385 if (ASCII_ISALPHA(*p))
5386 while (ASCII_ISALNUM(*p))
5387 ++p;
5388 if (!ends_excmd(*p) && !vim_iswhite(*p))
5389 {
5390 EMSG(_("E182: Invalid command name"));
5391 return;
5392 }
5393 end = p;
5394
5395 /* If there is nothing after the name, and no attributes were specified,
5396 * we are listing commands
5397 */
5398 p = skipwhite(end);
5399 if (!has_attr && ends_excmd(*p))
5400 {
5401 uc_list(name, end - name);
5402 }
5403 else if (!ASCII_ISUPPER(*name))
5404 {
5405 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5406 return;
5407 }
5408 else
5409 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5410 eap->forceit);
5411}
5412
5413/*
5414 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 * Clear all user commands, global and for current buffer.
5416 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005417/*ARGSUSED*/
5418 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419ex_comclear(eap)
5420 exarg_T *eap;
5421{
5422 uc_clear(&ucmds);
5423 uc_clear(&curbuf->b_ucmds);
5424}
5425
5426/*
5427 * Clear all user commands for "gap".
5428 */
5429 void
5430uc_clear(gap)
5431 garray_T *gap;
5432{
5433 int i;
5434 ucmd_T *cmd;
5435
5436 for (i = 0; i < gap->ga_len; ++i)
5437 {
5438 cmd = USER_CMD_GA(gap, i);
5439 vim_free(cmd->uc_name);
5440 vim_free(cmd->uc_rep);
5441# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5442 vim_free(cmd->uc_compl_arg);
5443# endif
5444 }
5445 ga_clear(gap);
5446}
5447
5448 static void
5449ex_delcommand(eap)
5450 exarg_T *eap;
5451{
5452 int i = 0;
5453 ucmd_T *cmd = NULL;
5454 int cmp = -1;
5455 garray_T *gap;
5456
5457 gap = &curbuf->b_ucmds;
5458 for (;;)
5459 {
5460 for (i = 0; i < gap->ga_len; ++i)
5461 {
5462 cmd = USER_CMD_GA(gap, i);
5463 cmp = STRCMP(eap->arg, cmd->uc_name);
5464 if (cmp <= 0)
5465 break;
5466 }
5467 if (gap == &ucmds || cmp == 0)
5468 break;
5469 gap = &ucmds;
5470 }
5471
5472 if (cmp != 0)
5473 {
5474 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5475 return;
5476 }
5477
5478 vim_free(cmd->uc_name);
5479 vim_free(cmd->uc_rep);
5480# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5481 vim_free(cmd->uc_compl_arg);
5482# endif
5483
5484 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486 if (i < gap->ga_len)
5487 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5488}
5489
5490 static char_u *
5491uc_split_args(arg, lenp)
5492 char_u *arg;
5493 size_t *lenp;
5494{
5495 char_u *buf;
5496 char_u *p;
5497 char_u *q;
5498 int len;
5499
5500 /* Precalculate length */
5501 p = arg;
5502 len = 2; /* Initial and final quotes */
5503
5504 while (*p)
5505 {
5506 if (p[0] == '\\' && vim_iswhite(p[1]))
5507 {
5508 len += 1;
5509 p += 2;
5510 }
5511 else if (*p == '\\' || *p == '"')
5512 {
5513 len += 2;
5514 p += 1;
5515 }
5516 else if (vim_iswhite(*p))
5517 {
5518 p = skipwhite(p);
5519 if (*p == NUL)
5520 break;
5521 len += 3; /* "," */
5522 }
5523 else
5524 {
5525 ++len;
5526 ++p;
5527 }
5528 }
5529
5530 buf = alloc(len + 1);
5531 if (buf == NULL)
5532 {
5533 *lenp = 0;
5534 return buf;
5535 }
5536
5537 p = arg;
5538 q = buf;
5539 *q++ = '"';
5540 while (*p)
5541 {
5542 if (p[0] == '\\' && vim_iswhite(p[1]))
5543 {
5544 *q++ = p[1];
5545 p += 2;
5546 }
5547 else if (*p == '\\' || *p == '"')
5548 {
5549 *q++ = '\\';
5550 *q++ = *p++;
5551 }
5552 else if (vim_iswhite(*p))
5553 {
5554 p = skipwhite(p);
5555 if (*p == NUL)
5556 break;
5557 *q++ = '"';
5558 *q++ = ',';
5559 *q++ = '"';
5560 }
5561 else
5562 {
5563 *q++ = *p++;
5564 }
5565 }
5566 *q++ = '"';
5567 *q = 0;
5568
5569 *lenp = len;
5570 return buf;
5571}
5572
5573/*
5574 * Check for a <> code in a user command.
5575 * "code" points to the '<'. "len" the length of the <> (inclusive).
5576 * "buf" is where the result is to be added.
5577 * "split_buf" points to a buffer used for splitting, caller should free it.
5578 * "split_len" is the length of what "split_buf" contains.
5579 * Returns the length of the replacement, which has been added to "buf".
5580 * Returns -1 if there was no match, and only the "<" has been copied.
5581 */
5582 static size_t
5583uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5584 char_u *code;
5585 size_t len;
5586 char_u *buf;
5587 ucmd_T *cmd; /* the user command we're expanding */
5588 exarg_T *eap; /* ex arguments */
5589 char_u **split_buf;
5590 size_t *split_len;
5591{
5592 size_t result = 0;
5593 char_u *p = code + 1;
5594 size_t l = len - 2;
5595 int quote = 0;
5596 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5597 ct_LT, ct_NONE } type = ct_NONE;
5598
5599 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5600 {
5601 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5602 p += 2;
5603 l -= 2;
5604 }
5605
5606 if (l < 1)
5607 type = ct_NONE;
5608 else if (STRNICMP(p, "args", l) == 0)
5609 type = ct_ARGS;
5610 else if (STRNICMP(p, "bang", l) == 0)
5611 type = ct_BANG;
5612 else if (STRNICMP(p, "count", l) == 0)
5613 type = ct_COUNT;
5614 else if (STRNICMP(p, "line1", l) == 0)
5615 type = ct_LINE1;
5616 else if (STRNICMP(p, "line2", l) == 0)
5617 type = ct_LINE2;
5618 else if (STRNICMP(p, "lt", l) == 0)
5619 type = ct_LT;
5620 else if (STRNICMP(p, "register", l) == 0)
5621 type = ct_REGISTER;
5622
5623 switch (type)
5624 {
5625 case ct_ARGS:
5626 /* Simple case first */
5627 if (*eap->arg == NUL)
5628 {
5629 if (quote == 1)
5630 {
5631 result = 2;
5632 if (buf != NULL)
5633 STRCPY(buf, "''");
5634 }
5635 else
5636 result = 0;
5637 break;
5638 }
5639
5640 /* When specified there is a single argument don't split it.
5641 * Works for ":Cmd %" when % is "a b c". */
5642 if ((eap->argt & NOSPC) && quote == 2)
5643 quote = 1;
5644
5645 switch (quote)
5646 {
5647 case 0: /* No quoting, no splitting */
5648 result = STRLEN(eap->arg);
5649 if (buf != NULL)
5650 STRCPY(buf, eap->arg);
5651 break;
5652 case 1: /* Quote, but don't split */
5653 result = STRLEN(eap->arg) + 2;
5654 for (p = eap->arg; *p; ++p)
5655 {
5656 if (*p == '\\' || *p == '"')
5657 ++result;
5658 }
5659
5660 if (buf != NULL)
5661 {
5662 *buf++ = '"';
5663 for (p = eap->arg; *p; ++p)
5664 {
5665 if (*p == '\\' || *p == '"')
5666 *buf++ = '\\';
5667 *buf++ = *p;
5668 }
5669 *buf = '"';
5670 }
5671
5672 break;
5673 case 2: /* Quote and split */
5674 /* This is hard, so only do it once, and cache the result */
5675 if (*split_buf == NULL)
5676 *split_buf = uc_split_args(eap->arg, split_len);
5677
5678 result = *split_len;
5679 if (buf != NULL && result != 0)
5680 STRCPY(buf, *split_buf);
5681
5682 break;
5683 }
5684 break;
5685
5686 case ct_BANG:
5687 result = eap->forceit ? 1 : 0;
5688 if (quote)
5689 result += 2;
5690 if (buf != NULL)
5691 {
5692 if (quote)
5693 *buf++ = '"';
5694 if (eap->forceit)
5695 *buf++ = '!';
5696 if (quote)
5697 *buf = '"';
5698 }
5699 break;
5700
5701 case ct_LINE1:
5702 case ct_LINE2:
5703 case ct_COUNT:
5704 {
5705 char num_buf[20];
5706 long num = (type == ct_LINE1) ? eap->line1 :
5707 (type == ct_LINE2) ? eap->line2 :
5708 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5709 size_t num_len;
5710
5711 sprintf(num_buf, "%ld", num);
5712 num_len = STRLEN(num_buf);
5713 result = num_len;
5714
5715 if (quote)
5716 result += 2;
5717
5718 if (buf != NULL)
5719 {
5720 if (quote)
5721 *buf++ = '"';
5722 STRCPY(buf, num_buf);
5723 buf += num_len;
5724 if (quote)
5725 *buf = '"';
5726 }
5727
5728 break;
5729 }
5730
5731 case ct_REGISTER:
5732 result = eap->regname ? 1 : 0;
5733 if (quote)
5734 result += 2;
5735 if (buf != NULL)
5736 {
5737 if (quote)
5738 *buf++ = '\'';
5739 if (eap->regname)
5740 *buf++ = eap->regname;
5741 if (quote)
5742 *buf = '\'';
5743 }
5744 break;
5745
5746 case ct_LT:
5747 result = 1;
5748 if (buf != NULL)
5749 *buf = '<';
5750 break;
5751
5752 default:
5753 /* Not recognized: just copy the '<' and return -1. */
5754 result = (size_t)-1;
5755 if (buf != NULL)
5756 *buf = '<';
5757 break;
5758 }
5759
5760 return result;
5761}
5762
5763 static void
5764do_ucmd(eap)
5765 exarg_T *eap;
5766{
5767 char_u *buf;
5768 char_u *p;
5769 char_u *q;
5770
5771 char_u *start;
5772 char_u *end;
5773 size_t len, totlen;
5774
5775 size_t split_len = 0;
5776 char_u *split_buf = NULL;
5777 ucmd_T *cmd;
5778#ifdef FEAT_EVAL
5779 scid_T save_current_SID = current_SID;
5780#endif
5781
5782 if (eap->cmdidx == CMD_USER)
5783 cmd = USER_CMD(eap->useridx);
5784 else
5785 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5786
5787 /*
5788 * Replace <> in the command by the arguments.
5789 */
5790 buf = NULL;
5791 for (;;)
5792 {
5793 p = cmd->uc_rep;
5794 q = buf;
5795 totlen = 0;
5796 while ((start = vim_strchr(p, '<')) != NULL
5797 && (end = vim_strchr(start + 1, '>')) != NULL)
5798 {
5799 /* Include the '>' */
5800 ++end;
5801
5802 /* Take everything up to the '<' */
5803 len = start - p;
5804 if (buf == NULL)
5805 totlen += len;
5806 else
5807 {
5808 mch_memmove(q, p, len);
5809 q += len;
5810 }
5811
5812 len = uc_check_code(start, end - start, q, cmd, eap,
5813 &split_buf, &split_len);
5814 if (len == (size_t)-1)
5815 {
5816 /* no match, continue after '<' */
5817 p = start + 1;
5818 len = 1;
5819 }
5820 else
5821 p = end;
5822 if (buf == NULL)
5823 totlen += len;
5824 else
5825 q += len;
5826 }
5827 if (buf != NULL) /* second time here, finished */
5828 {
5829 STRCPY(q, p);
5830 break;
5831 }
5832
5833 totlen += STRLEN(p); /* Add on the trailing characters */
5834 buf = alloc((unsigned)(totlen + 1));
5835 if (buf == NULL)
5836 {
5837 vim_free(split_buf);
5838 return;
5839 }
5840 }
5841
5842#ifdef FEAT_EVAL
5843 current_SID = cmd->uc_scriptID;
5844#endif
5845 (void)do_cmdline(buf, eap->getline, eap->cookie,
5846 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5847#ifdef FEAT_EVAL
5848 current_SID = save_current_SID;
5849#endif
5850 vim_free(buf);
5851 vim_free(split_buf);
5852}
5853
5854# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5855 static char_u *
5856get_user_command_name(idx)
5857 int idx;
5858{
5859 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5860}
5861
5862/*
5863 * Function given to ExpandGeneric() to obtain the list of user command names.
5864 */
5865/*ARGSUSED*/
5866 char_u *
5867get_user_commands(xp, idx)
5868 expand_T *xp;
5869 int idx;
5870{
5871 if (idx < curbuf->b_ucmds.ga_len)
5872 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5873 idx -= curbuf->b_ucmds.ga_len;
5874 if (idx < ucmds.ga_len)
5875 return USER_CMD(idx)->uc_name;
5876 return NULL;
5877}
5878
5879/*
5880 * Function given to ExpandGeneric() to obtain the list of user command
5881 * attributes.
5882 */
5883/*ARGSUSED*/
5884 char_u *
5885get_user_cmd_flags(xp, idx)
5886 expand_T *xp;
5887 int idx;
5888{
5889 static char *user_cmd_flags[] =
5890 {"bang", "bar", "buffer", "complete", "count",
5891 "nargs", "range", "register"};
5892
5893 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5894 return NULL;
5895 return (char_u *)user_cmd_flags[idx];
5896}
5897
5898/*
5899 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5900 */
5901/*ARGSUSED*/
5902 char_u *
5903get_user_cmd_nargs(xp, idx)
5904 expand_T *xp;
5905 int idx;
5906{
5907 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5908
5909 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5910 return NULL;
5911 return (char_u *)user_cmd_nargs[idx];
5912}
5913
5914/*
5915 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5916 */
5917/*ARGSUSED*/
5918 char_u *
5919get_user_cmd_complete(xp, idx)
5920 expand_T *xp;
5921 int idx;
5922{
5923 return (char_u *)command_complete[idx].name;
5924}
5925# endif /* FEAT_CMDL_COMPL */
5926
5927#endif /* FEAT_USR_CMDS */
5928
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005929#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5930/*
5931 * Parse a completion argument "value[vallen]".
5932 * The detected completion goes in "*complp", argument type in "*argt".
5933 * When there is an argument, for function and user defined completion, it's
5934 * copied to allocated memory and stored in "*compl_arg".
5935 * Returns FAIL if something is wrong.
5936 */
5937 int
5938parse_compl_arg(value, vallen, complp, argt, compl_arg)
5939 char_u *value;
5940 int vallen;
5941 int *complp;
5942 long *argt;
5943 char_u **compl_arg;
5944{
5945 char_u *arg = NULL;
5946 size_t arglen = 0;
5947 int i;
5948 int valend = vallen;
5949
5950 /* Look for any argument part - which is the part after any ',' */
5951 for (i = 0; i < vallen; ++i)
5952 {
5953 if (value[i] == ',')
5954 {
5955 arg = &value[i + 1];
5956 arglen = vallen - i - 1;
5957 valend = i;
5958 break;
5959 }
5960 }
5961
5962 for (i = 0; command_complete[i].expand != 0; ++i)
5963 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005964 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005965 && STRNCMP(value, command_complete[i].name, valend) == 0)
5966 {
5967 *complp = command_complete[i].expand;
5968 if (command_complete[i].expand == EXPAND_BUFFERS)
5969 *argt |= BUFNAME;
5970 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5971 || command_complete[i].expand == EXPAND_FILES)
5972 *argt |= XFILE;
5973 break;
5974 }
5975 }
5976
5977 if (command_complete[i].expand == 0)
5978 {
5979 EMSG2(_("E180: Invalid complete value: %s"), value);
5980 return FAIL;
5981 }
5982
5983# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5984 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
5985 && arg != NULL)
5986# else
5987 if (arg != NULL)
5988# endif
5989 {
5990 EMSG(_("E468: Completion argument only allowed for custom completion"));
5991 return FAIL;
5992 }
5993
5994# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5995 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
5996 && arg == NULL)
5997 {
5998 EMSG(_("E467: Custom completion requires a function argument"));
5999 return FAIL;
6000 }
6001
6002 if (arg != NULL)
6003 *compl_arg = vim_strnsave(arg, (int)arglen);
6004# endif
6005 return OK;
6006}
6007#endif
6008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 static void
6010ex_colorscheme(eap)
6011 exarg_T *eap;
6012{
6013 if (load_colors(eap->arg) == FAIL)
6014 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6015}
6016
6017 static void
6018ex_highlight(eap)
6019 exarg_T *eap;
6020{
6021 if (*eap->arg == NUL && eap->cmd[2] == '!')
6022 MSG(_("Greetings, Vim user!"));
6023 do_highlight(eap->arg, eap->forceit, FALSE);
6024}
6025
6026
6027/*
6028 * Call this function if we thought we were going to exit, but we won't
6029 * (because of an error). May need to restore the terminal mode.
6030 */
6031 void
6032not_exiting()
6033{
6034 exiting = FALSE;
6035 settmode(TMODE_RAW);
6036}
6037
6038/*
6039 * ":quit": quit current window, quit Vim if closed the last window.
6040 */
6041 static void
6042ex_quit(eap)
6043 exarg_T *eap;
6044{
6045#ifdef FEAT_CMDWIN
6046 if (cmdwin_type != 0)
6047 {
6048 cmdwin_result = Ctrl_C;
6049 return;
6050 }
6051#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006052 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006053 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006054 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006055 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006056 return;
6057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058
6059#ifdef FEAT_NETBEANS_INTG
6060 netbeansForcedQuit = eap->forceit;
6061#endif
6062
6063 /*
6064 * If there are more files or windows we won't exit.
6065 */
6066 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6067 exiting = TRUE;
6068 if ((!P_HID(curbuf)
6069 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6070 || check_more(TRUE, eap->forceit) == FAIL
6071 || (only_one_window() && check_changed_any(eap->forceit)))
6072 {
6073 not_exiting();
6074 }
6075 else
6076 {
6077#ifdef FEAT_WINDOWS
6078 if (only_one_window()) /* quit last window */
6079#endif
6080 getout(0);
6081#ifdef FEAT_WINDOWS
6082# ifdef FEAT_GUI
6083 need_mouse_correct = TRUE;
6084# endif
6085 /* close window; may free buffer */
6086 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6087#endif
6088 }
6089}
6090
6091/*
6092 * ":cquit".
6093 */
6094/*ARGSUSED*/
6095 static void
6096ex_cquit(eap)
6097 exarg_T *eap;
6098{
6099 getout(1); /* this does not always pass on the exit code to the Manx
6100 compiler. why? */
6101}
6102
6103/*
6104 * ":qall": try to quit all windows
6105 */
6106 static void
6107ex_quit_all(eap)
6108 exarg_T *eap;
6109{
6110# ifdef FEAT_CMDWIN
6111 if (cmdwin_type != 0)
6112 {
6113 if (eap->forceit)
6114 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6115 else
6116 cmdwin_result = K_XF2;
6117 return;
6118 }
6119# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006120
6121 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006122 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006123 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006124 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006125 return;
6126 }
6127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 exiting = TRUE;
6129 if (eap->forceit || !check_changed_any(FALSE))
6130 getout(0);
6131 not_exiting();
6132}
6133
6134#ifdef FEAT_WINDOWS
6135/*
6136 * ":close": close current window, unless it is the last one
6137 */
6138 static void
6139ex_close(eap)
6140 exarg_T *eap;
6141{
6142# ifdef FEAT_CMDWIN
6143 if (cmdwin_type != 0)
6144 cmdwin_result = K_IGNORE;
6145 else
6146# endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006147 if (!text_locked())
Bram Moolenaarf740b292006-02-16 22:11:02 +00006148 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149}
6150
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006151#ifdef FEAT_QUICKFIX
6152/*
6153 * ":pclose": Close any preview window.
6154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006156ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006158{
6159 win_T *win;
6160
6161 for (win = firstwin; win != NULL; win = win->w_next)
6162 if (win->w_p_pvw)
6163 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006164 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006165 break;
6166 }
6167}
6168#endif
6169
Bram Moolenaarf740b292006-02-16 22:11:02 +00006170/*
6171 * Close window "win" and take care of handling closing the last window for a
6172 * modified buffer.
6173 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006174 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006175ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006176 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006178 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
6180 int need_hide;
6181 buf_T *buf = win->w_buffer;
6182
6183 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006184 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 {
6186#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6187 if ((p_confirm || cmdmod.confirm) && p_write)
6188 {
6189 dialog_changed(buf, FALSE);
6190 if (buf_valid(buf) && bufIsChanged(buf))
6191 return;
6192 need_hide = FALSE;
6193 }
6194 else
6195#endif
6196 {
6197 EMSG(_(e_nowrtmsg));
6198 return;
6199 }
6200 }
6201
6202#ifdef FEAT_GUI
6203 need_mouse_correct = TRUE;
6204#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006207 if (tp == NULL)
6208 win_close(win, !need_hide && !P_HID(buf));
6209 else
6210 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211}
6212
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006214 * ":tabclose": close current tab page, unless it is the last one.
6215 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 */
6217 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006218ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 exarg_T *eap;
6220{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006221 tabpage_T *tp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006222 int h = tabpageline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006223
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224# ifdef FEAT_CMDWIN
6225 if (cmdwin_type != 0)
6226 cmdwin_result = K_IGNORE;
6227 else
6228# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006229 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006230 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006231 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006233 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006235 tp = find_tabpage((int)eap->line2);
6236 if (tp == NULL)
6237 {
6238 beep_flush();
6239 return;
6240 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006241 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006242 {
6243 tabpage_close_other(tp, eap->forceit);
6244 return;
6245 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006246 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00006247 if (!text_locked())
6248 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006250
6251 if (h != tabpageline_height())
6252 shell_new_rows();
6253}
6254
6255/*
6256 * ":tabonly": close all tab pages except the current one
6257 */
6258 static void
6259ex_tabonly(eap)
6260 exarg_T *eap;
6261{
6262 tabpage_T *tp;
6263 int done;
6264 int h = tabpageline_height();
6265
6266# ifdef FEAT_CMDWIN
6267 if (cmdwin_type != 0)
6268 cmdwin_result = K_IGNORE;
6269 else
6270# endif
6271 if (first_tabpage->tp_next == NULL)
6272 MSG(_("Already only one tab page"));
6273 else
6274 {
6275 /* Repeat this up to a 1000 times, because autocommands may mess
6276 * up the lists. */
6277 for (done = 0; done < 1000; ++done)
6278 {
6279 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6280 if (tp->tp_topframe != topframe)
6281 {
6282 tabpage_close_other(tp, eap->forceit);
6283 /* if we failed to close it quit */
6284 if (valid_tabpage(tp))
6285 done = 1000;
6286 /* start over, "tp" is now invalid */
6287 break;
6288 }
6289 if (first_tabpage->tp_next == NULL)
6290 break;
6291 }
6292 }
6293
6294 if (h != tabpageline_height())
6295 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297
6298/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006299 * Close the current tab page.
6300 */
6301 void
6302tabpage_close(forceit)
6303 int forceit;
6304{
6305 /* First close all the windows but the current one. If that worked then
6306 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006307 if (lastwin != firstwin)
6308 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006309 if (lastwin == firstwin)
6310 ex_win_close(forceit, curwin, NULL);
6311# ifdef FEAT_GUI
6312 need_mouse_correct = TRUE;
6313# endif
6314}
6315
6316/*
6317 * Close tab page "tp", which is not the current tab page.
6318 * Note that autocommands may make "tp" invalid.
6319 */
6320 void
6321tabpage_close_other(tp, forceit)
6322 tabpage_T *tp;
6323 int forceit;
6324{
6325 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006326 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006327
6328 /* Limit to 1000 windows, autocommands may add a window while we close
6329 * one. OK, so I'm paranoid... */
6330 while (++done < 1000)
6331 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006332 wp = tp->tp_firstwin;
6333 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006334
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006335 /* Autocommands may delete the tab page under our fingers and we may
6336 * fail to close a window with a modified buffer. */
6337 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006338 break;
6339 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006340 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006341}
6342
6343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 * ":only".
6345 */
6346 static void
6347ex_only(eap)
6348 exarg_T *eap;
6349{
6350# ifdef FEAT_GUI
6351 need_mouse_correct = TRUE;
6352# endif
6353 close_others(TRUE, eap->forceit);
6354}
6355
6356/*
6357 * ":all" and ":sall".
6358 */
6359 static void
6360ex_all(eap)
6361 exarg_T *eap;
6362{
6363 if (eap->addr_count == 0)
6364 eap->line2 = 9999;
6365 do_arg_all((int)eap->line2, eap->forceit);
6366}
6367#endif /* FEAT_WINDOWS */
6368
6369 static void
6370ex_hide(eap)
6371 exarg_T *eap;
6372{
6373 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6374 eap->errmsg = e_invarg;
6375 else
6376 {
6377 /* ":hide" or ":hide | cmd": hide current window */
6378 eap->nextcmd = check_nextcmd(eap->arg);
6379#ifdef FEAT_WINDOWS
6380 if (!eap->skip)
6381 {
6382# ifdef FEAT_GUI
6383 need_mouse_correct = TRUE;
6384# endif
6385 win_close(curwin, FALSE); /* don't free buffer */
6386 }
6387#endif
6388 }
6389}
6390
6391/*
6392 * ":stop" and ":suspend": Suspend Vim.
6393 */
6394 static void
6395ex_stop(eap)
6396 exarg_T *eap;
6397{
6398 /*
6399 * Disallow suspending for "rvim".
6400 */
6401 if (!check_restricted()
6402#ifdef WIN3264
6403 /*
6404 * Check if external commands are allowed now.
6405 */
6406 && can_end_termcap_mode(TRUE)
6407#endif
6408 )
6409 {
6410 if (!eap->forceit)
6411 autowrite_all();
6412 windgoto((int)Rows - 1, 0);
6413 out_char('\n');
6414 out_flush();
6415 stoptermcap();
6416 out_flush(); /* needed for SUN to restore xterm buffer */
6417#ifdef FEAT_TITLE
6418 mch_restore_title(3); /* restore window titles */
6419#endif
6420 ui_suspend(); /* call machine specific function */
6421#ifdef FEAT_TITLE
6422 maketitle();
6423 resettitle(); /* force updating the title */
6424#endif
6425 starttermcap();
6426 scroll_start(); /* scroll screen before redrawing */
6427 redraw_later_clear();
6428 shell_resized(); /* may have resized window */
6429 }
6430}
6431
6432/*
6433 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6434 */
6435 static void
6436ex_exit(eap)
6437 exarg_T *eap;
6438{
6439#ifdef FEAT_CMDWIN
6440 if (cmdwin_type != 0)
6441 {
6442 cmdwin_result = Ctrl_C;
6443 return;
6444 }
6445#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006446 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006447 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006448 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006449 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006450 return;
6451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 /*
6454 * if more files or windows we won't exit
6455 */
6456 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6457 exiting = TRUE;
6458 if ( ((eap->cmdidx == CMD_wq
6459 || curbufIsChanged())
6460 && do_write(eap) == FAIL)
6461 || check_more(TRUE, eap->forceit) == FAIL
6462 || (only_one_window() && check_changed_any(eap->forceit)))
6463 {
6464 not_exiting();
6465 }
6466 else
6467 {
6468#ifdef FEAT_WINDOWS
6469 if (only_one_window()) /* quit last window, exit Vim */
6470#endif
6471 getout(0);
6472#ifdef FEAT_WINDOWS
6473# ifdef FEAT_GUI
6474 need_mouse_correct = TRUE;
6475# endif
6476 /* quit current window, may free buffer */
6477 win_close(curwin, !P_HID(curwin->w_buffer));
6478#endif
6479 }
6480}
6481
6482/*
6483 * ":print", ":list", ":number".
6484 */
6485 static void
6486ex_print(eap)
6487 exarg_T *eap;
6488{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006489 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6490 EMSG(_(e_emptybuf));
6491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006493 for ( ;!got_int; ui_breakcheck())
6494 {
6495 print_line(eap->line1,
6496 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6497 || (eap->flags & EXFLAG_NR)),
6498 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6499 if (++eap->line1 > eap->line2)
6500 break;
6501 out_flush(); /* show one line at a time */
6502 }
6503 setpcmark();
6504 /* put cursor at last line */
6505 curwin->w_cursor.lnum = eap->line2;
6506 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 }
6508
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510}
6511
6512#ifdef FEAT_BYTEOFF
6513 static void
6514ex_goto(eap)
6515 exarg_T *eap;
6516{
6517 goto_byte(eap->line2);
6518}
6519#endif
6520
6521/*
6522 * ":shell".
6523 */
6524/*ARGSUSED*/
6525 static void
6526ex_shell(eap)
6527 exarg_T *eap;
6528{
6529 do_shell(NULL, 0);
6530}
6531
6532#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6533 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006534 || defined(FEAT_GUI_MSWIN) \
6535 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 || defined(PROTO)
6537
6538/*
6539 * Handle a file drop. The code is here because a drop is *nearly* like an
6540 * :args command, but not quite (we have a list of exact filenames, so we
6541 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6542 * code is very similar to :args and hence needs access to a lot of the static
6543 * functions in this file.
6544 *
6545 * The list should be allocated using alloc(), as should each item in the
6546 * list. This function takes over responsibility for freeing the list.
6547 *
6548 * XXX The list is made into the arggument list. This is freed using
6549 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6550 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6551 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6552 * file functionality is (currently) not in EMX this is not presently a
6553 * problem.
6554 */
6555 void
6556handle_drop(filec, filev, split)
6557 int filec; /* the number of files dropped */
6558 char_u **filev; /* the list of files dropped */
6559 int split; /* force splitting the window */
6560{
6561 exarg_T ea;
6562 int save_msg_scroll = msg_scroll;
6563
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006564 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006565 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568 /* Check whether the current buffer is changed. If so, we will need
6569 * to split the current window or data could be lost.
6570 * We don't need to check if the 'hidden' option is set, as in this
6571 * case the buffer won't be lost.
6572 */
6573 if (!P_HID(curbuf) && !split)
6574 {
6575 ++emsg_off;
6576 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6577 --emsg_off;
6578 }
6579 if (split)
6580 {
6581# ifdef FEAT_WINDOWS
6582 if (win_split(0, 0) == FAIL)
6583 return;
6584# ifdef FEAT_SCROLLBIND
6585 curwin->w_p_scb = FALSE;
6586# endif
6587
6588 /* When splitting the window, create a new alist. Otherwise the
6589 * existing one is overwritten. */
6590 alist_unlink(curwin->w_alist);
6591 alist_new();
6592# else
6593 return; /* can't split, always fail */
6594# endif
6595 }
6596
6597 /*
6598 * Set up the new argument list.
6599 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006600 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601
6602 /*
6603 * Move to the first file.
6604 */
6605 /* Fake up a minimal "next" command for do_argfile() */
6606 vim_memset(&ea, 0, sizeof(ea));
6607 ea.cmd = (char_u *)"next";
6608 do_argfile(&ea, 0);
6609
6610 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6611 * mode that is not needed here. */
6612 need_start_insertmode = FALSE;
6613
6614 /* Restore msg_scroll, otherwise a following command may cause scrolling
6615 * unexpectedly. The screen will be redrawn by the caller, thus
6616 * msg_scroll being set by displaying a message is irrelevant. */
6617 msg_scroll = save_msg_scroll;
6618}
6619#endif
6620
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621/*
6622 * Clear an argument list: free all file names and reset it to zero entries.
6623 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006624 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625alist_clear(al)
6626 alist_T *al;
6627{
6628 while (--al->al_ga.ga_len >= 0)
6629 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6630 ga_clear(&al->al_ga);
6631}
6632
6633/*
6634 * Init an argument list.
6635 */
6636 void
6637alist_init(al)
6638 alist_T *al;
6639{
6640 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6641}
6642
6643#if defined(FEAT_WINDOWS) || defined(PROTO)
6644
6645/*
6646 * Remove a reference from an argument list.
6647 * Ignored when the argument list is the global one.
6648 * If the argument list is no longer used by any window, free it.
6649 */
6650 void
6651alist_unlink(al)
6652 alist_T *al;
6653{
6654 if (al != &global_alist && --al->al_refcount <= 0)
6655 {
6656 alist_clear(al);
6657 vim_free(al);
6658 }
6659}
6660
6661# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6662/*
6663 * Create a new argument list and use it for the current window.
6664 */
6665 void
6666alist_new()
6667{
6668 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6669 if (curwin->w_alist == NULL)
6670 {
6671 curwin->w_alist = &global_alist;
6672 ++global_alist.al_refcount;
6673 }
6674 else
6675 {
6676 curwin->w_alist->al_refcount = 1;
6677 alist_init(curwin->w_alist);
6678 }
6679}
6680# endif
6681#endif
6682
6683#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6684/*
6685 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006686 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6687 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 */
6689 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006690alist_expand(fnum_list, fnum_len)
6691 int *fnum_list;
6692 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006695 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 char_u **new_arg_files;
6697 int new_arg_file_count;
6698 char_u *save_p_su = p_su;
6699 int i;
6700
6701 /* Don't use 'suffixes' here. This should work like the shell did the
6702 * expansion. Also, the vimrc file isn't read yet, thus the user
6703 * can't set the options. */
6704 p_su = empty_option;
6705 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6706 if (old_arg_files != NULL)
6707 {
6708 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006709 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6710 old_arg_count = GARGCOUNT;
6711 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 &new_arg_file_count, &new_arg_files,
6713 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6714 && new_arg_file_count > 0)
6715 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006716 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6717 TRUE, fnum_list, fnum_len);
6718 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
6721 p_su = save_p_su;
6722}
6723#endif
6724
6725/*
6726 * Set the argument list for the current window.
6727 * Takes over the allocated files[] and the allocated fnames in it.
6728 */
6729 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006730alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 alist_T *al;
6732 int count;
6733 char_u **files;
6734 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006735 int *fnum_list;
6736 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
6738 int i;
6739
6740 alist_clear(al);
6741 if (ga_grow(&al->al_ga, count) == OK)
6742 {
6743 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006744 {
6745 if (got_int)
6746 {
6747 /* When adding many buffers this can take a long time. Allow
6748 * interrupting here. */
6749 while (i < count)
6750 vim_free(files[i++]);
6751 break;
6752 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006753
6754 /* May set buffer name of a buffer previously used for the
6755 * argument list, so that it's re-used by alist_add. */
6756 if (fnum_list != NULL && i < fnum_len)
6757 buf_set_name(fnum_list[i], files[i]);
6758
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006760 ui_breakcheck();
6761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 vim_free(files);
6763 }
6764 else
6765 FreeWild(count, files);
6766#ifdef FEAT_WINDOWS
6767 if (al == &global_alist)
6768#endif
6769 arg_had_last = FALSE;
6770}
6771
6772/*
6773 * Add file "fname" to argument list "al".
6774 * "fname" must have been allocated and "al" must have been checked for room.
6775 */
6776 void
6777alist_add(al, fname, set_fnum)
6778 alist_T *al;
6779 char_u *fname;
6780 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6781{
6782 if (fname == NULL) /* don't add NULL file names */
6783 return;
6784#ifdef BACKSLASH_IN_FILENAME
6785 slash_adjust(fname);
6786#endif
6787 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6788 if (set_fnum > 0)
6789 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6790 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6791 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792}
6793
6794#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6795/*
6796 * Adjust slashes in file names. Called after 'shellslash' was set.
6797 */
6798 void
6799alist_slash_adjust()
6800{
6801 int i;
6802# ifdef FEAT_WINDOWS
6803 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006804 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805# endif
6806
6807 for (i = 0; i < GARGCOUNT; ++i)
6808 if (GARGLIST[i].ae_fname != NULL)
6809 slash_adjust(GARGLIST[i].ae_fname);
6810# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006811 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (wp->w_alist != &global_alist)
6813 for (i = 0; i < WARGCOUNT(wp); ++i)
6814 if (WARGLIST(wp)[i].ae_fname != NULL)
6815 slash_adjust(WARGLIST(wp)[i].ae_fname);
6816# endif
6817}
6818#endif
6819
6820/*
6821 * ":preserve".
6822 */
6823/*ARGSUSED*/
6824 static void
6825ex_preserve(eap)
6826 exarg_T *eap;
6827{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006828 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 ml_preserve(curbuf, TRUE);
6830}
6831
6832/*
6833 * ":recover".
6834 */
6835 static void
6836ex_recover(eap)
6837 exarg_T *eap;
6838{
6839 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6840 recoverymode = TRUE;
6841 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6842 && (*eap->arg == NUL
6843 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6844 ml_recover();
6845 recoverymode = FALSE;
6846}
6847
6848/*
6849 * Command modifier used in a wrong way.
6850 */
6851 static void
6852ex_wrongmodifier(eap)
6853 exarg_T *eap;
6854{
6855 eap->errmsg = e_invcmd;
6856}
6857
6858#ifdef FEAT_WINDOWS
6859/*
6860 * :sview [+command] file split window with new file, read-only
6861 * :split [[+command] file] split window with current or new file
6862 * :vsplit [[+command] file] split window vertically with current or new file
6863 * :new [[+command] file] split window with no or new file
6864 * :vnew [[+command] file] split vertically window with no or new file
6865 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006866 *
6867 * :tabedit open new Tab page with empty window
6868 * :tabedit [+command] file open new Tab page and edit "file"
6869 * :tabnew [[+command] file] just like :tabedit
6870 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 */
6872 void
6873ex_splitview(eap)
6874 exarg_T *eap;
6875{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006876 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006877# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006879# endif
6880# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006884# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6886 {
6887 ex_ni(eap);
6888 return;
6889 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006892# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006896# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 /* A ":split" in the quickfix window works like ":new". Don't want two
6898 * quickfix windows. */
6899 if (bt_quickfix(curbuf))
6900 {
6901 if (eap->cmdidx == CMD_split)
6902 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006903# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (eap->cmdidx == CMD_vsplit)
6905 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
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_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006911 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {
6913 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6914 FNAME_MESS, TRUE, curbuf->b_ffname);
6915 if (fname == NULL)
6916 goto theend;
6917 eap->arg = fname;
6918 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006919# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006923# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006925# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 && eap->cmdidx != CMD_new)
6929 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006930 if (
6931# ifdef FEAT_GUI
6932 !gui.in_use &&
6933# endif
6934 au_has_group((char_u *)"FileExplorer"))
6935 {
6936 /* No browsing supported but we do have the file explorer:
6937 * Edit the directory. */
6938 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6939 eap->arg = (char_u *)".";
6940 }
6941 else
6942 {
6943 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006945 if (fname == NULL)
6946 goto theend;
6947 eap->arg = fname;
6948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 }
6950 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006953 /*
6954 * Either open new tab page or split the window.
6955 */
6956 if (eap->cmdidx == CMD_tabedit
6957 || eap->cmdidx == CMD_tabfind
6958 || eap->cmdidx == CMD_tabnew)
6959 {
6960 if (win_new_tabpage() != FAIL)
6961 {
6962 do_exedit(eap, NULL);
6963
6964 /* set the alternate buffer for the window we came from */
6965 if (curwin != old_curwin
6966 && win_valid(old_curwin)
6967 && old_curwin->w_buffer != curbuf
6968 && !cmdmod.keepalt)
6969 old_curwin->w_alt_fnum = curbuf->b_fnum;
6970 }
6971 }
6972 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6974 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006975# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 /* Reset 'scrollbind' when editing another file, but keep it when
6977 * doing ":split" without arguments. */
6978 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006979# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 )
6983 curwin->w_p_scb = FALSE;
6984 else
6985 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 do_exedit(eap, old_curwin);
6988 }
6989
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006990# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006994# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995theend:
6996 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006999
7000/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007001 * :tab command
7002 */
7003 void
7004ex_tab(eap)
7005 exarg_T *eap;
7006{
7007 goto_tabpage((int)eap->line2);
7008}
7009
7010/*
7011 * :tabs command: List tabs and their contents.
7012 */
7013/*ARGSUSED*/
7014 static void
7015ex_tabs(eap)
7016 exarg_T *eap;
7017{
7018 tabpage_T *tp;
7019 win_T *wp;
7020 int tabcount = 1;
7021
7022 msg_start();
7023 msg_scroll = TRUE;
7024 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7025 {
7026 msg_putchar('\n');
7027 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7028 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7029 out_flush(); /* output one line at a time */
7030 ui_breakcheck();
7031
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007032 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007033 wp = firstwin;
7034 else
7035 wp = tp->tp_firstwin;
7036 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7037 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007038 msg_puts((char_u *)"\n ");
7039 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7040 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007041 if (buf_spname(wp->w_buffer) != NULL)
7042 STRCPY(IObuff, buf_spname(wp->w_buffer));
7043 else
7044 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7045 IObuff, IOSIZE, TRUE);
7046 msg_outtrans(IObuff);
7047 out_flush(); /* output one line at a time */
7048 ui_breakcheck();
7049 }
7050 }
7051}
7052
7053#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
7055/*
7056 * ":mode": Set screen mode.
7057 * If no argument given, just get the screen size and redraw.
7058 */
7059 static void
7060ex_mode(eap)
7061 exarg_T *eap;
7062{
7063 if (*eap->arg == NUL)
7064 shell_resized();
7065 else
7066 mch_screenmode(eap->arg);
7067}
7068
7069#ifdef FEAT_WINDOWS
7070/*
7071 * ":resize".
7072 * set, increment or decrement current window height
7073 */
7074 static void
7075ex_resize(eap)
7076 exarg_T *eap;
7077{
7078 int n;
7079 win_T *wp = curwin;
7080
7081 if (eap->addr_count > 0)
7082 {
7083 n = eap->line2;
7084 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7085 ;
7086 }
7087
7088#ifdef FEAT_GUI
7089 need_mouse_correct = TRUE;
7090#endif
7091 n = atol((char *)eap->arg);
7092#ifdef FEAT_VERTSPLIT
7093 if (cmdmod.split & WSP_VERT)
7094 {
7095 if (*eap->arg == '-' || *eap->arg == '+')
7096 n += W_WIDTH(curwin);
7097 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7098 n = 9999;
7099 win_setwidth_win((int)n, wp);
7100 }
7101 else
7102#endif
7103 {
7104 if (*eap->arg == '-' || *eap->arg == '+')
7105 n += curwin->w_height;
7106 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7107 n = 9999;
7108 win_setheight_win((int)n, wp);
7109 }
7110}
7111#endif
7112
7113/*
7114 * ":find [+command] <file>" command.
7115 */
7116 static void
7117ex_find(eap)
7118 exarg_T *eap;
7119{
7120#ifdef FEAT_SEARCHPATH
7121 char_u *fname;
7122 int count;
7123
7124 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7125 TRUE, curbuf->b_ffname);
7126 if (eap->addr_count > 0)
7127 {
7128 /* Repeat finding the file "count" times. This matters when it
7129 * appears several times in the path. */
7130 count = eap->line2;
7131 while (fname != NULL && --count > 0)
7132 {
7133 vim_free(fname);
7134 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7135 FALSE, curbuf->b_ffname);
7136 }
7137 }
7138
7139 if (fname != NULL)
7140 {
7141 eap->arg = fname;
7142#endif
7143 do_exedit(eap, NULL);
7144#ifdef FEAT_SEARCHPATH
7145 vim_free(fname);
7146 }
7147#endif
7148}
7149
7150/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007151 * ":open" simulation: for now just work like ":visual".
7152 */
7153 static void
7154ex_open(eap)
7155 exarg_T *eap;
7156{
7157 regmatch_T regmatch;
7158 char_u *p;
7159
7160 curwin->w_cursor.lnum = eap->line2;
7161 beginline(BL_SOL | BL_FIX);
7162 if (*eap->arg == '/')
7163 {
7164 /* ":open /pattern/": put cursor in column found with pattern */
7165 ++eap->arg;
7166 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7167 *p = NUL;
7168 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7169 if (regmatch.regprog != NULL)
7170 {
7171 regmatch.rm_ic = p_ic;
7172 p = ml_get_curline();
7173 if (vim_regexec(&regmatch, p, (colnr_T)0))
7174 curwin->w_cursor.col = regmatch.startp[0] - p;
7175 else
7176 EMSG(_(e_nomatch));
7177 vim_free(regmatch.regprog);
7178 }
7179 /* Move to the NUL, ignore any other arguments. */
7180 eap->arg += STRLEN(eap->arg);
7181 }
7182 check_cursor();
7183
7184 eap->cmdidx = CMD_visual;
7185 do_exedit(eap, NULL);
7186}
7187
7188/*
7189 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 */
7191 static void
7192ex_edit(eap)
7193 exarg_T *eap;
7194{
7195 do_exedit(eap, NULL);
7196}
7197
7198/*
7199 * ":edit <file>" command and alikes.
7200 */
7201/*ARGSUSED*/
7202 void
7203do_exedit(eap, old_curwin)
7204 exarg_T *eap;
7205 win_T *old_curwin; /* curwin before doing a split or NULL */
7206{
7207 int n;
7208#ifdef FEAT_WINDOWS
7209 int need_hide;
7210#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007211 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
7213 /*
7214 * ":vi" command ends Ex mode.
7215 */
7216 if (exmode_active && (eap->cmdidx == CMD_visual
7217 || eap->cmdidx == CMD_view))
7218 {
7219 exmode_active = FALSE;
7220 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007221 {
7222 /* Special case: ":global/pat/visual\NLvi-commands" */
7223 if (global_busy)
7224 {
7225 int rd = RedrawingDisabled;
7226 int nwr = no_wait_return;
7227 int ms = msg_scroll;
7228#ifdef FEAT_GUI
7229 int he = hold_gui_events;
7230#endif
7231
7232 if (eap->nextcmd != NULL)
7233 {
7234 stuffReadbuff(eap->nextcmd);
7235 eap->nextcmd = NULL;
7236 }
7237
7238 if (exmode_was != EXMODE_VIM)
7239 settmode(TMODE_RAW);
7240 RedrawingDisabled = 0;
7241 no_wait_return = 0;
7242 need_wait_return = FALSE;
7243 msg_scroll = 0;
7244#ifdef FEAT_GUI
7245 hold_gui_events = 0;
7246#endif
7247 must_redraw = CLEAR;
7248
7249 main_loop(FALSE, TRUE);
7250
7251 RedrawingDisabled = rd;
7252 no_wait_return = nwr;
7253 msg_scroll = ms;
7254#ifdef FEAT_GUI
7255 hold_gui_events = he;
7256#endif
7257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261
7262 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007263 || eap->cmdidx == CMD_tabnew
7264 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265#ifdef FEAT_VERTSPLIT
7266 || eap->cmdidx == CMD_vnew
7267#endif
7268 ) && *eap->arg == NUL)
7269 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007270 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 setpcmark();
7272 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7273 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7274 }
7275 else if ((eap->cmdidx != CMD_split
7276#ifdef FEAT_VERTSPLIT
7277 && eap->cmdidx != CMD_vsplit
7278#endif
7279 )
7280 || *eap->arg != NUL
7281#ifdef FEAT_BROWSE
7282 || cmdmod.browse
7283#endif
7284 )
7285 {
7286 n = readonlymode;
7287 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7288 readonlymode = TRUE;
7289 else if (eap->cmdidx == CMD_enew)
7290 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7291 empty buffer */
7292 setpcmark();
7293 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7294 NULL, eap,
7295 /* ":edit" goes to first line if Vi compatible */
7296 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7297 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7298 ? ECMD_ONE : eap->do_ecmd_lnum,
7299 (P_HID(curbuf) ? ECMD_HIDE : 0)
7300 + (eap->forceit ? ECMD_FORCEIT : 0)
7301#ifdef FEAT_LISTCMDS
7302 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7303#endif
7304 ) == FAIL)
7305 {
7306 /* Editing the file failed. If the window was split, close it. */
7307#ifdef FEAT_WINDOWS
7308 if (old_curwin != NULL)
7309 {
7310 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7311 if (!need_hide || P_HID(curbuf))
7312 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007313# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7314 cleanup_T cs;
7315
7316 /* Reset the error/interrupt/exception state here so that
7317 * aborting() returns FALSE when closing a window. */
7318 enter_cleanup(&cs);
7319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320# ifdef FEAT_GUI
7321 need_mouse_correct = TRUE;
7322# endif
7323 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007324
7325# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7326 /* Restore the error/interrupt/exception state if not
7327 * discarded by a new aborting error, interrupt, or
7328 * uncaught exception. */
7329 leave_cleanup(&cs);
7330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 }
7332 }
7333#endif
7334 }
7335 else if (readonlymode && curbuf->b_nwindows == 1)
7336 {
7337 /* When editing an already visited buffer, 'readonly' won't be set
7338 * but the previous value is kept. With ":view" and ":sview" we
7339 * want the file to be readonly, except when another window is
7340 * editing the same buffer. */
7341 curbuf->b_p_ro = TRUE;
7342 }
7343 readonlymode = n;
7344 }
7345 else
7346 {
7347 if (eap->do_ecmd_cmd != NULL)
7348 do_cmdline_cmd(eap->do_ecmd_cmd);
7349#ifdef FEAT_TITLE
7350 n = curwin->w_arg_idx_invalid;
7351#endif
7352 check_arg_idx(curwin);
7353#ifdef FEAT_TITLE
7354 if (n != curwin->w_arg_idx_invalid)
7355 maketitle();
7356#endif
7357 }
7358
7359#ifdef FEAT_WINDOWS
7360 /*
7361 * if ":split file" worked, set alternate file name in old window to new
7362 * file
7363 */
7364 if (old_curwin != NULL
7365 && *eap->arg != NUL
7366 && curwin != old_curwin
7367 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007368 && old_curwin->w_buffer != curbuf
7369 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 old_curwin->w_alt_fnum = curbuf->b_fnum;
7371#endif
7372
7373 ex_no_reprint = TRUE;
7374}
7375
7376#ifndef FEAT_GUI
7377/*
7378 * ":gui" and ":gvim" when there is no GUI.
7379 */
7380 static void
7381ex_nogui(eap)
7382 exarg_T *eap;
7383{
7384 eap->errmsg = e_nogvim;
7385}
7386#endif
7387
7388#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7389 static void
7390ex_tearoff(eap)
7391 exarg_T *eap;
7392{
7393 gui_make_tearoff(eap->arg);
7394}
7395#endif
7396
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007397#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 static void
7399ex_popup(eap)
7400 exarg_T *eap;
7401{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007402 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403}
7404#endif
7405
7406/*ARGSUSED*/
7407 static void
7408ex_swapname(eap)
7409 exarg_T *eap;
7410{
7411 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7412 MSG(_("No swap file"));
7413 else
7414 msg(curbuf->b_ml.ml_mfp->mf_fname);
7415}
7416
7417/*
7418 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7419 * offset.
7420 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7421 */
7422/*ARGSUSED*/
7423 static void
7424ex_syncbind(eap)
7425 exarg_T *eap;
7426{
7427#ifdef FEAT_SCROLLBIND
7428 win_T *wp;
7429 long topline;
7430 long y;
7431 linenr_T old_linenr = curwin->w_cursor.lnum;
7432
7433 setpcmark();
7434
7435 /*
7436 * determine max topline
7437 */
7438 if (curwin->w_p_scb)
7439 {
7440 topline = curwin->w_topline;
7441 for (wp = firstwin; wp; wp = wp->w_next)
7442 {
7443 if (wp->w_p_scb && wp->w_buffer)
7444 {
7445 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7446 if (topline > y)
7447 topline = y;
7448 }
7449 }
7450 if (topline < 1)
7451 topline = 1;
7452 }
7453 else
7454 {
7455 topline = 1;
7456 }
7457
7458
7459 /*
7460 * set all scrollbind windows to the same topline
7461 */
7462 wp = curwin;
7463 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7464 {
7465 if (curwin->w_p_scb)
7466 {
7467 y = topline - curwin->w_topline;
7468 if (y > 0)
7469 scrollup(y, TRUE);
7470 else
7471 scrolldown(-y, TRUE);
7472 curwin->w_scbind_pos = topline;
7473 redraw_later(VALID);
7474 cursor_correct();
7475#ifdef FEAT_WINDOWS
7476 curwin->w_redr_status = TRUE;
7477#endif
7478 }
7479 }
7480 curwin = wp;
7481 if (curwin->w_p_scb)
7482 {
7483 did_syncbind = TRUE;
7484 checkpcmark();
7485 if (old_linenr != curwin->w_cursor.lnum)
7486 {
7487 char_u ctrl_o[2];
7488
7489 ctrl_o[0] = Ctrl_O;
7490 ctrl_o[1] = 0;
7491 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7492 }
7493 }
7494#endif
7495}
7496
7497
7498 static void
7499ex_read(eap)
7500 exarg_T *eap;
7501{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007502 int i;
7503 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7504 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
7506 if (eap->usefilter) /* :r!cmd */
7507 do_bang(1, eap, FALSE, FALSE, TRUE);
7508 else
7509 {
7510 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7511 return;
7512
7513#ifdef FEAT_BROWSE
7514 if (cmdmod.browse)
7515 {
7516 char_u *browseFile;
7517
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007518 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 NULL, NULL, NULL, curbuf);
7520 if (browseFile != NULL)
7521 {
7522 i = readfile(browseFile, NULL,
7523 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7524 vim_free(browseFile);
7525 }
7526 else
7527 i = OK;
7528 }
7529 else
7530#endif
7531 if (*eap->arg == NUL)
7532 {
7533 if (check_fname() == FAIL) /* check for no file name */
7534 return;
7535 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7536 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7537 }
7538 else
7539 {
7540 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7541 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7542 i = readfile(eap->arg, NULL,
7543 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7544
7545 }
7546 if (i == FAIL)
7547 {
7548#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7549 if (!aborting())
7550#endif
7551 EMSG2(_(e_notopen), eap->arg);
7552 }
7553 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007554 {
7555 if (empty && exmode_active)
7556 {
7557 /* Delete the empty line that remains. Historically ex does
7558 * this but vi doesn't. */
7559 if (eap->line2 == 0)
7560 lnum = curbuf->b_ml.ml_line_count;
7561 else
7562 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007563 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007564 {
7565 ml_delete(lnum, FALSE);
7566 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007567 if (curwin->w_cursor.lnum > 1
7568 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007569 --curwin->w_cursor.lnum;
7570 }
7571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 }
7575}
7576
Bram Moolenaarea408852005-06-25 22:49:46 +00007577static char_u *prev_dir = NULL;
7578
7579#if defined(EXITFREE) || defined(PROTO)
7580 void
7581free_cd_dir()
7582{
7583 vim_free(prev_dir);
7584}
7585#endif
7586
7587
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588/*
7589 * ":cd", ":lcd", ":chdir" and ":lchdir".
7590 */
7591 static void
7592ex_cd(eap)
7593 exarg_T *eap;
7594{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 char_u *new_dir;
7596 char_u *tofree;
7597
7598 new_dir = eap->arg;
7599#if !defined(UNIX) && !defined(VMS)
7600 /* for non-UNIX ":cd" means: print current directory */
7601 if (*new_dir == NUL)
7602 ex_pwd(NULL);
7603 else
7604#endif
7605 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007606 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7607 && !eap->forceit)
7608 {
7609 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7610 return;
7611 }
7612
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 /* ":cd -": Change to previous directory */
7614 if (STRCMP(new_dir, "-") == 0)
7615 {
7616 if (prev_dir == NULL)
7617 {
7618 EMSG(_("E186: No previous directory"));
7619 return;
7620 }
7621 new_dir = prev_dir;
7622 }
7623
7624 /* Save current directory for next ":cd -" */
7625 tofree = prev_dir;
7626 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7627 prev_dir = vim_strsave(NameBuff);
7628 else
7629 prev_dir = NULL;
7630
7631#if defined(UNIX) || defined(VMS)
7632 /* for UNIX ":cd" means: go to home directory */
7633 if (*new_dir == NUL)
7634 {
7635 /* use NameBuff for home directory name */
7636# ifdef VMS
7637 char_u *p;
7638
7639 p = mch_getenv((char_u *)"SYS$LOGIN");
7640 if (p == NULL || *p == NUL) /* empty is the same as not set */
7641 NameBuff[0] = NUL;
7642 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007643 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644# else
7645 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7646# endif
7647 new_dir = NameBuff;
7648 }
7649#endif
7650 if (new_dir == NULL || vim_chdir(new_dir))
7651 EMSG(_(e_failed));
7652 else
7653 {
7654 vim_free(curwin->w_localdir);
7655 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7656 {
7657 /* If still in global directory, need to remember current
7658 * directory as global directory. */
7659 if (globaldir == NULL && prev_dir != NULL)
7660 globaldir = vim_strsave(prev_dir);
7661 /* Remember this local directory for the window. */
7662 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7663 curwin->w_localdir = vim_strsave(NameBuff);
7664 }
7665 else
7666 {
7667 /* We are now in the global directory, no need to remember its
7668 * name. */
7669 vim_free(globaldir);
7670 globaldir = NULL;
7671 curwin->w_localdir = NULL;
7672 }
7673
7674 shorten_fnames(TRUE);
7675
7676 /* Echo the new current directory if the command was typed. */
7677 if (KeyTyped)
7678 ex_pwd(eap);
7679 }
7680 vim_free(tofree);
7681 }
7682}
7683
7684/*
7685 * ":pwd".
7686 */
7687/*ARGSUSED*/
7688 static void
7689ex_pwd(eap)
7690 exarg_T *eap;
7691{
7692 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7693 {
7694#ifdef BACKSLASH_IN_FILENAME
7695 slash_adjust(NameBuff);
7696#endif
7697 msg(NameBuff);
7698 }
7699 else
7700 EMSG(_("E187: Unknown"));
7701}
7702
7703/*
7704 * ":=".
7705 */
7706 static void
7707ex_equal(eap)
7708 exarg_T *eap;
7709{
7710 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007711 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712}
7713
7714 static void
7715ex_sleep(eap)
7716 exarg_T *eap;
7717{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007718 int n;
7719 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
7721 if (cursor_valid())
7722 {
7723 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7724 if (n >= 0)
7725 windgoto((int)n, curwin->w_wcol);
7726 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007727
7728 len = eap->line2;
7729 switch (*eap->arg)
7730 {
7731 case 'm': break;
7732 case NUL: len *= 1000L; break;
7733 default: EMSG2(_(e_invarg2), eap->arg); return;
7734 }
7735 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737
7738/*
7739 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7740 */
7741 void
7742do_sleep(msec)
7743 long msec;
7744{
7745 long done;
7746
7747 cursor_on();
7748 out_flush();
7749 for (done = 0; !got_int && done < msec; done += 1000L)
7750 {
7751 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7752 ui_breakcheck();
7753 }
7754}
7755
7756 static void
7757do_exmap(eap, isabbrev)
7758 exarg_T *eap;
7759 int isabbrev;
7760{
7761 int mode;
7762 char_u *cmdp;
7763
7764 cmdp = eap->cmd;
7765 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7766
7767 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7768 eap->arg, mode, isabbrev))
7769 {
7770 case 1: EMSG(_(e_invarg));
7771 break;
7772 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7773 break;
7774 }
7775}
7776
7777/*
7778 * ":winsize" command (obsolete).
7779 */
7780 static void
7781ex_winsize(eap)
7782 exarg_T *eap;
7783{
7784 int w, h;
7785 char_u *arg = eap->arg;
7786 char_u *p;
7787
7788 w = getdigits(&arg);
7789 arg = skipwhite(arg);
7790 p = arg;
7791 h = getdigits(&arg);
7792 if (*p != NUL && *arg == NUL)
7793 set_shellsize(w, h, TRUE);
7794 else
7795 EMSG(_("E465: :winsize requires two number arguments"));
7796}
7797
7798#ifdef FEAT_WINDOWS
7799 static void
7800ex_wincmd(eap)
7801 exarg_T *eap;
7802{
7803 int xchar = NUL;
7804 char_u *p;
7805
7806 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7807 {
7808 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7809 if (eap->arg[1] == NUL)
7810 {
7811 EMSG(_(e_invarg));
7812 return;
7813 }
7814 xchar = eap->arg[1];
7815 p = eap->arg + 2;
7816 }
7817 else
7818 p = eap->arg + 1;
7819
7820 eap->nextcmd = check_nextcmd(p);
7821 p = skipwhite(p);
7822 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7823 EMSG(_(e_invarg));
7824 else
7825 {
7826 /* Pass flags on for ":vertical wincmd ]". */
7827 postponed_split_flags = cmdmod.split;
7828 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7829 postponed_split_flags = 0;
7830 }
7831}
7832#endif
7833
Bram Moolenaar843ee412004-06-30 16:16:41 +00007834#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835/*
7836 * ":winpos".
7837 */
7838 static void
7839ex_winpos(eap)
7840 exarg_T *eap;
7841{
7842 int x, y;
7843 char_u *arg = eap->arg;
7844 char_u *p;
7845
7846 if (*arg == NUL)
7847 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007848# if defined(FEAT_GUI) || defined(MSWIN)
7849# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007851# else
7852 if (mch_get_winpos(&x, &y) != FAIL)
7853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
7855 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7856 msg(IObuff);
7857 }
7858 else
7859# endif
7860 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7861 }
7862 else
7863 {
7864 x = getdigits(&arg);
7865 arg = skipwhite(arg);
7866 p = arg;
7867 y = getdigits(&arg);
7868 if (*p == NUL || *arg != NUL)
7869 {
7870 EMSG(_("E466: :winpos requires two number arguments"));
7871 return;
7872 }
7873# ifdef FEAT_GUI
7874 if (gui.in_use)
7875 gui_mch_set_winpos(x, y);
7876 else if (gui.starting)
7877 {
7878 /* Remember the coordinates for when the window is opened. */
7879 gui_win_x = x;
7880 gui_win_y = y;
7881 }
7882# ifdef HAVE_TGETENT
7883 else
7884# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007885# else
7886# ifdef MSWIN
7887 mch_set_winpos(x, y);
7888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889# endif
7890# ifdef HAVE_TGETENT
7891 if (*T_CWP)
7892 term_set_winpos(x, y);
7893# endif
7894 }
7895}
7896#endif
7897
7898/*
7899 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7900 */
7901 static void
7902ex_operators(eap)
7903 exarg_T *eap;
7904{
7905 oparg_T oa;
7906
7907 clear_oparg(&oa);
7908 oa.regname = eap->regname;
7909 oa.start.lnum = eap->line1;
7910 oa.end.lnum = eap->line2;
7911 oa.line_count = eap->line2 - eap->line1 + 1;
7912 oa.motion_type = MLINE;
7913#ifdef FEAT_VIRTUALEDIT
7914 virtual_op = FALSE;
7915#endif
7916 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7917 {
7918 setpcmark();
7919 curwin->w_cursor.lnum = eap->line1;
7920 beginline(BL_SOL | BL_FIX);
7921 }
7922
7923 switch (eap->cmdidx)
7924 {
7925 case CMD_delete:
7926 oa.op_type = OP_DELETE;
7927 op_delete(&oa);
7928 break;
7929
7930 case CMD_yank:
7931 oa.op_type = OP_YANK;
7932 (void)op_yank(&oa, FALSE, TRUE);
7933 break;
7934
7935 default: /* CMD_rshift or CMD_lshift */
7936 if ((eap->cmdidx == CMD_rshift)
7937#ifdef FEAT_RIGHTLEFT
7938 ^ curwin->w_p_rl
7939#endif
7940 )
7941 oa.op_type = OP_RSHIFT;
7942 else
7943 oa.op_type = OP_LSHIFT;
7944 op_shift(&oa, FALSE, eap->amount);
7945 break;
7946 }
7947#ifdef FEAT_VIRTUALEDIT
7948 virtual_op = MAYBE;
7949#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007950 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951}
7952
7953/*
7954 * ":put".
7955 */
7956 static void
7957ex_put(eap)
7958 exarg_T *eap;
7959{
7960 /* ":0put" works like ":1put!". */
7961 if (eap->line2 == 0)
7962 {
7963 eap->line2 = 1;
7964 eap->forceit = TRUE;
7965 }
7966 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007967 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7968 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969}
7970
7971/*
7972 * Handle ":copy" and ":move".
7973 */
7974 static void
7975ex_copymove(eap)
7976 exarg_T *eap;
7977{
7978 long n;
7979
7980 n = get_address(&eap->arg, FALSE, FALSE);
7981 if (eap->arg == NULL) /* error detected */
7982 {
7983 eap->nextcmd = NULL;
7984 return;
7985 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007986 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987
7988 /*
7989 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7990 */
7991 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7992 {
7993 EMSG(_(e_invaddr));
7994 return;
7995 }
7996
7997 if (eap->cmdidx == CMD_move)
7998 {
7999 if (do_move(eap->line1, eap->line2, n) == FAIL)
8000 return;
8001 }
8002 else
8003 ex_copy(eap->line1, eap->line2, n);
8004 u_clearline();
8005 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008006 ex_may_print(eap);
8007}
8008
8009/*
8010 * Print the current line if flags were given to the Ex command.
8011 */
8012 static void
8013ex_may_print(eap)
8014 exarg_T *eap;
8015{
8016 if (eap->flags != 0)
8017 {
8018 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8019 (eap->flags & EXFLAG_LIST));
8020 ex_no_reprint = TRUE;
8021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022}
8023
8024/*
8025 * ":smagic" and ":snomagic".
8026 */
8027 static void
8028ex_submagic(eap)
8029 exarg_T *eap;
8030{
8031 int magic_save = p_magic;
8032
8033 p_magic = (eap->cmdidx == CMD_smagic);
8034 do_sub(eap);
8035 p_magic = magic_save;
8036}
8037
8038/*
8039 * ":join".
8040 */
8041 static void
8042ex_join(eap)
8043 exarg_T *eap;
8044{
8045 curwin->w_cursor.lnum = eap->line1;
8046 if (eap->line1 == eap->line2)
8047 {
8048 if (eap->addr_count >= 2) /* :2,2join does nothing */
8049 return;
8050 if (eap->line2 == curbuf->b_ml.ml_line_count)
8051 {
8052 beep_flush();
8053 return;
8054 }
8055 ++eap->line2;
8056 }
8057 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8058 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008059 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
8062/*
8063 * ":[addr]@r" or ":[addr]*r": execute register
8064 */
8065 static void
8066ex_at(eap)
8067 exarg_T *eap;
8068{
8069 int c;
8070
8071 curwin->w_cursor.lnum = eap->line2;
8072
8073#ifdef USE_ON_FLY_SCROLL
8074 dont_scroll = TRUE; /* disallow scrolling here */
8075#endif
8076
8077 /* get the register name. No name means to use the previous one */
8078 c = *eap->arg;
8079 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8080 c = '@';
8081 /* put the register in mapbuf */
8082 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 else
8087 {
8088 int save_efr = exec_from_reg;
8089
8090 exec_from_reg = TRUE;
8091
8092 /*
8093 * Execute from the typeahead buffer.
8094 * Originally this didn't check for the typeahead buffer to be empty,
8095 * thus could read more Ex commands from stdin. It's not clear why,
8096 * it is certainly unexpected.
8097 */
8098 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8099 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8100
8101 exec_from_reg = save_efr;
8102 }
8103}
8104
8105/*
8106 * ":!".
8107 */
8108 static void
8109ex_bang(eap)
8110 exarg_T *eap;
8111{
8112 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8113}
8114
8115/*
8116 * ":undo".
8117 */
8118/*ARGSUSED*/
8119 static void
8120ex_undo(eap)
8121 exarg_T *eap;
8122{
8123 u_undo(1);
8124}
8125
8126/*
8127 * ":redo".
8128 */
8129/*ARGSUSED*/
8130 static void
8131ex_redo(eap)
8132 exarg_T *eap;
8133{
8134 u_redo(1);
8135}
8136
8137/*
8138 * ":redir": start/stop redirection.
8139 */
8140 static void
8141ex_redir(eap)
8142 exarg_T *eap;
8143{
8144 char *mode;
8145 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008146 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147
8148 if (STRICMP(eap->arg, "END") == 0)
8149 close_redir();
8150 else
8151 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008152 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008154 ++arg;
8155 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008157 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 mode = "a";
8159 }
8160 else
8161 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008162 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163
8164 close_redir();
8165
8166 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008167 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 if (fname == NULL)
8169 return;
8170#ifdef FEAT_BROWSE
8171 if (cmdmod.browse)
8172 {
8173 char_u *browseFile;
8174
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008175 browseFile = do_browse(BROWSE_SAVE,
8176 (char_u *)_("Save Redirection"),
8177 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 if (browseFile == NULL)
8179 return; /* operation cancelled */
8180 vim_free(fname);
8181 fname = browseFile;
8182 eap->forceit = TRUE; /* since dialog already asked */
8183 }
8184#endif
8185
8186 redir_fd = open_exfile(fname, eap->forceit, mode);
8187 vim_free(fname);
8188 }
8189#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008190 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {
8192 /* redirect to a register a-z (resp. A-Z for appending) */
8193 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008194 ++arg;
8195 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008197 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008199 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008201 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008202 if (*arg == '>' && arg[1] == '>')
8203 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008204 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8205 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008207 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008209 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008211 if (*arg == '>')
8212 arg++;
8213
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 /* make register empty */
8215 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8216 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008217 }
8218 if (*arg != NUL)
8219 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008220 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008221 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008222 }
8223 }
8224 else if (*arg == '=' && arg[1] == '>')
8225 {
8226 int append;
8227
8228 /* redirect to a variable */
8229 close_redir();
8230 arg += 2;
8231
8232 if (*arg == '>')
8233 {
8234 ++arg;
8235 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
8237 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008238 append = FALSE;
8239
8240 if (var_redir_start(skipwhite(arg), append) == OK)
8241 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 }
8243#endif
8244
8245 /* TODO: redirect to a buffer */
8246
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008248 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 }
8250}
8251
8252/*
8253 * ":redraw": force redraw
8254 */
8255 static void
8256ex_redraw(eap)
8257 exarg_T *eap;
8258{
8259 int r = RedrawingDisabled;
8260 int p = p_lz;
8261
8262 RedrawingDisabled = 0;
8263 p_lz = FALSE;
8264 update_topline();
8265 update_screen(eap->forceit ? CLEAR :
8266#ifdef FEAT_VISUAL
8267 VIsual_active ? INVERTED :
8268#endif
8269 0);
8270#ifdef FEAT_TITLE
8271 if (need_maketitle)
8272 maketitle();
8273#endif
8274 RedrawingDisabled = r;
8275 p_lz = p;
8276
8277 /* Reset msg_didout, so that a message that's there is overwritten. */
8278 msg_didout = FALSE;
8279 msg_col = 0;
8280
8281 out_flush();
8282}
8283
8284/*
8285 * ":redrawstatus": force redraw of status line(s)
8286 */
8287/*ARGSUSED*/
8288 static void
8289ex_redrawstatus(eap)
8290 exarg_T *eap;
8291{
8292#if defined(FEAT_WINDOWS)
8293 int r = RedrawingDisabled;
8294 int p = p_lz;
8295
8296 RedrawingDisabled = 0;
8297 p_lz = FALSE;
8298 if (eap->forceit)
8299 status_redraw_all();
8300 else
8301 status_redraw_curbuf();
8302 update_screen(
8303# ifdef FEAT_VISUAL
8304 VIsual_active ? INVERTED :
8305# endif
8306 0);
8307 RedrawingDisabled = r;
8308 p_lz = p;
8309 out_flush();
8310#endif
8311}
8312
8313 static void
8314close_redir()
8315{
8316 if (redir_fd != NULL)
8317 {
8318 fclose(redir_fd);
8319 redir_fd = NULL;
8320 }
8321#ifdef FEAT_EVAL
8322 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008323 if (redir_vname)
8324 {
8325 var_redir_stop();
8326 redir_vname = 0;
8327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328#endif
8329}
8330
8331#if defined(FEAT_SESSION) && defined(USE_CRNL)
8332# define MKSESSION_NL
8333static int mksession_nl = FALSE; /* use NL only in put_eol() */
8334#endif
8335
8336/*
8337 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8338 */
8339 static void
8340ex_mkrc(eap)
8341 exarg_T *eap;
8342{
8343 FILE *fd;
8344 int failed = FALSE;
8345 char_u *fname;
8346#ifdef FEAT_BROWSE
8347 char_u *browseFile = NULL;
8348#endif
8349#ifdef FEAT_SESSION
8350 int view_session = FALSE;
8351 int using_vdir = FALSE; /* using 'viewdir'? */
8352 char_u *viewFile = NULL;
8353 unsigned *flagp;
8354#endif
8355
8356 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8357 {
8358#ifdef FEAT_SESSION
8359 view_session = TRUE;
8360#else
8361 ex_ni(eap);
8362 return;
8363#endif
8364 }
8365
8366#ifdef FEAT_SESSION
8367 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8368 if (eap->cmdidx == CMD_mkview
8369 && (*eap->arg == NUL
8370 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8371 {
8372 eap->forceit = TRUE;
8373 fname = get_view_file(*eap->arg);
8374 if (fname == NULL)
8375 return;
8376 viewFile = fname;
8377 using_vdir = TRUE;
8378 }
8379 else
8380#endif
8381 if (*eap->arg != NUL)
8382 fname = eap->arg;
8383 else if (eap->cmdidx == CMD_mkvimrc)
8384 fname = (char_u *)VIMRC_FILE;
8385#ifdef FEAT_SESSION
8386 else if (eap->cmdidx == CMD_mksession)
8387 fname = (char_u *)SESSION_FILE;
8388#endif
8389 else
8390 fname = (char_u *)EXRC_FILE;
8391
8392#ifdef FEAT_BROWSE
8393 if (cmdmod.browse)
8394 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008395 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396# ifdef FEAT_SESSION
8397 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8398 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8399# endif
8400 (char_u *)_("Save Setup"),
8401 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8402 if (browseFile == NULL)
8403 goto theend;
8404 fname = browseFile;
8405 eap->forceit = TRUE; /* since dialog already asked */
8406 }
8407#endif
8408
8409#if defined(FEAT_SESSION) && defined(vim_mkdir)
8410 /* When using 'viewdir' may have to create the directory. */
8411 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008412 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413#endif
8414
8415 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8416 if (fd != NULL)
8417 {
8418#ifdef FEAT_SESSION
8419 if (eap->cmdidx == CMD_mkview)
8420 flagp = &vop_flags;
8421 else
8422 flagp = &ssop_flags;
8423#endif
8424
8425#ifdef MKSESSION_NL
8426 /* "unix" in 'sessionoptions': use NL line separator */
8427 if (view_session && (*flagp & SSOP_UNIX))
8428 mksession_nl = TRUE;
8429#endif
8430
8431 /* Write the version command for :mkvimrc */
8432 if (eap->cmdidx == CMD_mkvimrc)
8433 (void)put_line(fd, "version 6.0");
8434
8435#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008436 if (eap->cmdidx == CMD_mksession)
8437 {
8438 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8439 failed = TRUE;
8440 }
8441
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 if (eap->cmdidx != CMD_mkview)
8443#endif
8444 {
8445 /* Write setting 'compatible' first, because it has side effects.
8446 * For that same reason only do it when needed. */
8447 if (p_cp)
8448 (void)put_line(fd, "if !&cp | set cp | endif");
8449 else
8450 (void)put_line(fd, "if &cp | set nocp | endif");
8451 }
8452
8453#ifdef FEAT_SESSION
8454 if (!view_session
8455 || (eap->cmdidx == CMD_mksession
8456 && (*flagp & SSOP_OPTIONS)))
8457#endif
8458 failed |= (makemap(fd, NULL) == FAIL
8459 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8460
8461#ifdef FEAT_SESSION
8462 if (!failed && view_session)
8463 {
8464 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8465 failed = TRUE;
8466 if (eap->cmdidx == CMD_mksession)
8467 {
8468 char_u dirnow[MAXPATHL]; /* current directory */
8469
8470 /*
8471 * Change to session file's dir.
8472 */
8473 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8474 || mch_chdir((char *)dirnow) != 0)
8475 *dirnow = NUL;
8476 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8477 {
8478 if (vim_chdirfile(fname) == OK)
8479 shorten_fnames(TRUE);
8480 }
8481 else if (*dirnow != NUL
8482 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8483 {
8484 (void)mch_chdir((char *)globaldir);
8485 shorten_fnames(TRUE);
8486 }
8487
8488 failed |= (makeopens(fd, dirnow) == FAIL);
8489
8490 /* restore original dir */
8491 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8492 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8493 {
8494 if (mch_chdir((char *)dirnow) != 0)
8495 EMSG(_(e_prev_dir));
8496 shorten_fnames(TRUE);
8497 }
8498 }
8499 else
8500 {
8501 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8502 }
8503 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8504 == FAIL)
8505 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008506 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8507 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008508 if (eap->cmdidx == CMD_mksession)
8509 {
8510 if (put_line(fd, "unlet SessionLoad") == FAIL)
8511 failed = TRUE;
8512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 }
8514#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008515 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8516 failed = TRUE;
8517
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 failed |= fclose(fd);
8519
8520 if (failed)
8521 EMSG(_(e_write));
8522#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8523 else if (eap->cmdidx == CMD_mksession)
8524 {
8525 /* successful session write - set this_session var */
8526 char_u tbuf[MAXPATHL];
8527
8528 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8529 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8530 }
8531#endif
8532#ifdef MKSESSION_NL
8533 mksession_nl = FALSE;
8534#endif
8535 }
8536
8537#ifdef FEAT_BROWSE
8538theend:
8539 vim_free(browseFile);
8540#endif
8541#ifdef FEAT_SESSION
8542 vim_free(viewFile);
8543#endif
8544}
8545
Bram Moolenaardf177f62005-02-22 08:39:57 +00008546#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8547 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008548/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008549 int
8550vim_mkdir_emsg(name, prot)
8551 char_u *name;
8552 int prot;
8553{
8554 if (vim_mkdir(name, prot) != 0)
8555 {
8556 EMSG2(_("E739: Cannot create directory: %s"), name);
8557 return FAIL;
8558 }
8559 return OK;
8560}
8561#endif
8562
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563/*
8564 * Open a file for writing for an Ex command, with some checks.
8565 * Return file descriptor, or NULL on failure.
8566 */
8567 FILE *
8568open_exfile(fname, forceit, mode)
8569 char_u *fname;
8570 int forceit;
8571 char *mode; /* "w" for create new file or "a" for append */
8572{
8573 FILE *fd;
8574
8575#ifdef UNIX
8576 /* with Unix it is possible to open a directory */
8577 if (mch_isdir(fname))
8578 {
8579 EMSG2(_(e_isadir2), fname);
8580 return NULL;
8581 }
8582#endif
8583 if (!forceit && *mode != 'a' && vim_fexists(fname))
8584 {
8585 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8586 return NULL;
8587 }
8588
8589 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8590 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8591
8592 return fd;
8593}
8594
8595/*
8596 * ":mark" and ":k".
8597 */
8598 static void
8599ex_mark(eap)
8600 exarg_T *eap;
8601{
8602 pos_T pos;
8603
8604 if (*eap->arg == NUL) /* No argument? */
8605 EMSG(_(e_argreq));
8606 else if (eap->arg[1] != NUL) /* more than one character? */
8607 EMSG(_(e_trailing));
8608 else
8609 {
8610 pos = curwin->w_cursor; /* save curwin->w_cursor */
8611 curwin->w_cursor.lnum = eap->line2;
8612 beginline(BL_WHITE | BL_FIX);
8613 if (setmark(*eap->arg) == FAIL) /* set mark */
8614 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8615 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8616 }
8617}
8618
8619/*
8620 * Update w_topline, w_leftcol and the cursor position.
8621 */
8622 void
8623update_topline_cursor()
8624{
8625 check_cursor(); /* put cursor on valid line */
8626 update_topline();
8627 if (!curwin->w_p_wrap)
8628 validate_cursor();
8629 update_curswant();
8630}
8631
8632#ifdef FEAT_EX_EXTRA
8633/*
8634 * ":normal[!] {commands}": Execute normal mode commands.
8635 */
8636 static void
8637ex_normal(eap)
8638 exarg_T *eap;
8639{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 int save_msg_scroll = msg_scroll;
8641 int save_restart_edit = restart_edit;
8642 int save_msg_didout = msg_didout;
8643 int save_State = State;
8644 tasave_T tabuf;
8645 int save_insertmode = p_im;
8646 int save_finish_op = finish_op;
8647#ifdef FEAT_MBYTE
8648 char_u *arg = NULL;
8649 int l;
8650 char_u *p;
8651#endif
8652
8653 if (ex_normal_busy >= p_mmd)
8654 {
8655 EMSG(_("E192: Recursive use of :normal too deep"));
8656 return;
8657 }
8658 ++ex_normal_busy;
8659
8660 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8661 restart_edit = 0; /* don't go to Insert mode */
8662 p_im = FALSE; /* don't use 'insertmode' */
8663
8664#ifdef FEAT_MBYTE
8665 /*
8666 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8667 * this for the K_SPECIAL leading byte, otherwise special keys will not
8668 * work.
8669 */
8670 if (has_mbyte)
8671 {
8672 int len = 0;
8673
8674 /* Count the number of characters to be escaped. */
8675 for (p = eap->arg; *p != NUL; ++p)
8676 {
8677# ifdef FEAT_GUI
8678 if (*p == CSI) /* leadbyte CSI */
8679 len += 2;
8680# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008681 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8683# ifdef FEAT_GUI
8684 || *p == CSI
8685# endif
8686 )
8687 len += 2;
8688 }
8689 if (len > 0)
8690 {
8691 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8692 if (arg != NULL)
8693 {
8694 len = 0;
8695 for (p = eap->arg; *p != NUL; ++p)
8696 {
8697 arg[len++] = *p;
8698# ifdef FEAT_GUI
8699 if (*p == CSI)
8700 {
8701 arg[len++] = KS_EXTRA;
8702 arg[len++] = (int)KE_CSI;
8703 }
8704# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008705 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 {
8707 arg[len++] = *++p;
8708 if (*p == K_SPECIAL)
8709 {
8710 arg[len++] = KS_SPECIAL;
8711 arg[len++] = KE_FILLER;
8712 }
8713# ifdef FEAT_GUI
8714 else if (*p == CSI)
8715 {
8716 arg[len++] = KS_EXTRA;
8717 arg[len++] = (int)KE_CSI;
8718 }
8719# endif
8720 }
8721 arg[len] = NUL;
8722 }
8723 }
8724 }
8725 }
8726#endif
8727
8728 /*
8729 * Save the current typeahead. This is required to allow using ":normal"
8730 * from an event handler and makes sure we don't hang when the argument
8731 * ends with half a command.
8732 */
8733 save_typeahead(&tabuf);
8734 if (tabuf.typebuf_valid)
8735 {
8736 /*
8737 * Repeat the :normal command for each line in the range. When no
8738 * range given, execute it just once, without positioning the cursor
8739 * first.
8740 */
8741 do
8742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 if (eap->addr_count != 0)
8744 {
8745 curwin->w_cursor.lnum = eap->line1++;
8746 curwin->w_cursor.col = 0;
8747 }
8748
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008749 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750#ifdef FEAT_MBYTE
8751 arg != NULL ? arg :
8752#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008753 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 }
8755 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8756 }
8757
8758 /* Might not return to the main loop when in an event handler. */
8759 update_topline_cursor();
8760
8761 /* Restore the previous typeahead. */
8762 restore_typeahead(&tabuf);
8763
8764 --ex_normal_busy;
8765 msg_scroll = save_msg_scroll;
8766 restart_edit = save_restart_edit;
8767 p_im = save_insertmode;
8768 finish_op = save_finish_op;
8769 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8770
8771 /* Restore the state (needed when called from a function executed for
8772 * 'indentexpr'). */
8773 State = save_State;
8774#ifdef FEAT_MBYTE
8775 vim_free(arg);
8776#endif
8777}
8778
8779/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008780 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 */
8782 static void
8783ex_startinsert(eap)
8784 exarg_T *eap;
8785{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008786 if (eap->forceit)
8787 {
8788 coladvance((colnr_T)MAXCOL);
8789 curwin->w_curswant = MAXCOL;
8790 curwin->w_set_curswant = FALSE;
8791 }
8792
Bram Moolenaara40c5002005-01-09 21:16:21 +00008793 /* Ignore the command when already in Insert mode. Inserting an
8794 * expression register that invokes a function can do this. */
8795 if (State & INSERT)
8796 return;
8797
Bram Moolenaar64969662005-12-14 21:59:55 +00008798 if (eap->cmdidx == CMD_startinsert)
8799 restart_edit = 'a';
8800 else if (eap->cmdidx == CMD_startreplace)
8801 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008803 restart_edit = 'V';
8804
8805 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008807 if (eap->cmdidx == CMD_startinsert)
8808 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 curwin->w_curswant = 0; /* avoid MAXCOL */
8810 }
8811}
8812
8813/*
8814 * ":stopinsert"
8815 */
8816/*ARGSUSED*/
8817 static void
8818ex_stopinsert(eap)
8819 exarg_T *eap;
8820{
8821 restart_edit = 0;
8822 stop_insert_mode = TRUE;
8823}
8824#endif
8825
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008826#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8827/*
8828 * Execute normal mode command "cmd".
8829 * "remap" can be REMAP_NONE or REMAP_YES.
8830 */
8831 void
8832exec_normal_cmd(cmd, remap, silent)
8833 char_u *cmd;
8834 int remap;
8835 int silent;
8836{
8837 oparg_T oa;
8838
8839 /*
8840 * Stuff the argument into the typeahead buffer.
8841 * Execute normal_cmd() until there is no typeahead left.
8842 */
8843 clear_oparg(&oa);
8844 finish_op = FALSE;
8845 ins_typebuf(cmd, remap, 0, TRUE, silent);
8846 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8847 && !got_int)
8848 {
8849 update_topline_cursor();
8850 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8851 }
8852}
8853#endif
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855#ifdef FEAT_FIND_ID
8856 static void
8857ex_checkpath(eap)
8858 exarg_T *eap;
8859{
8860 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8861 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8862 (linenr_T)1, (linenr_T)MAXLNUM);
8863}
8864
8865#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8866/*
8867 * ":psearch"
8868 */
8869 static void
8870ex_psearch(eap)
8871 exarg_T *eap;
8872{
8873 g_do_tagpreview = p_pvh;
8874 ex_findpat(eap);
8875 g_do_tagpreview = 0;
8876}
8877#endif
8878
8879 static void
8880ex_findpat(eap)
8881 exarg_T *eap;
8882{
8883 int whole = TRUE;
8884 long n;
8885 char_u *p;
8886 int action;
8887
8888 switch (cmdnames[eap->cmdidx].cmd_name[2])
8889 {
8890 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8891 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8892 action = ACTION_GOTO;
8893 else
8894 action = ACTION_SHOW;
8895 break;
8896 case 'i': /* ":ilist" and ":dlist" */
8897 action = ACTION_SHOW_ALL;
8898 break;
8899 case 'u': /* ":ijump" and ":djump" */
8900 action = ACTION_GOTO;
8901 break;
8902 default: /* ":isplit" and ":dsplit" */
8903 action = ACTION_SPLIT;
8904 break;
8905 }
8906
8907 n = 1;
8908 if (vim_isdigit(*eap->arg)) /* get count */
8909 {
8910 n = getdigits(&eap->arg);
8911 eap->arg = skipwhite(eap->arg);
8912 }
8913 if (*eap->arg == '/') /* Match regexp, not just whole words */
8914 {
8915 whole = FALSE;
8916 ++eap->arg;
8917 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8918 if (*p)
8919 {
8920 *p++ = NUL;
8921 p = skipwhite(p);
8922
8923 /* Check for trailing illegal characters */
8924 if (!ends_excmd(*p))
8925 eap->errmsg = e_trailing;
8926 else
8927 eap->nextcmd = check_nextcmd(p);
8928 }
8929 }
8930 if (!eap->skip)
8931 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8932 whole, !eap->forceit,
8933 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8934 n, action, eap->line1, eap->line2);
8935}
8936#endif
8937
8938#ifdef FEAT_WINDOWS
8939
8940# ifdef FEAT_QUICKFIX
8941/*
8942 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8943 */
8944 static void
8945ex_ptag(eap)
8946 exarg_T *eap;
8947{
8948 g_do_tagpreview = p_pvh;
8949 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8950}
8951
8952/*
8953 * ":pedit"
8954 */
8955 static void
8956ex_pedit(eap)
8957 exarg_T *eap;
8958{
8959 win_T *curwin_save = curwin;
8960
8961 g_do_tagpreview = p_pvh;
8962 prepare_tagpreview();
8963 keep_help_flag = curwin_save->w_buffer->b_help;
8964 do_exedit(eap, NULL);
8965 keep_help_flag = FALSE;
8966 if (curwin != curwin_save && win_valid(curwin_save))
8967 {
8968 /* Return cursor to where we were */
8969 validate_cursor();
8970 redraw_later(VALID);
8971 win_enter(curwin_save, TRUE);
8972 }
8973 g_do_tagpreview = 0;
8974}
8975# endif
8976
8977/*
8978 * ":stag", ":stselect" and ":stjump".
8979 */
8980 static void
8981ex_stag(eap)
8982 exarg_T *eap;
8983{
8984 postponed_split = -1;
8985 postponed_split_flags = cmdmod.split;
8986 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8987 postponed_split_flags = 0;
8988}
8989#endif
8990
8991/*
8992 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8993 */
8994 static void
8995ex_tag(eap)
8996 exarg_T *eap;
8997{
8998 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8999}
9000
9001 static void
9002ex_tag_cmd(eap, name)
9003 exarg_T *eap;
9004 char_u *name;
9005{
9006 int cmd;
9007
9008 switch (name[1])
9009 {
9010 case 'j': cmd = DT_JUMP; /* ":tjump" */
9011 break;
9012 case 's': cmd = DT_SELECT; /* ":tselect" */
9013 break;
9014 case 'p': cmd = DT_PREV; /* ":tprevious" */
9015 break;
9016 case 'N': cmd = DT_PREV; /* ":tNext" */
9017 break;
9018 case 'n': cmd = DT_NEXT; /* ":tnext" */
9019 break;
9020 case 'o': cmd = DT_POP; /* ":pop" */
9021 break;
9022 case 'f': /* ":tfirst" */
9023 case 'r': cmd = DT_FIRST; /* ":trewind" */
9024 break;
9025 case 'l': cmd = DT_LAST; /* ":tlast" */
9026 break;
9027 default: /* ":tag" */
9028#ifdef FEAT_CSCOPE
9029 if (p_cst)
9030 {
9031 do_cstag(eap);
9032 return;
9033 }
9034#endif
9035 cmd = DT_TAG;
9036 break;
9037 }
9038
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009039 if (name[0] == 'l')
9040 {
9041#ifndef FEAT_QUICKFIX
9042 ex_ni(eap);
9043 return;
9044#else
9045 cmd = DT_LTAG;
9046#endif
9047 }
9048
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9050 eap->forceit, TRUE);
9051}
9052
9053/*
9054 * Evaluate cmdline variables.
9055 *
9056 * change '%' to curbuf->b_ffname
9057 * '#' to curwin->w_altfile
9058 * '<cword>' to word under the cursor
9059 * '<cWORD>' to WORD under the cursor
9060 * '<cfile>' to path name under the cursor
9061 * '<sfile>' to sourced file name
9062 * '<afile>' to file name for autocommand
9063 * '<abuf>' to buffer number for autocommand
9064 * '<amatch>' to matching name for autocommand
9065 *
9066 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9067 * "" for error without a message) and NULL is returned.
9068 * Returns an allocated string if a valid match was found.
9069 * Returns NULL if no match was found. "usedlen" then still contains the
9070 * number of characters to skip.
9071 */
9072 char_u *
9073eval_vars(src, usedlen, lnump, errormsg, srcstart)
9074 char_u *src; /* pointer into commandline */
9075 int *usedlen; /* characters after src that are used */
9076 linenr_T *lnump; /* line number for :e command, or NULL */
9077 char_u **errormsg; /* pointer to error message */
9078 char_u *srcstart; /* beginning of valid memory for src */
9079{
9080 int i;
9081 char_u *s;
9082 char_u *result;
9083 char_u *resultbuf = NULL;
9084 int resultlen;
9085 buf_T *buf;
9086 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9087 int spec_idx;
9088#ifdef FEAT_MODIFY_FNAME
9089 int skip_mod = FALSE;
9090#endif
9091 static char *(spec_str[]) =
9092 {
9093 "%",
9094#define SPEC_PERC 0
9095 "#",
9096#define SPEC_HASH 1
9097 "<cword>", /* cursor word */
9098#define SPEC_CWORD 2
9099 "<cWORD>", /* cursor WORD */
9100#define SPEC_CCWORD 3
9101 "<cfile>", /* cursor path name */
9102#define SPEC_CFILE 4
9103 "<sfile>", /* ":so" file name */
9104#define SPEC_SFILE 5
9105#ifdef FEAT_AUTOCMD
9106 "<afile>", /* autocommand file name */
9107# define SPEC_AFILE 6
9108 "<abuf>", /* autocommand buffer number */
9109# define SPEC_ABUF 7
9110 "<amatch>", /* autocommand match name */
9111# define SPEC_AMATCH 8
9112#endif
9113#ifdef FEAT_CLIENTSERVER
9114 "<client>"
9115# define SPEC_CLIENT 9
9116#endif
9117 };
9118#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9119
9120#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9121 char_u strbuf[30];
9122#endif
9123
9124 *errormsg = NULL;
9125
9126 /*
9127 * Check if there is something to do.
9128 */
9129 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9130 {
9131 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9132 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9133 break;
9134 }
9135 if (spec_idx == SPEC_COUNT) /* no match */
9136 {
9137 *usedlen = 1;
9138 return NULL;
9139 }
9140
9141 /*
9142 * Skip when preceded with a backslash "\%" and "\#".
9143 * Note: In "\\%" the % is also not recognized!
9144 */
9145 if (src > srcstart && src[-1] == '\\')
9146 {
9147 *usedlen = 0;
9148 STRCPY(src - 1, src); /* remove backslash */
9149 return NULL;
9150 }
9151
9152 /*
9153 * word or WORD under cursor
9154 */
9155 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9156 {
9157 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9158 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9159 if (resultlen == 0)
9160 {
9161 *errormsg = (char_u *)"";
9162 return NULL;
9163 }
9164 }
9165
9166 /*
9167 * '#': Alternate file name
9168 * '%': Current file name
9169 * File name under the cursor
9170 * File name for autocommand
9171 * and following modifiers
9172 */
9173 else
9174 {
9175 switch (spec_idx)
9176 {
9177 case SPEC_PERC: /* '%': current file */
9178 if (curbuf->b_fname == NULL)
9179 {
9180 result = (char_u *)"";
9181 valid = 0; /* Must have ":p:h" to be valid */
9182 }
9183 else
9184#ifdef RISCOS
9185 /* Always use the full path for RISC OS if possible. */
9186 result = curbuf->b_ffname;
9187 if (result == NULL)
9188 result = curbuf->b_fname;
9189#else
9190 result = curbuf->b_fname;
9191#endif
9192 break;
9193
9194 case SPEC_HASH: /* '#' or "#99": alternate file */
9195 if (src[1] == '#') /* "##": the argument list */
9196 {
9197 result = arg_all();
9198 resultbuf = result;
9199 *usedlen = 2;
9200#ifdef FEAT_MODIFY_FNAME
9201 skip_mod = TRUE;
9202#endif
9203 break;
9204 }
9205 s = src + 1;
9206 i = (int)getdigits(&s);
9207 *usedlen = (int)(s - src); /* length of what we expand */
9208
9209 buf = buflist_findnr(i);
9210 if (buf == NULL)
9211 {
9212 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9213 return NULL;
9214 }
9215 if (lnump != NULL)
9216 *lnump = ECMD_LAST;
9217 if (buf->b_fname == NULL)
9218 {
9219 result = (char_u *)"";
9220 valid = 0; /* Must have ":p:h" to be valid */
9221 }
9222 else
9223 result = buf->b_fname;
9224 break;
9225
9226#ifdef FEAT_SEARCHPATH
9227 case SPEC_CFILE: /* file name under cursor */
9228 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
9229 if (result == NULL)
9230 {
9231 *errormsg = (char_u *)"";
9232 return NULL;
9233 }
9234 resultbuf = result; /* remember allocated string */
9235 break;
9236#endif
9237
9238#ifdef FEAT_AUTOCMD
9239 case SPEC_AFILE: /* file name for autocommand */
9240 result = autocmd_fname;
9241 if (result == NULL)
9242 {
9243 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9244 return NULL;
9245 }
9246 break;
9247
9248 case SPEC_ABUF: /* buffer number for autocommand */
9249 if (autocmd_bufnr <= 0)
9250 {
9251 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9252 return NULL;
9253 }
9254 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9255 result = strbuf;
9256 break;
9257
9258 case SPEC_AMATCH: /* match name for autocommand */
9259 result = autocmd_match;
9260 if (result == NULL)
9261 {
9262 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9263 return NULL;
9264 }
9265 break;
9266
9267#endif
9268 case SPEC_SFILE: /* file name for ":so" command */
9269 result = sourcing_name;
9270 if (result == NULL)
9271 {
9272 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9273 return NULL;
9274 }
9275 break;
9276#if defined(FEAT_CLIENTSERVER)
9277 case SPEC_CLIENT: /* Source of last submitted input */
9278 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9279 result = strbuf;
9280 break;
9281#endif
9282 }
9283
9284 resultlen = (int)STRLEN(result); /* length of new string */
9285 if (src[*usedlen] == '<') /* remove the file name extension */
9286 {
9287 ++*usedlen;
9288#ifdef RISCOS
9289 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9290#else
9291 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9292#endif
9293 resultlen = (int)(s - result);
9294 }
9295#ifdef FEAT_MODIFY_FNAME
9296 else if (!skip_mod)
9297 {
9298 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9299 &resultlen);
9300 if (result == NULL)
9301 {
9302 *errormsg = (char_u *)"";
9303 return NULL;
9304 }
9305 }
9306#endif
9307 }
9308
9309 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9310 {
9311 if (valid != VALID_HEAD + VALID_PATH)
9312 /* xgettext:no-c-format */
9313 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9314 else
9315 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9316 result = NULL;
9317 }
9318 else
9319 result = vim_strnsave(result, resultlen);
9320 vim_free(resultbuf);
9321 return result;
9322}
9323
9324/*
9325 * Concatenate all files in the argument list, separated by spaces, and return
9326 * it in one allocated string.
9327 * Spaces and backslashes in the file names are escaped with a backslash.
9328 * Returns NULL when out of memory.
9329 */
9330 static char_u *
9331arg_all()
9332{
9333 int len;
9334 int idx;
9335 char_u *retval = NULL;
9336 char_u *p;
9337
9338 /*
9339 * Do this loop two times:
9340 * first time: compute the total length
9341 * second time: concatenate the names
9342 */
9343 for (;;)
9344 {
9345 len = 0;
9346 for (idx = 0; idx < ARGCOUNT; ++idx)
9347 {
9348 p = alist_name(&ARGLIST[idx]);
9349 if (p != NULL)
9350 {
9351 if (len > 0)
9352 {
9353 /* insert a space in between names */
9354 if (retval != NULL)
9355 retval[len] = ' ';
9356 ++len;
9357 }
9358 for ( ; *p != NUL; ++p)
9359 {
9360 if (*p == ' ' || *p == '\\')
9361 {
9362 /* insert a backslash */
9363 if (retval != NULL)
9364 retval[len] = '\\';
9365 ++len;
9366 }
9367 if (retval != NULL)
9368 retval[len] = *p;
9369 ++len;
9370 }
9371 }
9372 }
9373
9374 /* second time: break here */
9375 if (retval != NULL)
9376 {
9377 retval[len] = NUL;
9378 break;
9379 }
9380
9381 /* allocate memory */
9382 retval = alloc(len + 1);
9383 if (retval == NULL)
9384 break;
9385 }
9386
9387 return retval;
9388}
9389
9390#if defined(FEAT_AUTOCMD) || defined(PROTO)
9391/*
9392 * Expand the <sfile> string in "arg".
9393 *
9394 * Returns an allocated string, or NULL for any error.
9395 */
9396 char_u *
9397expand_sfile(arg)
9398 char_u *arg;
9399{
9400 char_u *errormsg;
9401 int len;
9402 char_u *result;
9403 char_u *newres;
9404 char_u *repl;
9405 int srclen;
9406 char_u *p;
9407
9408 result = vim_strsave(arg);
9409 if (result == NULL)
9410 return NULL;
9411
9412 for (p = result; *p; )
9413 {
9414 if (STRNCMP(p, "<sfile>", 7) != 0)
9415 ++p;
9416 else
9417 {
9418 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9419 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9420 if (errormsg != NULL)
9421 {
9422 if (*errormsg)
9423 emsg(errormsg);
9424 vim_free(result);
9425 return NULL;
9426 }
9427 if (repl == NULL) /* no match (cannot happen) */
9428 {
9429 p += srclen;
9430 continue;
9431 }
9432 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9433 newres = alloc(len);
9434 if (newres == NULL)
9435 {
9436 vim_free(repl);
9437 vim_free(result);
9438 return NULL;
9439 }
9440 mch_memmove(newres, result, (size_t)(p - result));
9441 STRCPY(newres + (p - result), repl);
9442 len = (int)STRLEN(newres);
9443 STRCAT(newres, p + srclen);
9444 vim_free(repl);
9445 vim_free(result);
9446 result = newres;
9447 p = newres + len; /* continue after the match */
9448 }
9449 }
9450
9451 return result;
9452}
9453#endif
9454
9455#ifdef FEAT_SESSION
9456static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9457static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9458static frame_T *ses_skipframe __ARGS((frame_T *fr));
9459static int ses_do_frame __ARGS((frame_T *fr));
9460static int ses_do_win __ARGS((win_T *wp));
9461static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9462static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9463static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9464
9465/*
9466 * Write openfile commands for the current buffers to an .exrc file.
9467 * Return FAIL on error, OK otherwise.
9468 */
9469 static int
9470makeopens(fd, dirnow)
9471 FILE *fd;
9472 char_u *dirnow; /* Current directory name */
9473{
9474 buf_T *buf;
9475 int only_save_windows = TRUE;
9476 int nr;
9477 int cnr = 1;
9478 int restore_size = TRUE;
9479 win_T *wp;
9480 char_u *sname;
9481 win_T *edited_win = NULL;
9482
9483 if (ssop_flags & SSOP_BUFFERS)
9484 only_save_windows = FALSE; /* Save ALL buffers */
9485
9486 /*
9487 * Begin by setting the this_session variable, and then other
9488 * sessionable variables.
9489 */
9490#ifdef FEAT_EVAL
9491 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9492 return FAIL;
9493 if (ssop_flags & SSOP_GLOBALS)
9494 if (store_session_globals(fd) == FAIL)
9495 return FAIL;
9496#endif
9497
9498 /*
9499 * Close all windows but one.
9500 */
9501 if (put_line(fd, "silent only") == FAIL)
9502 return FAIL;
9503
9504 /*
9505 * Now a :cd command to the session directory or the current directory
9506 */
9507 if (ssop_flags & SSOP_SESDIR)
9508 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009509 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9510 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 return FAIL;
9512 }
9513 else if (ssop_flags & SSOP_CURDIR)
9514 {
9515 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9516 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009517 || fputs("cd ", fd) < 0
9518 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9519 || put_eol(fd) == FAIL)
9520 {
9521 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 vim_free(sname);
9525 }
9526
9527 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009528 * If there is an empty, unnamed buffer we will wipe it out later.
9529 * Remember the buffer number.
9530 */
9531 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9532 return FAIL;
9533 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9534 return FAIL;
9535 if (put_line(fd, "endif") == FAIL)
9536 return FAIL;
9537
9538 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 * Now save the current files, current buffer first.
9540 */
9541 if (put_line(fd, "set shortmess=aoO") == FAIL)
9542 return FAIL;
9543
9544 /* Now put the other buffers into the buffer list */
9545 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9546 {
9547 if (!(only_save_windows && buf->b_nwindows == 0)
9548 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9549 && buf->b_fname != NULL
9550 && buf->b_p_bl)
9551 {
9552 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9553 : buf->b_wininfo->wi_fpos.lnum) < 0
9554 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9555 return FAIL;
9556 }
9557 }
9558
9559 /* the global argument list */
9560 if (ses_arglist(fd, "args", &global_alist.al_ga,
9561 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9562 return FAIL;
9563
9564 if (ssop_flags & SSOP_RESIZE)
9565 {
9566 /* Note: after the restore we still check it worked!*/
9567 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9568 || put_eol(fd) == FAIL)
9569 return FAIL;
9570 }
9571
9572#ifdef FEAT_GUI
9573 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9574 {
9575 int x, y;
9576
9577 if (gui_mch_get_winpos(&x, &y) == OK)
9578 {
9579 /* Note: after the restore we still check it worked!*/
9580 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9581 return FAIL;
9582 }
9583 }
9584#endif
9585
9586 /*
9587 * Before creating the window layout, try loading one file. If this is
9588 * aborted we don't end up with a number of useless windows.
9589 * This may have side effects! (e.g., compressed or network file).
9590 */
9591 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9592 {
9593 if (ses_do_win(wp)
9594 && wp->w_buffer->b_ffname != NULL
9595 && !wp->w_buffer->b_help
9596#ifdef FEAT_QUICKFIX
9597 && !bt_nofile(wp->w_buffer)
9598#endif
9599 )
9600 {
9601 if (fputs("edit ", fd) < 0
9602 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9603 return FAIL;
9604 if (!wp->w_arg_idx_invalid)
9605 edited_win = wp;
9606 break;
9607 }
9608 }
9609
9610 /*
9611 * Save current window layout.
9612 */
9613 if (put_line(fd, "set splitbelow splitright") == FAIL)
9614 return FAIL;
9615 if (ses_win_rec(fd, topframe) == FAIL)
9616 return FAIL;
9617 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9618 return FAIL;
9619 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9620 return FAIL;
9621
9622 /*
9623 * Check if window sizes can be restored (no windows omitted).
9624 * Remember the window number of the current window after restoring.
9625 */
9626 nr = 0;
9627 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9628 {
9629 if (ses_do_win(wp))
9630 ++nr;
9631 else
9632 restore_size = FALSE;
9633 if (curwin == wp)
9634 cnr = nr;
9635 }
9636
9637 /* Go to the first window. */
9638 if (put_line(fd, "wincmd t") == FAIL)
9639 return FAIL;
9640
9641 /*
9642 * If more than one window, see if sizes can be restored.
9643 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9644 * resized when moving between windows.
9645 * Do this before restoring the view, so that the topline and the cursor
9646 * can be set. This is done again below.
9647 */
9648 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9649 return FAIL;
9650 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9651 return FAIL;
9652
9653 /*
9654 * Restore the view of the window (options, file, cursor, etc.).
9655 */
9656 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9657 {
9658 if (!ses_do_win(wp))
9659 continue;
9660 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9661 return FAIL;
9662 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9663 return FAIL;
9664 }
9665
9666 /*
9667 * Restore cursor to the current window if it's not the first one.
9668 */
9669 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9670 return FAIL;
9671
9672 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009673 * Wipe out an empty unnamed buffer we started in.
9674 */
9675 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9676 return FAIL;
9677 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9678 return FAIL;
9679 if (put_line(fd, "endif") == FAIL)
9680 return FAIL;
9681 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9682 return FAIL;
9683
9684 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 * Restore window sizes again after jumping around in windows, because the
9686 * current window has a minimum size while others may not.
9687 */
9688 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9689 return FAIL;
9690
9691 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9692 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9693 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9694 return FAIL;
9695
9696 /*
9697 * Lastly, execute the x.vim file if it exists.
9698 */
9699 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9700 || put_line(fd, "if file_readable(s:sx)") == FAIL
9701 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9702 || put_line(fd, "endif") == FAIL)
9703 return FAIL;
9704
9705 return OK;
9706}
9707
9708 static int
9709ses_winsizes(fd, restore_size)
9710 FILE *fd;
9711 int restore_size;
9712{
9713 int n = 0;
9714 win_T *wp;
9715
9716 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9717 {
9718 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9719 {
9720 if (!ses_do_win(wp))
9721 continue;
9722 ++n;
9723
9724 /* restore height when not full height */
9725 if (wp->w_height + wp->w_status_height < topframe->fr_height
9726 && (fprintf(fd,
9727 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9728 n, (long)wp->w_height, Rows / 2, Rows) < 0
9729 || put_eol(fd) == FAIL))
9730 return FAIL;
9731
9732 /* restore width when not full width */
9733 if (wp->w_width < Columns && (fprintf(fd,
9734 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9735 n, (long)wp->w_width, Columns / 2, Columns) < 0
9736 || put_eol(fd) == FAIL))
9737 return FAIL;
9738 }
9739 }
9740 else
9741 {
9742 /* Just equalise window sizes */
9743 if (put_line(fd, "wincmd =") == FAIL)
9744 return FAIL;
9745 }
9746 return OK;
9747}
9748
9749/*
9750 * Write commands to "fd" to recursively create windows for frame "fr",
9751 * horizontally and vertically split.
9752 * After the commands the last window in the frame is the current window.
9753 * Returns FAIL when writing the commands to "fd" fails.
9754 */
9755 static int
9756ses_win_rec(fd, fr)
9757 FILE *fd;
9758 frame_T *fr;
9759{
9760 frame_T *frc;
9761 int count = 0;
9762
9763 if (fr->fr_layout != FR_LEAF)
9764 {
9765 /* Find first frame that's not skipped and then create a window for
9766 * each following one (first frame is already there). */
9767 frc = ses_skipframe(fr->fr_child);
9768 if (frc != NULL)
9769 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9770 {
9771 /* Make window as big as possible so that we have lots of room
9772 * to split. */
9773 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9774 || put_line(fd, fr->fr_layout == FR_COL
9775 ? "split" : "vsplit") == FAIL)
9776 return FAIL;
9777 ++count;
9778 }
9779
9780 /* Go back to the first window. */
9781 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9782 ? "%dwincmd k" : "%dwincmd h", count) < 0
9783 || put_eol(fd) == FAIL))
9784 return FAIL;
9785
9786 /* Recursively create frames/windows in each window of this column or
9787 * row. */
9788 frc = ses_skipframe(fr->fr_child);
9789 while (frc != NULL)
9790 {
9791 ses_win_rec(fd, frc);
9792 frc = ses_skipframe(frc->fr_next);
9793 /* Go to next window. */
9794 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9795 return FAIL;
9796 }
9797 }
9798 return OK;
9799}
9800
9801/*
9802 * Skip frames that don't contain windows we want to save in the Session.
9803 * Returns NULL when there none.
9804 */
9805 static frame_T *
9806ses_skipframe(fr)
9807 frame_T *fr;
9808{
9809 frame_T *frc;
9810
9811 for (frc = fr; frc != NULL; frc = frc->fr_next)
9812 if (ses_do_frame(frc))
9813 break;
9814 return frc;
9815}
9816
9817/*
9818 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9819 * the Session.
9820 */
9821 static int
9822ses_do_frame(fr)
9823 frame_T *fr;
9824{
9825 frame_T *frc;
9826
9827 if (fr->fr_layout == FR_LEAF)
9828 return ses_do_win(fr->fr_win);
9829 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9830 if (ses_do_frame(frc))
9831 return TRUE;
9832 return FALSE;
9833}
9834
9835/*
9836 * Return non-zero if window "wp" is to be stored in the Session.
9837 */
9838 static int
9839ses_do_win(wp)
9840 win_T *wp;
9841{
9842 if (wp->w_buffer->b_fname == NULL
9843#ifdef FEAT_QUICKFIX
9844 /* When 'buftype' is "nofile" can't restore the window contents. */
9845 || bt_nofile(wp->w_buffer)
9846#endif
9847 )
9848 return (ssop_flags & SSOP_BLANK);
9849 if (wp->w_buffer->b_help)
9850 return (ssop_flags & SSOP_HELP);
9851 return TRUE;
9852}
9853
9854/*
9855 * Write commands to "fd" to restore the view of a window.
9856 * Caller must make sure 'scrolloff' is zero.
9857 */
9858 static int
9859put_view(fd, wp, add_edit, flagp)
9860 FILE *fd;
9861 win_T *wp;
9862 int add_edit; /* add ":edit" command to view */
9863 unsigned *flagp; /* vop_flags or ssop_flags */
9864{
9865 win_T *save_curwin;
9866 int f;
9867 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009868 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
9870 /* Always restore cursor position for ":mksession". For ":mkview" only
9871 * when 'viewoptions' contains "cursor". */
9872 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9873
9874 /*
9875 * Local argument list.
9876 */
9877 if (wp->w_alist == &global_alist)
9878 {
9879 if (put_line(fd, "argglobal") == FAIL)
9880 return FAIL;
9881 }
9882 else
9883 {
9884 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9885 flagp == &vop_flags
9886 || !(*flagp & SSOP_CURDIR)
9887 || wp->w_localdir != NULL, flagp) == FAIL)
9888 return FAIL;
9889 }
9890
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009891 /* Only when part of a session: restore the argument index. Some
9892 * arguments may have been deleted, check if the index is valid. */
9893 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9894 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 {
9896 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9897 || put_eol(fd) == FAIL)
9898 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009899 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 }
9901
9902 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009903 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 {
9905 /*
9906 * Load the file.
9907 */
9908 if (wp->w_buffer->b_ffname != NULL
9909#ifdef FEAT_QUICKFIX
9910 && !bt_nofile(wp->w_buffer)
9911#endif
9912 )
9913 {
9914 /*
9915 * Editing a file in this buffer: use ":edit file".
9916 * This may have side effects! (e.g., compressed or network file).
9917 */
9918 if (fputs("edit ", fd) < 0
9919 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9920 return FAIL;
9921 }
9922 else
9923 {
9924 /* No file in this buffer, just make it empty. */
9925 if (put_line(fd, "enew") == FAIL)
9926 return FAIL;
9927#ifdef FEAT_QUICKFIX
9928 if (wp->w_buffer->b_ffname != NULL)
9929 {
9930 /* The buffer does have a name, but it's not a file name. */
9931 if (fputs("file ", fd) < 0
9932 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9933 return FAIL;
9934 }
9935#endif
9936 do_cursor = FALSE;
9937 }
9938 }
9939
9940 /*
9941 * Local mappings and abbreviations.
9942 */
9943 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9944 && makemap(fd, wp->w_buffer) == FAIL)
9945 return FAIL;
9946
9947 /*
9948 * Local options. Need to go to the window temporarily.
9949 * Store only local values when using ":mkview" and when ":mksession" is
9950 * used and 'sessionoptions' doesn't include "options".
9951 * Some folding options are always stored when "folds" is included,
9952 * otherwise the folds would not be restored correctly.
9953 */
9954 save_curwin = curwin;
9955 curwin = wp;
9956 curbuf = curwin->w_buffer;
9957 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9958 f = makeset(fd, OPT_LOCAL,
9959 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9960#ifdef FEAT_FOLDING
9961 else if (*flagp & SSOP_FOLDS)
9962 f = makefoldset(fd);
9963#endif
9964 else
9965 f = OK;
9966 curwin = save_curwin;
9967 curbuf = curwin->w_buffer;
9968 if (f == FAIL)
9969 return FAIL;
9970
9971#ifdef FEAT_FOLDING
9972 /*
9973 * Save Folds when 'buftype' is empty and for help files.
9974 */
9975 if ((*flagp & SSOP_FOLDS)
9976 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +00009977# ifdef FEAT_QUICKFIX
9978 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
9979# endif
9980 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 {
9982 if (put_folds(fd, wp) == FAIL)
9983 return FAIL;
9984 }
9985#endif
9986
9987 /*
9988 * Set the cursor after creating folds, since that moves the cursor.
9989 */
9990 if (do_cursor)
9991 {
9992
9993 /* Restore the cursor line in the file and relatively in the
9994 * window. Don't use "G", it changes the jumplist. */
9995 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9996 (long)wp->w_cursor.lnum,
9997 (long)(wp->w_cursor.lnum - wp->w_topline),
9998 (long)wp->w_height / 2, (long)wp->w_height) < 0
9999 || put_eol(fd) == FAIL
10000 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10001 || put_line(fd, "exe s:l") == FAIL
10002 || put_line(fd, "normal! zt") == FAIL
10003 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10004 || put_eol(fd) == FAIL)
10005 return FAIL;
10006 /* Restore the cursor column and left offset when not wrapping. */
10007 if (wp->w_cursor.col == 0)
10008 {
10009 if (put_line(fd, "normal! 0") == FAIL)
10010 return FAIL;
10011 }
10012 else
10013 {
10014 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10015 {
10016 if (fprintf(fd,
10017 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10018 (long)wp->w_cursor.col,
10019 (long)(wp->w_cursor.col - wp->w_leftcol),
10020 (long)wp->w_width / 2, (long)wp->w_width) < 0
10021 || put_eol(fd) == FAIL
10022 || put_line(fd, "if s:c > 0") == FAIL
10023 || fprintf(fd,
10024 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10025 (long)wp->w_cursor.col) < 0
10026 || put_eol(fd) == FAIL
10027 || put_line(fd, "else") == FAIL
10028 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10029 || put_eol(fd) == FAIL
10030 || put_line(fd, "endif") == FAIL)
10031 return FAIL;
10032 }
10033 else
10034 {
10035 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10036 || put_eol(fd) == FAIL)
10037 return FAIL;
10038 }
10039 }
10040 }
10041
10042 /*
10043 * Local directory.
10044 */
10045 if (wp->w_localdir != NULL)
10046 {
10047 if (fputs("lcd ", fd) < 0
10048 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10049 || put_eol(fd) == FAIL)
10050 return FAIL;
10051 }
10052
10053 return OK;
10054}
10055
10056/*
10057 * Write an argument list to the session file.
10058 * Returns FAIL if writing fails.
10059 */
10060 static int
10061ses_arglist(fd, cmd, gap, fullname, flagp)
10062 FILE *fd;
10063 char *cmd;
10064 garray_T *gap;
10065 int fullname; /* TRUE: use full path name */
10066 unsigned *flagp;
10067{
10068 int i;
10069 char_u buf[MAXPATHL];
10070 char_u *s;
10071
10072 if (gap->ga_len == 0)
10073 return put_line(fd, "silent! argdel *");
10074 if (fputs(cmd, fd) < 0)
10075 return FAIL;
10076 for (i = 0; i < gap->ga_len; ++i)
10077 {
10078 /* NULL file names are skipped (only happens when out of memory). */
10079 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10080 if (s != NULL)
10081 {
10082 if (fullname)
10083 {
10084 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10085 s = buf;
10086 }
10087 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10088 return FAIL;
10089 }
10090 }
10091 return put_eol(fd);
10092}
10093
10094/*
10095 * Write a buffer name to the session file.
10096 * Also ends the line.
10097 * Returns FAIL if writing fails.
10098 */
10099 static int
10100ses_fname(fd, buf, flagp)
10101 FILE *fd;
10102 buf_T *buf;
10103 unsigned *flagp;
10104{
10105 char_u *name;
10106
10107 /* Use the short file name if the current directory is known at the time
10108 * the session file will be sourced. Don't do this for ":mkview", we
10109 * don't know the current directory. */
10110 if (buf->b_sfname != NULL
10111 && flagp == &ssop_flags
10112 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10113 name = buf->b_sfname;
10114 else
10115 name = buf->b_ffname;
10116 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10117 return FAIL;
10118 return OK;
10119}
10120
10121/*
10122 * Write a file name to the session file.
10123 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10124 * characters.
10125 * Returns FAIL if writing fails.
10126 */
10127 static int
10128ses_put_fname(fd, name, flagp)
10129 FILE *fd;
10130 char_u *name;
10131 unsigned *flagp;
10132{
10133 char_u *sname;
10134 int retval = OK;
10135 int c;
10136
10137 sname = home_replace_save(NULL, name);
10138 if (sname != NULL)
10139 name = sname;
10140 while (*name != NUL)
10141 {
10142#ifdef FEAT_MBYTE
10143 {
10144 int l;
10145
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010146 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 {
10148 /* copy a multibyte char */
10149 while (--l >= 0)
10150 {
10151 if (putc(*name, fd) != *name)
10152 retval = FAIL;
10153 ++name;
10154 }
10155 continue;
10156 }
10157 }
10158#endif
10159 c = *name++;
10160 if (c == '\\' && (*flagp & SSOP_SLASH))
10161 /* change a backslash to a forward slash */
10162 c = '/';
10163 else if ((vim_strchr(escape_chars, c) != NULL
10164#ifdef BACKSLASH_IN_FILENAME
10165 && c != '\\'
10166#endif
10167 ) || c == '#' || c == '%')
10168 {
10169 /* escape a special character with a backslash */
10170 if (putc('\\', fd) != '\\')
10171 retval = FAIL;
10172 }
10173 if (putc(c, fd) != c)
10174 retval = FAIL;
10175 }
10176 vim_free(sname);
10177 return retval;
10178}
10179
10180/*
10181 * ":loadview [nr]"
10182 */
10183 static void
10184ex_loadview(eap)
10185 exarg_T *eap;
10186{
10187 char_u *fname;
10188
10189 fname = get_view_file(*eap->arg);
10190 if (fname != NULL)
10191 {
10192 do_source(fname, FALSE, FALSE);
10193 vim_free(fname);
10194 }
10195}
10196
10197/*
10198 * Get the name of the view file for the current buffer.
10199 */
10200 static char_u *
10201get_view_file(c)
10202 int c;
10203{
10204 int len = 0;
10205 char_u *p, *s;
10206 char_u *retval;
10207 char_u *sname;
10208
10209 if (curbuf->b_ffname == NULL)
10210 {
10211 EMSG(_(e_noname));
10212 return NULL;
10213 }
10214 sname = home_replace_save(NULL, curbuf->b_ffname);
10215 if (sname == NULL)
10216 return NULL;
10217
10218 /*
10219 * We want a file name without separators, because we're not going to make
10220 * a directory.
10221 * "normal" path separator -> "=+"
10222 * "=" -> "=="
10223 * ":" path separator -> "=-"
10224 */
10225 for (p = sname; *p; ++p)
10226 if (*p == '=' || vim_ispathsep(*p))
10227 ++len;
10228 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10229 if (retval != NULL)
10230 {
10231 STRCPY(retval, p_vdir);
10232 add_pathsep(retval);
10233 s = retval + STRLEN(retval);
10234 for (p = sname; *p; ++p)
10235 {
10236 if (*p == '=')
10237 {
10238 *s++ = '=';
10239 *s++ = '=';
10240 }
10241 else if (vim_ispathsep(*p))
10242 {
10243 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010244#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 || defined(VMS)
10246 if (*p == ':')
10247 *s++ = '-';
10248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010250 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 }
10252 else
10253 *s++ = *p;
10254 }
10255 *s++ = '=';
10256 *s++ = c;
10257 STRCPY(s, ".vim");
10258 }
10259
10260 vim_free(sname);
10261 return retval;
10262}
10263
10264#endif /* FEAT_SESSION */
10265
10266/*
10267 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10268 * Return FAIL for a write error.
10269 */
10270 int
10271put_eol(fd)
10272 FILE *fd;
10273{
10274 if (
10275#ifdef USE_CRNL
10276 (
10277# ifdef MKSESSION_NL
10278 !mksession_nl &&
10279# endif
10280 (putc('\r', fd) < 0)) ||
10281#endif
10282 (putc('\n', fd) < 0))
10283 return FAIL;
10284 return OK;
10285}
10286
10287/*
10288 * Write a line to "fd".
10289 * Return FAIL for a write error.
10290 */
10291 int
10292put_line(fd, s)
10293 FILE *fd;
10294 char *s;
10295{
10296 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10297 return FAIL;
10298 return OK;
10299}
10300
10301#ifdef FEAT_VIMINFO
10302/*
10303 * ":rviminfo" and ":wviminfo".
10304 */
10305 static void
10306ex_viminfo(eap)
10307 exarg_T *eap;
10308{
10309 char_u *save_viminfo;
10310
10311 save_viminfo = p_viminfo;
10312 if (*p_viminfo == NUL)
10313 p_viminfo = (char_u *)"'100";
10314 if (eap->cmdidx == CMD_rviminfo)
10315 {
10316 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10317 EMSG(_("E195: Cannot open viminfo file for reading"));
10318 }
10319 else
10320 write_viminfo(eap->arg, eap->forceit);
10321 p_viminfo = save_viminfo;
10322}
10323#endif
10324
10325#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010326/*
10327 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010328 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 void
10331dialog_msg(buff, format, fname)
10332 char_u *buff;
10333 char *format;
10334 char_u *fname;
10335{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 if (fname == NULL)
10337 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010338 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339}
10340#endif
10341
10342/*
10343 * ":behave {mswin,xterm}"
10344 */
10345 static void
10346ex_behave(eap)
10347 exarg_T *eap;
10348{
10349 if (STRCMP(eap->arg, "mswin") == 0)
10350 {
10351 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10352 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10353 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10354 set_option_value((char_u *)"keymodel", 0L,
10355 (char_u *)"startsel,stopsel", 0);
10356 }
10357 else if (STRCMP(eap->arg, "xterm") == 0)
10358 {
10359 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10360 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10361 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10362 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10363 }
10364 else
10365 EMSG2(_(e_invarg2), eap->arg);
10366}
10367
10368#ifdef FEAT_AUTOCMD
10369static int filetype_detect = FALSE;
10370static int filetype_plugin = FALSE;
10371static int filetype_indent = FALSE;
10372
10373/*
10374 * ":filetype [plugin] [indent] {on,off,detect}"
10375 * on: Load the filetype.vim file to install autocommands for file types.
10376 * off: Load the ftoff.vim file to remove all autocommands for file types.
10377 * plugin on: load filetype.vim and ftplugin.vim
10378 * plugin off: load ftplugof.vim
10379 * indent on: load filetype.vim and indent.vim
10380 * indent off: load indoff.vim
10381 */
10382 static void
10383ex_filetype(eap)
10384 exarg_T *eap;
10385{
10386 char_u *arg = eap->arg;
10387 int plugin = FALSE;
10388 int indent = FALSE;
10389
10390 if (*eap->arg == NUL)
10391 {
10392 /* Print current status. */
10393 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10394 filetype_detect ? "ON" : "OFF",
10395 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10396 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10397 return;
10398 }
10399
10400 /* Accept "plugin" and "indent" in any order. */
10401 for (;;)
10402 {
10403 if (STRNCMP(arg, "plugin", 6) == 0)
10404 {
10405 plugin = TRUE;
10406 arg = skipwhite(arg + 6);
10407 continue;
10408 }
10409 if (STRNCMP(arg, "indent", 6) == 0)
10410 {
10411 indent = TRUE;
10412 arg = skipwhite(arg + 6);
10413 continue;
10414 }
10415 break;
10416 }
10417 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10418 {
10419 if (*arg == 'o' || !filetype_detect)
10420 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010421 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 filetype_detect = TRUE;
10423 if (plugin)
10424 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010425 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 filetype_plugin = TRUE;
10427 }
10428 if (indent)
10429 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010430 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 filetype_indent = TRUE;
10432 }
10433 }
10434 if (*arg == 'd')
10435 {
10436 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010437 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 }
10439 }
10440 else if (STRCMP(arg, "off") == 0)
10441 {
10442 if (plugin || indent)
10443 {
10444 if (plugin)
10445 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010446 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 filetype_plugin = FALSE;
10448 }
10449 if (indent)
10450 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010451 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 filetype_indent = FALSE;
10453 }
10454 }
10455 else
10456 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010457 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 filetype_detect = FALSE;
10459 }
10460 }
10461 else
10462 EMSG2(_(e_invarg2), arg);
10463}
10464
10465/*
10466 * ":setfiletype {name}"
10467 */
10468 static void
10469ex_setfiletype(eap)
10470 exarg_T *eap;
10471{
10472 if (!did_filetype)
10473 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10474}
10475#endif
10476
10477/*ARGSUSED*/
10478 static void
10479ex_digraphs(eap)
10480 exarg_T *eap;
10481{
10482#ifdef FEAT_DIGRAPHS
10483 if (*eap->arg != NUL)
10484 putdigraph(eap->arg);
10485 else
10486 listdigraphs();
10487#else
10488 EMSG(_("E196: No digraphs in this version"));
10489#endif
10490}
10491
10492 static void
10493ex_set(eap)
10494 exarg_T *eap;
10495{
10496 int flags = 0;
10497
10498 if (eap->cmdidx == CMD_setlocal)
10499 flags = OPT_LOCAL;
10500 else if (eap->cmdidx == CMD_setglobal)
10501 flags = OPT_GLOBAL;
10502#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10503 if (cmdmod.browse && flags == 0)
10504 ex_options(eap);
10505 else
10506#endif
10507 (void)do_set(eap->arg, flags);
10508}
10509
10510#ifdef FEAT_SEARCH_EXTRA
10511/*
10512 * ":nohlsearch"
10513 */
10514/*ARGSUSED*/
10515 static void
10516ex_nohlsearch(eap)
10517 exarg_T *eap;
10518{
10519 no_hlsearch = TRUE;
10520 redraw_all_later(NOT_VALID);
10521}
10522
10523/*
10524 * ":match {group} {pattern}"
10525 * Sets nextcmd to the start of the next command, if any. Also called when
10526 * skipping commands to find the next command.
10527 */
10528 static void
10529ex_match(eap)
10530 exarg_T *eap;
10531{
10532 char_u *p;
10533 char_u *end;
10534 int c;
10535
10536 /* First clear any old pattern. */
10537 if (!eap->skip)
10538 {
10539 vim_free(curwin->w_match.regprog);
10540 curwin->w_match.regprog = NULL;
10541 redraw_later(NOT_VALID); /* always need a redraw */
10542 }
10543
10544 if (ends_excmd(*eap->arg))
10545 end = eap->arg;
10546 else if ((STRNICMP(eap->arg, "none", 4) == 0
10547 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10548 end = eap->arg + 4;
10549 else
10550 {
10551 p = skiptowhite(eap->arg);
10552 if (!eap->skip)
10553 {
10554 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10555 if (curwin->w_match_id == 0)
10556 {
10557 EMSG2(_(e_nogroup), eap->arg);
10558 return;
10559 }
10560 }
10561 p = skipwhite(p);
10562 if (*p == NUL)
10563 {
10564 /* There must be two arguments. */
10565 EMSG2(_(e_invarg2), eap->arg);
10566 return;
10567 }
10568 end = skip_regexp(p + 1, *p, TRUE, NULL);
10569 if (!eap->skip)
10570 {
10571 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10572 {
10573 eap->errmsg = e_trailing;
10574 return;
10575 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010576 if (*end != *p)
10577 {
10578 EMSG2(_(e_invarg2), p);
10579 return;
10580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581
10582 c = *end;
10583 *end = NUL;
10584 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10585 *end = c;
10586 if (curwin->w_match.regprog == NULL)
10587 {
10588 EMSG2(_(e_invarg2), p);
10589 return;
10590 }
10591 }
10592 }
10593 eap->nextcmd = find_nextcmd(end);
10594}
10595#endif
10596
10597#ifdef FEAT_CRYPT
10598/*
10599 * ":X": Get crypt key
10600 */
10601/*ARGSUSED*/
10602 static void
10603ex_X(eap)
10604 exarg_T *eap;
10605{
10606 (void)get_crypt_key(TRUE, TRUE);
10607}
10608#endif
10609
10610#ifdef FEAT_FOLDING
10611 static void
10612ex_fold(eap)
10613 exarg_T *eap;
10614{
10615 if (foldManualAllowed(TRUE))
10616 foldCreate(eap->line1, eap->line2);
10617}
10618
10619 static void
10620ex_foldopen(eap)
10621 exarg_T *eap;
10622{
10623 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10624 eap->forceit, FALSE);
10625}
10626
10627 static void
10628ex_folddo(eap)
10629 exarg_T *eap;
10630{
10631 linenr_T lnum;
10632
10633 /* First set the marks for all lines closed/open. */
10634 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10635 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10636 ml_setmarked(lnum);
10637
10638 /* Execute the command on the marked lines. */
10639 global_exe(eap->arg);
10640 ml_clearmarked(); /* clear rest of the marks */
10641}
10642#endif