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