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