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