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