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