blob: 6aaa3a6ef0cab1298f61f275721884d9675c381d [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 Moolenaar0b1cd522020-06-27 13:11:50 +020054static int eval7_leader(typval_T *rettv, int numeric_only, 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,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200164 exarg_T *eap,
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 Moolenaarfaf86262020-06-27 23:07:36 +0200169 evalarg_T evalarg;
170
171 CLEAR_FIELD(evalarg);
172 evalarg.eval_flags = skip ? 0 : EVAL_EVALUATE;
173 evalarg.eval_cookie = eap != NULL && eap->getline == getsourceline
174 ? eap->cookie : NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
176 if (skip)
177 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200178 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 else
181 {
182 *error = FALSE;
183 if (!skip)
184 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100185 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000186 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 }
188 }
189 if (skip)
190 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200191 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200193 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194}
195
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100196/*
197 * Call eval1() and give an error message if not done at a lower level.
198 */
199 static int
200eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
201{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100202 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203 int ret;
204 int did_emsg_before = did_emsg;
205 int called_emsg_before = called_emsg;
206
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200207 ret = eval1(arg, rettv, evaluate ? &EVALARG_EVALUATE : NULL);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 if (ret == FAIL)
209 {
210 // Report the invalid expression unless the expression evaluation has
211 // been cancelled due to an aborting error, an interrupt, or an
212 // exception, or we already gave a more specific error.
213 // Also check called_emsg for when using assert_fails().
214 if (!aborting() && did_emsg == did_emsg_before
215 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100216 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217 }
218 return ret;
219}
220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200222 * Return whether a typval is a valid expression to pass to eval_expr_typval()
223 * or eval_expr_to_bool(). An empty string returns FALSE;
224 */
225 int
226eval_expr_valid_arg(typval_T *tv)
227{
228 return tv->v_type != VAR_UNKNOWN
229 && (tv->v_type != VAR_STRING
230 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
231}
232
233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 * Evaluate an expression, which can be a function, partial or string.
235 * Pass arguments "argv[argc]".
236 * Return the result in "rettv" and OK or FAIL.
237 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200238 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100239eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
240{
241 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100242 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200243 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100244
245 if (expr->v_type == VAR_FUNC)
246 {
247 s = expr->vval.v_string;
248 if (s == NULL || *s == NUL)
249 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200250 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200251 funcexe.evaluate = TRUE;
252 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100253 return FAIL;
254 }
255 else if (expr->v_type == VAR_PARTIAL)
256 {
257 partial_T *partial = expr->vval.v_partial;
258
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200259 if (partial == NULL)
260 return FAIL;
261
Bram Moolenaar822ba242020-05-24 23:00:18 +0200262 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200263 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200265 if (call_def_function(partial->pt_func, argc, argv,
266 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 return FAIL;
268 }
269 else
270 {
271 s = partial_name(partial);
272 if (s == NULL || *s == NUL)
273 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200274 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 funcexe.evaluate = TRUE;
276 funcexe.partial = partial;
277 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
278 return FAIL;
279 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100280 }
281 else
282 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100283 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100284 if (s == NULL)
285 return FAIL;
286 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100287 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100289 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100290 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200291 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100292 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 return FAIL;
294 }
295 }
296 return OK;
297}
298
299/*
300 * Like eval_to_bool() but using a typval_T instead of a string.
301 * Works for string, funcref and partial.
302 */
303 int
304eval_expr_to_bool(typval_T *expr, int *error)
305{
306 typval_T rettv;
307 int res;
308
309 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
310 {
311 *error = TRUE;
312 return FALSE;
313 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100314 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100315 clear_tv(&rettv);
316 return res;
317}
318
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319/*
320 * Top level evaluation function, returning a string. If "skip" is TRUE,
321 * only parsing to "nextcmd" is done, without reporting errors. Return
322 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
323 */
324 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100325eval_to_string_skip(
326 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200327 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100328 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329{
Bram Moolenaar33570922005-01-25 22:26:29 +0000330 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 char_u *retval;
332
333 if (skip)
334 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200335 if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 retval = NULL;
337 else
338 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100339 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000340 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 }
342 if (skip)
343 --emsg_skip;
344
345 return retval;
346}
347
348/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000349 * Skip over an expression at "*pp".
350 * Return FAIL for an error, OK otherwise.
351 */
352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000354{
Bram Moolenaar33570922005-01-25 22:26:29 +0000355 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000356
357 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200358 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000359}
360
361/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200362 * Skip over an expression at "*pp".
363 * If in Vim9 script and line breaks are encountered, the lines are
364 * concatenated. "evalarg->eval_tofree" will be set accordingly.
365 * Return FAIL for an error, OK otherwise.
366 */
367 int
368skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
369{
370 typval_T rettv;
371 int res;
372 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
373 garray_T *gap = &evalarg->eval_ga;
374 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
375
376 if (vim9script && evalarg->eval_cookie != NULL)
377 {
378 ga_init2(gap, sizeof(char_u *), 10);
379 if (ga_grow(gap, 1) == OK)
380 // leave room for "start"
381 ++gap->ga_len;
382 }
383
384 // Don't evaluate the expression.
385 if (evalarg != NULL)
386 evalarg->eval_flags &= ~EVAL_EVALUATE;
387 *end = skipwhite(*end);
388 res = eval1(end, &rettv, evalarg);
389 if (evalarg != NULL)
390 evalarg->eval_flags = save_flags;
391
392 if (vim9script && evalarg->eval_cookie != NULL
393 && evalarg->eval_ga.ga_len > 1)
394 {
395 char_u *p;
396 size_t endoff = STRLEN(*end);
397
398 // Line breaks encountered, concatenate all the lines.
399 *((char_u **)gap->ga_data) = *start;
400 p = ga_concat_strings(gap, "");
401 *((char_u **)gap->ga_data) = NULL;
402 ga_clear_strings(gap);
403 gap->ga_itemsize = 0;
404 if (p == NULL)
405 return FAIL;
406 *start = p;
407 vim_free(evalarg->eval_tofree);
408 evalarg->eval_tofree = p;
409 // Compute "end" relative to the end.
410 *end = *start + STRLEN(*start) - endoff;
411 }
412
413 return res;
414}
415
416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000418 * When "convert" is TRUE convert a List into a sequence of lines and convert
419 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 * Return pointer to allocated memory, or NULL for failure.
421 */
422 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100423eval_to_string(
424 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100425 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426{
Bram Moolenaar33570922005-01-25 22:26:29 +0000427 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000429 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000430#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000431 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200434 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 retval = NULL;
436 else
437 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000438 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000439 {
440 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000441 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200442 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200443 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200444 if (tv.vval.v_list->lv_len > 0)
445 ga_append(&ga, NL);
446 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000447 ga_append(&ga, NUL);
448 retval = (char_u *)ga.ga_data;
449 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000450#ifdef FEAT_FLOAT
451 else if (convert && tv.v_type == VAR_FLOAT)
452 {
453 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
454 retval = vim_strsave(numbuf);
455 }
456#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000457 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100458 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000459 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
461
462 return retval;
463}
464
465/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000466 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200467 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 */
469 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100470eval_to_string_safe(
471 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100472 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200475 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200477 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000478 if (use_sandbox)
479 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200480 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200481 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000482 if (use_sandbox)
483 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200484 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200485 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 return retval;
487}
488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * Top level evaluation function, returning a number.
491 * Evaluates "expr" silently.
492 * Returns -1 for an error.
493 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200494 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100495eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
Bram Moolenaar33570922005-01-25 22:26:29 +0000497 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200498 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000499 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
501 ++emsg_off;
502
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200503 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 retval = -1;
505 else
506 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100507 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000508 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 }
510 --emsg_off;
511
512 return retval;
513}
514
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000515/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000516 * Top level evaluation function.
517 * Returns an allocated typval_T with the result.
518 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000519 */
520 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200521eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000522{
523 typval_T *tv;
524
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200525 tv = ALLOC_ONE(typval_T);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200526 if (tv != NULL && eval0(arg, tv, eap, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100527 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000528
529 return tv;
530}
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100533 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200534 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
535 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000536 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539call_vim_function(
540 char_u *func,
541 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200542 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200543 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000545 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200546 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100548 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200549 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200550 funcexe.firstline = curwin->w_cursor.lnum;
551 funcexe.lastline = curwin->w_cursor.lnum;
552 funcexe.evaluate = TRUE;
553 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000554 if (ret == FAIL)
555 clear_tv(rettv);
556
557 return ret;
558}
559
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100560/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100561 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100562 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200563 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
564 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100565 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200566 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100567call_func_retnr(
568 char_u *func,
569 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200570 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100571{
572 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200573 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100574
Bram Moolenaarded27a12018-08-01 19:06:03 +0200575 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100576 return -1;
577
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100578 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100579 clear_tv(&rettv);
580 return retval;
581}
582
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000583/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100584 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000585 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200586 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
587 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000588 */
589 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100590call_func_retstr(
591 char_u *func,
592 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200593 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000594{
595 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000596 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000597
Bram Moolenaarded27a12018-08-01 19:06:03 +0200598 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000599 return NULL;
600
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100601 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000602 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 return retval;
604}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000605
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000606/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100607 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200608 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
609 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000610 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000611 */
612 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100613call_func_retlist(
614 char_u *func,
615 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200616 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000617{
618 typval_T rettv;
619
Bram Moolenaarded27a12018-08-01 19:06:03 +0200620 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000621 return NULL;
622
623 if (rettv.v_type != VAR_LIST)
624 {
625 clear_tv(&rettv);
626 return NULL;
627 }
628
629 return rettv.vval.v_list;
630}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#ifdef FEAT_FOLDING
633/*
634 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
635 * it in "*cp". Doesn't give error messages.
636 */
637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100638eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639{
Bram Moolenaar33570922005-01-25 22:26:29 +0000640 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200641 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000643 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
644 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000647 if (use_sandbox)
648 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200649 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200651 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 retval = 0;
653 else
654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100655 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000656 if (tv.v_type == VAR_NUMBER)
657 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000658 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 retval = 0;
660 else
661 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100662 // If the result is a string, check if there is a non-digit before
663 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000664 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 if (!VIM_ISDIGIT(*s) && *s != '-')
666 *cp = *s++;
667 retval = atol((char *)s);
668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000669 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 if (use_sandbox)
673 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200674 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200676 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677}
678#endif
679
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000681 * Get an lval: variable, Dict item or List item that can be assigned a value
682 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
683 * "name.key", "name.key[expr]" etc.
684 * Indexing only works if "name" is an existing List or Dictionary.
685 * "name" points to the start of the name.
686 * If "rettv" is not NULL it points to the value to be assigned.
687 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
688 * wrong; must end in space or cmd separator.
689 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100690 * flags:
691 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100692 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100693 * GLV_NO_AUTOLOAD: do not use script autoloading
694 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000695 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000696 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000697 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000698 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200699 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100700get_lval(
701 char_u *name,
702 typval_T *rettv,
703 lval_T *lp,
704 int unlet,
705 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100706 int flags, // GLV_ values
707 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000709 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000710 char_u *expr_start, *expr_end;
711 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000712 dictitem_T *v;
713 typval_T var1;
714 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000715 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000716 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000717 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000718 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000719 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100720 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000721
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100722 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200723 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000724
725 if (skip)
726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100727 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000728 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000729 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000730 }
731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100732 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000733 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000735 if (expr_start != NULL)
736 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100737 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100738 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000739 && *p != '[' && *p != '.')
740 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100741 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000742 return NULL;
743 }
744
745 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
746 if (lp->ll_exp_name == NULL)
747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100748 // Report an invalid expression in braces, unless the
749 // expression evaluation has been cancelled due to an
750 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000751 if (!aborting() && !quiet)
752 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000753 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100754 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000755 return NULL;
756 }
757 }
758 lp->ll_name = lp->ll_exp_name;
759 }
760 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000762 lp->ll_name = name;
763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
765 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100766 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 char_u *tp = skipwhite(p + 1);
768
769 // parse the type after the name
770 lp->ll_type = parse_type(&tp, &si->sn_type_list);
771 lp->ll_name_end = tp;
772 }
773 }
774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100775 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000776 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
777 return p;
778
779 cc = *p;
780 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100781 // Only pass &ht when we would write to the variable, it prevents autoload
782 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100783 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
784 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000785 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100786 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000787 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000788 if (v == NULL)
789 return NULL;
790
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000791 /*
792 * Loop until no more [idx] or .key is following.
793 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000794 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100795 var1.v_type = VAR_UNKNOWN;
796 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000797 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000798 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000799 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
800 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100801 && lp->ll_tv->vval.v_dict != NULL)
802 && !(lp->ll_tv->v_type == VAR_BLOB
803 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100806 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000810 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000811 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100812 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000813 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000815
Bram Moolenaar8c711452005-01-14 21:53:12 +0000816 len = -1;
817 if (*p == '.')
818 {
819 key = p + 1;
820 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
821 ;
822 if (len == 0)
823 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000824 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100825 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000826 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000827 }
828 p = key + len;
829 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000830 else
831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000833 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000834 if (*p == ':')
835 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000836 else
837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000838 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200839 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000840 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100841 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000842 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100843 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000844 clear_tv(&var1);
845 return NULL;
846 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000847 }
848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100849 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000850 if (*p == ':')
851 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000853 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100855 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100856 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000858 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100859 if (rettv != NULL
860 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100861 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100862 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100863 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000864 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100866 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100867 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000869 }
870 p = skipwhite(p + 1);
871 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000873 else
874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200876 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200877 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000878 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100879 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000881 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100882 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000883 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100884 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100885 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000886 clear_tv(&var2);
887 return NULL;
888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000889 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000892 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000894
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100898 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100899 clear_tv(&var1);
900 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 }
903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100904 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000905 ++p;
906 }
907
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 {
910 if (len == -1)
911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100912 // "[key]": get key from "var1"
913 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200914 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000915 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000916 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000918 }
919 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000920 lp->ll_list = NULL;
921 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000922 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100924 // When assigning to a scope dictionary check that a function and
925 // variable name is valid (only variable name unless it is l: or
926 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200927 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200928 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200929 int prevval;
930 int wrong;
931
932 if (len != -1)
933 {
934 prevval = key[len];
935 key[len] = NUL;
936 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200937 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100938 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200939 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
940 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200941 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200942 || !valid_varname(key);
943 if (len != -1)
944 key[len] = prevval;
945 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200946 {
947 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200948 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200949 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200950 }
951
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000953 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100954 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200955 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100956 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100958 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100959 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200960 return NULL;
961 }
962
Bram Moolenaar31b81602019-02-10 22:14:27 +0100963 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000965 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100967 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100968 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 }
971 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000974 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100975 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 p = NULL;
978 break;
979 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100980 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100981 else if ((flags & GLV_READ_ONLY) == 0
982 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100983 {
984 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200985 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100986 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200987
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100988 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100991 else if (lp->ll_tv->v_type == VAR_BLOB)
992 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100993 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
994
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100995 /*
996 * Get the number and item for the only or first index of the List.
997 */
998 if (empty1)
999 lp->ll_n1 = 0;
1000 else
1001 // is number or string
1002 lp->ll_n1 = (long)tv_get_number(&var1);
1003 clear_tv(&var1);
1004
1005 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001006 || lp->ll_n1 > bloblen
1007 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001008 {
1009 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001010 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001011 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001012 return NULL;
1013 }
1014 if (lp->ll_range && !lp->ll_empty2)
1015 {
1016 lp->ll_n2 = (long)tv_get_number(&var2);
1017 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 if (lp->ll_n2 < 0
1019 || lp->ll_n2 >= bloblen
1020 || lp->ll_n2 < lp->ll_n1)
1021 {
1022 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001023 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001024 return NULL;
1025 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001026 }
1027 lp->ll_blob = lp->ll_tv->vval.v_blob;
1028 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001029 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001030 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 else
1032 {
1033 /*
1034 * Get the number and item for the only or first index of the List.
1035 */
1036 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001038 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001040 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001041 clear_tv(&var1);
1042
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001043 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 lp->ll_list = lp->ll_tv->vval.v_list;
1045 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1046 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001047 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001048 if (lp->ll_n1 < 0)
1049 {
1050 lp->ll_n1 = 0;
1051 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1052 }
1053 }
1054 if (lp->ll_li == NULL)
1055 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001056 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001057 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001058 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001059 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 }
1061
1062 /*
1063 * May need to find the item or absolute index for the second
1064 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065 * When no index given: "lp->ll_empty2" is TRUE.
1066 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001067 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001070 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001074 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001077 {
1078 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001079 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001080 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001081 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001082 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 }
1084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001085 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001086 if (lp->ll_n1 < 0)
1087 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1088 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001089 {
1090 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001091 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001092 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001093 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001094 }
1095
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001096 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001098 }
1099
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001100 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001102 return p;
1103}
1104
1105/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001106 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001107 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001109clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110{
1111 vim_free(lp->ll_exp_name);
1112 vim_free(lp->ll_newkey);
1113}
1114
1115/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001116 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001118 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1119 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001122set_var_lval(
1123 lval_T *lp,
1124 char_u *endp,
1125 typval_T *rettv,
1126 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001128 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129{
1130 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001131 listitem_T *ri;
1132 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133
1134 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001136 cc = *endp;
1137 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001138 if (lp->ll_blob != NULL)
1139 {
1140 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001141
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001142 if (op != NULL && *op != '=')
1143 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001144 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001145 return;
1146 }
1147
1148 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1149 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001150 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001151
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001152 if (lp->ll_empty2)
1153 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1154
1155 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001157 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001158 return;
1159 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001160 if (lp->ll_empty2)
1161 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001162
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001163 ir = 0;
1164 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1165 blob_set(lp->ll_blob, il,
1166 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001167 }
1168 else
1169 {
1170 val = (int)tv_get_number_chk(rettv, &error);
1171 if (!error)
1172 {
1173 garray_T *gap = &lp->ll_blob->bv_ga;
1174
1175 // Allow for appending a byte. Setting a byte beyond
1176 // the end is an error otherwise.
1177 if (lp->ll_n1 < gap->ga_len
1178 || (lp->ll_n1 == gap->ga_len
1179 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1180 {
1181 blob_set(lp->ll_blob, lp->ll_n1, val);
1182 if (lp->ll_n1 == gap->ga_len)
1183 ++gap->ga_len;
1184 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001185 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001186 }
1187 }
1188 }
1189 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001191 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001194 {
1195 emsg(_(e_cannot_mod));
1196 *endp = cc;
1197 return;
1198 }
1199
Bram Moolenaarff697e62019-02-12 22:28:33 +01001200 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001201 di = NULL;
1202 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1203 &tv, &di, TRUE, FALSE) == OK)
1204 {
1205 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001206 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1207 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001208 && tv_op(&tv, rettv, op) == OK)
1209 set_var(lp->ll_name, &tv, FALSE);
1210 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001211 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001213 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001215 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001217 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001218 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001219 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001220 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 else if (lp->ll_range)
1222 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001223 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001224 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001225
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 range"));
1229 return;
1230 }
1231
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001232 /*
1233 * Check whether any of the list items is locked
1234 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001235 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001236 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001237 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001238 return;
1239 ri = ri->li_next;
1240 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1241 break;
1242 ll_li = ll_li->li_next;
1243 ++ll_n1;
1244 }
1245
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 /*
1247 * Assign the List values to the list items.
1248 */
1249 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001250 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001251 if (op != NULL && *op != '=')
1252 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1253 else
1254 {
1255 clear_tv(&lp->ll_li->li_tv);
1256 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1257 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 ri = ri->li_next;
1259 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1260 break;
1261 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001263 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001264 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001265 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 ri = NULL;
1267 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001268 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001269 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001270 lp->ll_li = lp->ll_li->li_next;
1271 ++lp->ll_n1;
1272 }
1273 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001274 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001275 else if (lp->ll_empty2
1276 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001278 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279 }
1280 else
1281 {
1282 /*
1283 * Assign to a List or Dictionary item.
1284 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001286 {
1287 emsg(_("E996: Cannot lock a list or dict"));
1288 return;
1289 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 if (lp->ll_newkey != NULL)
1291 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001292 if (op != NULL && *op != '=')
1293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001294 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001295 return;
1296 }
1297
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001298 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001299 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300 if (di == NULL)
1301 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001302 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1303 {
1304 vim_free(di);
1305 return;
1306 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001308 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001309 else if (op != NULL && *op != '=')
1310 {
1311 tv_op(lp->ll_tv, rettv, op);
1312 return;
1313 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001314 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001316
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 /*
1318 * Assign the value to the variable or list item.
1319 */
1320 if (copy)
1321 copy_tv(rettv, lp->ll_tv);
1322 else
1323 {
1324 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001325 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001327 }
1328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329}
1330
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001331/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001332 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1333 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 * Returns OK or FAIL.
1335 */
1336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001337tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001338{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001339 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001340 char_u numbuf[NUMBUFLEN];
1341 char_u *s;
1342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001343 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001344 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001345 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001346 {
1347 switch (tv1->v_type)
1348 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001349 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001350 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 case VAR_DICT:
1353 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001354 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001355 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001356 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001357 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001358 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 break;
1360
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001361 case VAR_BLOB:
1362 if (*op != '+' || tv2->v_type != VAR_BLOB)
1363 break;
1364 // BLOB += BLOB
1365 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1366 {
1367 blob_T *b1 = tv1->vval.v_blob;
1368 blob_T *b2 = tv2->vval.v_blob;
1369 int i, len = blob_len(b2);
1370 for (i = 0; i < len; i++)
1371 ga_append(&b1->bv_ga, blob_get(b2, i));
1372 }
1373 return OK;
1374
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001375 case VAR_LIST:
1376 if (*op != '+' || tv2->v_type != VAR_LIST)
1377 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001378 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001379 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1380 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1381 return OK;
1382
1383 case VAR_NUMBER:
1384 case VAR_STRING:
1385 if (tv2->v_type == VAR_LIST)
1386 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001387 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001388 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001389 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001390 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001391#ifdef FEAT_FLOAT
1392 if (tv2->v_type == VAR_FLOAT)
1393 {
1394 float_T f = n;
1395
Bram Moolenaarff697e62019-02-12 22:28:33 +01001396 if (*op == '%')
1397 break;
1398 switch (*op)
1399 {
1400 case '+': f += tv2->vval.v_float; break;
1401 case '-': f -= tv2->vval.v_float; break;
1402 case '*': f *= tv2->vval.v_float; break;
1403 case '/': f /= tv2->vval.v_float; break;
1404 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001405 clear_tv(tv1);
1406 tv1->v_type = VAR_FLOAT;
1407 tv1->vval.v_float = f;
1408 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001409 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001410#endif
1411 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001412 switch (*op)
1413 {
1414 case '+': n += tv_get_number(tv2); break;
1415 case '-': n -= tv_get_number(tv2); break;
1416 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001417 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1418 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001419 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001420 clear_tv(tv1);
1421 tv1->v_type = VAR_NUMBER;
1422 tv1->vval.v_number = n;
1423 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001424 }
1425 else
1426 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001427 if (tv2->v_type == VAR_FLOAT)
1428 break;
1429
Bram Moolenaarff697e62019-02-12 22:28:33 +01001430 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001431 s = tv_get_string(tv1);
1432 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 clear_tv(tv1);
1434 tv1->v_type = VAR_STRING;
1435 tv1->vval.v_string = s;
1436 }
1437 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001438
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001439 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001440#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001441 {
1442 float_T f;
1443
Bram Moolenaarff697e62019-02-12 22:28:33 +01001444 if (*op == '%' || *op == '.'
1445 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001446 && tv2->v_type != VAR_NUMBER
1447 && tv2->v_type != VAR_STRING))
1448 break;
1449 if (tv2->v_type == VAR_FLOAT)
1450 f = tv2->vval.v_float;
1451 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001452 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001453 switch (*op)
1454 {
1455 case '+': tv1->vval.v_float += f; break;
1456 case '-': tv1->vval.v_float -= f; break;
1457 case '*': tv1->vval.v_float *= f; break;
1458 case '/': tv1->vval.v_float /= f; break;
1459 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001460 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001461#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001462 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001463 }
1464 }
1465
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001466 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467 return FAIL;
1468}
1469
1470/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001471 * Evaluate the expression used in a ":for var in expr" command.
1472 * "arg" points to "var".
1473 * Set "*errp" to TRUE for an error, FALSE otherwise;
1474 * Return a pointer that holds the info. Null when there is an error.
1475 */
1476 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001477eval_for_line(
1478 char_u *arg,
1479 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001480 exarg_T *eap,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001481 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001482{
Bram Moolenaar33570922005-01-25 22:26:29 +00001483 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001484 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001485 typval_T tv;
1486 list_T *l;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001487 evalarg_T evalarg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001488
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001489 CLEAR_FIELD(evalarg);
1490 evalarg.eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001491 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001493 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001494 if (fi == NULL)
1495 return NULL;
1496
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001497 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498 if (expr == NULL)
1499 return fi;
1500
1501 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001502 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001503 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001505 return fi;
1506 }
1507
1508 if (skip)
1509 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001510 if (eval0(skipwhite(expr + 2), &tv, eap, &evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001511 {
1512 *errp = FALSE;
1513 if (!skip)
1514 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001515 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001516 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001517 l = tv.vval.v_list;
1518 if (l == NULL)
1519 {
1520 // a null list is like an empty list: do nothing
1521 clear_tv(&tv);
1522 }
1523 else
1524 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001526 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001528 // No need to increment the refcount, it's already set for
1529 // the list being used in "tv".
1530 fi->fi_list = l;
1531 list_add_watch(l, &fi->fi_lw);
1532 fi->fi_lw.lw_item = l->lv_first;
1533 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001534 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001535 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001536 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001537 fi->fi_bi = 0;
1538 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001539 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001540 typval_T btv;
1541
1542 // Make a copy, so that the iteration still works when the
1543 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001545 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001546 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001547 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001548 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001549 else
1550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001551 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001552 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001553 }
1554 }
1555 }
1556 if (skip)
1557 --emsg_skip;
1558
1559 return fi;
1560}
1561
1562/*
1563 * Use the first item in a ":for" list. Advance to the next.
1564 * Assign the values to the variable (list). "arg" points to the first one.
1565 * Return TRUE when a valid item was found, FALSE when at end of list or
1566 * something wrong.
1567 */
1568 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001571 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001572 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001573 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1574 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001575 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001576
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001577 if (fi->fi_blob != NULL)
1578 {
1579 typval_T tv;
1580
1581 if (fi->fi_bi >= blob_len(fi->fi_blob))
1582 return FALSE;
1583 tv.v_type = VAR_NUMBER;
1584 tv.v_lock = VAR_FIXED;
1585 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1586 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001587 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001588 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001589 }
1590
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001591 item = fi->fi_lw.lw_item;
1592 if (item == NULL)
1593 result = FALSE;
1594 else
1595 {
1596 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001597 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001598 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001599 }
1600 return result;
1601}
1602
1603/*
1604 * Free the structure used to store info used by ":for".
1605 */
1606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001607free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001608{
Bram Moolenaar33570922005-01-25 22:26:29 +00001609 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001610
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001611 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001612 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001614 list_unref(fi->fi_list);
1615 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001616 if (fi != NULL && fi->fi_blob != NULL)
1617 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618 vim_free(fi);
1619}
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001622set_context_for_expression(
1623 expand_T *xp,
1624 char_u *arg,
1625 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
1627 int got_eq = FALSE;
1628 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001631 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 {
1633 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001634 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001636 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001637 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 {
1639 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001640 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001641 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642 break;
1643 }
1644 return;
1645 }
1646 }
1647 else
1648 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1649 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 while ((xp->xp_pattern = vim_strpbrk(arg,
1651 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1652 {
1653 c = *xp->xp_pattern;
1654 if (c == '&')
1655 {
1656 c = xp->xp_pattern[1];
1657 if (c == '&')
1658 {
1659 ++xp->xp_pattern;
1660 xp->xp_context = cmdidx != CMD_let || got_eq
1661 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1662 }
1663 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001666 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1667 xp->xp_pattern += 2;
1668
1669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
1671 else if (c == '$')
1672 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001673 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 xp->xp_context = EXPAND_ENV_VARS;
1675 }
1676 else if (c == '=')
1677 {
1678 got_eq = TRUE;
1679 xp->xp_context = EXPAND_EXPRESSION;
1680 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001681 else if (c == '#'
1682 && xp->xp_context == EXPAND_EXPRESSION)
1683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001684 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001685 break;
1686 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001687 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 && xp->xp_context == EXPAND_FUNCTIONS
1689 && vim_strchr(xp->xp_pattern, '(') == NULL)
1690 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001691 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 break;
1693 }
1694 else if (cmdidx != CMD_let || got_eq)
1695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001696 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
1698 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1699 if (c == '\\' && xp->xp_pattern[1] != NUL)
1700 ++xp->xp_pattern;
1701 xp->xp_context = EXPAND_NOTHING;
1702 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001703 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001705 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1707 /* skip */ ;
1708 xp->xp_context = EXPAND_NOTHING;
1709 }
1710 else if (c == '|')
1711 {
1712 if (xp->xp_pattern[1] == '|')
1713 {
1714 ++xp->xp_pattern;
1715 xp->xp_context = EXPAND_EXPRESSION;
1716 }
1717 else
1718 xp->xp_context = EXPAND_COMMANDS;
1719 }
1720 else
1721 xp->xp_context = EXPAND_EXPRESSION;
1722 }
1723 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001724 // Doesn't look like something valid, expand as an expression
1725 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 arg = xp->xp_pattern;
1728 if (*arg != NUL)
1729 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1730 /* skip */ ;
1731 }
1732 xp->xp_pattern = arg;
1733}
1734
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001736 * Return TRUE if "pat" matches "text".
1737 * Does not use 'cpo' and always uses 'magic'.
1738 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001739 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001740pattern_match(char_u *pat, char_u *text, int ic)
1741{
1742 int matches = FALSE;
1743 char_u *save_cpo;
1744 regmatch_T regmatch;
1745
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001746 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001747 save_cpo = p_cpo;
1748 p_cpo = (char_u *)"";
1749 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1750 if (regmatch.regprog != NULL)
1751 {
1752 regmatch.rm_ic = ic;
1753 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1754 vim_regfree(regmatch.regprog);
1755 }
1756 p_cpo = save_cpo;
1757 return matches;
1758}
1759
1760/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001761 * Handle a name followed by "(". Both for just "name(arg)" and for
1762 * "expr->name(arg)".
1763 * Returns OK or FAIL.
1764 */
1765 static int
1766eval_func(
1767 char_u **arg, // points to "(", will be advanced
1768 char_u *name,
1769 int name_len,
1770 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001771 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001772 typval_T *basetv) // "expr" for "expr->name(arg)"
1773{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001774 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001775 char_u *s = name;
1776 int len = name_len;
1777 partial_T *partial;
1778 int ret = OK;
1779
1780 if (!evaluate)
1781 check_vars(s, len);
1782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001783 // If "s" is the name of a variable of type VAR_FUNC
1784 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001785 s = deref_func_name(s, &len, &partial, !evaluate);
1786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001787 // Need to make a copy, in case evaluating the arguments makes
1788 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001789 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001790 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001791 ret = FAIL;
1792 else
1793 {
1794 funcexe_T funcexe;
1795
1796 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001797 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001798 funcexe.firstline = curwin->w_cursor.lnum;
1799 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001800 funcexe.evaluate = evaluate;
1801 funcexe.partial = partial;
1802 funcexe.basetv = basetv;
1803 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1804 }
1805 vim_free(s);
1806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001807 // If evaluate is FALSE rettv->v_type was not set in
1808 // get_func_tv, but it's needed in handle_subscript() to parse
1809 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001810 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1811 {
1812 rettv->vval.v_string = NULL;
1813 rettv->v_type = VAR_FUNC;
1814 }
1815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001816 // Stop the expression evaluation when immediately
1817 // aborting on error, or when an interrupt occurred or
1818 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001819 if (evaluate && aborting())
1820 {
1821 if (ret == OK)
1822 clear_tv(rettv);
1823 ret = FAIL;
1824 }
1825 return ret;
1826}
1827
1828/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001829 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1830 * and there is a next line, return the next line (skipping blanks) and set
1831 * "getnext".
1832 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1833 * "arg" must point somewhere inside a line, not at the start.
1834 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001835 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001836eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1837{
1838 *getnext = FALSE;
1839 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1840 && evalarg != NULL
1841 && evalarg->eval_cookie != NULL
1842 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
1843 && (*arg == '"' || *arg == '#')))
1844 && source_nextline(evalarg->eval_cookie) != NULL)
1845 {
1846 char_u *p = source_nextline(evalarg->eval_cookie);
1847
1848 if (p != NULL)
1849 {
1850 *getnext = TRUE;
1851 return skipwhite(p);
1852 }
1853 }
1854 return arg;
1855}
1856
1857/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001858 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001859 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001860 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001861eval_next_line(evalarg_T *evalarg)
1862{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001863 garray_T *gap = &evalarg->eval_ga;
1864 char_u *line;
1865
1866 line = getsourceline(0, evalarg->eval_cookie, 0, TRUE);
1867 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1868 {
1869 // Going to concatenate the lines after parsing.
1870 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1871 ++gap->ga_len;
1872 }
1873 else
1874 {
1875 vim_free(evalarg->eval_tofree);
1876 evalarg->eval_tofree = line;
1877 }
1878 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001879}
1880
Bram Moolenaar9215f012020-06-27 21:18:00 +02001881 char_u *
1882skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1883{
1884 int getnext;
1885 char_u *p = skipwhite(arg);
1886
1887 eval_next_non_blank(p, evalarg, &getnext);
1888 if (getnext)
1889 return eval_next_line(evalarg);
1890 return p;
1891}
1892
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001893/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001894 * After using "evalarg" filled from "eap" free the memory.
1895 */
1896 void
1897clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1898{
1899 if (evalarg != NULL && eap != NULL && evalarg->eval_tofree != NULL)
1900 {
1901 // We may need to keep the original command line, e.g. for
1902 // ":let" it has the variable names. But we may also need the
1903 // new one, "nextcmd" points into it. Keep both.
1904 vim_free(eap->cmdline_tofree);
1905 eap->cmdline_tofree = *eap->cmdlinep;
1906 *eap->cmdlinep = evalarg->eval_tofree;
1907 evalarg->eval_tofree = NULL;
1908 }
1909}
1910
1911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1915 */
1916
1917/*
1918 * Handle zero level expression.
1919 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001921 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001922 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 * Return OK or FAIL.
1924 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001925 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001926eval0(
1927 char_u *arg,
1928 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001929 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001930 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
1932 int ret;
1933 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001934 int did_emsg_before = did_emsg;
1935 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001936 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001939 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001940
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001941 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
1943 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001944 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 /*
1946 * Report the invalid expression unless the expression evaluation has
1947 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001948 * exception, or we already gave a more specific error.
1949 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001951 if (!aborting()
1952 && did_emsg == did_emsg_before
1953 && called_emsg == called_emsg_before
1954 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001955 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 ret = FAIL;
1957 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001958
1959 if (eap != NULL)
1960 eap->nextcmd = check_nextcmd(p);
1961
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001962 clear_evalarg(evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 return ret;
1965}
1966
1967/*
1968 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001969 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 *
1971 * "arg" must point to the first non-white of the expression.
1972 * "arg" is advanced to the next non-white after the recognized expression.
1973 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001974 * Note: "rettv.v_lock" is not set.
1975 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 * Return OK or FAIL.
1977 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001978 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001979eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
Bram Moolenaar793648f2020-06-26 21:28:25 +02001981 char_u *p;
1982 int getnext;
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 /*
1985 * Get the first variable.
1986 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001987 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 return FAIL;
1989
Bram Moolenaar793648f2020-06-26 21:28:25 +02001990 p = eval_next_non_blank(*arg, evalarg, &getnext);
1991 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001993 int result;
1994 typval_T var2;
1995 evalarg_T nested_evalarg;
1996 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02001997 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001998
Bram Moolenaar793648f2020-06-26 21:28:25 +02001999 if (getnext)
2000 *arg = eval_next_line(evalarg);
2001
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002002 if (evalarg == NULL)
2003 {
2004 CLEAR_FIELD(nested_evalarg);
2005 orig_flags = 0;
2006 }
2007 else
2008 {
2009 nested_evalarg = *evalarg;
2010 orig_flags = evalarg->eval_flags;
2011 }
2012
Bram Moolenaar7acde512020-06-24 23:02:40 +02002013 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002015 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002017 int error = FALSE;
2018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002019 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002022 if (error)
2023 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025
2026 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002027 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002029 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002030 nested_evalarg.eval_flags = result ? orig_flags
2031 : orig_flags & ~EVAL_EVALUATE;
2032 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 return FAIL;
2034
2035 /*
2036 * Check for the ":".
2037 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002038 p = eval_next_non_blank(*arg, evalarg, &getnext);
2039 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 return FAIL;
2045 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002046 if (getnext)
2047 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002050 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002052 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002053 nested_evalarg.eval_flags = !result ? orig_flags
2054 : orig_flags & ~EVAL_EVALUATE;
2055 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 return FAIL;
2060 }
2061 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064
2065 return OK;
2066}
2067
2068/*
2069 * Handle first level expression:
2070 * expr2 || expr2 || expr2 logical OR
2071 *
2072 * "arg" must point to the first non-white of the expression.
2073 * "arg" is advanced to the next non-white after the recognized expression.
2074 *
2075 * Return OK or FAIL.
2076 */
2077 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002078eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002080 char_u *p;
2081 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 long result;
2084 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002085 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 /*
2088 * Get the first variable.
2089 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 return FAIL;
2092
2093 /*
2094 * Repeat until there is no following "||".
2095 */
2096 first = TRUE;
2097 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002098 p = eval_next_non_blank(*arg, evalarg, &getnext);
2099 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101 evalarg_T nested_evalarg;
2102 int evaluate;
2103 int orig_flags;
2104
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002105 if (getnext)
2106 *arg = eval_next_line(evalarg);
2107
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108 if (evalarg == NULL)
2109 {
2110 CLEAR_FIELD(nested_evalarg);
2111 orig_flags = 0;
2112 evaluate = FALSE;
2113 }
2114 else
2115 {
2116 nested_evalarg = *evalarg;
2117 orig_flags = evalarg->eval_flags;
2118 evaluate = orig_flags & EVAL_EVALUATE;
2119 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 if (evaluate && first)
2122 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002123 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002126 if (error)
2127 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 first = FALSE;
2129 }
2130
2131 /*
2132 * Get the second variable.
2133 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002134 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002135 nested_evalarg.eval_flags = !result ? orig_flags
2136 : orig_flags & ~EVAL_EVALUATE;
2137 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 return FAIL;
2139
2140 /*
2141 * Compute the result.
2142 */
2143 if (evaluate && !result)
2144 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002145 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002148 if (error)
2149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151 if (evaluate)
2152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 rettv->v_type = VAR_NUMBER;
2154 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002156
2157 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159
2160 return OK;
2161}
2162
2163/*
2164 * Handle second level expression:
2165 * expr3 && expr3 && expr3 logical AND
2166 *
2167 * "arg" must point to the first non-white of the expression.
2168 * "arg" is advanced to the next non-white after the recognized expression.
2169 *
2170 * Return OK or FAIL.
2171 */
2172 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002173eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002175 char_u *p;
2176 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002177 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 long result;
2179 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002180 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
2182 /*
2183 * Get the first variable.
2184 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002185 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 return FAIL;
2187
2188 /*
2189 * Repeat until there is no following "&&".
2190 */
2191 first = TRUE;
2192 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002193 p = eval_next_non_blank(*arg, evalarg, &getnext);
2194 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002196 evalarg_T nested_evalarg;
2197 int orig_flags;
2198 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002199
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002200 if (getnext)
2201 *arg = eval_next_line(evalarg);
2202
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002203 if (evalarg == NULL)
2204 {
2205 CLEAR_FIELD(nested_evalarg);
2206 orig_flags = 0;
2207 evaluate = FALSE;
2208 }
2209 else
2210 {
2211 nested_evalarg = *evalarg;
2212 orig_flags = evalarg->eval_flags;
2213 evaluate = orig_flags & EVAL_EVALUATE;
2214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (evaluate && first)
2216 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002217 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002220 if (error)
2221 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 first = FALSE;
2223 }
2224
2225 /*
2226 * Get the second variable.
2227 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002228 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002229 nested_evalarg.eval_flags = result ? orig_flags
2230 : orig_flags & ~EVAL_EVALUATE;
2231 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 return FAIL;
2233
2234 /*
2235 * Compute the result.
2236 */
2237 if (evaluate && result)
2238 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002239 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002242 if (error)
2243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245 if (evaluate)
2246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 rettv->v_type = VAR_NUMBER;
2248 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002250
2251 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253
2254 return OK;
2255}
2256
2257/*
2258 * Handle third level expression:
2259 * var1 == var2
2260 * var1 =~ var2
2261 * var1 != var2
2262 * var1 !~ var2
2263 * var1 > var2
2264 * var1 >= var2
2265 * var1 < var2
2266 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002267 * var1 is var2
2268 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 *
2270 * "arg" must point to the first non-white of the expression.
2271 * "arg" is advanced to the next non-white after the recognized expression.
2272 *
2273 * Return OK or FAIL.
2274 */
2275 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002276eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277{
Bram Moolenaar33570922005-01-25 22:26:29 +00002278 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002280 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002282 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 /*
2287 * Get the first variable.
2288 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002289 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 return FAIL;
2291
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002292 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 switch (p[0])
2294 {
2295 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002296 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002298 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 break;
2300 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002301 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002303 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 break;
2305 case '>': if (p[1] != '=')
2306 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002307 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 len = 1;
2309 }
2310 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002311 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 break;
2313 case '<': if (p[1] != '=')
2314 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002315 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 len = 1;
2317 }
2318 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002319 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002321 case 'i': if (p[1] == 's')
2322 {
2323 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2324 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002325 i = p[len];
2326 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002327 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002328 }
2329 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331
2332 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002333 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002335 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002337 if (getnext)
2338 *arg = eval_next_line(evalarg);
2339
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002340 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (p[len] == '?')
2342 {
2343 ic = TRUE;
2344 ++len;
2345 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002346 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 else if (p[len] == '#')
2348 {
2349 ic = FALSE;
2350 ++len;
2351 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002352 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 else
2354 ic = p_ic;
2355
2356 /*
2357 * Get the second variable.
2358 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002359 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002360 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 return FAIL;
2364 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002365 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002366 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002367 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002368
2369 clear_tv(&var2);
2370 return ret;
2371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373
2374 return OK;
2375}
2376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 void
2378eval_addblob(typval_T *tv1, typval_T *tv2)
2379{
2380 blob_T *b1 = tv1->vval.v_blob;
2381 blob_T *b2 = tv2->vval.v_blob;
2382 blob_T *b = blob_alloc();
2383 int i;
2384
2385 if (b != NULL)
2386 {
2387 for (i = 0; i < blob_len(b1); i++)
2388 ga_append(&b->bv_ga, blob_get(b1, i));
2389 for (i = 0; i < blob_len(b2); i++)
2390 ga_append(&b->bv_ga, blob_get(b2, i));
2391
2392 clear_tv(tv1);
2393 rettv_blob_set(tv1, b);
2394 }
2395}
2396
2397 int
2398eval_addlist(typval_T *tv1, typval_T *tv2)
2399{
2400 typval_T var3;
2401
2402 // concatenate Lists
2403 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2404 {
2405 clear_tv(tv1);
2406 clear_tv(tv2);
2407 return FAIL;
2408 }
2409 clear_tv(tv1);
2410 *tv1 = var3;
2411 return OK;
2412}
2413
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414/*
2415 * Handle fourth level expression:
2416 * + number addition
2417 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002418 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002419 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 *
2421 * "arg" must point to the first non-white of the expression.
2422 * "arg" is advanced to the next non-white after the recognized expression.
2423 *
2424 * Return OK or FAIL.
2425 */
2426 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431 /*
2432 * Get the first variable.
2433 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return FAIL;
2436
2437 /*
2438 * Repeat computing, until no '+', '-' or '.' is following.
2439 */
2440 for (;;)
2441 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442 int getnext;
2443 char_u *p;
2444 int op;
2445 int concat;
2446 typval_T var2;
2447
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002448 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002449 p = eval_next_non_blank(*arg, evalarg, &getnext);
2450 op = *p;
2451 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002452 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002454 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002455 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002457 if ((op != '+' || (rettv->v_type != VAR_LIST
2458 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002459#ifdef FEAT_FLOAT
2460 && (op == '.' || rettv->v_type != VAR_FLOAT)
2461#endif
2462 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002464 // For "list + ...", an illegal use of the first operand as
2465 // a number cannot be determined before evaluating the 2nd
2466 // operand: if this is also a list, all is ok.
2467 // For "something . ...", "something - ..." or "non-list + ...",
2468 // we know that the first operand needs to be a string or number
2469 // without evaluating the 2nd operand. So check before to avoid
2470 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002471 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 {
2473 clear_tv(rettv);
2474 return FAIL;
2475 }
2476 }
2477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 /*
2479 * Get the second variable.
2480 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002481 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2482 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002483 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 return FAIL;
2488 }
2489
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002490 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
2492 /*
2493 * Compute the result.
2494 */
2495 if (op == '.')
2496 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002497 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2498 char_u *s1 = tv_get_string_buf(rettv, buf1);
2499 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002501 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 {
2503 clear_tv(rettv);
2504 clear_tv(&var2);
2505 return FAIL;
2506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 clear_tv(rettv);
2509 rettv->v_type = VAR_STRING;
2510 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002512 else if (op == '+' && rettv->v_type == VAR_BLOB
2513 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002515 else if (op == '+' && rettv->v_type == VAR_LIST
2516 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002517 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002519 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 else
2522 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002523 int error = FALSE;
2524 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002525#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002526 float_T f1 = 0, f2 = 0;
2527
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002528 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002529 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002530 f1 = rettv->vval.v_float;
2531 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002533 else
2534#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002535 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002536 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002537 if (error)
2538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002539 // This can only happen for "list + non-list". For
2540 // "non-list + ..." or "something - ...", we returned
2541 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002542 clear_tv(rettv);
2543 return FAIL;
2544 }
2545#ifdef FEAT_FLOAT
2546 if (var2.v_type == VAR_FLOAT)
2547 f1 = n1;
2548#endif
2549 }
2550#ifdef FEAT_FLOAT
2551 if (var2.v_type == VAR_FLOAT)
2552 {
2553 f2 = var2.vval.v_float;
2554 n2 = 0;
2555 }
2556 else
2557#endif
2558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002559 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002560 if (error)
2561 {
2562 clear_tv(rettv);
2563 clear_tv(&var2);
2564 return FAIL;
2565 }
2566#ifdef FEAT_FLOAT
2567 if (rettv->v_type == VAR_FLOAT)
2568 f2 = n2;
2569#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002572
2573#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002574 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002575 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2576 {
2577 if (op == '+')
2578 f1 = f1 + f2;
2579 else
2580 f1 = f1 - f2;
2581 rettv->v_type = VAR_FLOAT;
2582 rettv->vval.v_float = f1;
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002585#endif
2586 {
2587 if (op == '+')
2588 n1 = n1 + n2;
2589 else
2590 n1 = n1 - n2;
2591 rettv->v_type = VAR_NUMBER;
2592 rettv->vval.v_number = n1;
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597 }
2598 return OK;
2599}
2600
2601/*
2602 * Handle fifth level expression:
2603 * * number multiplication
2604 * / number division
2605 * % number modulo
2606 *
2607 * "arg" must point to the first non-white of the expression.
2608 * "arg" is advanced to the next non-white after the recognized expression.
2609 *
2610 * Return OK or FAIL.
2611 */
2612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002613eval6(
2614 char_u **arg,
2615 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002616 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002617 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
Bram Moolenaar33570922005-01-25 22:26:29 +00002619 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002621 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002622#ifdef FEAT_FLOAT
2623 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002624 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002625#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002626 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
2628 /*
2629 * Get the first variable.
2630 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002631 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 return FAIL;
2633
2634 /*
2635 * Repeat computing, until no '*', '/' or '%' is following.
2636 */
2637 for (;;)
2638 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002639 int evaluate = evalarg == NULL ? 0
2640 : (evalarg->eval_flags & EVAL_EVALUATE);
2641 int getnext;
2642
2643 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002644 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002646 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002647 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002649 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002651#ifdef FEAT_FLOAT
2652 if (rettv->v_type == VAR_FLOAT)
2653 {
2654 f1 = rettv->vval.v_float;
2655 use_float = TRUE;
2656 n1 = 0;
2657 }
2658 else
2659#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002660 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002662 if (error)
2663 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665 else
2666 n1 = 0;
2667
2668 /*
2669 * Get the second variable.
2670 */
2671 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002672 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 return FAIL;
2674
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002675 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002677#ifdef FEAT_FLOAT
2678 if (var2.v_type == VAR_FLOAT)
2679 {
2680 if (!use_float)
2681 {
2682 f1 = n1;
2683 use_float = TRUE;
2684 }
2685 f2 = var2.vval.v_float;
2686 n2 = 0;
2687 }
2688 else
2689#endif
2690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002691 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002692 clear_tv(&var2);
2693 if (error)
2694 return FAIL;
2695#ifdef FEAT_FLOAT
2696 if (use_float)
2697 f2 = n2;
2698#endif
2699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 /*
2702 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002703 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002705#ifdef FEAT_FLOAT
2706 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002708 if (op == '*')
2709 f1 = f1 * f2;
2710 else if (op == '/')
2711 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002712# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002713 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002714 if (f2 == 0.0)
2715 {
2716 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002717 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002718 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002719 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002720 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002721 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002722 }
2723 else
2724 f1 = f1 / f2;
2725# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002726 // We rely on the floating point library to handle divide
2727 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002728 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002729# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002732 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002734 return FAIL;
2735 }
2736 rettv->v_type = VAR_FLOAT;
2737 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 }
2739 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002742 if (op == '*')
2743 n1 = n1 * n2;
2744 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002745 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002747 n1 = num_modulus(n1, n2);
2748
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002749 rettv->v_type = VAR_NUMBER;
2750 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 }
2753 }
2754
2755 return OK;
2756}
2757
2758/*
2759 * Handle sixth level expression:
2760 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002761 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002762 * "string" string constant
2763 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 * &option-name option value
2765 * @r register contents
2766 * identifier variable value
2767 * function() function call
2768 * $VAR environment variable
2769 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002770 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002772 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002773 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 *
2775 * Also handle:
2776 * ! in front logical NOT
2777 * - in front unary minus
2778 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 * trailing [] subscript in String or List
2780 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002781 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 *
2783 * "arg" must point to the first non-white of the expression.
2784 * "arg" is advanced to the next non-white after the recognized expression.
2785 *
2786 * Return OK or FAIL.
2787 */
2788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002789eval7(
2790 char_u **arg,
2791 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002792 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002795 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2796 int evaluate = evalarg != NULL
2797 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 int len;
2799 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 char_u *start_leader, *end_leader;
2801 int ret = OK;
2802 char_u *alias;
2803
2804 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002805 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002806 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002808 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
2810 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002811 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 */
2813 start_leader = *arg;
2814 while (**arg == '!' || **arg == '-' || **arg == '+')
2815 *arg = skipwhite(*arg + 1);
2816 end_leader = *arg;
2817
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002818 if (**arg == '.' && (!isdigit(*(*arg + 1))
2819#ifdef FEAT_FLOAT
2820 || current_sctx.sc_version < 2
2821#endif
2822 ))
2823 {
2824 semsg(_(e_invexpr2), *arg);
2825 ++*arg;
2826 return FAIL;
2827 }
2828
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 switch (**arg)
2830 {
2831 /*
2832 * Number constant.
2833 */
2834 case '0':
2835 case '1':
2836 case '2':
2837 case '3':
2838 case '4':
2839 case '5':
2840 case '6':
2841 case '7':
2842 case '8':
2843 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002845
2846 // Apply prefixed "-" and "+" now. Matters especially when
2847 // "->" follows.
2848 if (ret == OK && evaluate && end_leader > start_leader)
2849 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 break;
2851
2852 /*
2853 * String constant: "string".
2854 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002855 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 break;
2857
2858 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002861 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002862 break;
2863
2864 /*
2865 * List: [expr, expr]
2866 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002867 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 break;
2869
2870 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002871 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002872 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002873 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002874 {
2875 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002876 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002877 }
2878 else
2879 ret = NOTDONE;
2880 break;
2881
2882 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002883 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002884 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002886 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002887 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002888 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 break;
2890
2891 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002892 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002894 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 break;
2896
2897 /*
2898 * Environment variable: $VAR.
2899 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002900 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 break;
2902
2903 /*
2904 * Register contents: @r.
2905 */
2906 case '@': ++*arg;
2907 if (evaluate)
2908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002910 rettv->vval.v_string = get_reg_contents(**arg,
2911 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913 if (**arg != NUL)
2914 ++*arg;
2915 break;
2916
2917 /*
2918 * nested expression: (expression).
2919 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002921 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002923
Bram Moolenaar9215f012020-06-27 21:18:00 +02002924 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002925 if (**arg == ')')
2926 ++*arg;
2927 else if (ret == OK)
2928 {
2929 emsg(_(e_missing_close));
2930 clear_tv(rettv);
2931 ret = FAIL;
2932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934 break;
2935
Bram Moolenaar8c711452005-01-14 21:53:12 +00002936 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 break;
2938 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002939
2940 if (ret == NOTDONE)
2941 {
2942 /*
2943 * Must be a variable or function name.
2944 * Can also be a curly-braces kind of name: {expr}.
2945 */
2946 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002947 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002948 if (alias != NULL)
2949 s = alias;
2950
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002951 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002952 ret = FAIL;
2953 else
2954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002955 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002956 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002957 else if (flags & EVAL_CONSTANT)
2958 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002959 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002960 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002962 {
2963 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002965 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002966 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002967 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002968 }
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 *arg = skipwhite(*arg);
2971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002972 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2973 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002974 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002975 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
2977 /*
2978 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2979 */
2980 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002981 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002982 return ret;
2983}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002984
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002985/*
2986 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002987 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002988 * Adjusts "end_leaderp" until it is at "start_leader".
2989 */
2990 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002991eval7_leader(
2992 typval_T *rettv,
2993 int numeric_only,
2994 char_u *start_leader,
2995 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002996{
2997 char_u *end_leader = *end_leaderp;
2998 int ret = OK;
2999 int error = FALSE;
3000 varnumber_T val = 0;
3001#ifdef FEAT_FLOAT
3002 float_T f = 0.0;
3003
3004 if (rettv->v_type == VAR_FLOAT)
3005 f = rettv->vval.v_float;
3006 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003007#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003008 val = tv_get_number_chk(rettv, &error);
3009 if (error)
3010 {
3011 clear_tv(rettv);
3012 ret = FAIL;
3013 }
3014 else
3015 {
3016 while (end_leader > start_leader)
3017 {
3018 --end_leader;
3019 if (*end_leader == '!')
3020 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003021 if (numeric_only)
3022 {
3023 ++end_leader;
3024 break;
3025 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003026#ifdef FEAT_FLOAT
3027 if (rettv->v_type == VAR_FLOAT)
3028 f = !f;
3029 else
3030#endif
3031 val = !val;
3032 }
3033 else if (*end_leader == '-')
3034 {
3035#ifdef FEAT_FLOAT
3036 if (rettv->v_type == VAR_FLOAT)
3037 f = -f;
3038 else
3039#endif
3040 val = -val;
3041 }
3042 }
3043#ifdef FEAT_FLOAT
3044 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003046 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003047 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003049 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003050#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003051 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003052 clear_tv(rettv);
3053 rettv->v_type = VAR_NUMBER;
3054 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003057 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 return ret;
3059}
3060
3061/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003062 * Call the function referred to in "rettv".
3063 */
3064 static int
3065call_func_rettv(
3066 char_u **arg,
3067 typval_T *rettv,
3068 int evaluate,
3069 dict_T *selfdict,
3070 typval_T *basetv)
3071{
3072 partial_T *pt = NULL;
3073 funcexe_T funcexe;
3074 typval_T functv;
3075 char_u *s;
3076 int ret;
3077
3078 // need to copy the funcref so that we can clear rettv
3079 if (evaluate)
3080 {
3081 functv = *rettv;
3082 rettv->v_type = VAR_UNKNOWN;
3083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003084 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003085 if (functv.v_type == VAR_PARTIAL)
3086 {
3087 pt = functv.vval.v_partial;
3088 s = partial_name(pt);
3089 }
3090 else
3091 s = functv.vval.v_string;
3092 }
3093 else
3094 s = (char_u *)"";
3095
Bram Moolenaara80faa82020-04-12 19:37:17 +02003096 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003097 funcexe.firstline = curwin->w_cursor.lnum;
3098 funcexe.lastline = curwin->w_cursor.lnum;
3099 funcexe.evaluate = evaluate;
3100 funcexe.partial = pt;
3101 funcexe.selfdict = selfdict;
3102 funcexe.basetv = basetv;
3103 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003105 // Clear the funcref afterwards, so that deleting it while
3106 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003107 if (evaluate)
3108 clear_tv(&functv);
3109
3110 return ret;
3111}
3112
3113/*
3114 * Evaluate "->method()".
3115 * "*arg" points to the '-'.
3116 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3117 */
3118 static int
3119eval_lambda(
3120 char_u **arg,
3121 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003122 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003123 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003124{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003125 int evaluate = evalarg != NULL
3126 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003127 typval_T base = *rettv;
3128 int ret;
3129
3130 // Skip over the ->.
3131 *arg += 2;
3132 rettv->v_type = VAR_UNKNOWN;
3133
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003134 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003135 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003136 return FAIL;
3137 else if (**arg != '(')
3138 {
3139 if (verbose)
3140 {
3141 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003142 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003143 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003145 }
3146 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003147 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003148 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003149 else
3150 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3151
3152 // Clear the funcref afterwards, so that deleting it while
3153 // evaluating the arguments is possible (see test55).
3154 if (evaluate)
3155 clear_tv(&base);
3156
3157 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003158}
3159
3160/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003161 * Evaluate "->method()".
3162 * "*arg" points to the '-'.
3163 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3164 */
3165 static int
3166eval_method(
3167 char_u **arg,
3168 typval_T *rettv,
3169 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003170 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003171{
3172 char_u *name;
3173 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003174 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003175 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003176 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003177
3178 // Skip over the ->.
3179 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003180 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003181
Bram Moolenaarac92e252019-08-03 21:58:38 +02003182 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003183 len = get_name_len(arg, &alias, evaluate, TRUE);
3184 if (alias != NULL)
3185 name = alias;
3186
3187 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003188 {
3189 if (verbose)
3190 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003191 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003192 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003193 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003194 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003195 if (**arg != '(')
3196 {
3197 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003199 ret = FAIL;
3200 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003201 else if (VIM_ISWHITE((*arg)[-1]))
3202 {
3203 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003204 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003205 ret = FAIL;
3206 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003207 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003208 ret = eval_func(arg, name, len, rettv,
3209 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003210 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003211
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003212 // Clear the funcref afterwards, so that deleting it while
3213 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003214 if (evaluate)
3215 clear_tv(&base);
3216
Bram Moolenaarac92e252019-08-03 21:58:38 +02003217 return ret;
3218}
3219
3220/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003221 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3222 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003223 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3224 */
3225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003226eval_index(
3227 char_u **arg,
3228 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003229 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003230 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003231{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003232 int evaluate = evalarg != NULL
3233 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003234 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003236 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003237 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003238 long len = -1;
3239 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003240 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003241 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003242
Bram Moolenaara03f2332016-02-06 18:09:59 +01003243 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003244 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003245 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003246 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003247 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003248 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003249 return FAIL;
3250 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003251#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003252 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003253 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003254 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003255#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003256 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003257 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003258 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003259 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003260 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003261 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003262 return FAIL;
3263 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003264 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003266 if (evaluate)
3267 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003268 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003269
3270 case VAR_STRING:
3271 case VAR_NUMBER:
3272 case VAR_LIST:
3273 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003274 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003275 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003276 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003277
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003278 init_tv(&var1);
3279 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003280 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003281 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003282 /*
3283 * dict.name
3284 */
3285 key = *arg + 1;
3286 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3287 ;
3288 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003289 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003290 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003291 }
3292 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003294 /*
3295 * something[idx]
3296 *
3297 * Get the (first) variable from inside the [].
3298 */
3299 *arg = skipwhite(*arg + 1);
3300 if (**arg == ':')
3301 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003302 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003303 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003304 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003305 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003306 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003307 clear_tv(&var1);
3308 return FAIL;
3309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003310
3311 /*
3312 * Get the second variable from inside the [:].
3313 */
3314 if (**arg == ':')
3315 {
3316 range = TRUE;
3317 *arg = skipwhite(*arg + 1);
3318 if (**arg == ']')
3319 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003320 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003322 if (!empty1)
3323 clear_tv(&var1);
3324 return FAIL;
3325 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003326 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003328 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003329 if (!empty1)
3330 clear_tv(&var1);
3331 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003332 return FAIL;
3333 }
3334 }
3335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003336 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003337 if (**arg != ']')
3338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003339 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003340 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003341 clear_tv(&var1);
3342 if (range)
3343 clear_tv(&var2);
3344 return FAIL;
3345 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003346 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003347 }
3348
3349 if (evaluate)
3350 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003351 n1 = 0;
3352 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003353 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003354 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003355 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003356 }
3357 if (range)
3358 {
3359 if (empty2)
3360 n2 = -1;
3361 else
3362 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003363 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003364 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003365 }
3366 }
3367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003368 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003369 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003370 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003371 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003373 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003374 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003375 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003376 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003377 case VAR_SPECIAL:
3378 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003379 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003380 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003381
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003382 case VAR_NUMBER:
3383 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003384 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003385 len = (long)STRLEN(s);
3386 if (range)
3387 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003388 // The resulting variable is a substring. If the indexes
3389 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003390 if (n1 < 0)
3391 {
3392 n1 = len + n1;
3393 if (n1 < 0)
3394 n1 = 0;
3395 }
3396 if (n2 < 0)
3397 n2 = len + n2;
3398 else if (n2 >= len)
3399 n2 = len;
3400 if (n1 >= len || n2 < 0 || n1 > n2)
3401 s = NULL;
3402 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003403 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003404 }
3405 else
3406 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003407 // The resulting variable is a string of a single
3408 // character. If the index is too big or negative the
3409 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003410 if (n1 >= len || n1 < 0)
3411 s = NULL;
3412 else
3413 s = vim_strnsave(s + n1, 1);
3414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003415 clear_tv(rettv);
3416 rettv->v_type = VAR_STRING;
3417 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003418 break;
3419
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003420 case VAR_BLOB:
3421 len = blob_len(rettv->vval.v_blob);
3422 if (range)
3423 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003424 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003425 // are out of range the result is empty.
3426 if (n1 < 0)
3427 {
3428 n1 = len + n1;
3429 if (n1 < 0)
3430 n1 = 0;
3431 }
3432 if (n2 < 0)
3433 n2 = len + n2;
3434 else if (n2 >= len)
3435 n2 = len - 1;
3436 if (n1 >= len || n2 < 0 || n1 > n2)
3437 {
3438 clear_tv(rettv);
3439 rettv->v_type = VAR_BLOB;
3440 rettv->vval.v_blob = NULL;
3441 }
3442 else
3443 {
3444 blob_T *blob = blob_alloc();
3445
3446 if (blob != NULL)
3447 {
3448 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3449 {
3450 blob_free(blob);
3451 return FAIL;
3452 }
3453 blob->bv_ga.ga_len = n2 - n1 + 1;
3454 for (i = n1; i <= n2; i++)
3455 blob_set(blob, i - n1,
3456 blob_get(rettv->vval.v_blob, i));
3457
3458 clear_tv(rettv);
3459 rettv_blob_set(rettv, blob);
3460 }
3461 }
3462 }
3463 else
3464 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003465 // The resulting variable is a byte value.
3466 // If the index is too big or negative that is an error.
3467 if (n1 < 0)
3468 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003469 if (n1 < len && n1 >= 0)
3470 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003471 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003472
3473 clear_tv(rettv);
3474 rettv->v_type = VAR_NUMBER;
3475 rettv->vval.v_number = v;
3476 }
3477 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003478 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003479 }
3480 break;
3481
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003482 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003483 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003484 if (n1 < 0)
3485 n1 = len + n1;
3486 if (!empty1 && (n1 < 0 || n1 >= len))
3487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003488 // For a range we allow invalid values and return an empty
3489 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003490 if (!range)
3491 {
3492 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003493 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003494 return FAIL;
3495 }
3496 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003497 }
3498 if (range)
3499 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003501
3502 if (n2 < 0)
3503 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003504 else if (n2 >= len)
3505 n2 = len - 1;
3506 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003507 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003508 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003509 if (l == NULL)
3510 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003511 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003512 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003513 }
3514 else
3515 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003516 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003517 clear_tv(rettv);
3518 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003519 }
3520 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003521
3522 case VAR_DICT:
3523 if (range)
3524 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003525 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003526 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003527 if (len == -1)
3528 clear_tv(&var1);
3529 return FAIL;
3530 }
3531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003533
3534 if (len == -1)
3535 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003536 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003537 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003538 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003539 clear_tv(&var1);
3540 return FAIL;
3541 }
3542 }
3543
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003544 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003545
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003546 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003547 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003548 if (len == -1)
3549 clear_tv(&var1);
3550 if (item == NULL)
3551 return FAIL;
3552
3553 copy_tv(&item->di_tv, &var1);
3554 clear_tv(rettv);
3555 *rettv = var1;
3556 }
3557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003558 }
3559 }
3560
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003561 return OK;
3562}
3563
3564/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003565 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003566 */
3567 char_u *
3568partial_name(partial_T *pt)
3569{
3570 if (pt->pt_name != NULL)
3571 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003572 if (pt->pt_func != NULL)
3573 return pt->pt_func->uf_name;
3574 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003575}
3576
Bram Moolenaarddecc252016-04-06 22:59:37 +02003577 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003578partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003579{
3580 int i;
3581
3582 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003583 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003584 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003585 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003586 if (pt->pt_name != NULL)
3587 {
3588 func_unref(pt->pt_name);
3589 vim_free(pt->pt_name);
3590 }
3591 else
3592 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003593
3594 if (pt->pt_funcstack != NULL)
3595 {
3596 // Decrease the reference count for the context of a closure. If down
3597 // to zero free it and clear the variables on the stack.
3598 if (--pt->pt_funcstack->fs_refcount == 0)
3599 {
3600 garray_T *gap = &pt->pt_funcstack->fs_ga;
3601 typval_T *stack = gap->ga_data;
3602
3603 for (i = 0; i < gap->ga_len; ++i)
3604 clear_tv(stack + i);
3605 ga_clear(gap);
3606 vim_free(pt->pt_funcstack);
3607 }
3608 pt->pt_funcstack = NULL;
3609 }
3610
Bram Moolenaarddecc252016-04-06 22:59:37 +02003611 vim_free(pt);
3612}
3613
3614/*
3615 * Unreference a closure: decrement the reference count and free it when it
3616 * becomes zero.
3617 */
3618 void
3619partial_unref(partial_T *pt)
3620{
3621 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003622 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003623}
3624
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003625/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003626 * Return the next (unique) copy ID.
3627 * Used for serializing nested structures.
3628 */
3629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003630get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003631{
3632 current_copyID += COPYID_INC;
3633 return current_copyID;
3634}
3635
3636/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003637 * Garbage collection for lists and dictionaries.
3638 *
3639 * We use reference counts to be able to free most items right away when they
3640 * are no longer used. But for composite items it's possible that it becomes
3641 * unused while the reference count is > 0: When there is a recursive
3642 * reference. Example:
3643 * :let l = [1, 2, 3]
3644 * :let d = {9: l}
3645 * :let l[1] = d
3646 *
3647 * Since this is quite unusual we handle this with garbage collection: every
3648 * once in a while find out which lists and dicts are not referenced from any
3649 * variable.
3650 *
3651 * Here is a good reference text about garbage collection (refers to Python
3652 * but it applies to all reference-counting mechanisms):
3653 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003654 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003655
3656/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003657 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003658 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003659 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003660 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003661 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003662garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003663{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003664 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003665 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003666 buf_T *buf;
3667 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003668 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003669 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003670
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003671 if (!testing)
3672 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003673 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003674 want_garbage_collect = FALSE;
3675 may_garbage_collect = FALSE;
3676 garbage_collect_at_exit = FALSE;
3677 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003678
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003679 // The execution stack can grow big, limit the size.
3680 if (exestack.ga_maxlen - exestack.ga_len > 500)
3681 {
3682 size_t new_len;
3683 char_u *pp;
3684 int n;
3685
3686 // Keep 150% of the current size, with a minimum of the growth size.
3687 n = exestack.ga_len / 2;
3688 if (n < exestack.ga_growsize)
3689 n = exestack.ga_growsize;
3690
3691 // Don't make it bigger though.
3692 if (exestack.ga_len + n < exestack.ga_maxlen)
3693 {
3694 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3695 pp = vim_realloc(exestack.ga_data, new_len);
3696 if (pp == NULL)
3697 return FAIL;
3698 exestack.ga_maxlen = exestack.ga_len + n;
3699 exestack.ga_data = pp;
3700 }
3701 }
3702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003703 // We advance by two because we add one for items referenced through
3704 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003705 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003706
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003707 /*
3708 * 1. Go through all accessible variables and mark all lists and dicts
3709 * with copyID.
3710 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003712 // Don't free variables in the previous_funccal list unless they are only
3713 // referenced through previous_funccal. This must be first, because if
3714 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003715 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003717 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003718 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003719
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003720 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003721 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003722 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3723 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003725 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003726 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003727 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3728 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003729 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003730 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3731 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003732#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003733 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003734 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3735 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003736 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003737 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003738 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3739 NULL, NULL);
3740#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003741
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003742 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003743 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003744 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3745 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003746 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003747 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003749 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003750 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003753 abort = abort || set_ref_in_functions(copyID);
3754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003755 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003758 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003759 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003760
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003761 // callbacks in buffers
3762 abort = abort || set_ref_in_buffers(copyID);
3763
Bram Moolenaar1dced572012-04-05 16:54:08 +02003764#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003765 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003766#endif
3767
Bram Moolenaardb913952012-06-29 12:54:53 +02003768#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003769 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003770#endif
3771
3772#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003773 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003774#endif
3775
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003776#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003777 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003778 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003779#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003780#ifdef FEAT_NETBEANS_INTG
3781 abort = abort || set_ref_in_nb_channel(copyID);
3782#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003783
Bram Moolenaare3188e22016-05-31 21:13:04 +02003784#ifdef FEAT_TIMERS
3785 abort = abort || set_ref_in_timer(copyID);
3786#endif
3787
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003788#ifdef FEAT_QUICKFIX
3789 abort = abort || set_ref_in_quickfix(copyID);
3790#endif
3791
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003792#ifdef FEAT_TERMINAL
3793 abort = abort || set_ref_in_term(copyID);
3794#endif
3795
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003796#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003797 abort = abort || set_ref_in_popups(copyID);
3798#endif
3799
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003800 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003801 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003802 /*
3803 * 2. Free lists and dictionaries that are not referenced.
3804 */
3805 did_free = free_unref_items(copyID);
3806
3807 /*
3808 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003810 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003812 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003813 else if (p_verbose > 0)
3814 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003815 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003816 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003817
3818 return did_free;
3819}
3820
3821/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003822 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003823 */
3824 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003825free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003826{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003827 int did_free = FALSE;
3828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003829 // Let all "free" functions know that we are here. This means no
3830 // dictionaries, lists, channels or jobs are to be freed, because we will
3831 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003832 in_free_unref_items = TRUE;
3833
3834 /*
3835 * PASS 1: free the contents of the items. We don't free the items
3836 * themselves yet, so that it is possible to decrement refcount counters
3837 */
3838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003839 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003840 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003842 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003843 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003844
3845#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003846 // Go through the list of jobs and free items without the copyID. This
3847 // must happen before doing channels, because jobs refer to channels, but
3848 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003849 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003851 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003852 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3853#endif
3854
3855 /*
3856 * PASS 2: free the items themselves.
3857 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003858 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003859 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003860
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003861#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003862 // Go through the list of jobs and free items without the copyID. This
3863 // must happen before doing channels, because jobs refer to channels, but
3864 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003865 free_unused_jobs(copyID, COPYID_MASK);
3866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003868 free_unused_channels(copyID, COPYID_MASK);
3869#endif
3870
3871 in_free_unref_items = FALSE;
3872
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003873 return did_free;
3874}
3875
3876/*
3877 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003878 * "list_stack" is used to add lists to be marked. Can be NULL.
3879 *
3880 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003881 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003882 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003883set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003884{
3885 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003886 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003887 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003888 hashtab_T *cur_ht;
3889 ht_stack_T *ht_stack = NULL;
3890 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003891
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003892 cur_ht = ht;
3893 for (;;)
3894 {
3895 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003897 // Mark each item in the hashtab. If the item contains a hashtab
3898 // it is added to ht_stack, if it contains a list it is added to
3899 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003900 todo = (int)cur_ht->ht_used;
3901 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3902 if (!HASHITEM_EMPTY(hi))
3903 {
3904 --todo;
3905 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3906 &ht_stack, list_stack);
3907 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003909
3910 if (ht_stack == NULL)
3911 break;
3912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003913 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003914 cur_ht = ht_stack->ht;
3915 tempitem = ht_stack;
3916 ht_stack = ht_stack->prev;
3917 free(tempitem);
3918 }
3919
3920 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003921}
3922
3923/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003924 * Mark a dict and its items with "copyID".
3925 * Returns TRUE if setting references failed somehow.
3926 */
3927 int
3928set_ref_in_dict(dict_T *d, int copyID)
3929{
3930 if (d != NULL && d->dv_copyID != copyID)
3931 {
3932 d->dv_copyID = copyID;
3933 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3934 }
3935 return FALSE;
3936}
3937
3938/*
3939 * Mark a list and its items with "copyID".
3940 * Returns TRUE if setting references failed somehow.
3941 */
3942 int
3943set_ref_in_list(list_T *ll, int copyID)
3944{
3945 if (ll != NULL && ll->lv_copyID != copyID)
3946 {
3947 ll->lv_copyID = copyID;
3948 return set_ref_in_list_items(ll, copyID, NULL);
3949 }
3950 return FALSE;
3951}
3952
3953/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003954 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003955 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3956 *
3957 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003958 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003959 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003960set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003961{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003962 listitem_T *li;
3963 int abort = FALSE;
3964 list_T *cur_l;
3965 list_stack_T *list_stack = NULL;
3966 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003967
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003968 cur_l = l;
3969 for (;;)
3970 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003971 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003972 // Mark each item in the list. If the item contains a hashtab
3973 // it is added to ht_stack, if it contains a list it is added to
3974 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003975 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
3976 abort = abort || set_ref_in_item(&li->li_tv, copyID,
3977 ht_stack, &list_stack);
3978 if (list_stack == NULL)
3979 break;
3980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003981 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003982 cur_l = list_stack->list;
3983 tempitem = list_stack;
3984 list_stack = list_stack->prev;
3985 free(tempitem);
3986 }
3987
3988 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003989}
3990
3991/*
3992 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003993 * "list_stack" is used to add lists to be marked. Can be NULL.
3994 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3995 *
3996 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003997 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003999set_ref_in_item(
4000 typval_T *tv,
4001 int copyID,
4002 ht_stack_T **ht_stack,
4003 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004004{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004005 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004006
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004007 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004008 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004009 dict_T *dd = tv->vval.v_dict;
4010
Bram Moolenaara03f2332016-02-06 18:09:59 +01004011 if (dd != NULL && dd->dv_copyID != copyID)
4012 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004013 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004014 dd->dv_copyID = copyID;
4015 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004016 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004017 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4018 }
4019 else
4020 {
4021 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4022 if (newitem == NULL)
4023 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004024 else
4025 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004026 newitem->ht = &dd->dv_hashtab;
4027 newitem->prev = *ht_stack;
4028 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004029 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004030 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004031 }
4032 }
4033 else if (tv->v_type == VAR_LIST)
4034 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004035 list_T *ll = tv->vval.v_list;
4036
Bram Moolenaara03f2332016-02-06 18:09:59 +01004037 if (ll != NULL && ll->lv_copyID != copyID)
4038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004039 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004040 ll->lv_copyID = copyID;
4041 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004042 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004043 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004044 }
4045 else
4046 {
4047 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004048 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004049 if (newitem == NULL)
4050 abort = TRUE;
4051 else
4052 {
4053 newitem->list = ll;
4054 newitem->prev = *list_stack;
4055 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004056 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004057 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004058 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004059 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004060 else if (tv->v_type == VAR_FUNC)
4061 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004062 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004063 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004064 else if (tv->v_type == VAR_PARTIAL)
4065 {
4066 partial_T *pt = tv->vval.v_partial;
4067 int i;
4068
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004069 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004070 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004071 // Didn't see this partial yet.
4072 pt->pt_copyID = copyID;
4073
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004074 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004075
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004076 if (pt->pt_dict != NULL)
4077 {
4078 typval_T dtv;
4079
4080 dtv.v_type = VAR_DICT;
4081 dtv.vval.v_dict = pt->pt_dict;
4082 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4083 }
4084
4085 for (i = 0; i < pt->pt_argc; ++i)
4086 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4087 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004088 if (pt->pt_funcstack != NULL)
4089 {
4090 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4091
4092 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4093 abort = abort || set_ref_in_item(stack + i, copyID,
4094 ht_stack, list_stack);
4095 }
4096
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004097 }
4098 }
4099#ifdef FEAT_JOB_CHANNEL
4100 else if (tv->v_type == VAR_JOB)
4101 {
4102 job_T *job = tv->vval.v_job;
4103 typval_T dtv;
4104
4105 if (job != NULL && job->jv_copyID != copyID)
4106 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004107 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004108 if (job->jv_channel != NULL)
4109 {
4110 dtv.v_type = VAR_CHANNEL;
4111 dtv.vval.v_channel = job->jv_channel;
4112 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4113 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004114 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004115 {
4116 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004117 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004118 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4119 }
4120 }
4121 }
4122 else if (tv->v_type == VAR_CHANNEL)
4123 {
4124 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004125 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004126 typval_T dtv;
4127 jsonq_T *jq;
4128 cbq_T *cq;
4129
4130 if (ch != NULL && ch->ch_copyID != copyID)
4131 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004132 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004133 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004134 {
4135 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4136 jq = jq->jq_next)
4137 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4138 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4139 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004140 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004141 {
4142 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004143 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004144 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4145 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004146 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004147 {
4148 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004149 dtv.vval.v_partial =
4150 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004151 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4152 }
4153 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004154 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004155 {
4156 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004157 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004158 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4159 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004160 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004161 {
4162 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004163 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004164 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4165 }
4166 }
4167 }
4168#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004169 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004170}
4171
Bram Moolenaar8c711452005-01-14 21:53:12 +00004172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004173 * Return a string with the string representation of a variable.
4174 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004175 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004176 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004177 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4178 * stings as "string()", otherwise does not put quotes around strings, as
4179 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004180 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4181 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004182 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004183 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004184 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004185echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004186 typval_T *tv,
4187 char_u **tofree,
4188 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004189 int copyID,
4190 int echo_style,
4191 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004192 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004193{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004194 static int recurse = 0;
4195 char_u *r = NULL;
4196
Bram Moolenaar33570922005-01-25 22:26:29 +00004197 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004198 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004199 if (!did_echo_string_emsg)
4200 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004201 // Only give this message once for a recursive call to avoid
4202 // flooding the user with errors. And stop iterating over lists
4203 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004204 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004205 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004207 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004208 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004209 }
4210 ++recurse;
4211
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 switch (tv->v_type)
4213 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004214 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004215 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004216 {
4217 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004218 r = tv->vval.v_string;
4219 if (r == NULL)
4220 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004221 }
4222 else
4223 {
4224 *tofree = string_quote(tv->vval.v_string, FALSE);
4225 r = *tofree;
4226 }
4227 break;
4228
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004229 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004230 if (echo_style)
4231 {
4232 *tofree = NULL;
4233 r = tv->vval.v_string;
4234 }
4235 else
4236 {
4237 *tofree = string_quote(tv->vval.v_string, TRUE);
4238 r = *tofree;
4239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004240 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004241
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004242 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004243 {
4244 partial_T *pt = tv->vval.v_partial;
4245 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004246 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004247 garray_T ga;
4248 int i;
4249 char_u *tf;
4250
4251 ga_init2(&ga, 1, 100);
4252 ga_concat(&ga, (char_u *)"function(");
4253 if (fname != NULL)
4254 {
4255 ga_concat(&ga, fname);
4256 vim_free(fname);
4257 }
4258 if (pt != NULL && pt->pt_argc > 0)
4259 {
4260 ga_concat(&ga, (char_u *)", [");
4261 for (i = 0; i < pt->pt_argc; ++i)
4262 {
4263 if (i > 0)
4264 ga_concat(&ga, (char_u *)", ");
4265 ga_concat(&ga,
4266 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4267 vim_free(tf);
4268 }
4269 ga_concat(&ga, (char_u *)"]");
4270 }
4271 if (pt != NULL && pt->pt_dict != NULL)
4272 {
4273 typval_T dtv;
4274
4275 ga_concat(&ga, (char_u *)", ");
4276 dtv.v_type = VAR_DICT;
4277 dtv.vval.v_dict = pt->pt_dict;
4278 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4279 vim_free(tf);
4280 }
4281 ga_concat(&ga, (char_u *)")");
4282
4283 *tofree = ga.ga_data;
4284 r = *tofree;
4285 break;
4286 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004287
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004288 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004289 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004290 break;
4291
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004292 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004293 if (tv->vval.v_list == NULL)
4294 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004295 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004296 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004297 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004298 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004299 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4300 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004301 {
4302 *tofree = NULL;
4303 r = (char_u *)"[...]";
4304 }
4305 else
4306 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004307 int old_copyID = tv->vval.v_list->lv_copyID;
4308
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004309 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004310 *tofree = list2string(tv, copyID, restore_copyID);
4311 if (restore_copyID)
4312 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004313 r = *tofree;
4314 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004315 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004316
Bram Moolenaar8c711452005-01-14 21:53:12 +00004317 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004318 if (tv->vval.v_dict == NULL)
4319 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004320 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004321 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004322 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004323 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004324 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4325 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004326 {
4327 *tofree = NULL;
4328 r = (char_u *)"{...}";
4329 }
4330 else
4331 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004332 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004333
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004334 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004335 *tofree = dict2string(tv, copyID, restore_copyID);
4336 if (restore_copyID)
4337 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004338 r = *tofree;
4339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004340 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004341
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004343 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004344 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004346 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004347 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004348 break;
4349
Bram Moolenaar835dc632016-02-07 14:27:38 +01004350 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004351 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004352 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004353 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004354 if (composite_val)
4355 {
4356 *tofree = string_quote(r, FALSE);
4357 r = *tofree;
4358 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004359 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004360
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004361 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004362#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004363 *tofree = NULL;
4364 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4365 r = numbuf;
4366 break;
4367#endif
4368
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004369 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004370 case VAR_SPECIAL:
4371 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004372 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004373 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004374 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004375
Bram Moolenaar8502c702014-06-17 12:51:16 +02004376 if (--recurse == 0)
4377 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004378 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004379}
4380
4381/*
4382 * Return a string with the string representation of a variable.
4383 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4384 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004385 * Does not put quotes around strings, as ":echo" displays values.
4386 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4387 * May return NULL.
4388 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004389 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004390echo_string(
4391 typval_T *tv,
4392 char_u **tofree,
4393 char_u *numbuf,
4394 int copyID)
4395{
4396 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4397}
4398
4399/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004400 * Return string "str" in ' quotes, doubling ' characters.
4401 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004402 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004403 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004404 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004405string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004406{
Bram Moolenaar33570922005-01-25 22:26:29 +00004407 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004408 char_u *p, *r, *s;
4409
Bram Moolenaar33570922005-01-25 22:26:29 +00004410 len = (function ? 13 : 3);
4411 if (str != NULL)
4412 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004413 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004414 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004415 if (*p == '\'')
4416 ++len;
4417 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004418 s = r = alloc(len);
4419 if (r != NULL)
4420 {
4421 if (function)
4422 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004423 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004424 r += 10;
4425 }
4426 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004427 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004428 if (str != NULL)
4429 for (p = str; *p != NUL; )
4430 {
4431 if (*p == '\'')
4432 *r++ = '\'';
4433 MB_COPY_CHAR(p, r);
4434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004435 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004436 if (function)
4437 *r++ = ')';
4438 *r++ = NUL;
4439 }
4440 return s;
4441}
4442
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004443#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004444/*
4445 * Convert the string "text" to a floating point number.
4446 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4447 * this always uses a decimal point.
4448 * Returns the length of the text that was consumed.
4449 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004451string2float(
4452 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004453 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004454{
4455 char *s = (char *)text;
4456 float_T f;
4457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004458 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004459 if (STRNICMP(text, "inf", 3) == 0)
4460 {
4461 *value = INFINITY;
4462 return 3;
4463 }
4464 if (STRNICMP(text, "-inf", 3) == 0)
4465 {
4466 *value = -INFINITY;
4467 return 4;
4468 }
4469 if (STRNICMP(text, "nan", 3) == 0)
4470 {
4471 *value = NAN;
4472 return 3;
4473 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004474 f = strtod(s, &s);
4475 *value = f;
4476 return (int)((char_u *)s - text);
4477}
4478#endif
4479
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004482 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004484 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004485var2fpos(
4486 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004487 int dollar_lnum, // TRUE when $ is last line
4488 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004490 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004492 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004494 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004495 if (varp->v_type == VAR_LIST)
4496 {
4497 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004498 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004499 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004500 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004501
4502 l = varp->vval.v_list;
4503 if (l == NULL)
4504 return NULL;
4505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004506 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004507 pos.lnum = list_find_nr(l, 0L, &error);
4508 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004509 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004511 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004512 pos.col = list_find_nr(l, 1L, &error);
4513 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004514 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004515 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004517 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004518 li = list_find(l, 1L);
4519 if (li != NULL && li->li_tv.v_type == VAR_STRING
4520 && li->li_tv.vval.v_string != NULL
4521 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4522 pos.col = len + 1;
4523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004524 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004525 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004526 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004527 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004529 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004530 pos.coladd = list_find_nr(l, 2L, &error);
4531 if (error)
4532 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004533
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004534 return &pos;
4535 }
4536
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004537 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004538 if (name == NULL)
4539 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004540 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004542 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004543 {
4544 if (VIsual_active)
4545 return &VIsual;
4546 return &curwin->w_cursor;
4547 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004550 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4552 return NULL;
4553 return pp;
4554 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004555
Bram Moolenaara5525202006-03-02 22:52:09 +00004556 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004557
Bram Moolenaar477933c2007-07-17 14:32:23 +00004558 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004559 {
4560 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004561 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004562 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004563 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004564 // In silent Ex mode topline is zero, but that's not a valid line
4565 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004566 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004567 return &pos;
4568 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004570 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004571 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004572 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004573 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004574 return &pos;
4575 }
4576 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004577 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004579 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 pos.lnum = curbuf->b_ml.ml_line_count;
4582 pos.col = 0;
4583 }
4584 else
4585 {
4586 pos.lnum = curwin->w_cursor.lnum;
4587 pos.col = (colnr_T)STRLEN(ml_get_curline());
4588 }
4589 return &pos;
4590 }
4591 return NULL;
4592}
4593
4594/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004595 * Convert list in "arg" into a position and optional file number.
4596 * When "fnump" is NULL there is no file number, only 3 items.
4597 * Note that the column is passed on as-is, the caller may want to decrement
4598 * it to use 1 for the first column.
4599 * Return FAIL when conversion is not possible, doesn't check the position for
4600 * validity.
4601 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004602 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004603list2fpos(
4604 typval_T *arg,
4605 pos_T *posp,
4606 int *fnump,
4607 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004608{
4609 list_T *l = arg->vval.v_list;
4610 long i = 0;
4611 long n;
4612
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004613 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4614 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004615 if (arg->v_type != VAR_LIST
4616 || l == NULL
4617 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004618 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004619 return FAIL;
4620
4621 if (fnump != NULL)
4622 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004623 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004624 if (n < 0)
4625 return FAIL;
4626 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004628 *fnump = n;
4629 }
4630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004631 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004632 if (n < 0)
4633 return FAIL;
4634 posp->lnum = n;
4635
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004636 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004637 if (n < 0)
4638 return FAIL;
4639 posp->col = n;
4640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004641 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004642 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004643 posp->coladd = 0;
4644 else
4645 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004646
Bram Moolenaar493c1782014-05-28 14:34:46 +02004647 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004648 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004649
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004650 return OK;
4651}
4652
4653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 * Get the length of an environment variable name.
4655 * Advance "arg" to the first character after the name.
4656 * Return 0 for error.
4657 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004658 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004659get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
4661 char_u *p;
4662 int len;
4663
4664 for (p = *arg; vim_isIDc(*p); ++p)
4665 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004666 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return 0;
4668
4669 len = (int)(p - *arg);
4670 *arg = p;
4671 return len;
4672}
4673
4674/*
4675 * Get the length of the name of a function or internal variable.
4676 * "arg" is advanced to the first non-white character after the name.
4677 * Return 0 if something is wrong.
4678 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004679 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004680get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681{
4682 char_u *p;
4683 int len;
4684
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004685 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004687 {
4688 if (*p == ':')
4689 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004690 // "s:" is start of "s:var", but "n:" is not and can be used in
4691 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004692 len = (int)(p - *arg);
4693 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4694 || len > 1)
4695 break;
4696 }
4697 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004698 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 return 0;
4700
4701 len = (int)(p - *arg);
4702 *arg = skipwhite(p);
4703
4704 return len;
4705}
4706
4707/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004708 * Get the length of the name of a variable or function.
4709 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004711 * Return -1 if curly braces expansion failed.
4712 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 * If the name contains 'magic' {}'s, expand them and return the
4714 * expanded name in an allocated string via 'alias' - caller must free.
4715 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004717get_name_len(
4718 char_u **arg,
4719 char_u **alias,
4720 int evaluate,
4721 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 char_u *p;
4725 char_u *expr_start;
4726 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
4730 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4731 && (*arg)[2] == (int)KE_SNR)
4732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004733 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 *arg += 3;
4735 return get_id_len(arg) + 3;
4736 }
4737 len = eval_fname_script(*arg);
4738 if (len > 0)
4739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004740 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 *arg += len;
4742 }
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004747 p = find_name_end(*arg, &expr_start, &expr_end,
4748 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 if (expr_start != NULL)
4750 {
4751 char_u *temp_string;
4752
4753 if (!evaluate)
4754 {
4755 len += (int)(p - *arg);
4756 *arg = skipwhite(p);
4757 return len;
4758 }
4759
4760 /*
4761 * Include any <SID> etc in the expanded string:
4762 * Thus the -len here.
4763 */
4764 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4765 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004766 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 *alias = temp_string;
4768 *arg = skipwhite(p);
4769 return (int)STRLEN(temp_string);
4770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004773 // Only give an error when there is something, otherwise it will be
4774 // reported at a higher level.
4775 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004776 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
4778 return len;
4779}
4780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781/*
4782 * Find the end of a variable or function name, taking care of magic braces.
4783 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4784 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004785 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004786 * Return a pointer to just after the name. Equal to "arg" if there is no
4787 * valid name.
4788 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004789 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004790find_name_end(
4791 char_u *arg,
4792 char_u **expr_start,
4793 char_u **expr_end,
4794 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004796 int mb_nest = 0;
4797 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004799 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004800 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004804 *expr_start = NULL;
4805 *expr_end = NULL;
4806 }
4807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004809 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4810 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004811 return arg;
4812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004813 for (p = arg; *p != NUL
4814 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004815 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004816 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004818 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004820 if (*p == '\'')
4821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004823 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004824 ;
4825 if (*p == NUL)
4826 break;
4827 }
4828 else if (*p == '"')
4829 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004830 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004831 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004832 if (*p == '\\' && p[1] != NUL)
4833 ++p;
4834 if (*p == NUL)
4835 break;
4836 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004837 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4838 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004839 // "s:" is start of "s:var", but "n:" is not and can be used in
4840 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004841 len = (int)(p - arg);
4842 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004843 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004844 break;
4845 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004846
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 if (*p == '[')
4850 ++br_nest;
4851 else if (*p == ']')
4852 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004854
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004855 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 if (*p == '{')
4858 {
4859 mb_nest++;
4860 if (expr_start != NULL && *expr_start == NULL)
4861 *expr_start = p;
4862 }
4863 else if (*p == '}')
4864 {
4865 mb_nest--;
4866 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4867 *expr_end = p;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871
4872 return p;
4873}
4874
4875/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004876 * Expands out the 'magic' {}'s in a variable/function name.
4877 * Note that this can call itself recursively, to deal with
4878 * constructs like foo{bar}{baz}{bam}
4879 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4880 * "in_start" ^
4881 * "expr_start" ^
4882 * "expr_end" ^
4883 * "in_end" ^
4884 *
4885 * Returns a new allocated string, which the caller must free.
4886 * Returns NULL for failure.
4887 */
4888 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004889make_expanded_name(
4890 char_u *in_start,
4891 char_u *expr_start,
4892 char_u *expr_end,
4893 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004894{
4895 char_u c1;
4896 char_u *retval = NULL;
4897 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004898
4899 if (expr_end == NULL || in_end == NULL)
4900 return NULL;
4901 *expr_start = NUL;
4902 *expr_end = NUL;
4903 c1 = *in_end;
4904 *in_end = NUL;
4905
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004906 temp_result = eval_to_string(expr_start + 1, FALSE);
4907 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004908 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004909 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4910 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004911 if (retval != NULL)
4912 {
4913 STRCPY(retval, in_start);
4914 STRCAT(retval, temp_result);
4915 STRCAT(retval, expr_end + 1);
4916 }
4917 }
4918 vim_free(temp_result);
4919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004920 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004921 *expr_start = '{';
4922 *expr_end = '}';
4923
4924 if (retval != NULL)
4925 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004926 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004927 if (expr_start != NULL)
4928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004929 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004930 temp_result = make_expanded_name(retval, expr_start,
4931 expr_end, temp_result);
4932 vim_free(retval);
4933 retval = temp_result;
4934 }
4935 }
4936
4937 return retval;
4938}
4939
4940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004942 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004944 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004945eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004947 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4948}
4949
4950/*
4951 * Return TRUE if character "c" can be used as the first character in a
4952 * variable or function name (excluding '{' and '}').
4953 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004955eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004956{
4957 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958}
4959
4960/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004961 * Handle:
4962 * - expr[expr], expr[expr:expr] subscript
4963 * - ".name" lookup
4964 * - function call with Funcref variable: func(expr)
4965 * - method call: var->method()
4966 *
4967 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004968 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004970handle_subscript(
4971 char_u **arg,
4972 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004973 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004974 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004975{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004976 int evaluate = evalarg != NULL
4977 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004978 int ret = OK;
4979 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004980
Bram Moolenaar61343f02019-07-20 21:11:13 +02004981 // "." is ".name" lookup when we found a dict or when evaluating and
4982 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004983 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02004984 && (((**arg == '['
4985 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02004986 || (!evaluate
4987 && (*arg)[1] != '.'
4988 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004989 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004990 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004991 && !VIM_ISWHITE(*(*arg - 1)))
4992 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 {
4994 if (**arg == '(')
4995 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004996 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01004997
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004998 // Stop the expression evaluation when immediately aborting on
4999 // error, or when an interrupt occurred or an exception was thrown
5000 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005001 if (aborting())
5002 {
5003 if (ret == OK)
5004 clear_tv(rettv);
5005 ret = FAIL;
5006 }
5007 dict_unref(selfdict);
5008 selfdict = NULL;
5009 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005010 else if (**arg == '-')
5011 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005012 if (ret == OK)
5013 {
5014 if ((*arg)[2] == '{')
5015 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005016 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005017 else
5018 // expr->name()
5019 ret = eval_method(arg, rettv, evaluate, verbose);
5020 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005021 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005022 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005023 {
5024 dict_unref(selfdict);
5025 if (rettv->v_type == VAR_DICT)
5026 {
5027 selfdict = rettv->vval.v_dict;
5028 if (selfdict != NULL)
5029 ++selfdict->dv_refcount;
5030 }
5031 else
5032 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005033 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005034 {
5035 clear_tv(rettv);
5036 ret = FAIL;
5037 }
5038 }
5039 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5042 // Don't do this when "Func" is already a partial that was bound
5043 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005044 if (selfdict != NULL
5045 && (rettv->v_type == VAR_FUNC
5046 || (rettv->v_type == VAR_PARTIAL
5047 && (rettv->vval.v_partial->pt_auto
5048 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005049 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005050
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005051 dict_unref(selfdict);
5052 return ret;
5053}
5054
5055/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005056 * Make a copy of an item.
5057 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005058 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5059 * reference to an already copied list/dict can be used.
5060 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005061 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005062 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005063item_copy(
5064 typval_T *from,
5065 typval_T *to,
5066 int deep,
5067 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005068{
5069 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005070 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005071
Bram Moolenaar33570922005-01-25 22:26:29 +00005072 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005074 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005075 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005076 }
5077 ++recurse;
5078
5079 switch (from->v_type)
5080 {
5081 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005083 case VAR_STRING:
5084 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005085 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005086 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005087 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005088 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005089 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005090 copy_tv(from, to);
5091 break;
5092 case VAR_LIST:
5093 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005094 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005095 if (from->vval.v_list == NULL)
5096 to->vval.v_list = NULL;
5097 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005099 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005100 to->vval.v_list = from->vval.v_list->lv_copylist;
5101 ++to->vval.v_list->lv_refcount;
5102 }
5103 else
5104 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5105 if (to->vval.v_list == NULL)
5106 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005107 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005108 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005110 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 case VAR_DICT:
5112 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005113 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005114 if (from->vval.v_dict == NULL)
5115 to->vval.v_dict = NULL;
5116 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005118 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005119 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5120 ++to->vval.v_dict->dv_refcount;
5121 }
5122 else
5123 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5124 if (to->vval.v_dict == NULL)
5125 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005126 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005127 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005128 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005130 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005131 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005132 }
5133 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005134 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005135}
5136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 void
5138echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5139{
5140 char_u *tofree;
5141 char_u numbuf[NUMBUFLEN];
5142 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5143
5144 if (*atstart)
5145 {
5146 *atstart = FALSE;
5147 // Call msg_start() after eval1(), evaluating the expression
5148 // may cause a message to appear.
5149 if (with_space)
5150 {
5151 // Mark the saved text as finishing the line, so that what
5152 // follows is displayed on a new line when scrolling back
5153 // at the more prompt.
5154 msg_sb_eol();
5155 msg_start();
5156 }
5157 }
5158 else if (with_space)
5159 msg_puts_attr(" ", echo_attr);
5160
5161 if (p != NULL)
5162 for ( ; *p != NUL && !got_int; ++p)
5163 {
5164 if (*p == '\n' || *p == '\r' || *p == TAB)
5165 {
5166 if (*p != TAB && *needclr)
5167 {
5168 // remove any text still there from the command
5169 msg_clr_eos();
5170 *needclr = FALSE;
5171 }
5172 msg_putchar_attr(*p, echo_attr);
5173 }
5174 else
5175 {
5176 if (has_mbyte)
5177 {
5178 int i = (*mb_ptr2len)(p);
5179
5180 (void)msg_outtrans_len_attr(p, i, echo_attr);
5181 p += i - 1;
5182 }
5183 else
5184 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5185 }
5186 }
5187 vim_free(tofree);
5188}
5189
Bram Moolenaare9a41262005-01-15 22:18:47 +00005190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 * ":echo expr1 ..." print each argument separated with a space, add a
5192 * newline at the end.
5193 * ":echon expr1 ..." print each argument plain.
5194 */
5195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005196ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
5198 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005199 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 char_u *p;
5201 int needclr = TRUE;
5202 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005203 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005204 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005205 evalarg_T evalarg;
5206
5207 CLEAR_FIELD(evalarg);
5208 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar7e8967f2020-06-27 21:56:17 +02005209 evalarg.eval_cookie = eap->getline == getsourceline ? eap->cookie : NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211 if (eap->skip)
5212 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005213 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005215 // If eval1() causes an error message the text from the command may
5216 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005217 need_clr_eos = needclr;
5218
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005220 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 /*
5223 * Report the invalid expression unless the expression evaluation
5224 * has been cancelled due to an aborting error, an interrupt, or an
5225 * exception.
5226 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005227 if (!aborting() && did_emsg == did_emsg_before
5228 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005229 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005230 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 break;
5232 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005233 need_clr_eos = FALSE;
5234
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005237
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 arg = skipwhite(arg);
5240 }
5241 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005242 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
5244 if (eap->skip)
5245 --emsg_skip;
5246 else
5247 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005248 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 if (needclr)
5250 msg_clr_eos();
5251 if (eap->cmdidx == CMD_echo)
5252 msg_end();
5253 }
5254}
5255
5256/*
5257 * ":echohl {name}".
5258 */
5259 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005260ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005262 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263}
5264
5265/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005266 * Returns the :echo attribute
5267 */
5268 int
5269get_echo_attr(void)
5270{
5271 return echo_attr;
5272}
5273
5274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 * ":execute expr1 ..." execute the result of an expression.
5276 * ":echomsg expr1 ..." Print a message
5277 * ":echoerr expr1 ..." Print an error
5278 * Each gets spaces around each argument and a newline at the end for
5279 * echo commands
5280 */
5281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005282ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
5284 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 int ret = OK;
5287 char_u *p;
5288 garray_T ga;
5289 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 ga_init2(&ga, 1, 80);
5292
5293 if (eap->skip)
5294 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005295 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005297 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5298 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 if (!eap->skip)
5302 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005303 char_u buf[NUMBUFLEN];
5304
5305 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005306 {
5307 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5308 {
5309 emsg(_(e_inval_string));
5310 p = NULL;
5311 }
5312 else
5313 p = tv_get_string_buf(&rettv, buf);
5314 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005315 else
5316 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005317 if (p == NULL)
5318 {
5319 clear_tv(&rettv);
5320 ret = FAIL;
5321 break;
5322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 len = (int)STRLEN(p);
5324 if (ga_grow(&ga, len + 2) == FAIL)
5325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005326 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 ret = FAIL;
5328 break;
5329 }
5330 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 ga.ga_len += len;
5334 }
5335
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005336 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 arg = skipwhite(arg);
5338 }
5339
5340 if (ret != FAIL && ga.ga_data != NULL)
5341 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005342 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 // Mark the already saved text as finishing the line, so that what
5345 // follows is displayed on a new line when scrolling back at the
5346 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005347 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005348 }
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005351 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005352 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005353 out_flush();
5354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 else if (eap->cmdidx == CMD_echoerr)
5356 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005357 int save_did_emsg = did_emsg;
5358
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005360 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 if (!force_abort)
5362 did_emsg = save_did_emsg;
5363 }
5364 else if (eap->cmdidx == CMD_execute)
5365 do_cmdline((char_u *)ga.ga_data,
5366 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5367 }
5368
5369 ga_clear(&ga);
5370
5371 if (eap->skip)
5372 --emsg_skip;
5373
5374 eap->nextcmd = check_nextcmd(arg);
5375}
5376
5377/*
5378 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5379 * "arg" points to the "&" or '+' when called, to "option" when returning.
5380 * Returns NULL when no option name found. Otherwise pointer to the char
5381 * after the option name.
5382 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005383 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005384find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
5386 char_u *p = *arg;
5387
5388 ++p;
5389 if (*p == 'g' && p[1] == ':')
5390 {
5391 *opt_flags = OPT_GLOBAL;
5392 p += 2;
5393 }
5394 else if (*p == 'l' && p[1] == ':')
5395 {
5396 *opt_flags = OPT_LOCAL;
5397 p += 2;
5398 }
5399 else
5400 *opt_flags = 0;
5401
5402 if (!ASCII_ISALPHA(*p))
5403 return NULL;
5404 *arg = p;
5405
5406 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005407 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else
5409 while (ASCII_ISALPHA(*p))
5410 ++p;
5411 return p;
5412}
5413
5414/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005415 * Display script name where an item was last set.
5416 * Should only be invoked when 'verbose' is non-zero.
5417 */
5418 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005419last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005420{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005421 char_u *p;
5422
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005423 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005424 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005425 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005426 if (p != NULL)
5427 {
5428 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005429 msg_puts(_("\n\tLast set from "));
5430 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005431 if (script_ctx.sc_lnum > 0)
5432 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005433 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005434 msg_outnum((long)script_ctx.sc_lnum);
5435 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005436 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005437 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005438 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005439 }
5440}
5441
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005442#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
5444/*
5445 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005446 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 * "flags" can be "g" to do a global substitute.
5448 * Returns an allocated string, NULL for error.
5449 */
5450 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005451do_string_sub(
5452 char_u *str,
5453 char_u *pat,
5454 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005455 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005456 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457{
5458 int sublen;
5459 regmatch_T regmatch;
5460 int i;
5461 int do_all;
5462 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005463 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 garray_T ga;
5465 char_u *ret;
5466 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005467 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005471 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
5473 ga_init2(&ga, 1, 200);
5474
5475 do_all = (flags[0] == 'g');
5476
5477 regmatch.rm_ic = p_ic;
5478 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5479 if (regmatch.regprog != NULL)
5480 {
5481 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005482 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5484 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005485 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005486 if (regmatch.startp[0] == regmatch.endp[0])
5487 {
5488 if (zero_width == regmatch.startp[0])
5489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005490 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005491 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005492 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5493 (size_t)i);
5494 ga.ga_len += i;
5495 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005496 continue;
5497 }
5498 zero_width = regmatch.startp[0];
5499 }
5500
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 /*
5502 * Get some space for a temporary buffer to do the substitution
5503 * into. It will contain:
5504 * - The text up to where the match is.
5505 * - The substituted text.
5506 * - The text after the match.
5507 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005508 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005509 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5511 {
5512 ga_clear(&ga);
5513 break;
5514 }
5515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005516 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 i = (int)(regmatch.startp[0] - tail);
5518 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005520 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 + ga.ga_len + i, TRUE, TRUE, FALSE);
5522 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005523 tail = regmatch.endp[0];
5524 if (*tail == NUL)
5525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 if (!do_all)
5527 break;
5528 }
5529
5530 if (ga.ga_data != NULL)
5531 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5532
Bram Moolenaar473de612013-06-08 18:19:48 +02005533 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535
5536 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5537 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005538 if (p_cpo == empty_option)
5539 p_cpo = save_cpo;
5540 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005541 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005542 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
5544 return ret;
5545}