blob: 141a97315bd27fb34d0e3c12424b0453b4e91117 [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
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020041 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010042 listwatch_T fi_lw; // keep an eye on the item used.
43 list_T *fi_list; // list being used
44 int fi_bi; // index of blob
45 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static 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 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
62 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020063 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010064num_divide(varnumber_T n1, varnumber_T n2)
65{
66 varnumber_T result;
67
68 if (n2 == 0) // give an error message?
69 {
70 if (n1 == 0)
71 result = VARNUM_MIN; // similar to NaN
72 else if (n1 < 0)
73 result = -VARNUM_MAX;
74 else
75 result = VARNUM_MAX;
76 }
77 else
78 result = n1 / n2;
79
80 return result;
81}
82
83/*
84 * Return "n1" modulus "n2", taking care of dividing by zero.
85 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020086 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010087num_modulus(varnumber_T n1, varnumber_T n2)
88{
89 // Give an error when n2 is 0?
90 return (n2 == 0) ? 0 : (n1 % n2);
91}
92
Bram Moolenaara1fa8922017-01-12 20:06:33 +010093#if defined(EBCDIC) || defined(PROTO)
94/*
95 * Compare struct fst by function name.
96 */
97 static int
98compare_func_name(const void *s1, const void *s2)
99{
100 struct fst *p1 = (struct fst *)s1;
101 struct fst *p2 = (struct fst *)s2;
102
103 return STRCMP(p1->f_name, p2->f_name);
104}
105
106/*
107 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100108 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100109 * On machines using EBCDIC we have to sort it.
110 */
111 static void
112sortFunctions(void)
113{
114 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
115
116 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
117}
118#endif
119
Bram Moolenaar33570922005-01-25 22:26:29 +0000120/*
121 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000122 */
123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100124eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000125{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200127 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000128
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200129#ifdef EBCDIC
130 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100131 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200132 */
133 sortFunctions();
134#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000135}
136
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000137#if defined(EXITFREE) || defined(PROTO)
138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100139eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200141 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100142 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200143 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000144
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200145 // autoloaded script names
146 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000147
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200148 // unreferenced lists and dicts
149 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200150
151 // functions not garbage collected
152 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153}
154#endif
155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
157 * Top level evaluation function, returning a boolean.
158 * Sets "error" to TRUE if there was an error.
159 * Return TRUE or FALSE.
160 */
161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100162eval_to_bool(
163 char_u *arg,
164 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200165 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100166 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167{
Bram Moolenaar33570922005-01-25 22:26:29 +0000168 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200169 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200170 evalarg_T evalarg;
171
172 CLEAR_FIELD(evalarg);
173 evalarg.eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200174 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
175 {
176 evalarg.eval_getline = eap->getline;
177 evalarg.eval_cookie = eap->cookie;
178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
180 if (skip)
181 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200182 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 else
185 {
186 *error = FALSE;
187 if (!skip)
188 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100189 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000190 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 }
192 }
193 if (skip)
194 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200195 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200197 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198}
199
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200/*
201 * Call eval1() and give an error message if not done at a lower level.
202 */
203 static int
204eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
205{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100206 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 int ret;
208 int did_emsg_before = did_emsg;
209 int called_emsg_before = called_emsg;
210
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200211 ret = eval1(arg, rettv, evaluate ? &EVALARG_EVALUATE : NULL);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212 if (ret == FAIL)
213 {
214 // Report the invalid expression unless the expression evaluation has
215 // been cancelled due to an aborting error, an interrupt, or an
216 // exception, or we already gave a more specific error.
217 // Also check called_emsg for when using assert_fails().
218 if (!aborting() && did_emsg == did_emsg_before
219 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100220 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 }
222 return ret;
223}
224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200226 * Return whether a typval is a valid expression to pass to eval_expr_typval()
227 * or eval_expr_to_bool(). An empty string returns FALSE;
228 */
229 int
230eval_expr_valid_arg(typval_T *tv)
231{
232 return tv->v_type != VAR_UNKNOWN
233 && (tv->v_type != VAR_STRING
234 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
235}
236
237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 * Evaluate an expression, which can be a function, partial or string.
239 * Pass arguments "argv[argc]".
240 * Return the result in "rettv" and OK or FAIL.
241 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200242 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100243eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
244{
245 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100246 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200247 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100248
249 if (expr->v_type == VAR_FUNC)
250 {
251 s = expr->vval.v_string;
252 if (s == NULL || *s == NUL)
253 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200254 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200255 funcexe.evaluate = TRUE;
256 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 return FAIL;
258 }
259 else if (expr->v_type == VAR_PARTIAL)
260 {
261 partial_T *partial = expr->vval.v_partial;
262
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200263 if (partial == NULL)
264 return FAIL;
265
Bram Moolenaar822ba242020-05-24 23:00:18 +0200266 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200267 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200269 if (call_def_function(partial->pt_func, argc, argv,
270 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 return FAIL;
272 }
273 else
274 {
275 s = partial_name(partial);
276 if (s == NULL || *s == NUL)
277 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200278 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 funcexe.evaluate = TRUE;
280 funcexe.partial = partial;
281 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
282 return FAIL;
283 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100284 }
285 else
286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100287 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 if (s == NULL)
289 return FAIL;
290 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100291 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100293 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200295 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100296 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 return FAIL;
298 }
299 }
300 return OK;
301}
302
303/*
304 * Like eval_to_bool() but using a typval_T instead of a string.
305 * Works for string, funcref and partial.
306 */
307 int
308eval_expr_to_bool(typval_T *expr, int *error)
309{
310 typval_T rettv;
311 int res;
312
313 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
314 {
315 *error = TRUE;
316 return FALSE;
317 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100318 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100319 clear_tv(&rettv);
320 return res;
321}
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323/*
324 * Top level evaluation function, returning a string. If "skip" is TRUE,
325 * only parsing to "nextcmd" is done, without reporting errors. Return
326 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
327 */
328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100329eval_to_string_skip(
330 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200331 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100332 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaar33570922005-01-25 22:26:29 +0000334 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 char_u *retval;
336
337 if (skip)
338 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200339 if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 retval = NULL;
341 else
342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100343 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000344 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 }
346 if (skip)
347 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200348 clear_evalarg(&EVALARG_EVALUATE, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
350 return retval;
351}
352
353/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000354 * Skip over an expression at "*pp".
355 * Return FAIL for an error, OK otherwise.
356 */
357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100358skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000359{
Bram Moolenaar33570922005-01-25 22:26:29 +0000360 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361
362 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200363 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000364}
365
366/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200367 * Skip over an expression at "*pp".
368 * If in Vim9 script and line breaks are encountered, the lines are
369 * concatenated. "evalarg->eval_tofree" will be set accordingly.
370 * Return FAIL for an error, OK otherwise.
371 */
372 int
373skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
374{
375 typval_T rettv;
376 int res;
377 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
378 garray_T *gap = &evalarg->eval_ga;
379 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
380
381 if (vim9script && evalarg->eval_cookie != NULL)
382 {
383 ga_init2(gap, sizeof(char_u *), 10);
384 if (ga_grow(gap, 1) == OK)
385 // leave room for "start"
386 ++gap->ga_len;
387 }
388
389 // Don't evaluate the expression.
390 if (evalarg != NULL)
391 evalarg->eval_flags &= ~EVAL_EVALUATE;
392 *end = skipwhite(*end);
393 res = eval1(end, &rettv, evalarg);
394 if (evalarg != NULL)
395 evalarg->eval_flags = save_flags;
396
397 if (vim9script && evalarg->eval_cookie != NULL
398 && evalarg->eval_ga.ga_len > 1)
399 {
400 char_u *p;
401 size_t endoff = STRLEN(*end);
402
403 // Line breaks encountered, concatenate all the lines.
404 *((char_u **)gap->ga_data) = *start;
405 p = ga_concat_strings(gap, "");
406 *((char_u **)gap->ga_data) = NULL;
407 ga_clear_strings(gap);
408 gap->ga_itemsize = 0;
409 if (p == NULL)
410 return FAIL;
411 *start = p;
412 vim_free(evalarg->eval_tofree);
413 evalarg->eval_tofree = p;
414 // Compute "end" relative to the end.
415 *end = *start + STRLEN(*start) - endoff;
416 }
417
418 return res;
419}
420
421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000423 * When "convert" is TRUE convert a List into a sequence of lines and convert
424 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 * Return pointer to allocated memory, or NULL for failure.
426 */
427 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100428eval_to_string(
429 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100430 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431{
Bram Moolenaar33570922005-01-25 22:26:29 +0000432 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000434 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000435#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000436 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200439 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 retval = NULL;
441 else
442 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000443 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000444 {
445 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000446 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200447 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200448 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200449 if (tv.vval.v_list->lv_len > 0)
450 ga_append(&ga, NL);
451 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000452 ga_append(&ga, NUL);
453 retval = (char_u *)ga.ga_data;
454 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000455#ifdef FEAT_FLOAT
456 else if (convert && tv.v_type == VAR_FLOAT)
457 {
458 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
459 retval = vim_strsave(numbuf);
460 }
461#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000462 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100463 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000464 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200466 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468 return retval;
469}
470
471/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000472 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200473 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 */
475 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100476eval_to_string_safe(
477 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100478 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
480 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200481 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200483 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000484 if (use_sandbox)
485 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200486 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200487 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000488 if (use_sandbox)
489 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200490 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200491 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 return retval;
493}
494
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495/*
496 * Top level evaluation function, returning a number.
497 * Evaluates "expr" silently.
498 * Returns -1 for an error.
499 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200500 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100501eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
Bram Moolenaar33570922005-01-25 22:26:29 +0000503 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200504 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000505 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
507 ++emsg_off;
508
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200509 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 retval = -1;
511 else
512 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100513 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000514 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 }
516 --emsg_off;
517
518 return retval;
519}
520
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000521/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000522 * Top level evaluation function.
523 * Returns an allocated typval_T with the result.
524 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000525 */
526 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200527eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000528{
529 typval_T *tv;
530
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200531 tv = ALLOC_ONE(typval_T);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200532 if (tv != NULL && eval0(arg, tv, eap, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100533 VIM_CLEAR(tv);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200534 clear_evalarg(&EVALARG_EVALUATE, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000535
536 return tv;
537}
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100540 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200541 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
542 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000543 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100546call_vim_function(
547 char_u *func,
548 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200549 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200550 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000552 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200553 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100555 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200556 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200557 funcexe.firstline = curwin->w_cursor.lnum;
558 funcexe.lastline = curwin->w_cursor.lnum;
559 funcexe.evaluate = TRUE;
560 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000561 if (ret == FAIL)
562 clear_tv(rettv);
563
564 return ret;
565}
566
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100567/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100568 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100569 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200570 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
571 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100572 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200573 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100574call_func_retnr(
575 char_u *func,
576 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200577 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100578{
579 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200580 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100581
Bram Moolenaarded27a12018-08-01 19:06:03 +0200582 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100583 return -1;
584
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100585 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100586 clear_tv(&rettv);
587 return retval;
588}
589
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000590/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100591 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000592 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200593 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
594 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000595 */
596 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100597call_func_retstr(
598 char_u *func,
599 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200600 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000601{
602 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000603 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000604
Bram Moolenaarded27a12018-08-01 19:06:03 +0200605 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000606 return NULL;
607
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100608 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000609 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 return retval;
611}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000612
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000613/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100614 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200615 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
616 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000617 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000618 */
619 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100620call_func_retlist(
621 char_u *func,
622 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200623 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000624{
625 typval_T rettv;
626
Bram Moolenaarded27a12018-08-01 19:06:03 +0200627 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000628 return NULL;
629
630 if (rettv.v_type != VAR_LIST)
631 {
632 clear_tv(&rettv);
633 return NULL;
634 }
635
636 return rettv.vval.v_list;
637}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639#ifdef FEAT_FOLDING
640/*
641 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
642 * it in "*cp". Doesn't give error messages.
643 */
644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100645eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
Bram Moolenaar33570922005-01-25 22:26:29 +0000647 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200648 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000650 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
651 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
653 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000654 if (use_sandbox)
655 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200656 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200658 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 retval = 0;
660 else
661 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100662 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000663 if (tv.v_type == VAR_NUMBER)
664 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000665 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 retval = 0;
667 else
668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100669 // If the result is a string, check if there is a non-digit before
670 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000671 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (!VIM_ISDIGIT(*s) && *s != '-')
673 *cp = *s++;
674 retval = atol((char *)s);
675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000676 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
678 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000679 if (use_sandbox)
680 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200681 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200682 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200684 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685}
686#endif
687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000689 * Get an lval: variable, Dict item or List item that can be assigned a value
690 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
691 * "name.key", "name.key[expr]" etc.
692 * Indexing only works if "name" is an existing List or Dictionary.
693 * "name" points to the start of the name.
694 * If "rettv" is not NULL it points to the value to be assigned.
695 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
696 * wrong; must end in space or cmd separator.
697 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100698 * flags:
699 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100700 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100701 * GLV_NO_AUTOLOAD: do not use script autoloading
702 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000703 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000704 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000705 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000706 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200707 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100708get_lval(
709 char_u *name,
710 typval_T *rettv,
711 lval_T *lp,
712 int unlet,
713 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100714 int flags, // GLV_ values
715 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000717 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000718 char_u *expr_start, *expr_end;
719 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000720 dictitem_T *v;
721 typval_T var1;
722 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000723 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000724 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000725 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000726 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000727 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100728 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100730 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200731 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000732
733 if (skip)
734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100735 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000736 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000737 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 }
739
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100740 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000741 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000743 if (expr_start != NULL)
744 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100745 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100746 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000747 && *p != '[' && *p != '.')
748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100749 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000750 return NULL;
751 }
752
753 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
754 if (lp->ll_exp_name == NULL)
755 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100756 // Report an invalid expression in braces, unless the
757 // expression evaluation has been cancelled due to an
758 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000759 if (!aborting() && !quiet)
760 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000761 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100762 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000763 return NULL;
764 }
765 }
766 lp->ll_name = lp->ll_exp_name;
767 }
768 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000770 lp->ll_name = name;
771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
773 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100774 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 char_u *tp = skipwhite(p + 1);
776
777 // parse the type after the name
778 lp->ll_type = parse_type(&tp, &si->sn_type_list);
779 lp->ll_name_end = tp;
780 }
781 }
782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100783 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
785 return p;
786
787 cc = *p;
788 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100789 // Only pass &ht when we would write to the variable, it prevents autoload
790 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100791 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
792 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000793 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100794 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000795 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000796 if (v == NULL)
797 return NULL;
798
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000799 /*
800 * Loop until no more [idx] or .key is following.
801 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000802 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100803 var1.v_type = VAR_UNKNOWN;
804 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000806 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
808 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100809 && lp->ll_tv->vval.v_dict != NULL)
810 && !(lp->ll_tv->v_type == VAR_BLOB
811 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000813 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100814 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000816 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000818 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000823
Bram Moolenaar8c711452005-01-14 21:53:12 +0000824 len = -1;
825 if (*p == '.')
826 {
827 key = p + 1;
828 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
829 ;
830 if (len == 0)
831 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100833 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000835 }
836 p = key + len;
837 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000838 else
839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100840 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000841 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000842 if (*p == ':')
843 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000844 else
845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000846 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200847 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100849 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100851 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000852 clear_tv(&var1);
853 return NULL;
854 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000855 }
856
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100857 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000858 if (*p == ':')
859 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100863 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100864 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000866 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100867 if (rettv != NULL
868 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100869 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100870 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100871 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100874 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100875 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000876 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000877 }
878 p = skipwhite(p + 1);
879 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000881 else
882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200884 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200885 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000886 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100887 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000889 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100890 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000891 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100892 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100893 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000894 clear_tv(&var2);
895 return NULL;
896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000897 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000899 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000900 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000902
Bram Moolenaar8c711452005-01-14 21:53:12 +0000903 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100906 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100907 clear_tv(&var1);
908 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000910 }
911
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100912 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000913 ++p;
914 }
915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000917 {
918 if (len == -1)
919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100920 // "[key]": get key from "var1"
921 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200922 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000924 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000925 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000926 }
927 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000928 lp->ll_list = NULL;
929 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000930 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100932 // When assigning to a scope dictionary check that a function and
933 // variable name is valid (only variable name unless it is l: or
934 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200935 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200936 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200937 int prevval;
938 int wrong;
939
940 if (len != -1)
941 {
942 prevval = key[len];
943 key[len] = NUL;
944 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200945 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100946 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200947 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
948 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200949 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200950 || !valid_varname(key);
951 if (len != -1)
952 key[len] = prevval;
953 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200954 {
955 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200956 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200957 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200958 }
959
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000960 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000961 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100962 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200963 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100964 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100966 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100967 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200968 return NULL;
969 }
970
Bram Moolenaar31b81602019-02-10 22:14:27 +0100971 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000974 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100975 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100976 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 }
979 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100983 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000985 p = NULL;
986 break;
987 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100988 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100989 else if ((flags & GLV_READ_ONLY) == 0
990 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100991 {
992 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200993 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100994 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200995
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100996 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100999 else if (lp->ll_tv->v_type == VAR_BLOB)
1000 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001001 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1002
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001003 /*
1004 * Get the number and item for the only or first index of the List.
1005 */
1006 if (empty1)
1007 lp->ll_n1 = 0;
1008 else
1009 // is number or string
1010 lp->ll_n1 = (long)tv_get_number(&var1);
1011 clear_tv(&var1);
1012
1013 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001014 || lp->ll_n1 > bloblen
1015 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001016 {
1017 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001018 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001019 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001020 return NULL;
1021 }
1022 if (lp->ll_range && !lp->ll_empty2)
1023 {
1024 lp->ll_n2 = (long)tv_get_number(&var2);
1025 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001026 if (lp->ll_n2 < 0
1027 || lp->ll_n2 >= bloblen
1028 || lp->ll_n2 < lp->ll_n1)
1029 {
1030 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001031 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001032 return NULL;
1033 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001034 }
1035 lp->ll_blob = lp->ll_tv->vval.v_blob;
1036 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001037 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001039 else
1040 {
1041 /*
1042 * Get the number and item for the only or first index of the List.
1043 */
1044 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001047 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001048 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001049 clear_tv(&var1);
1050
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001051 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 lp->ll_list = lp->ll_tv->vval.v_list;
1053 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1054 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001055 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001056 if (lp->ll_n1 < 0)
1057 {
1058 lp->ll_n1 = 0;
1059 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1060 }
1061 }
1062 if (lp->ll_li == NULL)
1063 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001064 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001065 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001066 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001067 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 }
1069
1070 /*
1071 * May need to find the item or absolute index for the second
1072 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 * When no index given: "lp->ll_empty2" is TRUE.
1074 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001075 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001076 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001077 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001078 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001079 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001080 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001081 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001082 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001083 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001084 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001085 {
1086 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001087 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001088 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001089 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001090 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001091 }
1092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001093 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 if (lp->ll_n1 < 0)
1095 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1096 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001097 {
1098 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001099 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001100 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001101 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001102 }
1103
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001104 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001106 }
1107
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001108 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 return p;
1111}
1112
1113/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001114 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001115 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001117clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118{
1119 vim_free(lp->ll_exp_name);
1120 vim_free(lp->ll_newkey);
1121}
1122
1123/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001124 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001126 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1127 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001130set_var_lval(
1131 lval_T *lp,
1132 char_u *endp,
1133 typval_T *rettv,
1134 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001136 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137{
1138 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001139 listitem_T *ri;
1140 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141
1142 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001144 cc = *endp;
1145 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001146 if (lp->ll_blob != NULL)
1147 {
1148 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001149
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001150 if (op != NULL && *op != '=')
1151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001152 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001153 return;
1154 }
1155
1156 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1157 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001158 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001159
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001160 if (lp->ll_empty2)
1161 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1162
1163 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001165 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 return;
1167 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001168 if (lp->ll_empty2)
1169 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001170
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001171 ir = 0;
1172 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1173 blob_set(lp->ll_blob, il,
1174 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001175 }
1176 else
1177 {
1178 val = (int)tv_get_number_chk(rettv, &error);
1179 if (!error)
1180 {
1181 garray_T *gap = &lp->ll_blob->bv_ga;
1182
1183 // Allow for appending a byte. Setting a byte beyond
1184 // the end is an error otherwise.
1185 if (lp->ll_n1 < gap->ga_len
1186 || (lp->ll_n1 == gap->ga_len
1187 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1188 {
1189 blob_set(lp->ll_blob, lp->ll_n1, val);
1190 if (lp->ll_n1 == gap->ga_len)
1191 ++gap->ga_len;
1192 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001193 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194 }
1195 }
1196 }
1197 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001199 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001202 {
1203 emsg(_(e_cannot_mod));
1204 *endp = cc;
1205 return;
1206 }
1207
Bram Moolenaarff697e62019-02-12 22:28:33 +01001208 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001209 di = NULL;
1210 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1211 &tv, &di, TRUE, FALSE) == OK)
1212 {
1213 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001214 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1215 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001216 && tv_op(&tv, rettv, op) == OK)
1217 set_var(lp->ll_name, &tv, FALSE);
1218 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001221 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001223 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001225 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001226 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001227 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001228 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 else if (lp->ll_range)
1230 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001231 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001232 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001235 {
1236 emsg(_("E996: Cannot lock a range"));
1237 return;
1238 }
1239
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001240 /*
1241 * Check whether any of the list items is locked
1242 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001243 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001244 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001245 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001246 return;
1247 ri = ri->li_next;
1248 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1249 break;
1250 ll_li = ll_li->li_next;
1251 ++ll_n1;
1252 }
1253
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 /*
1255 * Assign the List values to the list items.
1256 */
1257 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001258 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001259 if (op != NULL && *op != '=')
1260 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1261 else
1262 {
1263 clear_tv(&lp->ll_li->li_tv);
1264 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1265 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 ri = ri->li_next;
1267 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1268 break;
1269 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001270 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001271 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001272 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001273 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001274 ri = NULL;
1275 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001276 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001277 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001278 lp->ll_li = lp->ll_li->li_next;
1279 ++lp->ll_n1;
1280 }
1281 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001282 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283 else if (lp->ll_empty2
1284 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001286 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 }
1288 else
1289 {
1290 /*
1291 * Assign to a List or Dictionary item.
1292 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001294 {
1295 emsg(_("E996: Cannot lock a list or dict"));
1296 return;
1297 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 if (lp->ll_newkey != NULL)
1299 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001300 if (op != NULL && *op != '=')
1301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001302 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001303 return;
1304 }
1305
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001306 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001307 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308 if (di == NULL)
1309 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001310 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1311 {
1312 vim_free(di);
1313 return;
1314 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001316 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001317 else if (op != NULL && *op != '=')
1318 {
1319 tv_op(lp->ll_tv, rettv, op);
1320 return;
1321 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 /*
1326 * Assign the value to the variable or list item.
1327 */
1328 if (copy)
1329 copy_tv(rettv, lp->ll_tv);
1330 else
1331 {
1332 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001333 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 }
1336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337}
1338
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001339/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001340 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1341 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001342 * Returns OK or FAIL.
1343 */
1344 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001345tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001346{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001347 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001348 char_u numbuf[NUMBUFLEN];
1349 char_u *s;
1350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001351 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001352 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001353 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001354 {
1355 switch (tv1->v_type)
1356 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001357 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001358 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001360 case VAR_DICT:
1361 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001362 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001363 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001364 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001365 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001366 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001367 break;
1368
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001369 case VAR_BLOB:
1370 if (*op != '+' || tv2->v_type != VAR_BLOB)
1371 break;
1372 // BLOB += BLOB
1373 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1374 {
1375 blob_T *b1 = tv1->vval.v_blob;
1376 blob_T *b2 = tv2->vval.v_blob;
1377 int i, len = blob_len(b2);
1378 for (i = 0; i < len; i++)
1379 ga_append(&b1->bv_ga, blob_get(b2, i));
1380 }
1381 return OK;
1382
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001383 case VAR_LIST:
1384 if (*op != '+' || tv2->v_type != VAR_LIST)
1385 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001386 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001387 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1388 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1389 return OK;
1390
1391 case VAR_NUMBER:
1392 case VAR_STRING:
1393 if (tv2->v_type == VAR_LIST)
1394 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001395 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001396 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001397 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001398 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001399#ifdef FEAT_FLOAT
1400 if (tv2->v_type == VAR_FLOAT)
1401 {
1402 float_T f = n;
1403
Bram Moolenaarff697e62019-02-12 22:28:33 +01001404 if (*op == '%')
1405 break;
1406 switch (*op)
1407 {
1408 case '+': f += tv2->vval.v_float; break;
1409 case '-': f -= tv2->vval.v_float; break;
1410 case '*': f *= tv2->vval.v_float; break;
1411 case '/': f /= tv2->vval.v_float; break;
1412 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001413 clear_tv(tv1);
1414 tv1->v_type = VAR_FLOAT;
1415 tv1->vval.v_float = f;
1416 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001417 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001418#endif
1419 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001420 switch (*op)
1421 {
1422 case '+': n += tv_get_number(tv2); break;
1423 case '-': n -= tv_get_number(tv2); break;
1424 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001425 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1426 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001427 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001428 clear_tv(tv1);
1429 tv1->v_type = VAR_NUMBER;
1430 tv1->vval.v_number = n;
1431 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001432 }
1433 else
1434 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001435 if (tv2->v_type == VAR_FLOAT)
1436 break;
1437
Bram Moolenaarff697e62019-02-12 22:28:33 +01001438 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001439 s = tv_get_string(tv1);
1440 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001441 clear_tv(tv1);
1442 tv1->v_type = VAR_STRING;
1443 tv1->vval.v_string = s;
1444 }
1445 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001446
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001447 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001448#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001449 {
1450 float_T f;
1451
Bram Moolenaarff697e62019-02-12 22:28:33 +01001452 if (*op == '%' || *op == '.'
1453 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001454 && tv2->v_type != VAR_NUMBER
1455 && tv2->v_type != VAR_STRING))
1456 break;
1457 if (tv2->v_type == VAR_FLOAT)
1458 f = tv2->vval.v_float;
1459 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001460 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001461 switch (*op)
1462 {
1463 case '+': tv1->vval.v_float += f; break;
1464 case '-': tv1->vval.v_float -= f; break;
1465 case '*': tv1->vval.v_float *= f; break;
1466 case '/': tv1->vval.v_float /= f; break;
1467 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001468 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001469#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001470 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001471 }
1472 }
1473
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001474 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001475 return FAIL;
1476}
1477
1478/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001479 * Evaluate the expression used in a ":for var in expr" command.
1480 * "arg" points to "var".
1481 * Set "*errp" to TRUE for an error, FALSE otherwise;
1482 * Return a pointer that holds the info. Null when there is an error.
1483 */
1484 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001485eval_for_line(
1486 char_u *arg,
1487 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001488 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001489 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001490{
Bram Moolenaar33570922005-01-25 22:26:29 +00001491 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001493 typval_T tv;
1494 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001495 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001497 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001499 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500 if (fi == NULL)
1501 return NULL;
1502
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001503 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001504 if (expr == NULL)
1505 return fi;
1506
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001507 expr = skipwhite_and_linebreak(expr, evalarg);
1508 if (expr[0] != 'i' || expr[1] != 'n'
1509 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001510 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001512 return fi;
1513 }
1514
1515 if (skip)
1516 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001517 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1518 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001519 {
1520 *errp = FALSE;
1521 if (!skip)
1522 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001523 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001524 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001525 l = tv.vval.v_list;
1526 if (l == NULL)
1527 {
1528 // a null list is like an empty list: do nothing
1529 clear_tv(&tv);
1530 }
1531 else
1532 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001534 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001536 // No need to increment the refcount, it's already set for
1537 // the list being used in "tv".
1538 fi->fi_list = l;
1539 list_add_watch(l, &fi->fi_lw);
1540 fi->fi_lw.lw_item = l->lv_first;
1541 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001542 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001543 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001544 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001545 fi->fi_bi = 0;
1546 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001547 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001548 typval_T btv;
1549
1550 // Make a copy, so that the iteration still works when the
1551 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001553 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001554 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001555 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001556 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001557 else
1558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001559 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001560 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 }
1562 }
1563 }
1564 if (skip)
1565 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001566 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567
1568 return fi;
1569}
1570
1571/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001572 * Used when looping over a :for line, skip the "in expr" part.
1573 */
1574 void
1575skip_for_lines(void *fi_void, evalarg_T *evalarg)
1576{
1577 forinfo_T *fi = (forinfo_T *)fi_void;
1578 int i;
1579
1580 for (i = 0; i < fi->fi_break_count; ++i)
1581 eval_next_line(evalarg);
1582}
1583
1584/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001585 * Use the first item in a ":for" list. Advance to the next.
1586 * Assign the values to the variable (list). "arg" points to the first one.
1587 * Return TRUE when a valid item was found, FALSE when at end of list or
1588 * something wrong.
1589 */
1590 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001591next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001592{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001593 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001594 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001595 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1596 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001597 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001598
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001599 if (fi->fi_blob != NULL)
1600 {
1601 typval_T tv;
1602
1603 if (fi->fi_bi >= blob_len(fi->fi_blob))
1604 return FALSE;
1605 tv.v_type = VAR_NUMBER;
1606 tv.v_lock = VAR_FIXED;
1607 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1608 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001609 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001610 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001611 }
1612
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613 item = fi->fi_lw.lw_item;
1614 if (item == NULL)
1615 result = FALSE;
1616 else
1617 {
1618 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001619 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001620 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621 }
1622 return result;
1623}
1624
1625/*
1626 * Free the structure used to store info used by ":for".
1627 */
1628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001629free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630{
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001633 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001634 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001636 list_unref(fi->fi_list);
1637 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001638 if (fi != NULL && fi->fi_blob != NULL)
1639 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001640 vim_free(fi);
1641}
1642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001644set_context_for_expression(
1645 expand_T *xp,
1646 char_u *arg,
1647 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
1649 int got_eq = FALSE;
1650 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001653 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 {
1655 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001656 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001658 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001659 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 {
1661 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001662 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001663 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 break;
1665 }
1666 return;
1667 }
1668 }
1669 else
1670 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1671 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 while ((xp->xp_pattern = vim_strpbrk(arg,
1673 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1674 {
1675 c = *xp->xp_pattern;
1676 if (c == '&')
1677 {
1678 c = xp->xp_pattern[1];
1679 if (c == '&')
1680 {
1681 ++xp->xp_pattern;
1682 xp->xp_context = cmdidx != CMD_let || got_eq
1683 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1684 }
1685 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001686 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001688 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1689 xp->xp_pattern += 2;
1690
1691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
1693 else if (c == '$')
1694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001695 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 xp->xp_context = EXPAND_ENV_VARS;
1697 }
1698 else if (c == '=')
1699 {
1700 got_eq = TRUE;
1701 xp->xp_context = EXPAND_EXPRESSION;
1702 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001703 else if (c == '#'
1704 && xp->xp_context == EXPAND_EXPRESSION)
1705 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001706 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001707 break;
1708 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001709 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 && xp->xp_context == EXPAND_FUNCTIONS
1711 && vim_strchr(xp->xp_pattern, '(') == NULL)
1712 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001713 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 break;
1715 }
1716 else if (cmdidx != CMD_let || got_eq)
1717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001718 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
1720 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1721 if (c == '\\' && xp->xp_pattern[1] != NUL)
1722 ++xp->xp_pattern;
1723 xp->xp_context = EXPAND_NOTHING;
1724 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001725 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001727 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1729 /* skip */ ;
1730 xp->xp_context = EXPAND_NOTHING;
1731 }
1732 else if (c == '|')
1733 {
1734 if (xp->xp_pattern[1] == '|')
1735 {
1736 ++xp->xp_pattern;
1737 xp->xp_context = EXPAND_EXPRESSION;
1738 }
1739 else
1740 xp->xp_context = EXPAND_COMMANDS;
1741 }
1742 else
1743 xp->xp_context = EXPAND_EXPRESSION;
1744 }
1745 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001746 // Doesn't look like something valid, expand as an expression
1747 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 arg = xp->xp_pattern;
1750 if (*arg != NUL)
1751 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1752 /* skip */ ;
1753 }
1754 xp->xp_pattern = arg;
1755}
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001758 * Return TRUE if "pat" matches "text".
1759 * Does not use 'cpo' and always uses 'magic'.
1760 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001761 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001762pattern_match(char_u *pat, char_u *text, int ic)
1763{
1764 int matches = FALSE;
1765 char_u *save_cpo;
1766 regmatch_T regmatch;
1767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001768 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001769 save_cpo = p_cpo;
1770 p_cpo = (char_u *)"";
1771 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1772 if (regmatch.regprog != NULL)
1773 {
1774 regmatch.rm_ic = ic;
1775 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1776 vim_regfree(regmatch.regprog);
1777 }
1778 p_cpo = save_cpo;
1779 return matches;
1780}
1781
1782/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001783 * Handle a name followed by "(". Both for just "name(arg)" and for
1784 * "expr->name(arg)".
1785 * Returns OK or FAIL.
1786 */
1787 static int
1788eval_func(
1789 char_u **arg, // points to "(", will be advanced
1790 char_u *name,
1791 int name_len,
1792 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001793 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001794 typval_T *basetv) // "expr" for "expr->name(arg)"
1795{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001796 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001797 char_u *s = name;
1798 int len = name_len;
1799 partial_T *partial;
1800 int ret = OK;
1801
1802 if (!evaluate)
1803 check_vars(s, len);
1804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001805 // If "s" is the name of a variable of type VAR_FUNC
1806 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001807 s = deref_func_name(s, &len, &partial, !evaluate);
1808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001809 // Need to make a copy, in case evaluating the arguments makes
1810 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001811 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001812 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001813 ret = FAIL;
1814 else
1815 {
1816 funcexe_T funcexe;
1817
1818 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001819 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001820 funcexe.firstline = curwin->w_cursor.lnum;
1821 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001822 funcexe.evaluate = evaluate;
1823 funcexe.partial = partial;
1824 funcexe.basetv = basetv;
1825 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1826 }
1827 vim_free(s);
1828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001829 // If evaluate is FALSE rettv->v_type was not set in
1830 // get_func_tv, but it's needed in handle_subscript() to parse
1831 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001832 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1833 {
1834 rettv->vval.v_string = NULL;
1835 rettv->v_type = VAR_FUNC;
1836 }
1837
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001838 // Stop the expression evaluation when immediately
1839 // aborting on error, or when an interrupt occurred or
1840 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001841 if (evaluate && aborting())
1842 {
1843 if (ret == OK)
1844 clear_tv(rettv);
1845 ret = FAIL;
1846 }
1847 return ret;
1848}
1849
1850/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001851 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1852 * and there is a next line, return the next line (skipping blanks) and set
1853 * "getnext".
1854 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1855 * "arg" must point somewhere inside a line, not at the start.
1856 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001857 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001858eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1859{
1860 *getnext = FALSE;
1861 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1862 && evalarg != NULL
1863 && evalarg->eval_cookie != NULL
1864 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaard5053d02020-06-28 15:51:16 +02001865 && (*arg == '"' || *arg == '#'))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001866 {
Bram Moolenaard5053d02020-06-28 15:51:16 +02001867 char_u *p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001868
1869 if (p != NULL)
1870 {
1871 *getnext = TRUE;
1872 return skipwhite(p);
1873 }
1874 }
1875 return arg;
1876}
1877
1878/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001879 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001880 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001881 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001882eval_next_line(evalarg_T *evalarg)
1883{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001884 garray_T *gap = &evalarg->eval_ga;
1885 char_u *line;
1886
Bram Moolenaard5053d02020-06-28 15:51:16 +02001887 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001888 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001889 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1890 {
1891 // Going to concatenate the lines after parsing.
1892 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1893 ++gap->ga_len;
1894 }
1895 else
1896 {
1897 vim_free(evalarg->eval_tofree);
1898 evalarg->eval_tofree = line;
1899 }
1900 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001901}
1902
Bram Moolenaar9215f012020-06-27 21:18:00 +02001903 char_u *
1904skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1905{
1906 int getnext;
1907 char_u *p = skipwhite(arg);
1908
1909 eval_next_non_blank(p, evalarg, &getnext);
1910 if (getnext)
1911 return eval_next_line(evalarg);
1912 return p;
1913}
1914
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001915/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001916 * After using "evalarg" filled from "eap" free the memory.
1917 */
1918 void
1919clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1920{
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001921 if (evalarg != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001922 {
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001923 if (eap != NULL)
1924 {
1925 // We may need to keep the original command line, e.g. for
1926 // ":let" it has the variable names. But we may also need the
1927 // new one, "nextcmd" points into it. Keep both.
1928 vim_free(eap->cmdline_tofree);
1929 eap->cmdline_tofree = *eap->cmdlinep;
1930 *eap->cmdlinep = evalarg->eval_tofree;
1931 }
1932 else
1933 vim_free(evalarg->eval_tofree);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001934 evalarg->eval_tofree = NULL;
1935 }
1936}
1937
1938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1942 */
1943
1944/*
1945 * Handle zero level expression.
1946 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001948 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001949 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 * Return OK or FAIL.
1951 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001953eval0(
1954 char_u *arg,
1955 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001956 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001957 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 int ret;
1960 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001961 int did_emsg_before = did_emsg;
1962 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001963 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
1965 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001966 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001967
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001968 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
1970 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 /*
1973 * Report the invalid expression unless the expression evaluation has
1974 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001975 * exception, or we already gave a more specific error.
1976 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001978 if (!aborting()
1979 && did_emsg == did_emsg_before
1980 && called_emsg == called_emsg_before
1981 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 ret = FAIL;
1984 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001985
1986 if (eap != NULL)
1987 eap->nextcmd = check_nextcmd(p);
1988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 return ret;
1990}
1991
1992/*
1993 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001994 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 *
1996 * "arg" must point to the first non-white of the expression.
1997 * "arg" is advanced to the next non-white after the recognized expression.
1998 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001999 * Note: "rettv.v_lock" is not set.
2000 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 * Return OK or FAIL.
2002 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002003 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002004eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002006 char_u *p;
2007 int getnext;
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 /*
2010 * Get the first variable.
2011 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002012 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 return FAIL;
2014
Bram Moolenaar793648f2020-06-26 21:28:25 +02002015 p = eval_next_non_blank(*arg, evalarg, &getnext);
2016 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002018 int result;
2019 typval_T var2;
2020 evalarg_T nested_evalarg;
2021 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002022 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002023
Bram Moolenaar793648f2020-06-26 21:28:25 +02002024 if (getnext)
2025 *arg = eval_next_line(evalarg);
2026
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002027 if (evalarg == NULL)
2028 {
2029 CLEAR_FIELD(nested_evalarg);
2030 orig_flags = 0;
2031 }
2032 else
2033 {
2034 nested_evalarg = *evalarg;
2035 orig_flags = evalarg->eval_flags;
2036 }
2037
Bram Moolenaar7acde512020-06-24 23:02:40 +02002038 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002040 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002042 int error = FALSE;
2043
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002044 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002047 if (error)
2048 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050
2051 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002052 * Get the second 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, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 return FAIL;
2059
2060 /*
2061 * Check for the ":".
2062 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002063 p = eval_next_non_blank(*arg, evalarg, &getnext);
2064 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 return FAIL;
2070 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002071 if (getnext)
2072 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002075 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002077 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002078 nested_evalarg.eval_flags = !result ? orig_flags
2079 : orig_flags & ~EVAL_EVALUATE;
2080 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002083 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 return FAIL;
2085 }
2086 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002087 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089
2090 return OK;
2091}
2092
2093/*
2094 * Handle first level expression:
2095 * expr2 || expr2 || expr2 logical OR
2096 *
2097 * "arg" must point to the first non-white of the expression.
2098 * "arg" is advanced to the next non-white after the recognized expression.
2099 *
2100 * Return OK or FAIL.
2101 */
2102 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002103eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002105 char_u *p;
2106 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 long result;
2109 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002110 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
2112 /*
2113 * Get the first variable.
2114 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002115 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 return FAIL;
2117
2118 /*
2119 * Repeat until there is no following "||".
2120 */
2121 first = TRUE;
2122 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002123 p = eval_next_non_blank(*arg, evalarg, &getnext);
2124 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126 evalarg_T nested_evalarg;
2127 int evaluate;
2128 int orig_flags;
2129
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002130 if (getnext)
2131 *arg = eval_next_line(evalarg);
2132
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133 if (evalarg == NULL)
2134 {
2135 CLEAR_FIELD(nested_evalarg);
2136 orig_flags = 0;
2137 evaluate = FALSE;
2138 }
2139 else
2140 {
2141 nested_evalarg = *evalarg;
2142 orig_flags = evalarg->eval_flags;
2143 evaluate = orig_flags & EVAL_EVALUATE;
2144 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (evaluate && first)
2147 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002148 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002151 if (error)
2152 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 first = FALSE;
2154 }
2155
2156 /*
2157 * Get the second variable.
2158 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002159 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002160 nested_evalarg.eval_flags = !result ? orig_flags
2161 : orig_flags & ~EVAL_EVALUATE;
2162 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 return FAIL;
2164
2165 /*
2166 * Compute the result.
2167 */
2168 if (evaluate && !result)
2169 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002170 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002173 if (error)
2174 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176 if (evaluate)
2177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 rettv->v_type = VAR_NUMBER;
2179 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002181
2182 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 }
2184
2185 return OK;
2186}
2187
2188/*
2189 * Handle second level expression:
2190 * expr3 && expr3 && expr3 logical AND
2191 *
2192 * "arg" must point to the first non-white of the expression.
2193 * "arg" is advanced to the next non-white after the recognized expression.
2194 *
2195 * Return OK or FAIL.
2196 */
2197 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002198eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002200 char_u *p;
2201 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002202 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 long result;
2204 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002205 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
2207 /*
2208 * Get the first variable.
2209 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002210 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 return FAIL;
2212
2213 /*
2214 * Repeat until there is no following "&&".
2215 */
2216 first = TRUE;
2217 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002218 p = eval_next_non_blank(*arg, evalarg, &getnext);
2219 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002221 evalarg_T nested_evalarg;
2222 int orig_flags;
2223 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002224
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002225 if (getnext)
2226 *arg = eval_next_line(evalarg);
2227
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002228 if (evalarg == NULL)
2229 {
2230 CLEAR_FIELD(nested_evalarg);
2231 orig_flags = 0;
2232 evaluate = FALSE;
2233 }
2234 else
2235 {
2236 nested_evalarg = *evalarg;
2237 orig_flags = evalarg->eval_flags;
2238 evaluate = orig_flags & EVAL_EVALUATE;
2239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (evaluate && first)
2241 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002242 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002245 if (error)
2246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 first = FALSE;
2248 }
2249
2250 /*
2251 * Get the second variable.
2252 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002253 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002254 nested_evalarg.eval_flags = result ? orig_flags
2255 : orig_flags & ~EVAL_EVALUATE;
2256 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 return FAIL;
2258
2259 /*
2260 * Compute the result.
2261 */
2262 if (evaluate && result)
2263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002264 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002267 if (error)
2268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
2270 if (evaluate)
2271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 rettv->v_type = VAR_NUMBER;
2273 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002275
2276 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
2278
2279 return OK;
2280}
2281
2282/*
2283 * Handle third level expression:
2284 * var1 == var2
2285 * var1 =~ var2
2286 * var1 != var2
2287 * var1 !~ var2
2288 * var1 > var2
2289 * var1 >= var2
2290 * var1 < var2
2291 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002292 * var1 is var2
2293 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 *
2295 * "arg" must point to the first non-white of the expression.
2296 * "arg" is advanced to the next non-white after the recognized expression.
2297 *
2298 * Return OK or FAIL.
2299 */
2300 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
Bram Moolenaar33570922005-01-25 22:26:29 +00002303 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002305 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002307 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
2311 /*
2312 * Get the first variable.
2313 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002314 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 return FAIL;
2316
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002317 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 switch (p[0])
2319 {
2320 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002321 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002323 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 break;
2325 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002326 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002328 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 break;
2330 case '>': if (p[1] != '=')
2331 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002332 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 len = 1;
2334 }
2335 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002336 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 break;
2338 case '<': if (p[1] != '=')
2339 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002340 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 len = 1;
2342 }
2343 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002344 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002346 case 'i': if (p[1] == 's')
2347 {
2348 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2349 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002350 i = p[len];
2351 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002352 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002353 }
2354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356
2357 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002358 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002360 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002362 if (getnext)
2363 *arg = eval_next_line(evalarg);
2364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002365 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (p[len] == '?')
2367 {
2368 ic = TRUE;
2369 ++len;
2370 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002371 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 else if (p[len] == '#')
2373 {
2374 ic = FALSE;
2375 ++len;
2376 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002377 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 else
2379 ic = p_ic;
2380
2381 /*
2382 * Get the second variable.
2383 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002384 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002385 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 return FAIL;
2389 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002390 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002391 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002392 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002393
2394 clear_tv(&var2);
2395 return ret;
2396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398
2399 return OK;
2400}
2401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 void
2403eval_addblob(typval_T *tv1, typval_T *tv2)
2404{
2405 blob_T *b1 = tv1->vval.v_blob;
2406 blob_T *b2 = tv2->vval.v_blob;
2407 blob_T *b = blob_alloc();
2408 int i;
2409
2410 if (b != NULL)
2411 {
2412 for (i = 0; i < blob_len(b1); i++)
2413 ga_append(&b->bv_ga, blob_get(b1, i));
2414 for (i = 0; i < blob_len(b2); i++)
2415 ga_append(&b->bv_ga, blob_get(b2, i));
2416
2417 clear_tv(tv1);
2418 rettv_blob_set(tv1, b);
2419 }
2420}
2421
2422 int
2423eval_addlist(typval_T *tv1, typval_T *tv2)
2424{
2425 typval_T var3;
2426
2427 // concatenate Lists
2428 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2429 {
2430 clear_tv(tv1);
2431 clear_tv(tv2);
2432 return FAIL;
2433 }
2434 clear_tv(tv1);
2435 *tv1 = var3;
2436 return OK;
2437}
2438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439/*
2440 * Handle fourth level expression:
2441 * + number addition
2442 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002443 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002444 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 *
2446 * "arg" must point to the first non-white of the expression.
2447 * "arg" is advanced to the next non-white after the recognized expression.
2448 *
2449 * Return OK or FAIL.
2450 */
2451 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002452eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002454 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
2456 /*
2457 * Get the first variable.
2458 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002459 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 return FAIL;
2461
2462 /*
2463 * Repeat computing, until no '+', '-' or '.' is following.
2464 */
2465 for (;;)
2466 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002467 int getnext;
2468 char_u *p;
2469 int op;
2470 int concat;
2471 typval_T var2;
2472
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002473 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002474 p = eval_next_non_blank(*arg, evalarg, &getnext);
2475 op = *p;
2476 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002477 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002479 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002480 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002482 if ((op != '+' || (rettv->v_type != VAR_LIST
2483 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002484#ifdef FEAT_FLOAT
2485 && (op == '.' || rettv->v_type != VAR_FLOAT)
2486#endif
2487 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002488 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002489 // For "list + ...", an illegal use of the first operand as
2490 // a number cannot be determined before evaluating the 2nd
2491 // operand: if this is also a list, all is ok.
2492 // For "something . ...", "something - ..." or "non-list + ...",
2493 // we know that the first operand needs to be a string or number
2494 // without evaluating the 2nd operand. So check before to avoid
2495 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002496 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 {
2498 clear_tv(rettv);
2499 return FAIL;
2500 }
2501 }
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 /*
2504 * Get the second variable.
2505 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002506 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2507 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002508 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002509 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 return FAIL;
2513 }
2514
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002515 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 /*
2518 * Compute the result.
2519 */
2520 if (op == '.')
2521 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002522 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2523 char_u *s1 = tv_get_string_buf(rettv, buf1);
2524 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002526 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002527 {
2528 clear_tv(rettv);
2529 clear_tv(&var2);
2530 return FAIL;
2531 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 clear_tv(rettv);
2534 rettv->v_type = VAR_STRING;
2535 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002537 else if (op == '+' && rettv->v_type == VAR_BLOB
2538 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002540 else if (op == '+' && rettv->v_type == VAR_LIST
2541 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002542 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002544 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 else
2547 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002548 int error = FALSE;
2549 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002550#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551 float_T f1 = 0, f2 = 0;
2552
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002553 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002554 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002555 f1 = rettv->vval.v_float;
2556 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002557 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002558 else
2559#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002560 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002561 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002562 if (error)
2563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002564 // This can only happen for "list + non-list". For
2565 // "non-list + ..." or "something - ...", we returned
2566 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002567 clear_tv(rettv);
2568 return FAIL;
2569 }
2570#ifdef FEAT_FLOAT
2571 if (var2.v_type == VAR_FLOAT)
2572 f1 = n1;
2573#endif
2574 }
2575#ifdef FEAT_FLOAT
2576 if (var2.v_type == VAR_FLOAT)
2577 {
2578 f2 = var2.vval.v_float;
2579 n2 = 0;
2580 }
2581 else
2582#endif
2583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002584 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002585 if (error)
2586 {
2587 clear_tv(rettv);
2588 clear_tv(&var2);
2589 return FAIL;
2590 }
2591#ifdef FEAT_FLOAT
2592 if (rettv->v_type == VAR_FLOAT)
2593 f2 = n2;
2594#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002595 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002597
2598#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002599 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002600 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2601 {
2602 if (op == '+')
2603 f1 = f1 + f2;
2604 else
2605 f1 = f1 - f2;
2606 rettv->v_type = VAR_FLOAT;
2607 rettv->vval.v_float = f1;
2608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002610#endif
2611 {
2612 if (op == '+')
2613 n1 = n1 + n2;
2614 else
2615 n1 = n1 - n2;
2616 rettv->v_type = VAR_NUMBER;
2617 rettv->vval.v_number = n1;
2618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
2622 }
2623 return OK;
2624}
2625
2626/*
2627 * Handle fifth level expression:
2628 * * number multiplication
2629 * / number division
2630 * % number modulo
2631 *
2632 * "arg" must point to the first non-white of the expression.
2633 * "arg" is advanced to the next non-white after the recognized expression.
2634 *
2635 * Return OK or FAIL.
2636 */
2637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002638eval6(
2639 char_u **arg,
2640 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002641 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002642 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaar33570922005-01-25 22:26:29 +00002644 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002646 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002647#ifdef FEAT_FLOAT
2648 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002649 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002650#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002651 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 /*
2654 * Get the first variable.
2655 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002656 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 return FAIL;
2658
2659 /*
2660 * Repeat computing, until no '*', '/' or '%' is following.
2661 */
2662 for (;;)
2663 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002664 int evaluate = evalarg == NULL ? 0
2665 : (evalarg->eval_flags & EVAL_EVALUATE);
2666 int getnext;
2667
2668 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002669 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002671 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002672 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002674 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002676#ifdef FEAT_FLOAT
2677 if (rettv->v_type == VAR_FLOAT)
2678 {
2679 f1 = rettv->vval.v_float;
2680 use_float = TRUE;
2681 n1 = 0;
2682 }
2683 else
2684#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002685 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 if (error)
2688 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690 else
2691 n1 = 0;
2692
2693 /*
2694 * Get the second variable.
2695 */
2696 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 return FAIL;
2699
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002700 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002702#ifdef FEAT_FLOAT
2703 if (var2.v_type == VAR_FLOAT)
2704 {
2705 if (!use_float)
2706 {
2707 f1 = n1;
2708 use_float = TRUE;
2709 }
2710 f2 = var2.vval.v_float;
2711 n2 = 0;
2712 }
2713 else
2714#endif
2715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002716 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002717 clear_tv(&var2);
2718 if (error)
2719 return FAIL;
2720#ifdef FEAT_FLOAT
2721 if (use_float)
2722 f2 = n2;
2723#endif
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 /*
2727 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002728 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002730#ifdef FEAT_FLOAT
2731 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002733 if (op == '*')
2734 f1 = f1 * f2;
2735 else if (op == '/')
2736 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002737# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002738 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002739 if (f2 == 0.0)
2740 {
2741 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002742 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002743 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002744 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002745 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002746 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002747 }
2748 else
2749 f1 = f1 / f2;
2750# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002751 // We rely on the floating point library to handle divide
2752 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002753 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002754# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002757 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002759 return FAIL;
2760 }
2761 rettv->v_type = VAR_FLOAT;
2762 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002767 if (op == '*')
2768 n1 = n1 * n2;
2769 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002770 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002772 n1 = num_modulus(n1, n2);
2773
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002774 rettv->v_type = VAR_NUMBER;
2775 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 }
2778 }
2779
2780 return OK;
2781}
2782
2783/*
2784 * Handle sixth level expression:
2785 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002786 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002787 * "string" string constant
2788 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 * &option-name option value
2790 * @r register contents
2791 * identifier variable value
2792 * function() function call
2793 * $VAR environment variable
2794 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002795 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002797 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002798 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 *
2800 * Also handle:
2801 * ! in front logical NOT
2802 * - in front unary minus
2803 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 * trailing [] subscript in String or List
2805 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002806 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 *
2808 * "arg" must point to the first non-white of the expression.
2809 * "arg" is advanced to the next non-white after the recognized expression.
2810 *
2811 * Return OK or FAIL.
2812 */
2813 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002814eval7(
2815 char_u **arg,
2816 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002817 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002820 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2821 int evaluate = evalarg != NULL
2822 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 int len;
2824 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 char_u *start_leader, *end_leader;
2826 int ret = OK;
2827 char_u *alias;
2828
2829 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002830 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002831 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002833 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002836 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 */
2838 start_leader = *arg;
2839 while (**arg == '!' || **arg == '-' || **arg == '+')
2840 *arg = skipwhite(*arg + 1);
2841 end_leader = *arg;
2842
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002843 if (**arg == '.' && (!isdigit(*(*arg + 1))
2844#ifdef FEAT_FLOAT
2845 || current_sctx.sc_version < 2
2846#endif
2847 ))
2848 {
2849 semsg(_(e_invexpr2), *arg);
2850 ++*arg;
2851 return FAIL;
2852 }
2853
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 switch (**arg)
2855 {
2856 /*
2857 * Number constant.
2858 */
2859 case '0':
2860 case '1':
2861 case '2':
2862 case '3':
2863 case '4':
2864 case '5':
2865 case '6':
2866 case '7':
2867 case '8':
2868 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002870
2871 // Apply prefixed "-" and "+" now. Matters especially when
2872 // "->" follows.
2873 if (ret == OK && evaluate && end_leader > start_leader)
2874 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 break;
2876
2877 /*
2878 * String constant: "string".
2879 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002880 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 break;
2882
2883 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002886 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002887 break;
2888
2889 /*
2890 * List: [expr, expr]
2891 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002892 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 break;
2894
2895 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002896 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002897 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002898 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002899 {
2900 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002901 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002902 }
2903 else
2904 ret = NOTDONE;
2905 break;
2906
2907 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002908 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002909 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002910 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002911 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002912 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002913 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002914 break;
2915
2916 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002917 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002919 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 break;
2921
2922 /*
2923 * Environment variable: $VAR.
2924 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002925 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 break;
2927
2928 /*
2929 * Register contents: @r.
2930 */
2931 case '@': ++*arg;
2932 if (evaluate)
2933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002934 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002935 rettv->vval.v_string = get_reg_contents(**arg,
2936 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938 if (**arg != NUL)
2939 ++*arg;
2940 break;
2941
2942 /*
2943 * nested expression: (expression).
2944 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002945 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002946 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002947 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002948
Bram Moolenaar9215f012020-06-27 21:18:00 +02002949 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002950 if (**arg == ')')
2951 ++*arg;
2952 else if (ret == OK)
2953 {
2954 emsg(_(e_missing_close));
2955 clear_tv(rettv);
2956 ret = FAIL;
2957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959 break;
2960
Bram Moolenaar8c711452005-01-14 21:53:12 +00002961 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 break;
2963 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002964
2965 if (ret == NOTDONE)
2966 {
2967 /*
2968 * Must be a variable or function name.
2969 * Can also be a curly-braces kind of name: {expr}.
2970 */
2971 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002972 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002973 if (alias != NULL)
2974 s = alias;
2975
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002976 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002977 ret = FAIL;
2978 else
2979 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002980 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002981 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002982 else if (flags & EVAL_CONSTANT)
2983 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002984 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002985 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002987 {
2988 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002990 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002991 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002992 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002993 }
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 *arg = skipwhite(*arg);
2996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002997 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2998 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002999 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003000 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 /*
3003 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3004 */
3005 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003006 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003007 return ret;
3008}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003009
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003010/*
3011 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003012 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003013 * Adjusts "end_leaderp" until it is at "start_leader".
3014 */
3015 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003016eval7_leader(
3017 typval_T *rettv,
3018 int numeric_only,
3019 char_u *start_leader,
3020 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003021{
3022 char_u *end_leader = *end_leaderp;
3023 int ret = OK;
3024 int error = FALSE;
3025 varnumber_T val = 0;
3026#ifdef FEAT_FLOAT
3027 float_T f = 0.0;
3028
3029 if (rettv->v_type == VAR_FLOAT)
3030 f = rettv->vval.v_float;
3031 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003032#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003033 val = tv_get_number_chk(rettv, &error);
3034 if (error)
3035 {
3036 clear_tv(rettv);
3037 ret = FAIL;
3038 }
3039 else
3040 {
3041 while (end_leader > start_leader)
3042 {
3043 --end_leader;
3044 if (*end_leader == '!')
3045 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003046 if (numeric_only)
3047 {
3048 ++end_leader;
3049 break;
3050 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003051#ifdef FEAT_FLOAT
3052 if (rettv->v_type == VAR_FLOAT)
3053 f = !f;
3054 else
3055#endif
3056 val = !val;
3057 }
3058 else if (*end_leader == '-')
3059 {
3060#ifdef FEAT_FLOAT
3061 if (rettv->v_type == VAR_FLOAT)
3062 f = -f;
3063 else
3064#endif
3065 val = -val;
3066 }
3067 }
3068#ifdef FEAT_FLOAT
3069 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003071 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003072 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003074 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003075#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003076 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003077 clear_tv(rettv);
3078 rettv->v_type = VAR_NUMBER;
3079 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003082 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 return ret;
3084}
3085
3086/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003087 * Call the function referred to in "rettv".
3088 */
3089 static int
3090call_func_rettv(
3091 char_u **arg,
3092 typval_T *rettv,
3093 int evaluate,
3094 dict_T *selfdict,
3095 typval_T *basetv)
3096{
3097 partial_T *pt = NULL;
3098 funcexe_T funcexe;
3099 typval_T functv;
3100 char_u *s;
3101 int ret;
3102
3103 // need to copy the funcref so that we can clear rettv
3104 if (evaluate)
3105 {
3106 functv = *rettv;
3107 rettv->v_type = VAR_UNKNOWN;
3108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003109 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003110 if (functv.v_type == VAR_PARTIAL)
3111 {
3112 pt = functv.vval.v_partial;
3113 s = partial_name(pt);
3114 }
3115 else
3116 s = functv.vval.v_string;
3117 }
3118 else
3119 s = (char_u *)"";
3120
Bram Moolenaara80faa82020-04-12 19:37:17 +02003121 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003122 funcexe.firstline = curwin->w_cursor.lnum;
3123 funcexe.lastline = curwin->w_cursor.lnum;
3124 funcexe.evaluate = evaluate;
3125 funcexe.partial = pt;
3126 funcexe.selfdict = selfdict;
3127 funcexe.basetv = basetv;
3128 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3129
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003130 // Clear the funcref afterwards, so that deleting it while
3131 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003132 if (evaluate)
3133 clear_tv(&functv);
3134
3135 return ret;
3136}
3137
3138/*
3139 * Evaluate "->method()".
3140 * "*arg" points to the '-'.
3141 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3142 */
3143 static int
3144eval_lambda(
3145 char_u **arg,
3146 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003147 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003148 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003149{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003150 int evaluate = evalarg != NULL
3151 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003152 typval_T base = *rettv;
3153 int ret;
3154
3155 // Skip over the ->.
3156 *arg += 2;
3157 rettv->v_type = VAR_UNKNOWN;
3158
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003159 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003160 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003161 return FAIL;
3162 else if (**arg != '(')
3163 {
3164 if (verbose)
3165 {
3166 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003167 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003168 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003170 }
3171 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003172 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003173 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003174 else
3175 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3176
3177 // Clear the funcref afterwards, so that deleting it while
3178 // evaluating the arguments is possible (see test55).
3179 if (evaluate)
3180 clear_tv(&base);
3181
3182 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003183}
3184
3185/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003186 * Evaluate "->method()".
3187 * "*arg" points to the '-'.
3188 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3189 */
3190 static int
3191eval_method(
3192 char_u **arg,
3193 typval_T *rettv,
3194 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003195 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003196{
3197 char_u *name;
3198 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003199 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003200 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003201 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003202
3203 // Skip over the ->.
3204 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003205 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003206
Bram Moolenaarac92e252019-08-03 21:58:38 +02003207 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003208 len = get_name_len(arg, &alias, evaluate, TRUE);
3209 if (alias != NULL)
3210 name = alias;
3211
3212 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003213 {
3214 if (verbose)
3215 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003216 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003217 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003218 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003219 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003220 if (**arg != '(')
3221 {
3222 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003224 ret = FAIL;
3225 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003226 else if (VIM_ISWHITE((*arg)[-1]))
3227 {
3228 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003229 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003230 ret = FAIL;
3231 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003232 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003233 ret = eval_func(arg, name, len, rettv,
3234 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003235 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003236
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003237 // Clear the funcref afterwards, so that deleting it while
3238 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003239 if (evaluate)
3240 clear_tv(&base);
3241
Bram Moolenaarac92e252019-08-03 21:58:38 +02003242 return ret;
3243}
3244
3245/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003246 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3247 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003248 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3249 */
3250 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003251eval_index(
3252 char_u **arg,
3253 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003254 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003255 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003256{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003257 int evaluate = evalarg != NULL
3258 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003259 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003260 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003261 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003262 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263 long len = -1;
3264 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003265 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003266 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003267
Bram Moolenaara03f2332016-02-06 18:09:59 +01003268 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003269 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003270 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003271 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003272 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003273 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003274 return FAIL;
3275 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003276#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003277 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003278 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003279 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003280#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003281 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003282 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003283 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003284 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003285 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003286 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003287 return FAIL;
3288 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003289 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003291 if (evaluate)
3292 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003293 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003294
3295 case VAR_STRING:
3296 case VAR_NUMBER:
3297 case VAR_LIST:
3298 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003299 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003300 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003301 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003302
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003303 init_tv(&var1);
3304 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003305 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003306 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003307 /*
3308 * dict.name
3309 */
3310 key = *arg + 1;
3311 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3312 ;
3313 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003314 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003315 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003316 }
3317 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003318 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003319 /*
3320 * something[idx]
3321 *
3322 * Get the (first) variable from inside the [].
3323 */
3324 *arg = skipwhite(*arg + 1);
3325 if (**arg == ':')
3326 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003327 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003328 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003329 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003330 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003331 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 clear_tv(&var1);
3333 return FAIL;
3334 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003335
3336 /*
3337 * Get the second variable from inside the [:].
3338 */
3339 if (**arg == ':')
3340 {
3341 range = TRUE;
3342 *arg = skipwhite(*arg + 1);
3343 if (**arg == ']')
3344 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003345 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003347 if (!empty1)
3348 clear_tv(&var1);
3349 return FAIL;
3350 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003351 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003352 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003353 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003354 if (!empty1)
3355 clear_tv(&var1);
3356 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003357 return FAIL;
3358 }
3359 }
3360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003361 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003362 if (**arg != ']')
3363 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003364 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003365 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003366 clear_tv(&var1);
3367 if (range)
3368 clear_tv(&var2);
3369 return FAIL;
3370 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003371 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003372 }
3373
3374 if (evaluate)
3375 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003376 n1 = 0;
3377 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003378 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003379 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003380 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003381 }
3382 if (range)
3383 {
3384 if (empty2)
3385 n2 = -1;
3386 else
3387 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003388 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003389 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003390 }
3391 }
3392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003394 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003395 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003396 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003398 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003399 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003400 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003401 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003402 case VAR_SPECIAL:
3403 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003404 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003405 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003406
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003407 case VAR_NUMBER:
3408 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003409 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003410 len = (long)STRLEN(s);
3411 if (range)
3412 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003413 // The resulting variable is a substring. If the indexes
3414 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003415 if (n1 < 0)
3416 {
3417 n1 = len + n1;
3418 if (n1 < 0)
3419 n1 = 0;
3420 }
3421 if (n2 < 0)
3422 n2 = len + n2;
3423 else if (n2 >= len)
3424 n2 = len;
3425 if (n1 >= len || n2 < 0 || n1 > n2)
3426 s = NULL;
3427 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003428 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003429 }
3430 else
3431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003432 // The resulting variable is a string of a single
3433 // character. If the index is too big or negative the
3434 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003435 if (n1 >= len || n1 < 0)
3436 s = NULL;
3437 else
3438 s = vim_strnsave(s + n1, 1);
3439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003440 clear_tv(rettv);
3441 rettv->v_type = VAR_STRING;
3442 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003443 break;
3444
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003445 case VAR_BLOB:
3446 len = blob_len(rettv->vval.v_blob);
3447 if (range)
3448 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003449 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003450 // are out of range the result is empty.
3451 if (n1 < 0)
3452 {
3453 n1 = len + n1;
3454 if (n1 < 0)
3455 n1 = 0;
3456 }
3457 if (n2 < 0)
3458 n2 = len + n2;
3459 else if (n2 >= len)
3460 n2 = len - 1;
3461 if (n1 >= len || n2 < 0 || n1 > n2)
3462 {
3463 clear_tv(rettv);
3464 rettv->v_type = VAR_BLOB;
3465 rettv->vval.v_blob = NULL;
3466 }
3467 else
3468 {
3469 blob_T *blob = blob_alloc();
3470
3471 if (blob != NULL)
3472 {
3473 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3474 {
3475 blob_free(blob);
3476 return FAIL;
3477 }
3478 blob->bv_ga.ga_len = n2 - n1 + 1;
3479 for (i = n1; i <= n2; i++)
3480 blob_set(blob, i - n1,
3481 blob_get(rettv->vval.v_blob, i));
3482
3483 clear_tv(rettv);
3484 rettv_blob_set(rettv, blob);
3485 }
3486 }
3487 }
3488 else
3489 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003490 // The resulting variable is a byte value.
3491 // If the index is too big or negative that is an error.
3492 if (n1 < 0)
3493 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003494 if (n1 < len && n1 >= 0)
3495 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003496 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003497
3498 clear_tv(rettv);
3499 rettv->v_type = VAR_NUMBER;
3500 rettv->vval.v_number = v;
3501 }
3502 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003503 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003504 }
3505 break;
3506
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003507 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003508 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003509 if (n1 < 0)
3510 n1 = len + n1;
3511 if (!empty1 && (n1 < 0 || n1 >= len))
3512 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003513 // For a range we allow invalid values and return an empty
3514 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003515 if (!range)
3516 {
3517 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003518 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003519 return FAIL;
3520 }
3521 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003522 }
3523 if (range)
3524 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003525 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003526
3527 if (n2 < 0)
3528 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003529 else if (n2 >= len)
3530 n2 = len - 1;
3531 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003532 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003533 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003534 if (l == NULL)
3535 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003536 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003537 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 }
3539 else
3540 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003541 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 clear_tv(rettv);
3543 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 }
3545 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003546
3547 case VAR_DICT:
3548 if (range)
3549 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003550 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003551 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003552 if (len == -1)
3553 clear_tv(&var1);
3554 return FAIL;
3555 }
3556 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003558
3559 if (len == -1)
3560 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003561 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003562 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003563 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003564 clear_tv(&var1);
3565 return FAIL;
3566 }
3567 }
3568
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003569 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003570
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003571 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003572 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573 if (len == -1)
3574 clear_tv(&var1);
3575 if (item == NULL)
3576 return FAIL;
3577
3578 copy_tv(&item->di_tv, &var1);
3579 clear_tv(rettv);
3580 *rettv = var1;
3581 }
3582 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003583 }
3584 }
3585
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003586 return OK;
3587}
3588
3589/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003590 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003591 */
3592 char_u *
3593partial_name(partial_T *pt)
3594{
3595 if (pt->pt_name != NULL)
3596 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003597 if (pt->pt_func != NULL)
3598 return pt->pt_func->uf_name;
3599 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003600}
3601
Bram Moolenaarddecc252016-04-06 22:59:37 +02003602 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003603partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003604{
3605 int i;
3606
3607 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003608 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003609 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003610 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003611 if (pt->pt_name != NULL)
3612 {
3613 func_unref(pt->pt_name);
3614 vim_free(pt->pt_name);
3615 }
3616 else
3617 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003618
3619 if (pt->pt_funcstack != NULL)
3620 {
3621 // Decrease the reference count for the context of a closure. If down
3622 // to zero free it and clear the variables on the stack.
3623 if (--pt->pt_funcstack->fs_refcount == 0)
3624 {
3625 garray_T *gap = &pt->pt_funcstack->fs_ga;
3626 typval_T *stack = gap->ga_data;
3627
3628 for (i = 0; i < gap->ga_len; ++i)
3629 clear_tv(stack + i);
3630 ga_clear(gap);
3631 vim_free(pt->pt_funcstack);
3632 }
3633 pt->pt_funcstack = NULL;
3634 }
3635
Bram Moolenaarddecc252016-04-06 22:59:37 +02003636 vim_free(pt);
3637}
3638
3639/*
3640 * Unreference a closure: decrement the reference count and free it when it
3641 * becomes zero.
3642 */
3643 void
3644partial_unref(partial_T *pt)
3645{
3646 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003647 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003648}
3649
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003650/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003651 * Return the next (unique) copy ID.
3652 * Used for serializing nested structures.
3653 */
3654 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003656{
3657 current_copyID += COPYID_INC;
3658 return current_copyID;
3659}
3660
3661/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003662 * Garbage collection for lists and dictionaries.
3663 *
3664 * We use reference counts to be able to free most items right away when they
3665 * are no longer used. But for composite items it's possible that it becomes
3666 * unused while the reference count is > 0: When there is a recursive
3667 * reference. Example:
3668 * :let l = [1, 2, 3]
3669 * :let d = {9: l}
3670 * :let l[1] = d
3671 *
3672 * Since this is quite unusual we handle this with garbage collection: every
3673 * once in a while find out which lists and dicts are not referenced from any
3674 * variable.
3675 *
3676 * Here is a good reference text about garbage collection (refers to Python
3677 * but it applies to all reference-counting mechanisms):
3678 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003679 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003680
3681/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003682 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003683 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003684 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003685 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003686 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003687garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003688{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003689 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003690 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003691 buf_T *buf;
3692 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003693 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003694 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003695
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003696 if (!testing)
3697 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003698 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003699 want_garbage_collect = FALSE;
3700 may_garbage_collect = FALSE;
3701 garbage_collect_at_exit = FALSE;
3702 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003703
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003704 // The execution stack can grow big, limit the size.
3705 if (exestack.ga_maxlen - exestack.ga_len > 500)
3706 {
3707 size_t new_len;
3708 char_u *pp;
3709 int n;
3710
3711 // Keep 150% of the current size, with a minimum of the growth size.
3712 n = exestack.ga_len / 2;
3713 if (n < exestack.ga_growsize)
3714 n = exestack.ga_growsize;
3715
3716 // Don't make it bigger though.
3717 if (exestack.ga_len + n < exestack.ga_maxlen)
3718 {
3719 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3720 pp = vim_realloc(exestack.ga_data, new_len);
3721 if (pp == NULL)
3722 return FAIL;
3723 exestack.ga_maxlen = exestack.ga_len + n;
3724 exestack.ga_data = pp;
3725 }
3726 }
3727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003728 // We advance by two because we add one for items referenced through
3729 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003730 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003731
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003732 /*
3733 * 1. Go through all accessible variables and mark all lists and dicts
3734 * with copyID.
3735 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003736
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003737 // Don't free variables in the previous_funccal list unless they are only
3738 // referenced through previous_funccal. This must be first, because if
3739 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003741
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003742 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003743 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003745 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003746 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003747 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3748 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003750 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003751 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003752 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3753 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003754 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003755 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3756 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003757#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003758 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003759 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3760 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003761 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003762 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003763 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3764 NULL, NULL);
3765#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003767 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003768 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003769 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3770 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003771 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003772 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003774 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003776
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003777 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003778 abort = abort || set_ref_in_functions(copyID);
3779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003780 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003783 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003784 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003785
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003786 // callbacks in buffers
3787 abort = abort || set_ref_in_buffers(copyID);
3788
Bram Moolenaar1dced572012-04-05 16:54:08 +02003789#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003790 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003791#endif
3792
Bram Moolenaardb913952012-06-29 12:54:53 +02003793#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003794 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003795#endif
3796
3797#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003798 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003799#endif
3800
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003801#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003802 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003803 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003804#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003805#ifdef FEAT_NETBEANS_INTG
3806 abort = abort || set_ref_in_nb_channel(copyID);
3807#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003808
Bram Moolenaare3188e22016-05-31 21:13:04 +02003809#ifdef FEAT_TIMERS
3810 abort = abort || set_ref_in_timer(copyID);
3811#endif
3812
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003813#ifdef FEAT_QUICKFIX
3814 abort = abort || set_ref_in_quickfix(copyID);
3815#endif
3816
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003817#ifdef FEAT_TERMINAL
3818 abort = abort || set_ref_in_term(copyID);
3819#endif
3820
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003821#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003822 abort = abort || set_ref_in_popups(copyID);
3823#endif
3824
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003825 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003826 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003827 /*
3828 * 2. Free lists and dictionaries that are not referenced.
3829 */
3830 did_free = free_unref_items(copyID);
3831
3832 /*
3833 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003834 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003835 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003836 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003837 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003838 else if (p_verbose > 0)
3839 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003840 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003841 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003842
3843 return did_free;
3844}
3845
3846/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003847 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003848 */
3849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003850free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003851{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003852 int did_free = FALSE;
3853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003854 // Let all "free" functions know that we are here. This means no
3855 // dictionaries, lists, channels or jobs are to be freed, because we will
3856 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003857 in_free_unref_items = TRUE;
3858
3859 /*
3860 * PASS 1: free the contents of the items. We don't free the items
3861 * themselves yet, so that it is possible to decrement refcount counters
3862 */
3863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003864 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003865 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003868 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003869
3870#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003871 // Go through the list of jobs and free items without the copyID. This
3872 // must happen before doing channels, because jobs refer to channels, but
3873 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003874 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003876 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003877 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3878#endif
3879
3880 /*
3881 * PASS 2: free the items themselves.
3882 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003883 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003884 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003885
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003886#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003887 // Go through the list of jobs and free items without the copyID. This
3888 // must happen before doing channels, because jobs refer to channels, but
3889 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003890 free_unused_jobs(copyID, COPYID_MASK);
3891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003892 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003893 free_unused_channels(copyID, COPYID_MASK);
3894#endif
3895
3896 in_free_unref_items = FALSE;
3897
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003898 return did_free;
3899}
3900
3901/*
3902 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003903 * "list_stack" is used to add lists to be marked. Can be NULL.
3904 *
3905 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003906 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003907 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003908set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003909{
3910 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003911 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003912 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003913 hashtab_T *cur_ht;
3914 ht_stack_T *ht_stack = NULL;
3915 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003916
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003917 cur_ht = ht;
3918 for (;;)
3919 {
3920 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003922 // Mark each item in the hashtab. If the item contains a hashtab
3923 // it is added to ht_stack, if it contains a list it is added to
3924 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003925 todo = (int)cur_ht->ht_used;
3926 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3927 if (!HASHITEM_EMPTY(hi))
3928 {
3929 --todo;
3930 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3931 &ht_stack, list_stack);
3932 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003933 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003934
3935 if (ht_stack == NULL)
3936 break;
3937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003938 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003939 cur_ht = ht_stack->ht;
3940 tempitem = ht_stack;
3941 ht_stack = ht_stack->prev;
3942 free(tempitem);
3943 }
3944
3945 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003946}
3947
3948/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003949 * Mark a dict and its items with "copyID".
3950 * Returns TRUE if setting references failed somehow.
3951 */
3952 int
3953set_ref_in_dict(dict_T *d, int copyID)
3954{
3955 if (d != NULL && d->dv_copyID != copyID)
3956 {
3957 d->dv_copyID = copyID;
3958 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3959 }
3960 return FALSE;
3961}
3962
3963/*
3964 * Mark a list and its items with "copyID".
3965 * Returns TRUE if setting references failed somehow.
3966 */
3967 int
3968set_ref_in_list(list_T *ll, int copyID)
3969{
3970 if (ll != NULL && ll->lv_copyID != copyID)
3971 {
3972 ll->lv_copyID = copyID;
3973 return set_ref_in_list_items(ll, copyID, NULL);
3974 }
3975 return FALSE;
3976}
3977
3978/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003979 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003980 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3981 *
3982 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003983 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003984 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003985set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003986{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003987 listitem_T *li;
3988 int abort = FALSE;
3989 list_T *cur_l;
3990 list_stack_T *list_stack = NULL;
3991 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003992
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003993 cur_l = l;
3994 for (;;)
3995 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003996 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003997 // Mark each item in the list. If the item contains a hashtab
3998 // it is added to ht_stack, if it contains a list it is added to
3999 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004000 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4001 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4002 ht_stack, &list_stack);
4003 if (list_stack == NULL)
4004 break;
4005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004006 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004007 cur_l = list_stack->list;
4008 tempitem = list_stack;
4009 list_stack = list_stack->prev;
4010 free(tempitem);
4011 }
4012
4013 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004014}
4015
4016/*
4017 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004018 * "list_stack" is used to add lists to be marked. Can be NULL.
4019 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4020 *
4021 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004022 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004024set_ref_in_item(
4025 typval_T *tv,
4026 int copyID,
4027 ht_stack_T **ht_stack,
4028 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004029{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004030 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004031
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004032 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004033 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004034 dict_T *dd = tv->vval.v_dict;
4035
Bram Moolenaara03f2332016-02-06 18:09:59 +01004036 if (dd != NULL && dd->dv_copyID != copyID)
4037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004038 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004039 dd->dv_copyID = copyID;
4040 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004041 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004042 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4043 }
4044 else
4045 {
4046 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4047 if (newitem == NULL)
4048 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004049 else
4050 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004051 newitem->ht = &dd->dv_hashtab;
4052 newitem->prev = *ht_stack;
4053 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004054 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004055 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004056 }
4057 }
4058 else if (tv->v_type == VAR_LIST)
4059 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004060 list_T *ll = tv->vval.v_list;
4061
Bram Moolenaara03f2332016-02-06 18:09:59 +01004062 if (ll != NULL && ll->lv_copyID != copyID)
4063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004064 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004065 ll->lv_copyID = copyID;
4066 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004067 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004068 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004069 }
4070 else
4071 {
4072 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004073 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004074 if (newitem == NULL)
4075 abort = TRUE;
4076 else
4077 {
4078 newitem->list = ll;
4079 newitem->prev = *list_stack;
4080 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004081 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004082 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004083 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004084 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004085 else if (tv->v_type == VAR_FUNC)
4086 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004087 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004088 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004089 else if (tv->v_type == VAR_PARTIAL)
4090 {
4091 partial_T *pt = tv->vval.v_partial;
4092 int i;
4093
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004094 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004095 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004096 // Didn't see this partial yet.
4097 pt->pt_copyID = copyID;
4098
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004099 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004100
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004101 if (pt->pt_dict != NULL)
4102 {
4103 typval_T dtv;
4104
4105 dtv.v_type = VAR_DICT;
4106 dtv.vval.v_dict = pt->pt_dict;
4107 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4108 }
4109
4110 for (i = 0; i < pt->pt_argc; ++i)
4111 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4112 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004113 if (pt->pt_funcstack != NULL)
4114 {
4115 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4116
4117 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4118 abort = abort || set_ref_in_item(stack + i, copyID,
4119 ht_stack, list_stack);
4120 }
4121
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004122 }
4123 }
4124#ifdef FEAT_JOB_CHANNEL
4125 else if (tv->v_type == VAR_JOB)
4126 {
4127 job_T *job = tv->vval.v_job;
4128 typval_T dtv;
4129
4130 if (job != NULL && job->jv_copyID != copyID)
4131 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004132 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004133 if (job->jv_channel != NULL)
4134 {
4135 dtv.v_type = VAR_CHANNEL;
4136 dtv.vval.v_channel = job->jv_channel;
4137 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4138 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004139 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004140 {
4141 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004142 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004143 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4144 }
4145 }
4146 }
4147 else if (tv->v_type == VAR_CHANNEL)
4148 {
4149 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004150 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004151 typval_T dtv;
4152 jsonq_T *jq;
4153 cbq_T *cq;
4154
4155 if (ch != NULL && ch->ch_copyID != copyID)
4156 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004157 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004158 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004159 {
4160 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4161 jq = jq->jq_next)
4162 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4163 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4164 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004165 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004166 {
4167 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004168 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004169 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4170 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004171 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004172 {
4173 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004174 dtv.vval.v_partial =
4175 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004176 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4177 }
4178 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004179 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004180 {
4181 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004182 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004183 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4184 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004185 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004186 {
4187 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004188 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004189 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4190 }
4191 }
4192 }
4193#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004194 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004195}
4196
Bram Moolenaar8c711452005-01-14 21:53:12 +00004197/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004198 * Return a string with the string representation of a variable.
4199 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004200 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004201 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004202 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4203 * stings as "string()", otherwise does not put quotes around strings, as
4204 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004205 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4206 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004207 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004208 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004209 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004210echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004211 typval_T *tv,
4212 char_u **tofree,
4213 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004214 int copyID,
4215 int echo_style,
4216 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004217 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004218{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004219 static int recurse = 0;
4220 char_u *r = NULL;
4221
Bram Moolenaar33570922005-01-25 22:26:29 +00004222 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004223 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004224 if (!did_echo_string_emsg)
4225 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004226 // Only give this message once for a recursive call to avoid
4227 // flooding the user with errors. And stop iterating over lists
4228 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004229 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004230 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004232 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004233 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004234 }
4235 ++recurse;
4236
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004237 switch (tv->v_type)
4238 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004239 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004240 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004241 {
4242 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004243 r = tv->vval.v_string;
4244 if (r == NULL)
4245 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004246 }
4247 else
4248 {
4249 *tofree = string_quote(tv->vval.v_string, FALSE);
4250 r = *tofree;
4251 }
4252 break;
4253
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004254 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004255 if (echo_style)
4256 {
4257 *tofree = NULL;
4258 r = tv->vval.v_string;
4259 }
4260 else
4261 {
4262 *tofree = string_quote(tv->vval.v_string, TRUE);
4263 r = *tofree;
4264 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004265 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004266
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004267 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004268 {
4269 partial_T *pt = tv->vval.v_partial;
4270 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004271 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004272 garray_T ga;
4273 int i;
4274 char_u *tf;
4275
4276 ga_init2(&ga, 1, 100);
4277 ga_concat(&ga, (char_u *)"function(");
4278 if (fname != NULL)
4279 {
4280 ga_concat(&ga, fname);
4281 vim_free(fname);
4282 }
4283 if (pt != NULL && pt->pt_argc > 0)
4284 {
4285 ga_concat(&ga, (char_u *)", [");
4286 for (i = 0; i < pt->pt_argc; ++i)
4287 {
4288 if (i > 0)
4289 ga_concat(&ga, (char_u *)", ");
4290 ga_concat(&ga,
4291 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4292 vim_free(tf);
4293 }
4294 ga_concat(&ga, (char_u *)"]");
4295 }
4296 if (pt != NULL && pt->pt_dict != NULL)
4297 {
4298 typval_T dtv;
4299
4300 ga_concat(&ga, (char_u *)", ");
4301 dtv.v_type = VAR_DICT;
4302 dtv.vval.v_dict = pt->pt_dict;
4303 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4304 vim_free(tf);
4305 }
4306 ga_concat(&ga, (char_u *)")");
4307
4308 *tofree = ga.ga_data;
4309 r = *tofree;
4310 break;
4311 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004312
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004313 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004314 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004315 break;
4316
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004317 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004318 if (tv->vval.v_list == NULL)
4319 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004320 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004321 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004322 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004323 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004324 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4325 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004326 {
4327 *tofree = NULL;
4328 r = (char_u *)"[...]";
4329 }
4330 else
4331 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004332 int old_copyID = tv->vval.v_list->lv_copyID;
4333
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004334 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004335 *tofree = list2string(tv, copyID, restore_copyID);
4336 if (restore_copyID)
4337 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004338 r = *tofree;
4339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004340 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004341
Bram Moolenaar8c711452005-01-14 21:53:12 +00004342 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004343 if (tv->vval.v_dict == NULL)
4344 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004345 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004346 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004347 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004348 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004349 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4350 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004351 {
4352 *tofree = NULL;
4353 r = (char_u *)"{...}";
4354 }
4355 else
4356 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004357 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004358
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004359 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004360 *tofree = dict2string(tv, copyID, restore_copyID);
4361 if (restore_copyID)
4362 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004363 r = *tofree;
4364 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004365 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004367 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004368 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004369 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004371 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004372 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004373 break;
4374
Bram Moolenaar835dc632016-02-07 14:27:38 +01004375 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004376 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004377 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004378 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004379 if (composite_val)
4380 {
4381 *tofree = string_quote(r, FALSE);
4382 r = *tofree;
4383 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004384 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004386 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004387#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 *tofree = NULL;
4389 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4390 r = numbuf;
4391 break;
4392#endif
4393
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004394 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004395 case VAR_SPECIAL:
4396 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004397 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004398 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004399 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004400
Bram Moolenaar8502c702014-06-17 12:51:16 +02004401 if (--recurse == 0)
4402 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004403 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004404}
4405
4406/*
4407 * Return a string with the string representation of a variable.
4408 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4409 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004410 * Does not put quotes around strings, as ":echo" displays values.
4411 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4412 * May return NULL.
4413 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004414 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004415echo_string(
4416 typval_T *tv,
4417 char_u **tofree,
4418 char_u *numbuf,
4419 int copyID)
4420{
4421 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4422}
4423
4424/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004425 * Return string "str" in ' quotes, doubling ' characters.
4426 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004427 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004428 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004429 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004430string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004431{
Bram Moolenaar33570922005-01-25 22:26:29 +00004432 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004433 char_u *p, *r, *s;
4434
Bram Moolenaar33570922005-01-25 22:26:29 +00004435 len = (function ? 13 : 3);
4436 if (str != NULL)
4437 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004438 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004439 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004440 if (*p == '\'')
4441 ++len;
4442 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004443 s = r = alloc(len);
4444 if (r != NULL)
4445 {
4446 if (function)
4447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004448 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004449 r += 10;
4450 }
4451 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004452 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004453 if (str != NULL)
4454 for (p = str; *p != NUL; )
4455 {
4456 if (*p == '\'')
4457 *r++ = '\'';
4458 MB_COPY_CHAR(p, r);
4459 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004460 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004461 if (function)
4462 *r++ = ')';
4463 *r++ = NUL;
4464 }
4465 return s;
4466}
4467
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004468#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004469/*
4470 * Convert the string "text" to a floating point number.
4471 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4472 * this always uses a decimal point.
4473 * Returns the length of the text that was consumed.
4474 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004476string2float(
4477 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004479{
4480 char *s = (char *)text;
4481 float_T f;
4482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004483 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004484 if (STRNICMP(text, "inf", 3) == 0)
4485 {
4486 *value = INFINITY;
4487 return 3;
4488 }
4489 if (STRNICMP(text, "-inf", 3) == 0)
4490 {
4491 *value = -INFINITY;
4492 return 4;
4493 }
4494 if (STRNICMP(text, "nan", 3) == 0)
4495 {
4496 *value = NAN;
4497 return 3;
4498 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004499 f = strtod(s, &s);
4500 *value = f;
4501 return (int)((char_u *)s - text);
4502}
4503#endif
4504
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004507 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004509 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004510var2fpos(
4511 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004512 int dollar_lnum, // TRUE when $ is last line
4513 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004515 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004517 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004519 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004520 if (varp->v_type == VAR_LIST)
4521 {
4522 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004523 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004524 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004525 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004526
4527 l = varp->vval.v_list;
4528 if (l == NULL)
4529 return NULL;
4530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004531 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004532 pos.lnum = list_find_nr(l, 0L, &error);
4533 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004534 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004536 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004537 pos.col = list_find_nr(l, 1L, &error);
4538 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004539 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004540 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004542 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004543 li = list_find(l, 1L);
4544 if (li != NULL && li->li_tv.v_type == VAR_STRING
4545 && li->li_tv.vval.v_string != NULL
4546 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4547 pos.col = len + 1;
4548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004549 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004550 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004551 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004552 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004554 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004555 pos.coladd = list_find_nr(l, 2L, &error);
4556 if (error)
4557 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004558
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004559 return &pos;
4560 }
4561
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004562 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004563 if (name == NULL)
4564 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004565 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004567 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004568 {
4569 if (VIsual_active)
4570 return &VIsual;
4571 return &curwin->w_cursor;
4572 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004573 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004575 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4577 return NULL;
4578 return pp;
4579 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004580
Bram Moolenaara5525202006-03-02 22:52:09 +00004581 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004582
Bram Moolenaar477933c2007-07-17 14:32:23 +00004583 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004584 {
4585 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004586 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004587 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004588 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004589 // In silent Ex mode topline is zero, but that's not a valid line
4590 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004591 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004592 return &pos;
4593 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004594 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004595 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004596 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004597 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004598 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004599 return &pos;
4600 }
4601 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004602 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004604 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 pos.lnum = curbuf->b_ml.ml_line_count;
4607 pos.col = 0;
4608 }
4609 else
4610 {
4611 pos.lnum = curwin->w_cursor.lnum;
4612 pos.col = (colnr_T)STRLEN(ml_get_curline());
4613 }
4614 return &pos;
4615 }
4616 return NULL;
4617}
4618
4619/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004620 * Convert list in "arg" into a position and optional file number.
4621 * When "fnump" is NULL there is no file number, only 3 items.
4622 * Note that the column is passed on as-is, the caller may want to decrement
4623 * it to use 1 for the first column.
4624 * Return FAIL when conversion is not possible, doesn't check the position for
4625 * validity.
4626 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004628list2fpos(
4629 typval_T *arg,
4630 pos_T *posp,
4631 int *fnump,
4632 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004633{
4634 list_T *l = arg->vval.v_list;
4635 long i = 0;
4636 long n;
4637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4639 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004640 if (arg->v_type != VAR_LIST
4641 || l == NULL
4642 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004643 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004644 return FAIL;
4645
4646 if (fnump != NULL)
4647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004648 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004649 if (n < 0)
4650 return FAIL;
4651 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004652 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004653 *fnump = n;
4654 }
4655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004656 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004657 if (n < 0)
4658 return FAIL;
4659 posp->lnum = n;
4660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004661 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004662 if (n < 0)
4663 return FAIL;
4664 posp->col = n;
4665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004666 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004667 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004668 posp->coladd = 0;
4669 else
4670 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004671
Bram Moolenaar493c1782014-05-28 14:34:46 +02004672 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004673 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004674
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004675 return OK;
4676}
4677
4678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 * Get the length of an environment variable name.
4680 * Advance "arg" to the first character after the name.
4681 * Return 0 for error.
4682 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004683 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004684get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
4686 char_u *p;
4687 int len;
4688
4689 for (p = *arg; vim_isIDc(*p); ++p)
4690 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004691 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 return 0;
4693
4694 len = (int)(p - *arg);
4695 *arg = p;
4696 return len;
4697}
4698
4699/*
4700 * Get the length of the name of a function or internal variable.
4701 * "arg" is advanced to the first non-white character after the name.
4702 * Return 0 if something is wrong.
4703 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004705get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
4707 char_u *p;
4708 int len;
4709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004710 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004712 {
4713 if (*p == ':')
4714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004715 // "s:" is start of "s:var", but "n:" is not and can be used in
4716 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004717 len = (int)(p - *arg);
4718 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4719 || len > 1)
4720 break;
4721 }
4722 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004723 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return 0;
4725
4726 len = (int)(p - *arg);
4727 *arg = skipwhite(p);
4728
4729 return len;
4730}
4731
4732/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004733 * Get the length of the name of a variable or function.
4734 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004736 * Return -1 if curly braces expansion failed.
4737 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 * If the name contains 'magic' {}'s, expand them and return the
4739 * expanded name in an allocated string via 'alias' - caller must free.
4740 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004742get_name_len(
4743 char_u **arg,
4744 char_u **alias,
4745 int evaluate,
4746 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 char_u *p;
4750 char_u *expr_start;
4751 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004753 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4756 && (*arg)[2] == (int)KE_SNR)
4757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004758 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 *arg += 3;
4760 return get_id_len(arg) + 3;
4761 }
4762 len = eval_fname_script(*arg);
4763 if (len > 0)
4764 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 *arg += len;
4767 }
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004772 p = find_name_end(*arg, &expr_start, &expr_end,
4773 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (expr_start != NULL)
4775 {
4776 char_u *temp_string;
4777
4778 if (!evaluate)
4779 {
4780 len += (int)(p - *arg);
4781 *arg = skipwhite(p);
4782 return len;
4783 }
4784
4785 /*
4786 * Include any <SID> etc in the expanded string:
4787 * Thus the -len here.
4788 */
4789 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4790 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004791 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 *alias = temp_string;
4793 *arg = skipwhite(p);
4794 return (int)STRLEN(temp_string);
4795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
4797 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004798 // Only give an error when there is something, otherwise it will be
4799 // reported at a higher level.
4800 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004801 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
4803 return len;
4804}
4805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004806/*
4807 * Find the end of a variable or function name, taking care of magic braces.
4808 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4809 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004810 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 * Return a pointer to just after the name. Equal to "arg" if there is no
4812 * valid name.
4813 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004814 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004815find_name_end(
4816 char_u *arg,
4817 char_u **expr_start,
4818 char_u **expr_end,
4819 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 int mb_nest = 0;
4822 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004824 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004825 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 *expr_start = NULL;
4830 *expr_end = NULL;
4831 }
4832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004833 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004834 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4835 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004836 return arg;
4837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 for (p = arg; *p != NUL
4839 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004840 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004841 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004842 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004843 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004845 if (*p == '\'')
4846 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004847 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004848 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004849 ;
4850 if (*p == NUL)
4851 break;
4852 }
4853 else if (*p == '"')
4854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004855 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004856 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004857 if (*p == '\\' && p[1] != NUL)
4858 ++p;
4859 if (*p == NUL)
4860 break;
4861 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004862 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004864 // "s:" is start of "s:var", but "n:" is not and can be used in
4865 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004866 len = (int)(p - arg);
4867 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004868 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004869 break;
4870 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004872 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 if (*p == '[')
4875 ++br_nest;
4876 else if (*p == ']')
4877 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004879
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004880 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 if (*p == '{')
4883 {
4884 mb_nest++;
4885 if (expr_start != NULL && *expr_start == NULL)
4886 *expr_start = p;
4887 }
4888 else if (*p == '}')
4889 {
4890 mb_nest--;
4891 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4892 *expr_end = p;
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
4896
4897 return p;
4898}
4899
4900/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004901 * Expands out the 'magic' {}'s in a variable/function name.
4902 * Note that this can call itself recursively, to deal with
4903 * constructs like foo{bar}{baz}{bam}
4904 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4905 * "in_start" ^
4906 * "expr_start" ^
4907 * "expr_end" ^
4908 * "in_end" ^
4909 *
4910 * Returns a new allocated string, which the caller must free.
4911 * Returns NULL for failure.
4912 */
4913 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004914make_expanded_name(
4915 char_u *in_start,
4916 char_u *expr_start,
4917 char_u *expr_end,
4918 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004919{
4920 char_u c1;
4921 char_u *retval = NULL;
4922 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004923
4924 if (expr_end == NULL || in_end == NULL)
4925 return NULL;
4926 *expr_start = NUL;
4927 *expr_end = NUL;
4928 c1 = *in_end;
4929 *in_end = NUL;
4930
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004931 temp_result = eval_to_string(expr_start + 1, FALSE);
4932 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004933 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004934 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4935 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004936 if (retval != NULL)
4937 {
4938 STRCPY(retval, in_start);
4939 STRCAT(retval, temp_result);
4940 STRCAT(retval, expr_end + 1);
4941 }
4942 }
4943 vim_free(temp_result);
4944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004945 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004946 *expr_start = '{';
4947 *expr_end = '}';
4948
4949 if (retval != NULL)
4950 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004951 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004952 if (expr_start != NULL)
4953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004955 temp_result = make_expanded_name(retval, expr_start,
4956 expr_end, temp_result);
4957 vim_free(retval);
4958 retval = temp_result;
4959 }
4960 }
4961
4962 return retval;
4963}
4964
4965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004967 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004970eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004972 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4973}
4974
4975/*
4976 * Return TRUE if character "c" can be used as the first character in a
4977 * variable or function name (excluding '{' and '}').
4978 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004979 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004980eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004981{
4982 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983}
4984
4985/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004986 * Handle:
4987 * - expr[expr], expr[expr:expr] subscript
4988 * - ".name" lookup
4989 * - function call with Funcref variable: func(expr)
4990 * - method call: var->method()
4991 *
4992 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004995handle_subscript(
4996 char_u **arg,
4997 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004998 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004999 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005000{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005001 int evaluate = evalarg != NULL
5002 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005003 int ret = OK;
5004 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005005
Bram Moolenaar61343f02019-07-20 21:11:13 +02005006 // "." is ".name" lookup when we found a dict or when evaluating and
5007 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005008 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005009 && (((**arg == '['
5010 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005011 || (!evaluate
5012 && (*arg)[1] != '.'
5013 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005014 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005015 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005016 && !VIM_ISWHITE(*(*arg - 1)))
5017 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005018 {
5019 if (**arg == '(')
5020 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005021 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005022
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005023 // Stop the expression evaluation when immediately aborting on
5024 // error, or when an interrupt occurred or an exception was thrown
5025 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005026 if (aborting())
5027 {
5028 if (ret == OK)
5029 clear_tv(rettv);
5030 ret = FAIL;
5031 }
5032 dict_unref(selfdict);
5033 selfdict = NULL;
5034 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005035 else if (**arg == '-')
5036 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005037 if (ret == OK)
5038 {
5039 if ((*arg)[2] == '{')
5040 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005041 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005042 else
5043 // expr->name()
5044 ret = eval_method(arg, rettv, evaluate, verbose);
5045 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005046 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005047 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005048 {
5049 dict_unref(selfdict);
5050 if (rettv->v_type == VAR_DICT)
5051 {
5052 selfdict = rettv->vval.v_dict;
5053 if (selfdict != NULL)
5054 ++selfdict->dv_refcount;
5055 }
5056 else
5057 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005058 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005059 {
5060 clear_tv(rettv);
5061 ret = FAIL;
5062 }
5063 }
5064 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005066 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5067 // Don't do this when "Func" is already a partial that was bound
5068 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005069 if (selfdict != NULL
5070 && (rettv->v_type == VAR_FUNC
5071 || (rettv->v_type == VAR_PARTIAL
5072 && (rettv->vval.v_partial->pt_auto
5073 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005075
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 dict_unref(selfdict);
5077 return ret;
5078}
5079
5080/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005081 * Make a copy of an item.
5082 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005083 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5084 * reference to an already copied list/dict can be used.
5085 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005086 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005087 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005088item_copy(
5089 typval_T *from,
5090 typval_T *to,
5091 int deep,
5092 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005093{
5094 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005095 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005096
Bram Moolenaar33570922005-01-25 22:26:29 +00005097 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005099 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005100 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005101 }
5102 ++recurse;
5103
5104 switch (from->v_type)
5105 {
5106 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005107 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005108 case VAR_STRING:
5109 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005110 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005111 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005112 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005113 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005114 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005115 copy_tv(from, to);
5116 break;
5117 case VAR_LIST:
5118 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005119 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005120 if (from->vval.v_list == NULL)
5121 to->vval.v_list = NULL;
5122 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5123 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005124 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005125 to->vval.v_list = from->vval.v_list->lv_copylist;
5126 ++to->vval.v_list->lv_refcount;
5127 }
5128 else
5129 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5130 if (to->vval.v_list == NULL)
5131 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005132 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005133 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005135 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005136 case VAR_DICT:
5137 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005138 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005139 if (from->vval.v_dict == NULL)
5140 to->vval.v_dict = NULL;
5141 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005143 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005144 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5145 ++to->vval.v_dict->dv_refcount;
5146 }
5147 else
5148 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5149 if (to->vval.v_dict == NULL)
5150 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005151 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005152 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005153 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005155 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005156 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005157 }
5158 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005159 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005160}
5161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 void
5163echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5164{
5165 char_u *tofree;
5166 char_u numbuf[NUMBUFLEN];
5167 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5168
5169 if (*atstart)
5170 {
5171 *atstart = FALSE;
5172 // Call msg_start() after eval1(), evaluating the expression
5173 // may cause a message to appear.
5174 if (with_space)
5175 {
5176 // Mark the saved text as finishing the line, so that what
5177 // follows is displayed on a new line when scrolling back
5178 // at the more prompt.
5179 msg_sb_eol();
5180 msg_start();
5181 }
5182 }
5183 else if (with_space)
5184 msg_puts_attr(" ", echo_attr);
5185
5186 if (p != NULL)
5187 for ( ; *p != NUL && !got_int; ++p)
5188 {
5189 if (*p == '\n' || *p == '\r' || *p == TAB)
5190 {
5191 if (*p != TAB && *needclr)
5192 {
5193 // remove any text still there from the command
5194 msg_clr_eos();
5195 *needclr = FALSE;
5196 }
5197 msg_putchar_attr(*p, echo_attr);
5198 }
5199 else
5200 {
5201 if (has_mbyte)
5202 {
5203 int i = (*mb_ptr2len)(p);
5204
5205 (void)msg_outtrans_len_attr(p, i, echo_attr);
5206 p += i - 1;
5207 }
5208 else
5209 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5210 }
5211 }
5212 vim_free(tofree);
5213}
5214
Bram Moolenaare9a41262005-01-15 22:18:47 +00005215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 * ":echo expr1 ..." print each argument separated with a space, add a
5217 * newline at the end.
5218 * ":echon expr1 ..." print each argument plain.
5219 */
5220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005221ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222{
5223 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 char_u *p;
5226 int needclr = TRUE;
5227 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005228 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005229 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005230 evalarg_T evalarg;
5231
5232 CLEAR_FIELD(evalarg);
5233 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +02005234 if (getline_equal(eap->getline, eap->cookie, getsourceline))
5235 {
5236 evalarg.eval_getline = eap->getline;
5237 evalarg.eval_cookie = eap->cookie;
5238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 if (eap->skip)
5241 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005242 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005244 // If eval1() causes an error message the text from the command may
5245 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246 need_clr_eos = needclr;
5247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005249 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 /*
5252 * Report the invalid expression unless the expression evaluation
5253 * has been cancelled due to an aborting error, an interrupt, or an
5254 * exception.
5255 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005256 if (!aborting() && did_emsg == did_emsg_before
5257 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005258 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005259 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 break;
5261 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005262 need_clr_eos = FALSE;
5263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005266
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005267 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 arg = skipwhite(arg);
5269 }
5270 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005271 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 if (eap->skip)
5274 --emsg_skip;
5275 else
5276 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005277 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (needclr)
5279 msg_clr_eos();
5280 if (eap->cmdidx == CMD_echo)
5281 msg_end();
5282 }
5283}
5284
5285/*
5286 * ":echohl {name}".
5287 */
5288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005289ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005291 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292}
5293
5294/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005295 * Returns the :echo attribute
5296 */
5297 int
5298get_echo_attr(void)
5299{
5300 return echo_attr;
5301}
5302
5303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 * ":execute expr1 ..." execute the result of an expression.
5305 * ":echomsg expr1 ..." Print a message
5306 * ":echoerr expr1 ..." Print an error
5307 * Each gets spaces around each argument and a newline at the end for
5308 * echo commands
5309 */
5310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005311ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312{
5313 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005314 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 int ret = OK;
5316 char_u *p;
5317 garray_T ga;
5318 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 ga_init2(&ga, 1, 80);
5321
5322 if (eap->skip)
5323 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005324 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005326 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5327 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 if (!eap->skip)
5331 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005332 char_u buf[NUMBUFLEN];
5333
5334 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005335 {
5336 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5337 {
5338 emsg(_(e_inval_string));
5339 p = NULL;
5340 }
5341 else
5342 p = tv_get_string_buf(&rettv, buf);
5343 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005344 else
5345 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005346 if (p == NULL)
5347 {
5348 clear_tv(&rettv);
5349 ret = FAIL;
5350 break;
5351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 len = (int)STRLEN(p);
5353 if (ga_grow(&ga, len + 2) == FAIL)
5354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005355 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 ret = FAIL;
5357 break;
5358 }
5359 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 ga.ga_len += len;
5363 }
5364
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 arg = skipwhite(arg);
5367 }
5368
5369 if (ret != FAIL && ga.ga_data != NULL)
5370 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005371 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5372 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005373 // Mark the already saved text as finishing the line, so that what
5374 // follows is displayed on a new line when scrolling back at the
5375 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005376 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005377 }
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005380 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005381 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005382 out_flush();
5383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 else if (eap->cmdidx == CMD_echoerr)
5385 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005386 int save_did_emsg = did_emsg;
5387
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005388 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005389 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if (!force_abort)
5391 did_emsg = save_did_emsg;
5392 }
5393 else if (eap->cmdidx == CMD_execute)
5394 do_cmdline((char_u *)ga.ga_data,
5395 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5396 }
5397
5398 ga_clear(&ga);
5399
5400 if (eap->skip)
5401 --emsg_skip;
5402
5403 eap->nextcmd = check_nextcmd(arg);
5404}
5405
5406/*
5407 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5408 * "arg" points to the "&" or '+' when called, to "option" when returning.
5409 * Returns NULL when no option name found. Otherwise pointer to the char
5410 * after the option name.
5411 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005412 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005413find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 char_u *p = *arg;
5416
5417 ++p;
5418 if (*p == 'g' && p[1] == ':')
5419 {
5420 *opt_flags = OPT_GLOBAL;
5421 p += 2;
5422 }
5423 else if (*p == 'l' && p[1] == ':')
5424 {
5425 *opt_flags = OPT_LOCAL;
5426 p += 2;
5427 }
5428 else
5429 *opt_flags = 0;
5430
5431 if (!ASCII_ISALPHA(*p))
5432 return NULL;
5433 *arg = p;
5434
5435 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 else
5438 while (ASCII_ISALPHA(*p))
5439 ++p;
5440 return p;
5441}
5442
5443/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005444 * Display script name where an item was last set.
5445 * Should only be invoked when 'verbose' is non-zero.
5446 */
5447 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005448last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005449{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005450 char_u *p;
5451
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005452 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005453 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005454 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005455 if (p != NULL)
5456 {
5457 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005458 msg_puts(_("\n\tLast set from "));
5459 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005460 if (script_ctx.sc_lnum > 0)
5461 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005462 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005463 msg_outnum((long)script_ctx.sc_lnum);
5464 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005465 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005466 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005467 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005468 }
5469}
5470
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005471#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
5473/*
5474 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005475 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 * "flags" can be "g" to do a global substitute.
5477 * Returns an allocated string, NULL for error.
5478 */
5479 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005480do_string_sub(
5481 char_u *str,
5482 char_u *pat,
5483 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005484 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005485 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
5487 int sublen;
5488 regmatch_T regmatch;
5489 int i;
5490 int do_all;
5491 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005492 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 garray_T ga;
5494 char_u *ret;
5495 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005496 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005498 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005500 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
5502 ga_init2(&ga, 1, 200);
5503
5504 do_all = (flags[0] == 'g');
5505
5506 regmatch.rm_ic = p_ic;
5507 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5508 if (regmatch.regprog != NULL)
5509 {
5510 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005511 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5513 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005514 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005515 if (regmatch.startp[0] == regmatch.endp[0])
5516 {
5517 if (zero_width == regmatch.startp[0])
5518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005520 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005521 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5522 (size_t)i);
5523 ga.ga_len += i;
5524 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005525 continue;
5526 }
5527 zero_width = regmatch.startp[0];
5528 }
5529
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 /*
5531 * Get some space for a temporary buffer to do the substitution
5532 * into. It will contain:
5533 * - The text up to where the match is.
5534 * - The substituted text.
5535 * - The text after the match.
5536 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005537 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005538 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5540 {
5541 ga_clear(&ga);
5542 break;
5543 }
5544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005545 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 i = (int)(regmatch.startp[0] - tail);
5547 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005548 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005549 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 + ga.ga_len + i, TRUE, TRUE, FALSE);
5551 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005552 tail = regmatch.endp[0];
5553 if (*tail == NUL)
5554 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 if (!do_all)
5556 break;
5557 }
5558
5559 if (ga.ga_data != NULL)
5560 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5561
Bram Moolenaar473de612013-06-08 18:19:48 +02005562 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564
5565 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5566 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005567 if (p_cpo == empty_option)
5568 p_cpo = save_cpo;
5569 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005570 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005571 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
5573 return ret;
5574}