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