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