blob: 7415e4629c8b20c7cfdb9ea765b026256533fab7 [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;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003208#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3209 /* Avoid that the assignment uses EXPAND_FILES again. */
3210 if (compl != EXPAND_USER_DEFINED)
3211 compl = EXPAND_ENV_VARS;
3212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 }
3214 }
3215 }
3216
3217/*
3218 * 6. switch on command name
3219 */
3220 switch (cmdidx)
3221 {
3222 case CMD_cd:
3223 case CMD_chdir:
3224 case CMD_lcd:
3225 case CMD_lchdir:
3226 if (xp->xp_context == EXPAND_FILES)
3227 xp->xp_context = EXPAND_DIRECTORIES;
3228 break;
3229 case CMD_help:
3230 xp->xp_context = EXPAND_HELP;
3231 xp->xp_pattern = arg;
3232 break;
3233
3234 /* Command modifiers: return the argument. */
3235 case CMD_aboveleft:
3236 case CMD_belowright:
3237 case CMD_botright:
3238 case CMD_browse:
3239 case CMD_confirm:
3240 case CMD_folddoclosed:
3241 case CMD_folddoopen:
3242 case CMD_hide:
3243 case CMD_keepjumps:
3244 case CMD_keepmarks:
3245 case CMD_leftabove:
3246 case CMD_lockmarks:
3247 case CMD_rightbelow:
3248 case CMD_silent:
3249 case CMD_topleft:
3250 case CMD_verbose:
3251 case CMD_vertical:
3252 return arg;
3253
3254#ifdef FEAT_SEARCH_EXTRA
3255 case CMD_match:
3256 if (*arg == NUL || !ends_excmd(*arg))
3257 {
3258 /* Dummy call to clear variables. */
3259 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3260 xp->xp_context = EXPAND_HIGHLIGHT;
3261 xp->xp_pattern = arg;
3262 arg = skipwhite(skiptowhite(arg));
3263 if (*arg != NUL)
3264 {
3265 xp->xp_context = EXPAND_NOTHING;
3266 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3267 }
3268 }
3269 return find_nextcmd(arg);
3270#endif
3271
3272#ifdef FEAT_CMDL_COMPL
3273/*
3274 * All completion for the +cmdline_compl feature goes here.
3275 */
3276
3277# ifdef FEAT_USR_CMDS
3278 case CMD_command:
3279 /* Check for attributes */
3280 while (*arg == '-')
3281 {
3282 arg++; /* Skip "-" */
3283 p = skiptowhite(arg);
3284 if (*p == NUL)
3285 {
3286 /* Cursor is still in the attribute */
3287 p = vim_strchr(arg, '=');
3288 if (p == NULL)
3289 {
3290 /* No "=", so complete attribute names */
3291 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3292 xp->xp_pattern = arg;
3293 return NULL;
3294 }
3295
3296 /* For the -complete and -nargs attributes, we complete
3297 * their arguments as well.
3298 */
3299 if (STRNICMP(arg, "complete", p - arg) == 0)
3300 {
3301 xp->xp_context = EXPAND_USER_COMPLETE;
3302 xp->xp_pattern = p + 1;
3303 return NULL;
3304 }
3305 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3306 {
3307 xp->xp_context = EXPAND_USER_NARGS;
3308 xp->xp_pattern = p + 1;
3309 return NULL;
3310 }
3311 return NULL;
3312 }
3313 arg = skipwhite(p);
3314 }
3315
3316 /* After the attributes comes the new command name */
3317 p = skiptowhite(arg);
3318 if (*p == NUL)
3319 {
3320 xp->xp_context = EXPAND_USER_COMMANDS;
3321 xp->xp_pattern = arg;
3322 break;
3323 }
3324
3325 /* And finally comes a normal command */
3326 return skipwhite(p);
3327
3328 case CMD_delcommand:
3329 xp->xp_context = EXPAND_USER_COMMANDS;
3330 xp->xp_pattern = arg;
3331 break;
3332# endif
3333
3334 case CMD_global:
3335 case CMD_vglobal:
3336 delim = *arg; /* get the delimiter */
3337 if (delim)
3338 ++arg; /* skip delimiter if there is one */
3339
3340 while (arg[0] != NUL && arg[0] != delim)
3341 {
3342 if (arg[0] == '\\' && arg[1] != NUL)
3343 ++arg;
3344 ++arg;
3345 }
3346 if (arg[0] != NUL)
3347 return arg + 1;
3348 break;
3349 case CMD_and:
3350 case CMD_substitute:
3351 delim = *arg;
3352 if (delim)
3353 {
3354 /* skip "from" part */
3355 ++arg;
3356 arg = skip_regexp(arg, delim, p_magic, NULL);
3357 }
3358 /* skip "to" part */
3359 while (arg[0] != NUL && arg[0] != delim)
3360 {
3361 if (arg[0] == '\\' && arg[1] != NUL)
3362 ++arg;
3363 ++arg;
3364 }
3365 if (arg[0] != NUL) /* skip delimiter */
3366 ++arg;
3367 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3368 ++arg;
3369 if (arg[0] != NUL)
3370 return arg;
3371 break;
3372 case CMD_isearch:
3373 case CMD_dsearch:
3374 case CMD_ilist:
3375 case CMD_dlist:
3376 case CMD_ijump:
3377 case CMD_psearch:
3378 case CMD_djump:
3379 case CMD_isplit:
3380 case CMD_dsplit:
3381 arg = skipwhite(skipdigits(arg)); /* skip count */
3382 if (*arg == '/') /* Match regexp, not just whole words */
3383 {
3384 for (++arg; *arg && *arg != '/'; arg++)
3385 if (*arg == '\\' && arg[1] != NUL)
3386 arg++;
3387 if (*arg)
3388 {
3389 arg = skipwhite(arg + 1);
3390
3391 /* Check for trailing illegal characters */
3392 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3393 xp->xp_context = EXPAND_NOTHING;
3394 else
3395 return arg;
3396 }
3397 }
3398 break;
3399#ifdef FEAT_AUTOCMD
3400 case CMD_autocmd:
3401 return set_context_in_autocmd(xp, arg, FALSE);
3402
3403 case CMD_doautocmd:
3404 return set_context_in_autocmd(xp, arg, TRUE);
3405#endif
3406 case CMD_set:
3407 set_context_in_set_cmd(xp, arg, 0);
3408 break;
3409 case CMD_setglobal:
3410 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3411 break;
3412 case CMD_setlocal:
3413 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3414 break;
3415 case CMD_tag:
3416 case CMD_stag:
3417 case CMD_ptag:
3418 case CMD_tselect:
3419 case CMD_stselect:
3420 case CMD_ptselect:
3421 case CMD_tjump:
3422 case CMD_stjump:
3423 case CMD_ptjump:
3424 xp->xp_context = EXPAND_TAGS;
3425 xp->xp_pattern = arg;
3426 break;
3427 case CMD_augroup:
3428 xp->xp_context = EXPAND_AUGROUP;
3429 xp->xp_pattern = arg;
3430 break;
3431#ifdef FEAT_SYN_HL
3432 case CMD_syntax:
3433 set_context_in_syntax_cmd(xp, arg);
3434 break;
3435#endif
3436#ifdef FEAT_EVAL
3437 case CMD_let:
3438 case CMD_if:
3439 case CMD_elseif:
3440 case CMD_while:
3441 case CMD_echo:
3442 case CMD_echon:
3443 case CMD_execute:
3444 case CMD_echomsg:
3445 case CMD_echoerr:
3446 case CMD_call:
3447 case CMD_return:
3448 set_context_for_expression(xp, arg, cmdidx);
3449 break;
3450
3451 case CMD_unlet:
3452 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3453 arg = xp->xp_pattern + 1;
3454 xp->xp_context = EXPAND_USER_VARS;
3455 xp->xp_pattern = arg;
3456 break;
3457
3458 case CMD_function:
3459 case CMD_delfunction:
3460 xp->xp_context = EXPAND_USER_FUNC;
3461 xp->xp_pattern = arg;
3462 break;
3463
3464 case CMD_echohl:
3465 xp->xp_context = EXPAND_HIGHLIGHT;
3466 xp->xp_pattern = arg;
3467 break;
3468#endif
3469 case CMD_highlight:
3470 set_context_in_highlight_cmd(xp, arg);
3471 break;
3472#ifdef FEAT_LISTCMDS
3473 case CMD_bdelete:
3474 case CMD_bwipeout:
3475 case CMD_bunload:
3476 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3477 arg = xp->xp_pattern + 1;
3478 /*FALLTHROUGH*/
3479 case CMD_buffer:
3480 case CMD_sbuffer:
3481 case CMD_checktime:
3482 xp->xp_context = EXPAND_BUFFERS;
3483 xp->xp_pattern = arg;
3484 break;
3485#endif
3486#ifdef FEAT_USR_CMDS
3487 case CMD_USER:
3488 case CMD_USER_BUF:
3489 if (compl != EXPAND_NOTHING)
3490 {
3491 /* XFILE: file names are handled above */
3492 if (!(argt & XFILE))
3493 {
3494# ifdef FEAT_MENU
3495 if (compl == EXPAND_MENUS)
3496 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3497# endif
3498 if (compl == EXPAND_COMMANDS)
3499 return arg;
3500 if (compl == EXPAND_MAPPINGS)
3501 return set_context_in_map_cmd(xp, (char_u *)"map",
3502 arg, forceit, FALSE, FALSE, CMD_map);
3503 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3504 arg = xp->xp_pattern + 1;
3505 xp->xp_pattern = arg;
3506 }
3507 xp->xp_context = compl;
3508 }
3509 break;
3510#endif
3511 case CMD_map: case CMD_noremap:
3512 case CMD_nmap: case CMD_nnoremap:
3513 case CMD_vmap: case CMD_vnoremap:
3514 case CMD_omap: case CMD_onoremap:
3515 case CMD_imap: case CMD_inoremap:
3516 case CMD_cmap: case CMD_cnoremap:
3517 return set_context_in_map_cmd(xp, cmd, arg, forceit,
3518 FALSE, FALSE, cmdidx);
3519 case CMD_unmap:
3520 case CMD_nunmap:
3521 case CMD_vunmap:
3522 case CMD_ounmap:
3523 case CMD_iunmap:
3524 case CMD_cunmap:
3525 return set_context_in_map_cmd(xp, cmd, arg, forceit,
3526 FALSE, TRUE, cmdidx);
3527 case CMD_abbreviate: case CMD_noreabbrev:
3528 case CMD_cabbrev: case CMD_cnoreabbrev:
3529 case CMD_iabbrev: case CMD_inoreabbrev:
3530 return set_context_in_map_cmd(xp, cmd, arg, forceit,
3531 TRUE, FALSE, cmdidx);
3532 case CMD_unabbreviate:
3533 case CMD_cunabbrev:
3534 case CMD_iunabbrev:
3535 return set_context_in_map_cmd(xp, cmd, arg, forceit,
3536 TRUE, TRUE, cmdidx);
3537#ifdef FEAT_MENU
3538 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3539 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3540 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3541 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3542 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3543 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3544 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3545 case CMD_tmenu: case CMD_tunmenu:
3546 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3547 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3548#endif
3549
3550 case CMD_colorscheme:
3551 xp->xp_context = EXPAND_COLORS;
3552 xp->xp_pattern = arg;
3553 break;
3554
3555 case CMD_compiler:
3556 xp->xp_context = EXPAND_COMPILER;
3557 xp->xp_pattern = arg;
3558 break;
3559
3560#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3561 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3562 case CMD_language:
3563 if (*skiptowhite(arg) == NUL)
3564 {
3565 xp->xp_context = EXPAND_LANGUAGE;
3566 xp->xp_pattern = arg;
3567 }
3568 else
3569 xp->xp_context = EXPAND_NOTHING;
3570 break;
3571#endif
3572
3573#endif /* FEAT_CMDL_COMPL */
3574
3575 default:
3576 break;
3577 }
3578 return NULL;
3579}
3580
3581/*
3582 * skip a range specifier of the form: addr [,addr] [;addr] ..
3583 *
3584 * Backslashed delimiters after / or ? will be skipped, and commands will
3585 * not be expanded between /'s and ?'s or after "'".
3586 *
3587 * Returns the "cmd" pointer advanced to beyond the range.
3588 */
3589 char_u *
3590skip_range(cmd, ctx)
3591 char_u *cmd;
3592 int *ctx; /* pointer to xp_context or NULL */
3593{
3594 int delim;
3595
3596 while (*cmd != NUL && (vim_isspace(*cmd) || VIM_ISDIGIT(*cmd) ||
3597 vim_strchr((char_u *)".$%'/?-+,;", *cmd) != NULL))
3598 {
3599 if (*cmd == '\'')
3600 {
3601 if (*++cmd == NUL && ctx != NULL)
3602 *ctx = EXPAND_NOTHING;
3603 }
3604 else if (*cmd == '/' || *cmd == '?')
3605 {
3606 delim = *cmd++;
3607 while (*cmd != NUL && *cmd != delim)
3608 if (*cmd++ == '\\' && *cmd != NUL)
3609 ++cmd;
3610 if (*cmd == NUL && ctx != NULL)
3611 *ctx = EXPAND_NOTHING;
3612 }
3613 if (*cmd != NUL)
3614 ++cmd;
3615 }
3616 return cmd;
3617}
3618
3619/*
3620 * get a single EX address
3621 *
3622 * Set ptr to the next character after the part that was interpreted.
3623 * Set ptr to NULL when an error is encountered.
3624 *
3625 * Return MAXLNUM when no Ex address was found.
3626 */
3627 static linenr_T
3628get_address(ptr, skip, to_other_file)
3629 char_u **ptr;
3630 int skip; /* only skip the address, don't use it */
3631 int to_other_file; /* flag: may jump to other file */
3632{
3633 int c;
3634 int i;
3635 long n;
3636 char_u *cmd;
3637 pos_T pos;
3638 pos_T *fp;
3639 linenr_T lnum;
3640
3641 cmd = skipwhite(*ptr);
3642 lnum = MAXLNUM;
3643 do
3644 {
3645 switch (*cmd)
3646 {
3647 case '.': /* '.' - Cursor position */
3648 ++cmd;
3649 lnum = curwin->w_cursor.lnum;
3650 break;
3651
3652 case '$': /* '$' - last line */
3653 ++cmd;
3654 lnum = curbuf->b_ml.ml_line_count;
3655 break;
3656
3657 case '\'': /* ''' - mark */
3658 if (*++cmd == NUL)
3659 {
3660 cmd = NULL;
3661 goto error;
3662 }
3663 if (skip)
3664 ++cmd;
3665 else
3666 {
3667 /* Only accept a mark in another file when it is
3668 * used by itself: ":'M". */
3669 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3670 ++cmd;
3671 if (fp == (pos_T *)-1)
3672 /* Jumped to another file. */
3673 lnum = curwin->w_cursor.lnum;
3674 else
3675 {
3676 if (check_mark(fp) == FAIL)
3677 {
3678 cmd = NULL;
3679 goto error;
3680 }
3681 lnum = fp->lnum;
3682 }
3683 }
3684 break;
3685
3686 case '/':
3687 case '?': /* '/' or '?' - search */
3688 c = *cmd++;
3689 if (skip) /* skip "/pat/" */
3690 {
3691 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3692 if (*cmd == c)
3693 ++cmd;
3694 }
3695 else
3696 {
3697 pos = curwin->w_cursor; /* save curwin->w_cursor */
3698 /*
3699 * When '/' or '?' follows another address, start
3700 * from there.
3701 */
3702 if (lnum != MAXLNUM)
3703 curwin->w_cursor.lnum = lnum;
3704 /*
3705 * Start a forward search at the end of the line.
3706 * Start a backward search at the start of the line.
3707 * This makes sure we never match in the current
3708 * line, and can match anywhere in the
3709 * next/previous line.
3710 */
3711 if (c == '/')
3712 curwin->w_cursor.col = MAXCOL;
3713 else
3714 curwin->w_cursor.col = 0;
3715 searchcmdlen = 0;
3716 if (!do_search(NULL, c, cmd, 1L,
3717 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3718 {
3719 curwin->w_cursor = pos;
3720 cmd = NULL;
3721 goto error;
3722 }
3723 lnum = curwin->w_cursor.lnum;
3724 curwin->w_cursor = pos;
3725 /* adjust command string pointer */
3726 cmd += searchcmdlen;
3727 }
3728 break;
3729
3730 case '\\': /* "\?", "\/" or "\&", repeat search */
3731 ++cmd;
3732 if (*cmd == '&')
3733 i = RE_SUBST;
3734 else if (*cmd == '?' || *cmd == '/')
3735 i = RE_SEARCH;
3736 else
3737 {
3738 EMSG(_(e_backslash));
3739 cmd = NULL;
3740 goto error;
3741 }
3742
3743 if (!skip)
3744 {
3745 /*
3746 * When search follows another address, start from
3747 * there.
3748 */
3749 if (lnum != MAXLNUM)
3750 pos.lnum = lnum;
3751 else
3752 pos.lnum = curwin->w_cursor.lnum;
3753
3754 /*
3755 * Start the search just like for the above
3756 * do_search().
3757 */
3758 if (*cmd != '?')
3759 pos.col = MAXCOL;
3760 else
3761 pos.col = 0;
3762 if (searchit(curwin, curbuf, &pos,
3763 *cmd == '?' ? BACKWARD : FORWARD,
3764 (char_u *)"", 1L,
3765 SEARCH_MSG + SEARCH_START, i) != FAIL)
3766 lnum = pos.lnum;
3767 else
3768 {
3769 cmd = NULL;
3770 goto error;
3771 }
3772 }
3773 ++cmd;
3774 break;
3775
3776 default:
3777 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3778 lnum = getdigits(&cmd);
3779 }
3780
3781 for (;;)
3782 {
3783 cmd = skipwhite(cmd);
3784 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3785 break;
3786
3787 if (lnum == MAXLNUM)
3788 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3789 if (VIM_ISDIGIT(*cmd))
3790 i = '+'; /* "number" is same as "+number" */
3791 else
3792 i = *cmd++;
3793 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3794 n = 1;
3795 else
3796 n = getdigits(&cmd);
3797 if (i == '-')
3798 lnum -= n;
3799 else
3800 lnum += n;
3801 }
3802 } while (*cmd == '/' || *cmd == '?');
3803
3804error:
3805 *ptr = cmd;
3806 return lnum;
3807}
3808
3809/*
3810 * Function called for command which is Not Implemented. NI!
3811 */
3812 void
3813ex_ni(eap)
3814 exarg_T *eap;
3815{
3816 if (!eap->skip)
3817 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3818}
3819
3820#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003821 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822/*
3823 * Function called for script command which is Not Implemented. NI!
3824 * Skips over ":perl <<EOF" constructs.
3825 */
3826 static void
3827ex_script_ni(eap)
3828 exarg_T *eap;
3829{
3830 if (!eap->skip)
3831 ex_ni(eap);
3832 else
3833 vim_free(script_get(eap, eap->arg));
3834}
3835#endif
3836
3837/*
3838 * Check range in Ex command for validity.
3839 * Return NULL when valid, error message when invalid.
3840 */
3841 static char_u *
3842invalid_range(eap)
3843 exarg_T *eap;
3844{
3845 if ( eap->line1 < 0
3846 || eap->line2 < 0
3847 || eap->line1 > eap->line2
3848 || ((eap->argt & RANGE)
3849 && !(eap->argt & NOTADR)
3850 && eap->line2 > curbuf->b_ml.ml_line_count
3851#ifdef FEAT_DIFF
3852 + (eap->cmdidx == CMD_diffget)
3853#endif
3854 ))
3855 return (char_u *)_(e_invrange);
3856 return NULL;
3857}
3858
3859/*
3860 * Correct the range for zero line number, if required.
3861 */
3862 static void
3863correct_range(eap)
3864 exarg_T *eap;
3865{
3866 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
3867 {
3868 if (eap->line1 == 0)
3869 eap->line1 = 1;
3870 if (eap->line2 == 0)
3871 eap->line2 = 1;
3872 }
3873}
3874
3875/*
3876 * Expand file name in Ex command argument.
3877 * Return FAIL for failure, OK otherwise.
3878 */
3879 int
3880expand_filename(eap, cmdlinep, errormsgp)
3881 exarg_T *eap;
3882 char_u **cmdlinep;
3883 char_u **errormsgp;
3884{
3885 int has_wildcards; /* need to expand wildcards */
3886 char_u *repl;
3887 int srclen;
3888 char_u *p;
3889 int n;
3890
3891 /*
3892 * Decide to expand wildcards *before* replacing '%', '#', etc. If
3893 * the file name contains a wildcard it should not cause expanding.
3894 * (it will be expanded anyway if there is a wildcard before replacing).
3895 */
3896 has_wildcards = mch_has_wildcard(eap->arg);
3897 for (p = eap->arg; *p; )
3898 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003899#ifdef FEAT_EVAL
3900 /* Skip over `=expr`, wildcards in it are not expanded. */
3901 if (p[0] == '`' && p[1] == '=')
3902 {
3903 p += 2;
3904 (void)skip_expr(&p);
3905 if (*p == '`')
3906 ++p;
3907 continue;
3908 }
3909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 /*
3911 * Quick check if this cannot be the start of a special string.
3912 * Also removes backslash before '%', '#' and '<'.
3913 */
3914 if (vim_strchr((char_u *)"%#<", *p) == NULL)
3915 {
3916 ++p;
3917 continue;
3918 }
3919
3920 /*
3921 * Try to find a match at this position.
3922 */
3923 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
3924 if (*errormsgp != NULL) /* error detected */
3925 return FAIL;
3926 if (repl == NULL) /* no match found */
3927 {
3928 p += srclen;
3929 continue;
3930 }
3931
3932 /* Need to escape white space et al. with a backslash. Don't do this
3933 * for shell commands (may have to use quotes instead). Don't do this
3934 * for non-unix systems when there is a single argument (spaces don't
3935 * separate arguments then). */
3936 if (!eap->usefilter
3937 && eap->cmdidx != CMD_bang
3938 && eap->cmdidx != CMD_make
3939 && eap->cmdidx != CMD_grep
3940 && eap->cmdidx != CMD_grepadd
3941#ifndef UNIX
3942 && !(eap->argt & NOSPC)
3943#endif
3944 )
3945 {
3946 char_u *l;
3947#ifdef BACKSLASH_IN_FILENAME
3948 /* Don't escape a backslash here, because rem_backslash() doesn't
3949 * remove it later. */
3950 static char_u *nobslash = (char_u *)" \t\"|";
3951# define ESCAPE_CHARS nobslash
3952#else
3953# define ESCAPE_CHARS escape_chars
3954#endif
3955
3956 for (l = repl; *l; ++l)
3957 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
3958 {
3959 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
3960 if (l != NULL)
3961 {
3962 vim_free(repl);
3963 repl = l;
3964 }
3965 break;
3966 }
3967 }
3968
3969 /* For a shell command a '!' must be escaped. */
3970 if ((eap->usefilter || eap->cmdidx == CMD_bang)
3971 && vim_strchr(repl, '!') != NULL)
3972 {
3973 char_u *l;
3974
3975 l = vim_strsave_escaped(repl, (char_u *)"!");
3976 if (l != NULL)
3977 {
3978 vim_free(repl);
3979 repl = l;
3980 /* For a sh-like shell escape it another time. */
3981 if (strstr((char *)p_sh, "sh") != NULL)
3982 {
3983 l = vim_strsave_escaped(repl, (char_u *)"!");
3984 if (l != NULL)
3985 {
3986 vim_free(repl);
3987 repl = l;
3988 }
3989 }
3990 }
3991 }
3992
3993 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
3994 vim_free(repl);
3995 if (p == NULL)
3996 return FAIL;
3997 }
3998
3999 /*
4000 * One file argument: Expand wildcards.
4001 * Don't do this with ":r !command" or ":w !command".
4002 */
4003 if ((eap->argt & NOSPC) && !eap->usefilter)
4004 {
4005 /*
4006 * May do this twice:
4007 * 1. Replace environment variables.
4008 * 2. Replace any other wildcards, remove backslashes.
4009 */
4010 for (n = 1; n <= 2; ++n)
4011 {
4012 if (n == 2)
4013 {
4014#ifdef UNIX
4015 /*
4016 * Only for Unix we check for more than one file name.
4017 * For other systems spaces are considered to be part
4018 * of the file name.
4019 * Only check here if there is no wildcard, otherwise
4020 * ExpandOne() will check for errors. This allows
4021 * ":e `ls ve*.c`" on Unix.
4022 */
4023 if (!has_wildcards)
4024 for (p = eap->arg; *p; ++p)
4025 {
4026 /* skip escaped characters */
4027 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4028 ++p;
4029 else if (vim_iswhite(*p))
4030 {
4031 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4032 return FAIL;
4033 }
4034 }
4035#endif
4036
4037 /*
4038 * Halve the number of backslashes (this is Vi compatible).
4039 * For Unix and OS/2, when wildcards are expanded, this is
4040 * done by ExpandOne() below.
4041 */
4042#if defined(UNIX) || defined(OS2)
4043 if (!has_wildcards)
4044#endif
4045 backslash_halve(eap->arg);
4046#ifdef MACOS_CLASSIC
4047 /*
4048 * translate unix-like path components
4049 */
4050 slash_n_colon_adjust(eap->arg);
4051#endif
4052 }
4053
4054 if (has_wildcards)
4055 {
4056 if (n == 1)
4057 {
4058 /*
4059 * First loop: May expand environment variables. This
4060 * can be done much faster with expand_env() than with
4061 * something else (e.g., calling a shell).
4062 * After expanding environment variables, check again
4063 * if there are still wildcards present.
4064 */
4065 if (vim_strchr(eap->arg, '$') != NULL
4066 || vim_strchr(eap->arg, '~') != NULL)
4067 {
4068 expand_env_esc(eap->arg, NameBuff, MAXPATHL, TRUE);
4069 has_wildcards = mch_has_wildcard(NameBuff);
4070 p = NameBuff;
4071 }
4072 else
4073 p = NULL;
4074 }
4075 else /* n == 2 */
4076 {
4077 expand_T xpc;
4078
4079 ExpandInit(&xpc);
4080 xpc.xp_context = EXPAND_FILES;
4081 p = ExpandOne(&xpc, eap->arg, NULL,
4082 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4083 WILD_EXPAND_FREE);
4084 ExpandCleanup(&xpc);
4085 if (p == NULL)
4086 return FAIL;
4087 }
4088 if (p != NULL)
4089 {
4090 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4091 p, cmdlinep);
4092 if (n == 2) /* p came from ExpandOne() */
4093 vim_free(p);
4094 }
4095 }
4096 }
4097 }
4098 return OK;
4099}
4100
4101/*
4102 * Replace part of the command line, keeping eap->cmd, eap->arg and
4103 * eap->nextcmd correct.
4104 * "src" points to the part that is to be replaced, of length "srclen".
4105 * "repl" is the replacement string.
4106 * Returns a pointer to the character after the replaced string.
4107 * Returns NULL for failure.
4108 */
4109 static char_u *
4110repl_cmdline(eap, src, srclen, repl, cmdlinep)
4111 exarg_T *eap;
4112 char_u *src;
4113 int srclen;
4114 char_u *repl;
4115 char_u **cmdlinep;
4116{
4117 int len;
4118 int i;
4119 char_u *new_cmdline;
4120
4121 /*
4122 * The new command line is build in new_cmdline[].
4123 * First allocate it.
4124 * Careful: a "+cmd" argument may have been NUL terminated.
4125 */
4126 len = (int)STRLEN(repl);
4127 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4128 if (eap->nextcmd)
4129 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4130 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4131 return NULL; /* out of memory! */
4132
4133 /*
4134 * Copy the stuff before the expanded part.
4135 * Copy the expanded stuff.
4136 * Copy what came after the expanded part.
4137 * Copy the next commands, if there are any.
4138 */
4139 i = (int)(src - *cmdlinep); /* length of part before match */
4140 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
4141 mch_memmove(new_cmdline + i, repl, (size_t)len);
4142 i += len; /* remember the end of the string */
4143 STRCPY(new_cmdline + i, src + srclen);
4144 src = new_cmdline + i; /* remember where to continue */
4145
4146 if (eap->nextcmd) /* append next command */
4147 {
4148 i = (int)STRLEN(new_cmdline) + 1;
4149 STRCPY(new_cmdline + i, eap->nextcmd);
4150 eap->nextcmd = new_cmdline + i;
4151 }
4152 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4153 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4154 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4155 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4156 vim_free(*cmdlinep);
4157 *cmdlinep = new_cmdline;
4158
4159 return src;
4160}
4161
4162/*
4163 * Check for '|' to separate commands and '"' to start comments.
4164 */
4165 void
4166separate_nextcmd(eap)
4167 exarg_T *eap;
4168{
4169 char_u *p;
4170
4171 for (p = eap->arg; *p; ++p)
4172 {
4173 if (*p == Ctrl_V)
4174 {
4175 if (eap->argt & (USECTRLV | XFILE))
4176 ++p; /* skip CTRL-V and next char */
4177 else
4178 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4179 if (*p == NUL) /* stop at NUL after CTRL-V */
4180 break;
4181 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004182
4183#ifdef FEAT_EVAL
4184 /* Skip over `=expr` when wildcards are expanded. */
4185 else if (p[0] == '`' && p[1] == '=')
4186 {
4187 p += 2;
4188 (void)skip_expr(&p);
4189 if (*p == '`')
4190 ++p;
4191 }
4192#endif
4193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 /* Check for '"': start of comment or '|': next command */
4195 /* :@" and :*" do not start a comment!
4196 * :redir @" doesn't either. */
4197 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4198 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4199 || p != eap->arg)
4200 && (eap->cmdidx != CMD_redir
4201 || p != eap->arg + 1 || p[-1] != '@'))
4202 || *p == '|' || *p == '\n')
4203 {
4204 /*
4205 * We remove the '\' before the '|', unless USECTRLV is used
4206 * AND 'b' is present in 'cpoptions'.
4207 */
4208 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4209 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4210 {
4211 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4212 --p;
4213 }
4214 else
4215 {
4216 eap->nextcmd = check_nextcmd(p);
4217 *p = NUL;
4218 break;
4219 }
4220 }
4221#ifdef FEAT_MBYTE
4222 else if (has_mbyte)
4223 p += (*mb_ptr2len_check)(p) - 1; /* skip bytes of multi-byte char */
4224#endif
4225 }
4226 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4227 del_trailing_spaces(eap->arg);
4228}
4229
4230/*
4231 * get + command from ex argument
4232 */
4233 static char_u *
4234getargcmd(argp)
4235 char_u **argp;
4236{
4237 char_u *arg = *argp;
4238 char_u *command = NULL;
4239
4240 if (*arg == '+') /* +[command] */
4241 {
4242 ++arg;
4243 if (vim_isspace(*arg))
4244 command = dollar_command;
4245 else
4246 {
4247 command = arg;
4248 arg = skip_cmd_arg(command, TRUE);
4249 if (*arg != NUL)
4250 *arg++ = NUL; /* terminate command with NUL */
4251 }
4252
4253 arg = skipwhite(arg); /* skip over spaces */
4254 *argp = arg;
4255 }
4256 return command;
4257}
4258
4259/*
4260 * Find end of "+command" argument. Skip over "\ " and "\\".
4261 */
4262 static char_u *
4263skip_cmd_arg(p, rembs)
4264 char_u *p;
4265 int rembs; /* TRUE to halve the number of backslashes */
4266{
4267 while (*p && !vim_isspace(*p))
4268 {
4269 if (*p == '\\' && p[1] != NUL)
4270 {
4271 if (rembs)
4272 mch_memmove(p, p + 1, STRLEN(p));
4273 else
4274 ++p;
4275 }
4276#ifdef FEAT_MBYTE
4277 if (has_mbyte)
4278 p += (*mb_ptr2len_check)(p);
4279 else
4280#endif
4281 ++p;
4282 }
4283 return p;
4284}
4285
4286/*
4287 * Get "++opt=arg" argument.
4288 * Return FAIL or OK.
4289 */
4290 static int
4291getargopt(eap)
4292 exarg_T *eap;
4293{
4294 char_u *arg = eap->arg + 2;
4295 int *pp = NULL;
4296#ifdef FEAT_MBYTE
4297 char_u *p;
4298#endif
4299
4300 /* ":edit ++[no]bin[ary] file" */
4301 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4302 {
4303 if (*arg == 'n')
4304 {
4305 arg += 2;
4306 eap->force_bin = FORCE_NOBIN;
4307 }
4308 else
4309 eap->force_bin = FORCE_BIN;
4310 if (!checkforcmd(&arg, "binary", 3))
4311 return FAIL;
4312 eap->arg = skipwhite(arg);
4313 return OK;
4314 }
4315
4316 if (STRNCMP(arg, "ff", 2) == 0)
4317 {
4318 arg += 2;
4319 pp = &eap->force_ff;
4320 }
4321 else if (STRNCMP(arg, "fileformat", 10) == 0)
4322 {
4323 arg += 10;
4324 pp = &eap->force_ff;
4325 }
4326#ifdef FEAT_MBYTE
4327 else if (STRNCMP(arg, "enc", 3) == 0)
4328 {
4329 arg += 3;
4330 pp = &eap->force_enc;
4331 }
4332 else if (STRNCMP(arg, "encoding", 8) == 0)
4333 {
4334 arg += 8;
4335 pp = &eap->force_enc;
4336 }
4337#endif
4338
4339 if (pp == NULL || *arg != '=')
4340 return FAIL;
4341
4342 ++arg;
4343 *pp = (int)(arg - eap->cmd);
4344 arg = skip_cmd_arg(arg, FALSE);
4345 eap->arg = skipwhite(arg);
4346 *arg = NUL;
4347
4348#ifdef FEAT_MBYTE
4349 if (pp == &eap->force_ff)
4350 {
4351#endif
4352 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4353 return FAIL;
4354#ifdef FEAT_MBYTE
4355 }
4356 else
4357 {
4358 /* Make 'fileencoding' lower case. */
4359 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4360 *p = TOLOWER_ASC(*p);
4361 }
4362#endif
4363
4364 return OK;
4365}
4366
4367/*
4368 * ":abbreviate" and friends.
4369 */
4370 static void
4371ex_abbreviate(eap)
4372 exarg_T *eap;
4373{
4374 do_exmap(eap, TRUE); /* almost the same as mapping */
4375}
4376
4377/*
4378 * ":map" and friends.
4379 */
4380 static void
4381ex_map(eap)
4382 exarg_T *eap;
4383{
4384 /*
4385 * If we are sourcing .exrc or .vimrc in current directory we
4386 * print the mappings for security reasons.
4387 */
4388 if (secure)
4389 {
4390 secure = 2;
4391 msg_outtrans(eap->cmd);
4392 msg_putchar('\n');
4393 }
4394 do_exmap(eap, FALSE);
4395}
4396
4397/*
4398 * ":unmap" and friends.
4399 */
4400 static void
4401ex_unmap(eap)
4402 exarg_T *eap;
4403{
4404 do_exmap(eap, FALSE);
4405}
4406
4407/*
4408 * ":mapclear" and friends.
4409 */
4410 static void
4411ex_mapclear(eap)
4412 exarg_T *eap;
4413{
4414 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4415}
4416
4417/*
4418 * ":abclear" and friends.
4419 */
4420 static void
4421ex_abclear(eap)
4422 exarg_T *eap;
4423{
4424 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4425}
4426
4427#ifdef FEAT_AUTOCMD
4428 static void
4429ex_autocmd(eap)
4430 exarg_T *eap;
4431{
4432 /*
4433 * Disallow auto commands from .exrc and .vimrc in current
4434 * directory for security reasons.
4435 */
4436 if (secure)
4437 {
4438 secure = 2;
4439 eap->errmsg = e_curdir;
4440 }
4441 else if (eap->cmdidx == CMD_autocmd)
4442 do_autocmd(eap->arg, eap->forceit);
4443 else
4444 do_augroup(eap->arg, eap->forceit);
4445}
4446
4447/*
4448 * ":doautocmd": Apply the automatic commands to the current buffer.
4449 */
4450 static void
4451ex_doautocmd(eap)
4452 exarg_T *eap;
4453{
4454 (void)do_doautocmd(eap->arg, TRUE);
4455 do_modelines();
4456}
4457#endif
4458
4459#ifdef FEAT_LISTCMDS
4460/*
4461 * :[N]bunload[!] [N] [bufname] unload buffer
4462 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4463 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4464 */
4465 static void
4466ex_bunload(eap)
4467 exarg_T *eap;
4468{
4469 eap->errmsg = do_bufdel(
4470 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4471 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4472 : DOBUF_UNLOAD, eap->arg,
4473 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4474}
4475
4476/*
4477 * :[N]buffer [N] to buffer N
4478 * :[N]sbuffer [N] to buffer N
4479 */
4480 static void
4481ex_buffer(eap)
4482 exarg_T *eap;
4483{
4484 if (*eap->arg)
4485 eap->errmsg = e_trailing;
4486 else
4487 {
4488 if (eap->addr_count == 0) /* default is current buffer */
4489 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4490 else
4491 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4492 }
4493}
4494
4495/*
4496 * :[N]bmodified [N] to next mod. buffer
4497 * :[N]sbmodified [N] to next mod. buffer
4498 */
4499 static void
4500ex_bmodified(eap)
4501 exarg_T *eap;
4502{
4503 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4504}
4505
4506/*
4507 * :[N]bnext [N] to next buffer
4508 * :[N]sbnext [N] split and to next buffer
4509 */
4510 static void
4511ex_bnext(eap)
4512 exarg_T *eap;
4513{
4514 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4515}
4516
4517/*
4518 * :[N]bNext [N] to previous buffer
4519 * :[N]bprevious [N] to previous buffer
4520 * :[N]sbNext [N] split and to previous buffer
4521 * :[N]sbprevious [N] split and to previous buffer
4522 */
4523 static void
4524ex_bprevious(eap)
4525 exarg_T *eap;
4526{
4527 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4528}
4529
4530/*
4531 * :brewind to first buffer
4532 * :bfirst to first buffer
4533 * :sbrewind split and to first buffer
4534 * :sbfirst split and to first buffer
4535 */
4536 static void
4537ex_brewind(eap)
4538 exarg_T *eap;
4539{
4540 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4541}
4542
4543/*
4544 * :blast to last buffer
4545 * :sblast split and to last buffer
4546 */
4547 static void
4548ex_blast(eap)
4549 exarg_T *eap;
4550{
4551 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4552}
4553#endif
4554
4555 int
4556ends_excmd(c)
4557 int c;
4558{
4559 return (c == NUL || c == '|' || c == '"' || c == '\n');
4560}
4561
4562#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4563 || defined(PROTO)
4564/*
4565 * Return the next command, after the first '|' or '\n'.
4566 * Return NULL if not found.
4567 */
4568 char_u *
4569find_nextcmd(p)
4570 char_u *p;
4571{
4572 while (*p != '|' && *p != '\n')
4573 {
4574 if (*p == NUL)
4575 return NULL;
4576 ++p;
4577 }
4578 return (p + 1);
4579}
4580#endif
4581
4582/*
4583 * Check if *p is a separator between Ex commands.
4584 * Return NULL if it isn't, (p + 1) if it is.
4585 */
4586 char_u *
4587check_nextcmd(p)
4588 char_u *p;
4589{
4590 p = skipwhite(p);
4591 if (*p == '|' || *p == '\n')
4592 return (p + 1);
4593 else
4594 return NULL;
4595}
4596
4597/*
4598 * - if there are more files to edit
4599 * - and this is the last window
4600 * - and forceit not used
4601 * - and not repeated twice on a row
4602 * return FAIL and give error message if 'message' TRUE
4603 * return OK otherwise
4604 */
4605 static int
4606check_more(message, forceit)
4607 int message; /* when FALSE check only, no messages */
4608 int forceit;
4609{
4610 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4611
4612 if (!forceit && only_one_window() && ARGCOUNT > 1 && !arg_had_last
4613 && n >= 0 && quitmore == 0)
4614 {
4615 if (message)
4616 {
4617#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4618 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4619 {
4620 char_u buff[IOSIZE];
4621
4622 if (n == 1)
4623 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4624 else
4625 sprintf((char *)buff,
4626 _("%d more files to edit. Quit anyway?"), n);
4627 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4628 return OK;
4629 return FAIL;
4630 }
4631#endif
4632 if (n == 1)
4633 EMSG(_("E173: 1 more file to edit"));
4634 else
4635 EMSGN(_("E173: %ld more files to edit"), n);
4636 quitmore = 2; /* next try to quit is allowed */
4637 }
4638 return FAIL;
4639 }
4640 return OK;
4641}
4642
4643#ifdef FEAT_CMDL_COMPL
4644/*
4645 * Function given to ExpandGeneric() to obtain the list of command names.
4646 */
4647/*ARGSUSED*/
4648 char_u *
4649get_command_name(xp, idx)
4650 expand_T *xp;
4651 int idx;
4652{
4653 if (idx >= (int)CMD_SIZE)
4654# ifdef FEAT_USR_CMDS
4655 return get_user_command_name(idx);
4656# else
4657 return NULL;
4658# endif
4659 return cmdnames[idx].cmd_name;
4660}
4661#endif
4662
4663#if defined(FEAT_USR_CMDS) || defined(PROTO)
4664static 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));
4665static void uc_list __ARGS((char_u *name, size_t name_len));
4666static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4667static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4668static 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));
4669
4670 static int
4671uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4672 char_u *name;
4673 size_t name_len;
4674 char_u *rep;
4675 long argt;
4676 long def;
4677 int flags;
4678 int compl;
4679 char_u *compl_arg;
4680 int force;
4681{
4682 ucmd_T *cmd = NULL;
4683 char_u *p;
4684 int i;
4685 int cmp = 1;
4686 char_u *rep_buf = NULL;
4687 garray_T *gap;
4688
4689 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4690 if (rep_buf == NULL)
4691 {
4692 /* Can't replace termcodes - try using the string as is */
4693 rep_buf = vim_strsave(rep);
4694
4695 /* Give up if out of memory */
4696 if (rep_buf == NULL)
4697 return FAIL;
4698 }
4699
4700 /* get address of growarray: global or in curbuf */
4701 if (flags & UC_BUFFER)
4702 {
4703 gap = &curbuf->b_ucmds;
4704 if (gap->ga_itemsize == 0)
4705 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4706 }
4707 else
4708 gap = &ucmds;
4709
4710 /* Search for the command in the already defined commands. */
4711 for (i = 0; i < gap->ga_len; ++i)
4712 {
4713 size_t len;
4714
4715 cmd = USER_CMD_GA(gap, i);
4716 len = STRLEN(cmd->uc_name);
4717 cmp = STRNCMP(name, cmd->uc_name, name_len);
4718 if (cmp == 0)
4719 {
4720 if (name_len < len)
4721 cmp = -1;
4722 else if (name_len > len)
4723 cmp = 1;
4724 }
4725
4726 if (cmp == 0)
4727 {
4728 if (!force)
4729 {
4730 EMSG(_("E174: Command already exists: add ! to replace it"));
4731 goto fail;
4732 }
4733
4734 vim_free(cmd->uc_rep);
4735 cmd->uc_rep = 0;
4736 break;
4737 }
4738
4739 /* Stop as soon as we pass the name to add */
4740 if (cmp < 0)
4741 break;
4742 }
4743
4744 /* Extend the array unless we're replacing an existing command */
4745 if (cmp != 0)
4746 {
4747 if (ga_grow(gap, 1) != OK)
4748 goto fail;
4749 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
4750 goto fail;
4751
4752 cmd = USER_CMD_GA(gap, i);
4753 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
4754
4755 ++gap->ga_len;
4756 --gap->ga_room;
4757
4758 cmd->uc_name = p;
4759 }
4760
4761 cmd->uc_rep = rep_buf;
4762 cmd->uc_argt = argt;
4763 cmd->uc_def = def;
4764 cmd->uc_compl = compl;
4765#ifdef FEAT_EVAL
4766 cmd->uc_scriptID = current_SID;
4767# ifdef FEAT_CMDL_COMPL
4768 cmd->uc_compl_arg = compl_arg;
4769# endif
4770#endif
4771
4772 return OK;
4773
4774fail:
4775 vim_free(rep_buf);
4776#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4777 vim_free(compl_arg);
4778#endif
4779 return FAIL;
4780}
4781
4782/*
4783 * List of names for completion for ":command" with the EXPAND_ flag.
4784 * Must be alphabetical for completion.
4785 */
4786static struct
4787{
4788 int expand;
4789 char *name;
4790} command_complete[] =
4791{
4792 {EXPAND_AUGROUP, "augroup"},
4793 {EXPAND_BUFFERS, "buffer"},
4794 {EXPAND_COMMANDS, "command"},
4795#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4796 {EXPAND_USER_DEFINED, "custom"},
4797#endif
4798 {EXPAND_DIRECTORIES, "dir"},
4799 {EXPAND_ENV_VARS, "environment"},
4800 {EXPAND_EVENTS, "event"},
4801 {EXPAND_EXPRESSION, "expression"},
4802 {EXPAND_FILES, "file"},
4803 {EXPAND_FUNCTIONS, "function"},
4804 {EXPAND_HELP, "help"},
4805 {EXPAND_HIGHLIGHT, "highlight"},
4806 {EXPAND_MAPPINGS, "mapping"},
4807 {EXPAND_MENUS, "menu"},
4808 {EXPAND_SETTINGS, "option"},
4809 {EXPAND_TAGS, "tag"},
4810 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
4811 {EXPAND_USER_VARS, "var"},
4812 {0, NULL}
4813};
4814
4815 static void
4816uc_list(name, name_len)
4817 char_u *name;
4818 size_t name_len;
4819{
4820 int i, j;
4821 int found = FALSE;
4822 ucmd_T *cmd;
4823 int len;
4824 long a;
4825 garray_T *gap;
4826
4827 gap = &curbuf->b_ucmds;
4828 for (;;)
4829 {
4830 for (i = 0; i < gap->ga_len; ++i)
4831 {
4832 cmd = USER_CMD_GA(gap, i);
4833 a = cmd->uc_argt;
4834
4835 /* Skip commands which don't match the requested prefix */
4836 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
4837 continue;
4838
4839 /* Put out the title first time */
4840 if (!found)
4841 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
4842 found = TRUE;
4843 msg_putchar('\n');
4844 if (got_int)
4845 break;
4846
4847 /* Special cases */
4848 msg_putchar(a & BANG ? '!' : ' ');
4849 msg_putchar(a & REGSTR ? '"' : ' ');
4850 msg_putchar(gap != &ucmds ? 'b' : ' ');
4851 msg_putchar(' ');
4852
4853 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
4854 len = (int)STRLEN(cmd->uc_name) + 4;
4855
4856 do {
4857 msg_putchar(' ');
4858 ++len;
4859 } while (len < 16);
4860
4861 len = 0;
4862
4863 /* Arguments */
4864 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
4865 {
4866 case 0: IObuff[len++] = '0'; break;
4867 case (EXTRA): IObuff[len++] = '*'; break;
4868 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
4869 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
4870 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
4871 }
4872
4873 do {
4874 IObuff[len++] = ' ';
4875 } while (len < 5);
4876
4877 /* Range */
4878 if (a & (RANGE|COUNT))
4879 {
4880 if (a & COUNT)
4881 {
4882 /* -count=N */
4883 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
4884 len += (int)STRLEN(IObuff + len);
4885 }
4886 else if (a & DFLALL)
4887 IObuff[len++] = '%';
4888 else if (cmd->uc_def >= 0)
4889 {
4890 /* -range=N */
4891 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
4892 len += (int)STRLEN(IObuff + len);
4893 }
4894 else
4895 IObuff[len++] = '.';
4896 }
4897
4898 do {
4899 IObuff[len++] = ' ';
4900 } while (len < 11);
4901
4902 /* Completion */
4903 for (j = 0; command_complete[j].expand != 0; ++j)
4904 if (command_complete[j].expand == cmd->uc_compl)
4905 {
4906 STRCPY(IObuff + len, command_complete[j].name);
4907 len += (int)STRLEN(IObuff + len);
4908 break;
4909 }
4910
4911 do {
4912 IObuff[len++] = ' ';
4913 } while (len < 21);
4914
4915 IObuff[len] = '\0';
4916 msg_outtrans(IObuff);
4917
4918 msg_outtrans_special(cmd->uc_rep, FALSE);
4919 out_flush();
4920 ui_breakcheck();
4921 if (got_int)
4922 break;
4923 }
4924 if (gap == &ucmds || i < gap->ga_len)
4925 break;
4926 gap = &ucmds;
4927 }
4928
4929 if (!found)
4930 MSG(_("No user-defined commands found"));
4931}
4932
4933 static char_u *
4934uc_fun_cmd()
4935{
4936 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
4937 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
4938 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
4939 0xb9, 0x7f, 0};
4940 int i;
4941
4942 for (i = 0; fcmd[i]; ++i)
4943 IObuff[i] = fcmd[i] - 0x40;
4944 IObuff[i] = 0;
4945 return IObuff;
4946}
4947
4948 static int
4949uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
4950 char_u *attr;
4951 size_t len;
4952 long *argt;
4953 long *def;
4954 int *flags;
4955 int *compl;
4956 char_u **compl_arg;
4957{
4958 char_u *p;
4959
4960 if (len == 0)
4961 {
4962 EMSG(_("E175: No attribute specified"));
4963 return FAIL;
4964 }
4965
4966 /* First, try the simple attributes (no arguments) */
4967 if (STRNICMP(attr, "bang", len) == 0)
4968 *argt |= BANG;
4969 else if (STRNICMP(attr, "buffer", len) == 0)
4970 *flags |= UC_BUFFER;
4971 else if (STRNICMP(attr, "register", len) == 0)
4972 *argt |= REGSTR;
4973 else if (STRNICMP(attr, "bar", len) == 0)
4974 *argt |= TRLBAR;
4975 else
4976 {
4977 int i;
4978 char_u *val = NULL;
4979 size_t vallen = 0;
4980 size_t attrlen = len;
4981
4982 /* Look for the attribute name - which is the part before any '=' */
4983 for (i = 0; i < (int)len; ++i)
4984 {
4985 if (attr[i] == '=')
4986 {
4987 val = &attr[i + 1];
4988 vallen = len - i - 1;
4989 attrlen = i;
4990 break;
4991 }
4992 }
4993
4994 if (STRNICMP(attr, "nargs", attrlen) == 0)
4995 {
4996 if (vallen == 1)
4997 {
4998 if (*val == '0')
4999 /* Do nothing - this is the default */;
5000 else if (*val == '1')
5001 *argt |= (EXTRA | NOSPC | NEEDARG);
5002 else if (*val == '*')
5003 *argt |= EXTRA;
5004 else if (*val == '?')
5005 *argt |= (EXTRA | NOSPC);
5006 else if (*val == '+')
5007 *argt |= (EXTRA | NEEDARG);
5008 else
5009 goto wrong_nargs;
5010 }
5011 else
5012 {
5013wrong_nargs:
5014 EMSG(_("E176: Invalid number of arguments"));
5015 return FAIL;
5016 }
5017 }
5018 else if (STRNICMP(attr, "range", attrlen) == 0)
5019 {
5020 *argt |= RANGE;
5021 if (vallen == 1 && *val == '%')
5022 *argt |= DFLALL;
5023 else if (val != NULL)
5024 {
5025 p = val;
5026 if (*def >= 0)
5027 {
5028two_count:
5029 EMSG(_("E177: Count cannot be specified twice"));
5030 return FAIL;
5031 }
5032
5033 *def = getdigits(&p);
5034 *argt |= (ZEROR | NOTADR);
5035
5036 if (p != val + vallen || vallen == 0)
5037 {
5038invalid_count:
5039 EMSG(_("E178: Invalid default value for count"));
5040 return FAIL;
5041 }
5042 }
5043 }
5044 else if (STRNICMP(attr, "count", attrlen) == 0)
5045 {
5046 *argt |= (COUNT | ZEROR | NOTADR);
5047
5048 if (val != NULL)
5049 {
5050 p = val;
5051 if (*def >= 0)
5052 goto two_count;
5053
5054 *def = getdigits(&p);
5055
5056 if (p != val + vallen)
5057 goto invalid_count;
5058 }
5059
5060 if (*def < 0)
5061 *def = 0;
5062 }
5063 else if (STRNICMP(attr, "complete", attrlen) == 0)
5064 {
5065 char_u *arg = NULL;
5066 size_t arglen = 0;
5067
5068 if (val == NULL)
5069 {
5070 EMSG(_("E179: argument required for complete"));
5071 return FAIL;
5072 }
5073 /* Look for any argument part - which is the part after any ',' */
5074 for (i = 0; i < (int)vallen; ++i)
5075 {
5076 if (val[i] == ',')
5077 {
5078 arg = &val[i + 1];
5079 arglen = vallen - i - 1;
5080 vallen = i;
5081 break;
5082 }
5083 }
5084
5085 for (i = 0; command_complete[i].expand != 0; ++i)
5086 {
5087 if (STRLEN(command_complete[i].name) == vallen
5088 && STRNCMP(val, command_complete[i].name, vallen) == 0)
5089 {
5090 *compl = command_complete[i].expand;
5091 if (command_complete[i].expand == EXPAND_BUFFERS)
5092 *argt |= BUFNAME;
5093 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5094 || command_complete[i].expand == EXPAND_FILES)
5095 *argt |= XFILE;
5096 break;
5097 }
5098 }
5099
5100 if (command_complete[i].expand == 0)
5101 {
5102 EMSG2(_("E180: Invalid complete value: %s"), val);
5103 return FAIL;
5104 }
5105#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5106 if (*compl != EXPAND_USER_DEFINED && arg != NULL)
5107#else
5108 if (arg != NULL)
5109#endif
5110 {
5111 EMSG(_("E468: Completion argument only allowed for custom completion"));
5112 return FAIL;
5113 }
5114#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5115 if (*compl == EXPAND_USER_DEFINED && arg == NULL)
5116 {
5117 EMSG(_("E467: Custom completion requires a function argument"));
5118 return FAIL;
5119 }
5120 if (arg != NULL)
5121 *compl_arg = vim_strnsave(arg, (int)arglen);
5122#endif
5123 }
5124 else
5125 {
5126 char_u ch = attr[len];
5127 attr[len] = '\0';
5128 EMSG2(_("E181: Invalid attribute: %s"), attr);
5129 attr[len] = ch;
5130 return FAIL;
5131 }
5132 }
5133
5134 return OK;
5135}
5136
5137 static void
5138ex_command(eap)
5139 exarg_T *eap;
5140{
5141 char_u *name;
5142 char_u *end;
5143 char_u *p;
5144 long argt = 0;
5145 long def = -1;
5146 int flags = 0;
5147 int compl = EXPAND_NOTHING;
5148 char_u *compl_arg = NULL;
5149 int has_attr = (eap->arg[0] == '-');
5150
5151 p = eap->arg;
5152
5153 /* Check for attributes */
5154 while (*p == '-')
5155 {
5156 ++p;
5157 end = skiptowhite(p);
5158 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5159 == FAIL)
5160 return;
5161 p = skipwhite(end);
5162 }
5163
5164 /* Get the name (if any) and skip to the following argument */
5165 name = p;
5166 if (ASCII_ISALPHA(*p))
5167 while (ASCII_ISALNUM(*p))
5168 ++p;
5169 if (!ends_excmd(*p) && !vim_iswhite(*p))
5170 {
5171 EMSG(_("E182: Invalid command name"));
5172 return;
5173 }
5174 end = p;
5175
5176 /* If there is nothing after the name, and no attributes were specified,
5177 * we are listing commands
5178 */
5179 p = skipwhite(end);
5180 if (!has_attr && ends_excmd(*p))
5181 {
5182 uc_list(name, end - name);
5183 }
5184 else if (!ASCII_ISUPPER(*name))
5185 {
5186 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5187 return;
5188 }
5189 else
5190 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5191 eap->forceit);
5192}
5193
5194/*
5195 * ":comclear"
5196 */
5197/*ARGSUSED*/
5198/*
5199 * Clear all user commands, global and for current buffer.
5200 */
5201 static void
5202ex_comclear(eap)
5203 exarg_T *eap;
5204{
5205 uc_clear(&ucmds);
5206 uc_clear(&curbuf->b_ucmds);
5207}
5208
5209/*
5210 * Clear all user commands for "gap".
5211 */
5212 void
5213uc_clear(gap)
5214 garray_T *gap;
5215{
5216 int i;
5217 ucmd_T *cmd;
5218
5219 for (i = 0; i < gap->ga_len; ++i)
5220 {
5221 cmd = USER_CMD_GA(gap, i);
5222 vim_free(cmd->uc_name);
5223 vim_free(cmd->uc_rep);
5224# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5225 vim_free(cmd->uc_compl_arg);
5226# endif
5227 }
5228 ga_clear(gap);
5229}
5230
5231 static void
5232ex_delcommand(eap)
5233 exarg_T *eap;
5234{
5235 int i = 0;
5236 ucmd_T *cmd = NULL;
5237 int cmp = -1;
5238 garray_T *gap;
5239
5240 gap = &curbuf->b_ucmds;
5241 for (;;)
5242 {
5243 for (i = 0; i < gap->ga_len; ++i)
5244 {
5245 cmd = USER_CMD_GA(gap, i);
5246 cmp = STRCMP(eap->arg, cmd->uc_name);
5247 if (cmp <= 0)
5248 break;
5249 }
5250 if (gap == &ucmds || cmp == 0)
5251 break;
5252 gap = &ucmds;
5253 }
5254
5255 if (cmp != 0)
5256 {
5257 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5258 return;
5259 }
5260
5261 vim_free(cmd->uc_name);
5262 vim_free(cmd->uc_rep);
5263# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5264 vim_free(cmd->uc_compl_arg);
5265# endif
5266
5267 --gap->ga_len;
5268 ++gap->ga_room;
5269
5270 if (i < gap->ga_len)
5271 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5272}
5273
5274 static char_u *
5275uc_split_args(arg, lenp)
5276 char_u *arg;
5277 size_t *lenp;
5278{
5279 char_u *buf;
5280 char_u *p;
5281 char_u *q;
5282 int len;
5283
5284 /* Precalculate length */
5285 p = arg;
5286 len = 2; /* Initial and final quotes */
5287
5288 while (*p)
5289 {
5290 if (p[0] == '\\' && vim_iswhite(p[1]))
5291 {
5292 len += 1;
5293 p += 2;
5294 }
5295 else if (*p == '\\' || *p == '"')
5296 {
5297 len += 2;
5298 p += 1;
5299 }
5300 else if (vim_iswhite(*p))
5301 {
5302 p = skipwhite(p);
5303 if (*p == NUL)
5304 break;
5305 len += 3; /* "," */
5306 }
5307 else
5308 {
5309 ++len;
5310 ++p;
5311 }
5312 }
5313
5314 buf = alloc(len + 1);
5315 if (buf == NULL)
5316 {
5317 *lenp = 0;
5318 return buf;
5319 }
5320
5321 p = arg;
5322 q = buf;
5323 *q++ = '"';
5324 while (*p)
5325 {
5326 if (p[0] == '\\' && vim_iswhite(p[1]))
5327 {
5328 *q++ = p[1];
5329 p += 2;
5330 }
5331 else if (*p == '\\' || *p == '"')
5332 {
5333 *q++ = '\\';
5334 *q++ = *p++;
5335 }
5336 else if (vim_iswhite(*p))
5337 {
5338 p = skipwhite(p);
5339 if (*p == NUL)
5340 break;
5341 *q++ = '"';
5342 *q++ = ',';
5343 *q++ = '"';
5344 }
5345 else
5346 {
5347 *q++ = *p++;
5348 }
5349 }
5350 *q++ = '"';
5351 *q = 0;
5352
5353 *lenp = len;
5354 return buf;
5355}
5356
5357/*
5358 * Check for a <> code in a user command.
5359 * "code" points to the '<'. "len" the length of the <> (inclusive).
5360 * "buf" is where the result is to be added.
5361 * "split_buf" points to a buffer used for splitting, caller should free it.
5362 * "split_len" is the length of what "split_buf" contains.
5363 * Returns the length of the replacement, which has been added to "buf".
5364 * Returns -1 if there was no match, and only the "<" has been copied.
5365 */
5366 static size_t
5367uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5368 char_u *code;
5369 size_t len;
5370 char_u *buf;
5371 ucmd_T *cmd; /* the user command we're expanding */
5372 exarg_T *eap; /* ex arguments */
5373 char_u **split_buf;
5374 size_t *split_len;
5375{
5376 size_t result = 0;
5377 char_u *p = code + 1;
5378 size_t l = len - 2;
5379 int quote = 0;
5380 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5381 ct_LT, ct_NONE } type = ct_NONE;
5382
5383 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5384 {
5385 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5386 p += 2;
5387 l -= 2;
5388 }
5389
5390 if (l < 1)
5391 type = ct_NONE;
5392 else if (STRNICMP(p, "args", l) == 0)
5393 type = ct_ARGS;
5394 else if (STRNICMP(p, "bang", l) == 0)
5395 type = ct_BANG;
5396 else if (STRNICMP(p, "count", l) == 0)
5397 type = ct_COUNT;
5398 else if (STRNICMP(p, "line1", l) == 0)
5399 type = ct_LINE1;
5400 else if (STRNICMP(p, "line2", l) == 0)
5401 type = ct_LINE2;
5402 else if (STRNICMP(p, "lt", l) == 0)
5403 type = ct_LT;
5404 else if (STRNICMP(p, "register", l) == 0)
5405 type = ct_REGISTER;
5406
5407 switch (type)
5408 {
5409 case ct_ARGS:
5410 /* Simple case first */
5411 if (*eap->arg == NUL)
5412 {
5413 if (quote == 1)
5414 {
5415 result = 2;
5416 if (buf != NULL)
5417 STRCPY(buf, "''");
5418 }
5419 else
5420 result = 0;
5421 break;
5422 }
5423
5424 /* When specified there is a single argument don't split it.
5425 * Works for ":Cmd %" when % is "a b c". */
5426 if ((eap->argt & NOSPC) && quote == 2)
5427 quote = 1;
5428
5429 switch (quote)
5430 {
5431 case 0: /* No quoting, no splitting */
5432 result = STRLEN(eap->arg);
5433 if (buf != NULL)
5434 STRCPY(buf, eap->arg);
5435 break;
5436 case 1: /* Quote, but don't split */
5437 result = STRLEN(eap->arg) + 2;
5438 for (p = eap->arg; *p; ++p)
5439 {
5440 if (*p == '\\' || *p == '"')
5441 ++result;
5442 }
5443
5444 if (buf != NULL)
5445 {
5446 *buf++ = '"';
5447 for (p = eap->arg; *p; ++p)
5448 {
5449 if (*p == '\\' || *p == '"')
5450 *buf++ = '\\';
5451 *buf++ = *p;
5452 }
5453 *buf = '"';
5454 }
5455
5456 break;
5457 case 2: /* Quote and split */
5458 /* This is hard, so only do it once, and cache the result */
5459 if (*split_buf == NULL)
5460 *split_buf = uc_split_args(eap->arg, split_len);
5461
5462 result = *split_len;
5463 if (buf != NULL && result != 0)
5464 STRCPY(buf, *split_buf);
5465
5466 break;
5467 }
5468 break;
5469
5470 case ct_BANG:
5471 result = eap->forceit ? 1 : 0;
5472 if (quote)
5473 result += 2;
5474 if (buf != NULL)
5475 {
5476 if (quote)
5477 *buf++ = '"';
5478 if (eap->forceit)
5479 *buf++ = '!';
5480 if (quote)
5481 *buf = '"';
5482 }
5483 break;
5484
5485 case ct_LINE1:
5486 case ct_LINE2:
5487 case ct_COUNT:
5488 {
5489 char num_buf[20];
5490 long num = (type == ct_LINE1) ? eap->line1 :
5491 (type == ct_LINE2) ? eap->line2 :
5492 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5493 size_t num_len;
5494
5495 sprintf(num_buf, "%ld", num);
5496 num_len = STRLEN(num_buf);
5497 result = num_len;
5498
5499 if (quote)
5500 result += 2;
5501
5502 if (buf != NULL)
5503 {
5504 if (quote)
5505 *buf++ = '"';
5506 STRCPY(buf, num_buf);
5507 buf += num_len;
5508 if (quote)
5509 *buf = '"';
5510 }
5511
5512 break;
5513 }
5514
5515 case ct_REGISTER:
5516 result = eap->regname ? 1 : 0;
5517 if (quote)
5518 result += 2;
5519 if (buf != NULL)
5520 {
5521 if (quote)
5522 *buf++ = '\'';
5523 if (eap->regname)
5524 *buf++ = eap->regname;
5525 if (quote)
5526 *buf = '\'';
5527 }
5528 break;
5529
5530 case ct_LT:
5531 result = 1;
5532 if (buf != NULL)
5533 *buf = '<';
5534 break;
5535
5536 default:
5537 /* Not recognized: just copy the '<' and return -1. */
5538 result = (size_t)-1;
5539 if (buf != NULL)
5540 *buf = '<';
5541 break;
5542 }
5543
5544 return result;
5545}
5546
5547 static void
5548do_ucmd(eap)
5549 exarg_T *eap;
5550{
5551 char_u *buf;
5552 char_u *p;
5553 char_u *q;
5554
5555 char_u *start;
5556 char_u *end;
5557 size_t len, totlen;
5558
5559 size_t split_len = 0;
5560 char_u *split_buf = NULL;
5561 ucmd_T *cmd;
5562#ifdef FEAT_EVAL
5563 scid_T save_current_SID = current_SID;
5564#endif
5565
5566 if (eap->cmdidx == CMD_USER)
5567 cmd = USER_CMD(eap->useridx);
5568 else
5569 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5570
5571 /*
5572 * Replace <> in the command by the arguments.
5573 */
5574 buf = NULL;
5575 for (;;)
5576 {
5577 p = cmd->uc_rep;
5578 q = buf;
5579 totlen = 0;
5580 while ((start = vim_strchr(p, '<')) != NULL
5581 && (end = vim_strchr(start + 1, '>')) != NULL)
5582 {
5583 /* Include the '>' */
5584 ++end;
5585
5586 /* Take everything up to the '<' */
5587 len = start - p;
5588 if (buf == NULL)
5589 totlen += len;
5590 else
5591 {
5592 mch_memmove(q, p, len);
5593 q += len;
5594 }
5595
5596 len = uc_check_code(start, end - start, q, cmd, eap,
5597 &split_buf, &split_len);
5598 if (len == (size_t)-1)
5599 {
5600 /* no match, continue after '<' */
5601 p = start + 1;
5602 len = 1;
5603 }
5604 else
5605 p = end;
5606 if (buf == NULL)
5607 totlen += len;
5608 else
5609 q += len;
5610 }
5611 if (buf != NULL) /* second time here, finished */
5612 {
5613 STRCPY(q, p);
5614 break;
5615 }
5616
5617 totlen += STRLEN(p); /* Add on the trailing characters */
5618 buf = alloc((unsigned)(totlen + 1));
5619 if (buf == NULL)
5620 {
5621 vim_free(split_buf);
5622 return;
5623 }
5624 }
5625
5626#ifdef FEAT_EVAL
5627 current_SID = cmd->uc_scriptID;
5628#endif
5629 (void)do_cmdline(buf, eap->getline, eap->cookie,
5630 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5631#ifdef FEAT_EVAL
5632 current_SID = save_current_SID;
5633#endif
5634 vim_free(buf);
5635 vim_free(split_buf);
5636}
5637
5638# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5639 static char_u *
5640get_user_command_name(idx)
5641 int idx;
5642{
5643 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5644}
5645
5646/*
5647 * Function given to ExpandGeneric() to obtain the list of user command names.
5648 */
5649/*ARGSUSED*/
5650 char_u *
5651get_user_commands(xp, idx)
5652 expand_T *xp;
5653 int idx;
5654{
5655 if (idx < curbuf->b_ucmds.ga_len)
5656 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5657 idx -= curbuf->b_ucmds.ga_len;
5658 if (idx < ucmds.ga_len)
5659 return USER_CMD(idx)->uc_name;
5660 return NULL;
5661}
5662
5663/*
5664 * Function given to ExpandGeneric() to obtain the list of user command
5665 * attributes.
5666 */
5667/*ARGSUSED*/
5668 char_u *
5669get_user_cmd_flags(xp, idx)
5670 expand_T *xp;
5671 int idx;
5672{
5673 static char *user_cmd_flags[] =
5674 {"bang", "bar", "buffer", "complete", "count",
5675 "nargs", "range", "register"};
5676
5677 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5678 return NULL;
5679 return (char_u *)user_cmd_flags[idx];
5680}
5681
5682/*
5683 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5684 */
5685/*ARGSUSED*/
5686 char_u *
5687get_user_cmd_nargs(xp, idx)
5688 expand_T *xp;
5689 int idx;
5690{
5691 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5692
5693 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5694 return NULL;
5695 return (char_u *)user_cmd_nargs[idx];
5696}
5697
5698/*
5699 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5700 */
5701/*ARGSUSED*/
5702 char_u *
5703get_user_cmd_complete(xp, idx)
5704 expand_T *xp;
5705 int idx;
5706{
5707 return (char_u *)command_complete[idx].name;
5708}
5709# endif /* FEAT_CMDL_COMPL */
5710
5711#endif /* FEAT_USR_CMDS */
5712
5713 static void
5714ex_colorscheme(eap)
5715 exarg_T *eap;
5716{
5717 if (load_colors(eap->arg) == FAIL)
5718 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
5719}
5720
5721 static void
5722ex_highlight(eap)
5723 exarg_T *eap;
5724{
5725 if (*eap->arg == NUL && eap->cmd[2] == '!')
5726 MSG(_("Greetings, Vim user!"));
5727 do_highlight(eap->arg, eap->forceit, FALSE);
5728}
5729
5730
5731/*
5732 * Call this function if we thought we were going to exit, but we won't
5733 * (because of an error). May need to restore the terminal mode.
5734 */
5735 void
5736not_exiting()
5737{
5738 exiting = FALSE;
5739 settmode(TMODE_RAW);
5740}
5741
5742/*
5743 * ":quit": quit current window, quit Vim if closed the last window.
5744 */
5745 static void
5746ex_quit(eap)
5747 exarg_T *eap;
5748{
5749#ifdef FEAT_CMDWIN
5750 if (cmdwin_type != 0)
5751 {
5752 cmdwin_result = Ctrl_C;
5753 return;
5754 }
5755#endif
5756
5757#ifdef FEAT_NETBEANS_INTG
5758 netbeansForcedQuit = eap->forceit;
5759#endif
5760
5761 /*
5762 * If there are more files or windows we won't exit.
5763 */
5764 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5765 exiting = TRUE;
5766 if ((!P_HID(curbuf)
5767 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
5768 || check_more(TRUE, eap->forceit) == FAIL
5769 || (only_one_window() && check_changed_any(eap->forceit)))
5770 {
5771 not_exiting();
5772 }
5773 else
5774 {
5775#ifdef FEAT_WINDOWS
5776 if (only_one_window()) /* quit last window */
5777#endif
5778 getout(0);
5779#ifdef FEAT_WINDOWS
5780# ifdef FEAT_GUI
5781 need_mouse_correct = TRUE;
5782# endif
5783 /* close window; may free buffer */
5784 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
5785#endif
5786 }
5787}
5788
5789/*
5790 * ":cquit".
5791 */
5792/*ARGSUSED*/
5793 static void
5794ex_cquit(eap)
5795 exarg_T *eap;
5796{
5797 getout(1); /* this does not always pass on the exit code to the Manx
5798 compiler. why? */
5799}
5800
5801/*
5802 * ":qall": try to quit all windows
5803 */
5804 static void
5805ex_quit_all(eap)
5806 exarg_T *eap;
5807{
5808# ifdef FEAT_CMDWIN
5809 if (cmdwin_type != 0)
5810 {
5811 if (eap->forceit)
5812 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5813 else
5814 cmdwin_result = K_XF2;
5815 return;
5816 }
5817# endif
5818 exiting = TRUE;
5819 if (eap->forceit || !check_changed_any(FALSE))
5820 getout(0);
5821 not_exiting();
5822}
5823
5824#ifdef FEAT_WINDOWS
5825/*
5826 * ":close": close current window, unless it is the last one
5827 */
5828 static void
5829ex_close(eap)
5830 exarg_T *eap;
5831{
5832# ifdef FEAT_CMDWIN
5833 if (cmdwin_type != 0)
5834 cmdwin_result = K_IGNORE;
5835 else
5836# endif
5837 ex_win_close(eap, curwin);
5838}
5839
5840 static void
5841ex_win_close(eap, win)
5842 exarg_T *eap;
5843 win_T *win;
5844{
5845 int need_hide;
5846 buf_T *buf = win->w_buffer;
5847
5848 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
5849 if (need_hide && !P_HID(buf) && !eap->forceit)
5850 {
5851#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5852 if ((p_confirm || cmdmod.confirm) && p_write)
5853 {
5854 dialog_changed(buf, FALSE);
5855 if (buf_valid(buf) && bufIsChanged(buf))
5856 return;
5857 need_hide = FALSE;
5858 }
5859 else
5860#endif
5861 {
5862 EMSG(_(e_nowrtmsg));
5863 return;
5864 }
5865 }
5866
5867#ifdef FEAT_GUI
5868 need_mouse_correct = TRUE;
5869#endif
5870 /* free buffer when not hiding it or when it's a scratch buffer */
5871 win_close(win, !need_hide && !P_HID(buf));
5872}
5873
5874#ifdef FEAT_QUICKFIX
5875/*
5876 * ":pclose": Close any preview window.
5877 */
5878 static void
5879ex_pclose(eap)
5880 exarg_T *eap;
5881{
5882 win_T *win;
5883
5884 for (win = firstwin; win != NULL; win = win->w_next)
5885 if (win->w_p_pvw)
5886 {
5887 ex_win_close(eap, win);
5888 break;
5889 }
5890}
5891#endif
5892
5893/*
5894 * ":only".
5895 */
5896 static void
5897ex_only(eap)
5898 exarg_T *eap;
5899{
5900# ifdef FEAT_GUI
5901 need_mouse_correct = TRUE;
5902# endif
5903 close_others(TRUE, eap->forceit);
5904}
5905
5906/*
5907 * ":all" and ":sall".
5908 */
5909 static void
5910ex_all(eap)
5911 exarg_T *eap;
5912{
5913 if (eap->addr_count == 0)
5914 eap->line2 = 9999;
5915 do_arg_all((int)eap->line2, eap->forceit);
5916}
5917#endif /* FEAT_WINDOWS */
5918
5919 static void
5920ex_hide(eap)
5921 exarg_T *eap;
5922{
5923 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
5924 eap->errmsg = e_invarg;
5925 else
5926 {
5927 /* ":hide" or ":hide | cmd": hide current window */
5928 eap->nextcmd = check_nextcmd(eap->arg);
5929#ifdef FEAT_WINDOWS
5930 if (!eap->skip)
5931 {
5932# ifdef FEAT_GUI
5933 need_mouse_correct = TRUE;
5934# endif
5935 win_close(curwin, FALSE); /* don't free buffer */
5936 }
5937#endif
5938 }
5939}
5940
5941/*
5942 * ":stop" and ":suspend": Suspend Vim.
5943 */
5944 static void
5945ex_stop(eap)
5946 exarg_T *eap;
5947{
5948 /*
5949 * Disallow suspending for "rvim".
5950 */
5951 if (!check_restricted()
5952#ifdef WIN3264
5953 /*
5954 * Check if external commands are allowed now.
5955 */
5956 && can_end_termcap_mode(TRUE)
5957#endif
5958 )
5959 {
5960 if (!eap->forceit)
5961 autowrite_all();
5962 windgoto((int)Rows - 1, 0);
5963 out_char('\n');
5964 out_flush();
5965 stoptermcap();
5966 out_flush(); /* needed for SUN to restore xterm buffer */
5967#ifdef FEAT_TITLE
5968 mch_restore_title(3); /* restore window titles */
5969#endif
5970 ui_suspend(); /* call machine specific function */
5971#ifdef FEAT_TITLE
5972 maketitle();
5973 resettitle(); /* force updating the title */
5974#endif
5975 starttermcap();
5976 scroll_start(); /* scroll screen before redrawing */
5977 redraw_later_clear();
5978 shell_resized(); /* may have resized window */
5979 }
5980}
5981
5982/*
5983 * ":exit", ":xit" and ":wq": Write file and exit Vim.
5984 */
5985 static void
5986ex_exit(eap)
5987 exarg_T *eap;
5988{
5989#ifdef FEAT_CMDWIN
5990 if (cmdwin_type != 0)
5991 {
5992 cmdwin_result = Ctrl_C;
5993 return;
5994 }
5995#endif
5996
5997 /*
5998 * if more files or windows we won't exit
5999 */
6000 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6001 exiting = TRUE;
6002 if ( ((eap->cmdidx == CMD_wq
6003 || curbufIsChanged())
6004 && do_write(eap) == FAIL)
6005 || check_more(TRUE, eap->forceit) == FAIL
6006 || (only_one_window() && check_changed_any(eap->forceit)))
6007 {
6008 not_exiting();
6009 }
6010 else
6011 {
6012#ifdef FEAT_WINDOWS
6013 if (only_one_window()) /* quit last window, exit Vim */
6014#endif
6015 getout(0);
6016#ifdef FEAT_WINDOWS
6017# ifdef FEAT_GUI
6018 need_mouse_correct = TRUE;
6019# endif
6020 /* quit current window, may free buffer */
6021 win_close(curwin, !P_HID(curwin->w_buffer));
6022#endif
6023 }
6024}
6025
6026/*
6027 * ":print", ":list", ":number".
6028 */
6029 static void
6030ex_print(eap)
6031 exarg_T *eap;
6032{
6033 int save_list = 0; /* init for GCC */
6034
6035 if (eap->cmdidx == CMD_list)
6036 {
6037 save_list = curwin->w_p_list;
6038 curwin->w_p_list = 1;
6039 }
6040
6041 for ( ;!got_int; ui_breakcheck())
6042 {
6043 print_line(eap->line1,
6044 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound));
6045 if (++eap->line1 > eap->line2)
6046 break;
6047 out_flush(); /* show one line at a time */
6048 }
6049 setpcmark();
6050 /* put cursor at last line */
6051 curwin->w_cursor.lnum = eap->line2;
6052 beginline(BL_SOL | BL_FIX);
6053
6054 ex_no_reprint = TRUE;
6055
6056 if (eap->cmdidx == CMD_list)
6057 curwin->w_p_list = save_list;
6058}
6059
6060#ifdef FEAT_BYTEOFF
6061 static void
6062ex_goto(eap)
6063 exarg_T *eap;
6064{
6065 goto_byte(eap->line2);
6066}
6067#endif
6068
6069/*
6070 * ":shell".
6071 */
6072/*ARGSUSED*/
6073 static void
6074ex_shell(eap)
6075 exarg_T *eap;
6076{
6077 do_shell(NULL, 0);
6078}
6079
6080#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6081 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
6082 || defined(PROTO)
6083
6084/*
6085 * Handle a file drop. The code is here because a drop is *nearly* like an
6086 * :args command, but not quite (we have a list of exact filenames, so we
6087 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6088 * code is very similar to :args and hence needs access to a lot of the static
6089 * functions in this file.
6090 *
6091 * The list should be allocated using alloc(), as should each item in the
6092 * list. This function takes over responsibility for freeing the list.
6093 *
6094 * XXX The list is made into the arggument list. This is freed using
6095 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6096 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6097 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6098 * file functionality is (currently) not in EMX this is not presently a
6099 * problem.
6100 */
6101 void
6102handle_drop(filec, filev, split)
6103 int filec; /* the number of files dropped */
6104 char_u **filev; /* the list of files dropped */
6105 int split; /* force splitting the window */
6106{
6107 exarg_T ea;
6108 int save_msg_scroll = msg_scroll;
6109
6110# ifdef FEAT_CMDWIN
6111 if (cmdwin_type != 0)
6112 return;
6113# endif
6114
6115 /* Check whether the current buffer is changed. If so, we will need
6116 * to split the current window or data could be lost.
6117 * We don't need to check if the 'hidden' option is set, as in this
6118 * case the buffer won't be lost.
6119 */
6120 if (!P_HID(curbuf) && !split)
6121 {
6122 ++emsg_off;
6123 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6124 --emsg_off;
6125 }
6126 if (split)
6127 {
6128# ifdef FEAT_WINDOWS
6129 if (win_split(0, 0) == FAIL)
6130 return;
6131# ifdef FEAT_SCROLLBIND
6132 curwin->w_p_scb = FALSE;
6133# endif
6134
6135 /* When splitting the window, create a new alist. Otherwise the
6136 * existing one is overwritten. */
6137 alist_unlink(curwin->w_alist);
6138 alist_new();
6139# else
6140 return; /* can't split, always fail */
6141# endif
6142 }
6143
6144 /*
6145 * Set up the new argument list.
6146 */
6147 alist_set(ALIST(curwin), filec, filev, FALSE);
6148
6149 /*
6150 * Move to the first file.
6151 */
6152 /* Fake up a minimal "next" command for do_argfile() */
6153 vim_memset(&ea, 0, sizeof(ea));
6154 ea.cmd = (char_u *)"next";
6155 do_argfile(&ea, 0);
6156
6157 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6158 * mode that is not needed here. */
6159 need_start_insertmode = FALSE;
6160
6161 /* Restore msg_scroll, otherwise a following command may cause scrolling
6162 * unexpectedly. The screen will be redrawn by the caller, thus
6163 * msg_scroll being set by displaying a message is irrelevant. */
6164 msg_scroll = save_msg_scroll;
6165}
6166#endif
6167
6168static void alist_clear __ARGS((alist_T *al));
6169/*
6170 * Clear an argument list: free all file names and reset it to zero entries.
6171 */
6172 static void
6173alist_clear(al)
6174 alist_T *al;
6175{
6176 while (--al->al_ga.ga_len >= 0)
6177 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6178 ga_clear(&al->al_ga);
6179}
6180
6181/*
6182 * Init an argument list.
6183 */
6184 void
6185alist_init(al)
6186 alist_T *al;
6187{
6188 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6189}
6190
6191#if defined(FEAT_WINDOWS) || defined(PROTO)
6192
6193/*
6194 * Remove a reference from an argument list.
6195 * Ignored when the argument list is the global one.
6196 * If the argument list is no longer used by any window, free it.
6197 */
6198 void
6199alist_unlink(al)
6200 alist_T *al;
6201{
6202 if (al != &global_alist && --al->al_refcount <= 0)
6203 {
6204 alist_clear(al);
6205 vim_free(al);
6206 }
6207}
6208
6209# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6210/*
6211 * Create a new argument list and use it for the current window.
6212 */
6213 void
6214alist_new()
6215{
6216 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6217 if (curwin->w_alist == NULL)
6218 {
6219 curwin->w_alist = &global_alist;
6220 ++global_alist.al_refcount;
6221 }
6222 else
6223 {
6224 curwin->w_alist->al_refcount = 1;
6225 alist_init(curwin->w_alist);
6226 }
6227}
6228# endif
6229#endif
6230
6231#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6232/*
6233 * Expand the file names in the global argument list.
6234 */
6235 void
6236alist_expand()
6237{
6238 char_u **old_arg_files;
6239 char_u **new_arg_files;
6240 int new_arg_file_count;
6241 char_u *save_p_su = p_su;
6242 int i;
6243
6244 /* Don't use 'suffixes' here. This should work like the shell did the
6245 * expansion. Also, the vimrc file isn't read yet, thus the user
6246 * can't set the options. */
6247 p_su = empty_option;
6248 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6249 if (old_arg_files != NULL)
6250 {
6251 for (i = 0; i < GARGCOUNT; ++i)
6252 old_arg_files[i] = GARGLIST[i].ae_fname;
6253 if (expand_wildcards(GARGCOUNT, old_arg_files,
6254 &new_arg_file_count, &new_arg_files,
6255 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6256 && new_arg_file_count > 0)
6257 {
6258 alist_set(&global_alist, new_arg_file_count, new_arg_files, TRUE);
6259 }
6260 vim_free(old_arg_files);
6261 }
6262 p_su = save_p_su;
6263}
6264#endif
6265
6266/*
6267 * Set the argument list for the current window.
6268 * Takes over the allocated files[] and the allocated fnames in it.
6269 */
6270 void
6271alist_set(al, count, files, use_curbuf)
6272 alist_T *al;
6273 int count;
6274 char_u **files;
6275 int use_curbuf;
6276{
6277 int i;
6278
6279 alist_clear(al);
6280 if (ga_grow(&al->al_ga, count) == OK)
6281 {
6282 for (i = 0; i < count; ++i)
6283 alist_add(al, files[i], use_curbuf ? 2 : 1);
6284 vim_free(files);
6285 }
6286 else
6287 FreeWild(count, files);
6288#ifdef FEAT_WINDOWS
6289 if (al == &global_alist)
6290#endif
6291 arg_had_last = FALSE;
6292}
6293
6294/*
6295 * Add file "fname" to argument list "al".
6296 * "fname" must have been allocated and "al" must have been checked for room.
6297 */
6298 void
6299alist_add(al, fname, set_fnum)
6300 alist_T *al;
6301 char_u *fname;
6302 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6303{
6304 if (fname == NULL) /* don't add NULL file names */
6305 return;
6306#ifdef BACKSLASH_IN_FILENAME
6307 slash_adjust(fname);
6308#endif
6309 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6310 if (set_fnum > 0)
6311 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6312 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6313 ++al->al_ga.ga_len;
6314 --al->al_ga.ga_room;
6315}
6316
6317#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6318/*
6319 * Adjust slashes in file names. Called after 'shellslash' was set.
6320 */
6321 void
6322alist_slash_adjust()
6323{
6324 int i;
6325# ifdef FEAT_WINDOWS
6326 win_T *wp;
6327# endif
6328
6329 for (i = 0; i < GARGCOUNT; ++i)
6330 if (GARGLIST[i].ae_fname != NULL)
6331 slash_adjust(GARGLIST[i].ae_fname);
6332# ifdef FEAT_WINDOWS
6333 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6334 if (wp->w_alist != &global_alist)
6335 for (i = 0; i < WARGCOUNT(wp); ++i)
6336 if (WARGLIST(wp)[i].ae_fname != NULL)
6337 slash_adjust(WARGLIST(wp)[i].ae_fname);
6338# endif
6339}
6340#endif
6341
6342/*
6343 * ":preserve".
6344 */
6345/*ARGSUSED*/
6346 static void
6347ex_preserve(eap)
6348 exarg_T *eap;
6349{
6350 ml_preserve(curbuf, TRUE);
6351}
6352
6353/*
6354 * ":recover".
6355 */
6356 static void
6357ex_recover(eap)
6358 exarg_T *eap;
6359{
6360 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6361 recoverymode = TRUE;
6362 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6363 && (*eap->arg == NUL
6364 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6365 ml_recover();
6366 recoverymode = FALSE;
6367}
6368
6369/*
6370 * Command modifier used in a wrong way.
6371 */
6372 static void
6373ex_wrongmodifier(eap)
6374 exarg_T *eap;
6375{
6376 eap->errmsg = e_invcmd;
6377}
6378
6379#ifdef FEAT_WINDOWS
6380/*
6381 * :sview [+command] file split window with new file, read-only
6382 * :split [[+command] file] split window with current or new file
6383 * :vsplit [[+command] file] split window vertically with current or new file
6384 * :new [[+command] file] split window with no or new file
6385 * :vnew [[+command] file] split vertically window with no or new file
6386 * :sfind [+command] file split window with file in 'path'
6387 */
6388 void
6389ex_splitview(eap)
6390 exarg_T *eap;
6391{
6392 win_T *old_curwin;
6393#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6394 char_u *fname = NULL;
6395#endif
6396#ifdef FEAT_BROWSE
6397 int browse_flag = cmdmod.browse;
6398#endif
6399
6400#ifndef FEAT_VERTSPLIT
6401 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6402 {
6403 ex_ni(eap);
6404 return;
6405 }
6406#endif
6407
6408 old_curwin = curwin;
6409#ifdef FEAT_GUI
6410 need_mouse_correct = TRUE;
6411#endif
6412
6413#ifdef FEAT_QUICKFIX
6414 /* A ":split" in the quickfix window works like ":new". Don't want two
6415 * quickfix windows. */
6416 if (bt_quickfix(curbuf))
6417 {
6418 if (eap->cmdidx == CMD_split)
6419 eap->cmdidx = CMD_new;
6420# ifdef FEAT_VERTSPLIT
6421 if (eap->cmdidx == CMD_vsplit)
6422 eap->cmdidx = CMD_vnew;
6423# endif
6424 }
6425#endif
6426
6427#ifdef FEAT_SEARCHPATH
6428 if (eap->cmdidx == CMD_sfind)
6429 {
6430 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6431 FNAME_MESS, TRUE, curbuf->b_ffname);
6432 if (fname == NULL)
6433 goto theend;
6434 eap->arg = fname;
6435 }
6436# ifdef FEAT_BROWSE
6437 else
6438# endif
6439#endif
6440#ifdef FEAT_BROWSE
6441 if (cmdmod.browse
6442# ifdef FEAT_VERTSPLIT
6443 && eap->cmdidx != CMD_vnew
6444#endif
6445 && eap->cmdidx != CMD_new)
6446 {
6447 fname = do_browse(FALSE, (char_u *)_("Edit File in new window"),
6448 eap->arg, NULL, NULL, NULL, curbuf);
6449 if (fname == NULL)
6450 goto theend;
6451 eap->arg = fname;
6452 }
6453 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6454#endif
6455
6456 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6457 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6458 {
6459#ifdef FEAT_SCROLLBIND
6460 /* Reset 'scrollbind' when editing another file, but keep it when
6461 * doing ":split" without arguments. */
6462 if (*eap->arg != NUL
6463#ifdef FEAT_BROWSE
6464 || cmdmod.browse
6465#endif
6466 )
6467 curwin->w_p_scb = FALSE;
6468 else
6469 do_check_scrollbind(FALSE);
6470#endif
6471 do_exedit(eap, old_curwin);
6472 }
6473
6474#ifdef FEAT_BROWSE
6475 cmdmod.browse = browse_flag;
6476#endif
6477
6478#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6479theend:
6480 vim_free(fname);
6481#endif
6482}
6483#endif
6484
6485/*
6486 * ":mode": Set screen mode.
6487 * If no argument given, just get the screen size and redraw.
6488 */
6489 static void
6490ex_mode(eap)
6491 exarg_T *eap;
6492{
6493 if (*eap->arg == NUL)
6494 shell_resized();
6495 else
6496 mch_screenmode(eap->arg);
6497}
6498
6499#ifdef FEAT_WINDOWS
6500/*
6501 * ":resize".
6502 * set, increment or decrement current window height
6503 */
6504 static void
6505ex_resize(eap)
6506 exarg_T *eap;
6507{
6508 int n;
6509 win_T *wp = curwin;
6510
6511 if (eap->addr_count > 0)
6512 {
6513 n = eap->line2;
6514 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6515 ;
6516 }
6517
6518#ifdef FEAT_GUI
6519 need_mouse_correct = TRUE;
6520#endif
6521 n = atol((char *)eap->arg);
6522#ifdef FEAT_VERTSPLIT
6523 if (cmdmod.split & WSP_VERT)
6524 {
6525 if (*eap->arg == '-' || *eap->arg == '+')
6526 n += W_WIDTH(curwin);
6527 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6528 n = 9999;
6529 win_setwidth_win((int)n, wp);
6530 }
6531 else
6532#endif
6533 {
6534 if (*eap->arg == '-' || *eap->arg == '+')
6535 n += curwin->w_height;
6536 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6537 n = 9999;
6538 win_setheight_win((int)n, wp);
6539 }
6540}
6541#endif
6542
6543/*
6544 * ":find [+command] <file>" command.
6545 */
6546 static void
6547ex_find(eap)
6548 exarg_T *eap;
6549{
6550#ifdef FEAT_SEARCHPATH
6551 char_u *fname;
6552 int count;
6553
6554 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6555 TRUE, curbuf->b_ffname);
6556 if (eap->addr_count > 0)
6557 {
6558 /* Repeat finding the file "count" times. This matters when it
6559 * appears several times in the path. */
6560 count = eap->line2;
6561 while (fname != NULL && --count > 0)
6562 {
6563 vim_free(fname);
6564 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6565 FALSE, curbuf->b_ffname);
6566 }
6567 }
6568
6569 if (fname != NULL)
6570 {
6571 eap->arg = fname;
6572#endif
6573 do_exedit(eap, NULL);
6574#ifdef FEAT_SEARCHPATH
6575 vim_free(fname);
6576 }
6577#endif
6578}
6579
6580/*
6581 * ":edit", ":badd".
6582 */
6583 static void
6584ex_edit(eap)
6585 exarg_T *eap;
6586{
6587 do_exedit(eap, NULL);
6588}
6589
6590/*
6591 * ":edit <file>" command and alikes.
6592 */
6593/*ARGSUSED*/
6594 void
6595do_exedit(eap, old_curwin)
6596 exarg_T *eap;
6597 win_T *old_curwin; /* curwin before doing a split or NULL */
6598{
6599 int n;
6600#ifdef FEAT_WINDOWS
6601 int need_hide;
6602#endif
6603
6604 /*
6605 * ":vi" command ends Ex mode.
6606 */
6607 if (exmode_active && (eap->cmdidx == CMD_visual
6608 || eap->cmdidx == CMD_view))
6609 {
6610 exmode_active = FALSE;
6611 if (*eap->arg == NUL)
6612 return;
6613 }
6614
6615 if ((eap->cmdidx == CMD_new
6616#ifdef FEAT_VERTSPLIT
6617 || eap->cmdidx == CMD_vnew
6618#endif
6619 ) && *eap->arg == NUL)
6620 {
6621 /* ":new" without argument: edit an new empty buffer */
6622 setpcmark();
6623 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
6624 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
6625 }
6626 else if ((eap->cmdidx != CMD_split
6627#ifdef FEAT_VERTSPLIT
6628 && eap->cmdidx != CMD_vsplit
6629#endif
6630 )
6631 || *eap->arg != NUL
6632#ifdef FEAT_BROWSE
6633 || cmdmod.browse
6634#endif
6635 )
6636 {
6637 n = readonlymode;
6638 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6639 readonlymode = TRUE;
6640 else if (eap->cmdidx == CMD_enew)
6641 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6642 empty buffer */
6643 setpcmark();
6644 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6645 NULL, eap,
6646 /* ":edit" goes to first line if Vi compatible */
6647 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6648 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6649 ? ECMD_ONE : eap->do_ecmd_lnum,
6650 (P_HID(curbuf) ? ECMD_HIDE : 0)
6651 + (eap->forceit ? ECMD_FORCEIT : 0)
6652#ifdef FEAT_LISTCMDS
6653 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
6654#endif
6655 ) == FAIL)
6656 {
6657 /* Editing the file failed. If the window was split, close it. */
6658#ifdef FEAT_WINDOWS
6659 if (old_curwin != NULL)
6660 {
6661 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
6662 if (!need_hide || P_HID(curbuf))
6663 {
6664# ifdef FEAT_GUI
6665 need_mouse_correct = TRUE;
6666# endif
6667 win_close(curwin, !need_hide && !P_HID(curbuf));
6668 }
6669 }
6670#endif
6671 }
6672 else if (readonlymode && curbuf->b_nwindows == 1)
6673 {
6674 /* When editing an already visited buffer, 'readonly' won't be set
6675 * but the previous value is kept. With ":view" and ":sview" we
6676 * want the file to be readonly, except when another window is
6677 * editing the same buffer. */
6678 curbuf->b_p_ro = TRUE;
6679 }
6680 readonlymode = n;
6681 }
6682 else
6683 {
6684 if (eap->do_ecmd_cmd != NULL)
6685 do_cmdline_cmd(eap->do_ecmd_cmd);
6686#ifdef FEAT_TITLE
6687 n = curwin->w_arg_idx_invalid;
6688#endif
6689 check_arg_idx(curwin);
6690#ifdef FEAT_TITLE
6691 if (n != curwin->w_arg_idx_invalid)
6692 maketitle();
6693#endif
6694 }
6695
6696#ifdef FEAT_WINDOWS
6697 /*
6698 * if ":split file" worked, set alternate file name in old window to new
6699 * file
6700 */
6701 if (old_curwin != NULL
6702 && *eap->arg != NUL
6703 && curwin != old_curwin
6704 && win_valid(old_curwin)
6705 && old_curwin->w_buffer != curbuf)
6706 old_curwin->w_alt_fnum = curbuf->b_fnum;
6707#endif
6708
6709 ex_no_reprint = TRUE;
6710}
6711
6712#ifndef FEAT_GUI
6713/*
6714 * ":gui" and ":gvim" when there is no GUI.
6715 */
6716 static void
6717ex_nogui(eap)
6718 exarg_T *eap;
6719{
6720 eap->errmsg = e_nogvim;
6721}
6722#endif
6723
6724#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
6725 static void
6726ex_tearoff(eap)
6727 exarg_T *eap;
6728{
6729 gui_make_tearoff(eap->arg);
6730}
6731#endif
6732
Bram Moolenaar843ee412004-06-30 16:16:41 +00006733#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 static void
6735ex_popup(eap)
6736 exarg_T *eap;
6737{
6738 gui_make_popup(eap->arg);
6739}
6740#endif
6741
6742/*ARGSUSED*/
6743 static void
6744ex_swapname(eap)
6745 exarg_T *eap;
6746{
6747 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
6748 MSG(_("No swap file"));
6749 else
6750 msg(curbuf->b_ml.ml_mfp->mf_fname);
6751}
6752
6753/*
6754 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6755 * offset.
6756 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6757 */
6758/*ARGSUSED*/
6759 static void
6760ex_syncbind(eap)
6761 exarg_T *eap;
6762{
6763#ifdef FEAT_SCROLLBIND
6764 win_T *wp;
6765 long topline;
6766 long y;
6767 linenr_T old_linenr = curwin->w_cursor.lnum;
6768
6769 setpcmark();
6770
6771 /*
6772 * determine max topline
6773 */
6774 if (curwin->w_p_scb)
6775 {
6776 topline = curwin->w_topline;
6777 for (wp = firstwin; wp; wp = wp->w_next)
6778 {
6779 if (wp->w_p_scb && wp->w_buffer)
6780 {
6781 y = wp->w_buffer->b_ml.ml_line_count - p_so;
6782 if (topline > y)
6783 topline = y;
6784 }
6785 }
6786 if (topline < 1)
6787 topline = 1;
6788 }
6789 else
6790 {
6791 topline = 1;
6792 }
6793
6794
6795 /*
6796 * set all scrollbind windows to the same topline
6797 */
6798 wp = curwin;
6799 for (curwin = firstwin; curwin; curwin = curwin->w_next)
6800 {
6801 if (curwin->w_p_scb)
6802 {
6803 y = topline - curwin->w_topline;
6804 if (y > 0)
6805 scrollup(y, TRUE);
6806 else
6807 scrolldown(-y, TRUE);
6808 curwin->w_scbind_pos = topline;
6809 redraw_later(VALID);
6810 cursor_correct();
6811#ifdef FEAT_WINDOWS
6812 curwin->w_redr_status = TRUE;
6813#endif
6814 }
6815 }
6816 curwin = wp;
6817 if (curwin->w_p_scb)
6818 {
6819 did_syncbind = TRUE;
6820 checkpcmark();
6821 if (old_linenr != curwin->w_cursor.lnum)
6822 {
6823 char_u ctrl_o[2];
6824
6825 ctrl_o[0] = Ctrl_O;
6826 ctrl_o[1] = 0;
6827 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6828 }
6829 }
6830#endif
6831}
6832
6833
6834 static void
6835ex_read(eap)
6836 exarg_T *eap;
6837{
6838 int i;
6839
6840 if (eap->usefilter) /* :r!cmd */
6841 do_bang(1, eap, FALSE, FALSE, TRUE);
6842 else
6843 {
6844 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6845 return;
6846
6847#ifdef FEAT_BROWSE
6848 if (cmdmod.browse)
6849 {
6850 char_u *browseFile;
6851
6852 browseFile = do_browse(FALSE, (char_u *)_("Append File"), eap->arg,
6853 NULL, NULL, NULL, curbuf);
6854 if (browseFile != NULL)
6855 {
6856 i = readfile(browseFile, NULL,
6857 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6858 vim_free(browseFile);
6859 }
6860 else
6861 i = OK;
6862 }
6863 else
6864#endif
6865 if (*eap->arg == NUL)
6866 {
6867 if (check_fname() == FAIL) /* check for no file name */
6868 return;
6869 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6870 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6871 }
6872 else
6873 {
6874 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6875 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6876 i = readfile(eap->arg, NULL,
6877 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6878
6879 }
6880 if (i == FAIL)
6881 {
6882#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6883 if (!aborting())
6884#endif
6885 EMSG2(_(e_notopen), eap->arg);
6886 }
6887 else
6888 redraw_curbuf_later(VALID);
6889 }
6890}
6891
6892/*
6893 * ":cd", ":lcd", ":chdir" and ":lchdir".
6894 */
6895 static void
6896ex_cd(eap)
6897 exarg_T *eap;
6898{
6899 static char_u *prev_dir = NULL;
6900 char_u *new_dir;
6901 char_u *tofree;
6902
6903 new_dir = eap->arg;
6904#if !defined(UNIX) && !defined(VMS)
6905 /* for non-UNIX ":cd" means: print current directory */
6906 if (*new_dir == NUL)
6907 ex_pwd(NULL);
6908 else
6909#endif
6910 {
6911 /* ":cd -": Change to previous directory */
6912 if (STRCMP(new_dir, "-") == 0)
6913 {
6914 if (prev_dir == NULL)
6915 {
6916 EMSG(_("E186: No previous directory"));
6917 return;
6918 }
6919 new_dir = prev_dir;
6920 }
6921
6922 /* Save current directory for next ":cd -" */
6923 tofree = prev_dir;
6924 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6925 prev_dir = vim_strsave(NameBuff);
6926 else
6927 prev_dir = NULL;
6928
6929#if defined(UNIX) || defined(VMS)
6930 /* for UNIX ":cd" means: go to home directory */
6931 if (*new_dir == NUL)
6932 {
6933 /* use NameBuff for home directory name */
6934# ifdef VMS
6935 char_u *p;
6936
6937 p = mch_getenv((char_u *)"SYS$LOGIN");
6938 if (p == NULL || *p == NUL) /* empty is the same as not set */
6939 NameBuff[0] = NUL;
6940 else
6941 STRNCPY(NameBuff, p, MAXPATHL);
6942# else
6943 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6944# endif
6945 new_dir = NameBuff;
6946 }
6947#endif
6948 if (new_dir == NULL || vim_chdir(new_dir))
6949 EMSG(_(e_failed));
6950 else
6951 {
6952 vim_free(curwin->w_localdir);
6953 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6954 {
6955 /* If still in global directory, need to remember current
6956 * directory as global directory. */
6957 if (globaldir == NULL && prev_dir != NULL)
6958 globaldir = vim_strsave(prev_dir);
6959 /* Remember this local directory for the window. */
6960 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6961 curwin->w_localdir = vim_strsave(NameBuff);
6962 }
6963 else
6964 {
6965 /* We are now in the global directory, no need to remember its
6966 * name. */
6967 vim_free(globaldir);
6968 globaldir = NULL;
6969 curwin->w_localdir = NULL;
6970 }
6971
6972 shorten_fnames(TRUE);
6973
6974 /* Echo the new current directory if the command was typed. */
6975 if (KeyTyped)
6976 ex_pwd(eap);
6977 }
6978 vim_free(tofree);
6979 }
6980}
6981
6982/*
6983 * ":pwd".
6984 */
6985/*ARGSUSED*/
6986 static void
6987ex_pwd(eap)
6988 exarg_T *eap;
6989{
6990 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6991 {
6992#ifdef BACKSLASH_IN_FILENAME
6993 slash_adjust(NameBuff);
6994#endif
6995 msg(NameBuff);
6996 }
6997 else
6998 EMSG(_("E187: Unknown"));
6999}
7000
7001/*
7002 * ":=".
7003 */
7004 static void
7005ex_equal(eap)
7006 exarg_T *eap;
7007{
7008 smsg((char_u *)"%ld", (long)eap->line2);
7009}
7010
7011 static void
7012ex_sleep(eap)
7013 exarg_T *eap;
7014{
7015 int n;
7016
7017 if (cursor_valid())
7018 {
7019 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7020 if (n >= 0)
7021 windgoto((int)n, curwin->w_wcol);
7022 }
7023 do_sleep(eap->line2 * (*eap->arg == 'm' ? 1L : 1000L));
7024}
7025
7026/*
7027 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7028 */
7029 void
7030do_sleep(msec)
7031 long msec;
7032{
7033 long done;
7034
7035 cursor_on();
7036 out_flush();
7037 for (done = 0; !got_int && done < msec; done += 1000L)
7038 {
7039 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7040 ui_breakcheck();
7041 }
7042}
7043
7044 static void
7045do_exmap(eap, isabbrev)
7046 exarg_T *eap;
7047 int isabbrev;
7048{
7049 int mode;
7050 char_u *cmdp;
7051
7052 cmdp = eap->cmd;
7053 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7054
7055 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7056 eap->arg, mode, isabbrev))
7057 {
7058 case 1: EMSG(_(e_invarg));
7059 break;
7060 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7061 break;
7062 }
7063}
7064
7065/*
7066 * ":winsize" command (obsolete).
7067 */
7068 static void
7069ex_winsize(eap)
7070 exarg_T *eap;
7071{
7072 int w, h;
7073 char_u *arg = eap->arg;
7074 char_u *p;
7075
7076 w = getdigits(&arg);
7077 arg = skipwhite(arg);
7078 p = arg;
7079 h = getdigits(&arg);
7080 if (*p != NUL && *arg == NUL)
7081 set_shellsize(w, h, TRUE);
7082 else
7083 EMSG(_("E465: :winsize requires two number arguments"));
7084}
7085
7086#ifdef FEAT_WINDOWS
7087 static void
7088ex_wincmd(eap)
7089 exarg_T *eap;
7090{
7091 int xchar = NUL;
7092 char_u *p;
7093
7094 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7095 {
7096 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7097 if (eap->arg[1] == NUL)
7098 {
7099 EMSG(_(e_invarg));
7100 return;
7101 }
7102 xchar = eap->arg[1];
7103 p = eap->arg + 2;
7104 }
7105 else
7106 p = eap->arg + 1;
7107
7108 eap->nextcmd = check_nextcmd(p);
7109 p = skipwhite(p);
7110 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7111 EMSG(_(e_invarg));
7112 else
7113 {
7114 /* Pass flags on for ":vertical wincmd ]". */
7115 postponed_split_flags = cmdmod.split;
7116 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7117 postponed_split_flags = 0;
7118 }
7119}
7120#endif
7121
Bram Moolenaar843ee412004-06-30 16:16:41 +00007122#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123/*
7124 * ":winpos".
7125 */
7126 static void
7127ex_winpos(eap)
7128 exarg_T *eap;
7129{
7130 int x, y;
7131 char_u *arg = eap->arg;
7132 char_u *p;
7133
7134 if (*arg == NUL)
7135 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007136# if defined(FEAT_GUI) || defined(MSWIN)
7137# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007139# else
7140 if (mch_get_winpos(&x, &y) != FAIL)
7141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {
7143 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7144 msg(IObuff);
7145 }
7146 else
7147# endif
7148 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7149 }
7150 else
7151 {
7152 x = getdigits(&arg);
7153 arg = skipwhite(arg);
7154 p = arg;
7155 y = getdigits(&arg);
7156 if (*p == NUL || *arg != NUL)
7157 {
7158 EMSG(_("E466: :winpos requires two number arguments"));
7159 return;
7160 }
7161# ifdef FEAT_GUI
7162 if (gui.in_use)
7163 gui_mch_set_winpos(x, y);
7164 else if (gui.starting)
7165 {
7166 /* Remember the coordinates for when the window is opened. */
7167 gui_win_x = x;
7168 gui_win_y = y;
7169 }
7170# ifdef HAVE_TGETENT
7171 else
7172# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007173# else
7174# ifdef MSWIN
7175 mch_set_winpos(x, y);
7176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177# endif
7178# ifdef HAVE_TGETENT
7179 if (*T_CWP)
7180 term_set_winpos(x, y);
7181# endif
7182 }
7183}
7184#endif
7185
7186/*
7187 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7188 */
7189 static void
7190ex_operators(eap)
7191 exarg_T *eap;
7192{
7193 oparg_T oa;
7194
7195 clear_oparg(&oa);
7196 oa.regname = eap->regname;
7197 oa.start.lnum = eap->line1;
7198 oa.end.lnum = eap->line2;
7199 oa.line_count = eap->line2 - eap->line1 + 1;
7200 oa.motion_type = MLINE;
7201#ifdef FEAT_VIRTUALEDIT
7202 virtual_op = FALSE;
7203#endif
7204 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7205 {
7206 setpcmark();
7207 curwin->w_cursor.lnum = eap->line1;
7208 beginline(BL_SOL | BL_FIX);
7209 }
7210
7211 switch (eap->cmdidx)
7212 {
7213 case CMD_delete:
7214 oa.op_type = OP_DELETE;
7215 op_delete(&oa);
7216 break;
7217
7218 case CMD_yank:
7219 oa.op_type = OP_YANK;
7220 (void)op_yank(&oa, FALSE, TRUE);
7221 break;
7222
7223 default: /* CMD_rshift or CMD_lshift */
7224 if ((eap->cmdidx == CMD_rshift)
7225#ifdef FEAT_RIGHTLEFT
7226 ^ curwin->w_p_rl
7227#endif
7228 )
7229 oa.op_type = OP_RSHIFT;
7230 else
7231 oa.op_type = OP_LSHIFT;
7232 op_shift(&oa, FALSE, eap->amount);
7233 break;
7234 }
7235#ifdef FEAT_VIRTUALEDIT
7236 virtual_op = MAYBE;
7237#endif
7238}
7239
7240/*
7241 * ":put".
7242 */
7243 static void
7244ex_put(eap)
7245 exarg_T *eap;
7246{
7247 /* ":0put" works like ":1put!". */
7248 if (eap->line2 == 0)
7249 {
7250 eap->line2 = 1;
7251 eap->forceit = TRUE;
7252 }
7253 curwin->w_cursor.lnum = eap->line2;
7254 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L, PUT_LINE);
7255}
7256
7257/*
7258 * Handle ":copy" and ":move".
7259 */
7260 static void
7261ex_copymove(eap)
7262 exarg_T *eap;
7263{
7264 long n;
7265
7266 n = get_address(&eap->arg, FALSE, FALSE);
7267 if (eap->arg == NULL) /* error detected */
7268 {
7269 eap->nextcmd = NULL;
7270 return;
7271 }
7272
7273 /*
7274 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7275 */
7276 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7277 {
7278 EMSG(_(e_invaddr));
7279 return;
7280 }
7281
7282 if (eap->cmdidx == CMD_move)
7283 {
7284 if (do_move(eap->line1, eap->line2, n) == FAIL)
7285 return;
7286 }
7287 else
7288 ex_copy(eap->line1, eap->line2, n);
7289 u_clearline();
7290 beginline(BL_SOL | BL_FIX);
7291}
7292
7293/*
7294 * ":smagic" and ":snomagic".
7295 */
7296 static void
7297ex_submagic(eap)
7298 exarg_T *eap;
7299{
7300 int magic_save = p_magic;
7301
7302 p_magic = (eap->cmdidx == CMD_smagic);
7303 do_sub(eap);
7304 p_magic = magic_save;
7305}
7306
7307/*
7308 * ":join".
7309 */
7310 static void
7311ex_join(eap)
7312 exarg_T *eap;
7313{
7314 curwin->w_cursor.lnum = eap->line1;
7315 if (eap->line1 == eap->line2)
7316 {
7317 if (eap->addr_count >= 2) /* :2,2join does nothing */
7318 return;
7319 if (eap->line2 == curbuf->b_ml.ml_line_count)
7320 {
7321 beep_flush();
7322 return;
7323 }
7324 ++eap->line2;
7325 }
7326 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
7327 beginline(BL_WHITE | BL_FIX);
7328}
7329
7330/*
7331 * ":[addr]@r" or ":[addr]*r": execute register
7332 */
7333 static void
7334ex_at(eap)
7335 exarg_T *eap;
7336{
7337 int c;
7338
7339 curwin->w_cursor.lnum = eap->line2;
7340
7341#ifdef USE_ON_FLY_SCROLL
7342 dont_scroll = TRUE; /* disallow scrolling here */
7343#endif
7344
7345 /* get the register name. No name means to use the previous one */
7346 c = *eap->arg;
7347 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7348 c = '@';
7349 /* put the register in mapbuf */
7350 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
7351 beep_flush();
7352 else
7353 {
7354 int save_efr = exec_from_reg;
7355
7356 exec_from_reg = TRUE;
7357
7358 /*
7359 * Execute from the typeahead buffer.
7360 * Originally this didn't check for the typeahead buffer to be empty,
7361 * thus could read more Ex commands from stdin. It's not clear why,
7362 * it is certainly unexpected.
7363 */
7364 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
7365 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7366
7367 exec_from_reg = save_efr;
7368 }
7369}
7370
7371/*
7372 * ":!".
7373 */
7374 static void
7375ex_bang(eap)
7376 exarg_T *eap;
7377{
7378 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7379}
7380
7381/*
7382 * ":undo".
7383 */
7384/*ARGSUSED*/
7385 static void
7386ex_undo(eap)
7387 exarg_T *eap;
7388{
7389 u_undo(1);
7390}
7391
7392/*
7393 * ":redo".
7394 */
7395/*ARGSUSED*/
7396 static void
7397ex_redo(eap)
7398 exarg_T *eap;
7399{
7400 u_redo(1);
7401}
7402
7403/*
7404 * ":redir": start/stop redirection.
7405 */
7406 static void
7407ex_redir(eap)
7408 exarg_T *eap;
7409{
7410 char *mode;
7411 char_u *fname;
7412
7413 if (STRICMP(eap->arg, "END") == 0)
7414 close_redir();
7415 else
7416 {
7417 if (*eap->arg == '>')
7418 {
7419 ++eap->arg;
7420 if (*eap->arg == '>')
7421 {
7422 ++eap->arg;
7423 mode = "a";
7424 }
7425 else
7426 mode = "w";
7427 eap->arg = skipwhite(eap->arg);
7428
7429 close_redir();
7430
7431 /* Expand environment variables and "~/". */
7432 fname = expand_env_save(eap->arg);
7433 if (fname == NULL)
7434 return;
7435#ifdef FEAT_BROWSE
7436 if (cmdmod.browse)
7437 {
7438 char_u *browseFile;
7439
7440 browseFile = do_browse(TRUE, (char_u *)_("Save Redirection"),
7441 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
7442 if (browseFile == NULL)
7443 return; /* operation cancelled */
7444 vim_free(fname);
7445 fname = browseFile;
7446 eap->forceit = TRUE; /* since dialog already asked */
7447 }
7448#endif
7449
7450 redir_fd = open_exfile(fname, eap->forceit, mode);
7451 vim_free(fname);
7452 }
7453#ifdef FEAT_EVAL
7454 else if (*eap->arg == '@')
7455 {
7456 /* redirect to a register a-z (resp. A-Z for appending) */
7457 close_redir();
7458 ++eap->arg;
7459 if (ASCII_ISALPHA(*eap->arg)
7460# ifdef FEAT_CLIPBOARD
7461 || *eap->arg == '*'
7462# endif
7463 || *eap->arg == '"')
7464 {
7465 redir_reg = *eap->arg;
7466 if (islower(redir_reg)
7467# ifdef FEAT_CLIPBOARD
7468 || redir_reg == '*'
7469# endif
7470 || redir_reg == '"')
7471 {
7472 /* make register empty */
7473 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
7474 }
7475 }
7476 else
7477 EMSG(_(e_invarg));
7478 }
7479#endif
7480
7481 /* TODO: redirect to a buffer */
7482
7483 /* TODO: redirect to an internal variable */
7484
7485 else
7486 EMSG(_(e_invarg));
7487 }
7488}
7489
7490/*
7491 * ":redraw": force redraw
7492 */
7493 static void
7494ex_redraw(eap)
7495 exarg_T *eap;
7496{
7497 int r = RedrawingDisabled;
7498 int p = p_lz;
7499
7500 RedrawingDisabled = 0;
7501 p_lz = FALSE;
7502 update_topline();
7503 update_screen(eap->forceit ? CLEAR :
7504#ifdef FEAT_VISUAL
7505 VIsual_active ? INVERTED :
7506#endif
7507 0);
7508#ifdef FEAT_TITLE
7509 if (need_maketitle)
7510 maketitle();
7511#endif
7512 RedrawingDisabled = r;
7513 p_lz = p;
7514
7515 /* Reset msg_didout, so that a message that's there is overwritten. */
7516 msg_didout = FALSE;
7517 msg_col = 0;
7518
7519 out_flush();
7520}
7521
7522/*
7523 * ":redrawstatus": force redraw of status line(s)
7524 */
7525/*ARGSUSED*/
7526 static void
7527ex_redrawstatus(eap)
7528 exarg_T *eap;
7529{
7530#if defined(FEAT_WINDOWS)
7531 int r = RedrawingDisabled;
7532 int p = p_lz;
7533
7534 RedrawingDisabled = 0;
7535 p_lz = FALSE;
7536 if (eap->forceit)
7537 status_redraw_all();
7538 else
7539 status_redraw_curbuf();
7540 update_screen(
7541# ifdef FEAT_VISUAL
7542 VIsual_active ? INVERTED :
7543# endif
7544 0);
7545 RedrawingDisabled = r;
7546 p_lz = p;
7547 out_flush();
7548#endif
7549}
7550
7551 static void
7552close_redir()
7553{
7554 if (redir_fd != NULL)
7555 {
7556 fclose(redir_fd);
7557 redir_fd = NULL;
7558 }
7559#ifdef FEAT_EVAL
7560 redir_reg = 0;
7561#endif
7562}
7563
7564#if defined(FEAT_SESSION) && defined(USE_CRNL)
7565# define MKSESSION_NL
7566static int mksession_nl = FALSE; /* use NL only in put_eol() */
7567#endif
7568
7569/*
7570 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
7571 */
7572 static void
7573ex_mkrc(eap)
7574 exarg_T *eap;
7575{
7576 FILE *fd;
7577 int failed = FALSE;
7578 char_u *fname;
7579#ifdef FEAT_BROWSE
7580 char_u *browseFile = NULL;
7581#endif
7582#ifdef FEAT_SESSION
7583 int view_session = FALSE;
7584 int using_vdir = FALSE; /* using 'viewdir'? */
7585 char_u *viewFile = NULL;
7586 unsigned *flagp;
7587#endif
7588
7589 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
7590 {
7591#ifdef FEAT_SESSION
7592 view_session = TRUE;
7593#else
7594 ex_ni(eap);
7595 return;
7596#endif
7597 }
7598
7599#ifdef FEAT_SESSION
7600 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
7601 if (eap->cmdidx == CMD_mkview
7602 && (*eap->arg == NUL
7603 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
7604 {
7605 eap->forceit = TRUE;
7606 fname = get_view_file(*eap->arg);
7607 if (fname == NULL)
7608 return;
7609 viewFile = fname;
7610 using_vdir = TRUE;
7611 }
7612 else
7613#endif
7614 if (*eap->arg != NUL)
7615 fname = eap->arg;
7616 else if (eap->cmdidx == CMD_mkvimrc)
7617 fname = (char_u *)VIMRC_FILE;
7618#ifdef FEAT_SESSION
7619 else if (eap->cmdidx == CMD_mksession)
7620 fname = (char_u *)SESSION_FILE;
7621#endif
7622 else
7623 fname = (char_u *)EXRC_FILE;
7624
7625#ifdef FEAT_BROWSE
7626 if (cmdmod.browse)
7627 {
7628 browseFile = do_browse(TRUE,
7629# ifdef FEAT_SESSION
7630 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
7631 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
7632# endif
7633 (char_u *)_("Save Setup"),
7634 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
7635 if (browseFile == NULL)
7636 goto theend;
7637 fname = browseFile;
7638 eap->forceit = TRUE; /* since dialog already asked */
7639 }
7640#endif
7641
7642#if defined(FEAT_SESSION) && defined(vim_mkdir)
7643 /* When using 'viewdir' may have to create the directory. */
7644 if (using_vdir && !mch_isdir(p_vdir))
7645 vim_mkdir(p_vdir, 0755); /* ignore errors, open_exfile() will fail */
7646#endif
7647
7648 fd = open_exfile(fname, eap->forceit, WRITEBIN);
7649 if (fd != NULL)
7650 {
7651#ifdef FEAT_SESSION
7652 if (eap->cmdidx == CMD_mkview)
7653 flagp = &vop_flags;
7654 else
7655 flagp = &ssop_flags;
7656#endif
7657
7658#ifdef MKSESSION_NL
7659 /* "unix" in 'sessionoptions': use NL line separator */
7660 if (view_session && (*flagp & SSOP_UNIX))
7661 mksession_nl = TRUE;
7662#endif
7663
7664 /* Write the version command for :mkvimrc */
7665 if (eap->cmdidx == CMD_mkvimrc)
7666 (void)put_line(fd, "version 6.0");
7667
7668#ifdef FEAT_SESSION
7669 if (eap->cmdidx != CMD_mkview)
7670#endif
7671 {
7672 /* Write setting 'compatible' first, because it has side effects.
7673 * For that same reason only do it when needed. */
7674 if (p_cp)
7675 (void)put_line(fd, "if !&cp | set cp | endif");
7676 else
7677 (void)put_line(fd, "if &cp | set nocp | endif");
7678 }
7679
7680#ifdef FEAT_SESSION
7681 if (!view_session
7682 || (eap->cmdidx == CMD_mksession
7683 && (*flagp & SSOP_OPTIONS)))
7684#endif
7685 failed |= (makemap(fd, NULL) == FAIL
7686 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
7687
7688#ifdef FEAT_SESSION
7689 if (!failed && view_session)
7690 {
7691 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
7692 failed = TRUE;
7693 if (eap->cmdidx == CMD_mksession)
7694 {
7695 char_u dirnow[MAXPATHL]; /* current directory */
7696
7697 /*
7698 * Change to session file's dir.
7699 */
7700 if (mch_dirname(dirnow, MAXPATHL) == FAIL
7701 || mch_chdir((char *)dirnow) != 0)
7702 *dirnow = NUL;
7703 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
7704 {
7705 if (vim_chdirfile(fname) == OK)
7706 shorten_fnames(TRUE);
7707 }
7708 else if (*dirnow != NUL
7709 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
7710 {
7711 (void)mch_chdir((char *)globaldir);
7712 shorten_fnames(TRUE);
7713 }
7714
7715 failed |= (makeopens(fd, dirnow) == FAIL);
7716
7717 /* restore original dir */
7718 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
7719 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
7720 {
7721 if (mch_chdir((char *)dirnow) != 0)
7722 EMSG(_(e_prev_dir));
7723 shorten_fnames(TRUE);
7724 }
7725 }
7726 else
7727 {
7728 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
7729 }
7730 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
7731 == FAIL)
7732 failed = TRUE;
7733 }
7734#endif
7735 failed |= fclose(fd);
7736
7737 if (failed)
7738 EMSG(_(e_write));
7739#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
7740 else if (eap->cmdidx == CMD_mksession)
7741 {
7742 /* successful session write - set this_session var */
7743 char_u tbuf[MAXPATHL];
7744
7745 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
7746 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
7747 }
7748#endif
7749#ifdef MKSESSION_NL
7750 mksession_nl = FALSE;
7751#endif
7752 }
7753
7754#ifdef FEAT_BROWSE
7755theend:
7756 vim_free(browseFile);
7757#endif
7758#ifdef FEAT_SESSION
7759 vim_free(viewFile);
7760#endif
7761}
7762
7763/*
7764 * Open a file for writing for an Ex command, with some checks.
7765 * Return file descriptor, or NULL on failure.
7766 */
7767 FILE *
7768open_exfile(fname, forceit, mode)
7769 char_u *fname;
7770 int forceit;
7771 char *mode; /* "w" for create new file or "a" for append */
7772{
7773 FILE *fd;
7774
7775#ifdef UNIX
7776 /* with Unix it is possible to open a directory */
7777 if (mch_isdir(fname))
7778 {
7779 EMSG2(_(e_isadir2), fname);
7780 return NULL;
7781 }
7782#endif
7783 if (!forceit && *mode != 'a' && vim_fexists(fname))
7784 {
7785 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
7786 return NULL;
7787 }
7788
7789 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
7790 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
7791
7792 return fd;
7793}
7794
7795/*
7796 * ":mark" and ":k".
7797 */
7798 static void
7799ex_mark(eap)
7800 exarg_T *eap;
7801{
7802 pos_T pos;
7803
7804 if (*eap->arg == NUL) /* No argument? */
7805 EMSG(_(e_argreq));
7806 else if (eap->arg[1] != NUL) /* more than one character? */
7807 EMSG(_(e_trailing));
7808 else
7809 {
7810 pos = curwin->w_cursor; /* save curwin->w_cursor */
7811 curwin->w_cursor.lnum = eap->line2;
7812 beginline(BL_WHITE | BL_FIX);
7813 if (setmark(*eap->arg) == FAIL) /* set mark */
7814 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
7815 curwin->w_cursor = pos; /* restore curwin->w_cursor */
7816 }
7817}
7818
7819/*
7820 * Update w_topline, w_leftcol and the cursor position.
7821 */
7822 void
7823update_topline_cursor()
7824{
7825 check_cursor(); /* put cursor on valid line */
7826 update_topline();
7827 if (!curwin->w_p_wrap)
7828 validate_cursor();
7829 update_curswant();
7830}
7831
7832#ifdef FEAT_EX_EXTRA
7833/*
7834 * ":normal[!] {commands}": Execute normal mode commands.
7835 */
7836 static void
7837ex_normal(eap)
7838 exarg_T *eap;
7839{
7840 oparg_T oa;
7841 int save_msg_scroll = msg_scroll;
7842 int save_restart_edit = restart_edit;
7843 int save_msg_didout = msg_didout;
7844 int save_State = State;
7845 tasave_T tabuf;
7846 int save_insertmode = p_im;
7847 int save_finish_op = finish_op;
7848#ifdef FEAT_MBYTE
7849 char_u *arg = NULL;
7850 int l;
7851 char_u *p;
7852#endif
7853
7854 if (ex_normal_busy >= p_mmd)
7855 {
7856 EMSG(_("E192: Recursive use of :normal too deep"));
7857 return;
7858 }
7859 ++ex_normal_busy;
7860
7861 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
7862 restart_edit = 0; /* don't go to Insert mode */
7863 p_im = FALSE; /* don't use 'insertmode' */
7864
7865#ifdef FEAT_MBYTE
7866 /*
7867 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7868 * this for the K_SPECIAL leading byte, otherwise special keys will not
7869 * work.
7870 */
7871 if (has_mbyte)
7872 {
7873 int len = 0;
7874
7875 /* Count the number of characters to be escaped. */
7876 for (p = eap->arg; *p != NUL; ++p)
7877 {
7878# ifdef FEAT_GUI
7879 if (*p == CSI) /* leadbyte CSI */
7880 len += 2;
7881# endif
7882 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
7883 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
7884# ifdef FEAT_GUI
7885 || *p == CSI
7886# endif
7887 )
7888 len += 2;
7889 }
7890 if (len > 0)
7891 {
7892 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
7893 if (arg != NULL)
7894 {
7895 len = 0;
7896 for (p = eap->arg; *p != NUL; ++p)
7897 {
7898 arg[len++] = *p;
7899# ifdef FEAT_GUI
7900 if (*p == CSI)
7901 {
7902 arg[len++] = KS_EXTRA;
7903 arg[len++] = (int)KE_CSI;
7904 }
7905# endif
7906 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
7907 {
7908 arg[len++] = *++p;
7909 if (*p == K_SPECIAL)
7910 {
7911 arg[len++] = KS_SPECIAL;
7912 arg[len++] = KE_FILLER;
7913 }
7914# ifdef FEAT_GUI
7915 else if (*p == CSI)
7916 {
7917 arg[len++] = KS_EXTRA;
7918 arg[len++] = (int)KE_CSI;
7919 }
7920# endif
7921 }
7922 arg[len] = NUL;
7923 }
7924 }
7925 }
7926 }
7927#endif
7928
7929 /*
7930 * Save the current typeahead. This is required to allow using ":normal"
7931 * from an event handler and makes sure we don't hang when the argument
7932 * ends with half a command.
7933 */
7934 save_typeahead(&tabuf);
7935 if (tabuf.typebuf_valid)
7936 {
7937 /*
7938 * Repeat the :normal command for each line in the range. When no
7939 * range given, execute it just once, without positioning the cursor
7940 * first.
7941 */
7942 do
7943 {
7944 clear_oparg(&oa);
7945 finish_op = FALSE;
7946 if (eap->addr_count != 0)
7947 {
7948 curwin->w_cursor.lnum = eap->line1++;
7949 curwin->w_cursor.col = 0;
7950 }
7951
7952 /*
7953 * Stuff the argument into the typeahead buffer.
7954 * Execute normal_cmd() until there is no typeahead left.
7955 */
7956 ins_typebuf(
7957#ifdef FEAT_MBYTE
7958 arg != NULL ? arg :
7959#endif
7960 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, 0,
7961 TRUE, FALSE);
7962 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
7963 && !got_int)
7964 {
7965 update_topline_cursor();
7966 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
7967 }
7968 }
7969 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7970 }
7971
7972 /* Might not return to the main loop when in an event handler. */
7973 update_topline_cursor();
7974
7975 /* Restore the previous typeahead. */
7976 restore_typeahead(&tabuf);
7977
7978 --ex_normal_busy;
7979 msg_scroll = save_msg_scroll;
7980 restart_edit = save_restart_edit;
7981 p_im = save_insertmode;
7982 finish_op = save_finish_op;
7983 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
7984
7985 /* Restore the state (needed when called from a function executed for
7986 * 'indentexpr'). */
7987 State = save_State;
7988#ifdef FEAT_MBYTE
7989 vim_free(arg);
7990#endif
7991}
7992
7993/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007994 * ":startinsert" and ":startreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 */
7996 static void
7997ex_startinsert(eap)
7998 exarg_T *eap;
7999{
8000 if (eap->forceit)
8001 {
8002 coladvance((colnr_T)MAXCOL);
8003 curwin->w_curswant = MAXCOL;
8004 curwin->w_set_curswant = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008005 if (eap->cmdidx == CMD_startinsert)
8006 restart_edit = 'a';
8007 else
8008 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 }
8010 else
8011 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008012 if (eap->cmdidx == CMD_startinsert)
8013 restart_edit = 'i';
8014 else
8015 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 curwin->w_curswant = 0; /* avoid MAXCOL */
8017 }
8018}
8019
8020/*
8021 * ":stopinsert"
8022 */
8023/*ARGSUSED*/
8024 static void
8025ex_stopinsert(eap)
8026 exarg_T *eap;
8027{
8028 restart_edit = 0;
8029 stop_insert_mode = TRUE;
8030}
8031#endif
8032
8033#ifdef FEAT_FIND_ID
8034 static void
8035ex_checkpath(eap)
8036 exarg_T *eap;
8037{
8038 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8039 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8040 (linenr_T)1, (linenr_T)MAXLNUM);
8041}
8042
8043#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8044/*
8045 * ":psearch"
8046 */
8047 static void
8048ex_psearch(eap)
8049 exarg_T *eap;
8050{
8051 g_do_tagpreview = p_pvh;
8052 ex_findpat(eap);
8053 g_do_tagpreview = 0;
8054}
8055#endif
8056
8057 static void
8058ex_findpat(eap)
8059 exarg_T *eap;
8060{
8061 int whole = TRUE;
8062 long n;
8063 char_u *p;
8064 int action;
8065
8066 switch (cmdnames[eap->cmdidx].cmd_name[2])
8067 {
8068 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8069 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8070 action = ACTION_GOTO;
8071 else
8072 action = ACTION_SHOW;
8073 break;
8074 case 'i': /* ":ilist" and ":dlist" */
8075 action = ACTION_SHOW_ALL;
8076 break;
8077 case 'u': /* ":ijump" and ":djump" */
8078 action = ACTION_GOTO;
8079 break;
8080 default: /* ":isplit" and ":dsplit" */
8081 action = ACTION_SPLIT;
8082 break;
8083 }
8084
8085 n = 1;
8086 if (vim_isdigit(*eap->arg)) /* get count */
8087 {
8088 n = getdigits(&eap->arg);
8089 eap->arg = skipwhite(eap->arg);
8090 }
8091 if (*eap->arg == '/') /* Match regexp, not just whole words */
8092 {
8093 whole = FALSE;
8094 ++eap->arg;
8095 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8096 if (*p)
8097 {
8098 *p++ = NUL;
8099 p = skipwhite(p);
8100
8101 /* Check for trailing illegal characters */
8102 if (!ends_excmd(*p))
8103 eap->errmsg = e_trailing;
8104 else
8105 eap->nextcmd = check_nextcmd(p);
8106 }
8107 }
8108 if (!eap->skip)
8109 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8110 whole, !eap->forceit,
8111 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8112 n, action, eap->line1, eap->line2);
8113}
8114#endif
8115
8116#ifdef FEAT_WINDOWS
8117
8118# ifdef FEAT_QUICKFIX
8119/*
8120 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8121 */
8122 static void
8123ex_ptag(eap)
8124 exarg_T *eap;
8125{
8126 g_do_tagpreview = p_pvh;
8127 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8128}
8129
8130/*
8131 * ":pedit"
8132 */
8133 static void
8134ex_pedit(eap)
8135 exarg_T *eap;
8136{
8137 win_T *curwin_save = curwin;
8138
8139 g_do_tagpreview = p_pvh;
8140 prepare_tagpreview();
8141 keep_help_flag = curwin_save->w_buffer->b_help;
8142 do_exedit(eap, NULL);
8143 keep_help_flag = FALSE;
8144 if (curwin != curwin_save && win_valid(curwin_save))
8145 {
8146 /* Return cursor to where we were */
8147 validate_cursor();
8148 redraw_later(VALID);
8149 win_enter(curwin_save, TRUE);
8150 }
8151 g_do_tagpreview = 0;
8152}
8153# endif
8154
8155/*
8156 * ":stag", ":stselect" and ":stjump".
8157 */
8158 static void
8159ex_stag(eap)
8160 exarg_T *eap;
8161{
8162 postponed_split = -1;
8163 postponed_split_flags = cmdmod.split;
8164 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8165 postponed_split_flags = 0;
8166}
8167#endif
8168
8169/*
8170 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8171 */
8172 static void
8173ex_tag(eap)
8174 exarg_T *eap;
8175{
8176 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8177}
8178
8179 static void
8180ex_tag_cmd(eap, name)
8181 exarg_T *eap;
8182 char_u *name;
8183{
8184 int cmd;
8185
8186 switch (name[1])
8187 {
8188 case 'j': cmd = DT_JUMP; /* ":tjump" */
8189 break;
8190 case 's': cmd = DT_SELECT; /* ":tselect" */
8191 break;
8192 case 'p': cmd = DT_PREV; /* ":tprevious" */
8193 break;
8194 case 'N': cmd = DT_PREV; /* ":tNext" */
8195 break;
8196 case 'n': cmd = DT_NEXT; /* ":tnext" */
8197 break;
8198 case 'o': cmd = DT_POP; /* ":pop" */
8199 break;
8200 case 'f': /* ":tfirst" */
8201 case 'r': cmd = DT_FIRST; /* ":trewind" */
8202 break;
8203 case 'l': cmd = DT_LAST; /* ":tlast" */
8204 break;
8205 default: /* ":tag" */
8206#ifdef FEAT_CSCOPE
8207 if (p_cst)
8208 {
8209 do_cstag(eap);
8210 return;
8211 }
8212#endif
8213 cmd = DT_TAG;
8214 break;
8215 }
8216
8217 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8218 eap->forceit, TRUE);
8219}
8220
8221/*
8222 * Evaluate cmdline variables.
8223 *
8224 * change '%' to curbuf->b_ffname
8225 * '#' to curwin->w_altfile
8226 * '<cword>' to word under the cursor
8227 * '<cWORD>' to WORD under the cursor
8228 * '<cfile>' to path name under the cursor
8229 * '<sfile>' to sourced file name
8230 * '<afile>' to file name for autocommand
8231 * '<abuf>' to buffer number for autocommand
8232 * '<amatch>' to matching name for autocommand
8233 *
8234 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8235 * "" for error without a message) and NULL is returned.
8236 * Returns an allocated string if a valid match was found.
8237 * Returns NULL if no match was found. "usedlen" then still contains the
8238 * number of characters to skip.
8239 */
8240 char_u *
8241eval_vars(src, usedlen, lnump, errormsg, srcstart)
8242 char_u *src; /* pointer into commandline */
8243 int *usedlen; /* characters after src that are used */
8244 linenr_T *lnump; /* line number for :e command, or NULL */
8245 char_u **errormsg; /* pointer to error message */
8246 char_u *srcstart; /* beginning of valid memory for src */
8247{
8248 int i;
8249 char_u *s;
8250 char_u *result;
8251 char_u *resultbuf = NULL;
8252 int resultlen;
8253 buf_T *buf;
8254 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8255 int spec_idx;
8256#ifdef FEAT_MODIFY_FNAME
8257 int skip_mod = FALSE;
8258#endif
8259 static char *(spec_str[]) =
8260 {
8261 "%",
8262#define SPEC_PERC 0
8263 "#",
8264#define SPEC_HASH 1
8265 "<cword>", /* cursor word */
8266#define SPEC_CWORD 2
8267 "<cWORD>", /* cursor WORD */
8268#define SPEC_CCWORD 3
8269 "<cfile>", /* cursor path name */
8270#define SPEC_CFILE 4
8271 "<sfile>", /* ":so" file name */
8272#define SPEC_SFILE 5
8273#ifdef FEAT_AUTOCMD
8274 "<afile>", /* autocommand file name */
8275# define SPEC_AFILE 6
8276 "<abuf>", /* autocommand buffer number */
8277# define SPEC_ABUF 7
8278 "<amatch>", /* autocommand match name */
8279# define SPEC_AMATCH 8
8280#endif
8281#ifdef FEAT_CLIENTSERVER
8282 "<client>"
8283# define SPEC_CLIENT 9
8284#endif
8285 };
8286#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
8287
8288#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
8289 char_u strbuf[30];
8290#endif
8291
8292 *errormsg = NULL;
8293
8294 /*
8295 * Check if there is something to do.
8296 */
8297 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
8298 {
8299 *usedlen = (int)STRLEN(spec_str[spec_idx]);
8300 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
8301 break;
8302 }
8303 if (spec_idx == SPEC_COUNT) /* no match */
8304 {
8305 *usedlen = 1;
8306 return NULL;
8307 }
8308
8309 /*
8310 * Skip when preceded with a backslash "\%" and "\#".
8311 * Note: In "\\%" the % is also not recognized!
8312 */
8313 if (src > srcstart && src[-1] == '\\')
8314 {
8315 *usedlen = 0;
8316 STRCPY(src - 1, src); /* remove backslash */
8317 return NULL;
8318 }
8319
8320 /*
8321 * word or WORD under cursor
8322 */
8323 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
8324 {
8325 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
8326 (FIND_IDENT|FIND_STRING) : FIND_STRING);
8327 if (resultlen == 0)
8328 {
8329 *errormsg = (char_u *)"";
8330 return NULL;
8331 }
8332 }
8333
8334 /*
8335 * '#': Alternate file name
8336 * '%': Current file name
8337 * File name under the cursor
8338 * File name for autocommand
8339 * and following modifiers
8340 */
8341 else
8342 {
8343 switch (spec_idx)
8344 {
8345 case SPEC_PERC: /* '%': current file */
8346 if (curbuf->b_fname == NULL)
8347 {
8348 result = (char_u *)"";
8349 valid = 0; /* Must have ":p:h" to be valid */
8350 }
8351 else
8352#ifdef RISCOS
8353 /* Always use the full path for RISC OS if possible. */
8354 result = curbuf->b_ffname;
8355 if (result == NULL)
8356 result = curbuf->b_fname;
8357#else
8358 result = curbuf->b_fname;
8359#endif
8360 break;
8361
8362 case SPEC_HASH: /* '#' or "#99": alternate file */
8363 if (src[1] == '#') /* "##": the argument list */
8364 {
8365 result = arg_all();
8366 resultbuf = result;
8367 *usedlen = 2;
8368#ifdef FEAT_MODIFY_FNAME
8369 skip_mod = TRUE;
8370#endif
8371 break;
8372 }
8373 s = src + 1;
8374 i = (int)getdigits(&s);
8375 *usedlen = (int)(s - src); /* length of what we expand */
8376
8377 buf = buflist_findnr(i);
8378 if (buf == NULL)
8379 {
8380 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
8381 return NULL;
8382 }
8383 if (lnump != NULL)
8384 *lnump = ECMD_LAST;
8385 if (buf->b_fname == NULL)
8386 {
8387 result = (char_u *)"";
8388 valid = 0; /* Must have ":p:h" to be valid */
8389 }
8390 else
8391 result = buf->b_fname;
8392 break;
8393
8394#ifdef FEAT_SEARCHPATH
8395 case SPEC_CFILE: /* file name under cursor */
8396 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
8397 if (result == NULL)
8398 {
8399 *errormsg = (char_u *)"";
8400 return NULL;
8401 }
8402 resultbuf = result; /* remember allocated string */
8403 break;
8404#endif
8405
8406#ifdef FEAT_AUTOCMD
8407 case SPEC_AFILE: /* file name for autocommand */
8408 result = autocmd_fname;
8409 if (result == NULL)
8410 {
8411 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
8412 return NULL;
8413 }
8414 break;
8415
8416 case SPEC_ABUF: /* buffer number for autocommand */
8417 if (autocmd_bufnr <= 0)
8418 {
8419 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
8420 return NULL;
8421 }
8422 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8423 result = strbuf;
8424 break;
8425
8426 case SPEC_AMATCH: /* match name for autocommand */
8427 result = autocmd_match;
8428 if (result == NULL)
8429 {
8430 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
8431 return NULL;
8432 }
8433 break;
8434
8435#endif
8436 case SPEC_SFILE: /* file name for ":so" command */
8437 result = sourcing_name;
8438 if (result == NULL)
8439 {
8440 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
8441 return NULL;
8442 }
8443 break;
8444#if defined(FEAT_CLIENTSERVER)
8445 case SPEC_CLIENT: /* Source of last submitted input */
8446 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
8447 result = strbuf;
8448 break;
8449#endif
8450 }
8451
8452 resultlen = (int)STRLEN(result); /* length of new string */
8453 if (src[*usedlen] == '<') /* remove the file name extension */
8454 {
8455 ++*usedlen;
8456#ifdef RISCOS
8457 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
8458#else
8459 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
8460#endif
8461 resultlen = (int)(s - result);
8462 }
8463#ifdef FEAT_MODIFY_FNAME
8464 else if (!skip_mod)
8465 {
8466 valid |= modify_fname(src, usedlen, &result, &resultbuf,
8467 &resultlen);
8468 if (result == NULL)
8469 {
8470 *errormsg = (char_u *)"";
8471 return NULL;
8472 }
8473 }
8474#endif
8475 }
8476
8477 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8478 {
8479 if (valid != VALID_HEAD + VALID_PATH)
8480 /* xgettext:no-c-format */
8481 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
8482 else
8483 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
8484 result = NULL;
8485 }
8486 else
8487 result = vim_strnsave(result, resultlen);
8488 vim_free(resultbuf);
8489 return result;
8490}
8491
8492/*
8493 * Concatenate all files in the argument list, separated by spaces, and return
8494 * it in one allocated string.
8495 * Spaces and backslashes in the file names are escaped with a backslash.
8496 * Returns NULL when out of memory.
8497 */
8498 static char_u *
8499arg_all()
8500{
8501 int len;
8502 int idx;
8503 char_u *retval = NULL;
8504 char_u *p;
8505
8506 /*
8507 * Do this loop two times:
8508 * first time: compute the total length
8509 * second time: concatenate the names
8510 */
8511 for (;;)
8512 {
8513 len = 0;
8514 for (idx = 0; idx < ARGCOUNT; ++idx)
8515 {
8516 p = alist_name(&ARGLIST[idx]);
8517 if (p != NULL)
8518 {
8519 if (len > 0)
8520 {
8521 /* insert a space in between names */
8522 if (retval != NULL)
8523 retval[len] = ' ';
8524 ++len;
8525 }
8526 for ( ; *p != NUL; ++p)
8527 {
8528 if (*p == ' ' || *p == '\\')
8529 {
8530 /* insert a backslash */
8531 if (retval != NULL)
8532 retval[len] = '\\';
8533 ++len;
8534 }
8535 if (retval != NULL)
8536 retval[len] = *p;
8537 ++len;
8538 }
8539 }
8540 }
8541
8542 /* second time: break here */
8543 if (retval != NULL)
8544 {
8545 retval[len] = NUL;
8546 break;
8547 }
8548
8549 /* allocate memory */
8550 retval = alloc(len + 1);
8551 if (retval == NULL)
8552 break;
8553 }
8554
8555 return retval;
8556}
8557
8558#if defined(FEAT_AUTOCMD) || defined(PROTO)
8559/*
8560 * Expand the <sfile> string in "arg".
8561 *
8562 * Returns an allocated string, or NULL for any error.
8563 */
8564 char_u *
8565expand_sfile(arg)
8566 char_u *arg;
8567{
8568 char_u *errormsg;
8569 int len;
8570 char_u *result;
8571 char_u *newres;
8572 char_u *repl;
8573 int srclen;
8574 char_u *p;
8575
8576 result = vim_strsave(arg);
8577 if (result == NULL)
8578 return NULL;
8579
8580 for (p = result; *p; )
8581 {
8582 if (STRNCMP(p, "<sfile>", 7) != 0)
8583 ++p;
8584 else
8585 {
8586 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
8587 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
8588 if (errormsg != NULL)
8589 {
8590 if (*errormsg)
8591 emsg(errormsg);
8592 vim_free(result);
8593 return NULL;
8594 }
8595 if (repl == NULL) /* no match (cannot happen) */
8596 {
8597 p += srclen;
8598 continue;
8599 }
8600 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8601 newres = alloc(len);
8602 if (newres == NULL)
8603 {
8604 vim_free(repl);
8605 vim_free(result);
8606 return NULL;
8607 }
8608 mch_memmove(newres, result, (size_t)(p - result));
8609 STRCPY(newres + (p - result), repl);
8610 len = (int)STRLEN(newres);
8611 STRCAT(newres, p + srclen);
8612 vim_free(repl);
8613 vim_free(result);
8614 result = newres;
8615 p = newres + len; /* continue after the match */
8616 }
8617 }
8618
8619 return result;
8620}
8621#endif
8622
8623#ifdef FEAT_SESSION
8624static int ses_winsizes __ARGS((FILE *fd, int restore_size));
8625static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
8626static frame_T *ses_skipframe __ARGS((frame_T *fr));
8627static int ses_do_frame __ARGS((frame_T *fr));
8628static int ses_do_win __ARGS((win_T *wp));
8629static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
8630static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
8631static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
8632
8633/*
8634 * Write openfile commands for the current buffers to an .exrc file.
8635 * Return FAIL on error, OK otherwise.
8636 */
8637 static int
8638makeopens(fd, dirnow)
8639 FILE *fd;
8640 char_u *dirnow; /* Current directory name */
8641{
8642 buf_T *buf;
8643 int only_save_windows = TRUE;
8644 int nr;
8645 int cnr = 1;
8646 int restore_size = TRUE;
8647 win_T *wp;
8648 char_u *sname;
8649 win_T *edited_win = NULL;
8650
8651 if (ssop_flags & SSOP_BUFFERS)
8652 only_save_windows = FALSE; /* Save ALL buffers */
8653
8654 /*
8655 * Begin by setting the this_session variable, and then other
8656 * sessionable variables.
8657 */
8658#ifdef FEAT_EVAL
8659 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
8660 return FAIL;
8661 if (ssop_flags & SSOP_GLOBALS)
8662 if (store_session_globals(fd) == FAIL)
8663 return FAIL;
8664#endif
8665
8666 /*
8667 * Close all windows but one.
8668 */
8669 if (put_line(fd, "silent only") == FAIL)
8670 return FAIL;
8671
8672 /*
8673 * Now a :cd command to the session directory or the current directory
8674 */
8675 if (ssop_flags & SSOP_SESDIR)
8676 {
8677 if (put_line(fd, "exe \"cd \" . expand(\"<sfile>:p:h\")") == FAIL)
8678 return FAIL;
8679 }
8680 else if (ssop_flags & SSOP_CURDIR)
8681 {
8682 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
8683 if (sname == NULL
8684 || fprintf(fd, "cd %s", sname) < 0 || put_eol(fd) == FAIL)
8685 return FAIL;
8686 vim_free(sname);
8687 }
8688
8689 /*
8690 * Now save the current files, current buffer first.
8691 */
8692 if (put_line(fd, "set shortmess=aoO") == FAIL)
8693 return FAIL;
8694
8695 /* Now put the other buffers into the buffer list */
8696 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8697 {
8698 if (!(only_save_windows && buf->b_nwindows == 0)
8699 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
8700 && buf->b_fname != NULL
8701 && buf->b_p_bl)
8702 {
8703 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
8704 : buf->b_wininfo->wi_fpos.lnum) < 0
8705 || ses_fname(fd, buf, &ssop_flags) == FAIL)
8706 return FAIL;
8707 }
8708 }
8709
8710 /* the global argument list */
8711 if (ses_arglist(fd, "args", &global_alist.al_ga,
8712 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
8713 return FAIL;
8714
8715 if (ssop_flags & SSOP_RESIZE)
8716 {
8717 /* Note: after the restore we still check it worked!*/
8718 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
8719 || put_eol(fd) == FAIL)
8720 return FAIL;
8721 }
8722
8723#ifdef FEAT_GUI
8724 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
8725 {
8726 int x, y;
8727
8728 if (gui_mch_get_winpos(&x, &y) == OK)
8729 {
8730 /* Note: after the restore we still check it worked!*/
8731 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
8732 return FAIL;
8733 }
8734 }
8735#endif
8736
8737 /*
8738 * Before creating the window layout, try loading one file. If this is
8739 * aborted we don't end up with a number of useless windows.
8740 * This may have side effects! (e.g., compressed or network file).
8741 */
8742 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8743 {
8744 if (ses_do_win(wp)
8745 && wp->w_buffer->b_ffname != NULL
8746 && !wp->w_buffer->b_help
8747#ifdef FEAT_QUICKFIX
8748 && !bt_nofile(wp->w_buffer)
8749#endif
8750 )
8751 {
8752 if (fputs("edit ", fd) < 0
8753 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
8754 return FAIL;
8755 if (!wp->w_arg_idx_invalid)
8756 edited_win = wp;
8757 break;
8758 }
8759 }
8760
8761 /*
8762 * Save current window layout.
8763 */
8764 if (put_line(fd, "set splitbelow splitright") == FAIL)
8765 return FAIL;
8766 if (ses_win_rec(fd, topframe) == FAIL)
8767 return FAIL;
8768 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
8769 return FAIL;
8770 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
8771 return FAIL;
8772
8773 /*
8774 * Check if window sizes can be restored (no windows omitted).
8775 * Remember the window number of the current window after restoring.
8776 */
8777 nr = 0;
8778 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
8779 {
8780 if (ses_do_win(wp))
8781 ++nr;
8782 else
8783 restore_size = FALSE;
8784 if (curwin == wp)
8785 cnr = nr;
8786 }
8787
8788 /* Go to the first window. */
8789 if (put_line(fd, "wincmd t") == FAIL)
8790 return FAIL;
8791
8792 /*
8793 * If more than one window, see if sizes can be restored.
8794 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
8795 * resized when moving between windows.
8796 * Do this before restoring the view, so that the topline and the cursor
8797 * can be set. This is done again below.
8798 */
8799 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
8800 return FAIL;
8801 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
8802 return FAIL;
8803
8804 /*
8805 * Restore the view of the window (options, file, cursor, etc.).
8806 */
8807 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8808 {
8809 if (!ses_do_win(wp))
8810 continue;
8811 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
8812 return FAIL;
8813 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
8814 return FAIL;
8815 }
8816
8817 /*
8818 * Restore cursor to the current window if it's not the first one.
8819 */
8820 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
8821 return FAIL;
8822
8823 /*
8824 * Restore window sizes again after jumping around in windows, because the
8825 * current window has a minimum size while others may not.
8826 */
8827 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
8828 return FAIL;
8829
8830 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
8831 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
8832 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
8833 return FAIL;
8834
8835 /*
8836 * Lastly, execute the x.vim file if it exists.
8837 */
8838 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
8839 || put_line(fd, "if file_readable(s:sx)") == FAIL
8840 || put_line(fd, " exe \"source \" . s:sx") == FAIL
8841 || put_line(fd, "endif") == FAIL)
8842 return FAIL;
8843
8844 return OK;
8845}
8846
8847 static int
8848ses_winsizes(fd, restore_size)
8849 FILE *fd;
8850 int restore_size;
8851{
8852 int n = 0;
8853 win_T *wp;
8854
8855 if (restore_size && (ssop_flags & SSOP_WINSIZE))
8856 {
8857 for (wp = firstwin; wp != NULL; wp = wp->w_next)
8858 {
8859 if (!ses_do_win(wp))
8860 continue;
8861 ++n;
8862
8863 /* restore height when not full height */
8864 if (wp->w_height + wp->w_status_height < topframe->fr_height
8865 && (fprintf(fd,
8866 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
8867 n, (long)wp->w_height, Rows / 2, Rows) < 0
8868 || put_eol(fd) == FAIL))
8869 return FAIL;
8870
8871 /* restore width when not full width */
8872 if (wp->w_width < Columns && (fprintf(fd,
8873 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
8874 n, (long)wp->w_width, Columns / 2, Columns) < 0
8875 || put_eol(fd) == FAIL))
8876 return FAIL;
8877 }
8878 }
8879 else
8880 {
8881 /* Just equalise window sizes */
8882 if (put_line(fd, "wincmd =") == FAIL)
8883 return FAIL;
8884 }
8885 return OK;
8886}
8887
8888/*
8889 * Write commands to "fd" to recursively create windows for frame "fr",
8890 * horizontally and vertically split.
8891 * After the commands the last window in the frame is the current window.
8892 * Returns FAIL when writing the commands to "fd" fails.
8893 */
8894 static int
8895ses_win_rec(fd, fr)
8896 FILE *fd;
8897 frame_T *fr;
8898{
8899 frame_T *frc;
8900 int count = 0;
8901
8902 if (fr->fr_layout != FR_LEAF)
8903 {
8904 /* Find first frame that's not skipped and then create a window for
8905 * each following one (first frame is already there). */
8906 frc = ses_skipframe(fr->fr_child);
8907 if (frc != NULL)
8908 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
8909 {
8910 /* Make window as big as possible so that we have lots of room
8911 * to split. */
8912 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
8913 || put_line(fd, fr->fr_layout == FR_COL
8914 ? "split" : "vsplit") == FAIL)
8915 return FAIL;
8916 ++count;
8917 }
8918
8919 /* Go back to the first window. */
8920 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
8921 ? "%dwincmd k" : "%dwincmd h", count) < 0
8922 || put_eol(fd) == FAIL))
8923 return FAIL;
8924
8925 /* Recursively create frames/windows in each window of this column or
8926 * row. */
8927 frc = ses_skipframe(fr->fr_child);
8928 while (frc != NULL)
8929 {
8930 ses_win_rec(fd, frc);
8931 frc = ses_skipframe(frc->fr_next);
8932 /* Go to next window. */
8933 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
8934 return FAIL;
8935 }
8936 }
8937 return OK;
8938}
8939
8940/*
8941 * Skip frames that don't contain windows we want to save in the Session.
8942 * Returns NULL when there none.
8943 */
8944 static frame_T *
8945ses_skipframe(fr)
8946 frame_T *fr;
8947{
8948 frame_T *frc;
8949
8950 for (frc = fr; frc != NULL; frc = frc->fr_next)
8951 if (ses_do_frame(frc))
8952 break;
8953 return frc;
8954}
8955
8956/*
8957 * Return TRUE if frame "fr" has a window somewhere that we want to save in
8958 * the Session.
8959 */
8960 static int
8961ses_do_frame(fr)
8962 frame_T *fr;
8963{
8964 frame_T *frc;
8965
8966 if (fr->fr_layout == FR_LEAF)
8967 return ses_do_win(fr->fr_win);
8968 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
8969 if (ses_do_frame(frc))
8970 return TRUE;
8971 return FALSE;
8972}
8973
8974/*
8975 * Return non-zero if window "wp" is to be stored in the Session.
8976 */
8977 static int
8978ses_do_win(wp)
8979 win_T *wp;
8980{
8981 if (wp->w_buffer->b_fname == NULL
8982#ifdef FEAT_QUICKFIX
8983 /* When 'buftype' is "nofile" can't restore the window contents. */
8984 || bt_nofile(wp->w_buffer)
8985#endif
8986 )
8987 return (ssop_flags & SSOP_BLANK);
8988 if (wp->w_buffer->b_help)
8989 return (ssop_flags & SSOP_HELP);
8990 return TRUE;
8991}
8992
8993/*
8994 * Write commands to "fd" to restore the view of a window.
8995 * Caller must make sure 'scrolloff' is zero.
8996 */
8997 static int
8998put_view(fd, wp, add_edit, flagp)
8999 FILE *fd;
9000 win_T *wp;
9001 int add_edit; /* add ":edit" command to view */
9002 unsigned *flagp; /* vop_flags or ssop_flags */
9003{
9004 win_T *save_curwin;
9005 int f;
9006 int do_cursor;
9007
9008 /* Always restore cursor position for ":mksession". For ":mkview" only
9009 * when 'viewoptions' contains "cursor". */
9010 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9011
9012 /*
9013 * Local argument list.
9014 */
9015 if (wp->w_alist == &global_alist)
9016 {
9017 if (put_line(fd, "argglobal") == FAIL)
9018 return FAIL;
9019 }
9020 else
9021 {
9022 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9023 flagp == &vop_flags
9024 || !(*flagp & SSOP_CURDIR)
9025 || wp->w_localdir != NULL, flagp) == FAIL)
9026 return FAIL;
9027 }
9028
9029 /* Only when part of a session: restore the argument index. */
9030 if (wp->w_arg_idx != 0 && flagp == &ssop_flags)
9031 {
9032 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9033 || put_eol(fd) == FAIL)
9034 return FAIL;
9035 }
9036
9037 /* Edit the file. Skip this when ":next" already did it. */
9038 if (add_edit && (wp->w_arg_idx == 0 || flagp != &ssop_flags
9039 || wp->w_arg_idx_invalid))
9040 {
9041 /*
9042 * Load the file.
9043 */
9044 if (wp->w_buffer->b_ffname != NULL
9045#ifdef FEAT_QUICKFIX
9046 && !bt_nofile(wp->w_buffer)
9047#endif
9048 )
9049 {
9050 /*
9051 * Editing a file in this buffer: use ":edit file".
9052 * This may have side effects! (e.g., compressed or network file).
9053 */
9054 if (fputs("edit ", fd) < 0
9055 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9056 return FAIL;
9057 }
9058 else
9059 {
9060 /* No file in this buffer, just make it empty. */
9061 if (put_line(fd, "enew") == FAIL)
9062 return FAIL;
9063#ifdef FEAT_QUICKFIX
9064 if (wp->w_buffer->b_ffname != NULL)
9065 {
9066 /* The buffer does have a name, but it's not a file name. */
9067 if (fputs("file ", fd) < 0
9068 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9069 return FAIL;
9070 }
9071#endif
9072 do_cursor = FALSE;
9073 }
9074 }
9075
9076 /*
9077 * Local mappings and abbreviations.
9078 */
9079 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9080 && makemap(fd, wp->w_buffer) == FAIL)
9081 return FAIL;
9082
9083 /*
9084 * Local options. Need to go to the window temporarily.
9085 * Store only local values when using ":mkview" and when ":mksession" is
9086 * used and 'sessionoptions' doesn't include "options".
9087 * Some folding options are always stored when "folds" is included,
9088 * otherwise the folds would not be restored correctly.
9089 */
9090 save_curwin = curwin;
9091 curwin = wp;
9092 curbuf = curwin->w_buffer;
9093 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9094 f = makeset(fd, OPT_LOCAL,
9095 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9096#ifdef FEAT_FOLDING
9097 else if (*flagp & SSOP_FOLDS)
9098 f = makefoldset(fd);
9099#endif
9100 else
9101 f = OK;
9102 curwin = save_curwin;
9103 curbuf = curwin->w_buffer;
9104 if (f == FAIL)
9105 return FAIL;
9106
9107#ifdef FEAT_FOLDING
9108 /*
9109 * Save Folds when 'buftype' is empty and for help files.
9110 */
9111 if ((*flagp & SSOP_FOLDS)
9112 && wp->w_buffer->b_ffname != NULL
9113 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help))
9114 {
9115 if (put_folds(fd, wp) == FAIL)
9116 return FAIL;
9117 }
9118#endif
9119
9120 /*
9121 * Set the cursor after creating folds, since that moves the cursor.
9122 */
9123 if (do_cursor)
9124 {
9125
9126 /* Restore the cursor line in the file and relatively in the
9127 * window. Don't use "G", it changes the jumplist. */
9128 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9129 (long)wp->w_cursor.lnum,
9130 (long)(wp->w_cursor.lnum - wp->w_topline),
9131 (long)wp->w_height / 2, (long)wp->w_height) < 0
9132 || put_eol(fd) == FAIL
9133 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9134 || put_line(fd, "exe s:l") == FAIL
9135 || put_line(fd, "normal! zt") == FAIL
9136 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
9137 || put_eol(fd) == FAIL)
9138 return FAIL;
9139 /* Restore the cursor column and left offset when not wrapping. */
9140 if (wp->w_cursor.col == 0)
9141 {
9142 if (put_line(fd, "normal! 0") == FAIL)
9143 return FAIL;
9144 }
9145 else
9146 {
9147 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
9148 {
9149 if (fprintf(fd,
9150 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
9151 (long)wp->w_cursor.col,
9152 (long)(wp->w_cursor.col - wp->w_leftcol),
9153 (long)wp->w_width / 2, (long)wp->w_width) < 0
9154 || put_eol(fd) == FAIL
9155 || put_line(fd, "if s:c > 0") == FAIL
9156 || fprintf(fd,
9157 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
9158 (long)wp->w_cursor.col) < 0
9159 || put_eol(fd) == FAIL
9160 || put_line(fd, "else") == FAIL
9161 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
9162 || put_eol(fd) == FAIL
9163 || put_line(fd, "endif") == FAIL)
9164 return FAIL;
9165 }
9166 else
9167 {
9168 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
9169 || put_eol(fd) == FAIL)
9170 return FAIL;
9171 }
9172 }
9173 }
9174
9175 /*
9176 * Local directory.
9177 */
9178 if (wp->w_localdir != NULL)
9179 {
9180 if (fputs("lcd ", fd) < 0
9181 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
9182 || put_eol(fd) == FAIL)
9183 return FAIL;
9184 }
9185
9186 return OK;
9187}
9188
9189/*
9190 * Write an argument list to the session file.
9191 * Returns FAIL if writing fails.
9192 */
9193 static int
9194ses_arglist(fd, cmd, gap, fullname, flagp)
9195 FILE *fd;
9196 char *cmd;
9197 garray_T *gap;
9198 int fullname; /* TRUE: use full path name */
9199 unsigned *flagp;
9200{
9201 int i;
9202 char_u buf[MAXPATHL];
9203 char_u *s;
9204
9205 if (gap->ga_len == 0)
9206 return put_line(fd, "silent! argdel *");
9207 if (fputs(cmd, fd) < 0)
9208 return FAIL;
9209 for (i = 0; i < gap->ga_len; ++i)
9210 {
9211 /* NULL file names are skipped (only happens when out of memory). */
9212 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
9213 if (s != NULL)
9214 {
9215 if (fullname)
9216 {
9217 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
9218 s = buf;
9219 }
9220 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
9221 return FAIL;
9222 }
9223 }
9224 return put_eol(fd);
9225}
9226
9227/*
9228 * Write a buffer name to the session file.
9229 * Also ends the line.
9230 * Returns FAIL if writing fails.
9231 */
9232 static int
9233ses_fname(fd, buf, flagp)
9234 FILE *fd;
9235 buf_T *buf;
9236 unsigned *flagp;
9237{
9238 char_u *name;
9239
9240 /* Use the short file name if the current directory is known at the time
9241 * the session file will be sourced. Don't do this for ":mkview", we
9242 * don't know the current directory. */
9243 if (buf->b_sfname != NULL
9244 && flagp == &ssop_flags
9245 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
9246 name = buf->b_sfname;
9247 else
9248 name = buf->b_ffname;
9249 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
9250 return FAIL;
9251 return OK;
9252}
9253
9254/*
9255 * Write a file name to the session file.
9256 * Takes care of the "slash" option in 'sessionoptions' and escapes special
9257 * characters.
9258 * Returns FAIL if writing fails.
9259 */
9260 static int
9261ses_put_fname(fd, name, flagp)
9262 FILE *fd;
9263 char_u *name;
9264 unsigned *flagp;
9265{
9266 char_u *sname;
9267 int retval = OK;
9268 int c;
9269
9270 sname = home_replace_save(NULL, name);
9271 if (sname != NULL)
9272 name = sname;
9273 while (*name != NUL)
9274 {
9275#ifdef FEAT_MBYTE
9276 {
9277 int l;
9278
9279 if (has_mbyte && (l = (*mb_ptr2len_check)(name)) > 1)
9280 {
9281 /* copy a multibyte char */
9282 while (--l >= 0)
9283 {
9284 if (putc(*name, fd) != *name)
9285 retval = FAIL;
9286 ++name;
9287 }
9288 continue;
9289 }
9290 }
9291#endif
9292 c = *name++;
9293 if (c == '\\' && (*flagp & SSOP_SLASH))
9294 /* change a backslash to a forward slash */
9295 c = '/';
9296 else if ((vim_strchr(escape_chars, c) != NULL
9297#ifdef BACKSLASH_IN_FILENAME
9298 && c != '\\'
9299#endif
9300 ) || c == '#' || c == '%')
9301 {
9302 /* escape a special character with a backslash */
9303 if (putc('\\', fd) != '\\')
9304 retval = FAIL;
9305 }
9306 if (putc(c, fd) != c)
9307 retval = FAIL;
9308 }
9309 vim_free(sname);
9310 return retval;
9311}
9312
9313/*
9314 * ":loadview [nr]"
9315 */
9316 static void
9317ex_loadview(eap)
9318 exarg_T *eap;
9319{
9320 char_u *fname;
9321
9322 fname = get_view_file(*eap->arg);
9323 if (fname != NULL)
9324 {
9325 do_source(fname, FALSE, FALSE);
9326 vim_free(fname);
9327 }
9328}
9329
9330/*
9331 * Get the name of the view file for the current buffer.
9332 */
9333 static char_u *
9334get_view_file(c)
9335 int c;
9336{
9337 int len = 0;
9338 char_u *p, *s;
9339 char_u *retval;
9340 char_u *sname;
9341
9342 if (curbuf->b_ffname == NULL)
9343 {
9344 EMSG(_(e_noname));
9345 return NULL;
9346 }
9347 sname = home_replace_save(NULL, curbuf->b_ffname);
9348 if (sname == NULL)
9349 return NULL;
9350
9351 /*
9352 * We want a file name without separators, because we're not going to make
9353 * a directory.
9354 * "normal" path separator -> "=+"
9355 * "=" -> "=="
9356 * ":" path separator -> "=-"
9357 */
9358 for (p = sname; *p; ++p)
9359 if (*p == '=' || vim_ispathsep(*p))
9360 ++len;
9361 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
9362 if (retval != NULL)
9363 {
9364 STRCPY(retval, p_vdir);
9365 add_pathsep(retval);
9366 s = retval + STRLEN(retval);
9367 for (p = sname; *p; ++p)
9368 {
9369 if (*p == '=')
9370 {
9371 *s++ = '=';
9372 *s++ = '=';
9373 }
9374 else if (vim_ispathsep(*p))
9375 {
9376 *s++ = '=';
9377#ifdef MACOS_CLASSIC /* TODO: Is it also needed for MACOS_X? (Dany) */
9378 *s++ = '+';
9379#else
9380# if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
9381 || defined(VMS)
9382 if (*p == ':')
9383 *s++ = '-';
9384 else
9385# endif
9386 *s++ = '+';
9387#endif
9388 }
9389 else
9390 *s++ = *p;
9391 }
9392 *s++ = '=';
9393 *s++ = c;
9394 STRCPY(s, ".vim");
9395 }
9396
9397 vim_free(sname);
9398 return retval;
9399}
9400
9401#endif /* FEAT_SESSION */
9402
9403/*
9404 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
9405 * Return FAIL for a write error.
9406 */
9407 int
9408put_eol(fd)
9409 FILE *fd;
9410{
9411 if (
9412#ifdef USE_CRNL
9413 (
9414# ifdef MKSESSION_NL
9415 !mksession_nl &&
9416# endif
9417 (putc('\r', fd) < 0)) ||
9418#endif
9419 (putc('\n', fd) < 0))
9420 return FAIL;
9421 return OK;
9422}
9423
9424/*
9425 * Write a line to "fd".
9426 * Return FAIL for a write error.
9427 */
9428 int
9429put_line(fd, s)
9430 FILE *fd;
9431 char *s;
9432{
9433 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
9434 return FAIL;
9435 return OK;
9436}
9437
9438#ifdef FEAT_VIMINFO
9439/*
9440 * ":rviminfo" and ":wviminfo".
9441 */
9442 static void
9443ex_viminfo(eap)
9444 exarg_T *eap;
9445{
9446 char_u *save_viminfo;
9447
9448 save_viminfo = p_viminfo;
9449 if (*p_viminfo == NUL)
9450 p_viminfo = (char_u *)"'100";
9451 if (eap->cmdidx == CMD_rviminfo)
9452 {
9453 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
9454 EMSG(_("E195: Cannot open viminfo file for reading"));
9455 }
9456 else
9457 write_viminfo(eap->arg, eap->forceit);
9458 p_viminfo = save_viminfo;
9459}
9460#endif
9461
9462#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
9463 void
9464dialog_msg(buff, format, fname)
9465 char_u *buff;
9466 char *format;
9467 char_u *fname;
9468{
9469 int len;
9470
9471 if (fname == NULL)
9472 fname = (char_u *)_("Untitled");
9473 len = (int)STRLEN(format) + (int)STRLEN(fname);
9474 if (len >= IOSIZE)
9475 sprintf((char *)buff, format, (int)(IOSIZE - STRLEN(format)), fname);
9476 else
9477 sprintf((char *)buff, format, (int)STRLEN(fname), fname);
9478}
9479#endif
9480
9481/*
9482 * ":behave {mswin,xterm}"
9483 */
9484 static void
9485ex_behave(eap)
9486 exarg_T *eap;
9487{
9488 if (STRCMP(eap->arg, "mswin") == 0)
9489 {
9490 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9491 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9492 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9493 set_option_value((char_u *)"keymodel", 0L,
9494 (char_u *)"startsel,stopsel", 0);
9495 }
9496 else if (STRCMP(eap->arg, "xterm") == 0)
9497 {
9498 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9499 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9500 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9501 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9502 }
9503 else
9504 EMSG2(_(e_invarg2), eap->arg);
9505}
9506
9507#ifdef FEAT_AUTOCMD
9508static int filetype_detect = FALSE;
9509static int filetype_plugin = FALSE;
9510static int filetype_indent = FALSE;
9511
9512/*
9513 * ":filetype [plugin] [indent] {on,off,detect}"
9514 * on: Load the filetype.vim file to install autocommands for file types.
9515 * off: Load the ftoff.vim file to remove all autocommands for file types.
9516 * plugin on: load filetype.vim and ftplugin.vim
9517 * plugin off: load ftplugof.vim
9518 * indent on: load filetype.vim and indent.vim
9519 * indent off: load indoff.vim
9520 */
9521 static void
9522ex_filetype(eap)
9523 exarg_T *eap;
9524{
9525 char_u *arg = eap->arg;
9526 int plugin = FALSE;
9527 int indent = FALSE;
9528
9529 if (*eap->arg == NUL)
9530 {
9531 /* Print current status. */
9532 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
9533 filetype_detect ? "ON" : "OFF",
9534 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9535 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9536 return;
9537 }
9538
9539 /* Accept "plugin" and "indent" in any order. */
9540 for (;;)
9541 {
9542 if (STRNCMP(arg, "plugin", 6) == 0)
9543 {
9544 plugin = TRUE;
9545 arg = skipwhite(arg + 6);
9546 continue;
9547 }
9548 if (STRNCMP(arg, "indent", 6) == 0)
9549 {
9550 indent = TRUE;
9551 arg = skipwhite(arg + 6);
9552 continue;
9553 }
9554 break;
9555 }
9556 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9557 {
9558 if (*arg == 'o' || !filetype_detect)
9559 {
9560 cmd_runtime((char_u *)FILETYPE_FILE, TRUE);
9561 filetype_detect = TRUE;
9562 if (plugin)
9563 {
9564 cmd_runtime((char_u *)FTPLUGIN_FILE, TRUE);
9565 filetype_plugin = TRUE;
9566 }
9567 if (indent)
9568 {
9569 cmd_runtime((char_u *)INDENT_FILE, TRUE);
9570 filetype_indent = TRUE;
9571 }
9572 }
9573 if (*arg == 'd')
9574 {
9575 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
9576 do_modelines();
9577 }
9578 }
9579 else if (STRCMP(arg, "off") == 0)
9580 {
9581 if (plugin || indent)
9582 {
9583 if (plugin)
9584 {
9585 cmd_runtime((char_u *)FTPLUGOF_FILE, TRUE);
9586 filetype_plugin = FALSE;
9587 }
9588 if (indent)
9589 {
9590 cmd_runtime((char_u *)INDOFF_FILE, TRUE);
9591 filetype_indent = FALSE;
9592 }
9593 }
9594 else
9595 {
9596 cmd_runtime((char_u *)FTOFF_FILE, TRUE);
9597 filetype_detect = FALSE;
9598 }
9599 }
9600 else
9601 EMSG2(_(e_invarg2), arg);
9602}
9603
9604/*
9605 * ":setfiletype {name}"
9606 */
9607 static void
9608ex_setfiletype(eap)
9609 exarg_T *eap;
9610{
9611 if (!did_filetype)
9612 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
9613}
9614#endif
9615
9616/*ARGSUSED*/
9617 static void
9618ex_digraphs(eap)
9619 exarg_T *eap;
9620{
9621#ifdef FEAT_DIGRAPHS
9622 if (*eap->arg != NUL)
9623 putdigraph(eap->arg);
9624 else
9625 listdigraphs();
9626#else
9627 EMSG(_("E196: No digraphs in this version"));
9628#endif
9629}
9630
9631 static void
9632ex_set(eap)
9633 exarg_T *eap;
9634{
9635 int flags = 0;
9636
9637 if (eap->cmdidx == CMD_setlocal)
9638 flags = OPT_LOCAL;
9639 else if (eap->cmdidx == CMD_setglobal)
9640 flags = OPT_GLOBAL;
9641#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
9642 if (cmdmod.browse && flags == 0)
9643 ex_options(eap);
9644 else
9645#endif
9646 (void)do_set(eap->arg, flags);
9647}
9648
9649#ifdef FEAT_SEARCH_EXTRA
9650/*
9651 * ":nohlsearch"
9652 */
9653/*ARGSUSED*/
9654 static void
9655ex_nohlsearch(eap)
9656 exarg_T *eap;
9657{
9658 no_hlsearch = TRUE;
9659 redraw_all_later(NOT_VALID);
9660}
9661
9662/*
9663 * ":match {group} {pattern}"
9664 * Sets nextcmd to the start of the next command, if any. Also called when
9665 * skipping commands to find the next command.
9666 */
9667 static void
9668ex_match(eap)
9669 exarg_T *eap;
9670{
9671 char_u *p;
9672 char_u *end;
9673 int c;
9674
9675 /* First clear any old pattern. */
9676 if (!eap->skip)
9677 {
9678 vim_free(curwin->w_match.regprog);
9679 curwin->w_match.regprog = NULL;
9680 redraw_later(NOT_VALID); /* always need a redraw */
9681 }
9682
9683 if (ends_excmd(*eap->arg))
9684 end = eap->arg;
9685 else if ((STRNICMP(eap->arg, "none", 4) == 0
9686 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
9687 end = eap->arg + 4;
9688 else
9689 {
9690 p = skiptowhite(eap->arg);
9691 if (!eap->skip)
9692 {
9693 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
9694 if (curwin->w_match_id == 0)
9695 {
9696 EMSG2(_(e_nogroup), eap->arg);
9697 return;
9698 }
9699 }
9700 p = skipwhite(p);
9701 if (*p == NUL)
9702 {
9703 /* There must be two arguments. */
9704 EMSG2(_(e_invarg2), eap->arg);
9705 return;
9706 }
9707 end = skip_regexp(p + 1, *p, TRUE, NULL);
9708 if (!eap->skip)
9709 {
9710 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
9711 {
9712 eap->errmsg = e_trailing;
9713 return;
9714 }
9715
9716 c = *end;
9717 *end = NUL;
9718 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
9719 *end = c;
9720 if (curwin->w_match.regprog == NULL)
9721 {
9722 EMSG2(_(e_invarg2), p);
9723 return;
9724 }
9725 }
9726 }
9727 eap->nextcmd = find_nextcmd(end);
9728}
9729#endif
9730
9731#ifdef FEAT_CRYPT
9732/*
9733 * ":X": Get crypt key
9734 */
9735/*ARGSUSED*/
9736 static void
9737ex_X(eap)
9738 exarg_T *eap;
9739{
9740 (void)get_crypt_key(TRUE, TRUE);
9741}
9742#endif
9743
9744#ifdef FEAT_FOLDING
9745 static void
9746ex_fold(eap)
9747 exarg_T *eap;
9748{
9749 if (foldManualAllowed(TRUE))
9750 foldCreate(eap->line1, eap->line2);
9751}
9752
9753 static void
9754ex_foldopen(eap)
9755 exarg_T *eap;
9756{
9757 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9758 eap->forceit, FALSE);
9759}
9760
9761 static void
9762ex_folddo(eap)
9763 exarg_T *eap;
9764{
9765 linenr_T lnum;
9766
9767 /* First set the marks for all lines closed/open. */
9768 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9769 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9770 ml_setmarked(lnum);
9771
9772 /* Execute the command on the marked lines. */
9773 global_exe(eap->arg);
9774 ml_clearmarked(); /* clear rest of the marks */
9775}
9776#endif