xref: /vim-8.2.3635/src/eval.c (revision ca753ec8)
1 /* vi:set ts=8 sts=4 sw=4 noet:
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  * eval.c: Expression evaluation.
12  */
13 #define USING_FLOAT_STUFF
14 
15 #include "vim.h"
16 
17 #if defined(FEAT_EVAL) || defined(PROTO)
18 
19 #ifdef VMS
20 # include <float.h>
21 #endif
22 
23 #define NAMESPACE_CHAR	(char_u *)"abglstvw"
24 
25 /*
26  * When recursively copying lists and dicts we need to remember which ones we
27  * have done to avoid endless recursiveness.  This unique ID is used for that.
28  * The last bit is used for previous_funccal, ignored when comparing.
29  */
30 static int current_copyID = 0;
31 
32 /*
33  * Info used by a ":for" loop.
34  */
35 typedef struct
36 {
37     int		fi_semicolon;	// TRUE if ending in '; var]'
38     int		fi_varcount;	// nr of variables in the list
39     int		fi_break_count;	// nr of line breaks encountered
40     listwatch_T	fi_lw;		// keep an eye on the item used.
41     list_T	*fi_list;	// list being used
42     int		fi_bi;		// index of blob
43     blob_T	*fi_blob;	// blob being used
44 } forinfo_T;
45 
46 static int tv_op(typval_T *tv1, typval_T *tv2, char_u  *op);
47 static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48 static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49 static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50 static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51 static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52 static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
53 static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
54 
55 static int free_unref_items(int copyID);
56 static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
57 
58 /*
59  * Return "n1" divided by "n2", taking care of dividing by zero.
60  * If "failed" is not NULL set it to TRUE when dividing by zero fails.
61  */
62 	varnumber_T
63 num_divide(varnumber_T n1, varnumber_T n2, int *failed)
64 {
65     varnumber_T	result;
66 
67     if (n2 == 0)
68     {
69 	if (in_vim9script())
70 	{
71 	    emsg(_(e_divide_by_zero));
72 	    if (failed != NULL)
73 		*failed = TRUE;
74 	}
75 	if (n1 == 0)
76 	    result = VARNUM_MIN; // similar to NaN
77 	else if (n1 < 0)
78 	    result = -VARNUM_MAX;
79 	else
80 	    result = VARNUM_MAX;
81     }
82     else
83 	result = n1 / n2;
84 
85     return result;
86 }
87 
88 /*
89  * Return "n1" modulus "n2", taking care of dividing by zero.
90  * If "failed" is not NULL set it to TRUE when dividing by zero fails.
91  */
92 	varnumber_T
93 num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
94 {
95     if (n2 == 0 && in_vim9script())
96     {
97 	emsg(_(e_divide_by_zero));
98 	if (failed != NULL)
99 	    *failed = TRUE;
100     }
101     return (n2 == 0) ? 0 : (n1 % n2);
102 }
103 
104 #if defined(EBCDIC) || defined(PROTO)
105 /*
106  * Compare struct fst by function name.
107  */
108     static int
109 compare_func_name(const void *s1, const void *s2)
110 {
111     struct fst *p1 = (struct fst *)s1;
112     struct fst *p2 = (struct fst *)s2;
113 
114     return STRCMP(p1->f_name, p2->f_name);
115 }
116 
117 /*
118  * Sort the function table by function name.
119  * The sorting of the table above is ASCII dependent.
120  * On machines using EBCDIC we have to sort it.
121  */
122     static void
123 sortFunctions(void)
124 {
125     int		funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
126 
127     qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
128 }
129 #endif
130 
131 /*
132  * Initialize the global and v: variables.
133  */
134     void
135 eval_init(void)
136 {
137     evalvars_init();
138     func_init();
139 
140 #ifdef EBCDIC
141     /*
142      * Sort the function table, to enable binary search.
143      */
144     sortFunctions();
145 #endif
146 }
147 
148 #if defined(EXITFREE) || defined(PROTO)
149     void
150 eval_clear(void)
151 {
152     evalvars_clear();
153     free_scriptnames();  // must come after evalvars_clear().
154     free_locales();
155 
156     // autoloaded script names
157     free_autoload_scriptnames();
158 
159     // unreferenced lists and dicts
160     (void)garbage_collect(FALSE);
161 
162     // functions not garbage collected
163     free_all_functions();
164 }
165 #endif
166 
167     void
168 fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
169 {
170     CLEAR_FIELD(*evalarg);
171     evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
172     if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
173     {
174 	evalarg->eval_getline = eap->getline;
175 	evalarg->eval_cookie = eap->cookie;
176     }
177 }
178 
179 /*
180  * Top level evaluation function, returning a boolean.
181  * Sets "error" to TRUE if there was an error.
182  * Return TRUE or FALSE.
183  */
184     int
185 eval_to_bool(
186     char_u	*arg,
187     int		*error,
188     exarg_T	*eap,
189     int		skip)	    // only parse, don't execute
190 {
191     typval_T	tv;
192     varnumber_T	retval = FALSE;
193     evalarg_T	evalarg;
194 
195     fill_evalarg_from_eap(&evalarg, eap, skip);
196 
197     if (skip)
198 	++emsg_skip;
199     if (eval0(arg, &tv, eap, &evalarg) == FAIL)
200 	*error = TRUE;
201     else
202     {
203 	*error = FALSE;
204 	if (!skip)
205 	{
206 	    if (in_vim9script())
207 		retval = tv_get_bool_chk(&tv, error);
208 	    else
209 		retval = (tv_get_number_chk(&tv, error) != 0);
210 	    clear_tv(&tv);
211 	}
212     }
213     if (skip)
214 	--emsg_skip;
215     clear_evalarg(&evalarg, eap);
216 
217     return (int)retval;
218 }
219 
220 /*
221  * Call eval1() and give an error message if not done at a lower level.
222  */
223     static int
224 eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
225 {
226     char_u	*start = *arg;
227     int		ret;
228     int		did_emsg_before = did_emsg;
229     int		called_emsg_before = called_emsg;
230     evalarg_T	evalarg;
231 
232     fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
233 
234     ret = eval1(arg, rettv, &evalarg);
235     if (ret == FAIL)
236     {
237 	// Report the invalid expression unless the expression evaluation has
238 	// been cancelled due to an aborting error, an interrupt, or an
239 	// exception, or we already gave a more specific error.
240 	// Also check called_emsg for when using assert_fails().
241 	if (!aborting() && did_emsg == did_emsg_before
242 					  && called_emsg == called_emsg_before)
243 	    semsg(_(e_invexpr2), start);
244     }
245     clear_evalarg(&evalarg, eap);
246     return ret;
247 }
248 
249 /*
250  * Return whether a typval is a valid expression to pass to eval_expr_typval()
251  * or eval_expr_to_bool().  An empty string returns FALSE;
252  */
253     int
254 eval_expr_valid_arg(typval_T *tv)
255 {
256     return tv->v_type != VAR_UNKNOWN
257 	    && (tv->v_type != VAR_STRING
258 		  || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
259 }
260 
261 /*
262  * Evaluate an expression, which can be a function, partial or string.
263  * Pass arguments "argv[argc]".
264  * Return the result in "rettv" and OK or FAIL.
265  */
266     int
267 eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
268 {
269     char_u	*s;
270     char_u	buf[NUMBUFLEN];
271     funcexe_T	funcexe;
272 
273     if (expr->v_type == VAR_FUNC)
274     {
275 	s = expr->vval.v_string;
276 	if (s == NULL || *s == NUL)
277 	    return FAIL;
278 	CLEAR_FIELD(funcexe);
279 	funcexe.evaluate = TRUE;
280 	if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
281 	    return FAIL;
282     }
283     else if (expr->v_type == VAR_PARTIAL)
284     {
285 	partial_T   *partial = expr->vval.v_partial;
286 
287 	if (partial == NULL)
288 	    return FAIL;
289 
290 	if (partial->pt_func != NULL
291 			  && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
292 	{
293 	    if (call_def_function(partial->pt_func, argc, argv,
294 						       partial, rettv) == FAIL)
295 		return FAIL;
296 	}
297 	else
298 	{
299 	    s = partial_name(partial);
300 	    if (s == NULL || *s == NUL)
301 		return FAIL;
302 	    CLEAR_FIELD(funcexe);
303 	    funcexe.evaluate = TRUE;
304 	    funcexe.partial = partial;
305 	    if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 		return FAIL;
307 	}
308     }
309     else
310     {
311 	s = tv_get_string_buf_chk(expr, buf);
312 	if (s == NULL)
313 	    return FAIL;
314 	s = skipwhite(s);
315 	if (eval1_emsg(&s, rettv, NULL) == FAIL)
316 	    return FAIL;
317 	if (*skipwhite(s) != NUL)  // check for trailing chars after expr
318 	{
319 	    clear_tv(rettv);
320 	    semsg(_(e_invexpr2), s);
321 	    return FAIL;
322 	}
323     }
324     return OK;
325 }
326 
327 /*
328  * Like eval_to_bool() but using a typval_T instead of a string.
329  * Works for string, funcref and partial.
330  */
331     int
332 eval_expr_to_bool(typval_T *expr, int *error)
333 {
334     typval_T	rettv;
335     int		res;
336 
337     if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
338     {
339 	*error = TRUE;
340 	return FALSE;
341     }
342     res = (tv_get_bool_chk(&rettv, error) != 0);
343     clear_tv(&rettv);
344     return res;
345 }
346 
347 /*
348  * Top level evaluation function, returning a string.  If "skip" is TRUE,
349  * only parsing to "nextcmd" is done, without reporting errors.  Return
350  * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
351  */
352     char_u *
353 eval_to_string_skip(
354     char_u	*arg,
355     exarg_T	*eap,
356     int		skip)	    // only parse, don't execute
357 {
358     typval_T	tv;
359     char_u	*retval;
360     evalarg_T	evalarg;
361 
362     fill_evalarg_from_eap(&evalarg, eap, skip);
363     if (skip)
364 	++emsg_skip;
365     if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
366 	retval = NULL;
367     else
368     {
369 	retval = vim_strsave(tv_get_string(&tv));
370 	clear_tv(&tv);
371     }
372     if (skip)
373 	--emsg_skip;
374     clear_evalarg(&evalarg, eap);
375 
376     return retval;
377 }
378 
379 /*
380  * Skip over an expression at "*pp".
381  * Return FAIL for an error, OK otherwise.
382  */
383     int
384 skip_expr(char_u **pp, evalarg_T *evalarg)
385 {
386     typval_T	rettv;
387 
388     *pp = skipwhite(*pp);
389     return eval1(pp, &rettv, evalarg);
390 }
391 
392 /*
393  * Skip over an expression at "*pp".
394  * If in Vim9 script and line breaks are encountered, the lines are
395  * concatenated.  "evalarg->eval_tofree" will be set accordingly.
396  * "arg" is advanced to just after the expression.
397  * "start" is set to the start of the expression, "end" to just after the end.
398  * Also when the expression is copied to allocated memory.
399  * Return FAIL for an error, OK otherwise.
400  */
401     int
402 skip_expr_concatenate(
403 	char_u	    **arg,
404 	char_u	    **start,
405 	char_u	    **end,
406 	evalarg_T   *evalarg)
407 {
408     typval_T	rettv;
409     int		res;
410     int		vim9script = in_vim9script();
411     garray_T    *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
412     int		save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
413     int		evaluate = evalarg == NULL
414 			       ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
415 
416     if (vim9script && evaluate
417 	       && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
418     {
419 	ga_init2(gap, sizeof(char_u *), 10);
420 	// leave room for "start"
421 	if (ga_grow(gap, 1) == OK)
422 	    ++gap->ga_len;
423     }
424     *start = *arg;
425 
426     // Don't evaluate the expression.
427     if (evalarg != NULL)
428 	evalarg->eval_flags &= ~EVAL_EVALUATE;
429     *arg = skipwhite(*arg);
430     res = eval1(arg, &rettv, evalarg);
431     *end = *arg;
432     if (evalarg != NULL)
433 	evalarg->eval_flags = save_flags;
434 
435     if (vim9script && evaluate
436 	    && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
437     {
438 	if (evalarg->eval_ga.ga_len == 1)
439 	{
440 	    // just one line, no need to concatenate
441 	    ga_clear(gap);
442 	    gap->ga_itemsize = 0;
443 	}
444 	else
445 	{
446 	    char_u	    *p;
447 	    size_t	    endoff = STRLEN(*arg);
448 
449 	    // Line breaks encountered, concatenate all the lines.
450 	    *((char_u **)gap->ga_data) = *start;
451 	    p = ga_concat_strings(gap, "");
452 
453 	    // free the lines only when using getsourceline()
454 	    if (evalarg->eval_cookie != NULL)
455 	    {
456 		// Do not free the first line, the caller can still use it.
457 		*((char_u **)gap->ga_data) = NULL;
458 		// Do not free the last line, "arg" points into it, free it
459 		// later.
460 		vim_free(evalarg->eval_tofree);
461 		evalarg->eval_tofree =
462 				    ((char_u **)gap->ga_data)[gap->ga_len - 1];
463 		((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
464 		ga_clear_strings(gap);
465 	    }
466 	    else
467 		ga_clear(gap);
468 	    gap->ga_itemsize = 0;
469 	    if (p == NULL)
470 		return FAIL;
471 	    *start = p;
472 	    vim_free(evalarg->eval_tofree_lambda);
473 	    evalarg->eval_tofree_lambda = p;
474 	    // Compute "end" relative to the end.
475 	    *end = *start + STRLEN(*start) - endoff;
476 	}
477     }
478 
479     return res;
480 }
481 
482 /*
483  * Convert "tv" to a string.
484  * When "convert" is TRUE convert a List into a sequence of lines and convert
485  * a Float to a String.
486  * Returns an allocated string (NULL when out of memory).
487  */
488     char_u *
489 typval2string(typval_T *tv, int convert)
490 {
491     garray_T	ga;
492     char_u	*retval;
493 #ifdef FEAT_FLOAT
494     char_u	numbuf[NUMBUFLEN];
495 #endif
496 
497     if (convert && tv->v_type == VAR_LIST)
498     {
499 	ga_init2(&ga, (int)sizeof(char), 80);
500 	if (tv->vval.v_list != NULL)
501 	{
502 	    list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
503 	    if (tv->vval.v_list->lv_len > 0)
504 		ga_append(&ga, NL);
505 	}
506 	ga_append(&ga, NUL);
507 	retval = (char_u *)ga.ga_data;
508     }
509 #ifdef FEAT_FLOAT
510     else if (convert && tv->v_type == VAR_FLOAT)
511     {
512 	vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
513 	retval = vim_strsave(numbuf);
514     }
515 #endif
516     else
517 	retval = vim_strsave(tv_get_string(tv));
518     return retval;
519 }
520 
521 /*
522  * Top level evaluation function, returning a string.  Does not handle line
523  * breaks.
524  * When "convert" is TRUE convert a List into a sequence of lines and convert
525  * a Float to a String.
526  * Return pointer to allocated memory, or NULL for failure.
527  */
528     char_u *
529 eval_to_string_eap(
530     char_u	*arg,
531     int		convert,
532     exarg_T	*eap)
533 {
534     typval_T	tv;
535     char_u	*retval;
536     evalarg_T	evalarg;
537 
538     fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
539     if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
540 	retval = NULL;
541     else
542     {
543 	retval = typval2string(&tv, convert);
544 	clear_tv(&tv);
545     }
546     clear_evalarg(&evalarg, NULL);
547 
548     return retval;
549 }
550 
551     char_u *
552 eval_to_string(
553     char_u	*arg,
554     int		convert)
555 {
556     return eval_to_string_eap(arg, convert, NULL);
557 }
558 
559 /*
560  * Call eval_to_string() without using current local variables and using
561  * textwinlock.  When "use_sandbox" is TRUE use the sandbox.
562  * Use legacy Vim script syntax.
563  */
564     char_u *
565 eval_to_string_safe(
566     char_u	*arg,
567     int		use_sandbox)
568 {
569     char_u	*retval;
570     funccal_entry_T funccal_entry;
571     int		save_sc_version = current_sctx.sc_version;
572 
573     current_sctx.sc_version = 1;
574     save_funccal(&funccal_entry);
575     if (use_sandbox)
576 	++sandbox;
577     ++textwinlock;
578     retval = eval_to_string(arg, FALSE);
579     if (use_sandbox)
580 	--sandbox;
581     --textwinlock;
582     restore_funccal();
583     current_sctx.sc_version = save_sc_version;
584     return retval;
585 }
586 
587 /*
588  * Top level evaluation function, returning a number.
589  * Evaluates "expr" silently.
590  * Returns -1 for an error.
591  */
592     varnumber_T
593 eval_to_number(char_u *expr)
594 {
595     typval_T	rettv;
596     varnumber_T	retval;
597     char_u	*p = skipwhite(expr);
598 
599     ++emsg_off;
600 
601     if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
602 	retval = -1;
603     else
604     {
605 	retval = tv_get_number_chk(&rettv, NULL);
606 	clear_tv(&rettv);
607     }
608     --emsg_off;
609 
610     return retval;
611 }
612 
613 /*
614  * Top level evaluation function.
615  * Returns an allocated typval_T with the result.
616  * Returns NULL when there is an error.
617  */
618     typval_T *
619 eval_expr(char_u *arg, exarg_T *eap)
620 {
621     typval_T	*tv;
622     evalarg_T	evalarg;
623 
624     fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
625 
626     tv = ALLOC_ONE(typval_T);
627     if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
628 	VIM_CLEAR(tv);
629 
630     clear_evalarg(&evalarg, eap);
631     return tv;
632 }
633 
634 /*
635  * Call some Vim script function and return the result in "*rettv".
636  * Uses argv[0] to argv[argc - 1] for the function arguments.  argv[argc]
637  * should have type VAR_UNKNOWN.
638  * Returns OK or FAIL.
639  */
640     int
641 call_vim_function(
642     char_u      *func,
643     int		argc,
644     typval_T	*argv,
645     typval_T	*rettv)
646 {
647     int		ret;
648     funcexe_T	funcexe;
649 
650     rettv->v_type = VAR_UNKNOWN;		// clear_tv() uses this
651     CLEAR_FIELD(funcexe);
652     funcexe.firstline = curwin->w_cursor.lnum;
653     funcexe.lastline = curwin->w_cursor.lnum;
654     funcexe.evaluate = TRUE;
655     ret = call_func(func, -1, rettv, argc, argv, &funcexe);
656     if (ret == FAIL)
657 	clear_tv(rettv);
658 
659     return ret;
660 }
661 
662 /*
663  * Call Vim script function "func" and return the result as a number.
664  * Returns -1 when calling the function fails.
665  * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
666  * have type VAR_UNKNOWN.
667  */
668     varnumber_T
669 call_func_retnr(
670     char_u      *func,
671     int		argc,
672     typval_T	*argv)
673 {
674     typval_T	rettv;
675     varnumber_T	retval;
676 
677     if (call_vim_function(func, argc, argv, &rettv) == FAIL)
678 	return -1;
679 
680     retval = tv_get_number_chk(&rettv, NULL);
681     clear_tv(&rettv);
682     return retval;
683 }
684 
685 /*
686  * Call Vim script function like call_func_retnr() and drop the result.
687  * Returns FAIL when calling the function fails.
688  */
689     int
690 call_func_noret(
691     char_u      *func,
692     int		argc,
693     typval_T	*argv)
694 {
695     typval_T	rettv;
696 
697     if (call_vim_function(func, argc, argv, &rettv) == FAIL)
698 	return FAIL;
699     clear_tv(&rettv);
700     return OK;
701 }
702 
703 /*
704  * Call Vim script function "func" and return the result as a string.
705  * Uses "argv" and "argc" as call_func_retnr().
706  * Returns NULL when calling the function fails.
707  */
708     void *
709 call_func_retstr(
710     char_u      *func,
711     int		argc,
712     typval_T	*argv)
713 {
714     typval_T	rettv;
715     char_u	*retval;
716 
717     if (call_vim_function(func, argc, argv, &rettv) == FAIL)
718 	return NULL;
719 
720     retval = vim_strsave(tv_get_string(&rettv));
721     clear_tv(&rettv);
722     return retval;
723 }
724 
725 /*
726  * Call Vim script function "func" and return the result as a List.
727  * Uses "argv" and "argc" as call_func_retnr().
728  * Returns NULL when there is something wrong.
729  */
730     void *
731 call_func_retlist(
732     char_u      *func,
733     int		argc,
734     typval_T	*argv)
735 {
736     typval_T	rettv;
737 
738     if (call_vim_function(func, argc, argv, &rettv) == FAIL)
739 	return NULL;
740 
741     if (rettv.v_type != VAR_LIST)
742     {
743 	clear_tv(&rettv);
744 	return NULL;
745     }
746 
747     return rettv.vval.v_list;
748 }
749 
750 #ifdef FEAT_FOLDING
751 /*
752  * Evaluate "arg", which is 'foldexpr'.
753  * Note: caller must set "curwin" to match "arg".
754  * Returns the foldlevel, and any character preceding it in "*cp".  Doesn't
755  * give error messages.
756  */
757     int
758 eval_foldexpr(char_u *arg, int *cp)
759 {
760     typval_T	tv;
761     varnumber_T	retval;
762     char_u	*s;
763     int		use_sandbox = was_set_insecurely((char_u *)"foldexpr",
764 								   OPT_LOCAL);
765 
766     ++emsg_off;
767     if (use_sandbox)
768 	++sandbox;
769     ++textwinlock;
770     *cp = NUL;
771     if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
772 	retval = 0;
773     else
774     {
775 	// If the result is a number, just return the number.
776 	if (tv.v_type == VAR_NUMBER)
777 	    retval = tv.vval.v_number;
778 	else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
779 	    retval = 0;
780 	else
781 	{
782 	    // If the result is a string, check if there is a non-digit before
783 	    // the number.
784 	    s = tv.vval.v_string;
785 	    if (!VIM_ISDIGIT(*s) && *s != '-')
786 		*cp = *s++;
787 	    retval = atol((char *)s);
788 	}
789 	clear_tv(&tv);
790     }
791     --emsg_off;
792     if (use_sandbox)
793 	--sandbox;
794     --textwinlock;
795     clear_evalarg(&EVALARG_EVALUATE, NULL);
796 
797     return (int)retval;
798 }
799 #endif
800 
801 /*
802  * Get an lval: variable, Dict item or List item that can be assigned a value
803  * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
804  * "name.key", "name.key[expr]" etc.
805  * Indexing only works if "name" is an existing List or Dictionary.
806  * "name" points to the start of the name.
807  * If "rettv" is not NULL it points to the value to be assigned.
808  * "unlet" is TRUE for ":unlet": slightly different behavior when something is
809  * wrong; must end in space or cmd separator.
810  *
811  * flags:
812  *  GLV_QUIET:       do not give error messages
813  *  GLV_READ_ONLY:   will not change the variable
814  *  GLV_NO_AUTOLOAD: do not use script autoloading
815  *
816  * Returns a pointer to just after the name, including indexes.
817  * When an evaluation error occurs "lp->ll_name" is NULL;
818  * Returns NULL for a parsing error.  Still need to free items in "lp"!
819  */
820     char_u *
821 get_lval(
822     char_u	*name,
823     typval_T	*rettv,
824     lval_T	*lp,
825     int		unlet,
826     int		skip,
827     int		flags,	    // GLV_ values
828     int		fne_flags)  // flags for find_name_end()
829 {
830     char_u	*p;
831     char_u	*expr_start, *expr_end;
832     int		cc;
833     dictitem_T	*v;
834     typval_T	var1;
835     typval_T	var2;
836     int		empty1 = FALSE;
837     listitem_T	*ni;
838     char_u	*key = NULL;
839     int		len;
840     hashtab_T	*ht = NULL;
841     int		quiet = flags & GLV_QUIET;
842     int		writing;
843 
844     // Clear everything in "lp".
845     CLEAR_POINTER(lp);
846 
847     if (skip || (flags & GLV_COMPILING))
848     {
849 	// When skipping or compiling just find the end of the name.
850 	lp->ll_name = name;
851 	lp->ll_name_end = find_name_end(name, NULL, NULL,
852 						      FNE_INCL_BR | fne_flags);
853 	return lp->ll_name_end;
854     }
855 
856     // Find the end of the name.
857     p = find_name_end(name, &expr_start, &expr_end, fne_flags);
858     lp->ll_name_end = p;
859     if (expr_start != NULL)
860     {
861 	// Don't expand the name when we already know there is an error.
862 	if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
863 						    && *p != '[' && *p != '.')
864 	{
865 	    semsg(_(e_trailing_arg), p);
866 	    return NULL;
867 	}
868 
869 	lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
870 	if (lp->ll_exp_name == NULL)
871 	{
872 	    // Report an invalid expression in braces, unless the
873 	    // expression evaluation has been cancelled due to an
874 	    // aborting error, an interrupt, or an exception.
875 	    if (!aborting() && !quiet)
876 	    {
877 		emsg_severe = TRUE;
878 		semsg(_(e_invarg2), name);
879 		return NULL;
880 	    }
881 	}
882 	lp->ll_name = lp->ll_exp_name;
883     }
884     else
885     {
886 	lp->ll_name = name;
887 
888 	if (in_vim9script())
889 	{
890 	    // "a: type" is declaring variable "a" with a type, not "a:".
891 	    if (p == name + 2 && p[-1] == ':')
892 	    {
893 		--p;
894 		lp->ll_name_end = p;
895 	    }
896 	    if (*p == ':')
897 	    {
898 		scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
899 		char_u	 *tp = skipwhite(p + 1);
900 
901 		// parse the type after the name
902 		lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
903 		if (lp->ll_type == NULL && !quiet)
904 		    return NULL;
905 		lp->ll_name_end = tp;
906 	    }
907 	}
908     }
909 
910     // Without [idx] or .key we are done.
911     if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
912 	return p;
913 
914     cc = *p;
915     *p = NUL;
916     // When we would write to the variable pass &ht and prevent autoload.
917     writing = !(flags & GLV_READ_ONLY);
918     v = find_var(lp->ll_name, writing ? &ht : NULL,
919 					 (flags & GLV_NO_AUTOLOAD) || writing);
920     if (v == NULL && !quiet)
921 	semsg(_(e_undefined_variable_str), lp->ll_name);
922     *p = cc;
923     if (v == NULL)
924 	return NULL;
925 
926     if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
927     {
928 	if (!quiet)
929 	    semsg(_(e_variable_already_declared), lp->ll_name);
930 	return NULL;
931     }
932 
933     /*
934      * Loop until no more [idx] or .key is following.
935      */
936     lp->ll_tv = &v->di_tv;
937     var1.v_type = VAR_UNKNOWN;
938     var2.v_type = VAR_UNKNOWN;
939     while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
940     {
941 	if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
942 		&& !(lp->ll_tv->v_type == VAR_DICT)
943 		&& !(lp->ll_tv->v_type == VAR_BLOB
944 					   && lp->ll_tv->vval.v_blob != NULL))
945 	{
946 	    if (!quiet)
947 		emsg(_("E689: Can only index a List, Dictionary or Blob"));
948 	    return NULL;
949 	}
950 	if (lp->ll_range)
951 	{
952 	    if (!quiet)
953 		emsg(_("E708: [:] must come last"));
954 	    return NULL;
955 	}
956 
957 	if (in_vim9script() && lp->ll_valtype == NULL
958 		&& lp->ll_tv == &v->di_tv
959 		&& ht != NULL && ht == get_script_local_ht())
960 	{
961 	    svar_T  *sv = find_typval_in_script(lp->ll_tv);
962 
963 	    // Vim9 script local variable: get the type
964 	    if (sv != NULL)
965 		lp->ll_valtype = sv->sv_type;
966 	}
967 
968 	len = -1;
969 	if (*p == '.')
970 	{
971 	    key = p + 1;
972 	    for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
973 		;
974 	    if (len == 0)
975 	    {
976 		if (!quiet)
977 		    emsg(_(e_emptykey));
978 		return NULL;
979 	    }
980 	    p = key + len;
981 	}
982 	else
983 	{
984 	    // Get the index [expr] or the first index [expr: ].
985 	    p = skipwhite(p + 1);
986 	    if (*p == ':')
987 		empty1 = TRUE;
988 	    else
989 	    {
990 		empty1 = FALSE;
991 		if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL)  // recursive!
992 		    return NULL;
993 		if (tv_get_string_chk(&var1) == NULL)
994 		{
995 		    // not a number or string
996 		    clear_tv(&var1);
997 		    return NULL;
998 		}
999 		p = skipwhite(p);
1000 	    }
1001 
1002 	    // Optionally get the second index [ :expr].
1003 	    if (*p == ':')
1004 	    {
1005 		if (lp->ll_tv->v_type == VAR_DICT)
1006 		{
1007 		    if (!quiet)
1008 			emsg(_(e_cannot_slice_dictionary));
1009 		    clear_tv(&var1);
1010 		    return NULL;
1011 		}
1012 		if (rettv != NULL
1013 			&& !(rettv->v_type == VAR_LIST
1014 						 && rettv->vval.v_list != NULL)
1015 			&& !(rettv->v_type == VAR_BLOB
1016 						&& rettv->vval.v_blob != NULL))
1017 		{
1018 		    if (!quiet)
1019 			emsg(_("E709: [:] requires a List or Blob value"));
1020 		    clear_tv(&var1);
1021 		    return NULL;
1022 		}
1023 		p = skipwhite(p + 1);
1024 		if (*p == ']')
1025 		    lp->ll_empty2 = TRUE;
1026 		else
1027 		{
1028 		    lp->ll_empty2 = FALSE;
1029 		    // recursive!
1030 		    if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
1031 		    {
1032 			clear_tv(&var1);
1033 			return NULL;
1034 		    }
1035 		    if (tv_get_string_chk(&var2) == NULL)
1036 		    {
1037 			// not a number or string
1038 			clear_tv(&var1);
1039 			clear_tv(&var2);
1040 			return NULL;
1041 		    }
1042 		}
1043 		lp->ll_range = TRUE;
1044 	    }
1045 	    else
1046 		lp->ll_range = FALSE;
1047 
1048 	    if (*p != ']')
1049 	    {
1050 		if (!quiet)
1051 		    emsg(_(e_missbrac));
1052 		clear_tv(&var1);
1053 		clear_tv(&var2);
1054 		return NULL;
1055 	    }
1056 
1057 	    // Skip to past ']'.
1058 	    ++p;
1059 	}
1060 
1061 	if (lp->ll_tv->v_type == VAR_DICT)
1062 	{
1063 	    if (len == -1)
1064 	    {
1065 		// "[key]": get key from "var1"
1066 		key = tv_get_string_chk(&var1);	// is number or string
1067 		if (key == NULL)
1068 		{
1069 		    clear_tv(&var1);
1070 		    return NULL;
1071 		}
1072 	    }
1073 	    lp->ll_list = NULL;
1074 
1075 	    // a NULL dict is equivalent with an empty dict
1076 	    if (lp->ll_tv->vval.v_dict == NULL)
1077 	    {
1078 		lp->ll_tv->vval.v_dict = dict_alloc();
1079 		if (lp->ll_tv->vval.v_dict == NULL)
1080 		{
1081 		    clear_tv(&var1);
1082 		    return NULL;
1083 		}
1084 		++lp->ll_tv->vval.v_dict->dv_refcount;
1085 	    }
1086 	    lp->ll_dict = lp->ll_tv->vval.v_dict;
1087 
1088 	    lp->ll_di = dict_find(lp->ll_dict, key, len);
1089 
1090 	    // When assigning to a scope dictionary check that a function and
1091 	    // variable name is valid (only variable name unless it is l: or
1092 	    // g: dictionary). Disallow overwriting a builtin function.
1093 	    if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1094 	    {
1095 		int prevval;
1096 		int wrong;
1097 
1098 		if (len != -1)
1099 		{
1100 		    prevval = key[len];
1101 		    key[len] = NUL;
1102 		}
1103 		else
1104 		    prevval = 0; // avoid compiler warning
1105 		wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1106 			       && rettv->v_type == VAR_FUNC
1107 			       && var_wrong_func_name(key, lp->ll_di == NULL))
1108 			|| !valid_varname(key, TRUE);
1109 		if (len != -1)
1110 		    key[len] = prevval;
1111 		if (wrong)
1112 		{
1113 		    clear_tv(&var1);
1114 		    return NULL;
1115 		}
1116 	    }
1117 
1118 	    if (lp->ll_valtype != NULL)
1119 		// use the type of the member
1120 		lp->ll_valtype = lp->ll_valtype->tt_member;
1121 
1122 	    if (lp->ll_di == NULL)
1123 	    {
1124 		// Can't add "v:" or "a:" variable.
1125 		if (lp->ll_dict == get_vimvar_dict()
1126 			 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1127 		{
1128 		    semsg(_(e_illvar), name);
1129 		    clear_tv(&var1);
1130 		    return NULL;
1131 		}
1132 
1133 		// Key does not exist in dict: may need to add it.
1134 		if (*p == '[' || *p == '.' || unlet)
1135 		{
1136 		    if (!quiet)
1137 			semsg(_(e_dictkey), key);
1138 		    clear_tv(&var1);
1139 		    return NULL;
1140 		}
1141 		if (len == -1)
1142 		    lp->ll_newkey = vim_strsave(key);
1143 		else
1144 		    lp->ll_newkey = vim_strnsave(key, len);
1145 		clear_tv(&var1);
1146 		if (lp->ll_newkey == NULL)
1147 		    p = NULL;
1148 		break;
1149 	    }
1150 	    // existing variable, need to check if it can be changed
1151 	    else if ((flags & GLV_READ_ONLY) == 0
1152 			&& (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1153 			  || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
1154 	    {
1155 		clear_tv(&var1);
1156 		return NULL;
1157 	    }
1158 
1159 	    clear_tv(&var1);
1160 	    lp->ll_tv = &lp->ll_di->di_tv;
1161 	}
1162 	else if (lp->ll_tv->v_type == VAR_BLOB)
1163 	{
1164 	    long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1165 
1166 	    /*
1167 	     * Get the number and item for the only or first index of the List.
1168 	     */
1169 	    if (empty1)
1170 		lp->ll_n1 = 0;
1171 	    else
1172 		// is number or string
1173 		lp->ll_n1 = (long)tv_get_number(&var1);
1174 	    clear_tv(&var1);
1175 
1176 	    if (lp->ll_n1 < 0
1177 		    || lp->ll_n1 > bloblen
1178 		    || (lp->ll_range && lp->ll_n1 == bloblen))
1179 	    {
1180 		if (!quiet)
1181 		    semsg(_(e_blobidx), lp->ll_n1);
1182 		clear_tv(&var2);
1183 		return NULL;
1184 	    }
1185 	    if (lp->ll_range && !lp->ll_empty2)
1186 	    {
1187 		lp->ll_n2 = (long)tv_get_number(&var2);
1188 		clear_tv(&var2);
1189 		if (lp->ll_n2 < 0
1190 			|| lp->ll_n2 >= bloblen
1191 			|| lp->ll_n2 < lp->ll_n1)
1192 		{
1193 		    if (!quiet)
1194 			semsg(_(e_blobidx), lp->ll_n2);
1195 		    return NULL;
1196 		}
1197 	    }
1198 	    lp->ll_blob = lp->ll_tv->vval.v_blob;
1199 	    lp->ll_tv = NULL;
1200 	    break;
1201 	}
1202 	else
1203 	{
1204 	    /*
1205 	     * Get the number and item for the only or first index of the List.
1206 	     */
1207 	    if (empty1)
1208 		lp->ll_n1 = 0;
1209 	    else
1210 		// is number or string
1211 		lp->ll_n1 = (long)tv_get_number(&var1);
1212 	    clear_tv(&var1);
1213 
1214 	    lp->ll_dict = NULL;
1215 	    lp->ll_list = lp->ll_tv->vval.v_list;
1216 	    lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1217 	    if (lp->ll_li == NULL)
1218 	    {
1219 		if (lp->ll_n1 < 0)
1220 		{
1221 		    lp->ll_n1 = 0;
1222 		    lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1223 		}
1224 	    }
1225 	    if (lp->ll_li == NULL)
1226 	    {
1227 		clear_tv(&var2);
1228 		if (!quiet)
1229 		    semsg(_(e_listidx), lp->ll_n1);
1230 		return NULL;
1231 	    }
1232 
1233 	    if (lp->ll_valtype != NULL)
1234 		// use the type of the member
1235 		lp->ll_valtype = lp->ll_valtype->tt_member;
1236 
1237 	    /*
1238 	     * May need to find the item or absolute index for the second
1239 	     * index of a range.
1240 	     * When no index given: "lp->ll_empty2" is TRUE.
1241 	     * Otherwise "lp->ll_n2" is set to the second index.
1242 	     */
1243 	    if (lp->ll_range && !lp->ll_empty2)
1244 	    {
1245 		lp->ll_n2 = (long)tv_get_number(&var2);
1246 						    // is number or string
1247 		clear_tv(&var2);
1248 		if (lp->ll_n2 < 0)
1249 		{
1250 		    ni = list_find(lp->ll_list, lp->ll_n2);
1251 		    if (ni == NULL)
1252 		    {
1253 			if (!quiet)
1254 			    semsg(_(e_listidx), lp->ll_n2);
1255 			return NULL;
1256 		    }
1257 		    lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
1258 		}
1259 
1260 		// Check that lp->ll_n2 isn't before lp->ll_n1.
1261 		if (lp->ll_n1 < 0)
1262 		    lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1263 		if (lp->ll_n2 < lp->ll_n1)
1264 		{
1265 		    if (!quiet)
1266 			semsg(_(e_listidx), lp->ll_n2);
1267 		    return NULL;
1268 		}
1269 	    }
1270 
1271 	    lp->ll_tv = &lp->ll_li->li_tv;
1272 	}
1273     }
1274 
1275     clear_tv(&var1);
1276     lp->ll_name_end = p;
1277     return p;
1278 }
1279 
1280 /*
1281  * Clear lval "lp" that was filled by get_lval().
1282  */
1283     void
1284 clear_lval(lval_T *lp)
1285 {
1286     vim_free(lp->ll_exp_name);
1287     vim_free(lp->ll_newkey);
1288 }
1289 
1290 /*
1291  * Set a variable that was parsed by get_lval() to "rettv".
1292  * "endp" points to just after the parsed name.
1293  * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1294  * "%" for "%=", "." for ".=" or "=" for "=".
1295  */
1296     void
1297 set_var_lval(
1298     lval_T	*lp,
1299     char_u	*endp,
1300     typval_T	*rettv,
1301     int		copy,
1302     int		flags,	    // ASSIGN_CONST, ASSIGN_NO_DECL
1303     char_u	*op,
1304     int		var_idx)    // index for "let [a, b] = list"
1305 {
1306     int		cc;
1307     listitem_T	*ri;
1308     dictitem_T	*di;
1309 
1310     if (lp->ll_tv == NULL)
1311     {
1312 	cc = *endp;
1313 	*endp = NUL;
1314 	if (lp->ll_blob != NULL)
1315 	{
1316 	    int	    error = FALSE, val;
1317 
1318 	    if (op != NULL && *op != '=')
1319 	    {
1320 		semsg(_(e_letwrong), op);
1321 		return;
1322 	    }
1323 	    if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
1324 		return;
1325 
1326 	    if (lp->ll_range && rettv->v_type == VAR_BLOB)
1327 	    {
1328 		int	il, ir;
1329 
1330 		if (lp->ll_empty2)
1331 		    lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1332 
1333 		if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
1334 		{
1335 		    emsg(_("E972: Blob value does not have the right number of bytes"));
1336 		    return;
1337 		}
1338 		if (lp->ll_empty2)
1339 		    lp->ll_n2 = blob_len(lp->ll_blob);
1340 
1341 		ir = 0;
1342 		for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1343 		    blob_set(lp->ll_blob, il,
1344 			    blob_get(rettv->vval.v_blob, ir++));
1345 	    }
1346 	    else
1347 	    {
1348 		val = (int)tv_get_number_chk(rettv, &error);
1349 		if (!error)
1350 		{
1351 		    garray_T *gap = &lp->ll_blob->bv_ga;
1352 
1353 		    // Allow for appending a byte.  Setting a byte beyond
1354 		    // the end is an error otherwise.
1355 		    if (lp->ll_n1 < gap->ga_len
1356 			    || (lp->ll_n1 == gap->ga_len
1357 				&& ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1358 		    {
1359 			blob_set(lp->ll_blob, lp->ll_n1, val);
1360 			if (lp->ll_n1 == gap->ga_len)
1361 			    ++gap->ga_len;
1362 		    }
1363 		    // error for invalid range was already given in get_lval()
1364 		}
1365 	    }
1366 	}
1367 	else if (op != NULL && *op != '=')
1368 	{
1369 	    typval_T tv;
1370 
1371 	    if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
1372 	    {
1373 		emsg(_(e_cannot_mod));
1374 		*endp = cc;
1375 		return;
1376 	    }
1377 
1378 	    // handle +=, -=, *=, /=, %= and .=
1379 	    di = NULL;
1380 	    if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
1381 					     &tv, &di, TRUE, FALSE) == OK)
1382 	    {
1383 		if ((di == NULL
1384 			 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1385 			   && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
1386 			&& tv_op(&tv, rettv, op) == OK)
1387 		    set_var(lp->ll_name, &tv, FALSE);
1388 		clear_tv(&tv);
1389 	    }
1390 	}
1391 	else
1392 	{
1393 	    if (lp->ll_type != NULL
1394 		       && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
1395 		return;
1396 	    set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1397 							       flags, var_idx);
1398 	}
1399 	*endp = cc;
1400     }
1401     else if (value_check_lock(lp->ll_newkey == NULL
1402 		? lp->ll_tv->v_lock
1403 		: lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
1404 	;
1405     else if (lp->ll_range)
1406     {
1407 	listitem_T *ll_li = lp->ll_li;
1408 	int	    ll_n1 = lp->ll_n1;
1409 
1410 	if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
1411 	{
1412 	    emsg(_("E996: Cannot lock a range"));
1413 	    return;
1414 	}
1415 
1416 	/*
1417 	 * Check whether any of the list items is locked
1418 	 */
1419 	for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
1420 	{
1421 	    if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1422 		return;
1423 	    ri = ri->li_next;
1424 	    if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1425 		break;
1426 	    ll_li = ll_li->li_next;
1427 	    ++ll_n1;
1428 	}
1429 
1430 	/*
1431 	 * Assign the List values to the list items.
1432 	 */
1433 	for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
1434 	{
1435 	    if (op != NULL && *op != '=')
1436 		tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1437 	    else
1438 	    {
1439 		clear_tv(&lp->ll_li->li_tv);
1440 		copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1441 	    }
1442 	    ri = ri->li_next;
1443 	    if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1444 		break;
1445 	    if (lp->ll_li->li_next == NULL)
1446 	    {
1447 		// Need to add an empty item.
1448 		if (list_append_number(lp->ll_list, 0) == FAIL)
1449 		{
1450 		    ri = NULL;
1451 		    break;
1452 		}
1453 	    }
1454 	    lp->ll_li = lp->ll_li->li_next;
1455 	    ++lp->ll_n1;
1456 	}
1457 	if (ri != NULL)
1458 	    emsg(_(e_list_value_has_more_items_than_targets));
1459 	else if (lp->ll_empty2
1460 		? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
1461 		: lp->ll_n1 != lp->ll_n2)
1462 	    emsg(_(e_list_value_does_not_have_enough_items));
1463     }
1464     else
1465     {
1466 	/*
1467 	 * Assign to a List or Dictionary item.
1468 	 */
1469 	if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
1470 	{
1471 	    emsg(_("E996: Cannot lock a list or dict"));
1472 	    return;
1473 	}
1474 
1475 	if (lp->ll_valtype != NULL
1476 		    && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
1477 	    return;
1478 
1479 	if (lp->ll_newkey != NULL)
1480 	{
1481 	    if (op != NULL && *op != '=')
1482 	    {
1483 		semsg(_(e_letwrong), op);
1484 		return;
1485 	    }
1486 
1487 	    // Need to add an item to the Dictionary.
1488 	    di = dictitem_alloc(lp->ll_newkey);
1489 	    if (di == NULL)
1490 		return;
1491 	    if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1492 	    {
1493 		vim_free(di);
1494 		return;
1495 	    }
1496 	    lp->ll_tv = &di->di_tv;
1497 	}
1498 	else if (op != NULL && *op != '=')
1499 	{
1500 	    tv_op(lp->ll_tv, rettv, op);
1501 	    return;
1502 	}
1503 	else
1504 	    clear_tv(lp->ll_tv);
1505 
1506 	/*
1507 	 * Assign the value to the variable or list item.
1508 	 */
1509 	if (copy)
1510 	    copy_tv(rettv, lp->ll_tv);
1511 	else
1512 	{
1513 	    *lp->ll_tv = *rettv;
1514 	    lp->ll_tv->v_lock = 0;
1515 	    init_tv(rettv);
1516 	}
1517     }
1518 }
1519 
1520 /*
1521  * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1522  * and "tv1 .= tv2"
1523  * Returns OK or FAIL.
1524  */
1525     static int
1526 tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
1527 {
1528     varnumber_T	n;
1529     char_u	numbuf[NUMBUFLEN];
1530     char_u	*s;
1531     int		failed = FALSE;
1532 
1533     // Can't do anything with a Funcref, Dict, v:true on the right.
1534     if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
1535 		      && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1536     {
1537 	switch (tv1->v_type)
1538 	{
1539 	    case VAR_UNKNOWN:
1540 	    case VAR_ANY:
1541 	    case VAR_VOID:
1542 	    case VAR_DICT:
1543 	    case VAR_FUNC:
1544 	    case VAR_PARTIAL:
1545 	    case VAR_BOOL:
1546 	    case VAR_SPECIAL:
1547 	    case VAR_JOB:
1548 	    case VAR_CHANNEL:
1549 		break;
1550 
1551 	    case VAR_BLOB:
1552 		if (*op != '+' || tv2->v_type != VAR_BLOB)
1553 		    break;
1554 		// BLOB += BLOB
1555 		if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1556 		{
1557 		    blob_T  *b1 = tv1->vval.v_blob;
1558 		    blob_T  *b2 = tv2->vval.v_blob;
1559 		    int	i, len = blob_len(b2);
1560 		    for (i = 0; i < len; i++)
1561 			ga_append(&b1->bv_ga, blob_get(b2, i));
1562 		}
1563 		return OK;
1564 
1565 	    case VAR_LIST:
1566 		if (*op != '+' || tv2->v_type != VAR_LIST)
1567 		    break;
1568 		// List += List
1569 		if (tv2->vval.v_list != NULL)
1570 		{
1571 		    if (tv1->vval.v_list == NULL)
1572 		    {
1573 			tv1->vval.v_list = tv2->vval.v_list;
1574 			++tv1->vval.v_list->lv_refcount;
1575 		    }
1576 		    else
1577 			list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1578 		}
1579 		return OK;
1580 
1581 	    case VAR_NUMBER:
1582 	    case VAR_STRING:
1583 		if (tv2->v_type == VAR_LIST)
1584 		    break;
1585 		if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
1586 		{
1587 		    // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
1588 		    n = tv_get_number(tv1);
1589 #ifdef FEAT_FLOAT
1590 		    if (tv2->v_type == VAR_FLOAT)
1591 		    {
1592 			float_T f = n;
1593 
1594 			if (*op == '%')
1595 			    break;
1596 			switch (*op)
1597 			{
1598 			    case '+': f += tv2->vval.v_float; break;
1599 			    case '-': f -= tv2->vval.v_float; break;
1600 			    case '*': f *= tv2->vval.v_float; break;
1601 			    case '/': f /= tv2->vval.v_float; break;
1602 			}
1603 			clear_tv(tv1);
1604 			tv1->v_type = VAR_FLOAT;
1605 			tv1->vval.v_float = f;
1606 		    }
1607 		    else
1608 #endif
1609 		    {
1610 			switch (*op)
1611 			{
1612 			    case '+': n += tv_get_number(tv2); break;
1613 			    case '-': n -= tv_get_number(tv2); break;
1614 			    case '*': n *= tv_get_number(tv2); break;
1615 			    case '/': n = num_divide(n, tv_get_number(tv2),
1616 							       &failed); break;
1617 			    case '%': n = num_modulus(n, tv_get_number(tv2),
1618 							       &failed); break;
1619 			}
1620 			clear_tv(tv1);
1621 			tv1->v_type = VAR_NUMBER;
1622 			tv1->vval.v_number = n;
1623 		    }
1624 		}
1625 		else
1626 		{
1627 		    if (tv2->v_type == VAR_FLOAT)
1628 			break;
1629 
1630 		    // str .= str
1631 		    s = tv_get_string(tv1);
1632 		    s = concat_str(s, tv_get_string_buf(tv2, numbuf));
1633 		    clear_tv(tv1);
1634 		    tv1->v_type = VAR_STRING;
1635 		    tv1->vval.v_string = s;
1636 		}
1637 		return failed ? FAIL : OK;
1638 
1639 	    case VAR_FLOAT:
1640 #ifdef FEAT_FLOAT
1641 		{
1642 		    float_T f;
1643 
1644 		    if (*op == '%' || *op == '.'
1645 				   || (tv2->v_type != VAR_FLOAT
1646 				    && tv2->v_type != VAR_NUMBER
1647 				    && tv2->v_type != VAR_STRING))
1648 			break;
1649 		    if (tv2->v_type == VAR_FLOAT)
1650 			f = tv2->vval.v_float;
1651 		    else
1652 			f = tv_get_number(tv2);
1653 		    switch (*op)
1654 		    {
1655 			case '+': tv1->vval.v_float += f; break;
1656 			case '-': tv1->vval.v_float -= f; break;
1657 			case '*': tv1->vval.v_float *= f; break;
1658 			case '/': tv1->vval.v_float /= f; break;
1659 		    }
1660 		}
1661 #endif
1662 		return OK;
1663 	}
1664     }
1665 
1666     semsg(_(e_letwrong), op);
1667     return FAIL;
1668 }
1669 
1670 /*
1671  * Evaluate the expression used in a ":for var in expr" command.
1672  * "arg" points to "var".
1673  * Set "*errp" to TRUE for an error, FALSE otherwise;
1674  * Return a pointer that holds the info.  Null when there is an error.
1675  */
1676     void *
1677 eval_for_line(
1678     char_u	*arg,
1679     int		*errp,
1680     exarg_T	*eap,
1681     evalarg_T	*evalarg)
1682 {
1683     forinfo_T	*fi;
1684     char_u	*expr;
1685     typval_T	tv;
1686     list_T	*l;
1687     int		skip = !(evalarg->eval_flags & EVAL_EVALUATE);
1688 
1689     *errp = TRUE;	// default: there is an error
1690 
1691     fi = ALLOC_CLEAR_ONE(forinfo_T);
1692     if (fi == NULL)
1693 	return NULL;
1694 
1695     expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
1696     if (expr == NULL)
1697 	return fi;
1698 
1699     expr = skipwhite_and_linebreak(expr, evalarg);
1700     if (expr[0] != 'i' || expr[1] != 'n'
1701 				  || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
1702     {
1703 	emsg(_(e_missing_in));
1704 	return fi;
1705     }
1706 
1707     if (skip)
1708 	++emsg_skip;
1709     expr = skipwhite_and_linebreak(expr + 2, evalarg);
1710     if (eval0(expr, &tv, eap, evalarg) == OK)
1711     {
1712 	*errp = FALSE;
1713 	if (!skip)
1714 	{
1715 	    if (tv.v_type == VAR_LIST)
1716 	    {
1717 		l = tv.vval.v_list;
1718 		if (l == NULL)
1719 		{
1720 		    // a null list is like an empty list: do nothing
1721 		    clear_tv(&tv);
1722 		}
1723 		else
1724 		{
1725 		    // Need a real list here.
1726 		    CHECK_LIST_MATERIALIZE(l);
1727 
1728 		    // No need to increment the refcount, it's already set for
1729 		    // the list being used in "tv".
1730 		    fi->fi_list = l;
1731 		    list_add_watch(l, &fi->fi_lw);
1732 		    fi->fi_lw.lw_item = l->lv_first;
1733 		}
1734 	    }
1735 	    else if (tv.v_type == VAR_BLOB)
1736 	    {
1737 		fi->fi_bi = 0;
1738 		if (tv.vval.v_blob != NULL)
1739 		{
1740 		    typval_T btv;
1741 
1742 		    // Make a copy, so that the iteration still works when the
1743 		    // blob is changed.
1744 		    blob_copy(tv.vval.v_blob, &btv);
1745 		    fi->fi_blob = btv.vval.v_blob;
1746 		}
1747 		clear_tv(&tv);
1748 	    }
1749 	    else
1750 	    {
1751 		emsg(_(e_listreq));
1752 		clear_tv(&tv);
1753 	    }
1754 	}
1755     }
1756     if (skip)
1757 	--emsg_skip;
1758     fi->fi_break_count = evalarg->eval_break_count;
1759 
1760     return fi;
1761 }
1762 
1763 /*
1764  * Used when looping over a :for line, skip the "in expr" part.
1765  */
1766     void
1767 skip_for_lines(void *fi_void, evalarg_T *evalarg)
1768 {
1769     forinfo_T	*fi = (forinfo_T *)fi_void;
1770     int		i;
1771 
1772     for (i = 0; i < fi->fi_break_count; ++i)
1773 	eval_next_line(evalarg);
1774 }
1775 
1776 /*
1777  * Use the first item in a ":for" list.  Advance to the next.
1778  * Assign the values to the variable (list).  "arg" points to the first one.
1779  * Return TRUE when a valid item was found, FALSE when at end of list or
1780  * something wrong.
1781  */
1782     int
1783 next_for_item(void *fi_void, char_u *arg)
1784 {
1785     forinfo_T	*fi = (forinfo_T *)fi_void;
1786     int		result;
1787     int		flag = in_vim9script() ? ASSIGN_DECL : 0;
1788     listitem_T	*item;
1789 
1790     if (fi->fi_blob != NULL)
1791     {
1792 	typval_T	tv;
1793 
1794 	if (fi->fi_bi >= blob_len(fi->fi_blob))
1795 	    return FALSE;
1796 	tv.v_type = VAR_NUMBER;
1797 	tv.v_lock = VAR_FIXED;
1798 	tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1799 	++fi->fi_bi;
1800 	return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
1801 				       fi->fi_varcount, flag, NULL) == OK;
1802     }
1803 
1804     item = fi->fi_lw.lw_item;
1805     if (item == NULL)
1806 	result = FALSE;
1807     else
1808     {
1809 	fi->fi_lw.lw_item = item->li_next;
1810 	result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
1811 				      fi->fi_varcount, flag, NULL) == OK);
1812     }
1813     return result;
1814 }
1815 
1816 /*
1817  * Free the structure used to store info used by ":for".
1818  */
1819     void
1820 free_for_info(void *fi_void)
1821 {
1822     forinfo_T    *fi = (forinfo_T *)fi_void;
1823 
1824     if (fi != NULL && fi->fi_list != NULL)
1825     {
1826 	list_rem_watch(fi->fi_list, &fi->fi_lw);
1827 	list_unref(fi->fi_list);
1828     }
1829     if (fi != NULL && fi->fi_blob != NULL)
1830 	blob_unref(fi->fi_blob);
1831     vim_free(fi);
1832 }
1833 
1834     void
1835 set_context_for_expression(
1836     expand_T	*xp,
1837     char_u	*arg,
1838     cmdidx_T	cmdidx)
1839 {
1840     int		has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
1841     int		c;
1842     char_u	*p;
1843 
1844     if (cmdidx == CMD_let || cmdidx == CMD_var
1845 				 || cmdidx == CMD_const || cmdidx == CMD_final)
1846     {
1847 	xp->xp_context = EXPAND_USER_VARS;
1848 	if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
1849 	{
1850 	    // ":let var1 var2 ...": find last space.
1851 	    for (p = arg + STRLEN(arg); p >= arg; )
1852 	    {
1853 		xp->xp_pattern = p;
1854 		MB_PTR_BACK(arg, p);
1855 		if (VIM_ISWHITE(*p))
1856 		    break;
1857 	    }
1858 	    return;
1859 	}
1860     }
1861     else
1862 	xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1863 							  : EXPAND_EXPRESSION;
1864     while ((xp->xp_pattern = vim_strpbrk(arg,
1865 				  (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1866     {
1867 	c = *xp->xp_pattern;
1868 	if (c == '&')
1869 	{
1870 	    c = xp->xp_pattern[1];
1871 	    if (c == '&')
1872 	    {
1873 		++xp->xp_pattern;
1874 		xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1875 	    }
1876 	    else if (c != ' ')
1877 	    {
1878 		xp->xp_context = EXPAND_SETTINGS;
1879 		if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1880 		    xp->xp_pattern += 2;
1881 
1882 	    }
1883 	}
1884 	else if (c == '$')
1885 	{
1886 	    // environment variable
1887 	    xp->xp_context = EXPAND_ENV_VARS;
1888 	}
1889 	else if (c == '=')
1890 	{
1891 	    has_expr = TRUE;
1892 	    xp->xp_context = EXPAND_EXPRESSION;
1893 	}
1894 	else if (c == '#'
1895 		&& xp->xp_context == EXPAND_EXPRESSION)
1896 	{
1897 	    // Autoload function/variable contains '#'.
1898 	    break;
1899 	}
1900 	else if ((c == '<' || c == '#')
1901 		&& xp->xp_context == EXPAND_FUNCTIONS
1902 		&& vim_strchr(xp->xp_pattern, '(') == NULL)
1903 	{
1904 	    // Function name can start with "<SNR>" and contain '#'.
1905 	    break;
1906 	}
1907 	else if (has_expr)
1908 	{
1909 	    if (c == '"')	    // string
1910 	    {
1911 		while ((c = *++xp->xp_pattern) != NUL && c != '"')
1912 		    if (c == '\\' && xp->xp_pattern[1] != NUL)
1913 			++xp->xp_pattern;
1914 		xp->xp_context = EXPAND_NOTHING;
1915 	    }
1916 	    else if (c == '\'')	    // literal string
1917 	    {
1918 		// Trick: '' is like stopping and starting a literal string.
1919 		while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1920 		    /* skip */ ;
1921 		xp->xp_context = EXPAND_NOTHING;
1922 	    }
1923 	    else if (c == '|')
1924 	    {
1925 		if (xp->xp_pattern[1] == '|')
1926 		{
1927 		    ++xp->xp_pattern;
1928 		    xp->xp_context = EXPAND_EXPRESSION;
1929 		}
1930 		else
1931 		    xp->xp_context = EXPAND_COMMANDS;
1932 	    }
1933 	    else
1934 		xp->xp_context = EXPAND_EXPRESSION;
1935 	}
1936 	else
1937 	    // Doesn't look like something valid, expand as an expression
1938 	    // anyway.
1939 	    xp->xp_context = EXPAND_EXPRESSION;
1940 	arg = xp->xp_pattern;
1941 	if (*arg != NUL)
1942 	    while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1943 		/* skip */ ;
1944     }
1945 
1946     // ":exe one two" completes "two"
1947     if ((cmdidx == CMD_execute
1948 		|| cmdidx == CMD_echo
1949 		|| cmdidx == CMD_echon
1950 		|| cmdidx == CMD_echomsg)
1951 	    && xp->xp_context == EXPAND_EXPRESSION)
1952     {
1953 	for (;;)
1954 	{
1955 	    char_u *n = skiptowhite(arg);
1956 
1957 	    if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1958 		break;
1959 	    arg = skipwhite(n);
1960 	}
1961     }
1962 
1963     xp->xp_pattern = arg;
1964 }
1965 
1966 /*
1967  * Return TRUE if "pat" matches "text".
1968  * Does not use 'cpo' and always uses 'magic'.
1969  */
1970     int
1971 pattern_match(char_u *pat, char_u *text, int ic)
1972 {
1973     int		matches = FALSE;
1974     char_u	*save_cpo;
1975     regmatch_T	regmatch;
1976 
1977     // avoid 'l' flag in 'cpoptions'
1978     save_cpo = p_cpo;
1979     p_cpo = empty_option;
1980     regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1981     if (regmatch.regprog != NULL)
1982     {
1983 	regmatch.rm_ic = ic;
1984 	matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1985 	vim_regfree(regmatch.regprog);
1986     }
1987     p_cpo = save_cpo;
1988     return matches;
1989 }
1990 
1991 /*
1992  * Handle a name followed by "(".  Both for just "name(arg)" and for
1993  * "expr->name(arg)".
1994  * Returns OK or FAIL.
1995  */
1996     static int
1997 eval_func(
1998 	char_u	    **arg,	// points to "(", will be advanced
1999 	evalarg_T   *evalarg,
2000 	char_u	    *name,
2001 	int	    name_len,
2002 	typval_T    *rettv,
2003 	int	    flags,
2004 	typval_T    *basetv)	// "expr" for "expr->name(arg)"
2005 {
2006     int		evaluate = flags & EVAL_EVALUATE;
2007     char_u	*s = name;
2008     int		len = name_len;
2009     partial_T	*partial;
2010     int		ret = OK;
2011     type_T	*type = NULL;
2012 
2013     if (!evaluate)
2014 	check_vars(s, len);
2015 
2016     // If "s" is the name of a variable of type VAR_FUNC
2017     // use its contents.
2018     s = deref_func_name(s, &len, &partial,
2019 				    in_vim9script() ? &type : NULL, !evaluate);
2020 
2021     // Need to make a copy, in case evaluating the arguments makes
2022     // the name invalid.
2023     s = vim_strsave(s);
2024     if (s == NULL || (flags & EVAL_CONSTANT))
2025 	ret = FAIL;
2026     else
2027     {
2028 	funcexe_T funcexe;
2029 
2030 	// Invoke the function.
2031 	CLEAR_FIELD(funcexe);
2032 	funcexe.firstline = curwin->w_cursor.lnum;
2033 	funcexe.lastline = curwin->w_cursor.lnum;
2034 	funcexe.evaluate = evaluate;
2035 	funcexe.partial = partial;
2036 	funcexe.basetv = basetv;
2037 	funcexe.check_type = type;
2038 	ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
2039     }
2040     vim_free(s);
2041 
2042     // If evaluate is FALSE rettv->v_type was not set in
2043     // get_func_tv, but it's needed in handle_subscript() to parse
2044     // what follows. So set it here.
2045     if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2046     {
2047 	rettv->vval.v_string = NULL;
2048 	rettv->v_type = VAR_FUNC;
2049     }
2050 
2051     // Stop the expression evaluation when immediately
2052     // aborting on error, or when an interrupt occurred or
2053     // an exception was thrown but not caught.
2054     if (evaluate && aborting())
2055     {
2056 	if (ret == OK)
2057 	    clear_tv(rettv);
2058 	ret = FAIL;
2059     }
2060     return ret;
2061 }
2062 
2063 /*
2064  * Get the next line source line without advancing.  But do skip over comment
2065  * lines.
2066  */
2067     static char_u *
2068 getline_peek_skip_comments(evalarg_T *evalarg)
2069 {
2070     for (;;)
2071     {
2072 	char_u *next = getline_peek(evalarg->eval_getline,
2073 							 evalarg->eval_cookie);
2074 	char_u *p;
2075 
2076 	if (next == NULL)
2077 	    break;
2078 	p = skipwhite(next);
2079 	if (*p != NUL && !vim9_comment_start(p))
2080 	    return next;
2081 	(void)eval_next_line(evalarg);
2082     }
2083     return NULL;
2084 }
2085 
2086 /*
2087  * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2088  * comment) and there is a next line, return the next line (skipping blanks)
2089  * and set "getnext".
2090  * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2091  * "arg" must point somewhere inside a line, not at the start.
2092  */
2093     char_u *
2094 eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2095 {
2096     char_u *p = skipwhite(arg);
2097 
2098     *getnext = FALSE;
2099     if (in_vim9script()
2100 	    && evalarg != NULL
2101 	    && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
2102 	    && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
2103     {
2104 	char_u *next;
2105 
2106 	if (evalarg->eval_cookie != NULL)
2107 	    next = getline_peek_skip_comments(evalarg);
2108 	else
2109 	    next = peek_next_line_from_context(evalarg->eval_cctx);
2110 
2111 	if (next != NULL)
2112 	{
2113 	    *getnext = TRUE;
2114 	    return skipwhite(next);
2115 	}
2116     }
2117     return p;
2118 }
2119 
2120 /*
2121  * To be called after eval_next_non_blank() sets "getnext" to TRUE.
2122  */
2123     char_u *
2124 eval_next_line(evalarg_T *evalarg)
2125 {
2126     garray_T	*gap = &evalarg->eval_ga;
2127     char_u	*line;
2128 
2129     if (evalarg->eval_cookie != NULL)
2130 	line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2131 							   GETLINE_CONCAT_ALL);
2132     else
2133 	line = next_line_from_context(evalarg->eval_cctx, TRUE);
2134     ++evalarg->eval_break_count;
2135     if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2136     {
2137 	// Going to concatenate the lines after parsing.
2138 	((char_u **)gap->ga_data)[gap->ga_len] = line;
2139 	++gap->ga_len;
2140     }
2141     else if (evalarg->eval_cookie != NULL)
2142     {
2143 	vim_free(evalarg->eval_tofree);
2144 	evalarg->eval_tofree = line;
2145     }
2146     return skipwhite(line);
2147 }
2148 
2149 /*
2150  * Call eval_next_non_blank() and get the next line if needed.
2151  */
2152     char_u *
2153 skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2154 {
2155     int	    getnext;
2156     char_u  *p = skipwhite(arg);
2157 
2158     if (evalarg == NULL)
2159 	return skipwhite(arg);
2160     eval_next_non_blank(p, evalarg, &getnext);
2161     if (getnext)
2162 	return eval_next_line(evalarg);
2163     return p;
2164 }
2165 
2166 /*
2167  * After using "evalarg" filled from "eap": free the memory.
2168  */
2169     void
2170 clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2171 {
2172     if (evalarg != NULL)
2173     {
2174 	if (evalarg->eval_tofree != NULL)
2175 	{
2176 	    if (eap != NULL)
2177 	    {
2178 		// We may need to keep the original command line, e.g. for
2179 		// ":let" it has the variable names.  But we may also need the
2180 		// new one, "nextcmd" points into it.  Keep both.
2181 		vim_free(eap->cmdline_tofree);
2182 		eap->cmdline_tofree = *eap->cmdlinep;
2183 		*eap->cmdlinep = evalarg->eval_tofree;
2184 	    }
2185 	    else
2186 		vim_free(evalarg->eval_tofree);
2187 	    evalarg->eval_tofree = NULL;
2188 	}
2189 
2190 	vim_free(evalarg->eval_tofree_lambda);
2191 	evalarg->eval_tofree_lambda = NULL;
2192     }
2193 }
2194 
2195 /*
2196  * The "evaluate" argument: When FALSE, the argument is only parsed but not
2197  * executed.  The function may return OK, but the rettv will be of type
2198  * VAR_UNKNOWN.  The function still returns FAIL for a syntax error.
2199  */
2200 
2201 /*
2202  * Handle zero level expression.
2203  * This calls eval1() and handles error message and nextcmd.
2204  * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
2205  * Note: "rettv.v_lock" is not set.
2206  * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
2207  * Return OK or FAIL.
2208  */
2209     int
2210 eval0(
2211     char_u	*arg,
2212     typval_T	*rettv,
2213     exarg_T	*eap,
2214     evalarg_T	*evalarg)
2215 {
2216     int		ret;
2217     char_u	*p;
2218     int		did_emsg_before = did_emsg;
2219     int		called_emsg_before = called_emsg;
2220     int		flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2221 
2222     p = skipwhite(arg);
2223     ret = eval1(&p, rettv, evalarg);
2224     p = skipwhite(p);
2225 
2226     if (ret == FAIL || !ends_excmd2(arg, p))
2227     {
2228 	if (ret != FAIL)
2229 	    clear_tv(rettv);
2230 	/*
2231 	 * Report the invalid expression unless the expression evaluation has
2232 	 * been cancelled due to an aborting error, an interrupt, or an
2233 	 * exception, or we already gave a more specific error.
2234 	 * Also check called_emsg for when using assert_fails().
2235 	 */
2236 	if (!aborting()
2237 		&& did_emsg == did_emsg_before
2238 		&& called_emsg == called_emsg_before
2239 		&& (flags & EVAL_CONSTANT) == 0)
2240 	    semsg(_(e_invexpr2), arg);
2241 
2242 	// Some of the expression may not have been consumed.  Do not check for
2243 	// a next command to avoid more errors, unless "|" is following, which
2244 	// could only be a command separator.
2245 	if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2246 	    eap->nextcmd = check_nextcmd(p);
2247 	return FAIL;
2248     }
2249 
2250     if (eap != NULL)
2251 	eap->nextcmd = check_nextcmd(p);
2252 
2253     return ret;
2254 }
2255 
2256 /*
2257  * Handle top level expression:
2258  *	expr2 ? expr1 : expr1
2259  *	expr2 ?? expr1
2260  *
2261  * "arg" must point to the first non-white of the expression.
2262  * "arg" is advanced to just after the recognized expression.
2263  *
2264  * Note: "rettv.v_lock" is not set.
2265  *
2266  * Return OK or FAIL.
2267  */
2268     int
2269 eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2270 {
2271     char_u  *p;
2272     int	    getnext;
2273 
2274     CLEAR_POINTER(rettv);
2275 
2276     /*
2277      * Get the first variable.
2278      */
2279     if (eval2(arg, rettv, evalarg) == FAIL)
2280 	return FAIL;
2281 
2282     p = eval_next_non_blank(*arg, evalarg, &getnext);
2283     if (*p == '?')
2284     {
2285 	int		op_falsy = p[1] == '?';
2286 	int		result;
2287 	typval_T	var2;
2288 	evalarg_T	*evalarg_used = evalarg;
2289 	evalarg_T	local_evalarg;
2290 	int		orig_flags;
2291 	int		evaluate;
2292 	int		vim9script = in_vim9script();
2293 
2294 	if (evalarg == NULL)
2295 	{
2296 	    CLEAR_FIELD(local_evalarg);
2297 	    evalarg_used = &local_evalarg;
2298 	}
2299 	orig_flags = evalarg_used->eval_flags;
2300 	evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
2301 
2302 	if (getnext)
2303 	    *arg = eval_next_line(evalarg_used);
2304 	else
2305 	{
2306 	    if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
2307 	    {
2308 		error_white_both(p, 1);
2309 		clear_tv(rettv);
2310 		return FAIL;
2311 	    }
2312 	    *arg = p;
2313 	}
2314 
2315 	result = FALSE;
2316 	if (evaluate)
2317 	{
2318 	    int		error = FALSE;
2319 
2320 	    if (op_falsy)
2321 		result = tv2bool(rettv);
2322 	    else if (vim9script)
2323 		result = tv_get_bool_chk(rettv, &error);
2324 	    else if (tv_get_number_chk(rettv, &error) != 0)
2325 		result = TRUE;
2326 	    if (error || !op_falsy || !result)
2327 		clear_tv(rettv);
2328 	    if (error)
2329 		return FAIL;
2330 	}
2331 
2332 	/*
2333 	 * Get the second variable.  Recursive!
2334 	 */
2335 	if (op_falsy)
2336 	    ++*arg;
2337 	if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
2338 	{
2339 	    error_white_both(p, 1);
2340 	    clear_tv(rettv);
2341 	    return FAIL;
2342 	}
2343 	*arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2344 	evalarg_used->eval_flags = (op_falsy ? !result : result)
2345 				    ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2346 	if (eval1(arg, &var2, evalarg_used) == FAIL)
2347 	{
2348 	    evalarg_used->eval_flags = orig_flags;
2349 	    return FAIL;
2350 	}
2351 	if (!op_falsy || !result)
2352 	    *rettv = var2;
2353 
2354 	if (!op_falsy)
2355 	{
2356 	    /*
2357 	     * Check for the ":".
2358 	     */
2359 	    p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2360 	    if (*p != ':')
2361 	    {
2362 		emsg(_(e_missing_colon));
2363 		if (evaluate && result)
2364 		    clear_tv(rettv);
2365 		evalarg_used->eval_flags = orig_flags;
2366 		return FAIL;
2367 	    }
2368 	    if (getnext)
2369 		*arg = eval_next_line(evalarg_used);
2370 	    else
2371 	    {
2372 		if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
2373 		{
2374 		    error_white_both(p, 1);
2375 		    clear_tv(rettv);
2376 		    evalarg_used->eval_flags = orig_flags;
2377 		    return FAIL;
2378 		}
2379 		*arg = p;
2380 	    }
2381 
2382 	    /*
2383 	     * Get the third variable.  Recursive!
2384 	     */
2385 	    if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
2386 	    {
2387 		error_white_both(p, 1);
2388 		clear_tv(rettv);
2389 		evalarg_used->eval_flags = orig_flags;
2390 		return FAIL;
2391 	    }
2392 	    *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2393 	    evalarg_used->eval_flags = !result ? orig_flags
2394 						 : orig_flags & ~EVAL_EVALUATE;
2395 	    if (eval1(arg, &var2, evalarg_used) == FAIL)
2396 	    {
2397 		if (evaluate && result)
2398 		    clear_tv(rettv);
2399 		evalarg_used->eval_flags = orig_flags;
2400 		return FAIL;
2401 	    }
2402 	    if (evaluate && !result)
2403 		*rettv = var2;
2404 	}
2405 
2406 	if (evalarg == NULL)
2407 	    clear_evalarg(&local_evalarg, NULL);
2408 	else
2409 	    evalarg->eval_flags = orig_flags;
2410     }
2411 
2412     return OK;
2413 }
2414 
2415 /*
2416  * Handle first level expression:
2417  *	expr2 || expr2 || expr2	    logical OR
2418  *
2419  * "arg" must point to the first non-white of the expression.
2420  * "arg" is advanced to just after the recognized expression.
2421  *
2422  * Return OK or FAIL.
2423  */
2424     static int
2425 eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2426 {
2427     char_u	*p;
2428     int		getnext;
2429 
2430     /*
2431      * Get the first variable.
2432      */
2433     if (eval3(arg, rettv, evalarg) == FAIL)
2434 	return FAIL;
2435 
2436     /*
2437      * Handle the  "||" operator.
2438      */
2439     p = eval_next_non_blank(*arg, evalarg, &getnext);
2440     if (p[0] == '|' && p[1] == '|')
2441     {
2442 	evalarg_T   *evalarg_used = evalarg;
2443 	evalarg_T   local_evalarg;
2444 	int	    evaluate;
2445 	int	    orig_flags;
2446 	long	    result = FALSE;
2447 	typval_T    var2;
2448 	int	    error = FALSE;
2449 	int	    vim9script = in_vim9script();
2450 
2451 	if (evalarg == NULL)
2452 	{
2453 	    CLEAR_FIELD(local_evalarg);
2454 	    evalarg_used = &local_evalarg;
2455 	}
2456 	orig_flags = evalarg_used->eval_flags;
2457 	evaluate = orig_flags & EVAL_EVALUATE;
2458 	if (evaluate)
2459 	{
2460 	    if (vim9script)
2461 		result = tv_get_bool_chk(rettv, &error);
2462 	    else if (tv_get_number_chk(rettv, &error) != 0)
2463 		result = TRUE;
2464 	    clear_tv(rettv);
2465 	    if (error)
2466 		return FAIL;
2467 	}
2468 
2469 	/*
2470 	 * Repeat until there is no following "||".
2471 	 */
2472 	while (p[0] == '|' && p[1] == '|')
2473 	{
2474 	    if (getnext)
2475 		*arg = eval_next_line(evalarg_used);
2476 	    else
2477 	    {
2478 		if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2479 		{
2480 		    error_white_both(p, 2);
2481 		    clear_tv(rettv);
2482 		    return FAIL;
2483 		}
2484 		*arg = p;
2485 	    }
2486 
2487 	    /*
2488 	     * Get the second variable.
2489 	     */
2490 	    if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2491 	    {
2492 		error_white_both(p, 2);
2493 		clear_tv(rettv);
2494 		return FAIL;
2495 	    }
2496 	    *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2497 	    evalarg_used->eval_flags = !result ? orig_flags
2498 						 : orig_flags & ~EVAL_EVALUATE;
2499 	    if (eval3(arg, &var2, evalarg_used) == FAIL)
2500 		return FAIL;
2501 
2502 	    /*
2503 	     * Compute the result.
2504 	     */
2505 	    if (evaluate && !result)
2506 	    {
2507 		if (vim9script)
2508 		    result = tv_get_bool_chk(&var2, &error);
2509 		else if (tv_get_number_chk(&var2, &error) != 0)
2510 		    result = TRUE;
2511 		clear_tv(&var2);
2512 		if (error)
2513 		    return FAIL;
2514 	    }
2515 	    if (evaluate)
2516 	    {
2517 		if (vim9script)
2518 		{
2519 		    rettv->v_type = VAR_BOOL;
2520 		    rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
2521 		}
2522 		else
2523 		{
2524 		    rettv->v_type = VAR_NUMBER;
2525 		    rettv->vval.v_number = result;
2526 		}
2527 	    }
2528 
2529 	    p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2530 	}
2531 
2532 	if (evalarg == NULL)
2533 	    clear_evalarg(&local_evalarg, NULL);
2534 	else
2535 	    evalarg->eval_flags = orig_flags;
2536     }
2537 
2538     return OK;
2539 }
2540 
2541 /*
2542  * Handle second level expression:
2543  *	expr3 && expr3 && expr3	    logical AND
2544  *
2545  * "arg" must point to the first non-white of the expression.
2546  * "arg" is advanced to just after the recognized expression.
2547  *
2548  * Return OK or FAIL.
2549  */
2550     static int
2551 eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2552 {
2553     char_u	*p;
2554     int		getnext;
2555 
2556     /*
2557      * Get the first variable.
2558      */
2559     if (eval4(arg, rettv, evalarg) == FAIL)
2560 	return FAIL;
2561 
2562     /*
2563      * Handle the "&&" operator.
2564      */
2565     p = eval_next_non_blank(*arg, evalarg, &getnext);
2566     if (p[0] == '&' && p[1] == '&')
2567     {
2568 	evalarg_T   *evalarg_used = evalarg;
2569 	evalarg_T   local_evalarg;
2570 	int	    orig_flags;
2571 	int	    evaluate;
2572 	long	    result = TRUE;
2573 	typval_T    var2;
2574 	int	    error = FALSE;
2575 	int	    vim9script = in_vim9script();
2576 
2577 	if (evalarg == NULL)
2578 	{
2579 	    CLEAR_FIELD(local_evalarg);
2580 	    evalarg_used = &local_evalarg;
2581 	}
2582 	orig_flags = evalarg_used->eval_flags;
2583 	evaluate = orig_flags & EVAL_EVALUATE;
2584 	if (evaluate)
2585 	{
2586 	    if (vim9script)
2587 		result = tv_get_bool_chk(rettv, &error);
2588 	    else if (tv_get_number_chk(rettv, &error) == 0)
2589 		result = FALSE;
2590 	    clear_tv(rettv);
2591 	    if (error)
2592 		return FAIL;
2593 	}
2594 
2595 	/*
2596 	 * Repeat until there is no following "&&".
2597 	 */
2598 	while (p[0] == '&' && p[1] == '&')
2599 	{
2600 	    if (getnext)
2601 		*arg = eval_next_line(evalarg_used);
2602 	    else
2603 	    {
2604 		if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
2605 		{
2606 		    error_white_both(p, 2);
2607 		    clear_tv(rettv);
2608 		    return FAIL;
2609 		}
2610 		*arg = p;
2611 	    }
2612 
2613 	    /*
2614 	     * Get the second variable.
2615 	     */
2616 	    if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2617 	    {
2618 		error_white_both(p, 2);
2619 		clear_tv(rettv);
2620 		return FAIL;
2621 	    }
2622 	    *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2623 	    evalarg_used->eval_flags = result ? orig_flags
2624 						 : orig_flags & ~EVAL_EVALUATE;
2625 	    CLEAR_FIELD(var2);
2626 	    if (eval4(arg, &var2, evalarg_used) == FAIL)
2627 		return FAIL;
2628 
2629 	    /*
2630 	     * Compute the result.
2631 	     */
2632 	    if (evaluate && result)
2633 	    {
2634 		if (vim9script)
2635 		    result = tv_get_bool_chk(&var2, &error);
2636 		else if (tv_get_number_chk(&var2, &error) == 0)
2637 		    result = FALSE;
2638 		clear_tv(&var2);
2639 		if (error)
2640 		    return FAIL;
2641 	    }
2642 	    if (evaluate)
2643 	    {
2644 		if (vim9script)
2645 		{
2646 		    rettv->v_type = VAR_BOOL;
2647 		    rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
2648 		}
2649 		else
2650 		{
2651 		    rettv->v_type = VAR_NUMBER;
2652 		    rettv->vval.v_number = result;
2653 		}
2654 	    }
2655 
2656 	    p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2657 	}
2658 
2659 	if (evalarg == NULL)
2660 	    clear_evalarg(&local_evalarg, NULL);
2661 	else
2662 	    evalarg->eval_flags = orig_flags;
2663     }
2664 
2665     return OK;
2666 }
2667 
2668 /*
2669  * Handle third level expression:
2670  *	var1 == var2
2671  *	var1 =~ var2
2672  *	var1 != var2
2673  *	var1 !~ var2
2674  *	var1 > var2
2675  *	var1 >= var2
2676  *	var1 < var2
2677  *	var1 <= var2
2678  *	var1 is var2
2679  *	var1 isnot var2
2680  *
2681  * "arg" must point to the first non-white of the expression.
2682  * "arg" is advanced to just after the recognized expression.
2683  *
2684  * Return OK or FAIL.
2685  */
2686     static int
2687 eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2688 {
2689     char_u	*p;
2690     int		getnext;
2691     exprtype_T	type = EXPR_UNKNOWN;
2692     int		len = 2;
2693     int		type_is = FALSE;
2694 
2695     /*
2696      * Get the first variable.
2697      */
2698     if (eval5(arg, rettv, evalarg) == FAIL)
2699 	return FAIL;
2700 
2701     p = eval_next_non_blank(*arg, evalarg, &getnext);
2702     type = get_compare_type(p, &len, &type_is);
2703 
2704     /*
2705      * If there is a comparative operator, use it.
2706      */
2707     if (type != EXPR_UNKNOWN)
2708     {
2709 	typval_T    var2;
2710 	int	    ic;
2711 	int	    vim9script = in_vim9script();
2712 	int	    evaluate = evalarg == NULL
2713 				   ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
2714 
2715 	if (getnext)
2716 	    *arg = eval_next_line(evalarg);
2717 	else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2718 	{
2719 	    error_white_both(p, len);
2720 	    clear_tv(rettv);
2721 	    return FAIL;
2722 	}
2723 
2724 	if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2725 	{
2726 	    semsg(_(e_invexpr2), p);
2727 	    clear_tv(rettv);
2728 	    return FAIL;
2729 	}
2730 
2731 	// extra question mark appended: ignore case
2732 	if (p[len] == '?')
2733 	{
2734 	    ic = TRUE;
2735 	    ++len;
2736 	}
2737 	// extra '#' appended: match case
2738 	else if (p[len] == '#')
2739 	{
2740 	    ic = FALSE;
2741 	    ++len;
2742 	}
2743 	// nothing appended: use 'ignorecase' if not in Vim script
2744 	else
2745 	    ic = vim9script ? FALSE : p_ic;
2746 
2747 	/*
2748 	 * Get the second variable.
2749 	 */
2750 	if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2751 	{
2752 	    error_white_both(p, 1);
2753 	    clear_tv(rettv);
2754 	    return FAIL;
2755 	}
2756 	*arg = skipwhite_and_linebreak(p + len, evalarg);
2757 	if (eval5(arg, &var2, evalarg) == FAIL)
2758 	{
2759 	    clear_tv(rettv);
2760 	    return FAIL;
2761 	}
2762 	if (evaluate)
2763 	{
2764 	    int ret;
2765 
2766 	    if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
2767 	    {
2768 		ret = FAIL;
2769 		clear_tv(rettv);
2770 	    }
2771 	    else
2772 		ret = typval_compare(rettv, &var2, type, ic);
2773 	    clear_tv(&var2);
2774 	    return ret;
2775 	}
2776     }
2777 
2778     return OK;
2779 }
2780 
2781 /*
2782  * Make a copy of blob "tv1" and append blob "tv2".
2783  */
2784     void
2785 eval_addblob(typval_T *tv1, typval_T *tv2)
2786 {
2787     blob_T  *b1 = tv1->vval.v_blob;
2788     blob_T  *b2 = tv2->vval.v_blob;
2789     blob_T  *b = blob_alloc();
2790     int	    i;
2791 
2792     if (b != NULL)
2793     {
2794 	for (i = 0; i < blob_len(b1); i++)
2795 	    ga_append(&b->bv_ga, blob_get(b1, i));
2796 	for (i = 0; i < blob_len(b2); i++)
2797 	    ga_append(&b->bv_ga, blob_get(b2, i));
2798 
2799 	clear_tv(tv1);
2800 	rettv_blob_set(tv1, b);
2801     }
2802 }
2803 
2804 /*
2805  * Make a copy of list "tv1" and append list "tv2".
2806  */
2807     int
2808 eval_addlist(typval_T *tv1, typval_T *tv2)
2809 {
2810     typval_T var3;
2811 
2812     // concatenate Lists
2813     if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2814     {
2815 	clear_tv(tv1);
2816 	clear_tv(tv2);
2817 	return FAIL;
2818     }
2819     clear_tv(tv1);
2820     *tv1 = var3;
2821     return OK;
2822 }
2823 
2824 /*
2825  * Handle fourth level expression:
2826  *	+	number addition
2827  *	-	number subtraction
2828  *	.	string concatenation (if script version is 1)
2829  *	..	string concatenation
2830  *
2831  * "arg" must point to the first non-white of the expression.
2832  * "arg" is advanced to just after the recognized expression.
2833  *
2834  * Return OK or FAIL.
2835  */
2836     static int
2837 eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
2838 {
2839     /*
2840      * Get the first variable.
2841      */
2842     if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
2843 	return FAIL;
2844 
2845     /*
2846      * Repeat computing, until no '+', '-' or '.' is following.
2847      */
2848     for (;;)
2849     {
2850 	int	    evaluate;
2851 	int	    getnext;
2852 	char_u	    *p;
2853 	int	    op;
2854 	int	    oplen;
2855 	int	    concat;
2856 	typval_T    var2;
2857 	int	    vim9script = in_vim9script();
2858 
2859 	// "." is only string concatenation when scriptversion is 1
2860 	// "+=" and "-=" are assignment
2861 	p = eval_next_non_blank(*arg, evalarg, &getnext);
2862 	op = *p;
2863 	concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
2864 	if ((op != '+' && op != '-' && !concat) || p[1] == '=')
2865 	    break;
2866 
2867 	evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
2868 	oplen = (concat && p[1] == '.') ? 2 : 1;
2869 	if (getnext)
2870 	    *arg = eval_next_line(evalarg);
2871 	else
2872 	{
2873 	    if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2874 	    {
2875 		error_white_both(p, oplen);
2876 		clear_tv(rettv);
2877 		return FAIL;
2878 	    }
2879 	    *arg = p;
2880 	}
2881 	if ((op != '+' || (rettv->v_type != VAR_LIST
2882 						 && rettv->v_type != VAR_BLOB))
2883 #ifdef FEAT_FLOAT
2884 		&& (op == '.' || rettv->v_type != VAR_FLOAT)
2885 #endif
2886 		&& evaluate)
2887 	{
2888 	    int		error = FALSE;
2889 
2890 	    // For "list + ...", an illegal use of the first operand as
2891 	    // a number cannot be determined before evaluating the 2nd
2892 	    // operand: if this is also a list, all is ok.
2893 	    // For "something . ...", "something - ..." or "non-list + ...",
2894 	    // we know that the first operand needs to be a string or number
2895 	    // without evaluating the 2nd operand.  So check before to avoid
2896 	    // side effects after an error.
2897 	    if (op != '.')
2898 		tv_get_number_chk(rettv, &error);
2899 	    if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
2900 	    {
2901 		clear_tv(rettv);
2902 		return FAIL;
2903 	    }
2904 	}
2905 
2906 	/*
2907 	 * Get the second variable.
2908 	 */
2909 	if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
2910 	{
2911 	    error_white_both(p, oplen);
2912 	    clear_tv(rettv);
2913 	    return FAIL;
2914 	}
2915 	*arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
2916 	if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
2917 	{
2918 	    clear_tv(rettv);
2919 	    return FAIL;
2920 	}
2921 
2922 	if (evaluate)
2923 	{
2924 	    /*
2925 	     * Compute the result.
2926 	     */
2927 	    if (op == '.')
2928 	    {
2929 		char_u	buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2930 		char_u	*s1 = tv_get_string_buf(rettv, buf1);
2931 		char_u	*s2 = NULL;
2932 
2933 		if (vim9script && (var2.v_type == VAR_VOID
2934 			|| var2.v_type == VAR_CHANNEL
2935 			|| var2.v_type == VAR_JOB))
2936 		    emsg(_(e_inval_string));
2937 #ifdef FEAT_FLOAT
2938 		else if (vim9script && var2.v_type == VAR_FLOAT)
2939 		{
2940 		    vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2941 							    var2.vval.v_float);
2942 		    s2 = buf2;
2943 		}
2944 #endif
2945 		else
2946 		    s2 = tv_get_string_buf_chk(&var2, buf2);
2947 		if (s2 == NULL)		// type error ?
2948 		{
2949 		    clear_tv(rettv);
2950 		    clear_tv(&var2);
2951 		    return FAIL;
2952 		}
2953 		p = concat_str(s1, s2);
2954 		clear_tv(rettv);
2955 		rettv->v_type = VAR_STRING;
2956 		rettv->vval.v_string = p;
2957 	    }
2958 	    else if (op == '+' && rettv->v_type == VAR_BLOB
2959 						   && var2.v_type == VAR_BLOB)
2960 		eval_addblob(rettv, &var2);
2961 	    else if (op == '+' && rettv->v_type == VAR_LIST
2962 						   && var2.v_type == VAR_LIST)
2963 	    {
2964 		if (eval_addlist(rettv, &var2) == FAIL)
2965 		    return FAIL;
2966 	    }
2967 	    else
2968 	    {
2969 		int		error = FALSE;
2970 		varnumber_T	n1, n2;
2971 #ifdef FEAT_FLOAT
2972 		float_T	    f1 = 0, f2 = 0;
2973 
2974 		if (rettv->v_type == VAR_FLOAT)
2975 		{
2976 		    f1 = rettv->vval.v_float;
2977 		    n1 = 0;
2978 		}
2979 		else
2980 #endif
2981 		{
2982 		    n1 = tv_get_number_chk(rettv, &error);
2983 		    if (error)
2984 		    {
2985 			// This can only happen for "list + non-list".  For
2986 			// "non-list + ..." or "something - ...", we returned
2987 			// before evaluating the 2nd operand.
2988 			clear_tv(rettv);
2989 			return FAIL;
2990 		    }
2991 #ifdef FEAT_FLOAT
2992 		    if (var2.v_type == VAR_FLOAT)
2993 			f1 = n1;
2994 #endif
2995 		}
2996 #ifdef FEAT_FLOAT
2997 		if (var2.v_type == VAR_FLOAT)
2998 		{
2999 		    f2 = var2.vval.v_float;
3000 		    n2 = 0;
3001 		}
3002 		else
3003 #endif
3004 		{
3005 		    n2 = tv_get_number_chk(&var2, &error);
3006 		    if (error)
3007 		    {
3008 			clear_tv(rettv);
3009 			clear_tv(&var2);
3010 			return FAIL;
3011 		    }
3012 #ifdef FEAT_FLOAT
3013 		    if (rettv->v_type == VAR_FLOAT)
3014 			f2 = n2;
3015 #endif
3016 		}
3017 		clear_tv(rettv);
3018 
3019 #ifdef FEAT_FLOAT
3020 		// If there is a float on either side the result is a float.
3021 		if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3022 		{
3023 		    if (op == '+')
3024 			f1 = f1 + f2;
3025 		    else
3026 			f1 = f1 - f2;
3027 		    rettv->v_type = VAR_FLOAT;
3028 		    rettv->vval.v_float = f1;
3029 		}
3030 		else
3031 #endif
3032 		{
3033 		    if (op == '+')
3034 			n1 = n1 + n2;
3035 		    else
3036 			n1 = n1 - n2;
3037 		    rettv->v_type = VAR_NUMBER;
3038 		    rettv->vval.v_number = n1;
3039 		}
3040 	    }
3041 	    clear_tv(&var2);
3042 	}
3043     }
3044     return OK;
3045 }
3046 
3047 /*
3048  * Handle fifth level expression:
3049  *	*	number multiplication
3050  *	/	number division
3051  *	%	number modulo
3052  *
3053  * "arg" must point to the first non-white of the expression.
3054  * "arg" is advanced to just after the recognized expression.
3055  *
3056  * Return OK or FAIL.
3057  */
3058     static int
3059 eval6(
3060     char_u	**arg,
3061     typval_T	*rettv,
3062     evalarg_T	*evalarg,
3063     int		want_string)  // after "." operator
3064 {
3065 #ifdef FEAT_FLOAT
3066     int	    use_float = FALSE;
3067 #endif
3068 
3069     /*
3070      * Get the first variable.
3071      */
3072     if (eval7(arg, rettv, evalarg, want_string) == FAIL)
3073 	return FAIL;
3074 
3075     /*
3076      * Repeat computing, until no '*', '/' or '%' is following.
3077      */
3078     for (;;)
3079     {
3080 	int	    evaluate;
3081 	int	    getnext;
3082 	typval_T    var2;
3083 	char_u	    *p;
3084 	int	    op;
3085 	varnumber_T n1, n2;
3086 #ifdef FEAT_FLOAT
3087 	float_T	    f1, f2;
3088 #endif
3089 	int	    error;
3090 
3091 	p = eval_next_non_blank(*arg, evalarg, &getnext);
3092 	op = *p;
3093 	if (op != '*' && op != '/' && op != '%')
3094 	    break;
3095 
3096 	evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3097 	if (getnext)
3098 	    *arg = eval_next_line(evalarg);
3099 	else
3100 	{
3101 	    if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3102 	    {
3103 		error_white_both(p, 1);
3104 		clear_tv(rettv);
3105 		return FAIL;
3106 	    }
3107 	    *arg = p;
3108 	}
3109 
3110 #ifdef FEAT_FLOAT
3111 	f1 = 0;
3112 	f2 = 0;
3113 #endif
3114 	error = FALSE;
3115 	if (evaluate)
3116 	{
3117 #ifdef FEAT_FLOAT
3118 	    if (rettv->v_type == VAR_FLOAT)
3119 	    {
3120 		f1 = rettv->vval.v_float;
3121 		use_float = TRUE;
3122 		n1 = 0;
3123 	    }
3124 	    else
3125 #endif
3126 		n1 = tv_get_number_chk(rettv, &error);
3127 	    clear_tv(rettv);
3128 	    if (error)
3129 		return FAIL;
3130 	}
3131 	else
3132 	    n1 = 0;
3133 
3134 	/*
3135 	 * Get the second variable.
3136 	 */
3137 	if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3138 	{
3139 	    error_white_both(p, 1);
3140 	    clear_tv(rettv);
3141 	    return FAIL;
3142 	}
3143 	*arg = skipwhite_and_linebreak(*arg + 1, evalarg);
3144 	if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
3145 	    return FAIL;
3146 
3147 	if (evaluate)
3148 	{
3149 #ifdef FEAT_FLOAT
3150 	    if (var2.v_type == VAR_FLOAT)
3151 	    {
3152 		if (!use_float)
3153 		{
3154 		    f1 = n1;
3155 		    use_float = TRUE;
3156 		}
3157 		f2 = var2.vval.v_float;
3158 		n2 = 0;
3159 	    }
3160 	    else
3161 #endif
3162 	    {
3163 		n2 = tv_get_number_chk(&var2, &error);
3164 		clear_tv(&var2);
3165 		if (error)
3166 		    return FAIL;
3167 #ifdef FEAT_FLOAT
3168 		if (use_float)
3169 		    f2 = n2;
3170 #endif
3171 	    }
3172 
3173 	    /*
3174 	     * Compute the result.
3175 	     * When either side is a float the result is a float.
3176 	     */
3177 #ifdef FEAT_FLOAT
3178 	    if (use_float)
3179 	    {
3180 		if (op == '*')
3181 		    f1 = f1 * f2;
3182 		else if (op == '/')
3183 		{
3184 # ifdef VMS
3185 		    // VMS crashes on divide by zero, work around it
3186 		    if (f2 == 0.0)
3187 		    {
3188 			if (f1 == 0)
3189 			    f1 = -1 * __F_FLT_MAX - 1L;   // similar to NaN
3190 			else if (f1 < 0)
3191 			    f1 = -1 * __F_FLT_MAX;
3192 			else
3193 			    f1 = __F_FLT_MAX;
3194 		    }
3195 		    else
3196 			f1 = f1 / f2;
3197 # else
3198 		    // We rely on the floating point library to handle divide
3199 		    // by zero to result in "inf" and not a crash.
3200 		    f1 = f1 / f2;
3201 # endif
3202 		}
3203 		else
3204 		{
3205 		    emsg(_(e_modulus));
3206 		    return FAIL;
3207 		}
3208 		rettv->v_type = VAR_FLOAT;
3209 		rettv->vval.v_float = f1;
3210 	    }
3211 	    else
3212 #endif
3213 	    {
3214 		int	    failed = FALSE;
3215 
3216 		if (op == '*')
3217 		    n1 = n1 * n2;
3218 		else if (op == '/')
3219 		    n1 = num_divide(n1, n2, &failed);
3220 		else
3221 		    n1 = num_modulus(n1, n2, &failed);
3222 		if (failed)
3223 		    return FAIL;
3224 
3225 		rettv->v_type = VAR_NUMBER;
3226 		rettv->vval.v_number = n1;
3227 	    }
3228 	}
3229     }
3230 
3231     return OK;
3232 }
3233 
3234     int
3235 eval_leader(char_u **arg, int vim9)
3236 {
3237     char_u	*s = *arg;
3238     char_u	*p = *arg;
3239 
3240     while (*p == '!' || *p == '-' || *p == '+')
3241     {
3242 	char_u *n = skipwhite(p + 1);
3243 
3244 	// ++, --, -+ and +- are not accepted in Vim9 script
3245 	if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3246 	{
3247 	    semsg(_(e_invexpr2), s);
3248 	    return FAIL;
3249 	}
3250 	p = n;
3251     }
3252     *arg = p;
3253     return OK;
3254 }
3255 
3256 /*
3257  * Handle sixth level expression:
3258  *  number		number constant
3259  *  0zFFFFFFFF		Blob constant
3260  *  "string"		string constant
3261  *  'string'		literal string constant
3262  *  &option-name	option value
3263  *  @r			register contents
3264  *  identifier		variable value
3265  *  function()		function call
3266  *  $VAR		environment variable
3267  *  (expression)	nested expression
3268  *  [expr, expr]	List
3269  *  {arg, arg -> expr}	Lambda
3270  *  {key: val, key: val}   Dictionary
3271  *  #{key: val, key: val}  Dictionary with literal keys
3272  *
3273  *  Also handle:
3274  *  ! in front		logical NOT
3275  *  - in front		unary minus
3276  *  + in front		unary plus (ignored)
3277  *  trailing []		subscript in String or List
3278  *  trailing .name	entry in Dictionary
3279  *  trailing ->name()	method call
3280  *
3281  * "arg" must point to the first non-white of the expression.
3282  * "arg" is advanced to just after the recognized expression.
3283  *
3284  * Return OK or FAIL.
3285  */
3286     static int
3287 eval7(
3288     char_u	**arg,
3289     typval_T	*rettv,
3290     evalarg_T	*evalarg,
3291     int		want_string)	// after "." operator
3292 {
3293     int		evaluate = evalarg != NULL
3294 				      && (evalarg->eval_flags & EVAL_EVALUATE);
3295     int		len;
3296     char_u	*s;
3297     char_u	*start_leader, *end_leader;
3298     int		ret = OK;
3299     char_u	*alias;
3300 
3301     /*
3302      * Initialise variable so that clear_tv() can't mistake this for a
3303      * string and free a string that isn't there.
3304      */
3305     rettv->v_type = VAR_UNKNOWN;
3306 
3307     /*
3308      * Skip '!', '-' and '+' characters.  They are handled later.
3309      */
3310     start_leader = *arg;
3311     if (eval_leader(arg, in_vim9script()) == FAIL)
3312 	return FAIL;
3313     end_leader = *arg;
3314 
3315     if (**arg == '.' && (!isdigit(*(*arg + 1))
3316 #ifdef FEAT_FLOAT
3317 	    || current_sctx.sc_version < 2
3318 #endif
3319 	    ))
3320     {
3321 	semsg(_(e_invexpr2), *arg);
3322 	++*arg;
3323 	return FAIL;
3324     }
3325 
3326     switch (**arg)
3327     {
3328     /*
3329      * Number constant.
3330      */
3331     case '0':
3332     case '1':
3333     case '2':
3334     case '3':
3335     case '4':
3336     case '5':
3337     case '6':
3338     case '7':
3339     case '8':
3340     case '9':
3341     case '.':	ret = eval_number(arg, rettv, evaluate, want_string);
3342 
3343 		// Apply prefixed "-" and "+" now.  Matters especially when
3344 		// "->" follows.
3345 		if (ret == OK && evaluate && end_leader > start_leader
3346 						  && rettv->v_type != VAR_BLOB)
3347 		    ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
3348 		break;
3349 
3350     /*
3351      * String constant: "string".
3352      */
3353     case '"':	ret = eval_string(arg, rettv, evaluate);
3354 		break;
3355 
3356     /*
3357      * Literal string constant: 'str''ing'.
3358      */
3359     case '\'':	ret = eval_lit_string(arg, rettv, evaluate);
3360 		break;
3361 
3362     /*
3363      * List: [expr, expr]
3364      */
3365     case '[':	ret = eval_list(arg, rettv, evalarg, TRUE);
3366 		break;
3367 
3368     /*
3369      * Dictionary: #{key: val, key: val}
3370      */
3371     case '#':	if (!in_vim9script() && (*arg)[1] == '{')
3372 		{
3373 		    ++*arg;
3374 		    ret = eval_dict(arg, rettv, evalarg, TRUE);
3375 		}
3376 		else
3377 		    ret = NOTDONE;
3378 		break;
3379 
3380     /*
3381      * Lambda: {arg, arg -> expr}
3382      * Dictionary: {'key': val, 'key': val}
3383      */
3384     case '{':	if (in_vim9script())
3385 		    ret = NOTDONE;
3386 		else
3387 		    ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
3388 		if (ret == NOTDONE)
3389 		    ret = eval_dict(arg, rettv, evalarg, FALSE);
3390 		break;
3391 
3392     /*
3393      * Option value: &name
3394      */
3395     case '&':	ret = eval_option(arg, rettv, evaluate);
3396 		break;
3397 
3398     /*
3399      * Environment variable: $VAR.
3400      */
3401     case '$':	ret = eval_env_var(arg, rettv, evaluate);
3402 		break;
3403 
3404     /*
3405      * Register contents: @r.
3406      */
3407     case '@':	++*arg;
3408 		if (evaluate)
3409 		{
3410 		    rettv->v_type = VAR_STRING;
3411 		    rettv->vval.v_string = get_reg_contents(**arg,
3412 							    GREG_EXPR_SRC);
3413 		}
3414 		if (**arg != NUL)
3415 		    ++*arg;
3416 		break;
3417 
3418     /*
3419      * nested expression: (expression).
3420      * lambda: (arg) => expr
3421      */
3422     case '(':	ret = NOTDONE;
3423 		if (in_vim9script())
3424 		    ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3425 		if (ret == NOTDONE)
3426 		{
3427 		    *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
3428 		    ret = eval1(arg, rettv, evalarg);	// recursive!
3429 
3430 		    *arg = skipwhite_and_linebreak(*arg, evalarg);
3431 		    if (**arg == ')')
3432 			++*arg;
3433 		    else if (ret == OK)
3434 		    {
3435 			emsg(_(e_missing_close));
3436 			clear_tv(rettv);
3437 			ret = FAIL;
3438 		    }
3439 		}
3440 		break;
3441 
3442     default:	ret = NOTDONE;
3443 		break;
3444     }
3445 
3446     if (ret == NOTDONE)
3447     {
3448 	/*
3449 	 * Must be a variable or function name.
3450 	 * Can also be a curly-braces kind of name: {expr}.
3451 	 */
3452 	s = *arg;
3453 	len = get_name_len(arg, &alias, evaluate, TRUE);
3454 	if (alias != NULL)
3455 	    s = alias;
3456 
3457 	if (len <= 0)
3458 	    ret = FAIL;
3459 	else
3460 	{
3461 	    int	    flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3462 
3463 	    if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3464 	    {
3465 		// "name(..."  recursive!
3466 		*arg = skipwhite(*arg);
3467 		ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
3468 	    }
3469 	    else if (flags & EVAL_CONSTANT)
3470 		ret = FAIL;
3471 	    else if (evaluate)
3472 	    {
3473 		// get the value of "true", "false" or a variable
3474 		if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3475 		{
3476 		    rettv->v_type = VAR_BOOL;
3477 		    rettv->vval.v_number = VVAL_TRUE;
3478 		    ret = OK;
3479 		}
3480 		else if (len == 5 && in_vim9script()
3481 						&& STRNCMP(s, "false", 5) == 0)
3482 		{
3483 		    rettv->v_type = VAR_BOOL;
3484 		    rettv->vval.v_number = VVAL_FALSE;
3485 		    ret = OK;
3486 		}
3487 		else if (len == 4 && in_vim9script()
3488 						&& STRNCMP(s, "null", 4) == 0)
3489 		{
3490 		    rettv->v_type = VAR_SPECIAL;
3491 		    rettv->vval.v_number = VVAL_NULL;
3492 		    ret = OK;
3493 		}
3494 		else
3495 		    ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3496 	    }
3497 	    else
3498 	    {
3499 		// skip the name
3500 		check_vars(s, len);
3501 		ret = OK;
3502 	    }
3503 	}
3504 	vim_free(alias);
3505     }
3506 
3507     // Handle following '[', '(' and '.' for expr[expr], expr.name,
3508     // expr(expr), expr->name(expr)
3509     if (ret == OK)
3510 	ret = handle_subscript(arg, rettv, evalarg, TRUE);
3511 
3512     /*
3513      * Apply logical NOT and unary '-', from right to left, ignore '+'.
3514      */
3515     if (ret == OK && evaluate && end_leader > start_leader)
3516 	ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
3517     return ret;
3518 }
3519 
3520 /*
3521  * Apply the leading "!" and "-" before an eval7 expression to "rettv".
3522  * When "numeric_only" is TRUE only handle "+" and "-".
3523  * Adjusts "end_leaderp" until it is at "start_leader".
3524  */
3525     static int
3526 eval7_leader(
3527 	typval_T    *rettv,
3528 	int	    numeric_only,
3529 	char_u	    *start_leader,
3530 	char_u	    **end_leaderp)
3531 {
3532     char_u	*end_leader = *end_leaderp;
3533     int		ret = OK;
3534     int		error = FALSE;
3535     varnumber_T val = 0;
3536     vartype_T	type = rettv->v_type;
3537 #ifdef FEAT_FLOAT
3538     float_T	    f = 0.0;
3539 
3540     if (rettv->v_type == VAR_FLOAT)
3541 	f = rettv->vval.v_float;
3542     else
3543 #endif
3544     {
3545 	while (VIM_ISWHITE(end_leader[-1]))
3546 	    --end_leader;
3547 	if (in_vim9script() && end_leader[-1] == '!')
3548 	    val = tv2bool(rettv);
3549 	else
3550 	    val = tv_get_number_chk(rettv, &error);
3551     }
3552     if (error)
3553     {
3554 	clear_tv(rettv);
3555 	ret = FAIL;
3556     }
3557     else
3558     {
3559 	while (end_leader > start_leader)
3560 	{
3561 	    --end_leader;
3562 	    if (*end_leader == '!')
3563 	    {
3564 		if (numeric_only)
3565 		{
3566 		    ++end_leader;
3567 		    break;
3568 		}
3569 #ifdef FEAT_FLOAT
3570 		if (rettv->v_type == VAR_FLOAT)
3571 		{
3572 		    if (in_vim9script())
3573 		    {
3574 			rettv->v_type = VAR_BOOL;
3575 			val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3576 		    }
3577 		    else
3578 			f = !f;
3579 		}
3580 		else
3581 #endif
3582 		{
3583 		    val = !val;
3584 		    type = VAR_BOOL;
3585 		}
3586 	    }
3587 	    else if (*end_leader == '-')
3588 	    {
3589 #ifdef FEAT_FLOAT
3590 		if (rettv->v_type == VAR_FLOAT)
3591 		    f = -f;
3592 		else
3593 #endif
3594 		{
3595 		    val = -val;
3596 		    type = VAR_NUMBER;
3597 		}
3598 	    }
3599 	}
3600 #ifdef FEAT_FLOAT
3601 	if (rettv->v_type == VAR_FLOAT)
3602 	{
3603 	    clear_tv(rettv);
3604 	    rettv->vval.v_float = f;
3605 	}
3606 	else
3607 #endif
3608 	{
3609 	    clear_tv(rettv);
3610 	    if (in_vim9script())
3611 		rettv->v_type = type;
3612 	    else
3613 		rettv->v_type = VAR_NUMBER;
3614 	    rettv->vval.v_number = val;
3615 	}
3616     }
3617     *end_leaderp = end_leader;
3618     return ret;
3619 }
3620 
3621 /*
3622  * Call the function referred to in "rettv".
3623  */
3624     static int
3625 call_func_rettv(
3626 	char_u	    **arg,
3627 	evalarg_T   *evalarg,
3628 	typval_T    *rettv,
3629 	int	    evaluate,
3630 	dict_T	    *selfdict,
3631 	typval_T    *basetv)
3632 {
3633     partial_T	*pt = NULL;
3634     funcexe_T	funcexe;
3635     typval_T	functv;
3636     char_u	*s;
3637     int		ret;
3638 
3639     // need to copy the funcref so that we can clear rettv
3640     if (evaluate)
3641     {
3642 	functv = *rettv;
3643 	rettv->v_type = VAR_UNKNOWN;
3644 
3645 	// Invoke the function.  Recursive!
3646 	if (functv.v_type == VAR_PARTIAL)
3647 	{
3648 	    pt = functv.vval.v_partial;
3649 	    s = partial_name(pt);
3650 	}
3651 	else
3652 	    s = functv.vval.v_string;
3653     }
3654     else
3655 	s = (char_u *)"";
3656 
3657     CLEAR_FIELD(funcexe);
3658     funcexe.firstline = curwin->w_cursor.lnum;
3659     funcexe.lastline = curwin->w_cursor.lnum;
3660     funcexe.evaluate = evaluate;
3661     funcexe.partial = pt;
3662     funcexe.selfdict = selfdict;
3663     funcexe.basetv = basetv;
3664     ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
3665 
3666     // Clear the funcref afterwards, so that deleting it while
3667     // evaluating the arguments is possible (see test55).
3668     if (evaluate)
3669 	clear_tv(&functv);
3670 
3671     return ret;
3672 }
3673 
3674 /*
3675  * Evaluate "->method()".
3676  * "*arg" points to "method".
3677  * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3678  */
3679     static int
3680 eval_lambda(
3681     char_u	**arg,
3682     typval_T	*rettv,
3683     evalarg_T	*evalarg,
3684     int		verbose)	// give error messages
3685 {
3686     int		evaluate = evalarg != NULL
3687 				      && (evalarg->eval_flags & EVAL_EVALUATE);
3688     typval_T	base = *rettv;
3689     int		ret;
3690 
3691     rettv->v_type = VAR_UNKNOWN;
3692 
3693     if (**arg == '{')
3694     {
3695 	// ->{lambda}()
3696 	ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3697     }
3698     else
3699     {
3700 	// ->(lambda)()
3701 	++*arg;
3702 	ret = eval1(arg, rettv, evalarg);
3703 	*arg = skipwhite_and_linebreak(*arg, evalarg);
3704 	if (**arg != ')')
3705 	{
3706 	    emsg(_(e_missing_close));
3707 	    ret = FAIL;
3708 	}
3709 	++*arg;
3710     }
3711     if (ret != OK)
3712 	return FAIL;
3713     else if (**arg != '(')
3714     {
3715 	if (verbose)
3716 	{
3717 	    if (*skipwhite(*arg) == '(')
3718 		emsg(_(e_nowhitespace));
3719 	    else
3720 		semsg(_(e_missing_paren), "lambda");
3721 	}
3722 	clear_tv(rettv);
3723 	ret = FAIL;
3724     }
3725     else
3726 	ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
3727 
3728     // Clear the funcref afterwards, so that deleting it while
3729     // evaluating the arguments is possible (see test55).
3730     if (evaluate)
3731 	clear_tv(&base);
3732 
3733     return ret;
3734 }
3735 
3736 /*
3737  * Evaluate "->method()".
3738  * "*arg" points to "method".
3739  * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3740  */
3741     static int
3742 eval_method(
3743     char_u	**arg,
3744     typval_T	*rettv,
3745     evalarg_T	*evalarg,
3746     int		verbose)	// give error messages
3747 {
3748     char_u	*name;
3749     long	len;
3750     char_u	*alias;
3751     typval_T	base = *rettv;
3752     int		ret;
3753     int		evaluate = evalarg != NULL
3754 				      && (evalarg->eval_flags & EVAL_EVALUATE);
3755 
3756     rettv->v_type = VAR_UNKNOWN;
3757 
3758     name = *arg;
3759     len = get_name_len(arg, &alias, evaluate, TRUE);
3760     if (alias != NULL)
3761 	name = alias;
3762 
3763     if (len <= 0)
3764     {
3765 	if (verbose)
3766 	    emsg(_("E260: Missing name after ->"));
3767 	ret = FAIL;
3768     }
3769     else
3770     {
3771 	*arg = skipwhite(*arg);
3772 	if (**arg != '(')
3773 	{
3774 	    if (verbose)
3775 		semsg(_(e_missing_paren), name);
3776 	    ret = FAIL;
3777 	}
3778 	else if (VIM_ISWHITE((*arg)[-1]))
3779 	{
3780 	    if (verbose)
3781 		emsg(_(e_nowhitespace));
3782 	    ret = FAIL;
3783 	}
3784 	else
3785 	    ret = eval_func(arg, evalarg, name, len, rettv,
3786 					  evaluate ? EVAL_EVALUATE : 0, &base);
3787     }
3788 
3789     // Clear the funcref afterwards, so that deleting it while
3790     // evaluating the arguments is possible (see test55).
3791     if (evaluate)
3792 	clear_tv(&base);
3793 
3794     return ret;
3795 }
3796 
3797 /*
3798  * Evaluate an "[expr]" or "[expr:expr]" index.  Also "dict.key".
3799  * "*arg" points to the '[' or '.'.
3800  * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3801  */
3802     static int
3803 eval_index(
3804     char_u	**arg,
3805     typval_T	*rettv,
3806     evalarg_T	*evalarg,
3807     int		verbose)	// give error messages
3808 {
3809     int		evaluate = evalarg != NULL
3810 				      && (evalarg->eval_flags & EVAL_EVALUATE);
3811     int		empty1 = FALSE, empty2 = FALSE;
3812     typval_T	var1, var2;
3813     int		range = FALSE;
3814     char_u	*key = NULL;
3815     int		keylen = -1;
3816     int		vim9 = in_vim9script();
3817 
3818     if (check_can_index(rettv, evaluate, verbose) == FAIL)
3819 	return FAIL;
3820 
3821     init_tv(&var1);
3822     init_tv(&var2);
3823     if (**arg == '.')
3824     {
3825 	/*
3826 	 * dict.name
3827 	 */
3828 	key = *arg + 1;
3829 	for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
3830 	    ;
3831 	if (keylen == 0)
3832 	    return FAIL;
3833 	*arg = key + keylen;
3834     }
3835     else
3836     {
3837 	/*
3838 	 * something[idx]
3839 	 *
3840 	 * Get the (first) variable from inside the [].
3841 	 */
3842 	*arg = skipwhite_and_linebreak(*arg + 1, evalarg);
3843 	if (**arg == ':')
3844 	    empty1 = TRUE;
3845 	else if (eval1(arg, &var1, evalarg) == FAIL)	// recursive!
3846 	    return FAIL;
3847 	else if (vim9 && **arg == ':')
3848 	{
3849 	    semsg(_(e_white_space_required_before_and_after_str_at_str),
3850 								    ":", *arg);
3851 	    clear_tv(&var1);
3852 	    return FAIL;
3853 	}
3854 	else if (evaluate)
3855 	{
3856 #ifdef FEAT_FLOAT
3857 	    // allow for indexing with float
3858 	    if (vim9 && rettv->v_type == VAR_DICT
3859 						   && var1.v_type == VAR_FLOAT)
3860 	    {
3861 		var1.vval.v_string = typval_tostring(&var1, TRUE);
3862 		var1.v_type = VAR_STRING;
3863 	    }
3864 #endif
3865 	    if (tv_get_string_chk(&var1) == NULL)
3866 	    {
3867 		// not a number or string
3868 		clear_tv(&var1);
3869 		return FAIL;
3870 	    }
3871 	}
3872 
3873 	/*
3874 	 * Get the second variable from inside the [:].
3875 	 */
3876 	*arg = skipwhite_and_linebreak(*arg, evalarg);
3877 	if (**arg == ':')
3878 	{
3879 	    range = TRUE;
3880 	    ++*arg;
3881 	    if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
3882 	    {
3883 		semsg(_(e_white_space_required_before_and_after_str_at_str),
3884 								":", *arg - 1);
3885 		if (!empty1)
3886 		    clear_tv(&var1);
3887 		return FAIL;
3888 	    }
3889 	    *arg = skipwhite_and_linebreak(*arg, evalarg);
3890 	    if (**arg == ']')
3891 		empty2 = TRUE;
3892 	    else if (eval1(arg, &var2, evalarg) == FAIL)	// recursive!
3893 	    {
3894 		if (!empty1)
3895 		    clear_tv(&var1);
3896 		return FAIL;
3897 	    }
3898 	    else if (evaluate && tv_get_string_chk(&var2) == NULL)
3899 	    {
3900 		// not a number or string
3901 		if (!empty1)
3902 		    clear_tv(&var1);
3903 		clear_tv(&var2);
3904 		return FAIL;
3905 	    }
3906 	}
3907 
3908 	// Check for the ']'.
3909 	*arg = skipwhite_and_linebreak(*arg, evalarg);
3910 	if (**arg != ']')
3911 	{
3912 	    if (verbose)
3913 		emsg(_(e_missbrac));
3914 	    clear_tv(&var1);
3915 	    if (range)
3916 		clear_tv(&var2);
3917 	    return FAIL;
3918 	}
3919 	*arg = *arg + 1;	// skip over the ']'
3920     }
3921 
3922     if (evaluate)
3923     {
3924 	int res = eval_index_inner(rettv, range,
3925 		empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
3926 		key, keylen, verbose);
3927 
3928 	if (!empty1)
3929 	    clear_tv(&var1);
3930 	if (range)
3931 	    clear_tv(&var2);
3932 	return res;
3933     }
3934     return OK;
3935 }
3936 
3937 /*
3938  * Check if "rettv" can have an [index] or [sli:ce]
3939  */
3940     int
3941 check_can_index(typval_T *rettv, int evaluate, int verbose)
3942 {
3943     switch (rettv->v_type)
3944     {
3945 	case VAR_FUNC:
3946 	case VAR_PARTIAL:
3947 	    if (verbose)
3948 		emsg(_("E695: Cannot index a Funcref"));
3949 	    return FAIL;
3950 	case VAR_FLOAT:
3951 #ifdef FEAT_FLOAT
3952 	    if (verbose)
3953 		emsg(_(e_float_as_string));
3954 	    return FAIL;
3955 #endif
3956 	case VAR_BOOL:
3957 	case VAR_SPECIAL:
3958 	case VAR_JOB:
3959 	case VAR_CHANNEL:
3960 	    if (verbose)
3961 		emsg(_(e_cannot_index_special_variable));
3962 	    return FAIL;
3963 	case VAR_UNKNOWN:
3964 	case VAR_ANY:
3965 	case VAR_VOID:
3966 	    if (evaluate)
3967 	    {
3968 		emsg(_(e_cannot_index_special_variable));
3969 		return FAIL;
3970 	    }
3971 	    // FALLTHROUGH
3972 
3973 	case VAR_STRING:
3974 	case VAR_LIST:
3975 	case VAR_DICT:
3976 	case VAR_BLOB:
3977 	    break;
3978 	case VAR_NUMBER:
3979 	    if (in_vim9script())
3980 		emsg(_(e_cannot_index_number));
3981 	    break;
3982     }
3983     return OK;
3984 }
3985 
3986 /*
3987  * slice() function
3988  */
3989     void
3990 f_slice(typval_T *argvars, typval_T *rettv)
3991 {
3992     if (check_can_index(argvars, TRUE, FALSE) == OK)
3993     {
3994 	copy_tv(argvars, rettv);
3995 	eval_index_inner(rettv, TRUE, argvars + 1,
3996 		argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
3997 		TRUE, NULL, 0, FALSE);
3998     }
3999 }
4000 
4001 /*
4002  * Apply index or range to "rettv".
4003  * "var1" is the first index, NULL for [:expr].
4004  * "var2" is the second index, NULL for [expr] and [expr: ]
4005  * "exclusive" is TRUE for slice(): second index is exclusive, use character
4006  * index for string.
4007  * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4008  */
4009     int
4010 eval_index_inner(
4011 	typval_T    *rettv,
4012 	int	    is_range,
4013 	typval_T    *var1,
4014 	typval_T    *var2,
4015 	int	    exclusive,
4016 	char_u	    *key,
4017 	int	    keylen,
4018 	int	    verbose)
4019 {
4020     varnumber_T	    n1, n2 = 0;
4021     long	    len;
4022 
4023     n1 = 0;
4024     if (var1 != NULL && rettv->v_type != VAR_DICT)
4025 	n1 = tv_get_number(var1);
4026 
4027     if (is_range)
4028     {
4029 	if (rettv->v_type == VAR_DICT)
4030 	{
4031 	    if (verbose)
4032 		emsg(_(e_cannot_slice_dictionary));
4033 	    return FAIL;
4034 	}
4035 	if (var2 != NULL)
4036 	    n2 = tv_get_number(var2);
4037 	else
4038 	    n2 = VARNUM_MAX;
4039     }
4040 
4041     switch (rettv->v_type)
4042     {
4043 	case VAR_UNKNOWN:
4044 	case VAR_ANY:
4045 	case VAR_VOID:
4046 	case VAR_FUNC:
4047 	case VAR_PARTIAL:
4048 	case VAR_FLOAT:
4049 	case VAR_BOOL:
4050 	case VAR_SPECIAL:
4051 	case VAR_JOB:
4052 	case VAR_CHANNEL:
4053 	    break; // not evaluating, skipping over subscript
4054 
4055 	case VAR_NUMBER:
4056 	case VAR_STRING:
4057 	    {
4058 		char_u	*s = tv_get_string(rettv);
4059 
4060 		len = (long)STRLEN(s);
4061 		if (in_vim9script() || exclusive)
4062 		{
4063 		    if (is_range)
4064 			s = string_slice(s, n1, n2, exclusive);
4065 		    else
4066 			s = char_from_string(s, n1);
4067 		}
4068 		else if (is_range)
4069 		{
4070 		    // The resulting variable is a substring.  If the indexes
4071 		    // are out of range the result is empty.
4072 		    if (n1 < 0)
4073 		    {
4074 			n1 = len + n1;
4075 			if (n1 < 0)
4076 			    n1 = 0;
4077 		    }
4078 		    if (n2 < 0)
4079 			n2 = len + n2;
4080 		    else if (n2 >= len)
4081 			n2 = len;
4082 		    if (n1 >= len || n2 < 0 || n1 > n2)
4083 			s = NULL;
4084 		    else
4085 			s = vim_strnsave(s + n1, n2 - n1 + 1);
4086 		}
4087 		else
4088 		{
4089 		    // The resulting variable is a string of a single
4090 		    // character.  If the index is too big or negative the
4091 		    // result is empty.
4092 		    if (n1 >= len || n1 < 0)
4093 			s = NULL;
4094 		    else
4095 			s = vim_strnsave(s + n1, 1);
4096 		}
4097 		clear_tv(rettv);
4098 		rettv->v_type = VAR_STRING;
4099 		rettv->vval.v_string = s;
4100 	    }
4101 	    break;
4102 
4103 	case VAR_BLOB:
4104 	    len = blob_len(rettv->vval.v_blob);
4105 	    if (is_range)
4106 	    {
4107 		// The resulting variable is a sub-blob.  If the indexes
4108 		// are out of range the result is empty.
4109 		if (n1 < 0)
4110 		{
4111 		    n1 = len + n1;
4112 		    if (n1 < 0)
4113 			n1 = 0;
4114 		}
4115 		if (n2 < 0)
4116 		    n2 = len + n2;
4117 		else if (n2 >= len)
4118 		    n2 = len - (exclusive ? 0 : 1);
4119 		if (exclusive)
4120 		    --n2;
4121 		if (n1 >= len || n2 < 0 || n1 > n2)
4122 		{
4123 		    clear_tv(rettv);
4124 		    rettv->v_type = VAR_BLOB;
4125 		    rettv->vval.v_blob = NULL;
4126 		}
4127 		else
4128 		{
4129 		    blob_T  *blob = blob_alloc();
4130 		    long    i;
4131 
4132 		    if (blob != NULL)
4133 		    {
4134 			if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4135 			{
4136 			    blob_free(blob);
4137 			    return FAIL;
4138 			}
4139 			blob->bv_ga.ga_len = n2 - n1 + 1;
4140 			for (i = n1; i <= n2; i++)
4141 			    blob_set(blob, i - n1,
4142 					  blob_get(rettv->vval.v_blob, i));
4143 
4144 			clear_tv(rettv);
4145 			rettv_blob_set(rettv, blob);
4146 		    }
4147 		}
4148 	    }
4149 	    else
4150 	    {
4151 		// The resulting variable is a byte value.
4152 		// If the index is too big or negative that is an error.
4153 		if (n1 < 0)
4154 		    n1 = len + n1;
4155 		if (n1 < len && n1 >= 0)
4156 		{
4157 		    int v = blob_get(rettv->vval.v_blob, n1);
4158 
4159 		    clear_tv(rettv);
4160 		    rettv->v_type = VAR_NUMBER;
4161 		    rettv->vval.v_number = v;
4162 		}
4163 		else
4164 		    semsg(_(e_blobidx), n1);
4165 	    }
4166 	    break;
4167 
4168 	case VAR_LIST:
4169 	    if (var1 == NULL)
4170 		n1 = 0;
4171 	    if (var2 == NULL)
4172 		n2 = VARNUM_MAX;
4173 	    if (list_slice_or_index(rettv->vval.v_list,
4174 			  is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
4175 		return FAIL;
4176 	    break;
4177 
4178 	case VAR_DICT:
4179 	    {
4180 		dictitem_T	*item;
4181 		typval_T	tmp;
4182 
4183 		if (key == NULL)
4184 		{
4185 		    key = tv_get_string_chk(var1);
4186 		    if (key == NULL)
4187 			return FAIL;
4188 		}
4189 
4190 		item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4191 
4192 		if (item == NULL && verbose)
4193 		    semsg(_(e_dictkey), key);
4194 		if (item == NULL)
4195 		    return FAIL;
4196 
4197 		copy_tv(&item->di_tv, &tmp);
4198 		clear_tv(rettv);
4199 		*rettv = tmp;
4200 	    }
4201 	    break;
4202     }
4203     return OK;
4204 }
4205 
4206 /*
4207  * Return the function name of partial "pt".
4208  */
4209     char_u *
4210 partial_name(partial_T *pt)
4211 {
4212     if (pt->pt_name != NULL)
4213 	return pt->pt_name;
4214     if (pt->pt_func != NULL)
4215 	return pt->pt_func->uf_name;
4216     return (char_u *)"";
4217 }
4218 
4219     static void
4220 partial_free(partial_T *pt)
4221 {
4222     int i;
4223 
4224     for (i = 0; i < pt->pt_argc; ++i)
4225 	clear_tv(&pt->pt_argv[i]);
4226     vim_free(pt->pt_argv);
4227     dict_unref(pt->pt_dict);
4228     if (pt->pt_name != NULL)
4229     {
4230 	func_unref(pt->pt_name);
4231 	vim_free(pt->pt_name);
4232     }
4233     else
4234 	func_ptr_unref(pt->pt_func);
4235 
4236     // Decrease the reference count for the context of a closure.  If down
4237     // to the minimum it may be time to free it.
4238     if (pt->pt_funcstack != NULL)
4239     {
4240 	--pt->pt_funcstack->fs_refcount;
4241 	funcstack_check_refcount(pt->pt_funcstack);
4242     }
4243 
4244     vim_free(pt);
4245 }
4246 
4247 /*
4248  * Unreference a closure: decrement the reference count and free it when it
4249  * becomes zero.
4250  */
4251     void
4252 partial_unref(partial_T *pt)
4253 {
4254     if (pt != NULL)
4255     {
4256 	if (--pt->pt_refcount <= 0)
4257 	    partial_free(pt);
4258 
4259 	// If the reference count goes down to one, the funcstack may be the
4260 	// only reference and can be freed if no other partials reference it.
4261 	else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4262 	    funcstack_check_refcount(pt->pt_funcstack);
4263     }
4264 }
4265 
4266 /*
4267  * Return the next (unique) copy ID.
4268  * Used for serializing nested structures.
4269  */
4270     int
4271 get_copyID(void)
4272 {
4273     current_copyID += COPYID_INC;
4274     return current_copyID;
4275 }
4276 
4277 /*
4278  * Garbage collection for lists and dictionaries.
4279  *
4280  * We use reference counts to be able to free most items right away when they
4281  * are no longer used.  But for composite items it's possible that it becomes
4282  * unused while the reference count is > 0: When there is a recursive
4283  * reference.  Example:
4284  *	:let l = [1, 2, 3]
4285  *	:let d = {9: l}
4286  *	:let l[1] = d
4287  *
4288  * Since this is quite unusual we handle this with garbage collection: every
4289  * once in a while find out which lists and dicts are not referenced from any
4290  * variable.
4291  *
4292  * Here is a good reference text about garbage collection (refers to Python
4293  * but it applies to all reference-counting mechanisms):
4294  *	http://python.ca/nas/python/gc/
4295  */
4296 
4297 /*
4298  * Do garbage collection for lists and dicts.
4299  * When "testing" is TRUE this is called from test_garbagecollect_now().
4300  * Return TRUE if some memory was freed.
4301  */
4302     int
4303 garbage_collect(int testing)
4304 {
4305     int		copyID;
4306     int		abort = FALSE;
4307     buf_T	*buf;
4308     win_T	*wp;
4309     int		did_free = FALSE;
4310     tabpage_T	*tp;
4311 
4312     if (!testing)
4313     {
4314 	// Only do this once.
4315 	want_garbage_collect = FALSE;
4316 	may_garbage_collect = FALSE;
4317 	garbage_collect_at_exit = FALSE;
4318     }
4319 
4320     // The execution stack can grow big, limit the size.
4321     if (exestack.ga_maxlen - exestack.ga_len > 500)
4322     {
4323 	size_t	new_len;
4324 	char_u	*pp;
4325 	int	n;
4326 
4327 	// Keep 150% of the current size, with a minimum of the growth size.
4328 	n = exestack.ga_len / 2;
4329 	if (n < exestack.ga_growsize)
4330 	    n = exestack.ga_growsize;
4331 
4332 	// Don't make it bigger though.
4333 	if (exestack.ga_len + n < exestack.ga_maxlen)
4334 	{
4335 	    new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4336 	    pp = vim_realloc(exestack.ga_data, new_len);
4337 	    if (pp == NULL)
4338 		return FAIL;
4339 	    exestack.ga_maxlen = exestack.ga_len + n;
4340 	    exestack.ga_data = pp;
4341 	}
4342     }
4343 
4344     // We advance by two because we add one for items referenced through
4345     // previous_funccal.
4346     copyID = get_copyID();
4347 
4348     /*
4349      * 1. Go through all accessible variables and mark all lists and dicts
4350      *    with copyID.
4351      */
4352 
4353     // Don't free variables in the previous_funccal list unless they are only
4354     // referenced through previous_funccal.  This must be first, because if
4355     // the item is referenced elsewhere the funccal must not be freed.
4356     abort = abort || set_ref_in_previous_funccal(copyID);
4357 
4358     // script-local variables
4359     abort = abort || garbage_collect_scriptvars(copyID);
4360 
4361     // buffer-local variables
4362     FOR_ALL_BUFFERS(buf)
4363 	abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4364 								  NULL, NULL);
4365 
4366     // window-local variables
4367     FOR_ALL_TAB_WINDOWS(tp, wp)
4368 	abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4369 								  NULL, NULL);
4370     if (aucmd_win != NULL)
4371 	abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4372 								  NULL, NULL);
4373 #ifdef FEAT_PROP_POPUP
4374     FOR_ALL_POPUPWINS(wp)
4375 	abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4376 								  NULL, NULL);
4377     FOR_ALL_TABPAGES(tp)
4378 	FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
4379 		abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4380 								  NULL, NULL);
4381 #endif
4382 
4383     // tabpage-local variables
4384     FOR_ALL_TABPAGES(tp)
4385 	abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4386 								  NULL, NULL);
4387     // global variables
4388     abort = abort || garbage_collect_globvars(copyID);
4389 
4390     // function-local variables
4391     abort = abort || set_ref_in_call_stack(copyID);
4392 
4393     // named functions (matters for closures)
4394     abort = abort || set_ref_in_functions(copyID);
4395 
4396     // function call arguments, if v:testing is set.
4397     abort = abort || set_ref_in_func_args(copyID);
4398 
4399     // v: vars
4400     abort = abort || garbage_collect_vimvars(copyID);
4401 
4402     // callbacks in buffers
4403     abort = abort || set_ref_in_buffers(copyID);
4404 
4405 #ifdef FEAT_LUA
4406     abort = abort || set_ref_in_lua(copyID);
4407 #endif
4408 
4409 #ifdef FEAT_PYTHON
4410     abort = abort || set_ref_in_python(copyID);
4411 #endif
4412 
4413 #ifdef FEAT_PYTHON3
4414     abort = abort || set_ref_in_python3(copyID);
4415 #endif
4416 
4417 #ifdef FEAT_JOB_CHANNEL
4418     abort = abort || set_ref_in_channel(copyID);
4419     abort = abort || set_ref_in_job(copyID);
4420 #endif
4421 #ifdef FEAT_NETBEANS_INTG
4422     abort = abort || set_ref_in_nb_channel(copyID);
4423 #endif
4424 
4425 #ifdef FEAT_TIMERS
4426     abort = abort || set_ref_in_timer(copyID);
4427 #endif
4428 
4429 #ifdef FEAT_QUICKFIX
4430     abort = abort || set_ref_in_quickfix(copyID);
4431 #endif
4432 
4433 #ifdef FEAT_TERMINAL
4434     abort = abort || set_ref_in_term(copyID);
4435 #endif
4436 
4437 #ifdef FEAT_PROP_POPUP
4438     abort = abort || set_ref_in_popups(copyID);
4439 #endif
4440 
4441     if (!abort)
4442     {
4443 	/*
4444 	 * 2. Free lists and dictionaries that are not referenced.
4445 	 */
4446 	did_free = free_unref_items(copyID);
4447 
4448 	/*
4449 	 * 3. Check if any funccal can be freed now.
4450 	 *    This may call us back recursively.
4451 	 */
4452 	free_unref_funccal(copyID, testing);
4453     }
4454     else if (p_verbose > 0)
4455     {
4456 	verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
4457     }
4458 
4459     return did_free;
4460 }
4461 
4462 /*
4463  * Free lists, dictionaries, channels and jobs that are no longer referenced.
4464  */
4465     static int
4466 free_unref_items(int copyID)
4467 {
4468     int		did_free = FALSE;
4469 
4470     // Let all "free" functions know that we are here.  This means no
4471     // dictionaries, lists, channels or jobs are to be freed, because we will
4472     // do that here.
4473     in_free_unref_items = TRUE;
4474 
4475     /*
4476      * PASS 1: free the contents of the items.  We don't free the items
4477      * themselves yet, so that it is possible to decrement refcount counters
4478      */
4479 
4480     // Go through the list of dicts and free items without the copyID.
4481     did_free |= dict_free_nonref(copyID);
4482 
4483     // Go through the list of lists and free items without the copyID.
4484     did_free |= list_free_nonref(copyID);
4485 
4486 #ifdef FEAT_JOB_CHANNEL
4487     // Go through the list of jobs and free items without the copyID. This
4488     // must happen before doing channels, because jobs refer to channels, but
4489     // the reference from the channel to the job isn't tracked.
4490     did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4491 
4492     // Go through the list of channels and free items without the copyID.
4493     did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4494 #endif
4495 
4496     /*
4497      * PASS 2: free the items themselves.
4498      */
4499     dict_free_items(copyID);
4500     list_free_items(copyID);
4501 
4502 #ifdef FEAT_JOB_CHANNEL
4503     // Go through the list of jobs and free items without the copyID. This
4504     // must happen before doing channels, because jobs refer to channels, but
4505     // the reference from the channel to the job isn't tracked.
4506     free_unused_jobs(copyID, COPYID_MASK);
4507 
4508     // Go through the list of channels and free items without the copyID.
4509     free_unused_channels(copyID, COPYID_MASK);
4510 #endif
4511 
4512     in_free_unref_items = FALSE;
4513 
4514     return did_free;
4515 }
4516 
4517 /*
4518  * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
4519  * "list_stack" is used to add lists to be marked.  Can be NULL.
4520  *
4521  * Returns TRUE if setting references failed somehow.
4522  */
4523     int
4524 set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
4525 {
4526     int		todo;
4527     int		abort = FALSE;
4528     hashitem_T	*hi;
4529     hashtab_T	*cur_ht;
4530     ht_stack_T	*ht_stack = NULL;
4531     ht_stack_T	*tempitem;
4532 
4533     cur_ht = ht;
4534     for (;;)
4535     {
4536 	if (!abort)
4537 	{
4538 	    // Mark each item in the hashtab.  If the item contains a hashtab
4539 	    // it is added to ht_stack, if it contains a list it is added to
4540 	    // list_stack.
4541 	    todo = (int)cur_ht->ht_used;
4542 	    for (hi = cur_ht->ht_array; todo > 0; ++hi)
4543 		if (!HASHITEM_EMPTY(hi))
4544 		{
4545 		    --todo;
4546 		    abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4547 						       &ht_stack, list_stack);
4548 		}
4549 	}
4550 
4551 	if (ht_stack == NULL)
4552 	    break;
4553 
4554 	// take an item from the stack
4555 	cur_ht = ht_stack->ht;
4556 	tempitem = ht_stack;
4557 	ht_stack = ht_stack->prev;
4558 	free(tempitem);
4559     }
4560 
4561     return abort;
4562 }
4563 
4564 /*
4565  * Mark a dict and its items with "copyID".
4566  * Returns TRUE if setting references failed somehow.
4567  */
4568     int
4569 set_ref_in_dict(dict_T *d, int copyID)
4570 {
4571     if (d != NULL && d->dv_copyID != copyID)
4572     {
4573 	d->dv_copyID = copyID;
4574 	return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4575     }
4576     return FALSE;
4577 }
4578 
4579 /*
4580  * Mark a list and its items with "copyID".
4581  * Returns TRUE if setting references failed somehow.
4582  */
4583     int
4584 set_ref_in_list(list_T *ll, int copyID)
4585 {
4586     if (ll != NULL && ll->lv_copyID != copyID)
4587     {
4588 	ll->lv_copyID = copyID;
4589 	return set_ref_in_list_items(ll, copyID, NULL);
4590     }
4591     return FALSE;
4592 }
4593 
4594 /*
4595  * Mark all lists and dicts referenced through list "l" with "copyID".
4596  * "ht_stack" is used to add hashtabs to be marked.  Can be NULL.
4597  *
4598  * Returns TRUE if setting references failed somehow.
4599  */
4600     int
4601 set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
4602 {
4603     listitem_T	 *li;
4604     int		 abort = FALSE;
4605     list_T	 *cur_l;
4606     list_stack_T *list_stack = NULL;
4607     list_stack_T *tempitem;
4608 
4609     cur_l = l;
4610     for (;;)
4611     {
4612 	if (!abort && cur_l->lv_first != &range_list_item)
4613 	    // Mark each item in the list.  If the item contains a hashtab
4614 	    // it is added to ht_stack, if it contains a list it is added to
4615 	    // list_stack.
4616 	    for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4617 		abort = abort || set_ref_in_item(&li->li_tv, copyID,
4618 						       ht_stack, &list_stack);
4619 	if (list_stack == NULL)
4620 	    break;
4621 
4622 	// take an item from the stack
4623 	cur_l = list_stack->list;
4624 	tempitem = list_stack;
4625 	list_stack = list_stack->prev;
4626 	free(tempitem);
4627     }
4628 
4629     return abort;
4630 }
4631 
4632 /*
4633  * Mark all lists and dicts referenced through typval "tv" with "copyID".
4634  * "list_stack" is used to add lists to be marked.  Can be NULL.
4635  * "ht_stack" is used to add hashtabs to be marked.  Can be NULL.
4636  *
4637  * Returns TRUE if setting references failed somehow.
4638  */
4639     int
4640 set_ref_in_item(
4641     typval_T	    *tv,
4642     int		    copyID,
4643     ht_stack_T	    **ht_stack,
4644     list_stack_T    **list_stack)
4645 {
4646     int		abort = FALSE;
4647 
4648     if (tv->v_type == VAR_DICT)
4649     {
4650 	dict_T	*dd = tv->vval.v_dict;
4651 
4652 	if (dd != NULL && dd->dv_copyID != copyID)
4653 	{
4654 	    // Didn't see this dict yet.
4655 	    dd->dv_copyID = copyID;
4656 	    if (ht_stack == NULL)
4657 	    {
4658 		abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4659 	    }
4660 	    else
4661 	    {
4662 		ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4663 
4664 		if (newitem == NULL)
4665 		    abort = TRUE;
4666 		else
4667 		{
4668 		    newitem->ht = &dd->dv_hashtab;
4669 		    newitem->prev = *ht_stack;
4670 		    *ht_stack = newitem;
4671 		}
4672 	    }
4673 	}
4674     }
4675     else if (tv->v_type == VAR_LIST)
4676     {
4677 	list_T	*ll = tv->vval.v_list;
4678 
4679 	if (ll != NULL && ll->lv_copyID != copyID)
4680 	{
4681 	    // Didn't see this list yet.
4682 	    ll->lv_copyID = copyID;
4683 	    if (list_stack == NULL)
4684 	    {
4685 		abort = set_ref_in_list_items(ll, copyID, ht_stack);
4686 	    }
4687 	    else
4688 	    {
4689 		list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4690 
4691 		if (newitem == NULL)
4692 		    abort = TRUE;
4693 		else
4694 		{
4695 		    newitem->list = ll;
4696 		    newitem->prev = *list_stack;
4697 		    *list_stack = newitem;
4698 		}
4699 	    }
4700 	}
4701     }
4702     else if (tv->v_type == VAR_FUNC)
4703     {
4704 	abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
4705     }
4706     else if (tv->v_type == VAR_PARTIAL)
4707     {
4708 	partial_T	*pt = tv->vval.v_partial;
4709 	int		i;
4710 
4711 	if (pt != NULL && pt->pt_copyID != copyID)
4712 	{
4713 	    // Didn't see this partial yet.
4714 	    pt->pt_copyID = copyID;
4715 
4716 	    abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
4717 
4718 	    if (pt->pt_dict != NULL)
4719 	    {
4720 		typval_T dtv;
4721 
4722 		dtv.v_type = VAR_DICT;
4723 		dtv.vval.v_dict = pt->pt_dict;
4724 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4725 	    }
4726 
4727 	    for (i = 0; i < pt->pt_argc; ++i)
4728 		abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4729 							ht_stack, list_stack);
4730 	    if (pt->pt_funcstack != NULL)
4731 	    {
4732 		typval_T    *stack = pt->pt_funcstack->fs_ga.ga_data;
4733 
4734 		for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4735 		    abort = abort || set_ref_in_item(stack + i, copyID,
4736 							 ht_stack, list_stack);
4737 	    }
4738 
4739 	}
4740     }
4741 #ifdef FEAT_JOB_CHANNEL
4742     else if (tv->v_type == VAR_JOB)
4743     {
4744 	job_T	    *job = tv->vval.v_job;
4745 	typval_T    dtv;
4746 
4747 	if (job != NULL && job->jv_copyID != copyID)
4748 	{
4749 	    job->jv_copyID = copyID;
4750 	    if (job->jv_channel != NULL)
4751 	    {
4752 		dtv.v_type = VAR_CHANNEL;
4753 		dtv.vval.v_channel = job->jv_channel;
4754 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4755 	    }
4756 	    if (job->jv_exit_cb.cb_partial != NULL)
4757 	    {
4758 		dtv.v_type = VAR_PARTIAL;
4759 		dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
4760 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4761 	    }
4762 	}
4763     }
4764     else if (tv->v_type == VAR_CHANNEL)
4765     {
4766 	channel_T   *ch =tv->vval.v_channel;
4767 	ch_part_T   part;
4768 	typval_T    dtv;
4769 	jsonq_T	    *jq;
4770 	cbq_T	    *cq;
4771 
4772 	if (ch != NULL && ch->ch_copyID != copyID)
4773 	{
4774 	    ch->ch_copyID = copyID;
4775 	    for (part = PART_SOCK; part < PART_COUNT; ++part)
4776 	    {
4777 		for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4778 							     jq = jq->jq_next)
4779 		    set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4780 		for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4781 							     cq = cq->cq_next)
4782 		    if (cq->cq_callback.cb_partial != NULL)
4783 		    {
4784 			dtv.v_type = VAR_PARTIAL;
4785 			dtv.vval.v_partial = cq->cq_callback.cb_partial;
4786 			set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4787 		    }
4788 		if (ch->ch_part[part].ch_callback.cb_partial != NULL)
4789 		{
4790 		    dtv.v_type = VAR_PARTIAL;
4791 		    dtv.vval.v_partial =
4792 				      ch->ch_part[part].ch_callback.cb_partial;
4793 		    set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4794 		}
4795 	    }
4796 	    if (ch->ch_callback.cb_partial != NULL)
4797 	    {
4798 		dtv.v_type = VAR_PARTIAL;
4799 		dtv.vval.v_partial = ch->ch_callback.cb_partial;
4800 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4801 	    }
4802 	    if (ch->ch_close_cb.cb_partial != NULL)
4803 	    {
4804 		dtv.v_type = VAR_PARTIAL;
4805 		dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
4806 		set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4807 	    }
4808 	}
4809     }
4810 #endif
4811     return abort;
4812 }
4813 
4814 /*
4815  * Return a string with the string representation of a variable.
4816  * If the memory is allocated "tofree" is set to it, otherwise NULL.
4817  * "numbuf" is used for a number.
4818  * When "copyID" is not NULL replace recursive lists and dicts with "...".
4819  * When both "echo_style" and "composite_val" are FALSE, put quotes around
4820  * strings as "string()", otherwise does not put quotes around strings, as
4821  * ":echo" displays values.
4822  * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4823  * are replaced with "...".
4824  * May return NULL.
4825  */
4826     char_u *
4827 echo_string_core(
4828     typval_T	*tv,
4829     char_u	**tofree,
4830     char_u	*numbuf,
4831     int		copyID,
4832     int		echo_style,
4833     int		restore_copyID,
4834     int		composite_val)
4835 {
4836     static int	recurse = 0;
4837     char_u	*r = NULL;
4838 
4839     if (recurse >= DICT_MAXNEST)
4840     {
4841 	if (!did_echo_string_emsg)
4842 	{
4843 	    // Only give this message once for a recursive call to avoid
4844 	    // flooding the user with errors.  And stop iterating over lists
4845 	    // and dicts.
4846 	    did_echo_string_emsg = TRUE;
4847 	    emsg(_("E724: variable nested too deep for displaying"));
4848 	}
4849 	*tofree = NULL;
4850 	return (char_u *)"{E724}";
4851     }
4852     ++recurse;
4853 
4854     switch (tv->v_type)
4855     {
4856 	case VAR_STRING:
4857 	    if (echo_style && !composite_val)
4858 	    {
4859 		*tofree = NULL;
4860 		r = tv->vval.v_string;
4861 		if (r == NULL)
4862 		    r = (char_u *)"";
4863 	    }
4864 	    else
4865 	    {
4866 		*tofree = string_quote(tv->vval.v_string, FALSE);
4867 		r = *tofree;
4868 	    }
4869 	    break;
4870 
4871 	case VAR_FUNC:
4872 	    if (echo_style)
4873 	    {
4874 		*tofree = NULL;
4875 		r = tv->vval.v_string;
4876 	    }
4877 	    else
4878 	    {
4879 		*tofree = string_quote(tv->vval.v_string, TRUE);
4880 		r = *tofree;
4881 	    }
4882 	    break;
4883 
4884 	case VAR_PARTIAL:
4885 	    {
4886 		partial_T   *pt = tv->vval.v_partial;
4887 		char_u	    *fname = string_quote(pt == NULL ? NULL
4888 						    : partial_name(pt), FALSE);
4889 		garray_T    ga;
4890 		int	    i;
4891 		char_u	    *tf;
4892 
4893 		ga_init2(&ga, 1, 100);
4894 		ga_concat(&ga, (char_u *)"function(");
4895 		if (fname != NULL)
4896 		{
4897 		    ga_concat(&ga, fname);
4898 		    vim_free(fname);
4899 		}
4900 		if (pt != NULL && pt->pt_argc > 0)
4901 		{
4902 		    ga_concat(&ga, (char_u *)", [");
4903 		    for (i = 0; i < pt->pt_argc; ++i)
4904 		    {
4905 			if (i > 0)
4906 			    ga_concat(&ga, (char_u *)", ");
4907 			ga_concat(&ga,
4908 			     tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4909 			vim_free(tf);
4910 		    }
4911 		    ga_concat(&ga, (char_u *)"]");
4912 		}
4913 		if (pt != NULL && pt->pt_dict != NULL)
4914 		{
4915 		    typval_T dtv;
4916 
4917 		    ga_concat(&ga, (char_u *)", ");
4918 		    dtv.v_type = VAR_DICT;
4919 		    dtv.vval.v_dict = pt->pt_dict;
4920 		    ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4921 		    vim_free(tf);
4922 		}
4923 		ga_concat(&ga, (char_u *)")");
4924 
4925 		*tofree = ga.ga_data;
4926 		r = *tofree;
4927 		break;
4928 	    }
4929 
4930 	case VAR_BLOB:
4931 	    r = blob2string(tv->vval.v_blob, tofree, numbuf);
4932 	    break;
4933 
4934 	case VAR_LIST:
4935 	    if (tv->vval.v_list == NULL)
4936 	    {
4937 		// NULL list is equivalent to empty list.
4938 		*tofree = NULL;
4939 		r = (char_u *)"[]";
4940 	    }
4941 	    else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4942 		    && tv->vval.v_list->lv_len > 0)
4943 	    {
4944 		*tofree = NULL;
4945 		r = (char_u *)"[...]";
4946 	    }
4947 	    else
4948 	    {
4949 		int old_copyID = tv->vval.v_list->lv_copyID;
4950 
4951 		tv->vval.v_list->lv_copyID = copyID;
4952 		*tofree = list2string(tv, copyID, restore_copyID);
4953 		if (restore_copyID)
4954 		    tv->vval.v_list->lv_copyID = old_copyID;
4955 		r = *tofree;
4956 	    }
4957 	    break;
4958 
4959 	case VAR_DICT:
4960 	    if (tv->vval.v_dict == NULL)
4961 	    {
4962 		// NULL dict is equivalent to empty dict.
4963 		*tofree = NULL;
4964 		r = (char_u *)"{}";
4965 	    }
4966 	    else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4967 		    && tv->vval.v_dict->dv_hashtab.ht_used != 0)
4968 	    {
4969 		*tofree = NULL;
4970 		r = (char_u *)"{...}";
4971 	    }
4972 	    else
4973 	    {
4974 		int old_copyID = tv->vval.v_dict->dv_copyID;
4975 
4976 		tv->vval.v_dict->dv_copyID = copyID;
4977 		*tofree = dict2string(tv, copyID, restore_copyID);
4978 		if (restore_copyID)
4979 		    tv->vval.v_dict->dv_copyID = old_copyID;
4980 		r = *tofree;
4981 	    }
4982 	    break;
4983 
4984 	case VAR_NUMBER:
4985 	case VAR_UNKNOWN:
4986 	case VAR_ANY:
4987 	case VAR_VOID:
4988 	    *tofree = NULL;
4989 	    r = tv_get_string_buf(tv, numbuf);
4990 	    break;
4991 
4992 	case VAR_JOB:
4993 	case VAR_CHANNEL:
4994 	    *tofree = NULL;
4995 	    r = tv_get_string_buf(tv, numbuf);
4996 	    if (composite_val)
4997 	    {
4998 		*tofree = string_quote(r, FALSE);
4999 		r = *tofree;
5000 	    }
5001 	    break;
5002 
5003 	case VAR_FLOAT:
5004 #ifdef FEAT_FLOAT
5005 	    *tofree = NULL;
5006 	    vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5007 	    r = numbuf;
5008 	    break;
5009 #endif
5010 
5011 	case VAR_BOOL:
5012 	case VAR_SPECIAL:
5013 	    *tofree = NULL;
5014 	    r = (char_u *)get_var_special_name(tv->vval.v_number);
5015 	    break;
5016     }
5017 
5018     if (--recurse == 0)
5019 	did_echo_string_emsg = FALSE;
5020     return r;
5021 }
5022 
5023 /*
5024  * Return a string with the string representation of a variable.
5025  * If the memory is allocated "tofree" is set to it, otherwise NULL.
5026  * "numbuf" is used for a number.
5027  * Does not put quotes around strings, as ":echo" displays values.
5028  * When "copyID" is not NULL replace recursive lists and dicts with "...".
5029  * May return NULL.
5030  */
5031     char_u *
5032 echo_string(
5033     typval_T	*tv,
5034     char_u	**tofree,
5035     char_u	*numbuf,
5036     int		copyID)
5037 {
5038     return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5039 }
5040 
5041 /*
5042  * Return string "str" in ' quotes, doubling ' characters.
5043  * If "str" is NULL an empty string is assumed.
5044  * If "function" is TRUE make it function('string').
5045  */
5046     char_u *
5047 string_quote(char_u *str, int function)
5048 {
5049     unsigned	len;
5050     char_u	*p, *r, *s;
5051 
5052     len = (function ? 13 : 3);
5053     if (str != NULL)
5054     {
5055 	len += (unsigned)STRLEN(str);
5056 	for (p = str; *p != NUL; MB_PTR_ADV(p))
5057 	    if (*p == '\'')
5058 		++len;
5059     }
5060     s = r = alloc(len);
5061     if (r != NULL)
5062     {
5063 	if (function)
5064 	{
5065 	    STRCPY(r, "function('");
5066 	    r += 10;
5067 	}
5068 	else
5069 	    *r++ = '\'';
5070 	if (str != NULL)
5071 	    for (p = str; *p != NUL; )
5072 	    {
5073 		if (*p == '\'')
5074 		    *r++ = '\'';
5075 		MB_COPY_CHAR(p, r);
5076 	    }
5077 	*r++ = '\'';
5078 	if (function)
5079 	    *r++ = ')';
5080 	*r++ = NUL;
5081     }
5082     return s;
5083 }
5084 
5085 #if defined(FEAT_FLOAT) || defined(PROTO)
5086 /*
5087  * Convert the string "text" to a floating point number.
5088  * This uses strtod().  setlocale(LC_NUMERIC, "C") has been used to make sure
5089  * this always uses a decimal point.
5090  * Returns the length of the text that was consumed.
5091  */
5092     int
5093 string2float(
5094     char_u	*text,
5095     float_T	*value)	    // result stored here
5096 {
5097     char	*s = (char *)text;
5098     float_T	f;
5099 
5100     // MS-Windows does not deal with "inf" and "nan" properly.
5101     if (STRNICMP(text, "inf", 3) == 0)
5102     {
5103 	*value = INFINITY;
5104 	return 3;
5105     }
5106     if (STRNICMP(text, "-inf", 3) == 0)
5107     {
5108 	*value = -INFINITY;
5109 	return 4;
5110     }
5111     if (STRNICMP(text, "nan", 3) == 0)
5112     {
5113 	*value = NAN;
5114 	return 3;
5115     }
5116     f = strtod(s, &s);
5117     *value = f;
5118     return (int)((char_u *)s - text);
5119 }
5120 #endif
5121 
5122 /*
5123  * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5124  * character index.  Works only for loaded buffers. Returns -1 on failure.
5125  * The index of the first byte and the first character is zero.
5126  */
5127     int
5128 buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5129 {
5130     char_u	*str;
5131     char_u	*t;
5132     int		count;
5133 
5134     if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5135 	return -1;
5136 
5137     if (lnum > buf->b_ml.ml_line_count)
5138 	lnum = buf->b_ml.ml_line_count;
5139 
5140     str = ml_get_buf(buf, lnum, FALSE);
5141     if (str == NULL)
5142 	return -1;
5143 
5144     if (*str == NUL)
5145 	return 0;
5146 
5147     // count the number of characters
5148     t = str;
5149     for (count = 0; *t != NUL && t <= str + byteidx; count++)
5150 	t += mb_ptr2len(t);
5151 
5152     // In insert mode, when the cursor is at the end of a non-empty line,
5153     // byteidx points to the NUL character immediately past the end of the
5154     // string. In this case, add one to the character count.
5155     if (*t == NUL && byteidx != 0 && t == str + byteidx)
5156 	count++;
5157 
5158     return count - 1;
5159 }
5160 
5161 /*
5162  * Convert the specified character index of line 'lnum' in buffer 'buf' to a
5163  * byte index.  Works only for loaded buffers. Returns -1 on failure.
5164  * The index of the first byte and the first character is zero.
5165  */
5166     int
5167 buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5168 {
5169     char_u	*str;
5170     char_u	*t;
5171 
5172     if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5173 	return -1;
5174 
5175     if (lnum > buf->b_ml.ml_line_count)
5176 	lnum = buf->b_ml.ml_line_count;
5177 
5178     str = ml_get_buf(buf, lnum, FALSE);
5179     if (str == NULL)
5180 	return -1;
5181 
5182     // Convert the character offset to a byte offset
5183     t = str;
5184     while (*t != NUL && --charidx > 0)
5185 	t += mb_ptr2len(t);
5186 
5187     return t - str;
5188 }
5189 
5190 /*
5191  * Translate a String variable into a position.
5192  * Returns NULL when there is an error.
5193  */
5194     pos_T *
5195 var2fpos(
5196     typval_T	*varp,
5197     int		dollar_lnum,	// TRUE when $ is last line
5198     int		*fnum,		// set to fnum for '0, 'A, etc.
5199     int		charcol)	// return character column
5200 {
5201     char_u		*name;
5202     static pos_T	pos;
5203     pos_T		*pp;
5204 
5205     // Argument can be [lnum, col, coladd].
5206     if (varp->v_type == VAR_LIST)
5207     {
5208 	list_T		*l;
5209 	int		len;
5210 	int		error = FALSE;
5211 	listitem_T	*li;
5212 
5213 	l = varp->vval.v_list;
5214 	if (l == NULL)
5215 	    return NULL;
5216 
5217 	// Get the line number
5218 	pos.lnum = list_find_nr(l, 0L, &error);
5219 	if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
5220 	    return NULL;	// invalid line number
5221 	if (charcol)
5222 	    len = (long)mb_charlen(ml_get(pos.lnum));
5223 	else
5224 	    len = (long)STRLEN(ml_get(pos.lnum));
5225 
5226 	// Get the column number
5227 	// We accept "$" for the column number: last column.
5228 	li = list_find(l, 1L);
5229 	if (li != NULL && li->li_tv.v_type == VAR_STRING
5230 		&& li->li_tv.vval.v_string != NULL
5231 		&& STRCMP(li->li_tv.vval.v_string, "$") == 0)
5232 	{
5233 	    pos.col = len + 1;
5234 	}
5235 	else
5236 	{
5237 	    pos.col = list_find_nr(l, 1L, &error);
5238 	    if (error)
5239 		return NULL;
5240 	}
5241 
5242 	// Accept a position up to the NUL after the line.
5243 	if (pos.col == 0 || (int)pos.col > len + 1)
5244 	    return NULL;	// invalid column number
5245 	--pos.col;
5246 
5247 	// Get the virtual offset.  Defaults to zero.
5248 	pos.coladd = list_find_nr(l, 2L, &error);
5249 	if (error)
5250 	    pos.coladd = 0;
5251 
5252 	return &pos;
5253     }
5254 
5255     name = tv_get_string_chk(varp);
5256     if (name == NULL)
5257 	return NULL;
5258     if (name[0] == '.')				// cursor
5259     {
5260 	pos = curwin->w_cursor;
5261 	if (charcol)
5262 	    pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5263 	return &pos;
5264     }
5265     if (name[0] == 'v' && name[1] == NUL)	// Visual start
5266     {
5267 	if (VIsual_active)
5268 	    pos = VIsual;
5269 	else
5270 	    pos = curwin->w_cursor;
5271 	if (charcol)
5272 	    pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5273 	return &pos;
5274     }
5275     if (name[0] == '\'')			// mark
5276     {
5277 	pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
5278 	if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5279 	    return NULL;
5280 	if (charcol)
5281 	    pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
5282 	return pp;
5283     }
5284 
5285     pos.coladd = 0;
5286 
5287     if (name[0] == 'w' && dollar_lnum)
5288     {
5289 	pos.col = 0;
5290 	if (name[1] == '0')		// "w0": first visible line
5291 	{
5292 	    update_topline();
5293 	    // In silent Ex mode topline is zero, but that's not a valid line
5294 	    // number; use one instead.
5295 	    pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
5296 	    return &pos;
5297 	}
5298 	else if (name[1] == '$')	// "w$": last visible line
5299 	{
5300 	    validate_botline();
5301 	    // In silent Ex mode botline is zero, return zero then.
5302 	    pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
5303 	    return &pos;
5304 	}
5305     }
5306     else if (name[0] == '$')		// last column or line
5307     {
5308 	if (dollar_lnum)
5309 	{
5310 	    pos.lnum = curbuf->b_ml.ml_line_count;
5311 	    pos.col = 0;
5312 	}
5313 	else
5314 	{
5315 	    pos.lnum = curwin->w_cursor.lnum;
5316 	    if (charcol)
5317 		pos.col = (colnr_T)mb_charlen(ml_get_curline());
5318 	    else
5319 		pos.col = (colnr_T)STRLEN(ml_get_curline());
5320 	}
5321 	return &pos;
5322     }
5323     return NULL;
5324 }
5325 
5326 /*
5327  * Convert list in "arg" into a position and optional file number.
5328  * When "fnump" is NULL there is no file number, only 3 items.
5329  * Note that the column is passed on as-is, the caller may want to decrement
5330  * it to use 1 for the first column.
5331  * Return FAIL when conversion is not possible, doesn't check the position for
5332  * validity.
5333  */
5334     int
5335 list2fpos(
5336     typval_T	*arg,
5337     pos_T	*posp,
5338     int		*fnump,
5339     colnr_T	*curswantp,
5340     int		charcol)
5341 {
5342     list_T	*l = arg->vval.v_list;
5343     long	i = 0;
5344     long	n;
5345 
5346     // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5347     // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
5348     if (arg->v_type != VAR_LIST
5349 	    || l == NULL
5350 	    || l->lv_len < (fnump == NULL ? 2 : 3)
5351 	    || l->lv_len > (fnump == NULL ? 4 : 5))
5352 	return FAIL;
5353 
5354     if (fnump != NULL)
5355     {
5356 	n = list_find_nr(l, i++, NULL);	// fnum
5357 	if (n < 0)
5358 	    return FAIL;
5359 	if (n == 0)
5360 	    n = curbuf->b_fnum;		// current buffer
5361 	*fnump = n;
5362     }
5363 
5364     n = list_find_nr(l, i++, NULL);	// lnum
5365     if (n < 0)
5366 	return FAIL;
5367     posp->lnum = n;
5368 
5369     n = list_find_nr(l, i++, NULL);	// col
5370     if (n < 0)
5371 	return FAIL;
5372     // If character position is specified, then convert to byte position
5373     if (charcol)
5374     {
5375 	buf_T	*buf;
5376 
5377 	// Get the text for the specified line in a loaded buffer
5378 	buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5379 	if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5380 	    return FAIL;
5381 
5382 	n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
5383     }
5384     posp->col = n;
5385 
5386     n = list_find_nr(l, i, NULL);	// off
5387     if (n < 0)
5388 	posp->coladd = 0;
5389     else
5390 	posp->coladd = n;
5391 
5392     if (curswantp != NULL)
5393 	*curswantp = list_find_nr(l, i + 1, NULL);  // curswant
5394 
5395     return OK;
5396 }
5397 
5398 /*
5399  * Get the length of an environment variable name.
5400  * Advance "arg" to the first character after the name.
5401  * Return 0 for error.
5402  */
5403     int
5404 get_env_len(char_u **arg)
5405 {
5406     char_u	*p;
5407     int		len;
5408 
5409     for (p = *arg; vim_isIDc(*p); ++p)
5410 	;
5411     if (p == *arg)	    // no name found
5412 	return 0;
5413 
5414     len = (int)(p - *arg);
5415     *arg = p;
5416     return len;
5417 }
5418 
5419 /*
5420  * Get the length of the name of a function or internal variable.
5421  * "arg" is advanced to after the name.
5422  * Return 0 if something is wrong.
5423  */
5424     int
5425 get_id_len(char_u **arg)
5426 {
5427     char_u	*p;
5428     int		len;
5429 
5430     // Find the end of the name.
5431     for (p = *arg; eval_isnamec(*p); ++p)
5432     {
5433 	if (*p == ':')
5434 	{
5435 	    // "s:" is start of "s:var", but "n:" is not and can be used in
5436 	    // slice "[n:]".  Also "xx:" is not a namespace.
5437 	    len = (int)(p - *arg);
5438 	    if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5439 		    || len > 1)
5440 		break;
5441 	}
5442     }
5443     if (p == *arg)	    // no name found
5444 	return 0;
5445 
5446     len = (int)(p - *arg);
5447     *arg = p;
5448 
5449     return len;
5450 }
5451 
5452 /*
5453  * Get the length of the name of a variable or function.
5454  * Only the name is recognized, does not handle ".key" or "[idx]".
5455  * "arg" is advanced to the first non-white character after the name.
5456  * Return -1 if curly braces expansion failed.
5457  * Return 0 if something else is wrong.
5458  * If the name contains 'magic' {}'s, expand them and return the
5459  * expanded name in an allocated string via 'alias' - caller must free.
5460  */
5461     int
5462 get_name_len(
5463     char_u	**arg,
5464     char_u	**alias,
5465     int		evaluate,
5466     int		verbose)
5467 {
5468     int		len;
5469     char_u	*p;
5470     char_u	*expr_start;
5471     char_u	*expr_end;
5472 
5473     *alias = NULL;  // default to no alias
5474 
5475     if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5476 						  && (*arg)[2] == (int)KE_SNR)
5477     {
5478 	// hard coded <SNR>, already translated
5479 	*arg += 3;
5480 	return get_id_len(arg) + 3;
5481     }
5482     len = eval_fname_script(*arg);
5483     if (len > 0)
5484     {
5485 	// literal "<SID>", "s:" or "<SNR>"
5486 	*arg += len;
5487     }
5488 
5489     /*
5490      * Find the end of the name; check for {} construction.
5491      */
5492     p = find_name_end(*arg, &expr_start, &expr_end,
5493 					       len > 0 ? 0 : FNE_CHECK_START);
5494     if (expr_start != NULL)
5495     {
5496 	char_u	*temp_string;
5497 
5498 	if (!evaluate)
5499 	{
5500 	    len += (int)(p - *arg);
5501 	    *arg = skipwhite(p);
5502 	    return len;
5503 	}
5504 
5505 	/*
5506 	 * Include any <SID> etc in the expanded string:
5507 	 * Thus the -len here.
5508 	 */
5509 	temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5510 	if (temp_string == NULL)
5511 	    return -1;
5512 	*alias = temp_string;
5513 	*arg = skipwhite(p);
5514 	return (int)STRLEN(temp_string);
5515     }
5516 
5517     len += get_id_len(arg);
5518     // Only give an error when there is something, otherwise it will be
5519     // reported at a higher level.
5520     if (len == 0 && verbose && **arg != NUL)
5521 	semsg(_(e_invexpr2), *arg);
5522 
5523     return len;
5524 }
5525 
5526 /*
5527  * Find the end of a variable or function name, taking care of magic braces.
5528  * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5529  * start and end of the first magic braces item.
5530  * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
5531  * Return a pointer to just after the name.  Equal to "arg" if there is no
5532  * valid name.
5533  */
5534     char_u *
5535 find_name_end(
5536     char_u	*arg,
5537     char_u	**expr_start,
5538     char_u	**expr_end,
5539     int		flags)
5540 {
5541     int		mb_nest = 0;
5542     int		br_nest = 0;
5543     char_u	*p;
5544     int		len;
5545     int		vim9script = in_vim9script();
5546 
5547     if (expr_start != NULL)
5548     {
5549 	*expr_start = NULL;
5550 	*expr_end = NULL;
5551     }
5552 
5553     // Quick check for valid starting character.
5554     if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5555 						&& (*arg != '{' || vim9script))
5556 	return arg;
5557 
5558     for (p = arg; *p != NUL
5559 		    && (eval_isnamec(*p)
5560 			|| (*p == '{' && !vim9script)
5561 			|| ((flags & FNE_INCL_BR) && (*p == '['
5562 					 || (*p == '.' && eval_isdictc(p[1]))))
5563 			|| mb_nest != 0
5564 			|| br_nest != 0); MB_PTR_ADV(p))
5565     {
5566 	if (*p == '\'')
5567 	{
5568 	    // skip over 'string' to avoid counting [ and ] inside it.
5569 	    for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
5570 		;
5571 	    if (*p == NUL)
5572 		break;
5573 	}
5574 	else if (*p == '"')
5575 	{
5576 	    // skip over "str\"ing" to avoid counting [ and ] inside it.
5577 	    for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
5578 		if (*p == '\\' && p[1] != NUL)
5579 		    ++p;
5580 	    if (*p == NUL)
5581 		break;
5582 	}
5583 	else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5584 	{
5585 	    // "s:" is start of "s:var", but "n:" is not and can be used in
5586 	    // slice "[n:]".  Also "xx:" is not a namespace. But {ns}: is.
5587 	    len = (int)(p - arg);
5588 	    if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
5589 		    || (len > 1 && p[-1] != '}'))
5590 		break;
5591 	}
5592 
5593 	if (mb_nest == 0)
5594 	{
5595 	    if (*p == '[')
5596 		++br_nest;
5597 	    else if (*p == ']')
5598 		--br_nest;
5599 	}
5600 
5601 	if (br_nest == 0 && !vim9script)
5602 	{
5603 	    if (*p == '{')
5604 	    {
5605 		mb_nest++;
5606 		if (expr_start != NULL && *expr_start == NULL)
5607 		    *expr_start = p;
5608 	    }
5609 	    else if (*p == '}')
5610 	    {
5611 		mb_nest--;
5612 		if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5613 		    *expr_end = p;
5614 	    }
5615 	}
5616     }
5617 
5618     return p;
5619 }
5620 
5621 /*
5622  * Expands out the 'magic' {}'s in a variable/function name.
5623  * Note that this can call itself recursively, to deal with
5624  * constructs like foo{bar}{baz}{bam}
5625  * The four pointer arguments point to "foo{expre}ss{ion}bar"
5626  *			"in_start"      ^
5627  *			"expr_start"	   ^
5628  *			"expr_end"		 ^
5629  *			"in_end"			    ^
5630  *
5631  * Returns a new allocated string, which the caller must free.
5632  * Returns NULL for failure.
5633  */
5634     static char_u *
5635 make_expanded_name(
5636     char_u	*in_start,
5637     char_u	*expr_start,
5638     char_u	*expr_end,
5639     char_u	*in_end)
5640 {
5641     char_u	c1;
5642     char_u	*retval = NULL;
5643     char_u	*temp_result;
5644 
5645     if (expr_end == NULL || in_end == NULL)
5646 	return NULL;
5647     *expr_start	= NUL;
5648     *expr_end = NUL;
5649     c1 = *in_end;
5650     *in_end = NUL;
5651 
5652     temp_result = eval_to_string(expr_start + 1, FALSE);
5653     if (temp_result != NULL)
5654     {
5655 	retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5656 						   + (in_end - expr_end) + 1);
5657 	if (retval != NULL)
5658 	{
5659 	    STRCPY(retval, in_start);
5660 	    STRCAT(retval, temp_result);
5661 	    STRCAT(retval, expr_end + 1);
5662 	}
5663     }
5664     vim_free(temp_result);
5665 
5666     *in_end = c1;		// put char back for error messages
5667     *expr_start = '{';
5668     *expr_end = '}';
5669 
5670     if (retval != NULL)
5671     {
5672 	temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
5673 	if (expr_start != NULL)
5674 	{
5675 	    // Further expansion!
5676 	    temp_result = make_expanded_name(retval, expr_start,
5677 						       expr_end, temp_result);
5678 	    vim_free(retval);
5679 	    retval = temp_result;
5680 	}
5681     }
5682 
5683     return retval;
5684 }
5685 
5686 /*
5687  * Return TRUE if character "c" can be used in a variable or function name.
5688  * Does not include '{' or '}' for magic braces.
5689  */
5690     int
5691 eval_isnamec(int c)
5692 {
5693     return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
5694 }
5695 
5696 /*
5697  * Return TRUE if character "c" can be used as the first character in a
5698  * variable or function name (excluding '{' and '}').
5699  */
5700     int
5701 eval_isnamec1(int c)
5702 {
5703     return ASCII_ISALPHA(c) || c == '_';
5704 }
5705 
5706 /*
5707  * Return TRUE if character "c" can be used as the first character of a
5708  * dictionary key.
5709  */
5710     int
5711 eval_isdictc(int c)
5712 {
5713     return ASCII_ISALNUM(c) || c == '_';
5714 }
5715 
5716 /*
5717  * Handle:
5718  * - expr[expr], expr[expr:expr] subscript
5719  * - ".name" lookup
5720  * - function call with Funcref variable: func(expr)
5721  * - method call: var->method()
5722  *
5723  * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
5724  */
5725     int
5726 handle_subscript(
5727     char_u	**arg,
5728     typval_T	*rettv,
5729     evalarg_T	*evalarg,
5730     int		verbose)	// give error messages
5731 {
5732     int		evaluate = evalarg != NULL
5733 				      && (evalarg->eval_flags & EVAL_EVALUATE);
5734     int		ret = OK;
5735     dict_T	*selfdict = NULL;
5736     int		check_white = TRUE;
5737     int		getnext;
5738     char_u	*p;
5739 
5740     while (ret == OK)
5741     {
5742 	// When at the end of the line and ".name" or "->{" or "->X" follows in
5743 	// the next line then consume the line break.
5744 	p = eval_next_non_blank(*arg, evalarg, &getnext);
5745 	if (getnext
5746 	    && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
5747 		|| (p[0] == '-' && p[1] == '>'
5748 				     && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
5749 	{
5750 	    *arg = eval_next_line(evalarg);
5751 	    p = *arg;
5752 	    check_white = FALSE;
5753 	}
5754 
5755 	if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5756 			    || rettv->v_type == VAR_PARTIAL))
5757 		    && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
5758 	{
5759 	    ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5760 							       selfdict, NULL);
5761 
5762 	    // Stop the expression evaluation when immediately aborting on
5763 	    // error, or when an interrupt occurred or an exception was thrown
5764 	    // but not caught.
5765 	    if (aborting())
5766 	    {
5767 		if (ret == OK)
5768 		    clear_tv(rettv);
5769 		ret = FAIL;
5770 	    }
5771 	    dict_unref(selfdict);
5772 	    selfdict = NULL;
5773 	}
5774 	else if (p[0] == '-' && p[1] == '>')
5775 	{
5776 	    *arg = skipwhite(p + 2);
5777 	    if (ret == OK)
5778 	    {
5779 		if ((**arg == '{' && !in_vim9script()) || **arg == '(')
5780 		    // expr->{lambda}() or expr->(lambda)()
5781 		    ret = eval_lambda(arg, rettv, evalarg, verbose);
5782 		else
5783 		    // expr->name()
5784 		    ret = eval_method(arg, rettv, evalarg, verbose);
5785 	    }
5786 	}
5787 	// "." is ".name" lookup when we found a dict or when evaluating and
5788 	// scriptversion is at least 2, where string concatenation is "..".
5789 	else if (**arg == '['
5790 		|| (**arg == '.' && (rettv->v_type == VAR_DICT
5791 			|| (!evaluate
5792 			    && (*arg)[1] != '.'
5793 			    && current_sctx.sc_version >= 2))))
5794 	{
5795 	    dict_unref(selfdict);
5796 	    if (rettv->v_type == VAR_DICT)
5797 	    {
5798 		selfdict = rettv->vval.v_dict;
5799 		if (selfdict != NULL)
5800 		    ++selfdict->dv_refcount;
5801 	    }
5802 	    else
5803 		selfdict = NULL;
5804 	    if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
5805 	    {
5806 		clear_tv(rettv);
5807 		ret = FAIL;
5808 	    }
5809 	}
5810 	else
5811 	    break;
5812     }
5813 
5814     // Turn "dict.Func" into a partial for "Func" bound to "dict".
5815     // Don't do this when "Func" is already a partial that was bound
5816     // explicitly (pt_auto is FALSE).
5817     if (selfdict != NULL
5818 	    && (rettv->v_type == VAR_FUNC
5819 		|| (rettv->v_type == VAR_PARTIAL
5820 		    && (rettv->vval.v_partial->pt_auto
5821 			|| rettv->vval.v_partial->pt_dict == NULL))))
5822 	selfdict = make_partial(selfdict, rettv);
5823 
5824     dict_unref(selfdict);
5825     return ret;
5826 }
5827 
5828 /*
5829  * Make a copy of an item.
5830  * Lists and Dictionaries are also copied.  A deep copy if "deep" is set.
5831  * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5832  * reference to an already copied list/dict can be used.
5833  * Returns FAIL or OK.
5834  */
5835     int
5836 item_copy(
5837     typval_T	*from,
5838     typval_T	*to,
5839     int		deep,
5840     int		copyID)
5841 {
5842     static int	recurse = 0;
5843     int		ret = OK;
5844 
5845     if (recurse >= DICT_MAXNEST)
5846     {
5847 	emsg(_("E698: variable nested too deep for making a copy"));
5848 	return FAIL;
5849     }
5850     ++recurse;
5851 
5852     switch (from->v_type)
5853     {
5854 	case VAR_NUMBER:
5855 	case VAR_FLOAT:
5856 	case VAR_STRING:
5857 	case VAR_FUNC:
5858 	case VAR_PARTIAL:
5859 	case VAR_BOOL:
5860 	case VAR_SPECIAL:
5861 	case VAR_JOB:
5862 	case VAR_CHANNEL:
5863 	    copy_tv(from, to);
5864 	    break;
5865 	case VAR_LIST:
5866 	    to->v_type = VAR_LIST;
5867 	    to->v_lock = 0;
5868 	    if (from->vval.v_list == NULL)
5869 		to->vval.v_list = NULL;
5870 	    else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5871 	    {
5872 		// use the copy made earlier
5873 		to->vval.v_list = from->vval.v_list->lv_copylist;
5874 		++to->vval.v_list->lv_refcount;
5875 	    }
5876 	    else
5877 		to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5878 	    if (to->vval.v_list == NULL)
5879 		ret = FAIL;
5880 	    break;
5881 	case VAR_BLOB:
5882 	    ret = blob_copy(from->vval.v_blob, to);
5883 	    break;
5884 	case VAR_DICT:
5885 	    to->v_type = VAR_DICT;
5886 	    to->v_lock = 0;
5887 	    if (from->vval.v_dict == NULL)
5888 		to->vval.v_dict = NULL;
5889 	    else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5890 	    {
5891 		// use the copy made earlier
5892 		to->vval.v_dict = from->vval.v_dict->dv_copydict;
5893 		++to->vval.v_dict->dv_refcount;
5894 	    }
5895 	    else
5896 		to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5897 	    if (to->vval.v_dict == NULL)
5898 		ret = FAIL;
5899 	    break;
5900 	case VAR_UNKNOWN:
5901 	case VAR_ANY:
5902 	case VAR_VOID:
5903 	    internal_error_no_abort("item_copy(UNKNOWN)");
5904 	    ret = FAIL;
5905     }
5906     --recurse;
5907     return ret;
5908 }
5909 
5910     void
5911 echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5912 {
5913     char_u	*tofree;
5914     char_u	numbuf[NUMBUFLEN];
5915     char_u	*p = echo_string(rettv, &tofree, numbuf, get_copyID());
5916 
5917     if (*atstart)
5918     {
5919 	*atstart = FALSE;
5920 	// Call msg_start() after eval1(), evaluating the expression
5921 	// may cause a message to appear.
5922 	if (with_space)
5923 	{
5924 	    // Mark the saved text as finishing the line, so that what
5925 	    // follows is displayed on a new line when scrolling back
5926 	    // at the more prompt.
5927 	    msg_sb_eol();
5928 	    msg_start();
5929 	}
5930     }
5931     else if (with_space)
5932 	msg_puts_attr(" ", echo_attr);
5933 
5934     if (p != NULL)
5935 	for ( ; *p != NUL && !got_int; ++p)
5936 	{
5937 	    if (*p == '\n' || *p == '\r' || *p == TAB)
5938 	    {
5939 		if (*p != TAB && *needclr)
5940 		{
5941 		    // remove any text still there from the command
5942 		    msg_clr_eos();
5943 		    *needclr = FALSE;
5944 		}
5945 		msg_putchar_attr(*p, echo_attr);
5946 	    }
5947 	    else
5948 	    {
5949 		if (has_mbyte)
5950 		{
5951 		    int i = (*mb_ptr2len)(p);
5952 
5953 		    (void)msg_outtrans_len_attr(p, i, echo_attr);
5954 		    p += i - 1;
5955 		}
5956 		else
5957 		    (void)msg_outtrans_len_attr(p, 1, echo_attr);
5958 	    }
5959 	}
5960     vim_free(tofree);
5961 }
5962 
5963 /*
5964  * ":echo expr1 ..."	print each argument separated with a space, add a
5965  *			newline at the end.
5966  * ":echon expr1 ..."	print each argument plain.
5967  */
5968     void
5969 ex_echo(exarg_T *eap)
5970 {
5971     char_u	*arg = eap->arg;
5972     typval_T	rettv;
5973     char_u	*p;
5974     int		needclr = TRUE;
5975     int		atstart = TRUE;
5976     int		did_emsg_before = did_emsg;
5977     int		called_emsg_before = called_emsg;
5978     evalarg_T	evalarg;
5979 
5980     fill_evalarg_from_eap(&evalarg, eap, eap->skip);
5981 
5982     if (eap->skip)
5983 	++emsg_skip;
5984     while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
5985     {
5986 	// If eval1() causes an error message the text from the command may
5987 	// still need to be cleared. E.g., "echo 22,44".
5988 	need_clr_eos = needclr;
5989 
5990 	p = arg;
5991 	if (eval1(&arg, &rettv, &evalarg) == FAIL)
5992 	{
5993 	    /*
5994 	     * Report the invalid expression unless the expression evaluation
5995 	     * has been cancelled due to an aborting error, an interrupt, or an
5996 	     * exception.
5997 	     */
5998 	    if (!aborting() && did_emsg == did_emsg_before
5999 					  && called_emsg == called_emsg_before)
6000 		semsg(_(e_invexpr2), p);
6001 	    need_clr_eos = FALSE;
6002 	    break;
6003 	}
6004 	need_clr_eos = FALSE;
6005 
6006 	if (!eap->skip)
6007 	    echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
6008 
6009 	clear_tv(&rettv);
6010 	arg = skipwhite(arg);
6011     }
6012     eap->nextcmd = check_nextcmd(arg);
6013     clear_evalarg(&evalarg, eap);
6014 
6015     if (eap->skip)
6016 	--emsg_skip;
6017     else
6018     {
6019 	// remove text that may still be there from the command
6020 	if (needclr)
6021 	    msg_clr_eos();
6022 	if (eap->cmdidx == CMD_echo)
6023 	    msg_end();
6024     }
6025 }
6026 
6027 /*
6028  * ":echohl {name}".
6029  */
6030     void
6031 ex_echohl(exarg_T *eap)
6032 {
6033     echo_attr = syn_name2attr(eap->arg);
6034 }
6035 
6036 /*
6037  * Returns the :echo attribute
6038  */
6039     int
6040 get_echo_attr(void)
6041 {
6042     return echo_attr;
6043 }
6044 
6045 /*
6046  * ":execute expr1 ..."	execute the result of an expression.
6047  * ":echomsg expr1 ..."	Print a message
6048  * ":echoerr expr1 ..."	Print an error
6049  * Each gets spaces around each argument and a newline at the end for
6050  * echo commands
6051  */
6052     void
6053 ex_execute(exarg_T *eap)
6054 {
6055     char_u	*arg = eap->arg;
6056     typval_T	rettv;
6057     int		ret = OK;
6058     char_u	*p;
6059     garray_T	ga;
6060     int		len;
6061 
6062     ga_init2(&ga, 1, 80);
6063 
6064     if (eap->skip)
6065 	++emsg_skip;
6066     while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
6067     {
6068 	ret = eval1_emsg(&arg, &rettv, eap);
6069 	if (ret == FAIL)
6070 	    break;
6071 
6072 	if (!eap->skip)
6073 	{
6074 	    char_u   buf[NUMBUFLEN];
6075 
6076 	    if (eap->cmdidx == CMD_execute)
6077 	    {
6078 		if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6079 		{
6080 		    emsg(_(e_inval_string));
6081 		    p = NULL;
6082 		}
6083 		else
6084 		    p = tv_get_string_buf(&rettv, buf);
6085 	    }
6086 	    else
6087 		p = tv_stringify(&rettv, buf);
6088 	    if (p == NULL)
6089 	    {
6090 		clear_tv(&rettv);
6091 		ret = FAIL;
6092 		break;
6093 	    }
6094 	    len = (int)STRLEN(p);
6095 	    if (ga_grow(&ga, len + 2) == FAIL)
6096 	    {
6097 		clear_tv(&rettv);
6098 		ret = FAIL;
6099 		break;
6100 	    }
6101 	    if (ga.ga_len)
6102 		((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
6103 	    STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
6104 	    ga.ga_len += len;
6105 	}
6106 
6107 	clear_tv(&rettv);
6108 	arg = skipwhite(arg);
6109     }
6110 
6111     if (ret != FAIL && ga.ga_data != NULL)
6112     {
6113 	if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6114 	{
6115 	    // Mark the already saved text as finishing the line, so that what
6116 	    // follows is displayed on a new line when scrolling back at the
6117 	    // more prompt.
6118 	    msg_sb_eol();
6119 	}
6120 
6121 	if (eap->cmdidx == CMD_echomsg)
6122 	{
6123 	    msg_attr(ga.ga_data, echo_attr);
6124 	    out_flush();
6125 	}
6126 	else if (eap->cmdidx == CMD_echoerr)
6127 	{
6128 	    int		save_did_emsg = did_emsg;
6129 
6130 	    // We don't want to abort following commands, restore did_emsg.
6131 	    emsg(ga.ga_data);
6132 	    if (!force_abort)
6133 		did_emsg = save_did_emsg;
6134 	}
6135 	else if (eap->cmdidx == CMD_execute)
6136 	    do_cmdline((char_u *)ga.ga_data,
6137 		       eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6138     }
6139 
6140     ga_clear(&ga);
6141 
6142     if (eap->skip)
6143 	--emsg_skip;
6144 
6145     eap->nextcmd = check_nextcmd(arg);
6146 }
6147 
6148 /*
6149  * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6150  * "arg" points to the "&" or '+' when called, to "option" when returning.
6151  * Returns NULL when no option name found.  Otherwise pointer to the char
6152  * after the option name.
6153  */
6154     char_u *
6155 find_option_end(char_u **arg, int *opt_flags)
6156 {
6157     char_u	*p = *arg;
6158 
6159     ++p;
6160     if (*p == 'g' && p[1] == ':')
6161     {
6162 	*opt_flags = OPT_GLOBAL;
6163 	p += 2;
6164     }
6165     else if (*p == 'l' && p[1] == ':')
6166     {
6167 	*opt_flags = OPT_LOCAL;
6168 	p += 2;
6169     }
6170     else
6171 	*opt_flags = 0;
6172 
6173     if (!ASCII_ISALPHA(*p))
6174 	return NULL;
6175     *arg = p;
6176 
6177     if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
6178 	p += 4;	    // termcap option
6179     else
6180 	while (ASCII_ISALPHA(*p))
6181 	    ++p;
6182     return p;
6183 }
6184 
6185 /*
6186  * Display script name where an item was last set.
6187  * Should only be invoked when 'verbose' is non-zero.
6188  */
6189     void
6190 last_set_msg(sctx_T script_ctx)
6191 {
6192     char_u *p;
6193 
6194     if (script_ctx.sc_sid != 0)
6195     {
6196 	p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
6197 	if (p != NULL)
6198 	{
6199 	    verbose_enter();
6200 	    msg_puts(_("\n\tLast set from "));
6201 	    msg_puts((char *)p);
6202 	    if (script_ctx.sc_lnum > 0)
6203 	    {
6204 		msg_puts(_(line_msg));
6205 		msg_outnum((long)script_ctx.sc_lnum);
6206 	    }
6207 	    verbose_leave();
6208 	    vim_free(p);
6209 	}
6210     }
6211 }
6212 
6213 #endif // FEAT_EVAL
6214 
6215 /*
6216  * Perform a substitution on "str" with pattern "pat" and substitute "sub".
6217  * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
6218  * "flags" can be "g" to do a global substitute.
6219  * Returns an allocated string, NULL for error.
6220  */
6221     char_u *
6222 do_string_sub(
6223     char_u	*str,
6224     char_u	*pat,
6225     char_u	*sub,
6226     typval_T	*expr,
6227     char_u	*flags)
6228 {
6229     int		sublen;
6230     regmatch_T	regmatch;
6231     int		i;
6232     int		do_all;
6233     char_u	*tail;
6234     char_u	*end;
6235     garray_T	ga;
6236     char_u	*ret;
6237     char_u	*save_cpo;
6238     char_u	*zero_width = NULL;
6239 
6240     // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
6241     save_cpo = p_cpo;
6242     p_cpo = empty_option;
6243 
6244     ga_init2(&ga, 1, 200);
6245 
6246     do_all = (flags[0] == 'g');
6247 
6248     regmatch.rm_ic = p_ic;
6249     regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6250     if (regmatch.regprog != NULL)
6251     {
6252 	tail = str;
6253 	end = str + STRLEN(str);
6254 	while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6255 	{
6256 	    // Skip empty match except for first match.
6257 	    if (regmatch.startp[0] == regmatch.endp[0])
6258 	    {
6259 		if (zero_width == regmatch.startp[0])
6260 		{
6261 		    // avoid getting stuck on a match with an empty string
6262 		    i = mb_ptr2len(tail);
6263 		    mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6264 								   (size_t)i);
6265 		    ga.ga_len += i;
6266 		    tail += i;
6267 		    continue;
6268 		}
6269 		zero_width = regmatch.startp[0];
6270 	    }
6271 
6272 	    /*
6273 	     * Get some space for a temporary buffer to do the substitution
6274 	     * into.  It will contain:
6275 	     * - The text up to where the match is.
6276 	     * - The substituted text.
6277 	     * - The text after the match.
6278 	     */
6279 	    sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
6280 	    if (ga_grow(&ga, (int)((end - tail) + sublen -
6281 			    (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6282 	    {
6283 		ga_clear(&ga);
6284 		break;
6285 	    }
6286 
6287 	    // copy the text up to where the match is
6288 	    i = (int)(regmatch.startp[0] - tail);
6289 	    mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
6290 	    // add the substituted text
6291 	    (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
6292 					  + ga.ga_len + i, TRUE, TRUE, FALSE);
6293 	    ga.ga_len += i + sublen - 1;
6294 	    tail = regmatch.endp[0];
6295 	    if (*tail == NUL)
6296 		break;
6297 	    if (!do_all)
6298 		break;
6299 	}
6300 
6301 	if (ga.ga_data != NULL)
6302 	    STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6303 
6304 	vim_regfree(regmatch.regprog);
6305     }
6306 
6307     ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6308     ga_clear(&ga);
6309     if (p_cpo == empty_option)
6310 	p_cpo = save_cpo;
6311     else
6312     {
6313 	// Darn, evaluating {sub} expression or {expr} changed the value.
6314 	// If it's still empty it was changed and restored, need to restore in
6315 	// the complicated way.
6316 	if (*p_cpo == NUL)
6317 	    set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
6318 	free_string_option(save_cpo);
6319     }
6320 
6321     return ret;
6322 }
6323