blob: 285558df8fbfac728966606c625f0d28dc232eed [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
60 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020061 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010062num_divide(varnumber_T n1, varnumber_T n2)
63{
64 varnumber_T result;
65
66 if (n2 == 0) // give an error message?
67 {
68 if (n1 == 0)
69 result = VARNUM_MIN; // similar to NaN
70 else if (n1 < 0)
71 result = -VARNUM_MAX;
72 else
73 result = VARNUM_MAX;
74 }
75 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
83 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020084 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010085num_modulus(varnumber_T n1, varnumber_T n2)
86{
87 // Give an error when n2 is 0?
88 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaara1fa8922017-01-12 20:06:33 +010091#if defined(EBCDIC) || defined(PROTO)
92/*
93 * Compare struct fst by function name.
94 */
95 static int
96compare_func_name(const void *s1, const void *s2)
97{
98 struct fst *p1 = (struct fst *)s1;
99 struct fst *p2 = (struct fst *)s2;
100
101 return STRCMP(p1->f_name, p2->f_name);
102}
103
104/*
105 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100106 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107 * On machines using EBCDIC we have to sort it.
108 */
109 static void
110sortFunctions(void)
111{
112 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
113
114 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
115}
116#endif
117
Bram Moolenaar33570922005-01-25 22:26:29 +0000118/*
119 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000120 */
121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100122eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000123{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200124 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200125 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000126
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200127#ifdef EBCDIC
128 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100129 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200130 */
131 sortFunctions();
132#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000133}
134
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000135#if defined(EXITFREE) || defined(PROTO)
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100140 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200141 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000142
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200143 // autoloaded script names
144 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000145
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200146 // unreferenced lists and dicts
147 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200148
149 // functions not garbage collected
150 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151}
152#endif
153
Bram Moolenaare6b53242020-07-01 17:28:33 +0200154 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
156{
157 CLEAR_FIELD(*evalarg);
158 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
159 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
160 {
161 evalarg->eval_getline = eap->getline;
162 evalarg->eval_cookie = eap->cookie;
163 }
164}
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Top level evaluation function, returning a boolean.
168 * Sets "error" to TRUE if there was an error.
169 * Return TRUE or FALSE.
170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172eval_to_bool(
173 char_u *arg,
174 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200175 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar33570922005-01-25 22:26:29 +0000178 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200180 evalarg_T evalarg;
181
Bram Moolenaar37c83712020-06-30 21:18:36 +0200182 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 if (skip)
185 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200186 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 {
190 *error = FALSE;
191 if (!skip)
192 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200193 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200194 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200195 else
196 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000197 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 }
199 }
200 if (skip)
201 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200204 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205}
206
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207/*
208 * Call eval1() and give an error message if not done at a lower level.
209 */
210 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 int ret;
215 int did_emsg_before = did_emsg;
216 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200219 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
220
221 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 if (ret == FAIL)
223 {
224 // Report the invalid expression unless the expression evaluation has
225 // been cancelled due to an aborting error, an interrupt, or an
226 // exception, or we already gave a more specific error.
227 // Also check called_emsg for when using assert_fails().
228 if (!aborting() && did_emsg == did_emsg_before
229 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100230 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233 return ret;
234}
235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200237 * Return whether a typval is a valid expression to pass to eval_expr_typval()
238 * or eval_expr_to_bool(). An empty string returns FALSE;
239 */
240 int
241eval_expr_valid_arg(typval_T *tv)
242{
243 return tv->v_type != VAR_UNKNOWN
244 && (tv->v_type != VAR_STRING
245 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Evaluate an expression, which can be a function, partial or string.
250 * Pass arguments "argv[argc]".
251 * Return the result in "rettv" and OK or FAIL.
252 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200253 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100254eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
255{
256 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200258 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100259
260 if (expr->v_type == VAR_FUNC)
261 {
262 s = expr->vval.v_string;
263 if (s == NULL || *s == NUL)
264 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200265 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200266 funcexe.evaluate = TRUE;
267 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 else if (expr->v_type == VAR_PARTIAL)
271 {
272 partial_T *partial = expr->vval.v_partial;
273
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200274 if (partial == NULL)
275 return FAIL;
276
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200278 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200280 if (call_def_function(partial->pt_func, argc, argv,
281 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 return FAIL;
283 }
284 else
285 {
286 s = partial_name(partial);
287 if (s == NULL || *s == NUL)
288 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200289 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 funcexe.evaluate = TRUE;
291 funcexe.partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 }
296 else
297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100298 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 if (s == NULL)
300 return FAIL;
301 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200302 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200304 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200306 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100307 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 return FAIL;
309 }
310 }
311 return OK;
312}
313
314/*
315 * Like eval_to_bool() but using a typval_T instead of a string.
316 * Works for string, funcref and partial.
317 */
318 int
319eval_expr_to_bool(typval_T *expr, int *error)
320{
321 typval_T rettv;
322 int res;
323
324 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
325 {
326 *error = TRUE;
327 return FALSE;
328 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200329 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 clear_tv(&rettv);
331 return res;
332}
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
335 * Top level evaluation function, returning a string. If "skip" is TRUE,
336 * only parsing to "nextcmd" is done, without reporting errors. Return
337 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
338 */
339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340eval_to_string_skip(
341 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200342 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100343 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
Bram Moolenaar33570922005-01-25 22:26:29 +0000345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar37c83712020-06-30 21:18:36 +0200349 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (skip)
351 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200352 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 retval = NULL;
354 else
355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100356 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (skip)
360 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200361 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 return retval;
364}
365
366/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367 * Skip over an expression at "*pp".
368 * Return FAIL for an error, OK otherwise.
369 */
370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100371skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372{
Bram Moolenaar33570922005-01-25 22:26:29 +0000373 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374
375 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200376 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000377}
378
379/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Skip over an expression at "*pp".
381 * If in Vim9 script and line breaks are encountered, the lines are
382 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383 * "arg" is advanced to just after the expression.
384 * "start" is set to the start of the expression, "end" to just after the end.
385 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200386 * Return FAIL for an error, OK otherwise.
387 */
388 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200389skip_expr_concatenate(
390 char_u **arg,
391 char_u **start,
392 char_u **end,
393 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394{
395 typval_T rettv;
396 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200397 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200398 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 int evaluate = evalarg == NULL
401 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200403 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 {
406 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 ++gap->ga_len;
410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200422 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
427 // just one line, no need to concatenate
428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
438 p = ga_concat_strings(gap, "");
439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
454 ga_clear(gap);
455 gap->ga_itemsize = 0;
456 if (p == NULL)
457 return FAIL;
458 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 vim_free(evalarg->eval_tofree_lambda);
460 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 // Compute "end" relative to the end.
462 *end = *start + STRLEN(*start) - endoff;
463 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
465
466 return res;
467}
468
469/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200470 * Top level evaluation function, returning a string. Does not handle line
471 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Return pointer to allocated memory, or NULL for failure.
475 */
476 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100477eval_to_string(
478 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100479 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
Bram Moolenaar33570922005-01-25 22:26:29 +0000481 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000483 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000484#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200488 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 retval = NULL;
490 else
491 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000492 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000493 {
494 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000495 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200496 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200497 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200498 if (tv.vval.v_list->lv_len > 0)
499 ga_append(&ga, NL);
500 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000501 ga_append(&ga, NUL);
502 retval = (char_u *)ga.ga_data;
503 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000504#ifdef FEAT_FLOAT
505 else if (convert && tv.v_type == VAR_FLOAT)
506 {
507 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
508 retval = vim_strsave(numbuf);
509 }
510#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000511 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100512 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000513 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200515 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
517 return retval;
518}
519
520/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000521 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200522 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200523 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 */
525 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100526eval_to_string_safe(
527 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100528 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
530 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200531 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200532 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200534 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200535 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000536 if (use_sandbox)
537 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200538 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200539 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000540 if (use_sandbox)
541 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200542 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200543 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200544 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 return retval;
546}
547
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548/*
549 * Top level evaluation function, returning a number.
550 * Evaluates "expr" silently.
551 * Returns -1 for an error.
552 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200553 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
Bram Moolenaar33570922005-01-25 22:26:29 +0000556 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200557 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000558 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560 ++emsg_off;
561
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200562 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 retval = -1;
564 else
565 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100566 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000567 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
569 --emsg_off;
570
571 return retval;
572}
573
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000574/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000575 * Top level evaluation function.
576 * Returns an allocated typval_T with the result.
577 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000578 */
579 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200580eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000581{
582 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200583 evalarg_T evalarg;
584
585 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000586
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200587 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200588 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100589 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000590
Bram Moolenaar37c83712020-06-30 21:18:36 +0200591 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000592 return tv;
593}
594
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100596 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200597 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
598 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000599 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200601 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100602call_vim_function(
603 char_u *func,
604 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200605 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200606 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000608 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200609 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100611 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200612 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200613 funcexe.firstline = curwin->w_cursor.lnum;
614 funcexe.lastline = curwin->w_cursor.lnum;
615 funcexe.evaluate = TRUE;
616 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000617 if (ret == FAIL)
618 clear_tv(rettv);
619
620 return ret;
621}
622
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100623/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100624 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100625 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200626 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
627 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100628 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200629 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630call_func_retnr(
631 char_u *func,
632 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200633 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100634{
635 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200636 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100637
Bram Moolenaarded27a12018-08-01 19:06:03 +0200638 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100639 return -1;
640
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100641 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100642 clear_tv(&rettv);
643 return retval;
644}
645
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000646/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100647 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000648 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200649 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
650 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000651 */
652 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653call_func_retstr(
654 char_u *func,
655 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200656 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000657{
658 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000659 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000660
Bram Moolenaarded27a12018-08-01 19:06:03 +0200661 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000662 return NULL;
663
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100664 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000665 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 return retval;
667}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000668
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000669/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100670 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200671 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
672 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000673 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000674 */
675 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100676call_func_retlist(
677 char_u *func,
678 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200679 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000680{
681 typval_T rettv;
682
Bram Moolenaarded27a12018-08-01 19:06:03 +0200683 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000684 return NULL;
685
686 if (rettv.v_type != VAR_LIST)
687 {
688 clear_tv(&rettv);
689 return NULL;
690 }
691
692 return rettv.vval.v_list;
693}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#ifdef FEAT_FOLDING
696/*
697 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
698 * it in "*cp". Doesn't give error messages.
699 */
700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100701eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200704 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000706 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
707 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
709 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000710 if (use_sandbox)
711 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200712 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200714 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 retval = 0;
716 else
717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100718 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000719 if (tv.v_type == VAR_NUMBER)
720 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000721 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 retval = 0;
723 else
724 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100725 // If the result is a string, check if there is a non-digit before
726 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000727 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (!VIM_ISDIGIT(*s) && *s != '-')
729 *cp = *s++;
730 retval = atol((char *)s);
731 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000732 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000735 if (use_sandbox)
736 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200737 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200738 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200740 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741}
742#endif
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000745 * Get an lval: variable, Dict item or List item that can be assigned a value
746 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
747 * "name.key", "name.key[expr]" etc.
748 * Indexing only works if "name" is an existing List or Dictionary.
749 * "name" points to the start of the name.
750 * If "rettv" is not NULL it points to the value to be assigned.
751 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
752 * wrong; must end in space or cmd separator.
753 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100754 * flags:
755 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100756 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100757 * GLV_NO_AUTOLOAD: do not use script autoloading
758 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000759 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000760 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000761 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000762 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200763 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100764get_lval(
765 char_u *name,
766 typval_T *rettv,
767 lval_T *lp,
768 int unlet,
769 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 int flags, // GLV_ values
771 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000774 char_u *expr_start, *expr_end;
775 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000776 dictitem_T *v;
777 typval_T var1;
778 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000779 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000780 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000781 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000782 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100784 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100786 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200787 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788
789 if (skip)
790 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100791 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000792 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200793 lp->ll_name_end = find_name_end(name, NULL, NULL,
794 FNE_INCL_BR | fne_flags);
795 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000796 }
797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100798 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000799 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 if (expr_start != NULL)
802 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100803 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100804 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 && *p != '[' && *p != '.')
806 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200807 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 return NULL;
809 }
810
811 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
812 if (lp->ll_exp_name == NULL)
813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // Report an invalid expression in braces, unless the
815 // expression evaluation has been cancelled due to an
816 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 if (!aborting() && !quiet)
818 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000819 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 return NULL;
822 }
823 }
824 lp->ll_name = lp->ll_exp_name;
825 }
826 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 lp->ll_name = name;
829
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200830 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200832 // "a: type" is declaring variable "a" with a type, not "a:".
833 if (p == name + 2 && p[-1] == ':')
834 {
835 --p;
836 lp->ll_name_end = p;
837 }
838 if (*p == ':')
839 {
840 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
841 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200843 // parse the type after the name
844 lp->ll_type = parse_type(&tp, &si->sn_type_list);
845 lp->ll_name_end = tp;
846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 }
848 }
849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
852 return p;
853
854 cc = *p;
855 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100856 // Only pass &ht when we would write to the variable, it prevents autoload
857 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100858 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
859 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200861 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 if (v == NULL)
864 return NULL;
865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 /*
867 * Loop until no more [idx] or .key is following.
868 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100870 var1.v_type = VAR_UNKNOWN;
871 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200875 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100876 && !(lp->ll_tv->v_type == VAR_BLOB
877 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000885 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100886 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000889
Bram Moolenaar8c711452005-01-14 21:53:12 +0000890 len = -1;
891 if (*p == '.')
892 {
893 key = p + 1;
894 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
895 ;
896 if (len == 0)
897 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100899 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000901 }
902 p = key + len;
903 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000904 else
905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100906 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000907 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000908 if (*p == ':')
909 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000910 else
911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200913 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000914 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100915 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000916 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100917 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000918 clear_tv(&var1);
919 return NULL;
920 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200921 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000922 }
923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100924 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000925 if (*p == ':')
926 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000928 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200930 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100931 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000932 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000933 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100934 if (rettv != NULL
935 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100936 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100937 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100938 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100941 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100942 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000944 }
945 p = skipwhite(p + 1);
946 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000947 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000948 else
949 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200951 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200952 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000953 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100954 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000956 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100957 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100959 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100960 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000961 clear_tv(&var2);
962 return NULL;
963 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000966 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000967 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000968 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000969
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000971 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100973 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100974 clear_tv(&var1);
975 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 }
978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100979 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000980 ++p;
981 }
982
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000983 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000984 {
985 if (len == -1)
986 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100987 // "[key]": get key from "var1"
988 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200989 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 }
994 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000995 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200996
997 // a NULL dict is equivalent with an empty dict
998 if (lp->ll_tv->vval.v_dict == NULL)
999 {
1000 lp->ll_tv->vval.v_dict = dict_alloc();
1001 if (lp->ll_tv->vval.v_dict == NULL)
1002 {
1003 clear_tv(&var1);
1004 return NULL;
1005 }
1006 ++lp->ll_tv->vval.v_dict->dv_refcount;
1007 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001008 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001009
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001010 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001012 // When assigning to a scope dictionary check that a function and
1013 // variable name is valid (only variable name unless it is l: or
1014 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001015 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001016 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001017 int prevval;
1018 int wrong;
1019
1020 if (len != -1)
1021 {
1022 prevval = key[len];
1023 key[len] = NUL;
1024 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001025 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001026 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001027 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1028 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001029 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001030 || !valid_varname(key);
1031 if (len != -1)
1032 key[len] = prevval;
1033 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001034 {
1035 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001036 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001037 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001038 }
1039
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001041 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001042 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001043 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001044 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001046 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001047 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001048 return NULL;
1049 }
1050
Bram Moolenaar31b81602019-02-10 22:14:27 +01001051 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001053 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001055 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001056 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001058 }
1059 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001061 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001063 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 p = NULL;
1066 break;
1067 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001068 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001069 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001070 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1071 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001072 {
1073 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001074 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001075 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001076
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001077 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001079 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001080 else if (lp->ll_tv->v_type == VAR_BLOB)
1081 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001082 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1083
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001084 /*
1085 * Get the number and item for the only or first index of the List.
1086 */
1087 if (empty1)
1088 lp->ll_n1 = 0;
1089 else
1090 // is number or string
1091 lp->ll_n1 = (long)tv_get_number(&var1);
1092 clear_tv(&var1);
1093
1094 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001095 || lp->ll_n1 > bloblen
1096 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001097 {
1098 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001099 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001100 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001101 return NULL;
1102 }
1103 if (lp->ll_range && !lp->ll_empty2)
1104 {
1105 lp->ll_n2 = (long)tv_get_number(&var2);
1106 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001107 if (lp->ll_n2 < 0
1108 || lp->ll_n2 >= bloblen
1109 || lp->ll_n2 < lp->ll_n1)
1110 {
1111 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001112 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001113 return NULL;
1114 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001115 }
1116 lp->ll_blob = lp->ll_tv->vval.v_blob;
1117 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001118 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001119 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001120 else
1121 {
1122 /*
1123 * Get the number and item for the only or first index of the List.
1124 */
1125 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001127 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001128 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001129 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001130 clear_tv(&var1);
1131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001132 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 lp->ll_list = lp->ll_tv->vval.v_list;
1134 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1135 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001136 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001137 if (lp->ll_n1 < 0)
1138 {
1139 lp->ll_n1 = 0;
1140 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1141 }
1142 }
1143 if (lp->ll_li == NULL)
1144 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001146 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001147 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 }
1150
1151 /*
1152 * May need to find the item or absolute index for the second
1153 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 * When no index given: "lp->ll_empty2" is TRUE.
1155 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001156 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001157 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001158 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001159 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001160 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001165 if (ni == NULL)
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 Moolenaar7480b5c2005-01-16 22:07:38 +00001171 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001172 }
1173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001174 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 if (lp->ll_n1 < 0)
1176 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1177 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001178 {
1179 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001180 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001182 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001183 }
1184
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001185 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187 }
1188
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001189 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191 return p;
1192}
1193
1194/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001195 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001196 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001198clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199{
1200 vim_free(lp->ll_exp_name);
1201 vim_free(lp->ll_newkey);
1202}
1203
1204/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001205 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001207 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1208 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001211set_var_lval(
1212 lval_T *lp,
1213 char_u *endp,
1214 typval_T *rettv,
1215 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001216 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001217 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218{
1219 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 listitem_T *ri;
1221 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222
1223 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001225 cc = *endp;
1226 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001227 if (lp->ll_blob != NULL)
1228 {
1229 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001230
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001231 if (op != NULL && *op != '=')
1232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001233 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001234 return;
1235 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001236 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001237 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001238
1239 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1240 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001241 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001242
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001243 if (lp->ll_empty2)
1244 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1245
1246 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001248 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001249 return;
1250 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001251 if (lp->ll_empty2)
1252 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001253
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001254 ir = 0;
1255 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1256 blob_set(lp->ll_blob, il,
1257 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001258 }
1259 else
1260 {
1261 val = (int)tv_get_number_chk(rettv, &error);
1262 if (!error)
1263 {
1264 garray_T *gap = &lp->ll_blob->bv_ga;
1265
1266 // Allow for appending a byte. Setting a byte beyond
1267 // the end is an error otherwise.
1268 if (lp->ll_n1 < gap->ga_len
1269 || (lp->ll_n1 == gap->ga_len
1270 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1271 {
1272 blob_set(lp->ll_blob, lp->ll_n1, val);
1273 if (lp->ll_n1 == gap->ga_len)
1274 ++gap->ga_len;
1275 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001276 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001277 }
1278 }
1279 }
1280 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001282 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001284 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001285 {
1286 emsg(_(e_cannot_mod));
1287 *endp = cc;
1288 return;
1289 }
1290
Bram Moolenaarff697e62019-02-12 22:28:33 +01001291 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001292 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001293 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001294 &tv, &di, TRUE, FALSE) == OK)
1295 {
1296 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001297 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1298 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001299 && tv_op(&tv, rettv, op) == OK)
1300 set_var(lp->ll_name, &tv, FALSE);
1301 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001304 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001305 {
1306 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001307 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001308 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001310 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001311 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001313 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001314 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001315 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001316 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 else if (lp->ll_range)
1318 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001319 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001320 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001321
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001322 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001323 {
1324 emsg(_("E996: Cannot lock a range"));
1325 return;
1326 }
1327
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001328 /*
1329 * Check whether any of the list items is locked
1330 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001331 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001332 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001333 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001334 return;
1335 ri = ri->li_next;
1336 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1337 break;
1338 ll_li = ll_li->li_next;
1339 ++ll_n1;
1340 }
1341
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 /*
1343 * Assign the List values to the list items.
1344 */
1345 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001346 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001347 if (op != NULL && *op != '=')
1348 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1349 else
1350 {
1351 clear_tv(&lp->ll_li->li_tv);
1352 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1353 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001354 ri = ri->li_next;
1355 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1356 break;
1357 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001359 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001360 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001361 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001362 ri = NULL;
1363 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001364 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001365 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 lp->ll_li = lp->ll_li->li_next;
1367 ++lp->ll_n1;
1368 }
1369 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001370 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001371 else if (lp->ll_empty2
1372 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001374 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375 }
1376 else
1377 {
1378 /*
1379 * Assign to a List or Dictionary item.
1380 */
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001381 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001382 {
1383 emsg(_("E996: Cannot lock a list or dict"));
1384 return;
1385 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001386 if (lp->ll_newkey != NULL)
1387 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001388 if (op != NULL && *op != '=')
1389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001390 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001391 return;
1392 }
1393
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001394 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001395 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 if (di == NULL)
1397 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001398 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1399 {
1400 vim_free(di);
1401 return;
1402 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001404 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 else if (op != NULL && *op != '=')
1406 {
1407 tv_op(lp->ll_tv, rettv, op);
1408 return;
1409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001410 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001411 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001412
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 /*
1414 * Assign the value to the variable or list item.
1415 */
1416 if (copy)
1417 copy_tv(rettv, lp->ll_tv);
1418 else
1419 {
1420 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001421 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 }
1424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425}
1426
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001427/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001428 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1429 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001430 * Returns OK or FAIL.
1431 */
1432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001433tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001435 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001436 char_u numbuf[NUMBUFLEN];
1437 char_u *s;
1438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001439 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001440 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001441 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001442 {
1443 switch (tv1->v_type)
1444 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001445 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001446 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448 case VAR_DICT:
1449 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001450 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001451 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001452 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001453 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001454 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001455 break;
1456
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001457 case VAR_BLOB:
1458 if (*op != '+' || tv2->v_type != VAR_BLOB)
1459 break;
1460 // BLOB += BLOB
1461 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1462 {
1463 blob_T *b1 = tv1->vval.v_blob;
1464 blob_T *b2 = tv2->vval.v_blob;
1465 int i, len = blob_len(b2);
1466 for (i = 0; i < len; i++)
1467 ga_append(&b1->bv_ga, blob_get(b2, i));
1468 }
1469 return OK;
1470
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001471 case VAR_LIST:
1472 if (*op != '+' || tv2->v_type != VAR_LIST)
1473 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001474 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001475 if (tv2->vval.v_list != NULL)
1476 {
1477 if (tv1->vval.v_list == NULL)
1478 {
1479 tv1->vval.v_list = tv2->vval.v_list;
1480 ++tv1->vval.v_list->lv_refcount;
1481 }
1482 else
1483 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1484 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001485 return OK;
1486
1487 case VAR_NUMBER:
1488 case VAR_STRING:
1489 if (tv2->v_type == VAR_LIST)
1490 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001491 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001493 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001494 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001495#ifdef FEAT_FLOAT
1496 if (tv2->v_type == VAR_FLOAT)
1497 {
1498 float_T f = n;
1499
Bram Moolenaarff697e62019-02-12 22:28:33 +01001500 if (*op == '%')
1501 break;
1502 switch (*op)
1503 {
1504 case '+': f += tv2->vval.v_float; break;
1505 case '-': f -= tv2->vval.v_float; break;
1506 case '*': f *= tv2->vval.v_float; break;
1507 case '/': f /= tv2->vval.v_float; break;
1508 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001509 clear_tv(tv1);
1510 tv1->v_type = VAR_FLOAT;
1511 tv1->vval.v_float = f;
1512 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001514#endif
1515 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001516 switch (*op)
1517 {
1518 case '+': n += tv_get_number(tv2); break;
1519 case '-': n -= tv_get_number(tv2); break;
1520 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001521 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1522 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001523 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001524 clear_tv(tv1);
1525 tv1->v_type = VAR_NUMBER;
1526 tv1->vval.v_number = n;
1527 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528 }
1529 else
1530 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001531 if (tv2->v_type == VAR_FLOAT)
1532 break;
1533
Bram Moolenaarff697e62019-02-12 22:28:33 +01001534 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001535 s = tv_get_string(tv1);
1536 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 clear_tv(tv1);
1538 tv1->v_type = VAR_STRING;
1539 tv1->vval.v_string = s;
1540 }
1541 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001542
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001543 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001544#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001545 {
1546 float_T f;
1547
Bram Moolenaarff697e62019-02-12 22:28:33 +01001548 if (*op == '%' || *op == '.'
1549 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001550 && tv2->v_type != VAR_NUMBER
1551 && tv2->v_type != VAR_STRING))
1552 break;
1553 if (tv2->v_type == VAR_FLOAT)
1554 f = tv2->vval.v_float;
1555 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001556 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001557 switch (*op)
1558 {
1559 case '+': tv1->vval.v_float += f; break;
1560 case '-': tv1->vval.v_float -= f; break;
1561 case '*': tv1->vval.v_float *= f; break;
1562 case '/': tv1->vval.v_float /= f; break;
1563 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001564 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001565#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001566 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001567 }
1568 }
1569
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001570 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571 return FAIL;
1572}
1573
1574/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001575 * Evaluate the expression used in a ":for var in expr" command.
1576 * "arg" points to "var".
1577 * Set "*errp" to TRUE for an error, FALSE otherwise;
1578 * Return a pointer that holds the info. Null when there is an error.
1579 */
1580 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001581eval_for_line(
1582 char_u *arg,
1583 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001584 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001585 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001586{
Bram Moolenaar33570922005-01-25 22:26:29 +00001587 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001588 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 typval_T tv;
1590 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001591 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001593 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001594
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001595 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001596 if (fi == NULL)
1597 return NULL;
1598
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001599 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001600 if (expr == NULL)
1601 return fi;
1602
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001603 expr = skipwhite_and_linebreak(expr, evalarg);
1604 if (expr[0] != 'i' || expr[1] != 'n'
1605 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001608 return fi;
1609 }
1610
1611 if (skip)
1612 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001613 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1614 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001615 {
1616 *errp = FALSE;
1617 if (!skip)
1618 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001619 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001620 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001621 l = tv.vval.v_list;
1622 if (l == NULL)
1623 {
1624 // a null list is like an empty list: do nothing
1625 clear_tv(&tv);
1626 }
1627 else
1628 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001630 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001632 // No need to increment the refcount, it's already set for
1633 // the list being used in "tv".
1634 fi->fi_list = l;
1635 list_add_watch(l, &fi->fi_lw);
1636 fi->fi_lw.lw_item = l->lv_first;
1637 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001638 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001639 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001640 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001641 fi->fi_bi = 0;
1642 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001643 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001644 typval_T btv;
1645
1646 // Make a copy, so that the iteration still works when the
1647 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001649 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001650 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001651 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001652 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 else
1654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001655 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001656 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 }
1658 }
1659 }
1660 if (skip)
1661 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001662 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663
1664 return fi;
1665}
1666
1667/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001668 * Used when looping over a :for line, skip the "in expr" part.
1669 */
1670 void
1671skip_for_lines(void *fi_void, evalarg_T *evalarg)
1672{
1673 forinfo_T *fi = (forinfo_T *)fi_void;
1674 int i;
1675
1676 for (i = 0; i < fi->fi_break_count; ++i)
1677 eval_next_line(evalarg);
1678}
1679
1680/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 * Use the first item in a ":for" list. Advance to the next.
1682 * Assign the values to the variable (list). "arg" points to the first one.
1683 * Return TRUE when a valid item was found, FALSE when at end of list or
1684 * something wrong.
1685 */
1686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001687next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001689 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 int result;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001691 int flag = in_vim9script() ? ASSIGN_NO_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001692 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001694 if (fi->fi_blob != NULL)
1695 {
1696 typval_T tv;
1697
1698 if (fi->fi_bi >= blob_len(fi->fi_blob))
1699 return FALSE;
1700 tv.v_type = VAR_NUMBER;
1701 tv.v_lock = VAR_FIXED;
1702 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1703 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001704 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001705 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001706 }
1707
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 item = fi->fi_lw.lw_item;
1709 if (item == NULL)
1710 result = FALSE;
1711 else
1712 {
1713 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001714 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001715 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 }
1717 return result;
1718}
1719
1720/*
1721 * Free the structure used to store info used by ":for".
1722 */
1723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001724free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725{
Bram Moolenaar33570922005-01-25 22:26:29 +00001726 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001728 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001729 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001731 list_unref(fi->fi_list);
1732 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001733 if (fi != NULL && fi->fi_blob != NULL)
1734 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735 vim_free(fi);
1736}
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001739set_context_for_expression(
1740 expand_T *xp,
1741 char_u *arg,
1742 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001744 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001748 if (cmdidx == CMD_let || cmdidx == CMD_var
1749 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 {
1751 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001752 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001754 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001755 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 {
1757 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001758 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001759 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 break;
1761 }
1762 return;
1763 }
1764 }
1765 else
1766 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1767 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 while ((xp->xp_pattern = vim_strpbrk(arg,
1769 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1770 {
1771 c = *xp->xp_pattern;
1772 if (c == '&')
1773 {
1774 c = xp->xp_pattern[1];
1775 if (c == '&')
1776 {
1777 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001778 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 }
1780 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001783 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1784 xp->xp_pattern += 2;
1785
1786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788 else if (c == '$')
1789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001790 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 xp->xp_context = EXPAND_ENV_VARS;
1792 }
1793 else if (c == '=')
1794 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001795 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 xp->xp_context = EXPAND_EXPRESSION;
1797 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001798 else if (c == '#'
1799 && xp->xp_context == EXPAND_EXPRESSION)
1800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001801 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001802 break;
1803 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001804 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 && xp->xp_context == EXPAND_FUNCTIONS
1806 && vim_strchr(xp->xp_pattern, '(') == NULL)
1807 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001808 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 break;
1810 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001811 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001813 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 {
1815 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1816 if (c == '\\' && xp->xp_pattern[1] != NUL)
1817 ++xp->xp_pattern;
1818 xp->xp_context = EXPAND_NOTHING;
1819 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001820 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001822 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1824 /* skip */ ;
1825 xp->xp_context = EXPAND_NOTHING;
1826 }
1827 else if (c == '|')
1828 {
1829 if (xp->xp_pattern[1] == '|')
1830 {
1831 ++xp->xp_pattern;
1832 xp->xp_context = EXPAND_EXPRESSION;
1833 }
1834 else
1835 xp->xp_context = EXPAND_COMMANDS;
1836 }
1837 else
1838 xp->xp_context = EXPAND_EXPRESSION;
1839 }
1840 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001841 // Doesn't look like something valid, expand as an expression
1842 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 arg = xp->xp_pattern;
1845 if (*arg != NUL)
1846 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1847 /* skip */ ;
1848 }
1849 xp->xp_pattern = arg;
1850}
1851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001853 * Return TRUE if "pat" matches "text".
1854 * Does not use 'cpo' and always uses 'magic'.
1855 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001856 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001857pattern_match(char_u *pat, char_u *text, int ic)
1858{
1859 int matches = FALSE;
1860 char_u *save_cpo;
1861 regmatch_T regmatch;
1862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001863 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001864 save_cpo = p_cpo;
1865 p_cpo = (char_u *)"";
1866 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1867 if (regmatch.regprog != NULL)
1868 {
1869 regmatch.rm_ic = ic;
1870 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1871 vim_regfree(regmatch.regprog);
1872 }
1873 p_cpo = save_cpo;
1874 return matches;
1875}
1876
1877/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001878 * Handle a name followed by "(". Both for just "name(arg)" and for
1879 * "expr->name(arg)".
1880 * Returns OK or FAIL.
1881 */
1882 static int
1883eval_func(
1884 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001885 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001886 char_u *name,
1887 int name_len,
1888 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001889 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001890 typval_T *basetv) // "expr" for "expr->name(arg)"
1891{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001892 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001893 char_u *s = name;
1894 int len = name_len;
1895 partial_T *partial;
1896 int ret = OK;
1897
1898 if (!evaluate)
1899 check_vars(s, len);
1900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001901 // If "s" is the name of a variable of type VAR_FUNC
1902 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001903 s = deref_func_name(s, &len, &partial, !evaluate);
1904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001905 // Need to make a copy, in case evaluating the arguments makes
1906 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001907 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001908 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001909 ret = FAIL;
1910 else
1911 {
1912 funcexe_T funcexe;
1913
1914 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001915 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001916 funcexe.firstline = curwin->w_cursor.lnum;
1917 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001918 funcexe.evaluate = evaluate;
1919 funcexe.partial = partial;
1920 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001921 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001922 }
1923 vim_free(s);
1924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 // If evaluate is FALSE rettv->v_type was not set in
1926 // get_func_tv, but it's needed in handle_subscript() to parse
1927 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001928 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1929 {
1930 rettv->vval.v_string = NULL;
1931 rettv->v_type = VAR_FUNC;
1932 }
1933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001934 // Stop the expression evaluation when immediately
1935 // aborting on error, or when an interrupt occurred or
1936 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001937 if (evaluate && aborting())
1938 {
1939 if (ret == OK)
1940 clear_tv(rettv);
1941 ret = FAIL;
1942 }
1943 return ret;
1944}
1945
1946/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001947 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1948 * comment) and there is a next line, return the next line (skipping blanks)
1949 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001950 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1951 * "arg" must point somewhere inside a line, not at the start.
1952 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001953 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001954eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1955{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001956 char_u *p = skipwhite(arg);
1957
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001958 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001959 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001960 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001961 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001962 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001963 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001964 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001965
1966 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001967 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001968 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001969 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001970
Bram Moolenaar9d489562020-07-30 20:08:50 +02001971 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001972 {
1973 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001974 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001975 }
1976 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001977 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001978}
1979
1980/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001981 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001982 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001983 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001984eval_next_line(evalarg_T *evalarg)
1985{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001986 garray_T *gap = &evalarg->eval_ga;
1987 char_u *line;
1988
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001989 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02001990 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
1991 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001992 else
1993 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001994 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001995 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1996 {
1997 // Going to concatenate the lines after parsing.
1998 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1999 ++gap->ga_len;
2000 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002001 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002002 {
2003 vim_free(evalarg->eval_tofree);
2004 evalarg->eval_tofree = line;
2005 }
2006 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002007}
2008
Bram Moolenaare6b53242020-07-01 17:28:33 +02002009/*
2010 * Call eval_next_non_blank() and get the next line if needed.
2011 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002012 char_u *
2013skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2014{
2015 int getnext;
2016 char_u *p = skipwhite(arg);
2017
Bram Moolenaar962d7212020-07-04 14:15:00 +02002018 if (evalarg == NULL)
2019 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002020 eval_next_non_blank(p, evalarg, &getnext);
2021 if (getnext)
2022 return eval_next_line(evalarg);
2023 return p;
2024}
2025
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002026/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002027 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002028 */
2029 void
2030clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2031{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002032 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002033 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002034 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002035 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002036 if (eap != NULL)
2037 {
2038 // We may need to keep the original command line, e.g. for
2039 // ":let" it has the variable names. But we may also need the
2040 // new one, "nextcmd" points into it. Keep both.
2041 vim_free(eap->cmdline_tofree);
2042 eap->cmdline_tofree = *eap->cmdlinep;
2043 *eap->cmdlinep = evalarg->eval_tofree;
2044 }
2045 else
2046 vim_free(evalarg->eval_tofree);
2047 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002048 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002049
2050 vim_free(evalarg->eval_tofree_lambda);
2051 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002052 }
2053}
2054
2055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2059 */
2060
2061/*
2062 * Handle zero level expression.
2063 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002065 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002066 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 * Return OK or FAIL.
2068 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002070eval0(
2071 char_u *arg,
2072 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002073 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002074 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075{
2076 int ret;
2077 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002078 int did_emsg_before = did_emsg;
2079 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002080 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002083 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002084 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002085
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002086 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
2088 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002089 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 /*
2091 * Report the invalid expression unless the expression evaluation has
2092 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002093 * exception, or we already gave a more specific error.
2094 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002096 if (!aborting()
2097 && did_emsg == did_emsg_before
2098 && called_emsg == called_emsg_before
2099 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 ret = FAIL;
2102 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002103
2104 if (eap != NULL)
2105 eap->nextcmd = check_nextcmd(p);
2106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 return ret;
2108}
2109
2110/*
2111 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002112 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002113 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 *
2115 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002116 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002118 * Note: "rettv.v_lock" is not set.
2119 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 * Return OK or FAIL.
2121 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002122 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002123eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002125 char_u *p;
2126 int getnext;
2127
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002128 CLEAR_POINTER(rettv);
2129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 /*
2131 * Get the first variable.
2132 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 return FAIL;
2135
Bram Moolenaar793648f2020-06-26 21:28:25 +02002136 p = eval_next_non_blank(*arg, evalarg, &getnext);
2137 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002139 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002140 int result;
2141 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002142 evalarg_T *evalarg_used = evalarg;
2143 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002144 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002145 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002146 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002147
2148 if (evalarg == NULL)
2149 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002150 CLEAR_FIELD(local_evalarg);
2151 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002152 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002153 orig_flags = evalarg_used->eval_flags;
2154 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002155
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002156 if (getnext)
2157 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002158 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002159 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002160 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002161 {
2162 error_white_both(p, 1);
2163 clear_tv(rettv);
2164 return FAIL;
2165 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002166 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002167 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002170 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002172 int error = FALSE;
2173
Bram Moolenaar13106602020-10-04 16:06:05 +02002174 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002175 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002176 else if (vim9script)
2177 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002178 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002180 if (error || !op_falsy || !result)
2181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002182 if (error)
2183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185
2186 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002187 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002189 if (op_falsy)
2190 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002191 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002192 {
2193 error_white_both(p, 1);
2194 clear_tv(rettv);
2195 return FAIL;
2196 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002197 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002198 evalarg_used->eval_flags = (op_falsy ? !result : result)
2199 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2200 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002201 {
2202 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002204 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002205 if (!op_falsy || !result)
2206 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002208 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002210 /*
2211 * Check for the ":".
2212 */
2213 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2214 if (*p != ':')
2215 {
2216 emsg(_(e_missing_colon));
2217 if (evaluate && result)
2218 clear_tv(rettv);
2219 evalarg_used->eval_flags = orig_flags;
2220 return FAIL;
2221 }
2222 if (getnext)
2223 *arg = eval_next_line(evalarg_used);
2224 else
2225 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002226 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002227 {
2228 error_white_both(p, 1);
2229 clear_tv(rettv);
2230 evalarg_used->eval_flags = orig_flags;
2231 return FAIL;
2232 }
2233 *arg = p;
2234 }
2235
2236 /*
2237 * Get the third variable. Recursive!
2238 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002239 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002240 {
2241 error_white_both(p, 1);
2242 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002243 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002244 return FAIL;
2245 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002246 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2247 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002248 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002249 if (eval1(arg, &var2, evalarg_used) == FAIL)
2250 {
2251 if (evaluate && result)
2252 clear_tv(rettv);
2253 evalarg_used->eval_flags = orig_flags;
2254 return FAIL;
2255 }
2256 if (evaluate && !result)
2257 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002259
2260 if (evalarg == NULL)
2261 clear_evalarg(&local_evalarg, NULL);
2262 else
2263 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265
2266 return OK;
2267}
2268
2269/*
2270 * Handle first level expression:
2271 * expr2 || expr2 || expr2 logical OR
2272 *
2273 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002274 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 *
2276 * Return OK or FAIL.
2277 */
2278 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002281 char_u *p;
2282 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284 /*
2285 * Get the first variable.
2286 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 return FAIL;
2289
2290 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002291 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002293 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002294 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002296 evalarg_T *evalarg_used = evalarg;
2297 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 int evaluate;
2299 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002300 long result = FALSE;
2301 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002302 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002303 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002304
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002305 if (evalarg == NULL)
2306 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002307 CLEAR_FIELD(local_evalarg);
2308 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002309 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002310 orig_flags = evalarg_used->eval_flags;
2311 evaluate = orig_flags & EVAL_EVALUATE;
2312 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002313 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002314 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002315 result = tv_get_bool_chk(rettv, &error);
2316 else if (tv_get_number_chk(rettv, &error) != 0)
2317 result = TRUE;
2318 clear_tv(rettv);
2319 if (error)
2320 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
2322
2323 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002324 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002326 while (p[0] == '|' && p[1] == '|')
2327 {
2328 if (getnext)
2329 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002330 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002331 {
2332 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2333 {
2334 error_white_both(p, 2);
2335 clear_tv(rettv);
2336 return FAIL;
2337 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002338 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002339 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002340
2341 /*
2342 * Get the second variable.
2343 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002344 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2345 {
2346 error_white_both(p, 2);
2347 clear_tv(rettv);
2348 return FAIL;
2349 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002350 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2351 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002352 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002353 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002355
2356 /*
2357 * Compute the result.
2358 */
2359 if (evaluate && !result)
2360 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002361 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002362 result = tv_get_bool_chk(&var2, &error);
2363 else if (tv_get_number_chk(&var2, &error) != 0)
2364 result = TRUE;
2365 clear_tv(&var2);
2366 if (error)
2367 return FAIL;
2368 }
2369 if (evaluate)
2370 {
2371 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002372 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002373 rettv->v_type = VAR_BOOL;
2374 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002375 }
2376 else
2377 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002378 rettv->v_type = VAR_NUMBER;
2379 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002380 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002381 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002382
2383 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002385
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002386 if (evalarg == NULL)
2387 clear_evalarg(&local_evalarg, NULL);
2388 else
2389 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391
2392 return OK;
2393}
2394
2395/*
2396 * Handle second level expression:
2397 * expr3 && expr3 && expr3 logical AND
2398 *
2399 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002400 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 *
2402 * Return OK or FAIL.
2403 */
2404 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002405eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002407 char_u *p;
2408 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 /*
2411 * Get the first variable.
2412 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002413 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 return FAIL;
2415
2416 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002417 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002419 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002420 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002422 evalarg_T *evalarg_used = evalarg;
2423 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002424 int orig_flags;
2425 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002426 long result = TRUE;
2427 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002428 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002429 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002430
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002431 if (evalarg == NULL)
2432 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 CLEAR_FIELD(local_evalarg);
2434 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002435 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002436 orig_flags = evalarg_used->eval_flags;
2437 evaluate = orig_flags & EVAL_EVALUATE;
2438 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002440 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002441 result = tv_get_bool_chk(rettv, &error);
2442 else if (tv_get_number_chk(rettv, &error) == 0)
2443 result = FALSE;
2444 clear_tv(rettv);
2445 if (error)
2446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448
2449 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002450 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 while (p[0] == '&' && p[1] == '&')
2453 {
2454 if (getnext)
2455 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002456 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002457 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002458 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002459 {
2460 error_white_both(p, 2);
2461 clear_tv(rettv);
2462 return FAIL;
2463 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002464 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002465 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466
2467 /*
2468 * Get the second variable.
2469 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002470 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2471 {
2472 error_white_both(p, 2);
2473 clear_tv(rettv);
2474 return FAIL;
2475 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002476 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2477 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002478 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002479 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002480 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002482
2483 /*
2484 * Compute the result.
2485 */
2486 if (evaluate && result)
2487 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002488 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002489 result = tv_get_bool_chk(&var2, &error);
2490 else if (tv_get_number_chk(&var2, &error) == 0)
2491 result = FALSE;
2492 clear_tv(&var2);
2493 if (error)
2494 return FAIL;
2495 }
2496 if (evaluate)
2497 {
2498 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002499 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002500 rettv->v_type = VAR_BOOL;
2501 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002502 }
2503 else
2504 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002505 rettv->v_type = VAR_NUMBER;
2506 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002507 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002508 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002509
2510 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002512
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513 if (evalarg == NULL)
2514 clear_evalarg(&local_evalarg, NULL);
2515 else
2516 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
2518
2519 return OK;
2520}
2521
2522/*
2523 * Handle third level expression:
2524 * var1 == var2
2525 * var1 =~ var2
2526 * var1 != var2
2527 * var1 !~ var2
2528 * var1 > var2
2529 * var1 >= var2
2530 * var1 < var2
2531 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002532 * var1 is var2
2533 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 *
2535 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002536 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 *
2538 * Return OK or FAIL.
2539 */
2540 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002541eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002544 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002545 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002547 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549 /*
2550 * Get the first variable.
2551 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002552 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 return FAIL;
2554
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002555 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002556 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002559 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002561 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002563 typval_T var2;
2564 int ic;
2565 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002566 int evaluate = evalarg == NULL
2567 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002568
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002569 if (getnext)
2570 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002571 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2572 {
2573 error_white_both(p, len);
2574 clear_tv(rettv);
2575 return FAIL;
2576 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002577
Bram Moolenaar696ba232020-07-29 21:20:41 +02002578 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2579 {
2580 semsg(_(e_invexpr2), p);
2581 clear_tv(rettv);
2582 return FAIL;
2583 }
2584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002585 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (p[len] == '?')
2587 {
2588 ic = TRUE;
2589 ++len;
2590 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002591 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 else if (p[len] == '#')
2593 {
2594 ic = FALSE;
2595 ++len;
2596 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002597 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002599 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
2601 /*
2602 * Get the second variable.
2603 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002604 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2605 {
2606 error_white_both(p, 1);
2607 clear_tv(rettv);
2608 return FAIL;
2609 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002610 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002611 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 return FAIL;
2615 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002616 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002617 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002618 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002619
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002620 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002621 {
2622 ret = FAIL;
2623 clear_tv(rettv);
2624 }
2625 else
2626 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002627 clear_tv(&var2);
2628 return ret;
2629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631
2632 return OK;
2633}
2634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 void
2636eval_addblob(typval_T *tv1, typval_T *tv2)
2637{
2638 blob_T *b1 = tv1->vval.v_blob;
2639 blob_T *b2 = tv2->vval.v_blob;
2640 blob_T *b = blob_alloc();
2641 int i;
2642
2643 if (b != NULL)
2644 {
2645 for (i = 0; i < blob_len(b1); i++)
2646 ga_append(&b->bv_ga, blob_get(b1, i));
2647 for (i = 0; i < blob_len(b2); i++)
2648 ga_append(&b->bv_ga, blob_get(b2, i));
2649
2650 clear_tv(tv1);
2651 rettv_blob_set(tv1, b);
2652 }
2653}
2654
2655 int
2656eval_addlist(typval_T *tv1, typval_T *tv2)
2657{
2658 typval_T var3;
2659
2660 // concatenate Lists
2661 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2662 {
2663 clear_tv(tv1);
2664 clear_tv(tv2);
2665 return FAIL;
2666 }
2667 clear_tv(tv1);
2668 *tv1 = var3;
2669 return OK;
2670}
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672/*
2673 * Handle fourth level expression:
2674 * + number addition
2675 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002676 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002677 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 *
2679 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002680 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 *
2682 * Return OK or FAIL.
2683 */
2684 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 /*
2688 * Get the first variable.
2689 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002690 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 return FAIL;
2692
2693 /*
2694 * Repeat computing, until no '+', '-' or '.' is following.
2695 */
2696 for (;;)
2697 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002698 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002699 int getnext;
2700 char_u *p;
2701 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002702 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002703 int concat;
2704 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002705 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002706
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002707 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002708 p = eval_next_non_blank(*arg, evalarg, &getnext);
2709 op = *p;
2710 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002711 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002713
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002714 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002715 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002716 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002717 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002718 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002719 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002720 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002721 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002722 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002723 clear_tv(rettv);
2724 return FAIL;
2725 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002726 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002727 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002728 if ((op != '+' || (rettv->v_type != VAR_LIST
2729 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002730#ifdef FEAT_FLOAT
2731 && (op == '.' || rettv->v_type != VAR_FLOAT)
2732#endif
2733 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002735 // For "list + ...", an illegal use of the first operand as
2736 // a number cannot be determined before evaluating the 2nd
2737 // operand: if this is also a list, all is ok.
2738 // For "something . ...", "something - ..." or "non-list + ...",
2739 // we know that the first operand needs to be a string or number
2740 // without evaluating the 2nd operand. So check before to avoid
2741 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002742 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 {
2744 clear_tv(rettv);
2745 return FAIL;
2746 }
2747 }
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 /*
2750 * Get the second variable.
2751 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002752 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002753 {
2754 error_white_both(p, oplen);
2755 clear_tv(rettv);
2756 return FAIL;
2757 }
2758 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002759 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002761 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 return FAIL;
2763 }
2764
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002765 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
2767 /*
2768 * Compute the result.
2769 */
2770 if (op == '.')
2771 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002772 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2773 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002774 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002775
Bram Moolenaar2e086612020-08-25 22:37:48 +02002776 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002777 || var2.v_type == VAR_CHANNEL
2778 || var2.v_type == VAR_JOB))
2779 emsg(_(e_inval_string));
2780#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002781 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002782 {
2783 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2784 var2.vval.v_float);
2785 s2 = buf2;
2786 }
2787#endif
2788 else
2789 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002790 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002791 {
2792 clear_tv(rettv);
2793 clear_tv(&var2);
2794 return FAIL;
2795 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002796 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002797 clear_tv(rettv);
2798 rettv->v_type = VAR_STRING;
2799 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002801 else if (op == '+' && rettv->v_type == VAR_BLOB
2802 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002804 else if (op == '+' && rettv->v_type == VAR_LIST
2805 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002806 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002808 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 else
2811 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 int error = FALSE;
2813 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002814#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002815 float_T f1 = 0, f2 = 0;
2816
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002817 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002819 f1 = rettv->vval.v_float;
2820 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002821 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002822 else
2823#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002824 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002825 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002826 if (error)
2827 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002828 // This can only happen for "list + non-list". For
2829 // "non-list + ..." or "something - ...", we returned
2830 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002831 clear_tv(rettv);
2832 return FAIL;
2833 }
2834#ifdef FEAT_FLOAT
2835 if (var2.v_type == VAR_FLOAT)
2836 f1 = n1;
2837#endif
2838 }
2839#ifdef FEAT_FLOAT
2840 if (var2.v_type == VAR_FLOAT)
2841 {
2842 f2 = var2.vval.v_float;
2843 n2 = 0;
2844 }
2845 else
2846#endif
2847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002848 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002849 if (error)
2850 {
2851 clear_tv(rettv);
2852 clear_tv(&var2);
2853 return FAIL;
2854 }
2855#ifdef FEAT_FLOAT
2856 if (rettv->v_type == VAR_FLOAT)
2857 f2 = n2;
2858#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002860 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002861
2862#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002863 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002864 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2865 {
2866 if (op == '+')
2867 f1 = f1 + f2;
2868 else
2869 f1 = f1 - f2;
2870 rettv->v_type = VAR_FLOAT;
2871 rettv->vval.v_float = f1;
2872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002874#endif
2875 {
2876 if (op == '+')
2877 n1 = n1 + n2;
2878 else
2879 n1 = n1 - n2;
2880 rettv->v_type = VAR_NUMBER;
2881 rettv->vval.v_number = n1;
2882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
2886 }
2887 return OK;
2888}
2889
2890/*
2891 * Handle fifth level expression:
2892 * * number multiplication
2893 * / number division
2894 * % number modulo
2895 *
2896 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002897 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 *
2899 * Return OK or FAIL.
2900 */
2901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002902eval6(
2903 char_u **arg,
2904 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002905 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002906 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002908#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002909 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912 /*
2913 * Get the first variable.
2914 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002915 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 return FAIL;
2917
2918 /*
2919 * Repeat computing, until no '*', '/' or '%' is following.
2920 */
2921 for (;;)
2922 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002923 int evaluate;
2924 int getnext;
2925 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002926 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002927 int op;
2928 varnumber_T n1, n2;
2929#ifdef FEAT_FLOAT
2930 float_T f1, f2;
2931#endif
2932 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002933
Bram Moolenaar9d489562020-07-30 20:08:50 +02002934 p = eval_next_non_blank(*arg, evalarg, &getnext);
2935 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002936 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002938
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002939 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002941 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002942 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002943 {
2944 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2945 {
2946 error_white_both(p, 1);
2947 clear_tv(rettv);
2948 return FAIL;
2949 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002950 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002953#ifdef FEAT_FLOAT
2954 f1 = 0;
2955 f2 = 0;
2956#endif
2957 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002960#ifdef FEAT_FLOAT
2961 if (rettv->v_type == VAR_FLOAT)
2962 {
2963 f1 = rettv->vval.v_float;
2964 use_float = TRUE;
2965 n1 = 0;
2966 }
2967 else
2968#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002969 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002970 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002971 if (error)
2972 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 }
2974 else
2975 n1 = 0;
2976
2977 /*
2978 * Get the second variable.
2979 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002980 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2981 {
2982 error_white_both(p, 1);
2983 clear_tv(rettv);
2984 return FAIL;
2985 }
2986 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 return FAIL;
2989
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002990 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002992#ifdef FEAT_FLOAT
2993 if (var2.v_type == VAR_FLOAT)
2994 {
2995 if (!use_float)
2996 {
2997 f1 = n1;
2998 use_float = TRUE;
2999 }
3000 f2 = var2.vval.v_float;
3001 n2 = 0;
3002 }
3003 else
3004#endif
3005 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003006 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003007 clear_tv(&var2);
3008 if (error)
3009 return FAIL;
3010#ifdef FEAT_FLOAT
3011 if (use_float)
3012 f2 = n2;
3013#endif
3014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
3016 /*
3017 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003018 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003020#ifdef FEAT_FLOAT
3021 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003023 if (op == '*')
3024 f1 = f1 * f2;
3025 else if (op == '/')
3026 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003027# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003028 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003029 if (f2 == 0.0)
3030 {
3031 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003032 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003033 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003034 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003035 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003036 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003037 }
3038 else
3039 f1 = f1 / f2;
3040# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003041 // We rely on the floating point library to handle divide
3042 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003043 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003044# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003049 return FAIL;
3050 }
3051 rettv->v_type = VAR_FLOAT;
3052 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057 if (op == '*')
3058 n1 = n1 * n2;
3059 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003060 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003062 n1 = num_modulus(n1, n2);
3063
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003064 rettv->v_type = VAR_NUMBER;
3065 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 }
3068 }
3069
3070 return OK;
3071}
3072
3073/*
3074 * Handle sixth level expression:
3075 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003076 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003077 * "string" string constant
3078 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 * &option-name option value
3080 * @r register contents
3081 * identifier variable value
3082 * function() function call
3083 * $VAR environment variable
3084 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003085 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003087 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003088 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 *
3090 * Also handle:
3091 * ! in front logical NOT
3092 * - in front unary minus
3093 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003094 * trailing [] subscript in String or List
3095 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003096 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 *
3098 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003099 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 *
3101 * Return OK or FAIL.
3102 */
3103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003104eval7(
3105 char_u **arg,
3106 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003107 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003110 int evaluate = evalarg != NULL
3111 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 int len;
3113 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 char_u *start_leader, *end_leader;
3115 int ret = OK;
3116 char_u *alias;
3117
3118 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003120 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003122 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003125 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 */
3127 start_leader = *arg;
3128 while (**arg == '!' || **arg == '-' || **arg == '+')
3129 *arg = skipwhite(*arg + 1);
3130 end_leader = *arg;
3131
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003132 if (**arg == '.' && (!isdigit(*(*arg + 1))
3133#ifdef FEAT_FLOAT
3134 || current_sctx.sc_version < 2
3135#endif
3136 ))
3137 {
3138 semsg(_(e_invexpr2), *arg);
3139 ++*arg;
3140 return FAIL;
3141 }
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 switch (**arg)
3144 {
3145 /*
3146 * Number constant.
3147 */
3148 case '0':
3149 case '1':
3150 case '2':
3151 case '3':
3152 case '4':
3153 case '5':
3154 case '6':
3155 case '7':
3156 case '8':
3157 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003158 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003159
3160 // Apply prefixed "-" and "+" now. Matters especially when
3161 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003162 if (ret == OK && evaluate && end_leader > start_leader
3163 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003164 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 break;
3166
3167 /*
3168 * String constant: "string".
3169 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003170 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 break;
3172
3173 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003174 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003176 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003177 break;
3178
3179 /*
3180 * List: [expr, expr]
3181 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003182 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 break;
3184
3185 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003186 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003187 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003188 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003189 {
3190 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003191 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003192 }
3193 else
3194 ret = NOTDONE;
3195 break;
3196
3197 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003198 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003199 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003200 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003201 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003202 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003203 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003204 break;
3205
3206 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003207 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003209 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 break;
3211
3212 /*
3213 * Environment variable: $VAR.
3214 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003215 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 break;
3217
3218 /*
3219 * Register contents: @r.
3220 */
3221 case '@': ++*arg;
3222 if (evaluate)
3223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003224 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003225 rettv->vval.v_string = get_reg_contents(**arg,
3226 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228 if (**arg != NUL)
3229 ++*arg;
3230 break;
3231
3232 /*
3233 * nested expression: (expression).
3234 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003235 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003236 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003237 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003238
Bram Moolenaar9215f012020-06-27 21:18:00 +02003239 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003240 if (**arg == ')')
3241 ++*arg;
3242 else if (ret == OK)
3243 {
3244 emsg(_(e_missing_close));
3245 clear_tv(rettv);
3246 ret = FAIL;
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249 break;
3250
Bram Moolenaar8c711452005-01-14 21:53:12 +00003251 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 break;
3253 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003254
3255 if (ret == NOTDONE)
3256 {
3257 /*
3258 * Must be a variable or function name.
3259 * Can also be a curly-braces kind of name: {expr}.
3260 */
3261 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003262 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263 if (alias != NULL)
3264 s = alias;
3265
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003266 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267 ret = FAIL;
3268 else
3269 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003270 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3271
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003272 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3273 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003274 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003275 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003276 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003277 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003278 else if (flags & EVAL_CONSTANT)
3279 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003280 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003281 {
3282 // get the value of "true", "false" or a variable
3283 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3284 {
3285 rettv->v_type = VAR_BOOL;
3286 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003287 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003288 }
3289 else if (len == 5 && in_vim9script()
3290 && STRNCMP(s, "false", 4) == 0)
3291 {
3292 rettv->v_type = VAR_BOOL;
3293 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003294 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003295 }
3296 else
3297 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3298 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003299 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003300 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003301 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003302 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003303 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003304 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003305 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003306 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003307 }
3308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003309 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3310 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003311 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003312 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
3314 /*
3315 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3316 */
3317 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003318 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003319 return ret;
3320}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003321
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003322/*
3323 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003324 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003325 * Adjusts "end_leaderp" until it is at "start_leader".
3326 */
3327 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003328eval7_leader(
3329 typval_T *rettv,
3330 int numeric_only,
3331 char_u *start_leader,
3332 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003333{
3334 char_u *end_leader = *end_leaderp;
3335 int ret = OK;
3336 int error = FALSE;
3337 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003338 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003339#ifdef FEAT_FLOAT
3340 float_T f = 0.0;
3341
3342 if (rettv->v_type == VAR_FLOAT)
3343 f = rettv->vval.v_float;
3344 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003345#endif
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003346 if (in_vim9script() && end_leader[-1] == '!')
3347 val = tv2bool(rettv);
3348 else
3349 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003350 if (error)
3351 {
3352 clear_tv(rettv);
3353 ret = FAIL;
3354 }
3355 else
3356 {
3357 while (end_leader > start_leader)
3358 {
3359 --end_leader;
3360 if (*end_leader == '!')
3361 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003362 if (numeric_only)
3363 {
3364 ++end_leader;
3365 break;
3366 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003367#ifdef FEAT_FLOAT
3368 if (rettv->v_type == VAR_FLOAT)
3369 f = !f;
3370 else
3371#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003372 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003373 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003374 type = VAR_BOOL;
3375 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003376 }
3377 else if (*end_leader == '-')
3378 {
3379#ifdef FEAT_FLOAT
3380 if (rettv->v_type == VAR_FLOAT)
3381 f = -f;
3382 else
3383#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003384 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003385 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003386 type = VAR_NUMBER;
3387 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003388 }
3389 }
3390#ifdef FEAT_FLOAT
3391 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003393 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003394 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003396 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003397#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003398 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003399 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003400 if (in_vim9script())
3401 rettv->v_type = type;
3402 else
3403 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003404 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003407 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 return ret;
3409}
3410
3411/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003412 * Call the function referred to in "rettv".
3413 */
3414 static int
3415call_func_rettv(
3416 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003417 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003418 typval_T *rettv,
3419 int evaluate,
3420 dict_T *selfdict,
3421 typval_T *basetv)
3422{
3423 partial_T *pt = NULL;
3424 funcexe_T funcexe;
3425 typval_T functv;
3426 char_u *s;
3427 int ret;
3428
3429 // need to copy the funcref so that we can clear rettv
3430 if (evaluate)
3431 {
3432 functv = *rettv;
3433 rettv->v_type = VAR_UNKNOWN;
3434
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003435 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003436 if (functv.v_type == VAR_PARTIAL)
3437 {
3438 pt = functv.vval.v_partial;
3439 s = partial_name(pt);
3440 }
3441 else
3442 s = functv.vval.v_string;
3443 }
3444 else
3445 s = (char_u *)"";
3446
Bram Moolenaara80faa82020-04-12 19:37:17 +02003447 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003448 funcexe.firstline = curwin->w_cursor.lnum;
3449 funcexe.lastline = curwin->w_cursor.lnum;
3450 funcexe.evaluate = evaluate;
3451 funcexe.partial = pt;
3452 funcexe.selfdict = selfdict;
3453 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003454 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003456 // Clear the funcref afterwards, so that deleting it while
3457 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003458 if (evaluate)
3459 clear_tv(&functv);
3460
3461 return ret;
3462}
3463
3464/*
3465 * Evaluate "->method()".
3466 * "*arg" points to the '-'.
3467 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3468 */
3469 static int
3470eval_lambda(
3471 char_u **arg,
3472 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003473 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003474 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003475{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003476 int evaluate = evalarg != NULL
3477 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003478 typval_T base = *rettv;
3479 int ret;
3480
3481 // Skip over the ->.
3482 *arg += 2;
3483 rettv->v_type = VAR_UNKNOWN;
3484
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003485 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003486 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003487 return FAIL;
3488 else if (**arg != '(')
3489 {
3490 if (verbose)
3491 {
3492 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003493 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003494 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003496 }
3497 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003498 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003499 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003500 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003501 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003502
3503 // Clear the funcref afterwards, so that deleting it while
3504 // evaluating the arguments is possible (see test55).
3505 if (evaluate)
3506 clear_tv(&base);
3507
3508 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003509}
3510
3511/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003512 * Evaluate "->method()".
3513 * "*arg" points to the '-'.
3514 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3515 */
3516 static int
3517eval_method(
3518 char_u **arg,
3519 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003520 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003521 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003522{
3523 char_u *name;
3524 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003525 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003526 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003527 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003528 int evaluate = evalarg != NULL
3529 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003530
3531 // Skip over the ->.
3532 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003533 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003534
Bram Moolenaarac92e252019-08-03 21:58:38 +02003535 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003536 len = get_name_len(arg, &alias, evaluate, TRUE);
3537 if (alias != NULL)
3538 name = alias;
3539
3540 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003541 {
3542 if (verbose)
3543 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003544 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003545 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003546 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003547 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003548 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003549 if (**arg != '(')
3550 {
3551 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003553 ret = FAIL;
3554 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003555 else if (VIM_ISWHITE((*arg)[-1]))
3556 {
3557 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003558 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003559 ret = FAIL;
3560 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003561 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003562 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003563 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003564 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003565
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003566 // Clear the funcref afterwards, so that deleting it while
3567 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003568 if (evaluate)
3569 clear_tv(&base);
3570
Bram Moolenaarac92e252019-08-03 21:58:38 +02003571 return ret;
3572}
3573
3574/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003575 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3576 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003577 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3578 */
3579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003580eval_index(
3581 char_u **arg,
3582 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003583 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003584 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003585{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003586 int evaluate = evalarg != NULL
3587 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003588 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003589 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003591 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003592 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003593
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003594 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3595 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003597 init_tv(&var1);
3598 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 /*
3602 * dict.name
3603 */
3604 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003605 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003607 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003608 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003609 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003610 }
3611 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003612 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003613 /*
3614 * something[idx]
3615 *
3616 * Get the (first) variable from inside the [].
3617 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003618 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003619 if (**arg == ':')
3620 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003621 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003623 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003624 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003625 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003626 clear_tv(&var1);
3627 return FAIL;
3628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003629
3630 /*
3631 * Get the second variable from inside the [:].
3632 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003633 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003634 if (**arg == ':')
3635 {
3636 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003637 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003638 if (**arg == ']')
3639 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003640 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003642 if (!empty1)
3643 clear_tv(&var1);
3644 return FAIL;
3645 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003646 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003648 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003649 if (!empty1)
3650 clear_tv(&var1);
3651 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003652 return FAIL;
3653 }
3654 }
3655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003656 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003657 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 if (**arg != ']')
3659 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003660 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003661 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 clear_tv(&var1);
3663 if (range)
3664 clear_tv(&var2);
3665 return FAIL;
3666 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003667 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003668 }
3669
3670 if (evaluate)
3671 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003672 int res = eval_index_inner(rettv, range,
3673 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3674 key, keylen, verbose);
3675 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003676 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003677 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003678 clear_tv(&var2);
3679 return res;
3680 }
3681 return OK;
3682}
3683
3684/*
3685 * Check if "rettv" can have an [index] or [sli:ce]
3686 */
3687 int
3688check_can_index(typval_T *rettv, int evaluate, int verbose)
3689{
3690 switch (rettv->v_type)
3691 {
3692 case VAR_FUNC:
3693 case VAR_PARTIAL:
3694 if (verbose)
3695 emsg(_("E695: Cannot index a Funcref"));
3696 return FAIL;
3697 case VAR_FLOAT:
3698#ifdef FEAT_FLOAT
3699 if (verbose)
3700 emsg(_(e_float_as_string));
3701 return FAIL;
3702#endif
3703 case VAR_BOOL:
3704 case VAR_SPECIAL:
3705 case VAR_JOB:
3706 case VAR_CHANNEL:
3707 if (verbose)
3708 emsg(_(e_cannot_index_special_variable));
3709 return FAIL;
3710 case VAR_UNKNOWN:
3711 case VAR_ANY:
3712 case VAR_VOID:
3713 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003714 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003715 emsg(_(e_cannot_index_special_variable));
3716 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003717 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003718 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003719
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003720 case VAR_STRING:
3721 case VAR_LIST:
3722 case VAR_DICT:
3723 case VAR_BLOB:
3724 break;
3725 case VAR_NUMBER:
3726 if (in_vim9script())
3727 emsg(_(e_cannot_index_number));
3728 break;
3729 }
3730 return OK;
3731}
3732
3733/*
3734 * Apply index or range to "rettv".
3735 * "var1" is the first index, NULL for [:expr].
3736 * "var2" is the second index, NULL for [expr] and [expr: ]
3737 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3738 */
3739 int
3740eval_index_inner(
3741 typval_T *rettv,
3742 int is_range,
3743 typval_T *var1,
3744 typval_T *var2,
3745 char_u *key,
3746 int keylen,
3747 int verbose)
3748{
3749 long n1, n2 = 0;
3750 long len;
3751
3752 n1 = 0;
3753 if (var1 != NULL && rettv->v_type != VAR_DICT)
3754 n1 = tv_get_number(var1);
3755
3756 if (is_range)
3757 {
3758 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003759 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003760 if (verbose)
3761 emsg(_(e_cannot_slice_dictionary));
3762 return FAIL;
3763 }
3764 if (var2 == NULL)
3765 n2 = -1;
3766 else
3767 n2 = tv_get_number(var2);
3768 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003769
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003770 switch (rettv->v_type)
3771 {
3772 case VAR_UNKNOWN:
3773 case VAR_ANY:
3774 case VAR_VOID:
3775 case VAR_FUNC:
3776 case VAR_PARTIAL:
3777 case VAR_FLOAT:
3778 case VAR_BOOL:
3779 case VAR_SPECIAL:
3780 case VAR_JOB:
3781 case VAR_CHANNEL:
3782 break; // not evaluating, skipping over subscript
3783
3784 case VAR_NUMBER:
3785 case VAR_STRING:
3786 {
3787 char_u *s = tv_get_string(rettv);
3788
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003789 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003790 if (in_vim9script())
3791 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003792 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003793 s = string_slice(s, n1, n2);
3794 else
3795 s = char_from_string(s, n1);
3796 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003797 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003798 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003799 // The resulting variable is a substring. If the indexes
3800 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003801 if (n1 < 0)
3802 {
3803 n1 = len + n1;
3804 if (n1 < 0)
3805 n1 = 0;
3806 }
3807 if (n2 < 0)
3808 n2 = len + n2;
3809 else if (n2 >= len)
3810 n2 = len;
3811 if (n1 >= len || n2 < 0 || n1 > n2)
3812 s = NULL;
3813 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003814 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003815 }
3816 else
3817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003818 // The resulting variable is a string of a single
3819 // character. If the index is too big or negative the
3820 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003821 if (n1 >= len || n1 < 0)
3822 s = NULL;
3823 else
3824 s = vim_strnsave(s + n1, 1);
3825 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826 clear_tv(rettv);
3827 rettv->v_type = VAR_STRING;
3828 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003829 }
3830 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003831
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003832 case VAR_BLOB:
3833 len = blob_len(rettv->vval.v_blob);
3834 if (is_range)
3835 {
3836 // The resulting variable is a sub-blob. If the indexes
3837 // are out of range the result is empty.
3838 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003839 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003840 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003841 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003842 n1 = 0;
3843 }
3844 if (n2 < 0)
3845 n2 = len + n2;
3846 else if (n2 >= len)
3847 n2 = len - 1;
3848 if (n1 >= len || n2 < 0 || n1 > n2)
3849 {
3850 clear_tv(rettv);
3851 rettv->v_type = VAR_BLOB;
3852 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003853 }
3854 else
3855 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003856 blob_T *blob = blob_alloc();
3857 long i;
3858
3859 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003860 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003861 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003862 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003863 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003864 return FAIL;
3865 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003866 blob->bv_ga.ga_len = n2 - n1 + 1;
3867 for (i = n1; i <= n2; i++)
3868 blob_set(blob, i - n1,
3869 blob_get(rettv->vval.v_blob, i));
3870
3871 clear_tv(rettv);
3872 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003873 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003874 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003875 }
3876 else
3877 {
3878 // The resulting variable is a byte value.
3879 // If the index is too big or negative that is an error.
3880 if (n1 < 0)
3881 n1 = len + n1;
3882 if (n1 < len && n1 >= 0)
3883 {
3884 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003885
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003886 clear_tv(rettv);
3887 rettv->v_type = VAR_NUMBER;
3888 rettv->vval.v_number = v;
3889 }
3890 else
3891 semsg(_(e_blobidx), n1);
3892 }
3893 break;
3894
3895 case VAR_LIST:
3896 if (var1 == NULL)
3897 n1 = 0;
3898 if (var2 == NULL)
3899 n2 = -1;
3900 if (list_slice_or_index(rettv->vval.v_list,
3901 is_range, n1, n2, rettv, verbose) == FAIL)
3902 return FAIL;
3903 break;
3904
3905 case VAR_DICT:
3906 {
3907 dictitem_T *item;
3908 typval_T tmp;
3909
3910 if (key == NULL)
3911 {
3912 key = tv_get_string_chk(var1);
3913 if (key == NULL)
3914 return FAIL;
3915 }
3916
3917 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
3918
3919 if (item == NULL && verbose)
3920 semsg(_(e_dictkey), key);
3921 if (item == NULL)
3922 return FAIL;
3923
3924 copy_tv(&item->di_tv, &tmp);
3925 clear_tv(rettv);
3926 *rettv = tmp;
3927 }
3928 break;
3929 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003930 return OK;
3931}
3932
3933/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003934 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003935 */
3936 char_u *
3937partial_name(partial_T *pt)
3938{
3939 if (pt->pt_name != NULL)
3940 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003941 if (pt->pt_func != NULL)
3942 return pt->pt_func->uf_name;
3943 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003944}
3945
Bram Moolenaarddecc252016-04-06 22:59:37 +02003946 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003947partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003948{
3949 int i;
3950
3951 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003952 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003953 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003954 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003955 if (pt->pt_name != NULL)
3956 {
3957 func_unref(pt->pt_name);
3958 vim_free(pt->pt_name);
3959 }
3960 else
3961 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003962
3963 if (pt->pt_funcstack != NULL)
3964 {
3965 // Decrease the reference count for the context of a closure. If down
3966 // to zero free it and clear the variables on the stack.
3967 if (--pt->pt_funcstack->fs_refcount == 0)
3968 {
3969 garray_T *gap = &pt->pt_funcstack->fs_ga;
3970 typval_T *stack = gap->ga_data;
3971
3972 for (i = 0; i < gap->ga_len; ++i)
3973 clear_tv(stack + i);
3974 ga_clear(gap);
3975 vim_free(pt->pt_funcstack);
3976 }
3977 pt->pt_funcstack = NULL;
3978 }
3979
Bram Moolenaarddecc252016-04-06 22:59:37 +02003980 vim_free(pt);
3981}
3982
3983/*
3984 * Unreference a closure: decrement the reference count and free it when it
3985 * becomes zero.
3986 */
3987 void
3988partial_unref(partial_T *pt)
3989{
3990 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003991 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003992}
3993
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003994/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003995 * Return the next (unique) copy ID.
3996 * Used for serializing nested structures.
3997 */
3998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003999get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004000{
4001 current_copyID += COPYID_INC;
4002 return current_copyID;
4003}
4004
4005/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004006 * Garbage collection for lists and dictionaries.
4007 *
4008 * We use reference counts to be able to free most items right away when they
4009 * are no longer used. But for composite items it's possible that it becomes
4010 * unused while the reference count is > 0: When there is a recursive
4011 * reference. Example:
4012 * :let l = [1, 2, 3]
4013 * :let d = {9: l}
4014 * :let l[1] = d
4015 *
4016 * Since this is quite unusual we handle this with garbage collection: every
4017 * once in a while find out which lists and dicts are not referenced from any
4018 * variable.
4019 *
4020 * Here is a good reference text about garbage collection (refers to Python
4021 * but it applies to all reference-counting mechanisms):
4022 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004023 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004024
4025/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004026 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004027 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004028 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004029 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004030 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004031garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004032{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004033 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004034 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004035 buf_T *buf;
4036 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004037 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004038 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004039
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004040 if (!testing)
4041 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004042 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004043 want_garbage_collect = FALSE;
4044 may_garbage_collect = FALSE;
4045 garbage_collect_at_exit = FALSE;
4046 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004047
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004048 // The execution stack can grow big, limit the size.
4049 if (exestack.ga_maxlen - exestack.ga_len > 500)
4050 {
4051 size_t new_len;
4052 char_u *pp;
4053 int n;
4054
4055 // Keep 150% of the current size, with a minimum of the growth size.
4056 n = exestack.ga_len / 2;
4057 if (n < exestack.ga_growsize)
4058 n = exestack.ga_growsize;
4059
4060 // Don't make it bigger though.
4061 if (exestack.ga_len + n < exestack.ga_maxlen)
4062 {
4063 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4064 pp = vim_realloc(exestack.ga_data, new_len);
4065 if (pp == NULL)
4066 return FAIL;
4067 exestack.ga_maxlen = exestack.ga_len + n;
4068 exestack.ga_data = pp;
4069 }
4070 }
4071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004072 // We advance by two because we add one for items referenced through
4073 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004074 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004075
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004076 /*
4077 * 1. Go through all accessible variables and mark all lists and dicts
4078 * with copyID.
4079 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004080
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004081 // Don't free variables in the previous_funccal list unless they are only
4082 // referenced through previous_funccal. This must be first, because if
4083 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004084 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004086 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004087 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004089 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004090 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004091 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4092 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004094 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004095 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004096 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4097 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004098 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004099 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4100 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004101#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004102 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004103 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4104 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004105 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004106 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004107 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4108 NULL, NULL);
4109#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004110
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004111 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004112 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004113 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4114 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004115 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004116 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004118 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004119 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004121 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004122 abort = abort || set_ref_in_functions(copyID);
4123
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004124 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004125 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004127 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004128 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004129
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004130 // callbacks in buffers
4131 abort = abort || set_ref_in_buffers(copyID);
4132
Bram Moolenaar1dced572012-04-05 16:54:08 +02004133#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004134 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004135#endif
4136
Bram Moolenaardb913952012-06-29 12:54:53 +02004137#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004138 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004139#endif
4140
4141#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004142 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004143#endif
4144
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004145#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004146 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004147 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004148#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004149#ifdef FEAT_NETBEANS_INTG
4150 abort = abort || set_ref_in_nb_channel(copyID);
4151#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004152
Bram Moolenaare3188e22016-05-31 21:13:04 +02004153#ifdef FEAT_TIMERS
4154 abort = abort || set_ref_in_timer(copyID);
4155#endif
4156
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004157#ifdef FEAT_QUICKFIX
4158 abort = abort || set_ref_in_quickfix(copyID);
4159#endif
4160
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004161#ifdef FEAT_TERMINAL
4162 abort = abort || set_ref_in_term(copyID);
4163#endif
4164
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004165#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004166 abort = abort || set_ref_in_popups(copyID);
4167#endif
4168
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004169 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004170 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004171 /*
4172 * 2. Free lists and dictionaries that are not referenced.
4173 */
4174 did_free = free_unref_items(copyID);
4175
4176 /*
4177 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004179 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004181 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004182 else if (p_verbose > 0)
4183 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004184 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004185 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004186
4187 return did_free;
4188}
4189
4190/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004191 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004192 */
4193 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004194free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004195{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004196 int did_free = FALSE;
4197
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004198 // Let all "free" functions know that we are here. This means no
4199 // dictionaries, lists, channels or jobs are to be freed, because we will
4200 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004201 in_free_unref_items = TRUE;
4202
4203 /*
4204 * PASS 1: free the contents of the items. We don't free the items
4205 * themselves yet, so that it is possible to decrement refcount counters
4206 */
4207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004208 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004209 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004211 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004212 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004213
4214#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004215 // Go through the list of jobs and free items without the copyID. This
4216 // must happen before doing channels, because jobs refer to channels, but
4217 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004218 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004220 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004221 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4222#endif
4223
4224 /*
4225 * PASS 2: free the items themselves.
4226 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004227 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004228 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004229
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004230#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004231 // Go through the list of jobs and free items without the copyID. This
4232 // must happen before doing channels, because jobs refer to channels, but
4233 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004234 free_unused_jobs(copyID, COPYID_MASK);
4235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004236 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004237 free_unused_channels(copyID, COPYID_MASK);
4238#endif
4239
4240 in_free_unref_items = FALSE;
4241
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004242 return did_free;
4243}
4244
4245/*
4246 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004247 * "list_stack" is used to add lists to be marked. Can be NULL.
4248 *
4249 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004250 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004252set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004253{
4254 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004255 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004256 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004257 hashtab_T *cur_ht;
4258 ht_stack_T *ht_stack = NULL;
4259 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004260
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004261 cur_ht = ht;
4262 for (;;)
4263 {
4264 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004265 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004266 // Mark each item in the hashtab. If the item contains a hashtab
4267 // it is added to ht_stack, if it contains a list it is added to
4268 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004269 todo = (int)cur_ht->ht_used;
4270 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4271 if (!HASHITEM_EMPTY(hi))
4272 {
4273 --todo;
4274 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4275 &ht_stack, list_stack);
4276 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004277 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004278
4279 if (ht_stack == NULL)
4280 break;
4281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004282 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004283 cur_ht = ht_stack->ht;
4284 tempitem = ht_stack;
4285 ht_stack = ht_stack->prev;
4286 free(tempitem);
4287 }
4288
4289 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004290}
4291
4292/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004293 * Mark a dict and its items with "copyID".
4294 * Returns TRUE if setting references failed somehow.
4295 */
4296 int
4297set_ref_in_dict(dict_T *d, int copyID)
4298{
4299 if (d != NULL && d->dv_copyID != copyID)
4300 {
4301 d->dv_copyID = copyID;
4302 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4303 }
4304 return FALSE;
4305}
4306
4307/*
4308 * Mark a list and its items with "copyID".
4309 * Returns TRUE if setting references failed somehow.
4310 */
4311 int
4312set_ref_in_list(list_T *ll, int copyID)
4313{
4314 if (ll != NULL && ll->lv_copyID != copyID)
4315 {
4316 ll->lv_copyID = copyID;
4317 return set_ref_in_list_items(ll, copyID, NULL);
4318 }
4319 return FALSE;
4320}
4321
4322/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004323 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004324 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4325 *
4326 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004327 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004328 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004329set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004330{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004331 listitem_T *li;
4332 int abort = FALSE;
4333 list_T *cur_l;
4334 list_stack_T *list_stack = NULL;
4335 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004336
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004337 cur_l = l;
4338 for (;;)
4339 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004340 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004341 // Mark each item in the list. If the item contains a hashtab
4342 // it is added to ht_stack, if it contains a list it is added to
4343 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004344 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4345 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4346 ht_stack, &list_stack);
4347 if (list_stack == NULL)
4348 break;
4349
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004350 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004351 cur_l = list_stack->list;
4352 tempitem = list_stack;
4353 list_stack = list_stack->prev;
4354 free(tempitem);
4355 }
4356
4357 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004358}
4359
4360/*
4361 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004362 * "list_stack" is used to add lists to be marked. Can be NULL.
4363 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4364 *
4365 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004366 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004368set_ref_in_item(
4369 typval_T *tv,
4370 int copyID,
4371 ht_stack_T **ht_stack,
4372 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004373{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004374 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004375
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004376 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004377 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004378 dict_T *dd = tv->vval.v_dict;
4379
Bram Moolenaara03f2332016-02-06 18:09:59 +01004380 if (dd != NULL && dd->dv_copyID != copyID)
4381 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004382 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004383 dd->dv_copyID = copyID;
4384 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004385 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004386 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4387 }
4388 else
4389 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004390 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4391
Bram Moolenaara03f2332016-02-06 18:09:59 +01004392 if (newitem == NULL)
4393 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004394 else
4395 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004396 newitem->ht = &dd->dv_hashtab;
4397 newitem->prev = *ht_stack;
4398 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004399 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004400 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004401 }
4402 }
4403 else if (tv->v_type == VAR_LIST)
4404 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004405 list_T *ll = tv->vval.v_list;
4406
Bram Moolenaara03f2332016-02-06 18:09:59 +01004407 if (ll != NULL && ll->lv_copyID != copyID)
4408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004409 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004410 ll->lv_copyID = copyID;
4411 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004412 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004413 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004414 }
4415 else
4416 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004417 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4418
Bram Moolenaara03f2332016-02-06 18:09:59 +01004419 if (newitem == NULL)
4420 abort = TRUE;
4421 else
4422 {
4423 newitem->list = ll;
4424 newitem->prev = *list_stack;
4425 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004426 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004427 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004428 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004429 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004430 else if (tv->v_type == VAR_FUNC)
4431 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004432 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004433 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004434 else if (tv->v_type == VAR_PARTIAL)
4435 {
4436 partial_T *pt = tv->vval.v_partial;
4437 int i;
4438
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004439 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004440 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004441 // Didn't see this partial yet.
4442 pt->pt_copyID = copyID;
4443
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004444 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004445
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004446 if (pt->pt_dict != NULL)
4447 {
4448 typval_T dtv;
4449
4450 dtv.v_type = VAR_DICT;
4451 dtv.vval.v_dict = pt->pt_dict;
4452 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4453 }
4454
4455 for (i = 0; i < pt->pt_argc; ++i)
4456 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4457 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004458 if (pt->pt_funcstack != NULL)
4459 {
4460 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4461
4462 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4463 abort = abort || set_ref_in_item(stack + i, copyID,
4464 ht_stack, list_stack);
4465 }
4466
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004467 }
4468 }
4469#ifdef FEAT_JOB_CHANNEL
4470 else if (tv->v_type == VAR_JOB)
4471 {
4472 job_T *job = tv->vval.v_job;
4473 typval_T dtv;
4474
4475 if (job != NULL && job->jv_copyID != copyID)
4476 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004477 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004478 if (job->jv_channel != NULL)
4479 {
4480 dtv.v_type = VAR_CHANNEL;
4481 dtv.vval.v_channel = job->jv_channel;
4482 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4483 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004484 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004485 {
4486 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004487 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004488 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4489 }
4490 }
4491 }
4492 else if (tv->v_type == VAR_CHANNEL)
4493 {
4494 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004495 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004496 typval_T dtv;
4497 jsonq_T *jq;
4498 cbq_T *cq;
4499
4500 if (ch != NULL && ch->ch_copyID != copyID)
4501 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004502 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004503 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004504 {
4505 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4506 jq = jq->jq_next)
4507 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4508 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4509 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004510 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004511 {
4512 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004513 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004514 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4515 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004516 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004517 {
4518 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004519 dtv.vval.v_partial =
4520 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004521 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4522 }
4523 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004524 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004525 {
4526 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004527 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004528 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4529 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004530 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004531 {
4532 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004533 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004534 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4535 }
4536 }
4537 }
4538#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004539 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004540}
4541
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004543 * Return a string with the string representation of a variable.
4544 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004545 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004546 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004547 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4548 * stings as "string()", otherwise does not put quotes around strings, as
4549 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004550 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4551 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004552 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004553 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004554 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004555echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004556 typval_T *tv,
4557 char_u **tofree,
4558 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004559 int copyID,
4560 int echo_style,
4561 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004562 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004563{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004564 static int recurse = 0;
4565 char_u *r = NULL;
4566
Bram Moolenaar33570922005-01-25 22:26:29 +00004567 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004568 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004569 if (!did_echo_string_emsg)
4570 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004571 // Only give this message once for a recursive call to avoid
4572 // flooding the user with errors. And stop iterating over lists
4573 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004574 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004575 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004576 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004577 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004578 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004579 }
4580 ++recurse;
4581
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 switch (tv->v_type)
4583 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004584 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004585 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004586 {
4587 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004588 r = tv->vval.v_string;
4589 if (r == NULL)
4590 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004591 }
4592 else
4593 {
4594 *tofree = string_quote(tv->vval.v_string, FALSE);
4595 r = *tofree;
4596 }
4597 break;
4598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004600 if (echo_style)
4601 {
4602 *tofree = NULL;
4603 r = tv->vval.v_string;
4604 }
4605 else
4606 {
4607 *tofree = string_quote(tv->vval.v_string, TRUE);
4608 r = *tofree;
4609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004610 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004611
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004612 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004613 {
4614 partial_T *pt = tv->vval.v_partial;
4615 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004616 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004617 garray_T ga;
4618 int i;
4619 char_u *tf;
4620
4621 ga_init2(&ga, 1, 100);
4622 ga_concat(&ga, (char_u *)"function(");
4623 if (fname != NULL)
4624 {
4625 ga_concat(&ga, fname);
4626 vim_free(fname);
4627 }
4628 if (pt != NULL && pt->pt_argc > 0)
4629 {
4630 ga_concat(&ga, (char_u *)", [");
4631 for (i = 0; i < pt->pt_argc; ++i)
4632 {
4633 if (i > 0)
4634 ga_concat(&ga, (char_u *)", ");
4635 ga_concat(&ga,
4636 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4637 vim_free(tf);
4638 }
4639 ga_concat(&ga, (char_u *)"]");
4640 }
4641 if (pt != NULL && pt->pt_dict != NULL)
4642 {
4643 typval_T dtv;
4644
4645 ga_concat(&ga, (char_u *)", ");
4646 dtv.v_type = VAR_DICT;
4647 dtv.vval.v_dict = pt->pt_dict;
4648 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4649 vim_free(tf);
4650 }
4651 ga_concat(&ga, (char_u *)")");
4652
4653 *tofree = ga.ga_data;
4654 r = *tofree;
4655 break;
4656 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004657
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004658 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004659 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004660 break;
4661
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004663 if (tv->vval.v_list == NULL)
4664 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004665 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004666 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004667 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004668 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004669 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4670 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004671 {
4672 *tofree = NULL;
4673 r = (char_u *)"[...]";
4674 }
4675 else
4676 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004677 int old_copyID = tv->vval.v_list->lv_copyID;
4678
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004679 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004680 *tofree = list2string(tv, copyID, restore_copyID);
4681 if (restore_copyID)
4682 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004683 r = *tofree;
4684 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004685 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004686
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004688 if (tv->vval.v_dict == NULL)
4689 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004690 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004691 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004692 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004693 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004694 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4695 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004696 {
4697 *tofree = NULL;
4698 r = (char_u *)"{...}";
4699 }
4700 else
4701 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004702 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004703
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004704 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004705 *tofree = dict2string(tv, copyID, restore_copyID);
4706 if (restore_copyID)
4707 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004708 r = *tofree;
4709 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004710 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004712 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004713 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004714 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004716 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004717 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004718 break;
4719
Bram Moolenaar835dc632016-02-07 14:27:38 +01004720 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004721 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004722 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004723 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004724 if (composite_val)
4725 {
4726 *tofree = string_quote(r, FALSE);
4727 r = *tofree;
4728 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004730
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004732#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004733 *tofree = NULL;
4734 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4735 r = numbuf;
4736 break;
4737#endif
4738
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004739 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004740 case VAR_SPECIAL:
4741 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004742 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004743 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004744 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004745
Bram Moolenaar8502c702014-06-17 12:51:16 +02004746 if (--recurse == 0)
4747 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004748 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004749}
4750
4751/*
4752 * Return a string with the string representation of a variable.
4753 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4754 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004755 * Does not put quotes around strings, as ":echo" displays values.
4756 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4757 * May return NULL.
4758 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004759 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004760echo_string(
4761 typval_T *tv,
4762 char_u **tofree,
4763 char_u *numbuf,
4764 int copyID)
4765{
4766 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4767}
4768
4769/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004770 * Return string "str" in ' quotes, doubling ' characters.
4771 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004772 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004773 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004774 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004775string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004776{
Bram Moolenaar33570922005-01-25 22:26:29 +00004777 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004778 char_u *p, *r, *s;
4779
Bram Moolenaar33570922005-01-25 22:26:29 +00004780 len = (function ? 13 : 3);
4781 if (str != NULL)
4782 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004783 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004784 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004785 if (*p == '\'')
4786 ++len;
4787 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004788 s = r = alloc(len);
4789 if (r != NULL)
4790 {
4791 if (function)
4792 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004794 r += 10;
4795 }
4796 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004797 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004798 if (str != NULL)
4799 for (p = str; *p != NUL; )
4800 {
4801 if (*p == '\'')
4802 *r++ = '\'';
4803 MB_COPY_CHAR(p, r);
4804 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004806 if (function)
4807 *r++ = ')';
4808 *r++ = NUL;
4809 }
4810 return s;
4811}
4812
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004813#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814/*
4815 * Convert the string "text" to a floating point number.
4816 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4817 * this always uses a decimal point.
4818 * Returns the length of the text that was consumed.
4819 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004821string2float(
4822 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004823 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824{
4825 char *s = (char *)text;
4826 float_T f;
4827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004828 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004829 if (STRNICMP(text, "inf", 3) == 0)
4830 {
4831 *value = INFINITY;
4832 return 3;
4833 }
4834 if (STRNICMP(text, "-inf", 3) == 0)
4835 {
4836 *value = -INFINITY;
4837 return 4;
4838 }
4839 if (STRNICMP(text, "nan", 3) == 0)
4840 {
4841 *value = NAN;
4842 return 3;
4843 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844 f = strtod(s, &s);
4845 *value = f;
4846 return (int)((char_u *)s - text);
4847}
4848#endif
4849
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004852 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004854 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004855var2fpos(
4856 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 int dollar_lnum, // TRUE when $ is last line
4858 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004860 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004862 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004864 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004865 if (varp->v_type == VAR_LIST)
4866 {
4867 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004868 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004869 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004870 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004871
4872 l = varp->vval.v_list;
4873 if (l == NULL)
4874 return NULL;
4875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004876 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004877 pos.lnum = list_find_nr(l, 0L, &error);
4878 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004879 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004880 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004881
Bram Moolenaarec65d772020-08-20 22:29:12 +02004882 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004883 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004884 li = list_find(l, 1L);
4885 if (li != NULL && li->li_tv.v_type == VAR_STRING
4886 && li->li_tv.vval.v_string != NULL
4887 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02004888 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004889 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02004890 }
4891 else
4892 {
4893 pos.col = list_find_nr(l, 1L, &error);
4894 if (error)
4895 return NULL;
4896 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00004897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004899 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004900 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004901 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004904 pos.coladd = list_find_nr(l, 2L, &error);
4905 if (error)
4906 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004907
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004908 return &pos;
4909 }
4910
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004911 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 if (name == NULL)
4913 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004914 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004916 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004917 {
4918 if (VIsual_active)
4919 return &VIsual;
4920 return &curwin->w_cursor;
4921 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004924 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4926 return NULL;
4927 return pp;
4928 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004929
Bram Moolenaara5525202006-03-02 22:52:09 +00004930 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004931
Bram Moolenaar477933c2007-07-17 14:32:23 +00004932 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004933 {
4934 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004936 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004937 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004938 // In silent Ex mode topline is zero, but that's not a valid line
4939 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004940 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004941 return &pos;
4942 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004944 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004945 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004946 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004947 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004948 return &pos;
4949 }
4950 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004951 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004953 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
4955 pos.lnum = curbuf->b_ml.ml_line_count;
4956 pos.col = 0;
4957 }
4958 else
4959 {
4960 pos.lnum = curwin->w_cursor.lnum;
4961 pos.col = (colnr_T)STRLEN(ml_get_curline());
4962 }
4963 return &pos;
4964 }
4965 return NULL;
4966}
4967
4968/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004969 * Convert list in "arg" into a position and optional file number.
4970 * When "fnump" is NULL there is no file number, only 3 items.
4971 * Note that the column is passed on as-is, the caller may want to decrement
4972 * it to use 1 for the first column.
4973 * Return FAIL when conversion is not possible, doesn't check the position for
4974 * validity.
4975 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004976 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004977list2fpos(
4978 typval_T *arg,
4979 pos_T *posp,
4980 int *fnump,
4981 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004982{
4983 list_T *l = arg->vval.v_list;
4984 long i = 0;
4985 long n;
4986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004987 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4988 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004989 if (arg->v_type != VAR_LIST
4990 || l == NULL
4991 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004992 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004993 return FAIL;
4994
4995 if (fnump != NULL)
4996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004998 if (n < 0)
4999 return FAIL;
5000 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005001 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005002 *fnump = n;
5003 }
5004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005005 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005006 if (n < 0)
5007 return FAIL;
5008 posp->lnum = n;
5009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005010 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005011 if (n < 0)
5012 return FAIL;
5013 posp->col = n;
5014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005015 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005016 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005017 posp->coladd = 0;
5018 else
5019 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005020
Bram Moolenaar493c1782014-05-28 14:34:46 +02005021 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005022 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005023
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005024 return OK;
5025}
5026
5027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 * Get the length of an environment variable name.
5029 * Advance "arg" to the first character after the name.
5030 * Return 0 for error.
5031 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005032 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005033get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
5035 char_u *p;
5036 int len;
5037
5038 for (p = *arg; vim_isIDc(*p); ++p)
5039 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005040 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 return 0;
5042
5043 len = (int)(p - *arg);
5044 *arg = p;
5045 return len;
5046}
5047
5048/*
5049 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005050 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 * Return 0 if something is wrong.
5052 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005054get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
5056 char_u *p;
5057 int len;
5058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005059 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005061 {
5062 if (*p == ':')
5063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 // "s:" is start of "s:var", but "n:" is not and can be used in
5065 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005066 len = (int)(p - *arg);
5067 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5068 || len > 1)
5069 break;
5070 }
5071 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005072 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 return 0;
5074
5075 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005076 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
5078 return len;
5079}
5080
5081/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005082 * Get the length of the name of a variable or function.
5083 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005085 * Return -1 if curly braces expansion failed.
5086 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 * If the name contains 'magic' {}'s, expand them and return the
5088 * expanded name in an allocated string via 'alias' - caller must free.
5089 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005090 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005091get_name_len(
5092 char_u **arg,
5093 char_u **alias,
5094 int evaluate,
5095 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
5097 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 char_u *p;
5099 char_u *expr_start;
5100 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005102 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103
5104 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5105 && (*arg)[2] == (int)KE_SNR)
5106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005107 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 *arg += 3;
5109 return get_id_len(arg) + 3;
5110 }
5111 len = eval_fname_script(*arg);
5112 if (len > 0)
5113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 *arg += len;
5116 }
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005121 p = find_name_end(*arg, &expr_start, &expr_end,
5122 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (expr_start != NULL)
5124 {
5125 char_u *temp_string;
5126
5127 if (!evaluate)
5128 {
5129 len += (int)(p - *arg);
5130 *arg = skipwhite(p);
5131 return len;
5132 }
5133
5134 /*
5135 * Include any <SID> etc in the expanded string:
5136 * Thus the -len here.
5137 */
5138 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5139 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005140 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 *alias = temp_string;
5142 *arg = skipwhite(p);
5143 return (int)STRLEN(temp_string);
5144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
5146 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005147 // Only give an error when there is something, otherwise it will be
5148 // reported at a higher level.
5149 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005150 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151
5152 return len;
5153}
5154
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005155/*
5156 * Find the end of a variable or function name, taking care of magic braces.
5157 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5158 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005159 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 * Return a pointer to just after the name. Equal to "arg" if there is no
5161 * valid name.
5162 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005163 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005164find_name_end(
5165 char_u *arg,
5166 char_u **expr_start,
5167 char_u **expr_end,
5168 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005170 int mb_nest = 0;
5171 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005173 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005174 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 *expr_start = NULL;
5179 *expr_end = NULL;
5180 }
5181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005182 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005183 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5184 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005185 return arg;
5186
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 for (p = arg; *p != NUL
5188 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005189 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005190 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005191 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005192 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005193 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005194 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005195 if (*p == '\'')
5196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005198 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005199 ;
5200 if (*p == NUL)
5201 break;
5202 }
5203 else if (*p == '"')
5204 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005206 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005207 if (*p == '\\' && p[1] != NUL)
5208 ++p;
5209 if (*p == NUL)
5210 break;
5211 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005212 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5213 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005214 // "s:" is start of "s:var", but "n:" is not and can be used in
5215 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005216 len = (int)(p - arg);
5217 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005218 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005219 break;
5220 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005222 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 if (*p == '[')
5225 ++br_nest;
5226 else if (*p == ']')
5227 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005229
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005230 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005232 if (*p == '{')
5233 {
5234 mb_nest++;
5235 if (expr_start != NULL && *expr_start == NULL)
5236 *expr_start = p;
5237 }
5238 else if (*p == '}')
5239 {
5240 mb_nest--;
5241 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5242 *expr_end = p;
5243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
5246
5247 return p;
5248}
5249
5250/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005251 * Expands out the 'magic' {}'s in a variable/function name.
5252 * Note that this can call itself recursively, to deal with
5253 * constructs like foo{bar}{baz}{bam}
5254 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5255 * "in_start" ^
5256 * "expr_start" ^
5257 * "expr_end" ^
5258 * "in_end" ^
5259 *
5260 * Returns a new allocated string, which the caller must free.
5261 * Returns NULL for failure.
5262 */
5263 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005264make_expanded_name(
5265 char_u *in_start,
5266 char_u *expr_start,
5267 char_u *expr_end,
5268 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269{
5270 char_u c1;
5271 char_u *retval = NULL;
5272 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005273
5274 if (expr_end == NULL || in_end == NULL)
5275 return NULL;
5276 *expr_start = NUL;
5277 *expr_end = NUL;
5278 c1 = *in_end;
5279 *in_end = NUL;
5280
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005281 temp_result = eval_to_string(expr_start + 1, FALSE);
5282 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005284 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5285 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 if (retval != NULL)
5287 {
5288 STRCPY(retval, in_start);
5289 STRCAT(retval, temp_result);
5290 STRCAT(retval, expr_end + 1);
5291 }
5292 }
5293 vim_free(temp_result);
5294
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005295 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005296 *expr_start = '{';
5297 *expr_end = '}';
5298
5299 if (retval != NULL)
5300 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005301 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005302 if (expr_start != NULL)
5303 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005304 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005305 temp_result = make_expanded_name(retval, expr_start,
5306 expr_end, temp_result);
5307 vim_free(retval);
5308 retval = temp_result;
5309 }
5310 }
5311
5312 return retval;
5313}
5314
5315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005317 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005320eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005322 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005323}
5324
5325/*
5326 * Return TRUE if character "c" can be used as the first character in a
5327 * variable or function name (excluding '{' and '}').
5328 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005330eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005331{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005332 return ASCII_ISALPHA(c) || c == '_';
5333}
5334
5335/*
5336 * Return TRUE if character "c" can be used as the first character of a
5337 * dictionary key.
5338 */
5339 int
5340eval_isdictc(int c)
5341{
5342 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343}
5344
5345/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005346 * Return the character "str[index]" where "index" is the character index. If
5347 * "index" is out of range NULL is returned.
5348 */
5349 char_u *
5350char_from_string(char_u *str, varnumber_T index)
5351{
5352 size_t nbyte = 0;
5353 varnumber_T nchar = index;
5354 size_t slen;
5355
5356 if (str == NULL || index < 0)
5357 return NULL;
5358 slen = STRLEN(str);
5359 while (nchar > 0 && nbyte < slen)
5360 {
5361 nbyte += MB_CPTR2LEN(str + nbyte);
5362 --nchar;
5363 }
5364 if (nbyte >= slen)
5365 return NULL;
5366 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5367}
5368
5369/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005370 * Get the byte index for character index "idx" in string "str" with length
5371 * "str_len".
5372 * If going over the end return "str_len".
5373 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005374 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005375 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005376 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005377char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5378{
5379 varnumber_T nchar = idx;
5380 size_t nbyte = 0;
5381
5382 if (nchar >= 0)
5383 {
5384 while (nchar > 0 && nbyte < str_len)
5385 {
5386 nbyte += MB_CPTR2LEN(str + nbyte);
5387 --nchar;
5388 }
5389 }
5390 else
5391 {
5392 nbyte = str_len;
5393 while (nchar < 0 && nbyte > 0)
5394 {
5395 --nbyte;
5396 nbyte -= mb_head_off(str, str + nbyte);
5397 ++nchar;
5398 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005399 if (nchar < 0)
5400 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005401 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005402 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005403}
5404
5405/*
5406 * Return the slice "str[first:last]" using character indexes.
5407 * Return NULL when the result is empty.
5408 */
5409 char_u *
5410string_slice(char_u *str, varnumber_T first, varnumber_T last)
5411{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005412 long start_byte, end_byte;
5413 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005414
5415 if (str == NULL)
5416 return NULL;
5417 slen = STRLEN(str);
5418 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005419 if (start_byte < 0)
5420 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005421 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005422 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005423 else
5424 {
5425 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005426 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005427 // end index is inclusive
5428 end_byte += MB_CPTR2LEN(str + end_byte);
5429 }
5430
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005431 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005432 return NULL;
5433 return vim_strnsave(str + start_byte, end_byte - start_byte);
5434}
5435
5436/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005437 * Handle:
5438 * - expr[expr], expr[expr:expr] subscript
5439 * - ".name" lookup
5440 * - function call with Funcref variable: func(expr)
5441 * - method call: var->method()
5442 *
5443 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005444 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005446handle_subscript(
5447 char_u **arg,
5448 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005449 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005450 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005452 int evaluate = evalarg != NULL
5453 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005454 int ret = OK;
5455 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005456 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005457 int getnext;
5458 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005459
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005460 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005461 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005462 // When at the end of the line and ".name" or "->{" or "->X" follows in
5463 // the next line then consume the line break.
5464 p = eval_next_non_blank(*arg, evalarg, &getnext);
5465 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005466 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005467 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005468 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005469 {
5470 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005471 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005472 check_white = FALSE;
5473 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005474
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005475 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5476 || rettv->v_type == VAR_PARTIAL))
5477 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005478 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005479 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5480 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005481
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005482 // Stop the expression evaluation when immediately aborting on
5483 // error, or when an interrupt occurred or an exception was thrown
5484 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005485 if (aborting())
5486 {
5487 if (ret == OK)
5488 clear_tv(rettv);
5489 ret = FAIL;
5490 }
5491 dict_unref(selfdict);
5492 selfdict = NULL;
5493 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005494 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005495 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005496 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005497 if (ret == OK)
5498 {
5499 if ((*arg)[2] == '{')
5500 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005501 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005502 else
5503 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005504 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005505 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005506 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005507 // "." is ".name" lookup when we found a dict or when evaluating and
5508 // scriptversion is at least 2, where string concatenation is "..".
5509 else if (**arg == '['
5510 || (**arg == '.' && (rettv->v_type == VAR_DICT
5511 || (!evaluate
5512 && (*arg)[1] != '.'
5513 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005514 {
5515 dict_unref(selfdict);
5516 if (rettv->v_type == VAR_DICT)
5517 {
5518 selfdict = rettv->vval.v_dict;
5519 if (selfdict != NULL)
5520 ++selfdict->dv_refcount;
5521 }
5522 else
5523 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005524 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005525 {
5526 clear_tv(rettv);
5527 ret = FAIL;
5528 }
5529 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005530 else
5531 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005532 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005534 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5535 // Don't do this when "Func" is already a partial that was bound
5536 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005537 if (selfdict != NULL
5538 && (rettv->v_type == VAR_FUNC
5539 || (rettv->v_type == VAR_PARTIAL
5540 && (rettv->vval.v_partial->pt_auto
5541 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005542 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005543
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005544 dict_unref(selfdict);
5545 return ret;
5546}
5547
5548/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005549 * Make a copy of an item.
5550 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005551 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5552 * reference to an already copied list/dict can be used.
5553 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005554 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005555 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005556item_copy(
5557 typval_T *from,
5558 typval_T *to,
5559 int deep,
5560 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005561{
5562 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005563 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005564
Bram Moolenaar33570922005-01-25 22:26:29 +00005565 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005567 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005568 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005569 }
5570 ++recurse;
5571
5572 switch (from->v_type)
5573 {
5574 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005575 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005576 case VAR_STRING:
5577 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005578 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005579 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005580 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005581 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005582 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005583 copy_tv(from, to);
5584 break;
5585 case VAR_LIST:
5586 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005587 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005588 if (from->vval.v_list == NULL)
5589 to->vval.v_list = NULL;
5590 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5591 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005592 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005593 to->vval.v_list = from->vval.v_list->lv_copylist;
5594 ++to->vval.v_list->lv_refcount;
5595 }
5596 else
5597 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5598 if (to->vval.v_list == NULL)
5599 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005600 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005601 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005603 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005604 case VAR_DICT:
5605 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005606 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005607 if (from->vval.v_dict == NULL)
5608 to->vval.v_dict = NULL;
5609 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005611 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005612 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5613 ++to->vval.v_dict->dv_refcount;
5614 }
5615 else
5616 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5617 if (to->vval.v_dict == NULL)
5618 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005619 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005620 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005621 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005622 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005623 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005624 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005625 }
5626 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005627 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005628}
5629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630 void
5631echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5632{
5633 char_u *tofree;
5634 char_u numbuf[NUMBUFLEN];
5635 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5636
5637 if (*atstart)
5638 {
5639 *atstart = FALSE;
5640 // Call msg_start() after eval1(), evaluating the expression
5641 // may cause a message to appear.
5642 if (with_space)
5643 {
5644 // Mark the saved text as finishing the line, so that what
5645 // follows is displayed on a new line when scrolling back
5646 // at the more prompt.
5647 msg_sb_eol();
5648 msg_start();
5649 }
5650 }
5651 else if (with_space)
5652 msg_puts_attr(" ", echo_attr);
5653
5654 if (p != NULL)
5655 for ( ; *p != NUL && !got_int; ++p)
5656 {
5657 if (*p == '\n' || *p == '\r' || *p == TAB)
5658 {
5659 if (*p != TAB && *needclr)
5660 {
5661 // remove any text still there from the command
5662 msg_clr_eos();
5663 *needclr = FALSE;
5664 }
5665 msg_putchar_attr(*p, echo_attr);
5666 }
5667 else
5668 {
5669 if (has_mbyte)
5670 {
5671 int i = (*mb_ptr2len)(p);
5672
5673 (void)msg_outtrans_len_attr(p, i, echo_attr);
5674 p += i - 1;
5675 }
5676 else
5677 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5678 }
5679 }
5680 vim_free(tofree);
5681}
5682
Bram Moolenaare9a41262005-01-15 22:18:47 +00005683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 * ":echo expr1 ..." print each argument separated with a space, add a
5685 * newline at the end.
5686 * ":echon expr1 ..." print each argument plain.
5687 */
5688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005689ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
5691 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005692 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 char_u *p;
5694 int needclr = TRUE;
5695 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005696 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005697 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005698 evalarg_T evalarg;
5699
Bram Moolenaare707c882020-07-01 13:07:37 +02005700 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
5702 if (eap->skip)
5703 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005704 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005706 // If eval1() causes an error message the text from the command may
5707 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005708 need_clr_eos = needclr;
5709
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005711 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
5713 /*
5714 * Report the invalid expression unless the expression evaluation
5715 * has been cancelled due to an aborting error, an interrupt, or an
5716 * exception.
5717 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005718 if (!aborting() && did_emsg == did_emsg_before
5719 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005720 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005721 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 break;
5723 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005724 need_clr_eos = FALSE;
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005728
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005729 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 arg = skipwhite(arg);
5731 }
5732 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005733 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735 if (eap->skip)
5736 --emsg_skip;
5737 else
5738 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005739 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (needclr)
5741 msg_clr_eos();
5742 if (eap->cmdidx == CMD_echo)
5743 msg_end();
5744 }
5745}
5746
5747/*
5748 * ":echohl {name}".
5749 */
5750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005751ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005753 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754}
5755
5756/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005757 * Returns the :echo attribute
5758 */
5759 int
5760get_echo_attr(void)
5761{
5762 return echo_attr;
5763}
5764
5765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 * ":execute expr1 ..." execute the result of an expression.
5767 * ":echomsg expr1 ..." Print a message
5768 * ":echoerr expr1 ..." Print an error
5769 * Each gets spaces around each argument and a newline at the end for
5770 * echo commands
5771 */
5772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005773ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 int ret = OK;
5778 char_u *p;
5779 garray_T ga;
5780 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
5782 ga_init2(&ga, 1, 80);
5783
5784 if (eap->skip)
5785 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005786 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005788 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005789 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
5792 if (!eap->skip)
5793 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005794 char_u buf[NUMBUFLEN];
5795
5796 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005797 {
5798 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5799 {
5800 emsg(_(e_inval_string));
5801 p = NULL;
5802 }
5803 else
5804 p = tv_get_string_buf(&rettv, buf);
5805 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005806 else
5807 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005808 if (p == NULL)
5809 {
5810 clear_tv(&rettv);
5811 ret = FAIL;
5812 break;
5813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 len = (int)STRLEN(p);
5815 if (ga_grow(&ga, len + 2) == FAIL)
5816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005817 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 ret = FAIL;
5819 break;
5820 }
5821 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 ga.ga_len += len;
5825 }
5826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005827 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 arg = skipwhite(arg);
5829 }
5830
5831 if (ret != FAIL && ga.ga_data != NULL)
5832 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005833 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5834 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005835 // Mark the already saved text as finishing the line, so that what
5836 // follows is displayed on a new line when scrolling back at the
5837 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005838 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005839 }
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005842 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005843 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005844 out_flush();
5845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 else if (eap->cmdidx == CMD_echoerr)
5847 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005848 int save_did_emsg = did_emsg;
5849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005850 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005851 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (!force_abort)
5853 did_emsg = save_did_emsg;
5854 }
5855 else if (eap->cmdidx == CMD_execute)
5856 do_cmdline((char_u *)ga.ga_data,
5857 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5858 }
5859
5860 ga_clear(&ga);
5861
5862 if (eap->skip)
5863 --emsg_skip;
5864
5865 eap->nextcmd = check_nextcmd(arg);
5866}
5867
5868/*
5869 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5870 * "arg" points to the "&" or '+' when called, to "option" when returning.
5871 * Returns NULL when no option name found. Otherwise pointer to the char
5872 * after the option name.
5873 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005874 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005875find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 char_u *p = *arg;
5878
5879 ++p;
5880 if (*p == 'g' && p[1] == ':')
5881 {
5882 *opt_flags = OPT_GLOBAL;
5883 p += 2;
5884 }
5885 else if (*p == 'l' && p[1] == ':')
5886 {
5887 *opt_flags = OPT_LOCAL;
5888 p += 2;
5889 }
5890 else
5891 *opt_flags = 0;
5892
5893 if (!ASCII_ISALPHA(*p))
5894 return NULL;
5895 *arg = p;
5896
5897 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005898 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 else
5900 while (ASCII_ISALPHA(*p))
5901 ++p;
5902 return p;
5903}
5904
5905/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005906 * Display script name where an item was last set.
5907 * Should only be invoked when 'verbose' is non-zero.
5908 */
5909 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005910last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005911{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005912 char_u *p;
5913
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005914 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005915 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005916 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005917 if (p != NULL)
5918 {
5919 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005920 msg_puts(_("\n\tLast set from "));
5921 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005922 if (script_ctx.sc_lnum > 0)
5923 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005924 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005925 msg_outnum((long)script_ctx.sc_lnum);
5926 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005927 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005928 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005929 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005930 }
5931}
5932
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005933#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
5935/*
5936 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005937 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 * "flags" can be "g" to do a global substitute.
5939 * Returns an allocated string, NULL for error.
5940 */
5941 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942do_string_sub(
5943 char_u *str,
5944 char_u *pat,
5945 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005946 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005947 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 int sublen;
5950 regmatch_T regmatch;
5951 int i;
5952 int do_all;
5953 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005954 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 garray_T ga;
5956 char_u *ret;
5957 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005958 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005960 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005962 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963
5964 ga_init2(&ga, 1, 200);
5965
5966 do_all = (flags[0] == 'g');
5967
5968 regmatch.rm_ic = p_ic;
5969 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5970 if (regmatch.regprog != NULL)
5971 {
5972 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005973 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5975 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005976 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005977 if (regmatch.startp[0] == regmatch.endp[0])
5978 {
5979 if (zero_width == regmatch.startp[0])
5980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005981 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005982 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005983 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5984 (size_t)i);
5985 ga.ga_len += i;
5986 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005987 continue;
5988 }
5989 zero_width = regmatch.startp[0];
5990 }
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 /*
5993 * Get some space for a temporary buffer to do the substitution
5994 * into. It will contain:
5995 * - The text up to where the match is.
5996 * - The substituted text.
5997 * - The text after the match.
5998 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005999 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006000 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6002 {
6003 ga_clear(&ga);
6004 break;
6005 }
6006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006007 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 i = (int)(regmatch.startp[0] - tail);
6009 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006010 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006011 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 + ga.ga_len + i, TRUE, TRUE, FALSE);
6013 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006014 tail = regmatch.endp[0];
6015 if (*tail == NUL)
6016 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 if (!do_all)
6018 break;
6019 }
6020
6021 if (ga.ga_data != NULL)
6022 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6023
Bram Moolenaar473de612013-06-08 18:19:48 +02006024 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 }
6026
6027 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6028 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006029 if (p_cpo == empty_option)
6030 p_cpo = save_cpo;
6031 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006032 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006033 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034
6035 return ret;
6036}