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_INTERRUPT TRUE
66 static void catch_exception
__ARGS((except_T
*excp
));
67 static void finish_exception
__ARGS((except_T
*excp
));
68 static void discard_exception
__ARGS((except_T
*excp
, int was_finished
));
69 static void report_pending
__ARGS((int action
, int pending
, void *value
));
72 * When several errors appear in a row, setting "force_abort" is delayed until
73 * the failing command returned. "cause_abort" is set to TRUE meanwhile, in
74 * order to indicate that situation. This is useful when "force_abort" was set
75 * during execution of a function call from an expression: the aborting of the
76 * expression evaluation is done without producing any error messages, but all
77 * error messages on parsing errors during the expression evaluation are given
78 * (even if a try conditional is active).
80 static int cause_abort
= FALSE
;
83 * Return TRUE when immediately aborting on error, or when an interrupt
84 * occurred or an exception was thrown but not caught. Use for ":{range}call"
85 * to check whether an aborted function that does not handle a range itself
86 * should be called again for the next line in the range. Also used for
87 * cancelling expression evaluation after a function call caused an immediate
88 * abort. Note that the first emsg() call temporarily resets "force_abort"
89 * until the throw point for error messages has been reached. That is, during
90 * cancellation of an expression evaluation after an aborting function call or
91 * due to a parsing error, aborting() always returns the same value.
96 return (did_emsg
&& force_abort
) || got_int
|| did_throw
;
100 * The value of "force_abort" is temporarily reset by the first emsg() call
101 * during an expression evaluation, and "cause_abort" is used instead. It might
102 * be necessary to restore "force_abort" even before the throw point for the
103 * error message has been reached. update_force_abort() should be called then.
113 * Return TRUE if a command with a subcommand resulting in "retcode" should
114 * abort the script processing. Can be used to suppress an autocommand after
115 * execution of a failing subcommand as long as the error message has not been
116 * displayed and actually caused the abortion.
119 should_abort(retcode
)
122 return ((retcode
== FAIL
&& trylevel
!= 0 && !emsg_silent
) || aborting());
126 * Return TRUE if a function with the "abort" flag should not be considered
127 * ended on an error. This means that parsing commands is continued in order
128 * to find finally clauses to be executed, and that some errors in skipped
129 * commands are still reported.
134 /* This function is only called after an error. In this case, "force_abort"
135 * determines whether searching for finally clauses is necessary. */
140 * cause_errthrow(): Cause a throw of an error exception if appropriate.
141 * Return TRUE if the error message should not be displayed by emsg().
142 * Sets "ignore", if the emsg() call should be ignored completely.
144 * When several messages appear in the same command, the first is usually the
145 * most specific one and used as the exception value. The "severe" flag can be
146 * set to TRUE, if a later but severer message should be used instead.
149 cause_errthrow(mesg
, severe
, ignore
)
154 struct msglist
*elem
;
155 struct msglist
**plist
;
158 * Do nothing when displaying the interrupt message or reporting an
159 * uncaught exception (which has already been discarded then) at the top
160 * level. Also when no exception can be thrown. The message will be
161 * displayed by emsg().
163 if (suppress_errthrow
)
167 * If emsg() has not been called previously, temporarily reset
168 * "force_abort" until the throw point for error messages has been
169 * reached. This ensures that aborting() returns the same value for all
170 * errors that appear in the same command. This means particularly that
171 * for parsing errors during expression evaluation emsg() will be called
172 * multiply, even when the expression is evaluated from a finally clause
173 * that was activated due to an aborting error, interrupt, or exception.
177 cause_abort
= force_abort
;
182 * If no try conditional is active and no exception is being thrown and
183 * there has not been an error in a try conditional or a throw so far, do
184 * nothing (for compatibility of non-EH scripts). The message will then
185 * be displayed by emsg(). When ":silent!" was used and we are not
186 * currently throwing an exception, do nothing. The message text will
187 * then be stored to v:errmsg by emsg() without displaying it.
189 if (((trylevel
== 0 && !cause_abort
) || emsg_silent
) && !did_throw
)
193 * Ignore an interrupt message when inside a try conditional or when an
194 * exception is being thrown or when an error in a try conditional or
195 * throw has been detected previously. This is important in order that an
196 * interrupt exception is catchable by the innermost try conditional and
197 * not replaced by an interrupt message error exception.
199 if (mesg
== (char_u
*)_(e_interr
))
206 * Ensure that all commands in nested function calls and sourced files
207 * are aborted immediately.
212 * When an exception is being thrown, some commands (like conditionals) are
213 * not skipped. Errors in those commands may affect what of the subsequent
214 * commands are regarded part of catch and finally clauses. Catching the
215 * exception would then cause execution of commands not intended by the
216 * user, who wouldn't even get aware of the problem. Therefor, discard the
217 * exception currently being thrown to prevent it from being caught. Just
218 * execute finally clauses and terminate.
222 /* When discarding an interrupt exception, reset got_int to prevent the
223 * same interrupt being converted to an exception again and discarding
224 * the error exception we are about to throw here. */
225 if (current_exception
->type
== ET_INTERRUPT
)
227 discard_current_exception();
234 * Print error message immediately without searching for a matching
235 * catch clause; just finally clauses are executed before the script
244 * Prepare the throw of an error exception, so that everything will
245 * be aborted (except for executing finally clauses), until the error
246 * exception is caught; if still uncaught at the top level, the error
247 * message will be displayed and the script processing terminated
248 * then. - This function has no access to the conditional stack.
249 * Thus, the actual throw is made after the failing command has
250 * returned. - Throw only the first of several errors in a row, except
251 * a severe error is following.
253 if (msg_list
!= NULL
)
256 while (*plist
!= NULL
)
257 plist
= &(*plist
)->next
;
259 elem
= (struct msglist
*)alloc((unsigned)sizeof(struct msglist
));
262 suppress_errthrow
= TRUE
;
267 elem
->msg
= vim_strsave(mesg
);
268 if (elem
->msg
== NULL
)
271 suppress_errthrow
= TRUE
;
277 elem
->throw_msg
= NULL
;
279 if (plist
== msg_list
|| severe
)
283 /* Skip the extra "Vim " prefix for message "E458". */
285 if (STRNCMP(tmsg
, "Vim E", 5) == 0
286 && VIM_ISDIGIT(tmsg
[5])
287 && VIM_ISDIGIT(tmsg
[6])
288 && VIM_ISDIGIT(tmsg
[7])
291 (*msg_list
)->throw_msg
= &tmsg
[4];
293 (*msg_list
)->throw_msg
= tmsg
;
303 * Free a "msg_list" and the messages it contains.
309 struct msglist
*messages
, *next
;
312 while (messages
!= NULL
)
314 next
= messages
->next
;
315 vim_free(messages
->msg
);
322 * Throw the message specified in the call to cause_errthrow() above as an
323 * error exception. If cstack is NULL, postpone the throw until do_cmdline()
324 * has returned (see do_one_cmd()).
327 do_errthrow(cstack
, cmdname
)
328 struct condstack
*cstack
;
332 * Ensure that all commands in nested function calls and sourced files
333 * are aborted immediately.
341 /* If no exception is to be thrown or the conversion should be done after
342 * returning to a previous invocation of do_one_cmd(), do nothing. */
343 if (msg_list
== NULL
|| *msg_list
== NULL
)
346 if (throw_exception(*msg_list
, ET_ERROR
, cmdname
) == FAIL
)
347 free_msglist(*msg_list
);
359 * do_intthrow(): Replace the current exception by an interrupt or interrupt
360 * exception if appropriate. Return TRUE if the current exception is discarded,
365 struct condstack
*cstack
;
368 * If no interrupt occurred or no try conditional is active and no exception
369 * is being thrown, do nothing (for compatibility of non-EH scripts).
371 if (!got_int
|| (trylevel
== 0 && !did_throw
))
374 #ifdef THROW_TEST /* avoid warning for condition always true */
375 if (!THROW_ON_INTERRUPT
)
378 * The interrupt aborts everything except for executing finally clauses.
379 * Discard any user or error or interrupt exception currently being
383 discard_current_exception();
389 * Throw an interrupt exception, so that everything will be aborted
390 * (except for executing finally clauses), until the interrupt exception
391 * is caught; if still uncaught at the top level, the script processing
392 * will be terminated then. - If an interrupt exception is already
393 * being thrown, do nothing.
398 if (current_exception
->type
== ET_INTERRUPT
)
401 /* An interrupt exception replaces any user or error exception. */
402 discard_current_exception();
404 if (throw_exception("Vim:Interrupt", ET_INTERRUPT
, NULL
) != FAIL
)
413 * Throw a new exception. Return FAIL when out of memory or it was tried to
414 * throw an illegal user exception. "value" is the exception string for a user
415 * or interrupt exception, or points to a message list in case of an error
419 throw_exception(value
, type
, cmdname
)
425 char_u
*p
, *mesg
, *val
;
429 * Disallow faking Interrupt or error exceptions as user exceptions. They
430 * would be treated differently from real interrupt or error exceptions when
431 * no active try block is found, see do_cmdline().
435 if (STRNCMP((char_u
*)value
, "Vim", 3) == 0 &&
436 (((char_u
*)value
)[3] == NUL
|| ((char_u
*)value
)[3] == ':' ||
437 ((char_u
*)value
)[3] == '('))
439 EMSG(_("E608: Cannot :throw exceptions with 'Vim' prefix"));
444 excp
= (except_T
*)alloc((unsigned)sizeof(except_T
));
448 if (type
== ET_ERROR
)
450 /* Store the original message and prefix the exception value with
451 * "Vim:" or, if a command name is given, "Vim(cmdname):". */
452 excp
->messages
= (struct msglist
*)value
;
453 mesg
= excp
->messages
->throw_msg
;
454 if (cmdname
!= NULL
&& *cmdname
!= NUL
)
456 cmdlen
= (int)STRLEN(cmdname
);
457 excp
->value
= vim_strnsave((char_u
*)"Vim(",
458 4 + cmdlen
+ 2 + (int)STRLEN(mesg
));
459 if (excp
->value
== NULL
)
461 STRCPY(&excp
->value
[4], cmdname
);
462 STRCPY(&excp
->value
[4 + cmdlen
], "):");
463 val
= excp
->value
+ 4 + cmdlen
+ 2;
467 excp
->value
= vim_strnsave((char_u
*)"Vim:", 4 + (int)STRLEN(mesg
));
468 if (excp
->value
== NULL
)
470 val
= excp
->value
+ 4;
473 /* msg_add_fname may have been used to prefix the message with a file
474 * name in quotes. In the exception value, put the file name in
475 * parentheses and move it to the end. */
476 for (p
= mesg
; ; p
++)
482 || (VIM_ISDIGIT(p
[2])
484 || (VIM_ISDIGIT(p
[3])
487 if (*p
== NUL
|| p
== mesg
)
488 STRCAT(val
, mesg
); /* 'E123' missing or at beginning */
491 /* '"filename" E123: message text' */
492 if (mesg
[0] != '"' || p
-2 < &mesg
[1] ||
493 p
[-2] != '"' || p
[-1] != ' ')
494 /* "E123:" is part of the file name. */
499 sprintf((char *)(val
+ STRLEN(p
)), " (%s)", &mesg
[1]);
510 excp
->throw_name
= vim_strsave(sourcing_name
== NULL
511 ? (char_u
*)"" : sourcing_name
);
512 if (excp
->throw_name
== NULL
)
514 if (type
== ET_ERROR
)
515 vim_free(excp
->value
);
518 excp
->throw_lnum
= sourcing_lnum
;
520 if (p_verbose
>= 13 || debug_break_level
> 0)
522 int save_msg_silent
= msg_silent
;
524 if (debug_break_level
> 0)
525 msg_silent
= FALSE
; /* display messages */
529 if (debug_break_level
> 0 || *p_vfile
== NUL
)
530 msg_scroll
= TRUE
; /* always scroll up, don't overwrite */
532 smsg((char_u
*)_("Exception thrown: %s"), excp
->value
);
533 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
535 if (debug_break_level
> 0 || *p_vfile
== NUL
)
536 cmdline_row
= msg_row
;
538 if (debug_break_level
> 0)
539 msg_silent
= save_msg_silent
;
544 current_exception
= excp
;
549 suppress_errthrow
= TRUE
;
552 current_exception
= NULL
;
557 * Discard an exception. "was_finished" is set when the exception has been
558 * caught and the catch clause has been ended normally.
561 discard_exception(excp
, was_finished
)
565 char_u
*saved_IObuff
;
573 if (p_verbose
>= 13 || debug_break_level
> 0)
575 int save_msg_silent
= msg_silent
;
577 saved_IObuff
= vim_strsave(IObuff
);
578 if (debug_break_level
> 0)
579 msg_silent
= FALSE
; /* display messages */
583 if (debug_break_level
> 0 || *p_vfile
== NUL
)
584 msg_scroll
= TRUE
; /* always scroll up, don't overwrite */
586 ? (char_u
*)_("Exception finished: %s")
587 : (char_u
*)_("Exception discarded: %s"),
589 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
590 if (debug_break_level
> 0 || *p_vfile
== NUL
)
591 cmdline_row
= msg_row
;
593 if (debug_break_level
> 0)
594 msg_silent
= save_msg_silent
;
597 STRCPY(IObuff
, saved_IObuff
);
598 vim_free(saved_IObuff
);
600 if (excp
->type
!= ET_INTERRUPT
)
601 vim_free(excp
->value
);
602 if (excp
->type
== ET_ERROR
)
603 free_msglist(excp
->messages
);
604 vim_free(excp
->throw_name
);
609 * Discard the exception currently being thrown.
612 discard_current_exception()
614 discard_exception(current_exception
, FALSE
);
615 current_exception
= NULL
;
617 need_rethrow
= FALSE
;
621 * Put an exception on the caught stack.
624 catch_exception(excp
)
627 excp
->caught
= caught_stack
;
629 set_vim_var_string(VV_EXCEPTION
, excp
->value
, -1);
630 if (*excp
->throw_name
!= NUL
)
632 if (excp
->throw_lnum
!= 0)
633 vim_snprintf((char *)IObuff
, IOSIZE
, _("%s, line %ld"),
634 excp
->throw_name
, (long)excp
->throw_lnum
);
636 vim_snprintf((char *)IObuff
, IOSIZE
, "%s", excp
->throw_name
);
637 set_vim_var_string(VV_THROWPOINT
, IObuff
, -1);
640 /* throw_name not set on an exception from a command that was typed. */
641 set_vim_var_string(VV_THROWPOINT
, NULL
, -1);
643 if (p_verbose
>= 13 || debug_break_level
> 0)
645 int save_msg_silent
= msg_silent
;
647 if (debug_break_level
> 0)
648 msg_silent
= FALSE
; /* display messages */
652 if (debug_break_level
> 0 || *p_vfile
== NUL
)
653 msg_scroll
= TRUE
; /* always scroll up, don't overwrite */
655 smsg((char_u
*)_("Exception caught: %s"), excp
->value
);
656 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
658 if (debug_break_level
> 0 || *p_vfile
== NUL
)
659 cmdline_row
= msg_row
;
661 if (debug_break_level
> 0)
662 msg_silent
= save_msg_silent
;
669 * Remove an exception from the caught stack.
672 finish_exception(excp
)
675 if (excp
!= caught_stack
)
677 caught_stack
= caught_stack
->caught
;
678 if (caught_stack
!= NULL
)
680 set_vim_var_string(VV_EXCEPTION
, caught_stack
->value
, -1);
681 if (*caught_stack
->throw_name
!= NUL
)
683 if (caught_stack
->throw_lnum
!= 0)
684 vim_snprintf((char *)IObuff
, IOSIZE
,
685 _("%s, line %ld"), caught_stack
->throw_name
,
686 (long)caught_stack
->throw_lnum
);
688 vim_snprintf((char *)IObuff
, IOSIZE
, "%s",
689 caught_stack
->throw_name
);
690 set_vim_var_string(VV_THROWPOINT
, IObuff
, -1);
693 /* throw_name not set on an exception from a command that was
695 set_vim_var_string(VV_THROWPOINT
, NULL
, -1);
699 set_vim_var_string(VV_EXCEPTION
, NULL
, -1);
700 set_vim_var_string(VV_THROWPOINT
, NULL
, -1);
703 /* Discard the exception, but use the finish message for 'verbose'. */
704 discard_exception(excp
, TRUE
);
708 * Flags specifying the message displayed by report_pending.
715 * Report information about something pending in a finally clause if required by
716 * the 'verbose' option or when debugging. "action" tells whether something is
717 * made pending or something pending is resumed or discarded. "pending" tells
718 * what is pending. "value" specifies the return value for a pending ":return"
719 * or the exception value for a pending exception.
722 report_pending(action
, pending
, value
)
735 mesg
= (char_u
*)_("%s made pending");
738 mesg
= (char_u
*)_("%s resumed");
740 /* case RP_DISCARD: */
742 mesg
= (char_u
*)_("%s discarded");
761 /* ":return" command producing value, allocated */
762 s
= (char *)get_return_cmd(value
);
766 if (pending
& CSTP_THROW
)
768 vim_snprintf((char *)IObuff
, IOSIZE
,
769 (char *)mesg
, _("Exception"));
770 mesg
= vim_strnsave(IObuff
, (int)STRLEN(IObuff
) + 4);
771 STRCAT(mesg
, ": %s");
772 s
= (char *)((except_T
*)value
)->value
;
774 else if ((pending
& CSTP_ERROR
) && (pending
& CSTP_INTERRUPT
))
775 s
= _("Error and interrupt");
776 else if (pending
& CSTP_ERROR
)
778 else /* if (pending & CSTP_INTERRUPT) */
782 save_msg_silent
= msg_silent
;
783 if (debug_break_level
> 0)
784 msg_silent
= FALSE
; /* display messages */
786 msg_scroll
= TRUE
; /* always scroll up, don't overwrite */
787 smsg(mesg
, (char_u
*)s
);
788 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
789 cmdline_row
= msg_row
;
791 if (debug_break_level
> 0)
792 msg_silent
= save_msg_silent
;
794 if (pending
== CSTP_RETURN
)
796 else if (pending
& CSTP_THROW
)
801 * If something is made pending in a finally clause, report it if required by
802 * the 'verbose' option or when debugging.
805 report_make_pending(pending
, value
)
809 if (p_verbose
>= 14 || debug_break_level
> 0)
811 if (debug_break_level
<= 0)
813 report_pending(RP_MAKE
, pending
, value
);
814 if (debug_break_level
<= 0)
820 * If something pending in a finally clause is resumed at the ":endtry", report
821 * it if required by the 'verbose' option or when debugging.
824 report_resume_pending(pending
, value
)
828 if (p_verbose
>= 14 || debug_break_level
> 0)
830 if (debug_break_level
<= 0)
832 report_pending(RP_RESUME
, pending
, value
);
833 if (debug_break_level
<= 0)
839 * If something pending in a finally clause is discarded, report it if required
840 * by the 'verbose' option or when debugging.
843 report_discard_pending(pending
, value
)
847 if (p_verbose
>= 14 || debug_break_level
> 0)
849 if (debug_break_level
<= 0)
851 report_pending(RP_DISCARD
, pending
, value
);
852 if (debug_break_level
<= 0)
868 struct condstack
*cstack
= eap
->cstack
;
870 if (cstack
->cs_idx
== CSTACK_LEN
- 1)
871 eap
->errmsg
= (char_u
*)N_("E579: :if nesting too deep");
875 cstack
->cs_flags
[cstack
->cs_idx
] = 0;
878 * Don't do something after an error, interrupt, or throw, or when there
879 * is a surrounding conditional and it was not active.
881 skip
= did_emsg
|| got_int
|| did_throw
|| (cstack
->cs_idx
> 0
882 && !(cstack
->cs_flags
[cstack
->cs_idx
- 1] & CSF_ACTIVE
));
884 result
= eval_to_bool(eap
->arg
, &error
, &eap
->nextcmd
, skip
);
889 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_ACTIVE
| CSF_TRUE
;
892 /* set TRUE, so this conditional will never get active */
893 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_TRUE
;
905 if (eap
->cstack
->cs_idx
< 0
906 || (eap
->cstack
->cs_flags
[eap
->cstack
->cs_idx
]
907 & (CSF_WHILE
| CSF_FOR
| CSF_TRY
)))
908 eap
->errmsg
= (char_u
*)N_("E580: :endif without :if");
912 * When debugging or a breakpoint was encountered, display the debug
913 * prompt (if not already done). This shows the user that an ":endif"
914 * is executed when the ":if" or a previous ":elseif" was not TRUE.
915 * Handle a ">quit" debug command as if an interrupt had occurred before
916 * the ":endif". That is, throw an interrupt exception if appropriate.
917 * Doing this here prevents an exception for a parsing error being
918 * discarded by throwing the interrupt exception later on.
920 if (!(eap
->cstack
->cs_flags
[eap
->cstack
->cs_idx
] & CSF_TRUE
)
921 && dbg_check_skipped(eap
))
922 (void)do_intthrow(eap
->cstack
);
924 --eap
->cstack
->cs_idx
;
929 * ":else" and ":elseif".
938 struct condstack
*cstack
= eap
->cstack
;
941 * Don't do something after an error, interrupt, or throw, or when there is
942 * a surrounding conditional and it was not active.
944 skip
= did_emsg
|| got_int
|| did_throw
|| (cstack
->cs_idx
> 0
945 && !(cstack
->cs_flags
[cstack
->cs_idx
- 1] & CSF_ACTIVE
));
947 if (cstack
->cs_idx
< 0
948 || (cstack
->cs_flags
[cstack
->cs_idx
]
949 & (CSF_WHILE
| CSF_FOR
| CSF_TRY
)))
951 if (eap
->cmdidx
== CMD_else
)
953 eap
->errmsg
= (char_u
*)N_("E581: :else without :if");
956 eap
->errmsg
= (char_u
*)N_("E582: :elseif without :if");
959 else if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_ELSE
)
961 if (eap
->cmdidx
== CMD_else
)
963 eap
->errmsg
= (char_u
*)N_("E583: multiple :else");
966 eap
->errmsg
= (char_u
*)N_("E584: :elseif after :else");
970 /* if skipping or the ":if" was TRUE, reset ACTIVE, otherwise set it */
971 if (skip
|| cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRUE
)
973 if (eap
->errmsg
== NULL
)
974 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_TRUE
;
975 skip
= TRUE
; /* don't evaluate an ":elseif" */
978 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_ACTIVE
;
981 * When debugging or a breakpoint was encountered, display the debug prompt
982 * (if not already done). This shows the user that an ":else" or ":elseif"
983 * is executed when the ":if" or previous ":elseif" was not TRUE. Handle
984 * a ">quit" debug command as if an interrupt had occurred before the
985 * ":else" or ":elseif". That is, set "skip" and throw an interrupt
986 * exception if appropriate. Doing this here prevents that an exception
987 * for a parsing errors is discarded when throwing the interrupt exception
990 if (!skip
&& dbg_check_skipped(eap
) && got_int
)
992 (void)do_intthrow(cstack
);
996 if (eap
->cmdidx
== CMD_elseif
)
998 result
= eval_to_bool(eap
->arg
, &error
, &eap
->nextcmd
, skip
);
999 /* When throwing error exceptions, we want to throw always the first
1000 * of several errors in a row. This is what actually happens when
1001 * a conditional error was detected above and there is another failure
1002 * when parsing the expression. Since the skip flag is set in this
1003 * case, the parsing error will be ignored by emsg(). */
1005 if (!skip
&& !error
)
1008 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_ACTIVE
| CSF_TRUE
;
1010 cstack
->cs_flags
[cstack
->cs_idx
] = 0;
1012 else if (eap
->errmsg
== NULL
)
1013 /* set TRUE, so this conditional will never get active */
1014 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_TRUE
;
1017 cstack
->cs_flags
[cstack
->cs_idx
] |= CSF_ELSE
;
1021 * Handle ":while" and ":for".
1030 struct condstack
*cstack
= eap
->cstack
;
1032 if (cstack
->cs_idx
== CSTACK_LEN
- 1)
1033 eap
->errmsg
= (char_u
*)N_("E585: :while/:for nesting too deep");
1037 * The loop flag is set when we have jumped back from the matching
1038 * ":endwhile" or ":endfor". When not set, need to initialise this
1041 if ((cstack
->cs_lflags
& CSL_HAD_LOOP
) == 0)
1044 ++cstack
->cs_looplevel
;
1045 cstack
->cs_line
[cstack
->cs_idx
] = -1;
1047 cstack
->cs_flags
[cstack
->cs_idx
] =
1048 eap
->cmdidx
== CMD_while
? CSF_WHILE
: CSF_FOR
;
1051 * Don't do something after an error, interrupt, or throw, or when
1052 * there is a surrounding conditional and it was not active.
1054 skip
= did_emsg
|| got_int
|| did_throw
|| (cstack
->cs_idx
> 0
1055 && !(cstack
->cs_flags
[cstack
->cs_idx
- 1] & CSF_ACTIVE
));
1056 if (eap
->cmdidx
== CMD_while
)
1059 * ":while bool-expr"
1061 result
= eval_to_bool(eap
->arg
, &error
, &eap
->nextcmd
, skip
);
1068 * ":for var in list-expr"
1070 if ((cstack
->cs_lflags
& CSL_HAD_LOOP
) != 0)
1072 /* Jumping here from a ":continue" or ":endfor": use the
1073 * previously evaluated list. */
1074 fi
= cstack
->cs_forinfo
[cstack
->cs_idx
];
1079 /* Evaluate the argument and get the info in a structure. */
1080 fi
= eval_for_line(eap
->arg
, &error
, &eap
->nextcmd
, skip
);
1081 cstack
->cs_forinfo
[cstack
->cs_idx
] = fi
;
1084 /* use the element at the start of the list and advance */
1085 if (!error
&& fi
!= NULL
&& !skip
)
1086 result
= next_for_item(fi
, eap
->arg
);
1093 cstack
->cs_forinfo
[cstack
->cs_idx
] = NULL
;
1098 * If this cstack entry was just initialised and is active, set the
1099 * loop flag, so do_cmdline() will set the line number in cs_line[].
1100 * If executing the command a second time, clear the loop flag.
1102 if (!skip
&& !error
&& result
)
1104 cstack
->cs_flags
[cstack
->cs_idx
] |= (CSF_ACTIVE
| CSF_TRUE
);
1105 cstack
->cs_lflags
^= CSL_HAD_LOOP
;
1109 cstack
->cs_lflags
&= ~CSL_HAD_LOOP
;
1110 /* If the ":while" evaluates to FALSE or ":for" is past the end of
1111 * the list, show the debug prompt at the ":endwhile"/":endfor" as
1112 * if there was a ":break" in a ":while"/":for" evaluating to
1114 if (!skip
&& !error
)
1115 cstack
->cs_flags
[cstack
->cs_idx
] |= CSF_TRUE
;
1128 struct condstack
*cstack
= eap
->cstack
;
1130 if (cstack
->cs_looplevel
<= 0 || cstack
->cs_idx
< 0)
1131 eap
->errmsg
= (char_u
*)N_("E586: :continue without :while or :for");
1134 /* Try to find the matching ":while". This might stop at a try
1135 * conditional not in its finally clause (which is then to be executed
1136 * next). Therefor, inactivate all conditionals except the ":while"
1137 * itself (if reached). */
1138 idx
= cleanup_conditionals(cstack
, CSF_WHILE
| CSF_FOR
, FALSE
);
1139 if (idx
>= 0 && (cstack
->cs_flags
[idx
] & (CSF_WHILE
| CSF_FOR
)))
1141 rewind_conditionals(cstack
, idx
, CSF_TRY
, &cstack
->cs_trylevel
);
1144 * Set CSL_HAD_CONT, so do_cmdline() will jump back to the
1145 * matching ":while".
1147 cstack
->cs_lflags
|= CSL_HAD_CONT
; /* let do_cmdline() handle it */
1151 /* If a try conditional not in its finally clause is reached first,
1152 * make the ":continue" pending for execution at the ":endtry". */
1153 cstack
->cs_pending
[idx
] = CSTP_CONTINUE
;
1154 report_make_pending(CSTP_CONTINUE
, NULL
);
1167 struct condstack
*cstack
= eap
->cstack
;
1169 if (cstack
->cs_looplevel
<= 0 || cstack
->cs_idx
< 0)
1170 eap
->errmsg
= (char_u
*)N_("E587: :break without :while or :for");
1173 /* Inactivate conditionals until the matching ":while" or a try
1174 * conditional not in its finally clause (which is then to be
1175 * executed next) is found. In the latter case, make the ":break"
1176 * pending for execution at the ":endtry". */
1177 idx
= cleanup_conditionals(cstack
, CSF_WHILE
| CSF_FOR
, TRUE
);
1178 if (idx
>= 0 && !(cstack
->cs_flags
[idx
] & (CSF_WHILE
| CSF_FOR
)))
1180 cstack
->cs_pending
[idx
] = CSTP_BREAK
;
1181 report_make_pending(CSTP_BREAK
, NULL
);
1187 * ":endwhile" and ":endfor"
1193 struct condstack
*cstack
= eap
->cstack
;
1199 if (eap
->cmdidx
== CMD_endwhile
)
1210 if (cstack
->cs_looplevel
<= 0 || cstack
->cs_idx
< 0)
1214 fl
= cstack
->cs_flags
[cstack
->cs_idx
];
1217 /* If we are in a ":while" or ":for" but used the wrong endloop
1218 * command, do not rewind to the next enclosing ":for"/":while". */
1220 eap
->errmsg
= (char_u
*)_("E732: Using :endfor with :while");
1221 else if (fl
& CSF_FOR
)
1222 eap
->errmsg
= (char_u
*)_("E733: Using :endwhile with :for");
1224 if (!(fl
& (CSF_WHILE
| CSF_FOR
)))
1226 if (!(fl
& CSF_TRY
))
1227 eap
->errmsg
= e_endif
;
1228 else if (fl
& CSF_FINALLY
)
1229 eap
->errmsg
= e_endtry
;
1230 /* Try to find the matching ":while" and report what's missing. */
1231 for (idx
= cstack
->cs_idx
; idx
> 0; --idx
)
1233 fl
= cstack
->cs_flags
[idx
];
1234 if ((fl
& CSF_TRY
) && !(fl
& CSF_FINALLY
))
1236 /* Give up at a try conditional not in its finally clause.
1237 * Ignore the ":endwhile"/":endfor". */
1244 /* Cleanup and rewind all contained (and unclosed) conditionals. */
1245 (void)cleanup_conditionals(cstack
, CSF_WHILE
| CSF_FOR
, FALSE
);
1246 rewind_conditionals(cstack
, idx
, CSF_TRY
, &cstack
->cs_trylevel
);
1250 * When debugging or a breakpoint was encountered, display the debug
1251 * prompt (if not already done). This shows the user that an
1252 * ":endwhile"/":endfor" is executed when the ":while" was not TRUE or
1253 * after a ":break". Handle a ">quit" debug command as if an
1254 * interrupt had occurred before the ":endwhile"/":endfor". That is,
1255 * throw an interrupt exception if appropriate. Doing this here
1256 * prevents that an exception for a parsing error is discarded when
1257 * throwing the interrupt exception later on.
1259 else if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRUE
1260 && !(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_ACTIVE
)
1261 && dbg_check_skipped(eap
))
1262 (void)do_intthrow(cstack
);
1265 * Set loop flag, so do_cmdline() will jump back to the matching
1266 * ":while" or ":for".
1268 cstack
->cs_lflags
|= CSL_HAD_ENDLOOP
;
1280 char_u
*arg
= eap
->arg
;
1283 if (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
1284 value
= eval_to_string_skip(arg
, &eap
->nextcmd
, eap
->skip
);
1291 /* On error or when an exception is thrown during argument evaluation, do
1293 if (!eap
->skip
&& value
!= NULL
)
1295 if (throw_exception(value
, ET_USER
, NULL
) == FAIL
)
1298 do_throw(eap
->cstack
);
1303 * Throw the current exception through the specified cstack. Common routine
1304 * for ":throw" (user exception) and error and interrupt exceptions. Also
1305 * used for rethrowing an uncaught exception.
1309 struct condstack
*cstack
;
1312 int inactivate_try
= FALSE
;
1315 * Cleanup and inactivate up to the next surrounding try conditional that
1316 * is not in its finally clause. Normally, do not inactivate the try
1317 * conditional itself, so that its ACTIVE flag can be tested below. But
1318 * if a previous error or interrupt has not been converted to an exception,
1319 * inactivate the try conditional, too, as if the conversion had been done,
1320 * and reset the did_emsg or got_int flag, so this won't happen again at
1321 * the next surrounding try conditional.
1323 if (did_emsg
&& !THROW_ON_ERROR
)
1325 inactivate_try
= TRUE
;
1328 if (got_int
&& !THROW_ON_INTERRUPT
)
1330 inactivate_try
= TRUE
;
1333 idx
= cleanup_conditionals(cstack
, 0, inactivate_try
);
1337 * If this try conditional is active and we are before its first
1338 * ":catch", set THROWN so that the ":catch" commands will check
1339 * whether the exception matches. When the exception came from any of
1340 * the catch clauses, it will be made pending at the ":finally" (if
1341 * present) and rethrown at the ":endtry". This will also happen if
1342 * the try conditional is inactive. This is the case when we are
1343 * throwing an exception due to an error or interrupt on the way from
1344 * a preceding ":continue", ":break", ":return", ":finish", error or
1345 * interrupt (not converted to an exception) to the finally clause or
1346 * from a preceding throw of a user or error or interrupt exception to
1347 * the matching catch clause or the finally clause.
1349 if (!(cstack
->cs_flags
[idx
] & CSF_CAUGHT
))
1351 if (cstack
->cs_flags
[idx
] & CSF_ACTIVE
)
1352 cstack
->cs_flags
[idx
] |= CSF_THROWN
;
1354 /* THROWN may have already been set for a catchable exception
1355 * that has been discarded. Ensure it is reset for the new
1357 cstack
->cs_flags
[idx
] &= ~CSF_THROWN
;
1359 cstack
->cs_flags
[idx
] &= ~CSF_ACTIVE
;
1360 cstack
->cs_exception
[idx
] = current_exception
;
1363 /* TODO: Add optimization below. Not yet done because of interface
1364 * problems to eval.c and ex_cmds2.c. (Servatius) */
1368 * There are no catch clauses to check or finally clauses to execute.
1369 * End the current script or function. The exception will be rethrown
1372 if (getline_equal(eap
->getline
, eap
->cookie
, get_func_line
))
1373 current_funccal
->returned
= TRUE
;
1374 elseif (eap
->get_func_line
== getsourceline
)
1375 ((struct source_cookie
*)eap
->cookie
)->finished
= TRUE
;
1390 struct condstack
*cstack
= eap
->cstack
;
1392 if (cstack
->cs_idx
== CSTACK_LEN
- 1)
1393 eap
->errmsg
= (char_u
*)N_("E601: :try nesting too deep");
1397 ++cstack
->cs_trylevel
;
1398 cstack
->cs_flags
[cstack
->cs_idx
] = CSF_TRY
;
1399 cstack
->cs_pending
[cstack
->cs_idx
] = CSTP_NONE
;
1402 * Don't do something after an error, interrupt, or throw, or when there
1403 * is a surrounding conditional and it was not active.
1405 skip
= did_emsg
|| got_int
|| did_throw
|| (cstack
->cs_idx
> 0
1406 && !(cstack
->cs_flags
[cstack
->cs_idx
- 1] & CSF_ACTIVE
));
1410 /* Set ACTIVE and TRUE. TRUE means that the corresponding ":catch"
1411 * commands should check for a match if an exception is thrown and
1412 * that the finally clause needs to be executed. */
1413 cstack
->cs_flags
[cstack
->cs_idx
] |= CSF_ACTIVE
| CSF_TRUE
;
1416 * ":silent!", even when used in a try conditional, disables
1417 * displaying of error messages and conversion of errors to
1418 * exceptions. When the silent commands again open a try
1419 * conditional, save "emsg_silent" and reset it so that errors are
1420 * again converted to exceptions. The value is restored when that
1421 * try conditional is left. If it is left normally, the commands
1422 * following the ":endtry" are again silent. If it is left by
1423 * a ":continue", ":break", ":return", or ":finish", the commands
1424 * executed next are again silent. If it is left due to an
1425 * aborting error, an interrupt, or an exception, restoring
1426 * "emsg_silent" does not matter since we are already in the
1427 * aborting state and/or the exception has already been thrown.
1428 * The effect is then just freeing the memory that was allocated
1429 * to save the value.
1435 elem
= (eslist_T
*)alloc((unsigned)sizeof(struct eslist_elem
));
1437 EMSG(_(e_outofmem
));
1440 elem
->saved_emsg_silent
= emsg_silent
;
1441 elem
->next
= cstack
->cs_emsg_silent_list
;
1442 cstack
->cs_emsg_silent_list
= elem
;
1443 cstack
->cs_flags
[cstack
->cs_idx
] |= CSF_SILENT
;
1453 * ":catch /{pattern}/" and ":catch"
1460 int give_up
= FALSE
;
1466 regmatch_T regmatch
;
1468 struct condstack
*cstack
= eap
->cstack
;
1471 if (cstack
->cs_trylevel
<= 0 || cstack
->cs_idx
< 0)
1473 eap
->errmsg
= (char_u
*)N_("E603: :catch without :try");
1478 if (!(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRY
))
1480 /* Report what's missing if the matching ":try" is not in its
1481 * finally clause. */
1482 eap
->errmsg
= get_end_emsg(cstack
);
1485 for (idx
= cstack
->cs_idx
; idx
> 0; --idx
)
1486 if (cstack
->cs_flags
[idx
] & CSF_TRY
)
1488 if (cstack
->cs_flags
[idx
] & CSF_FINALLY
)
1490 /* Give up for a ":catch" after ":finally" and ignore it.
1492 eap
->errmsg
= (char_u
*)N_("E604: :catch after :finally");
1496 rewind_conditionals(cstack
, idx
, CSF_WHILE
| CSF_FOR
,
1497 &cstack
->cs_looplevel
);
1500 if (ends_excmd(*eap
->arg
)) /* no argument, catch all errors */
1502 pat
= (char_u
*)".*";
1504 eap
->nextcmd
= find_nextcmd(eap
->arg
);
1509 end
= skip_regexp(pat
, *eap
->arg
, TRUE
, NULL
);
1515 * Don't do something when no exception has been thrown or when the
1516 * corresponding try block never got active (because of an inactive
1517 * surrounding conditional or after an error or interrupt or throw).
1519 if (!did_throw
|| !(cstack
->cs_flags
[idx
] & CSF_TRUE
))
1523 * Check for a match only if an exception is thrown but not caught by
1524 * a previous ":catch". An exception that has replaced a discarded
1525 * exception is not checked (THROWN is not set then).
1527 if (!skip
&& (cstack
->cs_flags
[idx
] & CSF_THROWN
)
1528 && !(cstack
->cs_flags
[idx
] & CSF_CAUGHT
))
1530 if (end
!= NULL
&& *end
!= NUL
&& !ends_excmd(*skipwhite(end
+ 1)))
1532 EMSG(_(e_trailing
));
1536 /* When debugging or a breakpoint was encountered, display the
1537 * debug prompt (if not already done) before checking for a match.
1538 * This is a helpful hint for the user when the regular expression
1539 * matching fails. Handle a ">quit" debug command as if an
1540 * interrupt had occurred before the ":catch". That is, discard
1541 * the original exception, replace it by an interrupt exception,
1542 * and don't catch it in this try block. */
1543 if (!dbg_check_skipped(eap
) || !do_intthrow(cstack
))
1545 /* Terminate the pattern and avoid the 'l' flag in 'cpoptions'
1546 * while compiling it. */
1553 p_cpo
= (char_u
*)"";
1554 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
1555 regmatch
.rm_ic
= FALSE
;
1559 if (regmatch
.regprog
== NULL
)
1560 EMSG2(_(e_invarg2
), pat
);
1564 * Save the value of got_int and reset it. We don't want
1565 * a previous interruption cancel matching, only hitting
1566 * CTRL-C while matching should abort it.
1568 prev_got_int
= got_int
;
1570 caught
= vim_regexec_nl(®match
, current_exception
->value
,
1572 got_int
|= prev_got_int
;
1573 vim_free(regmatch
.regprog
);
1580 /* Make this ":catch" clause active and reset did_emsg, got_int,
1581 * and did_throw. Put the exception on the caught stack. */
1582 cstack
->cs_flags
[idx
] |= CSF_ACTIVE
| CSF_CAUGHT
;
1583 did_emsg
= got_int
= did_throw
= FALSE
;
1584 catch_exception((except_T
*)cstack
->cs_exception
[idx
]);
1585 /* It's mandatory that the current exception is stored in the cstack
1586 * so that it can be discarded at the next ":catch", ":finally", or
1587 * ":endtry" or when the catch clause is left by a ":continue",
1588 * ":break", ":return", ":finish", error, interrupt, or another
1590 if (cstack
->cs_exception
[cstack
->cs_idx
] != current_exception
)
1591 EMSG(_(e_internal
));
1596 * If there is a preceding catch clause and it caught the exception,
1597 * finish the exception now. This happens also after errors except
1598 * when this ":catch" was after the ":finally" or not within
1599 * a ":try". Make the try conditional inactive so that the
1600 * following catch clauses are skipped. On an error or interrupt
1601 * after the preceding try block or catch clause was left by
1602 * a ":continue", ":break", ":return", or ":finish", discard the
1605 cleanup_conditionals(cstack
, CSF_TRY
, TRUE
);
1610 eap
->nextcmd
= find_nextcmd(end
);
1622 int pending
= CSTP_NONE
;
1623 struct condstack
*cstack
= eap
->cstack
;
1625 if (cstack
->cs_trylevel
<= 0 || cstack
->cs_idx
< 0)
1626 eap
->errmsg
= (char_u
*)N_("E606: :finally without :try");
1629 if (!(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRY
))
1631 eap
->errmsg
= get_end_emsg(cstack
);
1632 for (idx
= cstack
->cs_idx
- 1; idx
> 0; --idx
)
1633 if (cstack
->cs_flags
[idx
] & CSF_TRY
)
1635 /* Make this error pending, so that the commands in the following
1636 * finally clause can be executed. This overrules also a pending
1637 * ":continue", ":break", ":return", or ":finish". */
1638 pending
= CSTP_ERROR
;
1641 idx
= cstack
->cs_idx
;
1643 if (cstack
->cs_flags
[idx
] & CSF_FINALLY
)
1645 /* Give up for a multiple ":finally" and ignore it. */
1646 eap
->errmsg
= (char_u
*)N_("E607: multiple :finally");
1649 rewind_conditionals(cstack
, idx
, CSF_WHILE
| CSF_FOR
,
1650 &cstack
->cs_looplevel
);
1653 * Don't do something when the corresponding try block never got active
1654 * (because of an inactive surrounding conditional or after an error or
1655 * interrupt or throw) or for a ":finally" without ":try" or a multiple
1656 * ":finally". After every other error (did_emsg or the conditional
1657 * errors detected above) or after an interrupt (got_int) or an
1658 * exception (did_throw), the finally clause must be executed.
1660 skip
= !(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRUE
);
1664 /* When debugging or a breakpoint was encountered, display the
1665 * debug prompt (if not already done). The user then knows that the
1666 * finally clause is executed. */
1667 if (dbg_check_skipped(eap
))
1669 /* Handle a ">quit" debug command as if an interrupt had
1670 * occurred before the ":finally". That is, discard the
1671 * original exception and replace it by an interrupt
1673 (void)do_intthrow(cstack
);
1677 * If there is a preceding catch clause and it caught the exception,
1678 * finish the exception now. This happens also after errors except
1679 * when this is a multiple ":finally" or one not within a ":try".
1680 * After an error or interrupt, this also discards a pending
1681 * ":continue", ":break", ":finish", or ":return" from the preceding
1682 * try block or catch clause.
1684 cleanup_conditionals(cstack
, CSF_TRY
, FALSE
);
1687 * Make did_emsg, got_int, did_throw pending. If set, they overrule
1688 * a pending ":continue", ":break", ":return", or ":finish". Then
1689 * we have particularly to discard a pending return value (as done
1690 * by the call to cleanup_conditionals() above when did_emsg or
1691 * got_int is set). The pending values are restored by the
1692 * ":endtry", except if there is a new error, interrupt, exception,
1693 * ":continue", ":break", ":return", or ":finish" in the following
1694 * finally clause. A missing ":endwhile", ":endfor" or ":endif"
1695 * detected here is treated as if did_emsg and did_throw had
1696 * already been set, respectively in case that the error is not
1697 * converted to an exception, did_throw had already been unset.
1698 * We must not set did_emsg here since that would suppress the
1701 if (pending
== CSTP_ERROR
|| did_emsg
|| got_int
|| did_throw
)
1703 if (cstack
->cs_pending
[cstack
->cs_idx
] == CSTP_RETURN
)
1705 report_discard_pending(CSTP_RETURN
,
1706 cstack
->cs_rettv
[cstack
->cs_idx
]);
1707 discard_pending_return(cstack
->cs_rettv
[cstack
->cs_idx
]);
1709 if (pending
== CSTP_ERROR
&& !did_emsg
)
1710 pending
|= (THROW_ON_ERROR
) ? CSTP_THROW
: 0;
1712 pending
|= did_throw
? CSTP_THROW
: 0;
1713 pending
|= did_emsg
? CSTP_ERROR
: 0;
1714 pending
|= got_int
? CSTP_INTERRUPT
: 0;
1715 cstack
->cs_pending
[cstack
->cs_idx
] = pending
;
1717 /* It's mandatory that the current exception is stored in the
1718 * cstack so that it can be rethrown at the ":endtry" or be
1719 * discarded if the finally clause is left by a ":continue",
1720 * ":break", ":return", ":finish", error, interrupt, or another
1721 * exception. When emsg() is called for a missing ":endif" or
1722 * a missing ":endwhile"/":endfor" detected here, the
1723 * exception will be discarded. */
1724 if (did_throw
&& cstack
->cs_exception
[cstack
->cs_idx
]
1725 != current_exception
)
1726 EMSG(_(e_internal
));
1730 * Set CSL_HAD_FINA, so do_cmdline() will reset did_emsg,
1731 * got_int, and did_throw and make the finally clause active.
1732 * This will happen after emsg() has been called for a missing
1733 * ":endif" or a missing ":endwhile"/":endfor" detected here, so
1734 * that the following finally clause will be executed even then.
1736 cstack
->cs_lflags
|= CSL_HAD_FINA
;
1750 int rethrow
= FALSE
;
1751 int pending
= CSTP_NONE
;
1753 struct condstack
*cstack
= eap
->cstack
;
1755 if (cstack
->cs_trylevel
<= 0 || cstack
->cs_idx
< 0)
1756 eap
->errmsg
= (char_u
*)N_("E602: :endtry without :try");
1760 * Don't do something after an error, interrupt or throw in the try
1761 * block, catch clause, or finally clause preceding this ":endtry" or
1762 * when an error or interrupt occurred after a ":continue", ":break",
1763 * ":return", or ":finish" in a try block or catch clause preceding this
1764 * ":endtry" or when the try block never got active (because of an
1765 * inactive surrounding conditional or after an error or interrupt or
1766 * throw) or when there is a surrounding conditional and it has been
1767 * made inactive by a ":continue", ":break", ":return", or ":finish" in
1768 * the finally clause. The latter case need not be tested since then
1769 * anything pending has already been discarded. */
1770 skip
= did_emsg
|| got_int
|| did_throw
||
1771 !(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRUE
);
1773 if (!(cstack
->cs_flags
[cstack
->cs_idx
] & CSF_TRY
))
1775 eap
->errmsg
= get_end_emsg(cstack
);
1776 /* Find the matching ":try" and report what's missing. */
1777 idx
= cstack
->cs_idx
;
1780 while (idx
> 0 && !(cstack
->cs_flags
[idx
] & CSF_TRY
));
1781 rewind_conditionals(cstack
, idx
, CSF_WHILE
| CSF_FOR
,
1782 &cstack
->cs_looplevel
);
1786 * If an exception is being thrown, discard it to prevent it from
1787 * being rethrown at the end of this function. It would be
1788 * discarded by the error message, anyway. Resets did_throw.
1789 * This does not affect the script termination due to the error
1790 * since "trylevel" is decremented after emsg() has been called.
1793 discard_current_exception();
1797 idx
= cstack
->cs_idx
;
1800 * If we stopped with the exception currently being thrown at this
1801 * try conditional since we didn't know that it doesn't have
1802 * a finally clause, we need to rethrow it after closing the try
1805 if (did_throw
&& (cstack
->cs_flags
[idx
] & CSF_TRUE
)
1806 && !(cstack
->cs_flags
[idx
] & CSF_FINALLY
))
1810 /* If there was no finally clause, show the user when debugging or
1811 * a breakpoint was encountered that the end of the try conditional has
1812 * been reached: display the debug prompt (if not already done). Do
1813 * this on normal control flow or when an exception was thrown, but not
1814 * on an interrupt or error not converted to an exception or when
1815 * a ":break", ":continue", ":return", or ":finish" is pending. These
1816 * actions are carried out immediately.
1818 if ((rethrow
|| (!skip
1819 && !(cstack
->cs_flags
[idx
] & CSF_FINALLY
)
1820 && !cstack
->cs_pending
[idx
]))
1821 && dbg_check_skipped(eap
))
1823 /* Handle a ">quit" debug command as if an interrupt had occurred
1824 * before the ":endtry". That is, throw an interrupt exception and
1825 * set "skip" and "rethrow". */
1829 (void)do_intthrow(cstack
);
1830 /* The do_intthrow() call may have reset did_throw or
1831 * cstack->cs_pending[idx].*/
1833 if (did_throw
&& !(cstack
->cs_flags
[idx
] & CSF_FINALLY
))
1839 * If a ":return" is pending, we need to resume it after closing the
1840 * try conditional; remember the return value. If there was a finally
1841 * clause making an exception pending, we need to rethrow it. Make it
1842 * the exception currently being thrown.
1846 pending
= cstack
->cs_pending
[idx
];
1847 cstack
->cs_pending
[idx
] = CSTP_NONE
;
1848 if (pending
== CSTP_RETURN
)
1849 rettv
= cstack
->cs_rettv
[idx
];
1850 else if (pending
& CSTP_THROW
)
1851 current_exception
= cstack
->cs_exception
[idx
];
1855 * Discard anything pending on an error, interrupt, or throw in the
1856 * finally clause. If there was no ":finally", discard a pending
1857 * ":continue", ":break", ":return", or ":finish" if an error or
1858 * interrupt occurred afterwards, but before the ":endtry" was reached.
1859 * If an exception was caught by the last of the catch clauses and there
1860 * was no finally clause, finish the exception now. This happens also
1861 * after errors except when this ":endtry" is not within a ":try".
1862 * Restore "emsg_silent" if it has been reset by this try conditional.
1864 (void)cleanup_conditionals(cstack
, CSF_TRY
| CSF_SILENT
, TRUE
);
1867 --cstack
->cs_trylevel
;
1871 report_resume_pending(pending
,
1872 (pending
== CSTP_RETURN
) ? rettv
:
1873 (pending
& CSTP_THROW
) ? (void *)current_exception
: NULL
);
1879 /* Reactivate a pending ":continue", ":break", ":return",
1880 * ":finish" from the try block or a catch clause of this try
1881 * conditional. This is skipped, if there was an error in an
1882 * (unskipped) conditional command or an interrupt afterwards
1883 * or if the finally clause is present and executed a new error,
1884 * interrupt, throw, ":continue", ":break", ":return", or
1893 do_return(eap
, FALSE
, FALSE
, rettv
);
1896 do_finish(eap
, FALSE
);
1899 /* When the finally clause was entered due to an error,
1900 * interrupt or throw (as opposed to a ":continue", ":break",
1901 * ":return", or ":finish"), restore the pending values of
1902 * did_emsg, got_int, and did_throw. This is skipped, if there
1903 * was a new error, interrupt, throw, ":continue", ":break",
1904 * ":return", or ":finish". in the finally clause. */
1906 if (pending
& CSTP_ERROR
)
1908 if (pending
& CSTP_INTERRUPT
)
1910 if (pending
& CSTP_THROW
)
1917 /* Rethrow the current exception (within this cstack). */
1923 * enter_cleanup() and leave_cleanup()
1925 * Functions to be called before/after invoking a sequence of autocommands for
1926 * cleanup for a failed command. (Failure means here that a call to emsg()
1927 * has been made, an interrupt occurred, or there is an uncaught exception
1928 * from a previous autocommand execution of the same command.)
1930 * Call enter_cleanup() with a pointer to a cleanup_T and pass the same
1931 * pointer to leave_cleanup(). The cleanup_T structure stores the pending
1932 * error/interrupt/exception state.
1936 * This function works a bit like ex_finally() except that there was not
1937 * actually an extra try block around the part that failed and an error or
1938 * interrupt has not (yet) been converted to an exception. This function
1939 * saves the error/interrupt/ exception state and prepares for the call to
1940 * do_cmdline() that is going to be made for the cleanup autocommand
1947 int pending
= CSTP_NONE
;
1950 * Postpone did_emsg, got_int, did_throw. The pending values will be
1951 * restored by leave_cleanup() except if there was an aborting error,
1952 * interrupt, or uncaught exception after this function ends.
1954 if (did_emsg
|| got_int
|| did_throw
|| need_rethrow
)
1956 csp
->pending
= (did_emsg
? CSTP_ERROR
: 0)
1957 | (got_int
? CSTP_INTERRUPT
: 0)
1958 | (did_throw
? CSTP_THROW
: 0)
1959 | (need_rethrow
? CSTP_THROW
: 0);
1961 /* If we are currently throwing an exception (did_throw), save it as
1962 * well. On an error not yet converted to an exception, update
1963 * "force_abort" and reset "cause_abort" (as do_errthrow() would do).
1964 * This is needed for the do_cmdline() call that is going to be made
1965 * for autocommand execution. We need not save *msg_list because
1966 * there is an extra instance for every call of do_cmdline(), anyway.
1968 if (did_throw
|| need_rethrow
)
1969 csp
->exception
= current_exception
;
1972 csp
->exception
= NULL
;
1975 force_abort
|= cause_abort
;
1976 cause_abort
= FALSE
;
1979 did_emsg
= got_int
= did_throw
= need_rethrow
= FALSE
;
1981 /* Report if required by the 'verbose' option or when debugging. */
1982 report_make_pending(pending
, csp
->exception
);
1986 csp
->pending
= CSTP_NONE
;
1987 csp
->exception
= NULL
;
1992 * See comment above enter_cleanup() for how this function is used.
1994 * This function is a bit like ex_endtry() except that there was not actually
1995 * an extra try block around the part that failed and an error or interrupt
1996 * had not (yet) been converted to an exception when the cleanup autocommand
1997 * sequence was invoked.
1999 * This function has to be called with the address of the cleanup_T structure
2000 * filled by enter_cleanup() as an argument; it restores the error/interrupt/
2001 * exception state saved by that function - except there was an aborting
2002 * error, an interrupt or an uncaught exception during execution of the
2003 * cleanup autocommands. In the latter case, the saved error/interrupt/
2004 * exception state is discarded.
2010 int pending
= csp
->pending
;
2012 if (pending
== CSTP_NONE
) /* nothing to do */
2015 /* If there was an aborting error, an interrupt, or an uncaught exception
2016 * after the corresponding call to enter_cleanup(), discard what has been
2017 * made pending by it. Report this to the user if required by the
2018 * 'verbose' option or when debugging. */
2019 if (aborting() || need_rethrow
)
2021 if (pending
& CSTP_THROW
)
2022 /* Cancel the pending exception (includes report). */
2023 discard_exception((except_T
*)csp
->exception
, FALSE
);
2025 report_discard_pending(pending
, NULL
);
2027 /* If an error was about to be converted to an exception when
2028 * enter_cleanup() was called, free the message list. */
2029 if (msg_list
!= NULL
)
2031 free_msglist(*msg_list
);
2037 * If there was no new error, interrupt, or throw between the calls
2038 * to enter_cleanup() and leave_cleanup(), restore the pending
2039 * error/interrupt/exception state.
2044 * If there was an exception being thrown when enter_cleanup() was
2045 * called, we need to rethrow it. Make it the exception currently
2048 if (pending
& CSTP_THROW
)
2049 current_exception
= csp
->exception
;
2052 * If an error was about to be converted to an exception when
2053 * enter_cleanup() was called, let "cause_abort" take the part of
2054 * "force_abort" (as done by cause_errthrow()).
2056 else if (pending
& CSTP_ERROR
)
2058 cause_abort
= force_abort
;
2059 force_abort
= FALSE
;
2063 * Restore the pending values of did_emsg, got_int, and did_throw.
2065 if (pending
& CSTP_ERROR
)
2067 if (pending
& CSTP_INTERRUPT
)
2069 if (pending
& CSTP_THROW
)
2070 need_rethrow
= TRUE
; /* did_throw will be set by do_one_cmd() */
2072 /* Report if required by the 'verbose' option or when debugging. */
2073 report_resume_pending(pending
,
2074 (pending
& CSTP_THROW
) ? (void *)current_exception
: NULL
);
2080 * Make conditionals inactive and discard what's pending in finally clauses
2081 * until the conditional type searched for or a try conditional not in its
2082 * finally clause is reached. If this is in an active catch clause, finish
2083 * the caught exception.
2084 * Return the cstack index where the search stopped.
2085 * Values used for "searched_cond" are (CSF_WHILE | CSF_FOR) or CSF_TRY or 0,
2086 * the latter meaning the innermost try conditional not in its finally clause.
2087 * "inclusive" tells whether the conditional searched for should be made
2088 * inactive itself (a try conditional not in its finally claused possibly find
2089 * before is always made inactive). If "inclusive" is TRUE and
2090 * "searched_cond" is CSF_TRY|CSF_SILENT, the saved former value of
2091 * "emsg_silent", if reset when the try conditional finally reached was
2092 * entered, is restored (unsed by ex_endtry()). This is normally done only
2093 * when such a try conditional is left.
2096 cleanup_conditionals(cstack
, searched_cond
, inclusive
)
2097 struct condstack
*cstack
;
2104 for (idx
= cstack
->cs_idx
; idx
>= 0; --idx
)
2106 if (cstack
->cs_flags
[idx
] & CSF_TRY
)
2109 * Discard anything pending in a finally clause and continue the
2110 * search. There may also be a pending ":continue", ":break",
2111 * ":return", or ":finish" before the finally clause. We must not
2112 * discard it, unless an error or interrupt occurred afterwards.
2114 if (did_emsg
|| got_int
|| (cstack
->cs_flags
[idx
] & CSF_FINALLY
))
2116 switch (cstack
->cs_pending
[idx
])
2124 report_discard_pending(cstack
->cs_pending
[idx
], NULL
);
2125 cstack
->cs_pending
[idx
] = CSTP_NONE
;
2129 report_discard_pending(CSTP_RETURN
,
2130 cstack
->cs_rettv
[idx
]);
2131 discard_pending_return(cstack
->cs_rettv
[idx
]);
2132 cstack
->cs_pending
[idx
] = CSTP_NONE
;
2136 if (cstack
->cs_flags
[idx
] & CSF_FINALLY
)
2138 if (cstack
->cs_pending
[idx
] & CSTP_THROW
)
2140 /* Cancel the pending exception. This is in the
2141 * finally clause, so that the stack of the
2142 * caught exceptions is not involved. */
2143 discard_exception((except_T
*)
2144 cstack
->cs_exception
[idx
],
2148 report_discard_pending(cstack
->cs_pending
[idx
],
2150 cstack
->cs_pending
[idx
] = CSTP_NONE
;
2157 * Stop at a try conditional not in its finally clause. If this try
2158 * conditional is in an active catch clause, finish the caught
2161 if (!(cstack
->cs_flags
[idx
] & CSF_FINALLY
))
2163 if ((cstack
->cs_flags
[idx
] & CSF_ACTIVE
)
2164 && (cstack
->cs_flags
[idx
] & CSF_CAUGHT
))
2165 finish_exception((except_T
*)cstack
->cs_exception
[idx
]);
2166 /* Stop at this try conditional - except the try block never
2167 * got active (because of an inactive surrounding conditional
2168 * or when the ":try" appeared after an error or interrupt or
2170 if (cstack
->cs_flags
[idx
] & CSF_TRUE
)
2172 if (searched_cond
== 0 && !inclusive
)
2179 /* Stop on the searched conditional type (even when the surrounding
2180 * conditional is not active or something has been made pending).
2181 * If "inclusive" is TRUE and "searched_cond" is CSF_TRY|CSF_SILENT,
2182 * check first whether "emsg_silent" needs to be restored. */
2183 if (cstack
->cs_flags
[idx
] & searched_cond
)
2189 cstack
->cs_flags
[idx
] &= ~CSF_ACTIVE
;
2190 if (stop
&& searched_cond
!= (CSF_TRY
| CSF_SILENT
))
2194 * When leaving a try conditional that reset "emsg_silent" on its
2195 * entry after saving the original value, restore that value here and
2196 * free the memory used to store it.
2198 if ((cstack
->cs_flags
[idx
] & CSF_TRY
)
2199 && (cstack
->cs_flags
[idx
] & CSF_SILENT
))
2203 elem
= cstack
->cs_emsg_silent_list
;
2204 cstack
->cs_emsg_silent_list
= elem
->next
;
2205 emsg_silent
= elem
->saved_emsg_silent
;
2207 cstack
->cs_flags
[idx
] &= ~CSF_SILENT
;
2216 * Return an appropriate error message for a missing endwhile/endfor/endif.
2219 get_end_emsg(cstack
)
2220 struct condstack
*cstack
;
2222 if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_WHILE
)
2224 if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_FOR
)
2231 * Rewind conditionals until index "idx" is reached. "cond_type" and
2232 * "cond_level" specify a conditional type and the address of a level variable
2233 * which is to be decremented with each skipped conditional of the specified
2235 * Also free "for info" structures where needed.
2238 rewind_conditionals(cstack
, idx
, cond_type
, cond_level
)
2239 struct condstack
*cstack
;
2244 while (cstack
->cs_idx
> idx
)
2246 if (cstack
->cs_flags
[cstack
->cs_idx
] & cond_type
)
2248 if (cstack
->cs_flags
[cstack
->cs_idx
] & CSF_FOR
)
2249 free_for_info(cstack
->cs_forinfo
[cstack
->cs_idx
]);
2255 * ":endfunction" when not after a ":function"
2262 EMSG(_("E193: :endfunction not inside a function"));
2266 * Return TRUE if the string "p" looks like a ":while" or ":for" command.
2274 /* skip modifiers, white space and ':' */
2277 while (*p
== ' ' || *p
== '\t' || *p
== ':')
2279 len
= modifier_len(p
);
2284 if ((p
[0] == 'w' && p
[1] == 'h')
2285 || (p
[0] == 'f' && p
[1] == 'o' && p
[2] == 'r'))
2290 #endif /* FEAT_EVAL */