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