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