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