blob: a98eea34bef4e9b9986cfe1fe99533adc36b86c2 [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;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200336 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar006ad482020-06-30 20:55:15 +0200338 CLEAR_FIELD(evalarg);
339 evalarg.eval_flags = skip ? 0 : EVAL_EVALUATE;
340 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
341 {
342 evalarg.eval_getline = eap->getline;
343 evalarg.eval_cookie = eap->cookie;
344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 if (skip)
346 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 retval = NULL;
349 else
350 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100351 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000352 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
354 if (skip)
355 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200356 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
358 return retval;
359}
360
361/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000362 * Skip over an expression at "*pp".
363 * Return FAIL for an error, OK otherwise.
364 */
365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100366skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367{
Bram Moolenaar33570922005-01-25 22:26:29 +0000368 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000369
370 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200371 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372}
373
374/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * Skip over an expression at "*pp".
376 * If in Vim9 script and line breaks are encountered, the lines are
377 * concatenated. "evalarg->eval_tofree" will be set accordingly.
378 * Return FAIL for an error, OK otherwise.
379 */
380 int
381skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
382{
383 typval_T rettv;
384 int res;
385 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
386 garray_T *gap = &evalarg->eval_ga;
387 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
388
389 if (vim9script && evalarg->eval_cookie != NULL)
390 {
391 ga_init2(gap, sizeof(char_u *), 10);
392 if (ga_grow(gap, 1) == OK)
393 // leave room for "start"
394 ++gap->ga_len;
395 }
396
397 // Don't evaluate the expression.
398 if (evalarg != NULL)
399 evalarg->eval_flags &= ~EVAL_EVALUATE;
400 *end = skipwhite(*end);
401 res = eval1(end, &rettv, evalarg);
402 if (evalarg != NULL)
403 evalarg->eval_flags = save_flags;
404
405 if (vim9script && evalarg->eval_cookie != NULL
406 && evalarg->eval_ga.ga_len > 1)
407 {
408 char_u *p;
409 size_t endoff = STRLEN(*end);
410
411 // Line breaks encountered, concatenate all the lines.
412 *((char_u **)gap->ga_data) = *start;
413 p = ga_concat_strings(gap, "");
414 *((char_u **)gap->ga_data) = NULL;
415 ga_clear_strings(gap);
416 gap->ga_itemsize = 0;
417 if (p == NULL)
418 return FAIL;
419 *start = p;
420 vim_free(evalarg->eval_tofree);
421 evalarg->eval_tofree = p;
422 // Compute "end" relative to the end.
423 *end = *start + STRLEN(*start) - endoff;
424 }
425
426 return res;
427}
428
429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000431 * When "convert" is TRUE convert a List into a sequence of lines and convert
432 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 * Return pointer to allocated memory, or NULL for failure.
434 */
435 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100436eval_to_string(
437 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100438 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
Bram Moolenaar33570922005-01-25 22:26:29 +0000440 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000442 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000443#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000444 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200447 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 retval = NULL;
449 else
450 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000451 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000452 {
453 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000454 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200455 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200456 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200457 if (tv.vval.v_list->lv_len > 0)
458 ga_append(&ga, NL);
459 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000460 ga_append(&ga, NUL);
461 retval = (char_u *)ga.ga_data;
462 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000463#ifdef FEAT_FLOAT
464 else if (convert && tv.v_type == VAR_FLOAT)
465 {
466 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
467 retval = vim_strsave(numbuf);
468 }
469#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000470 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100471 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000472 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200474 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475
476 return retval;
477}
478
479/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000480 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200481 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 */
483 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100484eval_to_string_safe(
485 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100486 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
488 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200489 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200491 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000492 if (use_sandbox)
493 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200494 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200495 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000496 if (use_sandbox)
497 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200498 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200499 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 return retval;
501}
502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503/*
504 * Top level evaluation function, returning a number.
505 * Evaluates "expr" silently.
506 * Returns -1 for an error.
507 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200508 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100509eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510{
Bram Moolenaar33570922005-01-25 22:26:29 +0000511 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200512 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000513 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
515 ++emsg_off;
516
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200517 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 retval = -1;
519 else
520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100521 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000522 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 }
524 --emsg_off;
525
526 return retval;
527}
528
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000529/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000530 * Top level evaluation function.
531 * Returns an allocated typval_T with the result.
532 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000533 */
534 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200535eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000536{
537 typval_T *tv;
538
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200539 tv = ALLOC_ONE(typval_T);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200540 if (tv != NULL && eval0(arg, tv, eap, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100541 VIM_CLEAR(tv);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200542 clear_evalarg(&EVALARG_EVALUATE, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000543
544 return tv;
545}
546
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100548 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200549 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
550 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000551 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200553 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554call_vim_function(
555 char_u *func,
556 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200557 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200558 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000560 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200561 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100563 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200564 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200565 funcexe.firstline = curwin->w_cursor.lnum;
566 funcexe.lastline = curwin->w_cursor.lnum;
567 funcexe.evaluate = TRUE;
568 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000569 if (ret == FAIL)
570 clear_tv(rettv);
571
572 return ret;
573}
574
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100575/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100576 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100577 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200578 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
579 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582call_func_retnr(
583 char_u *func,
584 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200585 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100586{
587 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200588 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100589
Bram Moolenaarded27a12018-08-01 19:06:03 +0200590 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100591 return -1;
592
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100593 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100594 clear_tv(&rettv);
595 return retval;
596}
597
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000598/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100599 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000600 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200601 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
602 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000603 */
604 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100605call_func_retstr(
606 char_u *func,
607 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200608 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000609{
610 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000611 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000612
Bram Moolenaarded27a12018-08-01 19:06:03 +0200613 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000614 return NULL;
615
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100616 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000617 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 return retval;
619}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000620
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000621/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100622 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200623 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
624 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000625 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000626 */
627 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100628call_func_retlist(
629 char_u *func,
630 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200631 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000632{
633 typval_T rettv;
634
Bram Moolenaarded27a12018-08-01 19:06:03 +0200635 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000636 return NULL;
637
638 if (rettv.v_type != VAR_LIST)
639 {
640 clear_tv(&rettv);
641 return NULL;
642 }
643
644 return rettv.vval.v_list;
645}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#ifdef FEAT_FOLDING
648/*
649 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
650 * it in "*cp". Doesn't give error messages.
651 */
652 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
Bram Moolenaar33570922005-01-25 22:26:29 +0000655 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200656 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000658 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
659 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000662 if (use_sandbox)
663 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200664 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200666 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 retval = 0;
668 else
669 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100670 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000671 if (tv.v_type == VAR_NUMBER)
672 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000673 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 retval = 0;
675 else
676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100677 // If the result is a string, check if there is a non-digit before
678 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000679 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (!VIM_ISDIGIT(*s) && *s != '-')
681 *cp = *s++;
682 retval = atol((char *)s);
683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000684 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
686 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000687 if (use_sandbox)
688 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200689 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200690 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200692 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694#endif
695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000697 * Get an lval: variable, Dict item or List item that can be assigned a value
698 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
699 * "name.key", "name.key[expr]" etc.
700 * Indexing only works if "name" is an existing List or Dictionary.
701 * "name" points to the start of the name.
702 * If "rettv" is not NULL it points to the value to be assigned.
703 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
704 * wrong; must end in space or cmd separator.
705 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100706 * flags:
707 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100708 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100709 * GLV_NO_AUTOLOAD: do not use script autoloading
710 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000711 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000712 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000713 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000714 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200715 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100716get_lval(
717 char_u *name,
718 typval_T *rettv,
719 lval_T *lp,
720 int unlet,
721 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100722 int flags, // GLV_ values
723 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000724{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000725 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000726 char_u *expr_start, *expr_end;
727 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 dictitem_T *v;
729 typval_T var1;
730 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000731 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000732 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000733 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000734 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000735 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100736 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100738 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200739 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000740
741 if (skip)
742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100743 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000744 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200745 lp->ll_name_end = find_name_end(name, NULL, NULL,
746 FNE_INCL_BR | fne_flags);
747 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000748 }
749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100750 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000751 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000753 if (expr_start != NULL)
754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100755 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100756 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000757 && *p != '[' && *p != '.')
758 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100759 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000760 return NULL;
761 }
762
763 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
764 if (lp->ll_exp_name == NULL)
765 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100766 // Report an invalid expression in braces, unless the
767 // expression evaluation has been cancelled due to an
768 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000769 if (!aborting() && !quiet)
770 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000771 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100772 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000773 return NULL;
774 }
775 }
776 lp->ll_name = lp->ll_exp_name;
777 }
778 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000780 lp->ll_name = name;
781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
783 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100784 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 char_u *tp = skipwhite(p + 1);
786
787 // parse the type after the name
788 lp->ll_type = parse_type(&tp, &si->sn_type_list);
789 lp->ll_name_end = tp;
790 }
791 }
792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100793 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
795 return p;
796
797 cc = *p;
798 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100799 // Only pass &ht when we would write to the variable, it prevents autoload
800 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100801 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
802 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100804 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000806 if (v == NULL)
807 return NULL;
808
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 /*
810 * Loop until no more [idx] or .key is following.
811 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000812 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100813 var1.v_type = VAR_UNKNOWN;
814 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
818 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100819 && lp->ll_tv->vval.v_dict != NULL)
820 && !(lp->ll_tv->v_type == VAR_BLOB
821 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100824 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000826 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000827 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000828 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000829 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100830 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000833
Bram Moolenaar8c711452005-01-14 21:53:12 +0000834 len = -1;
835 if (*p == '.')
836 {
837 key = p + 1;
838 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
839 ;
840 if (len == 0)
841 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000842 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100843 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000845 }
846 p = key + len;
847 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000848 else
849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000851 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000852 if (*p == ':')
853 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000854 else
855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000856 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200857 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100859 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000862 clear_tv(&var1);
863 return NULL;
864 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000865 }
866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100867 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000868 if (*p == ':')
869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100873 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100874 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000876 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100877 if (rettv != NULL
878 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100879 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100880 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100881 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100884 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100885 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000887 }
888 p = skipwhite(p + 1);
889 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000891 else
892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200894 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200895 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000896 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100897 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000899 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100900 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100902 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100903 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000904 clear_tv(&var2);
905 return NULL;
906 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000910 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000912
Bram Moolenaar8c711452005-01-14 21:53:12 +0000913 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100916 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100917 clear_tv(&var1);
918 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000919 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000920 }
921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100922 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 ++p;
924 }
925
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000927 {
928 if (len == -1)
929 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100930 // "[key]": get key from "var1"
931 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200932 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000934 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 }
937 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000938 lp->ll_list = NULL;
939 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000940 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100942 // When assigning to a scope dictionary check that a function and
943 // variable name is valid (only variable name unless it is l: or
944 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200945 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200946 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200947 int prevval;
948 int wrong;
949
950 if (len != -1)
951 {
952 prevval = key[len];
953 key[len] = NUL;
954 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200955 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100956 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200957 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
958 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200959 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200960 || !valid_varname(key);
961 if (len != -1)
962 key[len] = prevval;
963 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200964 {
965 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200966 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200967 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200968 }
969
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000971 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100972 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200973 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100974 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100976 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100977 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200978 return NULL;
979 }
980
Bram Moolenaar31b81602019-02-10 22:14:27 +0100981 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000983 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100985 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100986 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 }
989 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100993 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 p = NULL;
996 break;
997 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100999 else if ((flags & GLV_READ_ONLY) == 0
1000 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001001 {
1002 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001003 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001004 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001005
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001006 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001009 else if (lp->ll_tv->v_type == VAR_BLOB)
1010 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001011 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1012
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001013 /*
1014 * Get the number and item for the only or first index of the List.
1015 */
1016 if (empty1)
1017 lp->ll_n1 = 0;
1018 else
1019 // is number or string
1020 lp->ll_n1 = (long)tv_get_number(&var1);
1021 clear_tv(&var1);
1022
1023 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001024 || lp->ll_n1 > bloblen
1025 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001026 {
1027 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001028 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001029 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001030 return NULL;
1031 }
1032 if (lp->ll_range && !lp->ll_empty2)
1033 {
1034 lp->ll_n2 = (long)tv_get_number(&var2);
1035 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001036 if (lp->ll_n2 < 0
1037 || lp->ll_n2 >= bloblen
1038 || lp->ll_n2 < lp->ll_n1)
1039 {
1040 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001041 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001042 return NULL;
1043 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001044 }
1045 lp->ll_blob = lp->ll_tv->vval.v_blob;
1046 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001047 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 else
1050 {
1051 /*
1052 * Get the number and item for the only or first index of the List.
1053 */
1054 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001057 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001058 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001059 clear_tv(&var1);
1060
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001061 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 lp->ll_list = lp->ll_tv->vval.v_list;
1063 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1064 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001066 if (lp->ll_n1 < 0)
1067 {
1068 lp->ll_n1 = 0;
1069 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1070 }
1071 }
1072 if (lp->ll_li == NULL)
1073 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001074 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001075 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001076 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001078 }
1079
1080 /*
1081 * May need to find the item or absolute index for the second
1082 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001083 * When no index given: "lp->ll_empty2" is TRUE.
1084 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001086 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001087 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001088 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001089 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001090 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001092 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001095 {
1096 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001097 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001098 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001099 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001100 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001101 }
1102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001103 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001104 if (lp->ll_n1 < 0)
1105 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1106 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001107 {
1108 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001109 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001111 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001112 }
1113
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001114 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 }
1117
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001118 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 return p;
1121}
1122
1123/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001124 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001127clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128{
1129 vim_free(lp->ll_exp_name);
1130 vim_free(lp->ll_newkey);
1131}
1132
1133/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001134 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001136 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1137 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001140set_var_lval(
1141 lval_T *lp,
1142 char_u *endp,
1143 typval_T *rettv,
1144 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001146 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147{
1148 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 listitem_T *ri;
1150 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151
1152 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001154 cc = *endp;
1155 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001156 if (lp->ll_blob != NULL)
1157 {
1158 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001159
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001160 if (op != NULL && *op != '=')
1161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001162 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001163 return;
1164 }
1165
1166 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1167 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001168 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001169
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001170 if (lp->ll_empty2)
1171 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1172
1173 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001175 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001176 return;
1177 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001178 if (lp->ll_empty2)
1179 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001180
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001181 ir = 0;
1182 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1183 blob_set(lp->ll_blob, il,
1184 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001185 }
1186 else
1187 {
1188 val = (int)tv_get_number_chk(rettv, &error);
1189 if (!error)
1190 {
1191 garray_T *gap = &lp->ll_blob->bv_ga;
1192
1193 // Allow for appending a byte. Setting a byte beyond
1194 // the end is an error otherwise.
1195 if (lp->ll_n1 < gap->ga_len
1196 || (lp->ll_n1 == gap->ga_len
1197 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1198 {
1199 blob_set(lp->ll_blob, lp->ll_n1, val);
1200 if (lp->ll_n1 == gap->ga_len)
1201 ++gap->ga_len;
1202 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001203 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001204 }
1205 }
1206 }
1207 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001209 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001212 {
1213 emsg(_(e_cannot_mod));
1214 *endp = cc;
1215 return;
1216 }
1217
Bram Moolenaarff697e62019-02-12 22:28:33 +01001218 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001219 di = NULL;
1220 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1221 &tv, &di, TRUE, FALSE) == OK)
1222 {
1223 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001224 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1225 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001226 && tv_op(&tv, rettv, op) == OK)
1227 set_var(lp->ll_name, &tv, FALSE);
1228 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001231 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001233 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001235 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001236 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001237 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001238 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 else if (lp->ll_range)
1240 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001241 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001242 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001244 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001245 {
1246 emsg(_("E996: Cannot lock a range"));
1247 return;
1248 }
1249
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001250 /*
1251 * Check whether any of the list items is locked
1252 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001253 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001254 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001255 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001256 return;
1257 ri = ri->li_next;
1258 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1259 break;
1260 ll_li = ll_li->li_next;
1261 ++ll_n1;
1262 }
1263
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001264 /*
1265 * Assign the List values to the list items.
1266 */
1267 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001268 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001269 if (op != NULL && *op != '=')
1270 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1271 else
1272 {
1273 clear_tv(&lp->ll_li->li_tv);
1274 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1275 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 ri = ri->li_next;
1277 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1278 break;
1279 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001280 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001281 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001282 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001283 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001284 ri = NULL;
1285 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001286 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001287 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001288 lp->ll_li = lp->ll_li->li_next;
1289 ++lp->ll_n1;
1290 }
1291 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001292 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001293 else if (lp->ll_empty2
1294 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001296 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297 }
1298 else
1299 {
1300 /*
1301 * Assign to a List or Dictionary item.
1302 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001304 {
1305 emsg(_("E996: Cannot lock a list or dict"));
1306 return;
1307 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308 if (lp->ll_newkey != NULL)
1309 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 if (op != NULL && *op != '=')
1311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001312 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001313 return;
1314 }
1315
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001316 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001317 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001318 if (di == NULL)
1319 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001320 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1321 {
1322 vim_free(di);
1323 return;
1324 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001326 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001327 else if (op != NULL && *op != '=')
1328 {
1329 tv_op(lp->ll_tv, rettv, op);
1330 return;
1331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001332 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335 /*
1336 * Assign the value to the variable or list item.
1337 */
1338 if (copy)
1339 copy_tv(rettv, lp->ll_tv);
1340 else
1341 {
1342 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001343 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001344 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 }
1346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001347}
1348
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001349/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001350 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1351 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 * Returns OK or FAIL.
1353 */
1354 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001355tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001356{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001357 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001358 char_u numbuf[NUMBUFLEN];
1359 char_u *s;
1360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001361 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001362 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001363 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 {
1365 switch (tv1->v_type)
1366 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001367 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001368 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001370 case VAR_DICT:
1371 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001372 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001373 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001374 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001375 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001376 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377 break;
1378
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001379 case VAR_BLOB:
1380 if (*op != '+' || tv2->v_type != VAR_BLOB)
1381 break;
1382 // BLOB += BLOB
1383 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1384 {
1385 blob_T *b1 = tv1->vval.v_blob;
1386 blob_T *b2 = tv2->vval.v_blob;
1387 int i, len = blob_len(b2);
1388 for (i = 0; i < len; i++)
1389 ga_append(&b1->bv_ga, blob_get(b2, i));
1390 }
1391 return OK;
1392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001393 case VAR_LIST:
1394 if (*op != '+' || tv2->v_type != VAR_LIST)
1395 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001396 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001397 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1398 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1399 return OK;
1400
1401 case VAR_NUMBER:
1402 case VAR_STRING:
1403 if (tv2->v_type == VAR_LIST)
1404 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001405 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001406 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001407 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001408 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001409#ifdef FEAT_FLOAT
1410 if (tv2->v_type == VAR_FLOAT)
1411 {
1412 float_T f = n;
1413
Bram Moolenaarff697e62019-02-12 22:28:33 +01001414 if (*op == '%')
1415 break;
1416 switch (*op)
1417 {
1418 case '+': f += tv2->vval.v_float; break;
1419 case '-': f -= tv2->vval.v_float; break;
1420 case '*': f *= tv2->vval.v_float; break;
1421 case '/': f /= tv2->vval.v_float; break;
1422 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001423 clear_tv(tv1);
1424 tv1->v_type = VAR_FLOAT;
1425 tv1->vval.v_float = f;
1426 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001428#endif
1429 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001430 switch (*op)
1431 {
1432 case '+': n += tv_get_number(tv2); break;
1433 case '-': n -= tv_get_number(tv2); break;
1434 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001435 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1436 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001438 clear_tv(tv1);
1439 tv1->v_type = VAR_NUMBER;
1440 tv1->vval.v_number = n;
1441 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001442 }
1443 else
1444 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001445 if (tv2->v_type == VAR_FLOAT)
1446 break;
1447
Bram Moolenaarff697e62019-02-12 22:28:33 +01001448 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001449 s = tv_get_string(tv1);
1450 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001451 clear_tv(tv1);
1452 tv1->v_type = VAR_STRING;
1453 tv1->vval.v_string = s;
1454 }
1455 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001456
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001457 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001458#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001459 {
1460 float_T f;
1461
Bram Moolenaarff697e62019-02-12 22:28:33 +01001462 if (*op == '%' || *op == '.'
1463 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001464 && tv2->v_type != VAR_NUMBER
1465 && tv2->v_type != VAR_STRING))
1466 break;
1467 if (tv2->v_type == VAR_FLOAT)
1468 f = tv2->vval.v_float;
1469 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001470 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001471 switch (*op)
1472 {
1473 case '+': tv1->vval.v_float += f; break;
1474 case '-': tv1->vval.v_float -= f; break;
1475 case '*': tv1->vval.v_float *= f; break;
1476 case '/': tv1->vval.v_float /= f; break;
1477 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001478 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001479#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001480 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 }
1482 }
1483
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001484 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001485 return FAIL;
1486}
1487
1488/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001489 * Evaluate the expression used in a ":for var in expr" command.
1490 * "arg" points to "var".
1491 * Set "*errp" to TRUE for an error, FALSE otherwise;
1492 * Return a pointer that holds the info. Null when there is an error.
1493 */
1494 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495eval_for_line(
1496 char_u *arg,
1497 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001498 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001499 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500{
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001502 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001503 typval_T tv;
1504 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001505 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001507 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001508
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001509 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001510 if (fi == NULL)
1511 return NULL;
1512
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001513 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514 if (expr == NULL)
1515 return fi;
1516
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001517 expr = skipwhite_and_linebreak(expr, evalarg);
1518 if (expr[0] != 'i' || expr[1] != 'n'
1519 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001522 return fi;
1523 }
1524
1525 if (skip)
1526 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001527 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1528 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529 {
1530 *errp = FALSE;
1531 if (!skip)
1532 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001533 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001534 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001535 l = tv.vval.v_list;
1536 if (l == NULL)
1537 {
1538 // a null list is like an empty list: do nothing
1539 clear_tv(&tv);
1540 }
1541 else
1542 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001544 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001546 // No need to increment the refcount, it's already set for
1547 // the list being used in "tv".
1548 fi->fi_list = l;
1549 list_add_watch(l, &fi->fi_lw);
1550 fi->fi_lw.lw_item = l->lv_first;
1551 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001552 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001553 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001554 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001555 fi->fi_bi = 0;
1556 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001557 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001558 typval_T btv;
1559
1560 // Make a copy, so that the iteration still works when the
1561 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001563 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001564 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001565 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001566 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567 else
1568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001569 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001570 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001571 }
1572 }
1573 }
1574 if (skip)
1575 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001576 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001577
1578 return fi;
1579}
1580
1581/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001582 * Used when looping over a :for line, skip the "in expr" part.
1583 */
1584 void
1585skip_for_lines(void *fi_void, evalarg_T *evalarg)
1586{
1587 forinfo_T *fi = (forinfo_T *)fi_void;
1588 int i;
1589
1590 for (i = 0; i < fi->fi_break_count; ++i)
1591 eval_next_line(evalarg);
1592}
1593
1594/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001595 * Use the first item in a ":for" list. Advance to the next.
1596 * Assign the values to the variable (list). "arg" points to the first one.
1597 * Return TRUE when a valid item was found, FALSE when at end of list or
1598 * something wrong.
1599 */
1600 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001602{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001603 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001604 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001605 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1606 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001607 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001608
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001609 if (fi->fi_blob != NULL)
1610 {
1611 typval_T tv;
1612
1613 if (fi->fi_bi >= blob_len(fi->fi_blob))
1614 return FALSE;
1615 tv.v_type = VAR_NUMBER;
1616 tv.v_lock = VAR_FIXED;
1617 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1618 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001619 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001620 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001621 }
1622
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 item = fi->fi_lw.lw_item;
1624 if (item == NULL)
1625 result = FALSE;
1626 else
1627 {
1628 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001629 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001630 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001631 }
1632 return result;
1633}
1634
1635/*
1636 * Free the structure used to store info used by ":for".
1637 */
1638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001640{
Bram Moolenaar33570922005-01-25 22:26:29 +00001641 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001643 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001644 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001646 list_unref(fi->fi_list);
1647 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001648 if (fi != NULL && fi->fi_blob != NULL)
1649 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 vim_free(fi);
1651}
1652
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001654set_context_for_expression(
1655 expand_T *xp,
1656 char_u *arg,
1657 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658{
1659 int got_eq = FALSE;
1660 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001663 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 {
1665 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001666 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001668 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001669 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670 {
1671 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001672 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001673 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 break;
1675 }
1676 return;
1677 }
1678 }
1679 else
1680 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1681 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 while ((xp->xp_pattern = vim_strpbrk(arg,
1683 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1684 {
1685 c = *xp->xp_pattern;
1686 if (c == '&')
1687 {
1688 c = xp->xp_pattern[1];
1689 if (c == '&')
1690 {
1691 ++xp->xp_pattern;
1692 xp->xp_context = cmdidx != CMD_let || got_eq
1693 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1694 }
1695 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001698 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1699 xp->xp_pattern += 2;
1700
1701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 else if (c == '$')
1704 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001705 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 xp->xp_context = EXPAND_ENV_VARS;
1707 }
1708 else if (c == '=')
1709 {
1710 got_eq = TRUE;
1711 xp->xp_context = EXPAND_EXPRESSION;
1712 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001713 else if (c == '#'
1714 && xp->xp_context == EXPAND_EXPRESSION)
1715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001716 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001717 break;
1718 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001719 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 && xp->xp_context == EXPAND_FUNCTIONS
1721 && vim_strchr(xp->xp_pattern, '(') == NULL)
1722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001723 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 break;
1725 }
1726 else if (cmdidx != CMD_let || got_eq)
1727 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001728 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 {
1730 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1731 if (c == '\\' && xp->xp_pattern[1] != NUL)
1732 ++xp->xp_pattern;
1733 xp->xp_context = EXPAND_NOTHING;
1734 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001735 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001737 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1739 /* skip */ ;
1740 xp->xp_context = EXPAND_NOTHING;
1741 }
1742 else if (c == '|')
1743 {
1744 if (xp->xp_pattern[1] == '|')
1745 {
1746 ++xp->xp_pattern;
1747 xp->xp_context = EXPAND_EXPRESSION;
1748 }
1749 else
1750 xp->xp_context = EXPAND_COMMANDS;
1751 }
1752 else
1753 xp->xp_context = EXPAND_EXPRESSION;
1754 }
1755 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001756 // Doesn't look like something valid, expand as an expression
1757 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 arg = xp->xp_pattern;
1760 if (*arg != NUL)
1761 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1762 /* skip */ ;
1763 }
1764 xp->xp_pattern = arg;
1765}
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001768 * Return TRUE if "pat" matches "text".
1769 * Does not use 'cpo' and always uses 'magic'.
1770 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001771 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001772pattern_match(char_u *pat, char_u *text, int ic)
1773{
1774 int matches = FALSE;
1775 char_u *save_cpo;
1776 regmatch_T regmatch;
1777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001778 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001779 save_cpo = p_cpo;
1780 p_cpo = (char_u *)"";
1781 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1782 if (regmatch.regprog != NULL)
1783 {
1784 regmatch.rm_ic = ic;
1785 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1786 vim_regfree(regmatch.regprog);
1787 }
1788 p_cpo = save_cpo;
1789 return matches;
1790}
1791
1792/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001793 * Handle a name followed by "(". Both for just "name(arg)" and for
1794 * "expr->name(arg)".
1795 * Returns OK or FAIL.
1796 */
1797 static int
1798eval_func(
1799 char_u **arg, // points to "(", will be advanced
1800 char_u *name,
1801 int name_len,
1802 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001803 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001804 typval_T *basetv) // "expr" for "expr->name(arg)"
1805{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001806 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001807 char_u *s = name;
1808 int len = name_len;
1809 partial_T *partial;
1810 int ret = OK;
1811
1812 if (!evaluate)
1813 check_vars(s, len);
1814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001815 // If "s" is the name of a variable of type VAR_FUNC
1816 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001817 s = deref_func_name(s, &len, &partial, !evaluate);
1818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001819 // Need to make a copy, in case evaluating the arguments makes
1820 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001821 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001822 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001823 ret = FAIL;
1824 else
1825 {
1826 funcexe_T funcexe;
1827
1828 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001829 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001830 funcexe.firstline = curwin->w_cursor.lnum;
1831 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001832 funcexe.evaluate = evaluate;
1833 funcexe.partial = partial;
1834 funcexe.basetv = basetv;
1835 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1836 }
1837 vim_free(s);
1838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001839 // If evaluate is FALSE rettv->v_type was not set in
1840 // get_func_tv, but it's needed in handle_subscript() to parse
1841 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001842 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1843 {
1844 rettv->vval.v_string = NULL;
1845 rettv->v_type = VAR_FUNC;
1846 }
1847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001848 // Stop the expression evaluation when immediately
1849 // aborting on error, or when an interrupt occurred or
1850 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001851 if (evaluate && aborting())
1852 {
1853 if (ret == OK)
1854 clear_tv(rettv);
1855 ret = FAIL;
1856 }
1857 return ret;
1858}
1859
1860/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001861 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1862 * and there is a next line, return the next line (skipping blanks) and set
1863 * "getnext".
1864 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1865 * "arg" must point somewhere inside a line, not at the start.
1866 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001867 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001868eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1869{
1870 *getnext = FALSE;
1871 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1872 && evalarg != NULL
1873 && evalarg->eval_cookie != NULL
1874 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaard5053d02020-06-28 15:51:16 +02001875 && (*arg == '"' || *arg == '#'))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001876 {
Bram Moolenaard5053d02020-06-28 15:51:16 +02001877 char_u *p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001878
1879 if (p != NULL)
1880 {
1881 *getnext = TRUE;
1882 return skipwhite(p);
1883 }
1884 }
1885 return arg;
1886}
1887
1888/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001889 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001890 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001891 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001892eval_next_line(evalarg_T *evalarg)
1893{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001894 garray_T *gap = &evalarg->eval_ga;
1895 char_u *line;
1896
Bram Moolenaard5053d02020-06-28 15:51:16 +02001897 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001898 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001899 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1900 {
1901 // Going to concatenate the lines after parsing.
1902 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1903 ++gap->ga_len;
1904 }
1905 else
1906 {
1907 vim_free(evalarg->eval_tofree);
1908 evalarg->eval_tofree = line;
1909 }
1910 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001911}
1912
Bram Moolenaar9215f012020-06-27 21:18:00 +02001913 char_u *
1914skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1915{
1916 int getnext;
1917 char_u *p = skipwhite(arg);
1918
1919 eval_next_non_blank(p, evalarg, &getnext);
1920 if (getnext)
1921 return eval_next_line(evalarg);
1922 return p;
1923}
1924
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001925/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001926 * After using "evalarg" filled from "eap" free the memory.
1927 */
1928 void
1929clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1930{
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001931 if (evalarg != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001932 {
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001933 if (eap != NULL)
1934 {
1935 // We may need to keep the original command line, e.g. for
1936 // ":let" it has the variable names. But we may also need the
1937 // new one, "nextcmd" points into it. Keep both.
1938 vim_free(eap->cmdline_tofree);
1939 eap->cmdline_tofree = *eap->cmdlinep;
1940 *eap->cmdlinep = evalarg->eval_tofree;
1941 }
1942 else
1943 vim_free(evalarg->eval_tofree);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001944 evalarg->eval_tofree = NULL;
1945 }
1946}
1947
1948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1952 */
1953
1954/*
1955 * Handle zero level expression.
1956 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001958 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001959 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 * Return OK or FAIL.
1961 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001963eval0(
1964 char_u *arg,
1965 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001966 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001967 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968{
1969 int ret;
1970 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001971 int did_emsg_before = did_emsg;
1972 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001973 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001976 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001977
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001978 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 /*
1983 * Report the invalid expression unless the expression evaluation has
1984 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001985 * exception, or we already gave a more specific error.
1986 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001988 if (!aborting()
1989 && did_emsg == did_emsg_before
1990 && called_emsg == called_emsg_before
1991 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001992 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 ret = FAIL;
1994 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001995
1996 if (eap != NULL)
1997 eap->nextcmd = check_nextcmd(p);
1998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 return ret;
2000}
2001
2002/*
2003 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002004 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 *
2006 * "arg" must point to the first non-white of the expression.
2007 * "arg" is advanced to the next non-white after the recognized expression.
2008 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002009 * Note: "rettv.v_lock" is not set.
2010 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 * Return OK or FAIL.
2012 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002013 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002014eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002016 char_u *p;
2017 int getnext;
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 /*
2020 * Get the first variable.
2021 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002022 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 return FAIL;
2024
Bram Moolenaar793648f2020-06-26 21:28:25 +02002025 p = eval_next_non_blank(*arg, evalarg, &getnext);
2026 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002028 int result;
2029 typval_T var2;
2030 evalarg_T nested_evalarg;
2031 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002032 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002033
Bram Moolenaar793648f2020-06-26 21:28:25 +02002034 if (getnext)
2035 *arg = eval_next_line(evalarg);
2036
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002037 if (evalarg == NULL)
2038 {
2039 CLEAR_FIELD(nested_evalarg);
2040 orig_flags = 0;
2041 }
2042 else
2043 {
2044 nested_evalarg = *evalarg;
2045 orig_flags = evalarg->eval_flags;
2046 }
2047
Bram Moolenaar7acde512020-06-24 23:02:40 +02002048 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002050 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002052 int error = FALSE;
2053
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002054 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002057 if (error)
2058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060
2061 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002062 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002064 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002065 nested_evalarg.eval_flags = result ? orig_flags
2066 : orig_flags & ~EVAL_EVALUATE;
2067 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 return FAIL;
2069
2070 /*
2071 * Check for the ":".
2072 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002073 p = eval_next_non_blank(*arg, evalarg, &getnext);
2074 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 return FAIL;
2080 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002081 if (getnext)
2082 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
2084 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002085 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002087 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088 nested_evalarg.eval_flags = !result ? orig_flags
2089 : orig_flags & ~EVAL_EVALUATE;
2090 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
2092 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 return FAIL;
2095 }
2096 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
2099
2100 return OK;
2101}
2102
2103/*
2104 * Handle first level expression:
2105 * expr2 || expr2 || expr2 logical OR
2106 *
2107 * "arg" must point to the first non-white of the expression.
2108 * "arg" is advanced to the next non-white after the recognized expression.
2109 *
2110 * Return OK or FAIL.
2111 */
2112 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002115 char_u *p;
2116 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 long result;
2119 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002120 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122 /*
2123 * Get the first variable.
2124 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002125 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 return FAIL;
2127
2128 /*
2129 * Repeat until there is no following "||".
2130 */
2131 first = TRUE;
2132 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002133 p = eval_next_non_blank(*arg, evalarg, &getnext);
2134 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136 evalarg_T nested_evalarg;
2137 int evaluate;
2138 int orig_flags;
2139
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002140 if (getnext)
2141 *arg = eval_next_line(evalarg);
2142
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002143 if (evalarg == NULL)
2144 {
2145 CLEAR_FIELD(nested_evalarg);
2146 orig_flags = 0;
2147 evaluate = FALSE;
2148 }
2149 else
2150 {
2151 nested_evalarg = *evalarg;
2152 orig_flags = evalarg->eval_flags;
2153 evaluate = orig_flags & EVAL_EVALUATE;
2154 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (evaluate && first)
2157 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002158 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002161 if (error)
2162 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 first = FALSE;
2164 }
2165
2166 /*
2167 * Get the second variable.
2168 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002169 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002170 nested_evalarg.eval_flags = !result ? orig_flags
2171 : orig_flags & ~EVAL_EVALUATE;
2172 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 return FAIL;
2174
2175 /*
2176 * Compute the result.
2177 */
2178 if (evaluate && !result)
2179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002180 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002183 if (error)
2184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186 if (evaluate)
2187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 rettv->v_type = VAR_NUMBER;
2189 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002191
2192 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
2194
2195 return OK;
2196}
2197
2198/*
2199 * Handle second level expression:
2200 * expr3 && expr3 && expr3 logical AND
2201 *
2202 * "arg" must point to the first non-white of the expression.
2203 * "arg" is advanced to the next non-white after the recognized expression.
2204 *
2205 * Return OK or FAIL.
2206 */
2207 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002208eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002210 char_u *p;
2211 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002212 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 long result;
2214 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002215 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217 /*
2218 * Get the first variable.
2219 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002220 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 return FAIL;
2222
2223 /*
2224 * Repeat until there is no following "&&".
2225 */
2226 first = TRUE;
2227 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002228 p = eval_next_non_blank(*arg, evalarg, &getnext);
2229 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002231 evalarg_T nested_evalarg;
2232 int orig_flags;
2233 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002234
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002235 if (getnext)
2236 *arg = eval_next_line(evalarg);
2237
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002238 if (evalarg == NULL)
2239 {
2240 CLEAR_FIELD(nested_evalarg);
2241 orig_flags = 0;
2242 evaluate = FALSE;
2243 }
2244 else
2245 {
2246 nested_evalarg = *evalarg;
2247 orig_flags = evalarg->eval_flags;
2248 evaluate = orig_flags & EVAL_EVALUATE;
2249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (evaluate && first)
2251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002252 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002255 if (error)
2256 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 first = FALSE;
2258 }
2259
2260 /*
2261 * Get the second variable.
2262 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002263 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002264 nested_evalarg.eval_flags = result ? orig_flags
2265 : orig_flags & ~EVAL_EVALUATE;
2266 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 return FAIL;
2268
2269 /*
2270 * Compute the result.
2271 */
2272 if (evaluate && result)
2273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002274 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 if (error)
2278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280 if (evaluate)
2281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 rettv->v_type = VAR_NUMBER;
2283 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002285
2286 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288
2289 return OK;
2290}
2291
2292/*
2293 * Handle third level expression:
2294 * var1 == var2
2295 * var1 =~ var2
2296 * var1 != var2
2297 * var1 !~ var2
2298 * var1 > var2
2299 * var1 >= var2
2300 * var1 < var2
2301 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002302 * var1 is var2
2303 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 *
2305 * "arg" must point to the first non-white of the expression.
2306 * "arg" is advanced to the next non-white after the recognized expression.
2307 *
2308 * Return OK or FAIL.
2309 */
2310 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
Bram Moolenaar33570922005-01-25 22:26:29 +00002313 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002315 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002317 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321 /*
2322 * Get the first variable.
2323 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 return FAIL;
2326
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002327 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 switch (p[0])
2329 {
2330 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002331 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002333 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 break;
2335 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002336 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002338 type = EXPR_NOMATCH;
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_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 len = 1;
2344 }
2345 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002346 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 break;
2348 case '<': if (p[1] != '=')
2349 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002350 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 len = 1;
2352 }
2353 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002354 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002356 case 'i': if (p[1] == 's')
2357 {
2358 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2359 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002360 i = p[len];
2361 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002362 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002363 }
2364 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366
2367 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002368 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002370 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002372 if (getnext)
2373 *arg = eval_next_line(evalarg);
2374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002375 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (p[len] == '?')
2377 {
2378 ic = TRUE;
2379 ++len;
2380 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002381 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 else if (p[len] == '#')
2383 {
2384 ic = FALSE;
2385 ++len;
2386 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002387 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 else
2389 ic = p_ic;
2390
2391 /*
2392 * Get the second variable.
2393 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002394 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002395 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 return FAIL;
2399 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002400 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002401 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002402 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002403
2404 clear_tv(&var2);
2405 return ret;
2406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408
2409 return OK;
2410}
2411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 void
2413eval_addblob(typval_T *tv1, typval_T *tv2)
2414{
2415 blob_T *b1 = tv1->vval.v_blob;
2416 blob_T *b2 = tv2->vval.v_blob;
2417 blob_T *b = blob_alloc();
2418 int i;
2419
2420 if (b != NULL)
2421 {
2422 for (i = 0; i < blob_len(b1); i++)
2423 ga_append(&b->bv_ga, blob_get(b1, i));
2424 for (i = 0; i < blob_len(b2); i++)
2425 ga_append(&b->bv_ga, blob_get(b2, i));
2426
2427 clear_tv(tv1);
2428 rettv_blob_set(tv1, b);
2429 }
2430}
2431
2432 int
2433eval_addlist(typval_T *tv1, typval_T *tv2)
2434{
2435 typval_T var3;
2436
2437 // concatenate Lists
2438 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2439 {
2440 clear_tv(tv1);
2441 clear_tv(tv2);
2442 return FAIL;
2443 }
2444 clear_tv(tv1);
2445 *tv1 = var3;
2446 return OK;
2447}
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449/*
2450 * Handle fourth level expression:
2451 * + number addition
2452 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002453 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002454 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 *
2456 * "arg" must point to the first non-white of the expression.
2457 * "arg" is advanced to the next non-white after the recognized expression.
2458 *
2459 * Return OK or FAIL.
2460 */
2461 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002462eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002464 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 /*
2467 * Get the first variable.
2468 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 return FAIL;
2471
2472 /*
2473 * Repeat computing, until no '+', '-' or '.' is following.
2474 */
2475 for (;;)
2476 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002477 int getnext;
2478 char_u *p;
2479 int op;
2480 int concat;
2481 typval_T var2;
2482
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002483 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484 p = eval_next_non_blank(*arg, evalarg, &getnext);
2485 op = *p;
2486 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002487 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002489 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002490 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002492 if ((op != '+' || (rettv->v_type != VAR_LIST
2493 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002494#ifdef FEAT_FLOAT
2495 && (op == '.' || rettv->v_type != VAR_FLOAT)
2496#endif
2497 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002499 // For "list + ...", an illegal use of the first operand as
2500 // a number cannot be determined before evaluating the 2nd
2501 // operand: if this is also a list, all is ok.
2502 // For "something . ...", "something - ..." or "non-list + ...",
2503 // we know that the first operand needs to be a string or number
2504 // without evaluating the 2nd operand. So check before to avoid
2505 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002506 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 {
2508 clear_tv(rettv);
2509 return FAIL;
2510 }
2511 }
2512
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 /*
2514 * Get the second variable.
2515 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002516 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2517 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002518 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002519 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 return FAIL;
2523 }
2524
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002525 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 /*
2528 * Compute the result.
2529 */
2530 if (op == '.')
2531 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002532 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2533 char_u *s1 = tv_get_string_buf(rettv, buf1);
2534 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002536 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 {
2538 clear_tv(rettv);
2539 clear_tv(&var2);
2540 return FAIL;
2541 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 clear_tv(rettv);
2544 rettv->v_type = VAR_STRING;
2545 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002547 else if (op == '+' && rettv->v_type == VAR_BLOB
2548 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002550 else if (op == '+' && rettv->v_type == VAR_LIST
2551 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002552 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002554 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 else
2557 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002558 int error = FALSE;
2559 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002560#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002561 float_T f1 = 0, f2 = 0;
2562
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002563 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002564 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002565 f1 = rettv->vval.v_float;
2566 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002567 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002568 else
2569#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002571 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002572 if (error)
2573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002574 // This can only happen for "list + non-list". For
2575 // "non-list + ..." or "something - ...", we returned
2576 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002577 clear_tv(rettv);
2578 return FAIL;
2579 }
2580#ifdef FEAT_FLOAT
2581 if (var2.v_type == VAR_FLOAT)
2582 f1 = n1;
2583#endif
2584 }
2585#ifdef FEAT_FLOAT
2586 if (var2.v_type == VAR_FLOAT)
2587 {
2588 f2 = var2.vval.v_float;
2589 n2 = 0;
2590 }
2591 else
2592#endif
2593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002594 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002595 if (error)
2596 {
2597 clear_tv(rettv);
2598 clear_tv(&var2);
2599 return FAIL;
2600 }
2601#ifdef FEAT_FLOAT
2602 if (rettv->v_type == VAR_FLOAT)
2603 f2 = n2;
2604#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002605 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002607
2608#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002609 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002610 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2611 {
2612 if (op == '+')
2613 f1 = f1 + f2;
2614 else
2615 f1 = f1 - f2;
2616 rettv->v_type = VAR_FLOAT;
2617 rettv->vval.v_float = f1;
2618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002620#endif
2621 {
2622 if (op == '+')
2623 n1 = n1 + n2;
2624 else
2625 n1 = n1 - n2;
2626 rettv->v_type = VAR_NUMBER;
2627 rettv->vval.v_number = n1;
2628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632 }
2633 return OK;
2634}
2635
2636/*
2637 * Handle fifth level expression:
2638 * * number multiplication
2639 * / number division
2640 * % number modulo
2641 *
2642 * "arg" must point to the first non-white of the expression.
2643 * "arg" is advanced to the next non-white after the recognized expression.
2644 *
2645 * Return OK or FAIL.
2646 */
2647 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002648eval6(
2649 char_u **arg,
2650 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002651 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653{
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002656 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002657#ifdef FEAT_FLOAT
2658 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002659 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002660#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002661 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
2663 /*
2664 * Get the first variable.
2665 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002666 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 return FAIL;
2668
2669 /*
2670 * Repeat computing, until no '*', '/' or '%' is following.
2671 */
2672 for (;;)
2673 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002674 int evaluate = evalarg == NULL ? 0
2675 : (evalarg->eval_flags & EVAL_EVALUATE);
2676 int getnext;
2677
2678 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002679 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002681 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002682 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002684 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002686#ifdef FEAT_FLOAT
2687 if (rettv->v_type == VAR_FLOAT)
2688 {
2689 f1 = rettv->vval.v_float;
2690 use_float = TRUE;
2691 n1 = 0;
2692 }
2693 else
2694#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002695 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002696 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002697 if (error)
2698 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700 else
2701 n1 = 0;
2702
2703 /*
2704 * Get the second variable.
2705 */
2706 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002707 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 return FAIL;
2709
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002710 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002712#ifdef FEAT_FLOAT
2713 if (var2.v_type == VAR_FLOAT)
2714 {
2715 if (!use_float)
2716 {
2717 f1 = n1;
2718 use_float = TRUE;
2719 }
2720 f2 = var2.vval.v_float;
2721 n2 = 0;
2722 }
2723 else
2724#endif
2725 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002726 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002727 clear_tv(&var2);
2728 if (error)
2729 return FAIL;
2730#ifdef FEAT_FLOAT
2731 if (use_float)
2732 f2 = n2;
2733#endif
2734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 /*
2737 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002738 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002740#ifdef FEAT_FLOAT
2741 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002743 if (op == '*')
2744 f1 = f1 * f2;
2745 else if (op == '/')
2746 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002747# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002748 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002749 if (f2 == 0.0)
2750 {
2751 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002752 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002753 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002754 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002755 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002756 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002757 }
2758 else
2759 f1 = f1 / f2;
2760# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002761 // We rely on the floating point library to handle divide
2762 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002763 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002764# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002767 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002769 return FAIL;
2770 }
2771 rettv->v_type = VAR_FLOAT;
2772 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
2774 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002777 if (op == '*')
2778 n1 = n1 * n2;
2779 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002780 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002782 n1 = num_modulus(n1, n2);
2783
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002784 rettv->v_type = VAR_NUMBER;
2785 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 }
2788 }
2789
2790 return OK;
2791}
2792
2793/*
2794 * Handle sixth level expression:
2795 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002796 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002797 * "string" string constant
2798 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 * &option-name option value
2800 * @r register contents
2801 * identifier variable value
2802 * function() function call
2803 * $VAR environment variable
2804 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002805 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002807 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002808 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 *
2810 * Also handle:
2811 * ! in front logical NOT
2812 * - in front unary minus
2813 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 * trailing [] subscript in String or List
2815 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002816 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 *
2818 * "arg" must point to the first non-white of the expression.
2819 * "arg" is advanced to the next non-white after the recognized expression.
2820 *
2821 * Return OK or FAIL.
2822 */
2823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002824eval7(
2825 char_u **arg,
2826 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002827 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002830 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2831 int evaluate = evalarg != NULL
2832 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 int len;
2834 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 char_u *start_leader, *end_leader;
2836 int ret = OK;
2837 char_u *alias;
2838
2839 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002840 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002841 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002843 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
2845 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002846 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 */
2848 start_leader = *arg;
2849 while (**arg == '!' || **arg == '-' || **arg == '+')
2850 *arg = skipwhite(*arg + 1);
2851 end_leader = *arg;
2852
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002853 if (**arg == '.' && (!isdigit(*(*arg + 1))
2854#ifdef FEAT_FLOAT
2855 || current_sctx.sc_version < 2
2856#endif
2857 ))
2858 {
2859 semsg(_(e_invexpr2), *arg);
2860 ++*arg;
2861 return FAIL;
2862 }
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 switch (**arg)
2865 {
2866 /*
2867 * Number constant.
2868 */
2869 case '0':
2870 case '1':
2871 case '2':
2872 case '3':
2873 case '4':
2874 case '5':
2875 case '6':
2876 case '7':
2877 case '8':
2878 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002880
2881 // Apply prefixed "-" and "+" now. Matters especially when
2882 // "->" follows.
2883 if (ret == OK && evaluate && end_leader > start_leader)
2884 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 break;
2886
2887 /*
2888 * String constant: "string".
2889 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002890 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 break;
2892
2893 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002896 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002897 break;
2898
2899 /*
2900 * List: [expr, expr]
2901 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002902 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 break;
2904
2905 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002906 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002907 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002908 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002909 {
2910 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002911 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002912 }
2913 else
2914 ret = NOTDONE;
2915 break;
2916
2917 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002918 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002919 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002921 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002922 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002923 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002924 break;
2925
2926 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002927 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002929 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 break;
2931
2932 /*
2933 * Environment variable: $VAR.
2934 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 break;
2937
2938 /*
2939 * Register contents: @r.
2940 */
2941 case '@': ++*arg;
2942 if (evaluate)
2943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002945 rettv->vval.v_string = get_reg_contents(**arg,
2946 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 if (**arg != NUL)
2949 ++*arg;
2950 break;
2951
2952 /*
2953 * nested expression: (expression).
2954 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002955 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002956 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002957 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002958
Bram Moolenaar9215f012020-06-27 21:18:00 +02002959 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002960 if (**arg == ')')
2961 ++*arg;
2962 else if (ret == OK)
2963 {
2964 emsg(_(e_missing_close));
2965 clear_tv(rettv);
2966 ret = FAIL;
2967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
2969 break;
2970
Bram Moolenaar8c711452005-01-14 21:53:12 +00002971 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 break;
2973 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002974
2975 if (ret == NOTDONE)
2976 {
2977 /*
2978 * Must be a variable or function name.
2979 * Can also be a curly-braces kind of name: {expr}.
2980 */
2981 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002982 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002983 if (alias != NULL)
2984 s = alias;
2985
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002986 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002987 ret = FAIL;
2988 else
2989 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002990 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002991 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002992 else if (flags & EVAL_CONSTANT)
2993 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002994 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002995 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002997 {
2998 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003000 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003001 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003002 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003003 }
3004
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 *arg = skipwhite(*arg);
3006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003007 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3008 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003009 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003010 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 /*
3013 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3014 */
3015 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003016 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003017 return ret;
3018}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003019
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003020/*
3021 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003022 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003023 * Adjusts "end_leaderp" until it is at "start_leader".
3024 */
3025 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003026eval7_leader(
3027 typval_T *rettv,
3028 int numeric_only,
3029 char_u *start_leader,
3030 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003031{
3032 char_u *end_leader = *end_leaderp;
3033 int ret = OK;
3034 int error = FALSE;
3035 varnumber_T val = 0;
3036#ifdef FEAT_FLOAT
3037 float_T f = 0.0;
3038
3039 if (rettv->v_type == VAR_FLOAT)
3040 f = rettv->vval.v_float;
3041 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003043 val = tv_get_number_chk(rettv, &error);
3044 if (error)
3045 {
3046 clear_tv(rettv);
3047 ret = FAIL;
3048 }
3049 else
3050 {
3051 while (end_leader > start_leader)
3052 {
3053 --end_leader;
3054 if (*end_leader == '!')
3055 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003056 if (numeric_only)
3057 {
3058 ++end_leader;
3059 break;
3060 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003061#ifdef FEAT_FLOAT
3062 if (rettv->v_type == VAR_FLOAT)
3063 f = !f;
3064 else
3065#endif
3066 val = !val;
3067 }
3068 else if (*end_leader == '-')
3069 {
3070#ifdef FEAT_FLOAT
3071 if (rettv->v_type == VAR_FLOAT)
3072 f = -f;
3073 else
3074#endif
3075 val = -val;
3076 }
3077 }
3078#ifdef FEAT_FLOAT
3079 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003081 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003082 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003084 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003085#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003086 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003087 clear_tv(rettv);
3088 rettv->v_type = VAR_NUMBER;
3089 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003092 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 return ret;
3094}
3095
3096/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003097 * Call the function referred to in "rettv".
3098 */
3099 static int
3100call_func_rettv(
3101 char_u **arg,
3102 typval_T *rettv,
3103 int evaluate,
3104 dict_T *selfdict,
3105 typval_T *basetv)
3106{
3107 partial_T *pt = NULL;
3108 funcexe_T funcexe;
3109 typval_T functv;
3110 char_u *s;
3111 int ret;
3112
3113 // need to copy the funcref so that we can clear rettv
3114 if (evaluate)
3115 {
3116 functv = *rettv;
3117 rettv->v_type = VAR_UNKNOWN;
3118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003119 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003120 if (functv.v_type == VAR_PARTIAL)
3121 {
3122 pt = functv.vval.v_partial;
3123 s = partial_name(pt);
3124 }
3125 else
3126 s = functv.vval.v_string;
3127 }
3128 else
3129 s = (char_u *)"";
3130
Bram Moolenaara80faa82020-04-12 19:37:17 +02003131 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003132 funcexe.firstline = curwin->w_cursor.lnum;
3133 funcexe.lastline = curwin->w_cursor.lnum;
3134 funcexe.evaluate = evaluate;
3135 funcexe.partial = pt;
3136 funcexe.selfdict = selfdict;
3137 funcexe.basetv = basetv;
3138 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003140 // Clear the funcref afterwards, so that deleting it while
3141 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003142 if (evaluate)
3143 clear_tv(&functv);
3144
3145 return ret;
3146}
3147
3148/*
3149 * Evaluate "->method()".
3150 * "*arg" points to the '-'.
3151 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3152 */
3153 static int
3154eval_lambda(
3155 char_u **arg,
3156 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003157 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003158 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003159{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003160 int evaluate = evalarg != NULL
3161 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003162 typval_T base = *rettv;
3163 int ret;
3164
3165 // Skip over the ->.
3166 *arg += 2;
3167 rettv->v_type = VAR_UNKNOWN;
3168
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003169 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003170 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003171 return FAIL;
3172 else if (**arg != '(')
3173 {
3174 if (verbose)
3175 {
3176 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003177 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003178 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003180 }
3181 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003182 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003183 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003184 else
3185 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3186
3187 // Clear the funcref afterwards, so that deleting it while
3188 // evaluating the arguments is possible (see test55).
3189 if (evaluate)
3190 clear_tv(&base);
3191
3192 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003193}
3194
3195/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003196 * Evaluate "->method()".
3197 * "*arg" points to the '-'.
3198 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3199 */
3200 static int
3201eval_method(
3202 char_u **arg,
3203 typval_T *rettv,
3204 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003205 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003206{
3207 char_u *name;
3208 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003209 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003210 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003211 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003212
3213 // Skip over the ->.
3214 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003215 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003216
Bram Moolenaarac92e252019-08-03 21:58:38 +02003217 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003218 len = get_name_len(arg, &alias, evaluate, TRUE);
3219 if (alias != NULL)
3220 name = alias;
3221
3222 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003223 {
3224 if (verbose)
3225 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003226 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003227 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003228 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003229 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003230 if (**arg != '(')
3231 {
3232 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003234 ret = FAIL;
3235 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003236 else if (VIM_ISWHITE((*arg)[-1]))
3237 {
3238 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003239 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003240 ret = FAIL;
3241 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003242 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003243 ret = eval_func(arg, name, len, rettv,
3244 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003245 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003246
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003247 // Clear the funcref afterwards, so that deleting it while
3248 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003249 if (evaluate)
3250 clear_tv(&base);
3251
Bram Moolenaarac92e252019-08-03 21:58:38 +02003252 return ret;
3253}
3254
3255/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003256 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3257 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003258 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3259 */
3260 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003261eval_index(
3262 char_u **arg,
3263 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003264 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003265 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003266{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003267 int evaluate = evalarg != NULL
3268 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003269 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003271 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003272 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003273 long len = -1;
3274 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003275 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003276 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003277
Bram Moolenaara03f2332016-02-06 18:09:59 +01003278 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003279 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003280 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003281 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003282 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003283 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003284 return FAIL;
3285 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003286#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003287 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003288 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003289 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003290#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003291 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003292 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003293 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003294 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003295 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003296 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003297 return FAIL;
3298 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003299 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003301 if (evaluate)
3302 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003303 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003304
3305 case VAR_STRING:
3306 case VAR_NUMBER:
3307 case VAR_LIST:
3308 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003309 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003310 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003312
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003313 init_tv(&var1);
3314 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003315 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003316 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003317 /*
3318 * dict.name
3319 */
3320 key = *arg + 1;
3321 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3322 ;
3323 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003324 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003325 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003326 }
3327 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003328 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003329 /*
3330 * something[idx]
3331 *
3332 * Get the (first) variable from inside the [].
3333 */
3334 *arg = skipwhite(*arg + 1);
3335 if (**arg == ':')
3336 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003337 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003338 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003339 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003341 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003342 clear_tv(&var1);
3343 return FAIL;
3344 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003345
3346 /*
3347 * Get the second variable from inside the [:].
3348 */
3349 if (**arg == ':')
3350 {
3351 range = TRUE;
3352 *arg = skipwhite(*arg + 1);
3353 if (**arg == ']')
3354 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003355 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003357 if (!empty1)
3358 clear_tv(&var1);
3359 return FAIL;
3360 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003361 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003362 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003363 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003364 if (!empty1)
3365 clear_tv(&var1);
3366 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003367 return FAIL;
3368 }
3369 }
3370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003371 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003372 if (**arg != ']')
3373 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003374 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003375 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003376 clear_tv(&var1);
3377 if (range)
3378 clear_tv(&var2);
3379 return FAIL;
3380 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003381 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003382 }
3383
3384 if (evaluate)
3385 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003386 n1 = 0;
3387 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003388 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003389 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003390 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003391 }
3392 if (range)
3393 {
3394 if (empty2)
3395 n2 = -1;
3396 else
3397 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003398 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003400 }
3401 }
3402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003403 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003404 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003405 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003406 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003408 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003409 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003410 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003411 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003412 case VAR_SPECIAL:
3413 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003414 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003415 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003416
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003417 case VAR_NUMBER:
3418 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003419 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003420 len = (long)STRLEN(s);
3421 if (range)
3422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003423 // The resulting variable is a substring. If the indexes
3424 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003425 if (n1 < 0)
3426 {
3427 n1 = len + n1;
3428 if (n1 < 0)
3429 n1 = 0;
3430 }
3431 if (n2 < 0)
3432 n2 = len + n2;
3433 else if (n2 >= len)
3434 n2 = len;
3435 if (n1 >= len || n2 < 0 || n1 > n2)
3436 s = NULL;
3437 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003438 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003439 }
3440 else
3441 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003442 // The resulting variable is a string of a single
3443 // character. If the index is too big or negative the
3444 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003445 if (n1 >= len || n1 < 0)
3446 s = NULL;
3447 else
3448 s = vim_strnsave(s + n1, 1);
3449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 clear_tv(rettv);
3451 rettv->v_type = VAR_STRING;
3452 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003453 break;
3454
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003455 case VAR_BLOB:
3456 len = blob_len(rettv->vval.v_blob);
3457 if (range)
3458 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003459 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003460 // are out of range the result is empty.
3461 if (n1 < 0)
3462 {
3463 n1 = len + n1;
3464 if (n1 < 0)
3465 n1 = 0;
3466 }
3467 if (n2 < 0)
3468 n2 = len + n2;
3469 else if (n2 >= len)
3470 n2 = len - 1;
3471 if (n1 >= len || n2 < 0 || n1 > n2)
3472 {
3473 clear_tv(rettv);
3474 rettv->v_type = VAR_BLOB;
3475 rettv->vval.v_blob = NULL;
3476 }
3477 else
3478 {
3479 blob_T *blob = blob_alloc();
3480
3481 if (blob != NULL)
3482 {
3483 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3484 {
3485 blob_free(blob);
3486 return FAIL;
3487 }
3488 blob->bv_ga.ga_len = n2 - n1 + 1;
3489 for (i = n1; i <= n2; i++)
3490 blob_set(blob, i - n1,
3491 blob_get(rettv->vval.v_blob, i));
3492
3493 clear_tv(rettv);
3494 rettv_blob_set(rettv, blob);
3495 }
3496 }
3497 }
3498 else
3499 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003500 // The resulting variable is a byte value.
3501 // If the index is too big or negative that is an error.
3502 if (n1 < 0)
3503 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003504 if (n1 < len && n1 >= 0)
3505 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003506 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003507
3508 clear_tv(rettv);
3509 rettv->v_type = VAR_NUMBER;
3510 rettv->vval.v_number = v;
3511 }
3512 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003513 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003514 }
3515 break;
3516
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003517 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003518 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003519 if (n1 < 0)
3520 n1 = len + n1;
3521 if (!empty1 && (n1 < 0 || n1 >= len))
3522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003523 // For a range we allow invalid values and return an empty
3524 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003525 if (!range)
3526 {
3527 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003528 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003529 return FAIL;
3530 }
3531 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003532 }
3533 if (range)
3534 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003535 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003536
3537 if (n2 < 0)
3538 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003539 else if (n2 >= len)
3540 n2 = len - 1;
3541 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003542 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003543 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 if (l == NULL)
3545 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003546 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003547 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 }
3549 else
3550 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003551 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003552 clear_tv(rettv);
3553 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003554 }
3555 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003556
3557 case VAR_DICT:
3558 if (range)
3559 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003560 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003561 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003562 if (len == -1)
3563 clear_tv(&var1);
3564 return FAIL;
3565 }
3566 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003567 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003568
3569 if (len == -1)
3570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003571 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003572 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003574 clear_tv(&var1);
3575 return FAIL;
3576 }
3577 }
3578
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003579 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003581 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003582 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 if (len == -1)
3584 clear_tv(&var1);
3585 if (item == NULL)
3586 return FAIL;
3587
3588 copy_tv(&item->di_tv, &var1);
3589 clear_tv(rettv);
3590 *rettv = var1;
3591 }
3592 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003593 }
3594 }
3595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 return OK;
3597}
3598
3599/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003600 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003601 */
3602 char_u *
3603partial_name(partial_T *pt)
3604{
3605 if (pt->pt_name != NULL)
3606 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003607 if (pt->pt_func != NULL)
3608 return pt->pt_func->uf_name;
3609 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003610}
3611
Bram Moolenaarddecc252016-04-06 22:59:37 +02003612 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003613partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003614{
3615 int i;
3616
3617 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003618 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003619 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003620 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003621 if (pt->pt_name != NULL)
3622 {
3623 func_unref(pt->pt_name);
3624 vim_free(pt->pt_name);
3625 }
3626 else
3627 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003628
3629 if (pt->pt_funcstack != NULL)
3630 {
3631 // Decrease the reference count for the context of a closure. If down
3632 // to zero free it and clear the variables on the stack.
3633 if (--pt->pt_funcstack->fs_refcount == 0)
3634 {
3635 garray_T *gap = &pt->pt_funcstack->fs_ga;
3636 typval_T *stack = gap->ga_data;
3637
3638 for (i = 0; i < gap->ga_len; ++i)
3639 clear_tv(stack + i);
3640 ga_clear(gap);
3641 vim_free(pt->pt_funcstack);
3642 }
3643 pt->pt_funcstack = NULL;
3644 }
3645
Bram Moolenaarddecc252016-04-06 22:59:37 +02003646 vim_free(pt);
3647}
3648
3649/*
3650 * Unreference a closure: decrement the reference count and free it when it
3651 * becomes zero.
3652 */
3653 void
3654partial_unref(partial_T *pt)
3655{
3656 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003657 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003658}
3659
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003660/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003661 * Return the next (unique) copy ID.
3662 * Used for serializing nested structures.
3663 */
3664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003665get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003666{
3667 current_copyID += COPYID_INC;
3668 return current_copyID;
3669}
3670
3671/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003672 * Garbage collection for lists and dictionaries.
3673 *
3674 * We use reference counts to be able to free most items right away when they
3675 * are no longer used. But for composite items it's possible that it becomes
3676 * unused while the reference count is > 0: When there is a recursive
3677 * reference. Example:
3678 * :let l = [1, 2, 3]
3679 * :let d = {9: l}
3680 * :let l[1] = d
3681 *
3682 * Since this is quite unusual we handle this with garbage collection: every
3683 * once in a while find out which lists and dicts are not referenced from any
3684 * variable.
3685 *
3686 * Here is a good reference text about garbage collection (refers to Python
3687 * but it applies to all reference-counting mechanisms):
3688 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003689 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003690
3691/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003692 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003693 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003694 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003695 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003696 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003697garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003698{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003699 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003700 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003701 buf_T *buf;
3702 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003703 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003704 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003705
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003706 if (!testing)
3707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003708 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003709 want_garbage_collect = FALSE;
3710 may_garbage_collect = FALSE;
3711 garbage_collect_at_exit = FALSE;
3712 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003713
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003714 // The execution stack can grow big, limit the size.
3715 if (exestack.ga_maxlen - exestack.ga_len > 500)
3716 {
3717 size_t new_len;
3718 char_u *pp;
3719 int n;
3720
3721 // Keep 150% of the current size, with a minimum of the growth size.
3722 n = exestack.ga_len / 2;
3723 if (n < exestack.ga_growsize)
3724 n = exestack.ga_growsize;
3725
3726 // Don't make it bigger though.
3727 if (exestack.ga_len + n < exestack.ga_maxlen)
3728 {
3729 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3730 pp = vim_realloc(exestack.ga_data, new_len);
3731 if (pp == NULL)
3732 return FAIL;
3733 exestack.ga_maxlen = exestack.ga_len + n;
3734 exestack.ga_data = pp;
3735 }
3736 }
3737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003738 // We advance by two because we add one for items referenced through
3739 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003740 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003741
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003742 /*
3743 * 1. Go through all accessible variables and mark all lists and dicts
3744 * with copyID.
3745 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003746
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003747 // Don't free variables in the previous_funccal list unless they are only
3748 // referenced through previous_funccal. This must be first, because if
3749 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003750 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003753 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003755 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003756 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003757 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3758 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003760 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003761 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003762 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3763 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003764 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003765 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3766 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003767#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003768 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003769 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3770 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003771 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003772 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003773 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3774 NULL, NULL);
3775#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003776
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003777 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003778 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003779 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3780 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003781 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003782 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003784 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003785 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003787 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003788 abort = abort || set_ref_in_functions(copyID);
3789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003790 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003793 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003794 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003795
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003796 // callbacks in buffers
3797 abort = abort || set_ref_in_buffers(copyID);
3798
Bram Moolenaar1dced572012-04-05 16:54:08 +02003799#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003800 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003801#endif
3802
Bram Moolenaardb913952012-06-29 12:54:53 +02003803#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003804 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003805#endif
3806
3807#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003808 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003809#endif
3810
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003811#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003812 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003813 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003814#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003815#ifdef FEAT_NETBEANS_INTG
3816 abort = abort || set_ref_in_nb_channel(copyID);
3817#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003818
Bram Moolenaare3188e22016-05-31 21:13:04 +02003819#ifdef FEAT_TIMERS
3820 abort = abort || set_ref_in_timer(copyID);
3821#endif
3822
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003823#ifdef FEAT_QUICKFIX
3824 abort = abort || set_ref_in_quickfix(copyID);
3825#endif
3826
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003827#ifdef FEAT_TERMINAL
3828 abort = abort || set_ref_in_term(copyID);
3829#endif
3830
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003831#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003832 abort = abort || set_ref_in_popups(copyID);
3833#endif
3834
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003835 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003836 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003837 /*
3838 * 2. Free lists and dictionaries that are not referenced.
3839 */
3840 did_free = free_unref_items(copyID);
3841
3842 /*
3843 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003844 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003845 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003847 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003848 else if (p_verbose > 0)
3849 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003850 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003851 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003852
3853 return did_free;
3854}
3855
3856/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003857 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003858 */
3859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003860free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003861{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003862 int did_free = FALSE;
3863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003864 // Let all "free" functions know that we are here. This means no
3865 // dictionaries, lists, channels or jobs are to be freed, because we will
3866 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003867 in_free_unref_items = TRUE;
3868
3869 /*
3870 * PASS 1: free the contents of the items. We don't free the items
3871 * themselves yet, so that it is possible to decrement refcount counters
3872 */
3873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003874 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003875 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003877 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003878 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003879
3880#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 // Go through the list of jobs and free items without the copyID. This
3882 // must happen before doing channels, because jobs refer to channels, but
3883 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003884 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003886 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003887 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3888#endif
3889
3890 /*
3891 * PASS 2: free the items themselves.
3892 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003893 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003894 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003895
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003896#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003897 // Go through the list of jobs and free items without the copyID. This
3898 // must happen before doing channels, because jobs refer to channels, but
3899 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003900 free_unused_jobs(copyID, COPYID_MASK);
3901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003902 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003903 free_unused_channels(copyID, COPYID_MASK);
3904#endif
3905
3906 in_free_unref_items = FALSE;
3907
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908 return did_free;
3909}
3910
3911/*
3912 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003913 * "list_stack" is used to add lists to be marked. Can be NULL.
3914 *
3915 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003916 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003917 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003918set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003919{
3920 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003921 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003922 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003923 hashtab_T *cur_ht;
3924 ht_stack_T *ht_stack = NULL;
3925 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003926
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003927 cur_ht = ht;
3928 for (;;)
3929 {
3930 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003931 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003932 // Mark each item in the hashtab. If the item contains a hashtab
3933 // it is added to ht_stack, if it contains a list it is added to
3934 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003935 todo = (int)cur_ht->ht_used;
3936 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3937 if (!HASHITEM_EMPTY(hi))
3938 {
3939 --todo;
3940 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3941 &ht_stack, list_stack);
3942 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003943 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003944
3945 if (ht_stack == NULL)
3946 break;
3947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003948 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003949 cur_ht = ht_stack->ht;
3950 tempitem = ht_stack;
3951 ht_stack = ht_stack->prev;
3952 free(tempitem);
3953 }
3954
3955 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003956}
3957
3958/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003959 * Mark a dict and its items with "copyID".
3960 * Returns TRUE if setting references failed somehow.
3961 */
3962 int
3963set_ref_in_dict(dict_T *d, int copyID)
3964{
3965 if (d != NULL && d->dv_copyID != copyID)
3966 {
3967 d->dv_copyID = copyID;
3968 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3969 }
3970 return FALSE;
3971}
3972
3973/*
3974 * Mark a list and its items with "copyID".
3975 * Returns TRUE if setting references failed somehow.
3976 */
3977 int
3978set_ref_in_list(list_T *ll, int copyID)
3979{
3980 if (ll != NULL && ll->lv_copyID != copyID)
3981 {
3982 ll->lv_copyID = copyID;
3983 return set_ref_in_list_items(ll, copyID, NULL);
3984 }
3985 return FALSE;
3986}
3987
3988/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003989 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003990 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3991 *
3992 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003993 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003994 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003995set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003996{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003997 listitem_T *li;
3998 int abort = FALSE;
3999 list_T *cur_l;
4000 list_stack_T *list_stack = NULL;
4001 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004002
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004003 cur_l = l;
4004 for (;;)
4005 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004006 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004007 // Mark each item in the list. If the item contains a hashtab
4008 // it is added to ht_stack, if it contains a list it is added to
4009 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004010 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4011 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4012 ht_stack, &list_stack);
4013 if (list_stack == NULL)
4014 break;
4015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004016 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004017 cur_l = list_stack->list;
4018 tempitem = list_stack;
4019 list_stack = list_stack->prev;
4020 free(tempitem);
4021 }
4022
4023 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004024}
4025
4026/*
4027 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004028 * "list_stack" is used to add lists to be marked. Can be NULL.
4029 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4030 *
4031 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004032 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004033 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004034set_ref_in_item(
4035 typval_T *tv,
4036 int copyID,
4037 ht_stack_T **ht_stack,
4038 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004039{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004040 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004041
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004042 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004043 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004044 dict_T *dd = tv->vval.v_dict;
4045
Bram Moolenaara03f2332016-02-06 18:09:59 +01004046 if (dd != NULL && dd->dv_copyID != copyID)
4047 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004048 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004049 dd->dv_copyID = copyID;
4050 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004051 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004052 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4053 }
4054 else
4055 {
4056 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4057 if (newitem == NULL)
4058 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004059 else
4060 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004061 newitem->ht = &dd->dv_hashtab;
4062 newitem->prev = *ht_stack;
4063 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004064 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004065 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004066 }
4067 }
4068 else if (tv->v_type == VAR_LIST)
4069 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004070 list_T *ll = tv->vval.v_list;
4071
Bram Moolenaara03f2332016-02-06 18:09:59 +01004072 if (ll != NULL && ll->lv_copyID != copyID)
4073 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004074 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004075 ll->lv_copyID = copyID;
4076 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004077 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004078 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004079 }
4080 else
4081 {
4082 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004083 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004084 if (newitem == NULL)
4085 abort = TRUE;
4086 else
4087 {
4088 newitem->list = ll;
4089 newitem->prev = *list_stack;
4090 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004091 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004092 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004093 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004094 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004095 else if (tv->v_type == VAR_FUNC)
4096 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004097 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004098 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004099 else if (tv->v_type == VAR_PARTIAL)
4100 {
4101 partial_T *pt = tv->vval.v_partial;
4102 int i;
4103
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004104 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004105 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004106 // Didn't see this partial yet.
4107 pt->pt_copyID = copyID;
4108
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004109 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004110
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004111 if (pt->pt_dict != NULL)
4112 {
4113 typval_T dtv;
4114
4115 dtv.v_type = VAR_DICT;
4116 dtv.vval.v_dict = pt->pt_dict;
4117 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4118 }
4119
4120 for (i = 0; i < pt->pt_argc; ++i)
4121 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4122 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004123 if (pt->pt_funcstack != NULL)
4124 {
4125 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4126
4127 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4128 abort = abort || set_ref_in_item(stack + i, copyID,
4129 ht_stack, list_stack);
4130 }
4131
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004132 }
4133 }
4134#ifdef FEAT_JOB_CHANNEL
4135 else if (tv->v_type == VAR_JOB)
4136 {
4137 job_T *job = tv->vval.v_job;
4138 typval_T dtv;
4139
4140 if (job != NULL && job->jv_copyID != copyID)
4141 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004142 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004143 if (job->jv_channel != NULL)
4144 {
4145 dtv.v_type = VAR_CHANNEL;
4146 dtv.vval.v_channel = job->jv_channel;
4147 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4148 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004149 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004150 {
4151 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004152 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004153 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4154 }
4155 }
4156 }
4157 else if (tv->v_type == VAR_CHANNEL)
4158 {
4159 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004160 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004161 typval_T dtv;
4162 jsonq_T *jq;
4163 cbq_T *cq;
4164
4165 if (ch != NULL && ch->ch_copyID != copyID)
4166 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004167 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004168 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004169 {
4170 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4171 jq = jq->jq_next)
4172 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4173 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4174 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004175 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004176 {
4177 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004178 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004179 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4180 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004181 if (ch->ch_part[part].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 =
4185 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004186 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4187 }
4188 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004189 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004190 {
4191 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004192 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004193 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4194 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004195 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004196 {
4197 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004198 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004199 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4200 }
4201 }
4202 }
4203#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004204 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004205}
4206
Bram Moolenaar8c711452005-01-14 21:53:12 +00004207/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004208 * Return a string with the string representation of a variable.
4209 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004210 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004211 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004212 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4213 * stings as "string()", otherwise does not put quotes around strings, as
4214 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004215 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4216 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004217 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004218 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004219 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004220echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004221 typval_T *tv,
4222 char_u **tofree,
4223 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004224 int copyID,
4225 int echo_style,
4226 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004227 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004228{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004229 static int recurse = 0;
4230 char_u *r = NULL;
4231
Bram Moolenaar33570922005-01-25 22:26:29 +00004232 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004233 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004234 if (!did_echo_string_emsg)
4235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004236 // Only give this message once for a recursive call to avoid
4237 // flooding the user with errors. And stop iterating over lists
4238 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004239 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004240 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004242 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004243 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004244 }
4245 ++recurse;
4246
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004247 switch (tv->v_type)
4248 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004249 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004250 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004251 {
4252 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004253 r = tv->vval.v_string;
4254 if (r == NULL)
4255 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004256 }
4257 else
4258 {
4259 *tofree = string_quote(tv->vval.v_string, FALSE);
4260 r = *tofree;
4261 }
4262 break;
4263
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004264 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004265 if (echo_style)
4266 {
4267 *tofree = NULL;
4268 r = tv->vval.v_string;
4269 }
4270 else
4271 {
4272 *tofree = string_quote(tv->vval.v_string, TRUE);
4273 r = *tofree;
4274 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004275 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004276
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004277 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004278 {
4279 partial_T *pt = tv->vval.v_partial;
4280 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004281 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004282 garray_T ga;
4283 int i;
4284 char_u *tf;
4285
4286 ga_init2(&ga, 1, 100);
4287 ga_concat(&ga, (char_u *)"function(");
4288 if (fname != NULL)
4289 {
4290 ga_concat(&ga, fname);
4291 vim_free(fname);
4292 }
4293 if (pt != NULL && pt->pt_argc > 0)
4294 {
4295 ga_concat(&ga, (char_u *)", [");
4296 for (i = 0; i < pt->pt_argc; ++i)
4297 {
4298 if (i > 0)
4299 ga_concat(&ga, (char_u *)", ");
4300 ga_concat(&ga,
4301 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4302 vim_free(tf);
4303 }
4304 ga_concat(&ga, (char_u *)"]");
4305 }
4306 if (pt != NULL && pt->pt_dict != NULL)
4307 {
4308 typval_T dtv;
4309
4310 ga_concat(&ga, (char_u *)", ");
4311 dtv.v_type = VAR_DICT;
4312 dtv.vval.v_dict = pt->pt_dict;
4313 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4314 vim_free(tf);
4315 }
4316 ga_concat(&ga, (char_u *)")");
4317
4318 *tofree = ga.ga_data;
4319 r = *tofree;
4320 break;
4321 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004322
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004323 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004324 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004325 break;
4326
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004327 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004328 if (tv->vval.v_list == NULL)
4329 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004330 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004331 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004332 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004333 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004334 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4335 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004336 {
4337 *tofree = NULL;
4338 r = (char_u *)"[...]";
4339 }
4340 else
4341 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004342 int old_copyID = tv->vval.v_list->lv_copyID;
4343
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004344 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004345 *tofree = list2string(tv, copyID, restore_copyID);
4346 if (restore_copyID)
4347 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004348 r = *tofree;
4349 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004350 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004351
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004353 if (tv->vval.v_dict == NULL)
4354 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004355 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004356 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004357 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004358 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004359 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4360 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004361 {
4362 *tofree = NULL;
4363 r = (char_u *)"{...}";
4364 }
4365 else
4366 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004367 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004368
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004369 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004370 *tofree = dict2string(tv, copyID, restore_copyID);
4371 if (restore_copyID)
4372 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004373 r = *tofree;
4374 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004375 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004378 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004379 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004381 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004382 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004383 break;
4384
Bram Moolenaar835dc632016-02-07 14:27:38 +01004385 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004386 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004387 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004388 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004389 if (composite_val)
4390 {
4391 *tofree = string_quote(r, FALSE);
4392 r = *tofree;
4393 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004394 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004395
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004396 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004397#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004398 *tofree = NULL;
4399 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4400 r = numbuf;
4401 break;
4402#endif
4403
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004404 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004405 case VAR_SPECIAL:
4406 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004407 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004408 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004410
Bram Moolenaar8502c702014-06-17 12:51:16 +02004411 if (--recurse == 0)
4412 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004413 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004414}
4415
4416/*
4417 * Return a string with the string representation of a variable.
4418 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4419 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004420 * Does not put quotes around strings, as ":echo" displays values.
4421 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4422 * May return NULL.
4423 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004425echo_string(
4426 typval_T *tv,
4427 char_u **tofree,
4428 char_u *numbuf,
4429 int copyID)
4430{
4431 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4432}
4433
4434/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004435 * Return string "str" in ' quotes, doubling ' characters.
4436 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004437 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004438 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004439 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004440string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004441{
Bram Moolenaar33570922005-01-25 22:26:29 +00004442 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004443 char_u *p, *r, *s;
4444
Bram Moolenaar33570922005-01-25 22:26:29 +00004445 len = (function ? 13 : 3);
4446 if (str != NULL)
4447 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004448 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004449 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004450 if (*p == '\'')
4451 ++len;
4452 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004453 s = r = alloc(len);
4454 if (r != NULL)
4455 {
4456 if (function)
4457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004459 r += 10;
4460 }
4461 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004463 if (str != NULL)
4464 for (p = str; *p != NUL; )
4465 {
4466 if (*p == '\'')
4467 *r++ = '\'';
4468 MB_COPY_CHAR(p, r);
4469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004471 if (function)
4472 *r++ = ')';
4473 *r++ = NUL;
4474 }
4475 return s;
4476}
4477
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004478#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004479/*
4480 * Convert the string "text" to a floating point number.
4481 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4482 * this always uses a decimal point.
4483 * Returns the length of the text that was consumed.
4484 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004486string2float(
4487 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004489{
4490 char *s = (char *)text;
4491 float_T f;
4492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004493 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004494 if (STRNICMP(text, "inf", 3) == 0)
4495 {
4496 *value = INFINITY;
4497 return 3;
4498 }
4499 if (STRNICMP(text, "-inf", 3) == 0)
4500 {
4501 *value = -INFINITY;
4502 return 4;
4503 }
4504 if (STRNICMP(text, "nan", 3) == 0)
4505 {
4506 *value = NAN;
4507 return 3;
4508 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004509 f = strtod(s, &s);
4510 *value = f;
4511 return (int)((char_u *)s - text);
4512}
4513#endif
4514
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004517 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004519 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004520var2fpos(
4521 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004522 int dollar_lnum, // TRUE when $ is last line
4523 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004525 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004527 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004529 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004530 if (varp->v_type == VAR_LIST)
4531 {
4532 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004533 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004534 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004535 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004536
4537 l = varp->vval.v_list;
4538 if (l == NULL)
4539 return NULL;
4540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004541 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004542 pos.lnum = list_find_nr(l, 0L, &error);
4543 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004544 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004546 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004547 pos.col = list_find_nr(l, 1L, &error);
4548 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004549 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004550 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004551
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004552 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004553 li = list_find(l, 1L);
4554 if (li != NULL && li->li_tv.v_type == VAR_STRING
4555 && li->li_tv.vval.v_string != NULL
4556 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4557 pos.col = len + 1;
4558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004560 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004561 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004562 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004563
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004564 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004565 pos.coladd = list_find_nr(l, 2L, &error);
4566 if (error)
4567 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004568
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004569 return &pos;
4570 }
4571
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004572 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004573 if (name == NULL)
4574 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004575 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004577 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004578 {
4579 if (VIsual_active)
4580 return &VIsual;
4581 return &curwin->w_cursor;
4582 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004583 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004585 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4587 return NULL;
4588 return pp;
4589 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004590
Bram Moolenaara5525202006-03-02 22:52:09 +00004591 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004592
Bram Moolenaar477933c2007-07-17 14:32:23 +00004593 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004594 {
4595 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004596 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004597 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004598 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004599 // In silent Ex mode topline is zero, but that's not a valid line
4600 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004601 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004602 return &pos;
4603 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004604 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004605 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004606 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004607 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004608 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004609 return &pos;
4610 }
4611 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004612 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004614 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
4616 pos.lnum = curbuf->b_ml.ml_line_count;
4617 pos.col = 0;
4618 }
4619 else
4620 {
4621 pos.lnum = curwin->w_cursor.lnum;
4622 pos.col = (colnr_T)STRLEN(ml_get_curline());
4623 }
4624 return &pos;
4625 }
4626 return NULL;
4627}
4628
4629/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004630 * Convert list in "arg" into a position and optional file number.
4631 * When "fnump" is NULL there is no file number, only 3 items.
4632 * Note that the column is passed on as-is, the caller may want to decrement
4633 * it to use 1 for the first column.
4634 * Return FAIL when conversion is not possible, doesn't check the position for
4635 * validity.
4636 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004638list2fpos(
4639 typval_T *arg,
4640 pos_T *posp,
4641 int *fnump,
4642 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004643{
4644 list_T *l = arg->vval.v_list;
4645 long i = 0;
4646 long n;
4647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004648 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4649 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004650 if (arg->v_type != VAR_LIST
4651 || l == NULL
4652 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004653 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004654 return FAIL;
4655
4656 if (fnump != NULL)
4657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004658 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004659 if (n < 0)
4660 return FAIL;
4661 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004662 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004663 *fnump = n;
4664 }
4665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004666 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004667 if (n < 0)
4668 return FAIL;
4669 posp->lnum = n;
4670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004671 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004672 if (n < 0)
4673 return FAIL;
4674 posp->col = n;
4675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004676 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004677 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004678 posp->coladd = 0;
4679 else
4680 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004681
Bram Moolenaar493c1782014-05-28 14:34:46 +02004682 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004683 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004684
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004685 return OK;
4686}
4687
4688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 * Get the length of an environment variable name.
4690 * Advance "arg" to the first character after the name.
4691 * Return 0 for error.
4692 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004694get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
4696 char_u *p;
4697 int len;
4698
4699 for (p = *arg; vim_isIDc(*p); ++p)
4700 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004701 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return 0;
4703
4704 len = (int)(p - *arg);
4705 *arg = p;
4706 return len;
4707}
4708
4709/*
4710 * Get the length of the name of a function or internal variable.
4711 * "arg" is advanced to the first non-white character after the name.
4712 * Return 0 if something is wrong.
4713 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004715get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 char_u *p;
4718 int len;
4719
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004720 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004722 {
4723 if (*p == ':')
4724 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004725 // "s:" is start of "s:var", but "n:" is not and can be used in
4726 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004727 len = (int)(p - *arg);
4728 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4729 || len > 1)
4730 break;
4731 }
4732 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004733 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 return 0;
4735
4736 len = (int)(p - *arg);
4737 *arg = skipwhite(p);
4738
4739 return len;
4740}
4741
4742/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004743 * Get the length of the name of a variable or function.
4744 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004746 * Return -1 if curly braces expansion failed.
4747 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 * If the name contains 'magic' {}'s, expand them and return the
4749 * expanded name in an allocated string via 'alias' - caller must free.
4750 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004751 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004752get_name_len(
4753 char_u **arg,
4754 char_u **alias,
4755 int evaluate,
4756 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 char_u *p;
4760 char_u *expr_start;
4761 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004763 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
4765 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4766 && (*arg)[2] == (int)KE_SNR)
4767 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004768 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 *arg += 3;
4770 return get_id_len(arg) + 3;
4771 }
4772 len = eval_fname_script(*arg);
4773 if (len > 0)
4774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004775 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 *arg += len;
4777 }
4778
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004782 p = find_name_end(*arg, &expr_start, &expr_end,
4783 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (expr_start != NULL)
4785 {
4786 char_u *temp_string;
4787
4788 if (!evaluate)
4789 {
4790 len += (int)(p - *arg);
4791 *arg = skipwhite(p);
4792 return len;
4793 }
4794
4795 /*
4796 * Include any <SID> etc in the expanded string:
4797 * Thus the -len here.
4798 */
4799 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4800 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004801 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 *alias = temp_string;
4803 *arg = skipwhite(p);
4804 return (int)STRLEN(temp_string);
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004808 // Only give an error when there is something, otherwise it will be
4809 // reported at a higher level.
4810 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004811 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 return len;
4814}
4815
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816/*
4817 * Find the end of a variable or function name, taking care of magic braces.
4818 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4819 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004820 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 * Return a pointer to just after the name. Equal to "arg" if there is no
4822 * valid name.
4823 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004824 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004825find_name_end(
4826 char_u *arg,
4827 char_u **expr_start,
4828 char_u **expr_end,
4829 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 int mb_nest = 0;
4832 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004834 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004835 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 *expr_start = NULL;
4840 *expr_end = NULL;
4841 }
4842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004843 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004844 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4845 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004846 return arg;
4847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 for (p = arg; *p != NUL
4849 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004850 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004851 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004853 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004855 if (*p == '\'')
4856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 // skip over 'string' 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 ;
4860 if (*p == NUL)
4861 break;
4862 }
4863 else if (*p == '"')
4864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004865 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004866 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004867 if (*p == '\\' && p[1] != NUL)
4868 ++p;
4869 if (*p == NUL)
4870 break;
4871 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004872 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4873 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004874 // "s:" is start of "s:var", but "n:" is not and can be used in
4875 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004876 len = (int)(p - arg);
4877 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004878 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004879 break;
4880 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004881
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 if (*p == '[')
4885 ++br_nest;
4886 else if (*p == ']')
4887 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004889
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004890 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 if (*p == '{')
4893 {
4894 mb_nest++;
4895 if (expr_start != NULL && *expr_start == NULL)
4896 *expr_start = p;
4897 }
4898 else if (*p == '}')
4899 {
4900 mb_nest--;
4901 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4902 *expr_end = p;
4903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906
4907 return p;
4908}
4909
4910/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004911 * Expands out the 'magic' {}'s in a variable/function name.
4912 * Note that this can call itself recursively, to deal with
4913 * constructs like foo{bar}{baz}{bam}
4914 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4915 * "in_start" ^
4916 * "expr_start" ^
4917 * "expr_end" ^
4918 * "in_end" ^
4919 *
4920 * Returns a new allocated string, which the caller must free.
4921 * Returns NULL for failure.
4922 */
4923 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004924make_expanded_name(
4925 char_u *in_start,
4926 char_u *expr_start,
4927 char_u *expr_end,
4928 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004929{
4930 char_u c1;
4931 char_u *retval = NULL;
4932 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004933
4934 if (expr_end == NULL || in_end == NULL)
4935 return NULL;
4936 *expr_start = NUL;
4937 *expr_end = NUL;
4938 c1 = *in_end;
4939 *in_end = NUL;
4940
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004941 temp_result = eval_to_string(expr_start + 1, FALSE);
4942 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004943 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004944 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4945 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004946 if (retval != NULL)
4947 {
4948 STRCPY(retval, in_start);
4949 STRCAT(retval, temp_result);
4950 STRCAT(retval, expr_end + 1);
4951 }
4952 }
4953 vim_free(temp_result);
4954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004956 *expr_start = '{';
4957 *expr_end = '}';
4958
4959 if (retval != NULL)
4960 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004961 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004962 if (expr_start != NULL)
4963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004964 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004965 temp_result = make_expanded_name(retval, expr_start,
4966 expr_end, temp_result);
4967 vim_free(retval);
4968 retval = temp_result;
4969 }
4970 }
4971
4972 return retval;
4973}
4974
4975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004977 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004979 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004980eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004982 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4983}
4984
4985/*
4986 * Return TRUE if character "c" can be used as the first character in a
4987 * variable or function name (excluding '{' and '}').
4988 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004990eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004991{
4992 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993}
4994
4995/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004996 * Handle:
4997 * - expr[expr], expr[expr:expr] subscript
4998 * - ".name" lookup
4999 * - function call with Funcref variable: func(expr)
5000 * - method call: var->method()
5001 *
5002 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005003 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005005handle_subscript(
5006 char_u **arg,
5007 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005008 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005009 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005010{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005011 int evaluate = evalarg != NULL
5012 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005013 int ret = OK;
5014 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005015
Bram Moolenaar61343f02019-07-20 21:11:13 +02005016 // "." is ".name" lookup when we found a dict or when evaluating and
5017 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005018 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005019 && (((**arg == '['
5020 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005021 || (!evaluate
5022 && (*arg)[1] != '.'
5023 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005024 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005025 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005026 && !VIM_ISWHITE(*(*arg - 1)))
5027 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005028 {
5029 if (**arg == '(')
5030 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005031 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005032
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005033 // Stop the expression evaluation when immediately aborting on
5034 // error, or when an interrupt occurred or an exception was thrown
5035 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005036 if (aborting())
5037 {
5038 if (ret == OK)
5039 clear_tv(rettv);
5040 ret = FAIL;
5041 }
5042 dict_unref(selfdict);
5043 selfdict = NULL;
5044 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005045 else if (**arg == '-')
5046 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005047 if (ret == OK)
5048 {
5049 if ((*arg)[2] == '{')
5050 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005051 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005052 else
5053 // expr->name()
5054 ret = eval_method(arg, rettv, evaluate, verbose);
5055 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005056 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005058 {
5059 dict_unref(selfdict);
5060 if (rettv->v_type == VAR_DICT)
5061 {
5062 selfdict = rettv->vval.v_dict;
5063 if (selfdict != NULL)
5064 ++selfdict->dv_refcount;
5065 }
5066 else
5067 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005068 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005069 {
5070 clear_tv(rettv);
5071 ret = FAIL;
5072 }
5073 }
5074 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005076 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5077 // Don't do this when "Func" is already a partial that was bound
5078 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005079 if (selfdict != NULL
5080 && (rettv->v_type == VAR_FUNC
5081 || (rettv->v_type == VAR_PARTIAL
5082 && (rettv->vval.v_partial->pt_auto
5083 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005084 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005085
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005086 dict_unref(selfdict);
5087 return ret;
5088}
5089
5090/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005091 * Make a copy of an item.
5092 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005093 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5094 * reference to an already copied list/dict can be used.
5095 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005096 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005098item_copy(
5099 typval_T *from,
5100 typval_T *to,
5101 int deep,
5102 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005103{
5104 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005105 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005106
Bram Moolenaar33570922005-01-25 22:26:29 +00005107 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005109 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005110 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 }
5112 ++recurse;
5113
5114 switch (from->v_type)
5115 {
5116 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005117 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005118 case VAR_STRING:
5119 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005120 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005121 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005122 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005123 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005124 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005125 copy_tv(from, to);
5126 break;
5127 case VAR_LIST:
5128 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005129 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005130 if (from->vval.v_list == NULL)
5131 to->vval.v_list = NULL;
5132 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005134 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005135 to->vval.v_list = from->vval.v_list->lv_copylist;
5136 ++to->vval.v_list->lv_refcount;
5137 }
5138 else
5139 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5140 if (to->vval.v_list == NULL)
5141 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005142 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005143 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005145 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005146 case VAR_DICT:
5147 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005148 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005149 if (from->vval.v_dict == NULL)
5150 to->vval.v_dict = NULL;
5151 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005153 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005154 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5155 ++to->vval.v_dict->dv_refcount;
5156 }
5157 else
5158 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5159 if (to->vval.v_dict == NULL)
5160 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005161 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005162 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005163 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005165 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005166 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005167 }
5168 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005169 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170}
5171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 void
5173echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5174{
5175 char_u *tofree;
5176 char_u numbuf[NUMBUFLEN];
5177 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5178
5179 if (*atstart)
5180 {
5181 *atstart = FALSE;
5182 // Call msg_start() after eval1(), evaluating the expression
5183 // may cause a message to appear.
5184 if (with_space)
5185 {
5186 // Mark the saved text as finishing the line, so that what
5187 // follows is displayed on a new line when scrolling back
5188 // at the more prompt.
5189 msg_sb_eol();
5190 msg_start();
5191 }
5192 }
5193 else if (with_space)
5194 msg_puts_attr(" ", echo_attr);
5195
5196 if (p != NULL)
5197 for ( ; *p != NUL && !got_int; ++p)
5198 {
5199 if (*p == '\n' || *p == '\r' || *p == TAB)
5200 {
5201 if (*p != TAB && *needclr)
5202 {
5203 // remove any text still there from the command
5204 msg_clr_eos();
5205 *needclr = FALSE;
5206 }
5207 msg_putchar_attr(*p, echo_attr);
5208 }
5209 else
5210 {
5211 if (has_mbyte)
5212 {
5213 int i = (*mb_ptr2len)(p);
5214
5215 (void)msg_outtrans_len_attr(p, i, echo_attr);
5216 p += i - 1;
5217 }
5218 else
5219 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5220 }
5221 }
5222 vim_free(tofree);
5223}
5224
Bram Moolenaare9a41262005-01-15 22:18:47 +00005225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 * ":echo expr1 ..." print each argument separated with a space, add a
5227 * newline at the end.
5228 * ":echon expr1 ..." print each argument plain.
5229 */
5230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005231ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232{
5233 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005234 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 char_u *p;
5236 int needclr = TRUE;
5237 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005238 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005239 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005240 evalarg_T evalarg;
5241
5242 CLEAR_FIELD(evalarg);
5243 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +02005244 if (getline_equal(eap->getline, eap->cookie, getsourceline))
5245 {
5246 evalarg.eval_getline = eap->getline;
5247 evalarg.eval_cookie = eap->cookie;
5248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
5250 if (eap->skip)
5251 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005252 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005254 // If eval1() causes an error message the text from the command may
5255 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005256 need_clr_eos = needclr;
5257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005259 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
5261 /*
5262 * Report the invalid expression unless the expression evaluation
5263 * has been cancelled due to an aborting error, an interrupt, or an
5264 * exception.
5265 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005266 if (!aborting() && did_emsg == did_emsg_before
5267 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005268 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005269 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 break;
5271 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005272 need_clr_eos = FALSE;
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005276
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005277 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 arg = skipwhite(arg);
5279 }
5280 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005281 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 if (eap->skip)
5284 --emsg_skip;
5285 else
5286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005287 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (needclr)
5289 msg_clr_eos();
5290 if (eap->cmdidx == CMD_echo)
5291 msg_end();
5292 }
5293}
5294
5295/*
5296 * ":echohl {name}".
5297 */
5298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005299ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005301 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302}
5303
5304/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005305 * Returns the :echo attribute
5306 */
5307 int
5308get_echo_attr(void)
5309{
5310 return echo_attr;
5311}
5312
5313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 * ":execute expr1 ..." execute the result of an expression.
5315 * ":echomsg expr1 ..." Print a message
5316 * ":echoerr expr1 ..." Print an error
5317 * Each gets spaces around each argument and a newline at the end for
5318 * echo commands
5319 */
5320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005321ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322{
5323 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 int ret = OK;
5326 char_u *p;
5327 garray_T ga;
5328 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 ga_init2(&ga, 1, 80);
5331
5332 if (eap->skip)
5333 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005334 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005336 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5337 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
5340 if (!eap->skip)
5341 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005342 char_u buf[NUMBUFLEN];
5343
5344 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005345 {
5346 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5347 {
5348 emsg(_(e_inval_string));
5349 p = NULL;
5350 }
5351 else
5352 p = tv_get_string_buf(&rettv, buf);
5353 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005354 else
5355 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005356 if (p == NULL)
5357 {
5358 clear_tv(&rettv);
5359 ret = FAIL;
5360 break;
5361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 len = (int)STRLEN(p);
5363 if (ga_grow(&ga, len + 2) == FAIL)
5364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 ret = FAIL;
5367 break;
5368 }
5369 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 ga.ga_len += len;
5373 }
5374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 arg = skipwhite(arg);
5377 }
5378
5379 if (ret != FAIL && ga.ga_data != NULL)
5380 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005381 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5382 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 // Mark the already saved text as finishing the line, so that what
5384 // follows is displayed on a new line when scrolling back at the
5385 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005386 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005387 }
5388
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005390 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005391 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005392 out_flush();
5393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 else if (eap->cmdidx == CMD_echoerr)
5395 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005396 int save_did_emsg = did_emsg;
5397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005398 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005399 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 if (!force_abort)
5401 did_emsg = save_did_emsg;
5402 }
5403 else if (eap->cmdidx == CMD_execute)
5404 do_cmdline((char_u *)ga.ga_data,
5405 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5406 }
5407
5408 ga_clear(&ga);
5409
5410 if (eap->skip)
5411 --emsg_skip;
5412
5413 eap->nextcmd = check_nextcmd(arg);
5414}
5415
5416/*
5417 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5418 * "arg" points to the "&" or '+' when called, to "option" when returning.
5419 * Returns NULL when no option name found. Otherwise pointer to the char
5420 * after the option name.
5421 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005422 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005423find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 char_u *p = *arg;
5426
5427 ++p;
5428 if (*p == 'g' && p[1] == ':')
5429 {
5430 *opt_flags = OPT_GLOBAL;
5431 p += 2;
5432 }
5433 else if (*p == 'l' && p[1] == ':')
5434 {
5435 *opt_flags = OPT_LOCAL;
5436 p += 2;
5437 }
5438 else
5439 *opt_flags = 0;
5440
5441 if (!ASCII_ISALPHA(*p))
5442 return NULL;
5443 *arg = p;
5444
5445 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005446 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 else
5448 while (ASCII_ISALPHA(*p))
5449 ++p;
5450 return p;
5451}
5452
5453/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005454 * Display script name where an item was last set.
5455 * Should only be invoked when 'verbose' is non-zero.
5456 */
5457 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005458last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005459{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005460 char_u *p;
5461
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005462 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005463 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005464 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005465 if (p != NULL)
5466 {
5467 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005468 msg_puts(_("\n\tLast set from "));
5469 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005470 if (script_ctx.sc_lnum > 0)
5471 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005472 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005473 msg_outnum((long)script_ctx.sc_lnum);
5474 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005475 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005476 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005477 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005478 }
5479}
5480
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005481#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483/*
5484 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005485 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 * "flags" can be "g" to do a global substitute.
5487 * Returns an allocated string, NULL for error.
5488 */
5489 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005490do_string_sub(
5491 char_u *str,
5492 char_u *pat,
5493 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005494 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005495 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
5497 int sublen;
5498 regmatch_T regmatch;
5499 int i;
5500 int do_all;
5501 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005502 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 garray_T ga;
5504 char_u *ret;
5505 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005506 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005508 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005510 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
5512 ga_init2(&ga, 1, 200);
5513
5514 do_all = (flags[0] == 'g');
5515
5516 regmatch.rm_ic = p_ic;
5517 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5518 if (regmatch.regprog != NULL)
5519 {
5520 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005521 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5523 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005524 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005525 if (regmatch.startp[0] == regmatch.endp[0])
5526 {
5527 if (zero_width == regmatch.startp[0])
5528 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005529 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005530 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005531 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5532 (size_t)i);
5533 ga.ga_len += i;
5534 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005535 continue;
5536 }
5537 zero_width = regmatch.startp[0];
5538 }
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 /*
5541 * Get some space for a temporary buffer to do the substitution
5542 * into. It will contain:
5543 * - The text up to where the match is.
5544 * - The substituted text.
5545 * - The text after the match.
5546 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005547 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005548 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5550 {
5551 ga_clear(&ga);
5552 break;
5553 }
5554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005555 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 i = (int)(regmatch.startp[0] - tail);
5557 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005558 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005559 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 + ga.ga_len + i, TRUE, TRUE, FALSE);
5561 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005562 tail = regmatch.endp[0];
5563 if (*tail == NUL)
5564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 if (!do_all)
5566 break;
5567 }
5568
5569 if (ga.ga_data != NULL)
5570 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5571
Bram Moolenaar473de612013-06-08 18:19:48 +02005572 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574
5575 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5576 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005577 if (p_cpo == empty_option)
5578 p_cpo = save_cpo;
5579 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005580 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005581 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
5583 return ret;
5584}