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