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