1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
11 * ex_eval.c: functions for Ex command line for the +eval feature.
16 #if defined(FEAT_EVAL) || defined(PROTO)
18 static void free_msglist
__ARGS((struct msglist
*l
));
19 static int throw_exception
__ARGS((void *, int, char_u
*));
20 static char_u
*get_end_emsg
__ARGS((struct condstack
*cstack
));
23 * Exception handling terms:
25 * :try ":try" command \
27 * :catch RE ":catch" command |
28 * ... catch clause |- try conditional
29 * :finally ":finally" command |
30 * ... finally clause |
31 * :endtry ":endtry" command /
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.
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 asyncronously 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.
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"))
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
68 static void catch_exception
__ARGS((except_T
*excp
));
69 static void finish_exception
__ARGS((except_T
*excp
));
70 static void discard_exception
__ARGS((except_T
*excp
, int was_finished
));
71 static void report_pending
__ARGS((int action
, int pending
, void *value
));
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).
82 static int cause_abort
= FALSE
;
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.
98 return (did_emsg
&& force_abort
) || got_int
|| did_throw
;
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.
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.
121 should_abort(retcode
)
124 return ((retcode
== FAIL
&& trylevel
!= 0 && !emsg_silent
) || aborting());
128 * Return TRUE if a function with the "abort" flag should not be considered
129 * ended on an error. This means that parsing commands is continued in order
130 * to find finally clauses to be executed, and that some errors in skipped
131 * commands are still reported.
136 /* This function is only called after an error. In this case, "force_abort"
137 * determines whether searching for finally clauses is necessary. */
142 * cause_errthrow(): Cause a throw of an error exception if appropriate.
143 * Return TRUE if the error message should not be displayed by emsg().
144 * Sets "ignore", if the emsg() call should be ignored completely.
146 * When several messages appear in the same command, the first is usually the
147 * most specific one and used as the exception value. The "severe" flag can be
148 * set to TRUE, if a later but severer message should be used instead.
151 cause_errthrow(mesg
, severe
, ignore
)
156 struct msglist
*elem
;
157 struct msglist
**plist
;
160 * Do nothing when displaying the interrupt message or reporting an
161 * uncaught exception (which has already been discarded then) at the top
162 * level. Also when no exception can be thrown. The message will be
163 * displayed by emsg().
165 if (suppress_errthrow
)
169 * If emsg() has not been called previously, temporarily reset
170 * "force_abort" until the throw point for error messages has been
171 * reached. This ensures that aborting() returns the same value for all
172 * errors that appear in the same command. This means particularly that
173 * for parsing errors during expression evaluation emsg() will be called
174 * multiply, even when the expression is evaluated from a finally clause
175 * that was activated due to an aborting error, interrupt, or exception.
179 cause_abort
= force_abort
;
184 * If no try conditional is active and no exception is being thrown and
185 * there has not been an error in a try conditional or a throw so far, do
186 * nothing (for compatibility of non-EH scripts). The message will then
187 * be displayed by emsg(). When ":silent!" was used and we are not
188 * currently throwing an exception, do nothing. The message text will
189 * then be stored to v:errmsg by emsg() without displaying it.
191 if (((trylevel
== 0 && !cause_abort
) || emsg_silent
) && !did_throw
)
195 * Ignore an interrupt message when inside a try conditional or when an
196 * exception is being thrown or when an error in a try conditional or
197 * throw has been detected previously. This is important in order that an
198 * interrupt exception is catchable by the innermost try conditional and
199 * not replaced by an interrupt message error exception.
201 if (mesg
== (char_u
*)_(e_interr
))
208 * Ensure that all commands in nested function calls and sourced files
209 * are aborted immediately.
214 * When an exception is being thrown, some commands (like conditionals) are
215 * not skipped. Errors in those commands may affect what of the subsequent
216 * commands are regarded part of catch and finally clauses. Catching the
217 * exception would then cause execution of commands not intended by the
218 * user, who wouldn't even get aware of the problem. Therefor, discard the
219 * exception currently being thrown to prevent it from being caught. Just
220 * execute finally clauses and terminate.
224 /* When discarding an interrupt exception, reset got_int to prevent the
225 * same interrupt being converted to an exception again and discarding
226 * the error exception we are about to throw here. */
227 if (current_exception
->type
== ET_INTERRUPT
)
229 discard_current_exception();
236 * Print error message immediately without searching for a matching
237 * catch clause; just finally clauses are executed before the script
246 * Prepare the throw of an error exception, so that everything will
247 * be aborted (except for executing finally clauses), until the error
248 * exception is caught; if still uncaught at the top level, the error
249 * message will be displayed and the script processing terminated
250 * then. - This function has no access to the conditional stack.
251 * Thus, the actual throw is made after the failing command has
252 * returned. - Throw only the first of several errors in a row, except
253 * a severe error is following.
255 if (msg_list
!= NULL
)
258 while (*plist
!= NULL
)
259 plist
= &(*plist
)->next
;
261 elem
= (struct msglist
*)alloc((unsigned)sizeof(struct msglist
));
264 suppress_errthrow
= TRUE
;
269 elem
->msg
= vim_strsave(mesg
);
270 if (elem
->msg
== NULL
)
273 suppress_errthrow
= TRUE
;
279 elem
->throw_msg
= NULL
;
281 if (plist
== msg_list
|| severe
)
285 /* Skip the extra "Vim " prefix for message "E458". */
287 if (STRNCMP(tmsg
, "Vim E", 5) == 0
288 && VIM_ISDIGIT(tmsg
[5])
289 && VIM_ISDIGIT(tmsg
[6])
290 && VIM_ISDIGIT(tmsg
[7])
293 (*msg_list
)->throw_msg
= &tmsg
[4];
295 (*msg_list
)->throw_msg
= tmsg
;
305 * Free a "msg_list" and the messages it contains.
311 struct msglist
*messages
, *next
;
314 while (messages
!= NULL
)
316 next
= messages
->next
;
317 vim_free(messages
->msg
);
324 * Throw the message specified in the call to cause_errthrow() above as an
325 * error exception. If cstack is NULL, postpone the throw until do_cmdline()
326 * has returned (see do_one_cmd()).
329 do_errthrow(cstack
, cmdname
)
330 struct condstack
*cstack
;
334 * Ensure that all commands in nested function calls and sourced files
335 * are aborted immediately.
343 /* If no exception is to be thrown or the conversion should be done after
344 * returning to a previous invocation of do_one_cmd(), do nothing. */
345 if (msg_list
== NULL
|| *msg_list
== NULL
)
348 if (throw_exception(*msg_list
, ET_ERROR
, cmdname
) == FAIL
)
349 free_msglist(*msg_list
);
361 * do_intthrow(): Replace the current exception by an interrupt or interrupt
362 * exception if appropriate. Return TRUE if the current exception is discarded,
367 struct condstack
*cstack
;
370 * If no interrupt occurred or no try conditional is active and no exception
371 * is being thrown, do nothing (for compatibility of non-EH scripts).
373 if (!got_int
|| (trylevel
== 0 && !did_throw
))
376 #ifdef THROW_TEST /* avoid warning for condition always true */
377 if (!THROW_ON_INTERRUPT
)
380 * The interrupt aborts everything except for executing finally clauses.
381 * Discard any user or error or interrupt exception currently being
385 discard_current_exception();
391 * Throw an interrupt exception, so that everything will be aborted
392 * (except for executing finally clauses), until the interrupt exception
393 * is caught; if still uncaught at the top level, the script processing
394 * will be terminated then. - If an interrupt exception is already
395 * being thrown, do nothing.
400 if (current_exception
->type
== ET_INTERRUPT
)
403 /* An interrupt exception replaces any user or error exception. */
404 discard_current_exception();
406 if (throw_exception("Vim:Interrupt", ET_INTERRUPT
, NULL
) != FAIL
)
415 * Throw a new exception. Return FAIL when out of memory or it was tried to
416 * throw an illegal user exception. "value" is the exception string for a user
417 * or interrupt exception, or points to a message list in case of an error
421 throw_exception(value
, type
, cmdname
)
427 char_u
*p
, *mesg
, *val
;
431 * Disallow faking Interrupt or error exceptions as user exceptions. They
432 * would be treated differently from real interrupt or error exceptions when
433 * no active try block is found, see do_cmdline().
437 if (STRNCMP((char_u
*)value
, "Vim", 3) == 0 &&
438 (((char_u
*)value
)[3] == NUL
|| ((char_u
*)value
)[3] == ':' ||
439 ((char_u
*)value
)[3] == '('))
441 EMSG(_("E608: Cannot :throw exceptions with 'Vim' prefix"));
446 excp
= (except_T
*)alloc((unsigned)sizeof(except_T
));
450 if (type
== ET_ERROR
)
452 /* Store the original message and prefix the exception value with
453 * "Vim:" or, if a command name is given, "Vim(cmdname):". */
454 excp
->messages
= (struct msglist
*)value
;
455 mesg
= excp
->messages
->throw_msg
;
456 if (cmdname
!= NULL
&& *cmdname
!= NUL
)
458 cmdlen
= (int)STRLEN(cmdname
);
459 excp
->value
= vim_strnsave((char_u
*)"Vim(",
460 4 + cmdlen
+ 2 + (int)STRLEN(mesg
));
461 if (excp
->value
== NULL
)
463 STRCPY(&excp
->value
[4], cmdname
);
464 STRCPY(&excp
->value
[4 + cmdlen
], "):");
465 val
= excp
->value
+ 4 + cmdlen
+ 2;
469 excp
->value
= vim_strnsave((char_u
*)"Vim:", 4 + (int)STRLEN(mesg
));
470 if (excp
->value
== NULL
)
472 val
= excp
->value
+ 4;
475 /* msg_add_fname may have been used to prefix the message with a file
476 * name in quotes. In the exception value, put the file name in
477 * parentheses and move it to the end. */
478 for (p
= mesg
; ; p
++)
484 || (VIM_ISDIGIT(p
[2])
486 || (VIM_ISDIGIT(p
[3])
489 if (*p
== NUL
|| p
== mesg
)
490 STRCAT(val
, mesg
); /* 'E123' missing or at beginning */
493 /* '"filename" E123: message text' */
494 if (mesg
[0] != '"' || p
-2 < &mesg
[1] ||
495 p
[-2] != '"' || p
[-1] != ' ')
496 /* "E123:" is part of the file name. */
501 sprintf((char *)(val
+ STRLEN(p
)), " (%s)", &mesg
[1]);
512 excp
->throw_name
= vim_strsave(sourcing_name
== NULL
513 ? (char_u
*)"" : sourcing_name
);
514 if (excp
->throw_name
== NULL
)
516 if (type
== ET_ERROR
)
517 vim_free(excp
->value
);
520 excp
->throw_lnum
= sourcing_lnum
;
522 if (p_verbose
>= 13 || debug_break_level
> 0)
524 int save_msg_silent
= msg_silent
;
526 if (debug_break_level
> 0)
527 msg_silent
= FALSE
; /* display messages */
531 if (debug_break_level
> 0 || *p_vfile
== NUL
)
532 msg_scroll
= TRUE
; /* always scroll up, don't overwrite */
534 smsg((char_u
*)_("Exception thrown: %s"), excp
->value
);
535 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
537 if (debug_break_level
> 0 || *p_vfile
== NUL
)
538 cmdline_row
= msg_row
;
540 if (debug_break_level
> 0)
541 msg_silent
= save_msg_silent
;
546 current_exception
= excp
;
551 suppress_errthrow
= TRUE
;
554 current_exception
= NULL
;
559 * Discard an exception. "was_finished" is set when the exception has been
560 * caught and the catch clause has been ended normally.
563 discard_exception(excp
, was_finished
)
567 char_u
*saved_IObuff
;
575 if (p_verbose
>= 13 || debug_break_level
> 0)
577 int save_msg_silent
= msg_silent
;
579 saved_IObuff
= vim_strsave(IObuff
);
580 if (debug_break_level
> 0)
581 msg_silent
= FALSE
; /* display messages */
585 if (debug_break_level
> 0 || *p_vfile
== NUL
)
586 msg_scroll
= TRUE
; /* always scroll up, don't overwrite */
588 ? (char_u
*)_("Exception finished: %s")
589 : (char_u
*)_("Exception discarded: %s"),
591 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
592 if (debug_break_level
> 0 || *p_vfile
== NUL
)
593 cmdline_row
= msg_row
;
595 if (debug_break_level
> 0)
596 msg_silent
= save_msg_silent
;
599 STRCPY(IObuff
, saved_IObuff
);
600 vim_free(saved_IObuff
);
602 if (excp
->type
!= ET_INTERRUPT
)
603 vim_free(excp
->value
);
604 if (excp
->type
== ET_ERROR
)
605 free_msglist(excp
->messages
);
606 vim_free(excp
->throw_name
);
611 * Discard the exception currently being thrown.
614 discard_current_exception()
616 discard_exception(current_exception
, FALSE
);
617 current_exception
= NULL
;
619 need_rethrow
= FALSE
;
623 * Put an exception on the caught stack.
626 catch_exception(excp
)
629 excp
->caught
= caught_stack
;
631 set_vim_var_string(VV_EXCEPTION
, excp
->value
, -1);
632 if (*excp
->throw_name
!= NUL
)
634 if (excp
->throw_lnum
!= 0)
635 vim_snprintf((char *)IObuff
, IOSIZE
, _("%s, line %ld"),
636 excp
->throw_name
, (long)excp
->throw_lnum
);
638 vim_snprintf((char *)IObuff
, IOSIZE
, "%s", excp
->throw_name
);
639 set_vim_var_string(VV_THROWPOINT
, IObuff
, -1);
642 /* throw_name not set on an exception from a command that was typed. */
643 set_vim_var_string(VV_THROWPOINT
, NULL
, -1);
645 if (p_verbose
>= 13 || debug_break_level
> 0)
647 int save_msg_silent
= msg_silent
;
649 if (debug_break_level
> 0)
650 msg_silent
= FALSE
; /* display messages */
654 if (debug_break_level
> 0 || *p_vfile
== NUL
)
655 msg_scroll
= TRUE
; /* always scroll up, don't overwrite */
657 smsg((char_u
*)_("Exception caught: %s"), excp
->value
);
658 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
660 if (debug_break_level
> 0 || *p_vfile
== NUL
)
661 cmdline_row
= msg_row
;
663 if (debug_break_level
> 0)
664 msg_silent
= save_msg_silent
;
671 * Remove an exception from the caught stack.
674 finish_exception(excp
)
677 if (excp
!= caught_stack
)
679 caught_stack
= caught_stack
->caught
;
680 if (caught_stack
!= NULL
)
682 set_vim_var_string(VV_EXCEPTION
, caught_stack
->value
, -1);
683 if (*caught_stack
->throw_name
!= NUL
)
685 if (caught_stack
->throw_lnum
!= 0)
686 vim_snprintf((char *)IObuff
, IOSIZE
,
687 _("%s, line %ld"), caught_stack
->throw_name
,
688 (long)caught_stack
->throw_lnum
);
690 vim_snprintf((char *)IObuff
, IOSIZE
, "%s",
691 caught_stack
->throw_name
);
692 set_vim_var_string(VV_THROWPOINT
, IObuff
, -1);
695 /* throw_name not set on an exception from a command that was
697 set_vim_var_string(VV_THROWPOINT
, NULL
, -1);
701 set_vim_var_string(VV_EXCEPTION
, NULL
, -1);
702 set_vim_var_string(VV_THROWPOINT
, NULL
, -1);
705 /* Discard the exception, but use the finish message for 'verbose'. */
706 discard_exception(excp
, TRUE
);
710 * Flags specifying the message displayed by report_pending.
717 * Report information about something pending in a finally clause if required by
718 * the 'verbose' option or when debugging. "action" tells whether something is
719 * made pending or something pending is resumed or discarded. "pending" tells
720 * what is pending. "value" specifies the return value for a pending ":return"
721 * or the exception value for a pending exception.
724 report_pending(action
, pending
, value
)
737 mesg
= (char_u
*)_("%s made pending");
740 mesg
= (char_u
*)_("%s resumed");
742 /* case RP_DISCARD: */
744 mesg
= (char_u
*)_("%s discarded");
763 /* ":return" command producing value, allocated */
764 s
= (char *)get_return_cmd(value
);
768 if (pending
& CSTP_THROW
)
770 vim_snprintf((char *)IObuff
, IOSIZE
,
771 (char *)mesg
, _("Exception"));
772 mesg
= vim_strnsave(IObuff
, (int)STRLEN(IObuff
) + 4);
773 STRCAT(mesg
, ": %s");
774 s
= (char *)((except_T
*)value
)->value
;
776 else if ((pending
& CSTP_ERROR
) && (pending
& CSTP_INTERRUPT
))
777 s
= _("Error and interrupt");
778 else if (pending
& CSTP_ERROR
)
780 else /* if (pending & CSTP_INTERRUPT) */
784 save_msg_silent
= msg_silent
;
785 if (debug_break_level
> 0)
786 msg_silent
= FALSE
; /* display messages */
788 msg_scroll
= TRUE
; /* always scroll up, don't overwrite */
789 smsg(mesg
, (char_u
*)s
);
790 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
791 cmdline_row
= msg_row
;
793 if (debug_break_level
> 0)
794 msg_silent
= save_msg_silent
;
796 if (pending
== CSTP_RETURN
)
798 else if (pending
& CSTP_THROW
)
803 * If something is made pending in a finally clause, report it if required by
804 * the 'verbose' option or when debugging.
807 report_make_pending(pending
, value
)
811 if (p_verbose
>= 14 || debug_break_level
> 0)
813 if (debug_break_level
<= 0)
815 report_pending(RP_MAKE
, pending
, value
);
816 if (debug_break_level
<= 0)
822 * If something pending in a finally clause is resumed at the ":endtry", report
823 * it if required by the 'verbose' option or when debugging.
826 report_resume_pending(pending
, value
)
830 if (p_verbose
>= 14 || debug_break_level
> 0)
832 if (debug_break_level
<= 0)
834 report_pending(RP_RESUME
, pending
, value
);
835 if (debug_break_level
<= 0)
841 * If something pending in a finally clause is discarded, report it if required
842 * by the 'verbose' option or when debugging.
845 report_discard_pending(pending
, value
)
849 if (p_verbose
>= 14 || debug_break_level
> 0)
851 if (debug_break_level
<= 0)
853 report_pending(RP_DISCARD
, pending
, value
);
854 if (debug_break_level
<= 0)
870 struct condstack
*cstack
= eap
->cstack
;
872 if (cstack
->cs_idx
== CSTACK_LEN
- 1)
873 eap
->errmsg
= (char_u
*)N_("E579: :if nesting too deep");
877 cstack
->cs_flags
[cstack
->cs_idx
] = 0;
880 * Don't do something after an error, interrupt, or throw, or when there
881 * is a surrounding conditional and it was not active.
883 skip
= did_emsg
|| got_int
|| did_throw
|| (cstack
->cs_idx
> 0
884 && !(cstack
->cs_flags
[cstack
->cs_idx
- 1] & CSF_ACTIVE
));
886 result
= eval_to_bool(eap
->arg
, &error
, &eap
->nextcmd
, skip
);
891 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_ACTIVE
| CSF_TRUE
;
894 /* set TRUE, so this conditional will never get active */
895 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_TRUE
;
907 if (eap
->cstack
->cs_idx
< 0
908 || (eap
->cstack
->cs_flags
[eap
->cstack
->cs_idx
]
909 & (CSF_WHILE
| CSF_FOR
| CSF_TRY
)))
910 eap
->errmsg
= (char_u
*)N_("E580: :endif without :if");
914 * When debugging or a breakpoint was encountered, display the debug
915 * prompt (if not already done). This shows the user that an ":endif"
916 * is executed when the ":if" or a previous ":elseif" was not TRUE.
917 * Handle a ">quit" debug command as if an interrupt had occurred before
918 * the ":endif". That is, throw an interrupt exception if appropriate.
919 * Doing this here prevents an exception for a parsing error being
920 * discarded by throwing the interrupt exception later on.
922 if (!(eap
->cstack
->cs_flags
[eap
->cstack
->cs_idx
] & CSF_TRUE
)
923 && dbg_check_skipped(eap
))
924 (void)do_intthrow(eap
->cstack
);
926 --eap
->cstack
->cs_idx
;
931 * ":else" and ":elseif".
940 struct condstack
*cstack
= eap
->cstack
;
943 * Don't do something after an error, interrupt, or throw, or when there is
944 * a surrounding conditional and it was not active.
946 skip
= did_emsg
|| got_int
|| did_throw
|| (cstack
->cs_idx
> 0
947 && !(cstack
->cs_flags
[cstack
->cs_idx
- 1] & CSF_ACTIVE
));
949 if (cstack
->cs_idx
< 0
950 || (cstack
->cs_flags
[cstack
->cs_idx
]
951 & (CSF_WHILE
| CSF_FOR
| CSF_TRY
)))
953 if (eap
->cmdidx
== CMD_else
)
955 eap
->errmsg
= (char_u
*)N_("E581: :else without :if");
958 eap
->errmsg
= (char_u
*)N_("E582: :elseif without :if");
961 else if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_ELSE
)
963 if (eap
->cmdidx
== CMD_else
)
965 eap
->errmsg
= (char_u
*)N_("E583: multiple :else");
968 eap
->errmsg
= (char_u
*)N_("E584: :elseif after :else");
972 /* if skipping or the ":if" was TRUE, reset ACTIVE, otherwise set it */
973 if (skip
|| cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRUE
)
975 if (eap
->errmsg
== NULL
)
976 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_TRUE
;
977 skip
= TRUE
; /* don't evaluate an ":elseif" */
980 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_ACTIVE
;
983 * When debugging or a breakpoint was encountered, display the debug prompt
984 * (if not already done). This shows the user that an ":else" or ":elseif"
985 * is executed when the ":if" or previous ":elseif" was not TRUE. Handle
986 * a ">quit" debug command as if an interrupt had occurred before the
987 * ":else" or ":elseif". That is, set "skip" and throw an interrupt
988 * exception if appropriate. Doing this here prevents that an exception
989 * for a parsing errors is discarded when throwing the interrupt exception
992 if (!skip
&& dbg_check_skipped(eap
) && got_int
)
994 (void)do_intthrow(cstack
);
998 if (eap
->cmdidx
== CMD_elseif
)
1000 result
= eval_to_bool(eap
->arg
, &error
, &eap
->nextcmd
, skip
);
1001 /* When throwing error exceptions, we want to throw always the first
1002 * of several errors in a row. This is what actually happens when
1003 * a conditional error was detected above and there is another failure
1004 * when parsing the expression. Since the skip flag is set in this
1005 * case, the parsing error will be ignored by emsg(). */
1007 if (!skip
&& !error
)
1010 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_ACTIVE
| CSF_TRUE
;
1012 cstack
->cs_flags
[cstack
->cs_idx
] = 0;
1014 else if (eap
->errmsg
== NULL
)
1015 /* set TRUE, so this conditional will never get active */
1016 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_TRUE
;
1019 cstack
->cs_flags
[cstack
->cs_idx
] |= CSF_ELSE
;
1023 * Handle ":while" and ":for".
1032 struct condstack
*cstack
= eap
->cstack
;
1034 if (cstack
->cs_idx
== CSTACK_LEN
- 1)
1035 eap
->errmsg
= (char_u
*)N_("E585: :while/:for nesting too deep");
1039 * The loop flag is set when we have jumped back from the matching
1040 * ":endwhile" or ":endfor". When not set, need to initialise this
1043 if ((cstack
->cs_lflags
& CSL_HAD_LOOP
) == 0)
1046 ++cstack
->cs_looplevel
;
1047 cstack
->cs_line
[cstack
->cs_idx
] = -1;
1049 cstack
->cs_flags
[cstack
->cs_idx
] =
1050 eap
->cmdidx
== CMD_while
? CSF_WHILE
: CSF_FOR
;
1053 * Don't do something after an error, interrupt, or throw, or when
1054 * there is a surrounding conditional and it was not active.
1056 skip
= did_emsg
|| got_int
|| did_throw
|| (cstack
->cs_idx
> 0
1057 && !(cstack
->cs_flags
[cstack
->cs_idx
- 1] & CSF_ACTIVE
));
1058 if (eap
->cmdidx
== CMD_while
)
1061 * ":while bool-expr"
1063 result
= eval_to_bool(eap
->arg
, &error
, &eap
->nextcmd
, skip
);
1070 * ":for var in list-expr"
1072 if ((cstack
->cs_lflags
& CSL_HAD_LOOP
) != 0)
1074 /* Jumping here from a ":continue" or ":endfor": use the
1075 * previously evaluated list. */
1076 fi
= cstack
->cs_forinfo
[cstack
->cs_idx
];
1081 /* Evaluate the argument and get the info in a structure. */
1082 fi
= eval_for_line(eap
->arg
, &error
, &eap
->nextcmd
, skip
);
1083 cstack
->cs_forinfo
[cstack
->cs_idx
] = fi
;
1086 /* use the element at the start of the list and advance */
1087 if (!error
&& fi
!= NULL
&& !skip
)
1088 result
= next_for_item(fi
, eap
->arg
);
1095 cstack
->cs_forinfo
[cstack
->cs_idx
] = NULL
;
1100 * If this cstack entry was just initialised and is active, set the
1101 * loop flag, so do_cmdline() will set the line number in cs_line[].
1102 * If executing the command a second time, clear the loop flag.
1104 if (!skip
&& !error
&& result
)
1106 cstack
->cs_flags
[cstack
->cs_idx
] |= (CSF_ACTIVE
| CSF_TRUE
);
1107 cstack
->cs_lflags
^= CSL_HAD_LOOP
;
1111 cstack
->cs_lflags
&= ~CSL_HAD_LOOP
;
1112 /* If the ":while" evaluates to FALSE or ":for" is past the end of
1113 * the list, show the debug prompt at the ":endwhile"/":endfor" as
1114 * if there was a ":break" in a ":while"/":for" evaluating to
1116 if (!skip
&& !error
)
1117 cstack
->cs_flags
[cstack
->cs_idx
] |= CSF_TRUE
;
1130 struct condstack
*cstack
= eap
->cstack
;
1132 if (cstack
->cs_looplevel
<= 0 || cstack
->cs_idx
< 0)
1133 eap
->errmsg
= (char_u
*)N_("E586: :continue without :while or :for");
1136 /* Try to find the matching ":while". This might stop at a try
1137 * conditional not in its finally clause (which is then to be executed
1138 * next). Therefor, inactivate all conditionals except the ":while"
1139 * itself (if reached). */
1140 idx
= cleanup_conditionals(cstack
, CSF_WHILE
| CSF_FOR
, FALSE
);
1141 if (idx
>= 0 && (cstack
->cs_flags
[idx
] & (CSF_WHILE
| CSF_FOR
)))
1143 rewind_conditionals(cstack
, idx
, CSF_TRY
, &cstack
->cs_trylevel
);
1146 * Set CSL_HAD_CONT, so do_cmdline() will jump back to the
1147 * matching ":while".
1149 cstack
->cs_lflags
|= CSL_HAD_CONT
; /* let do_cmdline() handle it */
1153 /* If a try conditional not in its finally clause is reached first,
1154 * make the ":continue" pending for execution at the ":endtry". */
1155 cstack
->cs_pending
[idx
] = CSTP_CONTINUE
;
1156 report_make_pending(CSTP_CONTINUE
, NULL
);
1169 struct condstack
*cstack
= eap
->cstack
;
1171 if (cstack
->cs_looplevel
<= 0 || cstack
->cs_idx
< 0)
1172 eap
->errmsg
= (char_u
*)N_("E587: :break without :while or :for");
1175 /* Inactivate conditionals until the matching ":while" or a try
1176 * conditional not in its finally clause (which is then to be
1177 * executed next) is found. In the latter case, make the ":break"
1178 * pending for execution at the ":endtry". */
1179 idx
= cleanup_conditionals(cstack
, CSF_WHILE
| CSF_FOR
, TRUE
);
1180 if (idx
>= 0 && !(cstack
->cs_flags
[idx
] & (CSF_WHILE
| CSF_FOR
)))
1182 cstack
->cs_pending
[idx
] = CSTP_BREAK
;
1183 report_make_pending(CSTP_BREAK
, NULL
);
1189 * ":endwhile" and ":endfor"
1195 struct condstack
*cstack
= eap
->cstack
;
1201 if (eap
->cmdidx
== CMD_endwhile
)
1212 if (cstack
->cs_looplevel
<= 0 || cstack
->cs_idx
< 0)
1216 fl
= cstack
->cs_flags
[cstack
->cs_idx
];
1219 /* If we are in a ":while" or ":for" but used the wrong endloop
1220 * command, do not rewind to the next enclosing ":for"/":while". */
1222 eap
->errmsg
= (char_u
*)_("E732: Using :endfor with :while");
1223 else if (fl
& CSF_FOR
)
1224 eap
->errmsg
= (char_u
*)_("E733: Using :endwhile with :for");
1226 if (!(fl
& (CSF_WHILE
| CSF_FOR
)))
1228 if (!(fl
& CSF_TRY
))
1229 eap
->errmsg
= e_endif
;
1230 else if (fl
& CSF_FINALLY
)
1231 eap
->errmsg
= e_endtry
;
1232 /* Try to find the matching ":while" and report what's missing. */
1233 for (idx
= cstack
->cs_idx
; idx
> 0; --idx
)
1235 fl
= cstack
->cs_flags
[idx
];
1236 if ((fl
& CSF_TRY
) && !(fl
& CSF_FINALLY
))
1238 /* Give up at a try conditional not in its finally clause.
1239 * Ignore the ":endwhile"/":endfor". */
1246 /* Cleanup and rewind all contained (and unclosed) conditionals. */
1247 (void)cleanup_conditionals(cstack
, CSF_WHILE
| CSF_FOR
, FALSE
);
1248 rewind_conditionals(cstack
, idx
, CSF_TRY
, &cstack
->cs_trylevel
);
1252 * When debugging or a breakpoint was encountered, display the debug
1253 * prompt (if not already done). This shows the user that an
1254 * ":endwhile"/":endfor" is executed when the ":while" was not TRUE or
1255 * after a ":break". Handle a ">quit" debug command as if an
1256 * interrupt had occurred before the ":endwhile"/":endfor". That is,
1257 * throw an interrupt exception if appropriate. Doing this here
1258 * prevents that an exception for a parsing error is discarded when
1259 * throwing the interrupt exception later on.
1261 else if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRUE
1262 && !(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_ACTIVE
)
1263 && dbg_check_skipped(eap
))
1264 (void)do_intthrow(cstack
);
1267 * Set loop flag, so do_cmdline() will jump back to the matching
1268 * ":while" or ":for".
1270 cstack
->cs_lflags
|= CSL_HAD_ENDLOOP
;
1282 char_u
*arg
= eap
->arg
;
1285 if (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
1286 value
= eval_to_string_skip(arg
, &eap
->nextcmd
, eap
->skip
);
1293 /* On error or when an exception is thrown during argument evaluation, do
1295 if (!eap
->skip
&& value
!= NULL
)
1297 if (throw_exception(value
, ET_USER
, NULL
) == FAIL
)
1300 do_throw(eap
->cstack
);
1305 * Throw the current exception through the specified cstack. Common routine
1306 * for ":throw" (user exception) and error and interrupt exceptions. Also
1307 * used for rethrowing an uncaught exception.
1311 struct condstack
*cstack
;
1314 int inactivate_try
= FALSE
;
1317 * Cleanup and inactivate up to the next surrounding try conditional that
1318 * is not in its finally clause. Normally, do not inactivate the try
1319 * conditional itself, so that its ACTIVE flag can be tested below. But
1320 * if a previous error or interrupt has not been converted to an exception,
1321 * inactivate the try conditional, too, as if the conversion had been done,
1322 * and reset the did_emsg or got_int flag, so this won't happen again at
1323 * the next surrounding try conditional.
1325 #ifndef THROW_ON_ERROR_TRUE
1326 if (did_emsg
&& !THROW_ON_ERROR
)
1328 inactivate_try
= TRUE
;
1332 #ifndef THROW_ON_INTERRUPT_TRUE
1333 if (got_int
&& !THROW_ON_INTERRUPT
)
1335 inactivate_try
= TRUE
;
1339 idx
= cleanup_conditionals(cstack
, 0, inactivate_try
);
1343 * If this try conditional is active and we are before its first
1344 * ":catch", set THROWN so that the ":catch" commands will check
1345 * whether the exception matches. When the exception came from any of
1346 * the catch clauses, it will be made pending at the ":finally" (if
1347 * present) and rethrown at the ":endtry". This will also happen if
1348 * the try conditional is inactive. This is the case when we are
1349 * throwing an exception due to an error or interrupt on the way from
1350 * a preceding ":continue", ":break", ":return", ":finish", error or
1351 * interrupt (not converted to an exception) to the finally clause or
1352 * from a preceding throw of a user or error or interrupt exception to
1353 * the matching catch clause or the finally clause.
1355 if (!(cstack
->cs_flags
[idx
] & CSF_CAUGHT
))
1357 if (cstack
->cs_flags
[idx
] & CSF_ACTIVE
)
1358 cstack
->cs_flags
[idx
] |= CSF_THROWN
;
1360 /* THROWN may have already been set for a catchable exception
1361 * that has been discarded. Ensure it is reset for the new
1363 cstack
->cs_flags
[idx
] &= ~CSF_THROWN
;
1365 cstack
->cs_flags
[idx
] &= ~CSF_ACTIVE
;
1366 cstack
->cs_exception
[idx
] = current_exception
;
1369 /* TODO: Add optimization below. Not yet done because of interface
1370 * problems to eval.c and ex_cmds2.c. (Servatius) */
1374 * There are no catch clauses to check or finally clauses to execute.
1375 * End the current script or function. The exception will be rethrown
1378 if (getline_equal(eap
->getline
, eap
->cookie
, get_func_line
))
1379 current_funccal
->returned
= TRUE
;
1380 elseif (eap
->get_func_line
== getsourceline
)
1381 ((struct source_cookie
*)eap
->cookie
)->finished
= TRUE
;
1396 struct condstack
*cstack
= eap
->cstack
;
1398 if (cstack
->cs_idx
== CSTACK_LEN
- 1)
1399 eap
->errmsg
= (char_u
*)N_("E601: :try nesting too deep");
1403 ++cstack
->cs_trylevel
;
1404 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_TRY
;
1405 cstack
->cs_pending
[cstack
->cs_idx
] = CSTP_NONE
;
1408 * Don't do something after an error, interrupt, or throw, or when there
1409 * is a surrounding conditional and it was not active.
1411 skip
= did_emsg
|| got_int
|| did_throw
|| (cstack
->cs_idx
> 0
1412 && !(cstack
->cs_flags
[cstack
->cs_idx
- 1] & CSF_ACTIVE
));
1416 /* Set ACTIVE and TRUE. TRUE means that the corresponding ":catch"
1417 * commands should check for a match if an exception is thrown and
1418 * that the finally clause needs to be executed. */
1419 cstack
->cs_flags
[cstack
->cs_idx
] |= CSF_ACTIVE
| CSF_TRUE
;
1422 * ":silent!", even when used in a try conditional, disables
1423 * displaying of error messages and conversion of errors to
1424 * exceptions. When the silent commands again open a try
1425 * conditional, save "emsg_silent" and reset it so that errors are
1426 * again converted to exceptions. The value is restored when that
1427 * try conditional is left. If it is left normally, the commands
1428 * following the ":endtry" are again silent. If it is left by
1429 * a ":continue", ":break", ":return", or ":finish", the commands
1430 * executed next are again silent. If it is left due to an
1431 * aborting error, an interrupt, or an exception, restoring
1432 * "emsg_silent" does not matter since we are already in the
1433 * aborting state and/or the exception has already been thrown.
1434 * The effect is then just freeing the memory that was allocated
1435 * to save the value.
1441 elem
= (eslist_T
*)alloc((unsigned)sizeof(struct eslist_elem
));
1443 EMSG(_(e_outofmem
));
1446 elem
->saved_emsg_silent
= emsg_silent
;
1447 elem
->next
= cstack
->cs_emsg_silent_list
;
1448 cstack
->cs_emsg_silent_list
= elem
;
1449 cstack
->cs_flags
[cstack
->cs_idx
] |= CSF_SILENT
;
1459 * ":catch /{pattern}/" and ":catch"
1466 int give_up
= FALSE
;
1472 regmatch_T regmatch
;
1474 struct condstack
*cstack
= eap
->cstack
;
1477 if (cstack
->cs_trylevel
<= 0 || cstack
->cs_idx
< 0)
1479 eap
->errmsg
= (char_u
*)N_("E603: :catch without :try");
1484 if (!(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRY
))
1486 /* Report what's missing if the matching ":try" is not in its
1487 * finally clause. */
1488 eap
->errmsg
= get_end_emsg(cstack
);
1491 for (idx
= cstack
->cs_idx
; idx
> 0; --idx
)
1492 if (cstack
->cs_flags
[idx
] & CSF_TRY
)
1494 if (cstack
->cs_flags
[idx
] & CSF_FINALLY
)
1496 /* Give up for a ":catch" after ":finally" and ignore it.
1498 eap
->errmsg
= (char_u
*)N_("E604: :catch after :finally");
1502 rewind_conditionals(cstack
, idx
, CSF_WHILE
| CSF_FOR
,
1503 &cstack
->cs_looplevel
);
1506 if (ends_excmd(*eap
->arg
)) /* no argument, catch all errors */
1508 pat
= (char_u
*)".*";
1510 eap
->nextcmd
= find_nextcmd(eap
->arg
);
1515 end
= skip_regexp(pat
, *eap
->arg
, TRUE
, NULL
);
1521 * Don't do something when no exception has been thrown or when the
1522 * corresponding try block never got active (because of an inactive
1523 * surrounding conditional or after an error or interrupt or throw).
1525 if (!did_throw
|| !(cstack
->cs_flags
[idx
] & CSF_TRUE
))
1529 * Check for a match only if an exception is thrown but not caught by
1530 * a previous ":catch". An exception that has replaced a discarded
1531 * exception is not checked (THROWN is not set then).
1533 if (!skip
&& (cstack
->cs_flags
[idx
] & CSF_THROWN
)
1534 && !(cstack
->cs_flags
[idx
] & CSF_CAUGHT
))
1536 if (end
!= NULL
&& *end
!= NUL
&& !ends_excmd(*skipwhite(end
+ 1)))
1538 EMSG(_(e_trailing
));
1542 /* When debugging or a breakpoint was encountered, display the
1543 * debug prompt (if not already done) before checking for a match.
1544 * This is a helpful hint for the user when the regular expression
1545 * matching fails. Handle a ">quit" debug command as if an
1546 * interrupt had occurred before the ":catch". That is, discard
1547 * the original exception, replace it by an interrupt exception,
1548 * and don't catch it in this try block. */
1549 if (!dbg_check_skipped(eap
) || !do_intthrow(cstack
))
1551 /* Terminate the pattern and avoid the 'l' flag in 'cpoptions'
1552 * while compiling it. */
1559 p_cpo
= (char_u
*)"";
1560 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
1561 regmatch
.rm_ic
= FALSE
;
1565 if (regmatch
.regprog
== NULL
)
1566 EMSG2(_(e_invarg2
), pat
);
1570 * Save the value of got_int and reset it. We don't want
1571 * a previous interruption cancel matching, only hitting
1572 * CTRL-C while matching should abort it.
1574 prev_got_int
= got_int
;
1576 caught
= vim_regexec_nl(®match
, current_exception
->value
,
1578 got_int
|= prev_got_int
;
1579 vim_free(regmatch
.regprog
);
1586 /* Make this ":catch" clause active and reset did_emsg, got_int,
1587 * and did_throw. Put the exception on the caught stack. */
1588 cstack
->cs_flags
[idx
] |= CSF_ACTIVE
| CSF_CAUGHT
;
1589 did_emsg
= got_int
= did_throw
= FALSE
;
1590 catch_exception((except_T
*)cstack
->cs_exception
[idx
]);
1591 /* It's mandatory that the current exception is stored in the cstack
1592 * so that it can be discarded at the next ":catch", ":finally", or
1593 * ":endtry" or when the catch clause is left by a ":continue",
1594 * ":break", ":return", ":finish", error, interrupt, or another
1596 if (cstack
->cs_exception
[cstack
->cs_idx
] != current_exception
)
1597 EMSG(_(e_internal
));
1602 * If there is a preceding catch clause and it caught the exception,
1603 * finish the exception now. This happens also after errors except
1604 * when this ":catch" was after the ":finally" or not within
1605 * a ":try". Make the try conditional inactive so that the
1606 * following catch clauses are skipped. On an error or interrupt
1607 * after the preceding try block or catch clause was left by
1608 * a ":continue", ":break", ":return", or ":finish", discard the
1611 cleanup_conditionals(cstack
, CSF_TRY
, TRUE
);
1616 eap
->nextcmd
= find_nextcmd(end
);
1628 int pending
= CSTP_NONE
;
1629 struct condstack
*cstack
= eap
->cstack
;
1631 if (cstack
->cs_trylevel
<= 0 || cstack
->cs_idx
< 0)
1632 eap
->errmsg
= (char_u
*)N_("E606: :finally without :try");
1635 if (!(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRY
))
1637 eap
->errmsg
= get_end_emsg(cstack
);
1638 for (idx
= cstack
->cs_idx
- 1; idx
> 0; --idx
)
1639 if (cstack
->cs_flags
[idx
] & CSF_TRY
)
1641 /* Make this error pending, so that the commands in the following
1642 * finally clause can be executed. This overrules also a pending
1643 * ":continue", ":break", ":return", or ":finish". */
1644 pending
= CSTP_ERROR
;
1647 idx
= cstack
->cs_idx
;
1649 if (cstack
->cs_flags
[idx
] & CSF_FINALLY
)
1651 /* Give up for a multiple ":finally" and ignore it. */
1652 eap
->errmsg
= (char_u
*)N_("E607: multiple :finally");
1655 rewind_conditionals(cstack
, idx
, CSF_WHILE
| CSF_FOR
,
1656 &cstack
->cs_looplevel
);
1659 * Don't do something when the corresponding try block never got active
1660 * (because of an inactive surrounding conditional or after an error or
1661 * interrupt or throw) or for a ":finally" without ":try" or a multiple
1662 * ":finally". After every other error (did_emsg or the conditional
1663 * errors detected above) or after an interrupt (got_int) or an
1664 * exception (did_throw), the finally clause must be executed.
1666 skip
= !(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRUE
);
1670 /* When debugging or a breakpoint was encountered, display the
1671 * debug prompt (if not already done). The user then knows that the
1672 * finally clause is executed. */
1673 if (dbg_check_skipped(eap
))
1675 /* Handle a ">quit" debug command as if an interrupt had
1676 * occurred before the ":finally". That is, discard the
1677 * original exception and replace it by an interrupt
1679 (void)do_intthrow(cstack
);
1683 * If there is a preceding catch clause and it caught the exception,
1684 * finish the exception now. This happens also after errors except
1685 * when this is a multiple ":finally" or one not within a ":try".
1686 * After an error or interrupt, this also discards a pending
1687 * ":continue", ":break", ":finish", or ":return" from the preceding
1688 * try block or catch clause.
1690 cleanup_conditionals(cstack
, CSF_TRY
, FALSE
);
1693 * Make did_emsg, got_int, did_throw pending. If set, they overrule
1694 * a pending ":continue", ":break", ":return", or ":finish". Then
1695 * we have particularly to discard a pending return value (as done
1696 * by the call to cleanup_conditionals() above when did_emsg or
1697 * got_int is set). The pending values are restored by the
1698 * ":endtry", except if there is a new error, interrupt, exception,
1699 * ":continue", ":break", ":return", or ":finish" in the following
1700 * finally clause. A missing ":endwhile", ":endfor" or ":endif"
1701 * detected here is treated as if did_emsg and did_throw had
1702 * already been set, respectively in case that the error is not
1703 * converted to an exception, did_throw had already been unset.
1704 * We must not set did_emsg here since that would suppress the
1707 if (pending
== CSTP_ERROR
|| did_emsg
|| got_int
|| did_throw
)
1709 if (cstack
->cs_pending
[cstack
->cs_idx
] == CSTP_RETURN
)
1711 report_discard_pending(CSTP_RETURN
,
1712 cstack
->cs_rettv
[cstack
->cs_idx
]);
1713 discard_pending_return(cstack
->cs_rettv
[cstack
->cs_idx
]);
1715 if (pending
== CSTP_ERROR
&& !did_emsg
)
1716 pending
|= (THROW_ON_ERROR
) ? CSTP_THROW
: 0;
1718 pending
|= did_throw
? CSTP_THROW
: 0;
1719 pending
|= did_emsg
? CSTP_ERROR
: 0;
1720 pending
|= got_int
? CSTP_INTERRUPT
: 0;
1721 cstack
->cs_pending
[cstack
->cs_idx
] = pending
;
1723 /* It's mandatory that the current exception is stored in the
1724 * cstack so that it can be rethrown at the ":endtry" or be
1725 * discarded if the finally clause is left by a ":continue",
1726 * ":break", ":return", ":finish", error, interrupt, or another
1727 * exception. When emsg() is called for a missing ":endif" or
1728 * a missing ":endwhile"/":endfor" detected here, the
1729 * exception will be discarded. */
1730 if (did_throw
&& cstack
->cs_exception
[cstack
->cs_idx
]
1731 != current_exception
)
1732 EMSG(_(e_internal
));
1736 * Set CSL_HAD_FINA, so do_cmdline() will reset did_emsg,
1737 * got_int, and did_throw and make the finally clause active.
1738 * This will happen after emsg() has been called for a missing
1739 * ":endif" or a missing ":endwhile"/":endfor" detected here, so
1740 * that the following finally clause will be executed even then.
1742 cstack
->cs_lflags
|= CSL_HAD_FINA
;
1756 int rethrow
= FALSE
;
1757 int pending
= CSTP_NONE
;
1759 struct condstack
*cstack
= eap
->cstack
;
1761 if (cstack
->cs_trylevel
<= 0 || cstack
->cs_idx
< 0)
1762 eap
->errmsg
= (char_u
*)N_("E602: :endtry without :try");
1766 * Don't do something after an error, interrupt or throw in the try
1767 * block, catch clause, or finally clause preceding this ":endtry" or
1768 * when an error or interrupt occurred after a ":continue", ":break",
1769 * ":return", or ":finish" in a try block or catch clause preceding this
1770 * ":endtry" or when the try block never got active (because of an
1771 * inactive surrounding conditional or after an error or interrupt or
1772 * throw) or when there is a surrounding conditional and it has been
1773 * made inactive by a ":continue", ":break", ":return", or ":finish" in
1774 * the finally clause. The latter case need not be tested since then
1775 * anything pending has already been discarded. */
1776 skip
= did_emsg
|| got_int
|| did_throw
||
1777 !(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRUE
);
1779 if (!(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRY
))
1781 eap
->errmsg
= get_end_emsg(cstack
);
1782 /* Find the matching ":try" and report what's missing. */
1783 idx
= cstack
->cs_idx
;
1786 while (idx
> 0 && !(cstack
->cs_flags
[idx
] & CSF_TRY
));
1787 rewind_conditionals(cstack
, idx
, CSF_WHILE
| CSF_FOR
,
1788 &cstack
->cs_looplevel
);
1792 * If an exception is being thrown, discard it to prevent it from
1793 * being rethrown at the end of this function. It would be
1794 * discarded by the error message, anyway. Resets did_throw.
1795 * This does not affect the script termination due to the error
1796 * since "trylevel" is decremented after emsg() has been called.
1799 discard_current_exception();
1803 idx
= cstack
->cs_idx
;
1806 * If we stopped with the exception currently being thrown at this
1807 * try conditional since we didn't know that it doesn't have
1808 * a finally clause, we need to rethrow it after closing the try
1811 if (did_throw
&& (cstack
->cs_flags
[idx
] & CSF_TRUE
)
1812 && !(cstack
->cs_flags
[idx
] & CSF_FINALLY
))
1816 /* If there was no finally clause, show the user when debugging or
1817 * a breakpoint was encountered that the end of the try conditional has
1818 * been reached: display the debug prompt (if not already done). Do
1819 * this on normal control flow or when an exception was thrown, but not
1820 * on an interrupt or error not converted to an exception or when
1821 * a ":break", ":continue", ":return", or ":finish" is pending. These
1822 * actions are carried out immediately.
1824 if ((rethrow
|| (!skip
1825 && !(cstack
->cs_flags
[idx
] & CSF_FINALLY
)
1826 && !cstack
->cs_pending
[idx
]))
1827 && dbg_check_skipped(eap
))
1829 /* Handle a ">quit" debug command as if an interrupt had occurred
1830 * before the ":endtry". That is, throw an interrupt exception and
1831 * set "skip" and "rethrow". */
1835 (void)do_intthrow(cstack
);
1836 /* The do_intthrow() call may have reset did_throw or
1837 * cstack->cs_pending[idx].*/
1839 if (did_throw
&& !(cstack
->cs_flags
[idx
] & CSF_FINALLY
))
1845 * If a ":return" is pending, we need to resume it after closing the
1846 * try conditional; remember the return value. If there was a finally
1847 * clause making an exception pending, we need to rethrow it. Make it
1848 * the exception currently being thrown.
1852 pending
= cstack
->cs_pending
[idx
];
1853 cstack
->cs_pending
[idx
] = CSTP_NONE
;
1854 if (pending
== CSTP_RETURN
)
1855 rettv
= cstack
->cs_rettv
[idx
];
1856 else if (pending
& CSTP_THROW
)
1857 current_exception
= cstack
->cs_exception
[idx
];
1861 * Discard anything pending on an error, interrupt, or throw in the
1862 * finally clause. If there was no ":finally", discard a pending
1863 * ":continue", ":break", ":return", or ":finish" if an error or
1864 * interrupt occurred afterwards, but before the ":endtry" was reached.
1865 * If an exception was caught by the last of the catch clauses and there
1866 * was no finally clause, finish the exception now. This happens also
1867 * after errors except when this ":endtry" is not within a ":try".
1868 * Restore "emsg_silent" if it has been reset by this try conditional.
1870 (void)cleanup_conditionals(cstack
, CSF_TRY
| CSF_SILENT
, TRUE
);
1873 --cstack
->cs_trylevel
;
1877 report_resume_pending(pending
,
1878 (pending
== CSTP_RETURN
) ? rettv
:
1879 (pending
& CSTP_THROW
) ? (void *)current_exception
: NULL
);
1885 /* Reactivate a pending ":continue", ":break", ":return",
1886 * ":finish" from the try block or a catch clause of this try
1887 * conditional. This is skipped, if there was an error in an
1888 * (unskipped) conditional command or an interrupt afterwards
1889 * or if the finally clause is present and executed a new error,
1890 * interrupt, throw, ":continue", ":break", ":return", or
1899 do_return(eap
, FALSE
, FALSE
, rettv
);
1902 do_finish(eap
, FALSE
);
1905 /* When the finally clause was entered due to an error,
1906 * interrupt or throw (as opposed to a ":continue", ":break",
1907 * ":return", or ":finish"), restore the pending values of
1908 * did_emsg, got_int, and did_throw. This is skipped, if there
1909 * was a new error, interrupt, throw, ":continue", ":break",
1910 * ":return", or ":finish". in the finally clause. */
1912 if (pending
& CSTP_ERROR
)
1914 if (pending
& CSTP_INTERRUPT
)
1916 if (pending
& CSTP_THROW
)
1923 /* Rethrow the current exception (within this cstack). */
1929 * enter_cleanup() and leave_cleanup()
1931 * Functions to be called before/after invoking a sequence of autocommands for
1932 * cleanup for a failed command. (Failure means here that a call to emsg()
1933 * has been made, an interrupt occurred, or there is an uncaught exception
1934 * from a previous autocommand execution of the same command.)
1936 * Call enter_cleanup() with a pointer to a cleanup_T and pass the same
1937 * pointer to leave_cleanup(). The cleanup_T structure stores the pending
1938 * error/interrupt/exception state.
1942 * This function works a bit like ex_finally() except that there was not
1943 * actually an extra try block around the part that failed and an error or
1944 * interrupt has not (yet) been converted to an exception. This function
1945 * saves the error/interrupt/ exception state and prepares for the call to
1946 * do_cmdline() that is going to be made for the cleanup autocommand
1953 int pending
= CSTP_NONE
;
1956 * Postpone did_emsg, got_int, did_throw. The pending values will be
1957 * restored by leave_cleanup() except if there was an aborting error,
1958 * interrupt, or uncaught exception after this function ends.
1960 if (did_emsg
|| got_int
|| did_throw
|| need_rethrow
)
1962 csp
->pending
= (did_emsg
? CSTP_ERROR
: 0)
1963 | (got_int
? CSTP_INTERRUPT
: 0)
1964 | (did_throw
? CSTP_THROW
: 0)
1965 | (need_rethrow
? CSTP_THROW
: 0);
1967 /* If we are currently throwing an exception (did_throw), save it as
1968 * well. On an error not yet converted to an exception, update
1969 * "force_abort" and reset "cause_abort" (as do_errthrow() would do).
1970 * This is needed for the do_cmdline() call that is going to be made
1971 * for autocommand execution. We need not save *msg_list because
1972 * there is an extra instance for every call of do_cmdline(), anyway.
1974 if (did_throw
|| need_rethrow
)
1975 csp
->exception
= current_exception
;
1978 csp
->exception
= NULL
;
1981 force_abort
|= cause_abort
;
1982 cause_abort
= FALSE
;
1985 did_emsg
= got_int
= did_throw
= need_rethrow
= FALSE
;
1987 /* Report if required by the 'verbose' option or when debugging. */
1988 report_make_pending(pending
, csp
->exception
);
1992 csp
->pending
= CSTP_NONE
;
1993 csp
->exception
= NULL
;
1998 * See comment above enter_cleanup() for how this function is used.
2000 * This function is a bit like ex_endtry() except that there was not actually
2001 * an extra try block around the part that failed and an error or interrupt
2002 * had not (yet) been converted to an exception when the cleanup autocommand
2003 * sequence was invoked.
2005 * This function has to be called with the address of the cleanup_T structure
2006 * filled by enter_cleanup() as an argument; it restores the error/interrupt/
2007 * exception state saved by that function - except there was an aborting
2008 * error, an interrupt or an uncaught exception during execution of the
2009 * cleanup autocommands. In the latter case, the saved error/interrupt/
2010 * exception state is discarded.
2016 int pending
= csp
->pending
;
2018 if (pending
== CSTP_NONE
) /* nothing to do */
2021 /* If there was an aborting error, an interrupt, or an uncaught exception
2022 * after the corresponding call to enter_cleanup(), discard what has been
2023 * made pending by it. Report this to the user if required by the
2024 * 'verbose' option or when debugging. */
2025 if (aborting() || need_rethrow
)
2027 if (pending
& CSTP_THROW
)
2028 /* Cancel the pending exception (includes report). */
2029 discard_exception((except_T
*)csp
->exception
, FALSE
);
2031 report_discard_pending(pending
, NULL
);
2033 /* If an error was about to be converted to an exception when
2034 * enter_cleanup() was called, free the message list. */
2035 if (msg_list
!= NULL
)
2037 free_msglist(*msg_list
);
2043 * If there was no new error, interrupt, or throw between the calls
2044 * to enter_cleanup() and leave_cleanup(), restore the pending
2045 * error/interrupt/exception state.
2050 * If there was an exception being thrown when enter_cleanup() was
2051 * called, we need to rethrow it. Make it the exception currently
2054 if (pending
& CSTP_THROW
)
2055 current_exception
= csp
->exception
;
2058 * If an error was about to be converted to an exception when
2059 * enter_cleanup() was called, let "cause_abort" take the part of
2060 * "force_abort" (as done by cause_errthrow()).
2062 else if (pending
& CSTP_ERROR
)
2064 cause_abort
= force_abort
;
2065 force_abort
= FALSE
;
2069 * Restore the pending values of did_emsg, got_int, and did_throw.
2071 if (pending
& CSTP_ERROR
)
2073 if (pending
& CSTP_INTERRUPT
)
2075 if (pending
& CSTP_THROW
)
2076 need_rethrow
= TRUE
; /* did_throw will be set by do_one_cmd() */
2078 /* Report if required by the 'verbose' option or when debugging. */
2079 report_resume_pending(pending
,
2080 (pending
& CSTP_THROW
) ? (void *)current_exception
: NULL
);
2086 * Make conditionals inactive and discard what's pending in finally clauses
2087 * until the conditional type searched for or a try conditional not in its
2088 * finally clause is reached. If this is in an active catch clause, finish
2089 * the caught exception.
2090 * Return the cstack index where the search stopped.
2091 * Values used for "searched_cond" are (CSF_WHILE | CSF_FOR) or CSF_TRY or 0,
2092 * the latter meaning the innermost try conditional not in its finally clause.
2093 * "inclusive" tells whether the conditional searched for should be made
2094 * inactive itself (a try conditional not in its finally claused possibly find
2095 * before is always made inactive). If "inclusive" is TRUE and
2096 * "searched_cond" is CSF_TRY|CSF_SILENT, the saved former value of
2097 * "emsg_silent", if reset when the try conditional finally reached was
2098 * entered, is restored (unsed by ex_endtry()). This is normally done only
2099 * when such a try conditional is left.
2102 cleanup_conditionals(cstack
, searched_cond
, inclusive
)
2103 struct condstack
*cstack
;
2110 for (idx
= cstack
->cs_idx
; idx
>= 0; --idx
)
2112 if (cstack
->cs_flags
[idx
] & CSF_TRY
)
2115 * Discard anything pending in a finally clause and continue the
2116 * search. There may also be a pending ":continue", ":break",
2117 * ":return", or ":finish" before the finally clause. We must not
2118 * discard it, unless an error or interrupt occurred afterwards.
2120 if (did_emsg
|| got_int
|| (cstack
->cs_flags
[idx
] & CSF_FINALLY
))
2122 switch (cstack
->cs_pending
[idx
])
2130 report_discard_pending(cstack
->cs_pending
[idx
], NULL
);
2131 cstack
->cs_pending
[idx
] = CSTP_NONE
;
2135 report_discard_pending(CSTP_RETURN
,
2136 cstack
->cs_rettv
[idx
]);
2137 discard_pending_return(cstack
->cs_rettv
[idx
]);
2138 cstack
->cs_pending
[idx
] = CSTP_NONE
;
2142 if (cstack
->cs_flags
[idx
] & CSF_FINALLY
)
2144 if (cstack
->cs_pending
[idx
] & CSTP_THROW
)
2146 /* Cancel the pending exception. This is in the
2147 * finally clause, so that the stack of the
2148 * caught exceptions is not involved. */
2149 discard_exception((except_T
*)
2150 cstack
->cs_exception
[idx
],
2154 report_discard_pending(cstack
->cs_pending
[idx
],
2156 cstack
->cs_pending
[idx
] = CSTP_NONE
;
2163 * Stop at a try conditional not in its finally clause. If this try
2164 * conditional is in an active catch clause, finish the caught
2167 if (!(cstack
->cs_flags
[idx
] & CSF_FINALLY
))
2169 if ((cstack
->cs_flags
[idx
] & CSF_ACTIVE
)
2170 && (cstack
->cs_flags
[idx
] & CSF_CAUGHT
))
2171 finish_exception((except_T
*)cstack
->cs_exception
[idx
]);
2172 /* Stop at this try conditional - except the try block never
2173 * got active (because of an inactive surrounding conditional
2174 * or when the ":try" appeared after an error or interrupt or
2176 if (cstack
->cs_flags
[idx
] & CSF_TRUE
)
2178 if (searched_cond
== 0 && !inclusive
)
2185 /* Stop on the searched conditional type (even when the surrounding
2186 * conditional is not active or something has been made pending).
2187 * If "inclusive" is TRUE and "searched_cond" is CSF_TRY|CSF_SILENT,
2188 * check first whether "emsg_silent" needs to be restored. */
2189 if (cstack
->cs_flags
[idx
] & searched_cond
)
2195 cstack
->cs_flags
[idx
] &= ~CSF_ACTIVE
;
2196 if (stop
&& searched_cond
!= (CSF_TRY
| CSF_SILENT
))
2200 * When leaving a try conditional that reset "emsg_silent" on its
2201 * entry after saving the original value, restore that value here and
2202 * free the memory used to store it.
2204 if ((cstack
->cs_flags
[idx
] & CSF_TRY
)
2205 && (cstack
->cs_flags
[idx
] & CSF_SILENT
))
2209 elem
= cstack
->cs_emsg_silent_list
;
2210 cstack
->cs_emsg_silent_list
= elem
->next
;
2211 emsg_silent
= elem
->saved_emsg_silent
;
2213 cstack
->cs_flags
[idx
] &= ~CSF_SILENT
;
2222 * Return an appropriate error message for a missing endwhile/endfor/endif.
2225 get_end_emsg(cstack
)
2226 struct condstack
*cstack
;
2228 if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_WHILE
)
2230 if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_FOR
)
2237 * Rewind conditionals until index "idx" is reached. "cond_type" and
2238 * "cond_level" specify a conditional type and the address of a level variable
2239 * which is to be decremented with each skipped conditional of the specified
2241 * Also free "for info" structures where needed.
2244 rewind_conditionals(cstack
, idx
, cond_type
, cond_level
)
2245 struct condstack
*cstack
;
2250 while (cstack
->cs_idx
> idx
)
2252 if (cstack
->cs_flags
[cstack
->cs_idx
] & cond_type
)
2254 if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_FOR
)
2255 free_for_info(cstack
->cs_forinfo
[cstack
->cs_idx
]);
2261 * ":endfunction" when not after a ":function"
2265 exarg_T
*eap UNUSED
;
2267 EMSG(_("E193: :endfunction not inside a function"));
2271 * Return TRUE if the string "p" looks like a ":while" or ":for" command.
2279 /* skip modifiers, white space and ':' */
2282 while (*p
== ' ' || *p
== '\t' || *p
== ':')
2284 len
= modifier_len(p
);
2289 if ((p
[0] == 'w' && p
[1] == 'h')
2290 || (p
[0] == 'f' && p
[1] == 'o' && p
[2] == 'r'))
2295 #endif /* FEAT_EVAL */