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