xref: /vim-8.2.3635/src/ex_eval.c (revision bb76f24a)
1 /* vi:set ts=8 sts=4 sw=4 noet:
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_eval.c: functions for Ex command line for the +eval feature.
12  */
13 
14 #include "vim.h"
15 
16 #if defined(FEAT_EVAL) || defined(PROTO)
17 
18 static void	free_msglist(struct msglist *l);
19 static int	throw_exception(void *, int, char_u *);
20 static char_u	*get_end_emsg(struct condstack *cstack);
21 
22 /*
23  * Exception handling terms:
24  *
25  *	:try		":try" command		\
26  *	    ...		try block		|
27  *	:catch RE	":catch" command	|
28  *	    ...		catch clause		|- try conditional
29  *	:finally	":finally" command	|
30  *	    ...		finally clause		|
31  *	:endtry		":endtry" command	/
32  *
33  * The try conditional may have any number of catch clauses and at most one
34  * finally clause.  A ":throw" command can be inside the try block, a catch
35  * clause, the finally clause, or in a function called or script sourced from
36  * there or even outside the try conditional.  Try conditionals may be nested.
37  */
38 
39 /*
40  * Configuration whether an exception is thrown on error or interrupt.  When
41  * the preprocessor macros below evaluate to FALSE, an error (did_emsg) or
42  * interrupt (got_int) under an active try conditional terminates the script
43  * after the non-active finally clauses of all active try conditionals have been
44  * executed.  Otherwise, errors and/or interrupts are converted into catchable
45  * exceptions (did_throw additionally set), which terminate the script only if
46  * not caught.  For user exceptions, only did_throw is set.  (Note: got_int can
47  * be set asynchronously afterwards by a SIGINT, so did_throw && got_int is not
48  * a reliant test that the exception currently being thrown is an interrupt
49  * exception.  Similarly, did_emsg can be set afterwards on an error in an
50  * (unskipped) conditional command inside an inactive conditional, so did_throw
51  * && did_emsg is not a reliant test that the exception currently being thrown
52  * is an error exception.)  -  The macros can be defined as expressions checking
53  * for a variable that is allowed to be changed during execution of a script.
54  */
55 #if 0
56 /* Expressions used for testing during the development phase. */
57 # define THROW_ON_ERROR		(!eval_to_number("$VIMNOERRTHROW"))
58 # define THROW_ON_INTERRUPT	(!eval_to_number("$VIMNOINTTHROW"))
59 # define THROW_TEST
60 #else
61 /* Values used for the Vim release. */
62 # define THROW_ON_ERROR		TRUE
63 # define THROW_ON_ERROR_TRUE
64 # define THROW_ON_INTERRUPT	TRUE
65 # define THROW_ON_INTERRUPT_TRUE
66 #endif
67 
68 static void	catch_exception(except_T *excp);
69 static void	finish_exception(except_T *excp);
70 static void	discard_exception(except_T *excp, int was_finished);
71 static void	report_pending(int action, int pending, void *value);
72 
73 /*
74  * When several errors appear in a row, setting "force_abort" is delayed until
75  * the failing command returned.  "cause_abort" is set to TRUE meanwhile, in
76  * order to indicate that situation.  This is useful when "force_abort" was set
77  * during execution of a function call from an expression: the aborting of the
78  * expression evaluation is done without producing any error messages, but all
79  * error messages on parsing errors during the expression evaluation are given
80  * (even if a try conditional is active).
81  */
82 static int cause_abort = FALSE;
83 
84 /*
85  * Return TRUE when immediately aborting on error, or when an interrupt
86  * occurred or an exception was thrown but not caught.  Use for ":{range}call"
87  * to check whether an aborted function that does not handle a range itself
88  * should be called again for the next line in the range.  Also used for
89  * cancelling expression evaluation after a function call caused an immediate
90  * abort.  Note that the first emsg() call temporarily resets "force_abort"
91  * until the throw point for error messages has been reached.  That is, during
92  * cancellation of an expression evaluation after an aborting function call or
93  * due to a parsing error, aborting() always returns the same value.
94  */
95     int
96 aborting(void)
97 {
98     return (did_emsg && force_abort) || got_int || did_throw;
99 }
100 
101 /*
102  * The value of "force_abort" is temporarily reset by the first emsg() call
103  * during an expression evaluation, and "cause_abort" is used instead.  It might
104  * be necessary to restore "force_abort" even before the throw point for the
105  * error message has been reached.  update_force_abort() should be called then.
106  */
107     void
108 update_force_abort(void)
109 {
110     if (cause_abort)
111 	force_abort = TRUE;
112 }
113 
114 /*
115  * Return TRUE if a command with a subcommand resulting in "retcode" should
116  * abort the script processing.  Can be used to suppress an autocommand after
117  * execution of a failing subcommand as long as the error message has not been
118  * displayed and actually caused the abortion.
119  */
120     int
121 should_abort(int retcode)
122 {
123     return ((retcode == FAIL && trylevel != 0 && !emsg_silent) || aborting());
124 }
125 
126 /*
127  * Return TRUE if a function with the "abort" flag should not be considered
128  * ended on an error.  This means that parsing commands is continued in order
129  * to find finally clauses to be executed, and that some errors in skipped
130  * commands are still reported.
131  */
132     int
133 aborted_in_try(void)
134 {
135     /* This function is only called after an error.  In this case, "force_abort"
136      * determines whether searching for finally clauses is necessary. */
137     return force_abort;
138 }
139 
140 /*
141  * cause_errthrow(): Cause a throw of an error exception if appropriate.
142  * Return TRUE if the error message should not be displayed by emsg().
143  * Sets "ignore", if the emsg() call should be ignored completely.
144  *
145  * When several messages appear in the same command, the first is usually the
146  * most specific one and used as the exception value.  The "severe" flag can be
147  * set to TRUE, if a later but severer message should be used instead.
148  */
149     int
150 cause_errthrow(
151     char_u	*mesg,
152     int		severe,
153     int		*ignore)
154 {
155     struct msglist *elem;
156     struct msglist **plist;
157 
158     /*
159      * Do nothing when displaying the interrupt message or reporting an
160      * uncaught exception (which has already been discarded then) at the top
161      * level.  Also when no exception can be thrown.  The message will be
162      * displayed by emsg().
163      */
164     if (suppress_errthrow)
165 	return FALSE;
166 
167     /*
168      * If emsg() has not been called previously, temporarily reset
169      * "force_abort" until the throw point for error messages has been
170      * reached.  This ensures that aborting() returns the same value for all
171      * errors that appear in the same command.  This means particularly that
172      * for parsing errors during expression evaluation emsg() will be called
173      * multiply, even when the expression is evaluated from a finally clause
174      * that was activated due to an aborting error, interrupt, or exception.
175      */
176     if (!did_emsg)
177     {
178 	cause_abort = force_abort;
179 	force_abort = FALSE;
180     }
181 
182     /*
183      * If no try conditional is active and no exception is being thrown and
184      * there has not been an error in a try conditional or a throw so far, do
185      * nothing (for compatibility of non-EH scripts).  The message will then
186      * be displayed by emsg().  When ":silent!" was used and we are not
187      * currently throwing an exception, do nothing.  The message text will
188      * then be stored to v:errmsg by emsg() without displaying it.
189      */
190     if (((trylevel == 0 && !cause_abort) || emsg_silent) && !did_throw)
191 	return FALSE;
192 
193     /*
194      * Ignore an interrupt message when inside a try conditional or when an
195      * exception is being thrown or when an error in a try conditional or
196      * throw has been detected previously.  This is important in order that an
197      * interrupt exception is catchable by the innermost try conditional and
198      * not replaced by an interrupt message error exception.
199      */
200     if (mesg == (char_u *)_(e_interr))
201     {
202 	*ignore = TRUE;
203 	return TRUE;
204     }
205 
206     /*
207      * Ensure that all commands in nested function calls and sourced files
208      * are aborted immediately.
209      */
210     cause_abort = TRUE;
211 
212     /*
213      * When an exception is being thrown, some commands (like conditionals) are
214      * not skipped.  Errors in those commands may affect what of the subsequent
215      * commands are regarded part of catch and finally clauses.  Catching the
216      * exception would then cause execution of commands not intended by the
217      * user, who wouldn't even get aware of the problem.  Therefor, discard the
218      * exception currently being thrown to prevent it from being caught.  Just
219      * execute finally clauses and terminate.
220      */
221     if (did_throw)
222     {
223 	/* When discarding an interrupt exception, reset got_int to prevent the
224 	 * same interrupt being converted to an exception again and discarding
225 	 * the error exception we are about to throw here. */
226 	if (current_exception->type == ET_INTERRUPT)
227 	    got_int = FALSE;
228 	discard_current_exception();
229     }
230 
231 #ifdef THROW_TEST
232     if (!THROW_ON_ERROR)
233     {
234 	/*
235 	 * Print error message immediately without searching for a matching
236 	 * catch clause; just finally clauses are executed before the script
237 	 * is terminated.
238 	 */
239 	return FALSE;
240     }
241     else
242 #endif
243     {
244 	/*
245 	 * Prepare the throw of an error exception, so that everything will
246 	 * be aborted (except for executing finally clauses), until the error
247 	 * exception is caught; if still uncaught at the top level, the error
248 	 * message will be displayed and the script processing terminated
249 	 * then.  -  This function has no access to the conditional stack.
250 	 * Thus, the actual throw is made after the failing command has
251 	 * returned.  -  Throw only the first of several errors in a row, except
252 	 * a severe error is following.
253 	 */
254 	if (msg_list != NULL)
255 	{
256 	    plist = msg_list;
257 	    while (*plist != NULL)
258 		plist = &(*plist)->next;
259 
260 	    elem = (struct msglist *)alloc((unsigned)sizeof(struct msglist));
261 	    if (elem == NULL)
262 	    {
263 		suppress_errthrow = TRUE;
264 		EMSG(_(e_outofmem));
265 	    }
266 	    else
267 	    {
268 		elem->msg = vim_strsave(mesg);
269 		if (elem->msg == NULL)
270 		{
271 		    vim_free(elem);
272 		    suppress_errthrow = TRUE;
273 		    EMSG(_(e_outofmem));
274 		}
275 		else
276 		{
277 		    elem->next = NULL;
278 		    elem->throw_msg = NULL;
279 		    *plist = elem;
280 		    if (plist == msg_list || severe)
281 		    {
282 			char_u	    *tmsg;
283 
284 			/* Skip the extra "Vim " prefix for message "E458". */
285 			tmsg = elem->msg;
286 			if (STRNCMP(tmsg, "Vim E", 5) == 0
287 				&& VIM_ISDIGIT(tmsg[5])
288 				&& VIM_ISDIGIT(tmsg[6])
289 				&& VIM_ISDIGIT(tmsg[7])
290 				&& tmsg[8] == ':'
291 				&& tmsg[9] == ' ')
292 			    (*msg_list)->throw_msg = &tmsg[4];
293 			else
294 			    (*msg_list)->throw_msg = tmsg;
295 		    }
296 		}
297 	    }
298 	}
299 	return TRUE;
300     }
301 }
302 
303 /*
304  * Free a "msg_list" and the messages it contains.
305  */
306     static void
307 free_msglist(struct msglist *l)
308 {
309     struct msglist  *messages, *next;
310 
311     messages = l;
312     while (messages != NULL)
313     {
314 	next = messages->next;
315 	vim_free(messages->msg);
316 	vim_free(messages);
317 	messages = next;
318     }
319 }
320 
321 /*
322  * Free global "*msg_list" and the messages it contains, then set "*msg_list"
323  * to NULL.
324  */
325     void
326 free_global_msglist(void)
327 {
328     free_msglist(*msg_list);
329     *msg_list = NULL;
330 }
331 
332 /*
333  * Throw the message specified in the call to cause_errthrow() above as an
334  * error exception.  If cstack is NULL, postpone the throw until do_cmdline()
335  * has returned (see do_one_cmd()).
336  */
337     void
338 do_errthrow(struct condstack *cstack, char_u *cmdname)
339 {
340     /*
341      * Ensure that all commands in nested function calls and sourced files
342      * are aborted immediately.
343      */
344     if (cause_abort)
345     {
346 	cause_abort = FALSE;
347 	force_abort = TRUE;
348     }
349 
350     /* If no exception is to be thrown or the conversion should be done after
351      * returning to a previous invocation of do_one_cmd(), do nothing. */
352     if (msg_list == NULL || *msg_list == NULL)
353 	return;
354 
355     if (throw_exception(*msg_list, ET_ERROR, cmdname) == FAIL)
356 	free_msglist(*msg_list);
357     else
358     {
359 	if (cstack != NULL)
360 	    do_throw(cstack);
361 	else
362 	    need_rethrow = TRUE;
363     }
364     *msg_list = NULL;
365 }
366 
367 /*
368  * do_intthrow(): Replace the current exception by an interrupt or interrupt
369  * exception if appropriate.  Return TRUE if the current exception is discarded,
370  * FALSE otherwise.
371  */
372     int
373 do_intthrow(struct condstack *cstack)
374 {
375     /*
376      * If no interrupt occurred or no try conditional is active and no exception
377      * is being thrown, do nothing (for compatibility of non-EH scripts).
378      */
379     if (!got_int || (trylevel == 0 && !did_throw))
380 	return FALSE;
381 
382 #ifdef THROW_TEST	/* avoid warning for condition always true */
383     if (!THROW_ON_INTERRUPT)
384     {
385 	/*
386 	 * The interrupt aborts everything except for executing finally clauses.
387 	 * Discard any user or error or interrupt exception currently being
388 	 * thrown.
389 	 */
390 	if (did_throw)
391 	    discard_current_exception();
392     }
393     else
394 #endif
395     {
396 	/*
397 	 * Throw an interrupt exception, so that everything will be aborted
398 	 * (except for executing finally clauses), until the interrupt exception
399 	 * is caught; if still uncaught at the top level, the script processing
400 	 * will be terminated then.  -  If an interrupt exception is already
401 	 * being thrown, do nothing.
402 	 *
403 	 */
404 	if (did_throw)
405 	{
406 	    if (current_exception->type == ET_INTERRUPT)
407 		return FALSE;
408 
409 	    /* An interrupt exception replaces any user or error exception. */
410 	    discard_current_exception();
411 	}
412 	if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) != FAIL)
413 	    do_throw(cstack);
414     }
415 
416     return TRUE;
417 }
418 
419 /*
420  * Get an exception message that is to be stored in current_exception->value.
421  */
422     char_u *
423 get_exception_string(
424     void	*value,
425     int		type,
426     char_u	*cmdname,
427     int		*should_free)
428 {
429     char_u	*ret, *mesg;
430     int		cmdlen;
431     char_u	*p, *val;
432 
433     if (type == ET_ERROR)
434     {
435 	*should_free = TRUE;
436 	mesg = ((struct msglist *)value)->throw_msg;
437 	if (cmdname != NULL && *cmdname != NUL)
438 	{
439 	    cmdlen = (int)STRLEN(cmdname);
440 	    ret = vim_strnsave((char_u *)"Vim(",
441 					   4 + cmdlen + 2 + (int)STRLEN(mesg));
442 	    if (ret == NULL)
443 		return ret;
444 	    STRCPY(&ret[4], cmdname);
445 	    STRCPY(&ret[4 + cmdlen], "):");
446 	    val = ret + 4 + cmdlen + 2;
447 	}
448 	else
449 	{
450 	    ret = vim_strnsave((char_u *)"Vim:", 4 + (int)STRLEN(mesg));
451 	    if (ret == NULL)
452 		return ret;
453 	    val = ret + 4;
454 	}
455 
456 	/* msg_add_fname may have been used to prefix the message with a file
457 	 * name in quotes.  In the exception value, put the file name in
458 	 * parentheses and move it to the end. */
459 	for (p = mesg; ; p++)
460 	{
461 	    if (*p == NUL
462 		    || (*p == 'E'
463 			&& VIM_ISDIGIT(p[1])
464 			&& (p[2] == ':'
465 			    || (VIM_ISDIGIT(p[2])
466 				&& (p[3] == ':'
467 				    || (VIM_ISDIGIT(p[3])
468 					&& p[4] == ':'))))))
469 	    {
470 		if (*p == NUL || p == mesg)
471 		    STRCAT(val, mesg);  /* 'E123' missing or at beginning */
472 		else
473 		{
474 		    /* '"filename" E123: message text' */
475 		    if (mesg[0] != '"' || p-2 < &mesg[1] ||
476 			    p[-2] != '"' || p[-1] != ' ')
477 			/* "E123:" is part of the file name. */
478 			continue;
479 
480 		    STRCAT(val, p);
481 		    p[-2] = NUL;
482 		    sprintf((char *)(val + STRLEN(p)), " (%s)", &mesg[1]);
483 		    p[-2] = '"';
484 		}
485 		break;
486 	    }
487 	}
488     }
489     else
490     {
491 	*should_free = FALSE;
492 	ret = (char_u *)value;
493     }
494 
495     return ret;
496 }
497 
498 
499 /*
500  * Throw a new exception.  Return FAIL when out of memory or it was tried to
501  * throw an illegal user exception.  "value" is the exception string for a
502  * user or interrupt exception, or points to a message list in case of an
503  * error exception.
504  */
505     static int
506 throw_exception(void *value, int type, char_u *cmdname)
507 {
508     except_T	*excp;
509     int		should_free;
510 
511     /*
512      * Disallow faking Interrupt or error exceptions as user exceptions.  They
513      * would be treated differently from real interrupt or error exceptions
514      * when no active try block is found, see do_cmdline().
515      */
516     if (type == ET_USER)
517     {
518 	if (STRNCMP((char_u *)value, "Vim", 3) == 0
519 		&& (((char_u *)value)[3] == NUL || ((char_u *)value)[3] == ':'
520 		    || ((char_u *)value)[3] == '('))
521 	{
522 	    EMSG(_("E608: Cannot :throw exceptions with 'Vim' prefix"));
523 	    goto fail;
524 	}
525     }
526 
527     excp = (except_T *)alloc((unsigned)sizeof(except_T));
528     if (excp == NULL)
529 	goto nomem;
530 
531     if (type == ET_ERROR)
532 	/* Store the original message and prefix the exception value with
533 	 * "Vim:" or, if a command name is given, "Vim(cmdname):". */
534 	excp->messages = (struct msglist *)value;
535 
536     excp->value = get_exception_string(value, type, cmdname, &should_free);
537     if (excp->value == NULL && should_free)
538 	goto nomem;
539 
540     excp->type = type;
541     excp->throw_name = vim_strsave(sourcing_name == NULL
542 					      ? (char_u *)"" : sourcing_name);
543     if (excp->throw_name == NULL)
544     {
545 	if (should_free)
546 	    vim_free(excp->value);
547 	goto nomem;
548     }
549     excp->throw_lnum = sourcing_lnum;
550 
551     if (p_verbose >= 13 || debug_break_level > 0)
552     {
553 	int	save_msg_silent = msg_silent;
554 
555 	if (debug_break_level > 0)
556 	    msg_silent = FALSE;		/* display messages */
557 	else
558 	    verbose_enter();
559 	++no_wait_return;
560 	if (debug_break_level > 0 || *p_vfile == NUL)
561 	    msg_scroll = TRUE;	    /* always scroll up, don't overwrite */
562 
563 	smsg((char_u *)_("Exception thrown: %s"), excp->value);
564 	msg_puts((char_u *)"\n");   /* don't overwrite this either */
565 
566 	if (debug_break_level > 0 || *p_vfile == NUL)
567 	    cmdline_row = msg_row;
568 	--no_wait_return;
569 	if (debug_break_level > 0)
570 	    msg_silent = save_msg_silent;
571 	else
572 	    verbose_leave();
573     }
574 
575     current_exception = excp;
576     return OK;
577 
578 nomem:
579     vim_free(excp);
580     suppress_errthrow = TRUE;
581     EMSG(_(e_outofmem));
582 fail:
583     current_exception = NULL;
584     return FAIL;
585 }
586 
587 /*
588  * Discard an exception.  "was_finished" is set when the exception has been
589  * caught and the catch clause has been ended normally.
590  */
591     static void
592 discard_exception(except_T *excp, int was_finished)
593 {
594     char_u		*saved_IObuff;
595 
596     if (excp == NULL)
597     {
598 	EMSG(_(e_internal));
599 	return;
600     }
601 
602     if (p_verbose >= 13 || debug_break_level > 0)
603     {
604 	int	save_msg_silent = msg_silent;
605 
606 	saved_IObuff = vim_strsave(IObuff);
607 	if (debug_break_level > 0)
608 	    msg_silent = FALSE;		/* display messages */
609 	else
610 	    verbose_enter();
611 	++no_wait_return;
612 	if (debug_break_level > 0 || *p_vfile == NUL)
613 	    msg_scroll = TRUE;	    /* always scroll up, don't overwrite */
614 	smsg(was_finished
615 		    ? (char_u *)_("Exception finished: %s")
616 		    : (char_u *)_("Exception discarded: %s"),
617 		excp->value);
618 	msg_puts((char_u *)"\n");   /* don't overwrite this either */
619 	if (debug_break_level > 0 || *p_vfile == NUL)
620 	    cmdline_row = msg_row;
621 	--no_wait_return;
622 	if (debug_break_level > 0)
623 	    msg_silent = save_msg_silent;
624 	else
625 	    verbose_leave();
626 	STRCPY(IObuff, saved_IObuff);
627 	vim_free(saved_IObuff);
628     }
629     if (excp->type != ET_INTERRUPT)
630 	vim_free(excp->value);
631     if (excp->type == ET_ERROR)
632 	free_msglist(excp->messages);
633     vim_free(excp->throw_name);
634     vim_free(excp);
635 }
636 
637 /*
638  * Discard the exception currently being thrown.
639  */
640     void
641 discard_current_exception(void)
642 {
643     discard_exception(current_exception, FALSE);
644     current_exception = NULL;
645     did_throw = FALSE;
646     need_rethrow = FALSE;
647 }
648 
649 /*
650  * Put an exception on the caught stack.
651  */
652     static void
653 catch_exception(except_T *excp)
654 {
655     excp->caught = caught_stack;
656     caught_stack = excp;
657     set_vim_var_string(VV_EXCEPTION, excp->value, -1);
658     if (*excp->throw_name != NUL)
659     {
660 	if (excp->throw_lnum != 0)
661 	    vim_snprintf((char *)IObuff, IOSIZE, _("%s, line %ld"),
662 				    excp->throw_name, (long)excp->throw_lnum);
663 	else
664 	    vim_snprintf((char *)IObuff, IOSIZE, "%s", excp->throw_name);
665 	set_vim_var_string(VV_THROWPOINT, IObuff, -1);
666     }
667     else
668 	/* throw_name not set on an exception from a command that was typed. */
669 	set_vim_var_string(VV_THROWPOINT, NULL, -1);
670 
671     if (p_verbose >= 13 || debug_break_level > 0)
672     {
673 	int	save_msg_silent = msg_silent;
674 
675 	if (debug_break_level > 0)
676 	    msg_silent = FALSE;		/* display messages */
677 	else
678 	    verbose_enter();
679 	++no_wait_return;
680 	if (debug_break_level > 0 || *p_vfile == NUL)
681 	    msg_scroll = TRUE;	    /* always scroll up, don't overwrite */
682 
683 	smsg((char_u *)_("Exception caught: %s"), excp->value);
684 	msg_puts((char_u *)"\n");   /* don't overwrite this either */
685 
686 	if (debug_break_level > 0 || *p_vfile == NUL)
687 	    cmdline_row = msg_row;
688 	--no_wait_return;
689 	if (debug_break_level > 0)
690 	    msg_silent = save_msg_silent;
691 	else
692 	    verbose_leave();
693     }
694 }
695 
696 /*
697  * Remove an exception from the caught stack.
698  */
699     static void
700 finish_exception(except_T *excp)
701 {
702     if (excp != caught_stack)
703 	EMSG(_(e_internal));
704     caught_stack = caught_stack->caught;
705     if (caught_stack != NULL)
706     {
707 	set_vim_var_string(VV_EXCEPTION, caught_stack->value, -1);
708 	if (*caught_stack->throw_name != NUL)
709 	{
710 	    if (caught_stack->throw_lnum != 0)
711 		vim_snprintf((char *)IObuff, IOSIZE,
712 			_("%s, line %ld"), caught_stack->throw_name,
713 			(long)caught_stack->throw_lnum);
714 	    else
715 		vim_snprintf((char *)IObuff, IOSIZE, "%s",
716 						    caught_stack->throw_name);
717 	    set_vim_var_string(VV_THROWPOINT, IObuff, -1);
718 	}
719 	else
720 	    /* throw_name not set on an exception from a command that was
721 	     * typed. */
722 	    set_vim_var_string(VV_THROWPOINT, NULL, -1);
723     }
724     else
725     {
726 	set_vim_var_string(VV_EXCEPTION, NULL, -1);
727 	set_vim_var_string(VV_THROWPOINT, NULL, -1);
728     }
729 
730     /* Discard the exception, but use the finish message for 'verbose'. */
731     discard_exception(excp, TRUE);
732 }
733 
734 /*
735  * Flags specifying the message displayed by report_pending.
736  */
737 #define RP_MAKE		0
738 #define RP_RESUME	1
739 #define RP_DISCARD	2
740 
741 /*
742  * Report information about something pending in a finally clause if required by
743  * the 'verbose' option or when debugging.  "action" tells whether something is
744  * made pending or something pending is resumed or discarded.  "pending" tells
745  * what is pending.  "value" specifies the return value for a pending ":return"
746  * or the exception value for a pending exception.
747  */
748     static void
749 report_pending(int action, int pending, void *value)
750 {
751     char_u	*mesg;
752     char	*s;
753     int		save_msg_silent;
754 
755 
756     switch (action)
757     {
758 	case RP_MAKE:
759 	    mesg = (char_u *)_("%s made pending");
760 	    break;
761 	case RP_RESUME:
762 	    mesg = (char_u *)_("%s resumed");
763 	    break;
764 	/* case RP_DISCARD: */
765 	default:
766 	    mesg = (char_u *)_("%s discarded");
767 	    break;
768     }
769 
770     switch (pending)
771     {
772 	case CSTP_NONE:
773 	    return;
774 
775 	case CSTP_CONTINUE:
776 	    s = ":continue";
777 	    break;
778 	case CSTP_BREAK:
779 	    s = ":break";
780 	    break;
781 	case CSTP_FINISH:
782 	    s = ":finish";
783 	    break;
784 	case CSTP_RETURN:
785 	    /* ":return" command producing value, allocated */
786 	    s = (char *)get_return_cmd(value);
787 	    break;
788 
789 	default:
790 	    if (pending & CSTP_THROW)
791 	    {
792 		vim_snprintf((char *)IObuff, IOSIZE,
793 						(char *)mesg, _("Exception"));
794 		mesg = vim_strnsave(IObuff, (int)STRLEN(IObuff) + 4);
795 		STRCAT(mesg, ": %s");
796 		s = (char *)((except_T *)value)->value;
797 	    }
798 	    else if ((pending & CSTP_ERROR) && (pending & CSTP_INTERRUPT))
799 		s = _("Error and interrupt");
800 	    else if (pending & CSTP_ERROR)
801 		s = _("Error");
802 	    else /* if (pending & CSTP_INTERRUPT) */
803 		s = _("Interrupt");
804     }
805 
806     save_msg_silent = msg_silent;
807     if (debug_break_level > 0)
808 	msg_silent = FALSE;	/* display messages */
809     ++no_wait_return;
810     msg_scroll = TRUE;		/* always scroll up, don't overwrite */
811     smsg(mesg, (char_u *)s);
812     msg_puts((char_u *)"\n");   /* don't overwrite this either */
813     cmdline_row = msg_row;
814     --no_wait_return;
815     if (debug_break_level > 0)
816 	msg_silent = save_msg_silent;
817 
818     if (pending == CSTP_RETURN)
819 	vim_free(s);
820     else if (pending & CSTP_THROW)
821 	vim_free(mesg);
822 }
823 
824 /*
825  * If something is made pending in a finally clause, report it if required by
826  * the 'verbose' option or when debugging.
827  */
828     void
829 report_make_pending(int pending, void *value)
830 {
831     if (p_verbose >= 14 || debug_break_level > 0)
832     {
833 	if (debug_break_level <= 0)
834 	    verbose_enter();
835 	report_pending(RP_MAKE, pending, value);
836 	if (debug_break_level <= 0)
837 	    verbose_leave();
838     }
839 }
840 
841 /*
842  * If something pending in a finally clause is resumed at the ":endtry", report
843  * it if required by the 'verbose' option or when debugging.
844  */
845     void
846 report_resume_pending(int pending, void *value)
847 {
848     if (p_verbose >= 14 || debug_break_level > 0)
849     {
850 	if (debug_break_level <= 0)
851 	    verbose_enter();
852 	report_pending(RP_RESUME, pending, value);
853 	if (debug_break_level <= 0)
854 	    verbose_leave();
855     }
856 }
857 
858 /*
859  * If something pending in a finally clause is discarded, report it if required
860  * by the 'verbose' option or when debugging.
861  */
862     void
863 report_discard_pending(int pending, void *value)
864 {
865     if (p_verbose >= 14 || debug_break_level > 0)
866     {
867 	if (debug_break_level <= 0)
868 	    verbose_enter();
869 	report_pending(RP_DISCARD, pending, value);
870 	if (debug_break_level <= 0)
871 	    verbose_leave();
872     }
873 }
874 
875 
876 /*
877  * ":if".
878  */
879     void
880 ex_if(exarg_T *eap)
881 {
882     int		error;
883     int		skip;
884     int		result;
885     struct condstack	*cstack = eap->cstack;
886 
887     if (cstack->cs_idx == CSTACK_LEN - 1)
888 	eap->errmsg = (char_u *)N_("E579: :if nesting too deep");
889     else
890     {
891 	++cstack->cs_idx;
892 	cstack->cs_flags[cstack->cs_idx] = 0;
893 
894 	/*
895 	 * Don't do something after an error, interrupt, or throw, or when there
896 	 * is a surrounding conditional and it was not active.
897 	 */
898 	skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
899 		&& !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
900 
901 	result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
902 
903 	if (!skip && !error)
904 	{
905 	    if (result)
906 		cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE | CSF_TRUE;
907 	}
908 	else
909 	    /* set TRUE, so this conditional will never get active */
910 	    cstack->cs_flags[cstack->cs_idx] = CSF_TRUE;
911     }
912 }
913 
914 /*
915  * ":endif".
916  */
917     void
918 ex_endif(exarg_T *eap)
919 {
920     did_endif = TRUE;
921     if (eap->cstack->cs_idx < 0
922 	    || (eap->cstack->cs_flags[eap->cstack->cs_idx]
923 					   & (CSF_WHILE | CSF_FOR | CSF_TRY)))
924 	eap->errmsg = (char_u *)N_("E580: :endif without :if");
925     else
926     {
927 	/*
928 	 * When debugging or a breakpoint was encountered, display the debug
929 	 * prompt (if not already done).  This shows the user that an ":endif"
930 	 * is executed when the ":if" or a previous ":elseif" was not TRUE.
931 	 * Handle a ">quit" debug command as if an interrupt had occurred before
932 	 * the ":endif".  That is, throw an interrupt exception if appropriate.
933 	 * Doing this here prevents an exception for a parsing error being
934 	 * discarded by throwing the interrupt exception later on.
935 	 */
936 	if (!(eap->cstack->cs_flags[eap->cstack->cs_idx] & CSF_TRUE)
937 						    && dbg_check_skipped(eap))
938 	    (void)do_intthrow(eap->cstack);
939 
940 	--eap->cstack->cs_idx;
941     }
942 }
943 
944 /*
945  * ":else" and ":elseif".
946  */
947     void
948 ex_else(exarg_T *eap)
949 {
950     int		error;
951     int		skip;
952     int		result;
953     struct condstack	*cstack = eap->cstack;
954 
955     /*
956      * Don't do something after an error, interrupt, or throw, or when there is
957      * a surrounding conditional and it was not active.
958      */
959     skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
960 	    && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
961 
962     if (cstack->cs_idx < 0
963 	    || (cstack->cs_flags[cstack->cs_idx]
964 					   & (CSF_WHILE | CSF_FOR | CSF_TRY)))
965     {
966 	if (eap->cmdidx == CMD_else)
967 	{
968 	    eap->errmsg = (char_u *)N_("E581: :else without :if");
969 	    return;
970 	}
971 	eap->errmsg = (char_u *)N_("E582: :elseif without :if");
972 	skip = TRUE;
973     }
974     else if (cstack->cs_flags[cstack->cs_idx] & CSF_ELSE)
975     {
976 	if (eap->cmdidx == CMD_else)
977 	{
978 	    eap->errmsg = (char_u *)N_("E583: multiple :else");
979 	    return;
980 	}
981 	eap->errmsg = (char_u *)N_("E584: :elseif after :else");
982 	skip = TRUE;
983     }
984 
985     /* if skipping or the ":if" was TRUE, reset ACTIVE, otherwise set it */
986     if (skip || cstack->cs_flags[cstack->cs_idx] & CSF_TRUE)
987     {
988 	if (eap->errmsg == NULL)
989 	    cstack->cs_flags[cstack->cs_idx] = CSF_TRUE;
990 	skip = TRUE;	/* don't evaluate an ":elseif" */
991     }
992     else
993 	cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE;
994 
995     /*
996      * When debugging or a breakpoint was encountered, display the debug prompt
997      * (if not already done).  This shows the user that an ":else" or ":elseif"
998      * is executed when the ":if" or previous ":elseif" was not TRUE.  Handle
999      * a ">quit" debug command as if an interrupt had occurred before the
1000      * ":else" or ":elseif".  That is, set "skip" and throw an interrupt
1001      * exception if appropriate.  Doing this here prevents that an exception
1002      * for a parsing errors is discarded when throwing the interrupt exception
1003      * later on.
1004      */
1005     if (!skip && dbg_check_skipped(eap) && got_int)
1006     {
1007 	(void)do_intthrow(cstack);
1008 	skip = TRUE;
1009     }
1010 
1011     if (eap->cmdidx == CMD_elseif)
1012     {
1013 	result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
1014 	/* When throwing error exceptions, we want to throw always the first
1015 	 * of several errors in a row.  This is what actually happens when
1016 	 * a conditional error was detected above and there is another failure
1017 	 * when parsing the expression.  Since the skip flag is set in this
1018 	 * case, the parsing error will be ignored by emsg(). */
1019 
1020 	if (!skip && !error)
1021 	{
1022 	    if (result)
1023 		cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE | CSF_TRUE;
1024 	    else
1025 		cstack->cs_flags[cstack->cs_idx] = 0;
1026 	}
1027 	else if (eap->errmsg == NULL)
1028 	    /* set TRUE, so this conditional will never get active */
1029 	    cstack->cs_flags[cstack->cs_idx] = CSF_TRUE;
1030     }
1031     else
1032 	cstack->cs_flags[cstack->cs_idx] |= CSF_ELSE;
1033 }
1034 
1035 /*
1036  * Handle ":while" and ":for".
1037  */
1038     void
1039 ex_while(exarg_T *eap)
1040 {
1041     int		error;
1042     int		skip;
1043     int		result;
1044     struct condstack	*cstack = eap->cstack;
1045 
1046     if (cstack->cs_idx == CSTACK_LEN - 1)
1047 	eap->errmsg = (char_u *)N_("E585: :while/:for nesting too deep");
1048     else
1049     {
1050 	/*
1051 	 * The loop flag is set when we have jumped back from the matching
1052 	 * ":endwhile" or ":endfor".  When not set, need to initialise this
1053 	 * cstack entry.
1054 	 */
1055 	if ((cstack->cs_lflags & CSL_HAD_LOOP) == 0)
1056 	{
1057 	    ++cstack->cs_idx;
1058 	    ++cstack->cs_looplevel;
1059 	    cstack->cs_line[cstack->cs_idx] = -1;
1060 	}
1061 	cstack->cs_flags[cstack->cs_idx] =
1062 			       eap->cmdidx == CMD_while ? CSF_WHILE : CSF_FOR;
1063 
1064 	/*
1065 	 * Don't do something after an error, interrupt, or throw, or when
1066 	 * there is a surrounding conditional and it was not active.
1067 	 */
1068 	skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
1069 		&& !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
1070 	if (eap->cmdidx == CMD_while)
1071 	{
1072 	    /*
1073 	     * ":while bool-expr"
1074 	     */
1075 	    result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
1076 	}
1077 	else
1078 	{
1079 	    void *fi;
1080 
1081 	    /*
1082 	     * ":for var in list-expr"
1083 	     */
1084 	    if ((cstack->cs_lflags & CSL_HAD_LOOP) != 0)
1085 	    {
1086 		/* Jumping here from a ":continue" or ":endfor": use the
1087 		 * previously evaluated list. */
1088 		fi = cstack->cs_forinfo[cstack->cs_idx];
1089 		error = FALSE;
1090 	    }
1091 	    else
1092 	    {
1093 		/* Evaluate the argument and get the info in a structure. */
1094 		fi = eval_for_line(eap->arg, &error, &eap->nextcmd, skip);
1095 		cstack->cs_forinfo[cstack->cs_idx] = fi;
1096 	    }
1097 
1098 	    /* use the element at the start of the list and advance */
1099 	    if (!error && fi != NULL && !skip)
1100 		result = next_for_item(fi, eap->arg);
1101 	    else
1102 		result = FALSE;
1103 
1104 	    if (!result)
1105 	    {
1106 		free_for_info(fi);
1107 		cstack->cs_forinfo[cstack->cs_idx] = NULL;
1108 	    }
1109 	}
1110 
1111 	/*
1112 	 * If this cstack entry was just initialised and is active, set the
1113 	 * loop flag, so do_cmdline() will set the line number in cs_line[].
1114 	 * If executing the command a second time, clear the loop flag.
1115 	 */
1116 	if (!skip && !error && result)
1117 	{
1118 	    cstack->cs_flags[cstack->cs_idx] |= (CSF_ACTIVE | CSF_TRUE);
1119 	    cstack->cs_lflags ^= CSL_HAD_LOOP;
1120 	}
1121 	else
1122 	{
1123 	    cstack->cs_lflags &= ~CSL_HAD_LOOP;
1124 	    /* If the ":while" evaluates to FALSE or ":for" is past the end of
1125 	     * the list, show the debug prompt at the ":endwhile"/":endfor" as
1126 	     * if there was a ":break" in a ":while"/":for" evaluating to
1127 	     * TRUE. */
1128 	    if (!skip && !error)
1129 		cstack->cs_flags[cstack->cs_idx] |= CSF_TRUE;
1130 	}
1131     }
1132 }
1133 
1134 /*
1135  * ":continue"
1136  */
1137     void
1138 ex_continue(exarg_T *eap)
1139 {
1140     int		idx;
1141     struct condstack	*cstack = eap->cstack;
1142 
1143     if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0)
1144 	eap->errmsg = (char_u *)N_("E586: :continue without :while or :for");
1145     else
1146     {
1147 	/* Try to find the matching ":while".  This might stop at a try
1148 	 * conditional not in its finally clause (which is then to be executed
1149 	 * next).  Therefor, inactivate all conditionals except the ":while"
1150 	 * itself (if reached). */
1151 	idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
1152 	if (idx >= 0 && (cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR)))
1153 	{
1154 	    rewind_conditionals(cstack, idx, CSF_TRY, &cstack->cs_trylevel);
1155 
1156 	    /*
1157 	     * Set CSL_HAD_CONT, so do_cmdline() will jump back to the
1158 	     * matching ":while".
1159 	     */
1160 	    cstack->cs_lflags |= CSL_HAD_CONT;	/* let do_cmdline() handle it */
1161 	}
1162 	else
1163 	{
1164 	    /* If a try conditional not in its finally clause is reached first,
1165 	     * make the ":continue" pending for execution at the ":endtry". */
1166 	    cstack->cs_pending[idx] = CSTP_CONTINUE;
1167 	    report_make_pending(CSTP_CONTINUE, NULL);
1168 	}
1169     }
1170 }
1171 
1172 /*
1173  * ":break"
1174  */
1175     void
1176 ex_break(exarg_T *eap)
1177 {
1178     int		idx;
1179     struct condstack	*cstack = eap->cstack;
1180 
1181     if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0)
1182 	eap->errmsg = (char_u *)N_("E587: :break without :while or :for");
1183     else
1184     {
1185 	/* Inactivate conditionals until the matching ":while" or a try
1186 	 * conditional not in its finally clause (which is then to be
1187 	 * executed next) is found.  In the latter case, make the ":break"
1188 	 * pending for execution at the ":endtry". */
1189 	idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, TRUE);
1190 	if (idx >= 0 && !(cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR)))
1191 	{
1192 	    cstack->cs_pending[idx] = CSTP_BREAK;
1193 	    report_make_pending(CSTP_BREAK, NULL);
1194 	}
1195     }
1196 }
1197 
1198 /*
1199  * ":endwhile" and ":endfor"
1200  */
1201     void
1202 ex_endwhile(exarg_T *eap)
1203 {
1204     struct condstack	*cstack = eap->cstack;
1205     int			idx;
1206     char_u		*err;
1207     int			csf;
1208     int			fl;
1209 
1210     if (eap->cmdidx == CMD_endwhile)
1211     {
1212 	err = e_while;
1213 	csf = CSF_WHILE;
1214     }
1215     else
1216     {
1217 	err = e_for;
1218 	csf = CSF_FOR;
1219     }
1220 
1221     if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0)
1222 	eap->errmsg = err;
1223     else
1224     {
1225 	fl =  cstack->cs_flags[cstack->cs_idx];
1226 	if (!(fl & csf))
1227 	{
1228 	    /* If we are in a ":while" or ":for" but used the wrong endloop
1229 	     * command, do not rewind to the next enclosing ":for"/":while". */
1230 	    if (fl & CSF_WHILE)
1231 		eap->errmsg = (char_u *)_("E732: Using :endfor with :while");
1232 	    else if (fl & CSF_FOR)
1233 		eap->errmsg = (char_u *)_("E733: Using :endwhile with :for");
1234 	}
1235 	if (!(fl & (CSF_WHILE | CSF_FOR)))
1236 	{
1237 	    if (!(fl & CSF_TRY))
1238 		eap->errmsg = e_endif;
1239 	    else if (fl & CSF_FINALLY)
1240 		eap->errmsg = e_endtry;
1241 	    /* Try to find the matching ":while" and report what's missing. */
1242 	    for (idx = cstack->cs_idx; idx > 0; --idx)
1243 	    {
1244 		fl =  cstack->cs_flags[idx];
1245 		if ((fl & CSF_TRY) && !(fl & CSF_FINALLY))
1246 		{
1247 		    /* Give up at a try conditional not in its finally clause.
1248 		     * Ignore the ":endwhile"/":endfor". */
1249 		    eap->errmsg = err;
1250 		    return;
1251 		}
1252 		if (fl & csf)
1253 		    break;
1254 	    }
1255 	    /* Cleanup and rewind all contained (and unclosed) conditionals. */
1256 	    (void)cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
1257 	    rewind_conditionals(cstack, idx, CSF_TRY, &cstack->cs_trylevel);
1258 	}
1259 
1260 	/*
1261 	 * When debugging or a breakpoint was encountered, display the debug
1262 	 * prompt (if not already done).  This shows the user that an
1263 	 * ":endwhile"/":endfor" is executed when the ":while" was not TRUE or
1264 	 * after a ":break".  Handle a ">quit" debug command as if an
1265 	 * interrupt had occurred before the ":endwhile"/":endfor".  That is,
1266 	 * throw an interrupt exception if appropriate.  Doing this here
1267 	 * prevents that an exception for a parsing error is discarded when
1268 	 * throwing the interrupt exception later on.
1269 	 */
1270 	else if (cstack->cs_flags[cstack->cs_idx] & CSF_TRUE
1271 		&& !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)
1272 		&& dbg_check_skipped(eap))
1273 	    (void)do_intthrow(cstack);
1274 
1275 	/*
1276 	 * Set loop flag, so do_cmdline() will jump back to the matching
1277 	 * ":while" or ":for".
1278 	 */
1279 	cstack->cs_lflags |= CSL_HAD_ENDLOOP;
1280     }
1281 }
1282 
1283 
1284 /*
1285  * ":throw expr"
1286  */
1287     void
1288 ex_throw(exarg_T *eap)
1289 {
1290     char_u	*arg = eap->arg;
1291     char_u	*value;
1292 
1293     if (*arg != NUL && *arg != '|' && *arg != '\n')
1294 	value = eval_to_string_skip(arg, &eap->nextcmd, eap->skip);
1295     else
1296     {
1297 	EMSG(_(e_argreq));
1298 	value = NULL;
1299     }
1300 
1301     /* On error or when an exception is thrown during argument evaluation, do
1302      * not throw. */
1303     if (!eap->skip && value != NULL)
1304     {
1305 	if (throw_exception(value, ET_USER, NULL) == FAIL)
1306 	    vim_free(value);
1307 	else
1308 	    do_throw(eap->cstack);
1309     }
1310 }
1311 
1312 /*
1313  * Throw the current exception through the specified cstack.  Common routine
1314  * for ":throw" (user exception) and error and interrupt exceptions.  Also
1315  * used for rethrowing an uncaught exception.
1316  */
1317     void
1318 do_throw(struct condstack *cstack)
1319 {
1320     int		idx;
1321     int		inactivate_try = FALSE;
1322 
1323     /*
1324      * Cleanup and inactivate up to the next surrounding try conditional that
1325      * is not in its finally clause.  Normally, do not inactivate the try
1326      * conditional itself, so that its ACTIVE flag can be tested below.  But
1327      * if a previous error or interrupt has not been converted to an exception,
1328      * inactivate the try conditional, too, as if the conversion had been done,
1329      * and reset the did_emsg or got_int flag, so this won't happen again at
1330      * the next surrounding try conditional.
1331      */
1332 #ifndef THROW_ON_ERROR_TRUE
1333     if (did_emsg && !THROW_ON_ERROR)
1334     {
1335 	inactivate_try = TRUE;
1336 	did_emsg = FALSE;
1337     }
1338 #endif
1339 #ifndef THROW_ON_INTERRUPT_TRUE
1340     if (got_int && !THROW_ON_INTERRUPT)
1341     {
1342 	inactivate_try = TRUE;
1343 	got_int = FALSE;
1344     }
1345 #endif
1346     idx = cleanup_conditionals(cstack, 0, inactivate_try);
1347     if (idx >= 0)
1348     {
1349 	/*
1350 	 * If this try conditional is active and we are before its first
1351 	 * ":catch", set THROWN so that the ":catch" commands will check
1352 	 * whether the exception matches.  When the exception came from any of
1353 	 * the catch clauses, it will be made pending at the ":finally" (if
1354 	 * present) and rethrown at the ":endtry".  This will also happen if
1355 	 * the try conditional is inactive.  This is the case when we are
1356 	 * throwing an exception due to an error or interrupt on the way from
1357 	 * a preceding ":continue", ":break", ":return", ":finish", error or
1358 	 * interrupt (not converted to an exception) to the finally clause or
1359 	 * from a preceding throw of a user or error or interrupt exception to
1360 	 * the matching catch clause or the finally clause.
1361 	 */
1362 	if (!(cstack->cs_flags[idx] & CSF_CAUGHT))
1363 	{
1364 	    if (cstack->cs_flags[idx] & CSF_ACTIVE)
1365 		cstack->cs_flags[idx] |= CSF_THROWN;
1366 	    else
1367 		/* THROWN may have already been set for a catchable exception
1368 		 * that has been discarded.  Ensure it is reset for the new
1369 		 * exception. */
1370 		cstack->cs_flags[idx] &= ~CSF_THROWN;
1371 	}
1372 	cstack->cs_flags[idx] &= ~CSF_ACTIVE;
1373 	cstack->cs_exception[idx] = current_exception;
1374     }
1375 #if 0
1376     /* TODO: Add optimization below.  Not yet done because of interface
1377      * problems to eval.c and ex_cmds2.c. (Servatius) */
1378     else
1379     {
1380 	/*
1381 	 * There are no catch clauses to check or finally clauses to execute.
1382 	 * End the current script or function.  The exception will be rethrown
1383 	 * in the caller.
1384 	 */
1385 	if (getline_equal(eap->getline, eap->cookie, get_func_line))
1386 	    current_funccal->returned = TRUE;
1387 	elseif (eap->get_func_line == getsourceline)
1388 	    ((struct source_cookie *)eap->cookie)->finished = TRUE;
1389     }
1390 #endif
1391 
1392     did_throw = TRUE;
1393 }
1394 
1395 /*
1396  * ":try"
1397  */
1398     void
1399 ex_try(exarg_T *eap)
1400 {
1401     int		skip;
1402     struct condstack	*cstack = eap->cstack;
1403 
1404     if (cstack->cs_idx == CSTACK_LEN - 1)
1405 	eap->errmsg = (char_u *)N_("E601: :try nesting too deep");
1406     else
1407     {
1408 	++cstack->cs_idx;
1409 	++cstack->cs_trylevel;
1410 	cstack->cs_flags[cstack->cs_idx] = CSF_TRY;
1411 	cstack->cs_pending[cstack->cs_idx] = CSTP_NONE;
1412 
1413 	/*
1414 	 * Don't do something after an error, interrupt, or throw, or when there
1415 	 * is a surrounding conditional and it was not active.
1416 	 */
1417 	skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
1418 		&& !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
1419 
1420 	if (!skip)
1421 	{
1422 	    /* Set ACTIVE and TRUE.  TRUE means that the corresponding ":catch"
1423 	     * commands should check for a match if an exception is thrown and
1424 	     * that the finally clause needs to be executed. */
1425 	    cstack->cs_flags[cstack->cs_idx] |= CSF_ACTIVE | CSF_TRUE;
1426 
1427 	    /*
1428 	     * ":silent!", even when used in a try conditional, disables
1429 	     * displaying of error messages and conversion of errors to
1430 	     * exceptions.  When the silent commands again open a try
1431 	     * conditional, save "emsg_silent" and reset it so that errors are
1432 	     * again converted to exceptions.  The value is restored when that
1433 	     * try conditional is left.  If it is left normally, the commands
1434 	     * following the ":endtry" are again silent.  If it is left by
1435 	     * a ":continue", ":break", ":return", or ":finish", the commands
1436 	     * executed next are again silent.  If it is left due to an
1437 	     * aborting error, an interrupt, or an exception, restoring
1438 	     * "emsg_silent" does not matter since we are already in the
1439 	     * aborting state and/or the exception has already been thrown.
1440 	     * The effect is then just freeing the memory that was allocated
1441 	     * to save the value.
1442 	     */
1443 	    if (emsg_silent)
1444 	    {
1445 		eslist_T	*elem;
1446 
1447 		elem = (eslist_T *)alloc((unsigned)sizeof(struct eslist_elem));
1448 		if (elem == NULL)
1449 		    EMSG(_(e_outofmem));
1450 		else
1451 		{
1452 		    elem->saved_emsg_silent = emsg_silent;
1453 		    elem->next = cstack->cs_emsg_silent_list;
1454 		    cstack->cs_emsg_silent_list = elem;
1455 		    cstack->cs_flags[cstack->cs_idx] |= CSF_SILENT;
1456 		    emsg_silent = 0;
1457 		}
1458 	    }
1459 	}
1460 
1461     }
1462 }
1463 
1464 /*
1465  * ":catch /{pattern}/" and ":catch"
1466  */
1467     void
1468 ex_catch(exarg_T *eap)
1469 {
1470     int		idx = 0;
1471     int		give_up = FALSE;
1472     int		skip = FALSE;
1473     int		caught = FALSE;
1474     char_u	*end;
1475     int		save_char = 0;
1476     char_u	*save_cpo;
1477     regmatch_T	regmatch;
1478     int		prev_got_int;
1479     struct condstack	*cstack = eap->cstack;
1480     char_u	*pat;
1481 
1482     if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1483     {
1484 	eap->errmsg = (char_u *)N_("E603: :catch without :try");
1485 	give_up = TRUE;
1486     }
1487     else
1488     {
1489 	if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1490 	{
1491 	    /* Report what's missing if the matching ":try" is not in its
1492 	     * finally clause. */
1493 	    eap->errmsg = get_end_emsg(cstack);
1494 	    skip = TRUE;
1495 	}
1496 	for (idx = cstack->cs_idx; idx > 0; --idx)
1497 	    if (cstack->cs_flags[idx] & CSF_TRY)
1498 		break;
1499 	if (cstack->cs_flags[idx] & CSF_FINALLY)
1500 	{
1501 	    /* Give up for a ":catch" after ":finally" and ignore it.
1502 	     * Just parse. */
1503 	    eap->errmsg = (char_u *)N_("E604: :catch after :finally");
1504 	    give_up = TRUE;
1505 	}
1506 	else
1507 	    rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
1508 						       &cstack->cs_looplevel);
1509     }
1510 
1511     if (ends_excmd(*eap->arg))	/* no argument, catch all errors */
1512     {
1513 	pat = (char_u *)".*";
1514 	end = NULL;
1515 	eap->nextcmd = find_nextcmd(eap->arg);
1516     }
1517     else
1518     {
1519 	pat = eap->arg + 1;
1520 	end = skip_regexp(pat, *eap->arg, TRUE, NULL);
1521     }
1522 
1523     if (!give_up)
1524     {
1525 	/*
1526 	 * Don't do something when no exception has been thrown or when the
1527 	 * corresponding try block never got active (because of an inactive
1528 	 * surrounding conditional or after an error or interrupt or throw).
1529 	 */
1530 	if (!did_throw || !(cstack->cs_flags[idx] & CSF_TRUE))
1531 	    skip = TRUE;
1532 
1533 	/*
1534 	 * Check for a match only if an exception is thrown but not caught by
1535 	 * a previous ":catch".  An exception that has replaced a discarded
1536 	 * exception is not checked (THROWN is not set then).
1537 	 */
1538 	if (!skip && (cstack->cs_flags[idx] & CSF_THROWN)
1539 		&& !(cstack->cs_flags[idx] & CSF_CAUGHT))
1540 	{
1541 	    if (end != NULL && *end != NUL && !ends_excmd(*skipwhite(end + 1)))
1542 	    {
1543 		EMSG(_(e_trailing));
1544 		return;
1545 	    }
1546 
1547 	    /* When debugging or a breakpoint was encountered, display the
1548 	     * debug prompt (if not already done) before checking for a match.
1549 	     * This is a helpful hint for the user when the regular expression
1550 	     * matching fails.  Handle a ">quit" debug command as if an
1551 	     * interrupt had occurred before the ":catch".  That is, discard
1552 	     * the original exception, replace it by an interrupt exception,
1553 	     * and don't catch it in this try block. */
1554 	    if (!dbg_check_skipped(eap) || !do_intthrow(cstack))
1555 	    {
1556 		/* Terminate the pattern and avoid the 'l' flag in 'cpoptions'
1557 		 * while compiling it. */
1558 		if (end != NULL)
1559 		{
1560 		    save_char = *end;
1561 		    *end = NUL;
1562 		}
1563 		save_cpo  = p_cpo;
1564 		p_cpo = (char_u *)"";
1565 		/* Disable error messages, it will make current_exception
1566 		 * invalid. */
1567 		++emsg_off;
1568 		regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1569 		--emsg_off;
1570 		regmatch.rm_ic = FALSE;
1571 		if (end != NULL)
1572 		    *end = save_char;
1573 		p_cpo = save_cpo;
1574 		if (regmatch.regprog == NULL)
1575 		    EMSG2(_(e_invarg2), pat);
1576 		else
1577 		{
1578 		    /*
1579 		     * Save the value of got_int and reset it.  We don't want
1580 		     * a previous interruption cancel matching, only hitting
1581 		     * CTRL-C while matching should abort it.
1582 		     */
1583 		    prev_got_int = got_int;
1584 		    got_int = FALSE;
1585 		    caught = vim_regexec_nl(&regmatch, current_exception->value,
1586 			    (colnr_T)0);
1587 		    got_int |= prev_got_int;
1588 		    vim_regfree(regmatch.regprog);
1589 		}
1590 	    }
1591 	}
1592 
1593 	if (caught)
1594 	{
1595 	    /* Make this ":catch" clause active and reset did_emsg, got_int,
1596 	     * and did_throw.  Put the exception on the caught stack. */
1597 	    cstack->cs_flags[idx] |= CSF_ACTIVE | CSF_CAUGHT;
1598 	    did_emsg = got_int = did_throw = FALSE;
1599 	    catch_exception((except_T *)cstack->cs_exception[idx]);
1600 	    /* It's mandatory that the current exception is stored in the cstack
1601 	     * so that it can be discarded at the next ":catch", ":finally", or
1602 	     * ":endtry" or when the catch clause is left by a ":continue",
1603 	     * ":break", ":return", ":finish", error, interrupt, or another
1604 	     * exception. */
1605 	    if (cstack->cs_exception[cstack->cs_idx] != current_exception)
1606 		EMSG(_(e_internal));
1607 	}
1608 	else
1609 	{
1610 	    /*
1611 	     * If there is a preceding catch clause and it caught the exception,
1612 	     * finish the exception now.  This happens also after errors except
1613 	     * when this ":catch" was after the ":finally" or not within
1614 	     * a ":try".  Make the try conditional inactive so that the
1615 	     * following catch clauses are skipped.  On an error or interrupt
1616 	     * after the preceding try block or catch clause was left by
1617 	     * a ":continue", ":break", ":return", or ":finish", discard the
1618 	     * pending action.
1619 	     */
1620 	    cleanup_conditionals(cstack, CSF_TRY, TRUE);
1621 	}
1622     }
1623 
1624     if (end != NULL)
1625 	eap->nextcmd = find_nextcmd(end);
1626 }
1627 
1628 /*
1629  * ":finally"
1630  */
1631     void
1632 ex_finally(exarg_T *eap)
1633 {
1634     int		idx;
1635     int		skip = FALSE;
1636     int		pending = CSTP_NONE;
1637     struct condstack	*cstack = eap->cstack;
1638 
1639     if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1640 	eap->errmsg = (char_u *)N_("E606: :finally without :try");
1641     else
1642     {
1643 	if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1644 	{
1645 	    eap->errmsg = get_end_emsg(cstack);
1646 	    for (idx = cstack->cs_idx - 1; idx > 0; --idx)
1647 		if (cstack->cs_flags[idx] & CSF_TRY)
1648 		    break;
1649 	    /* Make this error pending, so that the commands in the following
1650 	     * finally clause can be executed.  This overrules also a pending
1651 	     * ":continue", ":break", ":return", or ":finish". */
1652 	    pending = CSTP_ERROR;
1653 	}
1654 	else
1655 	    idx = cstack->cs_idx;
1656 
1657 	if (cstack->cs_flags[idx] & CSF_FINALLY)
1658 	{
1659 	    /* Give up for a multiple ":finally" and ignore it. */
1660 	    eap->errmsg = (char_u *)N_("E607: multiple :finally");
1661 	    return;
1662 	}
1663 	rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
1664 						       &cstack->cs_looplevel);
1665 
1666 	/*
1667 	 * Don't do something when the corresponding try block never got active
1668 	 * (because of an inactive surrounding conditional or after an error or
1669 	 * interrupt or throw) or for a ":finally" without ":try" or a multiple
1670 	 * ":finally".  After every other error (did_emsg or the conditional
1671 	 * errors detected above) or after an interrupt (got_int) or an
1672 	 * exception (did_throw), the finally clause must be executed.
1673 	 */
1674 	skip = !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE);
1675 
1676 	if (!skip)
1677 	{
1678 	    /* When debugging or a breakpoint was encountered, display the
1679 	     * debug prompt (if not already done).  The user then knows that the
1680 	     * finally clause is executed. */
1681 	    if (dbg_check_skipped(eap))
1682 	    {
1683 		/* Handle a ">quit" debug command as if an interrupt had
1684 		 * occurred before the ":finally".  That is, discard the
1685 		 * original exception and replace it by an interrupt
1686 		 * exception. */
1687 		(void)do_intthrow(cstack);
1688 	    }
1689 
1690 	    /*
1691 	     * If there is a preceding catch clause and it caught the exception,
1692 	     * finish the exception now.  This happens also after errors except
1693 	     * when this is a multiple ":finally" or one not within a ":try".
1694 	     * After an error or interrupt, this also discards a pending
1695 	     * ":continue", ":break", ":finish", or ":return" from the preceding
1696 	     * try block or catch clause.
1697 	     */
1698 	    cleanup_conditionals(cstack, CSF_TRY, FALSE);
1699 
1700 	    /*
1701 	     * Make did_emsg, got_int, did_throw pending.  If set, they overrule
1702 	     * a pending ":continue", ":break", ":return", or ":finish".  Then
1703 	     * we have particularly to discard a pending return value (as done
1704 	     * by the call to cleanup_conditionals() above when did_emsg or
1705 	     * got_int is set).  The pending values are restored by the
1706 	     * ":endtry", except if there is a new error, interrupt, exception,
1707 	     * ":continue", ":break", ":return", or ":finish" in the following
1708 	     * finally clause.  A missing ":endwhile", ":endfor" or ":endif"
1709 	     * detected here is treated as if did_emsg and did_throw had
1710 	     * already been set, respectively in case that the error is not
1711 	     * converted to an exception, did_throw had already been unset.
1712 	     * We must not set did_emsg here since that would suppress the
1713 	     * error message.
1714 	     */
1715 	    if (pending == CSTP_ERROR || did_emsg || got_int || did_throw)
1716 	    {
1717 		if (cstack->cs_pending[cstack->cs_idx] == CSTP_RETURN)
1718 		{
1719 		    report_discard_pending(CSTP_RETURN,
1720 					   cstack->cs_rettv[cstack->cs_idx]);
1721 		    discard_pending_return(cstack->cs_rettv[cstack->cs_idx]);
1722 		}
1723 		if (pending == CSTP_ERROR && !did_emsg)
1724 		    pending |= (THROW_ON_ERROR) ? CSTP_THROW : 0;
1725 		else
1726 		    pending |= did_throw ? CSTP_THROW : 0;
1727 		pending |= did_emsg  ? CSTP_ERROR     : 0;
1728 		pending |= got_int   ? CSTP_INTERRUPT : 0;
1729 		cstack->cs_pending[cstack->cs_idx] = pending;
1730 
1731 		/* It's mandatory that the current exception is stored in the
1732 		 * cstack so that it can be rethrown at the ":endtry" or be
1733 		 * discarded if the finally clause is left by a ":continue",
1734 		 * ":break", ":return", ":finish", error, interrupt, or another
1735 		 * exception.  When emsg() is called for a missing ":endif" or
1736 		 * a missing ":endwhile"/":endfor" detected here, the
1737 		 * exception will be discarded. */
1738 		if (did_throw && cstack->cs_exception[cstack->cs_idx]
1739 							 != current_exception)
1740 		    EMSG(_(e_internal));
1741 	    }
1742 
1743 	    /*
1744 	     * Set CSL_HAD_FINA, so do_cmdline() will reset did_emsg,
1745 	     * got_int, and did_throw and make the finally clause active.
1746 	     * This will happen after emsg() has been called for a missing
1747 	     * ":endif" or a missing ":endwhile"/":endfor" detected here, so
1748 	     * that the following finally clause will be executed even then.
1749 	     */
1750 	    cstack->cs_lflags |= CSL_HAD_FINA;
1751 	}
1752     }
1753 }
1754 
1755 /*
1756  * ":endtry"
1757  */
1758     void
1759 ex_endtry(exarg_T *eap)
1760 {
1761     int		idx;
1762     int		skip;
1763     int		rethrow = FALSE;
1764     int		pending = CSTP_NONE;
1765     void	*rettv = NULL;
1766     struct condstack	*cstack = eap->cstack;
1767 
1768     if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1769 	eap->errmsg = (char_u *)N_("E602: :endtry without :try");
1770     else
1771     {
1772 	/*
1773 	 * Don't do something after an error, interrupt or throw in the try
1774 	 * block, catch clause, or finally clause preceding this ":endtry" or
1775 	 * when an error or interrupt occurred after a ":continue", ":break",
1776 	 * ":return", or ":finish" in a try block or catch clause preceding this
1777 	 * ":endtry" or when the try block never got active (because of an
1778 	 * inactive surrounding conditional or after an error or interrupt or
1779 	 * throw) or when there is a surrounding conditional and it has been
1780 	 * made inactive by a ":continue", ":break", ":return", or ":finish" in
1781 	 * the finally clause.  The latter case need not be tested since then
1782 	 * anything pending has already been discarded. */
1783 	skip = did_emsg || got_int || did_throw ||
1784 	    !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE);
1785 
1786 	if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1787 	{
1788 	    eap->errmsg = get_end_emsg(cstack);
1789 	    /* Find the matching ":try" and report what's missing. */
1790 	    idx = cstack->cs_idx;
1791 	    do
1792 		--idx;
1793 	    while (idx > 0 && !(cstack->cs_flags[idx] & CSF_TRY));
1794 	    rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
1795 						       &cstack->cs_looplevel);
1796 	    skip = TRUE;
1797 
1798 	    /*
1799 	     * If an exception is being thrown, discard it to prevent it from
1800 	     * being rethrown at the end of this function.  It would be
1801 	     * discarded by the error message, anyway.  Resets did_throw.
1802 	     * This does not affect the script termination due to the error
1803 	     * since "trylevel" is decremented after emsg() has been called.
1804 	     */
1805 	    if (did_throw)
1806 		discard_current_exception();
1807 	}
1808 	else
1809 	{
1810 	    idx = cstack->cs_idx;
1811 
1812 	    /*
1813 	     * If we stopped with the exception currently being thrown at this
1814 	     * try conditional since we didn't know that it doesn't have
1815 	     * a finally clause, we need to rethrow it after closing the try
1816 	     * conditional.
1817 	     */
1818 	    if (did_throw && (cstack->cs_flags[idx] & CSF_TRUE)
1819 		    && !(cstack->cs_flags[idx] & CSF_FINALLY))
1820 		rethrow = TRUE;
1821 	}
1822 
1823 	/* If there was no finally clause, show the user when debugging or
1824 	 * a breakpoint was encountered that the end of the try conditional has
1825 	 * been reached: display the debug prompt (if not already done).  Do
1826 	 * this on normal control flow or when an exception was thrown, but not
1827 	 * on an interrupt or error not converted to an exception or when
1828 	 * a ":break", ":continue", ":return", or ":finish" is pending.  These
1829 	 * actions are carried out immediately.
1830 	 */
1831 	if ((rethrow || (!skip
1832 			&& !(cstack->cs_flags[idx] & CSF_FINALLY)
1833 			&& !cstack->cs_pending[idx]))
1834 		&& dbg_check_skipped(eap))
1835 	{
1836 	    /* Handle a ">quit" debug command as if an interrupt had occurred
1837 	     * before the ":endtry".  That is, throw an interrupt exception and
1838 	     * set "skip" and "rethrow". */
1839 	    if (got_int)
1840 	    {
1841 		skip = TRUE;
1842 		(void)do_intthrow(cstack);
1843 		/* The do_intthrow() call may have reset did_throw or
1844 		 * cstack->cs_pending[idx].*/
1845 		rethrow = FALSE;
1846 		if (did_throw && !(cstack->cs_flags[idx] & CSF_FINALLY))
1847 		    rethrow = TRUE;
1848 	    }
1849 	}
1850 
1851 	/*
1852 	 * If a ":return" is pending, we need to resume it after closing the
1853 	 * try conditional; remember the return value.  If there was a finally
1854 	 * clause making an exception pending, we need to rethrow it.  Make it
1855 	 * the exception currently being thrown.
1856 	 */
1857 	if (!skip)
1858 	{
1859 	    pending = cstack->cs_pending[idx];
1860 	    cstack->cs_pending[idx] = CSTP_NONE;
1861 	    if (pending == CSTP_RETURN)
1862 		rettv = cstack->cs_rettv[idx];
1863 	    else if (pending & CSTP_THROW)
1864 		current_exception = cstack->cs_exception[idx];
1865 	}
1866 
1867 	/*
1868 	 * Discard anything pending on an error, interrupt, or throw in the
1869 	 * finally clause.  If there was no ":finally", discard a pending
1870 	 * ":continue", ":break", ":return", or ":finish" if an error or
1871 	 * interrupt occurred afterwards, but before the ":endtry" was reached.
1872 	 * If an exception was caught by the last of the catch clauses and there
1873 	 * was no finally clause, finish the exception now.  This happens also
1874 	 * after errors except when this ":endtry" is not within a ":try".
1875 	 * Restore "emsg_silent" if it has been reset by this try conditional.
1876 	 */
1877 	(void)cleanup_conditionals(cstack, CSF_TRY | CSF_SILENT, TRUE);
1878 
1879 	--cstack->cs_idx;
1880 	--cstack->cs_trylevel;
1881 
1882 	if (!skip)
1883 	{
1884 	    report_resume_pending(pending,
1885 		    (pending == CSTP_RETURN) ? rettv :
1886 		    (pending & CSTP_THROW) ? (void *)current_exception : NULL);
1887 	    switch (pending)
1888 	    {
1889 		case CSTP_NONE:
1890 		    break;
1891 
1892 		/* Reactivate a pending ":continue", ":break", ":return",
1893 		 * ":finish" from the try block or a catch clause of this try
1894 		 * conditional.  This is skipped, if there was an error in an
1895 		 * (unskipped) conditional command or an interrupt afterwards
1896 		 * or if the finally clause is present and executed a new error,
1897 		 * interrupt, throw, ":continue", ":break", ":return", or
1898 		 * ":finish". */
1899 		case CSTP_CONTINUE:
1900 		    ex_continue(eap);
1901 		    break;
1902 		case CSTP_BREAK:
1903 		    ex_break(eap);
1904 		    break;
1905 		case CSTP_RETURN:
1906 		    do_return(eap, FALSE, FALSE, rettv);
1907 		    break;
1908 		case CSTP_FINISH:
1909 		    do_finish(eap, FALSE);
1910 		    break;
1911 
1912 		/* When the finally clause was entered due to an error,
1913 		 * interrupt or throw (as opposed to a ":continue", ":break",
1914 		 * ":return", or ":finish"), restore the pending values of
1915 		 * did_emsg, got_int, and did_throw.  This is skipped, if there
1916 		 * was a new error, interrupt, throw, ":continue", ":break",
1917 		 * ":return", or ":finish".  in the finally clause. */
1918 		default:
1919 		    if (pending & CSTP_ERROR)
1920 			did_emsg = TRUE;
1921 		    if (pending & CSTP_INTERRUPT)
1922 			got_int = TRUE;
1923 		    if (pending & CSTP_THROW)
1924 			rethrow = TRUE;
1925 		    break;
1926 	    }
1927 	}
1928 
1929 	if (rethrow)
1930 	    /* Rethrow the current exception (within this cstack). */
1931 	    do_throw(cstack);
1932     }
1933 }
1934 
1935 /*
1936  * enter_cleanup() and leave_cleanup()
1937  *
1938  * Functions to be called before/after invoking a sequence of autocommands for
1939  * cleanup for a failed command.  (Failure means here that a call to emsg()
1940  * has been made, an interrupt occurred, or there is an uncaught exception
1941  * from a previous autocommand execution of the same command.)
1942  *
1943  * Call enter_cleanup() with a pointer to a cleanup_T and pass the same
1944  * pointer to leave_cleanup().  The cleanup_T structure stores the pending
1945  * error/interrupt/exception state.
1946  */
1947 
1948 /*
1949  * This function works a bit like ex_finally() except that there was not
1950  * actually an extra try block around the part that failed and an error or
1951  * interrupt has not (yet) been converted to an exception.  This function
1952  * saves the error/interrupt/ exception state and prepares for the call to
1953  * do_cmdline() that is going to be made for the cleanup autocommand
1954  * execution.
1955  */
1956     void
1957 enter_cleanup(cleanup_T *csp)
1958 {
1959     int		pending = CSTP_NONE;
1960 
1961     /*
1962      * Postpone did_emsg, got_int, did_throw.  The pending values will be
1963      * restored by leave_cleanup() except if there was an aborting error,
1964      * interrupt, or uncaught exception after this function ends.
1965      */
1966     if (did_emsg || got_int || did_throw || need_rethrow)
1967     {
1968 	csp->pending = (did_emsg     ? CSTP_ERROR     : 0)
1969 		     | (got_int      ? CSTP_INTERRUPT : 0)
1970 		     | (did_throw    ? CSTP_THROW     : 0)
1971 		     | (need_rethrow ? CSTP_THROW     : 0);
1972 
1973 	/* If we are currently throwing an exception (did_throw), save it as
1974 	 * well.  On an error not yet converted to an exception, update
1975 	 * "force_abort" and reset "cause_abort" (as do_errthrow() would do).
1976 	 * This is needed for the do_cmdline() call that is going to be made
1977 	 * for autocommand execution.  We need not save *msg_list because
1978 	 * there is an extra instance for every call of do_cmdline(), anyway.
1979 	 */
1980 	if (did_throw || need_rethrow)
1981 	    csp->exception = current_exception;
1982 	else
1983 	{
1984 	    csp->exception = NULL;
1985 	    if (did_emsg)
1986 	    {
1987 		force_abort |= cause_abort;
1988 		cause_abort = FALSE;
1989 	    }
1990 	}
1991 	did_emsg = got_int = did_throw = need_rethrow = FALSE;
1992 
1993 	/* Report if required by the 'verbose' option or when debugging.  */
1994 	report_make_pending(pending, csp->exception);
1995     }
1996     else
1997     {
1998 	csp->pending = CSTP_NONE;
1999 	csp->exception = NULL;
2000     }
2001 }
2002 
2003 /*
2004  * See comment above enter_cleanup() for how this function is used.
2005  *
2006  * This function is a bit like ex_endtry() except that there was not actually
2007  * an extra try block around the part that failed and an error or interrupt
2008  * had not (yet) been converted to an exception when the cleanup autocommand
2009  * sequence was invoked.
2010  *
2011  * This function has to be called with the address of the cleanup_T structure
2012  * filled by enter_cleanup() as an argument; it restores the error/interrupt/
2013  * exception state saved by that function - except there was an aborting
2014  * error, an interrupt or an uncaught exception during execution of the
2015  * cleanup autocommands.  In the latter case, the saved error/interrupt/
2016  * exception state is discarded.
2017  */
2018     void
2019 leave_cleanup(cleanup_T *csp)
2020 {
2021     int		pending = csp->pending;
2022 
2023     if (pending == CSTP_NONE)	/* nothing to do */
2024 	return;
2025 
2026     /* If there was an aborting error, an interrupt, or an uncaught exception
2027      * after the corresponding call to enter_cleanup(), discard what has been
2028      * made pending by it.  Report this to the user if required by the
2029      * 'verbose' option or when debugging. */
2030     if (aborting() || need_rethrow)
2031     {
2032 	if (pending & CSTP_THROW)
2033 	    /* Cancel the pending exception (includes report). */
2034 	    discard_exception((except_T *)csp->exception, FALSE);
2035 	else
2036 	    report_discard_pending(pending, NULL);
2037 
2038 	/* If an error was about to be converted to an exception when
2039 	 * enter_cleanup() was called, free the message list. */
2040 	if (msg_list != NULL)
2041 	    free_global_msglist();
2042     }
2043 
2044     /*
2045      * If there was no new error, interrupt, or throw between the calls
2046      * to enter_cleanup() and leave_cleanup(), restore the pending
2047      * error/interrupt/exception state.
2048      */
2049     else
2050     {
2051 	/*
2052 	 * If there was an exception being thrown when enter_cleanup() was
2053 	 * called, we need to rethrow it.  Make it the exception currently
2054 	 * being thrown.
2055 	 */
2056 	if (pending & CSTP_THROW)
2057 	    current_exception = csp->exception;
2058 
2059 	/*
2060 	 * If an error was about to be converted to an exception when
2061 	 * enter_cleanup() was called, let "cause_abort" take the part of
2062 	 * "force_abort" (as done by cause_errthrow()).
2063 	 */
2064 	else if (pending & CSTP_ERROR)
2065 	{
2066 	    cause_abort = force_abort;
2067 	    force_abort = FALSE;
2068 	}
2069 
2070 	/*
2071 	 * Restore the pending values of did_emsg, got_int, and did_throw.
2072 	 */
2073 	if (pending & CSTP_ERROR)
2074 	    did_emsg = TRUE;
2075 	if (pending & CSTP_INTERRUPT)
2076 	    got_int = TRUE;
2077 	if (pending & CSTP_THROW)
2078 	    need_rethrow = TRUE;    /* did_throw will be set by do_one_cmd() */
2079 
2080 	/* Report if required by the 'verbose' option or when debugging. */
2081 	report_resume_pending(pending,
2082 		   (pending & CSTP_THROW) ? (void *)current_exception : NULL);
2083     }
2084 }
2085 
2086 
2087 /*
2088  * Make conditionals inactive and discard what's pending in finally clauses
2089  * until the conditional type searched for or a try conditional not in its
2090  * finally clause is reached.  If this is in an active catch clause, finish
2091  * the caught exception.
2092  * Return the cstack index where the search stopped.
2093  * Values used for "searched_cond" are (CSF_WHILE | CSF_FOR) or CSF_TRY or 0,
2094  * the latter meaning the innermost try conditional not in its finally clause.
2095  * "inclusive" tells whether the conditional searched for should be made
2096  * inactive itself (a try conditional not in its finally clause possibly find
2097  * before is always made inactive).  If "inclusive" is TRUE and
2098  * "searched_cond" is CSF_TRY|CSF_SILENT, the saved former value of
2099  * "emsg_silent", if reset when the try conditional finally reached was
2100  * entered, is restored (used by ex_endtry()).  This is normally done only
2101  * when such a try conditional is left.
2102  */
2103     int
2104 cleanup_conditionals(
2105     struct condstack   *cstack,
2106     int		searched_cond,
2107     int		inclusive)
2108 {
2109     int		idx;
2110     int		stop = FALSE;
2111 
2112     for (idx = cstack->cs_idx; idx >= 0; --idx)
2113     {
2114 	if (cstack->cs_flags[idx] & CSF_TRY)
2115 	{
2116 	    /*
2117 	     * Discard anything pending in a finally clause and continue the
2118 	     * search.  There may also be a pending ":continue", ":break",
2119 	     * ":return", or ":finish" before the finally clause.  We must not
2120 	     * discard it, unless an error or interrupt occurred afterwards.
2121 	     */
2122 	    if (did_emsg || got_int || (cstack->cs_flags[idx] & CSF_FINALLY))
2123 	    {
2124 		switch (cstack->cs_pending[idx])
2125 		{
2126 		    case CSTP_NONE:
2127 			break;
2128 
2129 		    case CSTP_CONTINUE:
2130 		    case CSTP_BREAK:
2131 		    case CSTP_FINISH:
2132 			report_discard_pending(cstack->cs_pending[idx], NULL);
2133 			cstack->cs_pending[idx] = CSTP_NONE;
2134 			break;
2135 
2136 		    case CSTP_RETURN:
2137 			report_discard_pending(CSTP_RETURN,
2138 						      cstack->cs_rettv[idx]);
2139 			discard_pending_return(cstack->cs_rettv[idx]);
2140 			cstack->cs_pending[idx] = CSTP_NONE;
2141 			break;
2142 
2143 		    default:
2144 			if (cstack->cs_flags[idx] & CSF_FINALLY)
2145 			{
2146 			    if (cstack->cs_pending[idx] & CSTP_THROW)
2147 			    {
2148 				/* Cancel the pending exception.  This is in the
2149 				 * finally clause, so that the stack of the
2150 				 * caught exceptions is not involved. */
2151 				discard_exception((except_T *)
2152 					cstack->cs_exception[idx],
2153 					FALSE);
2154 			    }
2155 			    else
2156 				report_discard_pending(cstack->cs_pending[idx],
2157 					NULL);
2158 			    cstack->cs_pending[idx] = CSTP_NONE;
2159 			}
2160 			break;
2161 		}
2162 	    }
2163 
2164 	    /*
2165 	     * Stop at a try conditional not in its finally clause.  If this try
2166 	     * conditional is in an active catch clause, finish the caught
2167 	     * exception.
2168 	     */
2169 	    if (!(cstack->cs_flags[idx] & CSF_FINALLY))
2170 	    {
2171 		if ((cstack->cs_flags[idx] & CSF_ACTIVE)
2172 			&& (cstack->cs_flags[idx] & CSF_CAUGHT))
2173 		    finish_exception((except_T *)cstack->cs_exception[idx]);
2174 		/* Stop at this try conditional - except the try block never
2175 		 * got active (because of an inactive surrounding conditional
2176 		 * or when the ":try" appeared after an error or interrupt or
2177 		 * throw). */
2178 		if (cstack->cs_flags[idx] & CSF_TRUE)
2179 		{
2180 		    if (searched_cond == 0 && !inclusive)
2181 			break;
2182 		    stop = TRUE;
2183 		}
2184 	    }
2185 	}
2186 
2187 	/* Stop on the searched conditional type (even when the surrounding
2188 	 * conditional is not active or something has been made pending).
2189 	 * If "inclusive" is TRUE and "searched_cond" is CSF_TRY|CSF_SILENT,
2190 	 * check first whether "emsg_silent" needs to be restored. */
2191 	if (cstack->cs_flags[idx] & searched_cond)
2192 	{
2193 	    if (!inclusive)
2194 		break;
2195 	    stop = TRUE;
2196 	}
2197 	cstack->cs_flags[idx] &= ~CSF_ACTIVE;
2198 	if (stop && searched_cond != (CSF_TRY | CSF_SILENT))
2199 	    break;
2200 
2201 	/*
2202 	 * When leaving a try conditional that reset "emsg_silent" on its
2203 	 * entry after saving the original value, restore that value here and
2204 	 * free the memory used to store it.
2205 	 */
2206 	if ((cstack->cs_flags[idx] & CSF_TRY)
2207 		&& (cstack->cs_flags[idx] & CSF_SILENT))
2208 	{
2209 	    eslist_T	*elem;
2210 
2211 	    elem = cstack->cs_emsg_silent_list;
2212 	    cstack->cs_emsg_silent_list = elem->next;
2213 	    emsg_silent = elem->saved_emsg_silent;
2214 	    vim_free(elem);
2215 	    cstack->cs_flags[idx] &= ~CSF_SILENT;
2216 	}
2217 	if (stop)
2218 	    break;
2219     }
2220     return idx;
2221 }
2222 
2223 /*
2224  * Return an appropriate error message for a missing endwhile/endfor/endif.
2225  */
2226    static char_u *
2227 get_end_emsg(struct condstack *cstack)
2228 {
2229     if (cstack->cs_flags[cstack->cs_idx] & CSF_WHILE)
2230 	return e_endwhile;
2231     if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR)
2232 	return e_endfor;
2233     return e_endif;
2234 }
2235 
2236 
2237 /*
2238  * Rewind conditionals until index "idx" is reached.  "cond_type" and
2239  * "cond_level" specify a conditional type and the address of a level variable
2240  * which is to be decremented with each skipped conditional of the specified
2241  * type.
2242  * Also free "for info" structures where needed.
2243  */
2244     void
2245 rewind_conditionals(
2246     struct condstack   *cstack,
2247     int		idx,
2248     int		cond_type,
2249     int		*cond_level)
2250 {
2251     while (cstack->cs_idx > idx)
2252     {
2253 	if (cstack->cs_flags[cstack->cs_idx] & cond_type)
2254 	    --*cond_level;
2255 	if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR)
2256 	    free_for_info(cstack->cs_forinfo[cstack->cs_idx]);
2257 	--cstack->cs_idx;
2258     }
2259 }
2260 
2261 /*
2262  * ":endfunction" when not after a ":function"
2263  */
2264     void
2265 ex_endfunction(exarg_T *eap UNUSED)
2266 {
2267     EMSG(_("E193: :endfunction not inside a function"));
2268 }
2269 
2270 /*
2271  * Return TRUE if the string "p" looks like a ":while" or ":for" command.
2272  */
2273     int
2274 has_loop_cmd(char_u *p)
2275 {
2276     int		len;
2277 
2278     /* skip modifiers, white space and ':' */
2279     for (;;)
2280     {
2281 	while (*p == ' ' || *p == '\t' || *p == ':')
2282 	    ++p;
2283 	len = modifier_len(p);
2284 	if (len == 0)
2285 	    break;
2286 	p += len;
2287     }
2288     if ((p[0] == 'w' && p[1] == 'h')
2289 	    || (p[0] == 'f' && p[1] == 'o' && p[2] == 'r'))
2290 	return TRUE;
2291     return FALSE;
2292 }
2293 
2294 #endif /* FEAT_EVAL */
2295