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