-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
userfunc.c
5750 lines (5220 loc) · 139 KB
/
userfunc.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.
*/
/*
* userfunc.c: User defined function support
*/
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* All user-defined functions are found in this hashtable.
*/
static hashtab_T func_hashtab;
// Used by get_func_tv()
static garray_T funcargs = GA_EMPTY;
// pointer to funccal for currently active function
static funccall_T *current_funccal = NULL;
// Pointer to list of previously used funccal, still around because some
// item in it is still being used.
static funccall_T *previous_funccal = NULL;
static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
static char *e_funcdict = N_("E717: Dictionary entry already exists");
static char *e_funcref = N_("E718: Funcref required");
static char *e_nofunc = N_("E130: Unknown function: %s");
static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
static void func_clear(ufunc_T *fp, int force);
static int func_free(ufunc_T *fp, int force);
void
func_init()
{
hash_init(&func_hashtab);
}
/*
* Return the function hash table
*/
hashtab_T *
func_tbl_get(void)
{
return &func_hashtab;
}
/*
* Get one function argument.
* If "argtypes" is not NULL also get the type: "arg: type" (:def function).
* If "types_optional" is TRUE a missing type is OK, use "any".
* If "evalarg" is not NULL use it to check for an already declared name.
* Return a pointer to after the type.
* When something is wrong return "arg".
*/
static char_u *
one_function_arg(
char_u *arg,
garray_T *newargs,
garray_T *argtypes,
int types_optional,
evalarg_T *evalarg,
int is_vararg,
int skip)
{
char_u *p = arg;
char_u *arg_copy = NULL;
int is_underscore = FALSE;
while (ASCII_ISALNUM(*p) || *p == '_')
++p;
if (arg == p || isdigit(*arg)
|| (argtypes == NULL
&& ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
|| (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
{
if (!skip)
semsg(_("E125: Illegal argument: %s"), arg);
return arg;
}
// Vim9 script: cannot use script var name for argument. In function: also
// check local vars and arguments.
if (!skip && argtypes != NULL && check_defined(arg, p - arg,
evalarg == NULL ? NULL : evalarg->eval_cctx, TRUE) == FAIL)
return arg;
if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
return arg;
if (newargs != NULL)
{
int c;
int i;
c = *p;
*p = NUL;
arg_copy = vim_strsave(arg);
if (arg_copy == NULL)
{
*p = c;
return arg;
}
is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
if (argtypes == NULL || !is_underscore)
// Check for duplicate argument name.
for (i = 0; i < newargs->ga_len; ++i)
if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
{
semsg(_("E853: Duplicate argument name: %s"), arg_copy);
vim_free(arg_copy);
return arg;
}
((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
newargs->ga_len++;
*p = c;
}
// get any type from "arg: type"
if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
{
char_u *type = NULL;
if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
{
semsg(_(e_no_white_space_allowed_before_colon_str),
arg_copy == NULL ? arg : arg_copy);
p = skipwhite(p);
}
if (*p == ':')
{
++p;
if (!skip && !VIM_ISWHITE(*p))
{
semsg(_(e_white_space_required_after_str_str), ":", p - 1);
return arg;
}
type = skipwhite(p);
p = skip_type(type, TRUE);
if (!skip)
type = vim_strnsave(type, p - type);
}
else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
{
semsg(_(e_missing_argument_type_for_str),
arg_copy == NULL ? arg : arg_copy);
return arg;
}
if (!skip)
{
if (type == NULL && types_optional)
// lambda arguments default to "any" type
type = vim_strsave((char_u *)
(is_vararg ? "list<any>" : "any"));
((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
}
}
return p;
}
/*
* Get function arguments.
* "argp" should point to just after the "(", possibly to white space.
* "argp" is advanced just after "endchar".
*/
static int
get_function_args(
char_u **argp,
char_u endchar,
garray_T *newargs,
garray_T *argtypes, // NULL unless using :def
int types_optional, // types optional if "argtypes" is not NULL
evalarg_T *evalarg, // context or NULL
int *varargs,
garray_T *default_args,
int skip,
exarg_T *eap,
char_u **line_to_free)
{
int mustend = FALSE;
char_u *arg;
char_u *p;
int c;
int any_default = FALSE;
char_u *expr;
char_u *whitep = *argp;
if (newargs != NULL)
ga_init2(newargs, (int)sizeof(char_u *), 3);
if (argtypes != NULL)
ga_init2(argtypes, (int)sizeof(char_u *), 3);
if (!skip && default_args != NULL)
ga_init2(default_args, (int)sizeof(char_u *), 3);
if (varargs != NULL)
*varargs = FALSE;
/*
* Isolate the arguments: "arg1, arg2, ...)"
*/
arg = skipwhite(*argp);
p = arg;
while (*p != endchar)
{
while (eap != NULL && eap->getline != NULL
&& (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
{
char_u *theline;
// End of the line, get the next one.
theline = eap->getline(':', eap->cookie, 0, TRUE);
if (theline == NULL)
break;
vim_free(*line_to_free);
*line_to_free = theline;
whitep = (char_u *)" ";
p = skipwhite(theline);
}
if (mustend && *p != endchar)
{
if (!skip)
semsg(_(e_invarg2), *argp);
goto err_ret;
}
if (*p == endchar)
break;
if (p[0] == '.' && p[1] == '.' && p[2] == '.')
{
if (varargs != NULL)
*varargs = TRUE;
p += 3;
mustend = TRUE;
if (argtypes != NULL)
{
// ...name: list<type>
if (!eval_isnamec1(*p))
{
if (!skip)
emsg(_(e_missing_name_after_dots));
goto err_ret;
}
arg = p;
p = one_function_arg(p, newargs, argtypes, types_optional,
evalarg, TRUE, skip);
if (p == arg)
break;
if (*skipwhite(p) == '=')
{
emsg(_(e_cannot_use_default_for_variable_arguments));
break;
}
}
}
else
{
char_u *np;
arg = p;
p = one_function_arg(p, newargs, argtypes, types_optional,
evalarg, FALSE, skip);
if (p == arg)
break;
// Recognize " = expr" but not " == expr". A lambda can have
// "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
np = skipwhite(p);
if (*np == '=' && np[1] != '=' && np[1] != '~'
&& default_args != NULL)
{
typval_T rettv;
// find the end of the expression (doesn't evaluate it)
any_default = TRUE;
p = skipwhite(p) + 1;
whitep = p;
p = skipwhite(p);
expr = p;
if (eval1(&p, &rettv, NULL) != FAIL)
{
if (!skip)
{
if (ga_grow(default_args, 1) == FAIL)
goto err_ret;
// trim trailing whitespace
while (p > expr && VIM_ISWHITE(p[-1]))
p--;
c = *p;
*p = NUL;
expr = vim_strsave(expr);
if (expr == NULL)
{
*p = c;
goto err_ret;
}
((char_u **)(default_args->ga_data))
[default_args->ga_len] = expr;
default_args->ga_len++;
*p = c;
}
}
else
mustend = TRUE;
}
else if (any_default)
{
emsg(_("E989: Non-default argument follows default argument"));
goto err_ret;
}
if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
{
// Be tolerant when skipping
if (!skip)
{
semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
goto err_ret;
}
p = skipwhite(p);
}
if (*p == ',')
{
++p;
// Don't give this error when skipping, it makes the "->" not
// found in "{k,v -> x}" and give a confusing error.
// Allow missing space after comma in legacy functions.
if (!skip && argtypes != NULL
&& !IS_WHITE_OR_NUL(*p) && *p != endchar)
{
semsg(_(e_white_space_required_after_str_str), ",", p - 1);
goto err_ret;
}
}
else
mustend = TRUE;
}
whitep = p;
p = skipwhite(p);
}
if (*p != endchar)
goto err_ret;
++p; // skip "endchar"
*argp = p;
return OK;
err_ret:
if (newargs != NULL)
ga_clear_strings(newargs);
if (!skip && default_args != NULL)
ga_clear_strings(default_args);
return FAIL;
}
/*
* Parse the argument types, filling "fp->uf_arg_types".
* Return OK or FAIL.
*/
static int
parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
{
int len = 0;
ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
if (argtypes->ga_len > 0)
{
// When "varargs" is set the last name/type goes into uf_va_name
// and uf_va_type.
len = argtypes->ga_len - (varargs ? 1 : 0);
if (len > 0)
fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
if (fp->uf_arg_types != NULL)
{
int i;
type_T *type;
for (i = 0; i < len; ++ i)
{
char_u *p = ((char_u **)argtypes->ga_data)[i];
if (p == NULL)
// will get the type from the default value
type = &t_unknown;
else
type = parse_type(&p, &fp->uf_type_list, TRUE);
if (type == NULL)
return FAIL;
fp->uf_arg_types[i] = type;
}
}
}
if (varargs)
{
char_u *p;
// Move the last argument "...name: type" to uf_va_name and
// uf_va_type.
fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
[fp->uf_args.ga_len - 1];
--fp->uf_args.ga_len;
p = ((char_u **)argtypes->ga_data)[len];
if (p == NULL)
// TODO: get type from default value
fp->uf_va_type = &t_list_any;
else
{
fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
{
semsg(_(e_variable_arguments_type_must_be_list_str),
((char_u **)argtypes->ga_data)[len]);
return FAIL;
}
}
if (fp->uf_va_type == NULL)
return FAIL;
}
return OK;
}
static int
parse_return_type(ufunc_T *fp, char_u *ret_type)
{
if (ret_type == NULL)
fp->uf_ret_type = &t_void;
else
{
char_u *p = ret_type;
fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
if (fp->uf_ret_type == NULL)
{
fp->uf_ret_type = &t_void;
return FAIL;
}
}
return OK;
}
/*
* Register function "fp" as using "current_funccal" as its scope.
*/
static int
register_closure(ufunc_T *fp)
{
if (fp->uf_scoped == current_funccal)
// no change
return OK;
funccal_unref(fp->uf_scoped, fp, FALSE);
fp->uf_scoped = current_funccal;
current_funccal->fc_refcount++;
if (ga_grow(¤t_funccal->fc_funcs, 1) == FAIL)
return FAIL;
((ufunc_T **)current_funccal->fc_funcs.ga_data)
[current_funccal->fc_funcs.ga_len++] = fp;
return OK;
}
static void
set_ufunc_name(ufunc_T *fp, char_u *name)
{
// Add a type cast to avoid a warning for an overflow, the uf_name[] array
// actually extends beyond the struct.
STRCPY((void *)fp->uf_name, name);
if (name[0] == K_SPECIAL)
{
fp->uf_name_exp = alloc(STRLEN(name) + 3);
if (fp->uf_name_exp != NULL)
{
STRCPY(fp->uf_name_exp, "<SNR>");
STRCAT(fp->uf_name_exp, fp->uf_name + 3);
}
}
}
/*
* Get a name for a lambda. Returned in static memory.
*/
char_u *
get_lambda_name(void)
{
static char_u name[30];
static int lambda_no = 0;
sprintf((char*)name, "<lambda>%d", ++lambda_no);
return name;
}
#if defined(FEAT_LUA) || defined(PROTO)
/*
* Registers a native C callback which can be called from Vim script.
* Returns the name of the Vim script function.
*/
char_u *
register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
{
char_u *name = get_lambda_name();
ufunc_T *fp;
fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
if (fp == NULL)
return NULL;
fp->uf_def_status = UF_NOT_COMPILED;
fp->uf_refcount = 1;
fp->uf_varargs = TRUE;
fp->uf_flags = FC_CFUNC;
fp->uf_calls = 0;
fp->uf_script_ctx = current_sctx;
fp->uf_cb = cb;
fp->uf_cb_free = cb_free;
fp->uf_cb_state = state;
set_ufunc_name(fp, name);
hash_add(&func_hashtab, UF2HIKEY(fp));
return name;
}
#endif
/*
* Skip over "->" or "=>" after the arguments of a lambda.
* If ": type" is found make "ret_type" point to "type".
* If "white_error" is not NULL check for correct use of white space and set
* "white_error" to TRUE if there is an error.
* Return NULL if no valid arrow found.
*/
static char_u *
skip_arrow(
char_u *start,
int equal_arrow,
char_u **ret_type,
int *white_error)
{
char_u *s = start;
char_u *bef = start - 2; // "start" points to > of ->
if (equal_arrow)
{
if (*s == ':')
{
if (white_error != NULL && !VIM_ISWHITE(s[1]))
{
*white_error = TRUE;
semsg(_(e_white_space_required_after_str_str), ":", s);
return NULL;
}
s = skipwhite(s + 1);
*ret_type = s;
s = skip_type(s, TRUE);
if (s == *ret_type)
{
emsg(_(e_missing_return_type));
return NULL;
}
}
bef = s;
s = skipwhite(s);
if (*s != '=')
return NULL;
++s;
}
if (*s != '>')
return NULL;
if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
|| !IS_WHITE_OR_NUL(s[1])))
{
*white_error = TRUE;
semsg(_(e_white_space_required_before_and_after_str_at_str),
equal_arrow ? "=>" : "->", bef);
return NULL;
}
return skipwhite(s + 1);
}
/*
* Check if "*cmd" points to a function command and if so advance "*cmd" and
* return TRUE.
* Otherwise return FALSE;
* Do not consider "function(" to be a command.
*/
static int
is_function_cmd(char_u **cmd)
{
char_u *p = *cmd;
if (checkforcmd(&p, "function", 2))
{
if (*p == '(')
return FALSE;
*cmd = p;
return TRUE;
}
return FALSE;
}
/*
* Called when defining a function: The context may be needed for script
* variables declared in a block that is visible now but not when the function
* is compiled or called later.
*/
static void
function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
{
if (cstack != NULL && cstack->cs_idx >= 0)
{
int count = cstack->cs_idx + 1;
int i;
fp->uf_block_ids = ALLOC_MULT(int, count);
if (fp->uf_block_ids != NULL)
{
mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
sizeof(int) * count);
fp->uf_block_depth = count;
}
// Set flag in each block to indicate a function was defined. This
// is used to keep the variable when leaving the block, see
// hide_script_var().
for (i = 0; i <= cstack->cs_idx; ++i)
cstack->cs_flags[i] |= CSF_FUNC_DEF;
}
}
/*
* Read the body of a function, put every line in "newlines".
* This stops at "}", "endfunction" or "enddef".
* "newlines" must already have been initialized.
* "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
*/
static int
get_function_body(
exarg_T *eap,
garray_T *newlines,
char_u *line_arg_in,
char_u **line_to_free)
{
linenr_T sourcing_lnum_top = SOURCING_LNUM;
linenr_T sourcing_lnum_off;
int saved_wait_return = need_wait_return;
char_u *line_arg = line_arg_in;
int vim9_function = eap->cmdidx == CMD_def
|| eap->cmdidx == CMD_block;
#define MAX_FUNC_NESTING 50
char nesting_def[MAX_FUNC_NESTING];
char nesting_inline[MAX_FUNC_NESTING];
int nesting = 0;
getline_opt_T getline_options;
int indent = 2;
char_u *skip_until = NULL;
int ret = FAIL;
int is_heredoc = FALSE;
int heredoc_concat_len = 0;
garray_T heredoc_ga;
char_u *heredoc_trimmed = NULL;
ga_init2(&heredoc_ga, 1, 500);
// Detect having skipped over comment lines to find the return
// type. Add NULL lines to keep the line count correct.
sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
if (SOURCING_LNUM < sourcing_lnum_off)
{
sourcing_lnum_off -= SOURCING_LNUM;
if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
goto theend;
while (sourcing_lnum_off-- > 0)
((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
}
nesting_def[0] = vim9_function;
nesting_inline[0] = eap->cmdidx == CMD_block;
getline_options = vim9_function
? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
for (;;)
{
char_u *theline;
char_u *p;
char_u *arg;
if (KeyTyped)
{
msg_scroll = TRUE;
saved_wait_return = FALSE;
}
need_wait_return = FALSE;
if (line_arg != NULL)
{
// Use eap->arg, split up in parts by line breaks.
theline = line_arg;
p = vim_strchr(theline, '\n');
if (p == NULL)
line_arg += STRLEN(line_arg);
else
{
*p = NUL;
line_arg = p + 1;
}
}
else
{
vim_free(*line_to_free);
if (eap->getline == NULL)
theline = getcmdline(':', 0L, indent, getline_options);
else
theline = eap->getline(':', eap->cookie, indent,
getline_options);
*line_to_free = theline;
}
if (KeyTyped)
lines_left = Rows - 1;
if (theline == NULL)
{
// Use the start of the function for the line number.
SOURCING_LNUM = sourcing_lnum_top;
if (skip_until != NULL)
semsg(_(e_missing_heredoc_end_marker_str), skip_until);
else if (nesting_inline[nesting])
emsg(_(e_missing_end_block));
else if (eap->cmdidx == CMD_def)
emsg(_(e_missing_enddef));
else
emsg(_("E126: Missing :endfunction"));
goto theend;
}
// Detect line continuation: SOURCING_LNUM increased more than one.
sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
if (SOURCING_LNUM < sourcing_lnum_off)
sourcing_lnum_off -= SOURCING_LNUM;
else
sourcing_lnum_off = 0;
if (skip_until != NULL)
{
// Don't check for ":endfunc"/":enddef" between
// * ":append" and "."
// * ":python <<EOF" and "EOF"
// * ":let {var-name} =<< [trim] {marker}" and "{marker}"
if (heredoc_trimmed == NULL
|| (is_heredoc && skipwhite(theline) == theline)
|| STRNCMP(theline, heredoc_trimmed,
STRLEN(heredoc_trimmed)) == 0)
{
if (heredoc_trimmed == NULL)
p = theline;
else if (is_heredoc)
p = skipwhite(theline) == theline
? theline : theline + STRLEN(heredoc_trimmed);
else
p = theline + STRLEN(heredoc_trimmed);
if (STRCMP(p, skip_until) == 0)
{
VIM_CLEAR(skip_until);
VIM_CLEAR(heredoc_trimmed);
getline_options = vim9_function
? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
is_heredoc = FALSE;
if (heredoc_concat_len > 0)
{
// Replace the starting line with all the concatenated
// lines.
ga_concat(&heredoc_ga, theline);
vim_free(((char_u **)(newlines->ga_data))[
heredoc_concat_len - 1]);
((char_u **)(newlines->ga_data))[
heredoc_concat_len - 1] = heredoc_ga.ga_data;
ga_init(&heredoc_ga);
heredoc_concat_len = 0;
theline += STRLEN(theline); // skip the "EOF"
}
}
}
}
else
{
int c;
char_u *end;
// skip ':' and blanks
for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
;
// Check for "endfunction", "enddef" or "}".
// When a ":" follows it must be a dict key; "enddef: value,"
if (nesting_inline[nesting]
? *p == '}'
: (checkforcmd(&p, nesting_def[nesting]
? "enddef" : "endfunction", 4)
&& *p != ':'))
{
if (nesting-- == 0)
{
char_u *nextcmd = NULL;
if (*p == '|' || *p == '}')
nextcmd = p + 1;
else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
nextcmd = line_arg;
else if (*p != NUL && *p != (vim9_function ? '#' : '"')
&& (vim9_function || p_verbose > 0))
{
SOURCING_LNUM = sourcing_lnum_top
+ newlines->ga_len + 1;
if (eap->cmdidx == CMD_def)
semsg(_(e_text_found_after_enddef_str), p);
else
give_warning2((char_u *)
_("W22: Text found after :endfunction: %s"),
p, TRUE);
}
if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
{
// Another command follows. If the line came from "eap"
// we can simply point into it, otherwise we need to
// change "eap->cmdlinep".
eap->nextcmd = nextcmd;
if (*line_to_free != NULL)
{
vim_free(*eap->cmdlinep);
*eap->cmdlinep = *line_to_free;
*line_to_free = NULL;
}
}
break;
}
}
// Check for mismatched "endfunc" or "enddef".
// We don't check for "def" inside "func" thus we also can't check
// for "enddef".
// We continue to find the end of the function, although we might
// not find it.
else if (nesting_def[nesting])
{
if (checkforcmd(&p, "endfunction", 4) && *p != ':')
emsg(_(e_mismatched_endfunction));
}
else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
emsg(_(e_mismatched_enddef));
// Increase indent inside "if", "while", "for" and "try", decrease
// at "end".
if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
indent -= 2;
else if (STRNCMP(p, "if", 2) == 0
|| STRNCMP(p, "wh", 2) == 0
|| STRNCMP(p, "for", 3) == 0
|| STRNCMP(p, "try", 3) == 0)
indent += 2;
// Check for defining a function inside this function.
// Only recognize "def" inside "def", not inside "function",
// For backwards compatibility, see Test_function_python().
c = *p;
if (is_function_cmd(&p)
|| (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
{
if (*p == '!')
p = skipwhite(p + 1);
p += eval_fname_script(p);
vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
NULL, NULL));
if (*skipwhite(p) == '(')
{
if (nesting == MAX_FUNC_NESTING - 1)
emsg(_(e_function_nesting_too_deep));
else
{
++nesting;
nesting_def[nesting] = (c == 'd');
nesting_inline[nesting] = FALSE;
indent += 2;
}
}
}
if (nesting_def[nesting] ? *p != '#' : *p != '"')
{
// Not a comment line: check for nested inline function.
end = p + STRLEN(p) - 1;
while (end > p && VIM_ISWHITE(*end))
--end;
if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
{
int is_block;
// check for trailing "=> {": start of an inline function
--end;
while (end > p && VIM_ISWHITE(*end))
--end;
is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
if (!is_block)
{
char_u *s = p;
// check for line starting with "au" for :autocmd or
// "com" for :command, these can use a {} block
is_block = checkforcmd_noparen(&s, "autocmd", 2)
|| checkforcmd_noparen(&s, "command", 3);
}
if (is_block)
{
if (nesting == MAX_FUNC_NESTING - 1)
emsg(_(e_function_nesting_too_deep));
else
{
++nesting;
nesting_def[nesting] = TRUE;
nesting_inline[nesting] = TRUE;
indent += 2;
}
}
}
}
// Check for ":append", ":change", ":insert". Not for :def.
p = skip_range(p, FALSE, NULL);
if (!vim9_function
&& ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
|| (p[0] == 'c'
&& (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
&& (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
&& (STRNCMP(&p[3], "nge", 3) != 0
|| !ASCII_ISALPHA(p[6])))))))
|| (p[0] == 'i'
&& (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
&& (!ASCII_ISALPHA(p[2])
|| (p[2] == 's'
&& (!ASCII_ISALPHA(p[3])
|| p[3] == 'e'))))))))
skip_until = vim_strsave((char_u *)".");
// Check for ":python <<EOF", ":tcl <<EOF", etc.
arg = skipwhite(skiptowhite(p));
if (arg[0] == '<' && arg[1] =='<'
&& ((p[0] == 'p' && p[1] == 'y'
&& (!ASCII_ISALNUM(p[2]) || p[2] == 't'
|| ((p[2] == '3' || p[2] == 'x')
&& !ASCII_ISALPHA(p[3]))))
|| (p[0] == 'p' && p[1] == 'e'
&& (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
|| (p[0] == 't' && p[1] == 'c'
&& (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
|| (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
&& !ASCII_ISALPHA(p[3]))
|| (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
&& (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
|| (p[0] == 'm' && p[1] == 'z'
&& (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
))
{
// ":python <<" continues until a dot, like ":append"
p = skipwhite(arg + 2);
if (STRNCMP(p, "trim", 4) == 0)
{
// Ignore leading white space.
p = skipwhite(p + 4);
heredoc_trimmed = vim_strnsave(theline,
skipwhite(theline) - theline);
}
if (*p == NUL)
skip_until = vim_strsave((char_u *)".");
else
skip_until = vim_strnsave(p, skiptowhite(p) - p);
getline_options = GETLINE_NONE;
is_heredoc = TRUE;
if (eap->cmdidx == CMD_def)
heredoc_concat_len = newlines->ga_len + 1;
}
// Check for ":cmd v =<< [trim] EOF"
// and ":cmd [a, b] =<< [trim] EOF"
// and "lines =<< [trim] EOF" for Vim9
// Where "cmd" can be "let", "var", "final" or "const".
arg = skipwhite(skiptowhite(p));
if (*arg == '[')