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