blob: 38517e3e7f253ba99d7e9c4b09787ae6a4b1fd20 [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 Moolenaar12805862005-01-05 22:16:17 +00001175 if (fl & CSF_WHILE)
1176 eap->errmsg = (char_u *)_("E999: Using :endfor with :while");
1177 else if (fl & CSF_FOR)
1178 eap->errmsg = (char_u *)_("E999: Using :endwhile with :for");
1179 else if (!(fl & CSF_TRY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 eap->errmsg = e_endif;
Bram Moolenaar12805862005-01-05 22:16:17 +00001181 else if (fl & CSF_FINALLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 eap->errmsg = e_endtry;
Bram Moolenaar12805862005-01-05 22:16:17 +00001183 /* Try to find the matching ":while" and report what's missing. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 for (idx = cstack->cs_idx; idx > 0; --idx)
1185 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001186 fl = cstack->cs_flags[idx];
1187 if ((fl & CSF_TRY) && !(fl & CSF_FINALLY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
1189 /* Give up at a try conditional not in its finally clause.
Bram Moolenaar12805862005-01-05 22:16:17 +00001190 * Ignore the ":endwhile"/":endfor". */
1191 eap->errmsg = err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 return;
1193 }
Bram Moolenaar12805862005-01-05 22:16:17 +00001194 if (fl & csf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 break;
1196 }
1197 /* Cleanup and rewind all contained (and unclosed) conditionals. */
Bram Moolenaar12805862005-01-05 22:16:17 +00001198 (void)cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 rewind_conditionals(cstack, idx, CSF_TRY, &cstack->cs_trylevel);
1200 }
1201
1202 /*
1203 * When debugging or a breakpoint was encountered, display the debug
1204 * prompt (if not already done). This shows the user that an
Bram Moolenaar12805862005-01-05 22:16:17 +00001205 * ":endwhile"/":endfor" is executed when the ":while" was not TRUE or
1206 * after a ":break". Handle a ">quit" debug command as if an
1207 * interrupt had occurred before the ":endwhile"/":endfor". That is,
1208 * throw an interrupt exception if appropriate. Doing this here
1209 * prevents that an exception for a parsing error is discarded when
1210 * throwing the interrupt exception later on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 */
1212 else if (cstack->cs_flags[cstack->cs_idx] & CSF_TRUE
1213 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)
1214 && dbg_check_skipped(eap))
1215 (void)do_intthrow(cstack);
1216
1217 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001218 * Set loop flag, so do_cmdline() will jump back to the matching
1219 * ":while" or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001221 cstack->cs_lflags |= CSL_HAD_ENDLOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223}
1224
1225
1226/*
1227 * ":throw expr"
1228 */
1229 void
1230ex_throw(eap)
1231 exarg_T *eap;
1232{
1233 char_u *arg = eap->arg;
1234 char_u *value;
1235
1236 if (*arg != NUL && *arg != '|' && *arg != '\n')
1237 value = eval_to_string_skip(arg, &eap->nextcmd, eap->skip);
1238 else
1239 {
1240 EMSG(_(e_argreq));
1241 value = NULL;
1242 }
1243
1244 /* On error or when an exception is thrown during argument evaluation, do
1245 * not throw. */
1246 if (!eap->skip && value != NULL)
1247 {
1248 if (throw_exception(value, ET_USER, NULL) == FAIL)
1249 vim_free(value);
1250 else
1251 do_throw(eap->cstack);
1252 }
1253}
1254
1255/*
Bram Moolenaar269ec652004-07-29 08:43:53 +00001256 * Throw the current exception through the specified cstack. Common routine
1257 * for ":throw" (user exception) and error and interrupt exceptions. Also
1258 * used for rethrowing an uncaught exception.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 */
1260 void
1261do_throw(cstack)
1262 struct condstack *cstack;
1263{
1264 int idx;
1265 int inactivate_try = FALSE;
1266
1267 /*
1268 * Cleanup and inactivate up to the next surrounding try conditional that
1269 * is not in its finally clause. Normally, do not inactivate the try
1270 * conditional itself, so that its ACTIVE flag can be tested below. But
1271 * if a previous error or interrupt has not been converted to an exception,
1272 * inactivate the try conditional, too, as if the conversion had been done,
Bram Moolenaar269ec652004-07-29 08:43:53 +00001273 * and reset the did_emsg or got_int flag, so this won't happen again at
1274 * the next surrounding try conditional.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 */
1276 if (did_emsg && !THROW_ON_ERROR)
1277 {
1278 inactivate_try = TRUE;
1279 did_emsg = FALSE;
1280 }
1281 if (got_int && !THROW_ON_INTERRUPT)
1282 {
1283 inactivate_try = TRUE;
1284 got_int = FALSE;
1285 }
1286 idx = cleanup_conditionals(cstack, 0, inactivate_try);
1287 if (idx >= 0)
1288 {
1289 /*
1290 * If this try conditional is active and we are before its first
Bram Moolenaar269ec652004-07-29 08:43:53 +00001291 * ":catch", set THROWN so that the ":catch" commands will check
1292 * whether the exception matches. When the exception came from any of
1293 * the catch clauses, it will be made pending at the ":finally" (if
1294 * present) and rethrown at the ":endtry". This will also happen if
1295 * the try conditional is inactive. This is the case when we are
1296 * throwing an exception due to an error or interrupt on the way from
1297 * a preceding ":continue", ":break", ":return", ":finish", error or
1298 * interrupt (not converted to an exception) to the finally clause or
1299 * from a preceding throw of a user or error or interrupt exception to
1300 * the matching catch clause or the finally clause.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 */
1302 if (!(cstack->cs_flags[idx] & CSF_CAUGHT))
1303 {
1304 if (cstack->cs_flags[idx] & CSF_ACTIVE)
1305 cstack->cs_flags[idx] |= CSF_THROWN;
1306 else
1307 /* THROWN may have already been set for a catchable exception
1308 * that has been discarded. Ensure it is reset for the new
1309 * exception. */
1310 cstack->cs_flags[idx] &= ~CSF_THROWN;
1311 }
1312 cstack->cs_flags[idx] &= ~CSF_ACTIVE;
1313 cstack->cs_exception[idx] = current_exception;
1314 }
1315#if 0
Bram Moolenaar269ec652004-07-29 08:43:53 +00001316 /* TODO: Add optimization below. Not yet done because of interface
1317 * problems to eval.c and ex_cmds2.c. (Servatius) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 else
1319 {
1320 /*
1321 * There are no catch clauses to check or finally clauses to execute.
1322 * End the current script or function. The exception will be rethrown
1323 * in the caller.
1324 */
1325 if (getline_equal(eap->getline, eap->cookie, get_func_line))
1326 current_funccal->returned = TRUE;
1327 elseif (eap->get_func_line == getsourceline)
1328 ((struct source_cookie *)eap->cookie)->finished = TRUE;
1329 }
1330#endif
1331
1332 did_throw = TRUE;
1333}
1334
1335/*
1336 * ":try"
1337 */
1338 void
1339ex_try(eap)
1340 exarg_T *eap;
1341{
1342 int skip;
1343 struct condstack *cstack = eap->cstack;
1344
1345 if (cstack->cs_idx == CSTACK_LEN - 1)
1346 eap->errmsg = (char_u *)N_("E601: :try nesting too deep");
1347 else
1348 {
1349 ++cstack->cs_idx;
1350 ++cstack->cs_trylevel;
1351 cstack->cs_flags[cstack->cs_idx] = CSF_TRY;
1352 cstack->cs_pending[cstack->cs_idx] = CSTP_NONE;
1353
1354 /*
1355 * Don't do something after an error, interrupt, or throw, or when there
1356 * is a surrounding conditional and it was not active.
1357 */
1358 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
1359 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
1360
1361 if (!skip)
1362 {
1363 /* Set ACTIVE and TRUE. TRUE means that the corresponding ":catch"
1364 * commands should check for a match if an exception is thrown and
1365 * that the finally clause needs to be executed. */
1366 cstack->cs_flags[cstack->cs_idx] |= CSF_ACTIVE | CSF_TRUE;
1367
1368 /*
1369 * ":silent!", even when used in a try conditional, disables
1370 * displaying of error messages and conversion of errors to
1371 * exceptions. When the silent commands again open a try
1372 * conditional, save "emsg_silent" and reset it so that errors are
1373 * again converted to exceptions. The value is restored when that
1374 * try conditional is left. If it is left normally, the commands
1375 * following the ":endtry" are again silent. If it is left by
1376 * a ":continue", ":break", ":return", or ":finish", the commands
1377 * executed next are again silent. If it is left due to an
1378 * aborting error, an interrupt, or an exception, restoring
1379 * "emsg_silent" does not matter since we are already in the
1380 * aborting state and/or the exception has already been thrown.
1381 * The effect is then just freeing the memory that was allocated
1382 * to save the value.
1383 */
1384 if (emsg_silent)
1385 {
1386 eslist_T *elem;
1387
1388 elem = (eslist_T *)alloc((unsigned)sizeof(struct eslist_elem));
1389 if (elem == NULL)
1390 EMSG(_(e_outofmem));
1391 else
1392 {
1393 elem->saved_emsg_silent = emsg_silent;
1394 elem->next = cstack->cs_emsg_silent_list;
1395 cstack->cs_emsg_silent_list = elem;
1396 cstack->cs_flags[cstack->cs_idx] |= CSF_SILENT;
1397 emsg_silent = 0;
1398 }
1399 }
1400 }
1401
1402 }
1403}
1404
1405/*
1406 * ":catch /{pattern}/" and ":catch"
1407 */
1408 void
1409ex_catch(eap)
1410 exarg_T *eap;
1411{
1412 int idx = 0;
1413 int give_up = FALSE;
1414 int skip = FALSE;
1415 int caught = FALSE;
1416 char_u *end;
1417 int save_char = 0;
1418 char_u *save_cpo;
1419 regmatch_T regmatch;
1420 int prev_got_int;
1421 struct condstack *cstack = eap->cstack;
1422 char_u *pat;
1423
1424 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1425 {
1426 eap->errmsg = (char_u *)N_("E603: :catch without :try");
1427 give_up = TRUE;
1428 }
1429 else
1430 {
1431 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1432 {
1433 /* Report what's missing if the matching ":try" is not in its
1434 * finally clause. */
Bram Moolenaar12805862005-01-05 22:16:17 +00001435 eap->errmsg = get_end_emsg(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 skip = TRUE;
1437 }
1438 for (idx = cstack->cs_idx; idx > 0; --idx)
1439 if (cstack->cs_flags[idx] & CSF_TRY)
1440 break;
1441 if (cstack->cs_flags[idx] & CSF_FINALLY)
1442 {
1443 /* Give up for a ":catch" after ":finally" and ignore it.
1444 * Just parse. */
1445 eap->errmsg = (char_u *)N_("E604: :catch after :finally");
1446 give_up = TRUE;
1447 }
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001448 else
Bram Moolenaar12805862005-01-05 22:16:17 +00001449 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
1450 &cstack->cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452
1453 if (ends_excmd(*eap->arg)) /* no argument, catch all errors */
1454 {
1455 pat = (char_u *)".*";
1456 end = NULL;
1457 eap->nextcmd = find_nextcmd(eap->arg);
1458 }
1459 else
1460 {
1461 pat = eap->arg + 1;
1462 end = skip_regexp(pat, *eap->arg, TRUE, NULL);
1463 }
1464
1465 if (!give_up)
1466 {
1467 /*
1468 * Don't do something when no exception has been thrown or when the
1469 * corresponding try block never got active (because of an inactive
1470 * surrounding conditional or after an error or interrupt or throw).
1471 */
1472 if (!did_throw || !(cstack->cs_flags[idx] & CSF_TRUE))
1473 skip = TRUE;
1474
1475 /*
1476 * Check for a match only if an exception is thrown but not caught by
1477 * a previous ":catch". An exception that has replaced a discarded
1478 * exception is not checked (THROWN is not set then).
1479 */
1480 if (!skip && (cstack->cs_flags[idx] & CSF_THROWN)
1481 && !(cstack->cs_flags[idx] & CSF_CAUGHT))
1482 {
1483 if (end != NULL && *end != NUL && !ends_excmd(*skipwhite(end + 1)))
1484 {
1485 EMSG(_(e_trailing));
1486 return;
1487 }
1488
1489 /* When debugging or a breakpoint was encountered, display the
1490 * debug prompt (if not already done) before checking for a match.
1491 * This is a helpful hint for the user when the regular expression
1492 * matching fails. Handle a ">quit" debug command as if an
1493 * interrupt had occurred before the ":catch". That is, discard
1494 * the original exception, replace it by an interrupt exception,
1495 * and don't catch it in this try block. */
1496 if (!dbg_check_skipped(eap) || !do_intthrow(cstack))
1497 {
1498 /* Terminate the pattern and avoid the 'l' flag in 'cpoptions'
1499 * while compiling it. */
1500 if (end != NULL)
1501 {
1502 save_char = *end;
1503 *end = NUL;
1504 }
1505 save_cpo = p_cpo;
1506 p_cpo = (char_u *)"";
1507 regmatch.regprog = vim_regcomp(pat, TRUE);
1508 regmatch.rm_ic = FALSE;
1509 if (end != NULL)
1510 *end = save_char;
1511 p_cpo = save_cpo;
1512 if (regmatch.regprog == NULL)
1513 EMSG2(_(e_invarg2), pat);
1514 else
1515 {
1516 /*
1517 * Save the value of got_int and reset it. We don't want
1518 * a previous interruption cancel matching, only hitting
1519 * CTRL-C while matching should abort it.
1520 */
1521 prev_got_int = got_int;
1522 got_int = FALSE;
1523 caught = vim_regexec_nl(&regmatch, current_exception->value,
1524 (colnr_T)0);
1525 got_int |= prev_got_int;
1526 vim_free(regmatch.regprog);
1527 }
1528 }
1529 }
1530
1531 if (caught)
1532 {
1533 /* Make this ":catch" clause active and reset did_emsg, got_int,
1534 * and did_throw. Put the exception on the caught stack. */
1535 cstack->cs_flags[idx] |= CSF_ACTIVE | CSF_CAUGHT;
1536 did_emsg = got_int = did_throw = FALSE;
1537 catch_exception((except_T *)cstack->cs_exception[idx]);
1538 /* It's mandatory that the current exception is stored in the cstack
1539 * so that it can be discarded at the next ":catch", ":finally", or
1540 * ":endtry" or when the catch clause is left by a ":continue",
1541 * ":break", ":return", ":finish", error, interrupt, or another
1542 * exception. */
1543 if (cstack->cs_exception[cstack->cs_idx] != current_exception)
1544 EMSG(_(e_internal));
1545 }
1546 else
1547 {
1548 /*
1549 * If there is a preceding catch clause and it caught the exception,
1550 * finish the exception now. This happens also after errors except
1551 * when this ":catch" was after the ":finally" or not within
1552 * a ":try". Make the try conditional inactive so that the
1553 * following catch clauses are skipped. On an error or interrupt
1554 * after the preceding try block or catch clause was left by
1555 * a ":continue", ":break", ":return", or ":finish", discard the
1556 * pending action.
1557 */
1558 cleanup_conditionals(cstack, CSF_TRY, TRUE);
1559 }
1560 }
1561
1562 if (end != NULL)
1563 eap->nextcmd = find_nextcmd(end);
1564}
1565
1566/*
1567 * ":finally"
1568 */
1569 void
1570ex_finally(eap)
1571 exarg_T *eap;
1572{
1573 int idx;
1574 int skip = FALSE;
1575 int pending = CSTP_NONE;
1576 struct condstack *cstack = eap->cstack;
1577
1578 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1579 eap->errmsg = (char_u *)N_("E606: :finally without :try");
1580 else
1581 {
1582 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1583 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001584 eap->errmsg = get_end_emsg(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 for (idx = cstack->cs_idx - 1; idx > 0; --idx)
1586 if (cstack->cs_flags[idx] & CSF_TRY)
1587 break;
1588 /* Make this error pending, so that the commands in the following
1589 * finally clause can be executed. This overrules also a pending
1590 * ":continue", ":break", ":return", or ":finish". */
1591 pending = CSTP_ERROR;
1592 }
1593 else
1594 idx = cstack->cs_idx;
1595
1596 if (cstack->cs_flags[idx] & CSF_FINALLY)
1597 {
1598 /* Give up for a multiple ":finally" and ignore it. */
1599 eap->errmsg = (char_u *)N_("E607: multiple :finally");
1600 return;
1601 }
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001602 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
Bram Moolenaar12805862005-01-05 22:16:17 +00001603 &cstack->cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
1605 /*
1606 * Don't do something when the corresponding try block never got active
1607 * (because of an inactive surrounding conditional or after an error or
1608 * interrupt or throw) or for a ":finally" without ":try" or a multiple
1609 * ":finally". After every other error (did_emsg or the conditional
1610 * errors detected above) or after an interrupt (got_int) or an
1611 * exception (did_throw), the finally clause must be executed.
1612 */
1613 skip = !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE);
1614
1615 if (!skip)
1616 {
1617 /* When debugging or a breakpoint was encountered, display the
1618 * debug prompt (if not already done). The user then knows that the
1619 * finally clause is executed. */
1620 if (dbg_check_skipped(eap))
1621 {
1622 /* Handle a ">quit" debug command as if an interrupt had
1623 * occurred before the ":finally". That is, discard the
1624 * original exception and replace it by an interrupt
1625 * exception. */
1626 (void)do_intthrow(cstack);
1627 }
1628
1629 /*
1630 * If there is a preceding catch clause and it caught the exception,
1631 * finish the exception now. This happens also after errors except
1632 * when this is a multiple ":finally" or one not within a ":try".
1633 * After an error or interrupt, this also discards a pending
1634 * ":continue", ":break", ":finish", or ":return" from the preceding
1635 * try block or catch clause.
1636 */
1637 cleanup_conditionals(cstack, CSF_TRY, FALSE);
1638
1639 /*
1640 * Make did_emsg, got_int, did_throw pending. If set, they overrule
1641 * a pending ":continue", ":break", ":return", or ":finish". Then
1642 * we have particularly to discard a pending return value (as done
1643 * by the call to cleanup_conditionals() above when did_emsg or
1644 * got_int is set). The pending values are restored by the
1645 * ":endtry", except if there is a new error, interrupt, exception,
1646 * ":continue", ":break", ":return", or ":finish" in the following
Bram Moolenaar12805862005-01-05 22:16:17 +00001647 * finally clause. A missing ":endwhile", ":endfor" or ":endif"
1648 * detected here is treated as if did_emsg and did_throw had
1649 * already been set, respectively in case that the error is not
1650 * converted to an exception, did_throw had already been unset.
1651 * We must not set did_emsg here since that would suppress the
1652 * error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 */
1654 if (pending == CSTP_ERROR || did_emsg || got_int || did_throw)
1655 {
1656 if (cstack->cs_pending[cstack->cs_idx] == CSTP_RETURN)
1657 {
1658 report_discard_pending(CSTP_RETURN,
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001659 cstack->cs_rettv[cstack->cs_idx]);
1660 discard_pending_return(cstack->cs_rettv[cstack->cs_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
1662 if (pending == CSTP_ERROR && !did_emsg)
1663 pending |= (THROW_ON_ERROR) ? CSTP_THROW : 0;
1664 else
1665 pending |= did_throw ? CSTP_THROW : 0;
1666 pending |= did_emsg ? CSTP_ERROR : 0;
1667 pending |= got_int ? CSTP_INTERRUPT : 0;
1668 cstack->cs_pending[cstack->cs_idx] = pending;
1669
1670 /* It's mandatory that the current exception is stored in the
1671 * cstack so that it can be rethrown at the ":endtry" or be
1672 * discarded if the finally clause is left by a ":continue",
1673 * ":break", ":return", ":finish", error, interrupt, or another
1674 * exception. When emsg() is called for a missing ":endif" or
Bram Moolenaar12805862005-01-05 22:16:17 +00001675 * a missing ":endwhile"/":endfor" detected here, the
1676 * exception will be discarded. */
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001677 if (did_throw && cstack->cs_exception[cstack->cs_idx]
1678 != current_exception)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 EMSG(_(e_internal));
1680 }
1681
1682 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001683 * Set CSL_HAD_FINA, so do_cmdline() will reset did_emsg,
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001684 * got_int, and did_throw and make the finally clause active.
1685 * This will happen after emsg() has been called for a missing
Bram Moolenaar12805862005-01-05 22:16:17 +00001686 * ":endif" or a missing ":endwhile"/":endfor" detected here, so
1687 * that the following finally clause will be executed even then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001689 cstack->cs_lflags |= CSL_HAD_FINA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 }
1691 }
1692}
1693
1694/*
1695 * ":endtry"
1696 */
1697 void
1698ex_endtry(eap)
1699 exarg_T *eap;
1700{
1701 int idx;
1702 int skip;
1703 int rethrow = FALSE;
1704 int pending = CSTP_NONE;
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001705 void *rettv = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 struct condstack *cstack = eap->cstack;
1707
1708 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1709 eap->errmsg = (char_u *)N_("E602: :endtry without :try");
1710 else
1711 {
1712 /*
1713 * Don't do something after an error, interrupt or throw in the try
1714 * block, catch clause, or finally clause preceding this ":endtry" or
1715 * when an error or interrupt occurred after a ":continue", ":break",
1716 * ":return", or ":finish" in a try block or catch clause preceding this
1717 * ":endtry" or when the try block never got active (because of an
1718 * inactive surrounding conditional or after an error or interrupt or
1719 * throw) or when there is a surrounding conditional and it has been
1720 * made inactive by a ":continue", ":break", ":return", or ":finish" in
1721 * the finally clause. The latter case need not be tested since then
1722 * anything pending has already been discarded. */
1723 skip = did_emsg || got_int || did_throw ||
1724 !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE);
1725
1726 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1727 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001728 eap->errmsg = get_end_emsg(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /* Find the matching ":try" and report what's missing. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 idx = cstack->cs_idx;
1731 do
1732 --idx;
1733 while (idx > 0 && !(cstack->cs_flags[idx] & CSF_TRY));
Bram Moolenaar12805862005-01-05 22:16:17 +00001734 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
1735 &cstack->cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 skip = TRUE;
1737
1738 /*
1739 * If an exception is being thrown, discard it to prevent it from
1740 * being rethrown at the end of this function. It would be
1741 * discarded by the error message, anyway. Resets did_throw.
1742 * This does not affect the script termination due to the error
1743 * since "trylevel" is decremented after emsg() has been called.
1744 */
1745 if (did_throw)
1746 discard_current_exception();
1747 }
1748 else
1749 {
1750 idx = cstack->cs_idx;
1751
1752 /*
1753 * If we stopped with the exception currently being thrown at this
1754 * try conditional since we didn't know that it doesn't have
1755 * a finally clause, we need to rethrow it after closing the try
1756 * conditional.
1757 */
1758 if (did_throw && (cstack->cs_flags[idx] & CSF_TRUE)
1759 && !(cstack->cs_flags[idx] & CSF_FINALLY))
1760 rethrow = TRUE;
1761 }
1762
1763 /* If there was no finally clause, show the user when debugging or
1764 * a breakpoint was encountered that the end of the try conditional has
1765 * been reached: display the debug prompt (if not already done). Do
1766 * this on normal control flow or when an exception was thrown, but not
1767 * on an interrupt or error not converted to an exception or when
1768 * a ":break", ":continue", ":return", or ":finish" is pending. These
1769 * actions are carried out immediately.
1770 */
1771 if ((rethrow || (!skip
1772 && !(cstack->cs_flags[idx] & CSF_FINALLY)
1773 && !cstack->cs_pending[idx]))
1774 && dbg_check_skipped(eap))
1775 {
1776 /* Handle a ">quit" debug command as if an interrupt had occurred
1777 * before the ":endtry". That is, throw an interrupt exception and
1778 * set "skip" and "rethrow". */
1779 if (got_int)
1780 {
1781 skip = TRUE;
1782 (void)do_intthrow(cstack);
1783 /* The do_intthrow() call may have reset did_throw or
1784 * cstack->cs_pending[idx].*/
1785 rethrow = FALSE;
1786 if (did_throw && !(cstack->cs_flags[idx] & CSF_FINALLY))
1787 rethrow = TRUE;
1788 }
1789 }
1790
1791 /*
1792 * If a ":return" is pending, we need to resume it after closing the
1793 * try conditional; remember the return value. If there was a finally
1794 * clause making an exception pending, we need to rethrow it. Make it
1795 * the exception currently being thrown.
1796 */
1797 if (!skip)
1798 {
1799 pending = cstack->cs_pending[idx];
1800 cstack->cs_pending[idx] = CSTP_NONE;
1801 if (pending == CSTP_RETURN)
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001802 rettv = cstack->cs_rettv[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 else if (pending & CSTP_THROW)
1804 current_exception = cstack->cs_exception[idx];
1805 }
1806
1807 /*
1808 * Discard anything pending on an error, interrupt, or throw in the
1809 * finally clause. If there was no ":finally", discard a pending
1810 * ":continue", ":break", ":return", or ":finish" if an error or
1811 * interrupt occurred afterwards, but before the ":endtry" was reached.
1812 * If an exception was caught by the last of the catch clauses and there
1813 * was no finally clause, finish the exception now. This happens also
1814 * after errors except when this ":endtry" is not within a ":try".
1815 * Restore "emsg_silent" if it has been reset by this try conditional.
1816 */
1817 cleanup_conditionals(cstack, CSF_TRY | CSF_SILENT, TRUE);
1818
1819 --cstack->cs_idx;
1820 --cstack->cs_trylevel;
1821
1822 if (!skip)
1823 {
1824 report_resume_pending(pending,
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001825 (pending == CSTP_RETURN) ? rettv :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 (pending & CSTP_THROW) ? (void *)current_exception : NULL);
1827 switch (pending)
1828 {
1829 case CSTP_NONE:
1830 break;
1831
1832 /* Reactivate a pending ":continue", ":break", ":return",
1833 * ":finish" from the try block or a catch clause of this try
1834 * conditional. This is skipped, if there was an error in an
1835 * (unskipped) conditional command or an interrupt afterwards
1836 * or if the finally clause is present and executed a new error,
1837 * interrupt, throw, ":continue", ":break", ":return", or
1838 * ":finish". */
1839 case CSTP_CONTINUE:
1840 ex_continue(eap);
1841 break;
1842 case CSTP_BREAK:
1843 ex_break(eap);
1844 break;
1845 case CSTP_RETURN:
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001846 do_return(eap, FALSE, FALSE, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 break;
1848 case CSTP_FINISH:
1849 do_finish(eap, FALSE);
1850 break;
1851
1852 /* When the finally clause was entered due to an error,
1853 * interrupt or throw (as opposed to a ":continue", ":break",
1854 * ":return", or ":finish"), restore the pending values of
1855 * did_emsg, got_int, and did_throw. This is skipped, if there
1856 * was a new error, interrupt, throw, ":continue", ":break",
1857 * ":return", or ":finish". in the finally clause. */
1858 default:
1859 if (pending & CSTP_ERROR)
1860 did_emsg = TRUE;
1861 if (pending & CSTP_INTERRUPT)
1862 got_int = TRUE;
1863 if (pending & CSTP_THROW)
1864 rethrow = TRUE;
1865 break;
1866 }
1867 }
1868
1869 if (rethrow)
1870 /* Rethrow the current exception (within this cstack). */
1871 do_throw(cstack);
1872 }
1873}
1874
1875/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001876 * enter_cleanup() and leave_cleanup()
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001877 *
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001878 * Functions to be called before/after invoking a sequence of autocommands for
1879 * cleanup for a failed command. (Failure means here that a call to emsg()
1880 * has been made, an interrupt occurred, or there is an uncaught exception
1881 * from a previous autocommand execution of the same command.)
1882 *
1883 * Call enter_cleanup() with a pointer to a cleanup_T and pass the same
1884 * pointer to leave_cleanup(). The cleanup_T structure stores the pending
1885 * error/interrupt/exception state.
1886 */
1887
1888/*
1889 * This function works a bit like ex_finally() except that there was not
1890 * actually an extra try block around the part that failed and an error or
1891 * interrupt has not (yet) been converted to an exception. This function
1892 * saves the error/interrupt/ exception state and prepares for the call to
1893 * do_cmdline() that is going to be made for the cleanup autocommand
1894 * execution.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001895 */
1896 void
1897enter_cleanup(csp)
1898 cleanup_T *csp;
1899{
1900 int pending = CSTP_NONE;
1901
1902 /*
1903 * Postpone did_emsg, got_int, did_throw. The pending values will be
1904 * restored by leave_cleanup() except if there was an aborting error,
1905 * interrupt, or uncaught exception after this function ends.
1906 */
1907 if (did_emsg || got_int || did_throw || need_rethrow)
1908 {
1909 csp->pending = (did_emsg ? CSTP_ERROR : 0)
1910 | (got_int ? CSTP_INTERRUPT : 0)
1911 | (did_throw ? CSTP_THROW : 0)
1912 | (need_rethrow ? CSTP_THROW : 0);
1913
1914 /* If we are currently throwing an exception (did_throw), save it as
1915 * well. On an error not yet converted to an exception, update
1916 * "force_abort" and reset "cause_abort" (as do_errthrow() would do).
1917 * This is needed for the do_cmdline() call that is going to be made
1918 * for autocommand execution. We need not save *msg_list because
1919 * there is an extra instance for every call of do_cmdline(), anyway.
1920 */
1921 if (did_throw || need_rethrow)
1922 csp->exception = current_exception;
1923 else
1924 {
1925 csp->exception = NULL;
1926 if (did_emsg)
1927 {
1928 force_abort |= cause_abort;
1929 cause_abort = FALSE;
1930 }
1931 }
1932 did_emsg = got_int = did_throw = need_rethrow = FALSE;
1933
1934 /* Report if required by the 'verbose' option or when debugging. */
1935 report_make_pending(pending, csp->exception);
1936 }
1937 else
1938 {
1939 csp->pending = CSTP_NONE;
1940 csp->exception = NULL;
1941 }
1942}
1943
1944/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001945 * See comment above enter_cleanup() for how this function is used.
1946 *
1947 * This function is a bit like ex_endtry() except that there was not actually
1948 * an extra try block around the part that failed and an error or interrupt
1949 * had not (yet) been converted to an exception when the cleanup autocommand
1950 * sequence was invoked.
1951 *
1952 * This function has to be called with the address of the cleanup_T structure
1953 * filled by enter_cleanup() as an argument; it restores the error/interrupt/
1954 * exception state saved by that function - except there was an aborting
1955 * error, an interrupt or an uncaught exception during execution of the
1956 * cleanup autocommands. In the latter case, the saved error/interrupt/
1957 * exception state is discarded.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001958 */
1959 void
1960leave_cleanup(csp)
1961 cleanup_T *csp;
1962{
1963 int pending = csp->pending;
1964
1965 if (pending == CSTP_NONE) /* nothing to do */
1966 return;
1967
1968 /* If there was an aborting error, an interrupt, or an uncaught exception
1969 * after the corresponding call to enter_cleanup(), discard what has been
1970 * made pending by it. Report this to the user if required by the
1971 * 'verbose' option or when debugging. */
1972 if (aborting() || need_rethrow)
1973 {
1974 if (pending & CSTP_THROW)
1975 /* Cancel the pending exception (includes report). */
1976 discard_exception((except_T *)csp->exception, FALSE);
1977 else
1978 report_discard_pending(pending, NULL);
1979
1980 /* If an error was about to be converted to an exception when
1981 * enter_cleanup() was called, free the message list. */
1982 free_msglist(*msg_list);
1983 *msg_list = NULL;
1984 }
1985
1986 /*
1987 * If there was no new error, interrupt, or throw between the calls
1988 * to enter_cleanup() and leave_cleanup(), restore the pending
1989 * error/interrupt/exception state.
1990 */
1991 else
1992 {
1993 /*
1994 * If there was an exception being thrown when enter_cleanup() was
1995 * called, we need to rethrow it. Make it the exception currently
1996 * being thrown.
1997 */
1998 if (pending & CSTP_THROW)
1999 current_exception = csp->exception;
2000
2001 /*
2002 * If an error was about to be converted to an exception when
2003 * enter_cleanup() was called, let "cause_abort" take the part of
2004 * "force_abort" (as done by cause_errthrow()).
2005 */
2006 else if (pending & CSTP_ERROR)
2007 {
2008 cause_abort = force_abort;
2009 force_abort = FALSE;
2010 }
2011
2012 /*
2013 * Restore the pending values of did_emsg, got_int, and did_throw.
2014 */
2015 if (pending & CSTP_ERROR)
2016 did_emsg = TRUE;
2017 if (pending & CSTP_INTERRUPT)
2018 got_int = TRUE;
2019 if (pending & CSTP_THROW)
2020 need_rethrow = TRUE; /* did_throw will be set by do_one_cmd() */
2021
2022 /* Report if required by the 'verbose' option or when debugging. */
2023 report_resume_pending(pending,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002024 (pending & CSTP_THROW) ? (void *)current_exception : NULL);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002025 }
2026}
2027
2028
2029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 * Make conditionals inactive and discard what's pending in finally clauses
2031 * until the conditional type searched for or a try conditional not in its
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002032 * finally clause is reached. If this is in an active catch clause, finish
2033 * the caught exception.
2034 * Return the cstack index where the search stopped.
Bram Moolenaar12805862005-01-05 22:16:17 +00002035 * Values used for "searched_cond" are (CSF_WHILE | CSF_FOR) or CSF_TRY or 0,
2036 * the latter meaning the innermost try conditional not in its finally clause.
2037 * "inclusive" tells whether the conditional searched for should be made
2038 * inactive itself (a try conditional not in its finally claused possibly find
2039 * before is always made inactive). If "inclusive" is TRUE and
2040 * "searched_cond" is CSF_TRY|CSF_SILENT, the saved former value of
2041 * "emsg_silent", if reset when the try conditional finally reached was
2042 * entered, is restored (unsed by ex_endtry()). This is normally done only
2043 * when such a try conditional is left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 */
2045 int
2046cleanup_conditionals(cstack, searched_cond, inclusive)
2047 struct condstack *cstack;
2048 int searched_cond;
2049 int inclusive;
2050{
2051 int idx;
2052 int stop = FALSE;
2053
2054 for (idx = cstack->cs_idx; idx >= 0; --idx)
2055 {
2056 if (cstack->cs_flags[idx] & CSF_TRY)
2057 {
2058 /*
2059 * Discard anything pending in a finally clause and continue the
2060 * search. There may also be a pending ":continue", ":break",
2061 * ":return", or ":finish" before the finally clause. We must not
2062 * discard it, unless an error or interrupt occurred afterwards.
2063 */
2064 if (did_emsg || got_int ||
2065 (cstack->cs_flags[idx] & CSF_FINALLY))
2066 {
2067 switch (cstack->cs_pending[idx])
2068 {
2069 case CSTP_NONE:
2070 break;
2071
2072 case CSTP_CONTINUE:
2073 case CSTP_BREAK:
2074 case CSTP_FINISH:
2075 report_discard_pending(cstack->cs_pending[idx], NULL);
2076 cstack->cs_pending[idx] = CSTP_NONE;
2077 break;
2078
2079 case CSTP_RETURN:
2080 report_discard_pending(CSTP_RETURN,
Bram Moolenaarfca34d62005-01-04 21:38:36 +00002081 cstack->cs_rettv[idx]);
2082 discard_pending_return(cstack->cs_rettv[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 cstack->cs_pending[idx] = CSTP_NONE;
2084 break;
2085
2086 default:
2087 if (cstack->cs_flags[idx] & CSF_FINALLY)
2088 {
2089 if (cstack->cs_pending[idx] & CSTP_THROW)
2090 {
2091 /* Cancel the pending exception. This is in the
2092 * finally clause, so that the stack of the
2093 * caught exceptions is not involved. */
2094 discard_exception((except_T *)
2095 cstack->cs_exception[idx],
2096 FALSE);
2097 }
2098 else
2099 report_discard_pending(cstack->cs_pending[idx],
2100 NULL);
2101 cstack->cs_pending[idx] = CSTP_NONE;
2102 }
2103 break;
2104 }
2105 }
2106
2107 /*
2108 * Stop at a try conditional not in its finally clause. If this try
2109 * conditional is in an active catch clause, finish the caught
2110 * exception.
2111 */
2112 if (!(cstack->cs_flags[idx] & CSF_FINALLY))
2113 {
2114 if ((cstack->cs_flags[idx] & CSF_ACTIVE)
2115 && (cstack->cs_flags[idx] & CSF_CAUGHT))
2116 finish_exception((except_T *)cstack->cs_exception[idx]);
2117 /* Stop at this try conditional - except the try block never
2118 * got active (because of an inactive surrounding conditional
2119 * or when the ":try" appeared after an error or interrupt or
2120 * throw). */
2121 if (cstack->cs_flags[idx] & CSF_TRUE)
2122 {
2123 if (searched_cond == 0 && !inclusive)
2124 break;
2125 stop = TRUE;
2126 }
2127 }
2128 }
2129
2130 /* Stop on the searched conditional type (even when the surrounding
2131 * conditional is not active or something has been made pending).
2132 * If "inclusive" is TRUE and "searched_cond" is CSF_TRY|CSF_SILENT,
2133 * check first whether "emsg_silent" needs to be restored. */
2134 if (cstack->cs_flags[idx] & searched_cond)
2135 {
2136 if (!inclusive)
2137 break;
2138 stop = TRUE;
2139 }
2140 cstack->cs_flags[idx] &= ~CSF_ACTIVE;
2141 if (stop && searched_cond != (CSF_TRY | CSF_SILENT))
2142 break;
2143
2144 /*
2145 * When leaving a try conditinal that reset "emsg_silent" on its entry
2146 * after saving the original value, restore that value here and free the
2147 * memory used to store it.
2148 */
2149 if ((cstack->cs_flags[idx] & CSF_TRY)
2150 && (cstack->cs_flags[idx] & CSF_SILENT))
2151 {
2152 eslist_T *elem;
2153
2154 elem = cstack->cs_emsg_silent_list;
2155 cstack->cs_emsg_silent_list = elem->next;
2156 emsg_silent = elem->saved_emsg_silent;
2157 vim_free(elem);
2158 cstack->cs_flags[idx] &= ~CSF_SILENT;
2159 }
2160 if (stop)
2161 break;
2162 }
2163 return idx;
2164}
2165
2166/*
Bram Moolenaar12805862005-01-05 22:16:17 +00002167 * Return an appropriate error message for a missing endwhile/endfor/endif.
2168 */
2169 static char_u *
2170get_end_emsg(cstack)
2171 struct condstack *cstack;
2172{
2173 if (cstack->cs_flags[cstack->cs_idx] & CSF_WHILE)
2174 return e_endwhile;
2175 if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR)
2176 return e_endfor;
2177 return e_endif;
2178}
2179
2180
2181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 * Rewind conditionals until index "idx" is reached. "cond_type" and
2183 * "cond_level" specify a conditional type and the address of a level variable
2184 * which is to be decremented with each skipped conditional of the specified
2185 * type.
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002186 * Also free "for info" structures where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 */
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002188 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189rewind_conditionals(cstack, idx, cond_type, cond_level)
2190 struct condstack *cstack;
2191 int idx;
2192 int cond_type;
2193 int *cond_level;
2194{
2195 while (cstack->cs_idx > idx)
2196 {
2197 if (cstack->cs_flags[cstack->cs_idx] & cond_type)
2198 --*cond_level;
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002199 if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR)
2200 free_for_info(cstack->cs_forinfo[cstack->cs_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 --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 */