blob: 5b69dc736d1594344e3b2750e1ca97114be486f1 [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 Moolenaar9b5384b2020-06-29 22:31:36 +0200737 lp->ll_name_end = find_name_end(name, NULL, NULL,
738 FNE_INCL_BR | fne_flags);
739 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000740 }
741
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100742 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000743 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000745 if (expr_start != NULL)
746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100747 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100748 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000749 && *p != '[' && *p != '.')
750 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100751 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000752 return NULL;
753 }
754
755 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
756 if (lp->ll_exp_name == NULL)
757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100758 // Report an invalid expression in braces, unless the
759 // expression evaluation has been cancelled due to an
760 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000761 if (!aborting() && !quiet)
762 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000763 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100764 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000765 return NULL;
766 }
767 }
768 lp->ll_name = lp->ll_exp_name;
769 }
770 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 lp->ll_name = name;
773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
775 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100776 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 char_u *tp = skipwhite(p + 1);
778
779 // parse the type after the name
780 lp->ll_type = parse_type(&tp, &si->sn_type_list);
781 lp->ll_name_end = tp;
782 }
783 }
784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100785 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000786 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
787 return p;
788
789 cc = *p;
790 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100791 // Only pass &ht when we would write to the variable, it prevents autoload
792 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100793 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
794 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000795 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000797 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000798 if (v == NULL)
799 return NULL;
800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 /*
802 * Loop until no more [idx] or .key is following.
803 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000804 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100805 var1.v_type = VAR_UNKNOWN;
806 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
810 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100811 && lp->ll_tv->vval.v_dict != NULL)
812 && !(lp->ll_tv->v_type == VAR_BLOB
813 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100816 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000818 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100822 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000825
Bram Moolenaar8c711452005-01-14 21:53:12 +0000826 len = -1;
827 if (*p == '.')
828 {
829 key = p + 1;
830 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
831 ;
832 if (len == 0)
833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100835 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000837 }
838 p = key + len;
839 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000840 else
841 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100842 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000843 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000844 if (*p == ':')
845 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000846 else
847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000848 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200849 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100851 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100853 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000854 clear_tv(&var1);
855 return NULL;
856 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000857 }
858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100859 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000860 if (*p == ':')
861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100865 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100866 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000867 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000868 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100869 if (rettv != NULL
870 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100871 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100872 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100873 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100876 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100877 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000879 }
880 p = skipwhite(p + 1);
881 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000883 else
884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000885 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200886 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200887 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000888 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100889 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000891 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100892 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100894 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100895 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000896 clear_tv(&var2);
897 return NULL;
898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000899 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000901 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000903 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000904
Bram Moolenaar8c711452005-01-14 21:53:12 +0000905 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000906 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000907 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100908 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100909 clear_tv(&var1);
910 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 }
913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100914 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000915 ++p;
916 }
917
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 {
920 if (len == -1)
921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100922 // "[key]": get key from "var1"
923 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200924 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000925 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000926 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000928 }
929 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000930 lp->ll_list = NULL;
931 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000932 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100934 // When assigning to a scope dictionary check that a function and
935 // variable name is valid (only variable name unless it is l: or
936 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200937 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200938 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200939 int prevval;
940 int wrong;
941
942 if (len != -1)
943 {
944 prevval = key[len];
945 key[len] = NUL;
946 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200947 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100948 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200949 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
950 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200951 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200952 || !valid_varname(key);
953 if (len != -1)
954 key[len] = prevval;
955 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200956 {
957 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200958 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200959 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200960 }
961
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000963 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100964 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200965 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100966 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100968 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100969 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200970 return NULL;
971 }
972
Bram Moolenaar31b81602019-02-10 22:14:27 +0100973 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000974 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100977 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100978 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000980 }
981 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000983 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100985 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000987 p = NULL;
988 break;
989 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100990 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100991 else if ((flags & GLV_READ_ONLY) == 0
992 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100993 {
994 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200995 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100996 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200997
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100998 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001001 else if (lp->ll_tv->v_type == VAR_BLOB)
1002 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001003 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1004
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001005 /*
1006 * Get the number and item for the only or first index of the List.
1007 */
1008 if (empty1)
1009 lp->ll_n1 = 0;
1010 else
1011 // is number or string
1012 lp->ll_n1 = (long)tv_get_number(&var1);
1013 clear_tv(&var1);
1014
1015 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 || lp->ll_n1 > bloblen
1017 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001018 {
1019 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001020 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001021 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001022 return NULL;
1023 }
1024 if (lp->ll_range && !lp->ll_empty2)
1025 {
1026 lp->ll_n2 = (long)tv_get_number(&var2);
1027 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001028 if (lp->ll_n2 < 0
1029 || lp->ll_n2 >= bloblen
1030 || lp->ll_n2 < lp->ll_n1)
1031 {
1032 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001033 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001034 return NULL;
1035 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001036 }
1037 lp->ll_blob = lp->ll_tv->vval.v_blob;
1038 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001039 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001041 else
1042 {
1043 /*
1044 * Get the number and item for the only or first index of the List.
1045 */
1046 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001049 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001050 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001051 clear_tv(&var1);
1052
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001053 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 lp->ll_list = lp->ll_tv->vval.v_list;
1055 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1056 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001058 if (lp->ll_n1 < 0)
1059 {
1060 lp->ll_n1 = 0;
1061 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1062 }
1063 }
1064 if (lp->ll_li == NULL)
1065 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001066 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001067 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001068 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 }
1071
1072 /*
1073 * May need to find the item or absolute index for the second
1074 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 * When no index given: "lp->ll_empty2" is TRUE.
1076 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001077 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001079 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001080 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001081 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001082 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001083 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001086 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001087 {
1088 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001089 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001090 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001091 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001092 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001093 }
1094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001095 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001096 if (lp->ll_n1 < 0)
1097 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1098 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001099 {
1100 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001101 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001102 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001103 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001104 }
1105
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001106 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001108 }
1109
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001110 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001112 return p;
1113}
1114
1115/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001116 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001119clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120{
1121 vim_free(lp->ll_exp_name);
1122 vim_free(lp->ll_newkey);
1123}
1124
1125/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001126 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001128 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1129 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001130 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001132set_var_lval(
1133 lval_T *lp,
1134 char_u *endp,
1135 typval_T *rettv,
1136 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001138 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139{
1140 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001141 listitem_T *ri;
1142 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143
1144 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001145 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001146 cc = *endp;
1147 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001148 if (lp->ll_blob != NULL)
1149 {
1150 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001151
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001152 if (op != NULL && *op != '=')
1153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001154 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001155 return;
1156 }
1157
1158 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1159 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001160 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001161
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001162 if (lp->ll_empty2)
1163 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1164
1165 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001167 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 return;
1169 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001170 if (lp->ll_empty2)
1171 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001172
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001173 ir = 0;
1174 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1175 blob_set(lp->ll_blob, il,
1176 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001177 }
1178 else
1179 {
1180 val = (int)tv_get_number_chk(rettv, &error);
1181 if (!error)
1182 {
1183 garray_T *gap = &lp->ll_blob->bv_ga;
1184
1185 // Allow for appending a byte. Setting a byte beyond
1186 // the end is an error otherwise.
1187 if (lp->ll_n1 < gap->ga_len
1188 || (lp->ll_n1 == gap->ga_len
1189 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1190 {
1191 blob_set(lp->ll_blob, lp->ll_n1, val);
1192 if (lp->ll_n1 == gap->ga_len)
1193 ++gap->ga_len;
1194 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001195 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001196 }
1197 }
1198 }
1199 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001201 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001204 {
1205 emsg(_(e_cannot_mod));
1206 *endp = cc;
1207 return;
1208 }
1209
Bram Moolenaarff697e62019-02-12 22:28:33 +01001210 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001211 di = NULL;
1212 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1213 &tv, &di, TRUE, FALSE) == OK)
1214 {
1215 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001216 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1217 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001218 && tv_op(&tv, rettv, op) == OK)
1219 set_var(lp->ll_name, &tv, FALSE);
1220 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001223 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001225 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001227 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001228 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001229 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001230 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 else if (lp->ll_range)
1232 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001233 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001234 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001237 {
1238 emsg(_("E996: Cannot lock a range"));
1239 return;
1240 }
1241
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001242 /*
1243 * Check whether any of the list items is locked
1244 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001245 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001246 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001247 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001248 return;
1249 ri = ri->li_next;
1250 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1251 break;
1252 ll_li = ll_li->li_next;
1253 ++ll_n1;
1254 }
1255
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 /*
1257 * Assign the List values to the list items.
1258 */
1259 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001260 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001261 if (op != NULL && *op != '=')
1262 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1263 else
1264 {
1265 clear_tv(&lp->ll_li->li_tv);
1266 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1267 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001268 ri = ri->li_next;
1269 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1270 break;
1271 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001272 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001273 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001274 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001275 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 ri = NULL;
1277 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001278 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001279 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 lp->ll_li = lp->ll_li->li_next;
1281 ++lp->ll_n1;
1282 }
1283 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001284 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 else if (lp->ll_empty2
1286 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001288 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001289 }
1290 else
1291 {
1292 /*
1293 * Assign to a List or Dictionary item.
1294 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001296 {
1297 emsg(_("E996: Cannot lock a list or dict"));
1298 return;
1299 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300 if (lp->ll_newkey != NULL)
1301 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 if (op != NULL && *op != '=')
1303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001304 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001305 return;
1306 }
1307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001308 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001309 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 if (di == NULL)
1311 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001312 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1313 {
1314 vim_free(di);
1315 return;
1316 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001318 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001319 else if (op != NULL && *op != '=')
1320 {
1321 tv_op(lp->ll_tv, rettv, op);
1322 return;
1323 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001326
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001327 /*
1328 * Assign the value to the variable or list item.
1329 */
1330 if (copy)
1331 copy_tv(rettv, lp->ll_tv);
1332 else
1333 {
1334 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001335 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 }
1338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339}
1340
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001341/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001342 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1343 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001344 * Returns OK or FAIL.
1345 */
1346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001347tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001348{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001349 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001350 char_u numbuf[NUMBUFLEN];
1351 char_u *s;
1352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001353 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001354 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001355 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001356 {
1357 switch (tv1->v_type)
1358 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001359 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001360 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362 case VAR_DICT:
1363 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001364 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001365 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001366 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001367 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001368 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001369 break;
1370
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001371 case VAR_BLOB:
1372 if (*op != '+' || tv2->v_type != VAR_BLOB)
1373 break;
1374 // BLOB += BLOB
1375 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1376 {
1377 blob_T *b1 = tv1->vval.v_blob;
1378 blob_T *b2 = tv2->vval.v_blob;
1379 int i, len = blob_len(b2);
1380 for (i = 0; i < len; i++)
1381 ga_append(&b1->bv_ga, blob_get(b2, i));
1382 }
1383 return OK;
1384
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001385 case VAR_LIST:
1386 if (*op != '+' || tv2->v_type != VAR_LIST)
1387 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001388 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001389 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1390 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1391 return OK;
1392
1393 case VAR_NUMBER:
1394 case VAR_STRING:
1395 if (tv2->v_type == VAR_LIST)
1396 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001397 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001398 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001399 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001400 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001401#ifdef FEAT_FLOAT
1402 if (tv2->v_type == VAR_FLOAT)
1403 {
1404 float_T f = n;
1405
Bram Moolenaarff697e62019-02-12 22:28:33 +01001406 if (*op == '%')
1407 break;
1408 switch (*op)
1409 {
1410 case '+': f += tv2->vval.v_float; break;
1411 case '-': f -= tv2->vval.v_float; break;
1412 case '*': f *= tv2->vval.v_float; break;
1413 case '/': f /= tv2->vval.v_float; break;
1414 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001415 clear_tv(tv1);
1416 tv1->v_type = VAR_FLOAT;
1417 tv1->vval.v_float = f;
1418 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001419 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001420#endif
1421 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001422 switch (*op)
1423 {
1424 case '+': n += tv_get_number(tv2); break;
1425 case '-': n -= tv_get_number(tv2); break;
1426 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001427 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1428 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001429 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001430 clear_tv(tv1);
1431 tv1->v_type = VAR_NUMBER;
1432 tv1->vval.v_number = n;
1433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 }
1435 else
1436 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001437 if (tv2->v_type == VAR_FLOAT)
1438 break;
1439
Bram Moolenaarff697e62019-02-12 22:28:33 +01001440 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001441 s = tv_get_string(tv1);
1442 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001443 clear_tv(tv1);
1444 tv1->v_type = VAR_STRING;
1445 tv1->vval.v_string = s;
1446 }
1447 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001448
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001449 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001450#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001451 {
1452 float_T f;
1453
Bram Moolenaarff697e62019-02-12 22:28:33 +01001454 if (*op == '%' || *op == '.'
1455 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001456 && tv2->v_type != VAR_NUMBER
1457 && tv2->v_type != VAR_STRING))
1458 break;
1459 if (tv2->v_type == VAR_FLOAT)
1460 f = tv2->vval.v_float;
1461 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001462 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001463 switch (*op)
1464 {
1465 case '+': tv1->vval.v_float += f; break;
1466 case '-': tv1->vval.v_float -= f; break;
1467 case '*': tv1->vval.v_float *= f; break;
1468 case '/': tv1->vval.v_float /= f; break;
1469 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001470 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001471#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001472 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001473 }
1474 }
1475
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001476 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001477 return FAIL;
1478}
1479
1480/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481 * Evaluate the expression used in a ":for var in expr" command.
1482 * "arg" points to "var".
1483 * Set "*errp" to TRUE for an error, FALSE otherwise;
1484 * Return a pointer that holds the info. Null when there is an error.
1485 */
1486 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001487eval_for_line(
1488 char_u *arg,
1489 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001490 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001491 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492{
Bram Moolenaar33570922005-01-25 22:26:29 +00001493 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001494 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001495 typval_T tv;
1496 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001497 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001499 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001501 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001502 if (fi == NULL)
1503 return NULL;
1504
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001505 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506 if (expr == NULL)
1507 return fi;
1508
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001509 expr = skipwhite_and_linebreak(expr, evalarg);
1510 if (expr[0] != 'i' || expr[1] != 'n'
1511 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001512 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514 return fi;
1515 }
1516
1517 if (skip)
1518 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001519 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1520 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521 {
1522 *errp = FALSE;
1523 if (!skip)
1524 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001525 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001526 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001527 l = tv.vval.v_list;
1528 if (l == NULL)
1529 {
1530 // a null list is like an empty list: do nothing
1531 clear_tv(&tv);
1532 }
1533 else
1534 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001536 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001538 // No need to increment the refcount, it's already set for
1539 // the list being used in "tv".
1540 fi->fi_list = l;
1541 list_add_watch(l, &fi->fi_lw);
1542 fi->fi_lw.lw_item = l->lv_first;
1543 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001544 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001545 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001546 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001547 fi->fi_bi = 0;
1548 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001549 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001550 typval_T btv;
1551
1552 // Make a copy, so that the iteration still works when the
1553 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001555 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001556 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001557 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001558 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001559 else
1560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001561 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001562 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563 }
1564 }
1565 }
1566 if (skip)
1567 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001568 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569
1570 return fi;
1571}
1572
1573/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001574 * Used when looping over a :for line, skip the "in expr" part.
1575 */
1576 void
1577skip_for_lines(void *fi_void, evalarg_T *evalarg)
1578{
1579 forinfo_T *fi = (forinfo_T *)fi_void;
1580 int i;
1581
1582 for (i = 0; i < fi->fi_break_count; ++i)
1583 eval_next_line(evalarg);
1584}
1585
1586/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001587 * Use the first item in a ":for" list. Advance to the next.
1588 * Assign the values to the variable (list). "arg" points to the first one.
1589 * Return TRUE when a valid item was found, FALSE when at end of list or
1590 * something wrong.
1591 */
1592 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001593next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001594{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001595 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001596 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001597 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1598 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001599 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001600
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001601 if (fi->fi_blob != NULL)
1602 {
1603 typval_T tv;
1604
1605 if (fi->fi_bi >= blob_len(fi->fi_blob))
1606 return FALSE;
1607 tv.v_type = VAR_NUMBER;
1608 tv.v_lock = VAR_FIXED;
1609 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1610 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001611 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001612 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001613 }
1614
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001615 item = fi->fi_lw.lw_item;
1616 if (item == NULL)
1617 result = FALSE;
1618 else
1619 {
1620 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001621 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001622 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 }
1624 return result;
1625}
1626
1627/*
1628 * Free the structure used to store info used by ":for".
1629 */
1630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001631free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632{
Bram Moolenaar33570922005-01-25 22:26:29 +00001633 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001634
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001635 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001636 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001637 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001638 list_unref(fi->fi_list);
1639 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001640 if (fi != NULL && fi->fi_blob != NULL)
1641 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642 vim_free(fi);
1643}
1644
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646set_context_for_expression(
1647 expand_T *xp,
1648 char_u *arg,
1649 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
1651 int got_eq = FALSE;
1652 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001655 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 {
1657 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001658 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001660 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001661 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001662 {
1663 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001664 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001665 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 break;
1667 }
1668 return;
1669 }
1670 }
1671 else
1672 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1673 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 while ((xp->xp_pattern = vim_strpbrk(arg,
1675 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1676 {
1677 c = *xp->xp_pattern;
1678 if (c == '&')
1679 {
1680 c = xp->xp_pattern[1];
1681 if (c == '&')
1682 {
1683 ++xp->xp_pattern;
1684 xp->xp_context = cmdidx != CMD_let || got_eq
1685 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1686 }
1687 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001688 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001690 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1691 xp->xp_pattern += 2;
1692
1693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
1695 else if (c == '$')
1696 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001697 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 xp->xp_context = EXPAND_ENV_VARS;
1699 }
1700 else if (c == '=')
1701 {
1702 got_eq = TRUE;
1703 xp->xp_context = EXPAND_EXPRESSION;
1704 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001705 else if (c == '#'
1706 && xp->xp_context == EXPAND_EXPRESSION)
1707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001709 break;
1710 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001711 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 && xp->xp_context == EXPAND_FUNCTIONS
1713 && vim_strchr(xp->xp_pattern, '(') == NULL)
1714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001715 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 break;
1717 }
1718 else if (cmdidx != CMD_let || got_eq)
1719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001720 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
1722 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1723 if (c == '\\' && xp->xp_pattern[1] != NUL)
1724 ++xp->xp_pattern;
1725 xp->xp_context = EXPAND_NOTHING;
1726 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001727 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001729 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1731 /* skip */ ;
1732 xp->xp_context = EXPAND_NOTHING;
1733 }
1734 else if (c == '|')
1735 {
1736 if (xp->xp_pattern[1] == '|')
1737 {
1738 ++xp->xp_pattern;
1739 xp->xp_context = EXPAND_EXPRESSION;
1740 }
1741 else
1742 xp->xp_context = EXPAND_COMMANDS;
1743 }
1744 else
1745 xp->xp_context = EXPAND_EXPRESSION;
1746 }
1747 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001748 // Doesn't look like something valid, expand as an expression
1749 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 arg = xp->xp_pattern;
1752 if (*arg != NUL)
1753 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1754 /* skip */ ;
1755 }
1756 xp->xp_pattern = arg;
1757}
1758
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001760 * Return TRUE if "pat" matches "text".
1761 * Does not use 'cpo' and always uses 'magic'.
1762 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001763 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001764pattern_match(char_u *pat, char_u *text, int ic)
1765{
1766 int matches = FALSE;
1767 char_u *save_cpo;
1768 regmatch_T regmatch;
1769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001770 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001771 save_cpo = p_cpo;
1772 p_cpo = (char_u *)"";
1773 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1774 if (regmatch.regprog != NULL)
1775 {
1776 regmatch.rm_ic = ic;
1777 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1778 vim_regfree(regmatch.regprog);
1779 }
1780 p_cpo = save_cpo;
1781 return matches;
1782}
1783
1784/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001785 * Handle a name followed by "(". Both for just "name(arg)" and for
1786 * "expr->name(arg)".
1787 * Returns OK or FAIL.
1788 */
1789 static int
1790eval_func(
1791 char_u **arg, // points to "(", will be advanced
1792 char_u *name,
1793 int name_len,
1794 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001795 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001796 typval_T *basetv) // "expr" for "expr->name(arg)"
1797{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001798 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001799 char_u *s = name;
1800 int len = name_len;
1801 partial_T *partial;
1802 int ret = OK;
1803
1804 if (!evaluate)
1805 check_vars(s, len);
1806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001807 // If "s" is the name of a variable of type VAR_FUNC
1808 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001809 s = deref_func_name(s, &len, &partial, !evaluate);
1810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001811 // Need to make a copy, in case evaluating the arguments makes
1812 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001813 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001814 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001815 ret = FAIL;
1816 else
1817 {
1818 funcexe_T funcexe;
1819
1820 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001821 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001822 funcexe.firstline = curwin->w_cursor.lnum;
1823 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001824 funcexe.evaluate = evaluate;
1825 funcexe.partial = partial;
1826 funcexe.basetv = basetv;
1827 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1828 }
1829 vim_free(s);
1830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001831 // If evaluate is FALSE rettv->v_type was not set in
1832 // get_func_tv, but it's needed in handle_subscript() to parse
1833 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001834 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1835 {
1836 rettv->vval.v_string = NULL;
1837 rettv->v_type = VAR_FUNC;
1838 }
1839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001840 // Stop the expression evaluation when immediately
1841 // aborting on error, or when an interrupt occurred or
1842 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001843 if (evaluate && aborting())
1844 {
1845 if (ret == OK)
1846 clear_tv(rettv);
1847 ret = FAIL;
1848 }
1849 return ret;
1850}
1851
1852/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001853 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1854 * and there is a next line, return the next line (skipping blanks) and set
1855 * "getnext".
1856 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1857 * "arg" must point somewhere inside a line, not at the start.
1858 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001859 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001860eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1861{
1862 *getnext = FALSE;
1863 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1864 && evalarg != NULL
1865 && evalarg->eval_cookie != NULL
1866 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaard5053d02020-06-28 15:51:16 +02001867 && (*arg == '"' || *arg == '#'))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001868 {
Bram Moolenaard5053d02020-06-28 15:51:16 +02001869 char_u *p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001870
1871 if (p != NULL)
1872 {
1873 *getnext = TRUE;
1874 return skipwhite(p);
1875 }
1876 }
1877 return arg;
1878}
1879
1880/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001881 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001882 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001883 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001884eval_next_line(evalarg_T *evalarg)
1885{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001886 garray_T *gap = &evalarg->eval_ga;
1887 char_u *line;
1888
Bram Moolenaard5053d02020-06-28 15:51:16 +02001889 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001890 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001891 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1892 {
1893 // Going to concatenate the lines after parsing.
1894 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1895 ++gap->ga_len;
1896 }
1897 else
1898 {
1899 vim_free(evalarg->eval_tofree);
1900 evalarg->eval_tofree = line;
1901 }
1902 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001903}
1904
Bram Moolenaar9215f012020-06-27 21:18:00 +02001905 char_u *
1906skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1907{
1908 int getnext;
1909 char_u *p = skipwhite(arg);
1910
1911 eval_next_non_blank(p, evalarg, &getnext);
1912 if (getnext)
1913 return eval_next_line(evalarg);
1914 return p;
1915}
1916
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001917/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001918 * After using "evalarg" filled from "eap" free the memory.
1919 */
1920 void
1921clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1922{
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001923 if (evalarg != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001924 {
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001925 if (eap != NULL)
1926 {
1927 // We may need to keep the original command line, e.g. for
1928 // ":let" it has the variable names. But we may also need the
1929 // new one, "nextcmd" points into it. Keep both.
1930 vim_free(eap->cmdline_tofree);
1931 eap->cmdline_tofree = *eap->cmdlinep;
1932 *eap->cmdlinep = evalarg->eval_tofree;
1933 }
1934 else
1935 vim_free(evalarg->eval_tofree);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001936 evalarg->eval_tofree = NULL;
1937 }
1938}
1939
1940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1944 */
1945
1946/*
1947 * Handle zero level expression.
1948 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001950 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001951 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 * Return OK or FAIL.
1953 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001955eval0(
1956 char_u *arg,
1957 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001958 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001959 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960{
1961 int ret;
1962 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001963 int did_emsg_before = did_emsg;
1964 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001965 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
1967 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001968 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001969
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001970 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 /*
1975 * Report the invalid expression unless the expression evaluation has
1976 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001977 * exception, or we already gave a more specific error.
1978 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001980 if (!aborting()
1981 && did_emsg == did_emsg_before
1982 && called_emsg == called_emsg_before
1983 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001984 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 ret = FAIL;
1986 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001987
1988 if (eap != NULL)
1989 eap->nextcmd = check_nextcmd(p);
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 return ret;
1992}
1993
1994/*
1995 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001996 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 *
1998 * "arg" must point to the first non-white of the expression.
1999 * "arg" is advanced to the next non-white after the recognized expression.
2000 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002001 * Note: "rettv.v_lock" is not set.
2002 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 * Return OK or FAIL.
2004 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002005 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002006eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002008 char_u *p;
2009 int getnext;
2010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 /*
2012 * Get the first variable.
2013 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002014 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 return FAIL;
2016
Bram Moolenaar793648f2020-06-26 21:28:25 +02002017 p = eval_next_non_blank(*arg, evalarg, &getnext);
2018 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002020 int result;
2021 typval_T var2;
2022 evalarg_T nested_evalarg;
2023 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002024 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002025
Bram Moolenaar793648f2020-06-26 21:28:25 +02002026 if (getnext)
2027 *arg = eval_next_line(evalarg);
2028
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002029 if (evalarg == NULL)
2030 {
2031 CLEAR_FIELD(nested_evalarg);
2032 orig_flags = 0;
2033 }
2034 else
2035 {
2036 nested_evalarg = *evalarg;
2037 orig_flags = evalarg->eval_flags;
2038 }
2039
Bram Moolenaar7acde512020-06-24 23:02:40 +02002040 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002042 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002044 int error = FALSE;
2045
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002046 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002049 if (error)
2050 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052
2053 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002054 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002056 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002057 nested_evalarg.eval_flags = result ? orig_flags
2058 : orig_flags & ~EVAL_EVALUATE;
2059 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 return FAIL;
2061
2062 /*
2063 * Check for the ":".
2064 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002065 p = eval_next_non_blank(*arg, evalarg, &getnext);
2066 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 return FAIL;
2072 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002073 if (getnext)
2074 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
2076 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002077 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002079 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002080 nested_evalarg.eval_flags = !result ? orig_flags
2081 : orig_flags & ~EVAL_EVALUATE;
2082 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002085 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 return FAIL;
2087 }
2088 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002089 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 }
2091
2092 return OK;
2093}
2094
2095/*
2096 * Handle first level expression:
2097 * expr2 || expr2 || expr2 logical OR
2098 *
2099 * "arg" must point to the first non-white of the expression.
2100 * "arg" is advanced to the next non-white after the recognized expression.
2101 *
2102 * Return OK or FAIL.
2103 */
2104 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002105eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002107 char_u *p;
2108 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002109 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 long result;
2111 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002112 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114 /*
2115 * Get the first variable.
2116 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002117 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 return FAIL;
2119
2120 /*
2121 * Repeat until there is no following "||".
2122 */
2123 first = TRUE;
2124 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002125 p = eval_next_non_blank(*arg, evalarg, &getnext);
2126 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002128 evalarg_T nested_evalarg;
2129 int evaluate;
2130 int orig_flags;
2131
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002132 if (getnext)
2133 *arg = eval_next_line(evalarg);
2134
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002135 if (evalarg == NULL)
2136 {
2137 CLEAR_FIELD(nested_evalarg);
2138 orig_flags = 0;
2139 evaluate = FALSE;
2140 }
2141 else
2142 {
2143 nested_evalarg = *evalarg;
2144 orig_flags = evalarg->eval_flags;
2145 evaluate = orig_flags & EVAL_EVALUATE;
2146 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002147
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (evaluate && first)
2149 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002150 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002153 if (error)
2154 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 first = FALSE;
2156 }
2157
2158 /*
2159 * Get the second variable.
2160 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002161 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002162 nested_evalarg.eval_flags = !result ? orig_flags
2163 : orig_flags & ~EVAL_EVALUATE;
2164 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 return FAIL;
2166
2167 /*
2168 * Compute the result.
2169 */
2170 if (evaluate && !result)
2171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002172 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002175 if (error)
2176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178 if (evaluate)
2179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 rettv->v_type = VAR_NUMBER;
2181 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002183
2184 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186
2187 return OK;
2188}
2189
2190/*
2191 * Handle second level expression:
2192 * expr3 && expr3 && expr3 logical AND
2193 *
2194 * "arg" must point to the first non-white of the expression.
2195 * "arg" is advanced to the next non-white after the recognized expression.
2196 *
2197 * Return OK or FAIL.
2198 */
2199 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002200eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002202 char_u *p;
2203 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002204 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 long result;
2206 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002207 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
2209 /*
2210 * Get the first variable.
2211 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002212 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 return FAIL;
2214
2215 /*
2216 * Repeat until there is no following "&&".
2217 */
2218 first = TRUE;
2219 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002220 p = eval_next_non_blank(*arg, evalarg, &getnext);
2221 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002223 evalarg_T nested_evalarg;
2224 int orig_flags;
2225 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002226
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002227 if (getnext)
2228 *arg = eval_next_line(evalarg);
2229
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002230 if (evalarg == NULL)
2231 {
2232 CLEAR_FIELD(nested_evalarg);
2233 orig_flags = 0;
2234 evaluate = FALSE;
2235 }
2236 else
2237 {
2238 nested_evalarg = *evalarg;
2239 orig_flags = evalarg->eval_flags;
2240 evaluate = orig_flags & EVAL_EVALUATE;
2241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (evaluate && first)
2243 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002244 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002247 if (error)
2248 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 first = FALSE;
2250 }
2251
2252 /*
2253 * Get the second variable.
2254 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002255 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002256 nested_evalarg.eval_flags = result ? orig_flags
2257 : orig_flags & ~EVAL_EVALUATE;
2258 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 return FAIL;
2260
2261 /*
2262 * Compute the result.
2263 */
2264 if (evaluate && result)
2265 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002266 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002269 if (error)
2270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272 if (evaluate)
2273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 rettv->v_type = VAR_NUMBER;
2275 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002277
2278 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280
2281 return OK;
2282}
2283
2284/*
2285 * Handle third level expression:
2286 * var1 == var2
2287 * var1 =~ var2
2288 * var1 != var2
2289 * var1 !~ var2
2290 * var1 > var2
2291 * var1 >= var2
2292 * var1 < var2
2293 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002294 * var1 is var2
2295 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 *
2297 * "arg" must point to the first non-white of the expression.
2298 * "arg" is advanced to the next non-white after the recognized expression.
2299 *
2300 * Return OK or FAIL.
2301 */
2302 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
Bram Moolenaar33570922005-01-25 22:26:29 +00002305 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002307 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002309 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
2313 /*
2314 * Get the first variable.
2315 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002316 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 return FAIL;
2318
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002319 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 switch (p[0])
2321 {
2322 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002323 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002325 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 break;
2327 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002328 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002330 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 break;
2332 case '>': if (p[1] != '=')
2333 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002334 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 len = 1;
2336 }
2337 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002338 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 break;
2340 case '<': if (p[1] != '=')
2341 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002342 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 len = 1;
2344 }
2345 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002346 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002348 case 'i': if (p[1] == 's')
2349 {
2350 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2351 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002352 i = p[len];
2353 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002354 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002355 }
2356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358
2359 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002360 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002362 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002364 if (getnext)
2365 *arg = eval_next_line(evalarg);
2366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002367 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (p[len] == '?')
2369 {
2370 ic = TRUE;
2371 ++len;
2372 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002373 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 else if (p[len] == '#')
2375 {
2376 ic = FALSE;
2377 ++len;
2378 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002379 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 else
2381 ic = p_ic;
2382
2383 /*
2384 * Get the second variable.
2385 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002386 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002387 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 return FAIL;
2391 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002392 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002393 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002394 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002395
2396 clear_tv(&var2);
2397 return ret;
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400
2401 return OK;
2402}
2403
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 void
2405eval_addblob(typval_T *tv1, typval_T *tv2)
2406{
2407 blob_T *b1 = tv1->vval.v_blob;
2408 blob_T *b2 = tv2->vval.v_blob;
2409 blob_T *b = blob_alloc();
2410 int i;
2411
2412 if (b != NULL)
2413 {
2414 for (i = 0; i < blob_len(b1); i++)
2415 ga_append(&b->bv_ga, blob_get(b1, i));
2416 for (i = 0; i < blob_len(b2); i++)
2417 ga_append(&b->bv_ga, blob_get(b2, i));
2418
2419 clear_tv(tv1);
2420 rettv_blob_set(tv1, b);
2421 }
2422}
2423
2424 int
2425eval_addlist(typval_T *tv1, typval_T *tv2)
2426{
2427 typval_T var3;
2428
2429 // concatenate Lists
2430 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2431 {
2432 clear_tv(tv1);
2433 clear_tv(tv2);
2434 return FAIL;
2435 }
2436 clear_tv(tv1);
2437 *tv1 = var3;
2438 return OK;
2439}
2440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441/*
2442 * Handle fourth level expression:
2443 * + number addition
2444 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002445 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002446 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 *
2448 * "arg" must point to the first non-white of the expression.
2449 * "arg" is advanced to the next non-white after the recognized expression.
2450 *
2451 * Return OK or FAIL.
2452 */
2453 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002454eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458 /*
2459 * Get the first variable.
2460 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002461 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 return FAIL;
2463
2464 /*
2465 * Repeat computing, until no '+', '-' or '.' is following.
2466 */
2467 for (;;)
2468 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 int getnext;
2470 char_u *p;
2471 int op;
2472 int concat;
2473 typval_T var2;
2474
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002475 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476 p = eval_next_non_blank(*arg, evalarg, &getnext);
2477 op = *p;
2478 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002479 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002481 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002482 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002484 if ((op != '+' || (rettv->v_type != VAR_LIST
2485 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002486#ifdef FEAT_FLOAT
2487 && (op == '.' || rettv->v_type != VAR_FLOAT)
2488#endif
2489 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002490 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002491 // For "list + ...", an illegal use of the first operand as
2492 // a number cannot be determined before evaluating the 2nd
2493 // operand: if this is also a list, all is ok.
2494 // For "something . ...", "something - ..." or "non-list + ...",
2495 // we know that the first operand needs to be a string or number
2496 // without evaluating the 2nd operand. So check before to avoid
2497 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002498 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 {
2500 clear_tv(rettv);
2501 return FAIL;
2502 }
2503 }
2504
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 /*
2506 * Get the second variable.
2507 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002508 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2509 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002510 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002511 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 return FAIL;
2515 }
2516
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002517 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
2519 /*
2520 * Compute the result.
2521 */
2522 if (op == '.')
2523 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002524 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2525 char_u *s1 = tv_get_string_buf(rettv, buf1);
2526 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002528 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002529 {
2530 clear_tv(rettv);
2531 clear_tv(&var2);
2532 return FAIL;
2533 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 clear_tv(rettv);
2536 rettv->v_type = VAR_STRING;
2537 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002539 else if (op == '+' && rettv->v_type == VAR_BLOB
2540 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002542 else if (op == '+' && rettv->v_type == VAR_LIST
2543 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002544 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002546 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 else
2549 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002550 int error = FALSE;
2551 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002552#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002553 float_T f1 = 0, f2 = 0;
2554
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002555 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002556 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002557 f1 = rettv->vval.v_float;
2558 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002559 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002560 else
2561#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002562 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002563 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002564 if (error)
2565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002566 // This can only happen for "list + non-list". For
2567 // "non-list + ..." or "something - ...", we returned
2568 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002569 clear_tv(rettv);
2570 return FAIL;
2571 }
2572#ifdef FEAT_FLOAT
2573 if (var2.v_type == VAR_FLOAT)
2574 f1 = n1;
2575#endif
2576 }
2577#ifdef FEAT_FLOAT
2578 if (var2.v_type == VAR_FLOAT)
2579 {
2580 f2 = var2.vval.v_float;
2581 n2 = 0;
2582 }
2583 else
2584#endif
2585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002586 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002587 if (error)
2588 {
2589 clear_tv(rettv);
2590 clear_tv(&var2);
2591 return FAIL;
2592 }
2593#ifdef FEAT_FLOAT
2594 if (rettv->v_type == VAR_FLOAT)
2595 f2 = n2;
2596#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002599
2600#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002601 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002602 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2603 {
2604 if (op == '+')
2605 f1 = f1 + f2;
2606 else
2607 f1 = f1 - f2;
2608 rettv->v_type = VAR_FLOAT;
2609 rettv->vval.v_float = f1;
2610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002612#endif
2613 {
2614 if (op == '+')
2615 n1 = n1 + n2;
2616 else
2617 n1 = n1 - n2;
2618 rettv->v_type = VAR_NUMBER;
2619 rettv->vval.v_number = n1;
2620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624 }
2625 return OK;
2626}
2627
2628/*
2629 * Handle fifth level expression:
2630 * * number multiplication
2631 * / number division
2632 * % number modulo
2633 *
2634 * "arg" must point to the first non-white of the expression.
2635 * "arg" is advanced to the next non-white after the recognized expression.
2636 *
2637 * Return OK or FAIL.
2638 */
2639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002640eval6(
2641 char_u **arg,
2642 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002643 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002644 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002648 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002649#ifdef FEAT_FLOAT
2650 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002651 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002652#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002653 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
2655 /*
2656 * Get the first variable.
2657 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002658 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 return FAIL;
2660
2661 /*
2662 * Repeat computing, until no '*', '/' or '%' is following.
2663 */
2664 for (;;)
2665 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002666 int evaluate = evalarg == NULL ? 0
2667 : (evalarg->eval_flags & EVAL_EVALUATE);
2668 int getnext;
2669
2670 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002671 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002674 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002676 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002678#ifdef FEAT_FLOAT
2679 if (rettv->v_type == VAR_FLOAT)
2680 {
2681 f1 = rettv->vval.v_float;
2682 use_float = TRUE;
2683 n1 = 0;
2684 }
2685 else
2686#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002687 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002688 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002689 if (error)
2690 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692 else
2693 n1 = 0;
2694
2695 /*
2696 * Get the second variable.
2697 */
2698 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002699 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return FAIL;
2701
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002702 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002704#ifdef FEAT_FLOAT
2705 if (var2.v_type == VAR_FLOAT)
2706 {
2707 if (!use_float)
2708 {
2709 f1 = n1;
2710 use_float = TRUE;
2711 }
2712 f2 = var2.vval.v_float;
2713 n2 = 0;
2714 }
2715 else
2716#endif
2717 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002718 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002719 clear_tv(&var2);
2720 if (error)
2721 return FAIL;
2722#ifdef FEAT_FLOAT
2723 if (use_float)
2724 f2 = n2;
2725#endif
2726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 /*
2729 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002730 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002732#ifdef FEAT_FLOAT
2733 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002735 if (op == '*')
2736 f1 = f1 * f2;
2737 else if (op == '/')
2738 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002739# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002740 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002741 if (f2 == 0.0)
2742 {
2743 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002744 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002745 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002746 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002747 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002748 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002749 }
2750 else
2751 f1 = f1 / f2;
2752# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002753 // We rely on the floating point library to handle divide
2754 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002755 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002756# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002759 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002761 return FAIL;
2762 }
2763 rettv->v_type = VAR_FLOAT;
2764 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
2766 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002769 if (op == '*')
2770 n1 = n1 * n2;
2771 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002772 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002774 n1 = num_modulus(n1, n2);
2775
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002776 rettv->v_type = VAR_NUMBER;
2777 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 }
2781
2782 return OK;
2783}
2784
2785/*
2786 * Handle sixth level expression:
2787 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002788 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002789 * "string" string constant
2790 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 * &option-name option value
2792 * @r register contents
2793 * identifier variable value
2794 * function() function call
2795 * $VAR environment variable
2796 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002797 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002799 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002800 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 *
2802 * Also handle:
2803 * ! in front logical NOT
2804 * - in front unary minus
2805 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 * trailing [] subscript in String or List
2807 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002808 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 *
2810 * "arg" must point to the first non-white of the expression.
2811 * "arg" is advanced to the next non-white after the recognized expression.
2812 *
2813 * Return OK or FAIL.
2814 */
2815 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816eval7(
2817 char_u **arg,
2818 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002822 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2823 int evaluate = evalarg != NULL
2824 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 int len;
2826 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 char_u *start_leader, *end_leader;
2828 int ret = OK;
2829 char_u *alias;
2830
2831 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002832 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002833 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002835 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836
2837 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002838 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 */
2840 start_leader = *arg;
2841 while (**arg == '!' || **arg == '-' || **arg == '+')
2842 *arg = skipwhite(*arg + 1);
2843 end_leader = *arg;
2844
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002845 if (**arg == '.' && (!isdigit(*(*arg + 1))
2846#ifdef FEAT_FLOAT
2847 || current_sctx.sc_version < 2
2848#endif
2849 ))
2850 {
2851 semsg(_(e_invexpr2), *arg);
2852 ++*arg;
2853 return FAIL;
2854 }
2855
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 switch (**arg)
2857 {
2858 /*
2859 * Number constant.
2860 */
2861 case '0':
2862 case '1':
2863 case '2':
2864 case '3':
2865 case '4':
2866 case '5':
2867 case '6':
2868 case '7':
2869 case '8':
2870 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002872
2873 // Apply prefixed "-" and "+" now. Matters especially when
2874 // "->" follows.
2875 if (ret == OK && evaluate && end_leader > start_leader)
2876 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 break;
2878
2879 /*
2880 * String constant: "string".
2881 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002882 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 break;
2884
2885 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002889 break;
2890
2891 /*
2892 * List: [expr, expr]
2893 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002894 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 break;
2896
2897 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002898 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002899 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002900 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002901 {
2902 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002903 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002904 }
2905 else
2906 ret = NOTDONE;
2907 break;
2908
2909 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002910 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002911 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002912 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002913 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002914 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002915 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 break;
2917
2918 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002919 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002921 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 break;
2923
2924 /*
2925 * Environment variable: $VAR.
2926 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 break;
2929
2930 /*
2931 * Register contents: @r.
2932 */
2933 case '@': ++*arg;
2934 if (evaluate)
2935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002936 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002937 rettv->vval.v_string = get_reg_contents(**arg,
2938 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
2940 if (**arg != NUL)
2941 ++*arg;
2942 break;
2943
2944 /*
2945 * nested expression: (expression).
2946 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002947 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002948 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002949 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002950
Bram Moolenaar9215f012020-06-27 21:18:00 +02002951 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002952 if (**arg == ')')
2953 ++*arg;
2954 else if (ret == OK)
2955 {
2956 emsg(_(e_missing_close));
2957 clear_tv(rettv);
2958 ret = FAIL;
2959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961 break;
2962
Bram Moolenaar8c711452005-01-14 21:53:12 +00002963 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 break;
2965 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002966
2967 if (ret == NOTDONE)
2968 {
2969 /*
2970 * Must be a variable or function name.
2971 * Can also be a curly-braces kind of name: {expr}.
2972 */
2973 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002974 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002975 if (alias != NULL)
2976 s = alias;
2977
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002978 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002979 ret = FAIL;
2980 else
2981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002982 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002983 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002984 else if (flags & EVAL_CONSTANT)
2985 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002986 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002987 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002988 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002989 {
2990 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002993 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002994 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002995 }
2996
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 *arg = skipwhite(*arg);
2998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002999 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3000 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003001 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003002 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 /*
3005 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3006 */
3007 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003008 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003009 return ret;
3010}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003012/*
3013 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003014 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003015 * Adjusts "end_leaderp" until it is at "start_leader".
3016 */
3017 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003018eval7_leader(
3019 typval_T *rettv,
3020 int numeric_only,
3021 char_u *start_leader,
3022 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003023{
3024 char_u *end_leader = *end_leaderp;
3025 int ret = OK;
3026 int error = FALSE;
3027 varnumber_T val = 0;
3028#ifdef FEAT_FLOAT
3029 float_T f = 0.0;
3030
3031 if (rettv->v_type == VAR_FLOAT)
3032 f = rettv->vval.v_float;
3033 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003035 val = tv_get_number_chk(rettv, &error);
3036 if (error)
3037 {
3038 clear_tv(rettv);
3039 ret = FAIL;
3040 }
3041 else
3042 {
3043 while (end_leader > start_leader)
3044 {
3045 --end_leader;
3046 if (*end_leader == '!')
3047 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003048 if (numeric_only)
3049 {
3050 ++end_leader;
3051 break;
3052 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003053#ifdef FEAT_FLOAT
3054 if (rettv->v_type == VAR_FLOAT)
3055 f = !f;
3056 else
3057#endif
3058 val = !val;
3059 }
3060 else if (*end_leader == '-')
3061 {
3062#ifdef FEAT_FLOAT
3063 if (rettv->v_type == VAR_FLOAT)
3064 f = -f;
3065 else
3066#endif
3067 val = -val;
3068 }
3069 }
3070#ifdef FEAT_FLOAT
3071 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003073 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003074 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003076 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003077#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003078 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003079 clear_tv(rettv);
3080 rettv->v_type = VAR_NUMBER;
3081 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003084 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 return ret;
3086}
3087
3088/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003089 * Call the function referred to in "rettv".
3090 */
3091 static int
3092call_func_rettv(
3093 char_u **arg,
3094 typval_T *rettv,
3095 int evaluate,
3096 dict_T *selfdict,
3097 typval_T *basetv)
3098{
3099 partial_T *pt = NULL;
3100 funcexe_T funcexe;
3101 typval_T functv;
3102 char_u *s;
3103 int ret;
3104
3105 // need to copy the funcref so that we can clear rettv
3106 if (evaluate)
3107 {
3108 functv = *rettv;
3109 rettv->v_type = VAR_UNKNOWN;
3110
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003111 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003112 if (functv.v_type == VAR_PARTIAL)
3113 {
3114 pt = functv.vval.v_partial;
3115 s = partial_name(pt);
3116 }
3117 else
3118 s = functv.vval.v_string;
3119 }
3120 else
3121 s = (char_u *)"";
3122
Bram Moolenaara80faa82020-04-12 19:37:17 +02003123 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003124 funcexe.firstline = curwin->w_cursor.lnum;
3125 funcexe.lastline = curwin->w_cursor.lnum;
3126 funcexe.evaluate = evaluate;
3127 funcexe.partial = pt;
3128 funcexe.selfdict = selfdict;
3129 funcexe.basetv = basetv;
3130 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003132 // Clear the funcref afterwards, so that deleting it while
3133 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003134 if (evaluate)
3135 clear_tv(&functv);
3136
3137 return ret;
3138}
3139
3140/*
3141 * Evaluate "->method()".
3142 * "*arg" points to the '-'.
3143 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3144 */
3145 static int
3146eval_lambda(
3147 char_u **arg,
3148 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003149 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003150 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003151{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003152 int evaluate = evalarg != NULL
3153 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003154 typval_T base = *rettv;
3155 int ret;
3156
3157 // Skip over the ->.
3158 *arg += 2;
3159 rettv->v_type = VAR_UNKNOWN;
3160
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003161 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003162 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003163 return FAIL;
3164 else if (**arg != '(')
3165 {
3166 if (verbose)
3167 {
3168 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003169 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003170 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003172 }
3173 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003174 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003175 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003176 else
3177 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3178
3179 // Clear the funcref afterwards, so that deleting it while
3180 // evaluating the arguments is possible (see test55).
3181 if (evaluate)
3182 clear_tv(&base);
3183
3184 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003185}
3186
3187/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003188 * Evaluate "->method()".
3189 * "*arg" points to the '-'.
3190 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3191 */
3192 static int
3193eval_method(
3194 char_u **arg,
3195 typval_T *rettv,
3196 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003197 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003198{
3199 char_u *name;
3200 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003201 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003202 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003203 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003204
3205 // Skip over the ->.
3206 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003207 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003208
Bram Moolenaarac92e252019-08-03 21:58:38 +02003209 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003210 len = get_name_len(arg, &alias, evaluate, TRUE);
3211 if (alias != NULL)
3212 name = alias;
3213
3214 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003215 {
3216 if (verbose)
3217 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003218 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003219 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003220 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003221 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003222 if (**arg != '(')
3223 {
3224 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003226 ret = FAIL;
3227 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003228 else if (VIM_ISWHITE((*arg)[-1]))
3229 {
3230 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003231 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003232 ret = FAIL;
3233 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003234 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003235 ret = eval_func(arg, name, len, rettv,
3236 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003237 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003238
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003239 // Clear the funcref afterwards, so that deleting it while
3240 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003241 if (evaluate)
3242 clear_tv(&base);
3243
Bram Moolenaarac92e252019-08-03 21:58:38 +02003244 return ret;
3245}
3246
3247/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003248 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3249 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003250 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3251 */
3252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253eval_index(
3254 char_u **arg,
3255 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003256 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003258{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003259 int evaluate = evalarg != NULL
3260 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003261 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003262 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003263 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003264 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003265 long len = -1;
3266 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003267 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003268 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003269
Bram Moolenaara03f2332016-02-06 18:09:59 +01003270 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003271 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003272 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003273 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003274 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003275 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003276 return FAIL;
3277 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003278#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003279 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003280 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003281 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003282#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003283 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003284 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003285 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003286 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003287 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003288 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003289 return FAIL;
3290 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003291 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003293 if (evaluate)
3294 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003295 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003296
3297 case VAR_STRING:
3298 case VAR_NUMBER:
3299 case VAR_LIST:
3300 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003301 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003302 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003303 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003304
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003305 init_tv(&var1);
3306 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003307 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003308 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003309 /*
3310 * dict.name
3311 */
3312 key = *arg + 1;
3313 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3314 ;
3315 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003316 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003317 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003318 }
3319 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003320 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003321 /*
3322 * something[idx]
3323 *
3324 * Get the (first) variable from inside the [].
3325 */
3326 *arg = skipwhite(*arg + 1);
3327 if (**arg == ':')
3328 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003329 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003330 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003331 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003333 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003334 clear_tv(&var1);
3335 return FAIL;
3336 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003337
3338 /*
3339 * Get the second variable from inside the [:].
3340 */
3341 if (**arg == ':')
3342 {
3343 range = TRUE;
3344 *arg = skipwhite(*arg + 1);
3345 if (**arg == ']')
3346 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003347 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349 if (!empty1)
3350 clear_tv(&var1);
3351 return FAIL;
3352 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003353 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003354 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003355 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003356 if (!empty1)
3357 clear_tv(&var1);
3358 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003359 return FAIL;
3360 }
3361 }
3362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003363 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003364 if (**arg != ']')
3365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003366 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003367 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003368 clear_tv(&var1);
3369 if (range)
3370 clear_tv(&var2);
3371 return FAIL;
3372 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003373 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003374 }
3375
3376 if (evaluate)
3377 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003378 n1 = 0;
3379 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003381 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003382 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003383 }
3384 if (range)
3385 {
3386 if (empty2)
3387 n2 = -1;
3388 else
3389 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003390 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003391 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003392 }
3393 }
3394
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003395 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003396 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003397 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003398 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003400 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003401 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003402 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003403 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003404 case VAR_SPECIAL:
3405 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003406 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003407 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003408
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003409 case VAR_NUMBER:
3410 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003411 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003412 len = (long)STRLEN(s);
3413 if (range)
3414 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003415 // The resulting variable is a substring. If the indexes
3416 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003417 if (n1 < 0)
3418 {
3419 n1 = len + n1;
3420 if (n1 < 0)
3421 n1 = 0;
3422 }
3423 if (n2 < 0)
3424 n2 = len + n2;
3425 else if (n2 >= len)
3426 n2 = len;
3427 if (n1 >= len || n2 < 0 || n1 > n2)
3428 s = NULL;
3429 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003430 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003431 }
3432 else
3433 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003434 // The resulting variable is a string of a single
3435 // character. If the index is too big or negative the
3436 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003437 if (n1 >= len || n1 < 0)
3438 s = NULL;
3439 else
3440 s = vim_strnsave(s + n1, 1);
3441 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003442 clear_tv(rettv);
3443 rettv->v_type = VAR_STRING;
3444 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003445 break;
3446
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003447 case VAR_BLOB:
3448 len = blob_len(rettv->vval.v_blob);
3449 if (range)
3450 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003451 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003452 // are out of range the result is empty.
3453 if (n1 < 0)
3454 {
3455 n1 = len + n1;
3456 if (n1 < 0)
3457 n1 = 0;
3458 }
3459 if (n2 < 0)
3460 n2 = len + n2;
3461 else if (n2 >= len)
3462 n2 = len - 1;
3463 if (n1 >= len || n2 < 0 || n1 > n2)
3464 {
3465 clear_tv(rettv);
3466 rettv->v_type = VAR_BLOB;
3467 rettv->vval.v_blob = NULL;
3468 }
3469 else
3470 {
3471 blob_T *blob = blob_alloc();
3472
3473 if (blob != NULL)
3474 {
3475 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3476 {
3477 blob_free(blob);
3478 return FAIL;
3479 }
3480 blob->bv_ga.ga_len = n2 - n1 + 1;
3481 for (i = n1; i <= n2; i++)
3482 blob_set(blob, i - n1,
3483 blob_get(rettv->vval.v_blob, i));
3484
3485 clear_tv(rettv);
3486 rettv_blob_set(rettv, blob);
3487 }
3488 }
3489 }
3490 else
3491 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003492 // The resulting variable is a byte value.
3493 // If the index is too big or negative that is an error.
3494 if (n1 < 0)
3495 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003496 if (n1 < len && n1 >= 0)
3497 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003498 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003499
3500 clear_tv(rettv);
3501 rettv->v_type = VAR_NUMBER;
3502 rettv->vval.v_number = v;
3503 }
3504 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003505 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003506 }
3507 break;
3508
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003509 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003510 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003511 if (n1 < 0)
3512 n1 = len + n1;
3513 if (!empty1 && (n1 < 0 || n1 >= len))
3514 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003515 // For a range we allow invalid values and return an empty
3516 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003517 if (!range)
3518 {
3519 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003520 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003521 return FAIL;
3522 }
3523 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003524 }
3525 if (range)
3526 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003527 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003528
3529 if (n2 < 0)
3530 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003531 else if (n2 >= len)
3532 n2 = len - 1;
3533 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003534 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003535 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003536 if (l == NULL)
3537 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003538 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003539 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 }
3541 else
3542 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003543 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 clear_tv(rettv);
3545 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 }
3547 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003548
3549 case VAR_DICT:
3550 if (range)
3551 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003552 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003553 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003554 if (len == -1)
3555 clear_tv(&var1);
3556 return FAIL;
3557 }
3558 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003559 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003560
3561 if (len == -1)
3562 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003563 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003564 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003565 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003566 clear_tv(&var1);
3567 return FAIL;
3568 }
3569 }
3570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003571 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003572
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003573 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003574 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003575 if (len == -1)
3576 clear_tv(&var1);
3577 if (item == NULL)
3578 return FAIL;
3579
3580 copy_tv(&item->di_tv, &var1);
3581 clear_tv(rettv);
3582 *rettv = var1;
3583 }
3584 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003585 }
3586 }
3587
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003588 return OK;
3589}
3590
3591/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003592 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003593 */
3594 char_u *
3595partial_name(partial_T *pt)
3596{
3597 if (pt->pt_name != NULL)
3598 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003599 if (pt->pt_func != NULL)
3600 return pt->pt_func->uf_name;
3601 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003602}
3603
Bram Moolenaarddecc252016-04-06 22:59:37 +02003604 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003605partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003606{
3607 int i;
3608
3609 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003610 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003611 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003612 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003613 if (pt->pt_name != NULL)
3614 {
3615 func_unref(pt->pt_name);
3616 vim_free(pt->pt_name);
3617 }
3618 else
3619 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003620
3621 if (pt->pt_funcstack != NULL)
3622 {
3623 // Decrease the reference count for the context of a closure. If down
3624 // to zero free it and clear the variables on the stack.
3625 if (--pt->pt_funcstack->fs_refcount == 0)
3626 {
3627 garray_T *gap = &pt->pt_funcstack->fs_ga;
3628 typval_T *stack = gap->ga_data;
3629
3630 for (i = 0; i < gap->ga_len; ++i)
3631 clear_tv(stack + i);
3632 ga_clear(gap);
3633 vim_free(pt->pt_funcstack);
3634 }
3635 pt->pt_funcstack = NULL;
3636 }
3637
Bram Moolenaarddecc252016-04-06 22:59:37 +02003638 vim_free(pt);
3639}
3640
3641/*
3642 * Unreference a closure: decrement the reference count and free it when it
3643 * becomes zero.
3644 */
3645 void
3646partial_unref(partial_T *pt)
3647{
3648 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003649 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003650}
3651
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003652/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003653 * Return the next (unique) copy ID.
3654 * Used for serializing nested structures.
3655 */
3656 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003658{
3659 current_copyID += COPYID_INC;
3660 return current_copyID;
3661}
3662
3663/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003664 * Garbage collection for lists and dictionaries.
3665 *
3666 * We use reference counts to be able to free most items right away when they
3667 * are no longer used. But for composite items it's possible that it becomes
3668 * unused while the reference count is > 0: When there is a recursive
3669 * reference. Example:
3670 * :let l = [1, 2, 3]
3671 * :let d = {9: l}
3672 * :let l[1] = d
3673 *
3674 * Since this is quite unusual we handle this with garbage collection: every
3675 * once in a while find out which lists and dicts are not referenced from any
3676 * variable.
3677 *
3678 * Here is a good reference text about garbage collection (refers to Python
3679 * but it applies to all reference-counting mechanisms):
3680 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003681 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003682
3683/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003684 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003685 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003686 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003687 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003688 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003689garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003690{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003691 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003692 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003693 buf_T *buf;
3694 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003695 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003696 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003697
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003698 if (!testing)
3699 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003700 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003701 want_garbage_collect = FALSE;
3702 may_garbage_collect = FALSE;
3703 garbage_collect_at_exit = FALSE;
3704 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003705
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003706 // The execution stack can grow big, limit the size.
3707 if (exestack.ga_maxlen - exestack.ga_len > 500)
3708 {
3709 size_t new_len;
3710 char_u *pp;
3711 int n;
3712
3713 // Keep 150% of the current size, with a minimum of the growth size.
3714 n = exestack.ga_len / 2;
3715 if (n < exestack.ga_growsize)
3716 n = exestack.ga_growsize;
3717
3718 // Don't make it bigger though.
3719 if (exestack.ga_len + n < exestack.ga_maxlen)
3720 {
3721 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3722 pp = vim_realloc(exestack.ga_data, new_len);
3723 if (pp == NULL)
3724 return FAIL;
3725 exestack.ga_maxlen = exestack.ga_len + n;
3726 exestack.ga_data = pp;
3727 }
3728 }
3729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003730 // We advance by two because we add one for items referenced through
3731 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003732 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003733
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003734 /*
3735 * 1. Go through all accessible variables and mark all lists and dicts
3736 * with copyID.
3737 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003738
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003739 // Don't free variables in the previous_funccal list unless they are only
3740 // referenced through previous_funccal. This must be first, because if
3741 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003743
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003744 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003745 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003746
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003747 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003748 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003749 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3750 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003753 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003754 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3755 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003756 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003757 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3758 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003759#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003760 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003761 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3762 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003763 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003764 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003765 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3766 NULL, NULL);
3767#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003769 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003770 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003771 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3772 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003773 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003774 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003776 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003777 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003779 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003780 abort = abort || set_ref_in_functions(copyID);
3781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003782 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003785 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003786 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003787
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003788 // callbacks in buffers
3789 abort = abort || set_ref_in_buffers(copyID);
3790
Bram Moolenaar1dced572012-04-05 16:54:08 +02003791#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003792 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003793#endif
3794
Bram Moolenaardb913952012-06-29 12:54:53 +02003795#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003796 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003797#endif
3798
3799#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003800 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003801#endif
3802
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003803#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003804 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003805 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003806#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003807#ifdef FEAT_NETBEANS_INTG
3808 abort = abort || set_ref_in_nb_channel(copyID);
3809#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003810
Bram Moolenaare3188e22016-05-31 21:13:04 +02003811#ifdef FEAT_TIMERS
3812 abort = abort || set_ref_in_timer(copyID);
3813#endif
3814
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003815#ifdef FEAT_QUICKFIX
3816 abort = abort || set_ref_in_quickfix(copyID);
3817#endif
3818
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003819#ifdef FEAT_TERMINAL
3820 abort = abort || set_ref_in_term(copyID);
3821#endif
3822
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003823#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003824 abort = abort || set_ref_in_popups(copyID);
3825#endif
3826
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003827 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003828 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003829 /*
3830 * 2. Free lists and dictionaries that are not referenced.
3831 */
3832 did_free = free_unref_items(copyID);
3833
3834 /*
3835 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003836 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003837 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003839 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003840 else if (p_verbose > 0)
3841 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003842 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003843 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003844
3845 return did_free;
3846}
3847
3848/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003849 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003850 */
3851 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003852free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003853{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003854 int did_free = FALSE;
3855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003856 // Let all "free" functions know that we are here. This means no
3857 // dictionaries, lists, channels or jobs are to be freed, because we will
3858 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003859 in_free_unref_items = TRUE;
3860
3861 /*
3862 * PASS 1: free the contents of the items. We don't free the items
3863 * themselves yet, so that it is possible to decrement refcount counters
3864 */
3865
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003866 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003867 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003868
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003869 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003870 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003871
3872#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003873 // Go through the list of jobs and free items without the copyID. This
3874 // must happen before doing channels, because jobs refer to channels, but
3875 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003876 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003878 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003879 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3880#endif
3881
3882 /*
3883 * PASS 2: free the items themselves.
3884 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003885 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003886 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003887
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003888#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003889 // Go through the list of jobs and free items without the copyID. This
3890 // must happen before doing channels, because jobs refer to channels, but
3891 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003892 free_unused_jobs(copyID, COPYID_MASK);
3893
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003894 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003895 free_unused_channels(copyID, COPYID_MASK);
3896#endif
3897
3898 in_free_unref_items = FALSE;
3899
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003900 return did_free;
3901}
3902
3903/*
3904 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003905 * "list_stack" is used to add lists to be marked. Can be NULL.
3906 *
3907 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003909 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003910set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003911{
3912 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003913 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003914 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003915 hashtab_T *cur_ht;
3916 ht_stack_T *ht_stack = NULL;
3917 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003918
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003919 cur_ht = ht;
3920 for (;;)
3921 {
3922 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003923 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003924 // Mark each item in the hashtab. If the item contains a hashtab
3925 // it is added to ht_stack, if it contains a list it is added to
3926 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003927 todo = (int)cur_ht->ht_used;
3928 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3929 if (!HASHITEM_EMPTY(hi))
3930 {
3931 --todo;
3932 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3933 &ht_stack, list_stack);
3934 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003935 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003936
3937 if (ht_stack == NULL)
3938 break;
3939
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003940 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003941 cur_ht = ht_stack->ht;
3942 tempitem = ht_stack;
3943 ht_stack = ht_stack->prev;
3944 free(tempitem);
3945 }
3946
3947 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003948}
3949
3950/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003951 * Mark a dict and its items with "copyID".
3952 * Returns TRUE if setting references failed somehow.
3953 */
3954 int
3955set_ref_in_dict(dict_T *d, int copyID)
3956{
3957 if (d != NULL && d->dv_copyID != copyID)
3958 {
3959 d->dv_copyID = copyID;
3960 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3961 }
3962 return FALSE;
3963}
3964
3965/*
3966 * Mark a list and its items with "copyID".
3967 * Returns TRUE if setting references failed somehow.
3968 */
3969 int
3970set_ref_in_list(list_T *ll, int copyID)
3971{
3972 if (ll != NULL && ll->lv_copyID != copyID)
3973 {
3974 ll->lv_copyID = copyID;
3975 return set_ref_in_list_items(ll, copyID, NULL);
3976 }
3977 return FALSE;
3978}
3979
3980/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003981 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003982 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3983 *
3984 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003985 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003986 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003987set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003988{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003989 listitem_T *li;
3990 int abort = FALSE;
3991 list_T *cur_l;
3992 list_stack_T *list_stack = NULL;
3993 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003994
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003995 cur_l = l;
3996 for (;;)
3997 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003998 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003999 // Mark each item in the list. If the item contains a hashtab
4000 // it is added to ht_stack, if it contains a list it is added to
4001 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004002 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4003 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4004 ht_stack, &list_stack);
4005 if (list_stack == NULL)
4006 break;
4007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004008 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004009 cur_l = list_stack->list;
4010 tempitem = list_stack;
4011 list_stack = list_stack->prev;
4012 free(tempitem);
4013 }
4014
4015 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004016}
4017
4018/*
4019 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004020 * "list_stack" is used to add lists to be marked. Can be NULL.
4021 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4022 *
4023 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004024 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004026set_ref_in_item(
4027 typval_T *tv,
4028 int copyID,
4029 ht_stack_T **ht_stack,
4030 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004031{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004032 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004033
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004034 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004035 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004036 dict_T *dd = tv->vval.v_dict;
4037
Bram Moolenaara03f2332016-02-06 18:09:59 +01004038 if (dd != NULL && dd->dv_copyID != copyID)
4039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004040 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004041 dd->dv_copyID = copyID;
4042 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004043 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004044 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4045 }
4046 else
4047 {
4048 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4049 if (newitem == NULL)
4050 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004051 else
4052 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004053 newitem->ht = &dd->dv_hashtab;
4054 newitem->prev = *ht_stack;
4055 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004056 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004057 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004058 }
4059 }
4060 else if (tv->v_type == VAR_LIST)
4061 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004062 list_T *ll = tv->vval.v_list;
4063
Bram Moolenaara03f2332016-02-06 18:09:59 +01004064 if (ll != NULL && ll->lv_copyID != copyID)
4065 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004066 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004067 ll->lv_copyID = copyID;
4068 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004069 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004070 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004071 }
4072 else
4073 {
4074 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004075 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004076 if (newitem == NULL)
4077 abort = TRUE;
4078 else
4079 {
4080 newitem->list = ll;
4081 newitem->prev = *list_stack;
4082 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004083 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004084 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004085 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004086 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004087 else if (tv->v_type == VAR_FUNC)
4088 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004089 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004090 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004091 else if (tv->v_type == VAR_PARTIAL)
4092 {
4093 partial_T *pt = tv->vval.v_partial;
4094 int i;
4095
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004096 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004097 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004098 // Didn't see this partial yet.
4099 pt->pt_copyID = copyID;
4100
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004101 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004102
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004103 if (pt->pt_dict != NULL)
4104 {
4105 typval_T dtv;
4106
4107 dtv.v_type = VAR_DICT;
4108 dtv.vval.v_dict = pt->pt_dict;
4109 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4110 }
4111
4112 for (i = 0; i < pt->pt_argc; ++i)
4113 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4114 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004115 if (pt->pt_funcstack != NULL)
4116 {
4117 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4118
4119 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4120 abort = abort || set_ref_in_item(stack + i, copyID,
4121 ht_stack, list_stack);
4122 }
4123
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004124 }
4125 }
4126#ifdef FEAT_JOB_CHANNEL
4127 else if (tv->v_type == VAR_JOB)
4128 {
4129 job_T *job = tv->vval.v_job;
4130 typval_T dtv;
4131
4132 if (job != NULL && job->jv_copyID != copyID)
4133 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004134 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004135 if (job->jv_channel != NULL)
4136 {
4137 dtv.v_type = VAR_CHANNEL;
4138 dtv.vval.v_channel = job->jv_channel;
4139 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4140 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004141 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004142 {
4143 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004144 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004145 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4146 }
4147 }
4148 }
4149 else if (tv->v_type == VAR_CHANNEL)
4150 {
4151 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004152 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004153 typval_T dtv;
4154 jsonq_T *jq;
4155 cbq_T *cq;
4156
4157 if (ch != NULL && ch->ch_copyID != copyID)
4158 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004159 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004160 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004161 {
4162 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4163 jq = jq->jq_next)
4164 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4165 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4166 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004167 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004168 {
4169 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004170 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004171 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4172 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004173 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004174 {
4175 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004176 dtv.vval.v_partial =
4177 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004178 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4179 }
4180 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004181 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004182 {
4183 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004184 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004185 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4186 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004187 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004188 {
4189 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004190 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004191 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4192 }
4193 }
4194 }
4195#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004196 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004197}
4198
Bram Moolenaar8c711452005-01-14 21:53:12 +00004199/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004200 * Return a string with the string representation of a variable.
4201 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004202 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004203 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004204 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4205 * stings as "string()", otherwise does not put quotes around strings, as
4206 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004207 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4208 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004209 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004210 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004211 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004212echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004213 typval_T *tv,
4214 char_u **tofree,
4215 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004216 int copyID,
4217 int echo_style,
4218 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004219 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004220{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004221 static int recurse = 0;
4222 char_u *r = NULL;
4223
Bram Moolenaar33570922005-01-25 22:26:29 +00004224 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004225 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004226 if (!did_echo_string_emsg)
4227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004228 // Only give this message once for a recursive call to avoid
4229 // flooding the user with errors. And stop iterating over lists
4230 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004231 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004232 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004233 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004234 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004235 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004236 }
4237 ++recurse;
4238
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004239 switch (tv->v_type)
4240 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004241 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004242 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004243 {
4244 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004245 r = tv->vval.v_string;
4246 if (r == NULL)
4247 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004248 }
4249 else
4250 {
4251 *tofree = string_quote(tv->vval.v_string, FALSE);
4252 r = *tofree;
4253 }
4254 break;
4255
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004256 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004257 if (echo_style)
4258 {
4259 *tofree = NULL;
4260 r = tv->vval.v_string;
4261 }
4262 else
4263 {
4264 *tofree = string_quote(tv->vval.v_string, TRUE);
4265 r = *tofree;
4266 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004267 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004268
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004269 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004270 {
4271 partial_T *pt = tv->vval.v_partial;
4272 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004273 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004274 garray_T ga;
4275 int i;
4276 char_u *tf;
4277
4278 ga_init2(&ga, 1, 100);
4279 ga_concat(&ga, (char_u *)"function(");
4280 if (fname != NULL)
4281 {
4282 ga_concat(&ga, fname);
4283 vim_free(fname);
4284 }
4285 if (pt != NULL && pt->pt_argc > 0)
4286 {
4287 ga_concat(&ga, (char_u *)", [");
4288 for (i = 0; i < pt->pt_argc; ++i)
4289 {
4290 if (i > 0)
4291 ga_concat(&ga, (char_u *)", ");
4292 ga_concat(&ga,
4293 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4294 vim_free(tf);
4295 }
4296 ga_concat(&ga, (char_u *)"]");
4297 }
4298 if (pt != NULL && pt->pt_dict != NULL)
4299 {
4300 typval_T dtv;
4301
4302 ga_concat(&ga, (char_u *)", ");
4303 dtv.v_type = VAR_DICT;
4304 dtv.vval.v_dict = pt->pt_dict;
4305 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4306 vim_free(tf);
4307 }
4308 ga_concat(&ga, (char_u *)")");
4309
4310 *tofree = ga.ga_data;
4311 r = *tofree;
4312 break;
4313 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004314
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004315 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004316 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004317 break;
4318
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004319 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004320 if (tv->vval.v_list == NULL)
4321 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004322 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004323 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004324 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004325 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004326 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4327 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004328 {
4329 *tofree = NULL;
4330 r = (char_u *)"[...]";
4331 }
4332 else
4333 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004334 int old_copyID = tv->vval.v_list->lv_copyID;
4335
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004336 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004337 *tofree = list2string(tv, copyID, restore_copyID);
4338 if (restore_copyID)
4339 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004340 r = *tofree;
4341 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004342 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004343
Bram Moolenaar8c711452005-01-14 21:53:12 +00004344 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004345 if (tv->vval.v_dict == NULL)
4346 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004347 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004348 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004349 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004350 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004351 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4352 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004353 {
4354 *tofree = NULL;
4355 r = (char_u *)"{...}";
4356 }
4357 else
4358 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004359 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004360
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004361 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004362 *tofree = dict2string(tv, copyID, restore_copyID);
4363 if (restore_copyID)
4364 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004365 r = *tofree;
4366 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004367 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004368
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004370 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004371 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004373 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004374 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004375 break;
4376
Bram Moolenaar835dc632016-02-07 14:27:38 +01004377 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004378 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004379 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004380 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004381 if (composite_val)
4382 {
4383 *tofree = string_quote(r, FALSE);
4384 r = *tofree;
4385 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004386 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004387
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004389#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004390 *tofree = NULL;
4391 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4392 r = numbuf;
4393 break;
4394#endif
4395
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004396 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004397 case VAR_SPECIAL:
4398 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004399 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004400 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004401 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004402
Bram Moolenaar8502c702014-06-17 12:51:16 +02004403 if (--recurse == 0)
4404 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004405 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004406}
4407
4408/*
4409 * Return a string with the string representation of a variable.
4410 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4411 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004412 * Does not put quotes around strings, as ":echo" displays values.
4413 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4414 * May return NULL.
4415 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004416 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004417echo_string(
4418 typval_T *tv,
4419 char_u **tofree,
4420 char_u *numbuf,
4421 int copyID)
4422{
4423 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4424}
4425
4426/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004427 * Return string "str" in ' quotes, doubling ' characters.
4428 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004429 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004430 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004431 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004432string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004433{
Bram Moolenaar33570922005-01-25 22:26:29 +00004434 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004435 char_u *p, *r, *s;
4436
Bram Moolenaar33570922005-01-25 22:26:29 +00004437 len = (function ? 13 : 3);
4438 if (str != NULL)
4439 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004440 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004441 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004442 if (*p == '\'')
4443 ++len;
4444 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004445 s = r = alloc(len);
4446 if (r != NULL)
4447 {
4448 if (function)
4449 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004451 r += 10;
4452 }
4453 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004454 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004455 if (str != NULL)
4456 for (p = str; *p != NUL; )
4457 {
4458 if (*p == '\'')
4459 *r++ = '\'';
4460 MB_COPY_CHAR(p, r);
4461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004463 if (function)
4464 *r++ = ')';
4465 *r++ = NUL;
4466 }
4467 return s;
4468}
4469
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004470#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471/*
4472 * Convert the string "text" to a floating point number.
4473 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4474 * this always uses a decimal point.
4475 * Returns the length of the text that was consumed.
4476 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004478string2float(
4479 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004480 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004481{
4482 char *s = (char *)text;
4483 float_T f;
4484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004486 if (STRNICMP(text, "inf", 3) == 0)
4487 {
4488 *value = INFINITY;
4489 return 3;
4490 }
4491 if (STRNICMP(text, "-inf", 3) == 0)
4492 {
4493 *value = -INFINITY;
4494 return 4;
4495 }
4496 if (STRNICMP(text, "nan", 3) == 0)
4497 {
4498 *value = NAN;
4499 return 3;
4500 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004501 f = strtod(s, &s);
4502 *value = f;
4503 return (int)((char_u *)s - text);
4504}
4505#endif
4506
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004509 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004512var2fpos(
4513 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004514 int dollar_lnum, // TRUE when $ is last line
4515 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004517 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004519 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004521 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004522 if (varp->v_type == VAR_LIST)
4523 {
4524 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004525 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004526 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004527 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004528
4529 l = varp->vval.v_list;
4530 if (l == NULL)
4531 return NULL;
4532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004533 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004534 pos.lnum = list_find_nr(l, 0L, &error);
4535 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004536 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004538 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004539 pos.col = list_find_nr(l, 1L, &error);
4540 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004541 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004542 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004544 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004545 li = list_find(l, 1L);
4546 if (li != NULL && li->li_tv.v_type == VAR_STRING
4547 && li->li_tv.vval.v_string != NULL
4548 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4549 pos.col = len + 1;
4550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004551 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004552 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004553 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004554 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004556 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004557 pos.coladd = list_find_nr(l, 2L, &error);
4558 if (error)
4559 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004560
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004561 return &pos;
4562 }
4563
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004564 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004565 if (name == NULL)
4566 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004567 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004570 {
4571 if (VIsual_active)
4572 return &VIsual;
4573 return &curwin->w_cursor;
4574 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004575 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004577 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4579 return NULL;
4580 return pp;
4581 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004582
Bram Moolenaara5525202006-03-02 22:52:09 +00004583 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004584
Bram Moolenaar477933c2007-07-17 14:32:23 +00004585 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004586 {
4587 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004588 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004589 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004590 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004591 // In silent Ex mode topline is zero, but that's not a valid line
4592 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004593 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004594 return &pos;
4595 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004596 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004597 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004598 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004599 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004600 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004601 return &pos;
4602 }
4603 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004604 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004606 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 pos.lnum = curbuf->b_ml.ml_line_count;
4609 pos.col = 0;
4610 }
4611 else
4612 {
4613 pos.lnum = curwin->w_cursor.lnum;
4614 pos.col = (colnr_T)STRLEN(ml_get_curline());
4615 }
4616 return &pos;
4617 }
4618 return NULL;
4619}
4620
4621/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004622 * Convert list in "arg" into a position and optional file number.
4623 * When "fnump" is NULL there is no file number, only 3 items.
4624 * Note that the column is passed on as-is, the caller may want to decrement
4625 * it to use 1 for the first column.
4626 * Return FAIL when conversion is not possible, doesn't check the position for
4627 * validity.
4628 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004630list2fpos(
4631 typval_T *arg,
4632 pos_T *posp,
4633 int *fnump,
4634 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004635{
4636 list_T *l = arg->vval.v_list;
4637 long i = 0;
4638 long n;
4639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004640 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4641 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004642 if (arg->v_type != VAR_LIST
4643 || l == NULL
4644 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004645 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004646 return FAIL;
4647
4648 if (fnump != NULL)
4649 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004650 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004651 if (n < 0)
4652 return FAIL;
4653 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004655 *fnump = n;
4656 }
4657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004658 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004659 if (n < 0)
4660 return FAIL;
4661 posp->lnum = n;
4662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004663 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004664 if (n < 0)
4665 return FAIL;
4666 posp->col = n;
4667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004669 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004670 posp->coladd = 0;
4671 else
4672 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004673
Bram Moolenaar493c1782014-05-28 14:34:46 +02004674 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004675 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004676
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004677 return OK;
4678}
4679
4680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 * Get the length of an environment variable name.
4682 * Advance "arg" to the first character after the name.
4683 * Return 0 for error.
4684 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004686get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688 char_u *p;
4689 int len;
4690
4691 for (p = *arg; vim_isIDc(*p); ++p)
4692 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004693 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 return 0;
4695
4696 len = (int)(p - *arg);
4697 *arg = p;
4698 return len;
4699}
4700
4701/*
4702 * Get the length of the name of a function or internal variable.
4703 * "arg" is advanced to the first non-white character after the name.
4704 * Return 0 if something is wrong.
4705 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004707get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
4709 char_u *p;
4710 int len;
4711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004714 {
4715 if (*p == ':')
4716 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004717 // "s:" is start of "s:var", but "n:" is not and can be used in
4718 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004719 len = (int)(p - *arg);
4720 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4721 || len > 1)
4722 break;
4723 }
4724 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004725 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return 0;
4727
4728 len = (int)(p - *arg);
4729 *arg = skipwhite(p);
4730
4731 return len;
4732}
4733
4734/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004735 * Get the length of the name of a variable or function.
4736 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004738 * Return -1 if curly braces expansion failed.
4739 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 * If the name contains 'magic' {}'s, expand them and return the
4741 * expanded name in an allocated string via 'alias' - caller must free.
4742 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004743 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004744get_name_len(
4745 char_u **arg,
4746 char_u **alias,
4747 int evaluate,
4748 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 char_u *p;
4752 char_u *expr_start;
4753 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004755 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756
4757 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4758 && (*arg)[2] == (int)KE_SNR)
4759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 *arg += 3;
4762 return get_id_len(arg) + 3;
4763 }
4764 len = eval_fname_script(*arg);
4765 if (len > 0)
4766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004767 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 *arg += len;
4769 }
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004774 p = find_name_end(*arg, &expr_start, &expr_end,
4775 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 if (expr_start != NULL)
4777 {
4778 char_u *temp_string;
4779
4780 if (!evaluate)
4781 {
4782 len += (int)(p - *arg);
4783 *arg = skipwhite(p);
4784 return len;
4785 }
4786
4787 /*
4788 * Include any <SID> etc in the expanded string:
4789 * Thus the -len here.
4790 */
4791 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4792 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004793 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 *alias = temp_string;
4795 *arg = skipwhite(p);
4796 return (int)STRLEN(temp_string);
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004800 // Only give an error when there is something, otherwise it will be
4801 // reported at a higher level.
4802 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004803 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
4805 return len;
4806}
4807
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004808/*
4809 * Find the end of a variable or function name, taking care of magic braces.
4810 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4811 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004812 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004813 * Return a pointer to just after the name. Equal to "arg" if there is no
4814 * valid name.
4815 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004816 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004817find_name_end(
4818 char_u *arg,
4819 char_u **expr_start,
4820 char_u **expr_end,
4821 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 int mb_nest = 0;
4824 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004826 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004827 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 *expr_start = NULL;
4832 *expr_end = NULL;
4833 }
4834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004835 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004836 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4837 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004838 return arg;
4839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 for (p = arg; *p != NUL
4841 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004842 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004843 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004845 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004847 if (*p == '\'')
4848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004850 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004851 ;
4852 if (*p == NUL)
4853 break;
4854 }
4855 else if (*p == '"')
4856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004858 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004859 if (*p == '\\' && p[1] != NUL)
4860 ++p;
4861 if (*p == NUL)
4862 break;
4863 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004864 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004866 // "s:" is start of "s:var", but "n:" is not and can be used in
4867 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004868 len = (int)(p - arg);
4869 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004870 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004871 break;
4872 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 if (*p == '[')
4877 ++br_nest;
4878 else if (*p == ']')
4879 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004881
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004882 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 if (*p == '{')
4885 {
4886 mb_nest++;
4887 if (expr_start != NULL && *expr_start == NULL)
4888 *expr_start = p;
4889 }
4890 else if (*p == '}')
4891 {
4892 mb_nest--;
4893 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4894 *expr_end = p;
4895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898
4899 return p;
4900}
4901
4902/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004903 * Expands out the 'magic' {}'s in a variable/function name.
4904 * Note that this can call itself recursively, to deal with
4905 * constructs like foo{bar}{baz}{bam}
4906 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4907 * "in_start" ^
4908 * "expr_start" ^
4909 * "expr_end" ^
4910 * "in_end" ^
4911 *
4912 * Returns a new allocated string, which the caller must free.
4913 * Returns NULL for failure.
4914 */
4915 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004916make_expanded_name(
4917 char_u *in_start,
4918 char_u *expr_start,
4919 char_u *expr_end,
4920 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004921{
4922 char_u c1;
4923 char_u *retval = NULL;
4924 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004925
4926 if (expr_end == NULL || in_end == NULL)
4927 return NULL;
4928 *expr_start = NUL;
4929 *expr_end = NUL;
4930 c1 = *in_end;
4931 *in_end = NUL;
4932
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004933 temp_result = eval_to_string(expr_start + 1, FALSE);
4934 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004935 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004936 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4937 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004938 if (retval != NULL)
4939 {
4940 STRCPY(retval, in_start);
4941 STRCAT(retval, temp_result);
4942 STRCAT(retval, expr_end + 1);
4943 }
4944 }
4945 vim_free(temp_result);
4946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004947 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004948 *expr_start = '{';
4949 *expr_end = '}';
4950
4951 if (retval != NULL)
4952 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004953 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004954 if (expr_start != NULL)
4955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004956 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004957 temp_result = make_expanded_name(retval, expr_start,
4958 expr_end, temp_result);
4959 vim_free(retval);
4960 retval = temp_result;
4961 }
4962 }
4963
4964 return retval;
4965}
4966
4967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004969 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004972eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004974 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4975}
4976
4977/*
4978 * Return TRUE if character "c" can be used as the first character in a
4979 * variable or function name (excluding '{' and '}').
4980 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004982eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004983{
4984 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985}
4986
4987/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004988 * Handle:
4989 * - expr[expr], expr[expr:expr] subscript
4990 * - ".name" lookup
4991 * - function call with Funcref variable: func(expr)
4992 * - method call: var->method()
4993 *
4994 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004995 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004996 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004997handle_subscript(
4998 char_u **arg,
4999 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005000 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005001 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005002{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005003 int evaluate = evalarg != NULL
5004 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005005 int ret = OK;
5006 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005007
Bram Moolenaar61343f02019-07-20 21:11:13 +02005008 // "." is ".name" lookup when we found a dict or when evaluating and
5009 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005010 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005011 && (((**arg == '['
5012 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005013 || (!evaluate
5014 && (*arg)[1] != '.'
5015 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005016 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005017 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005018 && !VIM_ISWHITE(*(*arg - 1)))
5019 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005020 {
5021 if (**arg == '(')
5022 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005023 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005024
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005025 // Stop the expression evaluation when immediately aborting on
5026 // error, or when an interrupt occurred or an exception was thrown
5027 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005028 if (aborting())
5029 {
5030 if (ret == OK)
5031 clear_tv(rettv);
5032 ret = FAIL;
5033 }
5034 dict_unref(selfdict);
5035 selfdict = NULL;
5036 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005037 else if (**arg == '-')
5038 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005039 if (ret == OK)
5040 {
5041 if ((*arg)[2] == '{')
5042 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005043 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005044 else
5045 // expr->name()
5046 ret = eval_method(arg, rettv, evaluate, verbose);
5047 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005048 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005049 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005050 {
5051 dict_unref(selfdict);
5052 if (rettv->v_type == VAR_DICT)
5053 {
5054 selfdict = rettv->vval.v_dict;
5055 if (selfdict != NULL)
5056 ++selfdict->dv_refcount;
5057 }
5058 else
5059 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005060 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005061 {
5062 clear_tv(rettv);
5063 ret = FAIL;
5064 }
5065 }
5066 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005068 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5069 // Don't do this when "Func" is already a partial that was bound
5070 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005071 if (selfdict != NULL
5072 && (rettv->v_type == VAR_FUNC
5073 || (rettv->v_type == VAR_PARTIAL
5074 && (rettv->vval.v_partial->pt_auto
5075 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005076 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005077
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005078 dict_unref(selfdict);
5079 return ret;
5080}
5081
5082/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005083 * Make a copy of an item.
5084 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005085 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5086 * reference to an already copied list/dict can be used.
5087 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005088 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005089 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005090item_copy(
5091 typval_T *from,
5092 typval_T *to,
5093 int deep,
5094 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005095{
5096 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005097 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005098
Bram Moolenaar33570922005-01-25 22:26:29 +00005099 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005101 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005102 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005103 }
5104 ++recurse;
5105
5106 switch (from->v_type)
5107 {
5108 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005110 case VAR_STRING:
5111 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005112 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005113 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005114 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005115 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005116 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005117 copy_tv(from, to);
5118 break;
5119 case VAR_LIST:
5120 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005121 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005122 if (from->vval.v_list == NULL)
5123 to->vval.v_list = NULL;
5124 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005126 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005127 to->vval.v_list = from->vval.v_list->lv_copylist;
5128 ++to->vval.v_list->lv_refcount;
5129 }
5130 else
5131 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5132 if (to->vval.v_list == NULL)
5133 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005134 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005135 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005137 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005138 case VAR_DICT:
5139 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005140 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005141 if (from->vval.v_dict == NULL)
5142 to->vval.v_dict = NULL;
5143 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005145 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005146 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5147 ++to->vval.v_dict->dv_refcount;
5148 }
5149 else
5150 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5151 if (to->vval.v_dict == NULL)
5152 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005153 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005154 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005155 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005157 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005158 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 }
5160 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005161 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005162}
5163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 void
5165echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5166{
5167 char_u *tofree;
5168 char_u numbuf[NUMBUFLEN];
5169 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5170
5171 if (*atstart)
5172 {
5173 *atstart = FALSE;
5174 // Call msg_start() after eval1(), evaluating the expression
5175 // may cause a message to appear.
5176 if (with_space)
5177 {
5178 // Mark the saved text as finishing the line, so that what
5179 // follows is displayed on a new line when scrolling back
5180 // at the more prompt.
5181 msg_sb_eol();
5182 msg_start();
5183 }
5184 }
5185 else if (with_space)
5186 msg_puts_attr(" ", echo_attr);
5187
5188 if (p != NULL)
5189 for ( ; *p != NUL && !got_int; ++p)
5190 {
5191 if (*p == '\n' || *p == '\r' || *p == TAB)
5192 {
5193 if (*p != TAB && *needclr)
5194 {
5195 // remove any text still there from the command
5196 msg_clr_eos();
5197 *needclr = FALSE;
5198 }
5199 msg_putchar_attr(*p, echo_attr);
5200 }
5201 else
5202 {
5203 if (has_mbyte)
5204 {
5205 int i = (*mb_ptr2len)(p);
5206
5207 (void)msg_outtrans_len_attr(p, i, echo_attr);
5208 p += i - 1;
5209 }
5210 else
5211 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5212 }
5213 }
5214 vim_free(tofree);
5215}
5216
Bram Moolenaare9a41262005-01-15 22:18:47 +00005217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 * ":echo expr1 ..." print each argument separated with a space, add a
5219 * newline at the end.
5220 * ":echon expr1 ..." print each argument plain.
5221 */
5222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005223ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005226 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 char_u *p;
5228 int needclr = TRUE;
5229 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005230 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005231 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005232 evalarg_T evalarg;
5233
5234 CLEAR_FIELD(evalarg);
5235 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +02005236 if (getline_equal(eap->getline, eap->cookie, getsourceline))
5237 {
5238 evalarg.eval_getline = eap->getline;
5239 evalarg.eval_cookie = eap->cookie;
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
5242 if (eap->skip)
5243 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005244 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005246 // If eval1() causes an error message the text from the command may
5247 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005248 need_clr_eos = needclr;
5249
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005251 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
5253 /*
5254 * Report the invalid expression unless the expression evaluation
5255 * has been cancelled due to an aborting error, an interrupt, or an
5256 * exception.
5257 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005258 if (!aborting() && did_emsg == did_emsg_before
5259 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005260 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005261 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 break;
5263 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005264 need_clr_eos = FALSE;
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005268
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005269 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 arg = skipwhite(arg);
5271 }
5272 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005273 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 if (eap->skip)
5276 --emsg_skip;
5277 else
5278 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005279 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (needclr)
5281 msg_clr_eos();
5282 if (eap->cmdidx == CMD_echo)
5283 msg_end();
5284 }
5285}
5286
5287/*
5288 * ":echohl {name}".
5289 */
5290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005291ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005293 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294}
5295
5296/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005297 * Returns the :echo attribute
5298 */
5299 int
5300get_echo_attr(void)
5301{
5302 return echo_attr;
5303}
5304
5305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 * ":execute expr1 ..." execute the result of an expression.
5307 * ":echomsg expr1 ..." Print a message
5308 * ":echoerr expr1 ..." Print an error
5309 * Each gets spaces around each argument and a newline at the end for
5310 * echo commands
5311 */
5312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 int ret = OK;
5318 char_u *p;
5319 garray_T ga;
5320 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
5322 ga_init2(&ga, 1, 80);
5323
5324 if (eap->skip)
5325 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005326 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005328 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5329 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
5332 if (!eap->skip)
5333 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005334 char_u buf[NUMBUFLEN];
5335
5336 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005337 {
5338 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5339 {
5340 emsg(_(e_inval_string));
5341 p = NULL;
5342 }
5343 else
5344 p = tv_get_string_buf(&rettv, buf);
5345 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005346 else
5347 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005348 if (p == NULL)
5349 {
5350 clear_tv(&rettv);
5351 ret = FAIL;
5352 break;
5353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 len = (int)STRLEN(p);
5355 if (ga_grow(&ga, len + 2) == FAIL)
5356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005357 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 ret = FAIL;
5359 break;
5360 }
5361 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 ga.ga_len += len;
5365 }
5366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005367 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 arg = skipwhite(arg);
5369 }
5370
5371 if (ret != FAIL && ga.ga_data != NULL)
5372 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005373 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 // Mark the already saved text as finishing the line, so that what
5376 // follows is displayed on a new line when scrolling back at the
5377 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005378 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005379 }
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005382 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005383 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005384 out_flush();
5385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 else if (eap->cmdidx == CMD_echoerr)
5387 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005388 int save_did_emsg = did_emsg;
5389
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005390 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005391 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 if (!force_abort)
5393 did_emsg = save_did_emsg;
5394 }
5395 else if (eap->cmdidx == CMD_execute)
5396 do_cmdline((char_u *)ga.ga_data,
5397 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5398 }
5399
5400 ga_clear(&ga);
5401
5402 if (eap->skip)
5403 --emsg_skip;
5404
5405 eap->nextcmd = check_nextcmd(arg);
5406}
5407
5408/*
5409 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5410 * "arg" points to the "&" or '+' when called, to "option" when returning.
5411 * Returns NULL when no option name found. Otherwise pointer to the char
5412 * after the option name.
5413 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005414 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005415find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 char_u *p = *arg;
5418
5419 ++p;
5420 if (*p == 'g' && p[1] == ':')
5421 {
5422 *opt_flags = OPT_GLOBAL;
5423 p += 2;
5424 }
5425 else if (*p == 'l' && p[1] == ':')
5426 {
5427 *opt_flags = OPT_LOCAL;
5428 p += 2;
5429 }
5430 else
5431 *opt_flags = 0;
5432
5433 if (!ASCII_ISALPHA(*p))
5434 return NULL;
5435 *arg = p;
5436
5437 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005438 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 else
5440 while (ASCII_ISALPHA(*p))
5441 ++p;
5442 return p;
5443}
5444
5445/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005446 * Display script name where an item was last set.
5447 * Should only be invoked when 'verbose' is non-zero.
5448 */
5449 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005450last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005451{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005452 char_u *p;
5453
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005454 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005455 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005456 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005457 if (p != NULL)
5458 {
5459 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005460 msg_puts(_("\n\tLast set from "));
5461 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005462 if (script_ctx.sc_lnum > 0)
5463 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005464 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005465 msg_outnum((long)script_ctx.sc_lnum);
5466 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005467 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005468 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005469 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005470 }
5471}
5472
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005473#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475/*
5476 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005477 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 * "flags" can be "g" to do a global substitute.
5479 * Returns an allocated string, NULL for error.
5480 */
5481 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005482do_string_sub(
5483 char_u *str,
5484 char_u *pat,
5485 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005486 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005487 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 int sublen;
5490 regmatch_T regmatch;
5491 int i;
5492 int do_all;
5493 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005494 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 garray_T ga;
5496 char_u *ret;
5497 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005498 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005502 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
5504 ga_init2(&ga, 1, 200);
5505
5506 do_all = (flags[0] == 'g');
5507
5508 regmatch.rm_ic = p_ic;
5509 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5510 if (regmatch.regprog != NULL)
5511 {
5512 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005513 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5515 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005516 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005517 if (regmatch.startp[0] == regmatch.endp[0])
5518 {
5519 if (zero_width == regmatch.startp[0])
5520 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005521 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005522 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005523 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5524 (size_t)i);
5525 ga.ga_len += i;
5526 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005527 continue;
5528 }
5529 zero_width = regmatch.startp[0];
5530 }
5531
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 /*
5533 * Get some space for a temporary buffer to do the substitution
5534 * into. It will contain:
5535 * - The text up to where the match is.
5536 * - The substituted text.
5537 * - The text after the match.
5538 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005539 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005540 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5542 {
5543 ga_clear(&ga);
5544 break;
5545 }
5546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005547 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 i = (int)(regmatch.startp[0] - tail);
5549 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005551 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 + ga.ga_len + i, TRUE, TRUE, FALSE);
5553 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005554 tail = regmatch.endp[0];
5555 if (*tail == NUL)
5556 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 if (!do_all)
5558 break;
5559 }
5560
5561 if (ga.ga_data != NULL)
5562 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5563
Bram Moolenaar473de612013-06-08 18:19:48 +02005564 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566
5567 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5568 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005569 if (p_cpo == empty_option)
5570 p_cpo = save_cpo;
5571 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005572 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005573 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 return ret;
5576}