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