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