blob: 9b9b7f47caa7cc2d97d12cdfa2244de206df71f5 [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())
194 retval = tv2bool(&tv);
195 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)
875 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100876 && lp->ll_tv->vval.v_dict != NULL)
877 && !(lp->ll_tv->v_type == VAR_BLOB
878 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000879 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100881 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000883 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100887 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000889 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000890
Bram Moolenaar8c711452005-01-14 21:53:12 +0000891 len = -1;
892 if (*p == '.')
893 {
894 key = p + 1;
895 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
896 ;
897 if (len == 0)
898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100900 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 }
903 p = key + len;
904 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000905 else
906 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100907 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000908 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 if (*p == ':')
910 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000911 else
912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000913 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200914 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100916 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000917 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100918 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000919 clear_tv(&var1);
920 return NULL;
921 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200922 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 }
924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100925 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000926 if (*p == ':')
927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000930 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200931 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100932 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000934 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100935 if (rettv != NULL
936 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100937 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100938 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100939 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100942 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100943 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000945 }
946 p = skipwhite(p + 1);
947 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000949 else
950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200952 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200953 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000954 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100955 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000957 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100958 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100960 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100961 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000962 clear_tv(&var2);
963 return NULL;
964 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000967 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000970
Bram Moolenaar8c711452005-01-14 21:53:12 +0000971 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000972 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100974 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100975 clear_tv(&var1);
976 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 }
979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100980 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 ++p;
982 }
983
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000985 {
986 if (len == -1)
987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100988 // "[key]": get key from "var1"
989 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200990 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 }
995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000996 lp->ll_list = NULL;
997 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000998 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001000 // When assigning to a scope dictionary check that a function and
1001 // variable name is valid (only variable name unless it is l: or
1002 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001003 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001004 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001005 int prevval;
1006 int wrong;
1007
1008 if (len != -1)
1009 {
1010 prevval = key[len];
1011 key[len] = NUL;
1012 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001013 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001015 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1016 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001017 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001018 || !valid_varname(key);
1019 if (len != -1)
1020 key[len] = prevval;
1021 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001022 {
1023 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001024 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001025 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001026 }
1027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001030 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001031 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001032 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001034 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001035 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001036 return NULL;
1037 }
1038
Bram Moolenaar31b81602019-02-10 22:14:27 +01001039 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001041 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001043 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001044 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 }
1047 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001051 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001053 p = NULL;
1054 break;
1055 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001056 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001057 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001058 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1059 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001060 {
1061 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001062 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001063 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001064
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001065 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001067 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001068 else if (lp->ll_tv->v_type == VAR_BLOB)
1069 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001070 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1071
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001072 /*
1073 * Get the number and item for the only or first index of the List.
1074 */
1075 if (empty1)
1076 lp->ll_n1 = 0;
1077 else
1078 // is number or string
1079 lp->ll_n1 = (long)tv_get_number(&var1);
1080 clear_tv(&var1);
1081
1082 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001083 || lp->ll_n1 > bloblen
1084 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001085 {
1086 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001087 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001088 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001089 return NULL;
1090 }
1091 if (lp->ll_range && !lp->ll_empty2)
1092 {
1093 lp->ll_n2 = (long)tv_get_number(&var2);
1094 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001095 if (lp->ll_n2 < 0
1096 || lp->ll_n2 >= bloblen
1097 || lp->ll_n2 < lp->ll_n1)
1098 {
1099 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001100 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001101 return NULL;
1102 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001103 }
1104 lp->ll_blob = lp->ll_tv->vval.v_blob;
1105 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001106 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001108 else
1109 {
1110 /*
1111 * Get the number and item for the only or first index of the List.
1112 */
1113 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001114 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001115 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001116 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001117 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001118 clear_tv(&var1);
1119
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001120 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001121 lp->ll_list = lp->ll_tv->vval.v_list;
1122 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1123 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001124 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001125 if (lp->ll_n1 < 0)
1126 {
1127 lp->ll_n1 = 0;
1128 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1129 }
1130 }
1131 if (lp->ll_li == NULL)
1132 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001133 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001134 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001135 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 }
1138
1139 /*
1140 * May need to find the item or absolute index for the second
1141 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 * When no index given: "lp->ll_empty2" is TRUE.
1143 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001144 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001147 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001148 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001151 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001153 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001154 {
1155 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001156 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001157 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001158 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 }
1161
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001162 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001163 if (lp->ll_n1 < 0)
1164 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1165 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001166 {
1167 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001168 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001170 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001171 }
1172
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001173 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001174 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175 }
1176
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001177 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179 return p;
1180}
1181
1182/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001183 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001186clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001187{
1188 vim_free(lp->ll_exp_name);
1189 vim_free(lp->ll_newkey);
1190}
1191
1192/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001193 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001195 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1196 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001199set_var_lval(
1200 lval_T *lp,
1201 char_u *endp,
1202 typval_T *rettv,
1203 int copy,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001204 int flags, // LET_IS_CONST, LET_FORCEIT, LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001205 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206{
1207 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001208 listitem_T *ri;
1209 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210
1211 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001213 cc = *endp;
1214 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001215 if (lp->ll_blob != NULL)
1216 {
1217 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001218
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001219 if (op != NULL && *op != '=')
1220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001222 return;
1223 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001224 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001225 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001226
1227 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1228 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001229 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001230
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001231 if (lp->ll_empty2)
1232 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1233
1234 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001236 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001237 return;
1238 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001239 if (lp->ll_empty2)
1240 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001241
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001242 ir = 0;
1243 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1244 blob_set(lp->ll_blob, il,
1245 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001246 }
1247 else
1248 {
1249 val = (int)tv_get_number_chk(rettv, &error);
1250 if (!error)
1251 {
1252 garray_T *gap = &lp->ll_blob->bv_ga;
1253
1254 // Allow for appending a byte. Setting a byte beyond
1255 // the end is an error otherwise.
1256 if (lp->ll_n1 < gap->ga_len
1257 || (lp->ll_n1 == gap->ga_len
1258 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1259 {
1260 blob_set(lp->ll_blob, lp->ll_n1, val);
1261 if (lp->ll_n1 == gap->ga_len)
1262 ++gap->ga_len;
1263 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001264 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001265 }
1266 }
1267 }
1268 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001270 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001271
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001273 {
1274 emsg(_(e_cannot_mod));
1275 *endp = cc;
1276 return;
1277 }
1278
Bram Moolenaarff697e62019-02-12 22:28:33 +01001279 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001280 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001281 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001282 &tv, &di, TRUE, FALSE) == OK)
1283 {
1284 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001285 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1286 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001287 && tv_op(&tv, rettv, op) == OK)
1288 set_var(lp->ll_name, &tv, FALSE);
1289 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001291 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001292 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001293 {
1294 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001295 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001296 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001298 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001299 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001301 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001302 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001303 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001304 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 else if (lp->ll_range)
1306 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001307 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001308 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001311 {
1312 emsg(_("E996: Cannot lock a range"));
1313 return;
1314 }
1315
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001316 /*
1317 * Check whether any of the list items is locked
1318 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001319 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001320 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001321 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001322 return;
1323 ri = ri->li_next;
1324 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1325 break;
1326 ll_li = ll_li->li_next;
1327 ++ll_n1;
1328 }
1329
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 /*
1331 * Assign the List values to the list items.
1332 */
1333 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001334 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335 if (op != NULL && *op != '=')
1336 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1337 else
1338 {
1339 clear_tv(&lp->ll_li->li_tv);
1340 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1341 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 ri = ri->li_next;
1343 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1344 break;
1345 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001346 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001347 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001348 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001349 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001350 ri = NULL;
1351 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001352 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001353 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001354 lp->ll_li = lp->ll_li->li_next;
1355 ++lp->ll_n1;
1356 }
1357 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001358 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 else if (lp->ll_empty2
1360 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001361 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001362 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 }
1364 else
1365 {
1366 /*
1367 * Assign to a List or Dictionary item.
1368 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001370 {
1371 emsg(_("E996: Cannot lock a list or dict"));
1372 return;
1373 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374 if (lp->ll_newkey != NULL)
1375 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001376 if (op != NULL && *op != '=')
1377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001378 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001379 return;
1380 }
1381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001382 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001383 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001384 if (di == NULL)
1385 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001386 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1387 {
1388 vim_free(di);
1389 return;
1390 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001392 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001393 else if (op != NULL && *op != '=')
1394 {
1395 tv_op(lp->ll_tv, rettv, op);
1396 return;
1397 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001398 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001399 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001400
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001401 /*
1402 * Assign the value to the variable or list item.
1403 */
1404 if (copy)
1405 copy_tv(rettv, lp->ll_tv);
1406 else
1407 {
1408 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001409 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 }
1412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413}
1414
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001416 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1417 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001418 * Returns OK or FAIL.
1419 */
1420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001421tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001422{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001423 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001424 char_u numbuf[NUMBUFLEN];
1425 char_u *s;
1426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001427 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001428 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001429 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001430 {
1431 switch (tv1->v_type)
1432 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001433 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001434 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001436 case VAR_DICT:
1437 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001438 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001439 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001440 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001441 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001442 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001443 break;
1444
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001445 case VAR_BLOB:
1446 if (*op != '+' || tv2->v_type != VAR_BLOB)
1447 break;
1448 // BLOB += BLOB
1449 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1450 {
1451 blob_T *b1 = tv1->vval.v_blob;
1452 blob_T *b2 = tv2->vval.v_blob;
1453 int i, len = blob_len(b2);
1454 for (i = 0; i < len; i++)
1455 ga_append(&b1->bv_ga, blob_get(b2, i));
1456 }
1457 return OK;
1458
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 case VAR_LIST:
1460 if (*op != '+' || tv2->v_type != VAR_LIST)
1461 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001462 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001463 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1464 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1465 return OK;
1466
1467 case VAR_NUMBER:
1468 case VAR_STRING:
1469 if (tv2->v_type == VAR_LIST)
1470 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001471 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001472 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001473 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001474 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001475#ifdef FEAT_FLOAT
1476 if (tv2->v_type == VAR_FLOAT)
1477 {
1478 float_T f = n;
1479
Bram Moolenaarff697e62019-02-12 22:28:33 +01001480 if (*op == '%')
1481 break;
1482 switch (*op)
1483 {
1484 case '+': f += tv2->vval.v_float; break;
1485 case '-': f -= tv2->vval.v_float; break;
1486 case '*': f *= tv2->vval.v_float; break;
1487 case '/': f /= tv2->vval.v_float; break;
1488 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001489 clear_tv(tv1);
1490 tv1->v_type = VAR_FLOAT;
1491 tv1->vval.v_float = f;
1492 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001493 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001494#endif
1495 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001496 switch (*op)
1497 {
1498 case '+': n += tv_get_number(tv2); break;
1499 case '-': n -= tv_get_number(tv2); break;
1500 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001501 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1502 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001503 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001504 clear_tv(tv1);
1505 tv1->v_type = VAR_NUMBER;
1506 tv1->vval.v_number = n;
1507 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 }
1509 else
1510 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001511 if (tv2->v_type == VAR_FLOAT)
1512 break;
1513
Bram Moolenaarff697e62019-02-12 22:28:33 +01001514 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001515 s = tv_get_string(tv1);
1516 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001517 clear_tv(tv1);
1518 tv1->v_type = VAR_STRING;
1519 tv1->vval.v_string = s;
1520 }
1521 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001522
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001523 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001524#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001525 {
1526 float_T f;
1527
Bram Moolenaarff697e62019-02-12 22:28:33 +01001528 if (*op == '%' || *op == '.'
1529 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001530 && tv2->v_type != VAR_NUMBER
1531 && tv2->v_type != VAR_STRING))
1532 break;
1533 if (tv2->v_type == VAR_FLOAT)
1534 f = tv2->vval.v_float;
1535 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001536 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001537 switch (*op)
1538 {
1539 case '+': tv1->vval.v_float += f; break;
1540 case '-': tv1->vval.v_float -= f; break;
1541 case '*': tv1->vval.v_float *= f; break;
1542 case '/': tv1->vval.v_float /= f; break;
1543 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001544 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001545#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001546 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 }
1548 }
1549
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001550 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001551 return FAIL;
1552}
1553
1554/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 * Evaluate the expression used in a ":for var in expr" command.
1556 * "arg" points to "var".
1557 * Set "*errp" to TRUE for an error, FALSE otherwise;
1558 * Return a pointer that holds the info. Null when there is an error.
1559 */
1560 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561eval_for_line(
1562 char_u *arg,
1563 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001564 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001565 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001566{
Bram Moolenaar33570922005-01-25 22:26:29 +00001567 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001568 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T tv;
1570 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001571 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001572
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001573 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001574
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001575 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001576 if (fi == NULL)
1577 return NULL;
1578
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001579 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001580 if (expr == NULL)
1581 return fi;
1582
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001583 expr = skipwhite_and_linebreak(expr, evalarg);
1584 if (expr[0] != 'i' || expr[1] != 'n'
1585 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001586 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001588 return fi;
1589 }
1590
1591 if (skip)
1592 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001593 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1594 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001595 {
1596 *errp = FALSE;
1597 if (!skip)
1598 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001599 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001600 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001601 l = tv.vval.v_list;
1602 if (l == NULL)
1603 {
1604 // a null list is like an empty list: do nothing
1605 clear_tv(&tv);
1606 }
1607 else
1608 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001610 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001612 // No need to increment the refcount, it's already set for
1613 // the list being used in "tv".
1614 fi->fi_list = l;
1615 list_add_watch(l, &fi->fi_lw);
1616 fi->fi_lw.lw_item = l->lv_first;
1617 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001618 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001619 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001620 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001621 fi->fi_bi = 0;
1622 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001623 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001624 typval_T btv;
1625
1626 // Make a copy, so that the iteration still works when the
1627 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001629 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001630 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001631 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001632 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633 else
1634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001635 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001636 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001637 }
1638 }
1639 }
1640 if (skip)
1641 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001642 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643
1644 return fi;
1645}
1646
1647/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001648 * Used when looping over a :for line, skip the "in expr" part.
1649 */
1650 void
1651skip_for_lines(void *fi_void, evalarg_T *evalarg)
1652{
1653 forinfo_T *fi = (forinfo_T *)fi_void;
1654 int i;
1655
1656 for (i = 0; i < fi->fi_break_count; ++i)
1657 eval_next_line(evalarg);
1658}
1659
1660/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661 * Use the first item in a ":for" list. Advance to the next.
1662 * Assign the values to the variable (list). "arg" points to the first one.
1663 * Return TRUE when a valid item was found, FALSE when at end of list or
1664 * something wrong.
1665 */
1666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001667next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001669 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001671 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001672 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001674 if (fi->fi_blob != NULL)
1675 {
1676 typval_T tv;
1677
1678 if (fi->fi_bi >= blob_len(fi->fi_blob))
1679 return FALSE;
1680 tv.v_type = VAR_NUMBER;
1681 tv.v_lock = VAR_FIXED;
1682 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1683 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001684 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001685 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001686 }
1687
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 item = fi->fi_lw.lw_item;
1689 if (item == NULL)
1690 result = FALSE;
1691 else
1692 {
1693 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001694 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001695 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 }
1697 return result;
1698}
1699
1700/*
1701 * Free the structure used to store info used by ":for".
1702 */
1703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001704free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705{
Bram Moolenaar33570922005-01-25 22:26:29 +00001706 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001708 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001709 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001711 list_unref(fi->fi_list);
1712 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001713 if (fi != NULL && fi->fi_blob != NULL)
1714 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 vim_free(fi);
1716}
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001719set_context_for_expression(
1720 expand_T *xp,
1721 char_u *arg,
1722 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 int got_eq = FALSE;
1725 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001728 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 {
1730 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001731 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001733 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001734 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735 {
1736 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001737 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001738 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739 break;
1740 }
1741 return;
1742 }
1743 }
1744 else
1745 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1746 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 while ((xp->xp_pattern = vim_strpbrk(arg,
1748 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1749 {
1750 c = *xp->xp_pattern;
1751 if (c == '&')
1752 {
1753 c = xp->xp_pattern[1];
1754 if (c == '&')
1755 {
1756 ++xp->xp_pattern;
1757 xp->xp_context = cmdidx != CMD_let || got_eq
1758 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1759 }
1760 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001763 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1764 xp->xp_pattern += 2;
1765
1766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
1768 else if (c == '$')
1769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001770 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 xp->xp_context = EXPAND_ENV_VARS;
1772 }
1773 else if (c == '=')
1774 {
1775 got_eq = TRUE;
1776 xp->xp_context = EXPAND_EXPRESSION;
1777 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001778 else if (c == '#'
1779 && xp->xp_context == EXPAND_EXPRESSION)
1780 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001781 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001782 break;
1783 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001784 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 && xp->xp_context == EXPAND_FUNCTIONS
1786 && vim_strchr(xp->xp_pattern, '(') == NULL)
1787 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001788 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 break;
1790 }
1791 else if (cmdidx != CMD_let || got_eq)
1792 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001793 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 {
1795 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1796 if (c == '\\' && xp->xp_pattern[1] != NUL)
1797 ++xp->xp_pattern;
1798 xp->xp_context = EXPAND_NOTHING;
1799 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001800 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001802 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1804 /* skip */ ;
1805 xp->xp_context = EXPAND_NOTHING;
1806 }
1807 else if (c == '|')
1808 {
1809 if (xp->xp_pattern[1] == '|')
1810 {
1811 ++xp->xp_pattern;
1812 xp->xp_context = EXPAND_EXPRESSION;
1813 }
1814 else
1815 xp->xp_context = EXPAND_COMMANDS;
1816 }
1817 else
1818 xp->xp_context = EXPAND_EXPRESSION;
1819 }
1820 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001821 // Doesn't look like something valid, expand as an expression
1822 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 arg = xp->xp_pattern;
1825 if (*arg != NUL)
1826 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1827 /* skip */ ;
1828 }
1829 xp->xp_pattern = arg;
1830}
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001833 * Return TRUE if "pat" matches "text".
1834 * Does not use 'cpo' and always uses 'magic'.
1835 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001836 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001837pattern_match(char_u *pat, char_u *text, int ic)
1838{
1839 int matches = FALSE;
1840 char_u *save_cpo;
1841 regmatch_T regmatch;
1842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001843 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001844 save_cpo = p_cpo;
1845 p_cpo = (char_u *)"";
1846 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1847 if (regmatch.regprog != NULL)
1848 {
1849 regmatch.rm_ic = ic;
1850 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1851 vim_regfree(regmatch.regprog);
1852 }
1853 p_cpo = save_cpo;
1854 return matches;
1855}
1856
1857/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001858 * Handle a name followed by "(". Both for just "name(arg)" and for
1859 * "expr->name(arg)".
1860 * Returns OK or FAIL.
1861 */
1862 static int
1863eval_func(
1864 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001865 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001866 char_u *name,
1867 int name_len,
1868 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001869 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001870 typval_T *basetv) // "expr" for "expr->name(arg)"
1871{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001872 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001873 char_u *s = name;
1874 int len = name_len;
1875 partial_T *partial;
1876 int ret = OK;
1877
1878 if (!evaluate)
1879 check_vars(s, len);
1880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001881 // If "s" is the name of a variable of type VAR_FUNC
1882 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001883 s = deref_func_name(s, &len, &partial, !evaluate);
1884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001885 // Need to make a copy, in case evaluating the arguments makes
1886 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001887 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001888 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001889 ret = FAIL;
1890 else
1891 {
1892 funcexe_T funcexe;
1893
1894 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001895 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001896 funcexe.firstline = curwin->w_cursor.lnum;
1897 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001898 funcexe.evaluate = evaluate;
1899 funcexe.partial = partial;
1900 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001901 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001902 }
1903 vim_free(s);
1904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001905 // If evaluate is FALSE rettv->v_type was not set in
1906 // get_func_tv, but it's needed in handle_subscript() to parse
1907 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001908 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1909 {
1910 rettv->vval.v_string = NULL;
1911 rettv->v_type = VAR_FUNC;
1912 }
1913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001914 // Stop the expression evaluation when immediately
1915 // aborting on error, or when an interrupt occurred or
1916 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001917 if (evaluate && aborting())
1918 {
1919 if (ret == OK)
1920 clear_tv(rettv);
1921 ret = FAIL;
1922 }
1923 return ret;
1924}
1925
1926/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001927 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1928 * comment) and there is a next line, return the next line (skipping blanks)
1929 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001930 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1931 * "arg" must point somewhere inside a line, not at the start.
1932 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001933 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001934eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1935{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001936 char_u *p = skipwhite(arg);
1937
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001938 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001939 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001940 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001941 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001942 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001943 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001944 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001945
1946 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001947 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001948 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001949 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001950
Bram Moolenaar9d489562020-07-30 20:08:50 +02001951 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001952 {
1953 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001954 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001955 }
1956 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001957 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001958}
1959
1960/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001961 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001962 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001963 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001964eval_next_line(evalarg_T *evalarg)
1965{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001966 garray_T *gap = &evalarg->eval_ga;
1967 char_u *line;
1968
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001969 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02001970 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
1971 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001972 else
1973 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001974 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001975 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1976 {
1977 // Going to concatenate the lines after parsing.
1978 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1979 ++gap->ga_len;
1980 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001981 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001982 {
1983 vim_free(evalarg->eval_tofree);
1984 evalarg->eval_tofree = line;
1985 }
1986 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001987}
1988
Bram Moolenaare6b53242020-07-01 17:28:33 +02001989/*
1990 * Call eval_next_non_blank() and get the next line if needed.
1991 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001992 char_u *
1993skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1994{
1995 int getnext;
1996 char_u *p = skipwhite(arg);
1997
Bram Moolenaar962d7212020-07-04 14:15:00 +02001998 if (evalarg == NULL)
1999 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002000 eval_next_non_blank(p, evalarg, &getnext);
2001 if (getnext)
2002 return eval_next_line(evalarg);
2003 return p;
2004}
2005
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002006/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002007 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002008 */
2009 void
2010clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2011{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002012 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002013 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002014 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002015 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002016 if (eap != NULL)
2017 {
2018 // We may need to keep the original command line, e.g. for
2019 // ":let" it has the variable names. But we may also need the
2020 // new one, "nextcmd" points into it. Keep both.
2021 vim_free(eap->cmdline_tofree);
2022 eap->cmdline_tofree = *eap->cmdlinep;
2023 *eap->cmdlinep = evalarg->eval_tofree;
2024 }
2025 else
2026 vim_free(evalarg->eval_tofree);
2027 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002028 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002029
2030 vim_free(evalarg->eval_tofree_lambda);
2031 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002032 }
2033}
2034
2035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2039 */
2040
2041/*
2042 * Handle zero level expression.
2043 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002045 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002046 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 * Return OK or FAIL.
2048 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002050eval0(
2051 char_u *arg,
2052 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002053 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002054 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
2056 int ret;
2057 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002058 int did_emsg_before = did_emsg;
2059 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002060 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002063 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002064 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002065
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002066 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
2068 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 /*
2071 * Report the invalid expression unless the expression evaluation has
2072 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002073 * exception, or we already gave a more specific error.
2074 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002076 if (!aborting()
2077 && did_emsg == did_emsg_before
2078 && called_emsg == called_emsg_before
2079 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 ret = FAIL;
2082 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002083
2084 if (eap != NULL)
2085 eap->nextcmd = check_nextcmd(p);
2086
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 return ret;
2088}
2089
2090/*
2091 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002092 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 *
2094 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002095 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002097 * Note: "rettv.v_lock" is not set.
2098 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 * Return OK or FAIL.
2100 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002101 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002102eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002104 char_u *p;
2105 int getnext;
2106
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002107 CLEAR_POINTER(rettv);
2108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 /*
2110 * Get the first variable.
2111 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 return FAIL;
2114
Bram Moolenaar793648f2020-06-26 21:28:25 +02002115 p = eval_next_non_blank(*arg, evalarg, &getnext);
2116 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002118 int result;
2119 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002120 evalarg_T *evalarg_used = evalarg;
2121 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002122 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002123 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124
2125 if (evalarg == NULL)
2126 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002127 CLEAR_FIELD(local_evalarg);
2128 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002129 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002130 orig_flags = evalarg_used->eval_flags;
2131 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002132
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002133 if (getnext)
2134 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002135 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002136 {
2137 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2138 {
2139 error_white_both(p, 1);
2140 clear_tv(rettv);
2141 return FAIL;
2142 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002143 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002144 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002147 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002149 int error = FALSE;
2150
Bram Moolenaar56acb092020-08-16 14:48:19 +02002151 if (in_vim9script())
2152 result = tv2bool(rettv);
2153 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002156 if (error)
2157 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159
2160 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002161 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002163 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2164 {
2165 error_white_both(p, 1);
2166 clear_tv(rettv);
2167 return FAIL;
2168 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002169 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2170 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002171 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002172 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002173 {
2174 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 /*
2179 * Check for the ":".
2180 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002181 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002182 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002187 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 return FAIL;
2189 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002190 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002191 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002192 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002193 {
2194 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2195 {
2196 error_white_both(p, 1);
2197 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002198 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002199 return FAIL;
2200 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002201 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002205 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002207 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2208 {
2209 error_white_both(p, 1);
2210 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002211 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002212 return FAIL;
2213 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002214 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2215 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002216 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002217 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002221 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 return FAIL;
2223 }
2224 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002226
2227 if (evalarg == NULL)
2228 clear_evalarg(&local_evalarg, NULL);
2229 else
2230 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232
2233 return OK;
2234}
2235
2236/*
2237 * Handle first level expression:
2238 * expr2 || expr2 || expr2 logical OR
2239 *
2240 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002241 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 *
2243 * Return OK or FAIL.
2244 */
2245 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002246eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002248 char_u *p;
2249 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 /*
2252 * Get the first variable.
2253 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002254 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 return FAIL;
2256
2257 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002258 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002260 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002261 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002263 evalarg_T *evalarg_used = evalarg;
2264 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002265 int evaluate;
2266 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002267 long result = FALSE;
2268 typval_T var2;
2269 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002270 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002271
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002272 if (evalarg == NULL)
2273 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002274 CLEAR_FIELD(local_evalarg);
2275 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002276 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002277 orig_flags = evalarg_used->eval_flags;
2278 evaluate = orig_flags & EVAL_EVALUATE;
2279 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002281 if (vim9script)
2282 {
2283 result = tv2bool(rettv);
2284 }
2285 else
2286 {
2287 error = FALSE;
2288 if (tv_get_number_chk(rettv, &error) != 0)
2289 result = TRUE;
2290 clear_tv(rettv);
2291 if (error)
2292 return FAIL;
2293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295
2296 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002297 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002299 while (p[0] == '|' && p[1] == '|')
2300 {
2301 if (getnext)
2302 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002303 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002304 {
2305 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2306 {
2307 error_white_both(p, 2);
2308 clear_tv(rettv);
2309 return FAIL;
2310 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002311 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002312 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002313
2314 /*
2315 * Get the second variable.
2316 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002317 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2318 {
2319 error_white_both(p, 2);
2320 clear_tv(rettv);
2321 return FAIL;
2322 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002323 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2324 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002326 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002327 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002328
2329 /*
2330 * Compute the result.
2331 */
2332 if (evaluate && !result)
2333 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002334 if (vim9script)
2335 {
2336 clear_tv(rettv);
2337 *rettv = var2;
2338 result = tv2bool(rettv);
2339 }
2340 else
2341 {
2342 if (tv_get_number_chk(&var2, &error) != 0)
2343 result = TRUE;
2344 clear_tv(&var2);
2345 if (error)
2346 return FAIL;
2347 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002348 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002349 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002350 {
2351 rettv->v_type = VAR_NUMBER;
2352 rettv->vval.v_number = result;
2353 }
2354
2355 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002357
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002358 if (evalarg == NULL)
2359 clear_evalarg(&local_evalarg, NULL);
2360 else
2361 evalarg->eval_flags = orig_flags;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002362
2363 // Resulting value can be assigned to a bool.
2364 rettv->v_lock |= VAR_BOOL_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366
2367 return OK;
2368}
2369
2370/*
2371 * Handle second level expression:
2372 * expr3 && expr3 && expr3 logical AND
2373 *
2374 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002375 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 *
2377 * Return OK or FAIL.
2378 */
2379 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002380eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002382 char_u *p;
2383 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 /*
2386 * Get the first variable.
2387 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002388 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 return FAIL;
2390
2391 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002392 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002394 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002395 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002397 evalarg_T *evalarg_used = evalarg;
2398 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002399 int orig_flags;
2400 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002401 long result = TRUE;
2402 typval_T var2;
2403 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002404 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002405
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002406 if (evalarg == NULL)
2407 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002408 CLEAR_FIELD(local_evalarg);
2409 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002410 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002411 orig_flags = evalarg_used->eval_flags;
2412 evaluate = orig_flags & EVAL_EVALUATE;
2413 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002414 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002415 if (vim9script)
2416 {
2417 result = tv2bool(rettv);
2418 }
2419 else
2420 {
2421 error = FALSE;
2422 if (tv_get_number_chk(rettv, &error) == 0)
2423 result = FALSE;
2424 clear_tv(rettv);
2425 if (error)
2426 return FAIL;
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429
2430 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002431 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 while (p[0] == '&' && p[1] == '&')
2434 {
2435 if (getnext)
2436 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002437 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002438 {
2439 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2440 {
2441 error_white_both(p, 2);
2442 clear_tv(rettv);
2443 return FAIL;
2444 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002445 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002446 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002447
2448 /*
2449 * Get the second variable.
2450 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002451 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2452 {
2453 error_white_both(p, 2);
2454 clear_tv(rettv);
2455 return FAIL;
2456 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2458 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002459 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002460 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002461 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002462 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002463
2464 /*
2465 * Compute the result.
2466 */
2467 if (evaluate && result)
2468 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002469 if (vim9script)
2470 {
2471 clear_tv(rettv);
2472 *rettv = var2;
2473 result = tv2bool(rettv);
2474 }
2475 else
2476 {
2477 if (tv_get_number_chk(&var2, &error) == 0)
2478 result = FALSE;
2479 clear_tv(&var2);
2480 if (error)
2481 return FAIL;
2482 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002483 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002484 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002485 {
2486 rettv->v_type = VAR_NUMBER;
2487 rettv->vval.v_number = result;
2488 }
2489
2490 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002492
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002493 if (evalarg == NULL)
2494 clear_evalarg(&local_evalarg, NULL);
2495 else
2496 evalarg->eval_flags = orig_flags;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002497
2498 // Resulting value can be assigned to a bool.
2499 rettv->v_lock |= VAR_BOOL_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 }
2501
2502 return OK;
2503}
2504
2505/*
2506 * Handle third level expression:
2507 * var1 == var2
2508 * var1 =~ var2
2509 * var1 != var2
2510 * var1 !~ var2
2511 * var1 > var2
2512 * var1 >= var2
2513 * var1 < var2
2514 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002515 * var1 is var2
2516 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 *
2518 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002519 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 *
2521 * Return OK or FAIL.
2522 */
2523 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002524eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002527 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002528 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002530 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532 /*
2533 * Get the first variable.
2534 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002535 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 return FAIL;
2537
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002538 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002539 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
2541 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002542 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002544 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002546 typval_T var2;
2547 int ic;
2548 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002549 int evaluate = evalarg == NULL
2550 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002551
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002552 if (getnext)
2553 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002554 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2555 {
2556 error_white_both(p, len);
2557 clear_tv(rettv);
2558 return FAIL;
2559 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002560
Bram Moolenaar696ba232020-07-29 21:20:41 +02002561 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2562 {
2563 semsg(_(e_invexpr2), p);
2564 clear_tv(rettv);
2565 return FAIL;
2566 }
2567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002568 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (p[len] == '?')
2570 {
2571 ic = TRUE;
2572 ++len;
2573 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002574 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 else if (p[len] == '#')
2576 {
2577 ic = FALSE;
2578 ++len;
2579 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002580 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002582 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584 /*
2585 * Get the second variable.
2586 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002587 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2588 {
2589 error_white_both(p, 1);
2590 clear_tv(rettv);
2591 return FAIL;
2592 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002593 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002594 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 return FAIL;
2598 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002599 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002600 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002601 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002602
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002603 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002604 {
2605 ret = FAIL;
2606 clear_tv(rettv);
2607 }
2608 else
2609 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002610 clear_tv(&var2);
2611 return ret;
2612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614
2615 return OK;
2616}
2617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 void
2619eval_addblob(typval_T *tv1, typval_T *tv2)
2620{
2621 blob_T *b1 = tv1->vval.v_blob;
2622 blob_T *b2 = tv2->vval.v_blob;
2623 blob_T *b = blob_alloc();
2624 int i;
2625
2626 if (b != NULL)
2627 {
2628 for (i = 0; i < blob_len(b1); i++)
2629 ga_append(&b->bv_ga, blob_get(b1, i));
2630 for (i = 0; i < blob_len(b2); i++)
2631 ga_append(&b->bv_ga, blob_get(b2, i));
2632
2633 clear_tv(tv1);
2634 rettv_blob_set(tv1, b);
2635 }
2636}
2637
2638 int
2639eval_addlist(typval_T *tv1, typval_T *tv2)
2640{
2641 typval_T var3;
2642
2643 // concatenate Lists
2644 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2645 {
2646 clear_tv(tv1);
2647 clear_tv(tv2);
2648 return FAIL;
2649 }
2650 clear_tv(tv1);
2651 *tv1 = var3;
2652 return OK;
2653}
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655/*
2656 * Handle fourth level expression:
2657 * + number addition
2658 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002659 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002660 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 *
2662 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002663 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 *
2665 * Return OK or FAIL.
2666 */
2667 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002668eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 /*
2671 * Get the first variable.
2672 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 return FAIL;
2675
2676 /*
2677 * Repeat computing, until no '+', '-' or '.' is following.
2678 */
2679 for (;;)
2680 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002681 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002682 int getnext;
2683 char_u *p;
2684 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002685 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002686 int concat;
2687 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002688 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002689
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002690 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002691 p = eval_next_non_blank(*arg, evalarg, &getnext);
2692 op = *p;
2693 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002694 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002696
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002697 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002698 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002699 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002700 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002701 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002702 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002703 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002704 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002705 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002706 clear_tv(rettv);
2707 return FAIL;
2708 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002709 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002710 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002711 if ((op != '+' || (rettv->v_type != VAR_LIST
2712 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002713#ifdef FEAT_FLOAT
2714 && (op == '.' || rettv->v_type != VAR_FLOAT)
2715#endif
2716 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002718 // For "list + ...", an illegal use of the first operand as
2719 // a number cannot be determined before evaluating the 2nd
2720 // operand: if this is also a list, all is ok.
2721 // For "something . ...", "something - ..." or "non-list + ...",
2722 // we know that the first operand needs to be a string or number
2723 // without evaluating the 2nd operand. So check before to avoid
2724 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002725 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 {
2727 clear_tv(rettv);
2728 return FAIL;
2729 }
2730 }
2731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 /*
2733 * Get the second variable.
2734 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002735 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002736 {
2737 error_white_both(p, oplen);
2738 clear_tv(rettv);
2739 return FAIL;
2740 }
2741 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002742 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 return FAIL;
2746 }
2747
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002748 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
2750 /*
2751 * Compute the result.
2752 */
2753 if (op == '.')
2754 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002755 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2756 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002757 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002758
Bram Moolenaar2e086612020-08-25 22:37:48 +02002759 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002760 || var2.v_type == VAR_CHANNEL
2761 || var2.v_type == VAR_JOB))
2762 emsg(_(e_inval_string));
2763#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002764 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002765 {
2766 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2767 var2.vval.v_float);
2768 s2 = buf2;
2769 }
2770#endif
2771 else
2772 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002773 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002774 {
2775 clear_tv(rettv);
2776 clear_tv(&var2);
2777 return FAIL;
2778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002780 clear_tv(rettv);
2781 rettv->v_type = VAR_STRING;
2782 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002784 else if (op == '+' && rettv->v_type == VAR_BLOB
2785 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002787 else if (op == '+' && rettv->v_type == VAR_LIST
2788 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002789 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002791 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 else
2794 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002795 int error = FALSE;
2796 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002797#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002798 float_T f1 = 0, f2 = 0;
2799
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002800 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002801 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002802 f1 = rettv->vval.v_float;
2803 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002804 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002805 else
2806#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002808 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002809 if (error)
2810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002811 // This can only happen for "list + non-list". For
2812 // "non-list + ..." or "something - ...", we returned
2813 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002814 clear_tv(rettv);
2815 return FAIL;
2816 }
2817#ifdef FEAT_FLOAT
2818 if (var2.v_type == VAR_FLOAT)
2819 f1 = n1;
2820#endif
2821 }
2822#ifdef FEAT_FLOAT
2823 if (var2.v_type == VAR_FLOAT)
2824 {
2825 f2 = var2.vval.v_float;
2826 n2 = 0;
2827 }
2828 else
2829#endif
2830 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002831 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002832 if (error)
2833 {
2834 clear_tv(rettv);
2835 clear_tv(&var2);
2836 return FAIL;
2837 }
2838#ifdef FEAT_FLOAT
2839 if (rettv->v_type == VAR_FLOAT)
2840 f2 = n2;
2841#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002843 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002844
2845#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002846 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002847 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2848 {
2849 if (op == '+')
2850 f1 = f1 + f2;
2851 else
2852 f1 = f1 - f2;
2853 rettv->v_type = VAR_FLOAT;
2854 rettv->vval.v_float = f1;
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002857#endif
2858 {
2859 if (op == '+')
2860 n1 = n1 + n2;
2861 else
2862 n1 = n1 - n2;
2863 rettv->v_type = VAR_NUMBER;
2864 rettv->vval.v_number = n1;
2865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002867 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 }
2870 return OK;
2871}
2872
2873/*
2874 * Handle fifth level expression:
2875 * * number multiplication
2876 * / number division
2877 * % number modulo
2878 *
2879 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002880 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 *
2882 * Return OK or FAIL.
2883 */
2884 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002885eval6(
2886 char_u **arg,
2887 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002888 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002889 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002891#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002892 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894
2895 /*
2896 * Get the first variable.
2897 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002898 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 return FAIL;
2900
2901 /*
2902 * Repeat computing, until no '*', '/' or '%' is following.
2903 */
2904 for (;;)
2905 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002906 int evaluate;
2907 int getnext;
2908 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002909 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002910 int op;
2911 varnumber_T n1, n2;
2912#ifdef FEAT_FLOAT
2913 float_T f1, f2;
2914#endif
2915 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002916
Bram Moolenaar9d489562020-07-30 20:08:50 +02002917 p = eval_next_non_blank(*arg, evalarg, &getnext);
2918 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002919 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002921
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002922 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002923 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002924 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002925 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002926 {
2927 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2928 {
2929 error_white_both(p, 1);
2930 clear_tv(rettv);
2931 return FAIL;
2932 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002933 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002936#ifdef FEAT_FLOAT
2937 f1 = 0;
2938 f2 = 0;
2939#endif
2940 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002941 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002943#ifdef FEAT_FLOAT
2944 if (rettv->v_type == VAR_FLOAT)
2945 {
2946 f1 = rettv->vval.v_float;
2947 use_float = TRUE;
2948 n1 = 0;
2949 }
2950 else
2951#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002952 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002954 if (error)
2955 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 }
2957 else
2958 n1 = 0;
2959
2960 /*
2961 * Get the second variable.
2962 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002963 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2964 {
2965 error_white_both(p, 1);
2966 clear_tv(rettv);
2967 return FAIL;
2968 }
2969 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002970 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 return FAIL;
2972
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002973 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002975#ifdef FEAT_FLOAT
2976 if (var2.v_type == VAR_FLOAT)
2977 {
2978 if (!use_float)
2979 {
2980 f1 = n1;
2981 use_float = TRUE;
2982 }
2983 f2 = var2.vval.v_float;
2984 n2 = 0;
2985 }
2986 else
2987#endif
2988 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002989 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002990 clear_tv(&var2);
2991 if (error)
2992 return FAIL;
2993#ifdef FEAT_FLOAT
2994 if (use_float)
2995 f2 = n2;
2996#endif
2997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
2999 /*
3000 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003001 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003#ifdef FEAT_FLOAT
3004 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 if (op == '*')
3007 f1 = f1 * f2;
3008 else if (op == '/')
3009 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003010# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003011 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003012 if (f2 == 0.0)
3013 {
3014 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003015 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003016 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003017 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003018 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003019 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003020 }
3021 else
3022 f1 = f1 / f2;
3023# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003024 // We rely on the floating point library to handle divide
3025 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003026 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003027# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003030 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003032 return FAIL;
3033 }
3034 rettv->v_type = VAR_FLOAT;
3035 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 }
3037 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003040 if (op == '*')
3041 n1 = n1 * n2;
3042 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003043 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003045 n1 = num_modulus(n1, n2);
3046
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047 rettv->v_type = VAR_NUMBER;
3048 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051 }
3052
3053 return OK;
3054}
3055
3056/*
3057 * Handle sixth level expression:
3058 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003059 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003060 * "string" string constant
3061 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 * &option-name option value
3063 * @r register contents
3064 * identifier variable value
3065 * function() function call
3066 * $VAR environment variable
3067 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003068 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003070 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003071 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 *
3073 * Also handle:
3074 * ! in front logical NOT
3075 * - in front unary minus
3076 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003077 * trailing [] subscript in String or List
3078 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003079 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 *
3081 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003082 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 *
3084 * Return OK or FAIL.
3085 */
3086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087eval7(
3088 char_u **arg,
3089 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003090 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003093 int evaluate = evalarg != NULL
3094 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 int len;
3096 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 char_u *start_leader, *end_leader;
3098 int ret = OK;
3099 char_u *alias;
3100
3101 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003102 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003103 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003105 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
3107 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003108 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 */
3110 start_leader = *arg;
3111 while (**arg == '!' || **arg == '-' || **arg == '+')
3112 *arg = skipwhite(*arg + 1);
3113 end_leader = *arg;
3114
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003115 if (**arg == '.' && (!isdigit(*(*arg + 1))
3116#ifdef FEAT_FLOAT
3117 || current_sctx.sc_version < 2
3118#endif
3119 ))
3120 {
3121 semsg(_(e_invexpr2), *arg);
3122 ++*arg;
3123 return FAIL;
3124 }
3125
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 switch (**arg)
3127 {
3128 /*
3129 * Number constant.
3130 */
3131 case '0':
3132 case '1':
3133 case '2':
3134 case '3':
3135 case '4':
3136 case '5':
3137 case '6':
3138 case '7':
3139 case '8':
3140 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003141 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003142
3143 // Apply prefixed "-" and "+" now. Matters especially when
3144 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003145 if (ret == OK && evaluate && end_leader > start_leader
3146 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003147 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 break;
3149
3150 /*
3151 * String constant: "string".
3152 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003153 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 break;
3155
3156 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003157 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003159 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003160 break;
3161
3162 /*
3163 * List: [expr, expr]
3164 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003165 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 break;
3167
3168 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003169 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003170 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003171 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003172 {
3173 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003174 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003175 }
3176 else
3177 ret = NOTDONE;
3178 break;
3179
3180 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003181 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003182 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003183 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003184 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003185 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003186 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003187 break;
3188
3189 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003190 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003192 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 break;
3194
3195 /*
3196 * Environment variable: $VAR.
3197 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003198 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 break;
3200
3201 /*
3202 * Register contents: @r.
3203 */
3204 case '@': ++*arg;
3205 if (evaluate)
3206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003207 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003208 rettv->vval.v_string = get_reg_contents(**arg,
3209 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211 if (**arg != NUL)
3212 ++*arg;
3213 break;
3214
3215 /*
3216 * nested expression: (expression).
3217 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003218 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003219 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003221
Bram Moolenaar9215f012020-06-27 21:18:00 +02003222 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003223 if (**arg == ')')
3224 ++*arg;
3225 else if (ret == OK)
3226 {
3227 emsg(_(e_missing_close));
3228 clear_tv(rettv);
3229 ret = FAIL;
3230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232 break;
3233
Bram Moolenaar8c711452005-01-14 21:53:12 +00003234 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 break;
3236 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003237
3238 if (ret == NOTDONE)
3239 {
3240 /*
3241 * Must be a variable or function name.
3242 * Can also be a curly-braces kind of name: {expr}.
3243 */
3244 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003245 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003246 if (alias != NULL)
3247 s = alias;
3248
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003249 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003250 ret = FAIL;
3251 else
3252 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003253 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3254
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003255 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3256 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003257 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003258 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003259 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003260 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003261 else if (flags & EVAL_CONSTANT)
3262 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003264 {
3265 // get the value of "true", "false" or a variable
3266 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3267 {
3268 rettv->v_type = VAR_BOOL;
3269 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003270 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003271 }
3272 else if (len == 5 && in_vim9script()
3273 && STRNCMP(s, "false", 4) == 0)
3274 {
3275 rettv->v_type = VAR_BOOL;
3276 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003277 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003278 }
3279 else
3280 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3281 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003282 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003283 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003284 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003285 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003286 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003288 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003289 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003290 }
3291
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003292 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3293 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003294 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003295 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
3297 /*
3298 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3299 */
3300 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003301 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003302 return ret;
3303}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003304
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003305/*
3306 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003307 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003308 * Adjusts "end_leaderp" until it is at "start_leader".
3309 */
3310 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003311eval7_leader(
3312 typval_T *rettv,
3313 int numeric_only,
3314 char_u *start_leader,
3315 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003316{
3317 char_u *end_leader = *end_leaderp;
3318 int ret = OK;
3319 int error = FALSE;
3320 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003321 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003322#ifdef FEAT_FLOAT
3323 float_T f = 0.0;
3324
3325 if (rettv->v_type == VAR_FLOAT)
3326 f = rettv->vval.v_float;
3327 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003328#endif
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003329 if (in_vim9script() && end_leader[-1] == '!')
3330 val = tv2bool(rettv);
3331 else
3332 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003333 if (error)
3334 {
3335 clear_tv(rettv);
3336 ret = FAIL;
3337 }
3338 else
3339 {
3340 while (end_leader > start_leader)
3341 {
3342 --end_leader;
3343 if (*end_leader == '!')
3344 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003345 if (numeric_only)
3346 {
3347 ++end_leader;
3348 break;
3349 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003350#ifdef FEAT_FLOAT
3351 if (rettv->v_type == VAR_FLOAT)
3352 f = !f;
3353 else
3354#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003355 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003356 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003357 type = VAR_BOOL;
3358 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003359 }
3360 else if (*end_leader == '-')
3361 {
3362#ifdef FEAT_FLOAT
3363 if (rettv->v_type == VAR_FLOAT)
3364 f = -f;
3365 else
3366#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003367 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003368 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003369 type = VAR_NUMBER;
3370 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003371 }
3372 }
3373#ifdef FEAT_FLOAT
3374 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003376 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003377 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003379 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003380#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003381 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003382 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003383 if (in_vim9script())
3384 rettv->v_type = type;
3385 else
3386 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003387 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003390 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 return ret;
3392}
3393
3394/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003395 * Call the function referred to in "rettv".
3396 */
3397 static int
3398call_func_rettv(
3399 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003400 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003401 typval_T *rettv,
3402 int evaluate,
3403 dict_T *selfdict,
3404 typval_T *basetv)
3405{
3406 partial_T *pt = NULL;
3407 funcexe_T funcexe;
3408 typval_T functv;
3409 char_u *s;
3410 int ret;
3411
3412 // need to copy the funcref so that we can clear rettv
3413 if (evaluate)
3414 {
3415 functv = *rettv;
3416 rettv->v_type = VAR_UNKNOWN;
3417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003418 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003419 if (functv.v_type == VAR_PARTIAL)
3420 {
3421 pt = functv.vval.v_partial;
3422 s = partial_name(pt);
3423 }
3424 else
3425 s = functv.vval.v_string;
3426 }
3427 else
3428 s = (char_u *)"";
3429
Bram Moolenaara80faa82020-04-12 19:37:17 +02003430 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003431 funcexe.firstline = curwin->w_cursor.lnum;
3432 funcexe.lastline = curwin->w_cursor.lnum;
3433 funcexe.evaluate = evaluate;
3434 funcexe.partial = pt;
3435 funcexe.selfdict = selfdict;
3436 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003437 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003439 // Clear the funcref afterwards, so that deleting it while
3440 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003441 if (evaluate)
3442 clear_tv(&functv);
3443
3444 return ret;
3445}
3446
3447/*
3448 * Evaluate "->method()".
3449 * "*arg" points to the '-'.
3450 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3451 */
3452 static int
3453eval_lambda(
3454 char_u **arg,
3455 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003456 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003457 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003458{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003459 int evaluate = evalarg != NULL
3460 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003461 typval_T base = *rettv;
3462 int ret;
3463
3464 // Skip over the ->.
3465 *arg += 2;
3466 rettv->v_type = VAR_UNKNOWN;
3467
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003468 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003469 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003470 return FAIL;
3471 else if (**arg != '(')
3472 {
3473 if (verbose)
3474 {
3475 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003476 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003477 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003479 }
3480 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003481 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003482 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003483 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003484 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003485
3486 // Clear the funcref afterwards, so that deleting it while
3487 // evaluating the arguments is possible (see test55).
3488 if (evaluate)
3489 clear_tv(&base);
3490
3491 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003492}
3493
3494/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003495 * Evaluate "->method()".
3496 * "*arg" points to the '-'.
3497 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3498 */
3499 static int
3500eval_method(
3501 char_u **arg,
3502 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003503 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003504 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003505{
3506 char_u *name;
3507 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003508 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003509 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003510 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003511 int evaluate = evalarg != NULL
3512 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003513
3514 // Skip over the ->.
3515 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003516 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003517
Bram Moolenaarac92e252019-08-03 21:58:38 +02003518 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003519 len = get_name_len(arg, &alias, evaluate, TRUE);
3520 if (alias != NULL)
3521 name = alias;
3522
3523 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003524 {
3525 if (verbose)
3526 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003527 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003528 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003529 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003530 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003531 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003532 if (**arg != '(')
3533 {
3534 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003536 ret = FAIL;
3537 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003538 else if (VIM_ISWHITE((*arg)[-1]))
3539 {
3540 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003541 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003542 ret = FAIL;
3543 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003544 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003545 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003546 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003547 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003548
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003549 // Clear the funcref afterwards, so that deleting it while
3550 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003551 if (evaluate)
3552 clear_tv(&base);
3553
Bram Moolenaarac92e252019-08-03 21:58:38 +02003554 return ret;
3555}
3556
3557/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003558 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3559 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003560 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3561 */
3562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003563eval_index(
3564 char_u **arg,
3565 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003566 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003567 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003568{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003569 int evaluate = evalarg != NULL
3570 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003571 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003574 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003575 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003576
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003577 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3578 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003579
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003580 init_tv(&var1);
3581 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003582 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003583 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584 /*
3585 * dict.name
3586 */
3587 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003588 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003590 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003591 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003592 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003593 }
3594 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003595 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 /*
3597 * something[idx]
3598 *
3599 * Get the (first) variable from inside the [].
3600 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003601 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 if (**arg == ':')
3603 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003604 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003606 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003607 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003608 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 clear_tv(&var1);
3610 return FAIL;
3611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612
3613 /*
3614 * Get the second variable from inside the [:].
3615 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003616 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 if (**arg == ':')
3618 {
3619 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003620 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 if (**arg == ']')
3622 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003623 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003625 if (!empty1)
3626 clear_tv(&var1);
3627 return FAIL;
3628 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003629 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003631 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003632 if (!empty1)
3633 clear_tv(&var1);
3634 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635 return FAIL;
3636 }
3637 }
3638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003639 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003640 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003641 if (**arg != ']')
3642 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003643 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003644 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645 clear_tv(&var1);
3646 if (range)
3647 clear_tv(&var2);
3648 return FAIL;
3649 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003650 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003651 }
3652
3653 if (evaluate)
3654 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003655 int res = eval_index_inner(rettv, range,
3656 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3657 key, keylen, verbose);
3658 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003659 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003660 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003661 clear_tv(&var2);
3662 return res;
3663 }
3664 return OK;
3665}
3666
3667/*
3668 * Check if "rettv" can have an [index] or [sli:ce]
3669 */
3670 int
3671check_can_index(typval_T *rettv, int evaluate, int verbose)
3672{
3673 switch (rettv->v_type)
3674 {
3675 case VAR_FUNC:
3676 case VAR_PARTIAL:
3677 if (verbose)
3678 emsg(_("E695: Cannot index a Funcref"));
3679 return FAIL;
3680 case VAR_FLOAT:
3681#ifdef FEAT_FLOAT
3682 if (verbose)
3683 emsg(_(e_float_as_string));
3684 return FAIL;
3685#endif
3686 case VAR_BOOL:
3687 case VAR_SPECIAL:
3688 case VAR_JOB:
3689 case VAR_CHANNEL:
3690 if (verbose)
3691 emsg(_(e_cannot_index_special_variable));
3692 return FAIL;
3693 case VAR_UNKNOWN:
3694 case VAR_ANY:
3695 case VAR_VOID:
3696 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003697 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003698 emsg(_(e_cannot_index_special_variable));
3699 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003700 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003701 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003702
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003703 case VAR_STRING:
3704 case VAR_LIST:
3705 case VAR_DICT:
3706 case VAR_BLOB:
3707 break;
3708 case VAR_NUMBER:
3709 if (in_vim9script())
3710 emsg(_(e_cannot_index_number));
3711 break;
3712 }
3713 return OK;
3714}
3715
3716/*
3717 * Apply index or range to "rettv".
3718 * "var1" is the first index, NULL for [:expr].
3719 * "var2" is the second index, NULL for [expr] and [expr: ]
3720 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3721 */
3722 int
3723eval_index_inner(
3724 typval_T *rettv,
3725 int is_range,
3726 typval_T *var1,
3727 typval_T *var2,
3728 char_u *key,
3729 int keylen,
3730 int verbose)
3731{
3732 long n1, n2 = 0;
3733 long len;
3734
3735 n1 = 0;
3736 if (var1 != NULL && rettv->v_type != VAR_DICT)
3737 n1 = tv_get_number(var1);
3738
3739 if (is_range)
3740 {
3741 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003742 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003743 if (verbose)
3744 emsg(_(e_cannot_slice_dictionary));
3745 return FAIL;
3746 }
3747 if (var2 == NULL)
3748 n2 = -1;
3749 else
3750 n2 = tv_get_number(var2);
3751 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003752
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003753 switch (rettv->v_type)
3754 {
3755 case VAR_UNKNOWN:
3756 case VAR_ANY:
3757 case VAR_VOID:
3758 case VAR_FUNC:
3759 case VAR_PARTIAL:
3760 case VAR_FLOAT:
3761 case VAR_BOOL:
3762 case VAR_SPECIAL:
3763 case VAR_JOB:
3764 case VAR_CHANNEL:
3765 break; // not evaluating, skipping over subscript
3766
3767 case VAR_NUMBER:
3768 case VAR_STRING:
3769 {
3770 char_u *s = tv_get_string(rettv);
3771
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003772 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003773 if (in_vim9script())
3774 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003775 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003776 s = string_slice(s, n1, n2);
3777 else
3778 s = char_from_string(s, n1);
3779 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003780 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003782 // The resulting variable is a substring. If the indexes
3783 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003784 if (n1 < 0)
3785 {
3786 n1 = len + n1;
3787 if (n1 < 0)
3788 n1 = 0;
3789 }
3790 if (n2 < 0)
3791 n2 = len + n2;
3792 else if (n2 >= len)
3793 n2 = len;
3794 if (n1 >= len || n2 < 0 || n1 > n2)
3795 s = NULL;
3796 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003797 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003798 }
3799 else
3800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003801 // The resulting variable is a string of a single
3802 // character. If the index is too big or negative the
3803 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003804 if (n1 >= len || n1 < 0)
3805 s = NULL;
3806 else
3807 s = vim_strnsave(s + n1, 1);
3808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003809 clear_tv(rettv);
3810 rettv->v_type = VAR_STRING;
3811 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003812 }
3813 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003814
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003815 case VAR_BLOB:
3816 len = blob_len(rettv->vval.v_blob);
3817 if (is_range)
3818 {
3819 // The resulting variable is a sub-blob. If the indexes
3820 // are out of range the result is empty.
3821 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003822 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003823 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003824 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003825 n1 = 0;
3826 }
3827 if (n2 < 0)
3828 n2 = len + n2;
3829 else if (n2 >= len)
3830 n2 = len - 1;
3831 if (n1 >= len || n2 < 0 || n1 > n2)
3832 {
3833 clear_tv(rettv);
3834 rettv->v_type = VAR_BLOB;
3835 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003836 }
3837 else
3838 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003839 blob_T *blob = blob_alloc();
3840 long i;
3841
3842 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003843 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003844 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003845 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003846 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003847 return FAIL;
3848 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003849 blob->bv_ga.ga_len = n2 - n1 + 1;
3850 for (i = n1; i <= n2; i++)
3851 blob_set(blob, i - n1,
3852 blob_get(rettv->vval.v_blob, i));
3853
3854 clear_tv(rettv);
3855 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003856 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003857 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003858 }
3859 else
3860 {
3861 // The resulting variable is a byte value.
3862 // If the index is too big or negative that is an error.
3863 if (n1 < 0)
3864 n1 = len + n1;
3865 if (n1 < len && n1 >= 0)
3866 {
3867 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003868
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003869 clear_tv(rettv);
3870 rettv->v_type = VAR_NUMBER;
3871 rettv->vval.v_number = v;
3872 }
3873 else
3874 semsg(_(e_blobidx), n1);
3875 }
3876 break;
3877
3878 case VAR_LIST:
3879 if (var1 == NULL)
3880 n1 = 0;
3881 if (var2 == NULL)
3882 n2 = -1;
3883 if (list_slice_or_index(rettv->vval.v_list,
3884 is_range, n1, n2, rettv, verbose) == FAIL)
3885 return FAIL;
3886 break;
3887
3888 case VAR_DICT:
3889 {
3890 dictitem_T *item;
3891 typval_T tmp;
3892
3893 if (key == NULL)
3894 {
3895 key = tv_get_string_chk(var1);
3896 if (key == NULL)
3897 return FAIL;
3898 }
3899
3900 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
3901
3902 if (item == NULL && verbose)
3903 semsg(_(e_dictkey), key);
3904 if (item == NULL)
3905 return FAIL;
3906
3907 copy_tv(&item->di_tv, &tmp);
3908 clear_tv(rettv);
3909 *rettv = tmp;
3910 }
3911 break;
3912 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003913 return OK;
3914}
3915
3916/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003917 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003918 */
3919 char_u *
3920partial_name(partial_T *pt)
3921{
3922 if (pt->pt_name != NULL)
3923 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003924 if (pt->pt_func != NULL)
3925 return pt->pt_func->uf_name;
3926 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003927}
3928
Bram Moolenaarddecc252016-04-06 22:59:37 +02003929 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003930partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003931{
3932 int i;
3933
3934 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003935 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003936 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003937 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003938 if (pt->pt_name != NULL)
3939 {
3940 func_unref(pt->pt_name);
3941 vim_free(pt->pt_name);
3942 }
3943 else
3944 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003945
3946 if (pt->pt_funcstack != NULL)
3947 {
3948 // Decrease the reference count for the context of a closure. If down
3949 // to zero free it and clear the variables on the stack.
3950 if (--pt->pt_funcstack->fs_refcount == 0)
3951 {
3952 garray_T *gap = &pt->pt_funcstack->fs_ga;
3953 typval_T *stack = gap->ga_data;
3954
3955 for (i = 0; i < gap->ga_len; ++i)
3956 clear_tv(stack + i);
3957 ga_clear(gap);
3958 vim_free(pt->pt_funcstack);
3959 }
3960 pt->pt_funcstack = NULL;
3961 }
3962
Bram Moolenaarddecc252016-04-06 22:59:37 +02003963 vim_free(pt);
3964}
3965
3966/*
3967 * Unreference a closure: decrement the reference count and free it when it
3968 * becomes zero.
3969 */
3970 void
3971partial_unref(partial_T *pt)
3972{
3973 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003974 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003975}
3976
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003977/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003978 * Return the next (unique) copy ID.
3979 * Used for serializing nested structures.
3980 */
3981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003982get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003983{
3984 current_copyID += COPYID_INC;
3985 return current_copyID;
3986}
3987
3988/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003989 * Garbage collection for lists and dictionaries.
3990 *
3991 * We use reference counts to be able to free most items right away when they
3992 * are no longer used. But for composite items it's possible that it becomes
3993 * unused while the reference count is > 0: When there is a recursive
3994 * reference. Example:
3995 * :let l = [1, 2, 3]
3996 * :let d = {9: l}
3997 * :let l[1] = d
3998 *
3999 * Since this is quite unusual we handle this with garbage collection: every
4000 * once in a while find out which lists and dicts are not referenced from any
4001 * variable.
4002 *
4003 * Here is a good reference text about garbage collection (refers to Python
4004 * but it applies to all reference-counting mechanisms):
4005 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004006 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004007
4008/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004009 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004010 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004011 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004012 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004013 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004014garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004015{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004016 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004017 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004018 buf_T *buf;
4019 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004020 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004021 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004022
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004023 if (!testing)
4024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004025 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004026 want_garbage_collect = FALSE;
4027 may_garbage_collect = FALSE;
4028 garbage_collect_at_exit = FALSE;
4029 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004030
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004031 // The execution stack can grow big, limit the size.
4032 if (exestack.ga_maxlen - exestack.ga_len > 500)
4033 {
4034 size_t new_len;
4035 char_u *pp;
4036 int n;
4037
4038 // Keep 150% of the current size, with a minimum of the growth size.
4039 n = exestack.ga_len / 2;
4040 if (n < exestack.ga_growsize)
4041 n = exestack.ga_growsize;
4042
4043 // Don't make it bigger though.
4044 if (exestack.ga_len + n < exestack.ga_maxlen)
4045 {
4046 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4047 pp = vim_realloc(exestack.ga_data, new_len);
4048 if (pp == NULL)
4049 return FAIL;
4050 exestack.ga_maxlen = exestack.ga_len + n;
4051 exestack.ga_data = pp;
4052 }
4053 }
4054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004055 // We advance by two because we add one for items referenced through
4056 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004057 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004058
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004059 /*
4060 * 1. Go through all accessible variables and mark all lists and dicts
4061 * with copyID.
4062 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004064 // Don't free variables in the previous_funccal list unless they are only
4065 // referenced through previous_funccal. This must be first, because if
4066 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004067 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004069 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004070 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004072 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004073 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004074 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4075 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004077 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004079 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4080 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004081 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004082 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4083 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004084#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004085 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004086 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4087 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004088 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004089 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004090 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4091 NULL, NULL);
4092#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004094 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004095 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004096 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4097 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004098 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004099 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004103
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004104 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004105 abort = abort || set_ref_in_functions(copyID);
4106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004107 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004108 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004110 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004111 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004112
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004113 // callbacks in buffers
4114 abort = abort || set_ref_in_buffers(copyID);
4115
Bram Moolenaar1dced572012-04-05 16:54:08 +02004116#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004117 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004118#endif
4119
Bram Moolenaardb913952012-06-29 12:54:53 +02004120#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004121 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004122#endif
4123
4124#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004125 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004126#endif
4127
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004128#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004129 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004130 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004131#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004132#ifdef FEAT_NETBEANS_INTG
4133 abort = abort || set_ref_in_nb_channel(copyID);
4134#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004135
Bram Moolenaare3188e22016-05-31 21:13:04 +02004136#ifdef FEAT_TIMERS
4137 abort = abort || set_ref_in_timer(copyID);
4138#endif
4139
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004140#ifdef FEAT_QUICKFIX
4141 abort = abort || set_ref_in_quickfix(copyID);
4142#endif
4143
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004144#ifdef FEAT_TERMINAL
4145 abort = abort || set_ref_in_term(copyID);
4146#endif
4147
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004148#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004149 abort = abort || set_ref_in_popups(copyID);
4150#endif
4151
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004152 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004153 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004154 /*
4155 * 2. Free lists and dictionaries that are not referenced.
4156 */
4157 did_free = free_unref_items(copyID);
4158
4159 /*
4160 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004161 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004162 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004163 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004164 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004165 else if (p_verbose > 0)
4166 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004167 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004168 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004169
4170 return did_free;
4171}
4172
4173/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004174 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004175 */
4176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004177free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004178{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004179 int did_free = FALSE;
4180
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004181 // Let all "free" functions know that we are here. This means no
4182 // dictionaries, lists, channels or jobs are to be freed, because we will
4183 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004184 in_free_unref_items = TRUE;
4185
4186 /*
4187 * PASS 1: free the contents of the items. We don't free the items
4188 * themselves yet, so that it is possible to decrement refcount counters
4189 */
4190
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004191 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004192 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004193
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004194 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004195 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004196
4197#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004198 // Go through the list of jobs and free items without the copyID. This
4199 // must happen before doing channels, because jobs refer to channels, but
4200 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004201 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004203 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004204 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4205#endif
4206
4207 /*
4208 * PASS 2: free the items themselves.
4209 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004210 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004211 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004212
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004213#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004214 // Go through the list of jobs and free items without the copyID. This
4215 // must happen before doing channels, because jobs refer to channels, but
4216 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004217 free_unused_jobs(copyID, COPYID_MASK);
4218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004219 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004220 free_unused_channels(copyID, COPYID_MASK);
4221#endif
4222
4223 in_free_unref_items = FALSE;
4224
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004225 return did_free;
4226}
4227
4228/*
4229 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004230 * "list_stack" is used to add lists to be marked. Can be NULL.
4231 *
4232 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004233 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004234 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004235set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004236{
4237 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004238 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004239 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004240 hashtab_T *cur_ht;
4241 ht_stack_T *ht_stack = NULL;
4242 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004243
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004244 cur_ht = ht;
4245 for (;;)
4246 {
4247 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004248 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004249 // Mark each item in the hashtab. If the item contains a hashtab
4250 // it is added to ht_stack, if it contains a list it is added to
4251 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004252 todo = (int)cur_ht->ht_used;
4253 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4254 if (!HASHITEM_EMPTY(hi))
4255 {
4256 --todo;
4257 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4258 &ht_stack, list_stack);
4259 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004260 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004261
4262 if (ht_stack == NULL)
4263 break;
4264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004265 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004266 cur_ht = ht_stack->ht;
4267 tempitem = ht_stack;
4268 ht_stack = ht_stack->prev;
4269 free(tempitem);
4270 }
4271
4272 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004273}
4274
4275/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004276 * Mark a dict and its items with "copyID".
4277 * Returns TRUE if setting references failed somehow.
4278 */
4279 int
4280set_ref_in_dict(dict_T *d, int copyID)
4281{
4282 if (d != NULL && d->dv_copyID != copyID)
4283 {
4284 d->dv_copyID = copyID;
4285 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4286 }
4287 return FALSE;
4288}
4289
4290/*
4291 * Mark a list and its items with "copyID".
4292 * Returns TRUE if setting references failed somehow.
4293 */
4294 int
4295set_ref_in_list(list_T *ll, int copyID)
4296{
4297 if (ll != NULL && ll->lv_copyID != copyID)
4298 {
4299 ll->lv_copyID = copyID;
4300 return set_ref_in_list_items(ll, copyID, NULL);
4301 }
4302 return FALSE;
4303}
4304
4305/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004306 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004307 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4308 *
4309 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004310 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004311 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004312set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004313{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004314 listitem_T *li;
4315 int abort = FALSE;
4316 list_T *cur_l;
4317 list_stack_T *list_stack = NULL;
4318 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004319
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004320 cur_l = l;
4321 for (;;)
4322 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004323 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004324 // Mark each item in the list. If the item contains a hashtab
4325 // it is added to ht_stack, if it contains a list it is added to
4326 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004327 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4328 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4329 ht_stack, &list_stack);
4330 if (list_stack == NULL)
4331 break;
4332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004333 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004334 cur_l = list_stack->list;
4335 tempitem = list_stack;
4336 list_stack = list_stack->prev;
4337 free(tempitem);
4338 }
4339
4340 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004341}
4342
4343/*
4344 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004345 * "list_stack" is used to add lists to be marked. Can be NULL.
4346 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4347 *
4348 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004349 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004350 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004351set_ref_in_item(
4352 typval_T *tv,
4353 int copyID,
4354 ht_stack_T **ht_stack,
4355 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004356{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004357 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004358
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004359 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004360 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004361 dict_T *dd = tv->vval.v_dict;
4362
Bram Moolenaara03f2332016-02-06 18:09:59 +01004363 if (dd != NULL && dd->dv_copyID != copyID)
4364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004365 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004366 dd->dv_copyID = copyID;
4367 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004368 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004369 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4370 }
4371 else
4372 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004373 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4374
Bram Moolenaara03f2332016-02-06 18:09:59 +01004375 if (newitem == NULL)
4376 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004377 else
4378 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004379 newitem->ht = &dd->dv_hashtab;
4380 newitem->prev = *ht_stack;
4381 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004382 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004383 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004384 }
4385 }
4386 else if (tv->v_type == VAR_LIST)
4387 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004388 list_T *ll = tv->vval.v_list;
4389
Bram Moolenaara03f2332016-02-06 18:09:59 +01004390 if (ll != NULL && ll->lv_copyID != copyID)
4391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004392 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004393 ll->lv_copyID = copyID;
4394 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004395 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004396 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004397 }
4398 else
4399 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004400 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4401
Bram Moolenaara03f2332016-02-06 18:09:59 +01004402 if (newitem == NULL)
4403 abort = TRUE;
4404 else
4405 {
4406 newitem->list = ll;
4407 newitem->prev = *list_stack;
4408 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004409 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004410 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004411 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004412 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004413 else if (tv->v_type == VAR_FUNC)
4414 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004415 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004416 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004417 else if (tv->v_type == VAR_PARTIAL)
4418 {
4419 partial_T *pt = tv->vval.v_partial;
4420 int i;
4421
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004422 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004423 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004424 // Didn't see this partial yet.
4425 pt->pt_copyID = copyID;
4426
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004427 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004428
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004429 if (pt->pt_dict != NULL)
4430 {
4431 typval_T dtv;
4432
4433 dtv.v_type = VAR_DICT;
4434 dtv.vval.v_dict = pt->pt_dict;
4435 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4436 }
4437
4438 for (i = 0; i < pt->pt_argc; ++i)
4439 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4440 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004441 if (pt->pt_funcstack != NULL)
4442 {
4443 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4444
4445 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4446 abort = abort || set_ref_in_item(stack + i, copyID,
4447 ht_stack, list_stack);
4448 }
4449
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004450 }
4451 }
4452#ifdef FEAT_JOB_CHANNEL
4453 else if (tv->v_type == VAR_JOB)
4454 {
4455 job_T *job = tv->vval.v_job;
4456 typval_T dtv;
4457
4458 if (job != NULL && job->jv_copyID != copyID)
4459 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004460 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004461 if (job->jv_channel != NULL)
4462 {
4463 dtv.v_type = VAR_CHANNEL;
4464 dtv.vval.v_channel = job->jv_channel;
4465 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4466 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004467 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004468 {
4469 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004470 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004471 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4472 }
4473 }
4474 }
4475 else if (tv->v_type == VAR_CHANNEL)
4476 {
4477 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004478 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004479 typval_T dtv;
4480 jsonq_T *jq;
4481 cbq_T *cq;
4482
4483 if (ch != NULL && ch->ch_copyID != copyID)
4484 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004485 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004486 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004487 {
4488 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4489 jq = jq->jq_next)
4490 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4491 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4492 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004493 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004494 {
4495 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004496 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004497 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4498 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004499 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004500 {
4501 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004502 dtv.vval.v_partial =
4503 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004504 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4505 }
4506 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004507 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004508 {
4509 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004510 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004511 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4512 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004513 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004514 {
4515 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004516 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004517 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4518 }
4519 }
4520 }
4521#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004522 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004523}
4524
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004526 * Return a string with the string representation of a variable.
4527 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004528 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004529 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004530 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4531 * stings as "string()", otherwise does not put quotes around strings, as
4532 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004533 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4534 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004535 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004536 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004537 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004538echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004539 typval_T *tv,
4540 char_u **tofree,
4541 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004542 int copyID,
4543 int echo_style,
4544 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004545 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004546{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004547 static int recurse = 0;
4548 char_u *r = NULL;
4549
Bram Moolenaar33570922005-01-25 22:26:29 +00004550 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004551 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004552 if (!did_echo_string_emsg)
4553 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004554 // Only give this message once for a recursive call to avoid
4555 // flooding the user with errors. And stop iterating over lists
4556 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004557 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004558 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004559 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004560 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004561 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004562 }
4563 ++recurse;
4564
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004565 switch (tv->v_type)
4566 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004567 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004568 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004569 {
4570 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004571 r = tv->vval.v_string;
4572 if (r == NULL)
4573 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004574 }
4575 else
4576 {
4577 *tofree = string_quote(tv->vval.v_string, FALSE);
4578 r = *tofree;
4579 }
4580 break;
4581
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004583 if (echo_style)
4584 {
4585 *tofree = NULL;
4586 r = tv->vval.v_string;
4587 }
4588 else
4589 {
4590 *tofree = string_quote(tv->vval.v_string, TRUE);
4591 r = *tofree;
4592 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004593 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004594
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004595 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004596 {
4597 partial_T *pt = tv->vval.v_partial;
4598 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004599 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004600 garray_T ga;
4601 int i;
4602 char_u *tf;
4603
4604 ga_init2(&ga, 1, 100);
4605 ga_concat(&ga, (char_u *)"function(");
4606 if (fname != NULL)
4607 {
4608 ga_concat(&ga, fname);
4609 vim_free(fname);
4610 }
4611 if (pt != NULL && pt->pt_argc > 0)
4612 {
4613 ga_concat(&ga, (char_u *)", [");
4614 for (i = 0; i < pt->pt_argc; ++i)
4615 {
4616 if (i > 0)
4617 ga_concat(&ga, (char_u *)", ");
4618 ga_concat(&ga,
4619 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4620 vim_free(tf);
4621 }
4622 ga_concat(&ga, (char_u *)"]");
4623 }
4624 if (pt != NULL && pt->pt_dict != NULL)
4625 {
4626 typval_T dtv;
4627
4628 ga_concat(&ga, (char_u *)", ");
4629 dtv.v_type = VAR_DICT;
4630 dtv.vval.v_dict = pt->pt_dict;
4631 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4632 vim_free(tf);
4633 }
4634 ga_concat(&ga, (char_u *)")");
4635
4636 *tofree = ga.ga_data;
4637 r = *tofree;
4638 break;
4639 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004640
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004641 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004642 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004643 break;
4644
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004645 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004646 if (tv->vval.v_list == NULL)
4647 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004648 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004649 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004650 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004651 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004652 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4653 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004654 {
4655 *tofree = NULL;
4656 r = (char_u *)"[...]";
4657 }
4658 else
4659 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004660 int old_copyID = tv->vval.v_list->lv_copyID;
4661
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004662 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004663 *tofree = list2string(tv, copyID, restore_copyID);
4664 if (restore_copyID)
4665 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004666 r = *tofree;
4667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004668 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004669
Bram Moolenaar8c711452005-01-14 21:53:12 +00004670 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004671 if (tv->vval.v_dict == NULL)
4672 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004673 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004674 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004675 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004676 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004677 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4678 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004679 {
4680 *tofree = NULL;
4681 r = (char_u *)"{...}";
4682 }
4683 else
4684 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004685 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004686
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004687 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004688 *tofree = dict2string(tv, copyID, restore_copyID);
4689 if (restore_copyID)
4690 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004691 r = *tofree;
4692 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004693 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004696 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004697 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004699 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004700 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004701 break;
4702
Bram Moolenaar835dc632016-02-07 14:27:38 +01004703 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004704 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004705 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004706 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004707 if (composite_val)
4708 {
4709 *tofree = string_quote(r, FALSE);
4710 r = *tofree;
4711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004713
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004714 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004715#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 *tofree = NULL;
4717 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4718 r = numbuf;
4719 break;
4720#endif
4721
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004722 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004723 case VAR_SPECIAL:
4724 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004725 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004726 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004727 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004728
Bram Moolenaar8502c702014-06-17 12:51:16 +02004729 if (--recurse == 0)
4730 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004731 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004732}
4733
4734/*
4735 * Return a string with the string representation of a variable.
4736 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4737 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004738 * Does not put quotes around strings, as ":echo" displays values.
4739 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4740 * May return NULL.
4741 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004743echo_string(
4744 typval_T *tv,
4745 char_u **tofree,
4746 char_u *numbuf,
4747 int copyID)
4748{
4749 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4750}
4751
4752/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004753 * Return string "str" in ' quotes, doubling ' characters.
4754 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004756 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004757 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004758string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004759{
Bram Moolenaar33570922005-01-25 22:26:29 +00004760 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004761 char_u *p, *r, *s;
4762
Bram Moolenaar33570922005-01-25 22:26:29 +00004763 len = (function ? 13 : 3);
4764 if (str != NULL)
4765 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004766 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004767 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004768 if (*p == '\'')
4769 ++len;
4770 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004771 s = r = alloc(len);
4772 if (r != NULL)
4773 {
4774 if (function)
4775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004777 r += 10;
4778 }
4779 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004781 if (str != NULL)
4782 for (p = str; *p != NUL; )
4783 {
4784 if (*p == '\'')
4785 *r++ = '\'';
4786 MB_COPY_CHAR(p, r);
4787 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004789 if (function)
4790 *r++ = ')';
4791 *r++ = NUL;
4792 }
4793 return s;
4794}
4795
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004796#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797/*
4798 * Convert the string "text" to a floating point number.
4799 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4800 * this always uses a decimal point.
4801 * Returns the length of the text that was consumed.
4802 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004804string2float(
4805 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004806 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807{
4808 char *s = (char *)text;
4809 float_T f;
4810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004811 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004812 if (STRNICMP(text, "inf", 3) == 0)
4813 {
4814 *value = INFINITY;
4815 return 3;
4816 }
4817 if (STRNICMP(text, "-inf", 3) == 0)
4818 {
4819 *value = -INFINITY;
4820 return 4;
4821 }
4822 if (STRNICMP(text, "nan", 3) == 0)
4823 {
4824 *value = NAN;
4825 return 3;
4826 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827 f = strtod(s, &s);
4828 *value = f;
4829 return (int)((char_u *)s - text);
4830}
4831#endif
4832
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004835 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004837 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004838var2fpos(
4839 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004840 int dollar_lnum, // TRUE when $ is last line
4841 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004843 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004845 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004847 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004848 if (varp->v_type == VAR_LIST)
4849 {
4850 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004851 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004852 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004853 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004854
4855 l = varp->vval.v_list;
4856 if (l == NULL)
4857 return NULL;
4858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004859 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004860 pos.lnum = list_find_nr(l, 0L, &error);
4861 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004862 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004863 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004864
Bram Moolenaarec65d772020-08-20 22:29:12 +02004865 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004866 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004867 li = list_find(l, 1L);
4868 if (li != NULL && li->li_tv.v_type == VAR_STRING
4869 && li->li_tv.vval.v_string != NULL
4870 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02004871 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004872 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02004873 }
4874 else
4875 {
4876 pos.col = list_find_nr(l, 1L, &error);
4877 if (error)
4878 return NULL;
4879 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00004880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004881 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004882 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004883 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004884 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004886 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004887 pos.coladd = list_find_nr(l, 2L, &error);
4888 if (error)
4889 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004890
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004891 return &pos;
4892 }
4893
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004894 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004895 if (name == NULL)
4896 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004897 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004899 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004900 {
4901 if (VIsual_active)
4902 return &VIsual;
4903 return &curwin->w_cursor;
4904 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004905 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004907 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4909 return NULL;
4910 return pp;
4911 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004912
Bram Moolenaara5525202006-03-02 22:52:09 +00004913 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004914
Bram Moolenaar477933c2007-07-17 14:32:23 +00004915 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004916 {
4917 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004919 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004920 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // In silent Ex mode topline is zero, but that's not a valid line
4922 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004923 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004924 return &pos;
4925 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004926 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004927 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004928 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004929 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004930 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004931 return &pos;
4932 }
4933 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004934 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004936 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
4938 pos.lnum = curbuf->b_ml.ml_line_count;
4939 pos.col = 0;
4940 }
4941 else
4942 {
4943 pos.lnum = curwin->w_cursor.lnum;
4944 pos.col = (colnr_T)STRLEN(ml_get_curline());
4945 }
4946 return &pos;
4947 }
4948 return NULL;
4949}
4950
4951/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004952 * Convert list in "arg" into a position and optional file number.
4953 * When "fnump" is NULL there is no file number, only 3 items.
4954 * Note that the column is passed on as-is, the caller may want to decrement
4955 * it to use 1 for the first column.
4956 * Return FAIL when conversion is not possible, doesn't check the position for
4957 * validity.
4958 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004959 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004960list2fpos(
4961 typval_T *arg,
4962 pos_T *posp,
4963 int *fnump,
4964 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004965{
4966 list_T *l = arg->vval.v_list;
4967 long i = 0;
4968 long n;
4969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004970 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4971 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004972 if (arg->v_type != VAR_LIST
4973 || l == NULL
4974 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004975 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004976 return FAIL;
4977
4978 if (fnump != NULL)
4979 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004980 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004981 if (n < 0)
4982 return FAIL;
4983 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004985 *fnump = n;
4986 }
4987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004988 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004989 if (n < 0)
4990 return FAIL;
4991 posp->lnum = n;
4992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004993 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004994 if (n < 0)
4995 return FAIL;
4996 posp->col = n;
4997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004999 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005000 posp->coladd = 0;
5001 else
5002 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005003
Bram Moolenaar493c1782014-05-28 14:34:46 +02005004 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005005 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005006
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005007 return OK;
5008}
5009
5010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 * Get the length of an environment variable name.
5012 * Advance "arg" to the first character after the name.
5013 * Return 0 for error.
5014 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005015 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005016get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
5018 char_u *p;
5019 int len;
5020
5021 for (p = *arg; vim_isIDc(*p); ++p)
5022 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005023 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 return 0;
5025
5026 len = (int)(p - *arg);
5027 *arg = p;
5028 return len;
5029}
5030
5031/*
5032 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005033 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 * Return 0 if something is wrong.
5035 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005037get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
5039 char_u *p;
5040 int len;
5041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005042 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005044 {
5045 if (*p == ':')
5046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005047 // "s:" is start of "s:var", but "n:" is not and can be used in
5048 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005049 len = (int)(p - *arg);
5050 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5051 || len > 1)
5052 break;
5053 }
5054 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005055 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 return 0;
5057
5058 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005059 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
5061 return len;
5062}
5063
5064/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005065 * Get the length of the name of a variable or function.
5066 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005068 * Return -1 if curly braces expansion failed.
5069 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 * If the name contains 'magic' {}'s, expand them and return the
5071 * expanded name in an allocated string via 'alias' - caller must free.
5072 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005073 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005074get_name_len(
5075 char_u **arg,
5076 char_u **alias,
5077 int evaluate,
5078 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
5080 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 char_u *p;
5082 char_u *expr_start;
5083 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005085 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
5087 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5088 && (*arg)[2] == (int)KE_SNR)
5089 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005090 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 *arg += 3;
5092 return get_id_len(arg) + 3;
5093 }
5094 len = eval_fname_script(*arg);
5095 if (len > 0)
5096 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005097 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 *arg += len;
5099 }
5100
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005104 p = find_name_end(*arg, &expr_start, &expr_end,
5105 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (expr_start != NULL)
5107 {
5108 char_u *temp_string;
5109
5110 if (!evaluate)
5111 {
5112 len += (int)(p - *arg);
5113 *arg = skipwhite(p);
5114 return len;
5115 }
5116
5117 /*
5118 * Include any <SID> etc in the expanded string:
5119 * Thus the -len here.
5120 */
5121 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5122 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005123 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 *alias = temp_string;
5125 *arg = skipwhite(p);
5126 return (int)STRLEN(temp_string);
5127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005130 // Only give an error when there is something, otherwise it will be
5131 // reported at a higher level.
5132 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005133 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135 return len;
5136}
5137
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138/*
5139 * Find the end of a variable or function name, taking care of magic braces.
5140 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5141 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005142 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 * Return a pointer to just after the name. Equal to "arg" if there is no
5144 * valid name.
5145 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005146 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005147find_name_end(
5148 char_u *arg,
5149 char_u **expr_start,
5150 char_u **expr_end,
5151 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005153 int mb_nest = 0;
5154 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005156 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005157 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 *expr_start = NULL;
5162 *expr_end = NULL;
5163 }
5164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005165 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005166 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5167 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005168 return arg;
5169
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005170 for (p = arg; *p != NUL
5171 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005172 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005173 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005174 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005176 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005178 if (*p == '\'')
5179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005180 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005181 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005182 ;
5183 if (*p == NUL)
5184 break;
5185 }
5186 else if (*p == '"')
5187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005188 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005189 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005190 if (*p == '\\' && p[1] != NUL)
5191 ++p;
5192 if (*p == NUL)
5193 break;
5194 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005195 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // "s:" is start of "s:var", but "n:" is not and can be used in
5198 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005199 len = (int)(p - arg);
5200 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005201 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005202 break;
5203 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 if (*p == '[')
5208 ++br_nest;
5209 else if (*p == ']')
5210 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005212
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005213 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005215 if (*p == '{')
5216 {
5217 mb_nest++;
5218 if (expr_start != NULL && *expr_start == NULL)
5219 *expr_start = p;
5220 }
5221 else if (*p == '}')
5222 {
5223 mb_nest--;
5224 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5225 *expr_end = p;
5226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229
5230 return p;
5231}
5232
5233/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 * Expands out the 'magic' {}'s in a variable/function name.
5235 * Note that this can call itself recursively, to deal with
5236 * constructs like foo{bar}{baz}{bam}
5237 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5238 * "in_start" ^
5239 * "expr_start" ^
5240 * "expr_end" ^
5241 * "in_end" ^
5242 *
5243 * Returns a new allocated string, which the caller must free.
5244 * Returns NULL for failure.
5245 */
5246 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005247make_expanded_name(
5248 char_u *in_start,
5249 char_u *expr_start,
5250 char_u *expr_end,
5251 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005252{
5253 char_u c1;
5254 char_u *retval = NULL;
5255 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005256
5257 if (expr_end == NULL || in_end == NULL)
5258 return NULL;
5259 *expr_start = NUL;
5260 *expr_end = NUL;
5261 c1 = *in_end;
5262 *in_end = NUL;
5263
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005264 temp_result = eval_to_string(expr_start + 1, FALSE);
5265 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005266 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005267 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5268 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 if (retval != NULL)
5270 {
5271 STRCPY(retval, in_start);
5272 STRCAT(retval, temp_result);
5273 STRCAT(retval, expr_end + 1);
5274 }
5275 }
5276 vim_free(temp_result);
5277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005278 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005279 *expr_start = '{';
5280 *expr_end = '}';
5281
5282 if (retval != NULL)
5283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005284 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005285 if (expr_start != NULL)
5286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005287 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 temp_result = make_expanded_name(retval, expr_start,
5289 expr_end, temp_result);
5290 vim_free(retval);
5291 retval = temp_result;
5292 }
5293 }
5294
5295 return retval;
5296}
5297
5298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005300 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005303eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005305 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005306}
5307
5308/*
5309 * Return TRUE if character "c" can be used as the first character in a
5310 * variable or function name (excluding '{' and '}').
5311 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005314{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005315 return ASCII_ISALPHA(c) || c == '_';
5316}
5317
5318/*
5319 * Return TRUE if character "c" can be used as the first character of a
5320 * dictionary key.
5321 */
5322 int
5323eval_isdictc(int c)
5324{
5325 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326}
5327
5328/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005329 * Return the character "str[index]" where "index" is the character index. If
5330 * "index" is out of range NULL is returned.
5331 */
5332 char_u *
5333char_from_string(char_u *str, varnumber_T index)
5334{
5335 size_t nbyte = 0;
5336 varnumber_T nchar = index;
5337 size_t slen;
5338
5339 if (str == NULL || index < 0)
5340 return NULL;
5341 slen = STRLEN(str);
5342 while (nchar > 0 && nbyte < slen)
5343 {
5344 nbyte += MB_CPTR2LEN(str + nbyte);
5345 --nchar;
5346 }
5347 if (nbyte >= slen)
5348 return NULL;
5349 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5350}
5351
5352/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005353 * Get the byte index for character index "idx" in string "str" with length
5354 * "str_len".
5355 * If going over the end return "str_len".
5356 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005357 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005358 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005359 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005360char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5361{
5362 varnumber_T nchar = idx;
5363 size_t nbyte = 0;
5364
5365 if (nchar >= 0)
5366 {
5367 while (nchar > 0 && nbyte < str_len)
5368 {
5369 nbyte += MB_CPTR2LEN(str + nbyte);
5370 --nchar;
5371 }
5372 }
5373 else
5374 {
5375 nbyte = str_len;
5376 while (nchar < 0 && nbyte > 0)
5377 {
5378 --nbyte;
5379 nbyte -= mb_head_off(str, str + nbyte);
5380 ++nchar;
5381 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005382 if (nchar < 0)
5383 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005384 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005385 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005386}
5387
5388/*
5389 * Return the slice "str[first:last]" using character indexes.
5390 * Return NULL when the result is empty.
5391 */
5392 char_u *
5393string_slice(char_u *str, varnumber_T first, varnumber_T last)
5394{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005395 long start_byte, end_byte;
5396 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005397
5398 if (str == NULL)
5399 return NULL;
5400 slen = STRLEN(str);
5401 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005402 if (start_byte < 0)
5403 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005404 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005405 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005406 else
5407 {
5408 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005409 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005410 // end index is inclusive
5411 end_byte += MB_CPTR2LEN(str + end_byte);
5412 }
5413
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005414 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005415 return NULL;
5416 return vim_strnsave(str + start_byte, end_byte - start_byte);
5417}
5418
5419/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005420 * Handle:
5421 * - expr[expr], expr[expr:expr] subscript
5422 * - ".name" lookup
5423 * - function call with Funcref variable: func(expr)
5424 * - method call: var->method()
5425 *
5426 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005427 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005429handle_subscript(
5430 char_u **arg,
5431 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005432 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005433 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005434{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005435 int evaluate = evalarg != NULL
5436 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005437 int ret = OK;
5438 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005439 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005440 int getnext;
5441 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005442
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005443 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005444 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005445 // When at the end of the line and ".name" or "->{" or "->X" follows in
5446 // the next line then consume the line break.
5447 p = eval_next_non_blank(*arg, evalarg, &getnext);
5448 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005449 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005450 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005451 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005452 {
5453 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005454 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005455 check_white = FALSE;
5456 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005457
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005458 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5459 || rettv->v_type == VAR_PARTIAL))
5460 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005461 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005462 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5463 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005464
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005465 // Stop the expression evaluation when immediately aborting on
5466 // error, or when an interrupt occurred or an exception was thrown
5467 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005468 if (aborting())
5469 {
5470 if (ret == OK)
5471 clear_tv(rettv);
5472 ret = FAIL;
5473 }
5474 dict_unref(selfdict);
5475 selfdict = NULL;
5476 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005477 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005478 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005479 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005480 if (ret == OK)
5481 {
5482 if ((*arg)[2] == '{')
5483 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005484 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005485 else
5486 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005487 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005488 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005489 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005490 // "." is ".name" lookup when we found a dict or when evaluating and
5491 // scriptversion is at least 2, where string concatenation is "..".
5492 else if (**arg == '['
5493 || (**arg == '.' && (rettv->v_type == VAR_DICT
5494 || (!evaluate
5495 && (*arg)[1] != '.'
5496 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005497 {
5498 dict_unref(selfdict);
5499 if (rettv->v_type == VAR_DICT)
5500 {
5501 selfdict = rettv->vval.v_dict;
5502 if (selfdict != NULL)
5503 ++selfdict->dv_refcount;
5504 }
5505 else
5506 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005507 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005508 {
5509 clear_tv(rettv);
5510 ret = FAIL;
5511 }
5512 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005513 else
5514 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005515 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005517 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5518 // Don't do this when "Func" is already a partial that was bound
5519 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005520 if (selfdict != NULL
5521 && (rettv->v_type == VAR_FUNC
5522 || (rettv->v_type == VAR_PARTIAL
5523 && (rettv->vval.v_partial->pt_auto
5524 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005525 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005526
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005527 dict_unref(selfdict);
5528 return ret;
5529}
5530
5531/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005532 * Make a copy of an item.
5533 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005534 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5535 * reference to an already copied list/dict can be used.
5536 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005537 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005539item_copy(
5540 typval_T *from,
5541 typval_T *to,
5542 int deep,
5543 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005544{
5545 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005546 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005547
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005549 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005550 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005551 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005552 }
5553 ++recurse;
5554
5555 switch (from->v_type)
5556 {
5557 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005558 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005559 case VAR_STRING:
5560 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005561 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005562 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005563 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005564 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005565 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005566 copy_tv(from, to);
5567 break;
5568 case VAR_LIST:
5569 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005570 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005571 if (from->vval.v_list == NULL)
5572 to->vval.v_list = NULL;
5573 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005575 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005576 to->vval.v_list = from->vval.v_list->lv_copylist;
5577 ++to->vval.v_list->lv_refcount;
5578 }
5579 else
5580 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5581 if (to->vval.v_list == NULL)
5582 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005583 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005584 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005586 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005587 case VAR_DICT:
5588 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005589 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005590 if (from->vval.v_dict == NULL)
5591 to->vval.v_dict = NULL;
5592 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005594 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005595 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5596 ++to->vval.v_dict->dv_refcount;
5597 }
5598 else
5599 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5600 if (to->vval.v_dict == NULL)
5601 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005602 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005603 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005604 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005606 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005607 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005608 }
5609 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005610 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005611}
5612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613 void
5614echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5615{
5616 char_u *tofree;
5617 char_u numbuf[NUMBUFLEN];
5618 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5619
5620 if (*atstart)
5621 {
5622 *atstart = FALSE;
5623 // Call msg_start() after eval1(), evaluating the expression
5624 // may cause a message to appear.
5625 if (with_space)
5626 {
5627 // Mark the saved text as finishing the line, so that what
5628 // follows is displayed on a new line when scrolling back
5629 // at the more prompt.
5630 msg_sb_eol();
5631 msg_start();
5632 }
5633 }
5634 else if (with_space)
5635 msg_puts_attr(" ", echo_attr);
5636
5637 if (p != NULL)
5638 for ( ; *p != NUL && !got_int; ++p)
5639 {
5640 if (*p == '\n' || *p == '\r' || *p == TAB)
5641 {
5642 if (*p != TAB && *needclr)
5643 {
5644 // remove any text still there from the command
5645 msg_clr_eos();
5646 *needclr = FALSE;
5647 }
5648 msg_putchar_attr(*p, echo_attr);
5649 }
5650 else
5651 {
5652 if (has_mbyte)
5653 {
5654 int i = (*mb_ptr2len)(p);
5655
5656 (void)msg_outtrans_len_attr(p, i, echo_attr);
5657 p += i - 1;
5658 }
5659 else
5660 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5661 }
5662 }
5663 vim_free(tofree);
5664}
5665
Bram Moolenaare9a41262005-01-15 22:18:47 +00005666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 * ":echo expr1 ..." print each argument separated with a space, add a
5668 * newline at the end.
5669 * ":echon expr1 ..." print each argument plain.
5670 */
5671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005672ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
5674 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005675 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 char_u *p;
5677 int needclr = TRUE;
5678 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005679 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005680 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005681 evalarg_T evalarg;
5682
Bram Moolenaare707c882020-07-01 13:07:37 +02005683 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684
5685 if (eap->skip)
5686 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005687 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005689 // If eval1() causes an error message the text from the command may
5690 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005691 need_clr_eos = needclr;
5692
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005694 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {
5696 /*
5697 * Report the invalid expression unless the expression evaluation
5698 * has been cancelled due to an aborting error, an interrupt, or an
5699 * exception.
5700 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005701 if (!aborting() && did_emsg == did_emsg_before
5702 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005703 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005704 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 break;
5706 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005707 need_clr_eos = FALSE;
5708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005710 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005712 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 arg = skipwhite(arg);
5714 }
5715 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005716 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
5718 if (eap->skip)
5719 --emsg_skip;
5720 else
5721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005722 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (needclr)
5724 msg_clr_eos();
5725 if (eap->cmdidx == CMD_echo)
5726 msg_end();
5727 }
5728}
5729
5730/*
5731 * ":echohl {name}".
5732 */
5733 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005736 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737}
5738
5739/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005740 * Returns the :echo attribute
5741 */
5742 int
5743get_echo_attr(void)
5744{
5745 return echo_attr;
5746}
5747
5748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 * ":execute expr1 ..." execute the result of an expression.
5750 * ":echomsg expr1 ..." Print a message
5751 * ":echoerr expr1 ..." Print an error
5752 * Each gets spaces around each argument and a newline at the end for
5753 * echo commands
5754 */
5755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005756ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
5758 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005759 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 int ret = OK;
5761 char_u *p;
5762 garray_T ga;
5763 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765 ga_init2(&ga, 1, 80);
5766
5767 if (eap->skip)
5768 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005769 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005771 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005772 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774
5775 if (!eap->skip)
5776 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005777 char_u buf[NUMBUFLEN];
5778
5779 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005780 {
5781 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5782 {
5783 emsg(_(e_inval_string));
5784 p = NULL;
5785 }
5786 else
5787 p = tv_get_string_buf(&rettv, buf);
5788 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005789 else
5790 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005791 if (p == NULL)
5792 {
5793 clear_tv(&rettv);
5794 ret = FAIL;
5795 break;
5796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 len = (int)STRLEN(p);
5798 if (ga_grow(&ga, len + 2) == FAIL)
5799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005800 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 ret = FAIL;
5802 break;
5803 }
5804 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 ga.ga_len += len;
5808 }
5809
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005810 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 arg = skipwhite(arg);
5812 }
5813
5814 if (ret != FAIL && ga.ga_data != NULL)
5815 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005816 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005818 // Mark the already saved text as finishing the line, so that what
5819 // follows is displayed on a new line when scrolling back at the
5820 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005821 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005822 }
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005825 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005826 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005827 out_flush();
5828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 else if (eap->cmdidx == CMD_echoerr)
5830 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005831 int save_did_emsg = did_emsg;
5832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005833 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005834 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 if (!force_abort)
5836 did_emsg = save_did_emsg;
5837 }
5838 else if (eap->cmdidx == CMD_execute)
5839 do_cmdline((char_u *)ga.ga_data,
5840 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5841 }
5842
5843 ga_clear(&ga);
5844
5845 if (eap->skip)
5846 --emsg_skip;
5847
5848 eap->nextcmd = check_nextcmd(arg);
5849}
5850
5851/*
5852 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5853 * "arg" points to the "&" or '+' when called, to "option" when returning.
5854 * Returns NULL when no option name found. Otherwise pointer to the char
5855 * after the option name.
5856 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005857 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005858find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 char_u *p = *arg;
5861
5862 ++p;
5863 if (*p == 'g' && p[1] == ':')
5864 {
5865 *opt_flags = OPT_GLOBAL;
5866 p += 2;
5867 }
5868 else if (*p == 'l' && p[1] == ':')
5869 {
5870 *opt_flags = OPT_LOCAL;
5871 p += 2;
5872 }
5873 else
5874 *opt_flags = 0;
5875
5876 if (!ASCII_ISALPHA(*p))
5877 return NULL;
5878 *arg = p;
5879
5880 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005881 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 else
5883 while (ASCII_ISALPHA(*p))
5884 ++p;
5885 return p;
5886}
5887
5888/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005889 * Display script name where an item was last set.
5890 * Should only be invoked when 'verbose' is non-zero.
5891 */
5892 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005893last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005894{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005895 char_u *p;
5896
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005897 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005898 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005899 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005900 if (p != NULL)
5901 {
5902 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005903 msg_puts(_("\n\tLast set from "));
5904 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005905 if (script_ctx.sc_lnum > 0)
5906 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005907 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005908 msg_outnum((long)script_ctx.sc_lnum);
5909 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005910 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005911 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005912 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005913 }
5914}
5915
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005916#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918/*
5919 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005920 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 * "flags" can be "g" to do a global substitute.
5922 * Returns an allocated string, NULL for error.
5923 */
5924 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005925do_string_sub(
5926 char_u *str,
5927 char_u *pat,
5928 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005929 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005930 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
5932 int sublen;
5933 regmatch_T regmatch;
5934 int i;
5935 int do_all;
5936 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005937 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 garray_T ga;
5939 char_u *ret;
5940 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005941 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005943 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005945 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
5947 ga_init2(&ga, 1, 200);
5948
5949 do_all = (flags[0] == 'g');
5950
5951 regmatch.rm_ic = p_ic;
5952 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5953 if (regmatch.regprog != NULL)
5954 {
5955 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005956 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005959 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005960 if (regmatch.startp[0] == regmatch.endp[0])
5961 {
5962 if (zero_width == regmatch.startp[0])
5963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005965 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005966 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5967 (size_t)i);
5968 ga.ga_len += i;
5969 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005970 continue;
5971 }
5972 zero_width = regmatch.startp[0];
5973 }
5974
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 /*
5976 * Get some space for a temporary buffer to do the substitution
5977 * into. It will contain:
5978 * - The text up to where the match is.
5979 * - The substituted text.
5980 * - The text after the match.
5981 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005982 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005983 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5985 {
5986 ga_clear(&ga);
5987 break;
5988 }
5989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005990 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 i = (int)(regmatch.startp[0] - tail);
5992 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005993 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005994 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 + ga.ga_len + i, TRUE, TRUE, FALSE);
5996 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005997 tail = regmatch.endp[0];
5998 if (*tail == NUL)
5999 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (!do_all)
6001 break;
6002 }
6003
6004 if (ga.ga_data != NULL)
6005 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6006
Bram Moolenaar473de612013-06-08 18:19:48 +02006007 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 }
6009
6010 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6011 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006012 if (p_cpo == empty_option)
6013 p_cpo = save_cpo;
6014 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006015 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006016 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017
6018 return ret;
6019}