blob: 8e554354965ea8d310a5a8a7ec216ce8b30f2b31 [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 Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020054static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020055static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020056static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020059static 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 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107#if defined(EBCDIC) || defined(PROTO)
108/*
109 * Compare struct fst by function name.
110 */
111 static int
112compare_func_name(const void *s1, const void *s2)
113{
114 struct fst *p1 = (struct fst *)s1;
115 struct fst *p2 = (struct fst *)s2;
116
117 return STRCMP(p1->f_name, p2->f_name);
118}
119
120/*
121 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100122 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100123 * On machines using EBCDIC we have to sort it.
124 */
125 static void
126sortFunctions(void)
127{
128 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
129
130 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
131}
132#endif
133
Bram Moolenaar33570922005-01-25 22:26:29 +0000134/*
135 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000136 */
137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100138eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000139{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200140 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200141 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000142
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200143#ifdef EBCDIC
144 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100145 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200146 */
147 sortFunctions();
148#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000149}
150
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151#if defined(EXITFREE) || defined(PROTO)
152 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100153eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000154{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200155 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100156 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200157 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000158
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200159 // autoloaded script names
160 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000161
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200162 // unreferenced lists and dicts
163 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200164
165 // functions not garbage collected
166 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000167}
168#endif
169
Bram Moolenaare6b53242020-07-01 17:28:33 +0200170 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200171fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
172{
173 CLEAR_FIELD(*evalarg);
174 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
175 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
176 {
177 evalarg->eval_getline = eap->getline;
178 evalarg->eval_cookie = eap->cookie;
179 }
180}
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
183 * Top level evaluation function, returning a boolean.
184 * Sets "error" to TRUE if there was an error.
185 * Return TRUE or FALSE.
186 */
187 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100188eval_to_bool(
189 char_u *arg,
190 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200191 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100192 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193{
Bram Moolenaar33570922005-01-25 22:26:29 +0000194 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200195 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200196 evalarg_T evalarg;
197
Bram Moolenaar37c83712020-06-30 21:18:36 +0200198 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200 if (skip)
201 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 else
205 {
206 *error = FALSE;
207 if (!skip)
208 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200209 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200210 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200211 else
212 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000213 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
215 }
216 if (skip)
217 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200218 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200220 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221}
222
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223/*
224 * Call eval1() and give an error message if not done at a lower level.
225 */
226 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200227eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100228{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100229 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100230 int ret;
231 int did_emsg_before = did_emsg;
232 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200233 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100234
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200235 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
236
237 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100238 if (ret == FAIL)
239 {
240 // Report the invalid expression unless the expression evaluation has
241 // been cancelled due to an aborting error, an interrupt, or an
242 // exception, or we already gave a more specific error.
243 // Also check called_emsg for when using assert_fails().
244 if (!aborting() && did_emsg == did_emsg_before
245 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100246 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100247 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200248 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100249 return ret;
250}
251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200253 * Return whether a typval is a valid expression to pass to eval_expr_typval()
254 * or eval_expr_to_bool(). An empty string returns FALSE;
255 */
256 int
257eval_expr_valid_arg(typval_T *tv)
258{
259 return tv->v_type != VAR_UNKNOWN
260 && (tv->v_type != VAR_STRING
261 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
262}
263
264/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 * Evaluate an expression, which can be a function, partial or string.
266 * Pass arguments "argv[argc]".
267 * Return the result in "rettv" and OK or FAIL.
268 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200269 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100270eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
271{
272 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200274 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100275
276 if (expr->v_type == VAR_FUNC)
277 {
278 s = expr->vval.v_string;
279 if (s == NULL || *s == NUL)
280 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200281 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200282 funcexe.evaluate = TRUE;
283 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100284 return FAIL;
285 }
286 else if (expr->v_type == VAR_PARTIAL)
287 {
288 partial_T *partial = expr->vval.v_partial;
289
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200290 if (partial == NULL)
291 return FAIL;
292
Bram Moolenaar822ba242020-05-24 23:00:18 +0200293 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200294 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200296 if (call_def_function(partial->pt_func, argc, argv,
297 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 return FAIL;
299 }
300 else
301 {
302 s = partial_name(partial);
303 if (s == NULL || *s == NUL)
304 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200305 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 funcexe.evaluate = TRUE;
307 funcexe.partial = partial;
308 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
309 return FAIL;
310 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100311 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200312 else if (expr->v_type == VAR_INSTR)
313 {
314 return exe_typval_instr(expr, rettv);
315 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100316 else
317 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100318 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100319 if (s == NULL)
320 return FAIL;
321 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200322 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200324 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200326 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100327 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 return FAIL;
329 }
330 }
331 return OK;
332}
333
334/*
335 * Like eval_to_bool() but using a typval_T instead of a string.
336 * Works for string, funcref and partial.
337 */
338 int
339eval_expr_to_bool(typval_T *expr, int *error)
340{
341 typval_T rettv;
342 int res;
343
344 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
345 {
346 *error = TRUE;
347 return FALSE;
348 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200349 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100350 clear_tv(&rettv);
351 return res;
352}
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/*
355 * Top level evaluation function, returning a string. If "skip" is TRUE,
356 * only parsing to "nextcmd" is done, without reporting errors. Return
357 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
358 */
359 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100360eval_to_string_skip(
361 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200362 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100363 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364{
Bram Moolenaar33570922005-01-25 22:26:29 +0000365 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200367 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar37c83712020-06-30 21:18:36 +0200369 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 if (skip)
371 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200372 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 retval = NULL;
374 else
375 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100376 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 }
379 if (skip)
380 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
383 return retval;
384}
385
386/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000387 * Skip over an expression at "*pp".
388 * Return FAIL for an error, OK otherwise.
389 */
390 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200391skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000392{
Bram Moolenaar33570922005-01-25 22:26:29 +0000393 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000394
395 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200396 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000397}
398
399/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200400 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 * If in Vim9 script and line breaks are encountered, the lines are
402 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200403 * "arg" is advanced to just after the expression.
404 * "start" is set to the start of the expression, "end" to just after the end.
405 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 * Return FAIL for an error, OK otherwise.
407 */
408 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200409skip_expr_concatenate(
410 char_u **arg,
411 char_u **start,
412 char_u **end,
413 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200414{
415 typval_T rettv;
416 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200417 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200418 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200419 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200420 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200421 int evaluate = evalarg == NULL
422 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200423
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200424 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200426 {
427 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200428 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200429 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200430 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200431 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200432 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200433 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200434
435 // Don't evaluate the expression.
436 if (evalarg != NULL)
437 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200438 *arg = skipwhite(*arg);
439 res = eval1(arg, &rettv, evalarg);
440 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200441 if (evalarg != NULL)
442 evalarg->eval_flags = save_flags;
443
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200444 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200445 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200446 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200447 if (evalarg->eval_ga.ga_len == 1)
448 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200449 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200450 ga_clear(gap);
451 gap->ga_itemsize = 0;
452 }
453 else
454 {
455 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200457
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200458 // Line breaks encountered, concatenate all the lines.
459 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200460 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461
462 // free the lines only when using getsourceline()
463 if (evalarg->eval_cookie != NULL)
464 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200465 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200466 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200467 // Do not free the last line, "arg" points into it, free it
468 // later.
469 vim_free(evalarg->eval_tofree);
470 evalarg->eval_tofree =
471 ((char_u **)gap->ga_data)[gap->ga_len - 1];
472 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200473 ga_clear_strings(gap);
474 }
475 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200476 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200477 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200478
479 // free lines that were explicitly marked for freeing
480 ga_clear_strings(freegap);
481 }
482
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200483 gap->ga_itemsize = 0;
484 if (p == NULL)
485 return FAIL;
486 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487 vim_free(evalarg->eval_tofree_lambda);
488 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200489 // Compute "end" relative to the end.
490 *end = *start + STRLEN(*start) - endoff;
491 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492 }
493
494 return res;
495}
496
497/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100498 * Convert "tv" to a string.
499 * When "convert" is TRUE convert a List into a sequence of lines and convert
500 * a Float to a String.
501 * Returns an allocated string (NULL when out of memory).
502 */
503 char_u *
504typval2string(typval_T *tv, int convert)
505{
506 garray_T ga;
507 char_u *retval;
508#ifdef FEAT_FLOAT
509 char_u numbuf[NUMBUFLEN];
510#endif
511
512 if (convert && tv->v_type == VAR_LIST)
513 {
514 ga_init2(&ga, (int)sizeof(char), 80);
515 if (tv->vval.v_list != NULL)
516 {
517 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
518 if (tv->vval.v_list->lv_len > 0)
519 ga_append(&ga, NL);
520 }
521 ga_append(&ga, NUL);
522 retval = (char_u *)ga.ga_data;
523 }
524#ifdef FEAT_FLOAT
525 else if (convert && tv->v_type == VAR_FLOAT)
526 {
527 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
528 retval = vim_strsave(numbuf);
529 }
530#endif
531 else
532 retval = vim_strsave(tv_get_string(tv));
533 return retval;
534}
535
536/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200537 * Top level evaluation function, returning a string. Does not handle line
538 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000539 * When "convert" is TRUE convert a List into a sequence of lines and convert
540 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * Return pointer to allocated memory, or NULL for failure.
542 */
543 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100544eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100546 int convert,
547 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
Bram Moolenaar33570922005-01-25 22:26:29 +0000549 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100551 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100553 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
554 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 retval = NULL;
556 else
557 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100558 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000559 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100561 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
563 return retval;
564}
565
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100566 char_u *
567eval_to_string(
568 char_u *arg,
569 int convert)
570{
571 return eval_to_string_eap(arg, convert, NULL);
572}
573
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000575 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200576 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200577 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 */
579 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100580eval_to_string_safe(
581 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200585 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200586 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200588 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200589 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000590 if (use_sandbox)
591 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200592 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200593 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000594 if (use_sandbox)
595 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200596 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200597 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200598 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 return retval;
600}
601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602/*
603 * Top level evaluation function, returning a number.
604 * Evaluates "expr" silently.
605 * Returns -1 for an error.
606 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200607 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100608eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
Bram Moolenaar33570922005-01-25 22:26:29 +0000610 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200611 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000612 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614 ++emsg_off;
615
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200616 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 retval = -1;
618 else
619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100620 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000621 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 }
623 --emsg_off;
624
625 return retval;
626}
627
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000628/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000629 * Top level evaluation function.
630 * Returns an allocated typval_T with the result.
631 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000632 */
633 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200634eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000635{
636 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200637 evalarg_T evalarg;
638
639 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000640
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200641 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200642 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100643 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000644
Bram Moolenaar37c83712020-06-30 21:18:36 +0200645 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000646 return tv;
647}
648
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100650 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200651 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
652 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000653 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100656call_vim_function(
657 char_u *func,
658 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200659 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200660 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000662 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200663 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100665 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200666 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200667 funcexe.firstline = curwin->w_cursor.lnum;
668 funcexe.lastline = curwin->w_cursor.lnum;
669 funcexe.evaluate = TRUE;
670 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000671 if (ret == FAIL)
672 clear_tv(rettv);
673
674 return ret;
675}
676
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100677/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100678 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100679 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200680 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
681 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100682 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200683 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100684call_func_retnr(
685 char_u *func,
686 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200687 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100688{
689 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100691
Bram Moolenaarded27a12018-08-01 19:06:03 +0200692 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100693 return -1;
694
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100695 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100696 clear_tv(&rettv);
697 return retval;
698}
699
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000700/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100701 * Call Vim script function like call_func_retnr() and drop the result.
702 * Returns FAIL when calling the function fails.
703 */
704 int
705call_func_noret(
706 char_u *func,
707 int argc,
708 typval_T *argv)
709{
710 typval_T rettv;
711
712 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
713 return FAIL;
714 clear_tv(&rettv);
715 return OK;
716}
717
718/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100719 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100720 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000721 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000722 */
723 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100724call_func_retstr(
725 char_u *func,
726 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200727 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000728{
729 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000730 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731
Bram Moolenaarded27a12018-08-01 19:06:03 +0200732 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000733 return NULL;
734
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100735 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000736 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 return retval;
738}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000739
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000740/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100741 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100742 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000743 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000744 */
745 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100746call_func_retlist(
747 char_u *func,
748 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200749 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000750{
751 typval_T rettv;
752
Bram Moolenaarded27a12018-08-01 19:06:03 +0200753 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000754 return NULL;
755
756 if (rettv.v_type != VAR_LIST)
757 {
758 clear_tv(&rettv);
759 return NULL;
760 }
761
762 return rettv.vval.v_list;
763}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#ifdef FEAT_FOLDING
766/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100767 * Evaluate "arg", which is 'foldexpr'.
768 * Note: caller must set "curwin" to match "arg".
769 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
770 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100773eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
Bram Moolenaar33570922005-01-25 22:26:29 +0000775 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200776 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000778 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
779 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
781 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000782 if (use_sandbox)
783 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200784 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200786 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 retval = 0;
788 else
789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100790 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 if (tv.v_type == VAR_NUMBER)
792 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000793 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 retval = 0;
795 else
796 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100797 // If the result is a string, check if there is a non-digit before
798 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000799 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (!VIM_ISDIGIT(*s) && *s != '-')
801 *cp = *s++;
802 retval = atol((char *)s);
803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000804 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000807 if (use_sandbox)
808 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200809 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200810 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200812 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813}
814#endif
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 * Get an lval: variable, Dict item or List item that can be assigned a value
818 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
819 * "name.key", "name.key[expr]" etc.
820 * Indexing only works if "name" is an existing List or Dictionary.
821 * "name" points to the start of the name.
822 * If "rettv" is not NULL it points to the value to be assigned.
823 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
824 * wrong; must end in space or cmd separator.
825 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100826 * flags:
827 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100828 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100829 * GLV_NO_AUTOLOAD: do not use script autoloading
830 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000832 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000834 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200835 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100836get_lval(
837 char_u *name,
838 typval_T *rettv,
839 lval_T *lp,
840 int unlet,
841 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100842 int flags, // GLV_ values
843 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846 char_u *expr_start, *expr_end;
847 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000848 dictitem_T *v;
849 typval_T var1;
850 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000852 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000853 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000854 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100855 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100856 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100857 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100859 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200860 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100862 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000863 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100864 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200866 lp->ll_name_end = find_name_end(name, NULL, NULL,
867 FNE_INCL_BR | fne_flags);
868 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 }
870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100871 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000872 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 if (expr_start != NULL)
875 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100876 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100877 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 && *p != '[' && *p != '.')
879 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200880 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 return NULL;
882 }
883
884 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
885 if (lp->ll_exp_name == NULL)
886 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100887 // Report an invalid expression in braces, unless the
888 // expression evaluation has been cancelled due to an
889 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 if (!aborting() && !quiet)
891 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000892 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100893 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 return NULL;
895 }
896 }
897 lp->ll_name = lp->ll_exp_name;
898 }
899 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 lp->ll_name = name;
902
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200903 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200905 // "a: type" is declaring variable "a" with a type, not "a:".
906 if (p == name + 2 && p[-1] == ':')
907 {
908 --p;
909 lp->ll_name_end = p;
910 }
911 if (*p == ':')
912 {
913 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
914 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200916 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100917 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
918 if (lp->ll_type == NULL && !quiet)
919 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200920 lp->ll_name_end = tp;
921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 }
923 }
924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100925 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
927 return p;
928
929 cc = *p;
930 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100931 // When we would write to the variable pass &ht and prevent autoload.
932 writing = !(flags & GLV_READ_ONLY);
933 v = find_var(lp->ll_name, writing ? &ht : NULL,
934 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200936 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000937 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000938 if (v == NULL)
939 return NULL;
940
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100941 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
942 {
943 if (!quiet)
944 semsg(_(e_variable_already_declared), lp->ll_name);
945 return NULL;
946 }
947
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 /*
949 * Loop until no more [idx] or .key is following.
950 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000951 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100952 var1.v_type = VAR_UNKNOWN;
953 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200957 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100958 && !(lp->ll_tv->v_type == VAR_BLOB
959 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100962 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000966 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100968 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000971
Bram Moolenaar10c65862020-10-08 21:16:42 +0200972 if (in_vim9script() && lp->ll_valtype == NULL
973 && lp->ll_tv == &v->di_tv
974 && ht != NULL && ht == get_script_local_ht())
975 {
976 svar_T *sv = find_typval_in_script(lp->ll_tv);
977
978 // Vim9 script local variable: get the type
979 if (sv != NULL)
980 lp->ll_valtype = sv->sv_type;
981 }
982
Bram Moolenaar8c711452005-01-14 21:53:12 +0000983 len = -1;
984 if (*p == '.')
985 {
986 key = p + 1;
987 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
988 ;
989 if (len == 0)
990 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100992 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 }
995 p = key + len;
996 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000997 else
998 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100999 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001000 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 if (*p == ':')
1002 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001003 else
1004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001006 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001008 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001009 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001011 clear_tv(&var1);
1012 return NULL;
1013 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001014 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 }
1016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001017 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001018 if (*p == ':')
1019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001021 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001023 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001024 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001026 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001027 if (rettv != NULL
1028 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001029 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001030 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001031 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001034 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001035 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001037 }
1038 p = skipwhite(p + 1);
1039 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001041 else
1042 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001044 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001045 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001047 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001050 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001051 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001052 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001053 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001054 clear_tv(&var2);
1055 return NULL;
1056 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001059 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001062
Bram Moolenaar8c711452005-01-14 21:53:12 +00001063 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001064 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001066 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001067 clear_tv(&var1);
1068 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 }
1071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001072 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 ++p;
1074 }
1075
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001076 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001077 {
1078 if (len == -1)
1079 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001080 // "[key]": get key from "var1"
1081 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001082 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001084 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001086 }
1087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001088 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001089
1090 // a NULL dict is equivalent with an empty dict
1091 if (lp->ll_tv->vval.v_dict == NULL)
1092 {
1093 lp->ll_tv->vval.v_dict = dict_alloc();
1094 if (lp->ll_tv->vval.v_dict == NULL)
1095 {
1096 clear_tv(&var1);
1097 return NULL;
1098 }
1099 ++lp->ll_tv->vval.v_dict->dv_refcount;
1100 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001101 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001102
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001103 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001105 // When assigning to a scope dictionary check that a function and
1106 // variable name is valid (only variable name unless it is l: or
1107 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001108 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001109 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001110 int prevval;
1111 int wrong;
1112
1113 if (len != -1)
1114 {
1115 prevval = key[len];
1116 key[len] = NUL;
1117 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001118 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001119 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001120 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1121 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001122 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001123 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001124 if (len != -1)
1125 key[len] = prevval;
1126 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001127 {
1128 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001129 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001130 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001131 }
1132
Bram Moolenaar10c65862020-10-08 21:16:42 +02001133 if (lp->ll_valtype != NULL)
1134 // use the type of the member
1135 lp->ll_valtype = lp->ll_valtype->tt_member;
1136
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001138 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001139 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001140 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001141 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001143 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001144 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001145 return NULL;
1146 }
1147
Bram Moolenaar31b81602019-02-10 22:14:27 +01001148 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001152 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001153 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001155 }
1156 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001157 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001158 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001160 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001162 p = NULL;
1163 break;
1164 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001165 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001166 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001167 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1168 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001169 {
1170 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001171 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001172 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001173
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001174 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001177 else if (lp->ll_tv->v_type == VAR_BLOB)
1178 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001179 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1180
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 /*
1182 * Get the number and item for the only or first index of the List.
1183 */
1184 if (empty1)
1185 lp->ll_n1 = 0;
1186 else
1187 // is number or string
1188 lp->ll_n1 = (long)tv_get_number(&var1);
1189 clear_tv(&var1);
1190
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001191 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001192 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001193 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194 return NULL;
1195 }
1196 if (lp->ll_range && !lp->ll_empty2)
1197 {
1198 lp->ll_n2 = (long)tv_get_number(&var2);
1199 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001200 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1201 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001202 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001203 }
1204 lp->ll_blob = lp->ll_tv->vval.v_blob;
1205 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001206 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001207 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001208 else
1209 {
1210 /*
1211 * Get the number and item for the only or first index of the List.
1212 */
1213 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001215 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001216 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001217 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001218 clear_tv(&var1);
1219
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001220 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001222 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001223 if (lp->ll_li == NULL)
1224 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001225 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001226 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001227 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 }
1230
Bram Moolenaar10c65862020-10-08 21:16:42 +02001231 if (lp->ll_valtype != NULL)
1232 // use the type of the member
1233 lp->ll_valtype = lp->ll_valtype->tt_member;
1234
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 /*
1236 * May need to find the item or absolute index for the second
1237 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 * When no index given: "lp->ll_empty2" is TRUE.
1239 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001240 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001243 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001244 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001249 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001250 {
1251 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001252 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001254 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 }
1257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001258 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 if (lp->ll_n1 < 0)
1260 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1261 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001262 {
1263 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001264 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001266 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001267 }
1268
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 }
1272
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001273 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275 return p;
1276}
1277
1278/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001279 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001282clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283{
1284 vim_free(lp->ll_exp_name);
1285 vim_free(lp->ll_newkey);
1286}
1287
1288/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001289 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001291 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1292 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001293 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001295set_var_lval(
1296 lval_T *lp,
1297 char_u *endp,
1298 typval_T *rettv,
1299 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001300 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1301 char_u *op,
1302 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001303{
1304 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001305 listitem_T *ri;
1306 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307
1308 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001310 cc = *endp;
1311 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001312 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1313 return;
1314
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001315 if (lp->ll_blob != NULL)
1316 {
1317 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001318
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 if (op != NULL && *op != '=')
1320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001321 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001322 return;
1323 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001324 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001325 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326
1327 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1328 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001329 if (lp->ll_empty2)
1330 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1331
Bram Moolenaar68452172021-04-12 21:21:02 +02001332 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1333 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001334 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001335 }
1336 else
1337 {
1338 val = (int)tv_get_number_chk(rettv, &error);
1339 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001340 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001341 }
1342 }
1343 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001345 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001346
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001347 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1348 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001349 {
1350 emsg(_(e_cannot_mod));
1351 *endp = cc;
1352 return;
1353 }
1354
Bram Moolenaarff697e62019-02-12 22:28:33 +01001355 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001356 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001357 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001358 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001359 {
1360 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001361 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1362 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001363 && tv_op(&tv, rettv, op) == OK)
1364 set_var(lp->ll_name, &tv, FALSE);
1365 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001368 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001369 {
1370 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001371 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001372 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001373 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1374 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001375 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001376 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001377 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001378 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001379 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001380 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001381 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001382 else if (lp->ll_range)
1383 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001384 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001385 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001386
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001387 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1388 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001389 {
1390 emsg(_("E996: Cannot lock a range"));
1391 return;
1392 }
1393
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001394 /*
1395 * Check whether any of the list items is locked
1396 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001397 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001398 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001399 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001400 return;
1401 ri = ri->li_next;
1402 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1403 break;
1404 ll_li = ll_li->li_next;
1405 ++ll_n1;
1406 }
1407
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001408 /*
1409 * Assign the List values to the list items.
1410 */
1411 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001412 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001413 if (op != NULL && *op != '=')
1414 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1415 else
1416 {
1417 clear_tv(&lp->ll_li->li_tv);
1418 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1419 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 ri = ri->li_next;
1421 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1422 break;
1423 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001424 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001425 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001426 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001427 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 ri = NULL;
1429 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001430 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001431 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001432 lp->ll_li = lp->ll_li->li_next;
1433 ++lp->ll_n1;
1434 }
1435 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001436 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001437 else if (lp->ll_empty2
1438 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001440 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001441 }
1442 else
1443 {
1444 /*
1445 * Assign to a List or Dictionary item.
1446 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001447 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1448 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001449 {
1450 emsg(_("E996: Cannot lock a list or dict"));
1451 return;
1452 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001453
1454 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001455 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001456 return;
1457
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001458 if (lp->ll_newkey != NULL)
1459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 if (op != NULL && *op != '=')
1461 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001462 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001463 return;
1464 }
1465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001466 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001467 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001468 if (di == NULL)
1469 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001470 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1471 {
1472 vim_free(di);
1473 return;
1474 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001477 else if (op != NULL && *op != '=')
1478 {
1479 tv_op(lp->ll_tv, rettv, op);
1480 return;
1481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001484
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 /*
1486 * Assign the value to the variable or list item.
1487 */
1488 if (copy)
1489 copy_tv(rettv, lp->ll_tv);
1490 else
1491 {
1492 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001493 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 }
1496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497}
1498
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001499/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001500 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1501 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001502 * Returns OK or FAIL.
1503 */
1504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001505tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001507 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 char_u numbuf[NUMBUFLEN];
1509 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001510 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001512 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001513 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001514 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 {
1516 switch (tv1->v_type)
1517 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001518 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001519 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 case VAR_DICT:
1522 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001523 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001524 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001525 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001526 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001527 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001528 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 break;
1530
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001531 case VAR_BLOB:
1532 if (*op != '+' || tv2->v_type != VAR_BLOB)
1533 break;
1534 // BLOB += BLOB
1535 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1536 {
1537 blob_T *b1 = tv1->vval.v_blob;
1538 blob_T *b2 = tv2->vval.v_blob;
1539 int i, len = blob_len(b2);
1540 for (i = 0; i < len; i++)
1541 ga_append(&b1->bv_ga, blob_get(b2, i));
1542 }
1543 return OK;
1544
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545 case VAR_LIST:
1546 if (*op != '+' || tv2->v_type != VAR_LIST)
1547 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001548 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001549 if (tv2->vval.v_list != NULL)
1550 {
1551 if (tv1->vval.v_list == NULL)
1552 {
1553 tv1->vval.v_list = tv2->vval.v_list;
1554 ++tv1->vval.v_list->lv_refcount;
1555 }
1556 else
1557 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1558 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001559 return OK;
1560
1561 case VAR_NUMBER:
1562 case VAR_STRING:
1563 if (tv2->v_type == VAR_LIST)
1564 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001565 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001566 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001567 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001568 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569#ifdef FEAT_FLOAT
1570 if (tv2->v_type == VAR_FLOAT)
1571 {
1572 float_T f = n;
1573
Bram Moolenaarff697e62019-02-12 22:28:33 +01001574 if (*op == '%')
1575 break;
1576 switch (*op)
1577 {
1578 case '+': f += tv2->vval.v_float; break;
1579 case '-': f -= tv2->vval.v_float; break;
1580 case '*': f *= tv2->vval.v_float; break;
1581 case '/': f /= tv2->vval.v_float; break;
1582 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001583 clear_tv(tv1);
1584 tv1->v_type = VAR_FLOAT;
1585 tv1->vval.v_float = f;
1586 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001587 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588#endif
1589 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001590 switch (*op)
1591 {
1592 case '+': n += tv_get_number(tv2); break;
1593 case '-': n -= tv_get_number(tv2); break;
1594 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001595 case '/': n = num_divide(n, tv_get_number(tv2),
1596 &failed); break;
1597 case '%': n = num_modulus(n, tv_get_number(tv2),
1598 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001599 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600 clear_tv(tv1);
1601 tv1->v_type = VAR_NUMBER;
1602 tv1->vval.v_number = n;
1603 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001604 }
1605 else
1606 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001607 if (tv2->v_type == VAR_FLOAT)
1608 break;
1609
Bram Moolenaarff697e62019-02-12 22:28:33 +01001610 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001611 s = tv_get_string(tv1);
1612 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001613 clear_tv(tv1);
1614 tv1->v_type = VAR_STRING;
1615 tv1->vval.v_string = s;
1616 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001617 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001620#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 {
1622 float_T f;
1623
Bram Moolenaarff697e62019-02-12 22:28:33 +01001624 if (*op == '%' || *op == '.'
1625 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001626 && tv2->v_type != VAR_NUMBER
1627 && tv2->v_type != VAR_STRING))
1628 break;
1629 if (tv2->v_type == VAR_FLOAT)
1630 f = tv2->vval.v_float;
1631 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001632 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001633 switch (*op)
1634 {
1635 case '+': tv1->vval.v_float += f; break;
1636 case '-': tv1->vval.v_float -= f; break;
1637 case '*': tv1->vval.v_float *= f; break;
1638 case '/': tv1->vval.v_float /= f; break;
1639 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001642 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 }
1644 }
1645
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001646 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001647 return FAIL;
1648}
1649
1650/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 * Evaluate the expression used in a ":for var in expr" command.
1652 * "arg" points to "var".
1653 * Set "*errp" to TRUE for an error, FALSE otherwise;
1654 * Return a pointer that holds the info. Null when there is an error.
1655 */
1656 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001657eval_for_line(
1658 char_u *arg,
1659 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001660 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001661 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001662{
Bram Moolenaar33570922005-01-25 22:26:29 +00001663 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001665 typval_T tv;
1666 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001667 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001669 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001671 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 if (fi == NULL)
1673 return NULL;
1674
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001675 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676 if (expr == NULL)
1677 return fi;
1678
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001679 expr = skipwhite_and_linebreak(expr, evalarg);
1680 if (expr[0] != 'i' || expr[1] != 'n'
1681 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 return fi;
1685 }
1686
1687 if (skip)
1688 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001689 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1690 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 {
1692 *errp = FALSE;
1693 if (!skip)
1694 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001695 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001696 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001697 l = tv.vval.v_list;
1698 if (l == NULL)
1699 {
1700 // a null list is like an empty list: do nothing
1701 clear_tv(&tv);
1702 }
1703 else
1704 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001706 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001708 // No need to increment the refcount, it's already set for
1709 // the list being used in "tv".
1710 fi->fi_list = l;
1711 list_add_watch(l, &fi->fi_lw);
1712 fi->fi_lw.lw_item = l->lv_first;
1713 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001714 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001716 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001717 fi->fi_bi = 0;
1718 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001719 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001720 typval_T btv;
1721
1722 // Make a copy, so that the iteration still works when the
1723 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001725 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001726 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001727 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001728 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001729 else if (tv.v_type == VAR_STRING)
1730 {
1731 fi->fi_byte_idx = 0;
1732 fi->fi_string = tv.vval.v_string;
1733 tv.vval.v_string = NULL;
1734 if (fi->fi_string == NULL)
1735 fi->fi_string = vim_strsave((char_u *)"");
1736 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 else
1738 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001739 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 }
1742 }
1743 }
1744 if (skip)
1745 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001746 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747
1748 return fi;
1749}
1750
1751/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001752 * Used when looping over a :for line, skip the "in expr" part.
1753 */
1754 void
1755skip_for_lines(void *fi_void, evalarg_T *evalarg)
1756{
1757 forinfo_T *fi = (forinfo_T *)fi_void;
1758 int i;
1759
1760 for (i = 0; i < fi->fi_break_count; ++i)
1761 eval_next_line(evalarg);
1762}
1763
1764/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 * Use the first item in a ":for" list. Advance to the next.
1766 * Assign the values to the variable (list). "arg" points to the first one.
1767 * Return TRUE when a valid item was found, FALSE when at end of list or
1768 * something wrong.
1769 */
1770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001771next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001773 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001775 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1776 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1777 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001778 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001780 if (fi->fi_blob != NULL)
1781 {
1782 typval_T tv;
1783
1784 if (fi->fi_bi >= blob_len(fi->fi_blob))
1785 return FALSE;
1786 tv.v_type = VAR_NUMBER;
1787 tv.v_lock = VAR_FIXED;
1788 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1789 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001790 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001791 fi->fi_varcount, flag, NULL) == OK;
1792 }
1793
1794 if (fi->fi_string != NULL)
1795 {
1796 typval_T tv;
1797 int len;
1798
1799 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1800 if (len == 0)
1801 return FALSE;
1802 tv.v_type = VAR_STRING;
1803 tv.v_lock = VAR_FIXED;
1804 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1805 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001806 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001807 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001808 vim_free(tv.vval.v_string);
1809 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001810 }
1811
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 item = fi->fi_lw.lw_item;
1813 if (item == NULL)
1814 result = FALSE;
1815 else
1816 {
1817 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001818 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001819 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 }
1821 return result;
1822}
1823
1824/*
1825 * Free the structure used to store info used by ":for".
1826 */
1827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829{
Bram Moolenaar33570922005-01-25 22:26:29 +00001830 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001832 if (fi == NULL)
1833 return;
1834 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001835 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001837 list_unref(fi->fi_list);
1838 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001839 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001840 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001841 else
1842 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 vim_free(fi);
1844}
1845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001847set_context_for_expression(
1848 expand_T *xp,
1849 char_u *arg,
1850 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001852 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001854 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001856 if (cmdidx == CMD_let || cmdidx == CMD_var
1857 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 {
1859 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001860 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001862 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001863 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 {
1865 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001866 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001867 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 break;
1869 }
1870 return;
1871 }
1872 }
1873 else
1874 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1875 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 while ((xp->xp_pattern = vim_strpbrk(arg,
1877 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1878 {
1879 c = *xp->xp_pattern;
1880 if (c == '&')
1881 {
1882 c = xp->xp_pattern[1];
1883 if (c == '&')
1884 {
1885 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001886 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001891 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1892 xp->xp_pattern += 2;
1893
1894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 else if (c == '$')
1897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001898 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 xp->xp_context = EXPAND_ENV_VARS;
1900 }
1901 else if (c == '=')
1902 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001903 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 xp->xp_context = EXPAND_EXPRESSION;
1905 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001906 else if (c == '#'
1907 && xp->xp_context == EXPAND_EXPRESSION)
1908 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001909 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001910 break;
1911 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001912 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 && xp->xp_context == EXPAND_FUNCTIONS
1914 && vim_strchr(xp->xp_pattern, '(') == NULL)
1915 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001916 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 break;
1918 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001919 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001921 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1924 if (c == '\\' && xp->xp_pattern[1] != NUL)
1925 ++xp->xp_pattern;
1926 xp->xp_context = EXPAND_NOTHING;
1927 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001928 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001930 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1932 /* skip */ ;
1933 xp->xp_context = EXPAND_NOTHING;
1934 }
1935 else if (c == '|')
1936 {
1937 if (xp->xp_pattern[1] == '|')
1938 {
1939 ++xp->xp_pattern;
1940 xp->xp_context = EXPAND_EXPRESSION;
1941 }
1942 else
1943 xp->xp_context = EXPAND_COMMANDS;
1944 }
1945 else
1946 xp->xp_context = EXPAND_EXPRESSION;
1947 }
1948 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001949 // Doesn't look like something valid, expand as an expression
1950 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 arg = xp->xp_pattern;
1953 if (*arg != NUL)
1954 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1955 /* skip */ ;
1956 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001957
1958 // ":exe one two" completes "two"
1959 if ((cmdidx == CMD_execute
1960 || cmdidx == CMD_echo
1961 || cmdidx == CMD_echon
1962 || cmdidx == CMD_echomsg)
1963 && xp->xp_context == EXPAND_EXPRESSION)
1964 {
1965 for (;;)
1966 {
1967 char_u *n = skiptowhite(arg);
1968
1969 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1970 break;
1971 arg = skipwhite(n);
1972 }
1973 }
1974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 xp->xp_pattern = arg;
1976}
1977
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001979 * Return TRUE if "pat" matches "text".
1980 * Does not use 'cpo' and always uses 'magic'.
1981 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001982 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001983pattern_match(char_u *pat, char_u *text, int ic)
1984{
1985 int matches = FALSE;
1986 char_u *save_cpo;
1987 regmatch_T regmatch;
1988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001989 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001990 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001991 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001992 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1993 if (regmatch.regprog != NULL)
1994 {
1995 regmatch.rm_ic = ic;
1996 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1997 vim_regfree(regmatch.regprog);
1998 }
1999 p_cpo = save_cpo;
2000 return matches;
2001}
2002
2003/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002004 * Handle a name followed by "(". Both for just "name(arg)" and for
2005 * "expr->name(arg)".
2006 * Returns OK or FAIL.
2007 */
2008 static int
2009eval_func(
2010 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002011 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002012 char_u *name,
2013 int name_len,
2014 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002015 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002016 typval_T *basetv) // "expr" for "expr->name(arg)"
2017{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002018 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002019 char_u *s = name;
2020 int len = name_len;
2021 partial_T *partial;
2022 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002023 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002024
2025 if (!evaluate)
2026 check_vars(s, len);
2027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002028 // If "s" is the name of a variable of type VAR_FUNC
2029 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002030 s = deref_func_name(s, &len, &partial,
2031 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002033 // Need to make a copy, in case evaluating the arguments makes
2034 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002035 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002036 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002037 ret = FAIL;
2038 else
2039 {
2040 funcexe_T funcexe;
2041
2042 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002043 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002044 funcexe.firstline = curwin->w_cursor.lnum;
2045 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002046 funcexe.evaluate = evaluate;
2047 funcexe.partial = partial;
2048 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002049 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002050 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002051 }
2052 vim_free(s);
2053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002054 // If evaluate is FALSE rettv->v_type was not set in
2055 // get_func_tv, but it's needed in handle_subscript() to parse
2056 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002057 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2058 {
2059 rettv->vval.v_string = NULL;
2060 rettv->v_type = VAR_FUNC;
2061 }
2062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002063 // Stop the expression evaluation when immediately
2064 // aborting on error, or when an interrupt occurred or
2065 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002066 if (evaluate && aborting())
2067 {
2068 if (ret == OK)
2069 clear_tv(rettv);
2070 ret = FAIL;
2071 }
2072 return ret;
2073}
2074
2075/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002076 * Get the next line source line without advancing. But do skip over comment
2077 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002078 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002079 */
2080 static char_u *
2081getline_peek_skip_comments(evalarg_T *evalarg)
2082{
2083 for (;;)
2084 {
2085 char_u *next = getline_peek(evalarg->eval_getline,
2086 evalarg->eval_cookie);
2087 char_u *p;
2088
2089 if (next == NULL)
2090 break;
2091 p = skipwhite(next);
2092 if (*p != NUL && !vim9_comment_start(p))
2093 return next;
2094 (void)eval_next_line(evalarg);
2095 }
2096 return NULL;
2097}
2098
2099/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002100 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2101 * comment) and there is a next line, return the next line (skipping blanks)
2102 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002103 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2104 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002105 * "arg" must point somewhere inside a line, not at the start.
2106 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002107 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2109{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002110 char_u *p = skipwhite(arg);
2111
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002113 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002114 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002115 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002116 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002117 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002118 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002119
2120 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002121 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002122 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002123 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124
Bram Moolenaar9d489562020-07-30 20:08:50 +02002125 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126 {
2127 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002128 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002129 }
2130 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002131 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002132}
2133
2134/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002135 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002136 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002137 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002138 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002139eval_next_line(evalarg_T *evalarg)
2140{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002141 garray_T *gap = &evalarg->eval_ga;
2142 char_u *line;
2143
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002144 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002145 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2146 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002147 else
2148 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002149 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002150 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2151 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002152 char_u *p = skipwhite(line);
2153
2154 // Going to concatenate the lines after parsing. For an empty or
2155 // comment line use an empty string.
2156 if (*p == NUL || vim9_comment_start(p))
2157 {
2158 vim_free(line);
2159 line = vim_strsave((char_u *)"");
2160 }
2161
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002162 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2163 ++gap->ga_len;
2164 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002165 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002166 {
2167 vim_free(evalarg->eval_tofree);
2168 evalarg->eval_tofree = line;
2169 }
2170 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002171}
2172
Bram Moolenaare6b53242020-07-01 17:28:33 +02002173/*
2174 * Call eval_next_non_blank() and get the next line if needed.
2175 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002176 char_u *
2177skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2178{
2179 int getnext;
2180 char_u *p = skipwhite(arg);
2181
Bram Moolenaar962d7212020-07-04 14:15:00 +02002182 if (evalarg == NULL)
2183 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002184 eval_next_non_blank(p, evalarg, &getnext);
2185 if (getnext)
2186 return eval_next_line(evalarg);
2187 return p;
2188}
2189
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002190/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002191 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002192 */
2193 void
2194clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2195{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002196 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002197 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002198 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002199 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002200 if (eap != NULL)
2201 {
2202 // We may need to keep the original command line, e.g. for
2203 // ":let" it has the variable names. But we may also need the
2204 // new one, "nextcmd" points into it. Keep both.
2205 vim_free(eap->cmdline_tofree);
2206 eap->cmdline_tofree = *eap->cmdlinep;
2207 *eap->cmdlinep = evalarg->eval_tofree;
2208 }
2209 else
2210 vim_free(evalarg->eval_tofree);
2211 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002212 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002213
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002214 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2215 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002216 }
2217}
2218
2219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2223 */
2224
2225/*
2226 * Handle zero level expression.
2227 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002229 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002230 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 * Return OK or FAIL.
2232 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002233 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002234eval0(
2235 char_u *arg,
2236 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002237 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002238 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 int ret;
2241 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002242 int did_emsg_before = did_emsg;
2243 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002244 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002247 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002248 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002249
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002250 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
2252 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 /*
2255 * Report the invalid expression unless the expression evaluation has
2256 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002257 * exception, or we already gave a more specific error.
2258 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002260 if (!aborting()
2261 && did_emsg == did_emsg_before
2262 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002263 && (flags & EVAL_CONSTANT) == 0
2264 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002265 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002266
2267 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002268 // a next command to avoid more errors, unless "|" is following, which
2269 // could only be a command separator.
2270 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2271 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002274
2275 if (eap != NULL)
2276 eap->nextcmd = check_nextcmd(p);
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 return ret;
2279}
2280
2281/*
2282 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002283 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002284 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 *
2286 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002287 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002289 * Note: "rettv.v_lock" is not set.
2290 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 * Return OK or FAIL.
2292 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002293 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002296 char_u *p;
2297 int getnext;
2298
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002299 CLEAR_POINTER(rettv);
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 /*
2302 * Get the first variable.
2303 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002304 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 return FAIL;
2306
Bram Moolenaar793648f2020-06-26 21:28:25 +02002307 p = eval_next_non_blank(*arg, evalarg, &getnext);
2308 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002310 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311 int result;
2312 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002313 evalarg_T *evalarg_used = evalarg;
2314 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002315 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002316 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002317 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318
2319 if (evalarg == NULL)
2320 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002321 CLEAR_FIELD(local_evalarg);
2322 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002323 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002324 orig_flags = evalarg_used->eval_flags;
2325 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002326
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002327 if (getnext)
2328 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002329 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002330 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002331 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002332 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002333 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002334 clear_tv(rettv);
2335 return FAIL;
2336 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002337 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002338 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002341 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002343 int error = FALSE;
2344
Bram Moolenaar13106602020-10-04 16:06:05 +02002345 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002346 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002347 else if (vim9script)
2348 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002349 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002351 if (error || !op_falsy || !result)
2352 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 if (error)
2354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356
2357 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002358 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002360 if (op_falsy)
2361 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002362 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002363 {
mityu4ac198c2021-05-28 17:52:40 +02002364 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002365 clear_tv(rettv);
2366 return FAIL;
2367 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002368 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002369 evalarg_used->eval_flags = (op_falsy ? !result : result)
2370 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2371 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002372 {
2373 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002375 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002376 if (!op_falsy || !result)
2377 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002379 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002381 /*
2382 * Check for the ":".
2383 */
2384 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2385 if (*p != ':')
2386 {
2387 emsg(_(e_missing_colon));
2388 if (evaluate && result)
2389 clear_tv(rettv);
2390 evalarg_used->eval_flags = orig_flags;
2391 return FAIL;
2392 }
2393 if (getnext)
2394 *arg = eval_next_line(evalarg_used);
2395 else
2396 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002397 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002398 {
2399 error_white_both(p, 1);
2400 clear_tv(rettv);
2401 evalarg_used->eval_flags = orig_flags;
2402 return FAIL;
2403 }
2404 *arg = p;
2405 }
2406
2407 /*
2408 * Get the third variable. Recursive!
2409 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002410 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002411 {
mityu4ac198c2021-05-28 17:52:40 +02002412 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002413 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002414 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002415 return FAIL;
2416 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002417 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2418 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002419 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002420 if (eval1(arg, &var2, evalarg_used) == FAIL)
2421 {
2422 if (evaluate && result)
2423 clear_tv(rettv);
2424 evalarg_used->eval_flags = orig_flags;
2425 return FAIL;
2426 }
2427 if (evaluate && !result)
2428 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002430
2431 if (evalarg == NULL)
2432 clear_evalarg(&local_evalarg, NULL);
2433 else
2434 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436
2437 return OK;
2438}
2439
2440/*
2441 * Handle first level expression:
2442 * expr2 || expr2 || expr2 logical OR
2443 *
2444 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002445 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 *
2447 * Return OK or FAIL.
2448 */
2449 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002450eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002452 char_u *p;
2453 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 /*
2456 * Get the first variable.
2457 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002458 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 return FAIL;
2460
2461 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002464 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002465 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002467 evalarg_T *evalarg_used = evalarg;
2468 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 int evaluate;
2470 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002471 long result = FALSE;
2472 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002473 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002474 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002475
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476 if (evalarg == NULL)
2477 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478 CLEAR_FIELD(local_evalarg);
2479 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002480 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 orig_flags = evalarg_used->eval_flags;
2482 evaluate = orig_flags & EVAL_EVALUATE;
2483 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002485 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002486 result = tv_get_bool_chk(rettv, &error);
2487 else if (tv_get_number_chk(rettv, &error) != 0)
2488 result = TRUE;
2489 clear_tv(rettv);
2490 if (error)
2491 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493
2494 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 while (p[0] == '|' && p[1] == '|')
2498 {
2499 if (getnext)
2500 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002501 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002502 {
2503 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2504 {
2505 error_white_both(p, 2);
2506 clear_tv(rettv);
2507 return FAIL;
2508 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002509 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002510 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002511
2512 /*
2513 * Get the second variable.
2514 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002515 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2516 {
mityu4ac198c2021-05-28 17:52:40 +02002517 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002518 clear_tv(rettv);
2519 return FAIL;
2520 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2522 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002523 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002524 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002525 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002526
2527 /*
2528 * Compute the result.
2529 */
2530 if (evaluate && !result)
2531 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002532 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002533 result = tv_get_bool_chk(&var2, &error);
2534 else if (tv_get_number_chk(&var2, &error) != 0)
2535 result = TRUE;
2536 clear_tv(&var2);
2537 if (error)
2538 return FAIL;
2539 }
2540 if (evaluate)
2541 {
2542 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002543 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002544 rettv->v_type = VAR_BOOL;
2545 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002546 }
2547 else
2548 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002549 rettv->v_type = VAR_NUMBER;
2550 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002551 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002552 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002553
2554 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002556
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002557 if (evalarg == NULL)
2558 clear_evalarg(&local_evalarg, NULL);
2559 else
2560 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562
2563 return OK;
2564}
2565
2566/*
2567 * Handle second level expression:
2568 * expr3 && expr3 && expr3 logical AND
2569 *
2570 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002571 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 *
2573 * Return OK or FAIL.
2574 */
2575 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002576eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002578 char_u *p;
2579 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 /*
2582 * Get the first variable.
2583 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002584 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 return FAIL;
2586
2587 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002590 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002593 evalarg_T *evalarg_used = evalarg;
2594 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002595 int orig_flags;
2596 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002597 long result = TRUE;
2598 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002599 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002600 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002601
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002602 if (evalarg == NULL)
2603 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604 CLEAR_FIELD(local_evalarg);
2605 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002606 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002607 orig_flags = evalarg_used->eval_flags;
2608 evaluate = orig_flags & EVAL_EVALUATE;
2609 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002610 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002611 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002612 result = tv_get_bool_chk(rettv, &error);
2613 else if (tv_get_number_chk(rettv, &error) == 0)
2614 result = FALSE;
2615 clear_tv(rettv);
2616 if (error)
2617 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
2619
2620 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623 while (p[0] == '&' && p[1] == '&')
2624 {
2625 if (getnext)
2626 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002627 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002628 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002629 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002630 {
2631 error_white_both(p, 2);
2632 clear_tv(rettv);
2633 return FAIL;
2634 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002635 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002636 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637
2638 /*
2639 * Get the second variable.
2640 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002641 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2642 {
mityu4ac198c2021-05-28 17:52:40 +02002643 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002644 clear_tv(rettv);
2645 return FAIL;
2646 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2648 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002649 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002650 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002651 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002652 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653
2654 /*
2655 * Compute the result.
2656 */
2657 if (evaluate && result)
2658 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002659 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002660 result = tv_get_bool_chk(&var2, &error);
2661 else if (tv_get_number_chk(&var2, &error) == 0)
2662 result = FALSE;
2663 clear_tv(&var2);
2664 if (error)
2665 return FAIL;
2666 }
2667 if (evaluate)
2668 {
2669 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002670 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002671 rettv->v_type = VAR_BOOL;
2672 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002673 }
2674 else
2675 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002676 rettv->v_type = VAR_NUMBER;
2677 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002678 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002679 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002680
2681 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002683
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002684 if (evalarg == NULL)
2685 clear_evalarg(&local_evalarg, NULL);
2686 else
2687 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689
2690 return OK;
2691}
2692
2693/*
2694 * Handle third level expression:
2695 * var1 == var2
2696 * var1 =~ var2
2697 * var1 != var2
2698 * var1 !~ var2
2699 * var1 > var2
2700 * var1 >= var2
2701 * var1 < var2
2702 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002703 * var1 is var2
2704 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 *
2706 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002707 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 *
2709 * Return OK or FAIL.
2710 */
2711 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002712eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002715 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002716 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002718 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 /*
2721 * Get the first variable.
2722 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002723 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 return FAIL;
2725
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002726 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002727 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002730 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002732 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002734 typval_T var2;
2735 int ic;
2736 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002737 int evaluate = evalarg == NULL
2738 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002739
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002740 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002741 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002742 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002743 p = *arg;
2744 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002745 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2746 {
mityu4ac198c2021-05-28 17:52:40 +02002747 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002748 clear_tv(rettv);
2749 return FAIL;
2750 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002751
Bram Moolenaar696ba232020-07-29 21:20:41 +02002752 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2753 {
2754 semsg(_(e_invexpr2), p);
2755 clear_tv(rettv);
2756 return FAIL;
2757 }
2758
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002759 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (p[len] == '?')
2761 {
2762 ic = TRUE;
2763 ++len;
2764 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002765 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else if (p[len] == '#')
2767 {
2768 ic = FALSE;
2769 ++len;
2770 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002771 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002773 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
2775 /*
2776 * Get the second variable.
2777 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002778 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2779 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002780 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002781 clear_tv(rettv);
2782 return FAIL;
2783 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002784 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002785 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002787 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return FAIL;
2789 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002790 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002791 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002792 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002793
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002794 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002795 {
2796 ret = FAIL;
2797 clear_tv(rettv);
2798 }
2799 else
2800 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002801 clear_tv(&var2);
2802 return ret;
2803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 }
2805
2806 return OK;
2807}
2808
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002809/*
2810 * Make a copy of blob "tv1" and append blob "tv2".
2811 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 void
2813eval_addblob(typval_T *tv1, typval_T *tv2)
2814{
2815 blob_T *b1 = tv1->vval.v_blob;
2816 blob_T *b2 = tv2->vval.v_blob;
2817 blob_T *b = blob_alloc();
2818 int i;
2819
2820 if (b != NULL)
2821 {
2822 for (i = 0; i < blob_len(b1); i++)
2823 ga_append(&b->bv_ga, blob_get(b1, i));
2824 for (i = 0; i < blob_len(b2); i++)
2825 ga_append(&b->bv_ga, blob_get(b2, i));
2826
2827 clear_tv(tv1);
2828 rettv_blob_set(tv1, b);
2829 }
2830}
2831
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002832/*
2833 * Make a copy of list "tv1" and append list "tv2".
2834 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 int
2836eval_addlist(typval_T *tv1, typval_T *tv2)
2837{
2838 typval_T var3;
2839
2840 // concatenate Lists
2841 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2842 {
2843 clear_tv(tv1);
2844 clear_tv(tv2);
2845 return FAIL;
2846 }
2847 clear_tv(tv1);
2848 *tv1 = var3;
2849 return OK;
2850}
2851
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852/*
2853 * Handle fourth level expression:
2854 * + number addition
2855 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002856 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002857 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 *
2859 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002860 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 *
2862 * Return OK or FAIL.
2863 */
2864 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002865eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 /*
2868 * Get the first variable.
2869 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002870 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 return FAIL;
2872
2873 /*
2874 * Repeat computing, until no '+', '-' or '.' is following.
2875 */
2876 for (;;)
2877 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002878 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002879 int getnext;
2880 char_u *p;
2881 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002882 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002883 int concat;
2884 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002885 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002886
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002887 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002888 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002889 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002890 p = eval_next_non_blank(*arg, evalarg, &getnext);
2891 op = *p;
2892 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002893 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2894 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002896 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2897 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002898
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002899 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002900 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002901 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002902 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002903 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002904 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002905 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002906 {
mityu4ac198c2021-05-28 17:52:40 +02002907 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002908 clear_tv(rettv);
2909 return FAIL;
2910 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002911 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002912 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002913 if ((op != '+' || (rettv->v_type != VAR_LIST
2914 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002915#ifdef FEAT_FLOAT
2916 && (op == '.' || rettv->v_type != VAR_FLOAT)
2917#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002918 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002919 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002920 int error = FALSE;
2921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002922 // For "list + ...", an illegal use of the first operand as
2923 // a number cannot be determined before evaluating the 2nd
2924 // operand: if this is also a list, all is ok.
2925 // For "something . ...", "something - ..." or "non-list + ...",
2926 // we know that the first operand needs to be a string or number
2927 // without evaluating the 2nd operand. So check before to avoid
2928 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002929 if (op != '.')
2930 tv_get_number_chk(rettv, &error);
2931 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002932 {
2933 clear_tv(rettv);
2934 return FAIL;
2935 }
2936 }
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 /*
2939 * Get the second variable.
2940 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002941 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002942 {
mityu89dcb4d2021-05-28 13:50:17 +02002943 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002944 clear_tv(rettv);
2945 return FAIL;
2946 }
2947 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002948 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002950 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 return FAIL;
2952 }
2953
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002954 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
2956 /*
2957 * Compute the result.
2958 */
2959 if (op == '.')
2960 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002961 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2962 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002963 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002964
Bram Moolenaar2e086612020-08-25 22:37:48 +02002965 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002966 || var2.v_type == VAR_CHANNEL
2967 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002968 semsg(_(e_using_invalid_value_as_string_str),
2969 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002970#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002971 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002972 {
2973 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2974 var2.vval.v_float);
2975 s2 = buf2;
2976 }
2977#endif
2978 else
2979 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002980 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002981 {
2982 clear_tv(rettv);
2983 clear_tv(&var2);
2984 return FAIL;
2985 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002987 clear_tv(rettv);
2988 rettv->v_type = VAR_STRING;
2989 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002991 else if (op == '+' && rettv->v_type == VAR_BLOB
2992 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002994 else if (op == '+' && rettv->v_type == VAR_LIST
2995 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002996 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002998 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 else
3001 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003002 int error = FALSE;
3003 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003004#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003005 float_T f1 = 0, f2 = 0;
3006
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003007 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003008 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003009 f1 = rettv->vval.v_float;
3010 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003012 else
3013#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003014 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003015 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016 if (error)
3017 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003018 // This can only happen for "list + non-list" or
3019 // "blob + non-blob". For "non-list + ..." or
3020 // "something - ...", we returned before evaluating the
3021 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003022 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003023 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024 return FAIL;
3025 }
3026#ifdef FEAT_FLOAT
3027 if (var2.v_type == VAR_FLOAT)
3028 f1 = n1;
3029#endif
3030 }
3031#ifdef FEAT_FLOAT
3032 if (var2.v_type == VAR_FLOAT)
3033 {
3034 f2 = var2.vval.v_float;
3035 n2 = 0;
3036 }
3037 else
3038#endif
3039 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003040 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003041 if (error)
3042 {
3043 clear_tv(rettv);
3044 clear_tv(&var2);
3045 return FAIL;
3046 }
3047#ifdef FEAT_FLOAT
3048 if (rettv->v_type == VAR_FLOAT)
3049 f2 = n2;
3050#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003053
3054#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003055 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003056 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3057 {
3058 if (op == '+')
3059 f1 = f1 + f2;
3060 else
3061 f1 = f1 - f2;
3062 rettv->v_type = VAR_FLOAT;
3063 rettv->vval.v_float = f1;
3064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066#endif
3067 {
3068 if (op == '+')
3069 n1 = n1 + n2;
3070 else
3071 n1 = n1 - n2;
3072 rettv->v_type = VAR_NUMBER;
3073 rettv->vval.v_number = n1;
3074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
3078 }
3079 return OK;
3080}
3081
3082/*
3083 * Handle fifth level expression:
3084 * * number multiplication
3085 * / number division
3086 * % number modulo
3087 *
3088 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003089 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 *
3091 * Return OK or FAIL.
3092 */
3093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003094eval6(
3095 char_u **arg,
3096 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003097 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003098 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003100#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003101 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
3104 /*
3105 * Get the first variable.
3106 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003107 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 return FAIL;
3109
3110 /*
3111 * Repeat computing, until no '*', '/' or '%' is following.
3112 */
3113 for (;;)
3114 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003115 int evaluate;
3116 int getnext;
3117 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003118 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003119 int op;
3120 varnumber_T n1, n2;
3121#ifdef FEAT_FLOAT
3122 float_T f1, f2;
3123#endif
3124 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003125
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003126 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003127 p = eval_next_non_blank(*arg, evalarg, &getnext);
3128 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003129 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003131
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003132 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003133 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003134 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003135 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003136 {
3137 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3138 {
mityu4ac198c2021-05-28 17:52:40 +02003139 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003140 clear_tv(rettv);
3141 return FAIL;
3142 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003143 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003146#ifdef FEAT_FLOAT
3147 f1 = 0;
3148 f2 = 0;
3149#endif
3150 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003151 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153#ifdef FEAT_FLOAT
3154 if (rettv->v_type == VAR_FLOAT)
3155 {
3156 f1 = rettv->vval.v_float;
3157 use_float = TRUE;
3158 n1 = 0;
3159 }
3160 else
3161#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003162 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003163 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003164 if (error)
3165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
3167 else
3168 n1 = 0;
3169
3170 /*
3171 * Get the second variable.
3172 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003173 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3174 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003175 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003176 clear_tv(rettv);
3177 return FAIL;
3178 }
3179 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003180 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 return FAIL;
3182
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003183 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003185#ifdef FEAT_FLOAT
3186 if (var2.v_type == VAR_FLOAT)
3187 {
3188 if (!use_float)
3189 {
3190 f1 = n1;
3191 use_float = TRUE;
3192 }
3193 f2 = var2.vval.v_float;
3194 n2 = 0;
3195 }
3196 else
3197#endif
3198 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003199 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003200 clear_tv(&var2);
3201 if (error)
3202 return FAIL;
3203#ifdef FEAT_FLOAT
3204 if (use_float)
3205 f2 = n2;
3206#endif
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 /*
3210 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003211 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003213#ifdef FEAT_FLOAT
3214 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003216 if (op == '*')
3217 f1 = f1 * f2;
3218 else if (op == '/')
3219 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003220# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003221 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003222 if (f2 == 0.0)
3223 {
3224 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003225 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003226 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003227 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003228 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003229 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003230 }
3231 else
3232 f1 = f1 / f2;
3233# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003234 // We rely on the floating point library to handle divide
3235 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003236 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003237# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003240 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003242 return FAIL;
3243 }
3244 rettv->v_type = VAR_FLOAT;
3245 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003250 int failed = FALSE;
3251
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003252 if (op == '*')
3253 n1 = n1 * n2;
3254 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003255 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003257 n1 = num_modulus(n1, n2, &failed);
3258 if (failed)
3259 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003260
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003261 rettv->v_type = VAR_NUMBER;
3262 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
3265 }
3266
3267 return OK;
3268}
3269
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003270/*
3271 * Handle a type cast before a base level expression.
3272 * "arg" must point to the first non-white of the expression.
3273 * "arg" is advanced to just after the recognized expression.
3274 * Return OK or FAIL.
3275 */
3276 static int
3277eval7t(
3278 char_u **arg,
3279 typval_T *rettv,
3280 evalarg_T *evalarg,
3281 int want_string) // after "." operator
3282{
3283 type_T *want_type = NULL;
3284 garray_T type_list; // list of pointers to allocated types
3285 int res;
3286 int evaluate = evalarg == NULL ? 0
3287 : (evalarg->eval_flags & EVAL_EVALUATE);
3288
3289 // Recognize <type> in Vim9 script only.
3290 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3291 {
3292 ++*arg;
3293 ga_init2(&type_list, sizeof(type_T *), 10);
3294 want_type = parse_type(arg, &type_list, TRUE);
3295 if (want_type == NULL && (evaluate || **arg != '>'))
3296 {
3297 clear_type_list(&type_list);
3298 return FAIL;
3299 }
3300
3301 if (**arg != '>')
3302 {
3303 if (*skipwhite(*arg) == '>')
3304 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3305 else
3306 emsg(_(e_missing_gt));
3307 clear_type_list(&type_list);
3308 return FAIL;
3309 }
3310 ++*arg;
3311 *arg = skipwhite_and_linebreak(*arg, evalarg);
3312 }
3313
3314 res = eval7(arg, rettv, evalarg, want_string);
3315
3316 if (want_type != NULL && evaluate)
3317 {
3318 if (res == OK)
3319 {
3320 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3321
3322 if (!equal_type(want_type, actual))
3323 {
3324 if (want_type == &t_bool && actual != &t_bool
3325 && (actual->tt_flags & TTFLAG_BOOL_OK))
3326 {
3327 int n = tv2bool(rettv);
3328
3329 // can use "0" and "1" for boolean in some places
3330 clear_tv(rettv);
3331 rettv->v_type = VAR_BOOL;
3332 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3333 }
3334 else
3335 {
3336 where_T where;
3337
3338 where.wt_index = 0;
3339 where.wt_variable = TRUE;
3340 res = check_type(want_type, actual, TRUE, where);
3341 }
3342 }
3343 }
3344 clear_type_list(&type_list);
3345 }
3346
3347 return res;
3348}
3349
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003350 int
3351eval_leader(char_u **arg, int vim9)
3352{
3353 char_u *s = *arg;
3354 char_u *p = *arg;
3355
3356 while (*p == '!' || *p == '-' || *p == '+')
3357 {
3358 char_u *n = skipwhite(p + 1);
3359
3360 // ++, --, -+ and +- are not accepted in Vim9 script
3361 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3362 {
3363 semsg(_(e_invexpr2), s);
3364 return FAIL;
3365 }
3366 p = n;
3367 }
3368 *arg = p;
3369 return OK;
3370}
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372/*
3373 * Handle sixth level expression:
3374 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003375 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003376 * "string" string constant
3377 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 * &option-name option value
3379 * @r register contents
3380 * identifier variable value
3381 * function() function call
3382 * $VAR environment variable
3383 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003384 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003386 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003387 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 *
3389 * Also handle:
3390 * ! in front logical NOT
3391 * - in front unary minus
3392 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003393 * trailing [] subscript in String or List
3394 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003395 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 *
3397 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003398 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 *
3400 * Return OK or FAIL.
3401 */
3402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003403eval7(
3404 char_u **arg,
3405 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003406 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003409 int evaluate = evalarg != NULL
3410 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 int len;
3412 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 char_u *start_leader, *end_leader;
3414 int ret = OK;
3415 char_u *alias;
3416
3417 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003418 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003419 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003421 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003424 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 */
3426 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003427 if (eval_leader(arg, in_vim9script()) == FAIL)
3428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 end_leader = *arg;
3430
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003431 if (**arg == '.' && (!isdigit(*(*arg + 1))
3432#ifdef FEAT_FLOAT
3433 || current_sctx.sc_version < 2
3434#endif
3435 ))
3436 {
3437 semsg(_(e_invexpr2), *arg);
3438 ++*arg;
3439 return FAIL;
3440 }
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 switch (**arg)
3443 {
3444 /*
3445 * Number constant.
3446 */
3447 case '0':
3448 case '1':
3449 case '2':
3450 case '3':
3451 case '4':
3452 case '5':
3453 case '6':
3454 case '7':
3455 case '8':
3456 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003457 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003458
3459 // Apply prefixed "-" and "+" now. Matters especially when
3460 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003461 if (ret == OK && evaluate && end_leader > start_leader
3462 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003463 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 break;
3465
3466 /*
3467 * String constant: "string".
3468 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003469 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 break;
3471
3472 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003473 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003475 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003476 break;
3477
3478 /*
3479 * List: [expr, expr]
3480 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003481 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 break;
3483
3484 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003485 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003486 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003487 case '#': if (in_vim9script())
3488 {
3489 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3490 }
3491 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003492 {
3493 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003494 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003495 }
3496 else
3497 ret = NOTDONE;
3498 break;
3499
3500 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003501 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003502 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003503 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003504 case '{': if (in_vim9script())
3505 ret = NOTDONE;
3506 else
3507 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003508 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003509 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003510 break;
3511
3512 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003513 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003515 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 break;
3517
3518 /*
3519 * Environment variable: $VAR.
3520 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003521 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 break;
3523
3524 /*
3525 * Register contents: @r.
3526 */
3527 case '@': ++*arg;
3528 if (evaluate)
3529 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003530 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3531 semsg(_(e_syntax_error_at_str), *arg);
3532 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3533 emsg_invreg(**arg);
3534 else
3535 {
3536 rettv->v_type = VAR_STRING;
3537 rettv->vval.v_string = get_reg_contents(**arg,
3538 GREG_EXPR_SRC);
3539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541 if (**arg != NUL)
3542 ++*arg;
3543 break;
3544
3545 /*
3546 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003547 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003549 case '(': ret = NOTDONE;
3550 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003551 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003552 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003553 if (ret == OK && evaluate)
3554 {
3555 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3556
3557 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003558 if (compile_def_function(ufunc,
3559 TRUE, PROFILING(ufunc), NULL) == FAIL)
3560 {
3561 clear_tv(rettv);
3562 ret = FAIL;
3563 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003564 }
3565 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003566 if (ret == NOTDONE)
3567 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003568 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003569 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003570
Bram Moolenaar9215f012020-06-27 21:18:00 +02003571 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003572 if (**arg == ')')
3573 ++*arg;
3574 else if (ret == OK)
3575 {
3576 emsg(_(e_missing_close));
3577 clear_tv(rettv);
3578 ret = FAIL;
3579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581 break;
3582
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 break;
3585 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003586
3587 if (ret == NOTDONE)
3588 {
3589 /*
3590 * Must be a variable or function name.
3591 * Can also be a curly-braces kind of name: {expr}.
3592 */
3593 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003594 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 if (alias != NULL)
3596 s = alias;
3597
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003598 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 ret = FAIL;
3600 else
3601 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003602 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3603
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003604 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003605 {
3606 emsg(_(e_cannot_use_underscore_here));
3607 ret = FAIL;
3608 }
3609 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003610 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003611 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003612 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003613 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003614 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003615 else if (flags & EVAL_CONSTANT)
3616 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003618 {
3619 // get the value of "true", "false" or a variable
3620 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3621 {
3622 rettv->v_type = VAR_BOOL;
3623 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003624 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003625 }
3626 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003627 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003628 {
3629 rettv->v_type = VAR_BOOL;
3630 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003631 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003632 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003633 else if (len == 4 && in_vim9script()
3634 && STRNCMP(s, "null", 4) == 0)
3635 {
3636 rettv->v_type = VAR_SPECIAL;
3637 rettv->vval.v_number = VVAL_NULL;
3638 ret = OK;
3639 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003640 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003641 ret = eval_variable(s, len, rettv, NULL,
3642 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003643 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003644 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003645 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003646 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003647 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003648 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003649 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003651 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003652 }
3653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003654 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3655 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003656 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003657 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
3659 /*
3660 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3661 */
3662 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003663 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003664 return ret;
3665}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003666
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003667/*
3668 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003669 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003670 * Adjusts "end_leaderp" until it is at "start_leader".
3671 */
3672 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003673eval7_leader(
3674 typval_T *rettv,
3675 int numeric_only,
3676 char_u *start_leader,
3677 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003678{
3679 char_u *end_leader = *end_leaderp;
3680 int ret = OK;
3681 int error = FALSE;
3682 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003683 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003684#ifdef FEAT_FLOAT
3685 float_T f = 0.0;
3686
3687 if (rettv->v_type == VAR_FLOAT)
3688 f = rettv->vval.v_float;
3689 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003690#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003691 {
3692 while (VIM_ISWHITE(end_leader[-1]))
3693 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003694 if (in_vim9script() && end_leader[-1] == '!')
3695 val = tv2bool(rettv);
3696 else
3697 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003698 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003699 if (error)
3700 {
3701 clear_tv(rettv);
3702 ret = FAIL;
3703 }
3704 else
3705 {
3706 while (end_leader > start_leader)
3707 {
3708 --end_leader;
3709 if (*end_leader == '!')
3710 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003711 if (numeric_only)
3712 {
3713 ++end_leader;
3714 break;
3715 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003716#ifdef FEAT_FLOAT
3717 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003718 {
3719 if (in_vim9script())
3720 {
3721 rettv->v_type = VAR_BOOL;
3722 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3723 }
3724 else
3725 f = !f;
3726 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003727 else
3728#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003729 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003730 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003731 type = VAR_BOOL;
3732 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003733 }
3734 else if (*end_leader == '-')
3735 {
3736#ifdef FEAT_FLOAT
3737 if (rettv->v_type == VAR_FLOAT)
3738 f = -f;
3739 else
3740#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003741 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003742 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003743 type = VAR_NUMBER;
3744 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003745 }
3746 }
3747#ifdef FEAT_FLOAT
3748 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003750 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003751 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003754#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003755 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003756 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003757 if (in_vim9script())
3758 rettv->v_type = type;
3759 else
3760 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003761 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003764 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return ret;
3766}
3767
3768/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003769 * Call the function referred to in "rettv".
3770 */
3771 static int
3772call_func_rettv(
3773 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003774 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003775 typval_T *rettv,
3776 int evaluate,
3777 dict_T *selfdict,
3778 typval_T *basetv)
3779{
3780 partial_T *pt = NULL;
3781 funcexe_T funcexe;
3782 typval_T functv;
3783 char_u *s;
3784 int ret;
3785
3786 // need to copy the funcref so that we can clear rettv
3787 if (evaluate)
3788 {
3789 functv = *rettv;
3790 rettv->v_type = VAR_UNKNOWN;
3791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003792 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003793 if (functv.v_type == VAR_PARTIAL)
3794 {
3795 pt = functv.vval.v_partial;
3796 s = partial_name(pt);
3797 }
3798 else
3799 s = functv.vval.v_string;
3800 }
3801 else
3802 s = (char_u *)"";
3803
Bram Moolenaara80faa82020-04-12 19:37:17 +02003804 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003805 funcexe.firstline = curwin->w_cursor.lnum;
3806 funcexe.lastline = curwin->w_cursor.lnum;
3807 funcexe.evaluate = evaluate;
3808 funcexe.partial = pt;
3809 funcexe.selfdict = selfdict;
3810 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003811 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003813 // Clear the funcref afterwards, so that deleting it while
3814 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003815 if (evaluate)
3816 clear_tv(&functv);
3817
3818 return ret;
3819}
3820
3821/*
3822 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003823 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003824 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3825 */
3826 static int
3827eval_lambda(
3828 char_u **arg,
3829 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003830 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003831 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003832{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003833 int evaluate = evalarg != NULL
3834 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003835 typval_T base = *rettv;
3836 int ret;
3837
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003838 rettv->v_type = VAR_UNKNOWN;
3839
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003840 if (**arg == '{')
3841 {
3842 // ->{lambda}()
3843 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3844 }
3845 else
3846 {
3847 // ->(lambda)()
3848 ++*arg;
3849 ret = eval1(arg, rettv, evalarg);
3850 *arg = skipwhite_and_linebreak(*arg, evalarg);
3851 if (**arg != ')')
3852 {
3853 emsg(_(e_missing_close));
3854 ret = FAIL;
3855 }
3856 ++*arg;
3857 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003858 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003859 return FAIL;
3860 else if (**arg != '(')
3861 {
3862 if (verbose)
3863 {
3864 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003865 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003866 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003868 }
3869 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003870 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003871 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003872 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003873 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003874
3875 // Clear the funcref afterwards, so that deleting it while
3876 // evaluating the arguments is possible (see test55).
3877 if (evaluate)
3878 clear_tv(&base);
3879
3880 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003881}
3882
3883/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003884 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003885 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003886 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3887 */
3888 static int
3889eval_method(
3890 char_u **arg,
3891 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003892 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003893 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003894{
3895 char_u *name;
3896 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003897 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003898 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003899 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003900 int evaluate = evalarg != NULL
3901 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003902
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003903 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003904
Bram Moolenaarac92e252019-08-03 21:58:38 +02003905 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003906 len = get_name_len(arg, &alias, evaluate, TRUE);
3907 if (alias != NULL)
3908 name = alias;
3909
3910 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003911 {
3912 if (verbose)
3913 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003914 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003915 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003916 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003917 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003918 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003919 if (**arg != '(')
3920 {
3921 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003923 ret = FAIL;
3924 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003925 else if (VIM_ISWHITE((*arg)[-1]))
3926 {
3927 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003928 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003929 ret = FAIL;
3930 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003931 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003932 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003933 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003934 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003935
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003936 // Clear the funcref afterwards, so that deleting it while
3937 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003938 if (evaluate)
3939 clear_tv(&base);
3940
Bram Moolenaarac92e252019-08-03 21:58:38 +02003941 return ret;
3942}
3943
3944/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003945 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3946 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003947 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3948 */
3949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003950eval_index(
3951 char_u **arg,
3952 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003953 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003954 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003955{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003956 int evaluate = evalarg != NULL
3957 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003958 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003960 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003961 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003962 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003963 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003964
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003965 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3966 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003967
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003968 init_tv(&var1);
3969 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003970 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003971 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003972 /*
3973 * dict.name
3974 */
3975 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003976 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003977 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003978 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003979 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003980 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003981 }
3982 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003983 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003984 /*
3985 * something[idx]
3986 *
3987 * Get the (first) variable from inside the [].
3988 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003989 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003990 if (**arg == ':')
3991 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003992 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003993 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003994 else if (vim9 && **arg == ':')
3995 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003996 semsg(_(e_white_space_required_before_and_after_str_at_str),
3997 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003998 clear_tv(&var1);
3999 return FAIL;
4000 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004001 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004002 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004003#ifdef FEAT_FLOAT
4004 // allow for indexing with float
4005 if (vim9 && rettv->v_type == VAR_DICT
4006 && var1.v_type == VAR_FLOAT)
4007 {
4008 var1.vval.v_string = typval_tostring(&var1, TRUE);
4009 var1.v_type = VAR_STRING;
4010 }
4011#endif
4012 if (tv_get_string_chk(&var1) == NULL)
4013 {
4014 // not a number or string
4015 clear_tv(&var1);
4016 return FAIL;
4017 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004018 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004019
4020 /*
4021 * Get the second variable from inside the [:].
4022 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004023 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004024 if (**arg == ':')
4025 {
4026 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004027 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004028 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004029 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004030 semsg(_(e_white_space_required_before_and_after_str_at_str),
4031 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004032 if (!empty1)
4033 clear_tv(&var1);
4034 return FAIL;
4035 }
4036 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004037 if (**arg == ']')
4038 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004039 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004041 if (!empty1)
4042 clear_tv(&var1);
4043 return FAIL;
4044 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004045 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004047 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004048 if (!empty1)
4049 clear_tv(&var1);
4050 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004051 return FAIL;
4052 }
4053 }
4054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004055 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004056 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004057 if (**arg != ']')
4058 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004059 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004060 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004061 clear_tv(&var1);
4062 if (range)
4063 clear_tv(&var2);
4064 return FAIL;
4065 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004066 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004067 }
4068
4069 if (evaluate)
4070 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004071 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004072 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004073 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004074
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004075 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004076 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004077 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004078 clear_tv(&var2);
4079 return res;
4080 }
4081 return OK;
4082}
4083
4084/*
4085 * Check if "rettv" can have an [index] or [sli:ce]
4086 */
4087 int
4088check_can_index(typval_T *rettv, int evaluate, int verbose)
4089{
4090 switch (rettv->v_type)
4091 {
4092 case VAR_FUNC:
4093 case VAR_PARTIAL:
4094 if (verbose)
4095 emsg(_("E695: Cannot index a Funcref"));
4096 return FAIL;
4097 case VAR_FLOAT:
4098#ifdef FEAT_FLOAT
4099 if (verbose)
4100 emsg(_(e_float_as_string));
4101 return FAIL;
4102#endif
4103 case VAR_BOOL:
4104 case VAR_SPECIAL:
4105 case VAR_JOB:
4106 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004107 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004108 if (verbose)
4109 emsg(_(e_cannot_index_special_variable));
4110 return FAIL;
4111 case VAR_UNKNOWN:
4112 case VAR_ANY:
4113 case VAR_VOID:
4114 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004115 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004116 emsg(_(e_cannot_index_special_variable));
4117 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004118 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004119 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004120
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004121 case VAR_STRING:
4122 case VAR_LIST:
4123 case VAR_DICT:
4124 case VAR_BLOB:
4125 break;
4126 case VAR_NUMBER:
4127 if (in_vim9script())
4128 emsg(_(e_cannot_index_number));
4129 break;
4130 }
4131 return OK;
4132}
4133
4134/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004135 * slice() function
4136 */
4137 void
4138f_slice(typval_T *argvars, typval_T *rettv)
4139{
4140 if (check_can_index(argvars, TRUE, FALSE) == OK)
4141 {
4142 copy_tv(argvars, rettv);
4143 eval_index_inner(rettv, TRUE, argvars + 1,
4144 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4145 TRUE, NULL, 0, FALSE);
4146 }
4147}
4148
4149/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004150 * Apply index or range to "rettv".
4151 * "var1" is the first index, NULL for [:expr].
4152 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004153 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4154 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004155 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4156 */
4157 int
4158eval_index_inner(
4159 typval_T *rettv,
4160 int is_range,
4161 typval_T *var1,
4162 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004163 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004164 char_u *key,
4165 int keylen,
4166 int verbose)
4167{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004168 varnumber_T n1, n2 = 0;
4169 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004170
4171 n1 = 0;
4172 if (var1 != NULL && rettv->v_type != VAR_DICT)
4173 n1 = tv_get_number(var1);
4174
4175 if (is_range)
4176 {
4177 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004178 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004179 if (verbose)
4180 emsg(_(e_cannot_slice_dictionary));
4181 return FAIL;
4182 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004183 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004184 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004185 else
4186 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004187 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004188
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004189 switch (rettv->v_type)
4190 {
4191 case VAR_UNKNOWN:
4192 case VAR_ANY:
4193 case VAR_VOID:
4194 case VAR_FUNC:
4195 case VAR_PARTIAL:
4196 case VAR_FLOAT:
4197 case VAR_BOOL:
4198 case VAR_SPECIAL:
4199 case VAR_JOB:
4200 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004201 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004202 break; // not evaluating, skipping over subscript
4203
4204 case VAR_NUMBER:
4205 case VAR_STRING:
4206 {
4207 char_u *s = tv_get_string(rettv);
4208
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004209 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004210 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004211 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004212 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004213 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004214 else
4215 s = char_from_string(s, n1);
4216 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004217 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004218 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004219 // The resulting variable is a substring. If the indexes
4220 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004221 if (n1 < 0)
4222 {
4223 n1 = len + n1;
4224 if (n1 < 0)
4225 n1 = 0;
4226 }
4227 if (n2 < 0)
4228 n2 = len + n2;
4229 else if (n2 >= len)
4230 n2 = len;
4231 if (n1 >= len || n2 < 0 || n1 > n2)
4232 s = NULL;
4233 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004234 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235 }
4236 else
4237 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004238 // The resulting variable is a string of a single
4239 // character. If the index is too big or negative the
4240 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004241 if (n1 >= len || n1 < 0)
4242 s = NULL;
4243 else
4244 s = vim_strnsave(s + n1, 1);
4245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 clear_tv(rettv);
4247 rettv->v_type = VAR_STRING;
4248 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004249 }
4250 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004251
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004252 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004253 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4254 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004255 break;
4256
4257 case VAR_LIST:
4258 if (var1 == NULL)
4259 n1 = 0;
4260 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004261 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004262 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004263 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004264 return FAIL;
4265 break;
4266
4267 case VAR_DICT:
4268 {
4269 dictitem_T *item;
4270 typval_T tmp;
4271
4272 if (key == NULL)
4273 {
4274 key = tv_get_string_chk(var1);
4275 if (key == NULL)
4276 return FAIL;
4277 }
4278
4279 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4280
4281 if (item == NULL && verbose)
4282 semsg(_(e_dictkey), key);
4283 if (item == NULL)
4284 return FAIL;
4285
4286 copy_tv(&item->di_tv, &tmp);
4287 clear_tv(rettv);
4288 *rettv = tmp;
4289 }
4290 break;
4291 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004292 return OK;
4293}
4294
4295/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004296 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004297 */
4298 char_u *
4299partial_name(partial_T *pt)
4300{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004301 if (pt != NULL)
4302 {
4303 if (pt->pt_name != NULL)
4304 return pt->pt_name;
4305 if (pt->pt_func != NULL)
4306 return pt->pt_func->uf_name;
4307 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004308 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004309}
4310
Bram Moolenaarddecc252016-04-06 22:59:37 +02004311 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004312partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004313{
4314 int i;
4315
4316 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004317 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004318 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004319 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004320 if (pt->pt_name != NULL)
4321 {
4322 func_unref(pt->pt_name);
4323 vim_free(pt->pt_name);
4324 }
4325 else
4326 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004327
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004328 // Decrease the reference count for the context of a closure. If down
4329 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004330 if (pt->pt_funcstack != NULL)
4331 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004332 --pt->pt_funcstack->fs_refcount;
4333 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004334 }
4335
Bram Moolenaarddecc252016-04-06 22:59:37 +02004336 vim_free(pt);
4337}
4338
4339/*
4340 * Unreference a closure: decrement the reference count and free it when it
4341 * becomes zero.
4342 */
4343 void
4344partial_unref(partial_T *pt)
4345{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004346 if (pt != NULL)
4347 {
4348 if (--pt->pt_refcount <= 0)
4349 partial_free(pt);
4350
4351 // If the reference count goes down to one, the funcstack may be the
4352 // only reference and can be freed if no other partials reference it.
4353 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4354 funcstack_check_refcount(pt->pt_funcstack);
4355 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004356}
4357
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004359 * Return the next (unique) copy ID.
4360 * Used for serializing nested structures.
4361 */
4362 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004363get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004364{
4365 current_copyID += COPYID_INC;
4366 return current_copyID;
4367}
4368
4369/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004370 * Garbage collection for lists and dictionaries.
4371 *
4372 * We use reference counts to be able to free most items right away when they
4373 * are no longer used. But for composite items it's possible that it becomes
4374 * unused while the reference count is > 0: When there is a recursive
4375 * reference. Example:
4376 * :let l = [1, 2, 3]
4377 * :let d = {9: l}
4378 * :let l[1] = d
4379 *
4380 * Since this is quite unusual we handle this with garbage collection: every
4381 * once in a while find out which lists and dicts are not referenced from any
4382 * variable.
4383 *
4384 * Here is a good reference text about garbage collection (refers to Python
4385 * but it applies to all reference-counting mechanisms):
4386 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004387 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004388
4389/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004390 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004391 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004392 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004393 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004394 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004395garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004396{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004397 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004398 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004399 buf_T *buf;
4400 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004401 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004402 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004403
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004404 if (!testing)
4405 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004406 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004407 want_garbage_collect = FALSE;
4408 may_garbage_collect = FALSE;
4409 garbage_collect_at_exit = FALSE;
4410 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004411
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004412 // The execution stack can grow big, limit the size.
4413 if (exestack.ga_maxlen - exestack.ga_len > 500)
4414 {
4415 size_t new_len;
4416 char_u *pp;
4417 int n;
4418
4419 // Keep 150% of the current size, with a minimum of the growth size.
4420 n = exestack.ga_len / 2;
4421 if (n < exestack.ga_growsize)
4422 n = exestack.ga_growsize;
4423
4424 // Don't make it bigger though.
4425 if (exestack.ga_len + n < exestack.ga_maxlen)
4426 {
4427 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4428 pp = vim_realloc(exestack.ga_data, new_len);
4429 if (pp == NULL)
4430 return FAIL;
4431 exestack.ga_maxlen = exestack.ga_len + n;
4432 exestack.ga_data = pp;
4433 }
4434 }
4435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004436 // We advance by two because we add one for items referenced through
4437 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004438 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004439
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004440 /*
4441 * 1. Go through all accessible variables and mark all lists and dicts
4442 * with copyID.
4443 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004445 // Don't free variables in the previous_funccal list unless they are only
4446 // referenced through previous_funccal. This must be first, because if
4447 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004448 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004450 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004451 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004453 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004454 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004455 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4456 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004458 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004459 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4461 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004462 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004463 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4464 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004465#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004466 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004467 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4468 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004469 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004470 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004471 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4472 NULL, NULL);
4473#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004475 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004476 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004477 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4478 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004480 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004486 abort = abort || set_ref_in_functions(copyID);
4487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004489 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004490
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004491 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004492 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004493
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004494 // callbacks in buffers
4495 abort = abort || set_ref_in_buffers(copyID);
4496
Bram Moolenaar1dced572012-04-05 16:54:08 +02004497#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004498 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004499#endif
4500
Bram Moolenaardb913952012-06-29 12:54:53 +02004501#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004502 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004503#endif
4504
4505#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004506 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004507#endif
4508
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004509#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004510 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004511 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004512#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004513#ifdef FEAT_NETBEANS_INTG
4514 abort = abort || set_ref_in_nb_channel(copyID);
4515#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004516
Bram Moolenaare3188e22016-05-31 21:13:04 +02004517#ifdef FEAT_TIMERS
4518 abort = abort || set_ref_in_timer(copyID);
4519#endif
4520
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004521#ifdef FEAT_QUICKFIX
4522 abort = abort || set_ref_in_quickfix(copyID);
4523#endif
4524
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004525#ifdef FEAT_TERMINAL
4526 abort = abort || set_ref_in_term(copyID);
4527#endif
4528
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004529#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004530 abort = abort || set_ref_in_popups(copyID);
4531#endif
4532
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004533 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004534 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535 /*
4536 * 2. Free lists and dictionaries that are not referenced.
4537 */
4538 did_free = free_unref_items(copyID);
4539
4540 /*
4541 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004542 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004544 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004545 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004546 else if (p_verbose > 0)
4547 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004548 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004549 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004550
4551 return did_free;
4552}
4553
4554/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004555 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004556 */
4557 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004558free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004559{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004560 int did_free = FALSE;
4561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004562 // Let all "free" functions know that we are here. This means no
4563 // dictionaries, lists, channels or jobs are to be freed, because we will
4564 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004565 in_free_unref_items = TRUE;
4566
4567 /*
4568 * PASS 1: free the contents of the items. We don't free the items
4569 * themselves yet, so that it is possible to decrement refcount counters
4570 */
4571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004572 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004573 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004575 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004576 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004577
4578#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004579 // Go through the list of jobs and free items without the copyID. This
4580 // must happen before doing channels, because jobs refer to channels, but
4581 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004582 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4583
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004584 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004585 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4586#endif
4587
4588 /*
4589 * PASS 2: free the items themselves.
4590 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004591 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004592 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004593
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004594#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004595 // Go through the list of jobs and free items without the copyID. This
4596 // must happen before doing channels, because jobs refer to channels, but
4597 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004598 free_unused_jobs(copyID, COPYID_MASK);
4599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004600 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004601 free_unused_channels(copyID, COPYID_MASK);
4602#endif
4603
4604 in_free_unref_items = FALSE;
4605
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004606 return did_free;
4607}
4608
4609/*
4610 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004611 * "list_stack" is used to add lists to be marked. Can be NULL.
4612 *
4613 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004614 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004616set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617{
4618 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004619 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004620 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004621 hashtab_T *cur_ht;
4622 ht_stack_T *ht_stack = NULL;
4623 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004624
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004625 cur_ht = ht;
4626 for (;;)
4627 {
4628 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004630 // Mark each item in the hashtab. If the item contains a hashtab
4631 // it is added to ht_stack, if it contains a list it is added to
4632 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004633 todo = (int)cur_ht->ht_used;
4634 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4635 if (!HASHITEM_EMPTY(hi))
4636 {
4637 --todo;
4638 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4639 &ht_stack, list_stack);
4640 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004641 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004642
4643 if (ht_stack == NULL)
4644 break;
4645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004646 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004647 cur_ht = ht_stack->ht;
4648 tempitem = ht_stack;
4649 ht_stack = ht_stack->prev;
4650 free(tempitem);
4651 }
4652
4653 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004654}
4655
4656/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004657 * Mark a dict and its items with "copyID".
4658 * Returns TRUE if setting references failed somehow.
4659 */
4660 int
4661set_ref_in_dict(dict_T *d, int copyID)
4662{
4663 if (d != NULL && d->dv_copyID != copyID)
4664 {
4665 d->dv_copyID = copyID;
4666 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4667 }
4668 return FALSE;
4669}
4670
4671/*
4672 * Mark a list and its items with "copyID".
4673 * Returns TRUE if setting references failed somehow.
4674 */
4675 int
4676set_ref_in_list(list_T *ll, int copyID)
4677{
4678 if (ll != NULL && ll->lv_copyID != copyID)
4679 {
4680 ll->lv_copyID = copyID;
4681 return set_ref_in_list_items(ll, copyID, NULL);
4682 }
4683 return FALSE;
4684}
4685
4686/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004687 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004688 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4689 *
4690 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004691 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004692 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004693set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004694{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004695 listitem_T *li;
4696 int abort = FALSE;
4697 list_T *cur_l;
4698 list_stack_T *list_stack = NULL;
4699 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004700
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701 cur_l = l;
4702 for (;;)
4703 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004704 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004705 // Mark each item in the list. If the item contains a hashtab
4706 // it is added to ht_stack, if it contains a list it is added to
4707 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004708 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4709 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4710 ht_stack, &list_stack);
4711 if (list_stack == NULL)
4712 break;
4713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004714 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004715 cur_l = list_stack->list;
4716 tempitem = list_stack;
4717 list_stack = list_stack->prev;
4718 free(tempitem);
4719 }
4720
4721 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004722}
4723
4724/*
4725 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004726 * "list_stack" is used to add lists to be marked. Can be NULL.
4727 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4728 *
4729 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004730 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004731 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004732set_ref_in_item(
4733 typval_T *tv,
4734 int copyID,
4735 ht_stack_T **ht_stack,
4736 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004737{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004738 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004739
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004740 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004741 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004742 dict_T *dd = tv->vval.v_dict;
4743
Bram Moolenaara03f2332016-02-06 18:09:59 +01004744 if (dd != NULL && dd->dv_copyID != copyID)
4745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004746 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004747 dd->dv_copyID = copyID;
4748 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004749 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004750 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4751 }
4752 else
4753 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004754 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4755
Bram Moolenaara03f2332016-02-06 18:09:59 +01004756 if (newitem == NULL)
4757 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004758 else
4759 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004760 newitem->ht = &dd->dv_hashtab;
4761 newitem->prev = *ht_stack;
4762 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004763 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004764 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004765 }
4766 }
4767 else if (tv->v_type == VAR_LIST)
4768 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004769 list_T *ll = tv->vval.v_list;
4770
Bram Moolenaara03f2332016-02-06 18:09:59 +01004771 if (ll != NULL && ll->lv_copyID != copyID)
4772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004773 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004774 ll->lv_copyID = copyID;
4775 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004776 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004777 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004778 }
4779 else
4780 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004781 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4782
Bram Moolenaara03f2332016-02-06 18:09:59 +01004783 if (newitem == NULL)
4784 abort = TRUE;
4785 else
4786 {
4787 newitem->list = ll;
4788 newitem->prev = *list_stack;
4789 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004790 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004791 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004792 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004793 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004794 else if (tv->v_type == VAR_FUNC)
4795 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004796 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004797 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004798 else if (tv->v_type == VAR_PARTIAL)
4799 {
4800 partial_T *pt = tv->vval.v_partial;
4801 int i;
4802
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004803 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004804 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004805 // Didn't see this partial yet.
4806 pt->pt_copyID = copyID;
4807
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004808 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004809
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004810 if (pt->pt_dict != NULL)
4811 {
4812 typval_T dtv;
4813
4814 dtv.v_type = VAR_DICT;
4815 dtv.vval.v_dict = pt->pt_dict;
4816 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4817 }
4818
4819 for (i = 0; i < pt->pt_argc; ++i)
4820 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4821 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004822 if (pt->pt_funcstack != NULL)
4823 {
4824 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4825
4826 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4827 abort = abort || set_ref_in_item(stack + i, copyID,
4828 ht_stack, list_stack);
4829 }
4830
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004831 }
4832 }
4833#ifdef FEAT_JOB_CHANNEL
4834 else if (tv->v_type == VAR_JOB)
4835 {
4836 job_T *job = tv->vval.v_job;
4837 typval_T dtv;
4838
4839 if (job != NULL && job->jv_copyID != copyID)
4840 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004841 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004842 if (job->jv_channel != NULL)
4843 {
4844 dtv.v_type = VAR_CHANNEL;
4845 dtv.vval.v_channel = job->jv_channel;
4846 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4847 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004848 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004849 {
4850 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004851 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004852 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4853 }
4854 }
4855 }
4856 else if (tv->v_type == VAR_CHANNEL)
4857 {
4858 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004859 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004860 typval_T dtv;
4861 jsonq_T *jq;
4862 cbq_T *cq;
4863
4864 if (ch != NULL && ch->ch_copyID != copyID)
4865 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004866 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004867 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004868 {
4869 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4870 jq = jq->jq_next)
4871 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4872 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4873 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004874 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004875 {
4876 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004877 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004878 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4879 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004880 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004881 {
4882 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004883 dtv.vval.v_partial =
4884 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004885 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4886 }
4887 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004888 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004889 {
4890 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004891 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004892 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4893 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004894 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004895 {
4896 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004897 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004898 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4899 }
4900 }
4901 }
4902#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004903 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004904}
4905
Bram Moolenaar8c711452005-01-14 21:53:12 +00004906/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004907 * Return a string with the string representation of a variable.
4908 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004909 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004910 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004911 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004912 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004913 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004914 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4915 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004916 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004918 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004919echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004920 typval_T *tv,
4921 char_u **tofree,
4922 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004923 int copyID,
4924 int echo_style,
4925 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004926 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004927{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004928 static int recurse = 0;
4929 char_u *r = NULL;
4930
Bram Moolenaar33570922005-01-25 22:26:29 +00004931 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004932 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004933 if (!did_echo_string_emsg)
4934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 // Only give this message once for a recursive call to avoid
4936 // flooding the user with errors. And stop iterating over lists
4937 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004938 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004939 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004940 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004941 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004942 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004943 }
4944 ++recurse;
4945
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004946 switch (tv->v_type)
4947 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004948 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004949 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004950 {
4951 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004952 r = tv->vval.v_string;
4953 if (r == NULL)
4954 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004955 }
4956 else
4957 {
4958 *tofree = string_quote(tv->vval.v_string, FALSE);
4959 r = *tofree;
4960 }
4961 break;
4962
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004963 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004964 if (echo_style)
4965 {
4966 *tofree = NULL;
4967 r = tv->vval.v_string;
4968 }
4969 else
4970 {
4971 *tofree = string_quote(tv->vval.v_string, TRUE);
4972 r = *tofree;
4973 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004974 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004975
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004976 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004977 {
4978 partial_T *pt = tv->vval.v_partial;
4979 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004980 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004981 garray_T ga;
4982 int i;
4983 char_u *tf;
4984
4985 ga_init2(&ga, 1, 100);
4986 ga_concat(&ga, (char_u *)"function(");
4987 if (fname != NULL)
4988 {
4989 ga_concat(&ga, fname);
4990 vim_free(fname);
4991 }
4992 if (pt != NULL && pt->pt_argc > 0)
4993 {
4994 ga_concat(&ga, (char_u *)", [");
4995 for (i = 0; i < pt->pt_argc; ++i)
4996 {
4997 if (i > 0)
4998 ga_concat(&ga, (char_u *)", ");
4999 ga_concat(&ga,
5000 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5001 vim_free(tf);
5002 }
5003 ga_concat(&ga, (char_u *)"]");
5004 }
5005 if (pt != NULL && pt->pt_dict != NULL)
5006 {
5007 typval_T dtv;
5008
5009 ga_concat(&ga, (char_u *)", ");
5010 dtv.v_type = VAR_DICT;
5011 dtv.vval.v_dict = pt->pt_dict;
5012 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5013 vim_free(tf);
5014 }
5015 ga_concat(&ga, (char_u *)")");
5016
5017 *tofree = ga.ga_data;
5018 r = *tofree;
5019 break;
5020 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005021
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005022 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005023 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005024 break;
5025
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005026 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005027 if (tv->vval.v_list == NULL)
5028 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005029 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005030 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005031 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005032 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005033 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5034 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005035 {
5036 *tofree = NULL;
5037 r = (char_u *)"[...]";
5038 }
5039 else
5040 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005041 int old_copyID = tv->vval.v_list->lv_copyID;
5042
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005043 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005044 *tofree = list2string(tv, copyID, restore_copyID);
5045 if (restore_copyID)
5046 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005047 r = *tofree;
5048 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005049 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005050
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005052 if (tv->vval.v_dict == NULL)
5053 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005054 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005055 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005056 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005057 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005058 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5059 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005060 {
5061 *tofree = NULL;
5062 r = (char_u *)"{...}";
5063 }
5064 else
5065 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005066 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005067
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005068 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005069 *tofree = dict2string(tv, copyID, restore_copyID);
5070 if (restore_copyID)
5071 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005072 r = *tofree;
5073 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005074 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005075
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005077 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005078 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005080 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005081 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005082 break;
5083
Bram Moolenaar835dc632016-02-07 14:27:38 +01005084 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005085 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005086 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005087 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005088 if (composite_val)
5089 {
5090 *tofree = string_quote(r, FALSE);
5091 r = *tofree;
5092 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005093 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005094
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005095 case VAR_INSTR:
5096 *tofree = NULL;
5097 r = (char_u *)"instructions";
5098 break;
5099
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005100 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005101#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 *tofree = NULL;
5103 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5104 r = numbuf;
5105 break;
5106#endif
5107
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005108 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005109 case VAR_SPECIAL:
5110 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005111 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005112 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005114
Bram Moolenaar8502c702014-06-17 12:51:16 +02005115 if (--recurse == 0)
5116 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005117 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118}
5119
5120/*
5121 * Return a string with the string representation of a variable.
5122 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5123 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005124 * Does not put quotes around strings, as ":echo" displays values.
5125 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5126 * May return NULL.
5127 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005128 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005129echo_string(
5130 typval_T *tv,
5131 char_u **tofree,
5132 char_u *numbuf,
5133 int copyID)
5134{
5135 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5136}
5137
5138/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005139 * Return string "str" in ' quotes, doubling ' characters.
5140 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005142 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005143 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005144string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005145{
Bram Moolenaar33570922005-01-25 22:26:29 +00005146 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005147 char_u *p, *r, *s;
5148
Bram Moolenaar33570922005-01-25 22:26:29 +00005149 len = (function ? 13 : 3);
5150 if (str != NULL)
5151 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005152 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005153 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005154 if (*p == '\'')
5155 ++len;
5156 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005157 s = r = alloc(len);
5158 if (r != NULL)
5159 {
5160 if (function)
5161 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005163 r += 10;
5164 }
5165 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005167 if (str != NULL)
5168 for (p = str; *p != NUL; )
5169 {
5170 if (*p == '\'')
5171 *r++ = '\'';
5172 MB_COPY_CHAR(p, r);
5173 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005175 if (function)
5176 *r++ = ')';
5177 *r++ = NUL;
5178 }
5179 return s;
5180}
5181
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005182#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005183/*
5184 * Convert the string "text" to a floating point number.
5185 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5186 * this always uses a decimal point.
5187 * Returns the length of the text that was consumed.
5188 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005189 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005190string2float(
5191 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005192 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005193{
5194 char *s = (char *)text;
5195 float_T f;
5196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005198 if (STRNICMP(text, "inf", 3) == 0)
5199 {
5200 *value = INFINITY;
5201 return 3;
5202 }
5203 if (STRNICMP(text, "-inf", 3) == 0)
5204 {
5205 *value = -INFINITY;
5206 return 4;
5207 }
5208 if (STRNICMP(text, "nan", 3) == 0)
5209 {
5210 *value = NAN;
5211 return 3;
5212 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 f = strtod(s, &s);
5214 *value = f;
5215 return (int)((char_u *)s - text);
5216}
5217#endif
5218
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005219/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005220 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5221 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005222 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005223 */
5224 int
5225buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5226{
5227 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005228 char_u *t;
5229 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005230
5231 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5232 return -1;
5233
5234 if (lnum > buf->b_ml.ml_line_count)
5235 lnum = buf->b_ml.ml_line_count;
5236
5237 str = ml_get_buf(buf, lnum, FALSE);
5238 if (str == NULL)
5239 return -1;
5240
5241 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005242 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005243
Bram Moolenaar91458462021-01-13 20:08:38 +01005244 // count the number of characters
5245 t = str;
5246 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5247 t += mb_ptr2len(t);
5248
5249 // In insert mode, when the cursor is at the end of a non-empty line,
5250 // byteidx points to the NUL character immediately past the end of the
5251 // string. In this case, add one to the character count.
5252 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5253 count++;
5254
5255 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005256}
5257
5258/*
5259 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005260 * byte index. Works only for loaded buffers. Returns -1 on failure.
5261 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005262 */
5263 int
5264buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5265{
5266 char_u *str;
5267 char_u *t;
5268
5269 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5270 return -1;
5271
5272 if (lnum > buf->b_ml.ml_line_count)
5273 lnum = buf->b_ml.ml_line_count;
5274
5275 str = ml_get_buf(buf, lnum, FALSE);
5276 if (str == NULL)
5277 return -1;
5278
5279 // Convert the character offset to a byte offset
5280 t = str;
5281 while (*t != NUL && --charidx > 0)
5282 t += mb_ptr2len(t);
5283
Bram Moolenaar91458462021-01-13 20:08:38 +01005284 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005285}
5286
5287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005289 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005291 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005292var2fpos(
5293 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005294 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005295 int *fnum, // set to fnum for '0, 'A, etc.
5296 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005298 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005300 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005302 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005303 if (varp->v_type == VAR_LIST)
5304 {
5305 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005306 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005307 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005308 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005309
5310 l = varp->vval.v_list;
5311 if (l == NULL)
5312 return NULL;
5313
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005314 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005315 pos.lnum = list_find_nr(l, 0L, &error);
5316 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005317 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005318 if (charcol)
5319 len = (long)mb_charlen(ml_get(pos.lnum));
5320 else
5321 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005322
Bram Moolenaarec65d772020-08-20 22:29:12 +02005323 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005324 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005325 li = list_find(l, 1L);
5326 if (li != NULL && li->li_tv.v_type == VAR_STRING
5327 && li->li_tv.vval.v_string != NULL
5328 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005329 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005330 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005331 }
5332 else
5333 {
5334 pos.col = list_find_nr(l, 1L, &error);
5335 if (error)
5336 return NULL;
5337 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005338
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005339 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005340 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005341 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005342 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005343
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005345 pos.coladd = list_find_nr(l, 2L, &error);
5346 if (error)
5347 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005348
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005349 return &pos;
5350 }
5351
Bram Moolenaarc5809432021-03-27 21:23:30 +01005352 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5353 return NULL;
5354
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005355 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005356 if (name == NULL)
5357 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005358 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005359 {
5360 pos = curwin->w_cursor;
5361 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005362 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005363 return &pos;
5364 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005365 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005366 {
5367 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005368 pos = VIsual;
5369 else
5370 pos = curwin->w_cursor;
5371 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005372 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005373 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005374 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005377 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5379 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005380 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005381 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 return pp;
5383 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005384
Bram Moolenaara5525202006-03-02 22:52:09 +00005385 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005386
Bram Moolenaar477933c2007-07-17 14:32:23 +00005387 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005388 {
5389 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005390 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005391 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005392 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005393 // In silent Ex mode topline is zero, but that's not a valid line
5394 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005395 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005396 return &pos;
5397 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005398 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005399 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005400 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005401 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005402 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005403 return &pos;
5404 }
5405 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005406 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005408 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 pos.lnum = curbuf->b_ml.ml_line_count;
5411 pos.col = 0;
5412 }
5413 else
5414 {
5415 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005416 if (charcol)
5417 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5418 else
5419 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
5421 return &pos;
5422 }
5423 return NULL;
5424}
5425
5426/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005427 * Convert list in "arg" into a position and optional file number.
5428 * When "fnump" is NULL there is no file number, only 3 items.
5429 * Note that the column is passed on as-is, the caller may want to decrement
5430 * it to use 1 for the first column.
5431 * Return FAIL when conversion is not possible, doesn't check the position for
5432 * validity.
5433 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005435list2fpos(
5436 typval_T *arg,
5437 pos_T *posp,
5438 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005439 colnr_T *curswantp,
5440 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005441{
5442 list_T *l = arg->vval.v_list;
5443 long i = 0;
5444 long n;
5445
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005446 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5447 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005448 if (arg->v_type != VAR_LIST
5449 || l == NULL
5450 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005451 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005452 return FAIL;
5453
5454 if (fnump != NULL)
5455 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005457 if (n < 0)
5458 return FAIL;
5459 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005460 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005461 *fnump = n;
5462 }
5463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005464 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005465 if (n < 0)
5466 return FAIL;
5467 posp->lnum = n;
5468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005470 if (n < 0)
5471 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005472 // If character position is specified, then convert to byte position
5473 if (charcol)
5474 {
5475 buf_T *buf;
5476
5477 // Get the text for the specified line in a loaded buffer
5478 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5479 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5480 return FAIL;
5481
Bram Moolenaar91458462021-01-13 20:08:38 +01005482 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005483 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005484 posp->col = n;
5485
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005486 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005487 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005488 posp->coladd = 0;
5489 else
5490 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005491
Bram Moolenaar493c1782014-05-28 14:34:46 +02005492 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005493 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005494
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005495 return OK;
5496}
5497
5498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 * Get the length of an environment variable name.
5500 * Advance "arg" to the first character after the name.
5501 * Return 0 for error.
5502 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005503 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005504get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 char_u *p;
5507 int len;
5508
5509 for (p = *arg; vim_isIDc(*p); ++p)
5510 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005511 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 return 0;
5513
5514 len = (int)(p - *arg);
5515 *arg = p;
5516 return len;
5517}
5518
5519/*
5520 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005521 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 * Return 0 if something is wrong.
5523 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005524 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005525get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526{
5527 char_u *p;
5528 int len;
5529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005530 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005532 {
5533 if (*p == ':')
5534 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005535 // "s:" is start of "s:var", but "n:" is not and can be used in
5536 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005537 len = (int)(p - *arg);
5538 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5539 || len > 1)
5540 break;
5541 }
5542 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 return 0;
5545
5546 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005547 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549 return len;
5550}
5551
5552/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005553 * Get the length of the name of a variable or function.
5554 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005556 * Return -1 if curly braces expansion failed.
5557 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 * If the name contains 'magic' {}'s, expand them and return the
5559 * expanded name in an allocated string via 'alias' - caller must free.
5560 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005561 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005562get_name_len(
5563 char_u **arg,
5564 char_u **alias,
5565 int evaluate,
5566 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 char_u *p;
5570 char_u *expr_start;
5571 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005573 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5576 && (*arg)[2] == (int)KE_SNR)
5577 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005578 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 *arg += 3;
5580 return get_id_len(arg) + 3;
5581 }
5582 len = eval_fname_script(*arg);
5583 if (len > 0)
5584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 *arg += len;
5587 }
5588
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005590 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005592 p = find_name_end(*arg, &expr_start, &expr_end,
5593 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 if (expr_start != NULL)
5595 {
5596 char_u *temp_string;
5597
5598 if (!evaluate)
5599 {
5600 len += (int)(p - *arg);
5601 *arg = skipwhite(p);
5602 return len;
5603 }
5604
5605 /*
5606 * Include any <SID> etc in the expanded string:
5607 * Thus the -len here.
5608 */
5609 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5610 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005611 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 *alias = temp_string;
5613 *arg = skipwhite(p);
5614 return (int)STRLEN(temp_string);
5615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
5617 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005618 // Only give an error when there is something, otherwise it will be
5619 // reported at a higher level.
5620 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005621 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
5623 return len;
5624}
5625
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626/*
5627 * Find the end of a variable or function name, taking care of magic braces.
5628 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5629 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005630 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 * Return a pointer to just after the name. Equal to "arg" if there is no
5632 * valid name.
5633 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005634 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005635find_name_end(
5636 char_u *arg,
5637 char_u **expr_start,
5638 char_u **expr_end,
5639 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005641 int mb_nest = 0;
5642 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005644 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005645 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 *expr_start = NULL;
5650 *expr_end = NULL;
5651 }
5652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005653 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005654 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5655 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005656 return arg;
5657
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005658 for (p = arg; *p != NUL
5659 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005660 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005661 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005662 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005664 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005666 if (*p == '\'')
5667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005668 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005669 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005670 ;
5671 if (*p == NUL)
5672 break;
5673 }
5674 else if (*p == '"')
5675 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005676 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005677 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005678 if (*p == '\\' && p[1] != NUL)
5679 ++p;
5680 if (*p == NUL)
5681 break;
5682 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005683 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5684 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005685 // "s:" is start of "s:var", but "n:" is not and can be used in
5686 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005687 len = (int)(p - arg);
5688 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005689 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005690 break;
5691 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005692
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005695 if (*p == '[')
5696 ++br_nest;
5697 else if (*p == ']')
5698 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005700
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005701 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005703 if (*p == '{')
5704 {
5705 mb_nest++;
5706 if (expr_start != NULL && *expr_start == NULL)
5707 *expr_start = p;
5708 }
5709 else if (*p == '}')
5710 {
5711 mb_nest--;
5712 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5713 *expr_end = p;
5714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717
5718 return p;
5719}
5720
5721/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005722 * Expands out the 'magic' {}'s in a variable/function name.
5723 * Note that this can call itself recursively, to deal with
5724 * constructs like foo{bar}{baz}{bam}
5725 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5726 * "in_start" ^
5727 * "expr_start" ^
5728 * "expr_end" ^
5729 * "in_end" ^
5730 *
5731 * Returns a new allocated string, which the caller must free.
5732 * Returns NULL for failure.
5733 */
5734 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005735make_expanded_name(
5736 char_u *in_start,
5737 char_u *expr_start,
5738 char_u *expr_end,
5739 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005740{
5741 char_u c1;
5742 char_u *retval = NULL;
5743 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005744
5745 if (expr_end == NULL || in_end == NULL)
5746 return NULL;
5747 *expr_start = NUL;
5748 *expr_end = NUL;
5749 c1 = *in_end;
5750 *in_end = NUL;
5751
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005752 temp_result = eval_to_string(expr_start + 1, FALSE);
5753 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005754 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005755 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5756 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005757 if (retval != NULL)
5758 {
5759 STRCPY(retval, in_start);
5760 STRCAT(retval, temp_result);
5761 STRCAT(retval, expr_end + 1);
5762 }
5763 }
5764 vim_free(temp_result);
5765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005766 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005767 *expr_start = '{';
5768 *expr_end = '}';
5769
5770 if (retval != NULL)
5771 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005772 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005773 if (expr_start != NULL)
5774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005775 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005776 temp_result = make_expanded_name(retval, expr_start,
5777 expr_end, temp_result);
5778 vim_free(retval);
5779 retval = temp_result;
5780 }
5781 }
5782
5783 return retval;
5784}
5785
5786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005788 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005791eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005793 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005794}
5795
5796/*
5797 * Return TRUE if character "c" can be used as the first character in a
5798 * variable or function name (excluding '{' and '}').
5799 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005800 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005801eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005802{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005803 return ASCII_ISALPHA(c) || c == '_';
5804}
5805
5806/*
5807 * Return TRUE if character "c" can be used as the first character of a
5808 * dictionary key.
5809 */
5810 int
5811eval_isdictc(int c)
5812{
5813 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814}
5815
5816/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005817 * Handle:
5818 * - expr[expr], expr[expr:expr] subscript
5819 * - ".name" lookup
5820 * - function call with Funcref variable: func(expr)
5821 * - method call: var->method()
5822 *
5823 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005824 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005826handle_subscript(
5827 char_u **arg,
5828 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005829 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005830 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005831{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005832 int evaluate = evalarg != NULL
5833 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005834 int ret = OK;
5835 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005836 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005837 int getnext;
5838 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005839
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005840 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005841 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005842 // When at the end of the line and ".name" or "->{" or "->X" follows in
5843 // the next line then consume the line break.
5844 p = eval_next_non_blank(*arg, evalarg, &getnext);
5845 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005846 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005847 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005848 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005849 {
5850 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005851 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005852 check_white = FALSE;
5853 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005854
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005855 if (rettv->v_type == VAR_ANY)
5856 {
5857 char_u *exp_name;
5858 int cc;
5859 int idx;
5860 ufunc_T *ufunc;
5861 type_T *type;
5862
5863 // Found script from "import * as {name}", script item name must
5864 // follow.
5865 if (**arg != '.')
5866 {
5867 if (verbose)
5868 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5869 ret = FAIL;
5870 break;
5871 }
5872 ++*arg;
5873 if (IS_WHITE_OR_NUL(**arg))
5874 {
5875 if (verbose)
5876 emsg(_(e_no_white_space_allowed_after_dot));
5877 ret = FAIL;
5878 break;
5879 }
5880
5881 // isolate the name
5882 exp_name = *arg;
5883 while (eval_isnamec(**arg))
5884 ++*arg;
5885 cc = **arg;
5886 **arg = NUL;
5887
5888 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5889 evalarg->eval_cctx, verbose);
5890 **arg = cc;
5891 *arg = skipwhite(*arg);
5892
5893 if (idx < 0 && ufunc == NULL)
5894 {
5895 ret = FAIL;
5896 break;
5897 }
5898 if (idx >= 0)
5899 {
5900 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5901 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5902
5903 copy_tv(sv->sv_tv, rettv);
5904 }
5905 else
5906 {
5907 rettv->v_type = VAR_FUNC;
5908 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5909 }
5910 }
5911
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005912 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5913 || rettv->v_type == VAR_PARTIAL))
5914 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005915 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005916 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5917 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005918
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005919 // Stop the expression evaluation when immediately aborting on
5920 // error, or when an interrupt occurred or an exception was thrown
5921 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005922 if (aborting())
5923 {
5924 if (ret == OK)
5925 clear_tv(rettv);
5926 ret = FAIL;
5927 }
5928 dict_unref(selfdict);
5929 selfdict = NULL;
5930 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005931 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005932 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005933 if (in_vim9script())
5934 *arg = skipwhite(p + 2);
5935 else
5936 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005937 if (ret == OK)
5938 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005939 if (VIM_ISWHITE(**arg))
5940 {
5941 emsg(_(e_nowhitespace));
5942 ret = FAIL;
5943 }
5944 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005945 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005946 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005947 else
5948 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005949 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005950 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005951 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005952 // "." is ".name" lookup when we found a dict or when evaluating and
5953 // scriptversion is at least 2, where string concatenation is "..".
5954 else if (**arg == '['
5955 || (**arg == '.' && (rettv->v_type == VAR_DICT
5956 || (!evaluate
5957 && (*arg)[1] != '.'
5958 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005959 {
5960 dict_unref(selfdict);
5961 if (rettv->v_type == VAR_DICT)
5962 {
5963 selfdict = rettv->vval.v_dict;
5964 if (selfdict != NULL)
5965 ++selfdict->dv_refcount;
5966 }
5967 else
5968 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005969 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005970 {
5971 clear_tv(rettv);
5972 ret = FAIL;
5973 }
5974 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005975 else
5976 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005977 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005979 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5980 // Don't do this when "Func" is already a partial that was bound
5981 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005982 if (selfdict != NULL
5983 && (rettv->v_type == VAR_FUNC
5984 || (rettv->v_type == VAR_PARTIAL
5985 && (rettv->vval.v_partial->pt_auto
5986 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005987 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005988
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005989 dict_unref(selfdict);
5990 return ret;
5991}
5992
5993/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 * Make a copy of an item.
5995 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005996 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5997 * reference to an already copied list/dict can be used.
5998 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005999 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006001item_copy(
6002 typval_T *from,
6003 typval_T *to,
6004 int deep,
6005 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006{
6007 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006008 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006009
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006012 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006013 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006014 }
6015 ++recurse;
6016
6017 switch (from->v_type)
6018 {
6019 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006020 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006021 case VAR_STRING:
6022 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006023 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006024 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006025 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006026 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006027 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006028 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029 copy_tv(from, to);
6030 break;
6031 case VAR_LIST:
6032 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006033 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006034 if (from->vval.v_list == NULL)
6035 to->vval.v_list = NULL;
6036 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006038 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006039 to->vval.v_list = from->vval.v_list->lv_copylist;
6040 ++to->vval.v_list->lv_refcount;
6041 }
6042 else
6043 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6044 if (to->vval.v_list == NULL)
6045 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006046 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006047 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006049 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006050 case VAR_DICT:
6051 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006052 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006053 if (from->vval.v_dict == NULL)
6054 to->vval.v_dict = NULL;
6055 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6056 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006057 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006058 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6059 ++to->vval.v_dict->dv_refcount;
6060 }
6061 else
6062 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6063 if (to->vval.v_dict == NULL)
6064 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006065 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006066 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006067 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006068 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006069 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006070 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006071 }
6072 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006073 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006074}
6075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 void
6077echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6078{
6079 char_u *tofree;
6080 char_u numbuf[NUMBUFLEN];
6081 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6082
6083 if (*atstart)
6084 {
6085 *atstart = FALSE;
6086 // Call msg_start() after eval1(), evaluating the expression
6087 // may cause a message to appear.
6088 if (with_space)
6089 {
6090 // Mark the saved text as finishing the line, so that what
6091 // follows is displayed on a new line when scrolling back
6092 // at the more prompt.
6093 msg_sb_eol();
6094 msg_start();
6095 }
6096 }
6097 else if (with_space)
6098 msg_puts_attr(" ", echo_attr);
6099
6100 if (p != NULL)
6101 for ( ; *p != NUL && !got_int; ++p)
6102 {
6103 if (*p == '\n' || *p == '\r' || *p == TAB)
6104 {
6105 if (*p != TAB && *needclr)
6106 {
6107 // remove any text still there from the command
6108 msg_clr_eos();
6109 *needclr = FALSE;
6110 }
6111 msg_putchar_attr(*p, echo_attr);
6112 }
6113 else
6114 {
6115 if (has_mbyte)
6116 {
6117 int i = (*mb_ptr2len)(p);
6118
6119 (void)msg_outtrans_len_attr(p, i, echo_attr);
6120 p += i - 1;
6121 }
6122 else
6123 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6124 }
6125 }
6126 vim_free(tofree);
6127}
6128
Bram Moolenaare9a41262005-01-15 22:18:47 +00006129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 * ":echo expr1 ..." print each argument separated with a space, add a
6131 * newline at the end.
6132 * ":echon expr1 ..." print each argument plain.
6133 */
6134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006139 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 int needclr = TRUE;
6141 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006142 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006143 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006144 evalarg_T evalarg;
6145
Bram Moolenaare707c882020-07-01 13:07:37 +02006146 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
6148 if (eap->skip)
6149 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006150 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006152 // If eval1() causes an error message the text from the command may
6153 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006154 need_clr_eos = needclr;
6155
Bram Moolenaar68db9962021-05-09 23:19:22 +02006156 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006157 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {
6159 /*
6160 * Report the invalid expression unless the expression evaluation
6161 * has been cancelled due to an aborting error, an interrupt, or an
6162 * exception.
6163 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006164 if (!aborting() && did_emsg == did_emsg_before
6165 && called_emsg == called_emsg_before)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006166 semsg(_(e_invexpr2), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006167 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 break;
6169 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006170 need_clr_eos = FALSE;
6171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006173 {
6174 if (rettv.v_type == VAR_VOID)
6175 {
6176 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6177 break;
6178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006180 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006181
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006182 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 arg = skipwhite(arg);
6184 }
6185 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006186 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 if (eap->skip)
6189 --emsg_skip;
6190 else
6191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006192 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (needclr)
6194 msg_clr_eos();
6195 if (eap->cmdidx == CMD_echo)
6196 msg_end();
6197 }
6198}
6199
6200/*
6201 * ":echohl {name}".
6202 */
6203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006204ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006206 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207}
6208
6209/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006210 * Returns the :echo attribute
6211 */
6212 int
6213get_echo_attr(void)
6214{
6215 return echo_attr;
6216}
6217
6218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 * ":execute expr1 ..." execute the result of an expression.
6220 * ":echomsg expr1 ..." Print a message
6221 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006222 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 * Each gets spaces around each argument and a newline at the end for
6224 * echo commands
6225 */
6226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006227ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 int ret = OK;
6232 char_u *p;
6233 garray_T ga;
6234 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
6236 ga_init2(&ga, 1, 80);
6237
6238 if (eap->skip)
6239 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006240 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006242 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006243 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 if (!eap->skip)
6247 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006248 char_u buf[NUMBUFLEN];
6249
6250 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006251 {
6252 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6253 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006254 semsg(_(e_using_invalid_value_as_string_str),
6255 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006256 p = NULL;
6257 }
6258 else
6259 p = tv_get_string_buf(&rettv, buf);
6260 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006261 else
6262 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006263 if (p == NULL)
6264 {
6265 clear_tv(&rettv);
6266 ret = FAIL;
6267 break;
6268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 len = (int)STRLEN(p);
6270 if (ga_grow(&ga, len + 2) == FAIL)
6271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 ret = FAIL;
6274 break;
6275 }
6276 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 ga.ga_len += len;
6280 }
6281
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006282 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 arg = skipwhite(arg);
6284 }
6285
6286 if (ret != FAIL && ga.ga_data != NULL)
6287 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006288 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006290 // Mark the already saved text as finishing the line, so that what
6291 // follows is displayed on a new line when scrolling back at the
6292 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006293 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006294 }
6295
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006297 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006298 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006299 out_flush();
6300 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006301 else if (eap->cmdidx == CMD_echoconsole)
6302 {
6303 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6304 ui_write((char_u *)"\r\n", 2, TRUE);
6305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 else if (eap->cmdidx == CMD_echoerr)
6307 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006308 int save_did_emsg = did_emsg;
6309
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006310 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006311 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (!force_abort)
6313 did_emsg = save_did_emsg;
6314 }
6315 else if (eap->cmdidx == CMD_execute)
6316 do_cmdline((char_u *)ga.ga_data,
6317 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6318 }
6319
6320 ga_clear(&ga);
6321
6322 if (eap->skip)
6323 --emsg_skip;
6324
6325 eap->nextcmd = check_nextcmd(arg);
6326}
6327
6328/*
6329 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6330 * "arg" points to the "&" or '+' when called, to "option" when returning.
6331 * Returns NULL when no option name found. Otherwise pointer to the char
6332 * after the option name.
6333 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006334 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006335find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 char_u *p = *arg;
6338
6339 ++p;
6340 if (*p == 'g' && p[1] == ':')
6341 {
6342 *opt_flags = OPT_GLOBAL;
6343 p += 2;
6344 }
6345 else if (*p == 'l' && p[1] == ':')
6346 {
6347 *opt_flags = OPT_LOCAL;
6348 p += 2;
6349 }
6350 else
6351 *opt_flags = 0;
6352
6353 if (!ASCII_ISALPHA(*p))
6354 return NULL;
6355 *arg = p;
6356
6357 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006358 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 else
6360 while (ASCII_ISALPHA(*p))
6361 ++p;
6362 return p;
6363}
6364
6365/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006366 * Display script name where an item was last set.
6367 * Should only be invoked when 'verbose' is non-zero.
6368 */
6369 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006370last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006371{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006372 char_u *p;
6373
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006374 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006375 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006376 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006377 if (p != NULL)
6378 {
6379 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006380 msg_puts(_("\n\tLast set from "));
6381 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006382 if (script_ctx.sc_lnum > 0)
6383 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006384 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006385 msg_outnum((long)script_ctx.sc_lnum);
6386 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006387 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006388 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006389 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006390 }
6391}
6392
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006393#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
6395/*
6396 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006397 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 * "flags" can be "g" to do a global substitute.
6399 * Returns an allocated string, NULL for error.
6400 */
6401 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006402do_string_sub(
6403 char_u *str,
6404 char_u *pat,
6405 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006406 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006407 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
6409 int sublen;
6410 regmatch_T regmatch;
6411 int i;
6412 int do_all;
6413 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006414 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 garray_T ga;
6416 char_u *ret;
6417 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006418 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006420 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006422 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
6424 ga_init2(&ga, 1, 200);
6425
6426 do_all = (flags[0] == 'g');
6427
6428 regmatch.rm_ic = p_ic;
6429 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6430 if (regmatch.regprog != NULL)
6431 {
6432 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006433 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006436 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006437 if (regmatch.startp[0] == regmatch.endp[0])
6438 {
6439 if (zero_width == regmatch.startp[0])
6440 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006441 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006442 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006443 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6444 (size_t)i);
6445 ga.ga_len += i;
6446 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006447 continue;
6448 }
6449 zero_width = regmatch.startp[0];
6450 }
6451
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 /*
6453 * Get some space for a temporary buffer to do the substitution
6454 * into. It will contain:
6455 * - The text up to where the match is.
6456 * - The substituted text.
6457 * - The text after the match.
6458 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006459 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006460 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6462 {
6463 ga_clear(&ga);
6464 break;
6465 }
6466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006467 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 i = (int)(regmatch.startp[0] - tail);
6469 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006470 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006471 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 + ga.ga_len + i, TRUE, TRUE, FALSE);
6473 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006474 tail = regmatch.endp[0];
6475 if (*tail == NUL)
6476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (!do_all)
6478 break;
6479 }
6480
6481 if (ga.ga_data != NULL)
6482 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6483
Bram Moolenaar473de612013-06-08 18:19:48 +02006484 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
6486
6487 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6488 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006489 if (p_cpo == empty_option)
6490 p_cpo = save_cpo;
6491 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006492 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006493 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006494 // If it's still empty it was changed and restored, need to restore in
6495 // the complicated way.
6496 if (*p_cpo == NUL)
6497 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006498 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501 return ret;
6502}