-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_eval.c
2669 lines (2444 loc) · 75.9 KB
/
ex_eval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* ex_eval.c: functions for Ex command line for the +eval feature.
*/
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
static char *get_end_emsg(cstack_T *cstack);
/*
* Exception handling terms:
*
* :try ":try" command \
* ... try block |
* :catch RE ":catch" command |
* ... catch clause |- try conditional
* :finally ":finally" command |
* ... finally clause |
* :endtry ":endtry" command /
*
* The try conditional may have any number of catch clauses and at most one
* finally clause. A ":throw" command can be inside the try block, a catch
* clause, the finally clause, or in a function called or script sourced from
* there or even outside the try conditional. Try conditionals may be nested.
*/
/*
* Configuration whether an exception is thrown on error or interrupt. When
* the preprocessor macros below evaluate to FALSE, an error (did_emsg) or
* interrupt (got_int) under an active try conditional terminates the script
* after the non-active finally clauses of all active try conditionals have been
* executed. Otherwise, errors and/or interrupts are converted into catchable
* exceptions (did_throw additionally set), which terminate the script only if
* not caught. For user exceptions, only did_throw is set. (Note: got_int can
* be set asynchronously afterwards by a SIGINT, so did_throw && got_int is not
* a reliant test that the exception currently being thrown is an interrupt
* exception. Similarly, did_emsg can be set afterwards on an error in an
* (unskipped) conditional command inside an inactive conditional, so did_throw
* && did_emsg is not a reliant test that the exception currently being thrown
* is an error exception.) - The macros can be defined as expressions checking
* for a variable that is allowed to be changed during execution of a script.
*/
#if 0
// Expressions used for testing during the development phase.
# define THROW_ON_ERROR (!eval_to_number("$VIMNOERRTHROW"))
# define THROW_ON_INTERRUPT (!eval_to_number("$VIMNOINTTHROW"))
# define THROW_TEST
#else
// Values used for the Vim release.
# define THROW_ON_ERROR TRUE
# define THROW_ON_ERROR_TRUE
# define THROW_ON_INTERRUPT TRUE
# define THROW_ON_INTERRUPT_TRUE
#endif
/*
* When several errors appear in a row, setting "force_abort" is delayed until
* the failing command returned. "cause_abort" is set to TRUE meanwhile, in
* order to indicate that situation. This is useful when "force_abort" was set
* during execution of a function call from an expression: the aborting of the
* expression evaluation is done without producing any error messages, but all
* error messages on parsing errors during the expression evaluation are given
* (even if a try conditional is active).
*/
static int cause_abort = FALSE;
/*
* Return TRUE when immediately aborting on error, or when an interrupt
* occurred or an exception was thrown but not caught. Use for ":{range}call"
* to check whether an aborted function that does not handle a range itself
* should be called again for the next line in the range. Also used for
* cancelling expression evaluation after a function call caused an immediate
* abort. Note that the first emsg() call temporarily resets "force_abort"
* until the throw point for error messages has been reached. That is, during
* cancellation of an expression evaluation after an aborting function call or
* due to a parsing error, aborting() always returns the same value.
* "got_int" is also set by calling interrupt().
*/
int
aborting(void)
{
return (did_emsg && force_abort) || got_int || did_throw;
}
/*
* The value of "force_abort" is temporarily reset by the first emsg() call
* during an expression evaluation, and "cause_abort" is used instead. It might
* be necessary to restore "force_abort" even before the throw point for the
* error message has been reached. update_force_abort() should be called then.
*/
void
update_force_abort(void)
{
if (cause_abort)
force_abort = TRUE;
}
/*
* Return TRUE if a command with a subcommand resulting in "retcode" should
* abort the script processing. Can be used to suppress an autocommand after
* execution of a failing subcommand as long as the error message has not been
* displayed and actually caused the abortion.
*/
int
should_abort(int retcode)
{
return ((retcode == FAIL && trylevel != 0 && !emsg_silent) || aborting());
}
/*
* Return TRUE if a function with the "abort" flag should not be considered
* ended on an error. This means that parsing commands is continued in order
* to find finally clauses to be executed, and that some errors in skipped
* commands are still reported.
*/
int
aborted_in_try(void)
{
// This function is only called after an error. In this case, "force_abort"
// determines whether searching for finally clauses is necessary.
return force_abort;
}
/*
* cause_errthrow(): Cause a throw of an error exception if appropriate.
* Return TRUE if the error message should not be displayed by emsg().
* Sets "ignore", if the emsg() call should be ignored completely.
*
* When several messages appear in the same command, the first is usually the
* most specific one and used as the exception value. The "severe" flag can be
* set to TRUE, if a later but severer message should be used instead.
*/
int
cause_errthrow(
char_u *mesg,
int severe,
int *ignore)
{
msglist_T *elem;
msglist_T **plist;
/*
* Do nothing when displaying the interrupt message or reporting an
* uncaught exception (which has already been discarded then) at the top
* level. Also when no exception can be thrown. The message will be
* displayed by emsg().
*/
if (suppress_errthrow)
return FALSE;
/*
* If emsg() has not been called previously, temporarily reset
* "force_abort" until the throw point for error messages has been
* reached. This ensures that aborting() returns the same value for all
* errors that appear in the same command. This means particularly that
* for parsing errors during expression evaluation emsg() will be called
* multiply, even when the expression is evaluated from a finally clause
* that was activated due to an aborting error, interrupt, or exception.
*/
if (!did_emsg)
{
cause_abort = force_abort;
force_abort = FALSE;
}
/*
* If no try conditional is active and no exception is being thrown and
* there has not been an error in a try conditional or a throw so far, do
* nothing (for compatibility of non-EH scripts). The message will then
* be displayed by emsg(). When ":silent!" was used and we are not
* currently throwing an exception, do nothing. The message text will
* then be stored to v:errmsg by emsg() without displaying it.
*/
if (((trylevel == 0 && !cause_abort) || emsg_silent) && !did_throw)
return FALSE;
/*
* Ignore an interrupt message when inside a try conditional or when an
* exception is being thrown or when an error in a try conditional or
* throw has been detected previously. This is important in order that an
* interrupt exception is catchable by the innermost try conditional and
* not replaced by an interrupt message error exception.
*/
if (mesg == (char_u *)_(e_interrupted))
{
*ignore = TRUE;
return TRUE;
}
/*
* Ensure that all commands in nested function calls and sourced files
* are aborted immediately.
*/
cause_abort = TRUE;
/*
* When an exception is being thrown, some commands (like conditionals) are
* not skipped. Errors in those commands may affect what of the subsequent
* commands are regarded part of catch and finally clauses. Catching the
* exception would then cause execution of commands not intended by the
* user, who wouldn't even get aware of the problem. Therefore, discard the
* exception currently being thrown to prevent it from being caught. Just
* execute finally clauses and terminate.
*/
if (did_throw)
{
// When discarding an interrupt exception, reset got_int to prevent the
// same interrupt being converted to an exception again and discarding
// the error exception we are about to throw here.
if (current_exception->type == ET_INTERRUPT)
got_int = FALSE;
discard_current_exception();
}
#ifdef THROW_TEST
if (!THROW_ON_ERROR)
{
/*
* Print error message immediately without searching for a matching
* catch clause; just finally clauses are executed before the script
* is terminated.
*/
return FALSE;
}
else
#endif
{
/*
* Prepare the throw of an error exception, so that everything will
* be aborted (except for executing finally clauses), until the error
* exception is caught; if still uncaught at the top level, the error
* message will be displayed and the script processing terminated
* then. - This function has no access to the conditional stack.
* Thus, the actual throw is made after the failing command has
* returned. - Throw only the first of several errors in a row, except
* a severe error is following.
*/
if (msg_list != NULL)
{
plist = msg_list;
while (*plist != NULL)
plist = &(*plist)->next;
elem = ALLOC_CLEAR_ONE(msglist_T);
if (elem == NULL)
{
suppress_errthrow = TRUE;
emsg(_(e_out_of_memory));
}
else
{
elem->msg = (char *)vim_strsave(mesg);
if (elem->msg == NULL)
{
vim_free(elem);
suppress_errthrow = TRUE;
emsg(_(e_out_of_memory));
}
else
{
elem->next = NULL;
elem->throw_msg = NULL;
*plist = elem;
if (plist == msg_list || severe)
{
char *tmsg;
// Skip the extra "Vim " prefix for message "E458".
tmsg = elem->msg;
if (STRNCMP(tmsg, "Vim E", 5) == 0
&& VIM_ISDIGIT(tmsg[5])
&& VIM_ISDIGIT(tmsg[6])
&& VIM_ISDIGIT(tmsg[7])
&& tmsg[8] == ':'
&& tmsg[9] == ' ')
(*msg_list)->throw_msg = &tmsg[4];
else
(*msg_list)->throw_msg = tmsg;
}
// Get the source name and lnum now, it may change before
// reaching do_errthrow().
elem->sfile = estack_sfile(ESTACK_NONE);
elem->slnum = SOURCING_LNUM;
elem->msg_compiling = estack_compiling;
}
}
}
return TRUE;
}
}
/*
* Free a "msg_list" and the messages it contains.
*/
static void
free_msglist(msglist_T *l)
{
msglist_T *messages, *next;
messages = l;
while (messages != NULL)
{
next = messages->next;
vim_free(messages->msg);
vim_free(messages->sfile);
vim_free(messages);
messages = next;
}
}
/*
* Free global "*msg_list" and the messages it contains, then set "*msg_list"
* to NULL.
*/
void
free_global_msglist(void)
{
free_msglist(*msg_list);
*msg_list = NULL;
}
/*
* Throw the message specified in the call to cause_errthrow() above as an
* error exception. If cstack is NULL, postpone the throw until do_cmdline()
* has returned (see do_one_cmd()).
*/
void
do_errthrow(cstack_T *cstack, char_u *cmdname)
{
/*
* Ensure that all commands in nested function calls and sourced files
* are aborted immediately.
*/
if (cause_abort)
{
cause_abort = FALSE;
force_abort = TRUE;
}
// If no exception is to be thrown or the conversion should be done after
// returning to a previous invocation of do_one_cmd(), do nothing.
if (msg_list == NULL || *msg_list == NULL)
return;
if (throw_exception(*msg_list, ET_ERROR, cmdname) == FAIL)
free_msglist(*msg_list);
else
{
if (cstack != NULL)
do_throw(cstack);
else
need_rethrow = TRUE;
}
*msg_list = NULL;
}
/*
* do_intthrow(): Replace the current exception by an interrupt or interrupt
* exception if appropriate. Return TRUE if the current exception is discarded,
* FALSE otherwise.
*/
int
do_intthrow(cstack_T *cstack)
{
/*
* If no interrupt occurred or no try conditional is active and no exception
* is being thrown, do nothing (for compatibility of non-EH scripts).
*/
if (!got_int || (trylevel == 0 && !did_throw))
return FALSE;
#ifdef THROW_TEST // avoid warning for condition always true
if (!THROW_ON_INTERRUPT)
{
/*
* The interrupt aborts everything except for executing finally clauses.
* Discard any user or error or interrupt exception currently being
* thrown.
*/
if (did_throw)
discard_current_exception();
}
else
#endif
{
/*
* Throw an interrupt exception, so that everything will be aborted
* (except for executing finally clauses), until the interrupt exception
* is caught; if still uncaught at the top level, the script processing
* will be terminated then. - If an interrupt exception is already
* being thrown, do nothing.
*
*/
if (did_throw)
{
if (current_exception->type == ET_INTERRUPT)
return FALSE;
// An interrupt exception replaces any user or error exception.
discard_current_exception();
}
if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) != FAIL)
do_throw(cstack);
}
return TRUE;
}
/*
* Get an exception message that is to be stored in current_exception->value.
*/
char *
get_exception_string(
void *value,
except_type_T type,
char_u *cmdname,
int *should_free)
{
char *ret;
char *mesg;
int cmdlen;
char *p, *val;
if (type == ET_ERROR)
{
*should_free = TRUE;
mesg = ((msglist_T *)value)->throw_msg;
if (cmdname != NULL && *cmdname != NUL)
{
cmdlen = (int)STRLEN(cmdname);
ret = (char *)vim_strnsave((char_u *)"Vim(",
4 + cmdlen + 2 + STRLEN(mesg));
if (ret == NULL)
return ret;
STRCPY(&ret[4], cmdname);
STRCPY(&ret[4 + cmdlen], "):");
val = ret + 4 + cmdlen + 2;
}
else
{
ret = (char *)vim_strnsave((char_u *)"Vim:", 4 + STRLEN(mesg));
if (ret == NULL)
return ret;
val = ret + 4;
}
// msg_add_fname may have been used to prefix the message with a file
// name in quotes. In the exception value, put the file name in
// parentheses and move it to the end.
for (p = mesg; ; p++)
{
if (*p == NUL
|| (*p == 'E'
&& VIM_ISDIGIT(p[1])
&& (p[2] == ':'
|| (VIM_ISDIGIT(p[2])
&& (p[3] == ':'
|| (VIM_ISDIGIT(p[3])
&& p[4] == ':'))))))
{
if (*p == NUL || p == mesg)
STRCAT(val, mesg); // 'E123' missing or at beginning
else
{
// '"filename" E123: message text'
if (mesg[0] != '"' || p-2 < &mesg[1] ||
p[-2] != '"' || p[-1] != ' ')
// "E123:" is part of the file name.
continue;
STRCAT(val, p);
p[-2] = NUL;
sprintf((char *)(val + STRLEN(p)), " (%s)", &mesg[1]);
p[-2] = '"';
}
break;
}
}
}
else
{
*should_free = FALSE;
ret = value;
}
return ret;
}
/*
* Throw a new exception. Return FAIL when out of memory or it was tried to
* throw an illegal user exception. "value" is the exception string for a
* user or interrupt exception, or points to a message list in case of an
* error exception.
*/
int
throw_exception(void *value, except_type_T type, char_u *cmdname)
{
except_T *excp;
int should_free;
/*
* Disallow faking Interrupt or error exceptions as user exceptions. They
* would be treated differently from real interrupt or error exceptions
* when no active try block is found, see do_cmdline().
*/
if (type == ET_USER)
{
if (STRNCMP((char_u *)value, "Vim", 3) == 0
&& (((char_u *)value)[3] == NUL || ((char_u *)value)[3] == ':'
|| ((char_u *)value)[3] == '('))
{
emsg(_(e_cannot_throw_exceptions_with_vim_prefix));
goto fail;
}
}
excp = ALLOC_ONE(except_T);
if (excp == NULL)
goto nomem;
if (type == ET_ERROR)
// Store the original message and prefix the exception value with
// "Vim:" or, if a command name is given, "Vim(cmdname):".
excp->messages = (msglist_T *)value;
excp->value = get_exception_string(value, type, cmdname, &should_free);
if (excp->value == NULL && should_free)
goto nomem;
excp->type = type;
if (type == ET_ERROR && ((msglist_T *)value)->sfile != NULL)
{
msglist_T *entry = (msglist_T *)value;
excp->throw_name = entry->sfile;
entry->sfile = NULL;
excp->throw_lnum = entry->slnum;
}
else
{
excp->throw_name = estack_sfile(ESTACK_NONE);
if (excp->throw_name == NULL)
excp->throw_name = vim_strsave((char_u *)"");
if (excp->throw_name == NULL)
{
if (should_free)
vim_free(excp->value);
goto nomem;
}
excp->throw_lnum = SOURCING_LNUM;
}
if (p_verbose >= 13 || debug_break_level > 0)
{
int save_msg_silent = msg_silent;
if (debug_break_level > 0)
msg_silent = FALSE; // display messages
else
verbose_enter();
++no_wait_return;
if (debug_break_level > 0 || *p_vfile == NUL)
msg_scroll = TRUE; // always scroll up, don't overwrite
smsg(_("Exception thrown: %s"), excp->value);
msg_puts("\n"); // don't overwrite this either
if (debug_break_level > 0 || *p_vfile == NUL)
cmdline_row = msg_row;
--no_wait_return;
if (debug_break_level > 0)
msg_silent = save_msg_silent;
else
verbose_leave();
}
current_exception = excp;
return OK;
nomem:
vim_free(excp);
suppress_errthrow = TRUE;
emsg(_(e_out_of_memory));
fail:
current_exception = NULL;
return FAIL;
}
/*
* Discard an exception. "was_finished" is set when the exception has been
* caught and the catch clause has been ended normally.
*/
static void
discard_exception(except_T *excp, int was_finished)
{
char_u *saved_IObuff;
if (current_exception == excp)
current_exception = NULL;
if (excp == NULL)
{
internal_error("discard_exception()");
return;
}
if (p_verbose >= 13 || debug_break_level > 0)
{
int save_msg_silent = msg_silent;
saved_IObuff = vim_strsave(IObuff);
if (debug_break_level > 0)
msg_silent = FALSE; // display messages
else
verbose_enter();
++no_wait_return;
if (debug_break_level > 0 || *p_vfile == NUL)
msg_scroll = TRUE; // always scroll up, don't overwrite
smsg(was_finished
? _("Exception finished: %s")
: _("Exception discarded: %s"),
excp->value);
msg_puts("\n"); // don't overwrite this either
if (debug_break_level > 0 || *p_vfile == NUL)
cmdline_row = msg_row;
--no_wait_return;
if (debug_break_level > 0)
msg_silent = save_msg_silent;
else
verbose_leave();
STRCPY(IObuff, saved_IObuff);
vim_free(saved_IObuff);
}
if (excp->type != ET_INTERRUPT)
vim_free(excp->value);
if (excp->type == ET_ERROR)
free_msglist(excp->messages);
vim_free(excp->throw_name);
vim_free(excp);
}
/*
* Discard the exception currently being thrown.
*/
void
discard_current_exception(void)
{
if (current_exception != NULL)
discard_exception(current_exception, FALSE);
did_throw = FALSE;
need_rethrow = FALSE;
}
/*
* Put an exception on the caught stack.
*/
void
catch_exception(except_T *excp)
{
excp->caught = caught_stack;
caught_stack = excp;
set_vim_var_string(VV_EXCEPTION, (char_u *)excp->value, -1);
if (*excp->throw_name != NUL)
{
if (excp->throw_lnum != 0)
vim_snprintf((char *)IObuff, IOSIZE, _("%s, line %ld"),
excp->throw_name, (long)excp->throw_lnum);
else
vim_snprintf((char *)IObuff, IOSIZE, "%s", excp->throw_name);
set_vim_var_string(VV_THROWPOINT, IObuff, -1);
}
else
// throw_name not set on an exception from a command that was typed.
set_vim_var_string(VV_THROWPOINT, NULL, -1);
if (p_verbose >= 13 || debug_break_level > 0)
{
int save_msg_silent = msg_silent;
if (debug_break_level > 0)
msg_silent = FALSE; // display messages
else
verbose_enter();
++no_wait_return;
if (debug_break_level > 0 || *p_vfile == NUL)
msg_scroll = TRUE; // always scroll up, don't overwrite
smsg(_("Exception caught: %s"), excp->value);
msg_puts("\n"); // don't overwrite this either
if (debug_break_level > 0 || *p_vfile == NUL)
cmdline_row = msg_row;
--no_wait_return;
if (debug_break_level > 0)
msg_silent = save_msg_silent;
else
verbose_leave();
}
}
/*
* Remove an exception from the caught stack.
*/
static void
finish_exception(except_T *excp)
{
if (excp != caught_stack)
internal_error("finish_exception()");
caught_stack = caught_stack->caught;
if (caught_stack != NULL)
{
set_vim_var_string(VV_EXCEPTION, (char_u *)caught_stack->value, -1);
if (*caught_stack->throw_name != NUL)
{
if (caught_stack->throw_lnum != 0)
vim_snprintf((char *)IObuff, IOSIZE,
_("%s, line %ld"), caught_stack->throw_name,
(long)caught_stack->throw_lnum);
else
vim_snprintf((char *)IObuff, IOSIZE, "%s",
caught_stack->throw_name);
set_vim_var_string(VV_THROWPOINT, IObuff, -1);
}
else
// throw_name not set on an exception from a command that was
// typed.
set_vim_var_string(VV_THROWPOINT, NULL, -1);
}
else
{
set_vim_var_string(VV_EXCEPTION, NULL, -1);
set_vim_var_string(VV_THROWPOINT, NULL, -1);
}
// Discard the exception, but use the finish message for 'verbose'.
discard_exception(excp, TRUE);
}
/*
* Save the current exception state in "estate"
*/
void
exception_state_save(exception_state_T *estate)
{
estate->estate_current_exception = current_exception;
estate->estate_did_throw = did_throw;
estate->estate_need_rethrow = need_rethrow;
estate->estate_trylevel = trylevel;
estate->estate_did_emsg = did_emsg;
}
/*
* Restore the current exception state from "estate"
*/
void
exception_state_restore(exception_state_T *estate)
{
// Handle any outstanding exceptions before restoring the state
if (did_throw)
handle_did_throw();
current_exception = estate->estate_current_exception;
did_throw = estate->estate_did_throw;
need_rethrow = estate->estate_need_rethrow;
trylevel = estate->estate_trylevel;
did_emsg = estate->estate_did_emsg;
}
/*
* Clear the current exception state
*/
void
exception_state_clear(void)
{
current_exception = NULL;
did_throw = FALSE;
need_rethrow = FALSE;
trylevel = 0;
did_emsg = 0;
}
/*
* Flags specifying the message displayed by report_pending.
*/
#define RP_MAKE 0
#define RP_RESUME 1
#define RP_DISCARD 2
/*
* Report information about something pending in a finally clause if required by
* the 'verbose' option or when debugging. "action" tells whether something is
* made pending or something pending is resumed or discarded. "pending" tells
* what is pending. "value" specifies the return value for a pending ":return"
* or the exception value for a pending exception.
*/
static void
report_pending(int action, int pending, void *value)
{
char *mesg;
char *s;
int save_msg_silent;
switch (action)
{
case RP_MAKE:
mesg = _("%s made pending");
break;
case RP_RESUME:
mesg = _("%s resumed");
break;
// case RP_DISCARD:
default:
mesg = _("%s discarded");
break;
}
switch (pending)
{
case CSTP_NONE:
return;
case CSTP_CONTINUE:
s = ":continue";
break;
case CSTP_BREAK:
s = ":break";
break;
case CSTP_FINISH:
s = ":finish";
break;
case CSTP_RETURN:
// ":return" command producing value, allocated
s = (char *)get_return_cmd(value);
break;
default:
if (pending & CSTP_THROW)
{
vim_snprintf((char *)IObuff, IOSIZE, mesg, _("Exception"));
mesg = (char *)vim_strnsave(IObuff, STRLEN(IObuff) + 4);
STRCAT(mesg, ": %s");
s = (char *)((except_T *)value)->value;
}
else if ((pending & CSTP_ERROR) && (pending & CSTP_INTERRUPT))
s = _("Error and interrupt");
else if (pending & CSTP_ERROR)
s = _("Error");
else // if (pending & CSTP_INTERRUPT)
s = _("Interrupt");
}
save_msg_silent = msg_silent;
if (debug_break_level > 0)
msg_silent = FALSE; // display messages
++no_wait_return;
msg_scroll = TRUE; // always scroll up, don't overwrite
smsg(mesg, s);
msg_puts("\n"); // don't overwrite this either
cmdline_row = msg_row;
--no_wait_return;
if (debug_break_level > 0)
msg_silent = save_msg_silent;
if (pending == CSTP_RETURN)
vim_free(s);
else if (pending & CSTP_THROW)
vim_free(mesg);
}
/*
* If something is made pending in a finally clause, report it if required by
* the 'verbose' option or when debugging.
*/
void
report_make_pending(int pending, void *value)
{
if (p_verbose >= 14 || debug_break_level > 0)
{
if (debug_break_level <= 0)
verbose_enter();
report_pending(RP_MAKE, pending, value);
if (debug_break_level <= 0)
verbose_leave();
}
}
/*
* If something pending in a finally clause is resumed at the ":endtry", report
* it if required by the 'verbose' option or when debugging.
*/
static void
report_resume_pending(int pending, void *value)
{
if (p_verbose >= 14 || debug_break_level > 0)
{
if (debug_break_level <= 0)
verbose_enter();
report_pending(RP_RESUME, pending, value);
if (debug_break_level <= 0)
verbose_leave();
}
}
/*
* If something pending in a finally clause is discarded, report it if required
* by the 'verbose' option or when debugging.
*/
static void
report_discard_pending(int pending, void *value)
{
if (p_verbose >= 14 || debug_break_level > 0)
{
if (debug_break_level <= 0)
verbose_enter();
report_pending(RP_DISCARD, pending, value);
if (debug_break_level <= 0)
verbose_leave();
}
}
/*
* Return TRUE if "arg" is only a variable, register, environment variable,
* option name or string.
*/
int
cmd_is_name_only(char_u *arg)
{
char_u *p = arg;
char_u *alias = NULL;
int name_only = FALSE;
if (*p == '@')
{
++p;
if (*p != NUL)
++p;
}
else if (*p == '\'' || *p == '"')
{
int r;
if (*p == '"')
r = eval_string(&p, NULL, FALSE, FALSE);
else
r = eval_lit_string(&p, NULL, FALSE, FALSE);
if (r == FAIL)
return FALSE;
}
else
{
if (*p == '&')
{
++p;
if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
p += 2;
}
else if (*p == '$')
++p;
(void)get_name_len(&p, &alias, FALSE, FALSE);
}
name_only = ends_excmd2(arg, skipwhite(p));
vim_free(alias);
return name_only;
}
/*
* ":eval".
*/
void
ex_eval(exarg_T *eap)
{
typval_T tv;
evalarg_T evalarg;
int name_only = FALSE;
long lnum = SOURCING_LNUM;
if (in_vim9script())
name_only = cmd_is_name_only(eap->arg);
fill_evalarg_from_eap(&evalarg, eap, eap->skip);
if (eval0(eap->arg, &tv, eap, &evalarg) == OK)
{
clear_tv(&tv);
if (in_vim9script() && name_only
&& (evalarg.eval_tofree == NULL
|| ends_excmd2(evalarg.eval_tofree,
skipwhite(evalarg.eval_tofree))))
{