blob: d6c4ebee784b96f1d48d3298dbea2daeebb4295e [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 {
Bram Moolenaar051b7822005-05-19 21:00:46 +0000487 if (*p == NUL || p == mesg)
488 STRCAT(val, mesg); /* 'E123' missing or at beginning */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 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 */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000528 smsg((char_u *)_("Exception thrown: %s"), excp->value);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 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 */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000574 smsg(was_finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 ? (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)
Bram Moolenaar051b7822005-05-19 21:00:46 +0000619 vim_snprintf((char *)IObuff, IOSIZE, _("%s, line %ld"),
620 excp->throw_name, (long)excp->throw_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 else
Bram Moolenaar051b7822005-05-19 21:00:46 +0000622 vim_snprintf((char *)IObuff, IOSIZE, "%s", excp->throw_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 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 */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000637 smsg((char_u *)_("Exception caught: %s"), excp->value);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 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)
Bram Moolenaar051b7822005-05-19 21:00:46 +0000662 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 _("%s, line %ld"), caught_stack->throw_name,
664 (long)caught_stack->throw_lnum);
665 else
Bram Moolenaar051b7822005-05-19 21:00:46 +0000666 vim_snprintf((char *)IObuff, IOSIZE, "%s",
667 caught_stack->throw_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 set_vim_var_string(VV_THROWPOINT, IObuff, -1);
669 }
670 else
671 /* throw_name not set on an exception from a command that was
672 * typed. */
673 set_vim_var_string(VV_THROWPOINT, NULL, -1);
674 }
675 else
676 {
677 set_vim_var_string(VV_EXCEPTION, NULL, -1);
678 set_vim_var_string(VV_THROWPOINT, NULL, -1);
679 }
680
681 /* Discard the exception, but use the finish message for 'verbose'. */
682 discard_exception(excp, TRUE);
683}
684
685/*
686 * Flags specifying the message displayed by report_pending.
687 */
688#define RP_MAKE 0
689#define RP_RESUME 1
690#define RP_DISCARD 2
691
692/*
693 * Report information about something pending in a finally clause if required by
694 * the 'verbose' option or when debugging. "action" tells whether something is
695 * made pending or something pending is resumed or discarded. "pending" tells
696 * what is pending. "value" specifies the return value for a pending ":return"
697 * or the exception value for a pending exception.
698 */
699 static void
700report_pending(action, pending, value)
701 int action;
702 int pending;
703 void *value;
704{
705 char_u *mesg;
706 char *s;
707 int save_msg_silent;
708
709
710 switch (action)
711 {
712 case RP_MAKE:
713 mesg = (char_u *)_("%s made pending");
714 break;
715 case RP_RESUME:
716 mesg = (char_u *)_("%s resumed");
717 break;
718 /* case RP_DISCARD: */
719 default:
720 mesg = (char_u *)_("%s discarded");
721 break;
722 }
723
724 switch (pending)
725 {
726 case CSTP_NONE:
727 return;
728
729 case CSTP_CONTINUE:
730 s = ":continue";
731 break;
732 case CSTP_BREAK:
733 s = ":break";
734 break;
735 case CSTP_FINISH:
736 s = ":finish";
737 break;
738 case CSTP_RETURN:
739 /* ":return" command producing value, allocated */
740 s = (char *)get_return_cmd(value);
741 break;
742
743 default:
744 if (pending & CSTP_THROW)
745 {
Bram Moolenaar051b7822005-05-19 21:00:46 +0000746 vim_snprintf((char *)IObuff, IOSIZE,
747 (char *)mesg, _("Exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 mesg = vim_strnsave(IObuff, (int)STRLEN(IObuff) + 4);
749 STRCAT(mesg, ": %s");
750 s = (char *)((except_T *)value)->value;
751 }
752 else if ((pending & CSTP_ERROR) && (pending & CSTP_INTERRUPT))
753 s = _("Error and interrupt");
754 else if (pending & CSTP_ERROR)
755 s = _("Error");
756 else /* if (pending & CSTP_INTERRUPT) */
757 s = _("Interrupt");
758 }
759
760 save_msg_silent = msg_silent;
761 if (debug_break_level > 0)
762 msg_silent = FALSE; /* display messages */
763 ++no_wait_return;
764 msg_scroll = TRUE; /* always scroll up, don't overwrite */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000765 smsg(mesg, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 msg_puts((char_u *)"\n"); /* don't overwrite this either */
767 cmdline_row = msg_row;
768 --no_wait_return;
769 if (debug_break_level > 0)
770 msg_silent = save_msg_silent;
771
772 if (pending == CSTP_RETURN)
773 vim_free(s);
774 else if (pending & CSTP_THROW)
775 vim_free(mesg);
776}
777
778/*
779 * If something is made pending in a finally clause, report it if required by
780 * the 'verbose' option or when debugging.
781 */
782 void
783report_make_pending(pending, value)
784 int pending;
785 void *value;
786{
787 if (p_verbose >= 14 || debug_break_level > 0)
788 report_pending(RP_MAKE, pending, value);
789}
790
791/*
792 * If something pending in a finally clause is resumed at the ":endtry", report
793 * it if required by the 'verbose' option or when debugging.
794 */
795 void
796report_resume_pending(pending, value)
797 int pending;
798 void *value;
799{
800 if (p_verbose >= 14 || debug_break_level > 0)
801 report_pending(RP_RESUME, pending, value);
802}
803
804/*
805 * If something pending in a finally clause is discarded, report it if required
806 * by the 'verbose' option or when debugging.
807 */
808 void
809report_discard_pending(pending, value)
810 int pending;
811 void *value;
812{
813 if (p_verbose >= 14 || debug_break_level > 0)
814 report_pending(RP_DISCARD, pending, value);
815}
816
817
818/*
819 * ":if".
820 */
821 void
822ex_if(eap)
823 exarg_T *eap;
824{
825 int error;
826 int skip;
827 int result;
828 struct condstack *cstack = eap->cstack;
829
830 if (cstack->cs_idx == CSTACK_LEN - 1)
831 eap->errmsg = (char_u *)N_("E579: :if nesting too deep");
832 else
833 {
834 ++cstack->cs_idx;
835 cstack->cs_flags[cstack->cs_idx] = 0;
836
837 /*
838 * Don't do something after an error, interrupt, or throw, or when there
839 * is a surrounding conditional and it was not active.
840 */
841 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
842 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
843
844 result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
845
846 if (!skip && !error)
847 {
848 if (result)
849 cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE | CSF_TRUE;
850 }
851 else
852 /* set TRUE, so this conditional will never get active */
853 cstack->cs_flags[cstack->cs_idx] = CSF_TRUE;
854 }
855}
856
857/*
858 * ":endif".
859 */
860 void
861ex_endif(eap)
862 exarg_T *eap;
863{
864 did_endif = TRUE;
865 if (eap->cstack->cs_idx < 0
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000866 || (eap->cstack->cs_flags[eap->cstack->cs_idx]
867 & (CSF_WHILE | CSF_FOR | CSF_TRY)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 eap->errmsg = (char_u *)N_("E580: :endif without :if");
869 else
870 {
871 /*
872 * When debugging or a breakpoint was encountered, display the debug
873 * prompt (if not already done). This shows the user that an ":endif"
874 * is executed when the ":if" or a previous ":elseif" was not TRUE.
875 * Handle a ">quit" debug command as if an interrupt had occurred before
876 * the ":endif". That is, throw an interrupt exception if appropriate.
877 * Doing this here prevents an exception for a parsing error being
878 * discarded by throwing the interrupt exception later on.
879 */
880 if (!(eap->cstack->cs_flags[eap->cstack->cs_idx] & CSF_TRUE) &&
881 dbg_check_skipped(eap))
882 (void)do_intthrow(eap->cstack);
883
884 --eap->cstack->cs_idx;
885 }
886}
887
888/*
889 * ":else" and ":elseif".
890 */
891 void
892ex_else(eap)
893 exarg_T *eap;
894{
895 int error;
896 int skip;
897 int result;
898 struct condstack *cstack = eap->cstack;
899
900 /*
901 * Don't do something after an error, interrupt, or throw, or when there is
902 * a surrounding conditional and it was not active.
903 */
904 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
905 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
906
907 if (cstack->cs_idx < 0
Bram Moolenaar12805862005-01-05 22:16:17 +0000908 || (cstack->cs_flags[cstack->cs_idx]
909 & (CSF_WHILE | CSF_FOR | CSF_TRY)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 {
911 if (eap->cmdidx == CMD_else)
912 {
913 eap->errmsg = (char_u *)N_("E581: :else without :if");
914 return;
915 }
916 eap->errmsg = (char_u *)N_("E582: :elseif without :if");
917 skip = TRUE;
918 }
919 else if (cstack->cs_flags[cstack->cs_idx] & CSF_ELSE)
920 {
921 if (eap->cmdidx == CMD_else)
922 {
923 eap->errmsg = (char_u *)N_("E583: multiple :else");
924 return;
925 }
926 eap->errmsg = (char_u *)N_("E584: :elseif after :else");
927 skip = TRUE;
928 }
929
930 /* if skipping or the ":if" was TRUE, reset ACTIVE, otherwise set it */
931 if (skip || cstack->cs_flags[cstack->cs_idx] & CSF_TRUE)
932 {
933 if (eap->errmsg == NULL)
934 cstack->cs_flags[cstack->cs_idx] = CSF_TRUE;
935 skip = TRUE; /* don't evaluate an ":elseif" */
936 }
937 else
938 cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE;
939
940 /*
941 * When debugging or a breakpoint was encountered, display the debug prompt
942 * (if not already done). This shows the user that an ":else" or ":elseif"
943 * is executed when the ":if" or previous ":elseif" was not TRUE. Handle
944 * a ">quit" debug command as if an interrupt had occurred before the
945 * ":else" or ":elseif". That is, set "skip" and throw an interrupt
946 * exception if appropriate. Doing this here prevents that an exception
947 * for a parsing errors is discarded when throwing the interrupt exception
948 * later on.
949 */
950 if (!skip && dbg_check_skipped(eap) && got_int)
951 {
952 (void)do_intthrow(cstack);
953 skip = TRUE;
954 }
955
956 if (eap->cmdidx == CMD_elseif)
957 {
958 result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
959 /* When throwing error exceptions, we want to throw always the first
960 * of several errors in a row. This is what actually happens when
961 * a conditional error was detected above and there is another failure
962 * when parsing the expression. Since the skip flag is set in this
963 * case, the parsing error will be ignored by emsg(). */
964
965 if (!skip && !error)
966 {
967 if (result)
968 cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE | CSF_TRUE;
969 else
970 cstack->cs_flags[cstack->cs_idx] = 0;
971 }
972 else if (eap->errmsg == NULL)
973 /* set TRUE, so this conditional will never get active */
974 cstack->cs_flags[cstack->cs_idx] = CSF_TRUE;
975 }
976 else
977 cstack->cs_flags[cstack->cs_idx] |= CSF_ELSE;
978}
979
980/*
Bram Moolenaar12805862005-01-05 22:16:17 +0000981 * Handle ":while" and ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 */
983 void
984ex_while(eap)
985 exarg_T *eap;
986{
987 int error;
988 int skip;
989 int result;
990 struct condstack *cstack = eap->cstack;
991
992 if (cstack->cs_idx == CSTACK_LEN - 1)
Bram Moolenaar12805862005-01-05 22:16:17 +0000993 eap->errmsg = (char_u *)N_("E585: :while/:for nesting too deep");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 else
995 {
996 /*
Bram Moolenaar12805862005-01-05 22:16:17 +0000997 * The loop flag is set when we have jumped back from the matching
998 * ":endwhile" or ":endfor". When not set, need to initialise this
999 * cstack entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001001 if ((cstack->cs_lflags & CSL_HAD_LOOP) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 {
1003 ++cstack->cs_idx;
Bram Moolenaar12805862005-01-05 22:16:17 +00001004 ++cstack->cs_looplevel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 cstack->cs_line[cstack->cs_idx] = -1;
1006 }
Bram Moolenaar12805862005-01-05 22:16:17 +00001007 cstack->cs_flags[cstack->cs_idx] =
1008 eap->cmdidx == CMD_while ? CSF_WHILE : CSF_FOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
1010 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001011 * Don't do something after an error, interrupt, or throw, or when
1012 * there is a surrounding conditional and it was not active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 */
1014 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
1015 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
Bram Moolenaar12805862005-01-05 22:16:17 +00001016 if (eap->cmdidx == CMD_while)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001018 /*
1019 * ":while bool-expr"
1020 */
1021 result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 else
1024 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001025 void *fi;
1026
1027 /*
1028 * ":for var in list-expr"
1029 */
1030 if ((cstack->cs_lflags & CSL_HAD_LOOP) != 0)
1031 {
1032 /* Jumping here from a ":continue" or ":endfor": use the
1033 * previously evaluated list. */
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001034 fi = cstack->cs_forinfo[cstack->cs_idx];
Bram Moolenaar12805862005-01-05 22:16:17 +00001035 error = FALSE;
1036 }
1037 else
1038 {
1039 /* Evaluate the argument and get the info in a structure. */
1040 fi = eval_for_line(eap->arg, &error, &eap->nextcmd, skip);
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001041 cstack->cs_forinfo[cstack->cs_idx] = fi;
Bram Moolenaar12805862005-01-05 22:16:17 +00001042 }
1043
1044 /* use the element at the start of the list and advance */
1045 if (!error && fi != NULL && !skip)
1046 result = next_for_item(fi, eap->arg);
1047 else
1048 result = FALSE;
1049
1050 if (!result)
1051 {
1052 free_for_info(fi);
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001053 cstack->cs_forinfo[cstack->cs_idx] = NULL;
Bram Moolenaar12805862005-01-05 22:16:17 +00001054 }
1055 }
1056
1057 /*
1058 * If this cstack entry was just initialised and is active, set the
1059 * loop flag, so do_cmdline() will set the line number in cs_line[].
1060 * If executing the command a second time, clear the loop flag.
1061 */
1062 if (!skip && !error && result)
1063 {
1064 cstack->cs_flags[cstack->cs_idx] |= (CSF_ACTIVE | CSF_TRUE);
1065 cstack->cs_lflags ^= CSL_HAD_LOOP;
1066 }
1067 else
1068 {
1069 cstack->cs_lflags &= ~CSL_HAD_LOOP;
1070 /* If the ":while" evaluates to FALSE or ":for" is past the end of
1071 * the list, show the debug prompt at the ":endwhile"/":endfor" as
1072 * if there was a ":break" in a ":while"/":for" evaluating to
1073 * TRUE. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (!skip && !error)
1075 cstack->cs_flags[cstack->cs_idx] |= CSF_TRUE;
1076 }
1077 }
1078}
1079
1080/*
1081 * ":continue"
1082 */
1083 void
1084ex_continue(eap)
1085 exarg_T *eap;
1086{
1087 int idx;
1088 struct condstack *cstack = eap->cstack;
1089
Bram Moolenaar12805862005-01-05 22:16:17 +00001090 if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0)
1091 eap->errmsg = (char_u *)N_("E586: :continue without :while or :for");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 else
1093 {
1094 /* Try to find the matching ":while". This might stop at a try
1095 * conditional not in its finally clause (which is then to be executed
1096 * next). Therefor, inactivate all conditionals except the ":while"
1097 * itself (if reached). */
Bram Moolenaar12805862005-01-05 22:16:17 +00001098 idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
1099 if ((cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 {
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001101 rewind_conditionals(cstack, idx, CSF_TRY, &cstack->cs_trylevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
1103 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001104 * Set CSL_HAD_CONT, so do_cmdline() will jump back to the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * matching ":while".
1106 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001107 cstack->cs_lflags |= CSL_HAD_CONT; /* let do_cmdline() handle it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 }
1109 else
1110 {
1111 /* If a try conditional not in its finally clause is reached first,
1112 * make the ":continue" pending for execution at the ":endtry". */
1113 cstack->cs_pending[idx] = CSTP_CONTINUE;
1114 report_make_pending(CSTP_CONTINUE, NULL);
1115 }
1116 }
1117}
1118
1119/*
1120 * ":break"
1121 */
1122 void
1123ex_break(eap)
1124 exarg_T *eap;
1125{
1126 int idx;
1127 struct condstack *cstack = eap->cstack;
1128
Bram Moolenaar12805862005-01-05 22:16:17 +00001129 if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0)
1130 eap->errmsg = (char_u *)N_("E587: :break without :while or :for");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 else
1132 {
1133 /* Inactivate conditionals until the matching ":while" or a try
1134 * conditional not in its finally clause (which is then to be
1135 * executed next) is found. In the latter case, make the ":break"
1136 * pending for execution at the ":endtry". */
Bram Moolenaar12805862005-01-05 22:16:17 +00001137 idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, TRUE);
1138 if (!(cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
1140 cstack->cs_pending[idx] = CSTP_BREAK;
1141 report_make_pending(CSTP_BREAK, NULL);
1142 }
1143 }
1144}
1145
1146/*
Bram Moolenaar12805862005-01-05 22:16:17 +00001147 * ":endwhile" and ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 */
1149 void
1150ex_endwhile(eap)
1151 exarg_T *eap;
1152{
1153 struct condstack *cstack = eap->cstack;
Bram Moolenaar12805862005-01-05 22:16:17 +00001154 int idx;
1155 char_u *err;
1156 int csf;
1157 int fl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar12805862005-01-05 22:16:17 +00001159 if (eap->cmdidx == CMD_endwhile)
1160 {
1161 err = e_while;
1162 csf = CSF_WHILE;
1163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 else
1165 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001166 err = e_for;
1167 csf = CSF_FOR;
1168 }
1169
1170 if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0)
1171 eap->errmsg = err;
1172 else
1173 {
1174 fl = cstack->cs_flags[cstack->cs_idx];
1175 if (!(fl & csf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00001177 /* If we are in a ":while" or ":for" but used the wrong endloop
1178 * command, do not rewind to the next enclosing ":for"/":while". */
Bram Moolenaar12805862005-01-05 22:16:17 +00001179 if (fl & CSF_WHILE)
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00001180 eap->errmsg = (char_u *)_("E732: Using :endfor with :while");
Bram Moolenaar12805862005-01-05 22:16:17 +00001181 else if (fl & CSF_FOR)
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00001182 eap->errmsg = (char_u *)_("E733: Using :endwhile with :for");
1183 }
1184 if (!(fl & (CSF_WHILE | CSF_FOR)))
1185 {
1186 if (!(fl & CSF_TRY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 eap->errmsg = e_endif;
Bram Moolenaar12805862005-01-05 22:16:17 +00001188 else if (fl & CSF_FINALLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 eap->errmsg = e_endtry;
Bram Moolenaar12805862005-01-05 22:16:17 +00001190 /* Try to find the matching ":while" and report what's missing. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 for (idx = cstack->cs_idx; idx > 0; --idx)
1192 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001193 fl = cstack->cs_flags[idx];
1194 if ((fl & CSF_TRY) && !(fl & CSF_FINALLY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {
1196 /* Give up at a try conditional not in its finally clause.
Bram Moolenaar12805862005-01-05 22:16:17 +00001197 * Ignore the ":endwhile"/":endfor". */
1198 eap->errmsg = err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 return;
1200 }
Bram Moolenaar12805862005-01-05 22:16:17 +00001201 if (fl & csf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 break;
1203 }
1204 /* Cleanup and rewind all contained (and unclosed) conditionals. */
Bram Moolenaar12805862005-01-05 22:16:17 +00001205 (void)cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 rewind_conditionals(cstack, idx, CSF_TRY, &cstack->cs_trylevel);
1207 }
1208
1209 /*
1210 * When debugging or a breakpoint was encountered, display the debug
1211 * prompt (if not already done). This shows the user that an
Bram Moolenaar12805862005-01-05 22:16:17 +00001212 * ":endwhile"/":endfor" is executed when the ":while" was not TRUE or
1213 * after a ":break". Handle a ">quit" debug command as if an
1214 * interrupt had occurred before the ":endwhile"/":endfor". That is,
1215 * throw an interrupt exception if appropriate. Doing this here
1216 * prevents that an exception for a parsing error is discarded when
1217 * throwing the interrupt exception later on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 */
1219 else if (cstack->cs_flags[cstack->cs_idx] & CSF_TRUE
1220 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)
1221 && dbg_check_skipped(eap))
1222 (void)do_intthrow(cstack);
1223
1224 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001225 * Set loop flag, so do_cmdline() will jump back to the matching
1226 * ":while" or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001228 cstack->cs_lflags |= CSL_HAD_ENDLOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230}
1231
1232
1233/*
1234 * ":throw expr"
1235 */
1236 void
1237ex_throw(eap)
1238 exarg_T *eap;
1239{
1240 char_u *arg = eap->arg;
1241 char_u *value;
1242
1243 if (*arg != NUL && *arg != '|' && *arg != '\n')
1244 value = eval_to_string_skip(arg, &eap->nextcmd, eap->skip);
1245 else
1246 {
1247 EMSG(_(e_argreq));
1248 value = NULL;
1249 }
1250
1251 /* On error or when an exception is thrown during argument evaluation, do
1252 * not throw. */
1253 if (!eap->skip && value != NULL)
1254 {
1255 if (throw_exception(value, ET_USER, NULL) == FAIL)
1256 vim_free(value);
1257 else
1258 do_throw(eap->cstack);
1259 }
1260}
1261
1262/*
Bram Moolenaar269ec652004-07-29 08:43:53 +00001263 * Throw the current exception through the specified cstack. Common routine
1264 * for ":throw" (user exception) and error and interrupt exceptions. Also
1265 * used for rethrowing an uncaught exception.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
1267 void
1268do_throw(cstack)
1269 struct condstack *cstack;
1270{
1271 int idx;
1272 int inactivate_try = FALSE;
1273
1274 /*
1275 * Cleanup and inactivate up to the next surrounding try conditional that
1276 * is not in its finally clause. Normally, do not inactivate the try
1277 * conditional itself, so that its ACTIVE flag can be tested below. But
1278 * if a previous error or interrupt has not been converted to an exception,
1279 * inactivate the try conditional, too, as if the conversion had been done,
Bram Moolenaar269ec652004-07-29 08:43:53 +00001280 * and reset the did_emsg or got_int flag, so this won't happen again at
1281 * the next surrounding try conditional.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 */
1283 if (did_emsg && !THROW_ON_ERROR)
1284 {
1285 inactivate_try = TRUE;
1286 did_emsg = FALSE;
1287 }
1288 if (got_int && !THROW_ON_INTERRUPT)
1289 {
1290 inactivate_try = TRUE;
1291 got_int = FALSE;
1292 }
1293 idx = cleanup_conditionals(cstack, 0, inactivate_try);
1294 if (idx >= 0)
1295 {
1296 /*
1297 * If this try conditional is active and we are before its first
Bram Moolenaar269ec652004-07-29 08:43:53 +00001298 * ":catch", set THROWN so that the ":catch" commands will check
1299 * whether the exception matches. When the exception came from any of
1300 * the catch clauses, it will be made pending at the ":finally" (if
1301 * present) and rethrown at the ":endtry". This will also happen if
1302 * the try conditional is inactive. This is the case when we are
1303 * throwing an exception due to an error or interrupt on the way from
1304 * a preceding ":continue", ":break", ":return", ":finish", error or
1305 * interrupt (not converted to an exception) to the finally clause or
1306 * from a preceding throw of a user or error or interrupt exception to
1307 * the matching catch clause or the finally clause.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 */
1309 if (!(cstack->cs_flags[idx] & CSF_CAUGHT))
1310 {
1311 if (cstack->cs_flags[idx] & CSF_ACTIVE)
1312 cstack->cs_flags[idx] |= CSF_THROWN;
1313 else
1314 /* THROWN may have already been set for a catchable exception
1315 * that has been discarded. Ensure it is reset for the new
1316 * exception. */
1317 cstack->cs_flags[idx] &= ~CSF_THROWN;
1318 }
1319 cstack->cs_flags[idx] &= ~CSF_ACTIVE;
1320 cstack->cs_exception[idx] = current_exception;
1321 }
1322#if 0
Bram Moolenaar269ec652004-07-29 08:43:53 +00001323 /* TODO: Add optimization below. Not yet done because of interface
1324 * problems to eval.c and ex_cmds2.c. (Servatius) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 else
1326 {
1327 /*
1328 * There are no catch clauses to check or finally clauses to execute.
1329 * End the current script or function. The exception will be rethrown
1330 * in the caller.
1331 */
1332 if (getline_equal(eap->getline, eap->cookie, get_func_line))
1333 current_funccal->returned = TRUE;
1334 elseif (eap->get_func_line == getsourceline)
1335 ((struct source_cookie *)eap->cookie)->finished = TRUE;
1336 }
1337#endif
1338
1339 did_throw = TRUE;
1340}
1341
1342/*
1343 * ":try"
1344 */
1345 void
1346ex_try(eap)
1347 exarg_T *eap;
1348{
1349 int skip;
1350 struct condstack *cstack = eap->cstack;
1351
1352 if (cstack->cs_idx == CSTACK_LEN - 1)
1353 eap->errmsg = (char_u *)N_("E601: :try nesting too deep");
1354 else
1355 {
1356 ++cstack->cs_idx;
1357 ++cstack->cs_trylevel;
1358 cstack->cs_flags[cstack->cs_idx] = CSF_TRY;
1359 cstack->cs_pending[cstack->cs_idx] = CSTP_NONE;
1360
1361 /*
1362 * Don't do something after an error, interrupt, or throw, or when there
1363 * is a surrounding conditional and it was not active.
1364 */
1365 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
1366 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
1367
1368 if (!skip)
1369 {
1370 /* Set ACTIVE and TRUE. TRUE means that the corresponding ":catch"
1371 * commands should check for a match if an exception is thrown and
1372 * that the finally clause needs to be executed. */
1373 cstack->cs_flags[cstack->cs_idx] |= CSF_ACTIVE | CSF_TRUE;
1374
1375 /*
1376 * ":silent!", even when used in a try conditional, disables
1377 * displaying of error messages and conversion of errors to
1378 * exceptions. When the silent commands again open a try
1379 * conditional, save "emsg_silent" and reset it so that errors are
1380 * again converted to exceptions. The value is restored when that
1381 * try conditional is left. If it is left normally, the commands
1382 * following the ":endtry" are again silent. If it is left by
1383 * a ":continue", ":break", ":return", or ":finish", the commands
1384 * executed next are again silent. If it is left due to an
1385 * aborting error, an interrupt, or an exception, restoring
1386 * "emsg_silent" does not matter since we are already in the
1387 * aborting state and/or the exception has already been thrown.
1388 * The effect is then just freeing the memory that was allocated
1389 * to save the value.
1390 */
1391 if (emsg_silent)
1392 {
1393 eslist_T *elem;
1394
1395 elem = (eslist_T *)alloc((unsigned)sizeof(struct eslist_elem));
1396 if (elem == NULL)
1397 EMSG(_(e_outofmem));
1398 else
1399 {
1400 elem->saved_emsg_silent = emsg_silent;
1401 elem->next = cstack->cs_emsg_silent_list;
1402 cstack->cs_emsg_silent_list = elem;
1403 cstack->cs_flags[cstack->cs_idx] |= CSF_SILENT;
1404 emsg_silent = 0;
1405 }
1406 }
1407 }
1408
1409 }
1410}
1411
1412/*
1413 * ":catch /{pattern}/" and ":catch"
1414 */
1415 void
1416ex_catch(eap)
1417 exarg_T *eap;
1418{
1419 int idx = 0;
1420 int give_up = FALSE;
1421 int skip = FALSE;
1422 int caught = FALSE;
1423 char_u *end;
1424 int save_char = 0;
1425 char_u *save_cpo;
1426 regmatch_T regmatch;
1427 int prev_got_int;
1428 struct condstack *cstack = eap->cstack;
1429 char_u *pat;
1430
1431 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1432 {
1433 eap->errmsg = (char_u *)N_("E603: :catch without :try");
1434 give_up = TRUE;
1435 }
1436 else
1437 {
1438 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1439 {
1440 /* Report what's missing if the matching ":try" is not in its
1441 * finally clause. */
Bram Moolenaar12805862005-01-05 22:16:17 +00001442 eap->errmsg = get_end_emsg(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 skip = TRUE;
1444 }
1445 for (idx = cstack->cs_idx; idx > 0; --idx)
1446 if (cstack->cs_flags[idx] & CSF_TRY)
1447 break;
1448 if (cstack->cs_flags[idx] & CSF_FINALLY)
1449 {
1450 /* Give up for a ":catch" after ":finally" and ignore it.
1451 * Just parse. */
1452 eap->errmsg = (char_u *)N_("E604: :catch after :finally");
1453 give_up = TRUE;
1454 }
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001455 else
Bram Moolenaar12805862005-01-05 22:16:17 +00001456 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
1457 &cstack->cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
1459
1460 if (ends_excmd(*eap->arg)) /* no argument, catch all errors */
1461 {
1462 pat = (char_u *)".*";
1463 end = NULL;
1464 eap->nextcmd = find_nextcmd(eap->arg);
1465 }
1466 else
1467 {
1468 pat = eap->arg + 1;
1469 end = skip_regexp(pat, *eap->arg, TRUE, NULL);
1470 }
1471
1472 if (!give_up)
1473 {
1474 /*
1475 * Don't do something when no exception has been thrown or when the
1476 * corresponding try block never got active (because of an inactive
1477 * surrounding conditional or after an error or interrupt or throw).
1478 */
1479 if (!did_throw || !(cstack->cs_flags[idx] & CSF_TRUE))
1480 skip = TRUE;
1481
1482 /*
1483 * Check for a match only if an exception is thrown but not caught by
1484 * a previous ":catch". An exception that has replaced a discarded
1485 * exception is not checked (THROWN is not set then).
1486 */
1487 if (!skip && (cstack->cs_flags[idx] & CSF_THROWN)
1488 && !(cstack->cs_flags[idx] & CSF_CAUGHT))
1489 {
1490 if (end != NULL && *end != NUL && !ends_excmd(*skipwhite(end + 1)))
1491 {
1492 EMSG(_(e_trailing));
1493 return;
1494 }
1495
1496 /* When debugging or a breakpoint was encountered, display the
1497 * debug prompt (if not already done) before checking for a match.
1498 * This is a helpful hint for the user when the regular expression
1499 * matching fails. Handle a ">quit" debug command as if an
1500 * interrupt had occurred before the ":catch". That is, discard
1501 * the original exception, replace it by an interrupt exception,
1502 * and don't catch it in this try block. */
1503 if (!dbg_check_skipped(eap) || !do_intthrow(cstack))
1504 {
1505 /* Terminate the pattern and avoid the 'l' flag in 'cpoptions'
1506 * while compiling it. */
1507 if (end != NULL)
1508 {
1509 save_char = *end;
1510 *end = NUL;
1511 }
1512 save_cpo = p_cpo;
1513 p_cpo = (char_u *)"";
1514 regmatch.regprog = vim_regcomp(pat, TRUE);
1515 regmatch.rm_ic = FALSE;
1516 if (end != NULL)
1517 *end = save_char;
1518 p_cpo = save_cpo;
1519 if (regmatch.regprog == NULL)
1520 EMSG2(_(e_invarg2), pat);
1521 else
1522 {
1523 /*
1524 * Save the value of got_int and reset it. We don't want
1525 * a previous interruption cancel matching, only hitting
1526 * CTRL-C while matching should abort it.
1527 */
1528 prev_got_int = got_int;
1529 got_int = FALSE;
1530 caught = vim_regexec_nl(&regmatch, current_exception->value,
1531 (colnr_T)0);
1532 got_int |= prev_got_int;
1533 vim_free(regmatch.regprog);
1534 }
1535 }
1536 }
1537
1538 if (caught)
1539 {
1540 /* Make this ":catch" clause active and reset did_emsg, got_int,
1541 * and did_throw. Put the exception on the caught stack. */
1542 cstack->cs_flags[idx] |= CSF_ACTIVE | CSF_CAUGHT;
1543 did_emsg = got_int = did_throw = FALSE;
1544 catch_exception((except_T *)cstack->cs_exception[idx]);
1545 /* It's mandatory that the current exception is stored in the cstack
1546 * so that it can be discarded at the next ":catch", ":finally", or
1547 * ":endtry" or when the catch clause is left by a ":continue",
1548 * ":break", ":return", ":finish", error, interrupt, or another
1549 * exception. */
1550 if (cstack->cs_exception[cstack->cs_idx] != current_exception)
1551 EMSG(_(e_internal));
1552 }
1553 else
1554 {
1555 /*
1556 * If there is a preceding catch clause and it caught the exception,
1557 * finish the exception now. This happens also after errors except
1558 * when this ":catch" was after the ":finally" or not within
1559 * a ":try". Make the try conditional inactive so that the
1560 * following catch clauses are skipped. On an error or interrupt
1561 * after the preceding try block or catch clause was left by
1562 * a ":continue", ":break", ":return", or ":finish", discard the
1563 * pending action.
1564 */
1565 cleanup_conditionals(cstack, CSF_TRY, TRUE);
1566 }
1567 }
1568
1569 if (end != NULL)
1570 eap->nextcmd = find_nextcmd(end);
1571}
1572
1573/*
1574 * ":finally"
1575 */
1576 void
1577ex_finally(eap)
1578 exarg_T *eap;
1579{
1580 int idx;
1581 int skip = FALSE;
1582 int pending = CSTP_NONE;
1583 struct condstack *cstack = eap->cstack;
1584
1585 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1586 eap->errmsg = (char_u *)N_("E606: :finally without :try");
1587 else
1588 {
1589 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1590 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001591 eap->errmsg = get_end_emsg(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 for (idx = cstack->cs_idx - 1; idx > 0; --idx)
1593 if (cstack->cs_flags[idx] & CSF_TRY)
1594 break;
1595 /* Make this error pending, so that the commands in the following
1596 * finally clause can be executed. This overrules also a pending
1597 * ":continue", ":break", ":return", or ":finish". */
1598 pending = CSTP_ERROR;
1599 }
1600 else
1601 idx = cstack->cs_idx;
1602
1603 if (cstack->cs_flags[idx] & CSF_FINALLY)
1604 {
1605 /* Give up for a multiple ":finally" and ignore it. */
1606 eap->errmsg = (char_u *)N_("E607: multiple :finally");
1607 return;
1608 }
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001609 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
Bram Moolenaar12805862005-01-05 22:16:17 +00001610 &cstack->cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612 /*
1613 * Don't do something when the corresponding try block never got active
1614 * (because of an inactive surrounding conditional or after an error or
1615 * interrupt or throw) or for a ":finally" without ":try" or a multiple
1616 * ":finally". After every other error (did_emsg or the conditional
1617 * errors detected above) or after an interrupt (got_int) or an
1618 * exception (did_throw), the finally clause must be executed.
1619 */
1620 skip = !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE);
1621
1622 if (!skip)
1623 {
1624 /* When debugging or a breakpoint was encountered, display the
1625 * debug prompt (if not already done). The user then knows that the
1626 * finally clause is executed. */
1627 if (dbg_check_skipped(eap))
1628 {
1629 /* Handle a ">quit" debug command as if an interrupt had
1630 * occurred before the ":finally". That is, discard the
1631 * original exception and replace it by an interrupt
1632 * exception. */
1633 (void)do_intthrow(cstack);
1634 }
1635
1636 /*
1637 * If there is a preceding catch clause and it caught the exception,
1638 * finish the exception now. This happens also after errors except
1639 * when this is a multiple ":finally" or one not within a ":try".
1640 * After an error or interrupt, this also discards a pending
1641 * ":continue", ":break", ":finish", or ":return" from the preceding
1642 * try block or catch clause.
1643 */
1644 cleanup_conditionals(cstack, CSF_TRY, FALSE);
1645
1646 /*
1647 * Make did_emsg, got_int, did_throw pending. If set, they overrule
1648 * a pending ":continue", ":break", ":return", or ":finish". Then
1649 * we have particularly to discard a pending return value (as done
1650 * by the call to cleanup_conditionals() above when did_emsg or
1651 * got_int is set). The pending values are restored by the
1652 * ":endtry", except if there is a new error, interrupt, exception,
1653 * ":continue", ":break", ":return", or ":finish" in the following
Bram Moolenaar12805862005-01-05 22:16:17 +00001654 * finally clause. A missing ":endwhile", ":endfor" or ":endif"
1655 * detected here is treated as if did_emsg and did_throw had
1656 * already been set, respectively in case that the error is not
1657 * converted to an exception, did_throw had already been unset.
1658 * We must not set did_emsg here since that would suppress the
1659 * error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 */
1661 if (pending == CSTP_ERROR || did_emsg || got_int || did_throw)
1662 {
1663 if (cstack->cs_pending[cstack->cs_idx] == CSTP_RETURN)
1664 {
1665 report_discard_pending(CSTP_RETURN,
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001666 cstack->cs_rettv[cstack->cs_idx]);
1667 discard_pending_return(cstack->cs_rettv[cstack->cs_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 if (pending == CSTP_ERROR && !did_emsg)
1670 pending |= (THROW_ON_ERROR) ? CSTP_THROW : 0;
1671 else
1672 pending |= did_throw ? CSTP_THROW : 0;
1673 pending |= did_emsg ? CSTP_ERROR : 0;
1674 pending |= got_int ? CSTP_INTERRUPT : 0;
1675 cstack->cs_pending[cstack->cs_idx] = pending;
1676
1677 /* It's mandatory that the current exception is stored in the
1678 * cstack so that it can be rethrown at the ":endtry" or be
1679 * discarded if the finally clause is left by a ":continue",
1680 * ":break", ":return", ":finish", error, interrupt, or another
1681 * exception. When emsg() is called for a missing ":endif" or
Bram Moolenaar12805862005-01-05 22:16:17 +00001682 * a missing ":endwhile"/":endfor" detected here, the
1683 * exception will be discarded. */
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001684 if (did_throw && cstack->cs_exception[cstack->cs_idx]
1685 != current_exception)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 EMSG(_(e_internal));
1687 }
1688
1689 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001690 * Set CSL_HAD_FINA, so do_cmdline() will reset did_emsg,
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001691 * got_int, and did_throw and make the finally clause active.
1692 * This will happen after emsg() has been called for a missing
Bram Moolenaar12805862005-01-05 22:16:17 +00001693 * ":endif" or a missing ":endwhile"/":endfor" detected here, so
1694 * that the following finally clause will be executed even then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001696 cstack->cs_lflags |= CSL_HAD_FINA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
1698 }
1699}
1700
1701/*
1702 * ":endtry"
1703 */
1704 void
1705ex_endtry(eap)
1706 exarg_T *eap;
1707{
1708 int idx;
1709 int skip;
1710 int rethrow = FALSE;
1711 int pending = CSTP_NONE;
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001712 void *rettv = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 struct condstack *cstack = eap->cstack;
1714
1715 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1716 eap->errmsg = (char_u *)N_("E602: :endtry without :try");
1717 else
1718 {
1719 /*
1720 * Don't do something after an error, interrupt or throw in the try
1721 * block, catch clause, or finally clause preceding this ":endtry" or
1722 * when an error or interrupt occurred after a ":continue", ":break",
1723 * ":return", or ":finish" in a try block or catch clause preceding this
1724 * ":endtry" or when the try block never got active (because of an
1725 * inactive surrounding conditional or after an error or interrupt or
1726 * throw) or when there is a surrounding conditional and it has been
1727 * made inactive by a ":continue", ":break", ":return", or ":finish" in
1728 * the finally clause. The latter case need not be tested since then
1729 * anything pending has already been discarded. */
1730 skip = did_emsg || got_int || did_throw ||
1731 !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE);
1732
1733 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1734 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001735 eap->errmsg = get_end_emsg(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 /* Find the matching ":try" and report what's missing. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 idx = cstack->cs_idx;
1738 do
1739 --idx;
1740 while (idx > 0 && !(cstack->cs_flags[idx] & CSF_TRY));
Bram Moolenaar12805862005-01-05 22:16:17 +00001741 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
1742 &cstack->cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 skip = TRUE;
1744
1745 /*
1746 * If an exception is being thrown, discard it to prevent it from
1747 * being rethrown at the end of this function. It would be
1748 * discarded by the error message, anyway. Resets did_throw.
1749 * This does not affect the script termination due to the error
1750 * since "trylevel" is decremented after emsg() has been called.
1751 */
1752 if (did_throw)
1753 discard_current_exception();
1754 }
1755 else
1756 {
1757 idx = cstack->cs_idx;
1758
1759 /*
1760 * If we stopped with the exception currently being thrown at this
1761 * try conditional since we didn't know that it doesn't have
1762 * a finally clause, we need to rethrow it after closing the try
1763 * conditional.
1764 */
1765 if (did_throw && (cstack->cs_flags[idx] & CSF_TRUE)
1766 && !(cstack->cs_flags[idx] & CSF_FINALLY))
1767 rethrow = TRUE;
1768 }
1769
1770 /* If there was no finally clause, show the user when debugging or
1771 * a breakpoint was encountered that the end of the try conditional has
1772 * been reached: display the debug prompt (if not already done). Do
1773 * this on normal control flow or when an exception was thrown, but not
1774 * on an interrupt or error not converted to an exception or when
1775 * a ":break", ":continue", ":return", or ":finish" is pending. These
1776 * actions are carried out immediately.
1777 */
1778 if ((rethrow || (!skip
1779 && !(cstack->cs_flags[idx] & CSF_FINALLY)
1780 && !cstack->cs_pending[idx]))
1781 && dbg_check_skipped(eap))
1782 {
1783 /* Handle a ">quit" debug command as if an interrupt had occurred
1784 * before the ":endtry". That is, throw an interrupt exception and
1785 * set "skip" and "rethrow". */
1786 if (got_int)
1787 {
1788 skip = TRUE;
1789 (void)do_intthrow(cstack);
1790 /* The do_intthrow() call may have reset did_throw or
1791 * cstack->cs_pending[idx].*/
1792 rethrow = FALSE;
1793 if (did_throw && !(cstack->cs_flags[idx] & CSF_FINALLY))
1794 rethrow = TRUE;
1795 }
1796 }
1797
1798 /*
1799 * If a ":return" is pending, we need to resume it after closing the
1800 * try conditional; remember the return value. If there was a finally
1801 * clause making an exception pending, we need to rethrow it. Make it
1802 * the exception currently being thrown.
1803 */
1804 if (!skip)
1805 {
1806 pending = cstack->cs_pending[idx];
1807 cstack->cs_pending[idx] = CSTP_NONE;
1808 if (pending == CSTP_RETURN)
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001809 rettv = cstack->cs_rettv[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 else if (pending & CSTP_THROW)
1811 current_exception = cstack->cs_exception[idx];
1812 }
1813
1814 /*
1815 * Discard anything pending on an error, interrupt, or throw in the
1816 * finally clause. If there was no ":finally", discard a pending
1817 * ":continue", ":break", ":return", or ":finish" if an error or
1818 * interrupt occurred afterwards, but before the ":endtry" was reached.
1819 * If an exception was caught by the last of the catch clauses and there
1820 * was no finally clause, finish the exception now. This happens also
1821 * after errors except when this ":endtry" is not within a ":try".
1822 * Restore "emsg_silent" if it has been reset by this try conditional.
1823 */
1824 cleanup_conditionals(cstack, CSF_TRY | CSF_SILENT, TRUE);
1825
1826 --cstack->cs_idx;
1827 --cstack->cs_trylevel;
1828
1829 if (!skip)
1830 {
1831 report_resume_pending(pending,
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001832 (pending == CSTP_RETURN) ? rettv :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 (pending & CSTP_THROW) ? (void *)current_exception : NULL);
1834 switch (pending)
1835 {
1836 case CSTP_NONE:
1837 break;
1838
1839 /* Reactivate a pending ":continue", ":break", ":return",
1840 * ":finish" from the try block or a catch clause of this try
1841 * conditional. This is skipped, if there was an error in an
1842 * (unskipped) conditional command or an interrupt afterwards
1843 * or if the finally clause is present and executed a new error,
1844 * interrupt, throw, ":continue", ":break", ":return", or
1845 * ":finish". */
1846 case CSTP_CONTINUE:
1847 ex_continue(eap);
1848 break;
1849 case CSTP_BREAK:
1850 ex_break(eap);
1851 break;
1852 case CSTP_RETURN:
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001853 do_return(eap, FALSE, FALSE, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 break;
1855 case CSTP_FINISH:
1856 do_finish(eap, FALSE);
1857 break;
1858
1859 /* When the finally clause was entered due to an error,
1860 * interrupt or throw (as opposed to a ":continue", ":break",
1861 * ":return", or ":finish"), restore the pending values of
1862 * did_emsg, got_int, and did_throw. This is skipped, if there
1863 * was a new error, interrupt, throw, ":continue", ":break",
1864 * ":return", or ":finish". in the finally clause. */
1865 default:
1866 if (pending & CSTP_ERROR)
1867 did_emsg = TRUE;
1868 if (pending & CSTP_INTERRUPT)
1869 got_int = TRUE;
1870 if (pending & CSTP_THROW)
1871 rethrow = TRUE;
1872 break;
1873 }
1874 }
1875
1876 if (rethrow)
1877 /* Rethrow the current exception (within this cstack). */
1878 do_throw(cstack);
1879 }
1880}
1881
1882/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001883 * enter_cleanup() and leave_cleanup()
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001884 *
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001885 * Functions to be called before/after invoking a sequence of autocommands for
1886 * cleanup for a failed command. (Failure means here that a call to emsg()
1887 * has been made, an interrupt occurred, or there is an uncaught exception
1888 * from a previous autocommand execution of the same command.)
1889 *
1890 * Call enter_cleanup() with a pointer to a cleanup_T and pass the same
1891 * pointer to leave_cleanup(). The cleanup_T structure stores the pending
1892 * error/interrupt/exception state.
1893 */
1894
1895/*
1896 * This function works a bit like ex_finally() except that there was not
1897 * actually an extra try block around the part that failed and an error or
1898 * interrupt has not (yet) been converted to an exception. This function
1899 * saves the error/interrupt/ exception state and prepares for the call to
1900 * do_cmdline() that is going to be made for the cleanup autocommand
1901 * execution.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001902 */
1903 void
1904enter_cleanup(csp)
1905 cleanup_T *csp;
1906{
1907 int pending = CSTP_NONE;
1908
1909 /*
1910 * Postpone did_emsg, got_int, did_throw. The pending values will be
1911 * restored by leave_cleanup() except if there was an aborting error,
1912 * interrupt, or uncaught exception after this function ends.
1913 */
1914 if (did_emsg || got_int || did_throw || need_rethrow)
1915 {
1916 csp->pending = (did_emsg ? CSTP_ERROR : 0)
1917 | (got_int ? CSTP_INTERRUPT : 0)
1918 | (did_throw ? CSTP_THROW : 0)
1919 | (need_rethrow ? CSTP_THROW : 0);
1920
1921 /* If we are currently throwing an exception (did_throw), save it as
1922 * well. On an error not yet converted to an exception, update
1923 * "force_abort" and reset "cause_abort" (as do_errthrow() would do).
1924 * This is needed for the do_cmdline() call that is going to be made
1925 * for autocommand execution. We need not save *msg_list because
1926 * there is an extra instance for every call of do_cmdline(), anyway.
1927 */
1928 if (did_throw || need_rethrow)
1929 csp->exception = current_exception;
1930 else
1931 {
1932 csp->exception = NULL;
1933 if (did_emsg)
1934 {
1935 force_abort |= cause_abort;
1936 cause_abort = FALSE;
1937 }
1938 }
1939 did_emsg = got_int = did_throw = need_rethrow = FALSE;
1940
1941 /* Report if required by the 'verbose' option or when debugging. */
1942 report_make_pending(pending, csp->exception);
1943 }
1944 else
1945 {
1946 csp->pending = CSTP_NONE;
1947 csp->exception = NULL;
1948 }
1949}
1950
1951/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001952 * See comment above enter_cleanup() for how this function is used.
1953 *
1954 * This function is a bit like ex_endtry() except that there was not actually
1955 * an extra try block around the part that failed and an error or interrupt
1956 * had not (yet) been converted to an exception when the cleanup autocommand
1957 * sequence was invoked.
1958 *
1959 * This function has to be called with the address of the cleanup_T structure
1960 * filled by enter_cleanup() as an argument; it restores the error/interrupt/
1961 * exception state saved by that function - except there was an aborting
1962 * error, an interrupt or an uncaught exception during execution of the
1963 * cleanup autocommands. In the latter case, the saved error/interrupt/
1964 * exception state is discarded.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001965 */
1966 void
1967leave_cleanup(csp)
1968 cleanup_T *csp;
1969{
1970 int pending = csp->pending;
1971
1972 if (pending == CSTP_NONE) /* nothing to do */
1973 return;
1974
1975 /* If there was an aborting error, an interrupt, or an uncaught exception
1976 * after the corresponding call to enter_cleanup(), discard what has been
1977 * made pending by it. Report this to the user if required by the
1978 * 'verbose' option or when debugging. */
1979 if (aborting() || need_rethrow)
1980 {
1981 if (pending & CSTP_THROW)
1982 /* Cancel the pending exception (includes report). */
1983 discard_exception((except_T *)csp->exception, FALSE);
1984 else
1985 report_discard_pending(pending, NULL);
1986
1987 /* If an error was about to be converted to an exception when
1988 * enter_cleanup() was called, free the message list. */
1989 free_msglist(*msg_list);
1990 *msg_list = NULL;
1991 }
1992
1993 /*
1994 * If there was no new error, interrupt, or throw between the calls
1995 * to enter_cleanup() and leave_cleanup(), restore the pending
1996 * error/interrupt/exception state.
1997 */
1998 else
1999 {
2000 /*
2001 * If there was an exception being thrown when enter_cleanup() was
2002 * called, we need to rethrow it. Make it the exception currently
2003 * being thrown.
2004 */
2005 if (pending & CSTP_THROW)
2006 current_exception = csp->exception;
2007
2008 /*
2009 * If an error was about to be converted to an exception when
2010 * enter_cleanup() was called, let "cause_abort" take the part of
2011 * "force_abort" (as done by cause_errthrow()).
2012 */
2013 else if (pending & CSTP_ERROR)
2014 {
2015 cause_abort = force_abort;
2016 force_abort = FALSE;
2017 }
2018
2019 /*
2020 * Restore the pending values of did_emsg, got_int, and did_throw.
2021 */
2022 if (pending & CSTP_ERROR)
2023 did_emsg = TRUE;
2024 if (pending & CSTP_INTERRUPT)
2025 got_int = TRUE;
2026 if (pending & CSTP_THROW)
2027 need_rethrow = TRUE; /* did_throw will be set by do_one_cmd() */
2028
2029 /* Report if required by the 'verbose' option or when debugging. */
2030 report_resume_pending(pending,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002031 (pending & CSTP_THROW) ? (void *)current_exception : NULL);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002032 }
2033}
2034
2035
2036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 * Make conditionals inactive and discard what's pending in finally clauses
2038 * until the conditional type searched for or a try conditional not in its
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002039 * finally clause is reached. If this is in an active catch clause, finish
2040 * the caught exception.
2041 * Return the cstack index where the search stopped.
Bram Moolenaar12805862005-01-05 22:16:17 +00002042 * Values used for "searched_cond" are (CSF_WHILE | CSF_FOR) or CSF_TRY or 0,
2043 * the latter meaning the innermost try conditional not in its finally clause.
2044 * "inclusive" tells whether the conditional searched for should be made
2045 * inactive itself (a try conditional not in its finally claused possibly find
2046 * before is always made inactive). If "inclusive" is TRUE and
2047 * "searched_cond" is CSF_TRY|CSF_SILENT, the saved former value of
2048 * "emsg_silent", if reset when the try conditional finally reached was
2049 * entered, is restored (unsed by ex_endtry()). This is normally done only
2050 * when such a try conditional is left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 */
2052 int
2053cleanup_conditionals(cstack, searched_cond, inclusive)
2054 struct condstack *cstack;
2055 int searched_cond;
2056 int inclusive;
2057{
2058 int idx;
2059 int stop = FALSE;
2060
2061 for (idx = cstack->cs_idx; idx >= 0; --idx)
2062 {
2063 if (cstack->cs_flags[idx] & CSF_TRY)
2064 {
2065 /*
2066 * Discard anything pending in a finally clause and continue the
2067 * search. There may also be a pending ":continue", ":break",
2068 * ":return", or ":finish" before the finally clause. We must not
2069 * discard it, unless an error or interrupt occurred afterwards.
2070 */
2071 if (did_emsg || got_int ||
2072 (cstack->cs_flags[idx] & CSF_FINALLY))
2073 {
2074 switch (cstack->cs_pending[idx])
2075 {
2076 case CSTP_NONE:
2077 break;
2078
2079 case CSTP_CONTINUE:
2080 case CSTP_BREAK:
2081 case CSTP_FINISH:
2082 report_discard_pending(cstack->cs_pending[idx], NULL);
2083 cstack->cs_pending[idx] = CSTP_NONE;
2084 break;
2085
2086 case CSTP_RETURN:
2087 report_discard_pending(CSTP_RETURN,
Bram Moolenaarfca34d62005-01-04 21:38:36 +00002088 cstack->cs_rettv[idx]);
2089 discard_pending_return(cstack->cs_rettv[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 cstack->cs_pending[idx] = CSTP_NONE;
2091 break;
2092
2093 default:
2094 if (cstack->cs_flags[idx] & CSF_FINALLY)
2095 {
2096 if (cstack->cs_pending[idx] & CSTP_THROW)
2097 {
2098 /* Cancel the pending exception. This is in the
2099 * finally clause, so that the stack of the
2100 * caught exceptions is not involved. */
2101 discard_exception((except_T *)
2102 cstack->cs_exception[idx],
2103 FALSE);
2104 }
2105 else
2106 report_discard_pending(cstack->cs_pending[idx],
2107 NULL);
2108 cstack->cs_pending[idx] = CSTP_NONE;
2109 }
2110 break;
2111 }
2112 }
2113
2114 /*
2115 * Stop at a try conditional not in its finally clause. If this try
2116 * conditional is in an active catch clause, finish the caught
2117 * exception.
2118 */
2119 if (!(cstack->cs_flags[idx] & CSF_FINALLY))
2120 {
2121 if ((cstack->cs_flags[idx] & CSF_ACTIVE)
2122 && (cstack->cs_flags[idx] & CSF_CAUGHT))
2123 finish_exception((except_T *)cstack->cs_exception[idx]);
2124 /* Stop at this try conditional - except the try block never
2125 * got active (because of an inactive surrounding conditional
2126 * or when the ":try" appeared after an error or interrupt or
2127 * throw). */
2128 if (cstack->cs_flags[idx] & CSF_TRUE)
2129 {
2130 if (searched_cond == 0 && !inclusive)
2131 break;
2132 stop = TRUE;
2133 }
2134 }
2135 }
2136
2137 /* Stop on the searched conditional type (even when the surrounding
2138 * conditional is not active or something has been made pending).
2139 * If "inclusive" is TRUE and "searched_cond" is CSF_TRY|CSF_SILENT,
2140 * check first whether "emsg_silent" needs to be restored. */
2141 if (cstack->cs_flags[idx] & searched_cond)
2142 {
2143 if (!inclusive)
2144 break;
2145 stop = TRUE;
2146 }
2147 cstack->cs_flags[idx] &= ~CSF_ACTIVE;
2148 if (stop && searched_cond != (CSF_TRY | CSF_SILENT))
2149 break;
2150
2151 /*
2152 * When leaving a try conditinal that reset "emsg_silent" on its entry
2153 * after saving the original value, restore that value here and free the
2154 * memory used to store it.
2155 */
2156 if ((cstack->cs_flags[idx] & CSF_TRY)
2157 && (cstack->cs_flags[idx] & CSF_SILENT))
2158 {
2159 eslist_T *elem;
2160
2161 elem = cstack->cs_emsg_silent_list;
2162 cstack->cs_emsg_silent_list = elem->next;
2163 emsg_silent = elem->saved_emsg_silent;
2164 vim_free(elem);
2165 cstack->cs_flags[idx] &= ~CSF_SILENT;
2166 }
2167 if (stop)
2168 break;
2169 }
2170 return idx;
2171}
2172
2173/*
Bram Moolenaar12805862005-01-05 22:16:17 +00002174 * Return an appropriate error message for a missing endwhile/endfor/endif.
2175 */
2176 static char_u *
2177get_end_emsg(cstack)
2178 struct condstack *cstack;
2179{
2180 if (cstack->cs_flags[cstack->cs_idx] & CSF_WHILE)
2181 return e_endwhile;
2182 if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR)
2183 return e_endfor;
2184 return e_endif;
2185}
2186
2187
2188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 * Rewind conditionals until index "idx" is reached. "cond_type" and
2190 * "cond_level" specify a conditional type and the address of a level variable
2191 * which is to be decremented with each skipped conditional of the specified
2192 * type.
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002193 * Also free "for info" structures where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 */
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002195 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196rewind_conditionals(cstack, idx, cond_type, cond_level)
2197 struct condstack *cstack;
2198 int idx;
2199 int cond_type;
2200 int *cond_level;
2201{
2202 while (cstack->cs_idx > idx)
2203 {
2204 if (cstack->cs_flags[cstack->cs_idx] & cond_type)
2205 --*cond_level;
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002206 if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR)
2207 free_for_info(cstack->cs_forinfo[cstack->cs_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 --cstack->cs_idx;
2209 }
2210}
2211
2212/*
2213 * ":endfunction" when not after a ":function"
2214 */
2215/*ARGSUSED*/
2216 void
2217ex_endfunction(eap)
2218 exarg_T *eap;
2219{
2220 EMSG(_("E193: :endfunction not inside a function"));
2221}
2222
2223/*
Bram Moolenaar12805862005-01-05 22:16:17 +00002224 * Return TRUE if the string "p" looks like a ":while" or ":for" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 */
2226 int
Bram Moolenaar12805862005-01-05 22:16:17 +00002227has_loop_cmd(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 char_u *p;
2229{
2230 p = skipwhite(p);
2231 while (*p == ':')
2232 p = skipwhite(p + 1);
Bram Moolenaar12805862005-01-05 22:16:17 +00002233 if ((p[0] == 'w' && p[1] == 'h')
2234 || (p[0] == 'f' && p[1] == 'o' && p[2] == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return TRUE;
2236 return FALSE;
2237}
2238
2239#endif /* FEAT_EVAL */