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