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