blob: c8e33c2eeeaff6c344136dbb729b6139229307f8 [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
Bram Moolenaard25c16e2016-01-29 22:13:30 +010018static void free_msglist(struct msglist *l);
19static int throw_exception(void *, int, char_u *);
20static char_u *get_end_emsg(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
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020047 * be set asynchronously afterwards by a SIGINT, so did_throw && got_int is not
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 * 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
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000063# define THROW_ON_ERROR_TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +000064# define THROW_ON_INTERRUPT TRUE
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000065# define THROW_ON_INTERRUPT_TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#endif
67
Bram Moolenaard25c16e2016-01-29 22:13:30 +010068static void catch_exception(except_T *excp);
69static void finish_exception(except_T *excp);
70static void discard_exception(except_T *excp, int was_finished);
71static void report_pending(int action, int pending, void *value);
Bram Moolenaar071d4272004-06-13 20:20:40 +000072
73/*
74 * When several errors appear in a row, setting "force_abort" is delayed until
75 * the failing command returned. "cause_abort" is set to TRUE meanwhile, in
76 * order to indicate that situation. This is useful when "force_abort" was set
77 * during execution of a function call from an expression: the aborting of the
78 * expression evaluation is done without producing any error messages, but all
79 * error messages on parsing errors during the expression evaluation are given
80 * (even if a try conditional is active).
81 */
82static int cause_abort = FALSE;
83
84/*
Bram Moolenaarccc18222007-05-10 18:25:20 +000085 * Return TRUE when immediately aborting on error, or when an interrupt
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 * occurred or an exception was thrown but not caught. Use for ":{range}call"
87 * to check whether an aborted function that does not handle a range itself
88 * should be called again for the next line in the range. Also used for
89 * cancelling expression evaluation after a function call caused an immediate
90 * abort. Note that the first emsg() call temporarily resets "force_abort"
91 * until the throw point for error messages has been reached. That is, during
92 * cancellation of an expression evaluation after an aborting function call or
93 * due to a parsing error, aborting() always returns the same value.
94 */
95 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010096aborting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097{
98 return (did_emsg && force_abort) || got_int || did_throw;
99}
100
101/*
102 * The value of "force_abort" is temporarily reset by the first emsg() call
103 * during an expression evaluation, and "cause_abort" is used instead. It might
104 * be necessary to restore "force_abort" even before the throw point for the
105 * error message has been reached. update_force_abort() should be called then.
106 */
107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100108update_force_abort(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109{
110 if (cause_abort)
111 force_abort = TRUE;
112}
113
114/*
115 * Return TRUE if a command with a subcommand resulting in "retcode" should
116 * abort the script processing. Can be used to suppress an autocommand after
117 * execution of a failing subcommand as long as the error message has not been
118 * displayed and actually caused the abortion.
119 */
120 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100121should_abort(int retcode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return ((retcode == FAIL && trylevel != 0 && !emsg_silent) || aborting());
124}
125
126/*
127 * Return TRUE if a function with the "abort" flag should not be considered
128 * ended on an error. This means that parsing commands is continued in order
129 * to find finally clauses to be executed, and that some errors in skipped
130 * commands are still reported.
131 */
132 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100133aborted_in_try(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134{
135 /* This function is only called after an error. In this case, "force_abort"
136 * determines whether searching for finally clauses is necessary. */
137 return force_abort;
138}
139
140/*
141 * cause_errthrow(): Cause a throw of an error exception if appropriate.
142 * Return TRUE if the error message should not be displayed by emsg().
143 * Sets "ignore", if the emsg() call should be ignored completely.
144 *
145 * When several messages appear in the same command, the first is usually the
146 * most specific one and used as the exception value. The "severe" flag can be
147 * set to TRUE, if a later but severer message should be used instead.
148 */
149 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100150cause_errthrow(
151 char_u *mesg,
152 int severe,
153 int *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
155 struct msglist *elem;
156 struct msglist **plist;
157
158 /*
Bram Moolenaar57657d82006-04-21 22:12:41 +0000159 * Do nothing when displaying the interrupt message or reporting an
160 * uncaught exception (which has already been discarded then) at the top
161 * level. Also when no exception can be thrown. The message will be
162 * displayed by emsg().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 */
164 if (suppress_errthrow)
165 return FALSE;
166
167 /*
Bram Moolenaar57657d82006-04-21 22:12:41 +0000168 * If emsg() has not been called previously, temporarily reset
169 * "force_abort" until the throw point for error messages has been
170 * reached. This ensures that aborting() returns the same value for all
171 * errors that appear in the same command. This means particularly that
172 * for parsing errors during expression evaluation emsg() will be called
173 * multiply, even when the expression is evaluated from a finally clause
174 * that was activated due to an aborting error, interrupt, or exception.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 */
176 if (!did_emsg)
177 {
178 cause_abort = force_abort;
179 force_abort = FALSE;
180 }
181
182 /*
183 * If no try conditional is active and no exception is being thrown and
184 * there has not been an error in a try conditional or a throw so far, do
Bram Moolenaar57657d82006-04-21 22:12:41 +0000185 * nothing (for compatibility of non-EH scripts). The message will then
186 * be displayed by emsg(). When ":silent!" was used and we are not
187 * currently throwing an exception, do nothing. The message text will
188 * then be stored to v:errmsg by emsg() without displaying it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 */
190 if (((trylevel == 0 && !cause_abort) || emsg_silent) && !did_throw)
191 return FALSE;
192
193 /*
194 * Ignore an interrupt message when inside a try conditional or when an
Bram Moolenaar57657d82006-04-21 22:12:41 +0000195 * exception is being thrown or when an error in a try conditional or
196 * throw has been detected previously. This is important in order that an
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 * interrupt exception is catchable by the innermost try conditional and
198 * not replaced by an interrupt message error exception.
199 */
200 if (mesg == (char_u *)_(e_interr))
201 {
202 *ignore = TRUE;
203 return TRUE;
204 }
205
206 /*
207 * Ensure that all commands in nested function calls and sourced files
208 * are aborted immediately.
209 */
210 cause_abort = TRUE;
211
212 /*
213 * When an exception is being thrown, some commands (like conditionals) are
214 * not skipped. Errors in those commands may affect what of the subsequent
215 * commands are regarded part of catch and finally clauses. Catching the
216 * exception would then cause execution of commands not intended by the
217 * user, who wouldn't even get aware of the problem. Therefor, discard the
218 * exception currently being thrown to prevent it from being caught. Just
219 * execute finally clauses and terminate.
220 */
221 if (did_throw)
222 {
223 /* When discarding an interrupt exception, reset got_int to prevent the
224 * same interrupt being converted to an exception again and discarding
225 * the error exception we are about to throw here. */
226 if (current_exception->type == ET_INTERRUPT)
227 got_int = FALSE;
228 discard_current_exception();
229 }
230
231#ifdef THROW_TEST
232 if (!THROW_ON_ERROR)
233 {
234 /*
235 * Print error message immediately without searching for a matching
236 * catch clause; just finally clauses are executed before the script
237 * is terminated.
238 */
239 return FALSE;
240 }
241 else
242#endif
243 {
244 /*
245 * Prepare the throw of an error exception, so that everything will
246 * be aborted (except for executing finally clauses), until the error
247 * exception is caught; if still uncaught at the top level, the error
248 * message will be displayed and the script processing terminated
249 * then. - This function has no access to the conditional stack.
250 * Thus, the actual throw is made after the failing command has
251 * returned. - Throw only the first of several errors in a row, except
252 * a severe error is following.
253 */
254 if (msg_list != NULL)
255 {
256 plist = msg_list;
257 while (*plist != NULL)
258 plist = &(*plist)->next;
259
260 elem = (struct msglist *)alloc((unsigned)sizeof(struct msglist));
261 if (elem == NULL)
262 {
263 suppress_errthrow = TRUE;
264 EMSG(_(e_outofmem));
265 }
266 else
267 {
268 elem->msg = vim_strsave(mesg);
269 if (elem->msg == NULL)
270 {
271 vim_free(elem);
272 suppress_errthrow = TRUE;
273 EMSG(_(e_outofmem));
274 }
275 else
276 {
277 elem->next = NULL;
278 elem->throw_msg = NULL;
279 *plist = elem;
280 if (plist == msg_list || severe)
281 {
282 char_u *tmsg;
283
284 /* Skip the extra "Vim " prefix for message "E458". */
285 tmsg = elem->msg;
286 if (STRNCMP(tmsg, "Vim E", 5) == 0
287 && VIM_ISDIGIT(tmsg[5])
288 && VIM_ISDIGIT(tmsg[6])
289 && VIM_ISDIGIT(tmsg[7])
290 && tmsg[8] == ':'
291 && tmsg[9] == ' ')
292 (*msg_list)->throw_msg = &tmsg[4];
293 else
294 (*msg_list)->throw_msg = tmsg;
295 }
296 }
297 }
298 }
299 return TRUE;
300 }
301}
302
303/*
304 * Free a "msg_list" and the messages it contains.
305 */
306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100307free_msglist(struct msglist *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308{
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/*
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100322 * Free global "*msg_list" and the messages it contains, then set "*msg_list"
323 * to NULL.
324 */
325 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100326free_global_msglist(void)
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100327{
328 free_msglist(*msg_list);
329 *msg_list = NULL;
330}
331
332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 * Throw the message specified in the call to cause_errthrow() above as an
334 * error exception. If cstack is NULL, postpone the throw until do_cmdline()
335 * has returned (see do_one_cmd()).
336 */
337 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100338do_errthrow(struct condstack *cstack, char_u *cmdname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 /*
341 * Ensure that all commands in nested function calls and sourced files
342 * are aborted immediately.
343 */
344 if (cause_abort)
345 {
346 cause_abort = FALSE;
347 force_abort = TRUE;
348 }
349
350 /* If no exception is to be thrown or the conversion should be done after
351 * returning to a previous invocation of do_one_cmd(), do nothing. */
Bram Moolenaar4632d292006-11-28 17:36:37 +0000352 if (msg_list == NULL || *msg_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 return;
354
355 if (throw_exception(*msg_list, ET_ERROR, cmdname) == FAIL)
356 free_msglist(*msg_list);
357 else
358 {
359 if (cstack != NULL)
360 do_throw(cstack);
361 else
362 need_rethrow = TRUE;
363 }
364 *msg_list = NULL;
365}
366
367/*
368 * do_intthrow(): Replace the current exception by an interrupt or interrupt
369 * exception if appropriate. Return TRUE if the current exception is discarded,
370 * FALSE otherwise.
371 */
372 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100373do_intthrow(struct condstack *cstack)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374{
375 /*
376 * If no interrupt occurred or no try conditional is active and no exception
377 * is being thrown, do nothing (for compatibility of non-EH scripts).
378 */
379 if (!got_int || (trylevel == 0 && !did_throw))
380 return FALSE;
381
382#ifdef THROW_TEST /* avoid warning for condition always true */
383 if (!THROW_ON_INTERRUPT)
384 {
385 /*
386 * The interrupt aborts everything except for executing finally clauses.
387 * Discard any user or error or interrupt exception currently being
388 * thrown.
389 */
390 if (did_throw)
391 discard_current_exception();
392 }
393 else
394#endif
395 {
396 /*
397 * Throw an interrupt exception, so that everything will be aborted
398 * (except for executing finally clauses), until the interrupt exception
399 * is caught; if still uncaught at the top level, the script processing
400 * will be terminated then. - If an interrupt exception is already
401 * being thrown, do nothing.
402 *
403 */
404 if (did_throw)
405 {
406 if (current_exception->type == ET_INTERRUPT)
407 return FALSE;
408
409 /* An interrupt exception replaces any user or error exception. */
410 discard_current_exception();
411 }
412 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) != FAIL)
413 do_throw(cstack);
414 }
415
416 return TRUE;
417}
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419/*
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100420 * Get an exception message that is to be stored in current_exception->value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 */
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100422 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100423get_exception_string(
424 void *value,
425 int type,
426 char_u *cmdname,
427 int *should_free)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100429 char_u *ret, *mesg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 int cmdlen;
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100431 char_u *p, *val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
433 if (type == ET_ERROR)
434 {
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100435 *should_free = FALSE;
436 mesg = ((struct msglist *)value)->throw_msg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 if (cmdname != NULL && *cmdname != NUL)
438 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000439 cmdlen = (int)STRLEN(cmdname);
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100440 ret = vim_strnsave((char_u *)"Vim(",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 4 + cmdlen + 2 + (int)STRLEN(mesg));
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100442 if (ret == NULL)
443 return ret;
444 STRCPY(&ret[4], cmdname);
445 STRCPY(&ret[4 + cmdlen], "):");
446 val = ret + 4 + cmdlen + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 }
448 else
449 {
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100450 ret = vim_strnsave((char_u *)"Vim:", 4 + (int)STRLEN(mesg));
451 if (ret == NULL)
452 return ret;
453 val = ret + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 }
455
456 /* msg_add_fname may have been used to prefix the message with a file
457 * name in quotes. In the exception value, put the file name in
458 * parentheses and move it to the end. */
459 for (p = mesg; ; p++)
460 {
461 if (*p == NUL
462 || (*p == 'E'
463 && VIM_ISDIGIT(p[1])
464 && (p[2] == ':'
465 || (VIM_ISDIGIT(p[2])
466 && (p[3] == ':'
467 || (VIM_ISDIGIT(p[3])
468 && p[4] == ':'))))))
469 {
Bram Moolenaar051b7822005-05-19 21:00:46 +0000470 if (*p == NUL || p == mesg)
471 STRCAT(val, mesg); /* 'E123' missing or at beginning */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 else
473 {
474 /* '"filename" E123: message text' */
475 if (mesg[0] != '"' || p-2 < &mesg[1] ||
476 p[-2] != '"' || p[-1] != ' ')
477 /* "E123:" is part of the file name. */
478 continue;
479
480 STRCAT(val, p);
481 p[-2] = NUL;
482 sprintf((char *)(val + STRLEN(p)), " (%s)", &mesg[1]);
483 p[-2] = '"';
484 }
485 break;
486 }
487 }
488 }
489 else
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100490 {
491 *should_free = FALSE;
492 ret = (char_u *) value;
493 }
494
495 return ret;
496}
497
498
499/*
500 * Throw a new exception. Return FAIL when out of memory or it was tried to
501 * throw an illegal user exception. "value" is the exception string for a
502 * user or interrupt exception, or points to a message list in case of an
503 * error exception.
504 */
505 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100506throw_exception(void *value, int type, char_u *cmdname)
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100507{
508 except_T *excp;
509 int should_free;
510
511 /*
512 * Disallow faking Interrupt or error exceptions as user exceptions. They
513 * would be treated differently from real interrupt or error exceptions
514 * when no active try block is found, see do_cmdline().
515 */
516 if (type == ET_USER)
517 {
518 if (STRNCMP((char_u *)value, "Vim", 3) == 0
519 && (((char_u *)value)[3] == NUL || ((char_u *)value)[3] == ':'
520 || ((char_u *)value)[3] == '('))
521 {
522 EMSG(_("E608: Cannot :throw exceptions with 'Vim' prefix"));
523 goto fail;
524 }
525 }
526
527 excp = (except_T *)alloc((unsigned)sizeof(except_T));
528 if (excp == NULL)
529 goto nomem;
530
531 if (type == ET_ERROR)
532 /* Store the original message and prefix the exception value with
533 * "Vim:" or, if a command name is given, "Vim(cmdname):". */
534 excp->messages = (struct msglist *)value;
535
536 excp->value = get_exception_string(value, type, cmdname, &should_free);
537 if (excp->value == NULL && should_free)
538 goto nomem;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540 excp->type = type;
541 excp->throw_name = vim_strsave(sourcing_name == NULL
542 ? (char_u *)"" : sourcing_name);
543 if (excp->throw_name == NULL)
544 {
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100545 if (should_free)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 vim_free(excp->value);
547 goto nomem;
548 }
549 excp->throw_lnum = sourcing_lnum;
550
551 if (p_verbose >= 13 || debug_break_level > 0)
552 {
553 int save_msg_silent = msg_silent;
554
555 if (debug_break_level > 0)
556 msg_silent = FALSE; /* display messages */
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000557 else
558 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 ++no_wait_return;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000560 if (debug_break_level > 0 || *p_vfile == NUL)
561 msg_scroll = TRUE; /* always scroll up, don't overwrite */
562
Bram Moolenaar051b7822005-05-19 21:00:46 +0000563 smsg((char_u *)_("Exception thrown: %s"), excp->value);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000565
566 if (debug_break_level > 0 || *p_vfile == NUL)
567 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 --no_wait_return;
569 if (debug_break_level > 0)
570 msg_silent = save_msg_silent;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000571 else
572 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
574
575 current_exception = excp;
576 return OK;
577
578nomem:
579 vim_free(excp);
580 suppress_errthrow = TRUE;
581 EMSG(_(e_outofmem));
582fail:
583 current_exception = NULL;
584 return FAIL;
585}
586
587/*
588 * Discard an exception. "was_finished" is set when the exception has been
589 * caught and the catch clause has been ended normally.
590 */
591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592discard_exception(except_T *excp, int was_finished)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 char_u *saved_IObuff;
595
596 if (excp == NULL)
597 {
598 EMSG(_(e_internal));
599 return;
600 }
601
602 if (p_verbose >= 13 || debug_break_level > 0)
603 {
604 int save_msg_silent = msg_silent;
605
606 saved_IObuff = vim_strsave(IObuff);
607 if (debug_break_level > 0)
608 msg_silent = FALSE; /* display messages */
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000609 else
610 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 ++no_wait_return;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000612 if (debug_break_level > 0 || *p_vfile == NUL)
613 msg_scroll = TRUE; /* always scroll up, don't overwrite */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000614 smsg(was_finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 ? (char_u *)_("Exception finished: %s")
616 : (char_u *)_("Exception discarded: %s"),
617 excp->value);
618 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000619 if (debug_break_level > 0 || *p_vfile == NUL)
620 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 --no_wait_return;
622 if (debug_break_level > 0)
623 msg_silent = save_msg_silent;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000624 else
625 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 STRCPY(IObuff, saved_IObuff);
627 vim_free(saved_IObuff);
628 }
629 if (excp->type != ET_INTERRUPT)
630 vim_free(excp->value);
631 if (excp->type == ET_ERROR)
632 free_msglist(excp->messages);
633 vim_free(excp->throw_name);
634 vim_free(excp);
635}
636
637/*
638 * Discard the exception currently being thrown.
639 */
640 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100641discard_current_exception(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 discard_exception(current_exception, FALSE);
644 current_exception = NULL;
645 did_throw = FALSE;
646 need_rethrow = FALSE;
647}
648
649/*
650 * Put an exception on the caught stack.
651 */
652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100653catch_exception(except_T *excp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 excp->caught = caught_stack;
656 caught_stack = excp;
657 set_vim_var_string(VV_EXCEPTION, excp->value, -1);
658 if (*excp->throw_name != NUL)
659 {
660 if (excp->throw_lnum != 0)
Bram Moolenaar051b7822005-05-19 21:00:46 +0000661 vim_snprintf((char *)IObuff, IOSIZE, _("%s, line %ld"),
662 excp->throw_name, (long)excp->throw_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 else
Bram Moolenaar051b7822005-05-19 21:00:46 +0000664 vim_snprintf((char *)IObuff, IOSIZE, "%s", excp->throw_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 set_vim_var_string(VV_THROWPOINT, IObuff, -1);
666 }
667 else
668 /* throw_name not set on an exception from a command that was typed. */
669 set_vim_var_string(VV_THROWPOINT, NULL, -1);
670
671 if (p_verbose >= 13 || debug_break_level > 0)
672 {
673 int save_msg_silent = msg_silent;
674
675 if (debug_break_level > 0)
676 msg_silent = FALSE; /* display messages */
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000677 else
678 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 ++no_wait_return;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000680 if (debug_break_level > 0 || *p_vfile == NUL)
681 msg_scroll = TRUE; /* always scroll up, don't overwrite */
682
Bram Moolenaar051b7822005-05-19 21:00:46 +0000683 smsg((char_u *)_("Exception caught: %s"), excp->value);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000685
686 if (debug_break_level > 0 || *p_vfile == NUL)
687 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 --no_wait_return;
689 if (debug_break_level > 0)
690 msg_silent = save_msg_silent;
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000691 else
692 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694}
695
696/*
697 * Remove an exception from the caught stack.
698 */
699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100700finish_exception(except_T *excp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
702 if (excp != caught_stack)
703 EMSG(_(e_internal));
704 caught_stack = caught_stack->caught;
705 if (caught_stack != NULL)
706 {
707 set_vim_var_string(VV_EXCEPTION, caught_stack->value, -1);
708 if (*caught_stack->throw_name != NUL)
709 {
710 if (caught_stack->throw_lnum != 0)
Bram Moolenaar051b7822005-05-19 21:00:46 +0000711 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 _("%s, line %ld"), caught_stack->throw_name,
713 (long)caught_stack->throw_lnum);
714 else
Bram Moolenaar051b7822005-05-19 21:00:46 +0000715 vim_snprintf((char *)IObuff, IOSIZE, "%s",
716 caught_stack->throw_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 set_vim_var_string(VV_THROWPOINT, IObuff, -1);
718 }
719 else
720 /* throw_name not set on an exception from a command that was
721 * typed. */
722 set_vim_var_string(VV_THROWPOINT, NULL, -1);
723 }
724 else
725 {
726 set_vim_var_string(VV_EXCEPTION, NULL, -1);
727 set_vim_var_string(VV_THROWPOINT, NULL, -1);
728 }
729
730 /* Discard the exception, but use the finish message for 'verbose'. */
731 discard_exception(excp, TRUE);
732}
733
734/*
735 * Flags specifying the message displayed by report_pending.
736 */
737#define RP_MAKE 0
738#define RP_RESUME 1
739#define RP_DISCARD 2
740
741/*
742 * Report information about something pending in a finally clause if required by
743 * the 'verbose' option or when debugging. "action" tells whether something is
744 * made pending or something pending is resumed or discarded. "pending" tells
745 * what is pending. "value" specifies the return value for a pending ":return"
746 * or the exception value for a pending exception.
747 */
748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100749report_pending(int action, int pending, void *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751 char_u *mesg;
752 char *s;
753 int save_msg_silent;
754
755
756 switch (action)
757 {
758 case RP_MAKE:
759 mesg = (char_u *)_("%s made pending");
760 break;
761 case RP_RESUME:
762 mesg = (char_u *)_("%s resumed");
763 break;
764 /* case RP_DISCARD: */
765 default:
766 mesg = (char_u *)_("%s discarded");
767 break;
768 }
769
770 switch (pending)
771 {
772 case CSTP_NONE:
773 return;
774
775 case CSTP_CONTINUE:
776 s = ":continue";
777 break;
778 case CSTP_BREAK:
779 s = ":break";
780 break;
781 case CSTP_FINISH:
782 s = ":finish";
783 break;
784 case CSTP_RETURN:
785 /* ":return" command producing value, allocated */
786 s = (char *)get_return_cmd(value);
787 break;
788
789 default:
790 if (pending & CSTP_THROW)
791 {
Bram Moolenaar051b7822005-05-19 21:00:46 +0000792 vim_snprintf((char *)IObuff, IOSIZE,
793 (char *)mesg, _("Exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 mesg = vim_strnsave(IObuff, (int)STRLEN(IObuff) + 4);
795 STRCAT(mesg, ": %s");
796 s = (char *)((except_T *)value)->value;
797 }
798 else if ((pending & CSTP_ERROR) && (pending & CSTP_INTERRUPT))
799 s = _("Error and interrupt");
800 else if (pending & CSTP_ERROR)
801 s = _("Error");
802 else /* if (pending & CSTP_INTERRUPT) */
803 s = _("Interrupt");
804 }
805
806 save_msg_silent = msg_silent;
807 if (debug_break_level > 0)
808 msg_silent = FALSE; /* display messages */
809 ++no_wait_return;
810 msg_scroll = TRUE; /* always scroll up, don't overwrite */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000811 smsg(mesg, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 msg_puts((char_u *)"\n"); /* don't overwrite this either */
813 cmdline_row = msg_row;
814 --no_wait_return;
815 if (debug_break_level > 0)
816 msg_silent = save_msg_silent;
817
818 if (pending == CSTP_RETURN)
819 vim_free(s);
820 else if (pending & CSTP_THROW)
821 vim_free(mesg);
822}
823
824/*
825 * If something is made pending in a finally clause, report it if required by
826 * the 'verbose' option or when debugging.
827 */
828 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100829report_make_pending(int pending, void *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830{
831 if (p_verbose >= 14 || debug_break_level > 0)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000832 {
833 if (debug_break_level <= 0)
834 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 report_pending(RP_MAKE, pending, value);
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000836 if (debug_break_level <= 0)
837 verbose_leave();
838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840
841/*
842 * If something pending in a finally clause is resumed at the ":endtry", report
843 * it if required by the 'verbose' option or when debugging.
844 */
845 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100846report_resume_pending(int pending, void *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
848 if (p_verbose >= 14 || debug_break_level > 0)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000849 {
850 if (debug_break_level <= 0)
851 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 report_pending(RP_RESUME, pending, value);
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000853 if (debug_break_level <= 0)
854 verbose_leave();
855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856}
857
858/*
859 * If something pending in a finally clause is discarded, report it if required
860 * by the 'verbose' option or when debugging.
861 */
862 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100863report_discard_pending(int pending, void *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 if (p_verbose >= 14 || debug_break_level > 0)
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000866 {
867 if (debug_break_level <= 0)
868 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 report_pending(RP_DISCARD, pending, value);
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +0000870 if (debug_break_level <= 0)
871 verbose_leave();
872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873}
874
875
876/*
877 * ":if".
878 */
879 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100880ex_if(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882 int error;
883 int skip;
884 int result;
885 struct condstack *cstack = eap->cstack;
886
887 if (cstack->cs_idx == CSTACK_LEN - 1)
888 eap->errmsg = (char_u *)N_("E579: :if nesting too deep");
889 else
890 {
891 ++cstack->cs_idx;
892 cstack->cs_flags[cstack->cs_idx] = 0;
893
894 /*
895 * Don't do something after an error, interrupt, or throw, or when there
896 * is a surrounding conditional and it was not active.
897 */
898 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
899 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
900
901 result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
902
903 if (!skip && !error)
904 {
905 if (result)
906 cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE | CSF_TRUE;
907 }
908 else
909 /* set TRUE, so this conditional will never get active */
910 cstack->cs_flags[cstack->cs_idx] = CSF_TRUE;
911 }
912}
913
914/*
915 * ":endif".
916 */
917 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100918ex_endif(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919{
920 did_endif = TRUE;
921 if (eap->cstack->cs_idx < 0
Bram Moolenaarde8866b2005-01-06 23:24:37 +0000922 || (eap->cstack->cs_flags[eap->cstack->cs_idx]
923 & (CSF_WHILE | CSF_FOR | CSF_TRY)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 eap->errmsg = (char_u *)N_("E580: :endif without :if");
925 else
926 {
927 /*
928 * When debugging or a breakpoint was encountered, display the debug
929 * prompt (if not already done). This shows the user that an ":endif"
930 * is executed when the ":if" or a previous ":elseif" was not TRUE.
931 * Handle a ">quit" debug command as if an interrupt had occurred before
932 * the ":endif". That is, throw an interrupt exception if appropriate.
933 * Doing this here prevents an exception for a parsing error being
934 * discarded by throwing the interrupt exception later on.
935 */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000936 if (!(eap->cstack->cs_flags[eap->cstack->cs_idx] & CSF_TRUE)
937 && dbg_check_skipped(eap))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 (void)do_intthrow(eap->cstack);
939
940 --eap->cstack->cs_idx;
941 }
942}
943
944/*
945 * ":else" and ":elseif".
946 */
947 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100948ex_else(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 int error;
951 int skip;
952 int result;
953 struct condstack *cstack = eap->cstack;
954
955 /*
956 * Don't do something after an error, interrupt, or throw, or when there is
957 * a surrounding conditional and it was not active.
958 */
959 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
960 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
961
962 if (cstack->cs_idx < 0
Bram Moolenaar12805862005-01-05 22:16:17 +0000963 || (cstack->cs_flags[cstack->cs_idx]
964 & (CSF_WHILE | CSF_FOR | CSF_TRY)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 {
966 if (eap->cmdidx == CMD_else)
967 {
968 eap->errmsg = (char_u *)N_("E581: :else without :if");
969 return;
970 }
971 eap->errmsg = (char_u *)N_("E582: :elseif without :if");
972 skip = TRUE;
973 }
974 else if (cstack->cs_flags[cstack->cs_idx] & CSF_ELSE)
975 {
976 if (eap->cmdidx == CMD_else)
977 {
978 eap->errmsg = (char_u *)N_("E583: multiple :else");
979 return;
980 }
981 eap->errmsg = (char_u *)N_("E584: :elseif after :else");
982 skip = TRUE;
983 }
984
985 /* if skipping or the ":if" was TRUE, reset ACTIVE, otherwise set it */
986 if (skip || cstack->cs_flags[cstack->cs_idx] & CSF_TRUE)
987 {
988 if (eap->errmsg == NULL)
989 cstack->cs_flags[cstack->cs_idx] = CSF_TRUE;
990 skip = TRUE; /* don't evaluate an ":elseif" */
991 }
992 else
993 cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE;
994
995 /*
996 * When debugging or a breakpoint was encountered, display the debug prompt
997 * (if not already done). This shows the user that an ":else" or ":elseif"
998 * is executed when the ":if" or previous ":elseif" was not TRUE. Handle
999 * a ">quit" debug command as if an interrupt had occurred before the
1000 * ":else" or ":elseif". That is, set "skip" and throw an interrupt
1001 * exception if appropriate. Doing this here prevents that an exception
1002 * for a parsing errors is discarded when throwing the interrupt exception
1003 * later on.
1004 */
1005 if (!skip && dbg_check_skipped(eap) && got_int)
1006 {
1007 (void)do_intthrow(cstack);
1008 skip = TRUE;
1009 }
1010
1011 if (eap->cmdidx == CMD_elseif)
1012 {
1013 result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
1014 /* When throwing error exceptions, we want to throw always the first
1015 * of several errors in a row. This is what actually happens when
1016 * a conditional error was detected above and there is another failure
1017 * when parsing the expression. Since the skip flag is set in this
1018 * case, the parsing error will be ignored by emsg(). */
1019
1020 if (!skip && !error)
1021 {
1022 if (result)
1023 cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE | CSF_TRUE;
1024 else
1025 cstack->cs_flags[cstack->cs_idx] = 0;
1026 }
1027 else if (eap->errmsg == NULL)
1028 /* set TRUE, so this conditional will never get active */
1029 cstack->cs_flags[cstack->cs_idx] = CSF_TRUE;
1030 }
1031 else
1032 cstack->cs_flags[cstack->cs_idx] |= CSF_ELSE;
1033}
1034
1035/*
Bram Moolenaar12805862005-01-05 22:16:17 +00001036 * Handle ":while" and ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 */
1038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001039ex_while(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041 int error;
1042 int skip;
1043 int result;
1044 struct condstack *cstack = eap->cstack;
1045
1046 if (cstack->cs_idx == CSTACK_LEN - 1)
Bram Moolenaar12805862005-01-05 22:16:17 +00001047 eap->errmsg = (char_u *)N_("E585: :while/:for nesting too deep");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 else
1049 {
1050 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001051 * The loop flag is set when we have jumped back from the matching
1052 * ":endwhile" or ":endfor". When not set, need to initialise this
1053 * cstack entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001055 if ((cstack->cs_lflags & CSL_HAD_LOOP) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 ++cstack->cs_idx;
Bram Moolenaar12805862005-01-05 22:16:17 +00001058 ++cstack->cs_looplevel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 cstack->cs_line[cstack->cs_idx] = -1;
1060 }
Bram Moolenaar12805862005-01-05 22:16:17 +00001061 cstack->cs_flags[cstack->cs_idx] =
1062 eap->cmdidx == CMD_while ? CSF_WHILE : CSF_FOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
1064 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001065 * Don't do something after an error, interrupt, or throw, or when
1066 * there is a surrounding conditional and it was not active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 */
1068 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
1069 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
Bram Moolenaar12805862005-01-05 22:16:17 +00001070 if (eap->cmdidx == CMD_while)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001072 /*
1073 * ":while bool-expr"
1074 */
1075 result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 }
1077 else
1078 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001079 void *fi;
1080
1081 /*
1082 * ":for var in list-expr"
1083 */
1084 if ((cstack->cs_lflags & CSL_HAD_LOOP) != 0)
1085 {
1086 /* Jumping here from a ":continue" or ":endfor": use the
1087 * previously evaluated list. */
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001088 fi = cstack->cs_forinfo[cstack->cs_idx];
Bram Moolenaar12805862005-01-05 22:16:17 +00001089 error = FALSE;
1090 }
1091 else
1092 {
1093 /* Evaluate the argument and get the info in a structure. */
1094 fi = eval_for_line(eap->arg, &error, &eap->nextcmd, skip);
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001095 cstack->cs_forinfo[cstack->cs_idx] = fi;
Bram Moolenaar12805862005-01-05 22:16:17 +00001096 }
1097
1098 /* use the element at the start of the list and advance */
1099 if (!error && fi != NULL && !skip)
1100 result = next_for_item(fi, eap->arg);
1101 else
1102 result = FALSE;
1103
1104 if (!result)
1105 {
1106 free_for_info(fi);
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001107 cstack->cs_forinfo[cstack->cs_idx] = NULL;
Bram Moolenaar12805862005-01-05 22:16:17 +00001108 }
1109 }
1110
1111 /*
1112 * If this cstack entry was just initialised and is active, set the
1113 * loop flag, so do_cmdline() will set the line number in cs_line[].
1114 * If executing the command a second time, clear the loop flag.
1115 */
1116 if (!skip && !error && result)
1117 {
1118 cstack->cs_flags[cstack->cs_idx] |= (CSF_ACTIVE | CSF_TRUE);
1119 cstack->cs_lflags ^= CSL_HAD_LOOP;
1120 }
1121 else
1122 {
1123 cstack->cs_lflags &= ~CSL_HAD_LOOP;
1124 /* If the ":while" evaluates to FALSE or ":for" is past the end of
1125 * the list, show the debug prompt at the ":endwhile"/":endfor" as
1126 * if there was a ":break" in a ":while"/":for" evaluating to
1127 * TRUE. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (!skip && !error)
1129 cstack->cs_flags[cstack->cs_idx] |= CSF_TRUE;
1130 }
1131 }
1132}
1133
1134/*
1135 * ":continue"
1136 */
1137 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001138ex_continue(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140 int idx;
1141 struct condstack *cstack = eap->cstack;
1142
Bram Moolenaar12805862005-01-05 22:16:17 +00001143 if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0)
1144 eap->errmsg = (char_u *)N_("E586: :continue without :while or :for");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 else
1146 {
1147 /* Try to find the matching ":while". This might stop at a try
1148 * conditional not in its finally clause (which is then to be executed
1149 * next). Therefor, inactivate all conditionals except the ":while"
1150 * itself (if reached). */
Bram Moolenaar12805862005-01-05 22:16:17 +00001151 idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001152 if (idx >= 0 && (cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001154 rewind_conditionals(cstack, idx, CSF_TRY, &cstack->cs_trylevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
1156 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001157 * Set CSL_HAD_CONT, so do_cmdline() will jump back to the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 * matching ":while".
1159 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001160 cstack->cs_lflags |= CSL_HAD_CONT; /* let do_cmdline() handle it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162 else
1163 {
1164 /* If a try conditional not in its finally clause is reached first,
1165 * make the ":continue" pending for execution at the ":endtry". */
1166 cstack->cs_pending[idx] = CSTP_CONTINUE;
1167 report_make_pending(CSTP_CONTINUE, NULL);
1168 }
1169 }
1170}
1171
1172/*
1173 * ":break"
1174 */
1175 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001176ex_break(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177{
1178 int idx;
1179 struct condstack *cstack = eap->cstack;
1180
Bram Moolenaar12805862005-01-05 22:16:17 +00001181 if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0)
1182 eap->errmsg = (char_u *)N_("E587: :break without :while or :for");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 else
1184 {
1185 /* Inactivate conditionals until the matching ":while" or a try
1186 * conditional not in its finally clause (which is then to be
1187 * executed next) is found. In the latter case, make the ":break"
1188 * pending for execution at the ":endtry". */
Bram Moolenaar12805862005-01-05 22:16:17 +00001189 idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001190 if (idx >= 0 && !(cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
1192 cstack->cs_pending[idx] = CSTP_BREAK;
1193 report_make_pending(CSTP_BREAK, NULL);
1194 }
1195 }
1196}
1197
1198/*
Bram Moolenaar12805862005-01-05 22:16:17 +00001199 * ":endwhile" and ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 */
1201 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001202ex_endwhile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203{
1204 struct condstack *cstack = eap->cstack;
Bram Moolenaar12805862005-01-05 22:16:17 +00001205 int idx;
1206 char_u *err;
1207 int csf;
1208 int fl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaar12805862005-01-05 22:16:17 +00001210 if (eap->cmdidx == CMD_endwhile)
1211 {
1212 err = e_while;
1213 csf = CSF_WHILE;
1214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 else
1216 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001217 err = e_for;
1218 csf = CSF_FOR;
1219 }
1220
1221 if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0)
1222 eap->errmsg = err;
1223 else
1224 {
1225 fl = cstack->cs_flags[cstack->cs_idx];
1226 if (!(fl & csf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00001228 /* If we are in a ":while" or ":for" but used the wrong endloop
1229 * command, do not rewind to the next enclosing ":for"/":while". */
Bram Moolenaar12805862005-01-05 22:16:17 +00001230 if (fl & CSF_WHILE)
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00001231 eap->errmsg = (char_u *)_("E732: Using :endfor with :while");
Bram Moolenaar12805862005-01-05 22:16:17 +00001232 else if (fl & CSF_FOR)
Bram Moolenaar3a3a7232005-01-17 22:16:15 +00001233 eap->errmsg = (char_u *)_("E733: Using :endwhile with :for");
1234 }
1235 if (!(fl & (CSF_WHILE | CSF_FOR)))
1236 {
1237 if (!(fl & CSF_TRY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 eap->errmsg = e_endif;
Bram Moolenaar12805862005-01-05 22:16:17 +00001239 else if (fl & CSF_FINALLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 eap->errmsg = e_endtry;
Bram Moolenaar12805862005-01-05 22:16:17 +00001241 /* Try to find the matching ":while" and report what's missing. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 for (idx = cstack->cs_idx; idx > 0; --idx)
1243 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001244 fl = cstack->cs_flags[idx];
1245 if ((fl & CSF_TRY) && !(fl & CSF_FINALLY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 /* Give up at a try conditional not in its finally clause.
Bram Moolenaar12805862005-01-05 22:16:17 +00001248 * Ignore the ":endwhile"/":endfor". */
1249 eap->errmsg = err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 return;
1251 }
Bram Moolenaar12805862005-01-05 22:16:17 +00001252 if (fl & csf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 break;
1254 }
1255 /* Cleanup and rewind all contained (and unclosed) conditionals. */
Bram Moolenaar12805862005-01-05 22:16:17 +00001256 (void)cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 rewind_conditionals(cstack, idx, CSF_TRY, &cstack->cs_trylevel);
1258 }
1259
1260 /*
1261 * When debugging or a breakpoint was encountered, display the debug
1262 * prompt (if not already done). This shows the user that an
Bram Moolenaar12805862005-01-05 22:16:17 +00001263 * ":endwhile"/":endfor" is executed when the ":while" was not TRUE or
1264 * after a ":break". Handle a ">quit" debug command as if an
1265 * interrupt had occurred before the ":endwhile"/":endfor". That is,
1266 * throw an interrupt exception if appropriate. Doing this here
1267 * prevents that an exception for a parsing error is discarded when
1268 * throwing the interrupt exception later on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 */
1270 else if (cstack->cs_flags[cstack->cs_idx] & CSF_TRUE
1271 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)
1272 && dbg_check_skipped(eap))
1273 (void)do_intthrow(cstack);
1274
1275 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001276 * Set loop flag, so do_cmdline() will jump back to the matching
1277 * ":while" or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001279 cstack->cs_lflags |= CSL_HAD_ENDLOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281}
1282
1283
1284/*
1285 * ":throw expr"
1286 */
1287 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001288ex_throw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 char_u *arg = eap->arg;
1291 char_u *value;
1292
1293 if (*arg != NUL && *arg != '|' && *arg != '\n')
1294 value = eval_to_string_skip(arg, &eap->nextcmd, eap->skip);
1295 else
1296 {
1297 EMSG(_(e_argreq));
1298 value = NULL;
1299 }
1300
1301 /* On error or when an exception is thrown during argument evaluation, do
1302 * not throw. */
1303 if (!eap->skip && value != NULL)
1304 {
1305 if (throw_exception(value, ET_USER, NULL) == FAIL)
1306 vim_free(value);
1307 else
1308 do_throw(eap->cstack);
1309 }
1310}
1311
1312/*
Bram Moolenaar269ec652004-07-29 08:43:53 +00001313 * Throw the current exception through the specified cstack. Common routine
1314 * for ":throw" (user exception) and error and interrupt exceptions. Also
1315 * used for rethrowing an uncaught exception.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 */
1317 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001318do_throw(struct condstack *cstack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int idx;
1321 int inactivate_try = FALSE;
1322
1323 /*
1324 * Cleanup and inactivate up to the next surrounding try conditional that
1325 * is not in its finally clause. Normally, do not inactivate the try
1326 * conditional itself, so that its ACTIVE flag can be tested below. But
1327 * if a previous error or interrupt has not been converted to an exception,
1328 * inactivate the try conditional, too, as if the conversion had been done,
Bram Moolenaar269ec652004-07-29 08:43:53 +00001329 * and reset the did_emsg or got_int flag, so this won't happen again at
1330 * the next surrounding try conditional.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001332#ifndef THROW_ON_ERROR_TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (did_emsg && !THROW_ON_ERROR)
1334 {
1335 inactivate_try = TRUE;
1336 did_emsg = FALSE;
1337 }
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001338#endif
1339#ifndef THROW_ON_INTERRUPT_TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (got_int && !THROW_ON_INTERRUPT)
1341 {
1342 inactivate_try = TRUE;
1343 got_int = FALSE;
1344 }
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00001345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 idx = cleanup_conditionals(cstack, 0, inactivate_try);
1347 if (idx >= 0)
1348 {
1349 /*
1350 * If this try conditional is active and we are before its first
Bram Moolenaar269ec652004-07-29 08:43:53 +00001351 * ":catch", set THROWN so that the ":catch" commands will check
1352 * whether the exception matches. When the exception came from any of
1353 * the catch clauses, it will be made pending at the ":finally" (if
1354 * present) and rethrown at the ":endtry". This will also happen if
1355 * the try conditional is inactive. This is the case when we are
1356 * throwing an exception due to an error or interrupt on the way from
1357 * a preceding ":continue", ":break", ":return", ":finish", error or
1358 * interrupt (not converted to an exception) to the finally clause or
1359 * from a preceding throw of a user or error or interrupt exception to
1360 * the matching catch clause or the finally clause.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 */
1362 if (!(cstack->cs_flags[idx] & CSF_CAUGHT))
1363 {
1364 if (cstack->cs_flags[idx] & CSF_ACTIVE)
1365 cstack->cs_flags[idx] |= CSF_THROWN;
1366 else
1367 /* THROWN may have already been set for a catchable exception
1368 * that has been discarded. Ensure it is reset for the new
1369 * exception. */
1370 cstack->cs_flags[idx] &= ~CSF_THROWN;
1371 }
1372 cstack->cs_flags[idx] &= ~CSF_ACTIVE;
1373 cstack->cs_exception[idx] = current_exception;
1374 }
1375#if 0
Bram Moolenaar269ec652004-07-29 08:43:53 +00001376 /* TODO: Add optimization below. Not yet done because of interface
1377 * problems to eval.c and ex_cmds2.c. (Servatius) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 else
1379 {
1380 /*
1381 * There are no catch clauses to check or finally clauses to execute.
1382 * End the current script or function. The exception will be rethrown
1383 * in the caller.
1384 */
1385 if (getline_equal(eap->getline, eap->cookie, get_func_line))
1386 current_funccal->returned = TRUE;
1387 elseif (eap->get_func_line == getsourceline)
1388 ((struct source_cookie *)eap->cookie)->finished = TRUE;
1389 }
1390#endif
1391
1392 did_throw = TRUE;
1393}
1394
1395/*
1396 * ":try"
1397 */
1398 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001399ex_try(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401 int skip;
1402 struct condstack *cstack = eap->cstack;
1403
1404 if (cstack->cs_idx == CSTACK_LEN - 1)
1405 eap->errmsg = (char_u *)N_("E601: :try nesting too deep");
1406 else
1407 {
1408 ++cstack->cs_idx;
1409 ++cstack->cs_trylevel;
1410 cstack->cs_flags[cstack->cs_idx] = CSF_TRY;
1411 cstack->cs_pending[cstack->cs_idx] = CSTP_NONE;
1412
1413 /*
1414 * Don't do something after an error, interrupt, or throw, or when there
1415 * is a surrounding conditional and it was not active.
1416 */
1417 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
1418 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
1419
1420 if (!skip)
1421 {
1422 /* Set ACTIVE and TRUE. TRUE means that the corresponding ":catch"
1423 * commands should check for a match if an exception is thrown and
1424 * that the finally clause needs to be executed. */
1425 cstack->cs_flags[cstack->cs_idx] |= CSF_ACTIVE | CSF_TRUE;
1426
1427 /*
1428 * ":silent!", even when used in a try conditional, disables
1429 * displaying of error messages and conversion of errors to
1430 * exceptions. When the silent commands again open a try
1431 * conditional, save "emsg_silent" and reset it so that errors are
1432 * again converted to exceptions. The value is restored when that
1433 * try conditional is left. If it is left normally, the commands
1434 * following the ":endtry" are again silent. If it is left by
1435 * a ":continue", ":break", ":return", or ":finish", the commands
1436 * executed next are again silent. If it is left due to an
1437 * aborting error, an interrupt, or an exception, restoring
1438 * "emsg_silent" does not matter since we are already in the
1439 * aborting state and/or the exception has already been thrown.
1440 * The effect is then just freeing the memory that was allocated
1441 * to save the value.
1442 */
1443 if (emsg_silent)
1444 {
1445 eslist_T *elem;
1446
1447 elem = (eslist_T *)alloc((unsigned)sizeof(struct eslist_elem));
1448 if (elem == NULL)
1449 EMSG(_(e_outofmem));
1450 else
1451 {
1452 elem->saved_emsg_silent = emsg_silent;
1453 elem->next = cstack->cs_emsg_silent_list;
1454 cstack->cs_emsg_silent_list = elem;
1455 cstack->cs_flags[cstack->cs_idx] |= CSF_SILENT;
1456 emsg_silent = 0;
1457 }
1458 }
1459 }
1460
1461 }
1462}
1463
1464/*
1465 * ":catch /{pattern}/" and ":catch"
1466 */
1467 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001468ex_catch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470 int idx = 0;
1471 int give_up = FALSE;
1472 int skip = FALSE;
1473 int caught = FALSE;
1474 char_u *end;
1475 int save_char = 0;
1476 char_u *save_cpo;
1477 regmatch_T regmatch;
1478 int prev_got_int;
1479 struct condstack *cstack = eap->cstack;
1480 char_u *pat;
1481
1482 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1483 {
1484 eap->errmsg = (char_u *)N_("E603: :catch without :try");
1485 give_up = TRUE;
1486 }
1487 else
1488 {
1489 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1490 {
1491 /* Report what's missing if the matching ":try" is not in its
1492 * finally clause. */
Bram Moolenaar12805862005-01-05 22:16:17 +00001493 eap->errmsg = get_end_emsg(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 skip = TRUE;
1495 }
1496 for (idx = cstack->cs_idx; idx > 0; --idx)
1497 if (cstack->cs_flags[idx] & CSF_TRY)
1498 break;
1499 if (cstack->cs_flags[idx] & CSF_FINALLY)
1500 {
1501 /* Give up for a ":catch" after ":finally" and ignore it.
1502 * Just parse. */
1503 eap->errmsg = (char_u *)N_("E604: :catch after :finally");
1504 give_up = TRUE;
1505 }
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001506 else
Bram Moolenaar12805862005-01-05 22:16:17 +00001507 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
1508 &cstack->cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510
1511 if (ends_excmd(*eap->arg)) /* no argument, catch all errors */
1512 {
1513 pat = (char_u *)".*";
1514 end = NULL;
1515 eap->nextcmd = find_nextcmd(eap->arg);
1516 }
1517 else
1518 {
1519 pat = eap->arg + 1;
1520 end = skip_regexp(pat, *eap->arg, TRUE, NULL);
1521 }
1522
1523 if (!give_up)
1524 {
1525 /*
1526 * Don't do something when no exception has been thrown or when the
1527 * corresponding try block never got active (because of an inactive
1528 * surrounding conditional or after an error or interrupt or throw).
1529 */
1530 if (!did_throw || !(cstack->cs_flags[idx] & CSF_TRUE))
1531 skip = TRUE;
1532
1533 /*
1534 * Check for a match only if an exception is thrown but not caught by
1535 * a previous ":catch". An exception that has replaced a discarded
1536 * exception is not checked (THROWN is not set then).
1537 */
1538 if (!skip && (cstack->cs_flags[idx] & CSF_THROWN)
1539 && !(cstack->cs_flags[idx] & CSF_CAUGHT))
1540 {
1541 if (end != NULL && *end != NUL && !ends_excmd(*skipwhite(end + 1)))
1542 {
1543 EMSG(_(e_trailing));
1544 return;
1545 }
1546
1547 /* When debugging or a breakpoint was encountered, display the
1548 * debug prompt (if not already done) before checking for a match.
1549 * This is a helpful hint for the user when the regular expression
1550 * matching fails. Handle a ">quit" debug command as if an
1551 * interrupt had occurred before the ":catch". That is, discard
1552 * the original exception, replace it by an interrupt exception,
1553 * and don't catch it in this try block. */
1554 if (!dbg_check_skipped(eap) || !do_intthrow(cstack))
1555 {
1556 /* Terminate the pattern and avoid the 'l' flag in 'cpoptions'
1557 * while compiling it. */
1558 if (end != NULL)
1559 {
1560 save_char = *end;
1561 *end = NUL;
1562 }
1563 save_cpo = p_cpo;
1564 p_cpo = (char_u *)"";
Bram Moolenaar150cc272007-08-01 13:47:46 +00001565 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 regmatch.rm_ic = FALSE;
1567 if (end != NULL)
1568 *end = save_char;
1569 p_cpo = save_cpo;
1570 if (regmatch.regprog == NULL)
1571 EMSG2(_(e_invarg2), pat);
1572 else
1573 {
1574 /*
1575 * Save the value of got_int and reset it. We don't want
1576 * a previous interruption cancel matching, only hitting
1577 * CTRL-C while matching should abort it.
1578 */
1579 prev_got_int = got_int;
1580 got_int = FALSE;
1581 caught = vim_regexec_nl(&regmatch, current_exception->value,
1582 (colnr_T)0);
1583 got_int |= prev_got_int;
Bram Moolenaar473de612013-06-08 18:19:48 +02001584 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 }
1587 }
1588
1589 if (caught)
1590 {
1591 /* Make this ":catch" clause active and reset did_emsg, got_int,
1592 * and did_throw. Put the exception on the caught stack. */
1593 cstack->cs_flags[idx] |= CSF_ACTIVE | CSF_CAUGHT;
1594 did_emsg = got_int = did_throw = FALSE;
1595 catch_exception((except_T *)cstack->cs_exception[idx]);
1596 /* It's mandatory that the current exception is stored in the cstack
1597 * so that it can be discarded at the next ":catch", ":finally", or
1598 * ":endtry" or when the catch clause is left by a ":continue",
1599 * ":break", ":return", ":finish", error, interrupt, or another
1600 * exception. */
1601 if (cstack->cs_exception[cstack->cs_idx] != current_exception)
1602 EMSG(_(e_internal));
1603 }
1604 else
1605 {
1606 /*
1607 * If there is a preceding catch clause and it caught the exception,
1608 * finish the exception now. This happens also after errors except
1609 * when this ":catch" was after the ":finally" or not within
1610 * a ":try". Make the try conditional inactive so that the
1611 * following catch clauses are skipped. On an error or interrupt
1612 * after the preceding try block or catch clause was left by
1613 * a ":continue", ":break", ":return", or ":finish", discard the
1614 * pending action.
1615 */
1616 cleanup_conditionals(cstack, CSF_TRY, TRUE);
1617 }
1618 }
1619
1620 if (end != NULL)
1621 eap->nextcmd = find_nextcmd(end);
1622}
1623
1624/*
1625 * ":finally"
1626 */
1627 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001628ex_finally(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630 int idx;
1631 int skip = FALSE;
1632 int pending = CSTP_NONE;
1633 struct condstack *cstack = eap->cstack;
1634
1635 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1636 eap->errmsg = (char_u *)N_("E606: :finally without :try");
1637 else
1638 {
1639 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1640 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001641 eap->errmsg = get_end_emsg(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 for (idx = cstack->cs_idx - 1; idx > 0; --idx)
1643 if (cstack->cs_flags[idx] & CSF_TRY)
1644 break;
1645 /* Make this error pending, so that the commands in the following
1646 * finally clause can be executed. This overrules also a pending
1647 * ":continue", ":break", ":return", or ":finish". */
1648 pending = CSTP_ERROR;
1649 }
1650 else
1651 idx = cstack->cs_idx;
1652
1653 if (cstack->cs_flags[idx] & CSF_FINALLY)
1654 {
1655 /* Give up for a multiple ":finally" and ignore it. */
1656 eap->errmsg = (char_u *)N_("E607: multiple :finally");
1657 return;
1658 }
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001659 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
Bram Moolenaar12805862005-01-05 22:16:17 +00001660 &cstack->cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
1662 /*
1663 * Don't do something when the corresponding try block never got active
1664 * (because of an inactive surrounding conditional or after an error or
1665 * interrupt or throw) or for a ":finally" without ":try" or a multiple
1666 * ":finally". After every other error (did_emsg or the conditional
1667 * errors detected above) or after an interrupt (got_int) or an
1668 * exception (did_throw), the finally clause must be executed.
1669 */
1670 skip = !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE);
1671
1672 if (!skip)
1673 {
1674 /* When debugging or a breakpoint was encountered, display the
1675 * debug prompt (if not already done). The user then knows that the
1676 * finally clause is executed. */
1677 if (dbg_check_skipped(eap))
1678 {
1679 /* Handle a ">quit" debug command as if an interrupt had
1680 * occurred before the ":finally". That is, discard the
1681 * original exception and replace it by an interrupt
1682 * exception. */
1683 (void)do_intthrow(cstack);
1684 }
1685
1686 /*
1687 * If there is a preceding catch clause and it caught the exception,
1688 * finish the exception now. This happens also after errors except
1689 * when this is a multiple ":finally" or one not within a ":try".
1690 * After an error or interrupt, this also discards a pending
1691 * ":continue", ":break", ":finish", or ":return" from the preceding
1692 * try block or catch clause.
1693 */
1694 cleanup_conditionals(cstack, CSF_TRY, FALSE);
1695
1696 /*
1697 * Make did_emsg, got_int, did_throw pending. If set, they overrule
1698 * a pending ":continue", ":break", ":return", or ":finish". Then
1699 * we have particularly to discard a pending return value (as done
1700 * by the call to cleanup_conditionals() above when did_emsg or
1701 * got_int is set). The pending values are restored by the
1702 * ":endtry", except if there is a new error, interrupt, exception,
1703 * ":continue", ":break", ":return", or ":finish" in the following
Bram Moolenaar12805862005-01-05 22:16:17 +00001704 * finally clause. A missing ":endwhile", ":endfor" or ":endif"
1705 * detected here is treated as if did_emsg and did_throw had
1706 * already been set, respectively in case that the error is not
1707 * converted to an exception, did_throw had already been unset.
1708 * We must not set did_emsg here since that would suppress the
1709 * error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 */
1711 if (pending == CSTP_ERROR || did_emsg || got_int || did_throw)
1712 {
1713 if (cstack->cs_pending[cstack->cs_idx] == CSTP_RETURN)
1714 {
1715 report_discard_pending(CSTP_RETURN,
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001716 cstack->cs_rettv[cstack->cs_idx]);
1717 discard_pending_return(cstack->cs_rettv[cstack->cs_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719 if (pending == CSTP_ERROR && !did_emsg)
1720 pending |= (THROW_ON_ERROR) ? CSTP_THROW : 0;
1721 else
1722 pending |= did_throw ? CSTP_THROW : 0;
1723 pending |= did_emsg ? CSTP_ERROR : 0;
1724 pending |= got_int ? CSTP_INTERRUPT : 0;
1725 cstack->cs_pending[cstack->cs_idx] = pending;
1726
1727 /* It's mandatory that the current exception is stored in the
1728 * cstack so that it can be rethrown at the ":endtry" or be
1729 * discarded if the finally clause is left by a ":continue",
1730 * ":break", ":return", ":finish", error, interrupt, or another
1731 * exception. When emsg() is called for a missing ":endif" or
Bram Moolenaar12805862005-01-05 22:16:17 +00001732 * a missing ":endwhile"/":endfor" detected here, the
1733 * exception will be discarded. */
Bram Moolenaarde8866b2005-01-06 23:24:37 +00001734 if (did_throw && cstack->cs_exception[cstack->cs_idx]
1735 != current_exception)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 EMSG(_(e_internal));
1737 }
1738
1739 /*
Bram Moolenaar12805862005-01-05 22:16:17 +00001740 * Set CSL_HAD_FINA, so do_cmdline() will reset did_emsg,
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001741 * got_int, and did_throw and make the finally clause active.
1742 * This will happen after emsg() has been called for a missing
Bram Moolenaar12805862005-01-05 22:16:17 +00001743 * ":endif" or a missing ":endwhile"/":endfor" detected here, so
1744 * that the following finally clause will be executed even then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 */
Bram Moolenaar12805862005-01-05 22:16:17 +00001746 cstack->cs_lflags |= CSL_HAD_FINA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
1748 }
1749}
1750
1751/*
1752 * ":endtry"
1753 */
1754 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001755ex_endtry(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 int idx;
1758 int skip;
1759 int rethrow = FALSE;
1760 int pending = CSTP_NONE;
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001761 void *rettv = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 struct condstack *cstack = eap->cstack;
1763
1764 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0)
1765 eap->errmsg = (char_u *)N_("E602: :endtry without :try");
1766 else
1767 {
1768 /*
1769 * Don't do something after an error, interrupt or throw in the try
1770 * block, catch clause, or finally clause preceding this ":endtry" or
1771 * when an error or interrupt occurred after a ":continue", ":break",
1772 * ":return", or ":finish" in a try block or catch clause preceding this
1773 * ":endtry" or when the try block never got active (because of an
1774 * inactive surrounding conditional or after an error or interrupt or
1775 * throw) or when there is a surrounding conditional and it has been
1776 * made inactive by a ":continue", ":break", ":return", or ":finish" in
1777 * the finally clause. The latter case need not be tested since then
1778 * anything pending has already been discarded. */
1779 skip = did_emsg || got_int || did_throw ||
1780 !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE);
1781
1782 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY))
1783 {
Bram Moolenaar12805862005-01-05 22:16:17 +00001784 eap->errmsg = get_end_emsg(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 /* Find the matching ":try" and report what's missing. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 idx = cstack->cs_idx;
1787 do
1788 --idx;
1789 while (idx > 0 && !(cstack->cs_flags[idx] & CSF_TRY));
Bram Moolenaar12805862005-01-05 22:16:17 +00001790 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR,
1791 &cstack->cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 skip = TRUE;
1793
1794 /*
1795 * If an exception is being thrown, discard it to prevent it from
1796 * being rethrown at the end of this function. It would be
1797 * discarded by the error message, anyway. Resets did_throw.
1798 * This does not affect the script termination due to the error
1799 * since "trylevel" is decremented after emsg() has been called.
1800 */
1801 if (did_throw)
1802 discard_current_exception();
1803 }
1804 else
1805 {
1806 idx = cstack->cs_idx;
1807
1808 /*
1809 * If we stopped with the exception currently being thrown at this
1810 * try conditional since we didn't know that it doesn't have
1811 * a finally clause, we need to rethrow it after closing the try
1812 * conditional.
1813 */
1814 if (did_throw && (cstack->cs_flags[idx] & CSF_TRUE)
1815 && !(cstack->cs_flags[idx] & CSF_FINALLY))
1816 rethrow = TRUE;
1817 }
1818
1819 /* If there was no finally clause, show the user when debugging or
1820 * a breakpoint was encountered that the end of the try conditional has
1821 * been reached: display the debug prompt (if not already done). Do
1822 * this on normal control flow or when an exception was thrown, but not
1823 * on an interrupt or error not converted to an exception or when
1824 * a ":break", ":continue", ":return", or ":finish" is pending. These
1825 * actions are carried out immediately.
1826 */
1827 if ((rethrow || (!skip
1828 && !(cstack->cs_flags[idx] & CSF_FINALLY)
1829 && !cstack->cs_pending[idx]))
1830 && dbg_check_skipped(eap))
1831 {
1832 /* Handle a ">quit" debug command as if an interrupt had occurred
1833 * before the ":endtry". That is, throw an interrupt exception and
1834 * set "skip" and "rethrow". */
1835 if (got_int)
1836 {
1837 skip = TRUE;
1838 (void)do_intthrow(cstack);
1839 /* The do_intthrow() call may have reset did_throw or
1840 * cstack->cs_pending[idx].*/
1841 rethrow = FALSE;
1842 if (did_throw && !(cstack->cs_flags[idx] & CSF_FINALLY))
1843 rethrow = TRUE;
1844 }
1845 }
1846
1847 /*
1848 * If a ":return" is pending, we need to resume it after closing the
1849 * try conditional; remember the return value. If there was a finally
1850 * clause making an exception pending, we need to rethrow it. Make it
1851 * the exception currently being thrown.
1852 */
1853 if (!skip)
1854 {
1855 pending = cstack->cs_pending[idx];
1856 cstack->cs_pending[idx] = CSTP_NONE;
1857 if (pending == CSTP_RETURN)
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001858 rettv = cstack->cs_rettv[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 else if (pending & CSTP_THROW)
1860 current_exception = cstack->cs_exception[idx];
1861 }
1862
1863 /*
1864 * Discard anything pending on an error, interrupt, or throw in the
1865 * finally clause. If there was no ":finally", discard a pending
1866 * ":continue", ":break", ":return", or ":finish" if an error or
1867 * interrupt occurred afterwards, but before the ":endtry" was reached.
1868 * If an exception was caught by the last of the catch clauses and there
1869 * was no finally clause, finish the exception now. This happens also
1870 * after errors except when this ":endtry" is not within a ":try".
1871 * Restore "emsg_silent" if it has been reset by this try conditional.
1872 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001873 (void)cleanup_conditionals(cstack, CSF_TRY | CSF_SILENT, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874
1875 --cstack->cs_idx;
1876 --cstack->cs_trylevel;
1877
1878 if (!skip)
1879 {
1880 report_resume_pending(pending,
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001881 (pending == CSTP_RETURN) ? rettv :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 (pending & CSTP_THROW) ? (void *)current_exception : NULL);
1883 switch (pending)
1884 {
1885 case CSTP_NONE:
1886 break;
1887
1888 /* Reactivate a pending ":continue", ":break", ":return",
1889 * ":finish" from the try block or a catch clause of this try
1890 * conditional. This is skipped, if there was an error in an
1891 * (unskipped) conditional command or an interrupt afterwards
1892 * or if the finally clause is present and executed a new error,
1893 * interrupt, throw, ":continue", ":break", ":return", or
1894 * ":finish". */
1895 case CSTP_CONTINUE:
1896 ex_continue(eap);
1897 break;
1898 case CSTP_BREAK:
1899 ex_break(eap);
1900 break;
1901 case CSTP_RETURN:
Bram Moolenaarfca34d62005-01-04 21:38:36 +00001902 do_return(eap, FALSE, FALSE, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 break;
1904 case CSTP_FINISH:
1905 do_finish(eap, FALSE);
1906 break;
1907
1908 /* When the finally clause was entered due to an error,
1909 * interrupt or throw (as opposed to a ":continue", ":break",
1910 * ":return", or ":finish"), restore the pending values of
1911 * did_emsg, got_int, and did_throw. This is skipped, if there
1912 * was a new error, interrupt, throw, ":continue", ":break",
1913 * ":return", or ":finish". in the finally clause. */
1914 default:
1915 if (pending & CSTP_ERROR)
1916 did_emsg = TRUE;
1917 if (pending & CSTP_INTERRUPT)
1918 got_int = TRUE;
1919 if (pending & CSTP_THROW)
1920 rethrow = TRUE;
1921 break;
1922 }
1923 }
1924
1925 if (rethrow)
1926 /* Rethrow the current exception (within this cstack). */
1927 do_throw(cstack);
1928 }
1929}
1930
1931/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001932 * enter_cleanup() and leave_cleanup()
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001933 *
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001934 * Functions to be called before/after invoking a sequence of autocommands for
1935 * cleanup for a failed command. (Failure means here that a call to emsg()
1936 * has been made, an interrupt occurred, or there is an uncaught exception
1937 * from a previous autocommand execution of the same command.)
1938 *
1939 * Call enter_cleanup() with a pointer to a cleanup_T and pass the same
1940 * pointer to leave_cleanup(). The cleanup_T structure stores the pending
1941 * error/interrupt/exception state.
1942 */
1943
1944/*
1945 * This function works a bit like ex_finally() except that there was not
1946 * actually an extra try block around the part that failed and an error or
1947 * interrupt has not (yet) been converted to an exception. This function
1948 * saves the error/interrupt/ exception state and prepares for the call to
1949 * do_cmdline() that is going to be made for the cleanup autocommand
1950 * execution.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001951 */
1952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001953enter_cleanup(cleanup_T *csp)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001954{
1955 int pending = CSTP_NONE;
1956
1957 /*
1958 * Postpone did_emsg, got_int, did_throw. The pending values will be
1959 * restored by leave_cleanup() except if there was an aborting error,
1960 * interrupt, or uncaught exception after this function ends.
1961 */
1962 if (did_emsg || got_int || did_throw || need_rethrow)
1963 {
1964 csp->pending = (did_emsg ? CSTP_ERROR : 0)
1965 | (got_int ? CSTP_INTERRUPT : 0)
1966 | (did_throw ? CSTP_THROW : 0)
1967 | (need_rethrow ? CSTP_THROW : 0);
1968
1969 /* If we are currently throwing an exception (did_throw), save it as
1970 * well. On an error not yet converted to an exception, update
1971 * "force_abort" and reset "cause_abort" (as do_errthrow() would do).
1972 * This is needed for the do_cmdline() call that is going to be made
1973 * for autocommand execution. We need not save *msg_list because
1974 * there is an extra instance for every call of do_cmdline(), anyway.
1975 */
1976 if (did_throw || need_rethrow)
1977 csp->exception = current_exception;
1978 else
1979 {
1980 csp->exception = NULL;
1981 if (did_emsg)
1982 {
1983 force_abort |= cause_abort;
1984 cause_abort = FALSE;
1985 }
1986 }
1987 did_emsg = got_int = did_throw = need_rethrow = FALSE;
1988
1989 /* Report if required by the 'verbose' option or when debugging. */
1990 report_make_pending(pending, csp->exception);
1991 }
1992 else
1993 {
1994 csp->pending = CSTP_NONE;
1995 csp->exception = NULL;
1996 }
1997}
1998
1999/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002000 * See comment above enter_cleanup() for how this function is used.
2001 *
2002 * This function is a bit like ex_endtry() except that there was not actually
2003 * an extra try block around the part that failed and an error or interrupt
2004 * had not (yet) been converted to an exception when the cleanup autocommand
2005 * sequence was invoked.
2006 *
2007 * This function has to be called with the address of the cleanup_T structure
2008 * filled by enter_cleanup() as an argument; it restores the error/interrupt/
2009 * exception state saved by that function - except there was an aborting
2010 * error, an interrupt or an uncaught exception during execution of the
2011 * cleanup autocommands. In the latter case, the saved error/interrupt/
2012 * exception state is discarded.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002013 */
2014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002015leave_cleanup(cleanup_T *csp)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002016{
2017 int pending = csp->pending;
2018
2019 if (pending == CSTP_NONE) /* nothing to do */
2020 return;
2021
2022 /* If there was an aborting error, an interrupt, or an uncaught exception
2023 * after the corresponding call to enter_cleanup(), discard what has been
2024 * made pending by it. Report this to the user if required by the
2025 * 'verbose' option or when debugging. */
2026 if (aborting() || need_rethrow)
2027 {
2028 if (pending & CSTP_THROW)
2029 /* Cancel the pending exception (includes report). */
2030 discard_exception((except_T *)csp->exception, FALSE);
2031 else
2032 report_discard_pending(pending, NULL);
2033
2034 /* If an error was about to be converted to an exception when
2035 * enter_cleanup() was called, free the message list. */
Bram Moolenaar4632d292006-11-28 17:36:37 +00002036 if (msg_list != NULL)
Bram Moolenaar9fee7d42013-11-28 17:04:43 +01002037 free_global_msglist();
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002038 }
2039
2040 /*
2041 * If there was no new error, interrupt, or throw between the calls
2042 * to enter_cleanup() and leave_cleanup(), restore the pending
2043 * error/interrupt/exception state.
2044 */
2045 else
2046 {
2047 /*
2048 * If there was an exception being thrown when enter_cleanup() was
2049 * called, we need to rethrow it. Make it the exception currently
2050 * being thrown.
2051 */
2052 if (pending & CSTP_THROW)
2053 current_exception = csp->exception;
2054
2055 /*
2056 * If an error was about to be converted to an exception when
2057 * enter_cleanup() was called, let "cause_abort" take the part of
2058 * "force_abort" (as done by cause_errthrow()).
2059 */
2060 else if (pending & CSTP_ERROR)
2061 {
2062 cause_abort = force_abort;
2063 force_abort = FALSE;
2064 }
2065
2066 /*
2067 * Restore the pending values of did_emsg, got_int, and did_throw.
2068 */
2069 if (pending & CSTP_ERROR)
2070 did_emsg = TRUE;
2071 if (pending & CSTP_INTERRUPT)
2072 got_int = TRUE;
2073 if (pending & CSTP_THROW)
2074 need_rethrow = TRUE; /* did_throw will be set by do_one_cmd() */
2075
2076 /* Report if required by the 'verbose' option or when debugging. */
2077 report_resume_pending(pending,
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002078 (pending & CSTP_THROW) ? (void *)current_exception : NULL);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002079 }
2080}
2081
2082
2083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 * Make conditionals inactive and discard what's pending in finally clauses
2085 * until the conditional type searched for or a try conditional not in its
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002086 * finally clause is reached. If this is in an active catch clause, finish
2087 * the caught exception.
2088 * Return the cstack index where the search stopped.
Bram Moolenaar12805862005-01-05 22:16:17 +00002089 * Values used for "searched_cond" are (CSF_WHILE | CSF_FOR) or CSF_TRY or 0,
2090 * the latter meaning the innermost try conditional not in its finally clause.
2091 * "inclusive" tells whether the conditional searched for should be made
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002092 * inactive itself (a try conditional not in its finally clause possibly find
Bram Moolenaar12805862005-01-05 22:16:17 +00002093 * before is always made inactive). If "inclusive" is TRUE and
2094 * "searched_cond" is CSF_TRY|CSF_SILENT, the saved former value of
2095 * "emsg_silent", if reset when the try conditional finally reached was
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002096 * entered, is restored (used by ex_endtry()). This is normally done only
Bram Moolenaar12805862005-01-05 22:16:17 +00002097 * when such a try conditional is left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 */
2099 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002100cleanup_conditionals(
2101 struct condstack *cstack,
2102 int searched_cond,
2103 int inclusive)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
2105 int idx;
2106 int stop = FALSE;
2107
2108 for (idx = cstack->cs_idx; idx >= 0; --idx)
2109 {
2110 if (cstack->cs_flags[idx] & CSF_TRY)
2111 {
2112 /*
2113 * Discard anything pending in a finally clause and continue the
2114 * search. There may also be a pending ":continue", ":break",
2115 * ":return", or ":finish" before the finally clause. We must not
2116 * discard it, unless an error or interrupt occurred afterwards.
2117 */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002118 if (did_emsg || got_int || (cstack->cs_flags[idx] & CSF_FINALLY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
2120 switch (cstack->cs_pending[idx])
2121 {
2122 case CSTP_NONE:
2123 break;
2124
2125 case CSTP_CONTINUE:
2126 case CSTP_BREAK:
2127 case CSTP_FINISH:
2128 report_discard_pending(cstack->cs_pending[idx], NULL);
2129 cstack->cs_pending[idx] = CSTP_NONE;
2130 break;
2131
2132 case CSTP_RETURN:
2133 report_discard_pending(CSTP_RETURN,
Bram Moolenaarfca34d62005-01-04 21:38:36 +00002134 cstack->cs_rettv[idx]);
2135 discard_pending_return(cstack->cs_rettv[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 cstack->cs_pending[idx] = CSTP_NONE;
2137 break;
2138
2139 default:
2140 if (cstack->cs_flags[idx] & CSF_FINALLY)
2141 {
2142 if (cstack->cs_pending[idx] & CSTP_THROW)
2143 {
2144 /* Cancel the pending exception. This is in the
2145 * finally clause, so that the stack of the
2146 * caught exceptions is not involved. */
2147 discard_exception((except_T *)
2148 cstack->cs_exception[idx],
2149 FALSE);
2150 }
2151 else
2152 report_discard_pending(cstack->cs_pending[idx],
2153 NULL);
2154 cstack->cs_pending[idx] = CSTP_NONE;
2155 }
2156 break;
2157 }
2158 }
2159
2160 /*
2161 * Stop at a try conditional not in its finally clause. If this try
2162 * conditional is in an active catch clause, finish the caught
2163 * exception.
2164 */
2165 if (!(cstack->cs_flags[idx] & CSF_FINALLY))
2166 {
2167 if ((cstack->cs_flags[idx] & CSF_ACTIVE)
2168 && (cstack->cs_flags[idx] & CSF_CAUGHT))
2169 finish_exception((except_T *)cstack->cs_exception[idx]);
2170 /* Stop at this try conditional - except the try block never
2171 * got active (because of an inactive surrounding conditional
2172 * or when the ":try" appeared after an error or interrupt or
2173 * throw). */
2174 if (cstack->cs_flags[idx] & CSF_TRUE)
2175 {
2176 if (searched_cond == 0 && !inclusive)
2177 break;
2178 stop = TRUE;
2179 }
2180 }
2181 }
2182
2183 /* Stop on the searched conditional type (even when the surrounding
2184 * conditional is not active or something has been made pending).
2185 * If "inclusive" is TRUE and "searched_cond" is CSF_TRY|CSF_SILENT,
2186 * check first whether "emsg_silent" needs to be restored. */
2187 if (cstack->cs_flags[idx] & searched_cond)
2188 {
2189 if (!inclusive)
2190 break;
2191 stop = TRUE;
2192 }
2193 cstack->cs_flags[idx] &= ~CSF_ACTIVE;
2194 if (stop && searched_cond != (CSF_TRY | CSF_SILENT))
2195 break;
2196
2197 /*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002198 * When leaving a try conditional that reset "emsg_silent" on its
2199 * entry after saving the original value, restore that value here and
2200 * free the memory used to store it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 */
2202 if ((cstack->cs_flags[idx] & CSF_TRY)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002203 && (cstack->cs_flags[idx] & CSF_SILENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
2205 eslist_T *elem;
2206
2207 elem = cstack->cs_emsg_silent_list;
2208 cstack->cs_emsg_silent_list = elem->next;
2209 emsg_silent = elem->saved_emsg_silent;
2210 vim_free(elem);
2211 cstack->cs_flags[idx] &= ~CSF_SILENT;
2212 }
2213 if (stop)
2214 break;
2215 }
2216 return idx;
2217}
2218
2219/*
Bram Moolenaar12805862005-01-05 22:16:17 +00002220 * Return an appropriate error message for a missing endwhile/endfor/endif.
2221 */
2222 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002223get_end_emsg(struct condstack *cstack)
Bram Moolenaar12805862005-01-05 22:16:17 +00002224{
2225 if (cstack->cs_flags[cstack->cs_idx] & CSF_WHILE)
2226 return e_endwhile;
2227 if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR)
2228 return e_endfor;
2229 return e_endif;
2230}
2231
2232
2233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 * Rewind conditionals until index "idx" is reached. "cond_type" and
2235 * "cond_level" specify a conditional type and the address of a level variable
2236 * which is to be decremented with each skipped conditional of the specified
2237 * type.
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002238 * Also free "for info" structures where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 */
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002240 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002241rewind_conditionals(
2242 struct condstack *cstack,
2243 int idx,
2244 int cond_type,
2245 int *cond_level)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246{
2247 while (cstack->cs_idx > idx)
2248 {
2249 if (cstack->cs_flags[cstack->cs_idx] & cond_type)
2250 --*cond_level;
Bram Moolenaarde8866b2005-01-06 23:24:37 +00002251 if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR)
2252 free_for_info(cstack->cs_forinfo[cstack->cs_idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 --cstack->cs_idx;
2254 }
2255}
2256
2257/*
2258 * ":endfunction" when not after a ":function"
2259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002261ex_endfunction(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262{
2263 EMSG(_("E193: :endfunction not inside a function"));
2264}
2265
2266/*
Bram Moolenaar12805862005-01-05 22:16:17 +00002267 * Return TRUE if the string "p" looks like a ":while" or ":for" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 */
2269 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002270has_loop_cmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
Bram Moolenaared53fb92007-11-24 20:50:24 +00002272 int len;
2273
2274 /* skip modifiers, white space and ':' */
2275 for (;;)
2276 {
2277 while (*p == ' ' || *p == '\t' || *p == ':')
2278 ++p;
2279 len = modifier_len(p);
2280 if (len == 0)
2281 break;
2282 p += len;
2283 }
Bram Moolenaar12805862005-01-05 22:16:17 +00002284 if ((p[0] == 'w' && p[1] == 'h')
2285 || (p[0] == 'f' && p[1] == 'o' && p[2] == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 return TRUE;
2287 return FALSE;
2288}
2289
2290#endif /* FEAT_EVAL */