blob: 9ef3128638b7da8dfbb42f0534b1706bb1e8019c [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 Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static 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 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
60 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020061 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010062num_divide(varnumber_T n1, varnumber_T n2)
63{
64 varnumber_T result;
65
66 if (n2 == 0) // give an error message?
67 {
68 if (n1 == 0)
69 result = VARNUM_MIN; // similar to NaN
70 else if (n1 < 0)
71 result = -VARNUM_MAX;
72 else
73 result = VARNUM_MAX;
74 }
75 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
83 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020084 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010085num_modulus(varnumber_T n1, varnumber_T n2)
86{
87 // Give an error when n2 is 0?
88 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaara1fa8922017-01-12 20:06:33 +010091#if defined(EBCDIC) || defined(PROTO)
92/*
93 * Compare struct fst by function name.
94 */
95 static int
96compare_func_name(const void *s1, const void *s2)
97{
98 struct fst *p1 = (struct fst *)s1;
99 struct fst *p2 = (struct fst *)s2;
100
101 return STRCMP(p1->f_name, p2->f_name);
102}
103
104/*
105 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100106 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107 * On machines using EBCDIC we have to sort it.
108 */
109 static void
110sortFunctions(void)
111{
112 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
113
114 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
115}
116#endif
117
Bram Moolenaar33570922005-01-25 22:26:29 +0000118/*
119 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000120 */
121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100122eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000123{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200124 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200125 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000126
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200127#ifdef EBCDIC
128 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100129 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200130 */
131 sortFunctions();
132#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000133}
134
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000135#if defined(EXITFREE) || defined(PROTO)
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100140 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200141 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000142
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200143 // autoloaded script names
144 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000145
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200146 // unreferenced lists and dicts
147 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200148
149 // functions not garbage collected
150 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151}
152#endif
153
Bram Moolenaare6b53242020-07-01 17:28:33 +0200154 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
156{
157 CLEAR_FIELD(*evalarg);
158 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
159 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
160 {
161 evalarg->eval_getline = eap->getline;
162 evalarg->eval_cookie = eap->cookie;
163 }
164}
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Top level evaluation function, returning a boolean.
168 * Sets "error" to TRUE if there was an error.
169 * Return TRUE or FALSE.
170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172eval_to_bool(
173 char_u *arg,
174 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200175 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar33570922005-01-25 22:26:29 +0000178 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200180 evalarg_T evalarg;
181
Bram Moolenaar37c83712020-06-30 21:18:36 +0200182 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 if (skip)
185 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200186 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 {
190 *error = FALSE;
191 if (!skip)
192 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200193 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200194 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200195 else
196 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000197 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 }
199 }
200 if (skip)
201 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200204 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205}
206
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207/*
208 * Call eval1() and give an error message if not done at a lower level.
209 */
210 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 int ret;
215 int did_emsg_before = did_emsg;
216 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200219 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
220
221 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 if (ret == FAIL)
223 {
224 // Report the invalid expression unless the expression evaluation has
225 // been cancelled due to an aborting error, an interrupt, or an
226 // exception, or we already gave a more specific error.
227 // Also check called_emsg for when using assert_fails().
228 if (!aborting() && did_emsg == did_emsg_before
229 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100230 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233 return ret;
234}
235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200237 * Return whether a typval is a valid expression to pass to eval_expr_typval()
238 * or eval_expr_to_bool(). An empty string returns FALSE;
239 */
240 int
241eval_expr_valid_arg(typval_T *tv)
242{
243 return tv->v_type != VAR_UNKNOWN
244 && (tv->v_type != VAR_STRING
245 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Evaluate an expression, which can be a function, partial or string.
250 * Pass arguments "argv[argc]".
251 * Return the result in "rettv" and OK or FAIL.
252 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200253 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100254eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
255{
256 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200258 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100259
260 if (expr->v_type == VAR_FUNC)
261 {
262 s = expr->vval.v_string;
263 if (s == NULL || *s == NUL)
264 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200265 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200266 funcexe.evaluate = TRUE;
267 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 else if (expr->v_type == VAR_PARTIAL)
271 {
272 partial_T *partial = expr->vval.v_partial;
273
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200274 if (partial == NULL)
275 return FAIL;
276
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200278 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200280 if (call_def_function(partial->pt_func, argc, argv,
281 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 return FAIL;
283 }
284 else
285 {
286 s = partial_name(partial);
287 if (s == NULL || *s == NUL)
288 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200289 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 funcexe.evaluate = TRUE;
291 funcexe.partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 }
296 else
297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100298 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 if (s == NULL)
300 return FAIL;
301 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200302 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200304 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200306 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100307 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 return FAIL;
309 }
310 }
311 return OK;
312}
313
314/*
315 * Like eval_to_bool() but using a typval_T instead of a string.
316 * Works for string, funcref and partial.
317 */
318 int
319eval_expr_to_bool(typval_T *expr, int *error)
320{
321 typval_T rettv;
322 int res;
323
324 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
325 {
326 *error = TRUE;
327 return FALSE;
328 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200329 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 clear_tv(&rettv);
331 return res;
332}
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
335 * Top level evaluation function, returning a string. If "skip" is TRUE,
336 * only parsing to "nextcmd" is done, without reporting errors. Return
337 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
338 */
339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340eval_to_string_skip(
341 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200342 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100343 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
Bram Moolenaar33570922005-01-25 22:26:29 +0000345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar37c83712020-06-30 21:18:36 +0200349 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (skip)
351 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200352 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 retval = NULL;
354 else
355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100356 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (skip)
360 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200361 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 return retval;
364}
365
366/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367 * Skip over an expression at "*pp".
368 * Return FAIL for an error, OK otherwise.
369 */
370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100371skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372{
Bram Moolenaar33570922005-01-25 22:26:29 +0000373 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374
375 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200376 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000377}
378
379/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Skip over an expression at "*pp".
381 * If in Vim9 script and line breaks are encountered, the lines are
382 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383 * "arg" is advanced to just after the expression.
384 * "start" is set to the start of the expression, "end" to just after the end.
385 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200386 * Return FAIL for an error, OK otherwise.
387 */
388 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200389skip_expr_concatenate(
390 char_u **arg,
391 char_u **start,
392 char_u **end,
393 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394{
395 typval_T rettv;
396 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200397 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200398 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 int evaluate = evalarg == NULL
401 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200403 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 {
406 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 ++gap->ga_len;
410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200422 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
427 // just one line, no need to concatenate
428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
438 p = ga_concat_strings(gap, "");
439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
454 ga_clear(gap);
455 gap->ga_itemsize = 0;
456 if (p == NULL)
457 return FAIL;
458 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 vim_free(evalarg->eval_tofree_lambda);
460 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 // Compute "end" relative to the end.
462 *end = *start + STRLEN(*start) - endoff;
463 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
465
466 return res;
467}
468
469/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200470 * Top level evaluation function, returning a string. Does not handle line
471 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Return pointer to allocated memory, or NULL for failure.
475 */
476 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100477eval_to_string(
478 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100479 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
Bram Moolenaar33570922005-01-25 22:26:29 +0000481 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000483 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000484#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200488 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 retval = NULL;
490 else
491 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000492 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000493 {
494 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000495 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200496 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200497 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200498 if (tv.vval.v_list->lv_len > 0)
499 ga_append(&ga, NL);
500 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000501 ga_append(&ga, NUL);
502 retval = (char_u *)ga.ga_data;
503 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000504#ifdef FEAT_FLOAT
505 else if (convert && tv.v_type == VAR_FLOAT)
506 {
507 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
508 retval = vim_strsave(numbuf);
509 }
510#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000511 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100512 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000513 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200515 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
517 return retval;
518}
519
520/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000521 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200522 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200523 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 */
525 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100526eval_to_string_safe(
527 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100528 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
530 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200531 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200532 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200534 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200535 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000536 if (use_sandbox)
537 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200538 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200539 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000540 if (use_sandbox)
541 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200542 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200543 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200544 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 return retval;
546}
547
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548/*
549 * Top level evaluation function, returning a number.
550 * Evaluates "expr" silently.
551 * Returns -1 for an error.
552 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200553 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
Bram Moolenaar33570922005-01-25 22:26:29 +0000556 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200557 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000558 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560 ++emsg_off;
561
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200562 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 retval = -1;
564 else
565 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100566 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000567 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
569 --emsg_off;
570
571 return retval;
572}
573
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000574/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000575 * Top level evaluation function.
576 * Returns an allocated typval_T with the result.
577 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000578 */
579 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200580eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000581{
582 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200583 evalarg_T evalarg;
584
585 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000586
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200587 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200588 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100589 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000590
Bram Moolenaar37c83712020-06-30 21:18:36 +0200591 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000592 return tv;
593}
594
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100596 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200597 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
598 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000599 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200601 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100602call_vim_function(
603 char_u *func,
604 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200605 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200606 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000608 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200609 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100611 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200612 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200613 funcexe.firstline = curwin->w_cursor.lnum;
614 funcexe.lastline = curwin->w_cursor.lnum;
615 funcexe.evaluate = TRUE;
616 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000617 if (ret == FAIL)
618 clear_tv(rettv);
619
620 return ret;
621}
622
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100623/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100624 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100625 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200626 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
627 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100628 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200629 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630call_func_retnr(
631 char_u *func,
632 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200633 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100634{
635 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200636 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100637
Bram Moolenaarded27a12018-08-01 19:06:03 +0200638 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100639 return -1;
640
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100641 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100642 clear_tv(&rettv);
643 return retval;
644}
645
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000646/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100647 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000648 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200649 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
650 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000651 */
652 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653call_func_retstr(
654 char_u *func,
655 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200656 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000657{
658 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000659 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000660
Bram Moolenaarded27a12018-08-01 19:06:03 +0200661 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000662 return NULL;
663
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100664 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000665 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 return retval;
667}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000668
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000669/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100670 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200671 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
672 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000673 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000674 */
675 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100676call_func_retlist(
677 char_u *func,
678 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200679 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000680{
681 typval_T rettv;
682
Bram Moolenaarded27a12018-08-01 19:06:03 +0200683 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000684 return NULL;
685
686 if (rettv.v_type != VAR_LIST)
687 {
688 clear_tv(&rettv);
689 return NULL;
690 }
691
692 return rettv.vval.v_list;
693}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#ifdef FEAT_FOLDING
696/*
697 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
698 * it in "*cp". Doesn't give error messages.
699 */
700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100701eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200704 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000706 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
707 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
709 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000710 if (use_sandbox)
711 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200712 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200714 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 retval = 0;
716 else
717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100718 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000719 if (tv.v_type == VAR_NUMBER)
720 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000721 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 retval = 0;
723 else
724 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100725 // If the result is a string, check if there is a non-digit before
726 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000727 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (!VIM_ISDIGIT(*s) && *s != '-')
729 *cp = *s++;
730 retval = atol((char *)s);
731 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000732 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000735 if (use_sandbox)
736 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200737 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200738 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200740 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741}
742#endif
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000745 * Get an lval: variable, Dict item or List item that can be assigned a value
746 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
747 * "name.key", "name.key[expr]" etc.
748 * Indexing only works if "name" is an existing List or Dictionary.
749 * "name" points to the start of the name.
750 * If "rettv" is not NULL it points to the value to be assigned.
751 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
752 * wrong; must end in space or cmd separator.
753 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100754 * flags:
755 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100756 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100757 * GLV_NO_AUTOLOAD: do not use script autoloading
758 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000759 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000760 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000761 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000762 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200763 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100764get_lval(
765 char_u *name,
766 typval_T *rettv,
767 lval_T *lp,
768 int unlet,
769 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 int flags, // GLV_ values
771 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000774 char_u *expr_start, *expr_end;
775 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000776 dictitem_T *v;
777 typval_T var1;
778 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000779 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000780 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000781 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000782 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100784 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100786 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200787 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788
789 if (skip)
790 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100791 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000792 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200793 lp->ll_name_end = find_name_end(name, NULL, NULL,
794 FNE_INCL_BR | fne_flags);
795 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000796 }
797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100798 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000799 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 if (expr_start != NULL)
802 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100803 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100804 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 && *p != '[' && *p != '.')
806 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200807 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 return NULL;
809 }
810
811 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
812 if (lp->ll_exp_name == NULL)
813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // Report an invalid expression in braces, unless the
815 // expression evaluation has been cancelled due to an
816 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 if (!aborting() && !quiet)
818 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000819 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 return NULL;
822 }
823 }
824 lp->ll_name = lp->ll_exp_name;
825 }
826 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 lp->ll_name = name;
829
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200830 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200832 // "a: type" is declaring variable "a" with a type, not "a:".
833 if (p == name + 2 && p[-1] == ':')
834 {
835 --p;
836 lp->ll_name_end = p;
837 }
838 if (*p == ':')
839 {
840 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
841 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200843 // parse the type after the name
844 lp->ll_type = parse_type(&tp, &si->sn_type_list);
845 lp->ll_name_end = tp;
846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 }
848 }
849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
852 return p;
853
854 cc = *p;
855 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100856 // Only pass &ht when we would write to the variable, it prevents autoload
857 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100858 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
859 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200861 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 if (v == NULL)
864 return NULL;
865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 /*
867 * Loop until no more [idx] or .key is following.
868 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100870 var1.v_type = VAR_UNKNOWN;
871 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200875 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100876 && !(lp->ll_tv->v_type == VAR_BLOB
877 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000885 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100886 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000889
Bram Moolenaar10c65862020-10-08 21:16:42 +0200890 if (in_vim9script() && lp->ll_valtype == NULL
891 && lp->ll_tv == &v->di_tv
892 && ht != NULL && ht == get_script_local_ht())
893 {
894 svar_T *sv = find_typval_in_script(lp->ll_tv);
895
896 // Vim9 script local variable: get the type
897 if (sv != NULL)
898 lp->ll_valtype = sv->sv_type;
899 }
900
Bram Moolenaar8c711452005-01-14 21:53:12 +0000901 len = -1;
902 if (*p == '.')
903 {
904 key = p + 1;
905 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
906 ;
907 if (len == 0)
908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100910 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 }
913 p = key + len;
914 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000915 else
916 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100917 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000918 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 if (*p == ':')
920 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000921 else
922 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200924 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000925 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100926 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100928 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000929 clear_tv(&var1);
930 return NULL;
931 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200932 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000933 }
934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100935 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 if (*p == ':')
937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000938 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200941 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100942 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000944 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100945 if (rettv != NULL
946 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100947 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100948 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100949 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100952 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100953 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000955 }
956 p = skipwhite(p + 1);
957 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000958 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000959 else
960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200962 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200963 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100965 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000967 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100968 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100970 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100971 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000972 clear_tv(&var2);
973 return NULL;
974 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000980
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000982 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000983 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100984 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100985 clear_tv(&var1);
986 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 }
989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100990 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 ++p;
992 }
993
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 {
996 if (len == -1)
997 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 // "[key]": get key from "var1"
999 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001000 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 }
1005 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001006 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001007
1008 // a NULL dict is equivalent with an empty dict
1009 if (lp->ll_tv->vval.v_dict == NULL)
1010 {
1011 lp->ll_tv->vval.v_dict = dict_alloc();
1012 if (lp->ll_tv->vval.v_dict == NULL)
1013 {
1014 clear_tv(&var1);
1015 return NULL;
1016 }
1017 ++lp->ll_tv->vval.v_dict->dv_refcount;
1018 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001019 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001020
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001021 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001023 // When assigning to a scope dictionary check that a function and
1024 // variable name is valid (only variable name unless it is l: or
1025 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001026 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001027 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001028 int prevval;
1029 int wrong;
1030
1031 if (len != -1)
1032 {
1033 prevval = key[len];
1034 key[len] = NUL;
1035 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001036 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001037 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001038 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1039 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001040 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001041 || !valid_varname(key);
1042 if (len != -1)
1043 key[len] = prevval;
1044 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001045 {
1046 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001047 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001048 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001049 }
1050
Bram Moolenaar10c65862020-10-08 21:16:42 +02001051 if (lp->ll_valtype != NULL)
1052 // use the type of the member
1053 lp->ll_valtype = lp->ll_valtype->tt_member;
1054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001057 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001058 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001059 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001061 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001062 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001063 return NULL;
1064 }
1065
Bram Moolenaar31b81602019-02-10 22:14:27 +01001066 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001067 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001070 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 }
1074 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001078 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001079 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001080 p = NULL;
1081 break;
1082 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001083 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001084 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001085 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1086 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001087 {
1088 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001089 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001090 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001091
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001092 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001095 else if (lp->ll_tv->v_type == VAR_BLOB)
1096 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001097 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1098
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001099 /*
1100 * Get the number and item for the only or first index of the List.
1101 */
1102 if (empty1)
1103 lp->ll_n1 = 0;
1104 else
1105 // is number or string
1106 lp->ll_n1 = (long)tv_get_number(&var1);
1107 clear_tv(&var1);
1108
1109 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001110 || lp->ll_n1 > bloblen
1111 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001112 {
1113 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001114 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001115 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001116 return NULL;
1117 }
1118 if (lp->ll_range && !lp->ll_empty2)
1119 {
1120 lp->ll_n2 = (long)tv_get_number(&var2);
1121 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001122 if (lp->ll_n2 < 0
1123 || lp->ll_n2 >= bloblen
1124 || lp->ll_n2 < lp->ll_n1)
1125 {
1126 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001128 return NULL;
1129 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001130 }
1131 lp->ll_blob = lp->ll_tv->vval.v_blob;
1132 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001133 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 else
1136 {
1137 /*
1138 * Get the number and item for the only or first index of the List.
1139 */
1140 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001143 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001144 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var1);
1146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001147 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 lp->ll_list = lp->ll_tv->vval.v_list;
1149 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1150 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001151 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001152 if (lp->ll_n1 < 0)
1153 {
1154 lp->ll_n1 = 0;
1155 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1156 }
1157 }
1158 if (lp->ll_li == NULL)
1159 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001160 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001161 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001162 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001163 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001164 }
1165
Bram Moolenaar10c65862020-10-08 21:16:42 +02001166 if (lp->ll_valtype != NULL)
1167 // use the type of the member
1168 lp->ll_valtype = lp->ll_valtype->tt_member;
1169
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 /*
1171 * May need to find the item or absolute index for the second
1172 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001173 * When no index given: "lp->ll_empty2" is TRUE.
1174 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001178 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001179 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001180 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001182 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001184 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001185 {
1186 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001187 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001188 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001189 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001190 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001191 }
1192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001193 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 if (lp->ll_n1 < 0)
1195 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1196 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001197 {
1198 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001201 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001202 }
1203
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001204 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001206 }
1207
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001208 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 return p;
1211}
1212
1213/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001214 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001216 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001217clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218{
1219 vim_free(lp->ll_exp_name);
1220 vim_free(lp->ll_newkey);
1221}
1222
1223/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001224 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001226 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1227 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001230set_var_lval(
1231 lval_T *lp,
1232 char_u *endp,
1233 typval_T *rettv,
1234 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001235 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001236 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237{
1238 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001239 listitem_T *ri;
1240 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241
1242 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001244 cc = *endp;
1245 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001246 if (lp->ll_blob != NULL)
1247 {
1248 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001249
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001250 if (op != NULL && *op != '=')
1251 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001252 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001253 return;
1254 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001255 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001256 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001257
1258 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1259 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001260 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001261
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001262 if (lp->ll_empty2)
1263 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1264
1265 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001267 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001268 return;
1269 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001270 if (lp->ll_empty2)
1271 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001272
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001273 ir = 0;
1274 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1275 blob_set(lp->ll_blob, il,
1276 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001277 }
1278 else
1279 {
1280 val = (int)tv_get_number_chk(rettv, &error);
1281 if (!error)
1282 {
1283 garray_T *gap = &lp->ll_blob->bv_ga;
1284
1285 // Allow for appending a byte. Setting a byte beyond
1286 // the end is an error otherwise.
1287 if (lp->ll_n1 < gap->ga_len
1288 || (lp->ll_n1 == gap->ga_len
1289 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1290 {
1291 blob_set(lp->ll_blob, lp->ll_n1, val);
1292 if (lp->ll_n1 == gap->ga_len)
1293 ++gap->ga_len;
1294 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001295 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296 }
1297 }
1298 }
1299 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001301 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001303 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001304 {
1305 emsg(_(e_cannot_mod));
1306 *endp = cc;
1307 return;
1308 }
1309
Bram Moolenaarff697e62019-02-12 22:28:33 +01001310 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001311 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001312 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001313 &tv, &di, TRUE, FALSE) == OK)
1314 {
1315 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001316 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1317 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001318 && tv_op(&tv, rettv, op) == OK)
1319 set_var(lp->ll_name, &tv, FALSE);
1320 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001321 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001323 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001324 {
1325 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001326 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001327 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001329 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001330 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001332 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001333 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001334 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001335 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 else if (lp->ll_range)
1337 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001338 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001339 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001340
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001341 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001342 {
1343 emsg(_("E996: Cannot lock a range"));
1344 return;
1345 }
1346
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001347 /*
1348 * Check whether any of the list items is locked
1349 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001350 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001351 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001352 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001353 return;
1354 ri = ri->li_next;
1355 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1356 break;
1357 ll_li = ll_li->li_next;
1358 ++ll_n1;
1359 }
1360
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001361 /*
1362 * Assign the List values to the list items.
1363 */
1364 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001366 if (op != NULL && *op != '=')
1367 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1368 else
1369 {
1370 clear_tv(&lp->ll_li->li_tv);
1371 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1372 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 ri = ri->li_next;
1374 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1375 break;
1376 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001377 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001378 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001379 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001380 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001381 ri = NULL;
1382 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001383 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001384 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001385 lp->ll_li = lp->ll_li->li_next;
1386 ++lp->ll_n1;
1387 }
1388 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001389 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001390 else if (lp->ll_empty2
1391 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001392 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001393 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 }
1395 else
1396 {
1397 /*
1398 * Assign to a List or Dictionary item.
1399 */
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001400 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001401 {
1402 emsg(_("E996: Cannot lock a list or dict"));
1403 return;
1404 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001405
1406 if (lp->ll_valtype != NULL
1407 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1408 return;
1409
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 if (lp->ll_newkey != NULL)
1411 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001412 if (op != NULL && *op != '=')
1413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001414 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001415 return;
1416 }
1417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001418 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001419 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 if (di == NULL)
1421 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001422 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1423 {
1424 vim_free(di);
1425 return;
1426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001427 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001428 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001429 else if (op != NULL && *op != '=')
1430 {
1431 tv_op(lp->ll_tv, rettv, op);
1432 return;
1433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001436
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 /*
1438 * Assign the value to the variable or list item.
1439 */
1440 if (copy)
1441 copy_tv(rettv, lp->ll_tv);
1442 else
1443 {
1444 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001445 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 }
1448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449}
1450
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001451/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001452 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1453 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001454 * Returns OK or FAIL.
1455 */
1456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001459 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 char_u numbuf[NUMBUFLEN];
1461 char_u *s;
1462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001463 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001464 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001465 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466 {
1467 switch (tv1->v_type)
1468 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001469 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001470 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001472 case VAR_DICT:
1473 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001474 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001475 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001476 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001477 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001478 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 break;
1480
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001481 case VAR_BLOB:
1482 if (*op != '+' || tv2->v_type != VAR_BLOB)
1483 break;
1484 // BLOB += BLOB
1485 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1486 {
1487 blob_T *b1 = tv1->vval.v_blob;
1488 blob_T *b2 = tv2->vval.v_blob;
1489 int i, len = blob_len(b2);
1490 for (i = 0; i < len; i++)
1491 ga_append(&b1->bv_ga, blob_get(b2, i));
1492 }
1493 return OK;
1494
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495 case VAR_LIST:
1496 if (*op != '+' || tv2->v_type != VAR_LIST)
1497 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001498 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001499 if (tv2->vval.v_list != NULL)
1500 {
1501 if (tv1->vval.v_list == NULL)
1502 {
1503 tv1->vval.v_list = tv2->vval.v_list;
1504 ++tv1->vval.v_list->lv_refcount;
1505 }
1506 else
1507 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509 return OK;
1510
1511 case VAR_NUMBER:
1512 case VAR_STRING:
1513 if (tv2->v_type == VAR_LIST)
1514 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001515 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001516 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001517 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001518 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001519#ifdef FEAT_FLOAT
1520 if (tv2->v_type == VAR_FLOAT)
1521 {
1522 float_T f = n;
1523
Bram Moolenaarff697e62019-02-12 22:28:33 +01001524 if (*op == '%')
1525 break;
1526 switch (*op)
1527 {
1528 case '+': f += tv2->vval.v_float; break;
1529 case '-': f -= tv2->vval.v_float; break;
1530 case '*': f *= tv2->vval.v_float; break;
1531 case '/': f /= tv2->vval.v_float; break;
1532 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001533 clear_tv(tv1);
1534 tv1->v_type = VAR_FLOAT;
1535 tv1->vval.v_float = f;
1536 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001538#endif
1539 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001540 switch (*op)
1541 {
1542 case '+': n += tv_get_number(tv2); break;
1543 case '-': n -= tv_get_number(tv2); break;
1544 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001545 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1546 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001547 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001548 clear_tv(tv1);
1549 tv1->v_type = VAR_NUMBER;
1550 tv1->vval.v_number = n;
1551 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 }
1553 else
1554 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555 if (tv2->v_type == VAR_FLOAT)
1556 break;
1557
Bram Moolenaarff697e62019-02-12 22:28:33 +01001558 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001559 s = tv_get_string(tv1);
1560 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001561 clear_tv(tv1);
1562 tv1->v_type = VAR_STRING;
1563 tv1->vval.v_string = s;
1564 }
1565 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001568#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 {
1570 float_T f;
1571
Bram Moolenaarff697e62019-02-12 22:28:33 +01001572 if (*op == '%' || *op == '.'
1573 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001574 && tv2->v_type != VAR_NUMBER
1575 && tv2->v_type != VAR_STRING))
1576 break;
1577 if (tv2->v_type == VAR_FLOAT)
1578 f = tv2->vval.v_float;
1579 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001580 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001581 switch (*op)
1582 {
1583 case '+': tv1->vval.v_float += f; break;
1584 case '-': tv1->vval.v_float -= f; break;
1585 case '*': tv1->vval.v_float *= f; break;
1586 case '/': tv1->vval.v_float /= f; break;
1587 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001590 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591 }
1592 }
1593
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001595 return FAIL;
1596}
1597
1598/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001599 * Evaluate the expression used in a ":for var in expr" command.
1600 * "arg" points to "var".
1601 * Set "*errp" to TRUE for an error, FALSE otherwise;
1602 * Return a pointer that holds the info. Null when there is an error.
1603 */
1604 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001605eval_for_line(
1606 char_u *arg,
1607 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001608 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001609 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001610{
Bram Moolenaar33570922005-01-25 22:26:29 +00001611 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 typval_T tv;
1614 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001615 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001616
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001619 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 if (fi == NULL)
1621 return NULL;
1622
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001623 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001624 if (expr == NULL)
1625 return fi;
1626
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001627 expr = skipwhite_and_linebreak(expr, evalarg);
1628 if (expr[0] != 'i' || expr[1] != 'n'
1629 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 return fi;
1633 }
1634
1635 if (skip)
1636 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001637 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1638 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001639 {
1640 *errp = FALSE;
1641 if (!skip)
1642 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001643 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001644 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001645 l = tv.vval.v_list;
1646 if (l == NULL)
1647 {
1648 // a null list is like an empty list: do nothing
1649 clear_tv(&tv);
1650 }
1651 else
1652 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001654 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001656 // No need to increment the refcount, it's already set for
1657 // the list being used in "tv".
1658 fi->fi_list = l;
1659 list_add_watch(l, &fi->fi_lw);
1660 fi->fi_lw.lw_item = l->lv_first;
1661 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001662 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001663 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001664 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001665 fi->fi_bi = 0;
1666 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001668 typval_T btv;
1669
1670 // Make a copy, so that the iteration still works when the
1671 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001673 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001674 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001675 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001676 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 else
1678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001679 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001680 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 }
1682 }
1683 }
1684 if (skip)
1685 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001686 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687
1688 return fi;
1689}
1690
1691/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001692 * Used when looping over a :for line, skip the "in expr" part.
1693 */
1694 void
1695skip_for_lines(void *fi_void, evalarg_T *evalarg)
1696{
1697 forinfo_T *fi = (forinfo_T *)fi_void;
1698 int i;
1699
1700 for (i = 0; i < fi->fi_break_count; ++i)
1701 eval_next_line(evalarg);
1702}
1703
1704/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 * Use the first item in a ":for" list. Advance to the next.
1706 * Assign the values to the variable (list). "arg" points to the first one.
1707 * Return TRUE when a valid item was found, FALSE when at end of list or
1708 * something wrong.
1709 */
1710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001711next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001713 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 int result;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001715 int flag = in_vim9script() ? ASSIGN_NO_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001716 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001718 if (fi->fi_blob != NULL)
1719 {
1720 typval_T tv;
1721
1722 if (fi->fi_bi >= blob_len(fi->fi_blob))
1723 return FALSE;
1724 tv.v_type = VAR_NUMBER;
1725 tv.v_lock = VAR_FIXED;
1726 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1727 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001728 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001729 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001730 }
1731
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 item = fi->fi_lw.lw_item;
1733 if (item == NULL)
1734 result = FALSE;
1735 else
1736 {
1737 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001738 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001739 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 }
1741 return result;
1742}
1743
1744/*
1745 * Free the structure used to store info used by ":for".
1746 */
1747 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001748free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749{
Bram Moolenaar33570922005-01-25 22:26:29 +00001750 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001752 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001753 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001755 list_unref(fi->fi_list);
1756 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001757 if (fi != NULL && fi->fi_blob != NULL)
1758 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759 vim_free(fi);
1760}
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763set_context_for_expression(
1764 expand_T *xp,
1765 char_u *arg,
1766 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001768 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001772 if (cmdidx == CMD_let || cmdidx == CMD_var
1773 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 {
1775 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001776 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001778 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001779 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 {
1781 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001782 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001783 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 break;
1785 }
1786 return;
1787 }
1788 }
1789 else
1790 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1791 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 while ((xp->xp_pattern = vim_strpbrk(arg,
1793 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1794 {
1795 c = *xp->xp_pattern;
1796 if (c == '&')
1797 {
1798 c = xp->xp_pattern[1];
1799 if (c == '&')
1800 {
1801 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001802 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001807 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1808 xp->xp_pattern += 2;
1809
1810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 else if (c == '$')
1813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001814 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 xp->xp_context = EXPAND_ENV_VARS;
1816 }
1817 else if (c == '=')
1818 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001819 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 xp->xp_context = EXPAND_EXPRESSION;
1821 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001822 else if (c == '#'
1823 && xp->xp_context == EXPAND_EXPRESSION)
1824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001825 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001826 break;
1827 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001828 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 && xp->xp_context == EXPAND_FUNCTIONS
1830 && vim_strchr(xp->xp_pattern, '(') == NULL)
1831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001832 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 break;
1834 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001835 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001837 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
1839 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1840 if (c == '\\' && xp->xp_pattern[1] != NUL)
1841 ++xp->xp_pattern;
1842 xp->xp_context = EXPAND_NOTHING;
1843 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001844 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001846 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1848 /* skip */ ;
1849 xp->xp_context = EXPAND_NOTHING;
1850 }
1851 else if (c == '|')
1852 {
1853 if (xp->xp_pattern[1] == '|')
1854 {
1855 ++xp->xp_pattern;
1856 xp->xp_context = EXPAND_EXPRESSION;
1857 }
1858 else
1859 xp->xp_context = EXPAND_COMMANDS;
1860 }
1861 else
1862 xp->xp_context = EXPAND_EXPRESSION;
1863 }
1864 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001865 // Doesn't look like something valid, expand as an expression
1866 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 arg = xp->xp_pattern;
1869 if (*arg != NUL)
1870 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1871 /* skip */ ;
1872 }
1873 xp->xp_pattern = arg;
1874}
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001877 * Return TRUE if "pat" matches "text".
1878 * Does not use 'cpo' and always uses 'magic'.
1879 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001880 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001881pattern_match(char_u *pat, char_u *text, int ic)
1882{
1883 int matches = FALSE;
1884 char_u *save_cpo;
1885 regmatch_T regmatch;
1886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001887 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001888 save_cpo = p_cpo;
1889 p_cpo = (char_u *)"";
1890 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1891 if (regmatch.regprog != NULL)
1892 {
1893 regmatch.rm_ic = ic;
1894 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1895 vim_regfree(regmatch.regprog);
1896 }
1897 p_cpo = save_cpo;
1898 return matches;
1899}
1900
1901/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001902 * Handle a name followed by "(". Both for just "name(arg)" and for
1903 * "expr->name(arg)".
1904 * Returns OK or FAIL.
1905 */
1906 static int
1907eval_func(
1908 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001909 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001910 char_u *name,
1911 int name_len,
1912 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001913 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001914 typval_T *basetv) // "expr" for "expr->name(arg)"
1915{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001916 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001917 char_u *s = name;
1918 int len = name_len;
1919 partial_T *partial;
1920 int ret = OK;
1921
1922 if (!evaluate)
1923 check_vars(s, len);
1924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 // If "s" is the name of a variable of type VAR_FUNC
1926 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001927 s = deref_func_name(s, &len, &partial, !evaluate);
1928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 // Need to make a copy, in case evaluating the arguments makes
1930 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001931 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001932 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001933 ret = FAIL;
1934 else
1935 {
1936 funcexe_T funcexe;
1937
1938 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001939 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001940 funcexe.firstline = curwin->w_cursor.lnum;
1941 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001942 funcexe.evaluate = evaluate;
1943 funcexe.partial = partial;
1944 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001945 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001946 }
1947 vim_free(s);
1948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001949 // If evaluate is FALSE rettv->v_type was not set in
1950 // get_func_tv, but it's needed in handle_subscript() to parse
1951 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001952 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1953 {
1954 rettv->vval.v_string = NULL;
1955 rettv->v_type = VAR_FUNC;
1956 }
1957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001958 // Stop the expression evaluation when immediately
1959 // aborting on error, or when an interrupt occurred or
1960 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001961 if (evaluate && aborting())
1962 {
1963 if (ret == OK)
1964 clear_tv(rettv);
1965 ret = FAIL;
1966 }
1967 return ret;
1968}
1969
1970/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02001971 * Get the next line source line without advancing. But do skip over comment
1972 * lines.
1973 */
1974 static char_u *
1975getline_peek_skip_comments(evalarg_T *evalarg)
1976{
1977 for (;;)
1978 {
1979 char_u *next = getline_peek(evalarg->eval_getline,
1980 evalarg->eval_cookie);
1981 char_u *p;
1982
1983 if (next == NULL)
1984 break;
1985 p = skipwhite(next);
1986 if (*p != NUL && !vim9_comment_start(p))
1987 return next;
1988 (void)eval_next_line(evalarg);
1989 }
1990 return NULL;
1991}
1992
1993/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001994 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1995 * comment) and there is a next line, return the next line (skipping blanks)
1996 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001997 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1998 * "arg" must point somewhere inside a line, not at the start.
1999 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002000 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002001eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2002{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002003 char_u *p = skipwhite(arg);
2004
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002005 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002006 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002007 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002008 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002009 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002010 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002011 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002012
2013 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002014 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002015 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002016 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002017
Bram Moolenaar9d489562020-07-30 20:08:50 +02002018 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002019 {
2020 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002021 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002022 }
2023 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002024 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002025}
2026
2027/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002028 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002029 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002030 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002031eval_next_line(evalarg_T *evalarg)
2032{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002033 garray_T *gap = &evalarg->eval_ga;
2034 char_u *line;
2035
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002036 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002037 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2038 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002039 else
2040 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002041 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002042 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2043 {
2044 // Going to concatenate the lines after parsing.
2045 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2046 ++gap->ga_len;
2047 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002048 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002049 {
2050 vim_free(evalarg->eval_tofree);
2051 evalarg->eval_tofree = line;
2052 }
2053 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002054}
2055
Bram Moolenaare6b53242020-07-01 17:28:33 +02002056/*
2057 * Call eval_next_non_blank() and get the next line if needed.
2058 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002059 char_u *
2060skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2061{
2062 int getnext;
2063 char_u *p = skipwhite(arg);
2064
Bram Moolenaar962d7212020-07-04 14:15:00 +02002065 if (evalarg == NULL)
2066 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002067 eval_next_non_blank(p, evalarg, &getnext);
2068 if (getnext)
2069 return eval_next_line(evalarg);
2070 return p;
2071}
2072
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002073/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002074 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002075 */
2076 void
2077clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2078{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002079 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002080 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002081 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002082 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002083 if (eap != NULL)
2084 {
2085 // We may need to keep the original command line, e.g. for
2086 // ":let" it has the variable names. But we may also need the
2087 // new one, "nextcmd" points into it. Keep both.
2088 vim_free(eap->cmdline_tofree);
2089 eap->cmdline_tofree = *eap->cmdlinep;
2090 *eap->cmdlinep = evalarg->eval_tofree;
2091 }
2092 else
2093 vim_free(evalarg->eval_tofree);
2094 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002095 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002096
2097 vim_free(evalarg->eval_tofree_lambda);
2098 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002099 }
2100}
2101
2102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2106 */
2107
2108/*
2109 * Handle zero level expression.
2110 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002112 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 * Return OK or FAIL.
2115 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002117eval0(
2118 char_u *arg,
2119 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002120 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
2123 int ret;
2124 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002125 int did_emsg_before = did_emsg;
2126 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002127 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002130 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002131 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002132
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002133 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
2135 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 /*
2138 * Report the invalid expression unless the expression evaluation has
2139 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002140 * exception, or we already gave a more specific error.
2141 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002143 if (!aborting()
2144 && did_emsg == did_emsg_before
2145 && called_emsg == called_emsg_before
2146 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002147 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 ret = FAIL;
2149 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002150
2151 if (eap != NULL)
2152 eap->nextcmd = check_nextcmd(p);
2153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 return ret;
2155}
2156
2157/*
2158 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002159 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002160 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 *
2162 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002163 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002165 * Note: "rettv.v_lock" is not set.
2166 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 * Return OK or FAIL.
2168 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002169 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002170eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002172 char_u *p;
2173 int getnext;
2174
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002175 CLEAR_POINTER(rettv);
2176
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 /*
2178 * Get the first variable.
2179 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002180 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 return FAIL;
2182
Bram Moolenaar793648f2020-06-26 21:28:25 +02002183 p = eval_next_non_blank(*arg, evalarg, &getnext);
2184 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002186 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002187 int result;
2188 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002189 evalarg_T *evalarg_used = evalarg;
2190 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002191 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002192 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002193 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002194
2195 if (evalarg == NULL)
2196 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002197 CLEAR_FIELD(local_evalarg);
2198 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002199 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002200 orig_flags = evalarg_used->eval_flags;
2201 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002202
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002203 if (getnext)
2204 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002205 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002206 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002207 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002208 {
2209 error_white_both(p, 1);
2210 clear_tv(rettv);
2211 return FAIL;
2212 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002213 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002214 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002217 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002219 int error = FALSE;
2220
Bram Moolenaar13106602020-10-04 16:06:05 +02002221 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002222 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002223 else if (vim9script)
2224 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002225 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002227 if (error || !op_falsy || !result)
2228 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002229 if (error)
2230 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232
2233 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002234 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002236 if (op_falsy)
2237 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002238 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002239 {
2240 error_white_both(p, 1);
2241 clear_tv(rettv);
2242 return FAIL;
2243 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002244 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002245 evalarg_used->eval_flags = (op_falsy ? !result : result)
2246 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2247 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002248 {
2249 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002251 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002252 if (!op_falsy || !result)
2253 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002255 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002257 /*
2258 * Check for the ":".
2259 */
2260 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2261 if (*p != ':')
2262 {
2263 emsg(_(e_missing_colon));
2264 if (evaluate && result)
2265 clear_tv(rettv);
2266 evalarg_used->eval_flags = orig_flags;
2267 return FAIL;
2268 }
2269 if (getnext)
2270 *arg = eval_next_line(evalarg_used);
2271 else
2272 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002273 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002274 {
2275 error_white_both(p, 1);
2276 clear_tv(rettv);
2277 evalarg_used->eval_flags = orig_flags;
2278 return FAIL;
2279 }
2280 *arg = p;
2281 }
2282
2283 /*
2284 * Get the third variable. Recursive!
2285 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002286 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002287 {
2288 error_white_both(p, 1);
2289 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002290 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002291 return FAIL;
2292 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002293 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2294 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002295 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002296 if (eval1(arg, &var2, evalarg_used) == FAIL)
2297 {
2298 if (evaluate && result)
2299 clear_tv(rettv);
2300 evalarg_used->eval_flags = orig_flags;
2301 return FAIL;
2302 }
2303 if (evaluate && !result)
2304 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002306
2307 if (evalarg == NULL)
2308 clear_evalarg(&local_evalarg, NULL);
2309 else
2310 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312
2313 return OK;
2314}
2315
2316/*
2317 * Handle first level expression:
2318 * expr2 || expr2 || expr2 logical OR
2319 *
2320 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002321 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 *
2323 * Return OK or FAIL.
2324 */
2325 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002326eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002328 char_u *p;
2329 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331 /*
2332 * Get the first variable.
2333 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002334 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 return FAIL;
2336
2337 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002338 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002340 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002341 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002343 evalarg_T *evalarg_used = evalarg;
2344 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002345 int evaluate;
2346 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002347 long result = FALSE;
2348 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002349 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002350 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002351
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002352 if (evalarg == NULL)
2353 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002354 CLEAR_FIELD(local_evalarg);
2355 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002356 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002357 orig_flags = evalarg_used->eval_flags;
2358 evaluate = orig_flags & EVAL_EVALUATE;
2359 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002360 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002361 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002362 result = tv_get_bool_chk(rettv, &error);
2363 else if (tv_get_number_chk(rettv, &error) != 0)
2364 result = TRUE;
2365 clear_tv(rettv);
2366 if (error)
2367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369
2370 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002371 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002373 while (p[0] == '|' && p[1] == '|')
2374 {
2375 if (getnext)
2376 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002377 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002378 {
2379 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2380 {
2381 error_white_both(p, 2);
2382 clear_tv(rettv);
2383 return FAIL;
2384 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002385 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002386 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002387
2388 /*
2389 * Get the second variable.
2390 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002391 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2392 {
2393 error_white_both(p, 2);
2394 clear_tv(rettv);
2395 return FAIL;
2396 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002397 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2398 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002399 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002400 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002402
2403 /*
2404 * Compute the result.
2405 */
2406 if (evaluate && !result)
2407 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002408 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002409 result = tv_get_bool_chk(&var2, &error);
2410 else if (tv_get_number_chk(&var2, &error) != 0)
2411 result = TRUE;
2412 clear_tv(&var2);
2413 if (error)
2414 return FAIL;
2415 }
2416 if (evaluate)
2417 {
2418 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002419 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002420 rettv->v_type = VAR_BOOL;
2421 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002422 }
2423 else
2424 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002425 rettv->v_type = VAR_NUMBER;
2426 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002427 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002428 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002429
2430 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002432
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 if (evalarg == NULL)
2434 clear_evalarg(&local_evalarg, NULL);
2435 else
2436 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
2438
2439 return OK;
2440}
2441
2442/*
2443 * Handle second level expression:
2444 * expr3 && expr3 && expr3 logical AND
2445 *
2446 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002447 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 *
2449 * Return OK or FAIL.
2450 */
2451 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002452eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002454 char_u *p;
2455 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 /*
2458 * Get the first variable.
2459 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002460 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 return FAIL;
2462
2463 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002464 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002466 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002467 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002469 evalarg_T *evalarg_used = evalarg;
2470 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002471 int orig_flags;
2472 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002473 long result = TRUE;
2474 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002475 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002476 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002477
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002478 if (evalarg == NULL)
2479 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002480 CLEAR_FIELD(local_evalarg);
2481 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002482 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002483 orig_flags = evalarg_used->eval_flags;
2484 evaluate = orig_flags & EVAL_EVALUATE;
2485 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002486 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002487 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002488 result = tv_get_bool_chk(rettv, &error);
2489 else if (tv_get_number_chk(rettv, &error) == 0)
2490 result = FALSE;
2491 clear_tv(rettv);
2492 if (error)
2493 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495
2496 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002499 while (p[0] == '&' && p[1] == '&')
2500 {
2501 if (getnext)
2502 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002503 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002504 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002505 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002506 {
2507 error_white_both(p, 2);
2508 clear_tv(rettv);
2509 return FAIL;
2510 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002511 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002512 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513
2514 /*
2515 * Get the second variable.
2516 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002517 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2518 {
2519 error_white_both(p, 2);
2520 clear_tv(rettv);
2521 return FAIL;
2522 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002523 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2524 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002525 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002526 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002528 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529
2530 /*
2531 * Compute the result.
2532 */
2533 if (evaluate && result)
2534 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002535 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002536 result = tv_get_bool_chk(&var2, &error);
2537 else if (tv_get_number_chk(&var2, &error) == 0)
2538 result = FALSE;
2539 clear_tv(&var2);
2540 if (error)
2541 return FAIL;
2542 }
2543 if (evaluate)
2544 {
2545 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002546 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002547 rettv->v_type = VAR_BOOL;
2548 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002549 }
2550 else
2551 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002552 rettv->v_type = VAR_NUMBER;
2553 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002554 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002555 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002556
2557 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002559
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560 if (evalarg == NULL)
2561 clear_evalarg(&local_evalarg, NULL);
2562 else
2563 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
2565
2566 return OK;
2567}
2568
2569/*
2570 * Handle third level expression:
2571 * var1 == var2
2572 * var1 =~ var2
2573 * var1 != var2
2574 * var1 !~ var2
2575 * var1 > var2
2576 * var1 >= var2
2577 * var1 < var2
2578 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002579 * var1 is var2
2580 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 *
2582 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002583 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 *
2585 * Return OK or FAIL.
2586 */
2587 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002588eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002591 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002592 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002594 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
2596 /*
2597 * Get the first variable.
2598 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002599 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 return FAIL;
2601
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002602 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002603 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
2605 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002606 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002608 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002610 typval_T var2;
2611 int ic;
2612 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002613 int evaluate = evalarg == NULL
2614 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002615
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002616 if (getnext)
2617 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002618 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2619 {
2620 error_white_both(p, len);
2621 clear_tv(rettv);
2622 return FAIL;
2623 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002624
Bram Moolenaar696ba232020-07-29 21:20:41 +02002625 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2626 {
2627 semsg(_(e_invexpr2), p);
2628 clear_tv(rettv);
2629 return FAIL;
2630 }
2631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002632 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (p[len] == '?')
2634 {
2635 ic = TRUE;
2636 ++len;
2637 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002638 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 else if (p[len] == '#')
2640 {
2641 ic = FALSE;
2642 ++len;
2643 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002644 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002646 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
2648 /*
2649 * Get the second variable.
2650 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002651 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2652 {
2653 error_white_both(p, 1);
2654 clear_tv(rettv);
2655 return FAIL;
2656 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002657 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002658 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 return FAIL;
2662 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002663 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002664 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002665 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002666
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002667 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002668 {
2669 ret = FAIL;
2670 clear_tv(rettv);
2671 }
2672 else
2673 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002674 clear_tv(&var2);
2675 return ret;
2676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678
2679 return OK;
2680}
2681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 void
2683eval_addblob(typval_T *tv1, typval_T *tv2)
2684{
2685 blob_T *b1 = tv1->vval.v_blob;
2686 blob_T *b2 = tv2->vval.v_blob;
2687 blob_T *b = blob_alloc();
2688 int i;
2689
2690 if (b != NULL)
2691 {
2692 for (i = 0; i < blob_len(b1); i++)
2693 ga_append(&b->bv_ga, blob_get(b1, i));
2694 for (i = 0; i < blob_len(b2); i++)
2695 ga_append(&b->bv_ga, blob_get(b2, i));
2696
2697 clear_tv(tv1);
2698 rettv_blob_set(tv1, b);
2699 }
2700}
2701
2702 int
2703eval_addlist(typval_T *tv1, typval_T *tv2)
2704{
2705 typval_T var3;
2706
2707 // concatenate Lists
2708 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2709 {
2710 clear_tv(tv1);
2711 clear_tv(tv2);
2712 return FAIL;
2713 }
2714 clear_tv(tv1);
2715 *tv1 = var3;
2716 return OK;
2717}
2718
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719/*
2720 * Handle fourth level expression:
2721 * + number addition
2722 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002723 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002724 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 *
2726 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002727 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 *
2729 * Return OK or FAIL.
2730 */
2731 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002732eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 /*
2735 * Get the first variable.
2736 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002737 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 return FAIL;
2739
2740 /*
2741 * Repeat computing, until no '+', '-' or '.' is following.
2742 */
2743 for (;;)
2744 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002745 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002746 int getnext;
2747 char_u *p;
2748 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002749 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002750 int concat;
2751 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002752 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002753
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002754 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002755 p = eval_next_non_blank(*arg, evalarg, &getnext);
2756 op = *p;
2757 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002758 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002760
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002761 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002762 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002763 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002764 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002765 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002766 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002767 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002768 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002769 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002770 clear_tv(rettv);
2771 return FAIL;
2772 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002773 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002774 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002775 if ((op != '+' || (rettv->v_type != VAR_LIST
2776 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002777#ifdef FEAT_FLOAT
2778 && (op == '.' || rettv->v_type != VAR_FLOAT)
2779#endif
2780 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002782 // For "list + ...", an illegal use of the first operand as
2783 // a number cannot be determined before evaluating the 2nd
2784 // operand: if this is also a list, all is ok.
2785 // For "something . ...", "something - ..." or "non-list + ...",
2786 // we know that the first operand needs to be a string or number
2787 // without evaluating the 2nd operand. So check before to avoid
2788 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002789 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002790 {
2791 clear_tv(rettv);
2792 return FAIL;
2793 }
2794 }
2795
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 /*
2797 * Get the second variable.
2798 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002799 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002800 {
2801 error_white_both(p, oplen);
2802 clear_tv(rettv);
2803 return FAIL;
2804 }
2805 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002806 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002808 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 return FAIL;
2810 }
2811
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
2814 /*
2815 * Compute the result.
2816 */
2817 if (op == '.')
2818 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2820 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002821 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002822
Bram Moolenaar2e086612020-08-25 22:37:48 +02002823 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002824 || var2.v_type == VAR_CHANNEL
2825 || var2.v_type == VAR_JOB))
2826 emsg(_(e_inval_string));
2827#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002828 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002829 {
2830 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2831 var2.vval.v_float);
2832 s2 = buf2;
2833 }
2834#endif
2835 else
2836 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002837 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002838 {
2839 clear_tv(rettv);
2840 clear_tv(&var2);
2841 return FAIL;
2842 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002844 clear_tv(rettv);
2845 rettv->v_type = VAR_STRING;
2846 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002848 else if (op == '+' && rettv->v_type == VAR_BLOB
2849 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002851 else if (op == '+' && rettv->v_type == VAR_LIST
2852 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002853 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002855 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 else
2858 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002859 int error = FALSE;
2860 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002861#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862 float_T f1 = 0, f2 = 0;
2863
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002864 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002866 f1 = rettv->vval.v_float;
2867 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002868 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002869 else
2870#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002872 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002873 if (error)
2874 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002875 // This can only happen for "list + non-list". For
2876 // "non-list + ..." or "something - ...", we returned
2877 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002878 clear_tv(rettv);
2879 return FAIL;
2880 }
2881#ifdef FEAT_FLOAT
2882 if (var2.v_type == VAR_FLOAT)
2883 f1 = n1;
2884#endif
2885 }
2886#ifdef FEAT_FLOAT
2887 if (var2.v_type == VAR_FLOAT)
2888 {
2889 f2 = var2.vval.v_float;
2890 n2 = 0;
2891 }
2892 else
2893#endif
2894 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002895 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002896 if (error)
2897 {
2898 clear_tv(rettv);
2899 clear_tv(&var2);
2900 return FAIL;
2901 }
2902#ifdef FEAT_FLOAT
2903 if (rettv->v_type == VAR_FLOAT)
2904 f2 = n2;
2905#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002907 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002908
2909#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002910 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002911 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2912 {
2913 if (op == '+')
2914 f1 = f1 + f2;
2915 else
2916 f1 = f1 - f2;
2917 rettv->v_type = VAR_FLOAT;
2918 rettv->vval.v_float = f1;
2919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002921#endif
2922 {
2923 if (op == '+')
2924 n1 = n1 + n2;
2925 else
2926 n1 = n1 - n2;
2927 rettv->v_type = VAR_NUMBER;
2928 rettv->vval.v_number = n1;
2929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002931 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
2933 }
2934 return OK;
2935}
2936
2937/*
2938 * Handle fifth level expression:
2939 * * number multiplication
2940 * / number division
2941 * % number modulo
2942 *
2943 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002944 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 *
2946 * Return OK or FAIL.
2947 */
2948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002949eval6(
2950 char_u **arg,
2951 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002952 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002953 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002955#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002956 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 /*
2960 * Get the first variable.
2961 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002962 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 return FAIL;
2964
2965 /*
2966 * Repeat computing, until no '*', '/' or '%' is following.
2967 */
2968 for (;;)
2969 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002970 int evaluate;
2971 int getnext;
2972 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002973 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002974 int op;
2975 varnumber_T n1, n2;
2976#ifdef FEAT_FLOAT
2977 float_T f1, f2;
2978#endif
2979 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002980
Bram Moolenaar9d489562020-07-30 20:08:50 +02002981 p = eval_next_non_blank(*arg, evalarg, &getnext);
2982 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002983 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002985
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002986 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002988 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002989 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002990 {
2991 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2992 {
2993 error_white_both(p, 1);
2994 clear_tv(rettv);
2995 return FAIL;
2996 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002997 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003000#ifdef FEAT_FLOAT
3001 f1 = 0;
3002 f2 = 0;
3003#endif
3004 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003005 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003007#ifdef FEAT_FLOAT
3008 if (rettv->v_type == VAR_FLOAT)
3009 {
3010 f1 = rettv->vval.v_float;
3011 use_float = TRUE;
3012 n1 = 0;
3013 }
3014 else
3015#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003016 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003018 if (error)
3019 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021 else
3022 n1 = 0;
3023
3024 /*
3025 * Get the second variable.
3026 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003027 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3028 {
3029 error_white_both(p, 1);
3030 clear_tv(rettv);
3031 return FAIL;
3032 }
3033 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003034 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 return FAIL;
3036
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003037 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039#ifdef FEAT_FLOAT
3040 if (var2.v_type == VAR_FLOAT)
3041 {
3042 if (!use_float)
3043 {
3044 f1 = n1;
3045 use_float = TRUE;
3046 }
3047 f2 = var2.vval.v_float;
3048 n2 = 0;
3049 }
3050 else
3051#endif
3052 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003053 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003054 clear_tv(&var2);
3055 if (error)
3056 return FAIL;
3057#ifdef FEAT_FLOAT
3058 if (use_float)
3059 f2 = n2;
3060#endif
3061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 /*
3064 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067#ifdef FEAT_FLOAT
3068 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003070 if (op == '*')
3071 f1 = f1 * f2;
3072 else if (op == '/')
3073 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003074# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003075 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003076 if (f2 == 0.0)
3077 {
3078 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003079 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003080 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003081 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003082 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003083 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003084 }
3085 else
3086 f1 = f1 / f2;
3087# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 // We rely on the floating point library to handle divide
3089 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003090 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003091# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003094 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003096 return FAIL;
3097 }
3098 rettv->v_type = VAR_FLOAT;
3099 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003104 if (op == '*')
3105 n1 = n1 * n2;
3106 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003107 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003109 n1 = num_modulus(n1, n2);
3110
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111 rettv->v_type = VAR_NUMBER;
3112 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
3115 }
3116
3117 return OK;
3118}
3119
3120/*
3121 * Handle sixth level expression:
3122 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003123 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003124 * "string" string constant
3125 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 * &option-name option value
3127 * @r register contents
3128 * identifier variable value
3129 * function() function call
3130 * $VAR environment variable
3131 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003132 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003134 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003135 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 *
3137 * Also handle:
3138 * ! in front logical NOT
3139 * - in front unary minus
3140 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003141 * trailing [] subscript in String or List
3142 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003143 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 *
3145 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003146 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 *
3148 * Return OK or FAIL.
3149 */
3150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003151eval7(
3152 char_u **arg,
3153 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003154 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003157 int evaluate = evalarg != NULL
3158 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int len;
3160 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 char_u *start_leader, *end_leader;
3162 int ret = OK;
3163 char_u *alias;
3164
3165 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003166 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003167 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003169 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003172 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 */
3174 start_leader = *arg;
3175 while (**arg == '!' || **arg == '-' || **arg == '+')
3176 *arg = skipwhite(*arg + 1);
3177 end_leader = *arg;
3178
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003179 if (**arg == '.' && (!isdigit(*(*arg + 1))
3180#ifdef FEAT_FLOAT
3181 || current_sctx.sc_version < 2
3182#endif
3183 ))
3184 {
3185 semsg(_(e_invexpr2), *arg);
3186 ++*arg;
3187 return FAIL;
3188 }
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 switch (**arg)
3191 {
3192 /*
3193 * Number constant.
3194 */
3195 case '0':
3196 case '1':
3197 case '2':
3198 case '3':
3199 case '4':
3200 case '5':
3201 case '6':
3202 case '7':
3203 case '8':
3204 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003205 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003206
3207 // Apply prefixed "-" and "+" now. Matters especially when
3208 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003209 if (ret == OK && evaluate && end_leader > start_leader
3210 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003211 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 break;
3213
3214 /*
3215 * String constant: "string".
3216 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003217 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 break;
3219
3220 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003221 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003223 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003224 break;
3225
3226 /*
3227 * List: [expr, expr]
3228 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003229 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 break;
3231
3232 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003233 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003234 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003235 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003236 {
3237 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003238 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003239 }
3240 else
3241 ret = NOTDONE;
3242 break;
3243
3244 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003245 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003246 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003247 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003248 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003249 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003250 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003251 break;
3252
3253 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003254 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003256 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 break;
3258
3259 /*
3260 * Environment variable: $VAR.
3261 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003262 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 break;
3264
3265 /*
3266 * Register contents: @r.
3267 */
3268 case '@': ++*arg;
3269 if (evaluate)
3270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003271 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003272 rettv->vval.v_string = get_reg_contents(**arg,
3273 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275 if (**arg != NUL)
3276 ++*arg;
3277 break;
3278
3279 /*
3280 * nested expression: (expression).
3281 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003282 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003283 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003284 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003285
Bram Moolenaar9215f012020-06-27 21:18:00 +02003286 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003287 if (**arg == ')')
3288 ++*arg;
3289 else if (ret == OK)
3290 {
3291 emsg(_(e_missing_close));
3292 clear_tv(rettv);
3293 ret = FAIL;
3294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296 break;
3297
Bram Moolenaar8c711452005-01-14 21:53:12 +00003298 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 break;
3300 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003301
3302 if (ret == NOTDONE)
3303 {
3304 /*
3305 * Must be a variable or function name.
3306 * Can also be a curly-braces kind of name: {expr}.
3307 */
3308 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003309 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003310 if (alias != NULL)
3311 s = alias;
3312
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003313 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003314 ret = FAIL;
3315 else
3316 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003317 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3318
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003319 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3320 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003321 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003322 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003323 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003324 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003325 else if (flags & EVAL_CONSTANT)
3326 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003327 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003328 {
3329 // get the value of "true", "false" or a variable
3330 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3331 {
3332 rettv->v_type = VAR_BOOL;
3333 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003334 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003335 }
3336 else if (len == 5 && in_vim9script()
3337 && STRNCMP(s, "false", 4) == 0)
3338 {
3339 rettv->v_type = VAR_BOOL;
3340 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003341 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003342 }
3343 else
3344 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3345 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003346 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003347 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003348 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003349 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003350 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003351 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003352 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003353 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003354 }
3355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003356 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3357 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003358 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003359 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361 /*
3362 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3363 */
3364 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003365 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003366 return ret;
3367}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003368
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003369/*
3370 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003371 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003372 * Adjusts "end_leaderp" until it is at "start_leader".
3373 */
3374 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003375eval7_leader(
3376 typval_T *rettv,
3377 int numeric_only,
3378 char_u *start_leader,
3379 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003380{
3381 char_u *end_leader = *end_leaderp;
3382 int ret = OK;
3383 int error = FALSE;
3384 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003385 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003386#ifdef FEAT_FLOAT
3387 float_T f = 0.0;
3388
3389 if (rettv->v_type == VAR_FLOAT)
3390 f = rettv->vval.v_float;
3391 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003392#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003393 {
3394 while (VIM_ISWHITE(end_leader[-1]))
3395 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003396 if (in_vim9script() && end_leader[-1] == '!')
3397 val = tv2bool(rettv);
3398 else
3399 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003400 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003401 if (error)
3402 {
3403 clear_tv(rettv);
3404 ret = FAIL;
3405 }
3406 else
3407 {
3408 while (end_leader > start_leader)
3409 {
3410 --end_leader;
3411 if (*end_leader == '!')
3412 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003413 if (numeric_only)
3414 {
3415 ++end_leader;
3416 break;
3417 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003418#ifdef FEAT_FLOAT
3419 if (rettv->v_type == VAR_FLOAT)
3420 f = !f;
3421 else
3422#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003423 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003424 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003425 type = VAR_BOOL;
3426 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003427 }
3428 else if (*end_leader == '-')
3429 {
3430#ifdef FEAT_FLOAT
3431 if (rettv->v_type == VAR_FLOAT)
3432 f = -f;
3433 else
3434#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003435 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003436 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003437 type = VAR_NUMBER;
3438 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003439 }
3440 }
3441#ifdef FEAT_FLOAT
3442 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003444 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003445 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003447 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003448#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003449 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003450 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003451 if (in_vim9script())
3452 rettv->v_type = type;
3453 else
3454 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003455 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003458 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 return ret;
3460}
3461
3462/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003463 * Call the function referred to in "rettv".
3464 */
3465 static int
3466call_func_rettv(
3467 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003468 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003469 typval_T *rettv,
3470 int evaluate,
3471 dict_T *selfdict,
3472 typval_T *basetv)
3473{
3474 partial_T *pt = NULL;
3475 funcexe_T funcexe;
3476 typval_T functv;
3477 char_u *s;
3478 int ret;
3479
3480 // need to copy the funcref so that we can clear rettv
3481 if (evaluate)
3482 {
3483 functv = *rettv;
3484 rettv->v_type = VAR_UNKNOWN;
3485
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003486 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003487 if (functv.v_type == VAR_PARTIAL)
3488 {
3489 pt = functv.vval.v_partial;
3490 s = partial_name(pt);
3491 }
3492 else
3493 s = functv.vval.v_string;
3494 }
3495 else
3496 s = (char_u *)"";
3497
Bram Moolenaara80faa82020-04-12 19:37:17 +02003498 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003499 funcexe.firstline = curwin->w_cursor.lnum;
3500 funcexe.lastline = curwin->w_cursor.lnum;
3501 funcexe.evaluate = evaluate;
3502 funcexe.partial = pt;
3503 funcexe.selfdict = selfdict;
3504 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003505 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003506
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003507 // Clear the funcref afterwards, so that deleting it while
3508 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003509 if (evaluate)
3510 clear_tv(&functv);
3511
3512 return ret;
3513}
3514
3515/*
3516 * Evaluate "->method()".
3517 * "*arg" points to the '-'.
3518 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3519 */
3520 static int
3521eval_lambda(
3522 char_u **arg,
3523 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003524 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003525 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003526{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003527 int evaluate = evalarg != NULL
3528 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003529 typval_T base = *rettv;
3530 int ret;
3531
3532 // Skip over the ->.
3533 *arg += 2;
3534 rettv->v_type = VAR_UNKNOWN;
3535
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003536 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003537 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003538 return FAIL;
3539 else if (**arg != '(')
3540 {
3541 if (verbose)
3542 {
3543 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003544 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003545 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003547 }
3548 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003549 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003550 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003551 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003552 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003553
3554 // Clear the funcref afterwards, so that deleting it while
3555 // evaluating the arguments is possible (see test55).
3556 if (evaluate)
3557 clear_tv(&base);
3558
3559 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003560}
3561
3562/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003563 * Evaluate "->method()".
3564 * "*arg" points to the '-'.
3565 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3566 */
3567 static int
3568eval_method(
3569 char_u **arg,
3570 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003571 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003572 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003573{
3574 char_u *name;
3575 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003576 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003577 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003578 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003579 int evaluate = evalarg != NULL
3580 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003581
3582 // Skip over the ->.
3583 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003584 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003585
Bram Moolenaarac92e252019-08-03 21:58:38 +02003586 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003587 len = get_name_len(arg, &alias, evaluate, TRUE);
3588 if (alias != NULL)
3589 name = alias;
3590
3591 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003592 {
3593 if (verbose)
3594 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003595 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003596 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003597 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003598 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003599 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003600 if (**arg != '(')
3601 {
3602 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003604 ret = FAIL;
3605 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003606 else if (VIM_ISWHITE((*arg)[-1]))
3607 {
3608 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003609 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003610 ret = FAIL;
3611 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003612 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003613 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003614 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003615 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003616
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003617 // Clear the funcref afterwards, so that deleting it while
3618 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003619 if (evaluate)
3620 clear_tv(&base);
3621
Bram Moolenaarac92e252019-08-03 21:58:38 +02003622 return ret;
3623}
3624
3625/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003626 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3627 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003628 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3629 */
3630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631eval_index(
3632 char_u **arg,
3633 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003634 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003635 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003636{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003637 int evaluate = evalarg != NULL
3638 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003639 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003640 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003641 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003642 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003643 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003644
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003645 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3646 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003647
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003648 init_tv(&var1);
3649 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003652 /*
3653 * dict.name
3654 */
3655 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003656 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003658 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003659 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003660 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003661 }
3662 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003663 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664 /*
3665 * something[idx]
3666 *
3667 * Get the (first) variable from inside the [].
3668 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003669 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670 if (**arg == ':')
3671 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003672 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003674 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003675 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003676 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003677 clear_tv(&var1);
3678 return FAIL;
3679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680
3681 /*
3682 * Get the second variable from inside the [:].
3683 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003684 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003685 if (**arg == ':')
3686 {
3687 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003688 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689 if (**arg == ']')
3690 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003691 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003693 if (!empty1)
3694 clear_tv(&var1);
3695 return FAIL;
3696 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003697 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003698 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003699 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003700 if (!empty1)
3701 clear_tv(&var1);
3702 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003703 return FAIL;
3704 }
3705 }
3706
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003707 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003708 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 if (**arg != ']')
3710 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003711 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003712 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003713 clear_tv(&var1);
3714 if (range)
3715 clear_tv(&var2);
3716 return FAIL;
3717 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003718 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003719 }
3720
3721 if (evaluate)
3722 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003723 int res = eval_index_inner(rettv, range,
3724 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3725 key, keylen, verbose);
3726 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003728 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003729 clear_tv(&var2);
3730 return res;
3731 }
3732 return OK;
3733}
3734
3735/*
3736 * Check if "rettv" can have an [index] or [sli:ce]
3737 */
3738 int
3739check_can_index(typval_T *rettv, int evaluate, int verbose)
3740{
3741 switch (rettv->v_type)
3742 {
3743 case VAR_FUNC:
3744 case VAR_PARTIAL:
3745 if (verbose)
3746 emsg(_("E695: Cannot index a Funcref"));
3747 return FAIL;
3748 case VAR_FLOAT:
3749#ifdef FEAT_FLOAT
3750 if (verbose)
3751 emsg(_(e_float_as_string));
3752 return FAIL;
3753#endif
3754 case VAR_BOOL:
3755 case VAR_SPECIAL:
3756 case VAR_JOB:
3757 case VAR_CHANNEL:
3758 if (verbose)
3759 emsg(_(e_cannot_index_special_variable));
3760 return FAIL;
3761 case VAR_UNKNOWN:
3762 case VAR_ANY:
3763 case VAR_VOID:
3764 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003765 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003766 emsg(_(e_cannot_index_special_variable));
3767 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003768 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003769 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003770
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003771 case VAR_STRING:
3772 case VAR_LIST:
3773 case VAR_DICT:
3774 case VAR_BLOB:
3775 break;
3776 case VAR_NUMBER:
3777 if (in_vim9script())
3778 emsg(_(e_cannot_index_number));
3779 break;
3780 }
3781 return OK;
3782}
3783
3784/*
3785 * Apply index or range to "rettv".
3786 * "var1" is the first index, NULL for [:expr].
3787 * "var2" is the second index, NULL for [expr] and [expr: ]
3788 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3789 */
3790 int
3791eval_index_inner(
3792 typval_T *rettv,
3793 int is_range,
3794 typval_T *var1,
3795 typval_T *var2,
3796 char_u *key,
3797 int keylen,
3798 int verbose)
3799{
3800 long n1, n2 = 0;
3801 long len;
3802
3803 n1 = 0;
3804 if (var1 != NULL && rettv->v_type != VAR_DICT)
3805 n1 = tv_get_number(var1);
3806
3807 if (is_range)
3808 {
3809 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003810 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003811 if (verbose)
3812 emsg(_(e_cannot_slice_dictionary));
3813 return FAIL;
3814 }
3815 if (var2 == NULL)
3816 n2 = -1;
3817 else
3818 n2 = tv_get_number(var2);
3819 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003820
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003821 switch (rettv->v_type)
3822 {
3823 case VAR_UNKNOWN:
3824 case VAR_ANY:
3825 case VAR_VOID:
3826 case VAR_FUNC:
3827 case VAR_PARTIAL:
3828 case VAR_FLOAT:
3829 case VAR_BOOL:
3830 case VAR_SPECIAL:
3831 case VAR_JOB:
3832 case VAR_CHANNEL:
3833 break; // not evaluating, skipping over subscript
3834
3835 case VAR_NUMBER:
3836 case VAR_STRING:
3837 {
3838 char_u *s = tv_get_string(rettv);
3839
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003840 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003841 if (in_vim9script())
3842 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003843 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003844 s = string_slice(s, n1, n2);
3845 else
3846 s = char_from_string(s, n1);
3847 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003848 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003850 // The resulting variable is a substring. If the indexes
3851 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003852 if (n1 < 0)
3853 {
3854 n1 = len + n1;
3855 if (n1 < 0)
3856 n1 = 0;
3857 }
3858 if (n2 < 0)
3859 n2 = len + n2;
3860 else if (n2 >= len)
3861 n2 = len;
3862 if (n1 >= len || n2 < 0 || n1 > n2)
3863 s = NULL;
3864 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003865 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003866 }
3867 else
3868 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003869 // The resulting variable is a string of a single
3870 // character. If the index is too big or negative the
3871 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003872 if (n1 >= len || n1 < 0)
3873 s = NULL;
3874 else
3875 s = vim_strnsave(s + n1, 1);
3876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003877 clear_tv(rettv);
3878 rettv->v_type = VAR_STRING;
3879 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003880 }
3881 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003882
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003883 case VAR_BLOB:
3884 len = blob_len(rettv->vval.v_blob);
3885 if (is_range)
3886 {
3887 // The resulting variable is a sub-blob. If the indexes
3888 // are out of range the result is empty.
3889 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003890 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003891 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003892 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003893 n1 = 0;
3894 }
3895 if (n2 < 0)
3896 n2 = len + n2;
3897 else if (n2 >= len)
3898 n2 = len - 1;
3899 if (n1 >= len || n2 < 0 || n1 > n2)
3900 {
3901 clear_tv(rettv);
3902 rettv->v_type = VAR_BLOB;
3903 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003904 }
3905 else
3906 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003907 blob_T *blob = blob_alloc();
3908 long i;
3909
3910 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003911 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003912 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003913 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003914 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003915 return FAIL;
3916 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003917 blob->bv_ga.ga_len = n2 - n1 + 1;
3918 for (i = n1; i <= n2; i++)
3919 blob_set(blob, i - n1,
3920 blob_get(rettv->vval.v_blob, i));
3921
3922 clear_tv(rettv);
3923 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003925 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003926 }
3927 else
3928 {
3929 // The resulting variable is a byte value.
3930 // If the index is too big or negative that is an error.
3931 if (n1 < 0)
3932 n1 = len + n1;
3933 if (n1 < len && n1 >= 0)
3934 {
3935 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003936
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003937 clear_tv(rettv);
3938 rettv->v_type = VAR_NUMBER;
3939 rettv->vval.v_number = v;
3940 }
3941 else
3942 semsg(_(e_blobidx), n1);
3943 }
3944 break;
3945
3946 case VAR_LIST:
3947 if (var1 == NULL)
3948 n1 = 0;
3949 if (var2 == NULL)
3950 n2 = -1;
3951 if (list_slice_or_index(rettv->vval.v_list,
3952 is_range, n1, n2, rettv, verbose) == FAIL)
3953 return FAIL;
3954 break;
3955
3956 case VAR_DICT:
3957 {
3958 dictitem_T *item;
3959 typval_T tmp;
3960
3961 if (key == NULL)
3962 {
3963 key = tv_get_string_chk(var1);
3964 if (key == NULL)
3965 return FAIL;
3966 }
3967
3968 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
3969
3970 if (item == NULL && verbose)
3971 semsg(_(e_dictkey), key);
3972 if (item == NULL)
3973 return FAIL;
3974
3975 copy_tv(&item->di_tv, &tmp);
3976 clear_tv(rettv);
3977 *rettv = tmp;
3978 }
3979 break;
3980 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003981 return OK;
3982}
3983
3984/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003985 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003986 */
3987 char_u *
3988partial_name(partial_T *pt)
3989{
3990 if (pt->pt_name != NULL)
3991 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003992 if (pt->pt_func != NULL)
3993 return pt->pt_func->uf_name;
3994 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003995}
3996
Bram Moolenaarddecc252016-04-06 22:59:37 +02003997 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003998partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003999{
4000 int i;
4001
4002 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004003 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004004 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004005 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004006 if (pt->pt_name != NULL)
4007 {
4008 func_unref(pt->pt_name);
4009 vim_free(pt->pt_name);
4010 }
4011 else
4012 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004013
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004014 // Decrease the reference count for the context of a closure. If down
4015 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004016 if (pt->pt_funcstack != NULL)
4017 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004018 --pt->pt_funcstack->fs_refcount;
4019 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004020 }
4021
Bram Moolenaarddecc252016-04-06 22:59:37 +02004022 vim_free(pt);
4023}
4024
4025/*
4026 * Unreference a closure: decrement the reference count and free it when it
4027 * becomes zero.
4028 */
4029 void
4030partial_unref(partial_T *pt)
4031{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004032 if (pt != NULL)
4033 {
4034 if (--pt->pt_refcount <= 0)
4035 partial_free(pt);
4036
4037 // If the reference count goes down to one, the funcstack may be the
4038 // only reference and can be freed if no other partials reference it.
4039 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4040 funcstack_check_refcount(pt->pt_funcstack);
4041 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004042}
4043
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004044/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004045 * Return the next (unique) copy ID.
4046 * Used for serializing nested structures.
4047 */
4048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004049get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004050{
4051 current_copyID += COPYID_INC;
4052 return current_copyID;
4053}
4054
4055/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004056 * Garbage collection for lists and dictionaries.
4057 *
4058 * We use reference counts to be able to free most items right away when they
4059 * are no longer used. But for composite items it's possible that it becomes
4060 * unused while the reference count is > 0: When there is a recursive
4061 * reference. Example:
4062 * :let l = [1, 2, 3]
4063 * :let d = {9: l}
4064 * :let l[1] = d
4065 *
4066 * Since this is quite unusual we handle this with garbage collection: every
4067 * once in a while find out which lists and dicts are not referenced from any
4068 * variable.
4069 *
4070 * Here is a good reference text about garbage collection (refers to Python
4071 * but it applies to all reference-counting mechanisms):
4072 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004073 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004074
4075/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004076 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004077 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004078 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004079 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004080 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004081garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004082{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004083 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004084 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004085 buf_T *buf;
4086 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004087 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004088 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004089
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004090 if (!testing)
4091 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004092 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004093 want_garbage_collect = FALSE;
4094 may_garbage_collect = FALSE;
4095 garbage_collect_at_exit = FALSE;
4096 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004097
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004098 // The execution stack can grow big, limit the size.
4099 if (exestack.ga_maxlen - exestack.ga_len > 500)
4100 {
4101 size_t new_len;
4102 char_u *pp;
4103 int n;
4104
4105 // Keep 150% of the current size, with a minimum of the growth size.
4106 n = exestack.ga_len / 2;
4107 if (n < exestack.ga_growsize)
4108 n = exestack.ga_growsize;
4109
4110 // Don't make it bigger though.
4111 if (exestack.ga_len + n < exestack.ga_maxlen)
4112 {
4113 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4114 pp = vim_realloc(exestack.ga_data, new_len);
4115 if (pp == NULL)
4116 return FAIL;
4117 exestack.ga_maxlen = exestack.ga_len + n;
4118 exestack.ga_data = pp;
4119 }
4120 }
4121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004122 // We advance by two because we add one for items referenced through
4123 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004124 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004125
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004126 /*
4127 * 1. Go through all accessible variables and mark all lists and dicts
4128 * with copyID.
4129 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004130
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004131 // Don't free variables in the previous_funccal list unless they are only
4132 // referenced through previous_funccal. This must be first, because if
4133 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004134 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004136 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004137 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004139 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004140 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004141 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4142 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004144 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004145 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004146 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4147 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004148 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004149 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4150 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004151#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004152 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004153 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4154 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004155 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004156 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004157 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4158 NULL, NULL);
4159#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004160
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004161 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004162 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004163 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4164 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004165 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004166 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004167
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004168 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004169 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004170
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004171 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004172 abort = abort || set_ref_in_functions(copyID);
4173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004174 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004175 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004176
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004177 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004178 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004179
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004180 // callbacks in buffers
4181 abort = abort || set_ref_in_buffers(copyID);
4182
Bram Moolenaar1dced572012-04-05 16:54:08 +02004183#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004184 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004185#endif
4186
Bram Moolenaardb913952012-06-29 12:54:53 +02004187#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004188 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004189#endif
4190
4191#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004192 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004193#endif
4194
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004195#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004196 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004197 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004198#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004199#ifdef FEAT_NETBEANS_INTG
4200 abort = abort || set_ref_in_nb_channel(copyID);
4201#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004202
Bram Moolenaare3188e22016-05-31 21:13:04 +02004203#ifdef FEAT_TIMERS
4204 abort = abort || set_ref_in_timer(copyID);
4205#endif
4206
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004207#ifdef FEAT_QUICKFIX
4208 abort = abort || set_ref_in_quickfix(copyID);
4209#endif
4210
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004211#ifdef FEAT_TERMINAL
4212 abort = abort || set_ref_in_term(copyID);
4213#endif
4214
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004215#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004216 abort = abort || set_ref_in_popups(copyID);
4217#endif
4218
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004219 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004220 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004221 /*
4222 * 2. Free lists and dictionaries that are not referenced.
4223 */
4224 did_free = free_unref_items(copyID);
4225
4226 /*
4227 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004228 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004229 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004230 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004231 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004232 else if (p_verbose > 0)
4233 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004234 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004235 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004236
4237 return did_free;
4238}
4239
4240/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004241 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004242 */
4243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004244free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004245{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004246 int did_free = FALSE;
4247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004248 // Let all "free" functions know that we are here. This means no
4249 // dictionaries, lists, channels or jobs are to be freed, because we will
4250 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004251 in_free_unref_items = TRUE;
4252
4253 /*
4254 * PASS 1: free the contents of the items. We don't free the items
4255 * themselves yet, so that it is possible to decrement refcount counters
4256 */
4257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004258 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004259 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004260
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004261 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004262 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004263
4264#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004265 // Go through the list of jobs and free items without the copyID. This
4266 // must happen before doing channels, because jobs refer to channels, but
4267 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004268 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4269
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004270 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004271 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4272#endif
4273
4274 /*
4275 * PASS 2: free the items themselves.
4276 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004277 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004278 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004279
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004280#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004281 // Go through the list of jobs and free items without the copyID. This
4282 // must happen before doing channels, because jobs refer to channels, but
4283 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004284 free_unused_jobs(copyID, COPYID_MASK);
4285
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004286 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004287 free_unused_channels(copyID, COPYID_MASK);
4288#endif
4289
4290 in_free_unref_items = FALSE;
4291
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004292 return did_free;
4293}
4294
4295/*
4296 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004297 * "list_stack" is used to add lists to be marked. Can be NULL.
4298 *
4299 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004300 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004302set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004303{
4304 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004305 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004306 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004307 hashtab_T *cur_ht;
4308 ht_stack_T *ht_stack = NULL;
4309 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004310
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004311 cur_ht = ht;
4312 for (;;)
4313 {
4314 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004315 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004316 // Mark each item in the hashtab. If the item contains a hashtab
4317 // it is added to ht_stack, if it contains a list it is added to
4318 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004319 todo = (int)cur_ht->ht_used;
4320 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4321 if (!HASHITEM_EMPTY(hi))
4322 {
4323 --todo;
4324 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4325 &ht_stack, list_stack);
4326 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004327 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004328
4329 if (ht_stack == NULL)
4330 break;
4331
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004332 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004333 cur_ht = ht_stack->ht;
4334 tempitem = ht_stack;
4335 ht_stack = ht_stack->prev;
4336 free(tempitem);
4337 }
4338
4339 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004340}
4341
4342/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004343 * Mark a dict and its items with "copyID".
4344 * Returns TRUE if setting references failed somehow.
4345 */
4346 int
4347set_ref_in_dict(dict_T *d, int copyID)
4348{
4349 if (d != NULL && d->dv_copyID != copyID)
4350 {
4351 d->dv_copyID = copyID;
4352 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4353 }
4354 return FALSE;
4355}
4356
4357/*
4358 * Mark a list and its items with "copyID".
4359 * Returns TRUE if setting references failed somehow.
4360 */
4361 int
4362set_ref_in_list(list_T *ll, int copyID)
4363{
4364 if (ll != NULL && ll->lv_copyID != copyID)
4365 {
4366 ll->lv_copyID = copyID;
4367 return set_ref_in_list_items(ll, copyID, NULL);
4368 }
4369 return FALSE;
4370}
4371
4372/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004373 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004374 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4375 *
4376 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004377 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004378 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004379set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004380{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004381 listitem_T *li;
4382 int abort = FALSE;
4383 list_T *cur_l;
4384 list_stack_T *list_stack = NULL;
4385 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004386
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004387 cur_l = l;
4388 for (;;)
4389 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004390 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004391 // Mark each item in the list. If the item contains a hashtab
4392 // it is added to ht_stack, if it contains a list it is added to
4393 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004394 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4395 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4396 ht_stack, &list_stack);
4397 if (list_stack == NULL)
4398 break;
4399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004400 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004401 cur_l = list_stack->list;
4402 tempitem = list_stack;
4403 list_stack = list_stack->prev;
4404 free(tempitem);
4405 }
4406
4407 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004408}
4409
4410/*
4411 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004412 * "list_stack" is used to add lists to be marked. Can be NULL.
4413 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4414 *
4415 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004416 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004418set_ref_in_item(
4419 typval_T *tv,
4420 int copyID,
4421 ht_stack_T **ht_stack,
4422 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004423{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004424 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004425
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004426 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004427 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004428 dict_T *dd = tv->vval.v_dict;
4429
Bram Moolenaara03f2332016-02-06 18:09:59 +01004430 if (dd != NULL && dd->dv_copyID != copyID)
4431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004432 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004433 dd->dv_copyID = copyID;
4434 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004435 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004436 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4437 }
4438 else
4439 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004440 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4441
Bram Moolenaara03f2332016-02-06 18:09:59 +01004442 if (newitem == NULL)
4443 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004444 else
4445 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004446 newitem->ht = &dd->dv_hashtab;
4447 newitem->prev = *ht_stack;
4448 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004449 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004450 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004451 }
4452 }
4453 else if (tv->v_type == VAR_LIST)
4454 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004455 list_T *ll = tv->vval.v_list;
4456
Bram Moolenaara03f2332016-02-06 18:09:59 +01004457 if (ll != NULL && ll->lv_copyID != copyID)
4458 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004459 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004460 ll->lv_copyID = copyID;
4461 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004462 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004463 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004464 }
4465 else
4466 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004467 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4468
Bram Moolenaara03f2332016-02-06 18:09:59 +01004469 if (newitem == NULL)
4470 abort = TRUE;
4471 else
4472 {
4473 newitem->list = ll;
4474 newitem->prev = *list_stack;
4475 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004476 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004477 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004478 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004479 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004480 else if (tv->v_type == VAR_FUNC)
4481 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004482 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004483 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004484 else if (tv->v_type == VAR_PARTIAL)
4485 {
4486 partial_T *pt = tv->vval.v_partial;
4487 int i;
4488
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004489 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004490 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004491 // Didn't see this partial yet.
4492 pt->pt_copyID = copyID;
4493
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004494 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004495
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004496 if (pt->pt_dict != NULL)
4497 {
4498 typval_T dtv;
4499
4500 dtv.v_type = VAR_DICT;
4501 dtv.vval.v_dict = pt->pt_dict;
4502 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4503 }
4504
4505 for (i = 0; i < pt->pt_argc; ++i)
4506 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4507 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004508 if (pt->pt_funcstack != NULL)
4509 {
4510 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4511
4512 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4513 abort = abort || set_ref_in_item(stack + i, copyID,
4514 ht_stack, list_stack);
4515 }
4516
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004517 }
4518 }
4519#ifdef FEAT_JOB_CHANNEL
4520 else if (tv->v_type == VAR_JOB)
4521 {
4522 job_T *job = tv->vval.v_job;
4523 typval_T dtv;
4524
4525 if (job != NULL && job->jv_copyID != copyID)
4526 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004527 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004528 if (job->jv_channel != NULL)
4529 {
4530 dtv.v_type = VAR_CHANNEL;
4531 dtv.vval.v_channel = job->jv_channel;
4532 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4533 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004534 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004535 {
4536 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004537 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004538 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4539 }
4540 }
4541 }
4542 else if (tv->v_type == VAR_CHANNEL)
4543 {
4544 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004545 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004546 typval_T dtv;
4547 jsonq_T *jq;
4548 cbq_T *cq;
4549
4550 if (ch != NULL && ch->ch_copyID != copyID)
4551 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004552 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004553 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004554 {
4555 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4556 jq = jq->jq_next)
4557 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4558 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4559 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004560 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004561 {
4562 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004563 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004564 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4565 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004566 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004567 {
4568 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004569 dtv.vval.v_partial =
4570 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004571 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4572 }
4573 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004574 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004575 {
4576 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004577 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004578 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4579 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004580 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004581 {
4582 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004583 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004584 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4585 }
4586 }
4587 }
4588#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004589 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004590}
4591
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 * Return a string with the string representation of a variable.
4594 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004595 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004596 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004597 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4598 * stings as "string()", otherwise does not put quotes around strings, as
4599 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004600 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4601 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004602 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004604 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004605echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004606 typval_T *tv,
4607 char_u **tofree,
4608 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004609 int copyID,
4610 int echo_style,
4611 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004612 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004613{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004614 static int recurse = 0;
4615 char_u *r = NULL;
4616
Bram Moolenaar33570922005-01-25 22:26:29 +00004617 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004618 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004619 if (!did_echo_string_emsg)
4620 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004621 // Only give this message once for a recursive call to avoid
4622 // flooding the user with errors. And stop iterating over lists
4623 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004624 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004625 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004627 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004628 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004629 }
4630 ++recurse;
4631
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 switch (tv->v_type)
4633 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004634 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004635 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004636 {
4637 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004638 r = tv->vval.v_string;
4639 if (r == NULL)
4640 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004641 }
4642 else
4643 {
4644 *tofree = string_quote(tv->vval.v_string, FALSE);
4645 r = *tofree;
4646 }
4647 break;
4648
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004650 if (echo_style)
4651 {
4652 *tofree = NULL;
4653 r = tv->vval.v_string;
4654 }
4655 else
4656 {
4657 *tofree = string_quote(tv->vval.v_string, TRUE);
4658 r = *tofree;
4659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004660 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004661
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004662 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004663 {
4664 partial_T *pt = tv->vval.v_partial;
4665 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004666 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004667 garray_T ga;
4668 int i;
4669 char_u *tf;
4670
4671 ga_init2(&ga, 1, 100);
4672 ga_concat(&ga, (char_u *)"function(");
4673 if (fname != NULL)
4674 {
4675 ga_concat(&ga, fname);
4676 vim_free(fname);
4677 }
4678 if (pt != NULL && pt->pt_argc > 0)
4679 {
4680 ga_concat(&ga, (char_u *)", [");
4681 for (i = 0; i < pt->pt_argc; ++i)
4682 {
4683 if (i > 0)
4684 ga_concat(&ga, (char_u *)", ");
4685 ga_concat(&ga,
4686 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4687 vim_free(tf);
4688 }
4689 ga_concat(&ga, (char_u *)"]");
4690 }
4691 if (pt != NULL && pt->pt_dict != NULL)
4692 {
4693 typval_T dtv;
4694
4695 ga_concat(&ga, (char_u *)", ");
4696 dtv.v_type = VAR_DICT;
4697 dtv.vval.v_dict = pt->pt_dict;
4698 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4699 vim_free(tf);
4700 }
4701 ga_concat(&ga, (char_u *)")");
4702
4703 *tofree = ga.ga_data;
4704 r = *tofree;
4705 break;
4706 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004707
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004708 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004709 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004710 break;
4711
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004713 if (tv->vval.v_list == NULL)
4714 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004715 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004716 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004717 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004718 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004719 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4720 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004721 {
4722 *tofree = NULL;
4723 r = (char_u *)"[...]";
4724 }
4725 else
4726 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004727 int old_copyID = tv->vval.v_list->lv_copyID;
4728
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004729 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004730 *tofree = list2string(tv, copyID, restore_copyID);
4731 if (restore_copyID)
4732 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004733 r = *tofree;
4734 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004735 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004736
Bram Moolenaar8c711452005-01-14 21:53:12 +00004737 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004738 if (tv->vval.v_dict == NULL)
4739 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004740 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004741 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004742 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004743 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004744 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4745 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004746 {
4747 *tofree = NULL;
4748 r = (char_u *)"{...}";
4749 }
4750 else
4751 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004752 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004753
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004754 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004755 *tofree = dict2string(tv, copyID, restore_copyID);
4756 if (restore_copyID)
4757 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004758 r = *tofree;
4759 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004760 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004763 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004764 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004766 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004767 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004768 break;
4769
Bram Moolenaar835dc632016-02-07 14:27:38 +01004770 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004771 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004772 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004773 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004774 if (composite_val)
4775 {
4776 *tofree = string_quote(r, FALSE);
4777 r = *tofree;
4778 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004780
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004782#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783 *tofree = NULL;
4784 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4785 r = numbuf;
4786 break;
4787#endif
4788
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004789 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004790 case VAR_SPECIAL:
4791 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004792 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004793 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004794 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004795
Bram Moolenaar8502c702014-06-17 12:51:16 +02004796 if (--recurse == 0)
4797 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004798 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004799}
4800
4801/*
4802 * Return a string with the string representation of a variable.
4803 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4804 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004805 * Does not put quotes around strings, as ":echo" displays values.
4806 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4807 * May return NULL.
4808 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004810echo_string(
4811 typval_T *tv,
4812 char_u **tofree,
4813 char_u *numbuf,
4814 int copyID)
4815{
4816 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4817}
4818
4819/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004820 * Return string "str" in ' quotes, doubling ' characters.
4821 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004823 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004824 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004825string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004826{
Bram Moolenaar33570922005-01-25 22:26:29 +00004827 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004828 char_u *p, *r, *s;
4829
Bram Moolenaar33570922005-01-25 22:26:29 +00004830 len = (function ? 13 : 3);
4831 if (str != NULL)
4832 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004833 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004834 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004835 if (*p == '\'')
4836 ++len;
4837 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004838 s = r = alloc(len);
4839 if (r != NULL)
4840 {
4841 if (function)
4842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004843 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004844 r += 10;
4845 }
4846 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004848 if (str != NULL)
4849 for (p = str; *p != NUL; )
4850 {
4851 if (*p == '\'')
4852 *r++ = '\'';
4853 MB_COPY_CHAR(p, r);
4854 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004856 if (function)
4857 *r++ = ')';
4858 *r++ = NUL;
4859 }
4860 return s;
4861}
4862
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004863#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004864/*
4865 * Convert the string "text" to a floating point number.
4866 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4867 * this always uses a decimal point.
4868 * Returns the length of the text that was consumed.
4869 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004870 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004871string2float(
4872 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004873 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874{
4875 char *s = (char *)text;
4876 float_T f;
4877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004878 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004879 if (STRNICMP(text, "inf", 3) == 0)
4880 {
4881 *value = INFINITY;
4882 return 3;
4883 }
4884 if (STRNICMP(text, "-inf", 3) == 0)
4885 {
4886 *value = -INFINITY;
4887 return 4;
4888 }
4889 if (STRNICMP(text, "nan", 3) == 0)
4890 {
4891 *value = NAN;
4892 return 3;
4893 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894 f = strtod(s, &s);
4895 *value = f;
4896 return (int)((char_u *)s - text);
4897}
4898#endif
4899
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004902 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004905var2fpos(
4906 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004907 int dollar_lnum, // TRUE when $ is last line
4908 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004910 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004912 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004914 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004915 if (varp->v_type == VAR_LIST)
4916 {
4917 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004918 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004919 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004920 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004921
4922 l = varp->vval.v_list;
4923 if (l == NULL)
4924 return NULL;
4925
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004926 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004927 pos.lnum = list_find_nr(l, 0L, &error);
4928 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004929 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004930 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004931
Bram Moolenaarec65d772020-08-20 22:29:12 +02004932 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004933 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004934 li = list_find(l, 1L);
4935 if (li != NULL && li->li_tv.v_type == VAR_STRING
4936 && li->li_tv.vval.v_string != NULL
4937 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02004938 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004939 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02004940 }
4941 else
4942 {
4943 pos.col = list_find_nr(l, 1L, &error);
4944 if (error)
4945 return NULL;
4946 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00004947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004948 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004949 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004951 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004953 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004954 pos.coladd = list_find_nr(l, 2L, &error);
4955 if (error)
4956 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004957
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004958 return &pos;
4959 }
4960
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004961 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004962 if (name == NULL)
4963 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004964 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004966 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004967 {
4968 if (VIsual_active)
4969 return &VIsual;
4970 return &curwin->w_cursor;
4971 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004972 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004974 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4976 return NULL;
4977 return pp;
4978 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004979
Bram Moolenaara5525202006-03-02 22:52:09 +00004980 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004981
Bram Moolenaar477933c2007-07-17 14:32:23 +00004982 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004983 {
4984 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004986 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004987 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004988 // In silent Ex mode topline is zero, but that's not a valid line
4989 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004990 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004991 return &pos;
4992 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004993 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004994 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004995 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004996 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004997 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004998 return &pos;
4999 }
5000 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005001 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005003 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 pos.lnum = curbuf->b_ml.ml_line_count;
5006 pos.col = 0;
5007 }
5008 else
5009 {
5010 pos.lnum = curwin->w_cursor.lnum;
5011 pos.col = (colnr_T)STRLEN(ml_get_curline());
5012 }
5013 return &pos;
5014 }
5015 return NULL;
5016}
5017
5018/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005019 * Convert list in "arg" into a position and optional file number.
5020 * When "fnump" is NULL there is no file number, only 3 items.
5021 * Note that the column is passed on as-is, the caller may want to decrement
5022 * it to use 1 for the first column.
5023 * Return FAIL when conversion is not possible, doesn't check the position for
5024 * validity.
5025 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005027list2fpos(
5028 typval_T *arg,
5029 pos_T *posp,
5030 int *fnump,
5031 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005032{
5033 list_T *l = arg->vval.v_list;
5034 long i = 0;
5035 long n;
5036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005037 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5038 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005039 if (arg->v_type != VAR_LIST
5040 || l == NULL
5041 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005042 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005043 return FAIL;
5044
5045 if (fnump != NULL)
5046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005047 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005048 if (n < 0)
5049 return FAIL;
5050 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005051 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005052 *fnump = n;
5053 }
5054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005055 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005056 if (n < 0)
5057 return FAIL;
5058 posp->lnum = n;
5059
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005060 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005061 if (n < 0)
5062 return FAIL;
5063 posp->col = n;
5064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005065 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005066 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005067 posp->coladd = 0;
5068 else
5069 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005070
Bram Moolenaar493c1782014-05-28 14:34:46 +02005071 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005072 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005073
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005074 return OK;
5075}
5076
5077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 * Get the length of an environment variable name.
5079 * Advance "arg" to the first character after the name.
5080 * Return 0 for error.
5081 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005082 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005083get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
5085 char_u *p;
5086 int len;
5087
5088 for (p = *arg; vim_isIDc(*p); ++p)
5089 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005090 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 return 0;
5092
5093 len = (int)(p - *arg);
5094 *arg = p;
5095 return len;
5096}
5097
5098/*
5099 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005100 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 * Return 0 if something is wrong.
5102 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
5106 char_u *p;
5107 int len;
5108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005109 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005111 {
5112 if (*p == ':')
5113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // "s:" is start of "s:var", but "n:" is not and can be used in
5115 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005116 len = (int)(p - *arg);
5117 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5118 || len > 1)
5119 break;
5120 }
5121 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005122 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 return 0;
5124
5125 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005126 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127
5128 return len;
5129}
5130
5131/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005132 * Get the length of the name of a variable or function.
5133 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005135 * Return -1 if curly braces expansion failed.
5136 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 * If the name contains 'magic' {}'s, expand them and return the
5138 * expanded name in an allocated string via 'alias' - caller must free.
5139 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005140 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005141get_name_len(
5142 char_u **arg,
5143 char_u **alias,
5144 int evaluate,
5145 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
5147 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 char_u *p;
5149 char_u *expr_start;
5150 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005152 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
5154 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5155 && (*arg)[2] == (int)KE_SNR)
5156 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005157 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 *arg += 3;
5159 return get_id_len(arg) + 3;
5160 }
5161 len = eval_fname_script(*arg);
5162 if (len > 0)
5163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005164 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 *arg += len;
5166 }
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005171 p = find_name_end(*arg, &expr_start, &expr_end,
5172 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (expr_start != NULL)
5174 {
5175 char_u *temp_string;
5176
5177 if (!evaluate)
5178 {
5179 len += (int)(p - *arg);
5180 *arg = skipwhite(p);
5181 return len;
5182 }
5183
5184 /*
5185 * Include any <SID> etc in the expanded string:
5186 * Thus the -len here.
5187 */
5188 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5189 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005190 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 *alias = temp_string;
5192 *arg = skipwhite(p);
5193 return (int)STRLEN(temp_string);
5194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005197 // Only give an error when there is something, otherwise it will be
5198 // reported at a higher level.
5199 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005200 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202 return len;
5203}
5204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205/*
5206 * Find the end of a variable or function name, taking care of magic braces.
5207 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5208 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005209 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005210 * Return a pointer to just after the name. Equal to "arg" if there is no
5211 * valid name.
5212 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005213 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005214find_name_end(
5215 char_u *arg,
5216 char_u **expr_start,
5217 char_u **expr_end,
5218 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005220 int mb_nest = 0;
5221 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005223 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005224 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005226 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005228 *expr_start = NULL;
5229 *expr_end = NULL;
5230 }
5231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005233 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5234 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005235 return arg;
5236
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237 for (p = arg; *p != NUL
5238 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005239 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005240 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005241 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005242 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005243 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005244 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005245 if (*p == '\'')
5246 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005247 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005248 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005249 ;
5250 if (*p == NUL)
5251 break;
5252 }
5253 else if (*p == '"')
5254 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005255 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005256 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005257 if (*p == '\\' && p[1] != NUL)
5258 ++p;
5259 if (*p == NUL)
5260 break;
5261 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005262 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005264 // "s:" is start of "s:var", but "n:" is not and can be used in
5265 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005266 len = (int)(p - arg);
5267 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005268 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005269 break;
5270 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005271
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005272 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 if (*p == '[')
5275 ++br_nest;
5276 else if (*p == ']')
5277 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005279
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005280 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005282 if (*p == '{')
5283 {
5284 mb_nest++;
5285 if (expr_start != NULL && *expr_start == NULL)
5286 *expr_start = p;
5287 }
5288 else if (*p == '}')
5289 {
5290 mb_nest--;
5291 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5292 *expr_end = p;
5293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296
5297 return p;
5298}
5299
5300/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005301 * Expands out the 'magic' {}'s in a variable/function name.
5302 * Note that this can call itself recursively, to deal with
5303 * constructs like foo{bar}{baz}{bam}
5304 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5305 * "in_start" ^
5306 * "expr_start" ^
5307 * "expr_end" ^
5308 * "in_end" ^
5309 *
5310 * Returns a new allocated string, which the caller must free.
5311 * Returns NULL for failure.
5312 */
5313 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005314make_expanded_name(
5315 char_u *in_start,
5316 char_u *expr_start,
5317 char_u *expr_end,
5318 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005319{
5320 char_u c1;
5321 char_u *retval = NULL;
5322 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005323
5324 if (expr_end == NULL || in_end == NULL)
5325 return NULL;
5326 *expr_start = NUL;
5327 *expr_end = NUL;
5328 c1 = *in_end;
5329 *in_end = NUL;
5330
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005331 temp_result = eval_to_string(expr_start + 1, FALSE);
5332 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005333 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005334 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5335 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005336 if (retval != NULL)
5337 {
5338 STRCPY(retval, in_start);
5339 STRCAT(retval, temp_result);
5340 STRCAT(retval, expr_end + 1);
5341 }
5342 }
5343 vim_free(temp_result);
5344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005345 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005346 *expr_start = '{';
5347 *expr_end = '}';
5348
5349 if (retval != NULL)
5350 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005351 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005352 if (expr_start != NULL)
5353 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005354 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355 temp_result = make_expanded_name(retval, expr_start,
5356 expr_end, temp_result);
5357 vim_free(retval);
5358 retval = temp_result;
5359 }
5360 }
5361
5362 return retval;
5363}
5364
5365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005367 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005370eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005372 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005373}
5374
5375/*
5376 * Return TRUE if character "c" can be used as the first character in a
5377 * variable or function name (excluding '{' and '}').
5378 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005380eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005381{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005382 return ASCII_ISALPHA(c) || c == '_';
5383}
5384
5385/*
5386 * Return TRUE if character "c" can be used as the first character of a
5387 * dictionary key.
5388 */
5389 int
5390eval_isdictc(int c)
5391{
5392 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393}
5394
5395/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005396 * Return the character "str[index]" where "index" is the character index. If
5397 * "index" is out of range NULL is returned.
5398 */
5399 char_u *
5400char_from_string(char_u *str, varnumber_T index)
5401{
5402 size_t nbyte = 0;
5403 varnumber_T nchar = index;
5404 size_t slen;
5405
5406 if (str == NULL || index < 0)
5407 return NULL;
5408 slen = STRLEN(str);
5409 while (nchar > 0 && nbyte < slen)
5410 {
5411 nbyte += MB_CPTR2LEN(str + nbyte);
5412 --nchar;
5413 }
5414 if (nbyte >= slen)
5415 return NULL;
5416 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5417}
5418
5419/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005420 * Get the byte index for character index "idx" in string "str" with length
5421 * "str_len".
5422 * If going over the end return "str_len".
5423 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005424 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005425 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005426 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005427char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5428{
5429 varnumber_T nchar = idx;
5430 size_t nbyte = 0;
5431
5432 if (nchar >= 0)
5433 {
5434 while (nchar > 0 && nbyte < str_len)
5435 {
5436 nbyte += MB_CPTR2LEN(str + nbyte);
5437 --nchar;
5438 }
5439 }
5440 else
5441 {
5442 nbyte = str_len;
5443 while (nchar < 0 && nbyte > 0)
5444 {
5445 --nbyte;
5446 nbyte -= mb_head_off(str, str + nbyte);
5447 ++nchar;
5448 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005449 if (nchar < 0)
5450 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005451 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005452 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005453}
5454
5455/*
5456 * Return the slice "str[first:last]" using character indexes.
5457 * Return NULL when the result is empty.
5458 */
5459 char_u *
5460string_slice(char_u *str, varnumber_T first, varnumber_T last)
5461{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005462 long start_byte, end_byte;
5463 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005464
5465 if (str == NULL)
5466 return NULL;
5467 slen = STRLEN(str);
5468 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005469 if (start_byte < 0)
5470 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005471 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005472 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005473 else
5474 {
5475 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005476 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005477 // end index is inclusive
5478 end_byte += MB_CPTR2LEN(str + end_byte);
5479 }
5480
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005481 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005482 return NULL;
5483 return vim_strnsave(str + start_byte, end_byte - start_byte);
5484}
5485
5486/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005487 * Handle:
5488 * - expr[expr], expr[expr:expr] subscript
5489 * - ".name" lookup
5490 * - function call with Funcref variable: func(expr)
5491 * - method call: var->method()
5492 *
5493 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005494 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005496handle_subscript(
5497 char_u **arg,
5498 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005499 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005500 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005501{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005502 int evaluate = evalarg != NULL
5503 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005504 int ret = OK;
5505 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005506 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005507 int getnext;
5508 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005509
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005510 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005511 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005512 // When at the end of the line and ".name" or "->{" or "->X" follows in
5513 // the next line then consume the line break.
5514 p = eval_next_non_blank(*arg, evalarg, &getnext);
5515 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005516 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005517 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005518 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005519 {
5520 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005521 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005522 check_white = FALSE;
5523 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005524
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005525 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5526 || rettv->v_type == VAR_PARTIAL))
5527 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005528 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005529 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5530 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005531
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005532 // Stop the expression evaluation when immediately aborting on
5533 // error, or when an interrupt occurred or an exception was thrown
5534 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005535 if (aborting())
5536 {
5537 if (ret == OK)
5538 clear_tv(rettv);
5539 ret = FAIL;
5540 }
5541 dict_unref(selfdict);
5542 selfdict = NULL;
5543 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005544 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005545 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005546 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005547 if (ret == OK)
5548 {
5549 if ((*arg)[2] == '{')
5550 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005551 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005552 else
5553 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005554 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005555 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005556 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005557 // "." is ".name" lookup when we found a dict or when evaluating and
5558 // scriptversion is at least 2, where string concatenation is "..".
5559 else if (**arg == '['
5560 || (**arg == '.' && (rettv->v_type == VAR_DICT
5561 || (!evaluate
5562 && (*arg)[1] != '.'
5563 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005564 {
5565 dict_unref(selfdict);
5566 if (rettv->v_type == VAR_DICT)
5567 {
5568 selfdict = rettv->vval.v_dict;
5569 if (selfdict != NULL)
5570 ++selfdict->dv_refcount;
5571 }
5572 else
5573 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005574 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005575 {
5576 clear_tv(rettv);
5577 ret = FAIL;
5578 }
5579 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005580 else
5581 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005582 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005583
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005584 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5585 // Don't do this when "Func" is already a partial that was bound
5586 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005587 if (selfdict != NULL
5588 && (rettv->v_type == VAR_FUNC
5589 || (rettv->v_type == VAR_PARTIAL
5590 && (rettv->vval.v_partial->pt_auto
5591 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005592 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005593
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005594 dict_unref(selfdict);
5595 return ret;
5596}
5597
5598/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005599 * Make a copy of an item.
5600 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005601 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5602 * reference to an already copied list/dict can be used.
5603 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005604 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005606item_copy(
5607 typval_T *from,
5608 typval_T *to,
5609 int deep,
5610 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005611{
5612 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005613 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005614
Bram Moolenaar33570922005-01-25 22:26:29 +00005615 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005617 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005618 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005619 }
5620 ++recurse;
5621
5622 switch (from->v_type)
5623 {
5624 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005625 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005626 case VAR_STRING:
5627 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005628 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005629 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005630 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005631 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005632 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005633 copy_tv(from, to);
5634 break;
5635 case VAR_LIST:
5636 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005637 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005638 if (from->vval.v_list == NULL)
5639 to->vval.v_list = NULL;
5640 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5641 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005642 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005643 to->vval.v_list = from->vval.v_list->lv_copylist;
5644 ++to->vval.v_list->lv_refcount;
5645 }
5646 else
5647 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5648 if (to->vval.v_list == NULL)
5649 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005650 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005651 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005653 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005654 case VAR_DICT:
5655 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005656 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005657 if (from->vval.v_dict == NULL)
5658 to->vval.v_dict = NULL;
5659 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5660 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005661 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005662 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5663 ++to->vval.v_dict->dv_refcount;
5664 }
5665 else
5666 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5667 if (to->vval.v_dict == NULL)
5668 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005669 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005670 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005671 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005673 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005674 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005675 }
5676 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005677 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005678}
5679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005680 void
5681echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5682{
5683 char_u *tofree;
5684 char_u numbuf[NUMBUFLEN];
5685 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5686
5687 if (*atstart)
5688 {
5689 *atstart = FALSE;
5690 // Call msg_start() after eval1(), evaluating the expression
5691 // may cause a message to appear.
5692 if (with_space)
5693 {
5694 // Mark the saved text as finishing the line, so that what
5695 // follows is displayed on a new line when scrolling back
5696 // at the more prompt.
5697 msg_sb_eol();
5698 msg_start();
5699 }
5700 }
5701 else if (with_space)
5702 msg_puts_attr(" ", echo_attr);
5703
5704 if (p != NULL)
5705 for ( ; *p != NUL && !got_int; ++p)
5706 {
5707 if (*p == '\n' || *p == '\r' || *p == TAB)
5708 {
5709 if (*p != TAB && *needclr)
5710 {
5711 // remove any text still there from the command
5712 msg_clr_eos();
5713 *needclr = FALSE;
5714 }
5715 msg_putchar_attr(*p, echo_attr);
5716 }
5717 else
5718 {
5719 if (has_mbyte)
5720 {
5721 int i = (*mb_ptr2len)(p);
5722
5723 (void)msg_outtrans_len_attr(p, i, echo_attr);
5724 p += i - 1;
5725 }
5726 else
5727 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5728 }
5729 }
5730 vim_free(tofree);
5731}
5732
Bram Moolenaare9a41262005-01-15 22:18:47 +00005733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 * ":echo expr1 ..." print each argument separated with a space, add a
5735 * newline at the end.
5736 * ":echon expr1 ..." print each argument plain.
5737 */
5738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005739ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005742 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 char_u *p;
5744 int needclr = TRUE;
5745 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005746 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005747 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005748 evalarg_T evalarg;
5749
Bram Moolenaare707c882020-07-01 13:07:37 +02005750 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
5752 if (eap->skip)
5753 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005754 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005756 // If eval1() causes an error message the text from the command may
5757 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005758 need_clr_eos = needclr;
5759
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005761 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
5763 /*
5764 * Report the invalid expression unless the expression evaluation
5765 * has been cancelled due to an aborting error, an interrupt, or an
5766 * exception.
5767 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005768 if (!aborting() && did_emsg == did_emsg_before
5769 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005770 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005771 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 break;
5773 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005774 need_clr_eos = FALSE;
5775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005779 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 arg = skipwhite(arg);
5781 }
5782 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005783 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 if (eap->skip)
5786 --emsg_skip;
5787 else
5788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005789 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 if (needclr)
5791 msg_clr_eos();
5792 if (eap->cmdidx == CMD_echo)
5793 msg_end();
5794 }
5795}
5796
5797/*
5798 * ":echohl {name}".
5799 */
5800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005801ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005803 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804}
5805
5806/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005807 * Returns the :echo attribute
5808 */
5809 int
5810get_echo_attr(void)
5811{
5812 return echo_attr;
5813}
5814
5815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 * ":execute expr1 ..." execute the result of an expression.
5817 * ":echomsg expr1 ..." Print a message
5818 * ":echoerr expr1 ..." Print an error
5819 * Each gets spaces around each argument and a newline at the end for
5820 * echo commands
5821 */
5822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005823ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005826 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 int ret = OK;
5828 char_u *p;
5829 garray_T ga;
5830 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
5832 ga_init2(&ga, 1, 80);
5833
5834 if (eap->skip)
5835 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005836 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005838 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005839 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842 if (!eap->skip)
5843 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005844 char_u buf[NUMBUFLEN];
5845
5846 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005847 {
5848 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5849 {
5850 emsg(_(e_inval_string));
5851 p = NULL;
5852 }
5853 else
5854 p = tv_get_string_buf(&rettv, buf);
5855 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005856 else
5857 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005858 if (p == NULL)
5859 {
5860 clear_tv(&rettv);
5861 ret = FAIL;
5862 break;
5863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 len = (int)STRLEN(p);
5865 if (ga_grow(&ga, len + 2) == FAIL)
5866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005867 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 ret = FAIL;
5869 break;
5870 }
5871 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 ga.ga_len += len;
5875 }
5876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005877 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 arg = skipwhite(arg);
5879 }
5880
5881 if (ret != FAIL && ga.ga_data != NULL)
5882 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005883 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005885 // Mark the already saved text as finishing the line, so that what
5886 // follows is displayed on a new line when scrolling back at the
5887 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005888 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005889 }
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005892 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005893 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005894 out_flush();
5895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 else if (eap->cmdidx == CMD_echoerr)
5897 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005898 int save_did_emsg = did_emsg;
5899
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005900 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005901 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (!force_abort)
5903 did_emsg = save_did_emsg;
5904 }
5905 else if (eap->cmdidx == CMD_execute)
5906 do_cmdline((char_u *)ga.ga_data,
5907 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5908 }
5909
5910 ga_clear(&ga);
5911
5912 if (eap->skip)
5913 --emsg_skip;
5914
5915 eap->nextcmd = check_nextcmd(arg);
5916}
5917
5918/*
5919 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5920 * "arg" points to the "&" or '+' when called, to "option" when returning.
5921 * Returns NULL when no option name found. Otherwise pointer to the char
5922 * after the option name.
5923 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005924 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005925find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 char_u *p = *arg;
5928
5929 ++p;
5930 if (*p == 'g' && p[1] == ':')
5931 {
5932 *opt_flags = OPT_GLOBAL;
5933 p += 2;
5934 }
5935 else if (*p == 'l' && p[1] == ':')
5936 {
5937 *opt_flags = OPT_LOCAL;
5938 p += 2;
5939 }
5940 else
5941 *opt_flags = 0;
5942
5943 if (!ASCII_ISALPHA(*p))
5944 return NULL;
5945 *arg = p;
5946
5947 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005948 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 else
5950 while (ASCII_ISALPHA(*p))
5951 ++p;
5952 return p;
5953}
5954
5955/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005956 * Display script name where an item was last set.
5957 * Should only be invoked when 'verbose' is non-zero.
5958 */
5959 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005960last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005961{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005962 char_u *p;
5963
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005964 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005965 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005966 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005967 if (p != NULL)
5968 {
5969 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005970 msg_puts(_("\n\tLast set from "));
5971 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005972 if (script_ctx.sc_lnum > 0)
5973 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005974 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005975 msg_outnum((long)script_ctx.sc_lnum);
5976 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005977 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005978 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005979 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005980 }
5981}
5982
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005983#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984
5985/*
5986 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005987 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 * "flags" can be "g" to do a global substitute.
5989 * Returns an allocated string, NULL for error.
5990 */
5991 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005992do_string_sub(
5993 char_u *str,
5994 char_u *pat,
5995 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005996 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005997 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
5999 int sublen;
6000 regmatch_T regmatch;
6001 int i;
6002 int do_all;
6003 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006004 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 garray_T ga;
6006 char_u *ret;
6007 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006008 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006010 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006012 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013
6014 ga_init2(&ga, 1, 200);
6015
6016 do_all = (flags[0] == 'g');
6017
6018 regmatch.rm_ic = p_ic;
6019 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6020 if (regmatch.regprog != NULL)
6021 {
6022 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006023 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6025 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006026 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006027 if (regmatch.startp[0] == regmatch.endp[0])
6028 {
6029 if (zero_width == regmatch.startp[0])
6030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006031 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006032 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006033 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6034 (size_t)i);
6035 ga.ga_len += i;
6036 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006037 continue;
6038 }
6039 zero_width = regmatch.startp[0];
6040 }
6041
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 /*
6043 * Get some space for a temporary buffer to do the substitution
6044 * into. It will contain:
6045 * - The text up to where the match is.
6046 * - The substituted text.
6047 * - The text after the match.
6048 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006049 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006050 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6052 {
6053 ga_clear(&ga);
6054 break;
6055 }
6056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006057 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 i = (int)(regmatch.startp[0] - tail);
6059 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006060 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006061 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 + ga.ga_len + i, TRUE, TRUE, FALSE);
6063 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006064 tail = regmatch.endp[0];
6065 if (*tail == NUL)
6066 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (!do_all)
6068 break;
6069 }
6070
6071 if (ga.ga_data != NULL)
6072 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6073
Bram Moolenaar473de612013-06-08 18:19:48 +02006074 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 }
6076
6077 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6078 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006079 if (p_cpo == empty_option)
6080 p_cpo = save_cpo;
6081 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006082 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006083 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
6085 return ret;
6086}