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