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