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