blob: 82045675d01b66634072492cae7c34f11fec291e [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;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200173 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
174 {
175 evalarg.eval_getline = eap->getline;
176 evalarg.eval_cookie = eap->cookie;
177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
179 if (skip)
180 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200181 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 else
184 {
185 *error = FALSE;
186 if (!skip)
187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100188 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000189 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 }
191 }
192 if (skip)
193 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200194 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200196 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197}
198
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199/*
200 * Call eval1() and give an error message if not done at a lower level.
201 */
202 static int
203eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
204{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100205 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206 int ret;
207 int did_emsg_before = did_emsg;
208 int called_emsg_before = called_emsg;
209
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200210 ret = eval1(arg, rettv, evaluate ? &EVALARG_EVALUATE : NULL);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211 if (ret == FAIL)
212 {
213 // Report the invalid expression unless the expression evaluation has
214 // been cancelled due to an aborting error, an interrupt, or an
215 // exception, or we already gave a more specific error.
216 // Also check called_emsg for when using assert_fails().
217 if (!aborting() && did_emsg == did_emsg_before
218 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100219 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100220 }
221 return ret;
222}
223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200225 * Return whether a typval is a valid expression to pass to eval_expr_typval()
226 * or eval_expr_to_bool(). An empty string returns FALSE;
227 */
228 int
229eval_expr_valid_arg(typval_T *tv)
230{
231 return tv->v_type != VAR_UNKNOWN
232 && (tv->v_type != VAR_STRING
233 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
234}
235
236/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 * Evaluate an expression, which can be a function, partial or string.
238 * Pass arguments "argv[argc]".
239 * Return the result in "rettv" and OK or FAIL.
240 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200241 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100242eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
243{
244 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100245 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200246 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100247
248 if (expr->v_type == VAR_FUNC)
249 {
250 s = expr->vval.v_string;
251 if (s == NULL || *s == NUL)
252 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200253 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200254 funcexe.evaluate = TRUE;
255 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100256 return FAIL;
257 }
258 else if (expr->v_type == VAR_PARTIAL)
259 {
260 partial_T *partial = expr->vval.v_partial;
261
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200262 if (partial == NULL)
263 return FAIL;
264
Bram Moolenaar822ba242020-05-24 23:00:18 +0200265 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200266 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200268 if (call_def_function(partial->pt_func, argc, argv,
269 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270 return FAIL;
271 }
272 else
273 {
274 s = partial_name(partial);
275 if (s == NULL || *s == NUL)
276 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200277 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 funcexe.evaluate = TRUE;
279 funcexe.partial = partial;
280 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
281 return FAIL;
282 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100283 }
284 else
285 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100286 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100287 if (s == NULL)
288 return FAIL;
289 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100290 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100291 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100292 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200294 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100295 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100296 return FAIL;
297 }
298 }
299 return OK;
300}
301
302/*
303 * Like eval_to_bool() but using a typval_T instead of a string.
304 * Works for string, funcref and partial.
305 */
306 int
307eval_expr_to_bool(typval_T *expr, int *error)
308{
309 typval_T rettv;
310 int res;
311
312 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
313 {
314 *error = TRUE;
315 return FALSE;
316 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100317 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100318 clear_tv(&rettv);
319 return res;
320}
321
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322/*
323 * Top level evaluation function, returning a string. If "skip" is TRUE,
324 * only parsing to "nextcmd" is done, without reporting errors. Return
325 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
326 */
327 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100328eval_to_string_skip(
329 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200330 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100331 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332{
Bram Moolenaar33570922005-01-25 22:26:29 +0000333 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 char_u *retval;
335
336 if (skip)
337 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200338 if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 retval = NULL;
340 else
341 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100342 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000343 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 }
345 if (skip)
346 --emsg_skip;
347
348 return retval;
349}
350
351/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000352 * Skip over an expression at "*pp".
353 * Return FAIL for an error, OK otherwise.
354 */
355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100356skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000357{
Bram Moolenaar33570922005-01-25 22:26:29 +0000358 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000359
360 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200361 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000362}
363
364/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200365 * Skip over an expression at "*pp".
366 * If in Vim9 script and line breaks are encountered, the lines are
367 * concatenated. "evalarg->eval_tofree" will be set accordingly.
368 * Return FAIL for an error, OK otherwise.
369 */
370 int
371skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
372{
373 typval_T rettv;
374 int res;
375 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
376 garray_T *gap = &evalarg->eval_ga;
377 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
378
379 if (vim9script && evalarg->eval_cookie != NULL)
380 {
381 ga_init2(gap, sizeof(char_u *), 10);
382 if (ga_grow(gap, 1) == OK)
383 // leave room for "start"
384 ++gap->ga_len;
385 }
386
387 // Don't evaluate the expression.
388 if (evalarg != NULL)
389 evalarg->eval_flags &= ~EVAL_EVALUATE;
390 *end = skipwhite(*end);
391 res = eval1(end, &rettv, evalarg);
392 if (evalarg != NULL)
393 evalarg->eval_flags = save_flags;
394
395 if (vim9script && evalarg->eval_cookie != NULL
396 && evalarg->eval_ga.ga_len > 1)
397 {
398 char_u *p;
399 size_t endoff = STRLEN(*end);
400
401 // Line breaks encountered, concatenate all the lines.
402 *((char_u **)gap->ga_data) = *start;
403 p = ga_concat_strings(gap, "");
404 *((char_u **)gap->ga_data) = NULL;
405 ga_clear_strings(gap);
406 gap->ga_itemsize = 0;
407 if (p == NULL)
408 return FAIL;
409 *start = p;
410 vim_free(evalarg->eval_tofree);
411 evalarg->eval_tofree = p;
412 // Compute "end" relative to the end.
413 *end = *start + STRLEN(*start) - endoff;
414 }
415
416 return res;
417}
418
419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000421 * When "convert" is TRUE convert a List into a sequence of lines and convert
422 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * Return pointer to allocated memory, or NULL for failure.
424 */
425 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100426eval_to_string(
427 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100428 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
Bram Moolenaar33570922005-01-25 22:26:29 +0000430 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000432 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000433#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000434 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200437 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 retval = NULL;
439 else
440 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000441 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000442 {
443 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000444 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200445 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200446 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200447 if (tv.vval.v_list->lv_len > 0)
448 ga_append(&ga, NL);
449 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000450 ga_append(&ga, NUL);
451 retval = (char_u *)ga.ga_data;
452 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000453#ifdef FEAT_FLOAT
454 else if (convert && tv.v_type == VAR_FLOAT)
455 {
456 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
457 retval = vim_strsave(numbuf);
458 }
459#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000460 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100461 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000462 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 }
464
465 return retval;
466}
467
468/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000469 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200470 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 */
472 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100473eval_to_string_safe(
474 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100475 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200478 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200480 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000481 if (use_sandbox)
482 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200483 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200484 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000485 if (use_sandbox)
486 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200487 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200488 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 return retval;
490}
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
493 * Top level evaluation function, returning a number.
494 * Evaluates "expr" silently.
495 * Returns -1 for an error.
496 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200497 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100498eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
Bram Moolenaar33570922005-01-25 22:26:29 +0000500 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200501 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000502 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
504 ++emsg_off;
505
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200506 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 retval = -1;
508 else
509 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100510 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000511 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 }
513 --emsg_off;
514
515 return retval;
516}
517
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000518/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000519 * Top level evaluation function.
520 * Returns an allocated typval_T with the result.
521 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000522 */
523 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200524eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000525{
526 typval_T *tv;
527
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200528 tv = ALLOC_ONE(typval_T);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200529 if (tv != NULL && eval0(arg, tv, eap, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100530 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000531
532 return tv;
533}
534
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100536 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200537 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
538 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000539 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100542call_vim_function(
543 char_u *func,
544 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200545 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200546 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000548 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200549 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100551 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200552 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200553 funcexe.firstline = curwin->w_cursor.lnum;
554 funcexe.lastline = curwin->w_cursor.lnum;
555 funcexe.evaluate = TRUE;
556 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000557 if (ret == FAIL)
558 clear_tv(rettv);
559
560 return ret;
561}
562
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100563/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100564 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100565 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200566 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
567 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100568 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200569 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100570call_func_retnr(
571 char_u *func,
572 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200573 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100574{
575 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200576 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100577
Bram Moolenaarded27a12018-08-01 19:06:03 +0200578 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100579 return -1;
580
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100581 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100582 clear_tv(&rettv);
583 return retval;
584}
585
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000586/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100587 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000588 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200589 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
590 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000591 */
592 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100593call_func_retstr(
594 char_u *func,
595 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200596 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000597{
598 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000599 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000600
Bram Moolenaarded27a12018-08-01 19:06:03 +0200601 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000602 return NULL;
603
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100604 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000605 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 return retval;
607}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000608
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000609/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100610 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200611 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
612 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000613 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000614 */
615 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100616call_func_retlist(
617 char_u *func,
618 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200619 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000620{
621 typval_T rettv;
622
Bram Moolenaarded27a12018-08-01 19:06:03 +0200623 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000624 return NULL;
625
626 if (rettv.v_type != VAR_LIST)
627 {
628 clear_tv(&rettv);
629 return NULL;
630 }
631
632 return rettv.vval.v_list;
633}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#ifdef FEAT_FOLDING
636/*
637 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
638 * it in "*cp". Doesn't give error messages.
639 */
640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
Bram Moolenaar33570922005-01-25 22:26:29 +0000643 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200644 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000646 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
647 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000650 if (use_sandbox)
651 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200652 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200654 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 retval = 0;
656 else
657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100658 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000659 if (tv.v_type == VAR_NUMBER)
660 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000661 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 retval = 0;
663 else
664 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100665 // If the result is a string, check if there is a non-digit before
666 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000667 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 if (!VIM_ISDIGIT(*s) && *s != '-')
669 *cp = *s++;
670 retval = atol((char *)s);
671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000672 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 }
674 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000675 if (use_sandbox)
676 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200677 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200679 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680}
681#endif
682
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000684 * Get an lval: variable, Dict item or List item that can be assigned a value
685 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
686 * "name.key", "name.key[expr]" etc.
687 * Indexing only works if "name" is an existing List or Dictionary.
688 * "name" points to the start of the name.
689 * If "rettv" is not NULL it points to the value to be assigned.
690 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
691 * wrong; must end in space or cmd separator.
692 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100693 * flags:
694 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100695 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100696 * GLV_NO_AUTOLOAD: do not use script autoloading
697 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000698 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000699 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000700 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000701 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200702 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703get_lval(
704 char_u *name,
705 typval_T *rettv,
706 lval_T *lp,
707 int unlet,
708 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100709 int flags, // GLV_ values
710 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000711{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000712 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000713 char_u *expr_start, *expr_end;
714 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000715 dictitem_T *v;
716 typval_T var1;
717 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000718 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000719 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000720 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000721 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000722 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100723 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100725 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200726 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000727
728 if (skip)
729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100730 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000731 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000732 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000733 }
734
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100735 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000736 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 if (expr_start != NULL)
739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100740 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100741 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000742 && *p != '[' && *p != '.')
743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100744 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000745 return NULL;
746 }
747
748 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
749 if (lp->ll_exp_name == NULL)
750 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100751 // Report an invalid expression in braces, unless the
752 // expression evaluation has been cancelled due to an
753 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000754 if (!aborting() && !quiet)
755 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000756 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100757 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000758 return NULL;
759 }
760 }
761 lp->ll_name = lp->ll_exp_name;
762 }
763 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000765 lp->ll_name = name;
766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
768 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100769 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 char_u *tp = skipwhite(p + 1);
771
772 // parse the type after the name
773 lp->ll_type = parse_type(&tp, &si->sn_type_list);
774 lp->ll_name_end = tp;
775 }
776 }
777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100778 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000779 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
780 return p;
781
782 cc = *p;
783 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 // Only pass &ht when we would write to the variable, it prevents autoload
785 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100786 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
787 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100789 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 if (v == NULL)
792 return NULL;
793
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 /*
795 * Loop until no more [idx] or .key is following.
796 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000797 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100798 var1.v_type = VAR_UNKNOWN;
799 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000800 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
803 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100804 && lp->ll_tv->vval.v_dict != NULL)
805 && !(lp->ll_tv->v_type == VAR_BLOB
806 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000807 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100809 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000811 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000813 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000814 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100815 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000817 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000818
Bram Moolenaar8c711452005-01-14 21:53:12 +0000819 len = -1;
820 if (*p == '.')
821 {
822 key = p + 1;
823 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
824 ;
825 if (len == 0)
826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000827 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100828 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000829 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000830 }
831 p = key + len;
832 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000833 else
834 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100835 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000836 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000837 if (*p == ':')
838 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000839 else
840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200842 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100844 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000845 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100846 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000847 clear_tv(&var1);
848 return NULL;
849 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000850 }
851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000853 if (*p == ':')
854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000856 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100858 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100859 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000861 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100862 if (rettv != NULL
863 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100864 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100865 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100866 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000867 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100869 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100870 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000871 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000872 }
873 p = skipwhite(p + 1);
874 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000876 else
877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200879 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200880 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000881 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100882 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000884 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100885 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000886 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100887 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100888 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000889 clear_tv(&var2);
890 return NULL;
891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000892 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000894 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000896 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000897
Bram Moolenaar8c711452005-01-14 21:53:12 +0000898 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000899 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100901 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100902 clear_tv(&var1);
903 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000905 }
906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100907 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000908 ++p;
909 }
910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 {
913 if (len == -1)
914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100915 // "[key]": get key from "var1"
916 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200917 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000918 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000921 }
922 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000923 lp->ll_list = NULL;
924 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000925 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100927 // When assigning to a scope dictionary check that a function and
928 // variable name is valid (only variable name unless it is l: or
929 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200930 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200931 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200932 int prevval;
933 int wrong;
934
935 if (len != -1)
936 {
937 prevval = key[len];
938 key[len] = NUL;
939 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200940 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100941 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200942 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
943 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200944 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200945 || !valid_varname(key);
946 if (len != -1)
947 key[len] = prevval;
948 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200949 {
950 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200951 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200952 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200953 }
954
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000956 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100957 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200958 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100959 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200960 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100961 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100962 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200963 return NULL;
964 }
965
Bram Moolenaar31b81602019-02-10 22:14:27 +0100966 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100970 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100971 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 }
974 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000975 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000976 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100978 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000980 p = NULL;
981 break;
982 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100983 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100984 else if ((flags & GLV_READ_ONLY) == 0
985 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100986 {
987 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200988 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100989 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200990
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100991 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100994 else if (lp->ll_tv->v_type == VAR_BLOB)
995 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100996 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
997
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100998 /*
999 * Get the number and item for the only or first index of the List.
1000 */
1001 if (empty1)
1002 lp->ll_n1 = 0;
1003 else
1004 // is number or string
1005 lp->ll_n1 = (long)tv_get_number(&var1);
1006 clear_tv(&var1);
1007
1008 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001009 || lp->ll_n1 > bloblen
1010 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001011 {
1012 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001013 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001014 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001015 return NULL;
1016 }
1017 if (lp->ll_range && !lp->ll_empty2)
1018 {
1019 lp->ll_n2 = (long)tv_get_number(&var2);
1020 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001021 if (lp->ll_n2 < 0
1022 || lp->ll_n2 >= bloblen
1023 || lp->ll_n2 < lp->ll_n1)
1024 {
1025 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001026 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001027 return NULL;
1028 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001029 }
1030 lp->ll_blob = lp->ll_tv->vval.v_blob;
1031 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001032 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001033 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 else
1035 {
1036 /*
1037 * Get the number and item for the only or first index of the List.
1038 */
1039 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001041 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001042 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001043 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001044 clear_tv(&var1);
1045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001046 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 lp->ll_list = lp->ll_tv->vval.v_list;
1048 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1049 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001051 if (lp->ll_n1 < 0)
1052 {
1053 lp->ll_n1 = 0;
1054 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1055 }
1056 }
1057 if (lp->ll_li == NULL)
1058 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001059 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001060 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001061 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001063 }
1064
1065 /*
1066 * May need to find the item or absolute index for the second
1067 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 * When no index given: "lp->ll_empty2" is TRUE.
1069 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001071 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001073 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001074 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001075 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001076 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001077 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001079 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001080 {
1081 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001082 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001083 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001084 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001086 }
1087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001088 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 if (lp->ll_n1 < 0)
1090 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1091 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001092 {
1093 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001094 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001095 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001096 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001097 }
1098
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001099 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001100 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001101 }
1102
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001103 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001105 return p;
1106}
1107
1108/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001109 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001112clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113{
1114 vim_free(lp->ll_exp_name);
1115 vim_free(lp->ll_newkey);
1116}
1117
1118/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001119 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001121 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1122 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001125set_var_lval(
1126 lval_T *lp,
1127 char_u *endp,
1128 typval_T *rettv,
1129 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001131 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132{
1133 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001134 listitem_T *ri;
1135 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136
1137 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001139 cc = *endp;
1140 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001141 if (lp->ll_blob != NULL)
1142 {
1143 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001144
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001145 if (op != NULL && *op != '=')
1146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001147 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001148 return;
1149 }
1150
1151 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1152 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001153 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001154
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001155 if (lp->ll_empty2)
1156 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1157
1158 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001160 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001161 return;
1162 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001163 if (lp->ll_empty2)
1164 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001165
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166 ir = 0;
1167 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1168 blob_set(lp->ll_blob, il,
1169 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001170 }
1171 else
1172 {
1173 val = (int)tv_get_number_chk(rettv, &error);
1174 if (!error)
1175 {
1176 garray_T *gap = &lp->ll_blob->bv_ga;
1177
1178 // Allow for appending a byte. Setting a byte beyond
1179 // the end is an error otherwise.
1180 if (lp->ll_n1 < gap->ga_len
1181 || (lp->ll_n1 == gap->ga_len
1182 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1183 {
1184 blob_set(lp->ll_blob, lp->ll_n1, val);
1185 if (lp->ll_n1 == gap->ga_len)
1186 ++gap->ga_len;
1187 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001188 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001189 }
1190 }
1191 }
1192 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001193 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001194 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001197 {
1198 emsg(_(e_cannot_mod));
1199 *endp = cc;
1200 return;
1201 }
1202
Bram Moolenaarff697e62019-02-12 22:28:33 +01001203 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001204 di = NULL;
1205 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1206 &tv, &di, TRUE, FALSE) == OK)
1207 {
1208 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001209 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1210 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001211 && tv_op(&tv, rettv, op) == OK)
1212 set_var(lp->ll_name, &tv, FALSE);
1213 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001216 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001218 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001220 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001221 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001222 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001223 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 else if (lp->ll_range)
1225 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001226 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001227 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001230 {
1231 emsg(_("E996: Cannot lock a range"));
1232 return;
1233 }
1234
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001235 /*
1236 * Check whether any of the list items is locked
1237 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001238 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001239 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001240 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001241 return;
1242 ri = ri->li_next;
1243 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1244 break;
1245 ll_li = ll_li->li_next;
1246 ++ll_n1;
1247 }
1248
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 /*
1250 * Assign the List values to the list items.
1251 */
1252 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001253 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001254 if (op != NULL && *op != '=')
1255 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1256 else
1257 {
1258 clear_tv(&lp->ll_li->li_tv);
1259 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1260 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 ri = ri->li_next;
1262 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1263 break;
1264 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001265 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001266 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001267 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001268 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 ri = NULL;
1270 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001271 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001272 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001273 lp->ll_li = lp->ll_li->li_next;
1274 ++lp->ll_n1;
1275 }
1276 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001277 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001278 else if (lp->ll_empty2
1279 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001281 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 }
1283 else
1284 {
1285 /*
1286 * Assign to a List or Dictionary item.
1287 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001289 {
1290 emsg(_("E996: Cannot lock a list or dict"));
1291 return;
1292 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001293 if (lp->ll_newkey != NULL)
1294 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001295 if (op != NULL && *op != '=')
1296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001297 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001298 return;
1299 }
1300
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001301 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001302 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001303 if (di == NULL)
1304 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001305 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1306 {
1307 vim_free(di);
1308 return;
1309 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001311 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001312 else if (op != NULL && *op != '=')
1313 {
1314 tv_op(lp->ll_tv, rettv, op);
1315 return;
1316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001317 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001318 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001319
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 /*
1321 * Assign the value to the variable or list item.
1322 */
1323 if (copy)
1324 copy_tv(rettv, lp->ll_tv);
1325 else
1326 {
1327 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001328 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 }
1331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001332}
1333
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001334/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001335 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1336 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001337 * Returns OK or FAIL.
1338 */
1339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001340tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001341{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001342 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001343 char_u numbuf[NUMBUFLEN];
1344 char_u *s;
1345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001346 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001347 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001348 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001349 {
1350 switch (tv1->v_type)
1351 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001352 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001353 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001355 case VAR_DICT:
1356 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001357 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001358 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001359 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001360 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001361 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362 break;
1363
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001364 case VAR_BLOB:
1365 if (*op != '+' || tv2->v_type != VAR_BLOB)
1366 break;
1367 // BLOB += BLOB
1368 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1369 {
1370 blob_T *b1 = tv1->vval.v_blob;
1371 blob_T *b2 = tv2->vval.v_blob;
1372 int i, len = blob_len(b2);
1373 for (i = 0; i < len; i++)
1374 ga_append(&b1->bv_ga, blob_get(b2, i));
1375 }
1376 return OK;
1377
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001378 case VAR_LIST:
1379 if (*op != '+' || tv2->v_type != VAR_LIST)
1380 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001381 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001382 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1383 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1384 return OK;
1385
1386 case VAR_NUMBER:
1387 case VAR_STRING:
1388 if (tv2->v_type == VAR_LIST)
1389 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001390 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001391 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001392 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001393 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001394#ifdef FEAT_FLOAT
1395 if (tv2->v_type == VAR_FLOAT)
1396 {
1397 float_T f = n;
1398
Bram Moolenaarff697e62019-02-12 22:28:33 +01001399 if (*op == '%')
1400 break;
1401 switch (*op)
1402 {
1403 case '+': f += tv2->vval.v_float; break;
1404 case '-': f -= tv2->vval.v_float; break;
1405 case '*': f *= tv2->vval.v_float; break;
1406 case '/': f /= tv2->vval.v_float; break;
1407 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001408 clear_tv(tv1);
1409 tv1->v_type = VAR_FLOAT;
1410 tv1->vval.v_float = f;
1411 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001412 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001413#endif
1414 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001415 switch (*op)
1416 {
1417 case '+': n += tv_get_number(tv2); break;
1418 case '-': n -= tv_get_number(tv2); break;
1419 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001420 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1421 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001422 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001423 clear_tv(tv1);
1424 tv1->v_type = VAR_NUMBER;
1425 tv1->vval.v_number = n;
1426 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427 }
1428 else
1429 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001430 if (tv2->v_type == VAR_FLOAT)
1431 break;
1432
Bram Moolenaarff697e62019-02-12 22:28:33 +01001433 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001434 s = tv_get_string(tv1);
1435 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001436 clear_tv(tv1);
1437 tv1->v_type = VAR_STRING;
1438 tv1->vval.v_string = s;
1439 }
1440 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001441
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001442 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001443#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001444 {
1445 float_T f;
1446
Bram Moolenaarff697e62019-02-12 22:28:33 +01001447 if (*op == '%' || *op == '.'
1448 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001449 && tv2->v_type != VAR_NUMBER
1450 && tv2->v_type != VAR_STRING))
1451 break;
1452 if (tv2->v_type == VAR_FLOAT)
1453 f = tv2->vval.v_float;
1454 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001455 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001456 switch (*op)
1457 {
1458 case '+': tv1->vval.v_float += f; break;
1459 case '-': tv1->vval.v_float -= f; break;
1460 case '*': tv1->vval.v_float *= f; break;
1461 case '/': tv1->vval.v_float /= f; break;
1462 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001463 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001464#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001465 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466 }
1467 }
1468
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001469 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001470 return FAIL;
1471}
1472
1473/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001474 * Evaluate the expression used in a ":for var in expr" command.
1475 * "arg" points to "var".
1476 * Set "*errp" to TRUE for an error, FALSE otherwise;
1477 * Return a pointer that holds the info. Null when there is an error.
1478 */
1479 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001480eval_for_line(
1481 char_u *arg,
1482 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001483 exarg_T *eap,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001485{
Bram Moolenaar33570922005-01-25 22:26:29 +00001486 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001488 typval_T tv;
1489 list_T *l;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001490 evalarg_T evalarg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001491
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001492 CLEAR_FIELD(evalarg);
1493 evalarg.eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001494 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001495
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001496 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001497 if (fi == NULL)
1498 return NULL;
1499
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001500 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001501 if (expr == NULL)
1502 return fi;
1503
1504 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001505 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001508 return fi;
1509 }
1510
1511 if (skip)
1512 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001513 if (eval0(skipwhite(expr + 2), &tv, eap, &evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514 {
1515 *errp = FALSE;
1516 if (!skip)
1517 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001518 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001519 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001520 l = tv.vval.v_list;
1521 if (l == NULL)
1522 {
1523 // a null list is like an empty list: do nothing
1524 clear_tv(&tv);
1525 }
1526 else
1527 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001529 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001531 // No need to increment the refcount, it's already set for
1532 // the list being used in "tv".
1533 fi->fi_list = l;
1534 list_add_watch(l, &fi->fi_lw);
1535 fi->fi_lw.lw_item = l->lv_first;
1536 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001537 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001538 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001539 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001540 fi->fi_bi = 0;
1541 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001542 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001543 typval_T btv;
1544
1545 // Make a copy, so that the iteration still works when the
1546 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001548 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001549 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001550 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001551 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001552 else
1553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001554 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001555 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556 }
1557 }
1558 }
1559 if (skip)
1560 --emsg_skip;
1561
1562 return fi;
1563}
1564
1565/*
1566 * Use the first item in a ":for" list. Advance to the next.
1567 * Assign the values to the variable (list). "arg" points to the first one.
1568 * Return TRUE when a valid item was found, FALSE when at end of list or
1569 * something wrong.
1570 */
1571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001574 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001575 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001576 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1577 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001578 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001579
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001580 if (fi->fi_blob != NULL)
1581 {
1582 typval_T tv;
1583
1584 if (fi->fi_bi >= blob_len(fi->fi_blob))
1585 return FALSE;
1586 tv.v_type = VAR_NUMBER;
1587 tv.v_lock = VAR_FIXED;
1588 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1589 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001590 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001591 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001592 }
1593
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001594 item = fi->fi_lw.lw_item;
1595 if (item == NULL)
1596 result = FALSE;
1597 else
1598 {
1599 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001600 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001601 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001602 }
1603 return result;
1604}
1605
1606/*
1607 * Free the structure used to store info used by ":for".
1608 */
1609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001610free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611{
Bram Moolenaar33570922005-01-25 22:26:29 +00001612 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001614 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001615 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001616 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001617 list_unref(fi->fi_list);
1618 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001619 if (fi != NULL && fi->fi_blob != NULL)
1620 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621 vim_free(fi);
1622}
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001625set_context_for_expression(
1626 expand_T *xp,
1627 char_u *arg,
1628 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630 int got_eq = FALSE;
1631 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001634 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 {
1636 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001637 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001639 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001640 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 {
1642 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001643 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001644 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 break;
1646 }
1647 return;
1648 }
1649 }
1650 else
1651 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1652 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 while ((xp->xp_pattern = vim_strpbrk(arg,
1654 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1655 {
1656 c = *xp->xp_pattern;
1657 if (c == '&')
1658 {
1659 c = xp->xp_pattern[1];
1660 if (c == '&')
1661 {
1662 ++xp->xp_pattern;
1663 xp->xp_context = cmdidx != CMD_let || got_eq
1664 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1665 }
1666 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001669 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1670 xp->xp_pattern += 2;
1671
1672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 }
1674 else if (c == '$')
1675 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001676 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 xp->xp_context = EXPAND_ENV_VARS;
1678 }
1679 else if (c == '=')
1680 {
1681 got_eq = TRUE;
1682 xp->xp_context = EXPAND_EXPRESSION;
1683 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001684 else if (c == '#'
1685 && xp->xp_context == EXPAND_EXPRESSION)
1686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001687 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001688 break;
1689 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001690 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 && xp->xp_context == EXPAND_FUNCTIONS
1692 && vim_strchr(xp->xp_pattern, '(') == NULL)
1693 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001694 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 break;
1696 }
1697 else if (cmdidx != CMD_let || got_eq)
1698 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001699 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 {
1701 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1702 if (c == '\\' && xp->xp_pattern[1] != NUL)
1703 ++xp->xp_pattern;
1704 xp->xp_context = EXPAND_NOTHING;
1705 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001706 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1710 /* skip */ ;
1711 xp->xp_context = EXPAND_NOTHING;
1712 }
1713 else if (c == '|')
1714 {
1715 if (xp->xp_pattern[1] == '|')
1716 {
1717 ++xp->xp_pattern;
1718 xp->xp_context = EXPAND_EXPRESSION;
1719 }
1720 else
1721 xp->xp_context = EXPAND_COMMANDS;
1722 }
1723 else
1724 xp->xp_context = EXPAND_EXPRESSION;
1725 }
1726 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001727 // Doesn't look like something valid, expand as an expression
1728 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 arg = xp->xp_pattern;
1731 if (*arg != NUL)
1732 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1733 /* skip */ ;
1734 }
1735 xp->xp_pattern = arg;
1736}
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001739 * Return TRUE if "pat" matches "text".
1740 * Does not use 'cpo' and always uses 'magic'.
1741 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001742 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001743pattern_match(char_u *pat, char_u *text, int ic)
1744{
1745 int matches = FALSE;
1746 char_u *save_cpo;
1747 regmatch_T regmatch;
1748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001749 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001750 save_cpo = p_cpo;
1751 p_cpo = (char_u *)"";
1752 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1753 if (regmatch.regprog != NULL)
1754 {
1755 regmatch.rm_ic = ic;
1756 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1757 vim_regfree(regmatch.regprog);
1758 }
1759 p_cpo = save_cpo;
1760 return matches;
1761}
1762
1763/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001764 * Handle a name followed by "(". Both for just "name(arg)" and for
1765 * "expr->name(arg)".
1766 * Returns OK or FAIL.
1767 */
1768 static int
1769eval_func(
1770 char_u **arg, // points to "(", will be advanced
1771 char_u *name,
1772 int name_len,
1773 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001774 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001775 typval_T *basetv) // "expr" for "expr->name(arg)"
1776{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001777 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001778 char_u *s = name;
1779 int len = name_len;
1780 partial_T *partial;
1781 int ret = OK;
1782
1783 if (!evaluate)
1784 check_vars(s, len);
1785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001786 // If "s" is the name of a variable of type VAR_FUNC
1787 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001788 s = deref_func_name(s, &len, &partial, !evaluate);
1789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001790 // Need to make a copy, in case evaluating the arguments makes
1791 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001792 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001793 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001794 ret = FAIL;
1795 else
1796 {
1797 funcexe_T funcexe;
1798
1799 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001800 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001801 funcexe.firstline = curwin->w_cursor.lnum;
1802 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001803 funcexe.evaluate = evaluate;
1804 funcexe.partial = partial;
1805 funcexe.basetv = basetv;
1806 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1807 }
1808 vim_free(s);
1809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001810 // If evaluate is FALSE rettv->v_type was not set in
1811 // get_func_tv, but it's needed in handle_subscript() to parse
1812 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001813 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1814 {
1815 rettv->vval.v_string = NULL;
1816 rettv->v_type = VAR_FUNC;
1817 }
1818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001819 // Stop the expression evaluation when immediately
1820 // aborting on error, or when an interrupt occurred or
1821 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001822 if (evaluate && aborting())
1823 {
1824 if (ret == OK)
1825 clear_tv(rettv);
1826 ret = FAIL;
1827 }
1828 return ret;
1829}
1830
1831/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001832 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1833 * and there is a next line, return the next line (skipping blanks) and set
1834 * "getnext".
1835 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1836 * "arg" must point somewhere inside a line, not at the start.
1837 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001838 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001839eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1840{
1841 *getnext = FALSE;
1842 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1843 && evalarg != NULL
1844 && evalarg->eval_cookie != NULL
1845 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaard5053d02020-06-28 15:51:16 +02001846 && (*arg == '"' || *arg == '#'))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001847 {
Bram Moolenaard5053d02020-06-28 15:51:16 +02001848 char_u *p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001849
1850 if (p != NULL)
1851 {
1852 *getnext = TRUE;
1853 return skipwhite(p);
1854 }
1855 }
1856 return arg;
1857}
1858
1859/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001860 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001861 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001862 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001863eval_next_line(evalarg_T *evalarg)
1864{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001865 garray_T *gap = &evalarg->eval_ga;
1866 char_u *line;
1867
Bram Moolenaard5053d02020-06-28 15:51:16 +02001868 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001869 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1870 {
1871 // Going to concatenate the lines after parsing.
1872 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1873 ++gap->ga_len;
1874 }
1875 else
1876 {
1877 vim_free(evalarg->eval_tofree);
1878 evalarg->eval_tofree = line;
1879 }
1880 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001881}
1882
Bram Moolenaar9215f012020-06-27 21:18:00 +02001883 char_u *
1884skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1885{
1886 int getnext;
1887 char_u *p = skipwhite(arg);
1888
1889 eval_next_non_blank(p, evalarg, &getnext);
1890 if (getnext)
1891 return eval_next_line(evalarg);
1892 return p;
1893}
1894
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001895/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001896 * After using "evalarg" filled from "eap" free the memory.
1897 */
1898 void
1899clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1900{
1901 if (evalarg != NULL && eap != NULL && evalarg->eval_tofree != NULL)
1902 {
1903 // We may need to keep the original command line, e.g. for
1904 // ":let" it has the variable names. But we may also need the
1905 // new one, "nextcmd" points into it. Keep both.
1906 vim_free(eap->cmdline_tofree);
1907 eap->cmdline_tofree = *eap->cmdlinep;
1908 *eap->cmdlinep = evalarg->eval_tofree;
1909 evalarg->eval_tofree = NULL;
1910 }
1911}
1912
1913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1917 */
1918
1919/*
1920 * Handle zero level expression.
1921 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001923 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001924 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 * Return OK or FAIL.
1926 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001928eval0(
1929 char_u *arg,
1930 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001931 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001932 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
1934 int ret;
1935 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001936 int did_emsg_before = did_emsg;
1937 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001938 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
1940 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001941 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001942
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001943 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 /*
1948 * Report the invalid expression unless the expression evaluation has
1949 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001950 * exception, or we already gave a more specific error.
1951 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001953 if (!aborting()
1954 && did_emsg == did_emsg_before
1955 && called_emsg == called_emsg_before
1956 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001957 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 ret = FAIL;
1959 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001960
1961 if (eap != NULL)
1962 eap->nextcmd = check_nextcmd(p);
1963
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001964 clear_evalarg(evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966 return ret;
1967}
1968
1969/*
1970 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001971 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 *
1973 * "arg" must point to the first non-white of the expression.
1974 * "arg" is advanced to the next non-white after the recognized expression.
1975 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001976 * Note: "rettv.v_lock" is not set.
1977 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 * Return OK or FAIL.
1979 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001980 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001981eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
Bram Moolenaar793648f2020-06-26 21:28:25 +02001983 char_u *p;
1984 int getnext;
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 /*
1987 * Get the first variable.
1988 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001989 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 return FAIL;
1991
Bram Moolenaar793648f2020-06-26 21:28:25 +02001992 p = eval_next_non_blank(*arg, evalarg, &getnext);
1993 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001995 int result;
1996 typval_T var2;
1997 evalarg_T nested_evalarg;
1998 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02001999 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002000
Bram Moolenaar793648f2020-06-26 21:28:25 +02002001 if (getnext)
2002 *arg = eval_next_line(evalarg);
2003
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002004 if (evalarg == NULL)
2005 {
2006 CLEAR_FIELD(nested_evalarg);
2007 orig_flags = 0;
2008 }
2009 else
2010 {
2011 nested_evalarg = *evalarg;
2012 orig_flags = evalarg->eval_flags;
2013 }
2014
Bram Moolenaar7acde512020-06-24 23:02:40 +02002015 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002017 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002019 int error = FALSE;
2020
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002021 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002024 if (error)
2025 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027
2028 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002029 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002031 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002032 nested_evalarg.eval_flags = result ? orig_flags
2033 : orig_flags & ~EVAL_EVALUATE;
2034 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 return FAIL;
2036
2037 /*
2038 * Check for the ":".
2039 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002040 p = eval_next_non_blank(*arg, evalarg, &getnext);
2041 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 return FAIL;
2047 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002048 if (getnext)
2049 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002052 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002054 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002055 nested_evalarg.eval_flags = !result ? orig_flags
2056 : orig_flags & ~EVAL_EVALUATE;
2057 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
2059 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 return FAIL;
2062 }
2063 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
2066
2067 return OK;
2068}
2069
2070/*
2071 * Handle first level expression:
2072 * expr2 || expr2 || expr2 logical OR
2073 *
2074 * "arg" must point to the first non-white of the expression.
2075 * "arg" is advanced to the next non-white after the recognized expression.
2076 *
2077 * Return OK or FAIL.
2078 */
2079 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002080eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002082 char_u *p;
2083 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 long result;
2086 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002087 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 /*
2090 * Get the first variable.
2091 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002092 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 return FAIL;
2094
2095 /*
2096 * Repeat until there is no following "||".
2097 */
2098 first = TRUE;
2099 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002100 p = eval_next_non_blank(*arg, evalarg, &getnext);
2101 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002103 evalarg_T nested_evalarg;
2104 int evaluate;
2105 int orig_flags;
2106
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002107 if (getnext)
2108 *arg = eval_next_line(evalarg);
2109
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110 if (evalarg == NULL)
2111 {
2112 CLEAR_FIELD(nested_evalarg);
2113 orig_flags = 0;
2114 evaluate = FALSE;
2115 }
2116 else
2117 {
2118 nested_evalarg = *evalarg;
2119 orig_flags = evalarg->eval_flags;
2120 evaluate = orig_flags & EVAL_EVALUATE;
2121 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (evaluate && first)
2124 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002125 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002128 if (error)
2129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 first = FALSE;
2131 }
2132
2133 /*
2134 * Get the second variable.
2135 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002136 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002137 nested_evalarg.eval_flags = !result ? orig_flags
2138 : orig_flags & ~EVAL_EVALUATE;
2139 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 return FAIL;
2141
2142 /*
2143 * Compute the result.
2144 */
2145 if (evaluate && !result)
2146 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002147 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002150 if (error)
2151 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 if (evaluate)
2154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 rettv->v_type = VAR_NUMBER;
2156 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002158
2159 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161
2162 return OK;
2163}
2164
2165/*
2166 * Handle second level expression:
2167 * expr3 && expr3 && expr3 logical AND
2168 *
2169 * "arg" must point to the first non-white of the expression.
2170 * "arg" is advanced to the next non-white after the recognized expression.
2171 *
2172 * Return OK or FAIL.
2173 */
2174 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002177 char_u *p;
2178 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002179 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 long result;
2181 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002182 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184 /*
2185 * Get the first variable.
2186 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002187 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 return FAIL;
2189
2190 /*
2191 * Repeat until there is no following "&&".
2192 */
2193 first = TRUE;
2194 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002195 p = eval_next_non_blank(*arg, evalarg, &getnext);
2196 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002198 evalarg_T nested_evalarg;
2199 int orig_flags;
2200 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002201
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002202 if (getnext)
2203 *arg = eval_next_line(evalarg);
2204
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002205 if (evalarg == NULL)
2206 {
2207 CLEAR_FIELD(nested_evalarg);
2208 orig_flags = 0;
2209 evaluate = FALSE;
2210 }
2211 else
2212 {
2213 nested_evalarg = *evalarg;
2214 orig_flags = evalarg->eval_flags;
2215 evaluate = orig_flags & EVAL_EVALUATE;
2216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (evaluate && first)
2218 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002219 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002222 if (error)
2223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 first = FALSE;
2225 }
2226
2227 /*
2228 * Get the second variable.
2229 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002230 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002231 nested_evalarg.eval_flags = result ? orig_flags
2232 : orig_flags & ~EVAL_EVALUATE;
2233 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 return FAIL;
2235
2236 /*
2237 * Compute the result.
2238 */
2239 if (evaluate && result)
2240 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002241 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002244 if (error)
2245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 if (evaluate)
2248 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 rettv->v_type = VAR_NUMBER;
2250 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002252
2253 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
2255
2256 return OK;
2257}
2258
2259/*
2260 * Handle third level expression:
2261 * var1 == var2
2262 * var1 =~ var2
2263 * var1 != var2
2264 * var1 !~ var2
2265 * var1 > var2
2266 * var1 >= var2
2267 * var1 < var2
2268 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002269 * var1 is var2
2270 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 *
2272 * "arg" must point to the first non-white of the expression.
2273 * "arg" is advanced to the next non-white after the recognized expression.
2274 *
2275 * Return OK or FAIL.
2276 */
2277 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002278eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
Bram Moolenaar33570922005-01-25 22:26:29 +00002280 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002282 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002284 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
2288 /*
2289 * Get the first variable.
2290 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 return FAIL;
2293
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002294 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 switch (p[0])
2296 {
2297 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002298 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002300 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 break;
2302 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002303 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002305 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 break;
2307 case '>': if (p[1] != '=')
2308 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002309 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 len = 1;
2311 }
2312 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002313 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 break;
2315 case '<': if (p[1] != '=')
2316 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002317 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 len = 1;
2319 }
2320 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002321 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002323 case 'i': if (p[1] == 's')
2324 {
2325 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2326 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002327 i = p[len];
2328 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002329 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002330 }
2331 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333
2334 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002335 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002337 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002339 if (getnext)
2340 *arg = eval_next_line(evalarg);
2341
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002342 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (p[len] == '?')
2344 {
2345 ic = TRUE;
2346 ++len;
2347 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002348 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 else if (p[len] == '#')
2350 {
2351 ic = FALSE;
2352 ++len;
2353 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002354 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 else
2356 ic = p_ic;
2357
2358 /*
2359 * Get the second variable.
2360 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002361 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002362 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 return FAIL;
2366 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002367 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002368 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002369 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002370
2371 clear_tv(&var2);
2372 return ret;
2373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375
2376 return OK;
2377}
2378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 void
2380eval_addblob(typval_T *tv1, typval_T *tv2)
2381{
2382 blob_T *b1 = tv1->vval.v_blob;
2383 blob_T *b2 = tv2->vval.v_blob;
2384 blob_T *b = blob_alloc();
2385 int i;
2386
2387 if (b != NULL)
2388 {
2389 for (i = 0; i < blob_len(b1); i++)
2390 ga_append(&b->bv_ga, blob_get(b1, i));
2391 for (i = 0; i < blob_len(b2); i++)
2392 ga_append(&b->bv_ga, blob_get(b2, i));
2393
2394 clear_tv(tv1);
2395 rettv_blob_set(tv1, b);
2396 }
2397}
2398
2399 int
2400eval_addlist(typval_T *tv1, typval_T *tv2)
2401{
2402 typval_T var3;
2403
2404 // concatenate Lists
2405 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2406 {
2407 clear_tv(tv1);
2408 clear_tv(tv2);
2409 return FAIL;
2410 }
2411 clear_tv(tv1);
2412 *tv1 = var3;
2413 return OK;
2414}
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416/*
2417 * Handle fourth level expression:
2418 * + number addition
2419 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002420 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002421 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 *
2423 * "arg" must point to the first non-white of the expression.
2424 * "arg" is advanced to the next non-white after the recognized expression.
2425 *
2426 * Return OK or FAIL.
2427 */
2428 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002431 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
2433 /*
2434 * Get the first variable.
2435 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 return FAIL;
2438
2439 /*
2440 * Repeat computing, until no '+', '-' or '.' is following.
2441 */
2442 for (;;)
2443 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444 int getnext;
2445 char_u *p;
2446 int op;
2447 int concat;
2448 typval_T var2;
2449
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002450 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 p = eval_next_non_blank(*arg, evalarg, &getnext);
2452 op = *p;
2453 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002454 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002457 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002459 if ((op != '+' || (rettv->v_type != VAR_LIST
2460 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002461#ifdef FEAT_FLOAT
2462 && (op == '.' || rettv->v_type != VAR_FLOAT)
2463#endif
2464 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002466 // For "list + ...", an illegal use of the first operand as
2467 // a number cannot be determined before evaluating the 2nd
2468 // operand: if this is also a list, all is ok.
2469 // For "something . ...", "something - ..." or "non-list + ...",
2470 // we know that the first operand needs to be a string or number
2471 // without evaluating the 2nd operand. So check before to avoid
2472 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002473 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 {
2475 clear_tv(rettv);
2476 return FAIL;
2477 }
2478 }
2479
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 /*
2481 * Get the second variable.
2482 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002483 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2484 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002485 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002486 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 return FAIL;
2490 }
2491
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 /*
2495 * Compute the result.
2496 */
2497 if (op == '.')
2498 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002499 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2500 char_u *s1 = tv_get_string_buf(rettv, buf1);
2501 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002503 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 {
2505 clear_tv(rettv);
2506 clear_tv(&var2);
2507 return FAIL;
2508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 clear_tv(rettv);
2511 rettv->v_type = VAR_STRING;
2512 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002514 else if (op == '+' && rettv->v_type == VAR_BLOB
2515 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002517 else if (op == '+' && rettv->v_type == VAR_LIST
2518 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002519 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002521 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 else
2524 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002525 int error = FALSE;
2526 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002527#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 float_T f1 = 0, f2 = 0;
2529
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002530 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002531 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002532 f1 = rettv->vval.v_float;
2533 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002534 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002535 else
2536#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002538 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002539 if (error)
2540 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002541 // This can only happen for "list + non-list". For
2542 // "non-list + ..." or "something - ...", we returned
2543 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002544 clear_tv(rettv);
2545 return FAIL;
2546 }
2547#ifdef FEAT_FLOAT
2548 if (var2.v_type == VAR_FLOAT)
2549 f1 = n1;
2550#endif
2551 }
2552#ifdef FEAT_FLOAT
2553 if (var2.v_type == VAR_FLOAT)
2554 {
2555 f2 = var2.vval.v_float;
2556 n2 = 0;
2557 }
2558 else
2559#endif
2560 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002561 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002562 if (error)
2563 {
2564 clear_tv(rettv);
2565 clear_tv(&var2);
2566 return FAIL;
2567 }
2568#ifdef FEAT_FLOAT
2569 if (rettv->v_type == VAR_FLOAT)
2570 f2 = n2;
2571#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002572 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002574
2575#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002576 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002577 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2578 {
2579 if (op == '+')
2580 f1 = f1 + f2;
2581 else
2582 f1 = f1 - f2;
2583 rettv->v_type = VAR_FLOAT;
2584 rettv->vval.v_float = f1;
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002587#endif
2588 {
2589 if (op == '+')
2590 n1 = n1 + n2;
2591 else
2592 n1 = n1 - n2;
2593 rettv->v_type = VAR_NUMBER;
2594 rettv->vval.v_number = n1;
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599 }
2600 return OK;
2601}
2602
2603/*
2604 * Handle fifth level expression:
2605 * * number multiplication
2606 * / number division
2607 * % number modulo
2608 *
2609 * "arg" must point to the first non-white of the expression.
2610 * "arg" is advanced to the next non-white after the recognized expression.
2611 *
2612 * Return OK or FAIL.
2613 */
2614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002615eval6(
2616 char_u **arg,
2617 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002619 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
Bram Moolenaar33570922005-01-25 22:26:29 +00002621 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002623 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002624#ifdef FEAT_FLOAT
2625 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002626 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002627#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002628 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
2630 /*
2631 * Get the first variable.
2632 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002633 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 return FAIL;
2635
2636 /*
2637 * Repeat computing, until no '*', '/' or '%' is following.
2638 */
2639 for (;;)
2640 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002641 int evaluate = evalarg == NULL ? 0
2642 : (evalarg->eval_flags & EVAL_EVALUATE);
2643 int getnext;
2644
2645 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002646 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002649 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002651 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002653#ifdef FEAT_FLOAT
2654 if (rettv->v_type == VAR_FLOAT)
2655 {
2656 f1 = rettv->vval.v_float;
2657 use_float = TRUE;
2658 n1 = 0;
2659 }
2660 else
2661#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002662 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002664 if (error)
2665 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 else
2668 n1 = 0;
2669
2670 /*
2671 * Get the second variable.
2672 */
2673 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002674 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 return FAIL;
2676
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002677 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002679#ifdef FEAT_FLOAT
2680 if (var2.v_type == VAR_FLOAT)
2681 {
2682 if (!use_float)
2683 {
2684 f1 = n1;
2685 use_float = TRUE;
2686 }
2687 f2 = var2.vval.v_float;
2688 n2 = 0;
2689 }
2690 else
2691#endif
2692 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002693 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002694 clear_tv(&var2);
2695 if (error)
2696 return FAIL;
2697#ifdef FEAT_FLOAT
2698 if (use_float)
2699 f2 = n2;
2700#endif
2701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 /*
2704 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002705 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002707#ifdef FEAT_FLOAT
2708 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002710 if (op == '*')
2711 f1 = f1 * f2;
2712 else if (op == '/')
2713 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002714# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002715 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002716 if (f2 == 0.0)
2717 {
2718 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002719 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002720 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002721 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002722 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002723 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002724 }
2725 else
2726 f1 = f1 / f2;
2727# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002728 // We rely on the floating point library to handle divide
2729 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002730 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002731# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002734 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002736 return FAIL;
2737 }
2738 rettv->v_type = VAR_FLOAT;
2739 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002744 if (op == '*')
2745 n1 = n1 * n2;
2746 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002747 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002749 n1 = num_modulus(n1, n2);
2750
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002751 rettv->v_type = VAR_NUMBER;
2752 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755 }
2756
2757 return OK;
2758}
2759
2760/*
2761 * Handle sixth level expression:
2762 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002763 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002764 * "string" string constant
2765 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 * &option-name option value
2767 * @r register contents
2768 * identifier variable value
2769 * function() function call
2770 * $VAR environment variable
2771 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002772 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002774 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002775 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 *
2777 * Also handle:
2778 * ! in front logical NOT
2779 * - in front unary minus
2780 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 * trailing [] subscript in String or List
2782 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002783 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 *
2785 * "arg" must point to the first non-white of the expression.
2786 * "arg" is advanced to the next non-white after the recognized expression.
2787 *
2788 * Return OK or FAIL.
2789 */
2790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002791eval7(
2792 char_u **arg,
2793 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002794 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002797 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2798 int evaluate = evalarg != NULL
2799 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 int len;
2801 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 char_u *start_leader, *end_leader;
2803 int ret = OK;
2804 char_u *alias;
2805
2806 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002808 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002810 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
2812 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002813 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 */
2815 start_leader = *arg;
2816 while (**arg == '!' || **arg == '-' || **arg == '+')
2817 *arg = skipwhite(*arg + 1);
2818 end_leader = *arg;
2819
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002820 if (**arg == '.' && (!isdigit(*(*arg + 1))
2821#ifdef FEAT_FLOAT
2822 || current_sctx.sc_version < 2
2823#endif
2824 ))
2825 {
2826 semsg(_(e_invexpr2), *arg);
2827 ++*arg;
2828 return FAIL;
2829 }
2830
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 switch (**arg)
2832 {
2833 /*
2834 * Number constant.
2835 */
2836 case '0':
2837 case '1':
2838 case '2':
2839 case '3':
2840 case '4':
2841 case '5':
2842 case '6':
2843 case '7':
2844 case '8':
2845 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002847
2848 // Apply prefixed "-" and "+" now. Matters especially when
2849 // "->" follows.
2850 if (ret == OK && evaluate && end_leader > start_leader)
2851 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 break;
2853
2854 /*
2855 * String constant: "string".
2856 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002857 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 break;
2859
2860 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002863 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002864 break;
2865
2866 /*
2867 * List: [expr, expr]
2868 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002869 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 break;
2871
2872 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002873 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002874 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002875 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002876 {
2877 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002878 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002879 }
2880 else
2881 ret = NOTDONE;
2882 break;
2883
2884 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002885 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002886 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002888 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002889 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002890 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 break;
2892
2893 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002894 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002896 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 break;
2898
2899 /*
2900 * Environment variable: $VAR.
2901 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002902 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 break;
2904
2905 /*
2906 * Register contents: @r.
2907 */
2908 case '@': ++*arg;
2909 if (evaluate)
2910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002912 rettv->vval.v_string = get_reg_contents(**arg,
2913 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 if (**arg != NUL)
2916 ++*arg;
2917 break;
2918
2919 /*
2920 * nested expression: (expression).
2921 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002923 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002924 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002925
Bram Moolenaar9215f012020-06-27 21:18:00 +02002926 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002927 if (**arg == ')')
2928 ++*arg;
2929 else if (ret == OK)
2930 {
2931 emsg(_(e_missing_close));
2932 clear_tv(rettv);
2933 ret = FAIL;
2934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
2936 break;
2937
Bram Moolenaar8c711452005-01-14 21:53:12 +00002938 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 break;
2940 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002941
2942 if (ret == NOTDONE)
2943 {
2944 /*
2945 * Must be a variable or function name.
2946 * Can also be a curly-braces kind of name: {expr}.
2947 */
2948 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002949 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002950 if (alias != NULL)
2951 s = alias;
2952
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002953 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002954 ret = FAIL;
2955 else
2956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002957 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002958 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002959 else if (flags & EVAL_CONSTANT)
2960 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002961 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002962 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002964 {
2965 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002966 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002967 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002968 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002969 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002970 }
2971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 *arg = skipwhite(*arg);
2973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002974 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2975 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002976 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002977 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979 /*
2980 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2981 */
2982 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002983 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002984 return ret;
2985}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002986
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002987/*
2988 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002989 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002990 * Adjusts "end_leaderp" until it is at "start_leader".
2991 */
2992 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002993eval7_leader(
2994 typval_T *rettv,
2995 int numeric_only,
2996 char_u *start_leader,
2997 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002998{
2999 char_u *end_leader = *end_leaderp;
3000 int ret = OK;
3001 int error = FALSE;
3002 varnumber_T val = 0;
3003#ifdef FEAT_FLOAT
3004 float_T f = 0.0;
3005
3006 if (rettv->v_type == VAR_FLOAT)
3007 f = rettv->vval.v_float;
3008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003009#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003010 val = tv_get_number_chk(rettv, &error);
3011 if (error)
3012 {
3013 clear_tv(rettv);
3014 ret = FAIL;
3015 }
3016 else
3017 {
3018 while (end_leader > start_leader)
3019 {
3020 --end_leader;
3021 if (*end_leader == '!')
3022 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003023 if (numeric_only)
3024 {
3025 ++end_leader;
3026 break;
3027 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003028#ifdef FEAT_FLOAT
3029 if (rettv->v_type == VAR_FLOAT)
3030 f = !f;
3031 else
3032#endif
3033 val = !val;
3034 }
3035 else if (*end_leader == '-')
3036 {
3037#ifdef FEAT_FLOAT
3038 if (rettv->v_type == VAR_FLOAT)
3039 f = -f;
3040 else
3041#endif
3042 val = -val;
3043 }
3044 }
3045#ifdef FEAT_FLOAT
3046 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003048 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003049 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003051 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003052#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003053 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003054 clear_tv(rettv);
3055 rettv->v_type = VAR_NUMBER;
3056 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003059 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 return ret;
3061}
3062
3063/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003064 * Call the function referred to in "rettv".
3065 */
3066 static int
3067call_func_rettv(
3068 char_u **arg,
3069 typval_T *rettv,
3070 int evaluate,
3071 dict_T *selfdict,
3072 typval_T *basetv)
3073{
3074 partial_T *pt = NULL;
3075 funcexe_T funcexe;
3076 typval_T functv;
3077 char_u *s;
3078 int ret;
3079
3080 // need to copy the funcref so that we can clear rettv
3081 if (evaluate)
3082 {
3083 functv = *rettv;
3084 rettv->v_type = VAR_UNKNOWN;
3085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003086 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003087 if (functv.v_type == VAR_PARTIAL)
3088 {
3089 pt = functv.vval.v_partial;
3090 s = partial_name(pt);
3091 }
3092 else
3093 s = functv.vval.v_string;
3094 }
3095 else
3096 s = (char_u *)"";
3097
Bram Moolenaara80faa82020-04-12 19:37:17 +02003098 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003099 funcexe.firstline = curwin->w_cursor.lnum;
3100 funcexe.lastline = curwin->w_cursor.lnum;
3101 funcexe.evaluate = evaluate;
3102 funcexe.partial = pt;
3103 funcexe.selfdict = selfdict;
3104 funcexe.basetv = basetv;
3105 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003107 // Clear the funcref afterwards, so that deleting it while
3108 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003109 if (evaluate)
3110 clear_tv(&functv);
3111
3112 return ret;
3113}
3114
3115/*
3116 * Evaluate "->method()".
3117 * "*arg" points to the '-'.
3118 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3119 */
3120 static int
3121eval_lambda(
3122 char_u **arg,
3123 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003124 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003125 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003126{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003127 int evaluate = evalarg != NULL
3128 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003129 typval_T base = *rettv;
3130 int ret;
3131
3132 // Skip over the ->.
3133 *arg += 2;
3134 rettv->v_type = VAR_UNKNOWN;
3135
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003136 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003137 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003138 return FAIL;
3139 else if (**arg != '(')
3140 {
3141 if (verbose)
3142 {
3143 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003144 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003145 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003147 }
3148 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003149 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003150 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003151 else
3152 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3153
3154 // Clear the funcref afterwards, so that deleting it while
3155 // evaluating the arguments is possible (see test55).
3156 if (evaluate)
3157 clear_tv(&base);
3158
3159 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003160}
3161
3162/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003163 * Evaluate "->method()".
3164 * "*arg" points to the '-'.
3165 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3166 */
3167 static int
3168eval_method(
3169 char_u **arg,
3170 typval_T *rettv,
3171 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003172 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003173{
3174 char_u *name;
3175 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003176 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003177 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003178 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003179
3180 // Skip over the ->.
3181 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003182 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003183
Bram Moolenaarac92e252019-08-03 21:58:38 +02003184 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003185 len = get_name_len(arg, &alias, evaluate, TRUE);
3186 if (alias != NULL)
3187 name = alias;
3188
3189 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003190 {
3191 if (verbose)
3192 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003193 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003194 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003195 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003196 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003197 if (**arg != '(')
3198 {
3199 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003201 ret = FAIL;
3202 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003203 else if (VIM_ISWHITE((*arg)[-1]))
3204 {
3205 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003206 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003207 ret = FAIL;
3208 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003209 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003210 ret = eval_func(arg, name, len, rettv,
3211 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003212 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003213
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003214 // Clear the funcref afterwards, so that deleting it while
3215 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003216 if (evaluate)
3217 clear_tv(&base);
3218
Bram Moolenaarac92e252019-08-03 21:58:38 +02003219 return ret;
3220}
3221
3222/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003223 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3224 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003225 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3226 */
3227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003228eval_index(
3229 char_u **arg,
3230 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003231 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003232 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003233{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003234 int evaluate = evalarg != NULL
3235 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003236 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003238 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003239 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003240 long len = -1;
3241 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003242 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003243 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003244
Bram Moolenaara03f2332016-02-06 18:09:59 +01003245 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003246 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003247 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003248 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003249 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003250 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003251 return FAIL;
3252 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003253#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003254 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003255 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003256 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003257#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003258 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003259 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003260 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003261 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003262 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003263 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003264 return FAIL;
3265 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003266 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003268 if (evaluate)
3269 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003270 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003271
3272 case VAR_STRING:
3273 case VAR_NUMBER:
3274 case VAR_LIST:
3275 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003276 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003277 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003278 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003279
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003280 init_tv(&var1);
3281 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003282 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003284 /*
3285 * dict.name
3286 */
3287 key = *arg + 1;
3288 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3289 ;
3290 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003291 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003292 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003293 }
3294 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003295 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003296 /*
3297 * something[idx]
3298 *
3299 * Get the (first) variable from inside the [].
3300 */
3301 *arg = skipwhite(*arg + 1);
3302 if (**arg == ':')
3303 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003304 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003305 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003306 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003307 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003308 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003309 clear_tv(&var1);
3310 return FAIL;
3311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003312
3313 /*
3314 * Get the second variable from inside the [:].
3315 */
3316 if (**arg == ':')
3317 {
3318 range = TRUE;
3319 *arg = skipwhite(*arg + 1);
3320 if (**arg == ']')
3321 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003322 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003324 if (!empty1)
3325 clear_tv(&var1);
3326 return FAIL;
3327 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003328 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003329 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003330 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003331 if (!empty1)
3332 clear_tv(&var1);
3333 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003334 return FAIL;
3335 }
3336 }
3337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003338 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003339 if (**arg != ']')
3340 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003341 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003342 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003343 clear_tv(&var1);
3344 if (range)
3345 clear_tv(&var2);
3346 return FAIL;
3347 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003348 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003349 }
3350
3351 if (evaluate)
3352 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003353 n1 = 0;
3354 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003356 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003358 }
3359 if (range)
3360 {
3361 if (empty2)
3362 n2 = -1;
3363 else
3364 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003365 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003366 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003367 }
3368 }
3369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003370 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003371 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003372 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003373 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003375 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003376 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003377 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003378 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003379 case VAR_SPECIAL:
3380 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003381 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003382 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003383
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003384 case VAR_NUMBER:
3385 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003386 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003387 len = (long)STRLEN(s);
3388 if (range)
3389 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003390 // The resulting variable is a substring. If the indexes
3391 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003392 if (n1 < 0)
3393 {
3394 n1 = len + n1;
3395 if (n1 < 0)
3396 n1 = 0;
3397 }
3398 if (n2 < 0)
3399 n2 = len + n2;
3400 else if (n2 >= len)
3401 n2 = len;
3402 if (n1 >= len || n2 < 0 || n1 > n2)
3403 s = NULL;
3404 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003405 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003406 }
3407 else
3408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003409 // The resulting variable is a string of a single
3410 // character. If the index is too big or negative the
3411 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003412 if (n1 >= len || n1 < 0)
3413 s = NULL;
3414 else
3415 s = vim_strnsave(s + n1, 1);
3416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003417 clear_tv(rettv);
3418 rettv->v_type = VAR_STRING;
3419 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003420 break;
3421
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003422 case VAR_BLOB:
3423 len = blob_len(rettv->vval.v_blob);
3424 if (range)
3425 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003426 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003427 // are out of range the result is empty.
3428 if (n1 < 0)
3429 {
3430 n1 = len + n1;
3431 if (n1 < 0)
3432 n1 = 0;
3433 }
3434 if (n2 < 0)
3435 n2 = len + n2;
3436 else if (n2 >= len)
3437 n2 = len - 1;
3438 if (n1 >= len || n2 < 0 || n1 > n2)
3439 {
3440 clear_tv(rettv);
3441 rettv->v_type = VAR_BLOB;
3442 rettv->vval.v_blob = NULL;
3443 }
3444 else
3445 {
3446 blob_T *blob = blob_alloc();
3447
3448 if (blob != NULL)
3449 {
3450 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3451 {
3452 blob_free(blob);
3453 return FAIL;
3454 }
3455 blob->bv_ga.ga_len = n2 - n1 + 1;
3456 for (i = n1; i <= n2; i++)
3457 blob_set(blob, i - n1,
3458 blob_get(rettv->vval.v_blob, i));
3459
3460 clear_tv(rettv);
3461 rettv_blob_set(rettv, blob);
3462 }
3463 }
3464 }
3465 else
3466 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003467 // The resulting variable is a byte value.
3468 // If the index is too big or negative that is an error.
3469 if (n1 < 0)
3470 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003471 if (n1 < len && n1 >= 0)
3472 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003473 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003474
3475 clear_tv(rettv);
3476 rettv->v_type = VAR_NUMBER;
3477 rettv->vval.v_number = v;
3478 }
3479 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003480 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003481 }
3482 break;
3483
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003484 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003485 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003486 if (n1 < 0)
3487 n1 = len + n1;
3488 if (!empty1 && (n1 < 0 || n1 >= len))
3489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003490 // For a range we allow invalid values and return an empty
3491 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003492 if (!range)
3493 {
3494 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003495 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003496 return FAIL;
3497 }
3498 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003499 }
3500 if (range)
3501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503
3504 if (n2 < 0)
3505 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003506 else if (n2 >= len)
3507 n2 = len - 1;
3508 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003509 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003510 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003511 if (l == NULL)
3512 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003513 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003514 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003515 }
3516 else
3517 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003518 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003519 clear_tv(rettv);
3520 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003521 }
3522 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003523
3524 case VAR_DICT:
3525 if (range)
3526 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003527 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003528 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003529 if (len == -1)
3530 clear_tv(&var1);
3531 return FAIL;
3532 }
3533 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003534 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003535
3536 if (len == -1)
3537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003538 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003539 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003540 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003541 clear_tv(&var1);
3542 return FAIL;
3543 }
3544 }
3545
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003546 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003547
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003548 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003549 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003550 if (len == -1)
3551 clear_tv(&var1);
3552 if (item == NULL)
3553 return FAIL;
3554
3555 copy_tv(&item->di_tv, &var1);
3556 clear_tv(rettv);
3557 *rettv = var1;
3558 }
3559 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003560 }
3561 }
3562
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003563 return OK;
3564}
3565
3566/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003567 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003568 */
3569 char_u *
3570partial_name(partial_T *pt)
3571{
3572 if (pt->pt_name != NULL)
3573 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003574 if (pt->pt_func != NULL)
3575 return pt->pt_func->uf_name;
3576 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003577}
3578
Bram Moolenaarddecc252016-04-06 22:59:37 +02003579 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003580partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003581{
3582 int i;
3583
3584 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003585 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003586 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003587 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003588 if (pt->pt_name != NULL)
3589 {
3590 func_unref(pt->pt_name);
3591 vim_free(pt->pt_name);
3592 }
3593 else
3594 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003595
3596 if (pt->pt_funcstack != NULL)
3597 {
3598 // Decrease the reference count for the context of a closure. If down
3599 // to zero free it and clear the variables on the stack.
3600 if (--pt->pt_funcstack->fs_refcount == 0)
3601 {
3602 garray_T *gap = &pt->pt_funcstack->fs_ga;
3603 typval_T *stack = gap->ga_data;
3604
3605 for (i = 0; i < gap->ga_len; ++i)
3606 clear_tv(stack + i);
3607 ga_clear(gap);
3608 vim_free(pt->pt_funcstack);
3609 }
3610 pt->pt_funcstack = NULL;
3611 }
3612
Bram Moolenaarddecc252016-04-06 22:59:37 +02003613 vim_free(pt);
3614}
3615
3616/*
3617 * Unreference a closure: decrement the reference count and free it when it
3618 * becomes zero.
3619 */
3620 void
3621partial_unref(partial_T *pt)
3622{
3623 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003624 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003625}
3626
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003627/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003628 * Return the next (unique) copy ID.
3629 * Used for serializing nested structures.
3630 */
3631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003633{
3634 current_copyID += COPYID_INC;
3635 return current_copyID;
3636}
3637
3638/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003639 * Garbage collection for lists and dictionaries.
3640 *
3641 * We use reference counts to be able to free most items right away when they
3642 * are no longer used. But for composite items it's possible that it becomes
3643 * unused while the reference count is > 0: When there is a recursive
3644 * reference. Example:
3645 * :let l = [1, 2, 3]
3646 * :let d = {9: l}
3647 * :let l[1] = d
3648 *
3649 * Since this is quite unusual we handle this with garbage collection: every
3650 * once in a while find out which lists and dicts are not referenced from any
3651 * variable.
3652 *
3653 * Here is a good reference text about garbage collection (refers to Python
3654 * but it applies to all reference-counting mechanisms):
3655 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003656 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003657
3658/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003659 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003660 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003661 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003662 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003663 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003664garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003665{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003666 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003667 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003668 buf_T *buf;
3669 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003670 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003671 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003672
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003673 if (!testing)
3674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003675 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003676 want_garbage_collect = FALSE;
3677 may_garbage_collect = FALSE;
3678 garbage_collect_at_exit = FALSE;
3679 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003680
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003681 // The execution stack can grow big, limit the size.
3682 if (exestack.ga_maxlen - exestack.ga_len > 500)
3683 {
3684 size_t new_len;
3685 char_u *pp;
3686 int n;
3687
3688 // Keep 150% of the current size, with a minimum of the growth size.
3689 n = exestack.ga_len / 2;
3690 if (n < exestack.ga_growsize)
3691 n = exestack.ga_growsize;
3692
3693 // Don't make it bigger though.
3694 if (exestack.ga_len + n < exestack.ga_maxlen)
3695 {
3696 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3697 pp = vim_realloc(exestack.ga_data, new_len);
3698 if (pp == NULL)
3699 return FAIL;
3700 exestack.ga_maxlen = exestack.ga_len + n;
3701 exestack.ga_data = pp;
3702 }
3703 }
3704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003705 // We advance by two because we add one for items referenced through
3706 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003707 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003708
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003709 /*
3710 * 1. Go through all accessible variables and mark all lists and dicts
3711 * with copyID.
3712 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003714 // Don't free variables in the previous_funccal list unless they are only
3715 // referenced through previous_funccal. This must be first, because if
3716 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003717 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003718
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003720 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003721
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003722 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003723 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003724 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3725 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003727 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003728 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003729 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3730 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003731 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003732 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3733 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003734#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003735 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003736 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3737 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003738 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003739 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003740 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3741 NULL, NULL);
3742#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003743
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003744 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003745 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003746 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3747 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003748 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003749 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003750
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003751 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003754 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003755 abort = abort || set_ref_in_functions(copyID);
3756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003757 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003760 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003761 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003762
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003763 // callbacks in buffers
3764 abort = abort || set_ref_in_buffers(copyID);
3765
Bram Moolenaar1dced572012-04-05 16:54:08 +02003766#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003767 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003768#endif
3769
Bram Moolenaardb913952012-06-29 12:54:53 +02003770#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003771 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003772#endif
3773
3774#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003775 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003776#endif
3777
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003778#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003779 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003780 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003781#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003782#ifdef FEAT_NETBEANS_INTG
3783 abort = abort || set_ref_in_nb_channel(copyID);
3784#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003785
Bram Moolenaare3188e22016-05-31 21:13:04 +02003786#ifdef FEAT_TIMERS
3787 abort = abort || set_ref_in_timer(copyID);
3788#endif
3789
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003790#ifdef FEAT_QUICKFIX
3791 abort = abort || set_ref_in_quickfix(copyID);
3792#endif
3793
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003794#ifdef FEAT_TERMINAL
3795 abort = abort || set_ref_in_term(copyID);
3796#endif
3797
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003798#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003799 abort = abort || set_ref_in_popups(copyID);
3800#endif
3801
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003802 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003803 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003804 /*
3805 * 2. Free lists and dictionaries that are not referenced.
3806 */
3807 did_free = free_unref_items(copyID);
3808
3809 /*
3810 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003812 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003814 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003815 else if (p_verbose > 0)
3816 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003817 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003818 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003819
3820 return did_free;
3821}
3822
3823/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003824 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003825 */
3826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003827free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003828{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003829 int did_free = FALSE;
3830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003831 // Let all "free" functions know that we are here. This means no
3832 // dictionaries, lists, channels or jobs are to be freed, because we will
3833 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003834 in_free_unref_items = TRUE;
3835
3836 /*
3837 * PASS 1: free the contents of the items. We don't free the items
3838 * themselves yet, so that it is possible to decrement refcount counters
3839 */
3840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003841 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003842 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003844 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003845 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003846
3847#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003848 // Go through the list of jobs and free items without the copyID. This
3849 // must happen before doing channels, because jobs refer to channels, but
3850 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003851 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003853 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003854 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3855#endif
3856
3857 /*
3858 * PASS 2: free the items themselves.
3859 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003860 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003861 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003862
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003863#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003864 // Go through the list of jobs and free items without the copyID. This
3865 // must happen before doing channels, because jobs refer to channels, but
3866 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003867 free_unused_jobs(copyID, COPYID_MASK);
3868
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003869 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003870 free_unused_channels(copyID, COPYID_MASK);
3871#endif
3872
3873 in_free_unref_items = FALSE;
3874
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003875 return did_free;
3876}
3877
3878/*
3879 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003880 * "list_stack" is used to add lists to be marked. Can be NULL.
3881 *
3882 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003883 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003884 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003885set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003886{
3887 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003888 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003889 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003890 hashtab_T *cur_ht;
3891 ht_stack_T *ht_stack = NULL;
3892 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003893
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003894 cur_ht = ht;
3895 for (;;)
3896 {
3897 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003898 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003899 // Mark each item in the hashtab. If the item contains a hashtab
3900 // it is added to ht_stack, if it contains a list it is added to
3901 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003902 todo = (int)cur_ht->ht_used;
3903 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3904 if (!HASHITEM_EMPTY(hi))
3905 {
3906 --todo;
3907 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3908 &ht_stack, list_stack);
3909 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003910 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003911
3912 if (ht_stack == NULL)
3913 break;
3914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003915 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003916 cur_ht = ht_stack->ht;
3917 tempitem = ht_stack;
3918 ht_stack = ht_stack->prev;
3919 free(tempitem);
3920 }
3921
3922 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003923}
3924
3925/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003926 * Mark a dict and its items with "copyID".
3927 * Returns TRUE if setting references failed somehow.
3928 */
3929 int
3930set_ref_in_dict(dict_T *d, int copyID)
3931{
3932 if (d != NULL && d->dv_copyID != copyID)
3933 {
3934 d->dv_copyID = copyID;
3935 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3936 }
3937 return FALSE;
3938}
3939
3940/*
3941 * Mark a list and its items with "copyID".
3942 * Returns TRUE if setting references failed somehow.
3943 */
3944 int
3945set_ref_in_list(list_T *ll, int copyID)
3946{
3947 if (ll != NULL && ll->lv_copyID != copyID)
3948 {
3949 ll->lv_copyID = copyID;
3950 return set_ref_in_list_items(ll, copyID, NULL);
3951 }
3952 return FALSE;
3953}
3954
3955/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003956 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003957 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3958 *
3959 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003960 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003961 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003962set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003963{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003964 listitem_T *li;
3965 int abort = FALSE;
3966 list_T *cur_l;
3967 list_stack_T *list_stack = NULL;
3968 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003969
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003970 cur_l = l;
3971 for (;;)
3972 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003973 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003974 // Mark each item in the list. If the item contains a hashtab
3975 // it is added to ht_stack, if it contains a list it is added to
3976 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003977 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
3978 abort = abort || set_ref_in_item(&li->li_tv, copyID,
3979 ht_stack, &list_stack);
3980 if (list_stack == NULL)
3981 break;
3982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003983 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003984 cur_l = list_stack->list;
3985 tempitem = list_stack;
3986 list_stack = list_stack->prev;
3987 free(tempitem);
3988 }
3989
3990 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003991}
3992
3993/*
3994 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003995 * "list_stack" is used to add lists to be marked. Can be NULL.
3996 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3997 *
3998 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003999 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001set_ref_in_item(
4002 typval_T *tv,
4003 int copyID,
4004 ht_stack_T **ht_stack,
4005 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004006{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004007 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004008
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004009 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004010 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004011 dict_T *dd = tv->vval.v_dict;
4012
Bram Moolenaara03f2332016-02-06 18:09:59 +01004013 if (dd != NULL && dd->dv_copyID != copyID)
4014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004015 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004016 dd->dv_copyID = copyID;
4017 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004018 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004019 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4020 }
4021 else
4022 {
4023 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4024 if (newitem == NULL)
4025 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004026 else
4027 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004028 newitem->ht = &dd->dv_hashtab;
4029 newitem->prev = *ht_stack;
4030 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004031 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004032 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004033 }
4034 }
4035 else if (tv->v_type == VAR_LIST)
4036 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004037 list_T *ll = tv->vval.v_list;
4038
Bram Moolenaara03f2332016-02-06 18:09:59 +01004039 if (ll != NULL && ll->lv_copyID != copyID)
4040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004041 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004042 ll->lv_copyID = copyID;
4043 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004044 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004045 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004046 }
4047 else
4048 {
4049 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004050 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004051 if (newitem == NULL)
4052 abort = TRUE;
4053 else
4054 {
4055 newitem->list = ll;
4056 newitem->prev = *list_stack;
4057 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004058 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004059 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004060 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004061 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004062 else if (tv->v_type == VAR_FUNC)
4063 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004064 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004065 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004066 else if (tv->v_type == VAR_PARTIAL)
4067 {
4068 partial_T *pt = tv->vval.v_partial;
4069 int i;
4070
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004071 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004072 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004073 // Didn't see this partial yet.
4074 pt->pt_copyID = copyID;
4075
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004076 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004077
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004078 if (pt->pt_dict != NULL)
4079 {
4080 typval_T dtv;
4081
4082 dtv.v_type = VAR_DICT;
4083 dtv.vval.v_dict = pt->pt_dict;
4084 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4085 }
4086
4087 for (i = 0; i < pt->pt_argc; ++i)
4088 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4089 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004090 if (pt->pt_funcstack != NULL)
4091 {
4092 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4093
4094 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4095 abort = abort || set_ref_in_item(stack + i, copyID,
4096 ht_stack, list_stack);
4097 }
4098
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004099 }
4100 }
4101#ifdef FEAT_JOB_CHANNEL
4102 else if (tv->v_type == VAR_JOB)
4103 {
4104 job_T *job = tv->vval.v_job;
4105 typval_T dtv;
4106
4107 if (job != NULL && job->jv_copyID != copyID)
4108 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004109 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004110 if (job->jv_channel != NULL)
4111 {
4112 dtv.v_type = VAR_CHANNEL;
4113 dtv.vval.v_channel = job->jv_channel;
4114 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4115 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004116 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004117 {
4118 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004119 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004120 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4121 }
4122 }
4123 }
4124 else if (tv->v_type == VAR_CHANNEL)
4125 {
4126 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004127 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004128 typval_T dtv;
4129 jsonq_T *jq;
4130 cbq_T *cq;
4131
4132 if (ch != NULL && ch->ch_copyID != copyID)
4133 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004134 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004135 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004136 {
4137 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4138 jq = jq->jq_next)
4139 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4140 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4141 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004142 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004143 {
4144 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004145 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004146 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4147 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004148 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004149 {
4150 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004151 dtv.vval.v_partial =
4152 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004153 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4154 }
4155 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004156 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004157 {
4158 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004159 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004160 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4161 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004162 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004163 {
4164 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004165 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004166 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4167 }
4168 }
4169 }
4170#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004171 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004172}
4173
Bram Moolenaar8c711452005-01-14 21:53:12 +00004174/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004175 * Return a string with the string representation of a variable.
4176 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004177 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004178 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004179 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4180 * stings as "string()", otherwise does not put quotes around strings, as
4181 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004182 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4183 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004184 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004185 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004186 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004187echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004188 typval_T *tv,
4189 char_u **tofree,
4190 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004191 int copyID,
4192 int echo_style,
4193 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004194 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004195{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004196 static int recurse = 0;
4197 char_u *r = NULL;
4198
Bram Moolenaar33570922005-01-25 22:26:29 +00004199 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004200 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004201 if (!did_echo_string_emsg)
4202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004203 // Only give this message once for a recursive call to avoid
4204 // flooding the user with errors. And stop iterating over lists
4205 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004206 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004207 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004209 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004210 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004211 }
4212 ++recurse;
4213
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004214 switch (tv->v_type)
4215 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004216 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004217 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004218 {
4219 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004220 r = tv->vval.v_string;
4221 if (r == NULL)
4222 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004223 }
4224 else
4225 {
4226 *tofree = string_quote(tv->vval.v_string, FALSE);
4227 r = *tofree;
4228 }
4229 break;
4230
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004231 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004232 if (echo_style)
4233 {
4234 *tofree = NULL;
4235 r = tv->vval.v_string;
4236 }
4237 else
4238 {
4239 *tofree = string_quote(tv->vval.v_string, TRUE);
4240 r = *tofree;
4241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004242 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004243
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004244 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004245 {
4246 partial_T *pt = tv->vval.v_partial;
4247 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004248 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004249 garray_T ga;
4250 int i;
4251 char_u *tf;
4252
4253 ga_init2(&ga, 1, 100);
4254 ga_concat(&ga, (char_u *)"function(");
4255 if (fname != NULL)
4256 {
4257 ga_concat(&ga, fname);
4258 vim_free(fname);
4259 }
4260 if (pt != NULL && pt->pt_argc > 0)
4261 {
4262 ga_concat(&ga, (char_u *)", [");
4263 for (i = 0; i < pt->pt_argc; ++i)
4264 {
4265 if (i > 0)
4266 ga_concat(&ga, (char_u *)", ");
4267 ga_concat(&ga,
4268 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4269 vim_free(tf);
4270 }
4271 ga_concat(&ga, (char_u *)"]");
4272 }
4273 if (pt != NULL && pt->pt_dict != NULL)
4274 {
4275 typval_T dtv;
4276
4277 ga_concat(&ga, (char_u *)", ");
4278 dtv.v_type = VAR_DICT;
4279 dtv.vval.v_dict = pt->pt_dict;
4280 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4281 vim_free(tf);
4282 }
4283 ga_concat(&ga, (char_u *)")");
4284
4285 *tofree = ga.ga_data;
4286 r = *tofree;
4287 break;
4288 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004289
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004290 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004291 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004292 break;
4293
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004294 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004295 if (tv->vval.v_list == NULL)
4296 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004297 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004298 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004299 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004300 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004301 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4302 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004303 {
4304 *tofree = NULL;
4305 r = (char_u *)"[...]";
4306 }
4307 else
4308 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004309 int old_copyID = tv->vval.v_list->lv_copyID;
4310
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004311 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004312 *tofree = list2string(tv, copyID, restore_copyID);
4313 if (restore_copyID)
4314 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004315 r = *tofree;
4316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004317 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004318
Bram Moolenaar8c711452005-01-14 21:53:12 +00004319 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004320 if (tv->vval.v_dict == NULL)
4321 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004322 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004323 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004324 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004325 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004326 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4327 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004328 {
4329 *tofree = NULL;
4330 r = (char_u *)"{...}";
4331 }
4332 else
4333 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004334 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004335
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004336 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004337 *tofree = dict2string(tv, copyID, restore_copyID);
4338 if (restore_copyID)
4339 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004340 r = *tofree;
4341 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004342 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004343
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004344 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004345 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004346 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004348 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004349 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004350 break;
4351
Bram Moolenaar835dc632016-02-07 14:27:38 +01004352 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004353 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004354 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004355 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004356 if (composite_val)
4357 {
4358 *tofree = string_quote(r, FALSE);
4359 r = *tofree;
4360 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004361 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004362
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004363 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004364#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004365 *tofree = NULL;
4366 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4367 r = numbuf;
4368 break;
4369#endif
4370
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004371 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004372 case VAR_SPECIAL:
4373 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004374 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004375 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004376 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004377
Bram Moolenaar8502c702014-06-17 12:51:16 +02004378 if (--recurse == 0)
4379 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004380 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004381}
4382
4383/*
4384 * Return a string with the string representation of a variable.
4385 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4386 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004387 * Does not put quotes around strings, as ":echo" displays values.
4388 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4389 * May return NULL.
4390 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004391 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004392echo_string(
4393 typval_T *tv,
4394 char_u **tofree,
4395 char_u *numbuf,
4396 int copyID)
4397{
4398 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4399}
4400
4401/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004402 * Return string "str" in ' quotes, doubling ' characters.
4403 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004404 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004405 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004406 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004407string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004408{
Bram Moolenaar33570922005-01-25 22:26:29 +00004409 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004410 char_u *p, *r, *s;
4411
Bram Moolenaar33570922005-01-25 22:26:29 +00004412 len = (function ? 13 : 3);
4413 if (str != NULL)
4414 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004415 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004416 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004417 if (*p == '\'')
4418 ++len;
4419 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004420 s = r = alloc(len);
4421 if (r != NULL)
4422 {
4423 if (function)
4424 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004425 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004426 r += 10;
4427 }
4428 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004429 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004430 if (str != NULL)
4431 for (p = str; *p != NUL; )
4432 {
4433 if (*p == '\'')
4434 *r++ = '\'';
4435 MB_COPY_CHAR(p, r);
4436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004437 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004438 if (function)
4439 *r++ = ')';
4440 *r++ = NUL;
4441 }
4442 return s;
4443}
4444
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004445#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004446/*
4447 * Convert the string "text" to a floating point number.
4448 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4449 * this always uses a decimal point.
4450 * Returns the length of the text that was consumed.
4451 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004453string2float(
4454 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004456{
4457 char *s = (char *)text;
4458 float_T f;
4459
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004460 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004461 if (STRNICMP(text, "inf", 3) == 0)
4462 {
4463 *value = INFINITY;
4464 return 3;
4465 }
4466 if (STRNICMP(text, "-inf", 3) == 0)
4467 {
4468 *value = -INFINITY;
4469 return 4;
4470 }
4471 if (STRNICMP(text, "nan", 3) == 0)
4472 {
4473 *value = NAN;
4474 return 3;
4475 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004476 f = strtod(s, &s);
4477 *value = f;
4478 return (int)((char_u *)s - text);
4479}
4480#endif
4481
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004484 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004486 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004487var2fpos(
4488 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004489 int dollar_lnum, // TRUE when $ is last line
4490 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004492 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004494 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004496 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004497 if (varp->v_type == VAR_LIST)
4498 {
4499 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004500 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004501 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004502 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004503
4504 l = varp->vval.v_list;
4505 if (l == NULL)
4506 return NULL;
4507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004508 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004509 pos.lnum = list_find_nr(l, 0L, &error);
4510 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004511 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004513 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004514 pos.col = list_find_nr(l, 1L, &error);
4515 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004516 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004517 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004519 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004520 li = list_find(l, 1L);
4521 if (li != NULL && li->li_tv.v_type == VAR_STRING
4522 && li->li_tv.vval.v_string != NULL
4523 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4524 pos.col = len + 1;
4525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004526 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004527 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004528 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004529 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004531 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004532 pos.coladd = list_find_nr(l, 2L, &error);
4533 if (error)
4534 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004535
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004536 return &pos;
4537 }
4538
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004539 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004540 if (name == NULL)
4541 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004542 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004544 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004545 {
4546 if (VIsual_active)
4547 return &VIsual;
4548 return &curwin->w_cursor;
4549 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004552 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4554 return NULL;
4555 return pp;
4556 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004557
Bram Moolenaara5525202006-03-02 22:52:09 +00004558 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004559
Bram Moolenaar477933c2007-07-17 14:32:23 +00004560 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004561 {
4562 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004563 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004564 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004565 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004566 // In silent Ex mode topline is zero, but that's not a valid line
4567 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004568 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004569 return &pos;
4570 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004571 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004572 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004573 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004574 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004575 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004576 return &pos;
4577 }
4578 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004579 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004581 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
4583 pos.lnum = curbuf->b_ml.ml_line_count;
4584 pos.col = 0;
4585 }
4586 else
4587 {
4588 pos.lnum = curwin->w_cursor.lnum;
4589 pos.col = (colnr_T)STRLEN(ml_get_curline());
4590 }
4591 return &pos;
4592 }
4593 return NULL;
4594}
4595
4596/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004597 * Convert list in "arg" into a position and optional file number.
4598 * When "fnump" is NULL there is no file number, only 3 items.
4599 * Note that the column is passed on as-is, the caller may want to decrement
4600 * it to use 1 for the first column.
4601 * Return FAIL when conversion is not possible, doesn't check the position for
4602 * validity.
4603 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004605list2fpos(
4606 typval_T *arg,
4607 pos_T *posp,
4608 int *fnump,
4609 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004610{
4611 list_T *l = arg->vval.v_list;
4612 long i = 0;
4613 long n;
4614
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004615 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4616 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004617 if (arg->v_type != VAR_LIST
4618 || l == NULL
4619 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004620 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004621 return FAIL;
4622
4623 if (fnump != NULL)
4624 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004625 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004626 if (n < 0)
4627 return FAIL;
4628 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004629 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004630 *fnump = n;
4631 }
4632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004633 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004634 if (n < 0)
4635 return FAIL;
4636 posp->lnum = n;
4637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004639 if (n < 0)
4640 return FAIL;
4641 posp->col = n;
4642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004644 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004645 posp->coladd = 0;
4646 else
4647 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004648
Bram Moolenaar493c1782014-05-28 14:34:46 +02004649 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004650 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004651
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004652 return OK;
4653}
4654
4655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 * Get the length of an environment variable name.
4657 * Advance "arg" to the first character after the name.
4658 * Return 0 for error.
4659 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004660 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004661get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662{
4663 char_u *p;
4664 int len;
4665
4666 for (p = *arg; vim_isIDc(*p); ++p)
4667 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 return 0;
4670
4671 len = (int)(p - *arg);
4672 *arg = p;
4673 return len;
4674}
4675
4676/*
4677 * Get the length of the name of a function or internal variable.
4678 * "arg" is advanced to the first non-white character after the name.
4679 * Return 0 if something is wrong.
4680 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004681 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004682get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683{
4684 char_u *p;
4685 int len;
4686
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004687 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004689 {
4690 if (*p == ':')
4691 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004692 // "s:" is start of "s:var", but "n:" is not and can be used in
4693 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004694 len = (int)(p - *arg);
4695 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4696 || len > 1)
4697 break;
4698 }
4699 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004700 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 return 0;
4702
4703 len = (int)(p - *arg);
4704 *arg = skipwhite(p);
4705
4706 return len;
4707}
4708
4709/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004710 * Get the length of the name of a variable or function.
4711 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004713 * Return -1 if curly braces expansion failed.
4714 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 * If the name contains 'magic' {}'s, expand them and return the
4716 * expanded name in an allocated string via 'alias' - caller must free.
4717 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004718 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004719get_name_len(
4720 char_u **arg,
4721 char_u **alias,
4722 int evaluate,
4723 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
4725 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 char_u *p;
4727 char_u *expr_start;
4728 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004730 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
4732 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4733 && (*arg)[2] == (int)KE_SNR)
4734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004735 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 *arg += 3;
4737 return get_id_len(arg) + 3;
4738 }
4739 len = eval_fname_script(*arg);
4740 if (len > 0)
4741 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004742 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 *arg += len;
4744 }
4745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004747 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004749 p = find_name_end(*arg, &expr_start, &expr_end,
4750 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (expr_start != NULL)
4752 {
4753 char_u *temp_string;
4754
4755 if (!evaluate)
4756 {
4757 len += (int)(p - *arg);
4758 *arg = skipwhite(p);
4759 return len;
4760 }
4761
4762 /*
4763 * Include any <SID> etc in the expanded string:
4764 * Thus the -len here.
4765 */
4766 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4767 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004768 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 *alias = temp_string;
4770 *arg = skipwhite(p);
4771 return (int)STRLEN(temp_string);
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
4774 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004775 // Only give an error when there is something, otherwise it will be
4776 // reported at a higher level.
4777 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004778 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 return len;
4781}
4782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783/*
4784 * Find the end of a variable or function name, taking care of magic braces.
4785 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4786 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004787 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004788 * Return a pointer to just after the name. Equal to "arg" if there is no
4789 * valid name.
4790 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004791 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004792find_name_end(
4793 char_u *arg,
4794 char_u **expr_start,
4795 char_u **expr_end,
4796 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798 int mb_nest = 0;
4799 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004801 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004802 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004804 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004806 *expr_start = NULL;
4807 *expr_end = NULL;
4808 }
4809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004810 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004811 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4812 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004813 return arg;
4814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 for (p = arg; *p != NUL
4816 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004817 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004818 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004820 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004822 if (*p == '\'')
4823 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004824 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004825 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004826 ;
4827 if (*p == NUL)
4828 break;
4829 }
4830 else if (*p == '"')
4831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004832 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004833 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004834 if (*p == '\\' && p[1] != NUL)
4835 ++p;
4836 if (*p == NUL)
4837 break;
4838 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004839 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4840 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004841 // "s:" is start of "s:var", but "n:" is not and can be used in
4842 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004843 len = (int)(p - arg);
4844 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004845 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004846 break;
4847 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 if (*p == '[')
4852 ++br_nest;
4853 else if (*p == ']')
4854 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004856
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004857 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 if (*p == '{')
4860 {
4861 mb_nest++;
4862 if (expr_start != NULL && *expr_start == NULL)
4863 *expr_start = p;
4864 }
4865 else if (*p == '}')
4866 {
4867 mb_nest--;
4868 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4869 *expr_end = p;
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873
4874 return p;
4875}
4876
4877/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004878 * Expands out the 'magic' {}'s in a variable/function name.
4879 * Note that this can call itself recursively, to deal with
4880 * constructs like foo{bar}{baz}{bam}
4881 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4882 * "in_start" ^
4883 * "expr_start" ^
4884 * "expr_end" ^
4885 * "in_end" ^
4886 *
4887 * Returns a new allocated string, which the caller must free.
4888 * Returns NULL for failure.
4889 */
4890 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004891make_expanded_name(
4892 char_u *in_start,
4893 char_u *expr_start,
4894 char_u *expr_end,
4895 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004896{
4897 char_u c1;
4898 char_u *retval = NULL;
4899 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004900
4901 if (expr_end == NULL || in_end == NULL)
4902 return NULL;
4903 *expr_start = NUL;
4904 *expr_end = NUL;
4905 c1 = *in_end;
4906 *in_end = NUL;
4907
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004908 temp_result = eval_to_string(expr_start + 1, FALSE);
4909 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004910 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004911 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4912 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004913 if (retval != NULL)
4914 {
4915 STRCPY(retval, in_start);
4916 STRCAT(retval, temp_result);
4917 STRCAT(retval, expr_end + 1);
4918 }
4919 }
4920 vim_free(temp_result);
4921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004923 *expr_start = '{';
4924 *expr_end = '}';
4925
4926 if (retval != NULL)
4927 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004928 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004929 if (expr_start != NULL)
4930 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004931 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004932 temp_result = make_expanded_name(retval, expr_start,
4933 expr_end, temp_result);
4934 vim_free(retval);
4935 retval = temp_result;
4936 }
4937 }
4938
4939 return retval;
4940}
4941
4942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004944 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004947eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004949 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4950}
4951
4952/*
4953 * Return TRUE if character "c" can be used as the first character in a
4954 * variable or function name (excluding '{' and '}').
4955 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004957eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004958{
4959 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960}
4961
4962/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004963 * Handle:
4964 * - expr[expr], expr[expr:expr] subscript
4965 * - ".name" lookup
4966 * - function call with Funcref variable: func(expr)
4967 * - method call: var->method()
4968 *
4969 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004970 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004972handle_subscript(
4973 char_u **arg,
4974 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004975 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004976 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004977{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004978 int evaluate = evalarg != NULL
4979 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004980 int ret = OK;
4981 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004982
Bram Moolenaar61343f02019-07-20 21:11:13 +02004983 // "." is ".name" lookup when we found a dict or when evaluating and
4984 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004985 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02004986 && (((**arg == '['
4987 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02004988 || (!evaluate
4989 && (*arg)[1] != '.'
4990 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004991 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004992 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004993 && !VIM_ISWHITE(*(*arg - 1)))
4994 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004995 {
4996 if (**arg == '(')
4997 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004998 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01004999
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005000 // Stop the expression evaluation when immediately aborting on
5001 // error, or when an interrupt occurred or an exception was thrown
5002 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005003 if (aborting())
5004 {
5005 if (ret == OK)
5006 clear_tv(rettv);
5007 ret = FAIL;
5008 }
5009 dict_unref(selfdict);
5010 selfdict = NULL;
5011 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005012 else if (**arg == '-')
5013 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005014 if (ret == OK)
5015 {
5016 if ((*arg)[2] == '{')
5017 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005018 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005019 else
5020 // expr->name()
5021 ret = eval_method(arg, rettv, evaluate, verbose);
5022 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005023 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005024 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005025 {
5026 dict_unref(selfdict);
5027 if (rettv->v_type == VAR_DICT)
5028 {
5029 selfdict = rettv->vval.v_dict;
5030 if (selfdict != NULL)
5031 ++selfdict->dv_refcount;
5032 }
5033 else
5034 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005035 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005036 {
5037 clear_tv(rettv);
5038 ret = FAIL;
5039 }
5040 }
5041 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005043 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5044 // Don't do this when "Func" is already a partial that was bound
5045 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005046 if (selfdict != NULL
5047 && (rettv->v_type == VAR_FUNC
5048 || (rettv->v_type == VAR_PARTIAL
5049 && (rettv->vval.v_partial->pt_auto
5050 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005051 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005052
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005053 dict_unref(selfdict);
5054 return ret;
5055}
5056
5057/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005058 * Make a copy of an item.
5059 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005060 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5061 * reference to an already copied list/dict can be used.
5062 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005063 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005065item_copy(
5066 typval_T *from,
5067 typval_T *to,
5068 int deep,
5069 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005070{
5071 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005072 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005073
Bram Moolenaar33570922005-01-25 22:26:29 +00005074 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005075 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005076 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005077 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005078 }
5079 ++recurse;
5080
5081 switch (from->v_type)
5082 {
5083 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005084 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005085 case VAR_STRING:
5086 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005087 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005088 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005089 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005090 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005091 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005092 copy_tv(from, to);
5093 break;
5094 case VAR_LIST:
5095 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005096 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005097 if (from->vval.v_list == NULL)
5098 to->vval.v_list = NULL;
5099 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005101 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005102 to->vval.v_list = from->vval.v_list->lv_copylist;
5103 ++to->vval.v_list->lv_refcount;
5104 }
5105 else
5106 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5107 if (to->vval.v_list == NULL)
5108 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005110 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005112 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005113 case VAR_DICT:
5114 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005115 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005116 if (from->vval.v_dict == NULL)
5117 to->vval.v_dict = NULL;
5118 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005120 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005121 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5122 ++to->vval.v_dict->dv_refcount;
5123 }
5124 else
5125 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5126 if (to->vval.v_dict == NULL)
5127 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005128 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005129 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005130 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005132 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005133 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005134 }
5135 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005136 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005137}
5138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 void
5140echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5141{
5142 char_u *tofree;
5143 char_u numbuf[NUMBUFLEN];
5144 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5145
5146 if (*atstart)
5147 {
5148 *atstart = FALSE;
5149 // Call msg_start() after eval1(), evaluating the expression
5150 // may cause a message to appear.
5151 if (with_space)
5152 {
5153 // Mark the saved text as finishing the line, so that what
5154 // follows is displayed on a new line when scrolling back
5155 // at the more prompt.
5156 msg_sb_eol();
5157 msg_start();
5158 }
5159 }
5160 else if (with_space)
5161 msg_puts_attr(" ", echo_attr);
5162
5163 if (p != NULL)
5164 for ( ; *p != NUL && !got_int; ++p)
5165 {
5166 if (*p == '\n' || *p == '\r' || *p == TAB)
5167 {
5168 if (*p != TAB && *needclr)
5169 {
5170 // remove any text still there from the command
5171 msg_clr_eos();
5172 *needclr = FALSE;
5173 }
5174 msg_putchar_attr(*p, echo_attr);
5175 }
5176 else
5177 {
5178 if (has_mbyte)
5179 {
5180 int i = (*mb_ptr2len)(p);
5181
5182 (void)msg_outtrans_len_attr(p, i, echo_attr);
5183 p += i - 1;
5184 }
5185 else
5186 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5187 }
5188 }
5189 vim_free(tofree);
5190}
5191
Bram Moolenaare9a41262005-01-15 22:18:47 +00005192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 * ":echo expr1 ..." print each argument separated with a space, add a
5194 * newline at the end.
5195 * ":echon expr1 ..." print each argument plain.
5196 */
5197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005198ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005201 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 char_u *p;
5203 int needclr = TRUE;
5204 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005205 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005206 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005207 evalarg_T evalarg;
5208
5209 CLEAR_FIELD(evalarg);
5210 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +02005211 if (getline_equal(eap->getline, eap->cookie, getsourceline))
5212 {
5213 evalarg.eval_getline = eap->getline;
5214 evalarg.eval_cookie = eap->cookie;
5215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
5217 if (eap->skip)
5218 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005219 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005221 // If eval1() causes an error message the text from the command may
5222 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 need_clr_eos = needclr;
5224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005226 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
5228 /*
5229 * Report the invalid expression unless the expression evaluation
5230 * has been cancelled due to an aborting error, an interrupt, or an
5231 * exception.
5232 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005233 if (!aborting() && did_emsg == did_emsg_before
5234 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005235 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005236 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 break;
5238 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 need_clr_eos = FALSE;
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005243
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005244 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 arg = skipwhite(arg);
5246 }
5247 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005248 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
5250 if (eap->skip)
5251 --emsg_skip;
5252 else
5253 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005254 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 if (needclr)
5256 msg_clr_eos();
5257 if (eap->cmdidx == CMD_echo)
5258 msg_end();
5259 }
5260}
5261
5262/*
5263 * ":echohl {name}".
5264 */
5265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005266ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005268 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269}
5270
5271/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005272 * Returns the :echo attribute
5273 */
5274 int
5275get_echo_attr(void)
5276{
5277 return echo_attr;
5278}
5279
5280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 * ":execute expr1 ..." execute the result of an expression.
5282 * ":echomsg expr1 ..." Print a message
5283 * ":echoerr expr1 ..." Print an error
5284 * Each gets spaces around each argument and a newline at the end for
5285 * echo commands
5286 */
5287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005288ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
5290 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 int ret = OK;
5293 char_u *p;
5294 garray_T ga;
5295 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 ga_init2(&ga, 1, 80);
5298
5299 if (eap->skip)
5300 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005301 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005303 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5304 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
5307 if (!eap->skip)
5308 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005309 char_u buf[NUMBUFLEN];
5310
5311 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005312 {
5313 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5314 {
5315 emsg(_(e_inval_string));
5316 p = NULL;
5317 }
5318 else
5319 p = tv_get_string_buf(&rettv, buf);
5320 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005321 else
5322 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005323 if (p == NULL)
5324 {
5325 clear_tv(&rettv);
5326 ret = FAIL;
5327 break;
5328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 len = (int)STRLEN(p);
5330 if (ga_grow(&ga, len + 2) == FAIL)
5331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005332 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 ret = FAIL;
5334 break;
5335 }
5336 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 ga.ga_len += len;
5340 }
5341
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005342 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 arg = skipwhite(arg);
5344 }
5345
5346 if (ret != FAIL && ga.ga_data != NULL)
5347 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005348 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5349 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005350 // Mark the already saved text as finishing the line, so that what
5351 // follows is displayed on a new line when scrolling back at the
5352 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005353 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005354 }
5355
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005357 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005358 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005359 out_flush();
5360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 else if (eap->cmdidx == CMD_echoerr)
5362 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005363 int save_did_emsg = did_emsg;
5364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005365 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005366 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 if (!force_abort)
5368 did_emsg = save_did_emsg;
5369 }
5370 else if (eap->cmdidx == CMD_execute)
5371 do_cmdline((char_u *)ga.ga_data,
5372 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5373 }
5374
5375 ga_clear(&ga);
5376
5377 if (eap->skip)
5378 --emsg_skip;
5379
5380 eap->nextcmd = check_nextcmd(arg);
5381}
5382
5383/*
5384 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5385 * "arg" points to the "&" or '+' when called, to "option" when returning.
5386 * Returns NULL when no option name found. Otherwise pointer to the char
5387 * after the option name.
5388 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005389 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005390find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 char_u *p = *arg;
5393
5394 ++p;
5395 if (*p == 'g' && p[1] == ':')
5396 {
5397 *opt_flags = OPT_GLOBAL;
5398 p += 2;
5399 }
5400 else if (*p == 'l' && p[1] == ':')
5401 {
5402 *opt_flags = OPT_LOCAL;
5403 p += 2;
5404 }
5405 else
5406 *opt_flags = 0;
5407
5408 if (!ASCII_ISALPHA(*p))
5409 return NULL;
5410 *arg = p;
5411
5412 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005413 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 else
5415 while (ASCII_ISALPHA(*p))
5416 ++p;
5417 return p;
5418}
5419
5420/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005421 * Display script name where an item was last set.
5422 * Should only be invoked when 'verbose' is non-zero.
5423 */
5424 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005425last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005426{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005427 char_u *p;
5428
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005429 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005430 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005431 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005432 if (p != NULL)
5433 {
5434 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005435 msg_puts(_("\n\tLast set from "));
5436 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005437 if (script_ctx.sc_lnum > 0)
5438 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005439 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005440 msg_outnum((long)script_ctx.sc_lnum);
5441 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005442 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005443 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005444 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005445 }
5446}
5447
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005448#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450/*
5451 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005452 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 * "flags" can be "g" to do a global substitute.
5454 * Returns an allocated string, NULL for error.
5455 */
5456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005457do_string_sub(
5458 char_u *str,
5459 char_u *pat,
5460 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005461 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005462 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 int sublen;
5465 regmatch_T regmatch;
5466 int i;
5467 int do_all;
5468 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005469 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 garray_T ga;
5471 char_u *ret;
5472 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005473 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005475 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005477 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
5479 ga_init2(&ga, 1, 200);
5480
5481 do_all = (flags[0] == 'g');
5482
5483 regmatch.rm_ic = p_ic;
5484 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5485 if (regmatch.regprog != NULL)
5486 {
5487 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005488 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5490 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005491 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005492 if (regmatch.startp[0] == regmatch.endp[0])
5493 {
5494 if (zero_width == regmatch.startp[0])
5495 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005496 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005497 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005498 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5499 (size_t)i);
5500 ga.ga_len += i;
5501 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005502 continue;
5503 }
5504 zero_width = regmatch.startp[0];
5505 }
5506
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 /*
5508 * Get some space for a temporary buffer to do the substitution
5509 * into. It will contain:
5510 * - The text up to where the match is.
5511 * - The substituted text.
5512 * - The text after the match.
5513 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005514 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005515 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5517 {
5518 ga_clear(&ga);
5519 break;
5520 }
5521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005522 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 i = (int)(regmatch.startp[0] - tail);
5524 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005525 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005526 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 + ga.ga_len + i, TRUE, TRUE, FALSE);
5528 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005529 tail = regmatch.endp[0];
5530 if (*tail == NUL)
5531 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 if (!do_all)
5533 break;
5534 }
5535
5536 if (ga.ga_data != NULL)
5537 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5538
Bram Moolenaar473de612013-06-08 18:19:48 +02005539 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541
5542 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5543 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005544 if (p_cpo == empty_option)
5545 p_cpo = save_cpo;
5546 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005547 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005548 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 return ret;
5551}