home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / src / ex_eval.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-05-30  |  58.6 KB  |  1,985 lines

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