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