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