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