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