blob: 899e4c8f478f066d438b40f6ca7d81f7691f1ea7 [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
42garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
43
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));
49static void ex_comclear __ARGS((exarg_T *eap));
50static void ex_delcommand __ARGS((exarg_T *eap));
51# ifdef FEAT_CMDL_COMPL
52static char_u *get_user_command_name __ARGS((int idx));
53# endif
54
55#else
56# define ex_command ex_ni
57# define ex_comclear ex_ni
58# define ex_delcommand ex_ni
59#endif
60
61#ifdef FEAT_EVAL
62static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*getline)(int, void *, int), void *cookie));
63#else
64static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*getline)(int, void *, int), void *cookie));
65static int if_level = 0; /* depth in :if */
66#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static char_u *find_command __ARGS((exarg_T *eap, int *full));
68
69static void ex_abbreviate __ARGS((exarg_T *eap));
70static void ex_map __ARGS((exarg_T *eap));
71static void ex_unmap __ARGS((exarg_T *eap));
72static void ex_mapclear __ARGS((exarg_T *eap));
73static void ex_abclear __ARGS((exarg_T *eap));
74#ifndef FEAT_MENU
75# define ex_emenu ex_ni
76# define ex_menu ex_ni
77# define ex_menutranslate ex_ni
78#endif
79#ifdef FEAT_AUTOCMD
80static void ex_autocmd __ARGS((exarg_T *eap));
81static void ex_doautocmd __ARGS((exarg_T *eap));
82#else
83# define ex_autocmd ex_ni
84# define ex_doautocmd ex_ni
85# define ex_doautoall ex_ni
86#endif
87#ifdef FEAT_LISTCMDS
88static void ex_bunload __ARGS((exarg_T *eap));
89static void ex_buffer __ARGS((exarg_T *eap));
90static void ex_bmodified __ARGS((exarg_T *eap));
91static void ex_bnext __ARGS((exarg_T *eap));
92static void ex_bprevious __ARGS((exarg_T *eap));
93static void ex_brewind __ARGS((exarg_T *eap));
94static void ex_blast __ARGS((exarg_T *eap));
95#else
96# define ex_bunload ex_ni
97# define ex_buffer ex_ni
98# define ex_bmodified ex_ni
99# define ex_bnext ex_ni
100# define ex_bprevious ex_ni
101# define ex_brewind ex_ni
102# define ex_blast ex_ni
103# define buflist_list ex_ni
104# define ex_checktime ex_ni
105#endif
106#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
107# define ex_buffer_all ex_ni
108#endif
109static char_u *getargcmd __ARGS((char_u **));
110static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
111static int getargopt __ARGS((exarg_T *eap));
112#ifndef FEAT_QUICKFIX
113# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000114# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115# define ex_cc ex_ni
116# define ex_cnext ex_ni
117# define ex_cfile ex_ni
118# define qf_list ex_ni
119# define qf_age ex_ni
120# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000121# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#endif
123#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
124# define ex_cclose ex_ni
125# define ex_copen ex_ni
126# define ex_cwindow ex_ni
127#endif
128
129static int check_more __ARGS((int, int));
130static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
131#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000132 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133static void ex_script_ni __ARGS((exarg_T *eap));
134#endif
135static char_u *invalid_range __ARGS((exarg_T *eap));
136static void correct_range __ARGS((exarg_T *eap));
137static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
138static void ex_highlight __ARGS((exarg_T *eap));
139static void ex_colorscheme __ARGS((exarg_T *eap));
140static void ex_quit __ARGS((exarg_T *eap));
141static void ex_cquit __ARGS((exarg_T *eap));
142static void ex_quit_all __ARGS((exarg_T *eap));
143#ifdef FEAT_WINDOWS
144static void ex_close __ARGS((exarg_T *eap));
145static void ex_win_close __ARGS((exarg_T *eap, win_T *win));
146static void ex_only __ARGS((exarg_T *eap));
147static void ex_all __ARGS((exarg_T *eap));
148static void ex_resize __ARGS((exarg_T *eap));
149static void ex_stag __ARGS((exarg_T *eap));
150#else
151# define ex_close ex_ni
152# define ex_only ex_ni
153# define ex_all ex_ni
154# define ex_resize ex_ni
155# define ex_splitview ex_ni
156# define ex_stag ex_ni
157#endif
158#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
159static void ex_pclose __ARGS((exarg_T *eap));
160static void ex_ptag __ARGS((exarg_T *eap));
161static void ex_pedit __ARGS((exarg_T *eap));
162#else
163# define ex_pclose ex_ni
164# define ex_ptag ex_ni
165# define ex_pedit ex_ni
166#endif
167static void ex_hide __ARGS((exarg_T *eap));
168static void ex_stop __ARGS((exarg_T *eap));
169static void ex_exit __ARGS((exarg_T *eap));
170static void ex_print __ARGS((exarg_T *eap));
171#ifdef FEAT_BYTEOFF
172static void ex_goto __ARGS((exarg_T *eap));
173#else
174# define ex_goto ex_ni
175#endif
176static void ex_shell __ARGS((exarg_T *eap));
177static void ex_preserve __ARGS((exarg_T *eap));
178static void ex_recover __ARGS((exarg_T *eap));
179#ifndef FEAT_LISTCMDS
180# define ex_argedit ex_ni
181# define ex_argadd ex_ni
182# define ex_argdelete ex_ni
183# define ex_listdo ex_ni
184#endif
185static void ex_mode __ARGS((exarg_T *eap));
186static void ex_wrongmodifier __ARGS((exarg_T *eap));
187static void ex_find __ARGS((exarg_T *eap));
188static void ex_edit __ARGS((exarg_T *eap));
189#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
190# define ex_drop ex_ni
191#endif
192#ifndef FEAT_GUI
193# define ex_gui ex_nogui
194static void ex_nogui __ARGS((exarg_T *eap));
195#endif
196#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
197static void ex_tearoff __ARGS((exarg_T *eap));
198#else
199# define ex_tearoff ex_ni
200#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000201#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202static void ex_popup __ARGS((exarg_T *eap));
203#else
204# define ex_popup ex_ni
205#endif
206#ifndef FEAT_GUI_MSWIN
207# define ex_simalt ex_ni
208#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000209#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_KDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# define gui_mch_find_dialog ex_ni
211# define gui_mch_replace_dialog ex_ni
212#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000213#if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_KDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214# define ex_helpfind ex_ni
215#endif
216#ifndef FEAT_CSCOPE
217# define do_cscope ex_ni
218# define do_scscope ex_ni
219# define do_cstag ex_ni
220#endif
221#ifndef FEAT_SYN_HL
222# define ex_syntax ex_ni
223#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000224#ifndef FEAT_MZSCHEME
225# define ex_mzscheme ex_script_ni
226# define ex_mzfile ex_ni
227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#ifndef FEAT_PERL
229# define ex_perl ex_script_ni
230# define ex_perldo ex_ni
231#endif
232#ifndef FEAT_PYTHON
233# define ex_python ex_script_ni
234# define ex_pyfile ex_ni
235#endif
236#ifndef FEAT_TCL
237# define ex_tcl ex_script_ni
238# define ex_tcldo ex_ni
239# define ex_tclfile ex_ni
240#endif
241#ifndef FEAT_RUBY
242# define ex_ruby ex_script_ni
243# define ex_rubydo ex_ni
244# define ex_rubyfile ex_ni
245#endif
246#ifndef FEAT_SNIFF
247# define ex_sniff ex_ni
248#endif
249#ifndef FEAT_KEYMAP
250# define ex_loadkeymap ex_ni
251#endif
252static void ex_swapname __ARGS((exarg_T *eap));
253static void ex_syncbind __ARGS((exarg_T *eap));
254static void ex_read __ARGS((exarg_T *eap));
255static void ex_cd __ARGS((exarg_T *eap));
256static void ex_pwd __ARGS((exarg_T *eap));
257static void ex_equal __ARGS((exarg_T *eap));
258static void ex_sleep __ARGS((exarg_T *eap));
259static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
260static void ex_winsize __ARGS((exarg_T *eap));
261#ifdef FEAT_WINDOWS
262static void ex_wincmd __ARGS((exarg_T *eap));
263#else
264# define ex_wincmd ex_ni
265#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000266#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267static void ex_winpos __ARGS((exarg_T *eap));
268#else
269# define ex_winpos ex_ni
270#endif
271static void ex_operators __ARGS((exarg_T *eap));
272static void ex_put __ARGS((exarg_T *eap));
273static void ex_copymove __ARGS((exarg_T *eap));
274static void ex_submagic __ARGS((exarg_T *eap));
275static void ex_join __ARGS((exarg_T *eap));
276static void ex_at __ARGS((exarg_T *eap));
277static void ex_bang __ARGS((exarg_T *eap));
278static void ex_undo __ARGS((exarg_T *eap));
279static void ex_redo __ARGS((exarg_T *eap));
280static void ex_redir __ARGS((exarg_T *eap));
281static void ex_redraw __ARGS((exarg_T *eap));
282static void ex_redrawstatus __ARGS((exarg_T *eap));
283static void close_redir __ARGS((void));
284static void ex_mkrc __ARGS((exarg_T *eap));
285static void ex_mark __ARGS((exarg_T *eap));
286#ifdef FEAT_USR_CMDS
287static char_u *uc_fun_cmd __ARGS((void));
288#endif
289#ifdef FEAT_EX_EXTRA
290static void ex_normal __ARGS((exarg_T *eap));
291static void ex_startinsert __ARGS((exarg_T *eap));
292static void ex_stopinsert __ARGS((exarg_T *eap));
293#else
294# define ex_normal ex_ni
295# define ex_align ex_ni
296# define ex_retab ex_ni
297# define ex_startinsert ex_ni
298# define ex_stopinsert ex_ni
299# define ex_helptags ex_ni
300#endif
301#ifdef FEAT_FIND_ID
302static void ex_checkpath __ARGS((exarg_T *eap));
303static void ex_findpat __ARGS((exarg_T *eap));
304#else
305# define ex_findpat ex_ni
306# define ex_checkpath ex_ni
307#endif
308#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
309static void ex_psearch __ARGS((exarg_T *eap));
310#else
311# define ex_psearch ex_ni
312#endif
313static void ex_tag __ARGS((exarg_T *eap));
314static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
315#ifndef FEAT_EVAL
316# define ex_scriptnames ex_ni
317# define ex_finish ex_ni
318# define ex_echo ex_ni
319# define ex_echohl ex_ni
320# define ex_execute ex_ni
321# define ex_call ex_ni
322# define ex_if ex_ni
323# define ex_endif ex_ni
324# define ex_else ex_ni
325# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000326# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327# define ex_continue ex_ni
328# define ex_break ex_ni
329# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000330# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331# define ex_throw ex_ni
332# define ex_try ex_ni
333# define ex_catch ex_ni
334# define ex_finally ex_ni
335# define ex_endtry ex_ni
336# define ex_endfunction ex_ni
337# define ex_let ex_ni
338# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000339# define ex_lockvar ex_ni
340# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341# define ex_function ex_ni
342# define ex_delfunction ex_ni
343# define ex_return ex_ni
344#endif
345static char_u *arg_all __ARGS((void));
346#ifdef FEAT_SESSION
347static int makeopens __ARGS((FILE *fd, char_u *dirnow));
348static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
349static void ex_loadview __ARGS((exarg_T *eap));
350static char_u *get_view_file __ARGS((int c));
351#else
352# define ex_loadview ex_ni
353#endif
354#ifndef FEAT_EVAL
355# define ex_compiler ex_ni
356#endif
357#ifdef FEAT_VIMINFO
358static void ex_viminfo __ARGS((exarg_T *eap));
359#else
360# define ex_viminfo ex_ni
361#endif
362static void ex_behave __ARGS((exarg_T *eap));
363#ifdef FEAT_AUTOCMD
364static void ex_filetype __ARGS((exarg_T *eap));
365static void ex_setfiletype __ARGS((exarg_T *eap));
366#else
367# define ex_filetype ex_ni
368# define ex_setfiletype ex_ni
369#endif
370#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000371# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372# define ex_diffpatch ex_ni
373# define ex_diffgetput ex_ni
374# define ex_diffsplit ex_ni
375# define ex_diffthis ex_ni
376# define ex_diffupdate ex_ni
377#endif
378static void ex_digraphs __ARGS((exarg_T *eap));
379static void ex_set __ARGS((exarg_T *eap));
380#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
381# define ex_options ex_ni
382#endif
383#ifdef FEAT_SEARCH_EXTRA
384static void ex_nohlsearch __ARGS((exarg_T *eap));
385static void ex_match __ARGS((exarg_T *eap));
386#else
387# define ex_nohlsearch ex_ni
388# define ex_match ex_ni
389#endif
390#ifdef FEAT_CRYPT
391static void ex_X __ARGS((exarg_T *eap));
392#else
393# define ex_X ex_ni
394#endif
395#ifdef FEAT_FOLDING
396static void ex_fold __ARGS((exarg_T *eap));
397static void ex_foldopen __ARGS((exarg_T *eap));
398static void ex_folddo __ARGS((exarg_T *eap));
399#else
400# define ex_fold ex_ni
401# define ex_foldopen ex_ni
402# define ex_folddo ex_ni
403#endif
404#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
405 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
406# define ex_language ex_ni
407#endif
408#ifndef FEAT_SIGNS
409# define ex_sign ex_ni
410#endif
411#ifndef FEAT_SUN_WORKSHOP
412# define ex_wsverb ex_ni
413#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000414#ifndef FEAT_NETBEANS_INTG
415# define ex_nbkey ex_ni
416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417
418#ifndef FEAT_EVAL
419# define ex_debug ex_ni
420# define ex_breakadd ex_ni
421# define ex_debuggreedy ex_ni
422# define ex_breakdel ex_ni
423# define ex_breaklist ex_ni
424#endif
425
426#ifndef FEAT_CMDHIST
427# define ex_history ex_ni
428#endif
429#ifndef FEAT_JUMPLIST
430# define ex_jumps ex_ni
431# define ex_changes ex_ni
432#endif
433
434/*
435 * Declare cmdnames[].
436 */
437#define DO_DECLARE_EXCMD
438#include "ex_cmds.h"
439
440/*
441 * Table used to quickly search for a command, based on its first character.
442 */
443cmdidx_T cmdidxs[27] =
444{
445 CMD_append,
446 CMD_buffer,
447 CMD_change,
448 CMD_delete,
449 CMD_edit,
450 CMD_file,
451 CMD_global,
452 CMD_help,
453 CMD_insert,
454 CMD_join,
455 CMD_k,
456 CMD_list,
457 CMD_move,
458 CMD_next,
459 CMD_open,
460 CMD_print,
461 CMD_quit,
462 CMD_read,
463 CMD_substitute,
464 CMD_t,
465 CMD_undo,
466 CMD_vglobal,
467 CMD_write,
468 CMD_xit,
469 CMD_yank,
470 CMD_z,
471 CMD_bang
472};
473
474static char_u dollar_command[2] = {'$', 0};
475
476
477#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000478/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479typedef struct
480{
481 char_u *line; /* command line */
482 linenr_T lnum; /* sourcing_lnum of the line */
483} wcmd_T;
484
485/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000486 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000488 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000490struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 garray_T *lines_gap; /* growarray with line info */
493 int current_line; /* last read line from growarray */
494 int repeating; /* TRUE when looping a second time */
495 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
496 char_u *(*getline) __ARGS((int, void *, int));
497 void *cookie;
498};
499
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000500static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
501static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000503
504/* Struct to save a few things while debugging. Used in do_cmdline() only. */
505struct dbg_stuff
506{
507 int trylevel;
508 int force_abort;
509 except_T *caught_stack;
510 char_u *vv_exception;
511 char_u *vv_throwpoint;
512 int did_emsg;
513 int got_int;
514 int did_throw;
515 int need_rethrow;
516 int check_cstack;
517 except_T *current_exception;
518};
519
520static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
521static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
522
523 static void
524save_dbg_stuff(dsp)
525 struct dbg_stuff *dsp;
526{
527 dsp->trylevel = trylevel; trylevel = 0;
528 dsp->force_abort = force_abort; force_abort = FALSE;
529 dsp->caught_stack = caught_stack; caught_stack = NULL;
530 dsp->vv_exception = v_exception(NULL);
531 dsp->vv_throwpoint = v_throwpoint(NULL);
532
533 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
534 dsp->did_emsg = did_emsg; did_emsg = FALSE;
535 dsp->got_int = got_int; got_int = FALSE;
536 dsp->did_throw = did_throw; did_throw = FALSE;
537 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
538 dsp->check_cstack = check_cstack; check_cstack = FALSE;
539 dsp->current_exception = current_exception; current_exception = NULL;
540}
541
542 static void
543restore_dbg_stuff(dsp)
544 struct dbg_stuff *dsp;
545{
546 suppress_errthrow = FALSE;
547 trylevel = dsp->trylevel;
548 force_abort = dsp->force_abort;
549 caught_stack = dsp->caught_stack;
550 (void)v_exception(dsp->vv_exception);
551 (void)v_throwpoint(dsp->vv_throwpoint);
552 did_emsg = dsp->did_emsg;
553 got_int = dsp->got_int;
554 did_throw = dsp->did_throw;
555 need_rethrow = dsp->need_rethrow;
556 check_cstack = dsp->check_cstack;
557 current_exception = dsp->current_exception;
558}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#endif
560
561
562/*
563 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
564 * command is given.
565 */
566 void
567do_exmode(improved)
568 int improved; /* TRUE for "improved Ex" mode */
569{
570 int save_msg_scroll;
571 int prev_msg_row;
572 linenr_T prev_line;
573
574 save_msg_scroll = msg_scroll;
575 ++RedrawingDisabled; /* don't redisplay the window */
576 ++no_wait_return; /* don't wait for return */
577 if (improved)
578 exmode_active = EXMODE_VIM;
579 else
580 {
581 settmode(TMODE_COOK);
582 exmode_active = EXMODE_NORMAL;
583 }
584
585 State = NORMAL;
586#ifdef FEAT_GUI
587 /* Ignore scrollbar and mouse events in Ex mode */
588 ++hold_gui_events;
589#endif
590#ifdef FEAT_SNIFF
591 want_sniff_request = 0; /* No K_SNIFF wanted */
592#endif
593
594 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
595 while (exmode_active)
596 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000597#ifdef FEAT_EX_EXTRA
598 /* Check for a ":normal" command and no more characters left. */
599 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
600 {
601 exmode_active = FALSE;
602 break;
603 }
604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 msg_scroll = TRUE;
606 need_wait_return = FALSE;
607 ex_pressedreturn = FALSE;
608 ex_no_reprint = FALSE;
609 prev_msg_row = msg_row;
610 prev_line = curwin->w_cursor.lnum;
611#ifdef FEAT_SNIFF
612 ProcessSniffRequests();
613#endif
614 if (improved)
615 {
616 cmdline_row = msg_row;
617 do_cmdline(NULL, getexline, NULL, 0);
618 }
619 else
620 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
621 lines_left = Rows - 1;
622
623 if (prev_line != curwin->w_cursor.lnum && !ex_no_reprint)
624 {
625 if (ex_pressedreturn)
626 {
627 /* go up one line, to overwrite the ":<CR>" line, so the
628 * output doensn't contain empty lines. */
629 msg_row = prev_msg_row;
630 if (prev_msg_row == Rows - 1)
631 msg_row--;
632 }
633 msg_col = 0;
634 print_line_no_prefix(curwin->w_cursor.lnum, FALSE);
635 msg_clr_eos();
636 }
637 else if (ex_pressedreturn) /* must be at EOF */
638 EMSG(_("E501: At end-of-file"));
639 }
640
641#ifdef FEAT_GUI
642 --hold_gui_events;
643#endif
644 if (!improved)
645 settmode(TMODE_RAW);
646 --RedrawingDisabled;
647 --no_wait_return;
648 update_screen(CLEAR);
649 need_wait_return = FALSE;
650 msg_scroll = save_msg_scroll;
651}
652
653/*
654 * Execute a simple command line. Used for translated commands like "*".
655 */
656 int
657do_cmdline_cmd(cmd)
658 char_u *cmd;
659{
660 return do_cmdline(cmd, NULL, NULL,
661 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
662}
663
664/*
665 * do_cmdline(): execute one Ex command line
666 *
667 * 1. Execute "cmdline" when it is not NULL.
668 * If "cmdline" is NULL, or more lines are needed, getline() is used.
669 * 2. Split up in parts separated with '|'.
670 *
671 * This function can be called recursively!
672 *
673 * flags:
674 * DOCMD_VERBOSE - The command will be included in the error message.
675 * DOCMD_NOWAIT - Don't call wait_return() and friends.
676 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
677 * DOCMD_KEYTYPED - Don't reset KeyTyped.
678 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
679 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
680 *
681 * return FAIL if cmdline could not be executed, OK otherwise
682 */
683 int
684do_cmdline(cmdline, getline, cookie, flags)
685 char_u *cmdline;
686 char_u *(*getline) __ARGS((int, void *, int));
687 void *cookie; /* argument for getline() */
688 int flags;
689{
690 char_u *next_cmdline; /* next cmd to execute */
691 char_u *cmdline_copy = NULL; /* copy of cmd line */
692 int used_getline = FALSE; /* used "getline" to obtain command */
693 static int recursive = 0; /* recursive depth */
694 int msg_didout_before_start = 0;
695 int count = 0; /* line number count */
696 int did_inc = FALSE; /* incremented RedrawingDisabled */
697 int retval = OK;
698#ifdef FEAT_EVAL
699 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000700 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 int current_line = 0; /* active line in lines_ga */
702 char_u *fname = NULL; /* function or script name */
703 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
704 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000705 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 int initial_trylevel;
707 struct msglist **saved_msg_list = NULL;
708 struct msglist *private_msg_list;
709
710 /* "getline" and "cookie" passed to do_one_cmd() */
711 char_u *(*cmd_getline) __ARGS((int, void *, int));
712 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000713 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 void *real_cookie;
715#else
716# define cmd_getline getline
717# define cmd_cookie cookie
718#endif
719 static int call_depth = 0; /* recursiveness */
720
721#ifdef FEAT_EVAL
722 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
723 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000724 * This ensures that the do_errthrow() call in do_one_cmd() does not
725 * combine the messages stored by an earlier invocation of do_one_cmd()
726 * with the command name of the later one. This would happen when
727 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 saved_msg_list = msg_list;
729 msg_list = &private_msg_list;
730 private_msg_list = NULL;
731#endif
732
733 /* It's possible to create an endless loop with ":execute", catch that
734 * here. The value of 200 allows nested function calls, ":source", etc. */
735 if (call_depth == 200)
736 {
737 EMSG(_("E169: Command too recursive"));
738#ifdef FEAT_EVAL
739 /* When converting to an exception, we do not include the command name
740 * since this is not an error of the specific command. */
741 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
742 msg_list = saved_msg_list;
743#endif
744 return FAIL;
745 }
746 ++call_depth;
747
748#ifdef FEAT_EVAL
749 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000750 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 cstack.cs_trylevel = 0;
752 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000753 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
755
756 real_cookie = getline_cookie(getline, cookie);
757
758 /* Inside a function use a higher nesting level. */
759 if (getline_equal(getline, cookie, get_func_line)
760 && ex_nesting_level == func_level(real_cookie))
761 ++ex_nesting_level;
762
763 /* Get the function or script name and the address where the next breakpoint
764 * line and the debug tick for a function or script are stored. */
765 if (getline_equal(getline, cookie, get_func_line))
766 {
767 fname = func_name(real_cookie);
768 breakpoint = func_breakpoint(real_cookie);
769 dbg_tick = func_dbg_tick(real_cookie);
770 }
771 else if (getline_equal(getline, cookie, getsourceline))
772 {
773 fname = sourcing_name;
774 breakpoint = source_breakpoint(real_cookie);
775 dbg_tick = source_dbg_tick(real_cookie);
776 }
777
778 /*
779 * Initialize "force_abort" and "suppress_errthrow" at the top level.
780 */
781 if (!recursive)
782 {
783 force_abort = FALSE;
784 suppress_errthrow = FALSE;
785 }
786
787 /*
788 * If requested, store and reset the global values controlling the
789 * exception handling (used when debugging).
790 */
791 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000792 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 initial_trylevel = trylevel;
795
796 /*
797 * "did_throw" will be set to TRUE when an exception is being thrown.
798 */
799 did_throw = FALSE;
800#endif
801 /*
802 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000803 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 * If force_abort is set, we cancel everything.
805 */
806 did_emsg = FALSE;
807
808 /*
809 * KeyTyped is only set when calling vgetc(). Reset it here when not
810 * calling vgetc() (sourced command lines).
811 */
812 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
813 KeyTyped = FALSE;
814
815 /*
816 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000817 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 * - for multiple commands on one line, separated with '|'
819 * - when repeating until there are no more lines (for ":source")
820 */
821 next_cmdline = cmdline;
822 do
823 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000824 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (next_cmdline == NULL
826#ifdef FEAT_EVAL
827 && !force_abort
828 && cstack.cs_idx < 0
829 && !(getline_equal(getline, cookie, get_func_line)
830 && func_has_abort(real_cookie))
831#endif
832 )
833 did_emsg = FALSE;
834
835 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000836 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 * 2. If no line given: Get an allocated line with getline().
838 * 3. If a line is given: Make a copy, so we can mess with it.
839 */
840
841#ifdef FEAT_EVAL
842 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000843 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
845 /* Each '|' separated command is stored separately in lines_ga, to
846 * be able to jump to it. Don't use next_cmdline now. */
847 vim_free(cmdline_copy);
848 cmdline_copy = NULL;
849
850 /* Check if a function has returned or, unless it has an unclosed
851 * try conditional, aborted. */
852 if (getline_equal(getline, cookie, get_func_line)
853 && func_has_ended(real_cookie))
854 {
855 retval = FAIL;
856 break;
857 }
858
859 /* Check if a sourced file hit a ":finish" command. */
860 if (source_finished(getline, cookie))
861 {
862 retval = FAIL;
863 break;
864 }
865
866 /* If breakpoints have been added/deleted need to check for it. */
867 if (breakpoint != NULL && dbg_tick != NULL
868 && *dbg_tick != debug_tick)
869 {
870 *breakpoint = dbg_find_breakpoint(
871 getline_equal(getline, cookie, getsourceline),
872 fname, sourcing_lnum);
873 *dbg_tick = debug_tick;
874 }
875
876 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
877 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
878
879 /* Did we encounter a breakpoint? */
880 if (breakpoint != NULL && *breakpoint != 0
881 && *breakpoint <= sourcing_lnum)
882 {
883 dbg_breakpoint(fname, sourcing_lnum);
884 /* Find next breakpoint. */
885 *breakpoint = dbg_find_breakpoint(
886 getline_equal(getline, cookie, getsourceline),
887 fname, sourcing_lnum);
888 *dbg_tick = debug_tick;
889 }
890 }
891
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000892 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000894 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 * again. Pass a different "getline" function to do_one_cmd()
896 * below, so that it stores lines in or reads them from
897 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 * while/for loop. */
899 cmd_getline = get_loop_line;
900 cmd_cookie = (void *)&cmd_loop_cookie;
901 cmd_loop_cookie.lines_gap = &lines_ga;
902 cmd_loop_cookie.current_line = current_line;
903 cmd_loop_cookie.getline = getline;
904 cmd_loop_cookie.cookie = cookie;
905 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907 else
908 {
909 cmd_getline = getline;
910 cmd_cookie = cookie;
911 }
912#endif
913
914 /* 2. If no line given, get an allocated line with getline(). */
915 if (next_cmdline == NULL)
916 {
917 /*
918 * Need to set msg_didout for the first line after an ":if",
919 * otherwise the ":if" will be overwritten.
920 */
921 if (count == 1 && getline_equal(getline, cookie, getexline))
922 msg_didout = TRUE;
923 if (getline == NULL || (next_cmdline = getline(':', cookie,
924#ifdef FEAT_EVAL
925 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
926#else
927 0
928#endif
929 )) == NULL)
930 {
931 /* Don't call wait_return for aborted command line. The NULL
932 * returned for the end of a sourced file or executed function
933 * doesn't do this. */
934 if (KeyTyped && !(flags & DOCMD_REPEAT))
935 need_wait_return = FALSE;
936 retval = FAIL;
937 break;
938 }
939 used_getline = TRUE;
940
941 /*
942 * Keep the first typed line. Clear it when more lines are typed.
943 */
944 if (flags & DOCMD_KEEPLINE)
945 {
946 vim_free(repeat_cmdline);
947 if (count == 0)
948 repeat_cmdline = vim_strsave(next_cmdline);
949 else
950 repeat_cmdline = NULL;
951 }
952 }
953
954 /* 3. Make a copy of the command so we can mess with it. */
955 else if (cmdline_copy == NULL)
956 {
957 next_cmdline = vim_strsave(next_cmdline);
958 if (next_cmdline == NULL)
959 {
960 EMSG(_(e_outofmem));
961 retval = FAIL;
962 break;
963 }
964 }
965 cmdline_copy = next_cmdline;
966
967#ifdef FEAT_EVAL
968 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000969 * Save the current line when inside a ":while" or ":for", and when
970 * the command looks like a ":while" or ":for", because we may need it
971 * later. When there is a '|' and another command, it is stored
972 * separately, because we need to be able to jump back to it from an
973 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 if (current_line == lines_ga.ga_len
976 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000978 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980 retval = FAIL;
981 break;
982 }
983 }
984 did_endif = FALSE;
985#endif
986
987 if (count++ == 0)
988 {
989 /*
990 * All output from the commands is put below each other, without
991 * waiting for a return. Don't do this when executing commands
992 * from a script or when being called recursive (e.g. for ":e
993 * +command file").
994 */
995 if (!(flags & DOCMD_NOWAIT) && !recursive)
996 {
997 msg_didout_before_start = msg_didout;
998 msg_didany = FALSE; /* no output yet */
999 msg_start();
1000 msg_scroll = TRUE; /* put messages below each other */
1001 ++no_wait_return; /* dont wait for return until finished */
1002 ++RedrawingDisabled;
1003 did_inc = TRUE;
1004 }
1005 }
1006
1007 if (p_verbose >= 15 && sourcing_name != NULL)
1008 {
1009 int c = -1;
1010
1011 ++no_wait_return;
1012 msg_scroll = TRUE; /* always scroll up, don't overwrite */
1013 /* Truncate long lines, smsg() can't handle that. */
1014 if (STRLEN(cmdline_copy) > 200)
1015 {
1016 c = cmdline_copy[200];
1017 cmdline_copy[200] = NUL;
1018 }
1019 smsg((char_u *)_("line %ld: %s"),
1020 (long)sourcing_lnum, cmdline_copy);
1021 msg_puts((char_u *)"\n"); /* don't overwrite this either */
1022 if (c >= 0)
1023 cmdline_copy[200] = c;
1024 cmdline_row = msg_row;
1025 --no_wait_return;
1026 }
1027
1028 /*
1029 * 2. Execute one '|' separated command.
1030 * do_one_cmd() will return NULL if there is no trailing '|'.
1031 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1032 */
1033 ++recursive;
1034 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1035#ifdef FEAT_EVAL
1036 &cstack,
1037#endif
1038 cmd_getline, cmd_cookie);
1039 --recursive;
1040
1041#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cmd_cookie == (void *)&cmd_loop_cookie)
1043 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001045 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046#endif
1047
1048 if (next_cmdline == NULL)
1049 {
1050 vim_free(cmdline_copy);
1051 cmdline_copy = NULL;
1052#ifdef FEAT_CMDHIST
1053 /*
1054 * If the command was typed, remember it for the ':' register.
1055 * Do this AFTER executing the command to make :@: work.
1056 */
1057 if (getline_equal(getline, cookie, getexline)
1058 && new_last_cmdline != NULL)
1059 {
1060 vim_free(last_cmdline);
1061 last_cmdline = new_last_cmdline;
1062 new_last_cmdline = NULL;
1063 }
1064#endif
1065 }
1066 else
1067 {
1068 /* need to copy the command after the '|' to cmdline_copy, for the
1069 * next do_one_cmd() */
1070 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1071 next_cmdline = cmdline_copy;
1072 }
1073
1074
1075#ifdef FEAT_EVAL
1076 /* reset did_emsg for a function that is not aborted by an error */
1077 if (did_emsg && !force_abort
1078 && getline_equal(getline, cookie, get_func_line)
1079 && !func_has_abort(real_cookie))
1080 did_emsg = FALSE;
1081
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 ++current_line;
1085
1086 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 * An ":endwhile", ":endfor" and ":continue" is handled here.
1088 * If we were executing commands, jump back to the ":while" or
1089 * ":for".
1090 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 /* Jump back to the matching ":while" or ":for". Be careful
1097 * not to use a cs_line[] from an entry that isn't a ":while"
1098 * or ":for": It would make "current_line" invalid and can
1099 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (!did_emsg && !got_int && !did_throw
1101 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 && (cstack.cs_flags[cstack.cs_idx]
1103 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 && cstack.cs_line[cstack.cs_idx] >= 0
1105 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1106 {
1107 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 /* remember we jumped there */
1109 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 line_breakcheck(); /* check if CTRL-C typed */
1111
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 /* Check for the next breakpoint at or after the ":while"
1113 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (breakpoint != NULL)
1115 {
1116 *breakpoint = dbg_find_breakpoint(
1117 getline_equal(getline, cookie, getsourceline),
1118 fname,
1119 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1120 *dbg_tick = debug_tick;
1121 }
1122 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001125 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001127 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1128 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 }
1131
1132 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1139 }
1140 }
1141
1142 /*
1143 * When not inside any ":while" loop, clear remembered lines.
1144 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001145 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {
1147 if (lines_ga.ga_len > 0)
1148 {
1149 sourcing_lnum =
1150 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1151 free_cmdlines(&lines_ga);
1152 }
1153 current_line = 0;
1154 }
1155
1156 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1158 * being restored at the ":endtry". Reset them here and set the
1159 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1160 * This includes the case where a missing ":endif", ":endwhile" or
1161 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 cstack.cs_lflags &= ~CSL_HAD_FINA;
1166 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1167 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 did_throw ? (void *)current_exception : NULL);
1169 did_emsg = got_int = did_throw = FALSE;
1170 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1171 }
1172
1173 /* Update global "trylevel" for recursive calls to do_cmdline() from
1174 * within this loop. */
1175 trylevel = initial_trylevel + cstack.cs_trylevel;
1176
1177 /*
1178 * If the outermost try conditional (accross function calls and sourced
1179 * files) is aborted because of an error, an interrupt, or an uncaught
1180 * exception, cancel everything. If it is left normally, reset
1181 * force_abort to get the non-EH compatible abortion behavior for
1182 * the rest of the script.
1183 */
1184 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1185 force_abort = FALSE;
1186
1187 /* Convert an interrupt to an exception if appropriate. */
1188 (void)do_intthrow(&cstack);
1189#endif /* FEAT_EVAL */
1190
1191 }
1192 /*
1193 * Continue executing command lines when:
1194 * - no CTRL-C typed, no aborting error, no exception thrown or try
1195 * conditionals need to be checked for executing finally clauses or
1196 * catching an interrupt exception
1197 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 * looping for ":source" command or function call.
1200 */
1201 while (!((got_int
1202#ifdef FEAT_EVAL
1203 || (did_emsg && force_abort) || did_throw
1204#endif
1205 )
1206#ifdef FEAT_EVAL
1207 && cstack.cs_trylevel == 0
1208#endif
1209 )
1210 && !(did_emsg && used_getline
1211 && (getline_equal(getline, cookie, getexmodeline)
1212 || getline_equal(getline, cookie, getexline)))
1213 && (next_cmdline != NULL
1214#ifdef FEAT_EVAL
1215 || cstack.cs_idx >= 0
1216#endif
1217 || (flags & DOCMD_REPEAT)));
1218
1219 vim_free(cmdline_copy);
1220#ifdef FEAT_EVAL
1221 free_cmdlines(&lines_ga);
1222 ga_clear(&lines_ga);
1223
1224 if (cstack.cs_idx >= 0)
1225 {
1226 /*
1227 * If a sourced file or executed function ran to its end, report the
1228 * unclosed conditional.
1229 */
1230 if (!got_int && !did_throw
1231 && ((getline_equal(getline, cookie, getsourceline)
1232 && !source_finished(getline, cookie))
1233 || (getline_equal(getline, cookie, get_func_line)
1234 && !func_has_ended(real_cookie))))
1235 {
1236 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1237 EMSG(_(e_endtry));
1238 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1239 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001240 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1241 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 else
1243 EMSG(_(e_endif));
1244 }
1245
1246 /*
1247 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1248 * ":endtry" in a sourced file or executed function. If the try
1249 * conditional is in its finally clause, ignore anything pending.
1250 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001251 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 */
1253 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001254 {
1255 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1256
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001257 if (idx >= 0)
1258 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001259 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1260 &cstack.cs_looplevel);
1261 }
1262 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 trylevel = initial_trylevel;
1264 }
1265
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001266 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1267 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * exception, do this now after rewinding the cstack. */
1269 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1270 ? (char_u *)"endfunction" : (char_u *)NULL);
1271
1272 if (trylevel == 0)
1273 {
1274 /*
1275 * When an exception is being thrown out of the outermost try
1276 * conditional, discard the uncaught exception, disable the conversion
1277 * of interrupts or errors to exceptions, and ensure that no more
1278 * commands are executed.
1279 */
1280 if (did_throw)
1281 {
1282 void *p = NULL;
1283 char_u *saved_sourcing_name;
1284 int saved_sourcing_lnum;
1285 struct msglist *messages = NULL, *next;
1286
1287 /*
1288 * If the uncaught exception is a user exception, report it as an
1289 * error. If it is an error exception, display the saved error
1290 * message now. For an interrupt exception, do nothing; the
1291 * interrupt message is given elsewhere.
1292 */
1293 switch (current_exception->type)
1294 {
1295 case ET_USER:
1296 sprintf((char *)IObuff, _("E605: Exception not caught: %s"),
1297 current_exception->value);
1298 p = vim_strsave(IObuff);
1299 break;
1300 case ET_ERROR:
1301 messages = current_exception->messages;
1302 current_exception->messages = NULL;
1303 break;
1304 case ET_INTERRUPT:
1305 break;
1306 default:
1307 p = vim_strsave((char_u *)_(e_internal));
1308 }
1309
1310 saved_sourcing_name = sourcing_name;
1311 saved_sourcing_lnum = sourcing_lnum;
1312 sourcing_name = current_exception->throw_name;
1313 sourcing_lnum = current_exception->throw_lnum;
1314 current_exception->throw_name = NULL;
1315
1316 discard_current_exception(); /* uses IObuff if 'verbose' */
1317 suppress_errthrow = TRUE;
1318 force_abort = TRUE;
1319
1320 if (messages != NULL)
1321 {
1322 do
1323 {
1324 next = messages->next;
1325 emsg(messages->msg);
1326 vim_free(messages->msg);
1327 vim_free(messages);
1328 messages = next;
1329 }
1330 while (messages != NULL);
1331 }
1332 else if (p != NULL)
1333 {
1334 emsg(p);
1335 vim_free(p);
1336 }
1337 vim_free(sourcing_name);
1338 sourcing_name = saved_sourcing_name;
1339 sourcing_lnum = saved_sourcing_lnum;
1340 }
1341
1342 /*
1343 * On an interrupt or an aborting error not converted to an exception,
1344 * disable the conversion of errors to exceptions. (Interrupts are not
1345 * converted any more, here.) This enables also the interrupt message
1346 * when force_abort is set and did_emsg unset in case of an interrupt
1347 * from a finally clause after an error.
1348 */
1349 else if (got_int || (did_emsg && force_abort))
1350 suppress_errthrow = TRUE;
1351 }
1352
1353 /*
1354 * The current cstack will be freed when do_cmdline() returns. An uncaught
1355 * exception will have to be rethrown in the previous cstack. If a function
1356 * has just returned or a script file was just finished and the previous
1357 * cstack belongs to the same function or, respectively, script file, it
1358 * will have to be checked for finally clauses to be executed due to the
1359 * ":return" or ":finish". This is done in do_one_cmd().
1360 */
1361 if (did_throw)
1362 need_rethrow = TRUE;
1363 if ((getline_equal(getline, cookie, getsourceline)
1364 && ex_nesting_level > source_level(real_cookie))
1365 || (getline_equal(getline, cookie, get_func_line)
1366 && ex_nesting_level > func_level(real_cookie) + 1))
1367 {
1368 if (!did_throw)
1369 check_cstack = TRUE;
1370 }
1371 else
1372 {
1373 /* When leaving a function, reduce nesting level. */
1374 if (getline_equal(getline, cookie, get_func_line))
1375 --ex_nesting_level;
1376 /*
1377 * Go to debug mode when returning from a function in which we are
1378 * single-stepping.
1379 */
1380 if ((getline_equal(getline, cookie, getsourceline)
1381 || getline_equal(getline, cookie, get_func_line))
1382 && ex_nesting_level + 1 <= debug_break_level)
1383 do_debug(getline_equal(getline, cookie, getsourceline)
1384 ? (char_u *)_("End of sourced file")
1385 : (char_u *)_("End of function"));
1386 }
1387
1388 /*
1389 * Restore the exception environment (done after returning from the
1390 * debugger).
1391 */
1392 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001393 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 msg_list = saved_msg_list;
1396#endif /* FEAT_EVAL */
1397
1398 /*
1399 * If there was too much output to fit on the command line, ask the user to
1400 * hit return before redrawing the screen. With the ":global" command we do
1401 * this only once after the command is finished.
1402 */
1403 if (did_inc)
1404 {
1405 --RedrawingDisabled;
1406 --no_wait_return;
1407 msg_scroll = FALSE;
1408
1409 /*
1410 * When just finished an ":if"-":else" which was typed, no need to
1411 * wait for hit-return. Also for an error situation.
1412 */
1413 if (retval == FAIL
1414#ifdef FEAT_EVAL
1415 || (did_endif && KeyTyped && !did_emsg)
1416#endif
1417 )
1418 {
1419 need_wait_return = FALSE;
1420 msg_didany = FALSE; /* don't wait when restarting edit */
1421 }
1422 else if (need_wait_return)
1423 {
1424 /*
1425 * The msg_start() above clears msg_didout. The wait_return we do
1426 * here should not overwrite the command that may be shown before
1427 * doing that.
1428 */
1429 msg_didout |= msg_didout_before_start;
1430 wait_return(FALSE);
1431 }
1432 }
1433
1434#ifndef FEAT_EVAL
1435 /*
1436 * Reset if_level, in case a sourced script file contains more ":if" than
1437 * ":endif" (could be ":if x | foo | endif").
1438 */
1439 if_level = 0;
1440#endif
1441
1442 --call_depth;
1443 return retval;
1444}
1445
1446#ifdef FEAT_EVAL
1447/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001448 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
1450 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001451get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 int c;
1453 void *cookie;
1454 int indent;
1455{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001456 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 wcmd_T *wp;
1458 char_u *line;
1459
1460 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1461 {
1462 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001463 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001465 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (cp->getline == NULL)
1467 line = getcmdline(c, 0L, indent);
1468 else
1469 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001470 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 ++cp->current_line;
1472
1473 return line;
1474 }
1475
1476 KeyTyped = FALSE;
1477 ++cp->current_line;
1478 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1479 sourcing_lnum = wp->lnum;
1480 return vim_strsave(wp->line);
1481}
1482
1483/*
1484 * Store a line in "gap" so that a ":while" loop can execute it again.
1485 */
1486 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001487store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 garray_T *gap;
1489 char_u *line;
1490{
1491 if (ga_grow(gap, 1) == FAIL)
1492 return FAIL;
1493 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1494 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1495 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 return OK;
1497}
1498
1499/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 static void
1503free_cmdlines(gap)
1504 garray_T *gap;
1505{
1506 while (gap->ga_len > 0)
1507 {
1508 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1509 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511}
1512#endif
1513
1514/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001515 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 * "func". * Otherwise return TRUE when "getline" equals "func".
1517 */
1518/*ARGSUSED*/
1519 int
1520getline_equal(getline, cookie, func)
1521 char_u *(*getline) __ARGS((int, void *, int));
1522 void *cookie; /* argument for getline() */
1523 char_u *(*func) __ARGS((int, void *, int));
1524{
1525#ifdef FEAT_EVAL
1526 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001529 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 * function that's orignally used to obtain the lines. This may be nested
1531 * several levels. */
1532 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533 cp = (struct loop_cookie *)cookie;
1534 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 {
1536 gp = cp->getline;
1537 cp = cp->cookie;
1538 }
1539 return gp == func;
1540#else
1541 return getline == func;
1542#endif
1543}
1544
1545#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1546/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 * getline function. Otherwise return "cookie".
1549 */
1550/*ARGSUSED*/
1551 void *
1552getline_cookie(getline, cookie)
1553 char_u *(*getline) __ARGS((int, void *, int));
1554 void *cookie; /* argument for getline() */
1555{
1556# ifdef FEAT_EVAL
1557 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001558 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 * cookie that's orignally used to obtain the lines. This may be nested
1562 * several levels. */
1563 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001564 cp = (struct loop_cookie *)cookie;
1565 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
1567 gp = cp->getline;
1568 cp = cp->cookie;
1569 }
1570 return cp;
1571# else
1572 return cookie;
1573# endif
1574}
1575#endif
1576
1577/*
1578 * Execute one Ex command.
1579 *
1580 * If 'sourcing' is TRUE, the command will be included in the error message.
1581 *
1582 * 1. skip comment lines and leading space
1583 * 2. handle command modifiers
1584 * 3. parse range
1585 * 4. parse command
1586 * 5. parse arguments
1587 * 6. switch on command name
1588 *
1589 * Note: "getline" can be NULL.
1590 *
1591 * This function may be called recursively!
1592 */
1593#if (_MSC_VER == 1200)
1594/*
Bram Moolenaared203462004-06-16 11:19:22 +00001595 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001597 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598#endif
1599 static char_u *
1600do_one_cmd(cmdlinep, sourcing,
1601#ifdef FEAT_EVAL
1602 cstack,
1603#endif
1604 getline, cookie)
1605 char_u **cmdlinep;
1606 int sourcing;
1607#ifdef FEAT_EVAL
1608 struct condstack *cstack;
1609#endif
1610 char_u *(*getline) __ARGS((int, void *, int));
1611 void *cookie; /* argument for getline() */
1612{
1613 char_u *p;
1614 linenr_T lnum;
1615 long n;
1616 char_u *errormsg = NULL; /* error message */
1617 exarg_T ea; /* Ex command arguments */
1618 long verbose_save = -1;
1619 int save_msg_scroll = 0;
1620 int did_silent = 0;
1621 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001622#ifdef HAVE_SANDBOX
1623 int did_sandbox = FALSE;
1624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 cmdmod_T save_cmdmod;
1626 int ni; /* set when Not Implemented */
1627
1628 vim_memset(&ea, 0, sizeof(ea));
1629 ea.line1 = 1;
1630 ea.line2 = 1;
1631#ifdef FEAT_EVAL
1632 ++ex_nesting_level;
1633#endif
1634
1635 /* when not editing the last file :q has to be typed twice */
1636 if (quitmore
1637#ifdef FEAT_EVAL
1638 /* avoid that a function call in 'statusline' does this */
1639 && !getline_equal(getline, cookie, get_func_line)
1640#endif
1641 )
1642 --quitmore;
1643
1644 /*
1645 * Reset browse, confirm, etc.. They are restored when returning, for
1646 * recursive calls.
1647 */
1648 save_cmdmod = cmdmod;
1649 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1650
1651 /*
1652 * Repeat until no more command modifiers are found.
1653 */
1654 ea.cmd = *cmdlinep;
1655 for (;;)
1656 {
1657/*
1658 * 1. skip comment lines and leading white space and colons
1659 */
1660 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1661 ++ea.cmd;
1662
1663 /* in ex mode, an empty line works like :+ */
1664 if (*ea.cmd == NUL && exmode_active
1665 && (getline_equal(getline, cookie, getexmodeline)
1666 || getline_equal(getline, cookie, getexline)))
1667 {
1668 ea.cmd = (char_u *)"+";
1669 ex_pressedreturn = TRUE;
1670 }
1671
1672 /* ignore comment and empty lines */
1673 if (*ea.cmd == '"' || *ea.cmd == NUL)
1674 goto doend;
1675
1676/*
1677 * 2. handle command modifiers.
1678 */
1679 p = ea.cmd;
1680 if (VIM_ISDIGIT(*ea.cmd))
1681 p = skipwhite(skipdigits(ea.cmd));
1682 switch (*p)
1683 {
1684 /* When adding an entry, also modify cmd_exists(). */
1685 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1686 break;
1687#ifdef FEAT_WINDOWS
1688 cmdmod.split |= WSP_ABOVE;
1689#endif
1690 continue;
1691
1692 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1693 {
1694#ifdef FEAT_WINDOWS
1695 cmdmod.split |= WSP_BELOW;
1696#endif
1697 continue;
1698 }
1699 if (checkforcmd(&ea.cmd, "browse", 3))
1700 {
1701#ifdef FEAT_BROWSE
1702 cmdmod.browse = TRUE;
1703#endif
1704 continue;
1705 }
1706 if (!checkforcmd(&ea.cmd, "botright", 2))
1707 break;
1708#ifdef FEAT_WINDOWS
1709 cmdmod.split |= WSP_BOT;
1710#endif
1711 continue;
1712
1713 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1714 break;
1715#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1716 cmdmod.confirm = TRUE;
1717#endif
1718 continue;
1719
1720 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1721 {
1722 cmdmod.keepmarks = TRUE;
1723 continue;
1724 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001725 if (checkforcmd(&ea.cmd, "keepalt", 5))
1726 {
1727 cmdmod.keepalt = TRUE;
1728 continue;
1729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1731 break;
1732 cmdmod.keepjumps = TRUE;
1733 continue;
1734
1735 /* ":hide" and ":hide | cmd" are not modifiers */
1736 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1737 || *p == NUL || ends_excmd(*p))
1738 break;
1739 ea.cmd = p;
1740 cmdmod.hide = TRUE;
1741 continue;
1742
1743 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1744 {
1745 cmdmod.lockmarks = TRUE;
1746 continue;
1747 }
1748
1749 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1750 break;
1751#ifdef FEAT_WINDOWS
1752 cmdmod.split |= WSP_ABOVE;
1753#endif
1754 continue;
1755
1756 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1757 break;
1758#ifdef FEAT_WINDOWS
1759 cmdmod.split |= WSP_BELOW;
1760#endif
1761 continue;
1762
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001763 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1764 {
1765#ifdef HAVE_SANDBOX
1766 if (!did_sandbox)
1767 ++sandbox;
1768 did_sandbox = TRUE;
1769#endif
1770 continue;
1771 }
1772 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 break;
1774 ++did_silent;
1775 ++msg_silent;
1776 save_msg_scroll = msg_scroll;
1777 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1778 {
1779 /* ":silent!", but not "silent !cmd" */
1780 ea.cmd = skipwhite(ea.cmd + 1);
1781 ++emsg_silent;
1782 ++did_esilent;
1783 }
1784 continue;
1785
1786 case 't': if (!checkforcmd(&ea.cmd, "topleft", 2))
1787 break;
1788#ifdef FEAT_WINDOWS
1789 cmdmod.split |= WSP_TOP;
1790#endif
1791 continue;
1792
1793 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1794 {
1795#ifdef FEAT_VERTSPLIT
1796 cmdmod.split |= WSP_VERT;
1797#endif
1798 continue;
1799 }
1800 if (!checkforcmd(&p, "verbose", 4))
1801 break;
1802 if (verbose_save < 0)
1803 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001804 if (vim_isdigit(*ea.cmd))
1805 p_verbose = atoi((char *)ea.cmd);
1806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 p_verbose = 1;
1808 ea.cmd = p;
1809 continue;
1810 }
1811 break;
1812 }
1813
1814#ifdef FEAT_EVAL
1815 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1816 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1817#else
1818 ea.skip = (if_level > 0);
1819#endif
1820
1821#ifdef FEAT_EVAL
1822 /* May go to debug mode. If this happens and the ">quit" debug command is
1823 * used, throw an interrupt exception and skip the next command. */
1824 dbg_check_breakpoint(&ea);
1825 if (!ea.skip && got_int)
1826 {
1827 ea.skip = TRUE;
1828 (void)do_intthrow(cstack);
1829 }
1830#endif
1831
1832/*
1833 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1834 *
1835 * where 'addr' is:
1836 *
1837 * % (entire file)
1838 * $ [+-NUM]
1839 * 'x [+-NUM] (where x denotes a currently defined mark)
1840 * . [+-NUM]
1841 * [+-NUM]..
1842 * NUM
1843 *
1844 * The ea.cmd pointer is updated to point to the first character following the
1845 * range spec. If an initial address is found, but no second, the upper bound
1846 * is equal to the lower.
1847 */
1848
1849 /* repeat for all ',' or ';' separated addresses */
1850 for (;;)
1851 {
1852 ea.line1 = ea.line2;
1853 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1854 ea.cmd = skipwhite(ea.cmd);
1855 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1856 if (ea.cmd == NULL) /* error detected */
1857 goto doend;
1858 if (lnum == MAXLNUM)
1859 {
1860 if (*ea.cmd == '%') /* '%' - all lines */
1861 {
1862 ++ea.cmd;
1863 ea.line1 = 1;
1864 ea.line2 = curbuf->b_ml.ml_line_count;
1865 ++ea.addr_count;
1866 }
1867 /* '*' - visual area */
1868 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1869 {
1870 pos_T *fp;
1871
1872 ++ea.cmd;
1873 if (!ea.skip)
1874 {
1875 fp = getmark('<', FALSE);
1876 if (check_mark(fp) == FAIL)
1877 goto doend;
1878 ea.line1 = fp->lnum;
1879 fp = getmark('>', FALSE);
1880 if (check_mark(fp) == FAIL)
1881 goto doend;
1882 ea.line2 = fp->lnum;
1883 ++ea.addr_count;
1884 }
1885 }
1886 }
1887 else
1888 ea.line2 = lnum;
1889 ea.addr_count++;
1890
1891 if (*ea.cmd == ';')
1892 {
1893 if (!ea.skip)
1894 curwin->w_cursor.lnum = ea.line2;
1895 }
1896 else if (*ea.cmd != ',')
1897 break;
1898 ++ea.cmd;
1899 }
1900
1901 /* One address given: set start and end lines */
1902 if (ea.addr_count == 1)
1903 {
1904 ea.line1 = ea.line2;
1905 /* ... but only implicit: really no address given */
1906 if (lnum == MAXLNUM)
1907 ea.addr_count = 0;
1908 }
1909
1910 /* Don't leave the cursor on an illegal line (caused by ';') */
1911 check_cursor_lnum();
1912
1913/*
1914 * 4. parse command
1915 */
1916
1917 /*
1918 * Skip ':' and any white space
1919 */
1920 ea.cmd = skipwhite(ea.cmd);
1921 while (*ea.cmd == ':')
1922 ea.cmd = skipwhite(ea.cmd + 1);
1923
1924 /*
1925 * If we got a line, but no command, then go to the line.
1926 * If we find a '|' or '\n' we set ea.nextcmd.
1927 */
1928 if (*ea.cmd == NUL || *ea.cmd == '"' ||
1929 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
1930 {
1931 /*
1932 * strange vi behaviour:
1933 * ":3" jumps to line 3
1934 * ":3|..." prints line 3
1935 * ":|" prints current line
1936 */
1937 if (ea.skip) /* skip this if inside :if */
1938 goto doend;
1939 if (*ea.cmd == '|')
1940 {
1941 ea.cmdidx = CMD_print;
1942 ea.argt = RANGE+COUNT+TRLBAR;
1943 if ((errormsg = invalid_range(&ea)) == NULL)
1944 {
1945 correct_range(&ea);
1946 ex_print(&ea);
1947 }
1948 }
1949 else if (ea.addr_count != 0)
1950 {
1951 if (ea.line2 < 0)
1952 errormsg = invalid_range(&ea);
1953 else
1954 {
1955 if (ea.line2 == 0)
1956 curwin->w_cursor.lnum = 1;
1957 else if (ea.line2 > curbuf->b_ml.ml_line_count)
1958 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1959 else
1960 curwin->w_cursor.lnum = ea.line2;
1961 beginline(BL_SOL | BL_FIX);
1962 }
1963 }
1964 goto doend;
1965 }
1966
1967 /* Find the command and let "p" point to after it. */
1968 p = find_command(&ea, NULL);
1969
1970#ifdef FEAT_USR_CMDS
1971 if (p == NULL)
1972 {
1973 if (!ea.skip)
1974 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
1975 goto doend;
1976 }
1977 /* Check for wrong commands. */
1978 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
1979 {
1980 errormsg = uc_fun_cmd();
1981 goto doend;
1982 }
1983#endif
1984 if (ea.cmdidx == CMD_SIZE)
1985 {
1986 if (!ea.skip)
1987 {
1988 STRCPY(IObuff, _("E492: Not an editor command"));
1989 if (!sourcing)
1990 {
1991 STRCAT(IObuff, ": ");
1992 STRNCAT(IObuff, *cmdlinep, 40);
1993 }
1994 errormsg = IObuff;
1995 }
1996 goto doend;
1997 }
1998
1999 ni = (
2000#ifdef FEAT_USR_CMDS
2001 !USER_CMDIDX(ea.cmdidx) &&
2002#endif
2003 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2004
2005#ifndef FEAT_EVAL
2006 /*
2007 * When the expression evaluation is disabled, recognize the ":if" and
2008 * ":endif" commands and ignore everything in between it.
2009 */
2010 if (ea.cmdidx == CMD_if)
2011 ++if_level;
2012 if (if_level)
2013 {
2014 if (ea.cmdidx == CMD_endif)
2015 --if_level;
2016 goto doend;
2017 }
2018
2019#endif
2020
2021 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2022 {
2023 ++p;
2024 ea.forceit = TRUE;
2025 }
2026 else
2027 ea.forceit = FALSE;
2028
2029/*
2030 * 5. parse arguments
2031 */
2032#ifdef FEAT_USR_CMDS
2033 if (!USER_CMDIDX(ea.cmdidx))
2034#endif
2035 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2036
2037 if (!ea.skip)
2038 {
2039#ifdef HAVE_SANDBOX
2040 if (sandbox != 0 && !(ea.argt & SBOXOK))
2041 {
2042 /* Command not allowed in sandbox. */
2043 errormsg = (char_u *)_(e_sandbox);
2044 goto doend;
2045 }
2046#endif
2047 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2048 {
2049 /* Command not allowed in non-'modifiable' buffer */
2050 errormsg = (char_u *)_(e_modifiable);
2051 goto doend;
2052 }
2053#ifdef FEAT_CMDWIN
2054 if (cmdwin_type != 0 && !(ea.argt & CMDWIN)
2055# ifdef FEAT_USR_CMDS
2056 && !USER_CMDIDX(ea.cmdidx)
2057# endif
2058 )
2059 {
2060 /* Command not allowed in cmdline window. */
2061 errormsg = (char_u *)_(e_cmdwin);
2062 goto doend;
2063 }
2064#endif
2065
2066 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2067 {
2068 /* no range allowed */
2069 errormsg = (char_u *)_(e_norange);
2070 goto doend;
2071 }
2072 }
2073
2074 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2075 {
2076 errormsg = (char_u *)_(e_nobang);
2077 goto doend;
2078 }
2079
2080 /*
2081 * Don't complain about the range if it is not used
2082 * (could happen if line_count is accidentally set to 0).
2083 */
2084 if (!ea.skip && !ni)
2085 {
2086 /*
2087 * If the range is backwards, ask for confirmation and, if given, swap
2088 * ea.line1 & ea.line2 so it's forwards again.
2089 * When global command is busy, don't ask, will fail below.
2090 */
2091 if (!global_busy && ea.line1 > ea.line2)
2092 {
2093 if (sourcing)
2094 {
2095 errormsg = (char_u *)_("E493: Backwards range given");
2096 goto doend;
2097 }
2098 else
2099 {
2100 int msg_silent_save = msg_silent;
2101
2102 msg_silent = 0;
2103 if (ask_yesno((char_u *)
2104 _("Backwards range given, OK to swap"), FALSE) != 'y')
2105 goto doend;
2106 msg_silent = msg_silent_save;
2107 }
2108 lnum = ea.line1;
2109 ea.line1 = ea.line2;
2110 ea.line2 = lnum;
2111 }
2112 if ((errormsg = invalid_range(&ea)) != NULL)
2113 goto doend;
2114 }
2115
2116 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2117 ea.line2 = 1;
2118
2119 correct_range(&ea);
2120
2121#ifdef FEAT_FOLDING
2122 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2123 {
2124 /* Put the first line at the start of a closed fold, put the last line
2125 * at the end of a closed fold. */
2126 (void)hasFolding(ea.line1, &ea.line1, NULL);
2127 (void)hasFolding(ea.line2, NULL, &ea.line2);
2128 }
2129#endif
2130
2131#ifdef FEAT_QUICKFIX
2132 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002133 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 * option here, so things like % get expanded.
Bram Moolenaar86b68352004-12-27 21:59:20 +00002135 * Don't do it when ":vimgrep" is used for ":grep".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00002137 if ((ea.cmdidx == CMD_make
2138 || ea.cmdidx == CMD_grep || ea.cmdidx == CMD_grepadd)
Bram Moolenaar81695252004-12-29 20:58:21 +00002139 && !grep_internal(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
2141 char_u *new_cmdline;
2142 char_u *program;
2143 char_u *pos;
2144 char_u *ptr;
2145 int len;
2146 int i;
2147
2148 if (ea.cmdidx == CMD_grep || ea.cmdidx == CMD_grepadd)
2149 {
2150 if (*curbuf->b_p_gp == NUL)
2151 program = p_gp;
2152 else
2153 program = curbuf->b_p_gp;
2154 }
2155 else
2156 {
2157 if (*curbuf->b_p_mp == NUL)
2158 program = p_mp;
2159 else
2160 program = curbuf->b_p_mp;
2161 }
2162
2163 p = skipwhite(p);
2164
2165 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
2166 { /* replace $* by given arguments */
2167 i = 1;
2168 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
2169 ++i;
2170 len = (int)STRLEN(p);
2171 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
2172 if (new_cmdline == NULL)
2173 goto doend; /* out of memory */
2174 ptr = new_cmdline;
2175 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
2176 {
2177 i = (int)(pos - program);
2178 STRNCPY(ptr, program, i);
2179 STRCPY(ptr += i, p);
2180 ptr += len;
2181 program = pos + 2;
2182 }
2183 STRCPY(ptr, program);
2184 }
2185 else
2186 {
2187 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
2188 if (new_cmdline == NULL)
2189 goto doend; /* out of memory */
2190 STRCPY(new_cmdline, program);
2191 STRCAT(new_cmdline, " ");
2192 STRCAT(new_cmdline, p);
2193 }
2194 msg_make(p);
2195 /* 'ea.cmd' is not set here, because it is not used at CMD_make */
2196 vim_free(*cmdlinep);
2197 *cmdlinep = new_cmdline;
2198 p = new_cmdline;
2199 }
2200#endif
2201
2202 /*
2203 * Skip to start of argument.
2204 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2205 */
2206 if (ea.cmdidx == CMD_bang)
2207 ea.arg = p;
2208 else
2209 ea.arg = skipwhite(p);
2210
2211 /*
2212 * Check for "++opt=val" argument.
2213 * Must be first, allow ":w ++enc=utf8 !cmd"
2214 */
2215 if (ea.argt & ARGOPT)
2216 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2217 if (getargopt(&ea) == FAIL && !ni)
2218 {
2219 errormsg = (char_u *)_(e_invarg);
2220 goto doend;
2221 }
2222
2223 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2224 {
2225 if (*ea.arg == '>') /* append */
2226 {
2227 if (*++ea.arg != '>') /* typed wrong */
2228 {
2229 errormsg = (char_u *)_("E494: Use w or w>>");
2230 goto doend;
2231 }
2232 ea.arg = skipwhite(ea.arg + 1);
2233 ea.append = TRUE;
2234 }
2235 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2236 {
2237 ++ea.arg;
2238 ea.usefilter = TRUE;
2239 }
2240 }
2241
2242 if (ea.cmdidx == CMD_read)
2243 {
2244 if (ea.forceit)
2245 {
2246 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2247 ea.forceit = FALSE;
2248 }
2249 else if (*ea.arg == '!') /* :r !filter */
2250 {
2251 ++ea.arg;
2252 ea.usefilter = TRUE;
2253 }
2254 }
2255
2256 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2257 {
2258 ea.amount = 1;
2259 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2260 {
2261 ++ea.arg;
2262 ++ea.amount;
2263 }
2264 ea.arg = skipwhite(ea.arg);
2265 }
2266
2267 /*
2268 * Check for "+command" argument, before checking for next command.
2269 * Don't do this for ":read !cmd" and ":write !cmd".
2270 */
2271 if ((ea.argt & EDITCMD) && !ea.usefilter)
2272 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2273
2274 /*
2275 * Check for '|' to separate commands and '"' to start comments.
2276 * Don't do this for ":read !cmd" and ":write !cmd".
2277 */
2278 if ((ea.argt & TRLBAR) && !ea.usefilter)
2279 separate_nextcmd(&ea);
2280
2281 /*
2282 * Check for <newline> to end a shell command.
2283 * Also do this for ":read !cmd" and ":write !cmd".
2284 */
2285 else if (ea.cmdidx == CMD_bang || ea.usefilter)
2286 {
2287 for (p = ea.arg; *p; ++p)
2288 {
2289 /* Remove one backslash before a newline, so that it's possible to
2290 * pass a newline to the shell and also a newline that is preceded
2291 * with a backslash. This makes it impossible to end a shell
2292 * command in a backslash, but that doesn't appear useful.
2293 * Halving the number of backslashes is incompatible with previous
2294 * versions. */
2295 if (*p == '\\' && p[1] == '\n')
2296 mch_memmove(p, p + 1, STRLEN(p));
2297 else if (*p == '\n')
2298 {
2299 ea.nextcmd = p + 1;
2300 *p = NUL;
2301 break;
2302 }
2303 }
2304 }
2305
2306 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2307 {
2308 ea.line1 = 1;
2309 ea.line2 = curbuf->b_ml.ml_line_count;
2310 }
2311
2312 /* accept numbered register only when no count allowed (:put) */
2313 if ( (ea.argt & REGSTR)
2314 && *ea.arg != NUL
2315#ifdef FEAT_USR_CMDS
2316 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2317 && USER_CMDIDX(ea.cmdidx)))
2318 /* Do not allow register = for user commands */
2319 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2320#else
2321 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2322#endif
2323 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2324 {
2325 ea.regname = *ea.arg++;
2326#ifdef FEAT_EVAL
2327 /* for '=' register: accept the rest of the line as an expression */
2328 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2329 {
2330 set_expr_line(vim_strsave(ea.arg));
2331 ea.arg += STRLEN(ea.arg);
2332 }
2333#endif
2334 ea.arg = skipwhite(ea.arg);
2335 }
2336
2337 /*
2338 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2339 * count, it's a buffer name.
2340 */
2341 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2342 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2343 || vim_iswhite(*p)))
2344 {
2345 n = getdigits(&ea.arg);
2346 ea.arg = skipwhite(ea.arg);
2347 if (n <= 0 && !ni)
2348 {
2349 errormsg = (char_u *)_(e_zerocount);
2350 goto doend;
2351 }
2352 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2353 {
2354 ea.line2 = n;
2355 if (ea.addr_count == 0)
2356 ea.addr_count = 1;
2357 }
2358 else
2359 {
2360 ea.line1 = ea.line2;
2361 ea.line2 += n - 1;
2362 ++ea.addr_count;
2363 /*
2364 * Be vi compatible: no error message for out of range.
2365 */
2366 if (ea.line2 > curbuf->b_ml.ml_line_count)
2367 ea.line2 = curbuf->b_ml.ml_line_count;
2368 }
2369 }
2370 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002371 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
2372 && vim_strchr((char_u *)"|\"", *ea.arg) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
2374 errormsg = (char_u *)_(e_trailing);
2375 goto doend;
2376 }
2377
2378 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2379 {
2380 errormsg = (char_u *)_(e_argreq);
2381 goto doend;
2382 }
2383
2384#ifdef FEAT_EVAL
2385 /*
2386 * Skip the command when it's not going to be executed.
2387 * The commands like :if, :endif, etc. always need to be executed.
2388 * Also make an exception for commands that handle a trailing command
2389 * themselves.
2390 */
2391 if (ea.skip)
2392 {
2393 switch (ea.cmdidx)
2394 {
2395 /* commands that need evaluation */
2396 case CMD_while:
2397 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002398 case CMD_for:
2399 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_if:
2401 case CMD_elseif:
2402 case CMD_else:
2403 case CMD_endif:
2404 case CMD_try:
2405 case CMD_catch:
2406 case CMD_finally:
2407 case CMD_endtry:
2408 case CMD_function:
2409 break;
2410
2411 /* Commands that handle '|' themselves. Check: A command should
2412 * either have the TRLBAR flag, appear in this list or appear in
2413 * the list at ":help :bar". */
2414 case CMD_aboveleft:
2415 case CMD_and:
2416 case CMD_belowright:
2417 case CMD_botright:
2418 case CMD_browse:
2419 case CMD_call:
2420 case CMD_confirm:
2421 case CMD_delfunction:
2422 case CMD_djump:
2423 case CMD_dlist:
2424 case CMD_dsearch:
2425 case CMD_dsplit:
2426 case CMD_echo:
2427 case CMD_echoerr:
2428 case CMD_echomsg:
2429 case CMD_echon:
2430 case CMD_execute:
2431 case CMD_help:
2432 case CMD_hide:
2433 case CMD_ijump:
2434 case CMD_ilist:
2435 case CMD_isearch:
2436 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002437 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 case CMD_keepjumps:
2439 case CMD_keepmarks:
2440 case CMD_leftabove:
2441 case CMD_let:
2442 case CMD_lockmarks:
2443 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002444 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_perl:
2446 case CMD_psearch:
2447 case CMD_python:
2448 case CMD_return:
2449 case CMD_rightbelow:
2450 case CMD_ruby:
2451 case CMD_silent:
2452 case CMD_smagic:
2453 case CMD_snomagic:
2454 case CMD_substitute:
2455 case CMD_syntax:
2456 case CMD_tcl:
2457 case CMD_throw:
2458 case CMD_tilde:
2459 case CMD_topleft:
2460 case CMD_unlet:
2461 case CMD_verbose:
2462 case CMD_vertical:
2463 break;
2464
2465 default: goto doend;
2466 }
2467 }
2468#endif
2469
2470 if (ea.argt & XFILE)
2471 {
2472 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2473 goto doend;
2474 }
2475
2476#ifdef FEAT_LISTCMDS
2477 /*
2478 * Accept buffer name. Cannot be used at the same time with a buffer
2479 * number. Don't do this for a user command.
2480 */
2481 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2482# ifdef FEAT_USR_CMDS
2483 && !USER_CMDIDX(ea.cmdidx)
2484# endif
2485 )
2486 {
2487 /*
2488 * :bdelete, :bwipeout and :bunload take several arguments, separated
2489 * by spaces: find next space (skipping over escaped characters).
2490 * The others take one argument: ignore trailing spaces.
2491 */
2492 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2493 || ea.cmdidx == CMD_bunload)
2494 p = skiptowhite_esc(ea.arg);
2495 else
2496 {
2497 p = ea.arg + STRLEN(ea.arg);
2498 while (p > ea.arg && vim_iswhite(p[-1]))
2499 --p;
2500 }
2501 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2502 if (ea.line2 < 0) /* failed */
2503 goto doend;
2504 ea.addr_count = 1;
2505 ea.arg = skipwhite(p);
2506 }
2507#endif
2508
2509/*
2510 * 6. switch on command name
2511 *
2512 * The "ea" structure holds the arguments that can be used.
2513 */
2514 ea.cmdlinep = cmdlinep;
2515 ea.getline = getline;
2516 ea.cookie = cookie;
2517#ifdef FEAT_EVAL
2518 ea.cstack = cstack;
2519#endif
2520
2521#ifdef FEAT_USR_CMDS
2522 if (USER_CMDIDX(ea.cmdidx))
2523 {
2524 /*
2525 * Execute a user-defined command.
2526 */
2527 do_ucmd(&ea);
2528 }
2529 else
2530#endif
2531 {
2532 /*
2533 * Call the function to execute the command.
2534 */
2535 ea.errmsg = NULL;
2536 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2537 if (ea.errmsg != NULL)
2538 errormsg = (char_u *)_(ea.errmsg);
2539 }
2540
2541#ifdef FEAT_EVAL
2542 /*
2543 * If the command just executed called do_cmdline(), any throw or ":return"
2544 * or ":finish" encountered there must also check the cstack of the still
2545 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2546 * exception, or reanimate a returned function or finished script file and
2547 * return or finish it again.
2548 */
2549 if (need_rethrow)
2550 do_throw(cstack);
2551 else if (check_cstack)
2552 {
2553 if (source_finished(getline, cookie))
2554 do_finish(&ea, TRUE);
2555 else if (getline_equal(getline, cookie, get_func_line)
2556 && current_func_returned())
2557 do_return(&ea, TRUE, FALSE, NULL);
2558 }
2559 need_rethrow = check_cstack = FALSE;
2560#endif
2561
2562doend:
2563 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2564 curwin->w_cursor.lnum = 1;
2565
2566 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2567 {
2568 if (sourcing)
2569 {
2570 if (errormsg != IObuff)
2571 {
2572 STRCPY(IObuff, errormsg);
2573 errormsg = IObuff;
2574 }
2575 STRCAT(errormsg, ": ");
2576 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2577 }
2578 emsg(errormsg);
2579 }
2580#ifdef FEAT_EVAL
2581 do_errthrow(cstack,
2582 (ea.cmdidx != CMD_SIZE
2583# ifdef FEAT_USR_CMDS
2584 && !USER_CMDIDX(ea.cmdidx)
2585# endif
2586 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2587#endif
2588
2589 if (verbose_save >= 0)
2590 p_verbose = verbose_save;
2591
2592 cmdmod = save_cmdmod;
2593
2594 if (did_silent > 0)
2595 {
2596 /* messages could be enabled for a serious error, need to check if the
2597 * counters don't become negative */
2598 msg_silent -= did_silent;
2599 if (msg_silent < 0)
2600 msg_silent = 0;
2601 emsg_silent -= did_esilent;
2602 if (emsg_silent < 0)
2603 emsg_silent = 0;
2604 /* Restore msg_scroll, it's set by file I/O commands, even when no
2605 * message is actually displayed. */
2606 msg_scroll = save_msg_scroll;
2607 }
2608
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002609#ifdef HAVE_SANDBOX
2610 if (did_sandbox)
2611 --sandbox;
2612#endif
2613
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2615 ea.nextcmd = NULL;
2616
2617#ifdef FEAT_EVAL
2618 --ex_nesting_level;
2619#endif
2620
2621 return ea.nextcmd;
2622}
2623#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002624 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625#endif
2626
2627/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002628 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 * If there is a match advance "pp" to the argument and return TRUE.
2630 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002631 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002633 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 char *cmd; /* name of command */
2635 int len; /* required length */
2636{
2637 int i;
2638
2639 for (i = 0; cmd[i] != NUL; ++i)
2640 if (cmd[i] != (*pp)[i])
2641 break;
2642 if (i >= len && !isalpha((*pp)[i]))
2643 {
2644 *pp = skipwhite(*pp + i);
2645 return TRUE;
2646 }
2647 return FALSE;
2648}
2649
2650/*
2651 * Find an Ex command by its name, either built-in or user.
2652 * Name can be found at eap->cmd.
2653 * Returns pointer to char after the command name.
2654 * Returns NULL for an ambiguous user command.
2655 */
2656/*ARGSUSED*/
2657 static char_u *
2658find_command(eap, full)
2659 exarg_T *eap;
2660 int *full;
2661{
2662 int len;
2663 char_u *p;
2664
2665 /*
2666 * Isolate the command and search for it in the command table.
2667 * Exeptions:
2668 * - the 'k' command can directly be followed by any character.
2669 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2670 * but :sre[wind] is another command, as are :scrip[tnames],
2671 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
2672 */
2673 p = eap->cmd;
2674 if (*p == 'k')
2675 {
2676 eap->cmdidx = CMD_k;
2677 ++p;
2678 }
2679 else if (p[0] == 's'
2680 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r' && p[3] != 'i' && p[4] != 'p')
2681 || p[1] == 'g'
2682 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2683 || p[1] == 'I'
2684 || (p[1] == 'r' && p[2] != 'e')))
2685 {
2686 eap->cmdidx = CMD_substitute;
2687 ++p;
2688 }
2689 else
2690 {
2691 while (ASCII_ISALPHA(*p))
2692 ++p;
2693 /* check for non-alpha command */
2694 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2695 ++p;
2696 len = (int)(p - eap->cmd);
2697
2698 if (ASCII_ISLOWER(*eap->cmd))
2699 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2700 else
2701 eap->cmdidx = cmdidxs[26];
2702
2703 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2704 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2705 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2706 (size_t)len) == 0)
2707 {
2708#ifdef FEAT_EVAL
2709 if (full != NULL
2710 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2711 *full = TRUE;
2712#endif
2713 break;
2714 }
2715
2716#ifdef FEAT_USR_CMDS
2717 /* Look for a user defined command as a last resort */
2718 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2719 {
2720 ucmd_T *cmd;
2721 int j, k, matchlen = 0;
2722 int found = FALSE, possible = FALSE;
2723 char_u *cp, *np; /* Point into typed cmd and test name */
2724 garray_T *gap;
2725 int amb_local = FALSE; /* Found ambiguous buffer-local
2726 command, only full match global
2727 is accepted. */
2728
2729 /* User defined commands may contain numbers */
2730 while (ASCII_ISALNUM(*p))
2731 ++p;
2732 len = (int)(p - eap->cmd);
2733
2734 /*
2735 * Look for buffer-local user commands first, then global ones.
2736 */
2737 gap = &curbuf->b_ucmds;
2738 for (;;)
2739 {
2740 for (j = 0; j < gap->ga_len; ++j)
2741 {
2742 cmd = USER_CMD_GA(gap, j);
2743 cp = eap->cmd;
2744 np = cmd->uc_name;
2745 k = 0;
2746 while (k < len && *np != NUL && *cp++ == *np++)
2747 k++;
2748 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2749 {
2750 /* If finding a second match, the command is
2751 * ambiguous. But not if a buffer-local command
2752 * wasn't a full match and a global command is a full
2753 * match. */
2754 if (k == len && found && *np != NUL)
2755 {
2756 if (gap == &ucmds)
2757 return NULL;
2758 amb_local = TRUE;
2759 }
2760
2761 if (!found || (k == len && *np == NUL))
2762 {
2763 /* If we matched up to a digit, then there could
2764 * be another command including the digit that we
2765 * should use instead.
2766 */
2767 if (k == len)
2768 found = TRUE;
2769 else
2770 possible = TRUE;
2771
2772 if (gap == &ucmds)
2773 eap->cmdidx = CMD_USER;
2774 else
2775 eap->cmdidx = CMD_USER_BUF;
2776 eap->argt = cmd->uc_argt;
2777 eap->useridx = j;
2778
2779 /* Do not search for further abbreviations
2780 * if this is an exact match. */
2781 matchlen = k;
2782 if (k == len && *np == NUL)
2783 {
2784 if (full != NULL)
2785 *full = TRUE;
2786 amb_local = FALSE;
2787 break;
2788 }
2789 }
2790 }
2791 }
2792
2793 /* Stop if we found a full match or searched all. */
2794 if (j < gap->ga_len || gap == &ucmds)
2795 break;
2796 gap = &ucmds;
2797 }
2798
2799 /* Only found ambiguous matches. */
2800 if (amb_local)
2801 return NULL;
2802
2803 /* The match we found may be followed immediately by a
2804 * number. Move *p back to point to it.
2805 */
2806 if (found || possible)
2807 p += matchlen - len;
2808 }
2809#endif
2810
2811 if (len == 0)
2812 eap->cmdidx = CMD_SIZE;
2813 }
2814
2815 return p;
2816}
2817
2818#if defined(FEAT_EVAL) || defined(PROTO)
2819/*
2820 * Return > 0 if an Ex command "name" exists.
2821 * Return 2 if there is an exact match.
2822 * Return 3 if there is an ambiguous match.
2823 */
2824 int
2825cmd_exists(name)
2826 char_u *name;
2827{
2828 exarg_T ea;
2829 int full = FALSE;
2830 int i;
2831 int j;
2832 static struct cmdmod
2833 {
2834 char *name;
2835 int minlen;
2836 } cmdmods[] = {
2837 {"aboveleft", 3},
2838 {"belowright", 3},
2839 {"botright", 2},
2840 {"browse", 3},
2841 {"confirm", 4},
2842 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002843 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {"keepjumps", 5},
2845 {"keepmarks", 3},
2846 {"leftabove", 5},
2847 {"lockmarks", 3},
2848 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002849 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {"silent", 3},
2851 {"topleft", 2},
2852 {"verbose", 4},
2853 {"vertical", 4},
2854 };
2855
2856 /* Check command modifiers. */
2857 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2858 {
2859 for (j = 0; name[j] != NUL; ++j)
2860 if (name[j] != cmdmods[i].name[j])
2861 break;
2862 if (name[j] == NUL && j >= cmdmods[i].minlen)
2863 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2864 }
2865
2866 /* Check built-in commands and user defined commands. */
2867 ea.cmd = name;
2868 ea.cmdidx = (cmdidx_T)0;
2869 if (find_command(&ea, &full) == NULL)
2870 return 3;
2871 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2872}
2873#endif
2874
2875/*
2876 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2877 * we don't need/want deleted. Maybe this could be done better if we didn't
2878 * repeat all this stuff. The only problem is that they may not stay
2879 * perfectly compatible with each other, but then the command line syntax
2880 * probably won't change that much -- webb.
2881 */
2882 char_u *
2883set_one_cmd_context(xp, buff)
2884 expand_T *xp;
2885 char_u *buff; /* buffer for command string */
2886{
2887 char_u *p;
2888 char_u *cmd, *arg;
2889 int i = 0;
2890 cmdidx_T cmdidx;
2891 long_u argt = 0;
2892#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
2893 int compl = EXPAND_NOTHING;
2894#endif
2895#ifdef FEAT_CMDL_COMPL
2896 int delim;
2897#endif
2898 int forceit = FALSE;
2899 int usefilter = FALSE; /* filter instead of file name */
2900
2901 xp->xp_pattern = buff;
2902 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
2903 xp->xp_backslash = XP_BS_NONE;
2904#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
2905 xp->xp_arg = NULL;
2906#endif
2907
2908/*
2909 * 2. skip comment lines and leading space, colons or bars
2910 */
2911 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2912 ;
2913 xp->xp_pattern = cmd;
2914
2915 if (*cmd == NUL)
2916 return NULL;
2917 if (*cmd == '"') /* ignore comment lines */
2918 {
2919 xp->xp_context = EXPAND_NOTHING;
2920 return NULL;
2921 }
2922
2923/*
2924 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
2925 */
2926 cmd = skip_range(cmd, &xp->xp_context);
2927
2928/*
2929 * 4. parse command
2930 */
2931
2932 cmd = skipwhite(cmd);
2933 xp->xp_pattern = cmd;
2934 if (*cmd == NUL)
2935 return NULL;
2936 if (*cmd == '"')
2937 {
2938 xp->xp_context = EXPAND_NOTHING;
2939 return NULL;
2940 }
2941
2942 if (*cmd == '|' || *cmd == '\n')
2943 return cmd + 1; /* There's another command */
2944
2945 /*
2946 * Isolate the command and search for it in the command table.
2947 * Exceptions:
2948 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002949 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2951 */
2952 if (*cmd == 'k' && cmd[1] != 'e')
2953 {
2954 cmdidx = CMD_k;
2955 p = cmd + 1;
2956 }
2957 else
2958 {
2959 p = cmd;
2960 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
2961 ++p;
2962 /* check for non-alpha command */
2963 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2964 ++p;
2965 i = (int)(p - cmd);
2966
2967 if (i == 0)
2968 {
2969 xp->xp_context = EXPAND_UNSUCCESSFUL;
2970 return NULL;
2971 }
2972 for (cmdidx = (cmdidx_T)0; (int)cmdidx < (int)CMD_SIZE;
2973 cmdidx = (cmdidx_T)((int)cmdidx + 1))
2974 if (STRNCMP(cmdnames[(int)cmdidx].cmd_name, cmd, (size_t)i) == 0)
2975 break;
2976
2977#ifdef FEAT_USR_CMDS
2978 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
2979 {
2980 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
2981 ++p;
2982 i = (int)(p - cmd);
2983 }
2984#endif
2985 }
2986
2987 /*
2988 * If the cursor is touching the command, and it ends in an alpha-numeric
2989 * character, complete the command name.
2990 */
2991 if (*p == NUL && ASCII_ISALNUM(p[-1]))
2992 return NULL;
2993
2994 if (cmdidx == CMD_SIZE)
2995 {
2996 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
2997 {
2998 cmdidx = CMD_substitute;
2999 p = cmd + 1;
3000 }
3001#ifdef FEAT_USR_CMDS
3002 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3003 {
3004 /* Look for a user defined command as a last resort */
3005 ucmd_T *uc;
3006 int j, k, matchlen = 0;
3007 int found = FALSE, possible = FALSE;
3008 char_u *cp, *np; /* Point into typed cmd and test name */
3009 garray_T *gap;
3010
3011 gap = &curbuf->b_ucmds;
3012 for (;;)
3013 {
3014 uc = USER_CMD_GA(gap, 0);
3015 for (j = 0; j < gap->ga_len; ++j, ++uc)
3016 {
3017 cp = cmd;
3018 np = uc->uc_name;
3019 k = 0;
3020 while (k < i && *np != NUL && *cp++ == *np++)
3021 k++;
3022 if (k == i || (*np == NUL && VIM_ISDIGIT(cmd[k])))
3023 {
3024 if (k == i && found)
3025 {
3026 /* Ambiguous abbreviation */
3027 xp->xp_context = EXPAND_UNSUCCESSFUL;
3028 return NULL;
3029 }
3030 if (!found)
3031 {
3032 /* If we matched up to a digit, then there could
3033 * be another command including the digit that we
3034 * should use instead.
3035 */
3036 if (k == i)
3037 found = TRUE;
3038 else
3039 possible = TRUE;
3040
3041 if (gap == &ucmds)
3042 cmdidx = CMD_USER;
3043 else
3044 cmdidx = CMD_USER_BUF;
3045 argt = uc->uc_argt;
3046#ifdef FEAT_CMDL_COMPL
3047 compl = uc->uc_compl;
3048# ifdef FEAT_EVAL
3049 xp->xp_arg = uc->uc_compl_arg;
3050 xp->xp_scriptID = uc->uc_scriptID;
3051# endif
3052#endif
3053 /* Do not search for further abbreviations
3054 * if this is an exact match
3055 */
3056 matchlen = k;
3057 if (k == i && *np == NUL)
3058 break;
3059 }
3060 }
3061 }
3062 if (gap == &ucmds || j < gap->ga_len)
3063 break;
3064 gap = &ucmds;
3065 }
3066
3067 /* The match we found may be followed immediately by a
3068 * number. Move *p back to point to it.
3069 */
3070 if (found || possible)
3071 p += matchlen - i;
3072 }
3073#endif
3074 }
3075 if (cmdidx == CMD_SIZE)
3076 {
3077 /* Not still touching the command and it was an illegal one */
3078 xp->xp_context = EXPAND_UNSUCCESSFUL;
3079 return NULL;
3080 }
3081
3082 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3083
3084 if (*p == '!') /* forced commands */
3085 {
3086 forceit = TRUE;
3087 ++p;
3088 }
3089
3090/*
3091 * 5. parse arguments
3092 */
3093#ifdef FEAT_USR_CMDS
3094 if (!USER_CMDIDX(cmdidx))
3095#endif
3096 argt = cmdnames[(int)cmdidx].cmd_argt;
3097
3098 arg = skipwhite(p);
3099
3100 if (cmdidx == CMD_write || cmdidx == CMD_update)
3101 {
3102 if (*arg == '>') /* append */
3103 {
3104 if (*++arg == '>')
3105 ++arg;
3106 arg = skipwhite(arg);
3107 }
3108 else if (*arg == '!' && cmdidx == CMD_write) /* :w !filter */
3109 {
3110 ++arg;
3111 usefilter = TRUE;
3112 }
3113 }
3114
3115 if (cmdidx == CMD_read)
3116 {
3117 usefilter = forceit; /* :r! filter if forced */
3118 if (*arg == '!') /* :r !filter */
3119 {
3120 ++arg;
3121 usefilter = TRUE;
3122 }
3123 }
3124
3125 if (cmdidx == CMD_lshift || cmdidx == CMD_rshift)
3126 {
3127 while (*arg == *cmd) /* allow any number of '>' or '<' */
3128 ++arg;
3129 arg = skipwhite(arg);
3130 }
3131
3132 /* Does command allow "+command"? */
3133 if ((argt & EDITCMD) && !usefilter && *arg == '+')
3134 {
3135 /* Check if we're in the +command */
3136 p = arg + 1;
3137 arg = skip_cmd_arg(arg, FALSE);
3138
3139 /* Still touching the command after '+'? */
3140 if (*arg == NUL)
3141 return p;
3142
3143 /* Skip space(s) after +command to get to the real argument */
3144 arg = skipwhite(arg);
3145 }
3146
3147 /*
3148 * Check for '|' to separate commands and '"' to start comments.
3149 * Don't do this for ":read !cmd" and ":write !cmd".
3150 */
3151 if ((argt & TRLBAR) && !usefilter)
3152 {
3153 p = arg;
3154 /* ":redir @" is not the start of a comment */
3155 if (cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
3156 p += 2;
3157 while (*p)
3158 {
3159 if (*p == Ctrl_V)
3160 {
3161 if (p[1] != NUL)
3162 ++p;
3163 }
3164 else if ( (*p == '"' && !(argt & NOTRLCOM))
3165 || *p == '|' || *p == '\n')
3166 {
3167 if (*(p - 1) != '\\')
3168 {
3169 if (*p == '|' || *p == '\n')
3170 return p + 1;
3171 return NULL; /* It's a comment */
3172 }
3173 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003174 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
3176 }
3177
3178 /* no arguments allowed */
3179 if (!(argt & EXTRA) && *arg != NUL &&
3180 vim_strchr((char_u *)"|\"", *arg) == NULL)
3181 return NULL;
3182
3183 /* Find start of last argument (argument just before cursor): */
3184 p = buff + STRLEN(buff);
3185 while (p != arg && *p != ' ' && *p != TAB)
3186 p--;
3187 if (*p == ' ' || *p == TAB)
3188 p++;
3189 xp->xp_pattern = p;
3190
Bram Moolenaar748bf032005-02-02 23:04:36 +00003191 if (argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 int in_quote = FALSE;
3194 char_u *bow = NULL; /* Beginning of word */
3195
3196 /*
3197 * Allow spaces within back-quotes to count as part of the argument
3198 * being expanded.
3199 */
3200 xp->xp_pattern = skipwhite(arg);
3201 for (p = xp->xp_pattern; *p; )
3202 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003203 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 ++p;
3205#ifdef SPACE_IN_FILENAME
3206 else if (vim_iswhite(*p) && (!(argt & NOSPC) || usefilter))
3207#else
3208 else if (vim_iswhite(*p))
3209#endif
3210 {
3211 p = skipwhite(p);
3212 if (in_quote)
3213 bow = p;
3214 else
3215 xp->xp_pattern = p;
3216 --p;
3217 }
3218 else if (*p == '`')
3219 {
3220 if (!in_quote)
3221 {
3222 xp->xp_pattern = p;
3223 bow = p + 1;
3224 }
3225 in_quote = !in_quote;
3226 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003227 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229
3230 /*
3231 * If we are still inside the quotes, and we passed a space, just
3232 * expand from there.
3233 */
3234 if (bow != NULL && in_quote)
3235 xp->xp_pattern = bow;
3236 xp->xp_context = EXPAND_FILES;
3237
3238 /* Check for environment variable */
3239 if (*xp->xp_pattern == '$'
3240#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3241 || *xp->xp_pattern == '%'
3242#endif
3243 )
3244 {
3245 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3246 if (!vim_isIDc(*p))
3247 break;
3248 if (*p == NUL)
3249 {
3250 xp->xp_context = EXPAND_ENV_VARS;
3251 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003252#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3253 /* Avoid that the assignment uses EXPAND_FILES again. */
3254 if (compl != EXPAND_USER_DEFINED)
3255 compl = EXPAND_ENV_VARS;
3256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258 }
3259 }
3260
3261/*
3262 * 6. switch on command name
3263 */
3264 switch (cmdidx)
3265 {
3266 case CMD_cd:
3267 case CMD_chdir:
3268 case CMD_lcd:
3269 case CMD_lchdir:
3270 if (xp->xp_context == EXPAND_FILES)
3271 xp->xp_context = EXPAND_DIRECTORIES;
3272 break;
3273 case CMD_help:
3274 xp->xp_context = EXPAND_HELP;
3275 xp->xp_pattern = arg;
3276 break;
3277
3278 /* Command modifiers: return the argument. */
3279 case CMD_aboveleft:
3280 case CMD_belowright:
3281 case CMD_botright:
3282 case CMD_browse:
3283 case CMD_confirm:
3284 case CMD_folddoclosed:
3285 case CMD_folddoopen:
3286 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003287 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 case CMD_keepjumps:
3289 case CMD_keepmarks:
3290 case CMD_leftabove:
3291 case CMD_lockmarks:
3292 case CMD_rightbelow:
3293 case CMD_silent:
3294 case CMD_topleft:
3295 case CMD_verbose:
3296 case CMD_vertical:
3297 return arg;
3298
3299#ifdef FEAT_SEARCH_EXTRA
3300 case CMD_match:
3301 if (*arg == NUL || !ends_excmd(*arg))
3302 {
3303 /* Dummy call to clear variables. */
3304 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3305 xp->xp_context = EXPAND_HIGHLIGHT;
3306 xp->xp_pattern = arg;
3307 arg = skipwhite(skiptowhite(arg));
3308 if (*arg != NUL)
3309 {
3310 xp->xp_context = EXPAND_NOTHING;
3311 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3312 }
3313 }
3314 return find_nextcmd(arg);
3315#endif
3316
3317#ifdef FEAT_CMDL_COMPL
3318/*
3319 * All completion for the +cmdline_compl feature goes here.
3320 */
3321
3322# ifdef FEAT_USR_CMDS
3323 case CMD_command:
3324 /* Check for attributes */
3325 while (*arg == '-')
3326 {
3327 arg++; /* Skip "-" */
3328 p = skiptowhite(arg);
3329 if (*p == NUL)
3330 {
3331 /* Cursor is still in the attribute */
3332 p = vim_strchr(arg, '=');
3333 if (p == NULL)
3334 {
3335 /* No "=", so complete attribute names */
3336 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3337 xp->xp_pattern = arg;
3338 return NULL;
3339 }
3340
3341 /* For the -complete and -nargs attributes, we complete
3342 * their arguments as well.
3343 */
3344 if (STRNICMP(arg, "complete", p - arg) == 0)
3345 {
3346 xp->xp_context = EXPAND_USER_COMPLETE;
3347 xp->xp_pattern = p + 1;
3348 return NULL;
3349 }
3350 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3351 {
3352 xp->xp_context = EXPAND_USER_NARGS;
3353 xp->xp_pattern = p + 1;
3354 return NULL;
3355 }
3356 return NULL;
3357 }
3358 arg = skipwhite(p);
3359 }
3360
3361 /* After the attributes comes the new command name */
3362 p = skiptowhite(arg);
3363 if (*p == NUL)
3364 {
3365 xp->xp_context = EXPAND_USER_COMMANDS;
3366 xp->xp_pattern = arg;
3367 break;
3368 }
3369
3370 /* And finally comes a normal command */
3371 return skipwhite(p);
3372
3373 case CMD_delcommand:
3374 xp->xp_context = EXPAND_USER_COMMANDS;
3375 xp->xp_pattern = arg;
3376 break;
3377# endif
3378
3379 case CMD_global:
3380 case CMD_vglobal:
3381 delim = *arg; /* get the delimiter */
3382 if (delim)
3383 ++arg; /* skip delimiter if there is one */
3384
3385 while (arg[0] != NUL && arg[0] != delim)
3386 {
3387 if (arg[0] == '\\' && arg[1] != NUL)
3388 ++arg;
3389 ++arg;
3390 }
3391 if (arg[0] != NUL)
3392 return arg + 1;
3393 break;
3394 case CMD_and:
3395 case CMD_substitute:
3396 delim = *arg;
3397 if (delim)
3398 {
3399 /* skip "from" part */
3400 ++arg;
3401 arg = skip_regexp(arg, delim, p_magic, NULL);
3402 }
3403 /* skip "to" part */
3404 while (arg[0] != NUL && arg[0] != delim)
3405 {
3406 if (arg[0] == '\\' && arg[1] != NUL)
3407 ++arg;
3408 ++arg;
3409 }
3410 if (arg[0] != NUL) /* skip delimiter */
3411 ++arg;
3412 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3413 ++arg;
3414 if (arg[0] != NUL)
3415 return arg;
3416 break;
3417 case CMD_isearch:
3418 case CMD_dsearch:
3419 case CMD_ilist:
3420 case CMD_dlist:
3421 case CMD_ijump:
3422 case CMD_psearch:
3423 case CMD_djump:
3424 case CMD_isplit:
3425 case CMD_dsplit:
3426 arg = skipwhite(skipdigits(arg)); /* skip count */
3427 if (*arg == '/') /* Match regexp, not just whole words */
3428 {
3429 for (++arg; *arg && *arg != '/'; arg++)
3430 if (*arg == '\\' && arg[1] != NUL)
3431 arg++;
3432 if (*arg)
3433 {
3434 arg = skipwhite(arg + 1);
3435
3436 /* Check for trailing illegal characters */
3437 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3438 xp->xp_context = EXPAND_NOTHING;
3439 else
3440 return arg;
3441 }
3442 }
3443 break;
3444#ifdef FEAT_AUTOCMD
3445 case CMD_autocmd:
3446 return set_context_in_autocmd(xp, arg, FALSE);
3447
3448 case CMD_doautocmd:
3449 return set_context_in_autocmd(xp, arg, TRUE);
3450#endif
3451 case CMD_set:
3452 set_context_in_set_cmd(xp, arg, 0);
3453 break;
3454 case CMD_setglobal:
3455 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3456 break;
3457 case CMD_setlocal:
3458 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3459 break;
3460 case CMD_tag:
3461 case CMD_stag:
3462 case CMD_ptag:
3463 case CMD_tselect:
3464 case CMD_stselect:
3465 case CMD_ptselect:
3466 case CMD_tjump:
3467 case CMD_stjump:
3468 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003469 if (*p_wop != NUL)
3470 xp->xp_context = EXPAND_TAGS_LISTFILES;
3471 else
3472 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 xp->xp_pattern = arg;
3474 break;
3475 case CMD_augroup:
3476 xp->xp_context = EXPAND_AUGROUP;
3477 xp->xp_pattern = arg;
3478 break;
3479#ifdef FEAT_SYN_HL
3480 case CMD_syntax:
3481 set_context_in_syntax_cmd(xp, arg);
3482 break;
3483#endif
3484#ifdef FEAT_EVAL
3485 case CMD_let:
3486 case CMD_if:
3487 case CMD_elseif:
3488 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003489 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 case CMD_echo:
3491 case CMD_echon:
3492 case CMD_execute:
3493 case CMD_echomsg:
3494 case CMD_echoerr:
3495 case CMD_call:
3496 case CMD_return:
3497 set_context_for_expression(xp, arg, cmdidx);
3498 break;
3499
3500 case CMD_unlet:
3501 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3502 arg = xp->xp_pattern + 1;
3503 xp->xp_context = EXPAND_USER_VARS;
3504 xp->xp_pattern = arg;
3505 break;
3506
3507 case CMD_function:
3508 case CMD_delfunction:
3509 xp->xp_context = EXPAND_USER_FUNC;
3510 xp->xp_pattern = arg;
3511 break;
3512
3513 case CMD_echohl:
3514 xp->xp_context = EXPAND_HIGHLIGHT;
3515 xp->xp_pattern = arg;
3516 break;
3517#endif
3518 case CMD_highlight:
3519 set_context_in_highlight_cmd(xp, arg);
3520 break;
3521#ifdef FEAT_LISTCMDS
3522 case CMD_bdelete:
3523 case CMD_bwipeout:
3524 case CMD_bunload:
3525 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3526 arg = xp->xp_pattern + 1;
3527 /*FALLTHROUGH*/
3528 case CMD_buffer:
3529 case CMD_sbuffer:
3530 case CMD_checktime:
3531 xp->xp_context = EXPAND_BUFFERS;
3532 xp->xp_pattern = arg;
3533 break;
3534#endif
3535#ifdef FEAT_USR_CMDS
3536 case CMD_USER:
3537 case CMD_USER_BUF:
3538 if (compl != EXPAND_NOTHING)
3539 {
3540 /* XFILE: file names are handled above */
3541 if (!(argt & XFILE))
3542 {
3543# ifdef FEAT_MENU
3544 if (compl == EXPAND_MENUS)
3545 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3546# endif
3547 if (compl == EXPAND_COMMANDS)
3548 return arg;
3549 if (compl == EXPAND_MAPPINGS)
3550 return set_context_in_map_cmd(xp, (char_u *)"map",
3551 arg, forceit, FALSE, FALSE, CMD_map);
3552 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3553 arg = xp->xp_pattern + 1;
3554 xp->xp_pattern = arg;
3555 }
3556 xp->xp_context = compl;
3557 }
3558 break;
3559#endif
3560 case CMD_map: case CMD_noremap:
3561 case CMD_nmap: case CMD_nnoremap:
3562 case CMD_vmap: case CMD_vnoremap:
3563 case CMD_omap: case CMD_onoremap:
3564 case CMD_imap: case CMD_inoremap:
3565 case CMD_cmap: case CMD_cnoremap:
3566 return set_context_in_map_cmd(xp, cmd, arg, forceit,
3567 FALSE, FALSE, cmdidx);
3568 case CMD_unmap:
3569 case CMD_nunmap:
3570 case CMD_vunmap:
3571 case CMD_ounmap:
3572 case CMD_iunmap:
3573 case CMD_cunmap:
3574 return set_context_in_map_cmd(xp, cmd, arg, forceit,
3575 FALSE, TRUE, cmdidx);
3576 case CMD_abbreviate: case CMD_noreabbrev:
3577 case CMD_cabbrev: case CMD_cnoreabbrev:
3578 case CMD_iabbrev: case CMD_inoreabbrev:
3579 return set_context_in_map_cmd(xp, cmd, arg, forceit,
3580 TRUE, FALSE, cmdidx);
3581 case CMD_unabbreviate:
3582 case CMD_cunabbrev:
3583 case CMD_iunabbrev:
3584 return set_context_in_map_cmd(xp, cmd, arg, forceit,
3585 TRUE, TRUE, cmdidx);
3586#ifdef FEAT_MENU
3587 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3588 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3589 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3590 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3591 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3592 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3593 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3594 case CMD_tmenu: case CMD_tunmenu:
3595 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3596 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3597#endif
3598
3599 case CMD_colorscheme:
3600 xp->xp_context = EXPAND_COLORS;
3601 xp->xp_pattern = arg;
3602 break;
3603
3604 case CMD_compiler:
3605 xp->xp_context = EXPAND_COMPILER;
3606 xp->xp_pattern = arg;
3607 break;
3608
3609#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3610 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3611 case CMD_language:
3612 if (*skiptowhite(arg) == NUL)
3613 {
3614 xp->xp_context = EXPAND_LANGUAGE;
3615 xp->xp_pattern = arg;
3616 }
3617 else
3618 xp->xp_context = EXPAND_NOTHING;
3619 break;
3620#endif
3621
3622#endif /* FEAT_CMDL_COMPL */
3623
3624 default:
3625 break;
3626 }
3627 return NULL;
3628}
3629
3630/*
3631 * skip a range specifier of the form: addr [,addr] [;addr] ..
3632 *
3633 * Backslashed delimiters after / or ? will be skipped, and commands will
3634 * not be expanded between /'s and ?'s or after "'".
3635 *
3636 * Returns the "cmd" pointer advanced to beyond the range.
3637 */
3638 char_u *
3639skip_range(cmd, ctx)
3640 char_u *cmd;
3641 int *ctx; /* pointer to xp_context or NULL */
3642{
3643 int delim;
3644
3645 while (*cmd != NUL && (vim_isspace(*cmd) || VIM_ISDIGIT(*cmd) ||
3646 vim_strchr((char_u *)".$%'/?-+,;", *cmd) != NULL))
3647 {
3648 if (*cmd == '\'')
3649 {
3650 if (*++cmd == NUL && ctx != NULL)
3651 *ctx = EXPAND_NOTHING;
3652 }
3653 else if (*cmd == '/' || *cmd == '?')
3654 {
3655 delim = *cmd++;
3656 while (*cmd != NUL && *cmd != delim)
3657 if (*cmd++ == '\\' && *cmd != NUL)
3658 ++cmd;
3659 if (*cmd == NUL && ctx != NULL)
3660 *ctx = EXPAND_NOTHING;
3661 }
3662 if (*cmd != NUL)
3663 ++cmd;
3664 }
3665 return cmd;
3666}
3667
3668/*
3669 * get a single EX address
3670 *
3671 * Set ptr to the next character after the part that was interpreted.
3672 * Set ptr to NULL when an error is encountered.
3673 *
3674 * Return MAXLNUM when no Ex address was found.
3675 */
3676 static linenr_T
3677get_address(ptr, skip, to_other_file)
3678 char_u **ptr;
3679 int skip; /* only skip the address, don't use it */
3680 int to_other_file; /* flag: may jump to other file */
3681{
3682 int c;
3683 int i;
3684 long n;
3685 char_u *cmd;
3686 pos_T pos;
3687 pos_T *fp;
3688 linenr_T lnum;
3689
3690 cmd = skipwhite(*ptr);
3691 lnum = MAXLNUM;
3692 do
3693 {
3694 switch (*cmd)
3695 {
3696 case '.': /* '.' - Cursor position */
3697 ++cmd;
3698 lnum = curwin->w_cursor.lnum;
3699 break;
3700
3701 case '$': /* '$' - last line */
3702 ++cmd;
3703 lnum = curbuf->b_ml.ml_line_count;
3704 break;
3705
3706 case '\'': /* ''' - mark */
3707 if (*++cmd == NUL)
3708 {
3709 cmd = NULL;
3710 goto error;
3711 }
3712 if (skip)
3713 ++cmd;
3714 else
3715 {
3716 /* Only accept a mark in another file when it is
3717 * used by itself: ":'M". */
3718 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3719 ++cmd;
3720 if (fp == (pos_T *)-1)
3721 /* Jumped to another file. */
3722 lnum = curwin->w_cursor.lnum;
3723 else
3724 {
3725 if (check_mark(fp) == FAIL)
3726 {
3727 cmd = NULL;
3728 goto error;
3729 }
3730 lnum = fp->lnum;
3731 }
3732 }
3733 break;
3734
3735 case '/':
3736 case '?': /* '/' or '?' - search */
3737 c = *cmd++;
3738 if (skip) /* skip "/pat/" */
3739 {
3740 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3741 if (*cmd == c)
3742 ++cmd;
3743 }
3744 else
3745 {
3746 pos = curwin->w_cursor; /* save curwin->w_cursor */
3747 /*
3748 * When '/' or '?' follows another address, start
3749 * from there.
3750 */
3751 if (lnum != MAXLNUM)
3752 curwin->w_cursor.lnum = lnum;
3753 /*
3754 * Start a forward search at the end of the line.
3755 * Start a backward search at the start of the line.
3756 * This makes sure we never match in the current
3757 * line, and can match anywhere in the
3758 * next/previous line.
3759 */
3760 if (c == '/')
3761 curwin->w_cursor.col = MAXCOL;
3762 else
3763 curwin->w_cursor.col = 0;
3764 searchcmdlen = 0;
3765 if (!do_search(NULL, c, cmd, 1L,
3766 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3767 {
3768 curwin->w_cursor = pos;
3769 cmd = NULL;
3770 goto error;
3771 }
3772 lnum = curwin->w_cursor.lnum;
3773 curwin->w_cursor = pos;
3774 /* adjust command string pointer */
3775 cmd += searchcmdlen;
3776 }
3777 break;
3778
3779 case '\\': /* "\?", "\/" or "\&", repeat search */
3780 ++cmd;
3781 if (*cmd == '&')
3782 i = RE_SUBST;
3783 else if (*cmd == '?' || *cmd == '/')
3784 i = RE_SEARCH;
3785 else
3786 {
3787 EMSG(_(e_backslash));
3788 cmd = NULL;
3789 goto error;
3790 }
3791
3792 if (!skip)
3793 {
3794 /*
3795 * When search follows another address, start from
3796 * there.
3797 */
3798 if (lnum != MAXLNUM)
3799 pos.lnum = lnum;
3800 else
3801 pos.lnum = curwin->w_cursor.lnum;
3802
3803 /*
3804 * Start the search just like for the above
3805 * do_search().
3806 */
3807 if (*cmd != '?')
3808 pos.col = MAXCOL;
3809 else
3810 pos.col = 0;
3811 if (searchit(curwin, curbuf, &pos,
3812 *cmd == '?' ? BACKWARD : FORWARD,
3813 (char_u *)"", 1L,
3814 SEARCH_MSG + SEARCH_START, i) != FAIL)
3815 lnum = pos.lnum;
3816 else
3817 {
3818 cmd = NULL;
3819 goto error;
3820 }
3821 }
3822 ++cmd;
3823 break;
3824
3825 default:
3826 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3827 lnum = getdigits(&cmd);
3828 }
3829
3830 for (;;)
3831 {
3832 cmd = skipwhite(cmd);
3833 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3834 break;
3835
3836 if (lnum == MAXLNUM)
3837 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3838 if (VIM_ISDIGIT(*cmd))
3839 i = '+'; /* "number" is same as "+number" */
3840 else
3841 i = *cmd++;
3842 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3843 n = 1;
3844 else
3845 n = getdigits(&cmd);
3846 if (i == '-')
3847 lnum -= n;
3848 else
3849 lnum += n;
3850 }
3851 } while (*cmd == '/' || *cmd == '?');
3852
3853error:
3854 *ptr = cmd;
3855 return lnum;
3856}
3857
3858/*
3859 * Function called for command which is Not Implemented. NI!
3860 */
3861 void
3862ex_ni(eap)
3863 exarg_T *eap;
3864{
3865 if (!eap->skip)
3866 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3867}
3868
3869#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003870 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871/*
3872 * Function called for script command which is Not Implemented. NI!
3873 * Skips over ":perl <<EOF" constructs.
3874 */
3875 static void
3876ex_script_ni(eap)
3877 exarg_T *eap;
3878{
3879 if (!eap->skip)
3880 ex_ni(eap);
3881 else
3882 vim_free(script_get(eap, eap->arg));
3883}
3884#endif
3885
3886/*
3887 * Check range in Ex command for validity.
3888 * Return NULL when valid, error message when invalid.
3889 */
3890 static char_u *
3891invalid_range(eap)
3892 exarg_T *eap;
3893{
3894 if ( eap->line1 < 0
3895 || eap->line2 < 0
3896 || eap->line1 > eap->line2
3897 || ((eap->argt & RANGE)
3898 && !(eap->argt & NOTADR)
3899 && eap->line2 > curbuf->b_ml.ml_line_count
3900#ifdef FEAT_DIFF
3901 + (eap->cmdidx == CMD_diffget)
3902#endif
3903 ))
3904 return (char_u *)_(e_invrange);
3905 return NULL;
3906}
3907
3908/*
3909 * Correct the range for zero line number, if required.
3910 */
3911 static void
3912correct_range(eap)
3913 exarg_T *eap;
3914{
3915 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
3916 {
3917 if (eap->line1 == 0)
3918 eap->line1 = 1;
3919 if (eap->line2 == 0)
3920 eap->line2 = 1;
3921 }
3922}
3923
Bram Moolenaar748bf032005-02-02 23:04:36 +00003924#ifdef FEAT_QUICKFIX
3925static char_u *skip_grep_pat __ARGS((exarg_T *eap));
3926
3927/*
3928 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3929 * pattern. Otherwise return eap->arg.
3930 */
3931 static char_u *
3932skip_grep_pat(eap)
3933 exarg_T *eap;
3934{
3935 char_u *p = eap->arg;
3936
3937 if (*p != NUL && (eap->cmdidx == CMD_vimgrep
3938 || eap->cmdidx == CMD_vimgrepadd || grep_internal(eap->cmdidx)))
3939 {
3940 p = skip_vimgrep_pat(p, NULL);
3941 if (p == NULL)
3942 p = eap->arg;
3943 else if (*p != NUL && !vim_iswhite(*p))
3944 ++p; /* step past ending separator of /pat/ */
3945 }
3946 return p;
3947}
3948#endif
3949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950/*
3951 * Expand file name in Ex command argument.
3952 * Return FAIL for failure, OK otherwise.
3953 */
3954 int
3955expand_filename(eap, cmdlinep, errormsgp)
3956 exarg_T *eap;
3957 char_u **cmdlinep;
3958 char_u **errormsgp;
3959{
3960 int has_wildcards; /* need to expand wildcards */
3961 char_u *repl;
3962 int srclen;
3963 char_u *p;
3964 int n;
3965
Bram Moolenaar748bf032005-02-02 23:04:36 +00003966#ifdef FEAT_QUICKFIX
3967 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
3968 p = skip_grep_pat(eap);
3969#else
3970 p = eap->arg;
3971#endif
3972
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 /*
3974 * Decide to expand wildcards *before* replacing '%', '#', etc. If
3975 * the file name contains a wildcard it should not cause expanding.
3976 * (it will be expanded anyway if there is a wildcard before replacing).
3977 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00003978 has_wildcards = mch_has_wildcard(p);
3979 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003981#ifdef FEAT_EVAL
3982 /* Skip over `=expr`, wildcards in it are not expanded. */
3983 if (p[0] == '`' && p[1] == '=')
3984 {
3985 p += 2;
3986 (void)skip_expr(&p);
3987 if (*p == '`')
3988 ++p;
3989 continue;
3990 }
3991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 /*
3993 * Quick check if this cannot be the start of a special string.
3994 * Also removes backslash before '%', '#' and '<'.
3995 */
3996 if (vim_strchr((char_u *)"%#<", *p) == NULL)
3997 {
3998 ++p;
3999 continue;
4000 }
4001
4002 /*
4003 * Try to find a match at this position.
4004 */
4005 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4006 if (*errormsgp != NULL) /* error detected */
4007 return FAIL;
4008 if (repl == NULL) /* no match found */
4009 {
4010 p += srclen;
4011 continue;
4012 }
4013
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004014 /* Wildcards won't be expanded below, the replacement is taken
4015 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4016 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4017 {
4018 char_u *l = repl;
4019
4020 repl = expand_env_save(repl);
4021 vim_free(l);
4022 }
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 /* Need to escape white space et al. with a backslash. Don't do this
4025 * for shell commands (may have to use quotes instead). Don't do this
4026 * for non-unix systems when there is a single argument (spaces don't
4027 * separate arguments then). */
4028 if (!eap->usefilter
4029 && eap->cmdidx != CMD_bang
4030 && eap->cmdidx != CMD_make
4031 && eap->cmdidx != CMD_grep
4032 && eap->cmdidx != CMD_grepadd
4033#ifndef UNIX
4034 && !(eap->argt & NOSPC)
4035#endif
4036 )
4037 {
4038 char_u *l;
4039#ifdef BACKSLASH_IN_FILENAME
4040 /* Don't escape a backslash here, because rem_backslash() doesn't
4041 * remove it later. */
4042 static char_u *nobslash = (char_u *)" \t\"|";
4043# define ESCAPE_CHARS nobslash
4044#else
4045# define ESCAPE_CHARS escape_chars
4046#endif
4047
4048 for (l = repl; *l; ++l)
4049 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4050 {
4051 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4052 if (l != NULL)
4053 {
4054 vim_free(repl);
4055 repl = l;
4056 }
4057 break;
4058 }
4059 }
4060
4061 /* For a shell command a '!' must be escaped. */
4062 if ((eap->usefilter || eap->cmdidx == CMD_bang)
4063 && vim_strchr(repl, '!') != NULL)
4064 {
4065 char_u *l;
4066
4067 l = vim_strsave_escaped(repl, (char_u *)"!");
4068 if (l != NULL)
4069 {
4070 vim_free(repl);
4071 repl = l;
4072 /* For a sh-like shell escape it another time. */
4073 if (strstr((char *)p_sh, "sh") != NULL)
4074 {
4075 l = vim_strsave_escaped(repl, (char_u *)"!");
4076 if (l != NULL)
4077 {
4078 vim_free(repl);
4079 repl = l;
4080 }
4081 }
4082 }
4083 }
4084
4085 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4086 vim_free(repl);
4087 if (p == NULL)
4088 return FAIL;
4089 }
4090
4091 /*
4092 * One file argument: Expand wildcards.
4093 * Don't do this with ":r !command" or ":w !command".
4094 */
4095 if ((eap->argt & NOSPC) && !eap->usefilter)
4096 {
4097 /*
4098 * May do this twice:
4099 * 1. Replace environment variables.
4100 * 2. Replace any other wildcards, remove backslashes.
4101 */
4102 for (n = 1; n <= 2; ++n)
4103 {
4104 if (n == 2)
4105 {
4106#ifdef UNIX
4107 /*
4108 * Only for Unix we check for more than one file name.
4109 * For other systems spaces are considered to be part
4110 * of the file name.
4111 * Only check here if there is no wildcard, otherwise
4112 * ExpandOne() will check for errors. This allows
4113 * ":e `ls ve*.c`" on Unix.
4114 */
4115 if (!has_wildcards)
4116 for (p = eap->arg; *p; ++p)
4117 {
4118 /* skip escaped characters */
4119 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4120 ++p;
4121 else if (vim_iswhite(*p))
4122 {
4123 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4124 return FAIL;
4125 }
4126 }
4127#endif
4128
4129 /*
4130 * Halve the number of backslashes (this is Vi compatible).
4131 * For Unix and OS/2, when wildcards are expanded, this is
4132 * done by ExpandOne() below.
4133 */
4134#if defined(UNIX) || defined(OS2)
4135 if (!has_wildcards)
4136#endif
4137 backslash_halve(eap->arg);
4138#ifdef MACOS_CLASSIC
4139 /*
4140 * translate unix-like path components
4141 */
4142 slash_n_colon_adjust(eap->arg);
4143#endif
4144 }
4145
4146 if (has_wildcards)
4147 {
4148 if (n == 1)
4149 {
4150 /*
4151 * First loop: May expand environment variables. This
4152 * can be done much faster with expand_env() than with
4153 * something else (e.g., calling a shell).
4154 * After expanding environment variables, check again
4155 * if there are still wildcards present.
4156 */
4157 if (vim_strchr(eap->arg, '$') != NULL
4158 || vim_strchr(eap->arg, '~') != NULL)
4159 {
4160 expand_env_esc(eap->arg, NameBuff, MAXPATHL, TRUE);
4161 has_wildcards = mch_has_wildcard(NameBuff);
4162 p = NameBuff;
4163 }
4164 else
4165 p = NULL;
4166 }
4167 else /* n == 2 */
4168 {
4169 expand_T xpc;
4170
4171 ExpandInit(&xpc);
4172 xpc.xp_context = EXPAND_FILES;
4173 p = ExpandOne(&xpc, eap->arg, NULL,
4174 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4175 WILD_EXPAND_FREE);
4176 ExpandCleanup(&xpc);
4177 if (p == NULL)
4178 return FAIL;
4179 }
4180 if (p != NULL)
4181 {
4182 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4183 p, cmdlinep);
4184 if (n == 2) /* p came from ExpandOne() */
4185 vim_free(p);
4186 }
4187 }
4188 }
4189 }
4190 return OK;
4191}
4192
4193/*
4194 * Replace part of the command line, keeping eap->cmd, eap->arg and
4195 * eap->nextcmd correct.
4196 * "src" points to the part that is to be replaced, of length "srclen".
4197 * "repl" is the replacement string.
4198 * Returns a pointer to the character after the replaced string.
4199 * Returns NULL for failure.
4200 */
4201 static char_u *
4202repl_cmdline(eap, src, srclen, repl, cmdlinep)
4203 exarg_T *eap;
4204 char_u *src;
4205 int srclen;
4206 char_u *repl;
4207 char_u **cmdlinep;
4208{
4209 int len;
4210 int i;
4211 char_u *new_cmdline;
4212
4213 /*
4214 * The new command line is build in new_cmdline[].
4215 * First allocate it.
4216 * Careful: a "+cmd" argument may have been NUL terminated.
4217 */
4218 len = (int)STRLEN(repl);
4219 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4220 if (eap->nextcmd)
4221 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4222 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4223 return NULL; /* out of memory! */
4224
4225 /*
4226 * Copy the stuff before the expanded part.
4227 * Copy the expanded stuff.
4228 * Copy what came after the expanded part.
4229 * Copy the next commands, if there are any.
4230 */
4231 i = (int)(src - *cmdlinep); /* length of part before match */
4232 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
4233 mch_memmove(new_cmdline + i, repl, (size_t)len);
4234 i += len; /* remember the end of the string */
4235 STRCPY(new_cmdline + i, src + srclen);
4236 src = new_cmdline + i; /* remember where to continue */
4237
4238 if (eap->nextcmd) /* append next command */
4239 {
4240 i = (int)STRLEN(new_cmdline) + 1;
4241 STRCPY(new_cmdline + i, eap->nextcmd);
4242 eap->nextcmd = new_cmdline + i;
4243 }
4244 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4245 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4246 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4247 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4248 vim_free(*cmdlinep);
4249 *cmdlinep = new_cmdline;
4250
4251 return src;
4252}
4253
4254/*
4255 * Check for '|' to separate commands and '"' to start comments.
4256 */
4257 void
4258separate_nextcmd(eap)
4259 exarg_T *eap;
4260{
4261 char_u *p;
4262
Bram Moolenaar86b68352004-12-27 21:59:20 +00004263#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004264 p = skip_grep_pat(eap);
4265#else
4266 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004267#endif
4268
4269 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
4271 if (*p == Ctrl_V)
4272 {
4273 if (eap->argt & (USECTRLV | XFILE))
4274 ++p; /* skip CTRL-V and next char */
4275 else
4276 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4277 if (*p == NUL) /* stop at NUL after CTRL-V */
4278 break;
4279 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004280
4281#ifdef FEAT_EVAL
4282 /* Skip over `=expr` when wildcards are expanded. */
4283 else if (p[0] == '`' && p[1] == '=')
4284 {
4285 p += 2;
4286 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004287 }
4288#endif
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 /* Check for '"': start of comment or '|': next command */
4291 /* :@" and :*" do not start a comment!
4292 * :redir @" doesn't either. */
4293 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4294 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4295 || p != eap->arg)
4296 && (eap->cmdidx != CMD_redir
4297 || p != eap->arg + 1 || p[-1] != '@'))
4298 || *p == '|' || *p == '\n')
4299 {
4300 /*
4301 * We remove the '\' before the '|', unless USECTRLV is used
4302 * AND 'b' is present in 'cpoptions'.
4303 */
4304 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4305 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4306 {
4307 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4308 --p;
4309 }
4310 else
4311 {
4312 eap->nextcmd = check_nextcmd(p);
4313 *p = NUL;
4314 break;
4315 }
4316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004318
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4320 del_trailing_spaces(eap->arg);
4321}
4322
4323/*
4324 * get + command from ex argument
4325 */
4326 static char_u *
4327getargcmd(argp)
4328 char_u **argp;
4329{
4330 char_u *arg = *argp;
4331 char_u *command = NULL;
4332
4333 if (*arg == '+') /* +[command] */
4334 {
4335 ++arg;
4336 if (vim_isspace(*arg))
4337 command = dollar_command;
4338 else
4339 {
4340 command = arg;
4341 arg = skip_cmd_arg(command, TRUE);
4342 if (*arg != NUL)
4343 *arg++ = NUL; /* terminate command with NUL */
4344 }
4345
4346 arg = skipwhite(arg); /* skip over spaces */
4347 *argp = arg;
4348 }
4349 return command;
4350}
4351
4352/*
4353 * Find end of "+command" argument. Skip over "\ " and "\\".
4354 */
4355 static char_u *
4356skip_cmd_arg(p, rembs)
4357 char_u *p;
4358 int rembs; /* TRUE to halve the number of backslashes */
4359{
4360 while (*p && !vim_isspace(*p))
4361 {
4362 if (*p == '\\' && p[1] != NUL)
4363 {
4364 if (rembs)
4365 mch_memmove(p, p + 1, STRLEN(p));
4366 else
4367 ++p;
4368 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004369 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 return p;
4372}
4373
4374/*
4375 * Get "++opt=arg" argument.
4376 * Return FAIL or OK.
4377 */
4378 static int
4379getargopt(eap)
4380 exarg_T *eap;
4381{
4382 char_u *arg = eap->arg + 2;
4383 int *pp = NULL;
4384#ifdef FEAT_MBYTE
4385 char_u *p;
4386#endif
4387
4388 /* ":edit ++[no]bin[ary] file" */
4389 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4390 {
4391 if (*arg == 'n')
4392 {
4393 arg += 2;
4394 eap->force_bin = FORCE_NOBIN;
4395 }
4396 else
4397 eap->force_bin = FORCE_BIN;
4398 if (!checkforcmd(&arg, "binary", 3))
4399 return FAIL;
4400 eap->arg = skipwhite(arg);
4401 return OK;
4402 }
4403
4404 if (STRNCMP(arg, "ff", 2) == 0)
4405 {
4406 arg += 2;
4407 pp = &eap->force_ff;
4408 }
4409 else if (STRNCMP(arg, "fileformat", 10) == 0)
4410 {
4411 arg += 10;
4412 pp = &eap->force_ff;
4413 }
4414#ifdef FEAT_MBYTE
4415 else if (STRNCMP(arg, "enc", 3) == 0)
4416 {
4417 arg += 3;
4418 pp = &eap->force_enc;
4419 }
4420 else if (STRNCMP(arg, "encoding", 8) == 0)
4421 {
4422 arg += 8;
4423 pp = &eap->force_enc;
4424 }
4425#endif
4426
4427 if (pp == NULL || *arg != '=')
4428 return FAIL;
4429
4430 ++arg;
4431 *pp = (int)(arg - eap->cmd);
4432 arg = skip_cmd_arg(arg, FALSE);
4433 eap->arg = skipwhite(arg);
4434 *arg = NUL;
4435
4436#ifdef FEAT_MBYTE
4437 if (pp == &eap->force_ff)
4438 {
4439#endif
4440 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4441 return FAIL;
4442#ifdef FEAT_MBYTE
4443 }
4444 else
4445 {
4446 /* Make 'fileencoding' lower case. */
4447 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4448 *p = TOLOWER_ASC(*p);
4449 }
4450#endif
4451
4452 return OK;
4453}
4454
4455/*
4456 * ":abbreviate" and friends.
4457 */
4458 static void
4459ex_abbreviate(eap)
4460 exarg_T *eap;
4461{
4462 do_exmap(eap, TRUE); /* almost the same as mapping */
4463}
4464
4465/*
4466 * ":map" and friends.
4467 */
4468 static void
4469ex_map(eap)
4470 exarg_T *eap;
4471{
4472 /*
4473 * If we are sourcing .exrc or .vimrc in current directory we
4474 * print the mappings for security reasons.
4475 */
4476 if (secure)
4477 {
4478 secure = 2;
4479 msg_outtrans(eap->cmd);
4480 msg_putchar('\n');
4481 }
4482 do_exmap(eap, FALSE);
4483}
4484
4485/*
4486 * ":unmap" and friends.
4487 */
4488 static void
4489ex_unmap(eap)
4490 exarg_T *eap;
4491{
4492 do_exmap(eap, FALSE);
4493}
4494
4495/*
4496 * ":mapclear" and friends.
4497 */
4498 static void
4499ex_mapclear(eap)
4500 exarg_T *eap;
4501{
4502 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4503}
4504
4505/*
4506 * ":abclear" and friends.
4507 */
4508 static void
4509ex_abclear(eap)
4510 exarg_T *eap;
4511{
4512 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4513}
4514
4515#ifdef FEAT_AUTOCMD
4516 static void
4517ex_autocmd(eap)
4518 exarg_T *eap;
4519{
4520 /*
4521 * Disallow auto commands from .exrc and .vimrc in current
4522 * directory for security reasons.
4523 */
4524 if (secure)
4525 {
4526 secure = 2;
4527 eap->errmsg = e_curdir;
4528 }
4529 else if (eap->cmdidx == CMD_autocmd)
4530 do_autocmd(eap->arg, eap->forceit);
4531 else
4532 do_augroup(eap->arg, eap->forceit);
4533}
4534
4535/*
4536 * ":doautocmd": Apply the automatic commands to the current buffer.
4537 */
4538 static void
4539ex_doautocmd(eap)
4540 exarg_T *eap;
4541{
4542 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004543 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544}
4545#endif
4546
4547#ifdef FEAT_LISTCMDS
4548/*
4549 * :[N]bunload[!] [N] [bufname] unload buffer
4550 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4551 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4552 */
4553 static void
4554ex_bunload(eap)
4555 exarg_T *eap;
4556{
4557 eap->errmsg = do_bufdel(
4558 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4559 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4560 : DOBUF_UNLOAD, eap->arg,
4561 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4562}
4563
4564/*
4565 * :[N]buffer [N] to buffer N
4566 * :[N]sbuffer [N] to buffer N
4567 */
4568 static void
4569ex_buffer(eap)
4570 exarg_T *eap;
4571{
4572 if (*eap->arg)
4573 eap->errmsg = e_trailing;
4574 else
4575 {
4576 if (eap->addr_count == 0) /* default is current buffer */
4577 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4578 else
4579 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4580 }
4581}
4582
4583/*
4584 * :[N]bmodified [N] to next mod. buffer
4585 * :[N]sbmodified [N] to next mod. buffer
4586 */
4587 static void
4588ex_bmodified(eap)
4589 exarg_T *eap;
4590{
4591 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4592}
4593
4594/*
4595 * :[N]bnext [N] to next buffer
4596 * :[N]sbnext [N] split and to next buffer
4597 */
4598 static void
4599ex_bnext(eap)
4600 exarg_T *eap;
4601{
4602 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4603}
4604
4605/*
4606 * :[N]bNext [N] to previous buffer
4607 * :[N]bprevious [N] to previous buffer
4608 * :[N]sbNext [N] split and to previous buffer
4609 * :[N]sbprevious [N] split and to previous buffer
4610 */
4611 static void
4612ex_bprevious(eap)
4613 exarg_T *eap;
4614{
4615 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4616}
4617
4618/*
4619 * :brewind to first buffer
4620 * :bfirst to first buffer
4621 * :sbrewind split and to first buffer
4622 * :sbfirst split and to first buffer
4623 */
4624 static void
4625ex_brewind(eap)
4626 exarg_T *eap;
4627{
4628 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4629}
4630
4631/*
4632 * :blast to last buffer
4633 * :sblast split and to last buffer
4634 */
4635 static void
4636ex_blast(eap)
4637 exarg_T *eap;
4638{
4639 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4640}
4641#endif
4642
4643 int
4644ends_excmd(c)
4645 int c;
4646{
4647 return (c == NUL || c == '|' || c == '"' || c == '\n');
4648}
4649
4650#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4651 || defined(PROTO)
4652/*
4653 * Return the next command, after the first '|' or '\n'.
4654 * Return NULL if not found.
4655 */
4656 char_u *
4657find_nextcmd(p)
4658 char_u *p;
4659{
4660 while (*p != '|' && *p != '\n')
4661 {
4662 if (*p == NUL)
4663 return NULL;
4664 ++p;
4665 }
4666 return (p + 1);
4667}
4668#endif
4669
4670/*
4671 * Check if *p is a separator between Ex commands.
4672 * Return NULL if it isn't, (p + 1) if it is.
4673 */
4674 char_u *
4675check_nextcmd(p)
4676 char_u *p;
4677{
4678 p = skipwhite(p);
4679 if (*p == '|' || *p == '\n')
4680 return (p + 1);
4681 else
4682 return NULL;
4683}
4684
4685/*
4686 * - if there are more files to edit
4687 * - and this is the last window
4688 * - and forceit not used
4689 * - and not repeated twice on a row
4690 * return FAIL and give error message if 'message' TRUE
4691 * return OK otherwise
4692 */
4693 static int
4694check_more(message, forceit)
4695 int message; /* when FALSE check only, no messages */
4696 int forceit;
4697{
4698 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4699
4700 if (!forceit && only_one_window() && ARGCOUNT > 1 && !arg_had_last
4701 && n >= 0 && quitmore == 0)
4702 {
4703 if (message)
4704 {
4705#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4706 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4707 {
4708 char_u buff[IOSIZE];
4709
4710 if (n == 1)
4711 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4712 else
4713 sprintf((char *)buff,
4714 _("%d more files to edit. Quit anyway?"), n);
4715 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4716 return OK;
4717 return FAIL;
4718 }
4719#endif
4720 if (n == 1)
4721 EMSG(_("E173: 1 more file to edit"));
4722 else
4723 EMSGN(_("E173: %ld more files to edit"), n);
4724 quitmore = 2; /* next try to quit is allowed */
4725 }
4726 return FAIL;
4727 }
4728 return OK;
4729}
4730
4731#ifdef FEAT_CMDL_COMPL
4732/*
4733 * Function given to ExpandGeneric() to obtain the list of command names.
4734 */
4735/*ARGSUSED*/
4736 char_u *
4737get_command_name(xp, idx)
4738 expand_T *xp;
4739 int idx;
4740{
4741 if (idx >= (int)CMD_SIZE)
4742# ifdef FEAT_USR_CMDS
4743 return get_user_command_name(idx);
4744# else
4745 return NULL;
4746# endif
4747 return cmdnames[idx].cmd_name;
4748}
4749#endif
4750
4751#if defined(FEAT_USR_CMDS) || defined(PROTO)
4752static 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));
4753static void uc_list __ARGS((char_u *name, size_t name_len));
4754static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4755static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4756static 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));
4757
4758 static int
4759uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4760 char_u *name;
4761 size_t name_len;
4762 char_u *rep;
4763 long argt;
4764 long def;
4765 int flags;
4766 int compl;
4767 char_u *compl_arg;
4768 int force;
4769{
4770 ucmd_T *cmd = NULL;
4771 char_u *p;
4772 int i;
4773 int cmp = 1;
4774 char_u *rep_buf = NULL;
4775 garray_T *gap;
4776
4777 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4778 if (rep_buf == NULL)
4779 {
4780 /* Can't replace termcodes - try using the string as is */
4781 rep_buf = vim_strsave(rep);
4782
4783 /* Give up if out of memory */
4784 if (rep_buf == NULL)
4785 return FAIL;
4786 }
4787
4788 /* get address of growarray: global or in curbuf */
4789 if (flags & UC_BUFFER)
4790 {
4791 gap = &curbuf->b_ucmds;
4792 if (gap->ga_itemsize == 0)
4793 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4794 }
4795 else
4796 gap = &ucmds;
4797
4798 /* Search for the command in the already defined commands. */
4799 for (i = 0; i < gap->ga_len; ++i)
4800 {
4801 size_t len;
4802
4803 cmd = USER_CMD_GA(gap, i);
4804 len = STRLEN(cmd->uc_name);
4805 cmp = STRNCMP(name, cmd->uc_name, name_len);
4806 if (cmp == 0)
4807 {
4808 if (name_len < len)
4809 cmp = -1;
4810 else if (name_len > len)
4811 cmp = 1;
4812 }
4813
4814 if (cmp == 0)
4815 {
4816 if (!force)
4817 {
4818 EMSG(_("E174: Command already exists: add ! to replace it"));
4819 goto fail;
4820 }
4821
4822 vim_free(cmd->uc_rep);
4823 cmd->uc_rep = 0;
4824 break;
4825 }
4826
4827 /* Stop as soon as we pass the name to add */
4828 if (cmp < 0)
4829 break;
4830 }
4831
4832 /* Extend the array unless we're replacing an existing command */
4833 if (cmp != 0)
4834 {
4835 if (ga_grow(gap, 1) != OK)
4836 goto fail;
4837 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
4838 goto fail;
4839
4840 cmd = USER_CMD_GA(gap, i);
4841 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
4842
4843 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
4845 cmd->uc_name = p;
4846 }
4847
4848 cmd->uc_rep = rep_buf;
4849 cmd->uc_argt = argt;
4850 cmd->uc_def = def;
4851 cmd->uc_compl = compl;
4852#ifdef FEAT_EVAL
4853 cmd->uc_scriptID = current_SID;
4854# ifdef FEAT_CMDL_COMPL
4855 cmd->uc_compl_arg = compl_arg;
4856# endif
4857#endif
4858
4859 return OK;
4860
4861fail:
4862 vim_free(rep_buf);
4863#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4864 vim_free(compl_arg);
4865#endif
4866 return FAIL;
4867}
4868
4869/*
4870 * List of names for completion for ":command" with the EXPAND_ flag.
4871 * Must be alphabetical for completion.
4872 */
4873static struct
4874{
4875 int expand;
4876 char *name;
4877} command_complete[] =
4878{
4879 {EXPAND_AUGROUP, "augroup"},
4880 {EXPAND_BUFFERS, "buffer"},
4881 {EXPAND_COMMANDS, "command"},
4882#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4883 {EXPAND_USER_DEFINED, "custom"},
4884#endif
4885 {EXPAND_DIRECTORIES, "dir"},
4886 {EXPAND_ENV_VARS, "environment"},
4887 {EXPAND_EVENTS, "event"},
4888 {EXPAND_EXPRESSION, "expression"},
4889 {EXPAND_FILES, "file"},
4890 {EXPAND_FUNCTIONS, "function"},
4891 {EXPAND_HELP, "help"},
4892 {EXPAND_HIGHLIGHT, "highlight"},
4893 {EXPAND_MAPPINGS, "mapping"},
4894 {EXPAND_MENUS, "menu"},
4895 {EXPAND_SETTINGS, "option"},
4896 {EXPAND_TAGS, "tag"},
4897 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
4898 {EXPAND_USER_VARS, "var"},
4899 {0, NULL}
4900};
4901
4902 static void
4903uc_list(name, name_len)
4904 char_u *name;
4905 size_t name_len;
4906{
4907 int i, j;
4908 int found = FALSE;
4909 ucmd_T *cmd;
4910 int len;
4911 long a;
4912 garray_T *gap;
4913
4914 gap = &curbuf->b_ucmds;
4915 for (;;)
4916 {
4917 for (i = 0; i < gap->ga_len; ++i)
4918 {
4919 cmd = USER_CMD_GA(gap, i);
4920 a = cmd->uc_argt;
4921
4922 /* Skip commands which don't match the requested prefix */
4923 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
4924 continue;
4925
4926 /* Put out the title first time */
4927 if (!found)
4928 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
4929 found = TRUE;
4930 msg_putchar('\n');
4931 if (got_int)
4932 break;
4933
4934 /* Special cases */
4935 msg_putchar(a & BANG ? '!' : ' ');
4936 msg_putchar(a & REGSTR ? '"' : ' ');
4937 msg_putchar(gap != &ucmds ? 'b' : ' ');
4938 msg_putchar(' ');
4939
4940 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
4941 len = (int)STRLEN(cmd->uc_name) + 4;
4942
4943 do {
4944 msg_putchar(' ');
4945 ++len;
4946 } while (len < 16);
4947
4948 len = 0;
4949
4950 /* Arguments */
4951 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
4952 {
4953 case 0: IObuff[len++] = '0'; break;
4954 case (EXTRA): IObuff[len++] = '*'; break;
4955 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
4956 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
4957 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
4958 }
4959
4960 do {
4961 IObuff[len++] = ' ';
4962 } while (len < 5);
4963
4964 /* Range */
4965 if (a & (RANGE|COUNT))
4966 {
4967 if (a & COUNT)
4968 {
4969 /* -count=N */
4970 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
4971 len += (int)STRLEN(IObuff + len);
4972 }
4973 else if (a & DFLALL)
4974 IObuff[len++] = '%';
4975 else if (cmd->uc_def >= 0)
4976 {
4977 /* -range=N */
4978 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
4979 len += (int)STRLEN(IObuff + len);
4980 }
4981 else
4982 IObuff[len++] = '.';
4983 }
4984
4985 do {
4986 IObuff[len++] = ' ';
4987 } while (len < 11);
4988
4989 /* Completion */
4990 for (j = 0; command_complete[j].expand != 0; ++j)
4991 if (command_complete[j].expand == cmd->uc_compl)
4992 {
4993 STRCPY(IObuff + len, command_complete[j].name);
4994 len += (int)STRLEN(IObuff + len);
4995 break;
4996 }
4997
4998 do {
4999 IObuff[len++] = ' ';
5000 } while (len < 21);
5001
5002 IObuff[len] = '\0';
5003 msg_outtrans(IObuff);
5004
5005 msg_outtrans_special(cmd->uc_rep, FALSE);
5006 out_flush();
5007 ui_breakcheck();
5008 if (got_int)
5009 break;
5010 }
5011 if (gap == &ucmds || i < gap->ga_len)
5012 break;
5013 gap = &ucmds;
5014 }
5015
5016 if (!found)
5017 MSG(_("No user-defined commands found"));
5018}
5019
5020 static char_u *
5021uc_fun_cmd()
5022{
5023 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5024 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5025 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5026 0xb9, 0x7f, 0};
5027 int i;
5028
5029 for (i = 0; fcmd[i]; ++i)
5030 IObuff[i] = fcmd[i] - 0x40;
5031 IObuff[i] = 0;
5032 return IObuff;
5033}
5034
5035 static int
5036uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5037 char_u *attr;
5038 size_t len;
5039 long *argt;
5040 long *def;
5041 int *flags;
5042 int *compl;
5043 char_u **compl_arg;
5044{
5045 char_u *p;
5046
5047 if (len == 0)
5048 {
5049 EMSG(_("E175: No attribute specified"));
5050 return FAIL;
5051 }
5052
5053 /* First, try the simple attributes (no arguments) */
5054 if (STRNICMP(attr, "bang", len) == 0)
5055 *argt |= BANG;
5056 else if (STRNICMP(attr, "buffer", len) == 0)
5057 *flags |= UC_BUFFER;
5058 else if (STRNICMP(attr, "register", len) == 0)
5059 *argt |= REGSTR;
5060 else if (STRNICMP(attr, "bar", len) == 0)
5061 *argt |= TRLBAR;
5062 else
5063 {
5064 int i;
5065 char_u *val = NULL;
5066 size_t vallen = 0;
5067 size_t attrlen = len;
5068
5069 /* Look for the attribute name - which is the part before any '=' */
5070 for (i = 0; i < (int)len; ++i)
5071 {
5072 if (attr[i] == '=')
5073 {
5074 val = &attr[i + 1];
5075 vallen = len - i - 1;
5076 attrlen = i;
5077 break;
5078 }
5079 }
5080
5081 if (STRNICMP(attr, "nargs", attrlen) == 0)
5082 {
5083 if (vallen == 1)
5084 {
5085 if (*val == '0')
5086 /* Do nothing - this is the default */;
5087 else if (*val == '1')
5088 *argt |= (EXTRA | NOSPC | NEEDARG);
5089 else if (*val == '*')
5090 *argt |= EXTRA;
5091 else if (*val == '?')
5092 *argt |= (EXTRA | NOSPC);
5093 else if (*val == '+')
5094 *argt |= (EXTRA | NEEDARG);
5095 else
5096 goto wrong_nargs;
5097 }
5098 else
5099 {
5100wrong_nargs:
5101 EMSG(_("E176: Invalid number of arguments"));
5102 return FAIL;
5103 }
5104 }
5105 else if (STRNICMP(attr, "range", attrlen) == 0)
5106 {
5107 *argt |= RANGE;
5108 if (vallen == 1 && *val == '%')
5109 *argt |= DFLALL;
5110 else if (val != NULL)
5111 {
5112 p = val;
5113 if (*def >= 0)
5114 {
5115two_count:
5116 EMSG(_("E177: Count cannot be specified twice"));
5117 return FAIL;
5118 }
5119
5120 *def = getdigits(&p);
5121 *argt |= (ZEROR | NOTADR);
5122
5123 if (p != val + vallen || vallen == 0)
5124 {
5125invalid_count:
5126 EMSG(_("E178: Invalid default value for count"));
5127 return FAIL;
5128 }
5129 }
5130 }
5131 else if (STRNICMP(attr, "count", attrlen) == 0)
5132 {
5133 *argt |= (COUNT | ZEROR | NOTADR);
5134
5135 if (val != NULL)
5136 {
5137 p = val;
5138 if (*def >= 0)
5139 goto two_count;
5140
5141 *def = getdigits(&p);
5142
5143 if (p != val + vallen)
5144 goto invalid_count;
5145 }
5146
5147 if (*def < 0)
5148 *def = 0;
5149 }
5150 else if (STRNICMP(attr, "complete", attrlen) == 0)
5151 {
5152 char_u *arg = NULL;
5153 size_t arglen = 0;
5154
5155 if (val == NULL)
5156 {
5157 EMSG(_("E179: argument required for complete"));
5158 return FAIL;
5159 }
5160 /* Look for any argument part - which is the part after any ',' */
5161 for (i = 0; i < (int)vallen; ++i)
5162 {
5163 if (val[i] == ',')
5164 {
5165 arg = &val[i + 1];
5166 arglen = vallen - i - 1;
5167 vallen = i;
5168 break;
5169 }
5170 }
5171
5172 for (i = 0; command_complete[i].expand != 0; ++i)
5173 {
5174 if (STRLEN(command_complete[i].name) == vallen
5175 && STRNCMP(val, command_complete[i].name, vallen) == 0)
5176 {
5177 *compl = command_complete[i].expand;
5178 if (command_complete[i].expand == EXPAND_BUFFERS)
5179 *argt |= BUFNAME;
5180 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5181 || command_complete[i].expand == EXPAND_FILES)
5182 *argt |= XFILE;
5183 break;
5184 }
5185 }
5186
5187 if (command_complete[i].expand == 0)
5188 {
5189 EMSG2(_("E180: Invalid complete value: %s"), val);
5190 return FAIL;
5191 }
5192#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5193 if (*compl != EXPAND_USER_DEFINED && arg != NULL)
5194#else
5195 if (arg != NULL)
5196#endif
5197 {
5198 EMSG(_("E468: Completion argument only allowed for custom completion"));
5199 return FAIL;
5200 }
5201#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5202 if (*compl == EXPAND_USER_DEFINED && arg == NULL)
5203 {
5204 EMSG(_("E467: Custom completion requires a function argument"));
5205 return FAIL;
5206 }
5207 if (arg != NULL)
5208 *compl_arg = vim_strnsave(arg, (int)arglen);
5209#endif
5210 }
5211 else
5212 {
5213 char_u ch = attr[len];
5214 attr[len] = '\0';
5215 EMSG2(_("E181: Invalid attribute: %s"), attr);
5216 attr[len] = ch;
5217 return FAIL;
5218 }
5219 }
5220
5221 return OK;
5222}
5223
5224 static void
5225ex_command(eap)
5226 exarg_T *eap;
5227{
5228 char_u *name;
5229 char_u *end;
5230 char_u *p;
5231 long argt = 0;
5232 long def = -1;
5233 int flags = 0;
5234 int compl = EXPAND_NOTHING;
5235 char_u *compl_arg = NULL;
5236 int has_attr = (eap->arg[0] == '-');
5237
5238 p = eap->arg;
5239
5240 /* Check for attributes */
5241 while (*p == '-')
5242 {
5243 ++p;
5244 end = skiptowhite(p);
5245 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5246 == FAIL)
5247 return;
5248 p = skipwhite(end);
5249 }
5250
5251 /* Get the name (if any) and skip to the following argument */
5252 name = p;
5253 if (ASCII_ISALPHA(*p))
5254 while (ASCII_ISALNUM(*p))
5255 ++p;
5256 if (!ends_excmd(*p) && !vim_iswhite(*p))
5257 {
5258 EMSG(_("E182: Invalid command name"));
5259 return;
5260 }
5261 end = p;
5262
5263 /* If there is nothing after the name, and no attributes were specified,
5264 * we are listing commands
5265 */
5266 p = skipwhite(end);
5267 if (!has_attr && ends_excmd(*p))
5268 {
5269 uc_list(name, end - name);
5270 }
5271 else if (!ASCII_ISUPPER(*name))
5272 {
5273 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5274 return;
5275 }
5276 else
5277 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5278 eap->forceit);
5279}
5280
5281/*
5282 * ":comclear"
5283 */
5284/*ARGSUSED*/
5285/*
5286 * Clear all user commands, global and for current buffer.
5287 */
5288 static void
5289ex_comclear(eap)
5290 exarg_T *eap;
5291{
5292 uc_clear(&ucmds);
5293 uc_clear(&curbuf->b_ucmds);
5294}
5295
5296/*
5297 * Clear all user commands for "gap".
5298 */
5299 void
5300uc_clear(gap)
5301 garray_T *gap;
5302{
5303 int i;
5304 ucmd_T *cmd;
5305
5306 for (i = 0; i < gap->ga_len; ++i)
5307 {
5308 cmd = USER_CMD_GA(gap, i);
5309 vim_free(cmd->uc_name);
5310 vim_free(cmd->uc_rep);
5311# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5312 vim_free(cmd->uc_compl_arg);
5313# endif
5314 }
5315 ga_clear(gap);
5316}
5317
5318 static void
5319ex_delcommand(eap)
5320 exarg_T *eap;
5321{
5322 int i = 0;
5323 ucmd_T *cmd = NULL;
5324 int cmp = -1;
5325 garray_T *gap;
5326
5327 gap = &curbuf->b_ucmds;
5328 for (;;)
5329 {
5330 for (i = 0; i < gap->ga_len; ++i)
5331 {
5332 cmd = USER_CMD_GA(gap, i);
5333 cmp = STRCMP(eap->arg, cmd->uc_name);
5334 if (cmp <= 0)
5335 break;
5336 }
5337 if (gap == &ucmds || cmp == 0)
5338 break;
5339 gap = &ucmds;
5340 }
5341
5342 if (cmp != 0)
5343 {
5344 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5345 return;
5346 }
5347
5348 vim_free(cmd->uc_name);
5349 vim_free(cmd->uc_rep);
5350# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5351 vim_free(cmd->uc_compl_arg);
5352# endif
5353
5354 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356 if (i < gap->ga_len)
5357 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5358}
5359
5360 static char_u *
5361uc_split_args(arg, lenp)
5362 char_u *arg;
5363 size_t *lenp;
5364{
5365 char_u *buf;
5366 char_u *p;
5367 char_u *q;
5368 int len;
5369
5370 /* Precalculate length */
5371 p = arg;
5372 len = 2; /* Initial and final quotes */
5373
5374 while (*p)
5375 {
5376 if (p[0] == '\\' && vim_iswhite(p[1]))
5377 {
5378 len += 1;
5379 p += 2;
5380 }
5381 else if (*p == '\\' || *p == '"')
5382 {
5383 len += 2;
5384 p += 1;
5385 }
5386 else if (vim_iswhite(*p))
5387 {
5388 p = skipwhite(p);
5389 if (*p == NUL)
5390 break;
5391 len += 3; /* "," */
5392 }
5393 else
5394 {
5395 ++len;
5396 ++p;
5397 }
5398 }
5399
5400 buf = alloc(len + 1);
5401 if (buf == NULL)
5402 {
5403 *lenp = 0;
5404 return buf;
5405 }
5406
5407 p = arg;
5408 q = buf;
5409 *q++ = '"';
5410 while (*p)
5411 {
5412 if (p[0] == '\\' && vim_iswhite(p[1]))
5413 {
5414 *q++ = p[1];
5415 p += 2;
5416 }
5417 else if (*p == '\\' || *p == '"')
5418 {
5419 *q++ = '\\';
5420 *q++ = *p++;
5421 }
5422 else if (vim_iswhite(*p))
5423 {
5424 p = skipwhite(p);
5425 if (*p == NUL)
5426 break;
5427 *q++ = '"';
5428 *q++ = ',';
5429 *q++ = '"';
5430 }
5431 else
5432 {
5433 *q++ = *p++;
5434 }
5435 }
5436 *q++ = '"';
5437 *q = 0;
5438
5439 *lenp = len;
5440 return buf;
5441}
5442
5443/*
5444 * Check for a <> code in a user command.
5445 * "code" points to the '<'. "len" the length of the <> (inclusive).
5446 * "buf" is where the result is to be added.
5447 * "split_buf" points to a buffer used for splitting, caller should free it.
5448 * "split_len" is the length of what "split_buf" contains.
5449 * Returns the length of the replacement, which has been added to "buf".
5450 * Returns -1 if there was no match, and only the "<" has been copied.
5451 */
5452 static size_t
5453uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5454 char_u *code;
5455 size_t len;
5456 char_u *buf;
5457 ucmd_T *cmd; /* the user command we're expanding */
5458 exarg_T *eap; /* ex arguments */
5459 char_u **split_buf;
5460 size_t *split_len;
5461{
5462 size_t result = 0;
5463 char_u *p = code + 1;
5464 size_t l = len - 2;
5465 int quote = 0;
5466 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5467 ct_LT, ct_NONE } type = ct_NONE;
5468
5469 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5470 {
5471 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5472 p += 2;
5473 l -= 2;
5474 }
5475
5476 if (l < 1)
5477 type = ct_NONE;
5478 else if (STRNICMP(p, "args", l) == 0)
5479 type = ct_ARGS;
5480 else if (STRNICMP(p, "bang", l) == 0)
5481 type = ct_BANG;
5482 else if (STRNICMP(p, "count", l) == 0)
5483 type = ct_COUNT;
5484 else if (STRNICMP(p, "line1", l) == 0)
5485 type = ct_LINE1;
5486 else if (STRNICMP(p, "line2", l) == 0)
5487 type = ct_LINE2;
5488 else if (STRNICMP(p, "lt", l) == 0)
5489 type = ct_LT;
5490 else if (STRNICMP(p, "register", l) == 0)
5491 type = ct_REGISTER;
5492
5493 switch (type)
5494 {
5495 case ct_ARGS:
5496 /* Simple case first */
5497 if (*eap->arg == NUL)
5498 {
5499 if (quote == 1)
5500 {
5501 result = 2;
5502 if (buf != NULL)
5503 STRCPY(buf, "''");
5504 }
5505 else
5506 result = 0;
5507 break;
5508 }
5509
5510 /* When specified there is a single argument don't split it.
5511 * Works for ":Cmd %" when % is "a b c". */
5512 if ((eap->argt & NOSPC) && quote == 2)
5513 quote = 1;
5514
5515 switch (quote)
5516 {
5517 case 0: /* No quoting, no splitting */
5518 result = STRLEN(eap->arg);
5519 if (buf != NULL)
5520 STRCPY(buf, eap->arg);
5521 break;
5522 case 1: /* Quote, but don't split */
5523 result = STRLEN(eap->arg) + 2;
5524 for (p = eap->arg; *p; ++p)
5525 {
5526 if (*p == '\\' || *p == '"')
5527 ++result;
5528 }
5529
5530 if (buf != NULL)
5531 {
5532 *buf++ = '"';
5533 for (p = eap->arg; *p; ++p)
5534 {
5535 if (*p == '\\' || *p == '"')
5536 *buf++ = '\\';
5537 *buf++ = *p;
5538 }
5539 *buf = '"';
5540 }
5541
5542 break;
5543 case 2: /* Quote and split */
5544 /* This is hard, so only do it once, and cache the result */
5545 if (*split_buf == NULL)
5546 *split_buf = uc_split_args(eap->arg, split_len);
5547
5548 result = *split_len;
5549 if (buf != NULL && result != 0)
5550 STRCPY(buf, *split_buf);
5551
5552 break;
5553 }
5554 break;
5555
5556 case ct_BANG:
5557 result = eap->forceit ? 1 : 0;
5558 if (quote)
5559 result += 2;
5560 if (buf != NULL)
5561 {
5562 if (quote)
5563 *buf++ = '"';
5564 if (eap->forceit)
5565 *buf++ = '!';
5566 if (quote)
5567 *buf = '"';
5568 }
5569 break;
5570
5571 case ct_LINE1:
5572 case ct_LINE2:
5573 case ct_COUNT:
5574 {
5575 char num_buf[20];
5576 long num = (type == ct_LINE1) ? eap->line1 :
5577 (type == ct_LINE2) ? eap->line2 :
5578 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5579 size_t num_len;
5580
5581 sprintf(num_buf, "%ld", num);
5582 num_len = STRLEN(num_buf);
5583 result = num_len;
5584
5585 if (quote)
5586 result += 2;
5587
5588 if (buf != NULL)
5589 {
5590 if (quote)
5591 *buf++ = '"';
5592 STRCPY(buf, num_buf);
5593 buf += num_len;
5594 if (quote)
5595 *buf = '"';
5596 }
5597
5598 break;
5599 }
5600
5601 case ct_REGISTER:
5602 result = eap->regname ? 1 : 0;
5603 if (quote)
5604 result += 2;
5605 if (buf != NULL)
5606 {
5607 if (quote)
5608 *buf++ = '\'';
5609 if (eap->regname)
5610 *buf++ = eap->regname;
5611 if (quote)
5612 *buf = '\'';
5613 }
5614 break;
5615
5616 case ct_LT:
5617 result = 1;
5618 if (buf != NULL)
5619 *buf = '<';
5620 break;
5621
5622 default:
5623 /* Not recognized: just copy the '<' and return -1. */
5624 result = (size_t)-1;
5625 if (buf != NULL)
5626 *buf = '<';
5627 break;
5628 }
5629
5630 return result;
5631}
5632
5633 static void
5634do_ucmd(eap)
5635 exarg_T *eap;
5636{
5637 char_u *buf;
5638 char_u *p;
5639 char_u *q;
5640
5641 char_u *start;
5642 char_u *end;
5643 size_t len, totlen;
5644
5645 size_t split_len = 0;
5646 char_u *split_buf = NULL;
5647 ucmd_T *cmd;
5648#ifdef FEAT_EVAL
5649 scid_T save_current_SID = current_SID;
5650#endif
5651
5652 if (eap->cmdidx == CMD_USER)
5653 cmd = USER_CMD(eap->useridx);
5654 else
5655 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5656
5657 /*
5658 * Replace <> in the command by the arguments.
5659 */
5660 buf = NULL;
5661 for (;;)
5662 {
5663 p = cmd->uc_rep;
5664 q = buf;
5665 totlen = 0;
5666 while ((start = vim_strchr(p, '<')) != NULL
5667 && (end = vim_strchr(start + 1, '>')) != NULL)
5668 {
5669 /* Include the '>' */
5670 ++end;
5671
5672 /* Take everything up to the '<' */
5673 len = start - p;
5674 if (buf == NULL)
5675 totlen += len;
5676 else
5677 {
5678 mch_memmove(q, p, len);
5679 q += len;
5680 }
5681
5682 len = uc_check_code(start, end - start, q, cmd, eap,
5683 &split_buf, &split_len);
5684 if (len == (size_t)-1)
5685 {
5686 /* no match, continue after '<' */
5687 p = start + 1;
5688 len = 1;
5689 }
5690 else
5691 p = end;
5692 if (buf == NULL)
5693 totlen += len;
5694 else
5695 q += len;
5696 }
5697 if (buf != NULL) /* second time here, finished */
5698 {
5699 STRCPY(q, p);
5700 break;
5701 }
5702
5703 totlen += STRLEN(p); /* Add on the trailing characters */
5704 buf = alloc((unsigned)(totlen + 1));
5705 if (buf == NULL)
5706 {
5707 vim_free(split_buf);
5708 return;
5709 }
5710 }
5711
5712#ifdef FEAT_EVAL
5713 current_SID = cmd->uc_scriptID;
5714#endif
5715 (void)do_cmdline(buf, eap->getline, eap->cookie,
5716 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5717#ifdef FEAT_EVAL
5718 current_SID = save_current_SID;
5719#endif
5720 vim_free(buf);
5721 vim_free(split_buf);
5722}
5723
5724# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5725 static char_u *
5726get_user_command_name(idx)
5727 int idx;
5728{
5729 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5730}
5731
5732/*
5733 * Function given to ExpandGeneric() to obtain the list of user command names.
5734 */
5735/*ARGSUSED*/
5736 char_u *
5737get_user_commands(xp, idx)
5738 expand_T *xp;
5739 int idx;
5740{
5741 if (idx < curbuf->b_ucmds.ga_len)
5742 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5743 idx -= curbuf->b_ucmds.ga_len;
5744 if (idx < ucmds.ga_len)
5745 return USER_CMD(idx)->uc_name;
5746 return NULL;
5747}
5748
5749/*
5750 * Function given to ExpandGeneric() to obtain the list of user command
5751 * attributes.
5752 */
5753/*ARGSUSED*/
5754 char_u *
5755get_user_cmd_flags(xp, idx)
5756 expand_T *xp;
5757 int idx;
5758{
5759 static char *user_cmd_flags[] =
5760 {"bang", "bar", "buffer", "complete", "count",
5761 "nargs", "range", "register"};
5762
5763 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5764 return NULL;
5765 return (char_u *)user_cmd_flags[idx];
5766}
5767
5768/*
5769 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5770 */
5771/*ARGSUSED*/
5772 char_u *
5773get_user_cmd_nargs(xp, idx)
5774 expand_T *xp;
5775 int idx;
5776{
5777 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5778
5779 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5780 return NULL;
5781 return (char_u *)user_cmd_nargs[idx];
5782}
5783
5784/*
5785 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5786 */
5787/*ARGSUSED*/
5788 char_u *
5789get_user_cmd_complete(xp, idx)
5790 expand_T *xp;
5791 int idx;
5792{
5793 return (char_u *)command_complete[idx].name;
5794}
5795# endif /* FEAT_CMDL_COMPL */
5796
5797#endif /* FEAT_USR_CMDS */
5798
5799 static void
5800ex_colorscheme(eap)
5801 exarg_T *eap;
5802{
5803 if (load_colors(eap->arg) == FAIL)
5804 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
5805}
5806
5807 static void
5808ex_highlight(eap)
5809 exarg_T *eap;
5810{
5811 if (*eap->arg == NUL && eap->cmd[2] == '!')
5812 MSG(_("Greetings, Vim user!"));
5813 do_highlight(eap->arg, eap->forceit, FALSE);
5814}
5815
5816
5817/*
5818 * Call this function if we thought we were going to exit, but we won't
5819 * (because of an error). May need to restore the terminal mode.
5820 */
5821 void
5822not_exiting()
5823{
5824 exiting = FALSE;
5825 settmode(TMODE_RAW);
5826}
5827
5828/*
5829 * ":quit": quit current window, quit Vim if closed the last window.
5830 */
5831 static void
5832ex_quit(eap)
5833 exarg_T *eap;
5834{
5835#ifdef FEAT_CMDWIN
5836 if (cmdwin_type != 0)
5837 {
5838 cmdwin_result = Ctrl_C;
5839 return;
5840 }
5841#endif
5842
5843#ifdef FEAT_NETBEANS_INTG
5844 netbeansForcedQuit = eap->forceit;
5845#endif
5846
5847 /*
5848 * If there are more files or windows we won't exit.
5849 */
5850 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5851 exiting = TRUE;
5852 if ((!P_HID(curbuf)
5853 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
5854 || check_more(TRUE, eap->forceit) == FAIL
5855 || (only_one_window() && check_changed_any(eap->forceit)))
5856 {
5857 not_exiting();
5858 }
5859 else
5860 {
5861#ifdef FEAT_WINDOWS
5862 if (only_one_window()) /* quit last window */
5863#endif
5864 getout(0);
5865#ifdef FEAT_WINDOWS
5866# ifdef FEAT_GUI
5867 need_mouse_correct = TRUE;
5868# endif
5869 /* close window; may free buffer */
5870 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
5871#endif
5872 }
5873}
5874
5875/*
5876 * ":cquit".
5877 */
5878/*ARGSUSED*/
5879 static void
5880ex_cquit(eap)
5881 exarg_T *eap;
5882{
5883 getout(1); /* this does not always pass on the exit code to the Manx
5884 compiler. why? */
5885}
5886
5887/*
5888 * ":qall": try to quit all windows
5889 */
5890 static void
5891ex_quit_all(eap)
5892 exarg_T *eap;
5893{
5894# ifdef FEAT_CMDWIN
5895 if (cmdwin_type != 0)
5896 {
5897 if (eap->forceit)
5898 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5899 else
5900 cmdwin_result = K_XF2;
5901 return;
5902 }
5903# endif
5904 exiting = TRUE;
5905 if (eap->forceit || !check_changed_any(FALSE))
5906 getout(0);
5907 not_exiting();
5908}
5909
5910#ifdef FEAT_WINDOWS
5911/*
5912 * ":close": close current window, unless it is the last one
5913 */
5914 static void
5915ex_close(eap)
5916 exarg_T *eap;
5917{
5918# ifdef FEAT_CMDWIN
5919 if (cmdwin_type != 0)
5920 cmdwin_result = K_IGNORE;
5921 else
5922# endif
5923 ex_win_close(eap, curwin);
5924}
5925
5926 static void
5927ex_win_close(eap, win)
5928 exarg_T *eap;
5929 win_T *win;
5930{
5931 int need_hide;
5932 buf_T *buf = win->w_buffer;
5933
5934 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
5935 if (need_hide && !P_HID(buf) && !eap->forceit)
5936 {
5937#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5938 if ((p_confirm || cmdmod.confirm) && p_write)
5939 {
5940 dialog_changed(buf, FALSE);
5941 if (buf_valid(buf) && bufIsChanged(buf))
5942 return;
5943 need_hide = FALSE;
5944 }
5945 else
5946#endif
5947 {
5948 EMSG(_(e_nowrtmsg));
5949 return;
5950 }
5951 }
5952
5953#ifdef FEAT_GUI
5954 need_mouse_correct = TRUE;
5955#endif
5956 /* free buffer when not hiding it or when it's a scratch buffer */
5957 win_close(win, !need_hide && !P_HID(buf));
5958}
5959
5960#ifdef FEAT_QUICKFIX
5961/*
5962 * ":pclose": Close any preview window.
5963 */
5964 static void
5965ex_pclose(eap)
5966 exarg_T *eap;
5967{
5968 win_T *win;
5969
5970 for (win = firstwin; win != NULL; win = win->w_next)
5971 if (win->w_p_pvw)
5972 {
5973 ex_win_close(eap, win);
5974 break;
5975 }
5976}
5977#endif
5978
5979/*
5980 * ":only".
5981 */
5982 static void
5983ex_only(eap)
5984 exarg_T *eap;
5985{
5986# ifdef FEAT_GUI
5987 need_mouse_correct = TRUE;
5988# endif
5989 close_others(TRUE, eap->forceit);
5990}
5991
5992/*
5993 * ":all" and ":sall".
5994 */
5995 static void
5996ex_all(eap)
5997 exarg_T *eap;
5998{
5999 if (eap->addr_count == 0)
6000 eap->line2 = 9999;
6001 do_arg_all((int)eap->line2, eap->forceit);
6002}
6003#endif /* FEAT_WINDOWS */
6004
6005 static void
6006ex_hide(eap)
6007 exarg_T *eap;
6008{
6009 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6010 eap->errmsg = e_invarg;
6011 else
6012 {
6013 /* ":hide" or ":hide | cmd": hide current window */
6014 eap->nextcmd = check_nextcmd(eap->arg);
6015#ifdef FEAT_WINDOWS
6016 if (!eap->skip)
6017 {
6018# ifdef FEAT_GUI
6019 need_mouse_correct = TRUE;
6020# endif
6021 win_close(curwin, FALSE); /* don't free buffer */
6022 }
6023#endif
6024 }
6025}
6026
6027/*
6028 * ":stop" and ":suspend": Suspend Vim.
6029 */
6030 static void
6031ex_stop(eap)
6032 exarg_T *eap;
6033{
6034 /*
6035 * Disallow suspending for "rvim".
6036 */
6037 if (!check_restricted()
6038#ifdef WIN3264
6039 /*
6040 * Check if external commands are allowed now.
6041 */
6042 && can_end_termcap_mode(TRUE)
6043#endif
6044 )
6045 {
6046 if (!eap->forceit)
6047 autowrite_all();
6048 windgoto((int)Rows - 1, 0);
6049 out_char('\n');
6050 out_flush();
6051 stoptermcap();
6052 out_flush(); /* needed for SUN to restore xterm buffer */
6053#ifdef FEAT_TITLE
6054 mch_restore_title(3); /* restore window titles */
6055#endif
6056 ui_suspend(); /* call machine specific function */
6057#ifdef FEAT_TITLE
6058 maketitle();
6059 resettitle(); /* force updating the title */
6060#endif
6061 starttermcap();
6062 scroll_start(); /* scroll screen before redrawing */
6063 redraw_later_clear();
6064 shell_resized(); /* may have resized window */
6065 }
6066}
6067
6068/*
6069 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6070 */
6071 static void
6072ex_exit(eap)
6073 exarg_T *eap;
6074{
6075#ifdef FEAT_CMDWIN
6076 if (cmdwin_type != 0)
6077 {
6078 cmdwin_result = Ctrl_C;
6079 return;
6080 }
6081#endif
6082
6083 /*
6084 * if more files or windows we won't exit
6085 */
6086 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6087 exiting = TRUE;
6088 if ( ((eap->cmdidx == CMD_wq
6089 || curbufIsChanged())
6090 && do_write(eap) == FAIL)
6091 || check_more(TRUE, eap->forceit) == FAIL
6092 || (only_one_window() && check_changed_any(eap->forceit)))
6093 {
6094 not_exiting();
6095 }
6096 else
6097 {
6098#ifdef FEAT_WINDOWS
6099 if (only_one_window()) /* quit last window, exit Vim */
6100#endif
6101 getout(0);
6102#ifdef FEAT_WINDOWS
6103# ifdef FEAT_GUI
6104 need_mouse_correct = TRUE;
6105# endif
6106 /* quit current window, may free buffer */
6107 win_close(curwin, !P_HID(curwin->w_buffer));
6108#endif
6109 }
6110}
6111
6112/*
6113 * ":print", ":list", ":number".
6114 */
6115 static void
6116ex_print(eap)
6117 exarg_T *eap;
6118{
6119 int save_list = 0; /* init for GCC */
6120
6121 if (eap->cmdidx == CMD_list)
6122 {
6123 save_list = curwin->w_p_list;
6124 curwin->w_p_list = 1;
6125 }
6126
6127 for ( ;!got_int; ui_breakcheck())
6128 {
6129 print_line(eap->line1,
6130 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound));
6131 if (++eap->line1 > eap->line2)
6132 break;
6133 out_flush(); /* show one line at a time */
6134 }
6135 setpcmark();
6136 /* put cursor at last line */
6137 curwin->w_cursor.lnum = eap->line2;
6138 beginline(BL_SOL | BL_FIX);
6139
6140 ex_no_reprint = TRUE;
6141
6142 if (eap->cmdidx == CMD_list)
6143 curwin->w_p_list = save_list;
6144}
6145
6146#ifdef FEAT_BYTEOFF
6147 static void
6148ex_goto(eap)
6149 exarg_T *eap;
6150{
6151 goto_byte(eap->line2);
6152}
6153#endif
6154
6155/*
6156 * ":shell".
6157 */
6158/*ARGSUSED*/
6159 static void
6160ex_shell(eap)
6161 exarg_T *eap;
6162{
6163 do_shell(NULL, 0);
6164}
6165
6166#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6167 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
6168 || defined(PROTO)
6169
6170/*
6171 * Handle a file drop. The code is here because a drop is *nearly* like an
6172 * :args command, but not quite (we have a list of exact filenames, so we
6173 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6174 * code is very similar to :args and hence needs access to a lot of the static
6175 * functions in this file.
6176 *
6177 * The list should be allocated using alloc(), as should each item in the
6178 * list. This function takes over responsibility for freeing the list.
6179 *
6180 * XXX The list is made into the arggument list. This is freed using
6181 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6182 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6183 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6184 * file functionality is (currently) not in EMX this is not presently a
6185 * problem.
6186 */
6187 void
6188handle_drop(filec, filev, split)
6189 int filec; /* the number of files dropped */
6190 char_u **filev; /* the list of files dropped */
6191 int split; /* force splitting the window */
6192{
6193 exarg_T ea;
6194 int save_msg_scroll = msg_scroll;
6195
6196# ifdef FEAT_CMDWIN
6197 if (cmdwin_type != 0)
6198 return;
6199# endif
6200
6201 /* Check whether the current buffer is changed. If so, we will need
6202 * to split the current window or data could be lost.
6203 * We don't need to check if the 'hidden' option is set, as in this
6204 * case the buffer won't be lost.
6205 */
6206 if (!P_HID(curbuf) && !split)
6207 {
6208 ++emsg_off;
6209 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6210 --emsg_off;
6211 }
6212 if (split)
6213 {
6214# ifdef FEAT_WINDOWS
6215 if (win_split(0, 0) == FAIL)
6216 return;
6217# ifdef FEAT_SCROLLBIND
6218 curwin->w_p_scb = FALSE;
6219# endif
6220
6221 /* When splitting the window, create a new alist. Otherwise the
6222 * existing one is overwritten. */
6223 alist_unlink(curwin->w_alist);
6224 alist_new();
6225# else
6226 return; /* can't split, always fail */
6227# endif
6228 }
6229
6230 /*
6231 * Set up the new argument list.
6232 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006233 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
6235 /*
6236 * Move to the first file.
6237 */
6238 /* Fake up a minimal "next" command for do_argfile() */
6239 vim_memset(&ea, 0, sizeof(ea));
6240 ea.cmd = (char_u *)"next";
6241 do_argfile(&ea, 0);
6242
6243 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6244 * mode that is not needed here. */
6245 need_start_insertmode = FALSE;
6246
6247 /* Restore msg_scroll, otherwise a following command may cause scrolling
6248 * unexpectedly. The screen will be redrawn by the caller, thus
6249 * msg_scroll being set by displaying a message is irrelevant. */
6250 msg_scroll = save_msg_scroll;
6251}
6252#endif
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254/*
6255 * Clear an argument list: free all file names and reset it to zero entries.
6256 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006257 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258alist_clear(al)
6259 alist_T *al;
6260{
6261 while (--al->al_ga.ga_len >= 0)
6262 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6263 ga_clear(&al->al_ga);
6264}
6265
6266/*
6267 * Init an argument list.
6268 */
6269 void
6270alist_init(al)
6271 alist_T *al;
6272{
6273 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6274}
6275
6276#if defined(FEAT_WINDOWS) || defined(PROTO)
6277
6278/*
6279 * Remove a reference from an argument list.
6280 * Ignored when the argument list is the global one.
6281 * If the argument list is no longer used by any window, free it.
6282 */
6283 void
6284alist_unlink(al)
6285 alist_T *al;
6286{
6287 if (al != &global_alist && --al->al_refcount <= 0)
6288 {
6289 alist_clear(al);
6290 vim_free(al);
6291 }
6292}
6293
6294# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6295/*
6296 * Create a new argument list and use it for the current window.
6297 */
6298 void
6299alist_new()
6300{
6301 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6302 if (curwin->w_alist == NULL)
6303 {
6304 curwin->w_alist = &global_alist;
6305 ++global_alist.al_refcount;
6306 }
6307 else
6308 {
6309 curwin->w_alist->al_refcount = 1;
6310 alist_init(curwin->w_alist);
6311 }
6312}
6313# endif
6314#endif
6315
6316#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6317/*
6318 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006319 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6320 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 */
6322 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006323alist_expand(fnum_list, fnum_len)
6324 int *fnum_list;
6325 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006328 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 char_u **new_arg_files;
6330 int new_arg_file_count;
6331 char_u *save_p_su = p_su;
6332 int i;
6333
6334 /* Don't use 'suffixes' here. This should work like the shell did the
6335 * expansion. Also, the vimrc file isn't read yet, thus the user
6336 * can't set the options. */
6337 p_su = empty_option;
6338 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6339 if (old_arg_files != NULL)
6340 {
6341 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006342 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6343 old_arg_count = GARGCOUNT;
6344 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 &new_arg_file_count, &new_arg_files,
6346 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6347 && new_arg_file_count > 0)
6348 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006349 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6350 TRUE, fnum_list, fnum_len);
6351 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 }
6354 p_su = save_p_su;
6355}
6356#endif
6357
6358/*
6359 * Set the argument list for the current window.
6360 * Takes over the allocated files[] and the allocated fnames in it.
6361 */
6362 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006363alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 alist_T *al;
6365 int count;
6366 char_u **files;
6367 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006368 int *fnum_list;
6369 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 int i;
6372
6373 alist_clear(al);
6374 if (ga_grow(&al->al_ga, count) == OK)
6375 {
6376 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006377 {
6378 if (got_int)
6379 {
6380 /* When adding many buffers this can take a long time. Allow
6381 * interrupting here. */
6382 while (i < count)
6383 vim_free(files[i++]);
6384 break;
6385 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006386
6387 /* May set buffer name of a buffer previously used for the
6388 * argument list, so that it's re-used by alist_add. */
6389 if (fnum_list != NULL && i < fnum_len)
6390 buf_set_name(fnum_list[i], files[i]);
6391
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006393 ui_breakcheck();
6394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 vim_free(files);
6396 }
6397 else
6398 FreeWild(count, files);
6399#ifdef FEAT_WINDOWS
6400 if (al == &global_alist)
6401#endif
6402 arg_had_last = FALSE;
6403}
6404
6405/*
6406 * Add file "fname" to argument list "al".
6407 * "fname" must have been allocated and "al" must have been checked for room.
6408 */
6409 void
6410alist_add(al, fname, set_fnum)
6411 alist_T *al;
6412 char_u *fname;
6413 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6414{
6415 if (fname == NULL) /* don't add NULL file names */
6416 return;
6417#ifdef BACKSLASH_IN_FILENAME
6418 slash_adjust(fname);
6419#endif
6420 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6421 if (set_fnum > 0)
6422 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6423 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6424 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425}
6426
6427#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6428/*
6429 * Adjust slashes in file names. Called after 'shellslash' was set.
6430 */
6431 void
6432alist_slash_adjust()
6433{
6434 int i;
6435# ifdef FEAT_WINDOWS
6436 win_T *wp;
6437# endif
6438
6439 for (i = 0; i < GARGCOUNT; ++i)
6440 if (GARGLIST[i].ae_fname != NULL)
6441 slash_adjust(GARGLIST[i].ae_fname);
6442# ifdef FEAT_WINDOWS
6443 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6444 if (wp->w_alist != &global_alist)
6445 for (i = 0; i < WARGCOUNT(wp); ++i)
6446 if (WARGLIST(wp)[i].ae_fname != NULL)
6447 slash_adjust(WARGLIST(wp)[i].ae_fname);
6448# endif
6449}
6450#endif
6451
6452/*
6453 * ":preserve".
6454 */
6455/*ARGSUSED*/
6456 static void
6457ex_preserve(eap)
6458 exarg_T *eap;
6459{
6460 ml_preserve(curbuf, TRUE);
6461}
6462
6463/*
6464 * ":recover".
6465 */
6466 static void
6467ex_recover(eap)
6468 exarg_T *eap;
6469{
6470 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6471 recoverymode = TRUE;
6472 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6473 && (*eap->arg == NUL
6474 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6475 ml_recover();
6476 recoverymode = FALSE;
6477}
6478
6479/*
6480 * Command modifier used in a wrong way.
6481 */
6482 static void
6483ex_wrongmodifier(eap)
6484 exarg_T *eap;
6485{
6486 eap->errmsg = e_invcmd;
6487}
6488
6489#ifdef FEAT_WINDOWS
6490/*
6491 * :sview [+command] file split window with new file, read-only
6492 * :split [[+command] file] split window with current or new file
6493 * :vsplit [[+command] file] split window vertically with current or new file
6494 * :new [[+command] file] split window with no or new file
6495 * :vnew [[+command] file] split vertically window with no or new file
6496 * :sfind [+command] file split window with file in 'path'
6497 */
6498 void
6499ex_splitview(eap)
6500 exarg_T *eap;
6501{
6502 win_T *old_curwin;
6503#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6504 char_u *fname = NULL;
6505#endif
6506#ifdef FEAT_BROWSE
6507 int browse_flag = cmdmod.browse;
6508#endif
6509
6510#ifndef FEAT_VERTSPLIT
6511 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6512 {
6513 ex_ni(eap);
6514 return;
6515 }
6516#endif
6517
6518 old_curwin = curwin;
6519#ifdef FEAT_GUI
6520 need_mouse_correct = TRUE;
6521#endif
6522
6523#ifdef FEAT_QUICKFIX
6524 /* A ":split" in the quickfix window works like ":new". Don't want two
6525 * quickfix windows. */
6526 if (bt_quickfix(curbuf))
6527 {
6528 if (eap->cmdidx == CMD_split)
6529 eap->cmdidx = CMD_new;
6530# ifdef FEAT_VERTSPLIT
6531 if (eap->cmdidx == CMD_vsplit)
6532 eap->cmdidx = CMD_vnew;
6533# endif
6534 }
6535#endif
6536
6537#ifdef FEAT_SEARCHPATH
6538 if (eap->cmdidx == CMD_sfind)
6539 {
6540 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6541 FNAME_MESS, TRUE, curbuf->b_ffname);
6542 if (fname == NULL)
6543 goto theend;
6544 eap->arg = fname;
6545 }
6546# ifdef FEAT_BROWSE
6547 else
6548# endif
6549#endif
6550#ifdef FEAT_BROWSE
6551 if (cmdmod.browse
6552# ifdef FEAT_VERTSPLIT
6553 && eap->cmdidx != CMD_vnew
6554#endif
6555 && eap->cmdidx != CMD_new)
6556 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006557 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 eap->arg, NULL, NULL, NULL, curbuf);
6559 if (fname == NULL)
6560 goto theend;
6561 eap->arg = fname;
6562 }
6563 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6564#endif
6565
6566 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6567 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6568 {
6569#ifdef FEAT_SCROLLBIND
6570 /* Reset 'scrollbind' when editing another file, but keep it when
6571 * doing ":split" without arguments. */
6572 if (*eap->arg != NUL
6573#ifdef FEAT_BROWSE
6574 || cmdmod.browse
6575#endif
6576 )
6577 curwin->w_p_scb = FALSE;
6578 else
6579 do_check_scrollbind(FALSE);
6580#endif
6581 do_exedit(eap, old_curwin);
6582 }
6583
6584#ifdef FEAT_BROWSE
6585 cmdmod.browse = browse_flag;
6586#endif
6587
6588#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6589theend:
6590 vim_free(fname);
6591#endif
6592}
6593#endif
6594
6595/*
6596 * ":mode": Set screen mode.
6597 * If no argument given, just get the screen size and redraw.
6598 */
6599 static void
6600ex_mode(eap)
6601 exarg_T *eap;
6602{
6603 if (*eap->arg == NUL)
6604 shell_resized();
6605 else
6606 mch_screenmode(eap->arg);
6607}
6608
6609#ifdef FEAT_WINDOWS
6610/*
6611 * ":resize".
6612 * set, increment or decrement current window height
6613 */
6614 static void
6615ex_resize(eap)
6616 exarg_T *eap;
6617{
6618 int n;
6619 win_T *wp = curwin;
6620
6621 if (eap->addr_count > 0)
6622 {
6623 n = eap->line2;
6624 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6625 ;
6626 }
6627
6628#ifdef FEAT_GUI
6629 need_mouse_correct = TRUE;
6630#endif
6631 n = atol((char *)eap->arg);
6632#ifdef FEAT_VERTSPLIT
6633 if (cmdmod.split & WSP_VERT)
6634 {
6635 if (*eap->arg == '-' || *eap->arg == '+')
6636 n += W_WIDTH(curwin);
6637 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6638 n = 9999;
6639 win_setwidth_win((int)n, wp);
6640 }
6641 else
6642#endif
6643 {
6644 if (*eap->arg == '-' || *eap->arg == '+')
6645 n += curwin->w_height;
6646 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6647 n = 9999;
6648 win_setheight_win((int)n, wp);
6649 }
6650}
6651#endif
6652
6653/*
6654 * ":find [+command] <file>" command.
6655 */
6656 static void
6657ex_find(eap)
6658 exarg_T *eap;
6659{
6660#ifdef FEAT_SEARCHPATH
6661 char_u *fname;
6662 int count;
6663
6664 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6665 TRUE, curbuf->b_ffname);
6666 if (eap->addr_count > 0)
6667 {
6668 /* Repeat finding the file "count" times. This matters when it
6669 * appears several times in the path. */
6670 count = eap->line2;
6671 while (fname != NULL && --count > 0)
6672 {
6673 vim_free(fname);
6674 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6675 FALSE, curbuf->b_ffname);
6676 }
6677 }
6678
6679 if (fname != NULL)
6680 {
6681 eap->arg = fname;
6682#endif
6683 do_exedit(eap, NULL);
6684#ifdef FEAT_SEARCHPATH
6685 vim_free(fname);
6686 }
6687#endif
6688}
6689
6690/*
6691 * ":edit", ":badd".
6692 */
6693 static void
6694ex_edit(eap)
6695 exarg_T *eap;
6696{
6697 do_exedit(eap, NULL);
6698}
6699
6700/*
6701 * ":edit <file>" command and alikes.
6702 */
6703/*ARGSUSED*/
6704 void
6705do_exedit(eap, old_curwin)
6706 exarg_T *eap;
6707 win_T *old_curwin; /* curwin before doing a split or NULL */
6708{
6709 int n;
6710#ifdef FEAT_WINDOWS
6711 int need_hide;
6712#endif
6713
6714 /*
6715 * ":vi" command ends Ex mode.
6716 */
6717 if (exmode_active && (eap->cmdidx == CMD_visual
6718 || eap->cmdidx == CMD_view))
6719 {
6720 exmode_active = FALSE;
6721 if (*eap->arg == NUL)
6722 return;
6723 }
6724
6725 if ((eap->cmdidx == CMD_new
6726#ifdef FEAT_VERTSPLIT
6727 || eap->cmdidx == CMD_vnew
6728#endif
6729 ) && *eap->arg == NUL)
6730 {
6731 /* ":new" without argument: edit an new empty buffer */
6732 setpcmark();
6733 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
6734 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
6735 }
6736 else if ((eap->cmdidx != CMD_split
6737#ifdef FEAT_VERTSPLIT
6738 && eap->cmdidx != CMD_vsplit
6739#endif
6740 )
6741 || *eap->arg != NUL
6742#ifdef FEAT_BROWSE
6743 || cmdmod.browse
6744#endif
6745 )
6746 {
6747 n = readonlymode;
6748 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6749 readonlymode = TRUE;
6750 else if (eap->cmdidx == CMD_enew)
6751 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6752 empty buffer */
6753 setpcmark();
6754 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6755 NULL, eap,
6756 /* ":edit" goes to first line if Vi compatible */
6757 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6758 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6759 ? ECMD_ONE : eap->do_ecmd_lnum,
6760 (P_HID(curbuf) ? ECMD_HIDE : 0)
6761 + (eap->forceit ? ECMD_FORCEIT : 0)
6762#ifdef FEAT_LISTCMDS
6763 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
6764#endif
6765 ) == FAIL)
6766 {
6767 /* Editing the file failed. If the window was split, close it. */
6768#ifdef FEAT_WINDOWS
6769 if (old_curwin != NULL)
6770 {
6771 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
6772 if (!need_hide || P_HID(curbuf))
6773 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006774# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6775 cleanup_T cs;
6776
6777 /* Reset the error/interrupt/exception state here so that
6778 * aborting() returns FALSE when closing a window. */
6779 enter_cleanup(&cs);
6780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781# ifdef FEAT_GUI
6782 need_mouse_correct = TRUE;
6783# endif
6784 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006785
6786# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6787 /* Restore the error/interrupt/exception state if not
6788 * discarded by a new aborting error, interrupt, or
6789 * uncaught exception. */
6790 leave_cleanup(&cs);
6791# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 }
6793 }
6794#endif
6795 }
6796 else if (readonlymode && curbuf->b_nwindows == 1)
6797 {
6798 /* When editing an already visited buffer, 'readonly' won't be set
6799 * but the previous value is kept. With ":view" and ":sview" we
6800 * want the file to be readonly, except when another window is
6801 * editing the same buffer. */
6802 curbuf->b_p_ro = TRUE;
6803 }
6804 readonlymode = n;
6805 }
6806 else
6807 {
6808 if (eap->do_ecmd_cmd != NULL)
6809 do_cmdline_cmd(eap->do_ecmd_cmd);
6810#ifdef FEAT_TITLE
6811 n = curwin->w_arg_idx_invalid;
6812#endif
6813 check_arg_idx(curwin);
6814#ifdef FEAT_TITLE
6815 if (n != curwin->w_arg_idx_invalid)
6816 maketitle();
6817#endif
6818 }
6819
6820#ifdef FEAT_WINDOWS
6821 /*
6822 * if ":split file" worked, set alternate file name in old window to new
6823 * file
6824 */
6825 if (old_curwin != NULL
6826 && *eap->arg != NUL
6827 && curwin != old_curwin
6828 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006829 && old_curwin->w_buffer != curbuf
6830 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 old_curwin->w_alt_fnum = curbuf->b_fnum;
6832#endif
6833
6834 ex_no_reprint = TRUE;
6835}
6836
6837#ifndef FEAT_GUI
6838/*
6839 * ":gui" and ":gvim" when there is no GUI.
6840 */
6841 static void
6842ex_nogui(eap)
6843 exarg_T *eap;
6844{
6845 eap->errmsg = e_nogvim;
6846}
6847#endif
6848
6849#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
6850 static void
6851ex_tearoff(eap)
6852 exarg_T *eap;
6853{
6854 gui_make_tearoff(eap->arg);
6855}
6856#endif
6857
Bram Moolenaar843ee412004-06-30 16:16:41 +00006858#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 static void
6860ex_popup(eap)
6861 exarg_T *eap;
6862{
6863 gui_make_popup(eap->arg);
6864}
6865#endif
6866
6867/*ARGSUSED*/
6868 static void
6869ex_swapname(eap)
6870 exarg_T *eap;
6871{
6872 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
6873 MSG(_("No swap file"));
6874 else
6875 msg(curbuf->b_ml.ml_mfp->mf_fname);
6876}
6877
6878/*
6879 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6880 * offset.
6881 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6882 */
6883/*ARGSUSED*/
6884 static void
6885ex_syncbind(eap)
6886 exarg_T *eap;
6887{
6888#ifdef FEAT_SCROLLBIND
6889 win_T *wp;
6890 long topline;
6891 long y;
6892 linenr_T old_linenr = curwin->w_cursor.lnum;
6893
6894 setpcmark();
6895
6896 /*
6897 * determine max topline
6898 */
6899 if (curwin->w_p_scb)
6900 {
6901 topline = curwin->w_topline;
6902 for (wp = firstwin; wp; wp = wp->w_next)
6903 {
6904 if (wp->w_p_scb && wp->w_buffer)
6905 {
6906 y = wp->w_buffer->b_ml.ml_line_count - p_so;
6907 if (topline > y)
6908 topline = y;
6909 }
6910 }
6911 if (topline < 1)
6912 topline = 1;
6913 }
6914 else
6915 {
6916 topline = 1;
6917 }
6918
6919
6920 /*
6921 * set all scrollbind windows to the same topline
6922 */
6923 wp = curwin;
6924 for (curwin = firstwin; curwin; curwin = curwin->w_next)
6925 {
6926 if (curwin->w_p_scb)
6927 {
6928 y = topline - curwin->w_topline;
6929 if (y > 0)
6930 scrollup(y, TRUE);
6931 else
6932 scrolldown(-y, TRUE);
6933 curwin->w_scbind_pos = topline;
6934 redraw_later(VALID);
6935 cursor_correct();
6936#ifdef FEAT_WINDOWS
6937 curwin->w_redr_status = TRUE;
6938#endif
6939 }
6940 }
6941 curwin = wp;
6942 if (curwin->w_p_scb)
6943 {
6944 did_syncbind = TRUE;
6945 checkpcmark();
6946 if (old_linenr != curwin->w_cursor.lnum)
6947 {
6948 char_u ctrl_o[2];
6949
6950 ctrl_o[0] = Ctrl_O;
6951 ctrl_o[1] = 0;
6952 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6953 }
6954 }
6955#endif
6956}
6957
6958
6959 static void
6960ex_read(eap)
6961 exarg_T *eap;
6962{
6963 int i;
6964
6965 if (eap->usefilter) /* :r!cmd */
6966 do_bang(1, eap, FALSE, FALSE, TRUE);
6967 else
6968 {
6969 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6970 return;
6971
6972#ifdef FEAT_BROWSE
6973 if (cmdmod.browse)
6974 {
6975 char_u *browseFile;
6976
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006977 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 NULL, NULL, NULL, curbuf);
6979 if (browseFile != NULL)
6980 {
6981 i = readfile(browseFile, NULL,
6982 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6983 vim_free(browseFile);
6984 }
6985 else
6986 i = OK;
6987 }
6988 else
6989#endif
6990 if (*eap->arg == NUL)
6991 {
6992 if (check_fname() == FAIL) /* check for no file name */
6993 return;
6994 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6995 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6996 }
6997 else
6998 {
6999 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7000 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7001 i = readfile(eap->arg, NULL,
7002 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7003
7004 }
7005 if (i == FAIL)
7006 {
7007#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7008 if (!aborting())
7009#endif
7010 EMSG2(_(e_notopen), eap->arg);
7011 }
7012 else
7013 redraw_curbuf_later(VALID);
7014 }
7015}
7016
7017/*
7018 * ":cd", ":lcd", ":chdir" and ":lchdir".
7019 */
7020 static void
7021ex_cd(eap)
7022 exarg_T *eap;
7023{
7024 static char_u *prev_dir = NULL;
7025 char_u *new_dir;
7026 char_u *tofree;
7027
7028 new_dir = eap->arg;
7029#if !defined(UNIX) && !defined(VMS)
7030 /* for non-UNIX ":cd" means: print current directory */
7031 if (*new_dir == NUL)
7032 ex_pwd(NULL);
7033 else
7034#endif
7035 {
7036 /* ":cd -": Change to previous directory */
7037 if (STRCMP(new_dir, "-") == 0)
7038 {
7039 if (prev_dir == NULL)
7040 {
7041 EMSG(_("E186: No previous directory"));
7042 return;
7043 }
7044 new_dir = prev_dir;
7045 }
7046
7047 /* Save current directory for next ":cd -" */
7048 tofree = prev_dir;
7049 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7050 prev_dir = vim_strsave(NameBuff);
7051 else
7052 prev_dir = NULL;
7053
7054#if defined(UNIX) || defined(VMS)
7055 /* for UNIX ":cd" means: go to home directory */
7056 if (*new_dir == NUL)
7057 {
7058 /* use NameBuff for home directory name */
7059# ifdef VMS
7060 char_u *p;
7061
7062 p = mch_getenv((char_u *)"SYS$LOGIN");
7063 if (p == NULL || *p == NUL) /* empty is the same as not set */
7064 NameBuff[0] = NUL;
7065 else
7066 STRNCPY(NameBuff, p, MAXPATHL);
7067# else
7068 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7069# endif
7070 new_dir = NameBuff;
7071 }
7072#endif
7073 if (new_dir == NULL || vim_chdir(new_dir))
7074 EMSG(_(e_failed));
7075 else
7076 {
7077 vim_free(curwin->w_localdir);
7078 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7079 {
7080 /* If still in global directory, need to remember current
7081 * directory as global directory. */
7082 if (globaldir == NULL && prev_dir != NULL)
7083 globaldir = vim_strsave(prev_dir);
7084 /* Remember this local directory for the window. */
7085 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7086 curwin->w_localdir = vim_strsave(NameBuff);
7087 }
7088 else
7089 {
7090 /* We are now in the global directory, no need to remember its
7091 * name. */
7092 vim_free(globaldir);
7093 globaldir = NULL;
7094 curwin->w_localdir = NULL;
7095 }
7096
7097 shorten_fnames(TRUE);
7098
7099 /* Echo the new current directory if the command was typed. */
7100 if (KeyTyped)
7101 ex_pwd(eap);
7102 }
7103 vim_free(tofree);
7104 }
7105}
7106
7107/*
7108 * ":pwd".
7109 */
7110/*ARGSUSED*/
7111 static void
7112ex_pwd(eap)
7113 exarg_T *eap;
7114{
7115 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7116 {
7117#ifdef BACKSLASH_IN_FILENAME
7118 slash_adjust(NameBuff);
7119#endif
7120 msg(NameBuff);
7121 }
7122 else
7123 EMSG(_("E187: Unknown"));
7124}
7125
7126/*
7127 * ":=".
7128 */
7129 static void
7130ex_equal(eap)
7131 exarg_T *eap;
7132{
7133 smsg((char_u *)"%ld", (long)eap->line2);
7134}
7135
7136 static void
7137ex_sleep(eap)
7138 exarg_T *eap;
7139{
7140 int n;
7141
7142 if (cursor_valid())
7143 {
7144 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7145 if (n >= 0)
7146 windgoto((int)n, curwin->w_wcol);
7147 }
7148 do_sleep(eap->line2 * (*eap->arg == 'm' ? 1L : 1000L));
7149}
7150
7151/*
7152 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7153 */
7154 void
7155do_sleep(msec)
7156 long msec;
7157{
7158 long done;
7159
7160 cursor_on();
7161 out_flush();
7162 for (done = 0; !got_int && done < msec; done += 1000L)
7163 {
7164 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7165 ui_breakcheck();
7166 }
7167}
7168
7169 static void
7170do_exmap(eap, isabbrev)
7171 exarg_T *eap;
7172 int isabbrev;
7173{
7174 int mode;
7175 char_u *cmdp;
7176
7177 cmdp = eap->cmd;
7178 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7179
7180 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7181 eap->arg, mode, isabbrev))
7182 {
7183 case 1: EMSG(_(e_invarg));
7184 break;
7185 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7186 break;
7187 }
7188}
7189
7190/*
7191 * ":winsize" command (obsolete).
7192 */
7193 static void
7194ex_winsize(eap)
7195 exarg_T *eap;
7196{
7197 int w, h;
7198 char_u *arg = eap->arg;
7199 char_u *p;
7200
7201 w = getdigits(&arg);
7202 arg = skipwhite(arg);
7203 p = arg;
7204 h = getdigits(&arg);
7205 if (*p != NUL && *arg == NUL)
7206 set_shellsize(w, h, TRUE);
7207 else
7208 EMSG(_("E465: :winsize requires two number arguments"));
7209}
7210
7211#ifdef FEAT_WINDOWS
7212 static void
7213ex_wincmd(eap)
7214 exarg_T *eap;
7215{
7216 int xchar = NUL;
7217 char_u *p;
7218
7219 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7220 {
7221 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7222 if (eap->arg[1] == NUL)
7223 {
7224 EMSG(_(e_invarg));
7225 return;
7226 }
7227 xchar = eap->arg[1];
7228 p = eap->arg + 2;
7229 }
7230 else
7231 p = eap->arg + 1;
7232
7233 eap->nextcmd = check_nextcmd(p);
7234 p = skipwhite(p);
7235 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7236 EMSG(_(e_invarg));
7237 else
7238 {
7239 /* Pass flags on for ":vertical wincmd ]". */
7240 postponed_split_flags = cmdmod.split;
7241 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7242 postponed_split_flags = 0;
7243 }
7244}
7245#endif
7246
Bram Moolenaar843ee412004-06-30 16:16:41 +00007247#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248/*
7249 * ":winpos".
7250 */
7251 static void
7252ex_winpos(eap)
7253 exarg_T *eap;
7254{
7255 int x, y;
7256 char_u *arg = eap->arg;
7257 char_u *p;
7258
7259 if (*arg == NUL)
7260 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007261# if defined(FEAT_GUI) || defined(MSWIN)
7262# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007264# else
7265 if (mch_get_winpos(&x, &y) != FAIL)
7266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {
7268 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7269 msg(IObuff);
7270 }
7271 else
7272# endif
7273 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7274 }
7275 else
7276 {
7277 x = getdigits(&arg);
7278 arg = skipwhite(arg);
7279 p = arg;
7280 y = getdigits(&arg);
7281 if (*p == NUL || *arg != NUL)
7282 {
7283 EMSG(_("E466: :winpos requires two number arguments"));
7284 return;
7285 }
7286# ifdef FEAT_GUI
7287 if (gui.in_use)
7288 gui_mch_set_winpos(x, y);
7289 else if (gui.starting)
7290 {
7291 /* Remember the coordinates for when the window is opened. */
7292 gui_win_x = x;
7293 gui_win_y = y;
7294 }
7295# ifdef HAVE_TGETENT
7296 else
7297# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007298# else
7299# ifdef MSWIN
7300 mch_set_winpos(x, y);
7301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302# endif
7303# ifdef HAVE_TGETENT
7304 if (*T_CWP)
7305 term_set_winpos(x, y);
7306# endif
7307 }
7308}
7309#endif
7310
7311/*
7312 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7313 */
7314 static void
7315ex_operators(eap)
7316 exarg_T *eap;
7317{
7318 oparg_T oa;
7319
7320 clear_oparg(&oa);
7321 oa.regname = eap->regname;
7322 oa.start.lnum = eap->line1;
7323 oa.end.lnum = eap->line2;
7324 oa.line_count = eap->line2 - eap->line1 + 1;
7325 oa.motion_type = MLINE;
7326#ifdef FEAT_VIRTUALEDIT
7327 virtual_op = FALSE;
7328#endif
7329 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7330 {
7331 setpcmark();
7332 curwin->w_cursor.lnum = eap->line1;
7333 beginline(BL_SOL | BL_FIX);
7334 }
7335
7336 switch (eap->cmdidx)
7337 {
7338 case CMD_delete:
7339 oa.op_type = OP_DELETE;
7340 op_delete(&oa);
7341 break;
7342
7343 case CMD_yank:
7344 oa.op_type = OP_YANK;
7345 (void)op_yank(&oa, FALSE, TRUE);
7346 break;
7347
7348 default: /* CMD_rshift or CMD_lshift */
7349 if ((eap->cmdidx == CMD_rshift)
7350#ifdef FEAT_RIGHTLEFT
7351 ^ curwin->w_p_rl
7352#endif
7353 )
7354 oa.op_type = OP_RSHIFT;
7355 else
7356 oa.op_type = OP_LSHIFT;
7357 op_shift(&oa, FALSE, eap->amount);
7358 break;
7359 }
7360#ifdef FEAT_VIRTUALEDIT
7361 virtual_op = MAYBE;
7362#endif
7363}
7364
7365/*
7366 * ":put".
7367 */
7368 static void
7369ex_put(eap)
7370 exarg_T *eap;
7371{
7372 /* ":0put" works like ":1put!". */
7373 if (eap->line2 == 0)
7374 {
7375 eap->line2 = 1;
7376 eap->forceit = TRUE;
7377 }
7378 curwin->w_cursor.lnum = eap->line2;
7379 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L, PUT_LINE);
7380}
7381
7382/*
7383 * Handle ":copy" and ":move".
7384 */
7385 static void
7386ex_copymove(eap)
7387 exarg_T *eap;
7388{
7389 long n;
7390
7391 n = get_address(&eap->arg, FALSE, FALSE);
7392 if (eap->arg == NULL) /* error detected */
7393 {
7394 eap->nextcmd = NULL;
7395 return;
7396 }
7397
7398 /*
7399 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7400 */
7401 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7402 {
7403 EMSG(_(e_invaddr));
7404 return;
7405 }
7406
7407 if (eap->cmdidx == CMD_move)
7408 {
7409 if (do_move(eap->line1, eap->line2, n) == FAIL)
7410 return;
7411 }
7412 else
7413 ex_copy(eap->line1, eap->line2, n);
7414 u_clearline();
7415 beginline(BL_SOL | BL_FIX);
7416}
7417
7418/*
7419 * ":smagic" and ":snomagic".
7420 */
7421 static void
7422ex_submagic(eap)
7423 exarg_T *eap;
7424{
7425 int magic_save = p_magic;
7426
7427 p_magic = (eap->cmdidx == CMD_smagic);
7428 do_sub(eap);
7429 p_magic = magic_save;
7430}
7431
7432/*
7433 * ":join".
7434 */
7435 static void
7436ex_join(eap)
7437 exarg_T *eap;
7438{
7439 curwin->w_cursor.lnum = eap->line1;
7440 if (eap->line1 == eap->line2)
7441 {
7442 if (eap->addr_count >= 2) /* :2,2join does nothing */
7443 return;
7444 if (eap->line2 == curbuf->b_ml.ml_line_count)
7445 {
7446 beep_flush();
7447 return;
7448 }
7449 ++eap->line2;
7450 }
7451 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
7452 beginline(BL_WHITE | BL_FIX);
7453}
7454
7455/*
7456 * ":[addr]@r" or ":[addr]*r": execute register
7457 */
7458 static void
7459ex_at(eap)
7460 exarg_T *eap;
7461{
7462 int c;
7463
7464 curwin->w_cursor.lnum = eap->line2;
7465
7466#ifdef USE_ON_FLY_SCROLL
7467 dont_scroll = TRUE; /* disallow scrolling here */
7468#endif
7469
7470 /* get the register name. No name means to use the previous one */
7471 c = *eap->arg;
7472 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7473 c = '@';
7474 /* put the register in mapbuf */
7475 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
7476 beep_flush();
7477 else
7478 {
7479 int save_efr = exec_from_reg;
7480
7481 exec_from_reg = TRUE;
7482
7483 /*
7484 * Execute from the typeahead buffer.
7485 * Originally this didn't check for the typeahead buffer to be empty,
7486 * thus could read more Ex commands from stdin. It's not clear why,
7487 * it is certainly unexpected.
7488 */
7489 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
7490 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7491
7492 exec_from_reg = save_efr;
7493 }
7494}
7495
7496/*
7497 * ":!".
7498 */
7499 static void
7500ex_bang(eap)
7501 exarg_T *eap;
7502{
7503 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7504}
7505
7506/*
7507 * ":undo".
7508 */
7509/*ARGSUSED*/
7510 static void
7511ex_undo(eap)
7512 exarg_T *eap;
7513{
7514 u_undo(1);
7515}
7516
7517/*
7518 * ":redo".
7519 */
7520/*ARGSUSED*/
7521 static void
7522ex_redo(eap)
7523 exarg_T *eap;
7524{
7525 u_redo(1);
7526}
7527
7528/*
7529 * ":redir": start/stop redirection.
7530 */
7531 static void
7532ex_redir(eap)
7533 exarg_T *eap;
7534{
7535 char *mode;
7536 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007537 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
7539 if (STRICMP(eap->arg, "END") == 0)
7540 close_redir();
7541 else
7542 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007543 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007545 ++arg;
7546 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007548 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 mode = "a";
7550 }
7551 else
7552 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007553 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554
7555 close_redir();
7556
7557 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007558 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 if (fname == NULL)
7560 return;
7561#ifdef FEAT_BROWSE
7562 if (cmdmod.browse)
7563 {
7564 char_u *browseFile;
7565
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007566 browseFile = do_browse(BROWSE_SAVE,
7567 (char_u *)_("Save Redirection"),
7568 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 if (browseFile == NULL)
7570 return; /* operation cancelled */
7571 vim_free(fname);
7572 fname = browseFile;
7573 eap->forceit = TRUE; /* since dialog already asked */
7574 }
7575#endif
7576
7577 redir_fd = open_exfile(fname, eap->forceit, mode);
7578 vim_free(fname);
7579 }
7580#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007581 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 {
7583 /* redirect to a register a-z (resp. A-Z for appending) */
7584 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007585 ++arg;
7586 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007588 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007590 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007592 redir_reg = *arg++;
7593 if (*arg == '>')
7594 ++arg;
7595 else if (*arg == NUL && (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007597 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007599 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 {
7601 /* make register empty */
7602 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
7603 }
Bram Moolenaarca472992005-01-21 11:46:23 +00007604 if (*arg != NUL)
7605 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607 else
Bram Moolenaarca472992005-01-21 11:46:23 +00007608 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
7610#endif
7611
7612 /* TODO: redirect to a buffer */
7613
7614 /* TODO: redirect to an internal variable */
7615
7616 else
Bram Moolenaarca472992005-01-21 11:46:23 +00007617 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 }
7619}
7620
7621/*
7622 * ":redraw": force redraw
7623 */
7624 static void
7625ex_redraw(eap)
7626 exarg_T *eap;
7627{
7628 int r = RedrawingDisabled;
7629 int p = p_lz;
7630
7631 RedrawingDisabled = 0;
7632 p_lz = FALSE;
7633 update_topline();
7634 update_screen(eap->forceit ? CLEAR :
7635#ifdef FEAT_VISUAL
7636 VIsual_active ? INVERTED :
7637#endif
7638 0);
7639#ifdef FEAT_TITLE
7640 if (need_maketitle)
7641 maketitle();
7642#endif
7643 RedrawingDisabled = r;
7644 p_lz = p;
7645
7646 /* Reset msg_didout, so that a message that's there is overwritten. */
7647 msg_didout = FALSE;
7648 msg_col = 0;
7649
7650 out_flush();
7651}
7652
7653/*
7654 * ":redrawstatus": force redraw of status line(s)
7655 */
7656/*ARGSUSED*/
7657 static void
7658ex_redrawstatus(eap)
7659 exarg_T *eap;
7660{
7661#if defined(FEAT_WINDOWS)
7662 int r = RedrawingDisabled;
7663 int p = p_lz;
7664
7665 RedrawingDisabled = 0;
7666 p_lz = FALSE;
7667 if (eap->forceit)
7668 status_redraw_all();
7669 else
7670 status_redraw_curbuf();
7671 update_screen(
7672# ifdef FEAT_VISUAL
7673 VIsual_active ? INVERTED :
7674# endif
7675 0);
7676 RedrawingDisabled = r;
7677 p_lz = p;
7678 out_flush();
7679#endif
7680}
7681
7682 static void
7683close_redir()
7684{
7685 if (redir_fd != NULL)
7686 {
7687 fclose(redir_fd);
7688 redir_fd = NULL;
7689 }
7690#ifdef FEAT_EVAL
7691 redir_reg = 0;
7692#endif
7693}
7694
7695#if defined(FEAT_SESSION) && defined(USE_CRNL)
7696# define MKSESSION_NL
7697static int mksession_nl = FALSE; /* use NL only in put_eol() */
7698#endif
7699
7700/*
7701 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
7702 */
7703 static void
7704ex_mkrc(eap)
7705 exarg_T *eap;
7706{
7707 FILE *fd;
7708 int failed = FALSE;
7709 char_u *fname;
7710#ifdef FEAT_BROWSE
7711 char_u *browseFile = NULL;
7712#endif
7713#ifdef FEAT_SESSION
7714 int view_session = FALSE;
7715 int using_vdir = FALSE; /* using 'viewdir'? */
7716 char_u *viewFile = NULL;
7717 unsigned *flagp;
7718#endif
7719
7720 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
7721 {
7722#ifdef FEAT_SESSION
7723 view_session = TRUE;
7724#else
7725 ex_ni(eap);
7726 return;
7727#endif
7728 }
7729
7730#ifdef FEAT_SESSION
7731 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
7732 if (eap->cmdidx == CMD_mkview
7733 && (*eap->arg == NUL
7734 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
7735 {
7736 eap->forceit = TRUE;
7737 fname = get_view_file(*eap->arg);
7738 if (fname == NULL)
7739 return;
7740 viewFile = fname;
7741 using_vdir = TRUE;
7742 }
7743 else
7744#endif
7745 if (*eap->arg != NUL)
7746 fname = eap->arg;
7747 else if (eap->cmdidx == CMD_mkvimrc)
7748 fname = (char_u *)VIMRC_FILE;
7749#ifdef FEAT_SESSION
7750 else if (eap->cmdidx == CMD_mksession)
7751 fname = (char_u *)SESSION_FILE;
7752#endif
7753 else
7754 fname = (char_u *)EXRC_FILE;
7755
7756#ifdef FEAT_BROWSE
7757 if (cmdmod.browse)
7758 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007759 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760# ifdef FEAT_SESSION
7761 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
7762 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
7763# endif
7764 (char_u *)_("Save Setup"),
7765 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
7766 if (browseFile == NULL)
7767 goto theend;
7768 fname = browseFile;
7769 eap->forceit = TRUE; /* since dialog already asked */
7770 }
7771#endif
7772
7773#if defined(FEAT_SESSION) && defined(vim_mkdir)
7774 /* When using 'viewdir' may have to create the directory. */
7775 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaarca472992005-01-21 11:46:23 +00007776 if (vim_mkdir(p_vdir, 0755) != 0)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00007777 EMSG2(_("E739: Cannot create directory: %s"), p_vdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778#endif
7779
7780 fd = open_exfile(fname, eap->forceit, WRITEBIN);
7781 if (fd != NULL)
7782 {
7783#ifdef FEAT_SESSION
7784 if (eap->cmdidx == CMD_mkview)
7785 flagp = &vop_flags;
7786 else
7787 flagp = &ssop_flags;
7788#endif
7789
7790#ifdef MKSESSION_NL
7791 /* "unix" in 'sessionoptions': use NL line separator */
7792 if (view_session && (*flagp & SSOP_UNIX))
7793 mksession_nl = TRUE;
7794#endif
7795
7796 /* Write the version command for :mkvimrc */
7797 if (eap->cmdidx == CMD_mkvimrc)
7798 (void)put_line(fd, "version 6.0");
7799
7800#ifdef FEAT_SESSION
7801 if (eap->cmdidx != CMD_mkview)
7802#endif
7803 {
7804 /* Write setting 'compatible' first, because it has side effects.
7805 * For that same reason only do it when needed. */
7806 if (p_cp)
7807 (void)put_line(fd, "if !&cp | set cp | endif");
7808 else
7809 (void)put_line(fd, "if &cp | set nocp | endif");
7810 }
7811
7812#ifdef FEAT_SESSION
7813 if (!view_session
7814 || (eap->cmdidx == CMD_mksession
7815 && (*flagp & SSOP_OPTIONS)))
7816#endif
7817 failed |= (makemap(fd, NULL) == FAIL
7818 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
7819
7820#ifdef FEAT_SESSION
7821 if (!failed && view_session)
7822 {
7823 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
7824 failed = TRUE;
7825 if (eap->cmdidx == CMD_mksession)
7826 {
7827 char_u dirnow[MAXPATHL]; /* current directory */
7828
7829 /*
7830 * Change to session file's dir.
7831 */
7832 if (mch_dirname(dirnow, MAXPATHL) == FAIL
7833 || mch_chdir((char *)dirnow) != 0)
7834 *dirnow = NUL;
7835 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
7836 {
7837 if (vim_chdirfile(fname) == OK)
7838 shorten_fnames(TRUE);
7839 }
7840 else if (*dirnow != NUL
7841 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
7842 {
7843 (void)mch_chdir((char *)globaldir);
7844 shorten_fnames(TRUE);
7845 }
7846
7847 failed |= (makeopens(fd, dirnow) == FAIL);
7848
7849 /* restore original dir */
7850 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
7851 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
7852 {
7853 if (mch_chdir((char *)dirnow) != 0)
7854 EMSG(_(e_prev_dir));
7855 shorten_fnames(TRUE);
7856 }
7857 }
7858 else
7859 {
7860 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
7861 }
7862 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
7863 == FAIL)
7864 failed = TRUE;
7865 }
7866#endif
7867 failed |= fclose(fd);
7868
7869 if (failed)
7870 EMSG(_(e_write));
7871#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
7872 else if (eap->cmdidx == CMD_mksession)
7873 {
7874 /* successful session write - set this_session var */
7875 char_u tbuf[MAXPATHL];
7876
7877 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
7878 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
7879 }
7880#endif
7881#ifdef MKSESSION_NL
7882 mksession_nl = FALSE;
7883#endif
7884 }
7885
7886#ifdef FEAT_BROWSE
7887theend:
7888 vim_free(browseFile);
7889#endif
7890#ifdef FEAT_SESSION
7891 vim_free(viewFile);
7892#endif
7893}
7894
7895/*
7896 * Open a file for writing for an Ex command, with some checks.
7897 * Return file descriptor, or NULL on failure.
7898 */
7899 FILE *
7900open_exfile(fname, forceit, mode)
7901 char_u *fname;
7902 int forceit;
7903 char *mode; /* "w" for create new file or "a" for append */
7904{
7905 FILE *fd;
7906
7907#ifdef UNIX
7908 /* with Unix it is possible to open a directory */
7909 if (mch_isdir(fname))
7910 {
7911 EMSG2(_(e_isadir2), fname);
7912 return NULL;
7913 }
7914#endif
7915 if (!forceit && *mode != 'a' && vim_fexists(fname))
7916 {
7917 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
7918 return NULL;
7919 }
7920
7921 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
7922 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
7923
7924 return fd;
7925}
7926
7927/*
7928 * ":mark" and ":k".
7929 */
7930 static void
7931ex_mark(eap)
7932 exarg_T *eap;
7933{
7934 pos_T pos;
7935
7936 if (*eap->arg == NUL) /* No argument? */
7937 EMSG(_(e_argreq));
7938 else if (eap->arg[1] != NUL) /* more than one character? */
7939 EMSG(_(e_trailing));
7940 else
7941 {
7942 pos = curwin->w_cursor; /* save curwin->w_cursor */
7943 curwin->w_cursor.lnum = eap->line2;
7944 beginline(BL_WHITE | BL_FIX);
7945 if (setmark(*eap->arg) == FAIL) /* set mark */
7946 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
7947 curwin->w_cursor = pos; /* restore curwin->w_cursor */
7948 }
7949}
7950
7951/*
7952 * Update w_topline, w_leftcol and the cursor position.
7953 */
7954 void
7955update_topline_cursor()
7956{
7957 check_cursor(); /* put cursor on valid line */
7958 update_topline();
7959 if (!curwin->w_p_wrap)
7960 validate_cursor();
7961 update_curswant();
7962}
7963
7964#ifdef FEAT_EX_EXTRA
7965/*
7966 * ":normal[!] {commands}": Execute normal mode commands.
7967 */
7968 static void
7969ex_normal(eap)
7970 exarg_T *eap;
7971{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 int save_msg_scroll = msg_scroll;
7973 int save_restart_edit = restart_edit;
7974 int save_msg_didout = msg_didout;
7975 int save_State = State;
7976 tasave_T tabuf;
7977 int save_insertmode = p_im;
7978 int save_finish_op = finish_op;
7979#ifdef FEAT_MBYTE
7980 char_u *arg = NULL;
7981 int l;
7982 char_u *p;
7983#endif
7984
7985 if (ex_normal_busy >= p_mmd)
7986 {
7987 EMSG(_("E192: Recursive use of :normal too deep"));
7988 return;
7989 }
7990 ++ex_normal_busy;
7991
7992 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
7993 restart_edit = 0; /* don't go to Insert mode */
7994 p_im = FALSE; /* don't use 'insertmode' */
7995
7996#ifdef FEAT_MBYTE
7997 /*
7998 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7999 * this for the K_SPECIAL leading byte, otherwise special keys will not
8000 * work.
8001 */
8002 if (has_mbyte)
8003 {
8004 int len = 0;
8005
8006 /* Count the number of characters to be escaped. */
8007 for (p = eap->arg; *p != NUL; ++p)
8008 {
8009# ifdef FEAT_GUI
8010 if (*p == CSI) /* leadbyte CSI */
8011 len += 2;
8012# endif
8013 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8014 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8015# ifdef FEAT_GUI
8016 || *p == CSI
8017# endif
8018 )
8019 len += 2;
8020 }
8021 if (len > 0)
8022 {
8023 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8024 if (arg != NULL)
8025 {
8026 len = 0;
8027 for (p = eap->arg; *p != NUL; ++p)
8028 {
8029 arg[len++] = *p;
8030# ifdef FEAT_GUI
8031 if (*p == CSI)
8032 {
8033 arg[len++] = KS_EXTRA;
8034 arg[len++] = (int)KE_CSI;
8035 }
8036# endif
8037 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8038 {
8039 arg[len++] = *++p;
8040 if (*p == K_SPECIAL)
8041 {
8042 arg[len++] = KS_SPECIAL;
8043 arg[len++] = KE_FILLER;
8044 }
8045# ifdef FEAT_GUI
8046 else if (*p == CSI)
8047 {
8048 arg[len++] = KS_EXTRA;
8049 arg[len++] = (int)KE_CSI;
8050 }
8051# endif
8052 }
8053 arg[len] = NUL;
8054 }
8055 }
8056 }
8057 }
8058#endif
8059
8060 /*
8061 * Save the current typeahead. This is required to allow using ":normal"
8062 * from an event handler and makes sure we don't hang when the argument
8063 * ends with half a command.
8064 */
8065 save_typeahead(&tabuf);
8066 if (tabuf.typebuf_valid)
8067 {
8068 /*
8069 * Repeat the :normal command for each line in the range. When no
8070 * range given, execute it just once, without positioning the cursor
8071 * first.
8072 */
8073 do
8074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 if (eap->addr_count != 0)
8076 {
8077 curwin->w_cursor.lnum = eap->line1++;
8078 curwin->w_cursor.col = 0;
8079 }
8080
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008081 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082#ifdef FEAT_MBYTE
8083 arg != NULL ? arg :
8084#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008085 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 }
8087 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8088 }
8089
8090 /* Might not return to the main loop when in an event handler. */
8091 update_topline_cursor();
8092
8093 /* Restore the previous typeahead. */
8094 restore_typeahead(&tabuf);
8095
8096 --ex_normal_busy;
8097 msg_scroll = save_msg_scroll;
8098 restart_edit = save_restart_edit;
8099 p_im = save_insertmode;
8100 finish_op = save_finish_op;
8101 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8102
8103 /* Restore the state (needed when called from a function executed for
8104 * 'indentexpr'). */
8105 State = save_State;
8106#ifdef FEAT_MBYTE
8107 vim_free(arg);
8108#endif
8109}
8110
8111/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008112 * ":startinsert" and ":startreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 */
8114 static void
8115ex_startinsert(eap)
8116 exarg_T *eap;
8117{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008118 if (eap->forceit)
8119 {
8120 coladvance((colnr_T)MAXCOL);
8121 curwin->w_curswant = MAXCOL;
8122 curwin->w_set_curswant = FALSE;
8123 }
8124
Bram Moolenaara40c5002005-01-09 21:16:21 +00008125 /* Ignore the command when already in Insert mode. Inserting an
8126 * expression register that invokes a function can do this. */
8127 if (State & INSERT)
8128 return;
8129
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 if (eap->forceit)
8131 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008132 if (eap->cmdidx == CMD_startinsert)
8133 restart_edit = 'a';
8134 else
8135 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 }
8137 else
8138 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008139 if (eap->cmdidx == CMD_startinsert)
8140 restart_edit = 'i';
8141 else
8142 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 curwin->w_curswant = 0; /* avoid MAXCOL */
8144 }
8145}
8146
8147/*
8148 * ":stopinsert"
8149 */
8150/*ARGSUSED*/
8151 static void
8152ex_stopinsert(eap)
8153 exarg_T *eap;
8154{
8155 restart_edit = 0;
8156 stop_insert_mode = TRUE;
8157}
8158#endif
8159
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008160#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8161/*
8162 * Execute normal mode command "cmd".
8163 * "remap" can be REMAP_NONE or REMAP_YES.
8164 */
8165 void
8166exec_normal_cmd(cmd, remap, silent)
8167 char_u *cmd;
8168 int remap;
8169 int silent;
8170{
8171 oparg_T oa;
8172
8173 /*
8174 * Stuff the argument into the typeahead buffer.
8175 * Execute normal_cmd() until there is no typeahead left.
8176 */
8177 clear_oparg(&oa);
8178 finish_op = FALSE;
8179 ins_typebuf(cmd, remap, 0, TRUE, silent);
8180 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8181 && !got_int)
8182 {
8183 update_topline_cursor();
8184 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8185 }
8186}
8187#endif
8188
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189#ifdef FEAT_FIND_ID
8190 static void
8191ex_checkpath(eap)
8192 exarg_T *eap;
8193{
8194 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8195 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8196 (linenr_T)1, (linenr_T)MAXLNUM);
8197}
8198
8199#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8200/*
8201 * ":psearch"
8202 */
8203 static void
8204ex_psearch(eap)
8205 exarg_T *eap;
8206{
8207 g_do_tagpreview = p_pvh;
8208 ex_findpat(eap);
8209 g_do_tagpreview = 0;
8210}
8211#endif
8212
8213 static void
8214ex_findpat(eap)
8215 exarg_T *eap;
8216{
8217 int whole = TRUE;
8218 long n;
8219 char_u *p;
8220 int action;
8221
8222 switch (cmdnames[eap->cmdidx].cmd_name[2])
8223 {
8224 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8225 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8226 action = ACTION_GOTO;
8227 else
8228 action = ACTION_SHOW;
8229 break;
8230 case 'i': /* ":ilist" and ":dlist" */
8231 action = ACTION_SHOW_ALL;
8232 break;
8233 case 'u': /* ":ijump" and ":djump" */
8234 action = ACTION_GOTO;
8235 break;
8236 default: /* ":isplit" and ":dsplit" */
8237 action = ACTION_SPLIT;
8238 break;
8239 }
8240
8241 n = 1;
8242 if (vim_isdigit(*eap->arg)) /* get count */
8243 {
8244 n = getdigits(&eap->arg);
8245 eap->arg = skipwhite(eap->arg);
8246 }
8247 if (*eap->arg == '/') /* Match regexp, not just whole words */
8248 {
8249 whole = FALSE;
8250 ++eap->arg;
8251 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8252 if (*p)
8253 {
8254 *p++ = NUL;
8255 p = skipwhite(p);
8256
8257 /* Check for trailing illegal characters */
8258 if (!ends_excmd(*p))
8259 eap->errmsg = e_trailing;
8260 else
8261 eap->nextcmd = check_nextcmd(p);
8262 }
8263 }
8264 if (!eap->skip)
8265 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8266 whole, !eap->forceit,
8267 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8268 n, action, eap->line1, eap->line2);
8269}
8270#endif
8271
8272#ifdef FEAT_WINDOWS
8273
8274# ifdef FEAT_QUICKFIX
8275/*
8276 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8277 */
8278 static void
8279ex_ptag(eap)
8280 exarg_T *eap;
8281{
8282 g_do_tagpreview = p_pvh;
8283 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8284}
8285
8286/*
8287 * ":pedit"
8288 */
8289 static void
8290ex_pedit(eap)
8291 exarg_T *eap;
8292{
8293 win_T *curwin_save = curwin;
8294
8295 g_do_tagpreview = p_pvh;
8296 prepare_tagpreview();
8297 keep_help_flag = curwin_save->w_buffer->b_help;
8298 do_exedit(eap, NULL);
8299 keep_help_flag = FALSE;
8300 if (curwin != curwin_save && win_valid(curwin_save))
8301 {
8302 /* Return cursor to where we were */
8303 validate_cursor();
8304 redraw_later(VALID);
8305 win_enter(curwin_save, TRUE);
8306 }
8307 g_do_tagpreview = 0;
8308}
8309# endif
8310
8311/*
8312 * ":stag", ":stselect" and ":stjump".
8313 */
8314 static void
8315ex_stag(eap)
8316 exarg_T *eap;
8317{
8318 postponed_split = -1;
8319 postponed_split_flags = cmdmod.split;
8320 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8321 postponed_split_flags = 0;
8322}
8323#endif
8324
8325/*
8326 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8327 */
8328 static void
8329ex_tag(eap)
8330 exarg_T *eap;
8331{
8332 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8333}
8334
8335 static void
8336ex_tag_cmd(eap, name)
8337 exarg_T *eap;
8338 char_u *name;
8339{
8340 int cmd;
8341
8342 switch (name[1])
8343 {
8344 case 'j': cmd = DT_JUMP; /* ":tjump" */
8345 break;
8346 case 's': cmd = DT_SELECT; /* ":tselect" */
8347 break;
8348 case 'p': cmd = DT_PREV; /* ":tprevious" */
8349 break;
8350 case 'N': cmd = DT_PREV; /* ":tNext" */
8351 break;
8352 case 'n': cmd = DT_NEXT; /* ":tnext" */
8353 break;
8354 case 'o': cmd = DT_POP; /* ":pop" */
8355 break;
8356 case 'f': /* ":tfirst" */
8357 case 'r': cmd = DT_FIRST; /* ":trewind" */
8358 break;
8359 case 'l': cmd = DT_LAST; /* ":tlast" */
8360 break;
8361 default: /* ":tag" */
8362#ifdef FEAT_CSCOPE
8363 if (p_cst)
8364 {
8365 do_cstag(eap);
8366 return;
8367 }
8368#endif
8369 cmd = DT_TAG;
8370 break;
8371 }
8372
8373 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8374 eap->forceit, TRUE);
8375}
8376
8377/*
8378 * Evaluate cmdline variables.
8379 *
8380 * change '%' to curbuf->b_ffname
8381 * '#' to curwin->w_altfile
8382 * '<cword>' to word under the cursor
8383 * '<cWORD>' to WORD under the cursor
8384 * '<cfile>' to path name under the cursor
8385 * '<sfile>' to sourced file name
8386 * '<afile>' to file name for autocommand
8387 * '<abuf>' to buffer number for autocommand
8388 * '<amatch>' to matching name for autocommand
8389 *
8390 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8391 * "" for error without a message) and NULL is returned.
8392 * Returns an allocated string if a valid match was found.
8393 * Returns NULL if no match was found. "usedlen" then still contains the
8394 * number of characters to skip.
8395 */
8396 char_u *
8397eval_vars(src, usedlen, lnump, errormsg, srcstart)
8398 char_u *src; /* pointer into commandline */
8399 int *usedlen; /* characters after src that are used */
8400 linenr_T *lnump; /* line number for :e command, or NULL */
8401 char_u **errormsg; /* pointer to error message */
8402 char_u *srcstart; /* beginning of valid memory for src */
8403{
8404 int i;
8405 char_u *s;
8406 char_u *result;
8407 char_u *resultbuf = NULL;
8408 int resultlen;
8409 buf_T *buf;
8410 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8411 int spec_idx;
8412#ifdef FEAT_MODIFY_FNAME
8413 int skip_mod = FALSE;
8414#endif
8415 static char *(spec_str[]) =
8416 {
8417 "%",
8418#define SPEC_PERC 0
8419 "#",
8420#define SPEC_HASH 1
8421 "<cword>", /* cursor word */
8422#define SPEC_CWORD 2
8423 "<cWORD>", /* cursor WORD */
8424#define SPEC_CCWORD 3
8425 "<cfile>", /* cursor path name */
8426#define SPEC_CFILE 4
8427 "<sfile>", /* ":so" file name */
8428#define SPEC_SFILE 5
8429#ifdef FEAT_AUTOCMD
8430 "<afile>", /* autocommand file name */
8431# define SPEC_AFILE 6
8432 "<abuf>", /* autocommand buffer number */
8433# define SPEC_ABUF 7
8434 "<amatch>", /* autocommand match name */
8435# define SPEC_AMATCH 8
8436#endif
8437#ifdef FEAT_CLIENTSERVER
8438 "<client>"
8439# define SPEC_CLIENT 9
8440#endif
8441 };
8442#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
8443
8444#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
8445 char_u strbuf[30];
8446#endif
8447
8448 *errormsg = NULL;
8449
8450 /*
8451 * Check if there is something to do.
8452 */
8453 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
8454 {
8455 *usedlen = (int)STRLEN(spec_str[spec_idx]);
8456 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
8457 break;
8458 }
8459 if (spec_idx == SPEC_COUNT) /* no match */
8460 {
8461 *usedlen = 1;
8462 return NULL;
8463 }
8464
8465 /*
8466 * Skip when preceded with a backslash "\%" and "\#".
8467 * Note: In "\\%" the % is also not recognized!
8468 */
8469 if (src > srcstart && src[-1] == '\\')
8470 {
8471 *usedlen = 0;
8472 STRCPY(src - 1, src); /* remove backslash */
8473 return NULL;
8474 }
8475
8476 /*
8477 * word or WORD under cursor
8478 */
8479 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
8480 {
8481 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
8482 (FIND_IDENT|FIND_STRING) : FIND_STRING);
8483 if (resultlen == 0)
8484 {
8485 *errormsg = (char_u *)"";
8486 return NULL;
8487 }
8488 }
8489
8490 /*
8491 * '#': Alternate file name
8492 * '%': Current file name
8493 * File name under the cursor
8494 * File name for autocommand
8495 * and following modifiers
8496 */
8497 else
8498 {
8499 switch (spec_idx)
8500 {
8501 case SPEC_PERC: /* '%': current file */
8502 if (curbuf->b_fname == NULL)
8503 {
8504 result = (char_u *)"";
8505 valid = 0; /* Must have ":p:h" to be valid */
8506 }
8507 else
8508#ifdef RISCOS
8509 /* Always use the full path for RISC OS if possible. */
8510 result = curbuf->b_ffname;
8511 if (result == NULL)
8512 result = curbuf->b_fname;
8513#else
8514 result = curbuf->b_fname;
8515#endif
8516 break;
8517
8518 case SPEC_HASH: /* '#' or "#99": alternate file */
8519 if (src[1] == '#') /* "##": the argument list */
8520 {
8521 result = arg_all();
8522 resultbuf = result;
8523 *usedlen = 2;
8524#ifdef FEAT_MODIFY_FNAME
8525 skip_mod = TRUE;
8526#endif
8527 break;
8528 }
8529 s = src + 1;
8530 i = (int)getdigits(&s);
8531 *usedlen = (int)(s - src); /* length of what we expand */
8532
8533 buf = buflist_findnr(i);
8534 if (buf == NULL)
8535 {
8536 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
8537 return NULL;
8538 }
8539 if (lnump != NULL)
8540 *lnump = ECMD_LAST;
8541 if (buf->b_fname == NULL)
8542 {
8543 result = (char_u *)"";
8544 valid = 0; /* Must have ":p:h" to be valid */
8545 }
8546 else
8547 result = buf->b_fname;
8548 break;
8549
8550#ifdef FEAT_SEARCHPATH
8551 case SPEC_CFILE: /* file name under cursor */
8552 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
8553 if (result == NULL)
8554 {
8555 *errormsg = (char_u *)"";
8556 return NULL;
8557 }
8558 resultbuf = result; /* remember allocated string */
8559 break;
8560#endif
8561
8562#ifdef FEAT_AUTOCMD
8563 case SPEC_AFILE: /* file name for autocommand */
8564 result = autocmd_fname;
8565 if (result == NULL)
8566 {
8567 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
8568 return NULL;
8569 }
8570 break;
8571
8572 case SPEC_ABUF: /* buffer number for autocommand */
8573 if (autocmd_bufnr <= 0)
8574 {
8575 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
8576 return NULL;
8577 }
8578 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8579 result = strbuf;
8580 break;
8581
8582 case SPEC_AMATCH: /* match name for autocommand */
8583 result = autocmd_match;
8584 if (result == NULL)
8585 {
8586 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
8587 return NULL;
8588 }
8589 break;
8590
8591#endif
8592 case SPEC_SFILE: /* file name for ":so" command */
8593 result = sourcing_name;
8594 if (result == NULL)
8595 {
8596 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
8597 return NULL;
8598 }
8599 break;
8600#if defined(FEAT_CLIENTSERVER)
8601 case SPEC_CLIENT: /* Source of last submitted input */
8602 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
8603 result = strbuf;
8604 break;
8605#endif
8606 }
8607
8608 resultlen = (int)STRLEN(result); /* length of new string */
8609 if (src[*usedlen] == '<') /* remove the file name extension */
8610 {
8611 ++*usedlen;
8612#ifdef RISCOS
8613 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
8614#else
8615 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
8616#endif
8617 resultlen = (int)(s - result);
8618 }
8619#ifdef FEAT_MODIFY_FNAME
8620 else if (!skip_mod)
8621 {
8622 valid |= modify_fname(src, usedlen, &result, &resultbuf,
8623 &resultlen);
8624 if (result == NULL)
8625 {
8626 *errormsg = (char_u *)"";
8627 return NULL;
8628 }
8629 }
8630#endif
8631 }
8632
8633 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8634 {
8635 if (valid != VALID_HEAD + VALID_PATH)
8636 /* xgettext:no-c-format */
8637 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
8638 else
8639 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
8640 result = NULL;
8641 }
8642 else
8643 result = vim_strnsave(result, resultlen);
8644 vim_free(resultbuf);
8645 return result;
8646}
8647
8648/*
8649 * Concatenate all files in the argument list, separated by spaces, and return
8650 * it in one allocated string.
8651 * Spaces and backslashes in the file names are escaped with a backslash.
8652 * Returns NULL when out of memory.
8653 */
8654 static char_u *
8655arg_all()
8656{
8657 int len;
8658 int idx;
8659 char_u *retval = NULL;
8660 char_u *p;
8661
8662 /*
8663 * Do this loop two times:
8664 * first time: compute the total length
8665 * second time: concatenate the names
8666 */
8667 for (;;)
8668 {
8669 len = 0;
8670 for (idx = 0; idx < ARGCOUNT; ++idx)
8671 {
8672 p = alist_name(&ARGLIST[idx]);
8673 if (p != NULL)
8674 {
8675 if (len > 0)
8676 {
8677 /* insert a space in between names */
8678 if (retval != NULL)
8679 retval[len] = ' ';
8680 ++len;
8681 }
8682 for ( ; *p != NUL; ++p)
8683 {
8684 if (*p == ' ' || *p == '\\')
8685 {
8686 /* insert a backslash */
8687 if (retval != NULL)
8688 retval[len] = '\\';
8689 ++len;
8690 }
8691 if (retval != NULL)
8692 retval[len] = *p;
8693 ++len;
8694 }
8695 }
8696 }
8697
8698 /* second time: break here */
8699 if (retval != NULL)
8700 {
8701 retval[len] = NUL;
8702 break;
8703 }
8704
8705 /* allocate memory */
8706 retval = alloc(len + 1);
8707 if (retval == NULL)
8708 break;
8709 }
8710
8711 return retval;
8712}
8713
8714#if defined(FEAT_AUTOCMD) || defined(PROTO)
8715/*
8716 * Expand the <sfile> string in "arg".
8717 *
8718 * Returns an allocated string, or NULL for any error.
8719 */
8720 char_u *
8721expand_sfile(arg)
8722 char_u *arg;
8723{
8724 char_u *errormsg;
8725 int len;
8726 char_u *result;
8727 char_u *newres;
8728 char_u *repl;
8729 int srclen;
8730 char_u *p;
8731
8732 result = vim_strsave(arg);
8733 if (result == NULL)
8734 return NULL;
8735
8736 for (p = result; *p; )
8737 {
8738 if (STRNCMP(p, "<sfile>", 7) != 0)
8739 ++p;
8740 else
8741 {
8742 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
8743 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
8744 if (errormsg != NULL)
8745 {
8746 if (*errormsg)
8747 emsg(errormsg);
8748 vim_free(result);
8749 return NULL;
8750 }
8751 if (repl == NULL) /* no match (cannot happen) */
8752 {
8753 p += srclen;
8754 continue;
8755 }
8756 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8757 newres = alloc(len);
8758 if (newres == NULL)
8759 {
8760 vim_free(repl);
8761 vim_free(result);
8762 return NULL;
8763 }
8764 mch_memmove(newres, result, (size_t)(p - result));
8765 STRCPY(newres + (p - result), repl);
8766 len = (int)STRLEN(newres);
8767 STRCAT(newres, p + srclen);
8768 vim_free(repl);
8769 vim_free(result);
8770 result = newres;
8771 p = newres + len; /* continue after the match */
8772 }
8773 }
8774
8775 return result;
8776}
8777#endif
8778
8779#ifdef FEAT_SESSION
8780static int ses_winsizes __ARGS((FILE *fd, int restore_size));
8781static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
8782static frame_T *ses_skipframe __ARGS((frame_T *fr));
8783static int ses_do_frame __ARGS((frame_T *fr));
8784static int ses_do_win __ARGS((win_T *wp));
8785static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
8786static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
8787static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
8788
8789/*
8790 * Write openfile commands for the current buffers to an .exrc file.
8791 * Return FAIL on error, OK otherwise.
8792 */
8793 static int
8794makeopens(fd, dirnow)
8795 FILE *fd;
8796 char_u *dirnow; /* Current directory name */
8797{
8798 buf_T *buf;
8799 int only_save_windows = TRUE;
8800 int nr;
8801 int cnr = 1;
8802 int restore_size = TRUE;
8803 win_T *wp;
8804 char_u *sname;
8805 win_T *edited_win = NULL;
8806
8807 if (ssop_flags & SSOP_BUFFERS)
8808 only_save_windows = FALSE; /* Save ALL buffers */
8809
8810 /*
8811 * Begin by setting the this_session variable, and then other
8812 * sessionable variables.
8813 */
8814#ifdef FEAT_EVAL
8815 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
8816 return FAIL;
8817 if (ssop_flags & SSOP_GLOBALS)
8818 if (store_session_globals(fd) == FAIL)
8819 return FAIL;
8820#endif
8821
8822 /*
8823 * Close all windows but one.
8824 */
8825 if (put_line(fd, "silent only") == FAIL)
8826 return FAIL;
8827
8828 /*
8829 * Now a :cd command to the session directory or the current directory
8830 */
8831 if (ssop_flags & SSOP_SESDIR)
8832 {
8833 if (put_line(fd, "exe \"cd \" . expand(\"<sfile>:p:h\")") == FAIL)
8834 return FAIL;
8835 }
8836 else if (ssop_flags & SSOP_CURDIR)
8837 {
8838 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
8839 if (sname == NULL
8840 || fprintf(fd, "cd %s", sname) < 0 || put_eol(fd) == FAIL)
8841 return FAIL;
8842 vim_free(sname);
8843 }
8844
8845 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008846 * If there is an empty, unnamed buffer we will wipe it out later.
8847 * Remember the buffer number.
8848 */
8849 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
8850 return FAIL;
8851 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
8852 return FAIL;
8853 if (put_line(fd, "endif") == FAIL)
8854 return FAIL;
8855
8856 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 * Now save the current files, current buffer first.
8858 */
8859 if (put_line(fd, "set shortmess=aoO") == FAIL)
8860 return FAIL;
8861
8862 /* Now put the other buffers into the buffer list */
8863 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8864 {
8865 if (!(only_save_windows && buf->b_nwindows == 0)
8866 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
8867 && buf->b_fname != NULL
8868 && buf->b_p_bl)
8869 {
8870 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
8871 : buf->b_wininfo->wi_fpos.lnum) < 0
8872 || ses_fname(fd, buf, &ssop_flags) == FAIL)
8873 return FAIL;
8874 }
8875 }
8876
8877 /* the global argument list */
8878 if (ses_arglist(fd, "args", &global_alist.al_ga,
8879 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
8880 return FAIL;
8881
8882 if (ssop_flags & SSOP_RESIZE)
8883 {
8884 /* Note: after the restore we still check it worked!*/
8885 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
8886 || put_eol(fd) == FAIL)
8887 return FAIL;
8888 }
8889
8890#ifdef FEAT_GUI
8891 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
8892 {
8893 int x, y;
8894
8895 if (gui_mch_get_winpos(&x, &y) == OK)
8896 {
8897 /* Note: after the restore we still check it worked!*/
8898 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
8899 return FAIL;
8900 }
8901 }
8902#endif
8903
8904 /*
8905 * Before creating the window layout, try loading one file. If this is
8906 * aborted we don't end up with a number of useless windows.
8907 * This may have side effects! (e.g., compressed or network file).
8908 */
8909 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8910 {
8911 if (ses_do_win(wp)
8912 && wp->w_buffer->b_ffname != NULL
8913 && !wp->w_buffer->b_help
8914#ifdef FEAT_QUICKFIX
8915 && !bt_nofile(wp->w_buffer)
8916#endif
8917 )
8918 {
8919 if (fputs("edit ", fd) < 0
8920 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
8921 return FAIL;
8922 if (!wp->w_arg_idx_invalid)
8923 edited_win = wp;
8924 break;
8925 }
8926 }
8927
8928 /*
8929 * Save current window layout.
8930 */
8931 if (put_line(fd, "set splitbelow splitright") == FAIL)
8932 return FAIL;
8933 if (ses_win_rec(fd, topframe) == FAIL)
8934 return FAIL;
8935 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
8936 return FAIL;
8937 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
8938 return FAIL;
8939
8940 /*
8941 * Check if window sizes can be restored (no windows omitted).
8942 * Remember the window number of the current window after restoring.
8943 */
8944 nr = 0;
8945 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
8946 {
8947 if (ses_do_win(wp))
8948 ++nr;
8949 else
8950 restore_size = FALSE;
8951 if (curwin == wp)
8952 cnr = nr;
8953 }
8954
8955 /* Go to the first window. */
8956 if (put_line(fd, "wincmd t") == FAIL)
8957 return FAIL;
8958
8959 /*
8960 * If more than one window, see if sizes can be restored.
8961 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
8962 * resized when moving between windows.
8963 * Do this before restoring the view, so that the topline and the cursor
8964 * can be set. This is done again below.
8965 */
8966 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
8967 return FAIL;
8968 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
8969 return FAIL;
8970
8971 /*
8972 * Restore the view of the window (options, file, cursor, etc.).
8973 */
8974 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8975 {
8976 if (!ses_do_win(wp))
8977 continue;
8978 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
8979 return FAIL;
8980 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
8981 return FAIL;
8982 }
8983
8984 /*
8985 * Restore cursor to the current window if it's not the first one.
8986 */
8987 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
8988 return FAIL;
8989
8990 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008991 * Wipe out an empty unnamed buffer we started in.
8992 */
8993 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
8994 return FAIL;
8995 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
8996 return FAIL;
8997 if (put_line(fd, "endif") == FAIL)
8998 return FAIL;
8999 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9000 return FAIL;
9001
9002 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 * Restore window sizes again after jumping around in windows, because the
9004 * current window has a minimum size while others may not.
9005 */
9006 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9007 return FAIL;
9008
9009 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9010 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9011 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9012 return FAIL;
9013
9014 /*
9015 * Lastly, execute the x.vim file if it exists.
9016 */
9017 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9018 || put_line(fd, "if file_readable(s:sx)") == FAIL
9019 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9020 || put_line(fd, "endif") == FAIL)
9021 return FAIL;
9022
9023 return OK;
9024}
9025
9026 static int
9027ses_winsizes(fd, restore_size)
9028 FILE *fd;
9029 int restore_size;
9030{
9031 int n = 0;
9032 win_T *wp;
9033
9034 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9035 {
9036 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9037 {
9038 if (!ses_do_win(wp))
9039 continue;
9040 ++n;
9041
9042 /* restore height when not full height */
9043 if (wp->w_height + wp->w_status_height < topframe->fr_height
9044 && (fprintf(fd,
9045 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9046 n, (long)wp->w_height, Rows / 2, Rows) < 0
9047 || put_eol(fd) == FAIL))
9048 return FAIL;
9049
9050 /* restore width when not full width */
9051 if (wp->w_width < Columns && (fprintf(fd,
9052 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9053 n, (long)wp->w_width, Columns / 2, Columns) < 0
9054 || put_eol(fd) == FAIL))
9055 return FAIL;
9056 }
9057 }
9058 else
9059 {
9060 /* Just equalise window sizes */
9061 if (put_line(fd, "wincmd =") == FAIL)
9062 return FAIL;
9063 }
9064 return OK;
9065}
9066
9067/*
9068 * Write commands to "fd" to recursively create windows for frame "fr",
9069 * horizontally and vertically split.
9070 * After the commands the last window in the frame is the current window.
9071 * Returns FAIL when writing the commands to "fd" fails.
9072 */
9073 static int
9074ses_win_rec(fd, fr)
9075 FILE *fd;
9076 frame_T *fr;
9077{
9078 frame_T *frc;
9079 int count = 0;
9080
9081 if (fr->fr_layout != FR_LEAF)
9082 {
9083 /* Find first frame that's not skipped and then create a window for
9084 * each following one (first frame is already there). */
9085 frc = ses_skipframe(fr->fr_child);
9086 if (frc != NULL)
9087 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9088 {
9089 /* Make window as big as possible so that we have lots of room
9090 * to split. */
9091 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9092 || put_line(fd, fr->fr_layout == FR_COL
9093 ? "split" : "vsplit") == FAIL)
9094 return FAIL;
9095 ++count;
9096 }
9097
9098 /* Go back to the first window. */
9099 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9100 ? "%dwincmd k" : "%dwincmd h", count) < 0
9101 || put_eol(fd) == FAIL))
9102 return FAIL;
9103
9104 /* Recursively create frames/windows in each window of this column or
9105 * row. */
9106 frc = ses_skipframe(fr->fr_child);
9107 while (frc != NULL)
9108 {
9109 ses_win_rec(fd, frc);
9110 frc = ses_skipframe(frc->fr_next);
9111 /* Go to next window. */
9112 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9113 return FAIL;
9114 }
9115 }
9116 return OK;
9117}
9118
9119/*
9120 * Skip frames that don't contain windows we want to save in the Session.
9121 * Returns NULL when there none.
9122 */
9123 static frame_T *
9124ses_skipframe(fr)
9125 frame_T *fr;
9126{
9127 frame_T *frc;
9128
9129 for (frc = fr; frc != NULL; frc = frc->fr_next)
9130 if (ses_do_frame(frc))
9131 break;
9132 return frc;
9133}
9134
9135/*
9136 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9137 * the Session.
9138 */
9139 static int
9140ses_do_frame(fr)
9141 frame_T *fr;
9142{
9143 frame_T *frc;
9144
9145 if (fr->fr_layout == FR_LEAF)
9146 return ses_do_win(fr->fr_win);
9147 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9148 if (ses_do_frame(frc))
9149 return TRUE;
9150 return FALSE;
9151}
9152
9153/*
9154 * Return non-zero if window "wp" is to be stored in the Session.
9155 */
9156 static int
9157ses_do_win(wp)
9158 win_T *wp;
9159{
9160 if (wp->w_buffer->b_fname == NULL
9161#ifdef FEAT_QUICKFIX
9162 /* When 'buftype' is "nofile" can't restore the window contents. */
9163 || bt_nofile(wp->w_buffer)
9164#endif
9165 )
9166 return (ssop_flags & SSOP_BLANK);
9167 if (wp->w_buffer->b_help)
9168 return (ssop_flags & SSOP_HELP);
9169 return TRUE;
9170}
9171
9172/*
9173 * Write commands to "fd" to restore the view of a window.
9174 * Caller must make sure 'scrolloff' is zero.
9175 */
9176 static int
9177put_view(fd, wp, add_edit, flagp)
9178 FILE *fd;
9179 win_T *wp;
9180 int add_edit; /* add ":edit" command to view */
9181 unsigned *flagp; /* vop_flags or ssop_flags */
9182{
9183 win_T *save_curwin;
9184 int f;
9185 int do_cursor;
9186
9187 /* Always restore cursor position for ":mksession". For ":mkview" only
9188 * when 'viewoptions' contains "cursor". */
9189 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9190
9191 /*
9192 * Local argument list.
9193 */
9194 if (wp->w_alist == &global_alist)
9195 {
9196 if (put_line(fd, "argglobal") == FAIL)
9197 return FAIL;
9198 }
9199 else
9200 {
9201 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9202 flagp == &vop_flags
9203 || !(*flagp & SSOP_CURDIR)
9204 || wp->w_localdir != NULL, flagp) == FAIL)
9205 return FAIL;
9206 }
9207
9208 /* Only when part of a session: restore the argument index. */
9209 if (wp->w_arg_idx != 0 && flagp == &ssop_flags)
9210 {
9211 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9212 || put_eol(fd) == FAIL)
9213 return FAIL;
9214 }
9215
9216 /* Edit the file. Skip this when ":next" already did it. */
9217 if (add_edit && (wp->w_arg_idx == 0 || flagp != &ssop_flags
9218 || wp->w_arg_idx_invalid))
9219 {
9220 /*
9221 * Load the file.
9222 */
9223 if (wp->w_buffer->b_ffname != NULL
9224#ifdef FEAT_QUICKFIX
9225 && !bt_nofile(wp->w_buffer)
9226#endif
9227 )
9228 {
9229 /*
9230 * Editing a file in this buffer: use ":edit file".
9231 * This may have side effects! (e.g., compressed or network file).
9232 */
9233 if (fputs("edit ", fd) < 0
9234 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9235 return FAIL;
9236 }
9237 else
9238 {
9239 /* No file in this buffer, just make it empty. */
9240 if (put_line(fd, "enew") == FAIL)
9241 return FAIL;
9242#ifdef FEAT_QUICKFIX
9243 if (wp->w_buffer->b_ffname != NULL)
9244 {
9245 /* The buffer does have a name, but it's not a file name. */
9246 if (fputs("file ", fd) < 0
9247 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9248 return FAIL;
9249 }
9250#endif
9251 do_cursor = FALSE;
9252 }
9253 }
9254
9255 /*
9256 * Local mappings and abbreviations.
9257 */
9258 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9259 && makemap(fd, wp->w_buffer) == FAIL)
9260 return FAIL;
9261
9262 /*
9263 * Local options. Need to go to the window temporarily.
9264 * Store only local values when using ":mkview" and when ":mksession" is
9265 * used and 'sessionoptions' doesn't include "options".
9266 * Some folding options are always stored when "folds" is included,
9267 * otherwise the folds would not be restored correctly.
9268 */
9269 save_curwin = curwin;
9270 curwin = wp;
9271 curbuf = curwin->w_buffer;
9272 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9273 f = makeset(fd, OPT_LOCAL,
9274 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9275#ifdef FEAT_FOLDING
9276 else if (*flagp & SSOP_FOLDS)
9277 f = makefoldset(fd);
9278#endif
9279 else
9280 f = OK;
9281 curwin = save_curwin;
9282 curbuf = curwin->w_buffer;
9283 if (f == FAIL)
9284 return FAIL;
9285
9286#ifdef FEAT_FOLDING
9287 /*
9288 * Save Folds when 'buftype' is empty and for help files.
9289 */
9290 if ((*flagp & SSOP_FOLDS)
9291 && wp->w_buffer->b_ffname != NULL
9292 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help))
9293 {
9294 if (put_folds(fd, wp) == FAIL)
9295 return FAIL;
9296 }
9297#endif
9298
9299 /*
9300 * Set the cursor after creating folds, since that moves the cursor.
9301 */
9302 if (do_cursor)
9303 {
9304
9305 /* Restore the cursor line in the file and relatively in the
9306 * window. Don't use "G", it changes the jumplist. */
9307 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9308 (long)wp->w_cursor.lnum,
9309 (long)(wp->w_cursor.lnum - wp->w_topline),
9310 (long)wp->w_height / 2, (long)wp->w_height) < 0
9311 || put_eol(fd) == FAIL
9312 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9313 || put_line(fd, "exe s:l") == FAIL
9314 || put_line(fd, "normal! zt") == FAIL
9315 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
9316 || put_eol(fd) == FAIL)
9317 return FAIL;
9318 /* Restore the cursor column and left offset when not wrapping. */
9319 if (wp->w_cursor.col == 0)
9320 {
9321 if (put_line(fd, "normal! 0") == FAIL)
9322 return FAIL;
9323 }
9324 else
9325 {
9326 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
9327 {
9328 if (fprintf(fd,
9329 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
9330 (long)wp->w_cursor.col,
9331 (long)(wp->w_cursor.col - wp->w_leftcol),
9332 (long)wp->w_width / 2, (long)wp->w_width) < 0
9333 || put_eol(fd) == FAIL
9334 || put_line(fd, "if s:c > 0") == FAIL
9335 || fprintf(fd,
9336 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
9337 (long)wp->w_cursor.col) < 0
9338 || put_eol(fd) == FAIL
9339 || put_line(fd, "else") == FAIL
9340 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
9341 || put_eol(fd) == FAIL
9342 || put_line(fd, "endif") == FAIL)
9343 return FAIL;
9344 }
9345 else
9346 {
9347 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
9348 || put_eol(fd) == FAIL)
9349 return FAIL;
9350 }
9351 }
9352 }
9353
9354 /*
9355 * Local directory.
9356 */
9357 if (wp->w_localdir != NULL)
9358 {
9359 if (fputs("lcd ", fd) < 0
9360 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
9361 || put_eol(fd) == FAIL)
9362 return FAIL;
9363 }
9364
9365 return OK;
9366}
9367
9368/*
9369 * Write an argument list to the session file.
9370 * Returns FAIL if writing fails.
9371 */
9372 static int
9373ses_arglist(fd, cmd, gap, fullname, flagp)
9374 FILE *fd;
9375 char *cmd;
9376 garray_T *gap;
9377 int fullname; /* TRUE: use full path name */
9378 unsigned *flagp;
9379{
9380 int i;
9381 char_u buf[MAXPATHL];
9382 char_u *s;
9383
9384 if (gap->ga_len == 0)
9385 return put_line(fd, "silent! argdel *");
9386 if (fputs(cmd, fd) < 0)
9387 return FAIL;
9388 for (i = 0; i < gap->ga_len; ++i)
9389 {
9390 /* NULL file names are skipped (only happens when out of memory). */
9391 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
9392 if (s != NULL)
9393 {
9394 if (fullname)
9395 {
9396 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
9397 s = buf;
9398 }
9399 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
9400 return FAIL;
9401 }
9402 }
9403 return put_eol(fd);
9404}
9405
9406/*
9407 * Write a buffer name to the session file.
9408 * Also ends the line.
9409 * Returns FAIL if writing fails.
9410 */
9411 static int
9412ses_fname(fd, buf, flagp)
9413 FILE *fd;
9414 buf_T *buf;
9415 unsigned *flagp;
9416{
9417 char_u *name;
9418
9419 /* Use the short file name if the current directory is known at the time
9420 * the session file will be sourced. Don't do this for ":mkview", we
9421 * don't know the current directory. */
9422 if (buf->b_sfname != NULL
9423 && flagp == &ssop_flags
9424 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
9425 name = buf->b_sfname;
9426 else
9427 name = buf->b_ffname;
9428 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
9429 return FAIL;
9430 return OK;
9431}
9432
9433/*
9434 * Write a file name to the session file.
9435 * Takes care of the "slash" option in 'sessionoptions' and escapes special
9436 * characters.
9437 * Returns FAIL if writing fails.
9438 */
9439 static int
9440ses_put_fname(fd, name, flagp)
9441 FILE *fd;
9442 char_u *name;
9443 unsigned *flagp;
9444{
9445 char_u *sname;
9446 int retval = OK;
9447 int c;
9448
9449 sname = home_replace_save(NULL, name);
9450 if (sname != NULL)
9451 name = sname;
9452 while (*name != NUL)
9453 {
9454#ifdef FEAT_MBYTE
9455 {
9456 int l;
9457
9458 if (has_mbyte && (l = (*mb_ptr2len_check)(name)) > 1)
9459 {
9460 /* copy a multibyte char */
9461 while (--l >= 0)
9462 {
9463 if (putc(*name, fd) != *name)
9464 retval = FAIL;
9465 ++name;
9466 }
9467 continue;
9468 }
9469 }
9470#endif
9471 c = *name++;
9472 if (c == '\\' && (*flagp & SSOP_SLASH))
9473 /* change a backslash to a forward slash */
9474 c = '/';
9475 else if ((vim_strchr(escape_chars, c) != NULL
9476#ifdef BACKSLASH_IN_FILENAME
9477 && c != '\\'
9478#endif
9479 ) || c == '#' || c == '%')
9480 {
9481 /* escape a special character with a backslash */
9482 if (putc('\\', fd) != '\\')
9483 retval = FAIL;
9484 }
9485 if (putc(c, fd) != c)
9486 retval = FAIL;
9487 }
9488 vim_free(sname);
9489 return retval;
9490}
9491
9492/*
9493 * ":loadview [nr]"
9494 */
9495 static void
9496ex_loadview(eap)
9497 exarg_T *eap;
9498{
9499 char_u *fname;
9500
9501 fname = get_view_file(*eap->arg);
9502 if (fname != NULL)
9503 {
9504 do_source(fname, FALSE, FALSE);
9505 vim_free(fname);
9506 }
9507}
9508
9509/*
9510 * Get the name of the view file for the current buffer.
9511 */
9512 static char_u *
9513get_view_file(c)
9514 int c;
9515{
9516 int len = 0;
9517 char_u *p, *s;
9518 char_u *retval;
9519 char_u *sname;
9520
9521 if (curbuf->b_ffname == NULL)
9522 {
9523 EMSG(_(e_noname));
9524 return NULL;
9525 }
9526 sname = home_replace_save(NULL, curbuf->b_ffname);
9527 if (sname == NULL)
9528 return NULL;
9529
9530 /*
9531 * We want a file name without separators, because we're not going to make
9532 * a directory.
9533 * "normal" path separator -> "=+"
9534 * "=" -> "=="
9535 * ":" path separator -> "=-"
9536 */
9537 for (p = sname; *p; ++p)
9538 if (*p == '=' || vim_ispathsep(*p))
9539 ++len;
9540 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
9541 if (retval != NULL)
9542 {
9543 STRCPY(retval, p_vdir);
9544 add_pathsep(retval);
9545 s = retval + STRLEN(retval);
9546 for (p = sname; *p; ++p)
9547 {
9548 if (*p == '=')
9549 {
9550 *s++ = '=';
9551 *s++ = '=';
9552 }
9553 else if (vim_ispathsep(*p))
9554 {
9555 *s++ = '=';
9556#ifdef MACOS_CLASSIC /* TODO: Is it also needed for MACOS_X? (Dany) */
9557 *s++ = '+';
9558#else
9559# if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
9560 || defined(VMS)
9561 if (*p == ':')
9562 *s++ = '-';
9563 else
9564# endif
9565 *s++ = '+';
9566#endif
9567 }
9568 else
9569 *s++ = *p;
9570 }
9571 *s++ = '=';
9572 *s++ = c;
9573 STRCPY(s, ".vim");
9574 }
9575
9576 vim_free(sname);
9577 return retval;
9578}
9579
9580#endif /* FEAT_SESSION */
9581
9582/*
9583 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
9584 * Return FAIL for a write error.
9585 */
9586 int
9587put_eol(fd)
9588 FILE *fd;
9589{
9590 if (
9591#ifdef USE_CRNL
9592 (
9593# ifdef MKSESSION_NL
9594 !mksession_nl &&
9595# endif
9596 (putc('\r', fd) < 0)) ||
9597#endif
9598 (putc('\n', fd) < 0))
9599 return FAIL;
9600 return OK;
9601}
9602
9603/*
9604 * Write a line to "fd".
9605 * Return FAIL for a write error.
9606 */
9607 int
9608put_line(fd, s)
9609 FILE *fd;
9610 char *s;
9611{
9612 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
9613 return FAIL;
9614 return OK;
9615}
9616
9617#ifdef FEAT_VIMINFO
9618/*
9619 * ":rviminfo" and ":wviminfo".
9620 */
9621 static void
9622ex_viminfo(eap)
9623 exarg_T *eap;
9624{
9625 char_u *save_viminfo;
9626
9627 save_viminfo = p_viminfo;
9628 if (*p_viminfo == NUL)
9629 p_viminfo = (char_u *)"'100";
9630 if (eap->cmdidx == CMD_rviminfo)
9631 {
9632 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
9633 EMSG(_("E195: Cannot open viminfo file for reading"));
9634 }
9635 else
9636 write_viminfo(eap->arg, eap->forceit);
9637 p_viminfo = save_viminfo;
9638}
9639#endif
9640
9641#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
9642 void
9643dialog_msg(buff, format, fname)
9644 char_u *buff;
9645 char *format;
9646 char_u *fname;
9647{
9648 int len;
9649
9650 if (fname == NULL)
9651 fname = (char_u *)_("Untitled");
9652 len = (int)STRLEN(format) + (int)STRLEN(fname);
9653 if (len >= IOSIZE)
9654 sprintf((char *)buff, format, (int)(IOSIZE - STRLEN(format)), fname);
9655 else
9656 sprintf((char *)buff, format, (int)STRLEN(fname), fname);
9657}
9658#endif
9659
9660/*
9661 * ":behave {mswin,xterm}"
9662 */
9663 static void
9664ex_behave(eap)
9665 exarg_T *eap;
9666{
9667 if (STRCMP(eap->arg, "mswin") == 0)
9668 {
9669 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9670 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9671 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9672 set_option_value((char_u *)"keymodel", 0L,
9673 (char_u *)"startsel,stopsel", 0);
9674 }
9675 else if (STRCMP(eap->arg, "xterm") == 0)
9676 {
9677 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9678 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9679 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9680 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9681 }
9682 else
9683 EMSG2(_(e_invarg2), eap->arg);
9684}
9685
9686#ifdef FEAT_AUTOCMD
9687static int filetype_detect = FALSE;
9688static int filetype_plugin = FALSE;
9689static int filetype_indent = FALSE;
9690
9691/*
9692 * ":filetype [plugin] [indent] {on,off,detect}"
9693 * on: Load the filetype.vim file to install autocommands for file types.
9694 * off: Load the ftoff.vim file to remove all autocommands for file types.
9695 * plugin on: load filetype.vim and ftplugin.vim
9696 * plugin off: load ftplugof.vim
9697 * indent on: load filetype.vim and indent.vim
9698 * indent off: load indoff.vim
9699 */
9700 static void
9701ex_filetype(eap)
9702 exarg_T *eap;
9703{
9704 char_u *arg = eap->arg;
9705 int plugin = FALSE;
9706 int indent = FALSE;
9707
9708 if (*eap->arg == NUL)
9709 {
9710 /* Print current status. */
9711 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
9712 filetype_detect ? "ON" : "OFF",
9713 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9714 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9715 return;
9716 }
9717
9718 /* Accept "plugin" and "indent" in any order. */
9719 for (;;)
9720 {
9721 if (STRNCMP(arg, "plugin", 6) == 0)
9722 {
9723 plugin = TRUE;
9724 arg = skipwhite(arg + 6);
9725 continue;
9726 }
9727 if (STRNCMP(arg, "indent", 6) == 0)
9728 {
9729 indent = TRUE;
9730 arg = skipwhite(arg + 6);
9731 continue;
9732 }
9733 break;
9734 }
9735 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9736 {
9737 if (*arg == 'o' || !filetype_detect)
9738 {
9739 cmd_runtime((char_u *)FILETYPE_FILE, TRUE);
9740 filetype_detect = TRUE;
9741 if (plugin)
9742 {
9743 cmd_runtime((char_u *)FTPLUGIN_FILE, TRUE);
9744 filetype_plugin = TRUE;
9745 }
9746 if (indent)
9747 {
9748 cmd_runtime((char_u *)INDENT_FILE, TRUE);
9749 filetype_indent = TRUE;
9750 }
9751 }
9752 if (*arg == 'd')
9753 {
9754 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00009755 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 }
9757 }
9758 else if (STRCMP(arg, "off") == 0)
9759 {
9760 if (plugin || indent)
9761 {
9762 if (plugin)
9763 {
9764 cmd_runtime((char_u *)FTPLUGOF_FILE, TRUE);
9765 filetype_plugin = FALSE;
9766 }
9767 if (indent)
9768 {
9769 cmd_runtime((char_u *)INDOFF_FILE, TRUE);
9770 filetype_indent = FALSE;
9771 }
9772 }
9773 else
9774 {
9775 cmd_runtime((char_u *)FTOFF_FILE, TRUE);
9776 filetype_detect = FALSE;
9777 }
9778 }
9779 else
9780 EMSG2(_(e_invarg2), arg);
9781}
9782
9783/*
9784 * ":setfiletype {name}"
9785 */
9786 static void
9787ex_setfiletype(eap)
9788 exarg_T *eap;
9789{
9790 if (!did_filetype)
9791 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
9792}
9793#endif
9794
9795/*ARGSUSED*/
9796 static void
9797ex_digraphs(eap)
9798 exarg_T *eap;
9799{
9800#ifdef FEAT_DIGRAPHS
9801 if (*eap->arg != NUL)
9802 putdigraph(eap->arg);
9803 else
9804 listdigraphs();
9805#else
9806 EMSG(_("E196: No digraphs in this version"));
9807#endif
9808}
9809
9810 static void
9811ex_set(eap)
9812 exarg_T *eap;
9813{
9814 int flags = 0;
9815
9816 if (eap->cmdidx == CMD_setlocal)
9817 flags = OPT_LOCAL;
9818 else if (eap->cmdidx == CMD_setglobal)
9819 flags = OPT_GLOBAL;
9820#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
9821 if (cmdmod.browse && flags == 0)
9822 ex_options(eap);
9823 else
9824#endif
9825 (void)do_set(eap->arg, flags);
9826}
9827
9828#ifdef FEAT_SEARCH_EXTRA
9829/*
9830 * ":nohlsearch"
9831 */
9832/*ARGSUSED*/
9833 static void
9834ex_nohlsearch(eap)
9835 exarg_T *eap;
9836{
9837 no_hlsearch = TRUE;
9838 redraw_all_later(NOT_VALID);
9839}
9840
9841/*
9842 * ":match {group} {pattern}"
9843 * Sets nextcmd to the start of the next command, if any. Also called when
9844 * skipping commands to find the next command.
9845 */
9846 static void
9847ex_match(eap)
9848 exarg_T *eap;
9849{
9850 char_u *p;
9851 char_u *end;
9852 int c;
9853
9854 /* First clear any old pattern. */
9855 if (!eap->skip)
9856 {
9857 vim_free(curwin->w_match.regprog);
9858 curwin->w_match.regprog = NULL;
9859 redraw_later(NOT_VALID); /* always need a redraw */
9860 }
9861
9862 if (ends_excmd(*eap->arg))
9863 end = eap->arg;
9864 else if ((STRNICMP(eap->arg, "none", 4) == 0
9865 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
9866 end = eap->arg + 4;
9867 else
9868 {
9869 p = skiptowhite(eap->arg);
9870 if (!eap->skip)
9871 {
9872 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
9873 if (curwin->w_match_id == 0)
9874 {
9875 EMSG2(_(e_nogroup), eap->arg);
9876 return;
9877 }
9878 }
9879 p = skipwhite(p);
9880 if (*p == NUL)
9881 {
9882 /* There must be two arguments. */
9883 EMSG2(_(e_invarg2), eap->arg);
9884 return;
9885 }
9886 end = skip_regexp(p + 1, *p, TRUE, NULL);
9887 if (!eap->skip)
9888 {
9889 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
9890 {
9891 eap->errmsg = e_trailing;
9892 return;
9893 }
9894
9895 c = *end;
9896 *end = NUL;
9897 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
9898 *end = c;
9899 if (curwin->w_match.regprog == NULL)
9900 {
9901 EMSG2(_(e_invarg2), p);
9902 return;
9903 }
9904 }
9905 }
9906 eap->nextcmd = find_nextcmd(end);
9907}
9908#endif
9909
9910#ifdef FEAT_CRYPT
9911/*
9912 * ":X": Get crypt key
9913 */
9914/*ARGSUSED*/
9915 static void
9916ex_X(eap)
9917 exarg_T *eap;
9918{
9919 (void)get_crypt_key(TRUE, TRUE);
9920}
9921#endif
9922
9923#ifdef FEAT_FOLDING
9924 static void
9925ex_fold(eap)
9926 exarg_T *eap;
9927{
9928 if (foldManualAllowed(TRUE))
9929 foldCreate(eap->line1, eap->line2);
9930}
9931
9932 static void
9933ex_foldopen(eap)
9934 exarg_T *eap;
9935{
9936 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9937 eap->forceit, FALSE);
9938}
9939
9940 static void
9941ex_folddo(eap)
9942 exarg_T *eap;
9943{
9944 linenr_T lnum;
9945
9946 /* First set the marks for all lines closed/open. */
9947 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9948 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9949 ml_setmarked(lnum);
9950
9951 /* Execute the command on the marked lines. */
9952 global_exe(eap->arg);
9953 ml_clearmarked(); /* clear rest of the marks */
9954}
9955#endif