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