blob: ffb69de8d2180dfccf2050343c187d58048041c1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010025#define NAMESPACE_CHAR (char_u *)"abglstvw"
26
Bram Moolenaar532c7802005-01-27 14:44:31 +000027/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000028 * When recursively copying lists and dicts we need to remember which ones we
29 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000030 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000031 */
32static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020033
Bram Moolenaar071d4272004-06-13 20:20:40 +000034/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000035 * Info used by a ":for" loop.
36 */
Bram Moolenaar33570922005-01-25 22:26:29 +000037typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000038{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010039 int fi_semicolon; // TRUE if ending in '; var]'
40 int fi_varcount; // nr of variables in the list
41 listwatch_T fi_lw; // keep an eye on the item used.
42 list_T *fi_list; // list being used
43 int fi_bi; // index of blob
44 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000045} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000046
Bram Moolenaar48e697e2016-01-23 22:17:30 +010047static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
53static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +020054static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000055
Bram Moolenaar48e697e2016-01-23 22:17:30 +010056static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020057static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020058
Bram Moolenaare21c1582019-03-02 11:57:09 +010059/*
60 * Return "n1" divided by "n2", taking care of dividing by zero.
61 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020062 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010063num_divide(varnumber_T n1, varnumber_T n2)
64{
65 varnumber_T result;
66
67 if (n2 == 0) // give an error message?
68 {
69 if (n1 == 0)
70 result = VARNUM_MIN; // similar to NaN
71 else if (n1 < 0)
72 result = -VARNUM_MAX;
73 else
74 result = VARNUM_MAX;
75 }
76 else
77 result = n1 / n2;
78
79 return result;
80}
81
82/*
83 * Return "n1" modulus "n2", taking care of dividing by zero.
84 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010086num_modulus(varnumber_T n1, varnumber_T n2)
87{
88 // Give an error when n2 is 0?
89 return (n2 == 0) ? 0 : (n1 % n2);
90}
91
Bram Moolenaara1fa8922017-01-12 20:06:33 +010092#if defined(EBCDIC) || defined(PROTO)
93/*
94 * Compare struct fst by function name.
95 */
96 static int
97compare_func_name(const void *s1, const void *s2)
98{
99 struct fst *p1 = (struct fst *)s1;
100 struct fst *p2 = (struct fst *)s2;
101
102 return STRCMP(p1->f_name, p2->f_name);
103}
104
105/*
106 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100107 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100108 * On machines using EBCDIC we have to sort it.
109 */
110 static void
111sortFunctions(void)
112{
113 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
114
115 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
116}
117#endif
118
Bram Moolenaar33570922005-01-25 22:26:29 +0000119/*
120 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000121 */
122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100123eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000124{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200125 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200126 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000127
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200128#ifdef EBCDIC
129 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100130 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200131 */
132 sortFunctions();
133#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000134}
135
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000136#if defined(EXITFREE) || defined(PROTO)
137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100138eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000139{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200140 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100141 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200142 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000143
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200144 // autoloaded script names
145 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000146
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200147 // unreferenced lists and dicts
148 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200149
150 // functions not garbage collected
151 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000152}
153#endif
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155/*
156 * Top level evaluation function, returning a boolean.
157 * Sets "error" to TRUE if there was an error.
158 * Return TRUE or FALSE.
159 */
160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100161eval_to_bool(
162 char_u *arg,
163 int *error,
164 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100165 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166{
Bram Moolenaar33570922005-01-25 22:26:29 +0000167 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200168 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
170 if (skip)
171 ++emsg_skip;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200172 if (eval0(arg, &tv, nextcmd, skip ? NULL : &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
175 {
176 *error = FALSE;
177 if (!skip)
178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
185
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200186 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187}
188
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100189/*
190 * Call eval1() and give an error message if not done at a lower level.
191 */
192 static int
193eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
194{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100195 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100196 int ret;
197 int did_emsg_before = did_emsg;
198 int called_emsg_before = called_emsg;
199
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200200 ret = eval1(arg, rettv, evaluate ? &EVALARG_EVALUATE : NULL);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201 if (ret == FAIL)
202 {
203 // Report the invalid expression unless the expression evaluation has
204 // been cancelled due to an aborting error, an interrupt, or an
205 // exception, or we already gave a more specific error.
206 // Also check called_emsg for when using assert_fails().
207 if (!aborting() && did_emsg == did_emsg_before
208 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100209 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210 }
211 return ret;
212}
213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200215 * Return whether a typval is a valid expression to pass to eval_expr_typval()
216 * or eval_expr_to_bool(). An empty string returns FALSE;
217 */
218 int
219eval_expr_valid_arg(typval_T *tv)
220{
221 return tv->v_type != VAR_UNKNOWN
222 && (tv->v_type != VAR_STRING
223 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
224}
225
226/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227 * Evaluate an expression, which can be a function, partial or string.
228 * Pass arguments "argv[argc]".
229 * Return the result in "rettv" and OK or FAIL.
230 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200231 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100232eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
233{
234 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100235 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200236 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100237
238 if (expr->v_type == VAR_FUNC)
239 {
240 s = expr->vval.v_string;
241 if (s == NULL || *s == NUL)
242 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200243 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200244 funcexe.evaluate = TRUE;
245 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100246 return FAIL;
247 }
248 else if (expr->v_type == VAR_PARTIAL)
249 {
250 partial_T *partial = expr->vval.v_partial;
251
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200252 if (partial == NULL)
253 return FAIL;
254
Bram Moolenaar822ba242020-05-24 23:00:18 +0200255 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200256 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200258 if (call_def_function(partial->pt_func, argc, argv,
259 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 return FAIL;
261 }
262 else
263 {
264 s = partial_name(partial);
265 if (s == NULL || *s == NUL)
266 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200267 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 funcexe.evaluate = TRUE;
269 funcexe.partial = partial;
270 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
271 return FAIL;
272 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 }
274 else
275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100276 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100277 if (s == NULL)
278 return FAIL;
279 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100280 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100282 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100283 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200284 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100285 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 return FAIL;
287 }
288 }
289 return OK;
290}
291
292/*
293 * Like eval_to_bool() but using a typval_T instead of a string.
294 * Works for string, funcref and partial.
295 */
296 int
297eval_expr_to_bool(typval_T *expr, int *error)
298{
299 typval_T rettv;
300 int res;
301
302 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
303 {
304 *error = TRUE;
305 return FALSE;
306 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100307 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 clear_tv(&rettv);
309 return res;
310}
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312/*
313 * Top level evaluation function, returning a string. If "skip" is TRUE,
314 * only parsing to "nextcmd" is done, without reporting errors. Return
315 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
316 */
317 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100318eval_to_string_skip(
319 char_u *arg,
320 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100321 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322{
Bram Moolenaar33570922005-01-25 22:26:29 +0000323 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 char_u *retval;
325
326 if (skip)
327 ++emsg_skip;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200328 if (eval0(arg, &tv, nextcmd, skip ? NULL : &EVALARG_EVALUATE)
329 == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 retval = NULL;
331 else
332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100333 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000334 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 }
336 if (skip)
337 --emsg_skip;
338
339 return retval;
340}
341
342/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000343 * Skip over an expression at "*pp".
344 * Return FAIL for an error, OK otherwise.
345 */
346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100347skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000348{
Bram Moolenaar33570922005-01-25 22:26:29 +0000349 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000350
351 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200352 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000353}
354
355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000357 * When "convert" is TRUE convert a List into a sequence of lines and convert
358 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 * Return pointer to allocated memory, or NULL for failure.
360 */
361 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100362eval_to_string(
363 char_u *arg,
364 char_u **nextcmd,
365 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366{
Bram Moolenaar33570922005-01-25 22:26:29 +0000367 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000369 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000370#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000371 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200374 if (eval0(arg, &tv, nextcmd, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 retval = NULL;
376 else
377 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000378 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000379 {
380 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000381 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200382 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200383 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200384 if (tv.vval.v_list->lv_len > 0)
385 ga_append(&ga, NL);
386 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000387 ga_append(&ga, NUL);
388 retval = (char_u *)ga.ga_data;
389 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000390#ifdef FEAT_FLOAT
391 else if (convert && tv.v_type == VAR_FLOAT)
392 {
393 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
394 retval = vim_strsave(numbuf);
395 }
396#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000397 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100398 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000399 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
401
402 return retval;
403}
404
405/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000406 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200407 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 */
409 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100410eval_to_string_safe(
411 char_u *arg,
412 char_u **nextcmd,
413 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
415 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200416 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200418 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000419 if (use_sandbox)
420 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200421 ++textwinlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000422 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000423 if (use_sandbox)
424 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200425 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200426 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 return retval;
428}
429
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430/*
431 * Top level evaluation function, returning a number.
432 * Evaluates "expr" silently.
433 * Returns -1 for an error.
434 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200435 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100436eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
Bram Moolenaar33570922005-01-25 22:26:29 +0000438 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200439 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000440 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442 ++emsg_off;
443
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200444 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 retval = -1;
446 else
447 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100448 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000449 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 }
451 --emsg_off;
452
453 return retval;
454}
455
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000456/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000457 * Top level evaluation function.
458 * Returns an allocated typval_T with the result.
459 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000460 */
461 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100462eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000463{
464 typval_T *tv;
465
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200466 tv = ALLOC_ONE(typval_T);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200467 if (tv != NULL && eval0(arg, tv, nextcmd, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100468 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000469
470 return tv;
471}
472
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100474 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200475 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
476 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000477 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100480call_vim_function(
481 char_u *func,
482 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200483 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200484 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000486 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200487 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100489 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200490 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200491 funcexe.firstline = curwin->w_cursor.lnum;
492 funcexe.lastline = curwin->w_cursor.lnum;
493 funcexe.evaluate = TRUE;
494 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000495 if (ret == FAIL)
496 clear_tv(rettv);
497
498 return ret;
499}
500
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100501/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100502 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100503 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200504 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
505 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100506 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200507 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100508call_func_retnr(
509 char_u *func,
510 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200511 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100512{
513 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200514 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100515
Bram Moolenaarded27a12018-08-01 19:06:03 +0200516 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100517 return -1;
518
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100519 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100520 clear_tv(&rettv);
521 return retval;
522}
523
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000524/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100525 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000526 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200527 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
528 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000529 */
530 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100531call_func_retstr(
532 char_u *func,
533 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200534 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000535{
536 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000537 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000538
Bram Moolenaarded27a12018-08-01 19:06:03 +0200539 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000540 return NULL;
541
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100542 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000543 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 return retval;
545}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000546
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000547/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100548 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200549 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
550 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000551 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000552 */
553 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554call_func_retlist(
555 char_u *func,
556 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200557 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000558{
559 typval_T rettv;
560
Bram Moolenaarded27a12018-08-01 19:06:03 +0200561 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000562 return NULL;
563
564 if (rettv.v_type != VAR_LIST)
565 {
566 clear_tv(&rettv);
567 return NULL;
568 }
569
570 return rettv.vval.v_list;
571}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573#ifdef FEAT_FOLDING
574/*
575 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
576 * it in "*cp". Doesn't give error messages.
577 */
578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100579eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
Bram Moolenaar33570922005-01-25 22:26:29 +0000581 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200582 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000584 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
585 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
587 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000588 if (use_sandbox)
589 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200590 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200592 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 retval = 0;
594 else
595 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100596 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000597 if (tv.v_type == VAR_NUMBER)
598 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000599 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 retval = 0;
601 else
602 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100603 // If the result is a string, check if there is a non-digit before
604 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000605 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (!VIM_ISDIGIT(*s) && *s != '-')
607 *cp = *s++;
608 retval = atol((char *)s);
609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000610 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 }
612 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000613 if (use_sandbox)
614 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200615 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200617 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618}
619#endif
620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000622 * Get an lval: variable, Dict item or List item that can be assigned a value
623 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
624 * "name.key", "name.key[expr]" etc.
625 * Indexing only works if "name" is an existing List or Dictionary.
626 * "name" points to the start of the name.
627 * If "rettv" is not NULL it points to the value to be assigned.
628 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
629 * wrong; must end in space or cmd separator.
630 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100631 * flags:
632 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100633 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100634 * GLV_NO_AUTOLOAD: do not use script autoloading
635 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000636 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000637 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000638 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000639 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200640 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641get_lval(
642 char_u *name,
643 typval_T *rettv,
644 lval_T *lp,
645 int unlet,
646 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100647 int flags, // GLV_ values
648 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000649{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000650 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000651 char_u *expr_start, *expr_end;
652 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000653 dictitem_T *v;
654 typval_T var1;
655 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000656 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000657 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000658 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000659 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000660 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100661 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100663 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200664 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000665
666 if (skip)
667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100668 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000669 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000670 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000671 }
672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100673 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000674 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000676 if (expr_start != NULL)
677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100678 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100679 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000680 && *p != '[' && *p != '.')
681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100682 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000683 return NULL;
684 }
685
686 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
687 if (lp->ll_exp_name == NULL)
688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100689 // Report an invalid expression in braces, unless the
690 // expression evaluation has been cancelled due to an
691 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000692 if (!aborting() && !quiet)
693 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000694 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100695 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000696 return NULL;
697 }
698 }
699 lp->ll_name = lp->ll_exp_name;
700 }
701 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000703 lp->ll_name = name;
704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
706 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100707 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 char_u *tp = skipwhite(p + 1);
709
710 // parse the type after the name
711 lp->ll_type = parse_type(&tp, &si->sn_type_list);
712 lp->ll_name_end = tp;
713 }
714 }
715
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100716 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000717 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
718 return p;
719
720 cc = *p;
721 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100722 // Only pass &ht when we would write to the variable, it prevents autoload
723 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100724 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
725 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000726 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100727 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000728 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000729 if (v == NULL)
730 return NULL;
731
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000732 /*
733 * Loop until no more [idx] or .key is following.
734 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000735 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100736 var1.v_type = VAR_UNKNOWN;
737 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000740 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
741 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100742 && lp->ll_tv->vval.v_dict != NULL)
743 && !(lp->ll_tv->v_type == VAR_BLOB
744 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000746 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100747 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000748 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000750 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000752 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100753 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000754 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000755 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000756
Bram Moolenaar8c711452005-01-14 21:53:12 +0000757 len = -1;
758 if (*p == '.')
759 {
760 key = p + 1;
761 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
762 ;
763 if (len == 0)
764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000765 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100766 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000767 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000768 }
769 p = key + len;
770 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000771 else
772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100773 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000774 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000775 if (*p == ':')
776 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000777 else
778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000779 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200780 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000781 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100782 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785 clear_tv(&var1);
786 return NULL;
787 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000788 }
789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100790 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000791 if (*p == ':')
792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000793 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000795 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000799 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100800 if (rettv != NULL
801 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100802 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100803 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100804 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000805 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100807 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000810 }
811 p = skipwhite(p + 1);
812 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000813 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000814 else
815 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200817 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200818 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000819 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100820 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000822 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100823 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100826 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000827 clear_tv(&var2);
828 return NULL;
829 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000830 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000832 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000833 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000835
Bram Moolenaar8c711452005-01-14 21:53:12 +0000836 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000837 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100839 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100840 clear_tv(&var1);
841 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000842 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000843 }
844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100845 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000846 ++p;
847 }
848
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000850 {
851 if (len == -1)
852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100853 // "[key]": get key from "var1"
854 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200855 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000857 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000859 }
860 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000861 lp->ll_list = NULL;
862 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000863 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100865 // When assigning to a scope dictionary check that a function and
866 // variable name is valid (only variable name unless it is l: or
867 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200868 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200869 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200870 int prevval;
871 int wrong;
872
873 if (len != -1)
874 {
875 prevval = key[len];
876 key[len] = NUL;
877 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200878 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100879 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200880 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
881 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200882 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200883 || !valid_varname(key);
884 if (len != -1)
885 key[len] = prevval;
886 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200887 {
888 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200889 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200890 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200891 }
892
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000894 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100895 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200896 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100897 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100899 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100900 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200901 return NULL;
902 }
903
Bram Moolenaar31b81602019-02-10 22:14:27 +0100904 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000906 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000907 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100908 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100909 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000911 }
912 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000914 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100916 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000918 p = NULL;
919 break;
920 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100921 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100922 else if ((flags & GLV_READ_ONLY) == 0
923 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100924 {
925 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200926 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100927 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200928
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100929 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000930 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000931 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100932 else if (lp->ll_tv->v_type == VAR_BLOB)
933 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100934 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
935
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100936 /*
937 * Get the number and item for the only or first index of the List.
938 */
939 if (empty1)
940 lp->ll_n1 = 0;
941 else
942 // is number or string
943 lp->ll_n1 = (long)tv_get_number(&var1);
944 clear_tv(&var1);
945
946 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100947 || lp->ll_n1 > bloblen
948 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100949 {
950 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100951 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100952 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100953 return NULL;
954 }
955 if (lp->ll_range && !lp->ll_empty2)
956 {
957 lp->ll_n2 = (long)tv_get_number(&var2);
958 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100959 if (lp->ll_n2 < 0
960 || lp->ll_n2 >= bloblen
961 || lp->ll_n2 < lp->ll_n1)
962 {
963 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100964 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100965 return NULL;
966 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100967 }
968 lp->ll_blob = lp->ll_tv->vval.v_blob;
969 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100970 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100971 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000972 else
973 {
974 /*
975 * Get the number and item for the only or first index of the List.
976 */
977 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100980 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100981 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100982 clear_tv(&var1);
983
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000984 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 lp->ll_list = lp->ll_tv->vval.v_list;
986 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
987 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000989 if (lp->ll_n1 < 0)
990 {
991 lp->ll_n1 = 0;
992 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
993 }
994 }
995 if (lp->ll_li == NULL)
996 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100997 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200998 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100999 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 }
1002
1003 /*
1004 * May need to find the item or absolute index for the second
1005 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 * When no index given: "lp->ll_empty2" is TRUE.
1007 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001010 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001011 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001012 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001016 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001017 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001018 {
1019 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001020 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001022 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
1025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001026 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 if (lp->ll_n1 < 0)
1028 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1029 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001030 {
1031 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001032 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001034 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001035 }
1036
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 }
1040
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001041 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 return p;
1044}
1045
1046/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001047 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001050clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001051{
1052 vim_free(lp->ll_exp_name);
1053 vim_free(lp->ll_newkey);
1054}
1055
1056/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001057 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001059 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1060 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001063set_var_lval(
1064 lval_T *lp,
1065 char_u *endp,
1066 typval_T *rettv,
1067 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001069 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070{
1071 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001072 listitem_T *ri;
1073 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074
1075 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001077 cc = *endp;
1078 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001079 if (lp->ll_blob != NULL)
1080 {
1081 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001082
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001083 if (op != NULL && *op != '=')
1084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001085 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001086 return;
1087 }
1088
1089 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1090 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001091 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001092
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001093 if (lp->ll_empty2)
1094 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1095
1096 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001098 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001099 return;
1100 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001101 if (lp->ll_empty2)
1102 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001103
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001104 ir = 0;
1105 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1106 blob_set(lp->ll_blob, il,
1107 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001108 }
1109 else
1110 {
1111 val = (int)tv_get_number_chk(rettv, &error);
1112 if (!error)
1113 {
1114 garray_T *gap = &lp->ll_blob->bv_ga;
1115
1116 // Allow for appending a byte. Setting a byte beyond
1117 // the end is an error otherwise.
1118 if (lp->ll_n1 < gap->ga_len
1119 || (lp->ll_n1 == gap->ga_len
1120 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1121 {
1122 blob_set(lp->ll_blob, lp->ll_n1, val);
1123 if (lp->ll_n1 == gap->ga_len)
1124 ++gap->ga_len;
1125 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001126 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001127 }
1128 }
1129 }
1130 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001132 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001135 {
1136 emsg(_(e_cannot_mod));
1137 *endp = cc;
1138 return;
1139 }
1140
Bram Moolenaarff697e62019-02-12 22:28:33 +01001141 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001142 di = NULL;
1143 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1144 &tv, &di, TRUE, FALSE) == OK)
1145 {
1146 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001147 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1148 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001149 && tv_op(&tv, rettv, op) == OK)
1150 set_var(lp->ll_name, &tv, FALSE);
1151 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001154 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001156 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001157 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001158 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001159 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001160 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001161 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 else if (lp->ll_range)
1163 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001164 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001165 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001168 {
1169 emsg(_("E996: Cannot lock a range"));
1170 return;
1171 }
1172
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001173 /*
1174 * Check whether any of the list items is locked
1175 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001176 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001177 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001178 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001179 return;
1180 ri = ri->li_next;
1181 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1182 break;
1183 ll_li = ll_li->li_next;
1184 ++ll_n1;
1185 }
1186
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001187 /*
1188 * Assign the List values to the list items.
1189 */
1190 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001191 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001192 if (op != NULL && *op != '=')
1193 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1194 else
1195 {
1196 clear_tv(&lp->ll_li->li_tv);
1197 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1198 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 ri = ri->li_next;
1200 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1201 break;
1202 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001203 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001204 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001205 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001206 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 ri = NULL;
1208 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001209 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001210 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 lp->ll_li = lp->ll_li->li_next;
1212 ++lp->ll_n1;
1213 }
1214 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001215 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001216 else if (lp->ll_empty2
1217 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001219 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 }
1221 else
1222 {
1223 /*
1224 * Assign to a List or Dictionary item.
1225 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001227 {
1228 emsg(_("E996: Cannot lock a list or dict"));
1229 return;
1230 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 if (lp->ll_newkey != NULL)
1232 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001233 if (op != NULL && *op != '=')
1234 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001235 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001236 return;
1237 }
1238
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001239 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001240 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 if (di == NULL)
1242 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001243 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1244 {
1245 vim_free(di);
1246 return;
1247 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001249 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001250 else if (op != NULL && *op != '=')
1251 {
1252 tv_op(lp->ll_tv, rettv, op);
1253 return;
1254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001255 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 /*
1259 * Assign the value to the variable or list item.
1260 */
1261 if (copy)
1262 copy_tv(rettv, lp->ll_tv);
1263 else
1264 {
1265 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001266 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 }
1269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270}
1271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001272/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001273 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1274 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001275 * Returns OK or FAIL.
1276 */
1277 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001279{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001280 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 char_u numbuf[NUMBUFLEN];
1282 char_u *s;
1283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001284 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001285 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001286 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001287 {
1288 switch (tv1->v_type)
1289 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001290 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001291 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001293 case VAR_DICT:
1294 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001295 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001296 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001297 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001298 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001299 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001300 break;
1301
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001302 case VAR_BLOB:
1303 if (*op != '+' || tv2->v_type != VAR_BLOB)
1304 break;
1305 // BLOB += BLOB
1306 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1307 {
1308 blob_T *b1 = tv1->vval.v_blob;
1309 blob_T *b2 = tv2->vval.v_blob;
1310 int i, len = blob_len(b2);
1311 for (i = 0; i < len; i++)
1312 ga_append(&b1->bv_ga, blob_get(b2, i));
1313 }
1314 return OK;
1315
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001316 case VAR_LIST:
1317 if (*op != '+' || tv2->v_type != VAR_LIST)
1318 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001319 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001320 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1321 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1322 return OK;
1323
1324 case VAR_NUMBER:
1325 case VAR_STRING:
1326 if (tv2->v_type == VAR_LIST)
1327 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001328 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001329 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001330 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001331 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001332#ifdef FEAT_FLOAT
1333 if (tv2->v_type == VAR_FLOAT)
1334 {
1335 float_T f = n;
1336
Bram Moolenaarff697e62019-02-12 22:28:33 +01001337 if (*op == '%')
1338 break;
1339 switch (*op)
1340 {
1341 case '+': f += tv2->vval.v_float; break;
1342 case '-': f -= tv2->vval.v_float; break;
1343 case '*': f *= tv2->vval.v_float; break;
1344 case '/': f /= tv2->vval.v_float; break;
1345 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001346 clear_tv(tv1);
1347 tv1->v_type = VAR_FLOAT;
1348 tv1->vval.v_float = f;
1349 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001350 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001351#endif
1352 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001353 switch (*op)
1354 {
1355 case '+': n += tv_get_number(tv2); break;
1356 case '-': n -= tv_get_number(tv2); break;
1357 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001358 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1359 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001360 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001361 clear_tv(tv1);
1362 tv1->v_type = VAR_NUMBER;
1363 tv1->vval.v_number = n;
1364 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 }
1366 else
1367 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001368 if (tv2->v_type == VAR_FLOAT)
1369 break;
1370
Bram Moolenaarff697e62019-02-12 22:28:33 +01001371 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001372 s = tv_get_string(tv1);
1373 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001374 clear_tv(tv1);
1375 tv1->v_type = VAR_STRING;
1376 tv1->vval.v_string = s;
1377 }
1378 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001379
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001380 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001381#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001382 {
1383 float_T f;
1384
Bram Moolenaarff697e62019-02-12 22:28:33 +01001385 if (*op == '%' || *op == '.'
1386 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001387 && tv2->v_type != VAR_NUMBER
1388 && tv2->v_type != VAR_STRING))
1389 break;
1390 if (tv2->v_type == VAR_FLOAT)
1391 f = tv2->vval.v_float;
1392 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001393 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001394 switch (*op)
1395 {
1396 case '+': tv1->vval.v_float += f; break;
1397 case '-': tv1->vval.v_float -= f; break;
1398 case '*': tv1->vval.v_float *= f; break;
1399 case '/': tv1->vval.v_float /= f; break;
1400 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001401 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001402#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001403 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001404 }
1405 }
1406
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001407 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001408 return FAIL;
1409}
1410
1411/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412 * Evaluate the expression used in a ":for var in expr" command.
1413 * "arg" points to "var".
1414 * Set "*errp" to TRUE for an error, FALSE otherwise;
1415 * Return a pointer that holds the info. Null when there is an error.
1416 */
1417 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001418eval_for_line(
1419 char_u *arg,
1420 int *errp,
1421 char_u **nextcmdp,
1422 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001423{
Bram Moolenaar33570922005-01-25 22:26:29 +00001424 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001425 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T tv;
1427 list_T *l;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001428 evalarg_T evalarg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001430 CLEAR_FIELD(evalarg);
1431 evalarg.eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001432 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001433
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001434 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001435 if (fi == NULL)
1436 return NULL;
1437
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001438 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001439 if (expr == NULL)
1440 return fi;
1441
1442 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001443 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001444 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001446 return fi;
1447 }
1448
1449 if (skip)
1450 ++emsg_skip;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001451 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, &evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001452 {
1453 *errp = FALSE;
1454 if (!skip)
1455 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001456 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001457 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001458 l = tv.vval.v_list;
1459 if (l == NULL)
1460 {
1461 // a null list is like an empty list: do nothing
1462 clear_tv(&tv);
1463 }
1464 else
1465 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001467 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001469 // No need to increment the refcount, it's already set for
1470 // the list being used in "tv".
1471 fi->fi_list = l;
1472 list_add_watch(l, &fi->fi_lw);
1473 fi->fi_lw.lw_item = l->lv_first;
1474 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001475 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001476 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001477 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001478 fi->fi_bi = 0;
1479 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001480 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001481 typval_T btv;
1482
1483 // Make a copy, so that the iteration still works when the
1484 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001486 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001487 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001488 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001489 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001490 else
1491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001492 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001493 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001494 }
1495 }
1496 }
1497 if (skip)
1498 --emsg_skip;
1499
1500 return fi;
1501}
1502
1503/*
1504 * Use the first item in a ":for" list. Advance to the next.
1505 * Assign the values to the variable (list). "arg" points to the first one.
1506 * Return TRUE when a valid item was found, FALSE when at end of list or
1507 * something wrong.
1508 */
1509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001510next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001511{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001512 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001513 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001514 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1515 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001516 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001518 if (fi->fi_blob != NULL)
1519 {
1520 typval_T tv;
1521
1522 if (fi->fi_bi >= blob_len(fi->fi_blob))
1523 return FALSE;
1524 tv.v_type = VAR_NUMBER;
1525 tv.v_lock = VAR_FIXED;
1526 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1527 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001528 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001529 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001530 }
1531
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001532 item = fi->fi_lw.lw_item;
1533 if (item == NULL)
1534 result = FALSE;
1535 else
1536 {
1537 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001538 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001539 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001540 }
1541 return result;
1542}
1543
1544/*
1545 * Free the structure used to store info used by ":for".
1546 */
1547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001548free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001549{
Bram Moolenaar33570922005-01-25 22:26:29 +00001550 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001551
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001552 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001553 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001554 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001555 list_unref(fi->fi_list);
1556 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001557 if (fi != NULL && fi->fi_blob != NULL)
1558 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001559 vim_free(fi);
1560}
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001563set_context_for_expression(
1564 expand_T *xp,
1565 char_u *arg,
1566 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
1568 int got_eq = FALSE;
1569 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001572 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573 {
1574 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001575 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001577 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001578 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001579 {
1580 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001581 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001582 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001583 break;
1584 }
1585 return;
1586 }
1587 }
1588 else
1589 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1590 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 while ((xp->xp_pattern = vim_strpbrk(arg,
1592 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1593 {
1594 c = *xp->xp_pattern;
1595 if (c == '&')
1596 {
1597 c = xp->xp_pattern[1];
1598 if (c == '&')
1599 {
1600 ++xp->xp_pattern;
1601 xp->xp_context = cmdidx != CMD_let || got_eq
1602 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1603 }
1604 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001607 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1608 xp->xp_pattern += 2;
1609
1610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 else if (c == '$')
1613 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001614 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 xp->xp_context = EXPAND_ENV_VARS;
1616 }
1617 else if (c == '=')
1618 {
1619 got_eq = TRUE;
1620 xp->xp_context = EXPAND_EXPRESSION;
1621 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001622 else if (c == '#'
1623 && xp->xp_context == EXPAND_EXPRESSION)
1624 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001625 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001626 break;
1627 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001628 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 && xp->xp_context == EXPAND_FUNCTIONS
1630 && vim_strchr(xp->xp_pattern, '(') == NULL)
1631 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001632 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 break;
1634 }
1635 else if (cmdidx != CMD_let || got_eq)
1636 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001637 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
1639 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1640 if (c == '\\' && xp->xp_pattern[1] != NUL)
1641 ++xp->xp_pattern;
1642 xp->xp_context = EXPAND_NOTHING;
1643 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001644 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001646 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1648 /* skip */ ;
1649 xp->xp_context = EXPAND_NOTHING;
1650 }
1651 else if (c == '|')
1652 {
1653 if (xp->xp_pattern[1] == '|')
1654 {
1655 ++xp->xp_pattern;
1656 xp->xp_context = EXPAND_EXPRESSION;
1657 }
1658 else
1659 xp->xp_context = EXPAND_COMMANDS;
1660 }
1661 else
1662 xp->xp_context = EXPAND_EXPRESSION;
1663 }
1664 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001665 // Doesn't look like something valid, expand as an expression
1666 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 arg = xp->xp_pattern;
1669 if (*arg != NUL)
1670 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1671 /* skip */ ;
1672 }
1673 xp->xp_pattern = arg;
1674}
1675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001677 * Return TRUE if "pat" matches "text".
1678 * Does not use 'cpo' and always uses 'magic'.
1679 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001680 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001681pattern_match(char_u *pat, char_u *text, int ic)
1682{
1683 int matches = FALSE;
1684 char_u *save_cpo;
1685 regmatch_T regmatch;
1686
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001687 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001688 save_cpo = p_cpo;
1689 p_cpo = (char_u *)"";
1690 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1691 if (regmatch.regprog != NULL)
1692 {
1693 regmatch.rm_ic = ic;
1694 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1695 vim_regfree(regmatch.regprog);
1696 }
1697 p_cpo = save_cpo;
1698 return matches;
1699}
1700
1701/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001702 * Handle a name followed by "(". Both for just "name(arg)" and for
1703 * "expr->name(arg)".
1704 * Returns OK or FAIL.
1705 */
1706 static int
1707eval_func(
1708 char_u **arg, // points to "(", will be advanced
1709 char_u *name,
1710 int name_len,
1711 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001712 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001713 typval_T *basetv) // "expr" for "expr->name(arg)"
1714{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001715 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001716 char_u *s = name;
1717 int len = name_len;
1718 partial_T *partial;
1719 int ret = OK;
1720
1721 if (!evaluate)
1722 check_vars(s, len);
1723
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001724 // If "s" is the name of a variable of type VAR_FUNC
1725 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001726 s = deref_func_name(s, &len, &partial, !evaluate);
1727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001728 // Need to make a copy, in case evaluating the arguments makes
1729 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001730 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001731 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001732 ret = FAIL;
1733 else
1734 {
1735 funcexe_T funcexe;
1736
1737 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001738 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001739 funcexe.firstline = curwin->w_cursor.lnum;
1740 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001741 funcexe.evaluate = evaluate;
1742 funcexe.partial = partial;
1743 funcexe.basetv = basetv;
1744 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1745 }
1746 vim_free(s);
1747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001748 // If evaluate is FALSE rettv->v_type was not set in
1749 // get_func_tv, but it's needed in handle_subscript() to parse
1750 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001751 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1752 {
1753 rettv->vval.v_string = NULL;
1754 rettv->v_type = VAR_FUNC;
1755 }
1756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001757 // Stop the expression evaluation when immediately
1758 // aborting on error, or when an interrupt occurred or
1759 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001760 if (evaluate && aborting())
1761 {
1762 if (ret == OK)
1763 clear_tv(rettv);
1764 ret = FAIL;
1765 }
1766 return ret;
1767}
1768
1769/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001770 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1771 * and there is a next line, return the next line (skipping blanks) and set
1772 * "getnext".
1773 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1774 * "arg" must point somewhere inside a line, not at the start.
1775 */
1776 static char_u *
1777eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1778{
1779 *getnext = FALSE;
1780 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1781 && evalarg != NULL
1782 && evalarg->eval_cookie != NULL
1783 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
1784 && (*arg == '"' || *arg == '#')))
1785 && source_nextline(evalarg->eval_cookie) != NULL)
1786 {
1787 char_u *p = source_nextline(evalarg->eval_cookie);
1788
1789 if (p != NULL)
1790 {
1791 *getnext = TRUE;
1792 return skipwhite(p);
1793 }
1794 }
1795 return arg;
1796}
1797
1798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1802 */
1803
1804/*
1805 * Handle zero level expression.
1806 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001808 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001809 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 * Return OK or FAIL.
1811 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813eval0(
1814 char_u *arg,
1815 typval_T *rettv,
1816 char_u **nextcmd,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001817 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 int ret;
1820 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001821 int did_emsg_before = did_emsg;
1822 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001823 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001826 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001827 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 /*
1832 * Report the invalid expression unless the expression evaluation has
1833 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001834 * exception, or we already gave a more specific error.
1835 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001837 if (!aborting()
1838 && did_emsg == did_emsg_before
1839 && called_emsg == called_emsg_before
1840 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001841 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 ret = FAIL;
1843 }
1844 if (nextcmd != NULL)
1845 *nextcmd = check_nextcmd(p);
1846
1847 return ret;
1848}
1849
1850/*
1851 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001852 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 *
1854 * "arg" must point to the first non-white of the expression.
1855 * "arg" is advanced to the next non-white after the recognized expression.
1856 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001857 * Note: "rettv.v_lock" is not set.
1858 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 * Return OK or FAIL.
1860 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001861 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001862eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 /*
1865 * Get the first variable.
1866 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001867 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 return FAIL;
1869
1870 if ((*arg)[0] == '?')
1871 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001872 int result;
1873 typval_T var2;
1874 evalarg_T nested_evalarg;
1875 int orig_flags;
1876
1877 if (evalarg == NULL)
1878 {
1879 CLEAR_FIELD(nested_evalarg);
1880 orig_flags = 0;
1881 }
1882 else
1883 {
1884 nested_evalarg = *evalarg;
1885 orig_flags = evalarg->eval_flags;
1886 }
1887
1888 int evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001891 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001893 int error = FALSE;
1894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001895 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001898 if (error)
1899 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901
1902 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001903 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 */
1905 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001906 nested_evalarg.eval_flags = result ? orig_flags
1907 : orig_flags & ~EVAL_EVALUATE;
1908 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 return FAIL;
1910
1911 /*
1912 * Check for the ":".
1913 */
1914 if ((*arg)[0] != ':')
1915 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 return FAIL;
1920 }
1921
1922 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001923 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
1925 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001926 nested_evalarg.eval_flags = !result ? orig_flags
1927 : orig_flags & ~EVAL_EVALUATE;
1928 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
1930 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 return FAIL;
1933 }
1934 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937
1938 return OK;
1939}
1940
1941/*
1942 * Handle first level expression:
1943 * expr2 || expr2 || expr2 logical OR
1944 *
1945 * "arg" must point to the first non-white of the expression.
1946 * "arg" is advanced to the next non-white after the recognized expression.
1947 *
1948 * Return OK or FAIL.
1949 */
1950 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001951eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
Bram Moolenaar33570922005-01-25 22:26:29 +00001953 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 long result;
1955 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001956 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 /*
1959 * Get the first variable.
1960 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001961 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 return FAIL;
1963
1964 /*
1965 * Repeat until there is no following "||".
1966 */
1967 first = TRUE;
1968 result = FALSE;
1969 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1970 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001971 evalarg_T nested_evalarg;
1972 int evaluate;
1973 int orig_flags;
1974
1975 if (evalarg == NULL)
1976 {
1977 CLEAR_FIELD(nested_evalarg);
1978 orig_flags = 0;
1979 evaluate = FALSE;
1980 }
1981 else
1982 {
1983 nested_evalarg = *evalarg;
1984 orig_flags = evalarg->eval_flags;
1985 evaluate = orig_flags & EVAL_EVALUATE;
1986 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (evaluate && first)
1989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001990 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001993 if (error)
1994 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 first = FALSE;
1996 }
1997
1998 /*
1999 * Get the second variable.
2000 */
2001 *arg = skipwhite(*arg + 2);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002002 nested_evalarg.eval_flags = !result ? orig_flags
2003 : orig_flags & ~EVAL_EVALUATE;
2004 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 return FAIL;
2006
2007 /*
2008 * Compute the result.
2009 */
2010 if (evaluate && !result)
2011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002012 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002015 if (error)
2016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018 if (evaluate)
2019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 rettv->v_type = VAR_NUMBER;
2021 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023 }
2024
2025 return OK;
2026}
2027
2028/*
2029 * Handle second level expression:
2030 * expr3 && expr3 && expr3 logical AND
2031 *
2032 * "arg" must point to the first non-white of the expression.
2033 * "arg" is advanced to the next non-white after the recognized expression.
2034 *
2035 * Return OK or FAIL.
2036 */
2037 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002038eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
Bram Moolenaar33570922005-01-25 22:26:29 +00002040 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 long result;
2042 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002043 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045 /*
2046 * Get the first variable.
2047 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002048 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 return FAIL;
2050
2051 /*
2052 * Repeat until there is no following "&&".
2053 */
2054 first = TRUE;
2055 result = TRUE;
2056 while ((*arg)[0] == '&' && (*arg)[1] == '&')
2057 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002058 evalarg_T nested_evalarg;
2059 int orig_flags;
2060 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002061
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002062 if (evalarg == NULL)
2063 {
2064 CLEAR_FIELD(nested_evalarg);
2065 orig_flags = 0;
2066 evaluate = FALSE;
2067 }
2068 else
2069 {
2070 nested_evalarg = *evalarg;
2071 orig_flags = evalarg->eval_flags;
2072 evaluate = orig_flags & EVAL_EVALUATE;
2073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (evaluate && first)
2075 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002076 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002079 if (error)
2080 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 first = FALSE;
2082 }
2083
2084 /*
2085 * Get the second variable.
2086 */
2087 *arg = skipwhite(*arg + 2);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088 nested_evalarg.eval_flags = result ? orig_flags
2089 : orig_flags & ~EVAL_EVALUATE;
2090 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 return FAIL;
2092
2093 /*
2094 * Compute the result.
2095 */
2096 if (evaluate && result)
2097 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002098 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002101 if (error)
2102 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
2104 if (evaluate)
2105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 rettv->v_type = VAR_NUMBER;
2107 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
2109 }
2110
2111 return OK;
2112}
2113
2114/*
2115 * Handle third level expression:
2116 * var1 == var2
2117 * var1 =~ var2
2118 * var1 != var2
2119 * var1 !~ var2
2120 * var1 > var2
2121 * var1 >= var2
2122 * var1 < var2
2123 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002124 * var1 is var2
2125 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 *
2127 * "arg" must point to the first non-white of the expression.
2128 * "arg" is advanced to the next non-white after the recognized expression.
2129 *
2130 * Return OK or FAIL.
2131 */
2132 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
Bram Moolenaar33570922005-01-25 22:26:29 +00002135 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 char_u *p;
2137 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002138 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 /*
2143 * Get the first variable.
2144 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002145 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 return FAIL;
2147
2148 p = *arg;
2149 switch (p[0])
2150 {
2151 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002152 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002154 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 break;
2156 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002157 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002159 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 break;
2161 case '>': if (p[1] != '=')
2162 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002163 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 len = 1;
2165 }
2166 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002167 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 break;
2169 case '<': if (p[1] != '=')
2170 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002171 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 len = 1;
2173 }
2174 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002175 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002177 case 'i': if (p[1] == 's')
2178 {
2179 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2180 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002181 i = p[len];
2182 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002183 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002184 }
2185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187
2188 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002189 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002191 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002193 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 if (p[len] == '?')
2195 {
2196 ic = TRUE;
2197 ++len;
2198 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002199 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 else if (p[len] == '#')
2201 {
2202 ic = FALSE;
2203 ++len;
2204 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002205 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 else
2207 ic = p_ic;
2208
2209 /*
2210 * Get the second variable.
2211 */
2212 *arg = skipwhite(p + len);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002213 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 return FAIL;
2217 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002218 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002219 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002220 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002221
2222 clear_tv(&var2);
2223 return ret;
2224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 }
2226
2227 return OK;
2228}
2229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 void
2231eval_addblob(typval_T *tv1, typval_T *tv2)
2232{
2233 blob_T *b1 = tv1->vval.v_blob;
2234 blob_T *b2 = tv2->vval.v_blob;
2235 blob_T *b = blob_alloc();
2236 int i;
2237
2238 if (b != NULL)
2239 {
2240 for (i = 0; i < blob_len(b1); i++)
2241 ga_append(&b->bv_ga, blob_get(b1, i));
2242 for (i = 0; i < blob_len(b2); i++)
2243 ga_append(&b->bv_ga, blob_get(b2, i));
2244
2245 clear_tv(tv1);
2246 rettv_blob_set(tv1, b);
2247 }
2248}
2249
2250 int
2251eval_addlist(typval_T *tv1, typval_T *tv2)
2252{
2253 typval_T var3;
2254
2255 // concatenate Lists
2256 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2257 {
2258 clear_tv(tv1);
2259 clear_tv(tv2);
2260 return FAIL;
2261 }
2262 clear_tv(tv1);
2263 *tv1 = var3;
2264 return OK;
2265}
2266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267/*
2268 * Handle fourth level expression:
2269 * + number addition
2270 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002271 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002272 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 *
2274 * "arg" must point to the first non-white of the expression.
2275 * "arg" is advanced to the next non-white after the recognized expression.
2276 *
2277 * Return OK or FAIL.
2278 */
2279 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002282 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284 /*
2285 * Get the first variable.
2286 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 return FAIL;
2289
2290 /*
2291 * Repeat computing, until no '+', '-' or '.' is following.
2292 */
2293 for (;;)
2294 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002295 int getnext;
2296 char_u *p;
2297 int op;
2298 int concat;
2299 typval_T var2;
2300
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002301 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002302 p = eval_next_non_blank(*arg, evalarg, &getnext);
2303 op = *p;
2304 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002305 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307 if (getnext)
2308 *arg = skipwhite(getsourceline(0, evalarg->eval_cookie, 0, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002310 if ((op != '+' || (rettv->v_type != VAR_LIST
2311 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002312#ifdef FEAT_FLOAT
2313 && (op == '.' || rettv->v_type != VAR_FLOAT)
2314#endif
2315 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002317 // For "list + ...", an illegal use of the first operand as
2318 // a number cannot be determined before evaluating the 2nd
2319 // operand: if this is also a list, all is ok.
2320 // For "something . ...", "something - ..." or "non-list + ...",
2321 // we know that the first operand needs to be a string or number
2322 // without evaluating the 2nd operand. So check before to avoid
2323 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002325 {
2326 clear_tv(rettv);
2327 return FAIL;
2328 }
2329 }
2330
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 /*
2332 * Get the second variable.
2333 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002334 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2335 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 return FAIL;
2341 }
2342
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002343 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 /*
2346 * Compute the result.
2347 */
2348 if (op == '.')
2349 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002350 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2351 char_u *s1 = tv_get_string_buf(rettv, buf1);
2352 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002354 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 {
2356 clear_tv(rettv);
2357 clear_tv(&var2);
2358 return FAIL;
2359 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 clear_tv(rettv);
2362 rettv->v_type = VAR_STRING;
2363 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002365 else if (op == '+' && rettv->v_type == VAR_BLOB
2366 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002368 else if (op == '+' && rettv->v_type == VAR_LIST
2369 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002370 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002372 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 else
2375 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002376 int error = FALSE;
2377 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002378#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002379 float_T f1 = 0, f2 = 0;
2380
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002381 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002383 f1 = rettv->vval.v_float;
2384 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002385 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002386 else
2387#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002389 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002390 if (error)
2391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002392 // This can only happen for "list + non-list". For
2393 // "non-list + ..." or "something - ...", we returned
2394 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002395 clear_tv(rettv);
2396 return FAIL;
2397 }
2398#ifdef FEAT_FLOAT
2399 if (var2.v_type == VAR_FLOAT)
2400 f1 = n1;
2401#endif
2402 }
2403#ifdef FEAT_FLOAT
2404 if (var2.v_type == VAR_FLOAT)
2405 {
2406 f2 = var2.vval.v_float;
2407 n2 = 0;
2408 }
2409 else
2410#endif
2411 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002412 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002413 if (error)
2414 {
2415 clear_tv(rettv);
2416 clear_tv(&var2);
2417 return FAIL;
2418 }
2419#ifdef FEAT_FLOAT
2420 if (rettv->v_type == VAR_FLOAT)
2421 f2 = n2;
2422#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002425
2426#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002427 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002428 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2429 {
2430 if (op == '+')
2431 f1 = f1 + f2;
2432 else
2433 f1 = f1 - f2;
2434 rettv->v_type = VAR_FLOAT;
2435 rettv->vval.v_float = f1;
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002438#endif
2439 {
2440 if (op == '+')
2441 n1 = n1 + n2;
2442 else
2443 n1 = n1 - n2;
2444 rettv->v_type = VAR_NUMBER;
2445 rettv->vval.v_number = n1;
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450 }
2451 return OK;
2452}
2453
2454/*
2455 * Handle fifth level expression:
2456 * * number multiplication
2457 * / number division
2458 * % number modulo
2459 *
2460 * "arg" must point to the first non-white of the expression.
2461 * "arg" is advanced to the next non-white after the recognized expression.
2462 *
2463 * Return OK or FAIL.
2464 */
2465 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002466eval6(
2467 char_u **arg,
2468 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002470 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
Bram Moolenaar33570922005-01-25 22:26:29 +00002472 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002474 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002475#ifdef FEAT_FLOAT
2476 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002477 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002478#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /*
2482 * Get the first variable.
2483 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 return FAIL;
2486
2487 /*
2488 * Repeat computing, until no '*', '/' or '%' is following.
2489 */
2490 for (;;)
2491 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492 int evaluate = evalarg == NULL ? 0
2493 : (evalarg->eval_flags & EVAL_EVALUATE);
2494 int getnext;
2495
2496 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002497 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002499 if (getnext)
2500 *arg = skipwhite(getsourceline(0, evalarg->eval_cookie, 0, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002502 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002504#ifdef FEAT_FLOAT
2505 if (rettv->v_type == VAR_FLOAT)
2506 {
2507 f1 = rettv->vval.v_float;
2508 use_float = TRUE;
2509 n1 = 0;
2510 }
2511 else
2512#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002513 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002515 if (error)
2516 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
2518 else
2519 n1 = 0;
2520
2521 /*
2522 * Get the second variable.
2523 */
2524 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002525 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 return FAIL;
2527
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002530#ifdef FEAT_FLOAT
2531 if (var2.v_type == VAR_FLOAT)
2532 {
2533 if (!use_float)
2534 {
2535 f1 = n1;
2536 use_float = TRUE;
2537 }
2538 f2 = var2.vval.v_float;
2539 n2 = 0;
2540 }
2541 else
2542#endif
2543 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002544 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002545 clear_tv(&var2);
2546 if (error)
2547 return FAIL;
2548#ifdef FEAT_FLOAT
2549 if (use_float)
2550 f2 = n2;
2551#endif
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 /*
2555 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002556 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002558#ifdef FEAT_FLOAT
2559 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002561 if (op == '*')
2562 f1 = f1 * f2;
2563 else if (op == '/')
2564 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002565# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002566 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002567 if (f2 == 0.0)
2568 {
2569 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002570 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002571 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002572 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002573 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002574 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002575 }
2576 else
2577 f1 = f1 / f2;
2578# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002579 // We rely on the floating point library to handle divide
2580 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002581 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002582# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002585 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002587 return FAIL;
2588 }
2589 rettv->v_type = VAR_FLOAT;
2590 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002595 if (op == '*')
2596 n1 = n1 * n2;
2597 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002598 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002600 n1 = num_modulus(n1, n2);
2601
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002602 rettv->v_type = VAR_NUMBER;
2603 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606 }
2607
2608 return OK;
2609}
2610
2611/*
2612 * Handle sixth level expression:
2613 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002614 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002615 * "string" string constant
2616 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 * &option-name option value
2618 * @r register contents
2619 * identifier variable value
2620 * function() function call
2621 * $VAR environment variable
2622 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002623 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002625 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002626 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 *
2628 * Also handle:
2629 * ! in front logical NOT
2630 * - in front unary minus
2631 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 * trailing [] subscript in String or List
2633 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002634 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 *
2636 * "arg" must point to the first non-white of the expression.
2637 * "arg" is advanced to the next non-white after the recognized expression.
2638 *
2639 * Return OK or FAIL.
2640 */
2641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002642eval7(
2643 char_u **arg,
2644 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002645 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2649 int evaluate = evalarg != NULL
2650 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 int len;
2652 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 char_u *start_leader, *end_leader;
2654 int ret = OK;
2655 char_u *alias;
2656
2657 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002659 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
2663 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002664 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 */
2666 start_leader = *arg;
2667 while (**arg == '!' || **arg == '-' || **arg == '+')
2668 *arg = skipwhite(*arg + 1);
2669 end_leader = *arg;
2670
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002671 if (**arg == '.' && (!isdigit(*(*arg + 1))
2672#ifdef FEAT_FLOAT
2673 || current_sctx.sc_version < 2
2674#endif
2675 ))
2676 {
2677 semsg(_(e_invexpr2), *arg);
2678 ++*arg;
2679 return FAIL;
2680 }
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 switch (**arg)
2683 {
2684 /*
2685 * Number constant.
2686 */
2687 case '0':
2688 case '1':
2689 case '2':
2690 case '3':
2691 case '4':
2692 case '5':
2693 case '6':
2694 case '7':
2695 case '8':
2696 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 break;
2699
2700 /*
2701 * String constant: "string".
2702 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 break;
2705
2706 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002709 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002710 break;
2711
2712 /*
2713 * List: [expr, expr]
2714 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002715 case '[': ret = get_list_tv(arg, rettv, flags, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 break;
2717
2718 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002719 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002720 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002721 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002722 {
2723 ++*arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002724 ret = eval_dict(arg, rettv, flags, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002725 }
2726 else
2727 ret = NOTDONE;
2728 break;
2729
2730 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002731 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002732 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002734 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2735 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002736 ret = eval_dict(arg, rettv, flags, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 break;
2738
2739 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002740 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002742 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 break;
2744
2745 /*
2746 * Environment variable: $VAR.
2747 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002748 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 break;
2750
2751 /*
2752 * Register contents: @r.
2753 */
2754 case '@': ++*arg;
2755 if (evaluate)
2756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002757 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002758 rettv->vval.v_string = get_reg_contents(**arg,
2759 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 if (**arg != NUL)
2762 ++*arg;
2763 break;
2764
2765 /*
2766 * nested expression: (expression).
2767 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002768 case '(': {
2769 *arg = skipwhite(*arg + 1);
2770 ret = eval1(arg, rettv, evalarg); // recursive!
2771 if (**arg == ')')
2772 ++*arg;
2773 else if (ret == OK)
2774 {
2775 emsg(_(e_missing_close));
2776 clear_tv(rettv);
2777 ret = FAIL;
2778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 break;
2781
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 break;
2784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785
2786 if (ret == NOTDONE)
2787 {
2788 /*
2789 * Must be a variable or function name.
2790 * Can also be a curly-braces kind of name: {expr}.
2791 */
2792 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002793 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 if (alias != NULL)
2795 s = alias;
2796
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002797 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 ret = FAIL;
2799 else
2800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002801 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002802 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002803 else if (flags & EVAL_CONSTANT)
2804 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002806 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002807 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002808 {
2809 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002810 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002813 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 }
2815
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 *arg = skipwhite(*arg);
2817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2819 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002820 if (ret == OK)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002821 ret = handle_subscript(arg, rettv, flags, TRUE,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002822 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
2824 /*
2825 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2826 */
2827 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002828 ret = eval7_leader(rettv, start_leader, &end_leader);
2829 return ret;
2830}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002831
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002832/*
2833 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2834 * Adjusts "end_leaderp" until it is at "start_leader".
2835 */
2836 static int
2837eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2838{
2839 char_u *end_leader = *end_leaderp;
2840 int ret = OK;
2841 int error = FALSE;
2842 varnumber_T val = 0;
2843#ifdef FEAT_FLOAT
2844 float_T f = 0.0;
2845
2846 if (rettv->v_type == VAR_FLOAT)
2847 f = rettv->vval.v_float;
2848 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002849#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002850 val = tv_get_number_chk(rettv, &error);
2851 if (error)
2852 {
2853 clear_tv(rettv);
2854 ret = FAIL;
2855 }
2856 else
2857 {
2858 while (end_leader > start_leader)
2859 {
2860 --end_leader;
2861 if (*end_leader == '!')
2862 {
2863#ifdef FEAT_FLOAT
2864 if (rettv->v_type == VAR_FLOAT)
2865 f = !f;
2866 else
2867#endif
2868 val = !val;
2869 }
2870 else if (*end_leader == '-')
2871 {
2872#ifdef FEAT_FLOAT
2873 if (rettv->v_type == VAR_FLOAT)
2874 f = -f;
2875 else
2876#endif
2877 val = -val;
2878 }
2879 }
2880#ifdef FEAT_FLOAT
2881 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002883 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002884 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002886 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002887#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002888 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002889 clear_tv(rettv);
2890 rettv->v_type = VAR_NUMBER;
2891 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002894 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 return ret;
2896}
2897
2898/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002899 * Call the function referred to in "rettv".
2900 */
2901 static int
2902call_func_rettv(
2903 char_u **arg,
2904 typval_T *rettv,
2905 int evaluate,
2906 dict_T *selfdict,
2907 typval_T *basetv)
2908{
2909 partial_T *pt = NULL;
2910 funcexe_T funcexe;
2911 typval_T functv;
2912 char_u *s;
2913 int ret;
2914
2915 // need to copy the funcref so that we can clear rettv
2916 if (evaluate)
2917 {
2918 functv = *rettv;
2919 rettv->v_type = VAR_UNKNOWN;
2920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002921 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002922 if (functv.v_type == VAR_PARTIAL)
2923 {
2924 pt = functv.vval.v_partial;
2925 s = partial_name(pt);
2926 }
2927 else
2928 s = functv.vval.v_string;
2929 }
2930 else
2931 s = (char_u *)"";
2932
Bram Moolenaara80faa82020-04-12 19:37:17 +02002933 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002934 funcexe.firstline = curwin->w_cursor.lnum;
2935 funcexe.lastline = curwin->w_cursor.lnum;
2936 funcexe.evaluate = evaluate;
2937 funcexe.partial = pt;
2938 funcexe.selfdict = selfdict;
2939 funcexe.basetv = basetv;
2940 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002942 // Clear the funcref afterwards, so that deleting it while
2943 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002944 if (evaluate)
2945 clear_tv(&functv);
2946
2947 return ret;
2948}
2949
2950/*
2951 * Evaluate "->method()".
2952 * "*arg" points to the '-'.
2953 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2954 */
2955 static int
2956eval_lambda(
2957 char_u **arg,
2958 typval_T *rettv,
2959 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002960 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002961{
2962 typval_T base = *rettv;
2963 int ret;
2964
2965 // Skip over the ->.
2966 *arg += 2;
2967 rettv->v_type = VAR_UNKNOWN;
2968
2969 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002970 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002971 return FAIL;
2972 else if (**arg != '(')
2973 {
2974 if (verbose)
2975 {
2976 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002977 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002978 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002980 }
2981 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002982 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002983 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002984 else
2985 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2986
2987 // Clear the funcref afterwards, so that deleting it while
2988 // evaluating the arguments is possible (see test55).
2989 if (evaluate)
2990 clear_tv(&base);
2991
2992 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002993}
2994
2995/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002996 * Evaluate "->method()".
2997 * "*arg" points to the '-'.
2998 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2999 */
3000 static int
3001eval_method(
3002 char_u **arg,
3003 typval_T *rettv,
3004 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003005 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003006{
3007 char_u *name;
3008 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003009 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003010 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003011 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003012
3013 // Skip over the ->.
3014 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003015 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003016
Bram Moolenaarac92e252019-08-03 21:58:38 +02003017 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003018 len = get_name_len(arg, &alias, evaluate, TRUE);
3019 if (alias != NULL)
3020 name = alias;
3021
3022 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003023 {
3024 if (verbose)
3025 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003026 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003027 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003028 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003029 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003030 if (**arg != '(')
3031 {
3032 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003034 ret = FAIL;
3035 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003036 else if (VIM_ISWHITE((*arg)[-1]))
3037 {
3038 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003039 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003040 ret = FAIL;
3041 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003042 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003043 ret = eval_func(arg, name, len, rettv,
3044 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003045 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003046
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003047 // Clear the funcref afterwards, so that deleting it while
3048 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003049 if (evaluate)
3050 clear_tv(&base);
3051
Bram Moolenaarac92e252019-08-03 21:58:38 +02003052 return ret;
3053}
3054
3055/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003056 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3057 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003058 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3059 */
3060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003061eval_index(
3062 char_u **arg,
3063 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003064 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003065 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003066{
Bram Moolenaar32e35112020-05-14 22:41:15 +02003067 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003068 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003069 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003070 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003071 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003072 long len = -1;
3073 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003074 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003075 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003076
Bram Moolenaara03f2332016-02-06 18:09:59 +01003077 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003078 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003079 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003080 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003081 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003082 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003083 return FAIL;
3084 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003085#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003086 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003087 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003088 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003089#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003090 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003091 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003092 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003093 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003094 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003095 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003096 return FAIL;
3097 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003098 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003100 if (evaluate)
3101 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003102 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003103
3104 case VAR_STRING:
3105 case VAR_NUMBER:
3106 case VAR_LIST:
3107 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003108 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003109 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003110 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003111
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003112 init_tv(&var1);
3113 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003114 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003115 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003116 /*
3117 * dict.name
3118 */
3119 key = *arg + 1;
3120 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3121 ;
3122 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003123 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003124 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003125 }
3126 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003127 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003128 evalarg_T evalarg;
3129
3130 CLEAR_FIELD(evalarg);
3131 evalarg.eval_flags = flags;
3132
Bram Moolenaar8c711452005-01-14 21:53:12 +00003133 /*
3134 * something[idx]
3135 *
3136 * Get the (first) variable from inside the [].
3137 */
3138 *arg = skipwhite(*arg + 1);
3139 if (**arg == ':')
3140 empty1 = TRUE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003141 else if (eval1(arg, &var1, &evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003142 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003143 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003145 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003146 clear_tv(&var1);
3147 return FAIL;
3148 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003149
3150 /*
3151 * Get the second variable from inside the [:].
3152 */
3153 if (**arg == ':')
3154 {
3155 range = TRUE;
3156 *arg = skipwhite(*arg + 1);
3157 if (**arg == ']')
3158 empty2 = TRUE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003159 else if (eval1(arg, &var2, &evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003161 if (!empty1)
3162 clear_tv(&var1);
3163 return FAIL;
3164 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003165 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003167 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003168 if (!empty1)
3169 clear_tv(&var1);
3170 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003171 return FAIL;
3172 }
3173 }
3174
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003175 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003176 if (**arg != ']')
3177 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003178 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003179 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003180 clear_tv(&var1);
3181 if (range)
3182 clear_tv(&var2);
3183 return FAIL;
3184 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003186 }
3187
3188 if (evaluate)
3189 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003190 n1 = 0;
3191 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003192 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003193 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003194 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003195 }
3196 if (range)
3197 {
3198 if (empty2)
3199 n2 = -1;
3200 else
3201 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003202 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003203 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003204 }
3205 }
3206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003207 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003208 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003209 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003210 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003212 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003213 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003214 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003215 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003216 case VAR_SPECIAL:
3217 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003218 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003219 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003220
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003221 case VAR_NUMBER:
3222 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003223 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003224 len = (long)STRLEN(s);
3225 if (range)
3226 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003227 // The resulting variable is a substring. If the indexes
3228 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003229 if (n1 < 0)
3230 {
3231 n1 = len + n1;
3232 if (n1 < 0)
3233 n1 = 0;
3234 }
3235 if (n2 < 0)
3236 n2 = len + n2;
3237 else if (n2 >= len)
3238 n2 = len;
3239 if (n1 >= len || n2 < 0 || n1 > n2)
3240 s = NULL;
3241 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003242 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003243 }
3244 else
3245 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003246 // The resulting variable is a string of a single
3247 // character. If the index is too big or negative the
3248 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003249 if (n1 >= len || n1 < 0)
3250 s = NULL;
3251 else
3252 s = vim_strnsave(s + n1, 1);
3253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003254 clear_tv(rettv);
3255 rettv->v_type = VAR_STRING;
3256 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003257 break;
3258
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003259 case VAR_BLOB:
3260 len = blob_len(rettv->vval.v_blob);
3261 if (range)
3262 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003263 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003264 // are out of range the result is empty.
3265 if (n1 < 0)
3266 {
3267 n1 = len + n1;
3268 if (n1 < 0)
3269 n1 = 0;
3270 }
3271 if (n2 < 0)
3272 n2 = len + n2;
3273 else if (n2 >= len)
3274 n2 = len - 1;
3275 if (n1 >= len || n2 < 0 || n1 > n2)
3276 {
3277 clear_tv(rettv);
3278 rettv->v_type = VAR_BLOB;
3279 rettv->vval.v_blob = NULL;
3280 }
3281 else
3282 {
3283 blob_T *blob = blob_alloc();
3284
3285 if (blob != NULL)
3286 {
3287 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3288 {
3289 blob_free(blob);
3290 return FAIL;
3291 }
3292 blob->bv_ga.ga_len = n2 - n1 + 1;
3293 for (i = n1; i <= n2; i++)
3294 blob_set(blob, i - n1,
3295 blob_get(rettv->vval.v_blob, i));
3296
3297 clear_tv(rettv);
3298 rettv_blob_set(rettv, blob);
3299 }
3300 }
3301 }
3302 else
3303 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003304 // The resulting variable is a byte value.
3305 // If the index is too big or negative that is an error.
3306 if (n1 < 0)
3307 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003308 if (n1 < len && n1 >= 0)
3309 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003310 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003311
3312 clear_tv(rettv);
3313 rettv->v_type = VAR_NUMBER;
3314 rettv->vval.v_number = v;
3315 }
3316 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003317 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003318 }
3319 break;
3320
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003321 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003322 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003323 if (n1 < 0)
3324 n1 = len + n1;
3325 if (!empty1 && (n1 < 0 || n1 >= len))
3326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003327 // For a range we allow invalid values and return an empty
3328 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003329 if (!range)
3330 {
3331 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003332 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003333 return FAIL;
3334 }
3335 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003336 }
3337 if (range)
3338 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003339 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003340
3341 if (n2 < 0)
3342 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003343 else if (n2 >= len)
3344 n2 = len - 1;
3345 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003346 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003347 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003348 if (l == NULL)
3349 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003350 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003351 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003352 }
3353 else
3354 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003355 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003356 clear_tv(rettv);
3357 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003358 }
3359 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003360
3361 case VAR_DICT:
3362 if (range)
3363 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003364 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003365 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003366 if (len == -1)
3367 clear_tv(&var1);
3368 return FAIL;
3369 }
3370 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003371 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003372
3373 if (len == -1)
3374 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003375 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003376 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003377 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003378 clear_tv(&var1);
3379 return FAIL;
3380 }
3381 }
3382
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003383 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003384
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003385 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003386 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003387 if (len == -1)
3388 clear_tv(&var1);
3389 if (item == NULL)
3390 return FAIL;
3391
3392 copy_tv(&item->di_tv, &var1);
3393 clear_tv(rettv);
3394 *rettv = var1;
3395 }
3396 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003397 }
3398 }
3399
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003400 return OK;
3401}
3402
3403/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003404 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003405 */
3406 char_u *
3407partial_name(partial_T *pt)
3408{
3409 if (pt->pt_name != NULL)
3410 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003411 if (pt->pt_func != NULL)
3412 return pt->pt_func->uf_name;
3413 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003414}
3415
Bram Moolenaarddecc252016-04-06 22:59:37 +02003416 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003417partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003418{
3419 int i;
3420
3421 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003422 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003423 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003424 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003425 if (pt->pt_name != NULL)
3426 {
3427 func_unref(pt->pt_name);
3428 vim_free(pt->pt_name);
3429 }
3430 else
3431 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003432
3433 if (pt->pt_funcstack != NULL)
3434 {
3435 // Decrease the reference count for the context of a closure. If down
3436 // to zero free it and clear the variables on the stack.
3437 if (--pt->pt_funcstack->fs_refcount == 0)
3438 {
3439 garray_T *gap = &pt->pt_funcstack->fs_ga;
3440 typval_T *stack = gap->ga_data;
3441
3442 for (i = 0; i < gap->ga_len; ++i)
3443 clear_tv(stack + i);
3444 ga_clear(gap);
3445 vim_free(pt->pt_funcstack);
3446 }
3447 pt->pt_funcstack = NULL;
3448 }
3449
Bram Moolenaarddecc252016-04-06 22:59:37 +02003450 vim_free(pt);
3451}
3452
3453/*
3454 * Unreference a closure: decrement the reference count and free it when it
3455 * becomes zero.
3456 */
3457 void
3458partial_unref(partial_T *pt)
3459{
3460 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003461 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003462}
3463
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003464/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003465 * Return the next (unique) copy ID.
3466 * Used for serializing nested structures.
3467 */
3468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003469get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003470{
3471 current_copyID += COPYID_INC;
3472 return current_copyID;
3473}
3474
3475/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003476 * Garbage collection for lists and dictionaries.
3477 *
3478 * We use reference counts to be able to free most items right away when they
3479 * are no longer used. But for composite items it's possible that it becomes
3480 * unused while the reference count is > 0: When there is a recursive
3481 * reference. Example:
3482 * :let l = [1, 2, 3]
3483 * :let d = {9: l}
3484 * :let l[1] = d
3485 *
3486 * Since this is quite unusual we handle this with garbage collection: every
3487 * once in a while find out which lists and dicts are not referenced from any
3488 * variable.
3489 *
3490 * Here is a good reference text about garbage collection (refers to Python
3491 * but it applies to all reference-counting mechanisms):
3492 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003493 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003494
3495/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003496 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003497 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003498 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003499 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003500 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003501garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003502{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003503 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003504 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003505 buf_T *buf;
3506 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003507 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003508 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003509
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003510 if (!testing)
3511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003512 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003513 want_garbage_collect = FALSE;
3514 may_garbage_collect = FALSE;
3515 garbage_collect_at_exit = FALSE;
3516 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003517
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003518 // The execution stack can grow big, limit the size.
3519 if (exestack.ga_maxlen - exestack.ga_len > 500)
3520 {
3521 size_t new_len;
3522 char_u *pp;
3523 int n;
3524
3525 // Keep 150% of the current size, with a minimum of the growth size.
3526 n = exestack.ga_len / 2;
3527 if (n < exestack.ga_growsize)
3528 n = exestack.ga_growsize;
3529
3530 // Don't make it bigger though.
3531 if (exestack.ga_len + n < exestack.ga_maxlen)
3532 {
3533 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3534 pp = vim_realloc(exestack.ga_data, new_len);
3535 if (pp == NULL)
3536 return FAIL;
3537 exestack.ga_maxlen = exestack.ga_len + n;
3538 exestack.ga_data = pp;
3539 }
3540 }
3541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003542 // We advance by two because we add one for items referenced through
3543 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003544 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003545
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003546 /*
3547 * 1. Go through all accessible variables and mark all lists and dicts
3548 * with copyID.
3549 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003551 // Don't free variables in the previous_funccal list unless they are only
3552 // referenced through previous_funccal. This must be first, because if
3553 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003556 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003557 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003559 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003560 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003561 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3562 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003563
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003564 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003565 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003566 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3567 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003568 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003569 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3570 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003571#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003572 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003573 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3574 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003575 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003576 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003577 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3578 NULL, NULL);
3579#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003581 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003582 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003583 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3584 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003585 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003586 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003588 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003589 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003591 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003592 abort = abort || set_ref_in_functions(copyID);
3593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003594 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003595 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003597 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003598 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003599
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003600 // callbacks in buffers
3601 abort = abort || set_ref_in_buffers(copyID);
3602
Bram Moolenaar1dced572012-04-05 16:54:08 +02003603#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003604 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003605#endif
3606
Bram Moolenaardb913952012-06-29 12:54:53 +02003607#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003608 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003609#endif
3610
3611#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003612 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003613#endif
3614
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003615#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003616 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003617 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003618#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003619#ifdef FEAT_NETBEANS_INTG
3620 abort = abort || set_ref_in_nb_channel(copyID);
3621#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003622
Bram Moolenaare3188e22016-05-31 21:13:04 +02003623#ifdef FEAT_TIMERS
3624 abort = abort || set_ref_in_timer(copyID);
3625#endif
3626
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003627#ifdef FEAT_QUICKFIX
3628 abort = abort || set_ref_in_quickfix(copyID);
3629#endif
3630
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003631#ifdef FEAT_TERMINAL
3632 abort = abort || set_ref_in_term(copyID);
3633#endif
3634
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003635#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003636 abort = abort || set_ref_in_popups(copyID);
3637#endif
3638
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003639 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003640 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003641 /*
3642 * 2. Free lists and dictionaries that are not referenced.
3643 */
3644 did_free = free_unref_items(copyID);
3645
3646 /*
3647 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003648 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003649 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003650 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003651 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003652 else if (p_verbose > 0)
3653 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003654 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003655 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003656
3657 return did_free;
3658}
3659
3660/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003661 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003662 */
3663 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003664free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003665{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003666 int did_free = FALSE;
3667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003668 // Let all "free" functions know that we are here. This means no
3669 // dictionaries, lists, channels or jobs are to be freed, because we will
3670 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003671 in_free_unref_items = TRUE;
3672
3673 /*
3674 * PASS 1: free the contents of the items. We don't free the items
3675 * themselves yet, so that it is possible to decrement refcount counters
3676 */
3677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003678 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003679 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003681 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003682 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003683
3684#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003685 // Go through the list of jobs and free items without the copyID. This
3686 // must happen before doing channels, because jobs refer to channels, but
3687 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003688 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003690 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003691 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3692#endif
3693
3694 /*
3695 * PASS 2: free the items themselves.
3696 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003697 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003698 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003699
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003700#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003701 // Go through the list of jobs and free items without the copyID. This
3702 // must happen before doing channels, because jobs refer to channels, but
3703 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003704 free_unused_jobs(copyID, COPYID_MASK);
3705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003706 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003707 free_unused_channels(copyID, COPYID_MASK);
3708#endif
3709
3710 in_free_unref_items = FALSE;
3711
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003712 return did_free;
3713}
3714
3715/*
3716 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003717 * "list_stack" is used to add lists to be marked. Can be NULL.
3718 *
3719 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003720 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003722set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003723{
3724 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003725 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003726 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003727 hashtab_T *cur_ht;
3728 ht_stack_T *ht_stack = NULL;
3729 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003730
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003731 cur_ht = ht;
3732 for (;;)
3733 {
3734 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003736 // Mark each item in the hashtab. If the item contains a hashtab
3737 // it is added to ht_stack, if it contains a list it is added to
3738 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003739 todo = (int)cur_ht->ht_used;
3740 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3741 if (!HASHITEM_EMPTY(hi))
3742 {
3743 --todo;
3744 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3745 &ht_stack, list_stack);
3746 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003747 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003748
3749 if (ht_stack == NULL)
3750 break;
3751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003753 cur_ht = ht_stack->ht;
3754 tempitem = ht_stack;
3755 ht_stack = ht_stack->prev;
3756 free(tempitem);
3757 }
3758
3759 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003760}
3761
3762/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003763 * Mark a dict and its items with "copyID".
3764 * Returns TRUE if setting references failed somehow.
3765 */
3766 int
3767set_ref_in_dict(dict_T *d, int copyID)
3768{
3769 if (d != NULL && d->dv_copyID != copyID)
3770 {
3771 d->dv_copyID = copyID;
3772 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3773 }
3774 return FALSE;
3775}
3776
3777/*
3778 * Mark a list and its items with "copyID".
3779 * Returns TRUE if setting references failed somehow.
3780 */
3781 int
3782set_ref_in_list(list_T *ll, int copyID)
3783{
3784 if (ll != NULL && ll->lv_copyID != copyID)
3785 {
3786 ll->lv_copyID = copyID;
3787 return set_ref_in_list_items(ll, copyID, NULL);
3788 }
3789 return FALSE;
3790}
3791
3792/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003793 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003794 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3795 *
3796 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003797 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003798 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003799set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003800{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003801 listitem_T *li;
3802 int abort = FALSE;
3803 list_T *cur_l;
3804 list_stack_T *list_stack = NULL;
3805 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003806
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003807 cur_l = l;
3808 for (;;)
3809 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003810 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003811 // Mark each item in the list. If the item contains a hashtab
3812 // it is added to ht_stack, if it contains a list it is added to
3813 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003814 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
3815 abort = abort || set_ref_in_item(&li->li_tv, copyID,
3816 ht_stack, &list_stack);
3817 if (list_stack == NULL)
3818 break;
3819
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003820 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003821 cur_l = list_stack->list;
3822 tempitem = list_stack;
3823 list_stack = list_stack->prev;
3824 free(tempitem);
3825 }
3826
3827 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003828}
3829
3830/*
3831 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003832 * "list_stack" is used to add lists to be marked. Can be NULL.
3833 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3834 *
3835 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003836 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003838set_ref_in_item(
3839 typval_T *tv,
3840 int copyID,
3841 ht_stack_T **ht_stack,
3842 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003843{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003844 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00003845
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003846 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003847 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003848 dict_T *dd = tv->vval.v_dict;
3849
Bram Moolenaara03f2332016-02-06 18:09:59 +01003850 if (dd != NULL && dd->dv_copyID != copyID)
3851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003852 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003853 dd->dv_copyID = copyID;
3854 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003855 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003856 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
3857 }
3858 else
3859 {
3860 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
3861 if (newitem == NULL)
3862 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003863 else
3864 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003865 newitem->ht = &dd->dv_hashtab;
3866 newitem->prev = *ht_stack;
3867 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003868 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003869 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003870 }
3871 }
3872 else if (tv->v_type == VAR_LIST)
3873 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003874 list_T *ll = tv->vval.v_list;
3875
Bram Moolenaara03f2332016-02-06 18:09:59 +01003876 if (ll != NULL && ll->lv_copyID != copyID)
3877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003878 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003879 ll->lv_copyID = copyID;
3880 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003881 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003882 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01003883 }
3884 else
3885 {
3886 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003887 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003888 if (newitem == NULL)
3889 abort = TRUE;
3890 else
3891 {
3892 newitem->list = ll;
3893 newitem->prev = *list_stack;
3894 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003895 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003896 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003897 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003898 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003899 else if (tv->v_type == VAR_FUNC)
3900 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003901 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003902 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003903 else if (tv->v_type == VAR_PARTIAL)
3904 {
3905 partial_T *pt = tv->vval.v_partial;
3906 int i;
3907
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003908 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003909 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003910 // Didn't see this partial yet.
3911 pt->pt_copyID = copyID;
3912
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003913 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003914
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003915 if (pt->pt_dict != NULL)
3916 {
3917 typval_T dtv;
3918
3919 dtv.v_type = VAR_DICT;
3920 dtv.vval.v_dict = pt->pt_dict;
3921 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3922 }
3923
3924 for (i = 0; i < pt->pt_argc; ++i)
3925 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
3926 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003927 if (pt->pt_funcstack != NULL)
3928 {
3929 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
3930
3931 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
3932 abort = abort || set_ref_in_item(stack + i, copyID,
3933 ht_stack, list_stack);
3934 }
3935
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003936 }
3937 }
3938#ifdef FEAT_JOB_CHANNEL
3939 else if (tv->v_type == VAR_JOB)
3940 {
3941 job_T *job = tv->vval.v_job;
3942 typval_T dtv;
3943
3944 if (job != NULL && job->jv_copyID != copyID)
3945 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02003946 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003947 if (job->jv_channel != NULL)
3948 {
3949 dtv.v_type = VAR_CHANNEL;
3950 dtv.vval.v_channel = job->jv_channel;
3951 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3952 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003953 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003954 {
3955 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003956 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003957 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3958 }
3959 }
3960 }
3961 else if (tv->v_type == VAR_CHANNEL)
3962 {
3963 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003964 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003965 typval_T dtv;
3966 jsonq_T *jq;
3967 cbq_T *cq;
3968
3969 if (ch != NULL && ch->ch_copyID != copyID)
3970 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02003971 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003972 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003973 {
3974 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
3975 jq = jq->jq_next)
3976 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
3977 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
3978 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003979 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003980 {
3981 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003982 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003983 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3984 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003985 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003986 {
3987 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003988 dtv.vval.v_partial =
3989 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003990 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3991 }
3992 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003993 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003994 {
3995 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003996 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003997 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3998 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003999 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004000 {
4001 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004002 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004003 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4004 }
4005 }
4006 }
4007#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004008 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004009}
4010
Bram Moolenaar8c711452005-01-14 21:53:12 +00004011/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004012 * Return a string with the string representation of a variable.
4013 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004014 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004015 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004016 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4017 * stings as "string()", otherwise does not put quotes around strings, as
4018 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004019 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4020 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004021 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004022 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004023 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004024echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004025 typval_T *tv,
4026 char_u **tofree,
4027 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004028 int copyID,
4029 int echo_style,
4030 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004031 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004032{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004033 static int recurse = 0;
4034 char_u *r = NULL;
4035
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004037 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004038 if (!did_echo_string_emsg)
4039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004040 // Only give this message once for a recursive call to avoid
4041 // flooding the user with errors. And stop iterating over lists
4042 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004043 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004044 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004045 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004046 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004047 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004048 }
4049 ++recurse;
4050
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004051 switch (tv->v_type)
4052 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004053 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004054 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004055 {
4056 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004057 r = tv->vval.v_string;
4058 if (r == NULL)
4059 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004060 }
4061 else
4062 {
4063 *tofree = string_quote(tv->vval.v_string, FALSE);
4064 r = *tofree;
4065 }
4066 break;
4067
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004068 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004069 if (echo_style)
4070 {
4071 *tofree = NULL;
4072 r = tv->vval.v_string;
4073 }
4074 else
4075 {
4076 *tofree = string_quote(tv->vval.v_string, TRUE);
4077 r = *tofree;
4078 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004079 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004080
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004081 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004082 {
4083 partial_T *pt = tv->vval.v_partial;
4084 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004085 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004086 garray_T ga;
4087 int i;
4088 char_u *tf;
4089
4090 ga_init2(&ga, 1, 100);
4091 ga_concat(&ga, (char_u *)"function(");
4092 if (fname != NULL)
4093 {
4094 ga_concat(&ga, fname);
4095 vim_free(fname);
4096 }
4097 if (pt != NULL && pt->pt_argc > 0)
4098 {
4099 ga_concat(&ga, (char_u *)", [");
4100 for (i = 0; i < pt->pt_argc; ++i)
4101 {
4102 if (i > 0)
4103 ga_concat(&ga, (char_u *)", ");
4104 ga_concat(&ga,
4105 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4106 vim_free(tf);
4107 }
4108 ga_concat(&ga, (char_u *)"]");
4109 }
4110 if (pt != NULL && pt->pt_dict != NULL)
4111 {
4112 typval_T dtv;
4113
4114 ga_concat(&ga, (char_u *)", ");
4115 dtv.v_type = VAR_DICT;
4116 dtv.vval.v_dict = pt->pt_dict;
4117 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4118 vim_free(tf);
4119 }
4120 ga_concat(&ga, (char_u *)")");
4121
4122 *tofree = ga.ga_data;
4123 r = *tofree;
4124 break;
4125 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004126
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004127 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004128 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004129 break;
4130
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004131 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004132 if (tv->vval.v_list == NULL)
4133 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004134 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004135 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004136 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004137 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004138 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4139 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004140 {
4141 *tofree = NULL;
4142 r = (char_u *)"[...]";
4143 }
4144 else
4145 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004146 int old_copyID = tv->vval.v_list->lv_copyID;
4147
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004148 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004149 *tofree = list2string(tv, copyID, restore_copyID);
4150 if (restore_copyID)
4151 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004152 r = *tofree;
4153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004154 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004155
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004157 if (tv->vval.v_dict == NULL)
4158 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004159 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004160 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004161 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004162 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004163 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4164 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004165 {
4166 *tofree = NULL;
4167 r = (char_u *)"{...}";
4168 }
4169 else
4170 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004171 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004172
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004173 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004174 *tofree = dict2string(tv, copyID, restore_copyID);
4175 if (restore_copyID)
4176 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004177 r = *tofree;
4178 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004179 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004180
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004182 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004183 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004185 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004186 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004187 break;
4188
Bram Moolenaar835dc632016-02-07 14:27:38 +01004189 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004190 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004191 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004192 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004193 if (composite_val)
4194 {
4195 *tofree = string_quote(r, FALSE);
4196 r = *tofree;
4197 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004198 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004199
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004200 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004201#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004202 *tofree = NULL;
4203 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4204 r = numbuf;
4205 break;
4206#endif
4207
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004208 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004209 case VAR_SPECIAL:
4210 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004211 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004212 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004214
Bram Moolenaar8502c702014-06-17 12:51:16 +02004215 if (--recurse == 0)
4216 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004217 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004218}
4219
4220/*
4221 * Return a string with the string representation of a variable.
4222 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4223 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004224 * Does not put quotes around strings, as ":echo" displays values.
4225 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4226 * May return NULL.
4227 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004228 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004229echo_string(
4230 typval_T *tv,
4231 char_u **tofree,
4232 char_u *numbuf,
4233 int copyID)
4234{
4235 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4236}
4237
4238/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004239 * Return string "str" in ' quotes, doubling ' characters.
4240 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004241 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004242 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004243 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004244string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004245{
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004247 char_u *p, *r, *s;
4248
Bram Moolenaar33570922005-01-25 22:26:29 +00004249 len = (function ? 13 : 3);
4250 if (str != NULL)
4251 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004252 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004253 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004254 if (*p == '\'')
4255 ++len;
4256 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004257 s = r = alloc(len);
4258 if (r != NULL)
4259 {
4260 if (function)
4261 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004263 r += 10;
4264 }
4265 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004266 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004267 if (str != NULL)
4268 for (p = str; *p != NUL; )
4269 {
4270 if (*p == '\'')
4271 *r++ = '\'';
4272 MB_COPY_CHAR(p, r);
4273 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004274 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004275 if (function)
4276 *r++ = ')';
4277 *r++ = NUL;
4278 }
4279 return s;
4280}
4281
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004282#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004283/*
4284 * Convert the string "text" to a floating point number.
4285 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4286 * this always uses a decimal point.
4287 * Returns the length of the text that was consumed.
4288 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004290string2float(
4291 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004292 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004293{
4294 char *s = (char *)text;
4295 float_T f;
4296
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004297 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004298 if (STRNICMP(text, "inf", 3) == 0)
4299 {
4300 *value = INFINITY;
4301 return 3;
4302 }
4303 if (STRNICMP(text, "-inf", 3) == 0)
4304 {
4305 *value = -INFINITY;
4306 return 4;
4307 }
4308 if (STRNICMP(text, "nan", 3) == 0)
4309 {
4310 *value = NAN;
4311 return 3;
4312 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004313 f = strtod(s, &s);
4314 *value = f;
4315 return (int)((char_u *)s - text);
4316}
4317#endif
4318
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004321 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004324var2fpos(
4325 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004326 int dollar_lnum, // TRUE when $ is last line
4327 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004329 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004331 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004333 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004334 if (varp->v_type == VAR_LIST)
4335 {
4336 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004337 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004338 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004339 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004340
4341 l = varp->vval.v_list;
4342 if (l == NULL)
4343 return NULL;
4344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004345 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004346 pos.lnum = list_find_nr(l, 0L, &error);
4347 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004348 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004349
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004350 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004351 pos.col = list_find_nr(l, 1L, &error);
4352 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004353 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004354 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004356 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004357 li = list_find(l, 1L);
4358 if (li != NULL && li->li_tv.v_type == VAR_STRING
4359 && li->li_tv.vval.v_string != NULL
4360 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4361 pos.col = len + 1;
4362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004363 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004364 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004365 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004366 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004369 pos.coladd = list_find_nr(l, 2L, &error);
4370 if (error)
4371 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004372
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004373 return &pos;
4374 }
4375
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004376 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377 if (name == NULL)
4378 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004379 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004381 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004382 {
4383 if (VIsual_active)
4384 return &VIsual;
4385 return &curwin->w_cursor;
4386 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004387 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004389 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4391 return NULL;
4392 return pp;
4393 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004394
Bram Moolenaara5525202006-03-02 22:52:09 +00004395 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004396
Bram Moolenaar477933c2007-07-17 14:32:23 +00004397 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004398 {
4399 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004400 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004401 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004402 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004403 // In silent Ex mode topline is zero, but that's not a valid line
4404 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004405 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004406 return &pos;
4407 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004408 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004409 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004410 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004411 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004412 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004413 return &pos;
4414 }
4415 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004416 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004418 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
4420 pos.lnum = curbuf->b_ml.ml_line_count;
4421 pos.col = 0;
4422 }
4423 else
4424 {
4425 pos.lnum = curwin->w_cursor.lnum;
4426 pos.col = (colnr_T)STRLEN(ml_get_curline());
4427 }
4428 return &pos;
4429 }
4430 return NULL;
4431}
4432
4433/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004434 * Convert list in "arg" into a position and optional file number.
4435 * When "fnump" is NULL there is no file number, only 3 items.
4436 * Note that the column is passed on as-is, the caller may want to decrement
4437 * it to use 1 for the first column.
4438 * Return FAIL when conversion is not possible, doesn't check the position for
4439 * validity.
4440 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004442list2fpos(
4443 typval_T *arg,
4444 pos_T *posp,
4445 int *fnump,
4446 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004447{
4448 list_T *l = arg->vval.v_list;
4449 long i = 0;
4450 long n;
4451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004452 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4453 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004454 if (arg->v_type != VAR_LIST
4455 || l == NULL
4456 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004457 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004458 return FAIL;
4459
4460 if (fnump != NULL)
4461 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004462 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004463 if (n < 0)
4464 return FAIL;
4465 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004466 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004467 *fnump = n;
4468 }
4469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004470 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004471 if (n < 0)
4472 return FAIL;
4473 posp->lnum = n;
4474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004475 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004476 if (n < 0)
4477 return FAIL;
4478 posp->col = n;
4479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004480 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004481 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004482 posp->coladd = 0;
4483 else
4484 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004485
Bram Moolenaar493c1782014-05-28 14:34:46 +02004486 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004487 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004488
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004489 return OK;
4490}
4491
4492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 * Get the length of an environment variable name.
4494 * Advance "arg" to the first character after the name.
4495 * Return 0 for error.
4496 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004498get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499{
4500 char_u *p;
4501 int len;
4502
4503 for (p = *arg; vim_isIDc(*p); ++p)
4504 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004505 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 return 0;
4507
4508 len = (int)(p - *arg);
4509 *arg = p;
4510 return len;
4511}
4512
4513/*
4514 * Get the length of the name of a function or internal variable.
4515 * "arg" is advanced to the first non-white character after the name.
4516 * Return 0 if something is wrong.
4517 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004519get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520{
4521 char_u *p;
4522 int len;
4523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004524 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004526 {
4527 if (*p == ':')
4528 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004529 // "s:" is start of "s:var", but "n:" is not and can be used in
4530 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004531 len = (int)(p - *arg);
4532 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4533 || len > 1)
4534 break;
4535 }
4536 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004537 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 return 0;
4539
4540 len = (int)(p - *arg);
4541 *arg = skipwhite(p);
4542
4543 return len;
4544}
4545
4546/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004547 * Get the length of the name of a variable or function.
4548 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004550 * Return -1 if curly braces expansion failed.
4551 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 * If the name contains 'magic' {}'s, expand them and return the
4553 * expanded name in an allocated string via 'alias' - caller must free.
4554 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004555 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004556get_name_len(
4557 char_u **arg,
4558 char_u **alias,
4559 int evaluate,
4560 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
4562 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 char_u *p;
4564 char_u *expr_start;
4565 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004567 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4570 && (*arg)[2] == (int)KE_SNR)
4571 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004572 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 *arg += 3;
4574 return get_id_len(arg) + 3;
4575 }
4576 len = eval_fname_script(*arg);
4577 if (len > 0)
4578 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004579 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 *arg += len;
4581 }
4582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004586 p = find_name_end(*arg, &expr_start, &expr_end,
4587 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (expr_start != NULL)
4589 {
4590 char_u *temp_string;
4591
4592 if (!evaluate)
4593 {
4594 len += (int)(p - *arg);
4595 *arg = skipwhite(p);
4596 return len;
4597 }
4598
4599 /*
4600 * Include any <SID> etc in the expanded string:
4601 * Thus the -len here.
4602 */
4603 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4604 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004605 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 *alias = temp_string;
4607 *arg = skipwhite(p);
4608 return (int)STRLEN(temp_string);
4609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
4611 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004612 // Only give an error when there is something, otherwise it will be
4613 // reported at a higher level.
4614 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004615 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
4617 return len;
4618}
4619
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620/*
4621 * Find the end of a variable or function name, taking care of magic braces.
4622 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4623 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004624 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 * Return a pointer to just after the name. Equal to "arg" if there is no
4626 * valid name.
4627 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004628 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004629find_name_end(
4630 char_u *arg,
4631 char_u **expr_start,
4632 char_u **expr_end,
4633 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004635 int mb_nest = 0;
4636 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004638 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004639 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 *expr_start = NULL;
4644 *expr_end = NULL;
4645 }
4646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004647 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004648 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4649 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004650 return arg;
4651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 for (p = arg; *p != NUL
4653 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004654 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004655 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004657 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004658 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004659 if (*p == '\'')
4660 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004661 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004662 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004663 ;
4664 if (*p == NUL)
4665 break;
4666 }
4667 else if (*p == '"')
4668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004670 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004671 if (*p == '\\' && p[1] != NUL)
4672 ++p;
4673 if (*p == NUL)
4674 break;
4675 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004676 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004678 // "s:" is start of "s:var", but "n:" is not and can be used in
4679 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004680 len = (int)(p - arg);
4681 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004682 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004683 break;
4684 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004685
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004686 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004688 if (*p == '[')
4689 ++br_nest;
4690 else if (*p == ']')
4691 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004693
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004694 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 if (*p == '{')
4697 {
4698 mb_nest++;
4699 if (expr_start != NULL && *expr_start == NULL)
4700 *expr_start = p;
4701 }
4702 else if (*p == '}')
4703 {
4704 mb_nest--;
4705 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4706 *expr_end = p;
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710
4711 return p;
4712}
4713
4714/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004715 * Expands out the 'magic' {}'s in a variable/function name.
4716 * Note that this can call itself recursively, to deal with
4717 * constructs like foo{bar}{baz}{bam}
4718 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4719 * "in_start" ^
4720 * "expr_start" ^
4721 * "expr_end" ^
4722 * "in_end" ^
4723 *
4724 * Returns a new allocated string, which the caller must free.
4725 * Returns NULL for failure.
4726 */
4727 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004728make_expanded_name(
4729 char_u *in_start,
4730 char_u *expr_start,
4731 char_u *expr_end,
4732 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004733{
4734 char_u c1;
4735 char_u *retval = NULL;
4736 char_u *temp_result;
4737 char_u *nextcmd = NULL;
4738
4739 if (expr_end == NULL || in_end == NULL)
4740 return NULL;
4741 *expr_start = NUL;
4742 *expr_end = NUL;
4743 c1 = *in_end;
4744 *in_end = NUL;
4745
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004746 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004747 if (temp_result != NULL && nextcmd == NULL)
4748 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004749 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4750 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004751 if (retval != NULL)
4752 {
4753 STRCPY(retval, in_start);
4754 STRCAT(retval, temp_result);
4755 STRCAT(retval, expr_end + 1);
4756 }
4757 }
4758 vim_free(temp_result);
4759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004761 *expr_start = '{';
4762 *expr_end = '}';
4763
4764 if (retval != NULL)
4765 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004766 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767 if (expr_start != NULL)
4768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004769 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004770 temp_result = make_expanded_name(retval, expr_start,
4771 expr_end, temp_result);
4772 vim_free(retval);
4773 retval = temp_result;
4774 }
4775 }
4776
4777 return retval;
4778}
4779
4780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004782 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004785eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004787 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4788}
4789
4790/*
4791 * Return TRUE if character "c" can be used as the first character in a
4792 * variable or function name (excluding '{' and '}').
4793 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004794 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004795eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004796{
4797 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798}
4799
4800/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004801 * Handle:
4802 * - expr[expr], expr[expr:expr] subscript
4803 * - ".name" lookup
4804 * - function call with Funcref variable: func(expr)
4805 * - method call: var->method()
4806 *
4807 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004808 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004810handle_subscript(
4811 char_u **arg,
4812 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004813 int flags, // do more than finding the end
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004814 int verbose, // give error messages
4815 char_u *start_leader, // start of '!' and '-' prefixes
4816 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004817{
Bram Moolenaar32e35112020-05-14 22:41:15 +02004818 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004819 int ret = OK;
4820 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004821
Bram Moolenaar61343f02019-07-20 21:11:13 +02004822 // "." is ".name" lookup when we found a dict or when evaluating and
4823 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004824 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02004825 && (((**arg == '['
4826 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02004827 || (!evaluate
4828 && (*arg)[1] != '.'
4829 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004830 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004831 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004832 && !VIM_ISWHITE(*(*arg - 1)))
4833 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004834 {
4835 if (**arg == '(')
4836 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004837 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01004838
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004839 // Stop the expression evaluation when immediately aborting on
4840 // error, or when an interrupt occurred or an exception was thrown
4841 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004842 if (aborting())
4843 {
4844 if (ret == OK)
4845 clear_tv(rettv);
4846 ret = FAIL;
4847 }
4848 dict_unref(selfdict);
4849 selfdict = NULL;
4850 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004851 else if (**arg == '-')
4852 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004853 // Expression "-1.0->method()" applies the leader "-" before
4854 // applying ->.
4855 if (evaluate && *end_leaderp > start_leader)
4856 ret = eval7_leader(rettv, start_leader, end_leaderp);
4857 if (ret == OK)
4858 {
4859 if ((*arg)[2] == '{')
4860 // expr->{lambda}()
4861 ret = eval_lambda(arg, rettv, evaluate, verbose);
4862 else
4863 // expr->name()
4864 ret = eval_method(arg, rettv, evaluate, verbose);
4865 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004866 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004867 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004868 {
4869 dict_unref(selfdict);
4870 if (rettv->v_type == VAR_DICT)
4871 {
4872 selfdict = rettv->vval.v_dict;
4873 if (selfdict != NULL)
4874 ++selfdict->dv_refcount;
4875 }
4876 else
4877 selfdict = NULL;
Bram Moolenaar32e35112020-05-14 22:41:15 +02004878 if (eval_index(arg, rettv, flags, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004879 {
4880 clear_tv(rettv);
4881 ret = FAIL;
4882 }
4883 }
4884 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01004885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004886 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4887 // Don't do this when "Func" is already a partial that was bound
4888 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02004889 if (selfdict != NULL
4890 && (rettv->v_type == VAR_FUNC
4891 || (rettv->v_type == VAR_PARTIAL
4892 && (rettv->vval.v_partial->pt_auto
4893 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004894 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01004895
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004896 dict_unref(selfdict);
4897 return ret;
4898}
4899
4900/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004901 * Make a copy of an item.
4902 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004903 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
4904 * reference to an already copied list/dict can be used.
4905 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004906 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004907 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004908item_copy(
4909 typval_T *from,
4910 typval_T *to,
4911 int deep,
4912 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004913{
4914 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004915 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004916
Bram Moolenaar33570922005-01-25 22:26:29 +00004917 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004919 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004920 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004921 }
4922 ++recurse;
4923
4924 switch (from->v_type)
4925 {
4926 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004928 case VAR_STRING:
4929 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004930 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004931 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01004932 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004933 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004934 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004935 copy_tv(from, to);
4936 break;
4937 case VAR_LIST:
4938 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004939 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004940 if (from->vval.v_list == NULL)
4941 to->vval.v_list = NULL;
4942 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
4943 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004944 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004945 to->vval.v_list = from->vval.v_list->lv_copylist;
4946 ++to->vval.v_list->lv_refcount;
4947 }
4948 else
4949 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
4950 if (to->vval.v_list == NULL)
4951 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004952 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01004953 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01004955 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004956 case VAR_DICT:
4957 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004958 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004959 if (from->vval.v_dict == NULL)
4960 to->vval.v_dict = NULL;
4961 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
4962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004963 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004964 to->vval.v_dict = from->vval.v_dict->dv_copydict;
4965 ++to->vval.v_dict->dv_refcount;
4966 }
4967 else
4968 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
4969 if (to->vval.v_dict == NULL)
4970 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004971 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01004972 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004973 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01004975 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004976 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004977 }
4978 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004979 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004980}
4981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 void
4983echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
4984{
4985 char_u *tofree;
4986 char_u numbuf[NUMBUFLEN];
4987 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
4988
4989 if (*atstart)
4990 {
4991 *atstart = FALSE;
4992 // Call msg_start() after eval1(), evaluating the expression
4993 // may cause a message to appear.
4994 if (with_space)
4995 {
4996 // Mark the saved text as finishing the line, so that what
4997 // follows is displayed on a new line when scrolling back
4998 // at the more prompt.
4999 msg_sb_eol();
5000 msg_start();
5001 }
5002 }
5003 else if (with_space)
5004 msg_puts_attr(" ", echo_attr);
5005
5006 if (p != NULL)
5007 for ( ; *p != NUL && !got_int; ++p)
5008 {
5009 if (*p == '\n' || *p == '\r' || *p == TAB)
5010 {
5011 if (*p != TAB && *needclr)
5012 {
5013 // remove any text still there from the command
5014 msg_clr_eos();
5015 *needclr = FALSE;
5016 }
5017 msg_putchar_attr(*p, echo_attr);
5018 }
5019 else
5020 {
5021 if (has_mbyte)
5022 {
5023 int i = (*mb_ptr2len)(p);
5024
5025 (void)msg_outtrans_len_attr(p, i, echo_attr);
5026 p += i - 1;
5027 }
5028 else
5029 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5030 }
5031 }
5032 vim_free(tofree);
5033}
5034
Bram Moolenaare9a41262005-01-15 22:18:47 +00005035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 * ":echo expr1 ..." print each argument separated with a space, add a
5037 * newline at the end.
5038 * ":echon expr1 ..." print each argument plain.
5039 */
5040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005041ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
5043 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005044 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 char_u *p;
5046 int needclr = TRUE;
5047 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005048 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005049 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005050 evalarg_T evalarg;
5051
5052 CLEAR_FIELD(evalarg);
5053 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 if (eap->skip)
5056 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005057 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005059 // If eval1() causes an error message the text from the command may
5060 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 need_clr_eos = needclr;
5062
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005064 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 /*
5067 * Report the invalid expression unless the expression evaluation
5068 * has been cancelled due to an aborting error, an interrupt, or an
5069 * exception.
5070 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005071 if (!aborting() && did_emsg == did_emsg_before
5072 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005073 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005074 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 break;
5076 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005077 need_clr_eos = FALSE;
5078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005081
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 arg = skipwhite(arg);
5084 }
5085 eap->nextcmd = check_nextcmd(arg);
5086
5087 if (eap->skip)
5088 --emsg_skip;
5089 else
5090 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005091 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 if (needclr)
5093 msg_clr_eos();
5094 if (eap->cmdidx == CMD_echo)
5095 msg_end();
5096 }
5097}
5098
5099/*
5100 * ":echohl {name}".
5101 */
5102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005103ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005105 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106}
5107
5108/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005109 * Returns the :echo attribute
5110 */
5111 int
5112get_echo_attr(void)
5113{
5114 return echo_attr;
5115}
5116
5117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 * ":execute expr1 ..." execute the result of an expression.
5119 * ":echomsg expr1 ..." Print a message
5120 * ":echoerr expr1 ..." Print an error
5121 * Each gets spaces around each argument and a newline at the end for
5122 * echo commands
5123 */
5124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005125ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
5127 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005128 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 int ret = OK;
5130 char_u *p;
5131 garray_T ga;
5132 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 ga_init2(&ga, 1, 80);
5135
5136 if (eap->skip)
5137 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005138 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005140 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5141 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
5144 if (!eap->skip)
5145 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005146 char_u buf[NUMBUFLEN];
5147
5148 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005149 {
5150 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5151 {
5152 emsg(_(e_inval_string));
5153 p = NULL;
5154 }
5155 else
5156 p = tv_get_string_buf(&rettv, buf);
5157 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005158 else
5159 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005160 if (p == NULL)
5161 {
5162 clear_tv(&rettv);
5163 ret = FAIL;
5164 break;
5165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 len = (int)STRLEN(p);
5167 if (ga_grow(&ga, len + 2) == FAIL)
5168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 ret = FAIL;
5171 break;
5172 }
5173 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 ga.ga_len += len;
5177 }
5178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 arg = skipwhite(arg);
5181 }
5182
5183 if (ret != FAIL && ga.ga_data != NULL)
5184 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005185 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005187 // Mark the already saved text as finishing the line, so that what
5188 // follows is displayed on a new line when scrolling back at the
5189 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005190 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005191 }
5192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005194 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005195 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005196 out_flush();
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 else if (eap->cmdidx == CMD_echoerr)
5199 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005200 int save_did_emsg = did_emsg;
5201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005203 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (!force_abort)
5205 did_emsg = save_did_emsg;
5206 }
5207 else if (eap->cmdidx == CMD_execute)
5208 do_cmdline((char_u *)ga.ga_data,
5209 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5210 }
5211
5212 ga_clear(&ga);
5213
5214 if (eap->skip)
5215 --emsg_skip;
5216
5217 eap->nextcmd = check_nextcmd(arg);
5218}
5219
5220/*
5221 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5222 * "arg" points to the "&" or '+' when called, to "option" when returning.
5223 * Returns NULL when no option name found. Otherwise pointer to the char
5224 * after the option name.
5225 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005226 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005227find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
5229 char_u *p = *arg;
5230
5231 ++p;
5232 if (*p == 'g' && p[1] == ':')
5233 {
5234 *opt_flags = OPT_GLOBAL;
5235 p += 2;
5236 }
5237 else if (*p == 'l' && p[1] == ':')
5238 {
5239 *opt_flags = OPT_LOCAL;
5240 p += 2;
5241 }
5242 else
5243 *opt_flags = 0;
5244
5245 if (!ASCII_ISALPHA(*p))
5246 return NULL;
5247 *arg = p;
5248
5249 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 else
5252 while (ASCII_ISALPHA(*p))
5253 ++p;
5254 return p;
5255}
5256
5257/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005258 * Display script name where an item was last set.
5259 * Should only be invoked when 'verbose' is non-zero.
5260 */
5261 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005262last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005263{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005264 char_u *p;
5265
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005266 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005267 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005268 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005269 if (p != NULL)
5270 {
5271 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005272 msg_puts(_("\n\tLast set from "));
5273 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005274 if (script_ctx.sc_lnum > 0)
5275 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005276 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005277 msg_outnum((long)script_ctx.sc_lnum);
5278 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005279 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005280 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005281 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005282 }
5283}
5284
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005285#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
5287/*
5288 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005289 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 * "flags" can be "g" to do a global substitute.
5291 * Returns an allocated string, NULL for error.
5292 */
5293 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005294do_string_sub(
5295 char_u *str,
5296 char_u *pat,
5297 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005298 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005299 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300{
5301 int sublen;
5302 regmatch_T regmatch;
5303 int i;
5304 int do_all;
5305 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005306 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 garray_T ga;
5308 char_u *ret;
5309 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005310 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005314 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316 ga_init2(&ga, 1, 200);
5317
5318 do_all = (flags[0] == 'g');
5319
5320 regmatch.rm_ic = p_ic;
5321 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5322 if (regmatch.regprog != NULL)
5323 {
5324 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005325 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005328 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005329 if (regmatch.startp[0] == regmatch.endp[0])
5330 {
5331 if (zero_width == regmatch.startp[0])
5332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005333 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005334 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005335 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5336 (size_t)i);
5337 ga.ga_len += i;
5338 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005339 continue;
5340 }
5341 zero_width = regmatch.startp[0];
5342 }
5343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 /*
5345 * Get some space for a temporary buffer to do the substitution
5346 * into. It will contain:
5347 * - The text up to where the match is.
5348 * - The substituted text.
5349 * - The text after the match.
5350 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005351 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005352 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5354 {
5355 ga_clear(&ga);
5356 break;
5357 }
5358
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 i = (int)(regmatch.startp[0] - tail);
5361 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005363 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 + ga.ga_len + i, TRUE, TRUE, FALSE);
5365 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005366 tail = regmatch.endp[0];
5367 if (*tail == NUL)
5368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 if (!do_all)
5370 break;
5371 }
5372
5373 if (ga.ga_data != NULL)
5374 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5375
Bram Moolenaar473de612013-06-08 18:19:48 +02005376 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378
5379 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5380 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005381 if (p_cpo == empty_option)
5382 p_cpo = save_cpo;
5383 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005384 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005385 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
5387 return ret;
5388}