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