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