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