blob: 9fb3b4e2ca69f85e333d6cfadddbc3ae45b394e6 [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 }
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001465 if ((lp->ll_tv->vval.v_dict == get_globvar_dict()
1466 || lp->ll_tv->vval.v_dict ==
1467 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_vars->sv_dict)
1468 && (rettv->v_type == VAR_FUNC
1469 || rettv->v_type == VAR_PARTIAL)
1470 && var_wrong_func_name(lp->ll_newkey, TRUE))
1471 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001473 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001474 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 if (di == NULL)
1476 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001477 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1478 {
1479 vim_free(di);
1480 return;
1481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001483 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 else if (op != NULL && *op != '=')
1485 {
1486 tv_op(lp->ll_tv, rettv, op);
1487 return;
1488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001490 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001491
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 /*
1493 * Assign the value to the variable or list item.
1494 */
1495 if (copy)
1496 copy_tv(rettv, lp->ll_tv);
1497 else
1498 {
1499 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001500 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001501 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502 }
1503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504}
1505
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001507 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1508 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509 * Returns OK or FAIL.
1510 */
1511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001512tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001514 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 char_u numbuf[NUMBUFLEN];
1516 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001517 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001519 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001520 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001521 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001522 {
1523 switch (tv1->v_type)
1524 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001525 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001526 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528 case VAR_DICT:
1529 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001530 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001531 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001532 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001533 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001534 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001535 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001536 break;
1537
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001538 case VAR_BLOB:
1539 if (*op != '+' || tv2->v_type != VAR_BLOB)
1540 break;
1541 // BLOB += BLOB
1542 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1543 {
1544 blob_T *b1 = tv1->vval.v_blob;
1545 blob_T *b2 = tv2->vval.v_blob;
1546 int i, len = blob_len(b2);
1547 for (i = 0; i < len; i++)
1548 ga_append(&b1->bv_ga, blob_get(b2, i));
1549 }
1550 return OK;
1551
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 case VAR_LIST:
1553 if (*op != '+' || tv2->v_type != VAR_LIST)
1554 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001555 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001556 if (tv2->vval.v_list != NULL)
1557 {
1558 if (tv1->vval.v_list == NULL)
1559 {
1560 tv1->vval.v_list = tv2->vval.v_list;
1561 ++tv1->vval.v_list->lv_refcount;
1562 }
1563 else
1564 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1565 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001566 return OK;
1567
1568 case VAR_NUMBER:
1569 case VAR_STRING:
1570 if (tv2->v_type == VAR_LIST)
1571 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001572 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001573 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001574 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001575 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576#ifdef FEAT_FLOAT
1577 if (tv2->v_type == VAR_FLOAT)
1578 {
1579 float_T f = n;
1580
Bram Moolenaarff697e62019-02-12 22:28:33 +01001581 if (*op == '%')
1582 break;
1583 switch (*op)
1584 {
1585 case '+': f += tv2->vval.v_float; break;
1586 case '-': f -= tv2->vval.v_float; break;
1587 case '*': f *= tv2->vval.v_float; break;
1588 case '/': f /= tv2->vval.v_float; break;
1589 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590 clear_tv(tv1);
1591 tv1->v_type = VAR_FLOAT;
1592 tv1->vval.v_float = f;
1593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001594 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595#endif
1596 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001597 switch (*op)
1598 {
1599 case '+': n += tv_get_number(tv2); break;
1600 case '-': n -= tv_get_number(tv2); break;
1601 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001602 case '/': n = num_divide(n, tv_get_number(tv2),
1603 &failed); break;
1604 case '%': n = num_modulus(n, tv_get_number(tv2),
1605 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001606 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001607 clear_tv(tv1);
1608 tv1->v_type = VAR_NUMBER;
1609 tv1->vval.v_number = n;
1610 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001611 }
1612 else
1613 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001614 if (tv2->v_type == VAR_FLOAT)
1615 break;
1616
Bram Moolenaarff697e62019-02-12 22:28:33 +01001617 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001618 s = tv_get_string(tv1);
1619 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001620 clear_tv(tv1);
1621 tv1->v_type = VAR_STRING;
1622 tv1->vval.v_string = s;
1623 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001624 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001626 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001627#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001628 {
1629 float_T f;
1630
Bram Moolenaarff697e62019-02-12 22:28:33 +01001631 if (*op == '%' || *op == '.'
1632 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001633 && tv2->v_type != VAR_NUMBER
1634 && tv2->v_type != VAR_STRING))
1635 break;
1636 if (tv2->v_type == VAR_FLOAT)
1637 f = tv2->vval.v_float;
1638 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001639 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001640 switch (*op)
1641 {
1642 case '+': tv1->vval.v_float += f; break;
1643 case '-': tv1->vval.v_float -= f; break;
1644 case '*': tv1->vval.v_float *= f; break;
1645 case '/': tv1->vval.v_float /= f; break;
1646 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001647 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001648#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001649 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001650 }
1651 }
1652
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001653 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001654 return FAIL;
1655}
1656
1657/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 * Evaluate the expression used in a ":for var in expr" command.
1659 * "arg" points to "var".
1660 * Set "*errp" to TRUE for an error, FALSE otherwise;
1661 * Return a pointer that holds the info. Null when there is an error.
1662 */
1663 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001664eval_for_line(
1665 char_u *arg,
1666 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001667 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001668 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669{
Bram Moolenaar33570922005-01-25 22:26:29 +00001670 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001672 typval_T tv;
1673 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001674 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001676 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001678 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 if (fi == NULL)
1680 return NULL;
1681
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001682 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 if (expr == NULL)
1684 return fi;
1685
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001686 expr = skipwhite_and_linebreak(expr, evalarg);
1687 if (expr[0] != 'i' || expr[1] != 'n'
1688 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 return fi;
1692 }
1693
1694 if (skip)
1695 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001696 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1697 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 {
1699 *errp = FALSE;
1700 if (!skip)
1701 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001702 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001703 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001704 l = tv.vval.v_list;
1705 if (l == NULL)
1706 {
1707 // a null list is like an empty list: do nothing
1708 clear_tv(&tv);
1709 }
1710 else
1711 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001713 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 // No need to increment the refcount, it's already set for
1716 // the list being used in "tv".
1717 fi->fi_list = l;
1718 list_add_watch(l, &fi->fi_lw);
1719 fi->fi_lw.lw_item = l->lv_first;
1720 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001721 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001722 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001723 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001724 fi->fi_bi = 0;
1725 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001726 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001727 typval_T btv;
1728
1729 // Make a copy, so that the iteration still works when the
1730 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001732 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001733 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001734 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001735 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001736 else if (tv.v_type == VAR_STRING)
1737 {
1738 fi->fi_byte_idx = 0;
1739 fi->fi_string = tv.vval.v_string;
1740 tv.vval.v_string = NULL;
1741 if (fi->fi_string == NULL)
1742 fi->fi_string = vim_strsave((char_u *)"");
1743 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 else
1745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001746 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001747 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 }
1749 }
1750 }
1751 if (skip)
1752 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001753 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754
1755 return fi;
1756}
1757
1758/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001759 * Used when looping over a :for line, skip the "in expr" part.
1760 */
1761 void
1762skip_for_lines(void *fi_void, evalarg_T *evalarg)
1763{
1764 forinfo_T *fi = (forinfo_T *)fi_void;
1765 int i;
1766
1767 for (i = 0; i < fi->fi_break_count; ++i)
1768 eval_next_line(evalarg);
1769}
1770
1771/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 * Use the first item in a ":for" list. Advance to the next.
1773 * Assign the values to the variable (list). "arg" points to the first one.
1774 * Return TRUE when a valid item was found, FALSE when at end of list or
1775 * something wrong.
1776 */
1777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001780 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001782 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1783 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1784 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001785 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001787 if (fi->fi_blob != NULL)
1788 {
1789 typval_T tv;
1790
1791 if (fi->fi_bi >= blob_len(fi->fi_blob))
1792 return FALSE;
1793 tv.v_type = VAR_NUMBER;
1794 tv.v_lock = VAR_FIXED;
1795 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1796 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001797 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001798 fi->fi_varcount, flag, NULL) == OK;
1799 }
1800
1801 if (fi->fi_string != NULL)
1802 {
1803 typval_T tv;
1804 int len;
1805
1806 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1807 if (len == 0)
1808 return FALSE;
1809 tv.v_type = VAR_STRING;
1810 tv.v_lock = VAR_FIXED;
1811 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1812 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001813 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001814 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001815 vim_free(tv.vval.v_string);
1816 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001817 }
1818
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 item = fi->fi_lw.lw_item;
1820 if (item == NULL)
1821 result = FALSE;
1822 else
1823 {
1824 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001825 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001826 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827 }
1828 return result;
1829}
1830
1831/*
1832 * Free the structure used to store info used by ":for".
1833 */
1834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001835free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836{
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001839 if (fi == NULL)
1840 return;
1841 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001842 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001844 list_unref(fi->fi_list);
1845 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001846 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001847 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001848 else
1849 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 vim_free(fi);
1851}
1852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001854set_context_for_expression(
1855 expand_T *xp,
1856 char_u *arg,
1857 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001859 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001863 if (cmdidx == CMD_let || cmdidx == CMD_var
1864 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 {
1866 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001867 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001869 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001870 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 {
1872 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001873 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001874 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 break;
1876 }
1877 return;
1878 }
1879 }
1880 else
1881 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1882 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 while ((xp->xp_pattern = vim_strpbrk(arg,
1884 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1885 {
1886 c = *xp->xp_pattern;
1887 if (c == '&')
1888 {
1889 c = xp->xp_pattern[1];
1890 if (c == '&')
1891 {
1892 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001893 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
1895 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001898 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1899 xp->xp_pattern += 2;
1900
1901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 else if (c == '$')
1904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001905 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 xp->xp_context = EXPAND_ENV_VARS;
1907 }
1908 else if (c == '=')
1909 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001910 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 xp->xp_context = EXPAND_EXPRESSION;
1912 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001913 else if (c == '#'
1914 && xp->xp_context == EXPAND_EXPRESSION)
1915 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001916 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001917 break;
1918 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001919 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 && xp->xp_context == EXPAND_FUNCTIONS
1921 && vim_strchr(xp->xp_pattern, '(') == NULL)
1922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001923 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 break;
1925 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001926 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001928 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
1930 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1931 if (c == '\\' && xp->xp_pattern[1] != NUL)
1932 ++xp->xp_pattern;
1933 xp->xp_context = EXPAND_NOTHING;
1934 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001935 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001937 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1939 /* skip */ ;
1940 xp->xp_context = EXPAND_NOTHING;
1941 }
1942 else if (c == '|')
1943 {
1944 if (xp->xp_pattern[1] == '|')
1945 {
1946 ++xp->xp_pattern;
1947 xp->xp_context = EXPAND_EXPRESSION;
1948 }
1949 else
1950 xp->xp_context = EXPAND_COMMANDS;
1951 }
1952 else
1953 xp->xp_context = EXPAND_EXPRESSION;
1954 }
1955 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001956 // Doesn't look like something valid, expand as an expression
1957 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 arg = xp->xp_pattern;
1960 if (*arg != NUL)
1961 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1962 /* skip */ ;
1963 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001964
1965 // ":exe one two" completes "two"
1966 if ((cmdidx == CMD_execute
1967 || cmdidx == CMD_echo
1968 || cmdidx == CMD_echon
1969 || cmdidx == CMD_echomsg)
1970 && xp->xp_context == EXPAND_EXPRESSION)
1971 {
1972 for (;;)
1973 {
1974 char_u *n = skiptowhite(arg);
1975
1976 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1977 break;
1978 arg = skipwhite(n);
1979 }
1980 }
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 xp->xp_pattern = arg;
1983}
1984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001986 * Return TRUE if "pat" matches "text".
1987 * Does not use 'cpo' and always uses 'magic'.
1988 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001989 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001990pattern_match(char_u *pat, char_u *text, int ic)
1991{
1992 int matches = FALSE;
1993 char_u *save_cpo;
1994 regmatch_T regmatch;
1995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001996 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001997 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001998 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001999 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2000 if (regmatch.regprog != NULL)
2001 {
2002 regmatch.rm_ic = ic;
2003 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2004 vim_regfree(regmatch.regprog);
2005 }
2006 p_cpo = save_cpo;
2007 return matches;
2008}
2009
2010/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002011 * Handle a name followed by "(". Both for just "name(arg)" and for
2012 * "expr->name(arg)".
2013 * Returns OK or FAIL.
2014 */
2015 static int
2016eval_func(
2017 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002018 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002019 char_u *name,
2020 int name_len,
2021 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002022 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002023 typval_T *basetv) // "expr" for "expr->name(arg)"
2024{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002025 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 char_u *s = name;
2027 int len = name_len;
2028 partial_T *partial;
2029 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002030 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002031
2032 if (!evaluate)
2033 check_vars(s, len);
2034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002035 // If "s" is the name of a variable of type VAR_FUNC
2036 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002037 s = deref_func_name(s, &len, &partial,
2038 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 // Need to make a copy, in case evaluating the arguments makes
2041 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002042 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002043 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002044 ret = FAIL;
2045 else
2046 {
2047 funcexe_T funcexe;
2048
2049 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002050 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002051 funcexe.firstline = curwin->w_cursor.lnum;
2052 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002053 funcexe.evaluate = evaluate;
2054 funcexe.partial = partial;
2055 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002056 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002057 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002058 }
2059 vim_free(s);
2060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002061 // If evaluate is FALSE rettv->v_type was not set in
2062 // get_func_tv, but it's needed in handle_subscript() to parse
2063 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002064 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2065 {
2066 rettv->vval.v_string = NULL;
2067 rettv->v_type = VAR_FUNC;
2068 }
2069
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002070 // Stop the expression evaluation when immediately
2071 // aborting on error, or when an interrupt occurred or
2072 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002073 if (evaluate && aborting())
2074 {
2075 if (ret == OK)
2076 clear_tv(rettv);
2077 ret = FAIL;
2078 }
2079 return ret;
2080}
2081
2082/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002083 * Get the next line source line without advancing. But do skip over comment
2084 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002085 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002086 */
2087 static char_u *
2088getline_peek_skip_comments(evalarg_T *evalarg)
2089{
2090 for (;;)
2091 {
2092 char_u *next = getline_peek(evalarg->eval_getline,
2093 evalarg->eval_cookie);
2094 char_u *p;
2095
2096 if (next == NULL)
2097 break;
2098 p = skipwhite(next);
2099 if (*p != NUL && !vim9_comment_start(p))
2100 return next;
2101 (void)eval_next_line(evalarg);
2102 }
2103 return NULL;
2104}
2105
2106/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002107 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2108 * comment) and there is a next line, return the next line (skipping blanks)
2109 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002110 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2111 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112 * "arg" must point somewhere inside a line, not at the start.
2113 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002114 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002115eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2116{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002117 char_u *p = skipwhite(arg);
2118
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002119 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002120 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002122 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002123 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002125 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002126
2127 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002128 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002129 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002130 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002131
Bram Moolenaar9d489562020-07-30 20:08:50 +02002132 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133 {
2134 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002135 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136 }
2137 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002138 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002139}
2140
2141/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002142 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002143 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002144 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002145 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002146eval_next_line(evalarg_T *evalarg)
2147{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002148 garray_T *gap = &evalarg->eval_ga;
2149 char_u *line;
2150
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002151 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002152 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2153 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002154 else
2155 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002156 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002157 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2158 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002159 char_u *p = skipwhite(line);
2160
2161 // Going to concatenate the lines after parsing. For an empty or
2162 // comment line use an empty string.
2163 if (*p == NUL || vim9_comment_start(p))
2164 {
2165 vim_free(line);
2166 line = vim_strsave((char_u *)"");
2167 }
2168
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002169 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2170 ++gap->ga_len;
2171 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002172 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002173 {
2174 vim_free(evalarg->eval_tofree);
2175 evalarg->eval_tofree = line;
2176 }
2177 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002178}
2179
Bram Moolenaare6b53242020-07-01 17:28:33 +02002180/*
2181 * Call eval_next_non_blank() and get the next line if needed.
2182 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002183 char_u *
2184skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2185{
2186 int getnext;
2187 char_u *p = skipwhite(arg);
2188
Bram Moolenaar962d7212020-07-04 14:15:00 +02002189 if (evalarg == NULL)
2190 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002191 eval_next_non_blank(p, evalarg, &getnext);
2192 if (getnext)
2193 return eval_next_line(evalarg);
2194 return p;
2195}
2196
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002197/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002198 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002199 */
2200 void
2201clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2202{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002203 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002204 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002205 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002206 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002207 if (eap != NULL)
2208 {
2209 // We may need to keep the original command line, e.g. for
2210 // ":let" it has the variable names. But we may also need the
2211 // new one, "nextcmd" points into it. Keep both.
2212 vim_free(eap->cmdline_tofree);
2213 eap->cmdline_tofree = *eap->cmdlinep;
2214 *eap->cmdlinep = evalarg->eval_tofree;
2215 }
2216 else
2217 vim_free(evalarg->eval_tofree);
2218 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002219 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002220
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002221 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2222 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002223 }
2224}
2225
2226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2230 */
2231
2232/*
2233 * Handle zero level expression.
2234 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002236 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002237 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 * Return OK or FAIL.
2239 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002240 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002241eval0(
2242 char_u *arg,
2243 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002244 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002245 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246{
2247 int ret;
2248 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002249 int did_emsg_before = did_emsg;
2250 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002251 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002254 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002255 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002256
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002257 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 /*
2262 * Report the invalid expression unless the expression evaluation has
2263 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002264 * exception, or we already gave a more specific error.
2265 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002267 if (!aborting()
2268 && did_emsg == did_emsg_before
2269 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002270 && (flags & EVAL_CONSTANT) == 0
2271 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002272 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002273
2274 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002275 // a next command to avoid more errors, unless "|" is following, which
2276 // could only be a command separator.
2277 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2278 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002281
2282 if (eap != NULL)
2283 eap->nextcmd = check_nextcmd(p);
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 return ret;
2286}
2287
2288/*
2289 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002290 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002291 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 *
2293 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002294 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002296 * Note: "rettv.v_lock" is not set.
2297 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * Return OK or FAIL.
2299 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002300 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002303 char_u *p;
2304 int getnext;
2305
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002306 CLEAR_POINTER(rettv);
2307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 /*
2309 * Get the first variable.
2310 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return FAIL;
2313
Bram Moolenaar793648f2020-06-26 21:28:25 +02002314 p = eval_next_non_blank(*arg, evalarg, &getnext);
2315 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002317 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318 int result;
2319 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002320 evalarg_T *evalarg_used = evalarg;
2321 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002322 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002323 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002324 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325
2326 if (evalarg == NULL)
2327 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002328 CLEAR_FIELD(local_evalarg);
2329 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002330 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002331 orig_flags = evalarg_used->eval_flags;
2332 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002333
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002334 if (getnext)
2335 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002336 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002337 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002338 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002339 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002340 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002341 clear_tv(rettv);
2342 return FAIL;
2343 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002344 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002345 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002348 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 int error = FALSE;
2351
Bram Moolenaar13106602020-10-04 16:06:05 +02002352 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002353 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002354 else if (vim9script)
2355 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002356 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002358 if (error || !op_falsy || !result)
2359 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 if (error)
2361 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
2363
2364 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002365 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002367 if (op_falsy)
2368 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002369 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002370 {
mityu4ac198c2021-05-28 17:52:40 +02002371 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002372 clear_tv(rettv);
2373 return FAIL;
2374 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002375 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002376 evalarg_used->eval_flags = (op_falsy ? !result : result)
2377 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2378 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002379 {
2380 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002382 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002383 if (!op_falsy || !result)
2384 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002386 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002388 /*
2389 * Check for the ":".
2390 */
2391 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2392 if (*p != ':')
2393 {
2394 emsg(_(e_missing_colon));
2395 if (evaluate && result)
2396 clear_tv(rettv);
2397 evalarg_used->eval_flags = orig_flags;
2398 return FAIL;
2399 }
2400 if (getnext)
2401 *arg = eval_next_line(evalarg_used);
2402 else
2403 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002404 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002405 {
2406 error_white_both(p, 1);
2407 clear_tv(rettv);
2408 evalarg_used->eval_flags = orig_flags;
2409 return FAIL;
2410 }
2411 *arg = p;
2412 }
2413
2414 /*
2415 * Get the third variable. Recursive!
2416 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002417 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002418 {
mityu4ac198c2021-05-28 17:52:40 +02002419 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002420 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002421 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002422 return FAIL;
2423 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002424 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2425 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002427 if (eval1(arg, &var2, evalarg_used) == FAIL)
2428 {
2429 if (evaluate && result)
2430 clear_tv(rettv);
2431 evalarg_used->eval_flags = orig_flags;
2432 return FAIL;
2433 }
2434 if (evaluate && !result)
2435 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437
2438 if (evalarg == NULL)
2439 clear_evalarg(&local_evalarg, NULL);
2440 else
2441 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443
2444 return OK;
2445}
2446
2447/*
2448 * Handle first level expression:
2449 * expr2 || expr2 || expr2 logical OR
2450 *
2451 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002452 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 *
2454 * Return OK or FAIL.
2455 */
2456 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002459 char_u *p;
2460 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 /*
2463 * Get the first variable.
2464 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 return FAIL;
2467
2468 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002469 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002471 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002472 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002474 evalarg_T *evalarg_used = evalarg;
2475 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476 int evaluate;
2477 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478 long result = FALSE;
2479 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002480 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002481 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002482
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002483 if (evalarg == NULL)
2484 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002485 CLEAR_FIELD(local_evalarg);
2486 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002487 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 orig_flags = evalarg_used->eval_flags;
2489 evaluate = orig_flags & EVAL_EVALUATE;
2490 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002491 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002492 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002493 result = tv_get_bool_chk(rettv, &error);
2494 else if (tv_get_number_chk(rettv, &error) != 0)
2495 result = TRUE;
2496 clear_tv(rettv);
2497 if (error)
2498 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500
2501 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002502 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002504 while (p[0] == '|' && p[1] == '|')
2505 {
2506 if (getnext)
2507 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002508 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002509 {
2510 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2511 {
2512 error_white_both(p, 2);
2513 clear_tv(rettv);
2514 return FAIL;
2515 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002516 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002517 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002518
2519 /*
2520 * Get the second variable.
2521 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002522 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2523 {
mityu4ac198c2021-05-28 17:52:40 +02002524 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002525 clear_tv(rettv);
2526 return FAIL;
2527 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002528 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2529 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002530 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002531 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533
2534 /*
2535 * Compute the result.
2536 */
2537 if (evaluate && !result)
2538 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002539 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002540 result = tv_get_bool_chk(&var2, &error);
2541 else if (tv_get_number_chk(&var2, &error) != 0)
2542 result = TRUE;
2543 clear_tv(&var2);
2544 if (error)
2545 return FAIL;
2546 }
2547 if (evaluate)
2548 {
2549 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002550 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002551 rettv->v_type = VAR_BOOL;
2552 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002553 }
2554 else
2555 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002556 rettv->v_type = VAR_NUMBER;
2557 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002558 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560
2561 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002563
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 if (evalarg == NULL)
2565 clear_evalarg(&local_evalarg, NULL);
2566 else
2567 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569
2570 return OK;
2571}
2572
2573/*
2574 * Handle second level expression:
2575 * expr3 && expr3 && expr3 logical AND
2576 *
2577 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002578 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 *
2580 * Return OK or FAIL.
2581 */
2582 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002585 char_u *p;
2586 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588 /*
2589 * Get the first variable.
2590 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 return FAIL;
2593
2594 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002595 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002597 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 evalarg_T *evalarg_used = evalarg;
2601 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002602 int orig_flags;
2603 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604 long result = TRUE;
2605 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002606 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002607 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002608
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002609 if (evalarg == NULL)
2610 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002611 CLEAR_FIELD(local_evalarg);
2612 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614 orig_flags = evalarg_used->eval_flags;
2615 evaluate = orig_flags & EVAL_EVALUATE;
2616 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002617 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002618 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002619 result = tv_get_bool_chk(rettv, &error);
2620 else if (tv_get_number_chk(rettv, &error) == 0)
2621 result = FALSE;
2622 clear_tv(rettv);
2623 if (error)
2624 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
2626
2627 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002628 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002630 while (p[0] == '&' && p[1] == '&')
2631 {
2632 if (getnext)
2633 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002634 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002635 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002636 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002637 {
2638 error_white_both(p, 2);
2639 clear_tv(rettv);
2640 return FAIL;
2641 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002642 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002643 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002644
2645 /*
2646 * Get the second variable.
2647 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002648 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2649 {
mityu4ac198c2021-05-28 17:52:40 +02002650 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002651 clear_tv(rettv);
2652 return FAIL;
2653 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2655 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002656 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002657 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002658 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002659 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660
2661 /*
2662 * Compute the result.
2663 */
2664 if (evaluate && result)
2665 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002666 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002667 result = tv_get_bool_chk(&var2, &error);
2668 else if (tv_get_number_chk(&var2, &error) == 0)
2669 result = FALSE;
2670 clear_tv(&var2);
2671 if (error)
2672 return FAIL;
2673 }
2674 if (evaluate)
2675 {
2676 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002677 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002678 rettv->v_type = VAR_BOOL;
2679 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002680 }
2681 else
2682 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002683 rettv->v_type = VAR_NUMBER;
2684 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002685 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002686 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002687
2688 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002690
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 if (evalarg == NULL)
2692 clear_evalarg(&local_evalarg, NULL);
2693 else
2694 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696
2697 return OK;
2698}
2699
2700/*
2701 * Handle third level expression:
2702 * var1 == var2
2703 * var1 =~ var2
2704 * var1 != var2
2705 * var1 !~ var2
2706 * var1 > var2
2707 * var1 >= var2
2708 * var1 < var2
2709 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002710 * var1 is var2
2711 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 *
2713 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002714 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 *
2716 * Return OK or FAIL.
2717 */
2718 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002719eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002722 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002723 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002725 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
2727 /*
2728 * Get the first variable.
2729 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002730 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 return FAIL;
2732
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002733 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002734 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002737 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002739 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002741 typval_T var2;
2742 int ic;
2743 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002744 int evaluate = evalarg == NULL
2745 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002746
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002747 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002748 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002749 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002750 p = *arg;
2751 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002752 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2753 {
mityu4ac198c2021-05-28 17:52:40 +02002754 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002755 clear_tv(rettv);
2756 return FAIL;
2757 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002758
Bram Moolenaar696ba232020-07-29 21:20:41 +02002759 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2760 {
2761 semsg(_(e_invexpr2), p);
2762 clear_tv(rettv);
2763 return FAIL;
2764 }
2765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002766 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (p[len] == '?')
2768 {
2769 ic = TRUE;
2770 ++len;
2771 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002772 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else if (p[len] == '#')
2774 {
2775 ic = FALSE;
2776 ++len;
2777 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002778 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002780 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
2782 /*
2783 * Get the second variable.
2784 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002785 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2786 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002787 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002788 clear_tv(rettv);
2789 return FAIL;
2790 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002791 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002792 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 return FAIL;
2796 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002797 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002798 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002799 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002800
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002801 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002802 {
2803 ret = FAIL;
2804 clear_tv(rettv);
2805 }
2806 else
2807 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002808 clear_tv(&var2);
2809 return ret;
2810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812
2813 return OK;
2814}
2815
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002816/*
2817 * Make a copy of blob "tv1" and append blob "tv2".
2818 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 void
2820eval_addblob(typval_T *tv1, typval_T *tv2)
2821{
2822 blob_T *b1 = tv1->vval.v_blob;
2823 blob_T *b2 = tv2->vval.v_blob;
2824 blob_T *b = blob_alloc();
2825 int i;
2826
2827 if (b != NULL)
2828 {
2829 for (i = 0; i < blob_len(b1); i++)
2830 ga_append(&b->bv_ga, blob_get(b1, i));
2831 for (i = 0; i < blob_len(b2); i++)
2832 ga_append(&b->bv_ga, blob_get(b2, i));
2833
2834 clear_tv(tv1);
2835 rettv_blob_set(tv1, b);
2836 }
2837}
2838
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002839/*
2840 * Make a copy of list "tv1" and append list "tv2".
2841 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 int
2843eval_addlist(typval_T *tv1, typval_T *tv2)
2844{
2845 typval_T var3;
2846
2847 // concatenate Lists
2848 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2849 {
2850 clear_tv(tv1);
2851 clear_tv(tv2);
2852 return FAIL;
2853 }
2854 clear_tv(tv1);
2855 *tv1 = var3;
2856 return OK;
2857}
2858
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859/*
2860 * Handle fourth level expression:
2861 * + number addition
2862 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002863 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002864 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 *
2866 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002867 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 *
2869 * Return OK or FAIL.
2870 */
2871 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002872eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 /*
2875 * Get the first variable.
2876 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 return FAIL;
2879
2880 /*
2881 * Repeat computing, until no '+', '-' or '.' is following.
2882 */
2883 for (;;)
2884 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002885 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002886 int getnext;
2887 char_u *p;
2888 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002889 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002890 int concat;
2891 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002892 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002894 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002895 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002896 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 p = eval_next_non_blank(*arg, evalarg, &getnext);
2898 op = *p;
2899 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002900 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2901 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002903 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2904 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002905
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002906 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002907 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002908 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002909 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002910 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002911 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002912 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002913 {
mityu4ac198c2021-05-28 17:52:40 +02002914 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002915 clear_tv(rettv);
2916 return FAIL;
2917 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002918 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002919 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002920 if ((op != '+' || (rettv->v_type != VAR_LIST
2921 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002922#ifdef FEAT_FLOAT
2923 && (op == '.' || rettv->v_type != VAR_FLOAT)
2924#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002925 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002926 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002927 int error = FALSE;
2928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002929 // For "list + ...", an illegal use of the first operand as
2930 // a number cannot be determined before evaluating the 2nd
2931 // operand: if this is also a list, all is ok.
2932 // For "something . ...", "something - ..." or "non-list + ...",
2933 // we know that the first operand needs to be a string or number
2934 // without evaluating the 2nd operand. So check before to avoid
2935 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002936 if (op != '.')
2937 tv_get_number_chk(rettv, &error);
2938 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002939 {
2940 clear_tv(rettv);
2941 return FAIL;
2942 }
2943 }
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 /*
2946 * Get the second variable.
2947 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002948 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002949 {
mityu89dcb4d2021-05-28 13:50:17 +02002950 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002951 clear_tv(rettv);
2952 return FAIL;
2953 }
2954 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002955 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 return FAIL;
2959 }
2960
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002961 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
2963 /*
2964 * Compute the result.
2965 */
2966 if (op == '.')
2967 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002968 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2969 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002970 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002971
Bram Moolenaar2e086612020-08-25 22:37:48 +02002972 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002973 || var2.v_type == VAR_CHANNEL
2974 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002975 semsg(_(e_using_invalid_value_as_string_str),
2976 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002977#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002978 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002979 {
2980 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2981 var2.vval.v_float);
2982 s2 = buf2;
2983 }
2984#endif
2985 else
2986 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002987 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002988 {
2989 clear_tv(rettv);
2990 clear_tv(&var2);
2991 return FAIL;
2992 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 clear_tv(rettv);
2995 rettv->v_type = VAR_STRING;
2996 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002998 else if (op == '+' && rettv->v_type == VAR_BLOB
2999 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003001 else if (op == '+' && rettv->v_type == VAR_LIST
3002 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003003 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003005 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 else
3008 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003009 int error = FALSE;
3010 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003011#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003012 float_T f1 = 0, f2 = 0;
3013
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003014 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003015 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016 f1 = rettv->vval.v_float;
3017 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003018 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 else
3020#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003022 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003023 if (error)
3024 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003025 // This can only happen for "list + non-list" or
3026 // "blob + non-blob". For "non-list + ..." or
3027 // "something - ...", we returned before evaluating the
3028 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003029 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003030 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003031 return FAIL;
3032 }
3033#ifdef FEAT_FLOAT
3034 if (var2.v_type == VAR_FLOAT)
3035 f1 = n1;
3036#endif
3037 }
3038#ifdef FEAT_FLOAT
3039 if (var2.v_type == VAR_FLOAT)
3040 {
3041 f2 = var2.vval.v_float;
3042 n2 = 0;
3043 }
3044 else
3045#endif
3046 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003047 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003048 if (error)
3049 {
3050 clear_tv(rettv);
3051 clear_tv(&var2);
3052 return FAIL;
3053 }
3054#ifdef FEAT_FLOAT
3055 if (rettv->v_type == VAR_FLOAT)
3056 f2 = n2;
3057#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060
3061#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003062 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3064 {
3065 if (op == '+')
3066 f1 = f1 + f2;
3067 else
3068 f1 = f1 - f2;
3069 rettv->v_type = VAR_FLOAT;
3070 rettv->vval.v_float = f1;
3071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073#endif
3074 {
3075 if (op == '+')
3076 n1 = n1 + n2;
3077 else
3078 n1 = n1 - n2;
3079 rettv->v_type = VAR_NUMBER;
3080 rettv->vval.v_number = n1;
3081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003083 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
3085 }
3086 return OK;
3087}
3088
3089/*
3090 * Handle fifth level expression:
3091 * * number multiplication
3092 * / number division
3093 * % number modulo
3094 *
3095 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003096 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 *
3098 * Return OK or FAIL.
3099 */
3100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003101eval6(
3102 char_u **arg,
3103 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003104 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003105 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003107#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003108 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
3111 /*
3112 * Get the first variable.
3113 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003114 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 return FAIL;
3116
3117 /*
3118 * Repeat computing, until no '*', '/' or '%' is following.
3119 */
3120 for (;;)
3121 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003122 int evaluate;
3123 int getnext;
3124 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003125 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003126 int op;
3127 varnumber_T n1, n2;
3128#ifdef FEAT_FLOAT
3129 float_T f1, f2;
3130#endif
3131 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003132
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003133 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003134 p = eval_next_non_blank(*arg, evalarg, &getnext);
3135 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003136 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003138
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003139 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003140 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003141 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003142 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003143 {
3144 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3145 {
mityu4ac198c2021-05-28 17:52:40 +02003146 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003147 clear_tv(rettv);
3148 return FAIL;
3149 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003150 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003153#ifdef FEAT_FLOAT
3154 f1 = 0;
3155 f2 = 0;
3156#endif
3157 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003158 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160#ifdef FEAT_FLOAT
3161 if (rettv->v_type == VAR_FLOAT)
3162 {
3163 f1 = rettv->vval.v_float;
3164 use_float = TRUE;
3165 n1 = 0;
3166 }
3167 else
3168#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003169 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003170 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003171 if (error)
3172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
3174 else
3175 n1 = 0;
3176
3177 /*
3178 * Get the second variable.
3179 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003180 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3181 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003182 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003183 clear_tv(rettv);
3184 return FAIL;
3185 }
3186 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003187 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 return FAIL;
3189
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003190 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192#ifdef FEAT_FLOAT
3193 if (var2.v_type == VAR_FLOAT)
3194 {
3195 if (!use_float)
3196 {
3197 f1 = n1;
3198 use_float = TRUE;
3199 }
3200 f2 = var2.vval.v_float;
3201 n2 = 0;
3202 }
3203 else
3204#endif
3205 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003206 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207 clear_tv(&var2);
3208 if (error)
3209 return FAIL;
3210#ifdef FEAT_FLOAT
3211 if (use_float)
3212 f2 = n2;
3213#endif
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
3216 /*
3217 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003218 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220#ifdef FEAT_FLOAT
3221 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223 if (op == '*')
3224 f1 = f1 * f2;
3225 else if (op == '/')
3226 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003227# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003228 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003229 if (f2 == 0.0)
3230 {
3231 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003232 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003233 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003234 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003235 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003236 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003237 }
3238 else
3239 f1 = f1 / f2;
3240# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003241 // We rely on the floating point library to handle divide
3242 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003243 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003244# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003247 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003249 return FAIL;
3250 }
3251 rettv->v_type = VAR_FLOAT;
3252 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
3254 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003257 int failed = FALSE;
3258
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003259 if (op == '*')
3260 n1 = n1 * n2;
3261 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003262 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003264 n1 = num_modulus(n1, n2, &failed);
3265 if (failed)
3266 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003267
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003268 rettv->v_type = VAR_NUMBER;
3269 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
3272 }
3273
3274 return OK;
3275}
3276
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003277/*
3278 * Handle a type cast before a base level expression.
3279 * "arg" must point to the first non-white of the expression.
3280 * "arg" is advanced to just after the recognized expression.
3281 * Return OK or FAIL.
3282 */
3283 static int
3284eval7t(
3285 char_u **arg,
3286 typval_T *rettv,
3287 evalarg_T *evalarg,
3288 int want_string) // after "." operator
3289{
3290 type_T *want_type = NULL;
3291 garray_T type_list; // list of pointers to allocated types
3292 int res;
3293 int evaluate = evalarg == NULL ? 0
3294 : (evalarg->eval_flags & EVAL_EVALUATE);
3295
3296 // Recognize <type> in Vim9 script only.
3297 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3298 {
3299 ++*arg;
3300 ga_init2(&type_list, sizeof(type_T *), 10);
3301 want_type = parse_type(arg, &type_list, TRUE);
3302 if (want_type == NULL && (evaluate || **arg != '>'))
3303 {
3304 clear_type_list(&type_list);
3305 return FAIL;
3306 }
3307
3308 if (**arg != '>')
3309 {
3310 if (*skipwhite(*arg) == '>')
3311 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3312 else
3313 emsg(_(e_missing_gt));
3314 clear_type_list(&type_list);
3315 return FAIL;
3316 }
3317 ++*arg;
3318 *arg = skipwhite_and_linebreak(*arg, evalarg);
3319 }
3320
3321 res = eval7(arg, rettv, evalarg, want_string);
3322
3323 if (want_type != NULL && evaluate)
3324 {
3325 if (res == OK)
3326 {
3327 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3328
3329 if (!equal_type(want_type, actual))
3330 {
3331 if (want_type == &t_bool && actual != &t_bool
3332 && (actual->tt_flags & TTFLAG_BOOL_OK))
3333 {
3334 int n = tv2bool(rettv);
3335
3336 // can use "0" and "1" for boolean in some places
3337 clear_tv(rettv);
3338 rettv->v_type = VAR_BOOL;
3339 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3340 }
3341 else
3342 {
3343 where_T where;
3344
3345 where.wt_index = 0;
3346 where.wt_variable = TRUE;
3347 res = check_type(want_type, actual, TRUE, where);
3348 }
3349 }
3350 }
3351 clear_type_list(&type_list);
3352 }
3353
3354 return res;
3355}
3356
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003357 int
3358eval_leader(char_u **arg, int vim9)
3359{
3360 char_u *s = *arg;
3361 char_u *p = *arg;
3362
3363 while (*p == '!' || *p == '-' || *p == '+')
3364 {
3365 char_u *n = skipwhite(p + 1);
3366
3367 // ++, --, -+ and +- are not accepted in Vim9 script
3368 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3369 {
3370 semsg(_(e_invexpr2), s);
3371 return FAIL;
3372 }
3373 p = n;
3374 }
3375 *arg = p;
3376 return OK;
3377}
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379/*
3380 * Handle sixth level expression:
3381 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003382 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003383 * "string" string constant
3384 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 * &option-name option value
3386 * @r register contents
3387 * identifier variable value
3388 * function() function call
3389 * $VAR environment variable
3390 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003391 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003393 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003394 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 *
3396 * Also handle:
3397 * ! in front logical NOT
3398 * - in front unary minus
3399 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003400 * trailing [] subscript in String or List
3401 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003402 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 *
3404 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003405 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 *
3407 * Return OK or FAIL.
3408 */
3409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003410eval7(
3411 char_u **arg,
3412 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003413 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003416 int evaluate = evalarg != NULL
3417 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 int len;
3419 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 char_u *start_leader, *end_leader;
3421 int ret = OK;
3422 char_u *alias;
3423
3424 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003426 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003428 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003431 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003434 if (eval_leader(arg, in_vim9script()) == FAIL)
3435 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 end_leader = *arg;
3437
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003438 if (**arg == '.' && (!isdigit(*(*arg + 1))
3439#ifdef FEAT_FLOAT
3440 || current_sctx.sc_version < 2
3441#endif
3442 ))
3443 {
3444 semsg(_(e_invexpr2), *arg);
3445 ++*arg;
3446 return FAIL;
3447 }
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 switch (**arg)
3450 {
3451 /*
3452 * Number constant.
3453 */
3454 case '0':
3455 case '1':
3456 case '2':
3457 case '3':
3458 case '4':
3459 case '5':
3460 case '6':
3461 case '7':
3462 case '8':
3463 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003464 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003465
3466 // Apply prefixed "-" and "+" now. Matters especially when
3467 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003468 if (ret == OK && evaluate && end_leader > start_leader
3469 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003470 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 break;
3472
3473 /*
3474 * String constant: "string".
3475 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003476 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 break;
3478
3479 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003480 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003482 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003483 break;
3484
3485 /*
3486 * List: [expr, expr]
3487 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003488 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 break;
3490
3491 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003492 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003493 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003494 case '#': if (in_vim9script())
3495 {
3496 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3497 }
3498 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003499 {
3500 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003501 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003502 }
3503 else
3504 ret = NOTDONE;
3505 break;
3506
3507 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003508 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003509 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003510 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003511 case '{': if (in_vim9script())
3512 ret = NOTDONE;
3513 else
3514 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003515 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003516 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 break;
3518
3519 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003520 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003522 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 break;
3524
3525 /*
3526 * Environment variable: $VAR.
3527 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003528 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 break;
3530
3531 /*
3532 * Register contents: @r.
3533 */
3534 case '@': ++*arg;
3535 if (evaluate)
3536 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003537 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3538 semsg(_(e_syntax_error_at_str), *arg);
3539 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3540 emsg_invreg(**arg);
3541 else
3542 {
3543 rettv->v_type = VAR_STRING;
3544 rettv->vval.v_string = get_reg_contents(**arg,
3545 GREG_EXPR_SRC);
3546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
3548 if (**arg != NUL)
3549 ++*arg;
3550 break;
3551
3552 /*
3553 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003554 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003556 case '(': ret = NOTDONE;
3557 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003558 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003559 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003560 if (ret == OK && evaluate)
3561 {
3562 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3563
3564 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003565 if (compile_def_function(ufunc,
3566 TRUE, PROFILING(ufunc), NULL) == FAIL)
3567 {
3568 clear_tv(rettv);
3569 ret = FAIL;
3570 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003571 }
3572 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003573 if (ret == NOTDONE)
3574 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003575 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003576 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003577
Bram Moolenaar9215f012020-06-27 21:18:00 +02003578 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003579 if (**arg == ')')
3580 ++*arg;
3581 else if (ret == OK)
3582 {
3583 emsg(_(e_missing_close));
3584 clear_tv(rettv);
3585 ret = FAIL;
3586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 break;
3589
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 break;
3592 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593
3594 if (ret == NOTDONE)
3595 {
3596 /*
3597 * Must be a variable or function name.
3598 * Can also be a curly-braces kind of name: {expr}.
3599 */
3600 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003601 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 if (alias != NULL)
3603 s = alias;
3604
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003605 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 ret = FAIL;
3607 else
3608 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003609 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3610
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003611 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003612 {
3613 emsg(_(e_cannot_use_underscore_here));
3614 ret = FAIL;
3615 }
3616 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003617 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003618 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003619 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003620 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003621 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003622 else if (flags & EVAL_CONSTANT)
3623 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003624 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003625 {
3626 // get the value of "true", "false" or a variable
3627 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3628 {
3629 rettv->v_type = VAR_BOOL;
3630 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003631 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003632 }
3633 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003634 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003635 {
3636 rettv->v_type = VAR_BOOL;
3637 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003638 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003639 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003640 else if (len == 4 && in_vim9script()
3641 && STRNCMP(s, "null", 4) == 0)
3642 {
3643 rettv->v_type = VAR_SPECIAL;
3644 rettv->vval.v_number = VVAL_NULL;
3645 ret = OK;
3646 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003647 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003648 ret = eval_variable(s, len, rettv, NULL,
3649 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003650 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003651 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003652 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003653 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003654 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003655 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003656 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003658 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659 }
3660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003661 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3662 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003663 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003664 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 /*
3667 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3668 */
3669 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003670 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003671 return ret;
3672}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003673
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003674/*
3675 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003676 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003677 * Adjusts "end_leaderp" until it is at "start_leader".
3678 */
3679 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003680eval7_leader(
3681 typval_T *rettv,
3682 int numeric_only,
3683 char_u *start_leader,
3684 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003685{
3686 char_u *end_leader = *end_leaderp;
3687 int ret = OK;
3688 int error = FALSE;
3689 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003690 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003691#ifdef FEAT_FLOAT
3692 float_T f = 0.0;
3693
3694 if (rettv->v_type == VAR_FLOAT)
3695 f = rettv->vval.v_float;
3696 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003697#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003698 {
3699 while (VIM_ISWHITE(end_leader[-1]))
3700 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003701 if (in_vim9script() && end_leader[-1] == '!')
3702 val = tv2bool(rettv);
3703 else
3704 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003705 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003706 if (error)
3707 {
3708 clear_tv(rettv);
3709 ret = FAIL;
3710 }
3711 else
3712 {
3713 while (end_leader > start_leader)
3714 {
3715 --end_leader;
3716 if (*end_leader == '!')
3717 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003718 if (numeric_only)
3719 {
3720 ++end_leader;
3721 break;
3722 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003723#ifdef FEAT_FLOAT
3724 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003725 {
3726 if (in_vim9script())
3727 {
3728 rettv->v_type = VAR_BOOL;
3729 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3730 }
3731 else
3732 f = !f;
3733 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003734 else
3735#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003736 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003737 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003738 type = VAR_BOOL;
3739 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003740 }
3741 else if (*end_leader == '-')
3742 {
3743#ifdef FEAT_FLOAT
3744 if (rettv->v_type == VAR_FLOAT)
3745 f = -f;
3746 else
3747#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003748 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003749 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003750 type = VAR_NUMBER;
3751 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003752 }
3753 }
3754#ifdef FEAT_FLOAT
3755 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003757 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003758 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003760 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003761#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003763 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003764 if (in_vim9script())
3765 rettv->v_type = type;
3766 else
3767 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003768 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003771 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return ret;
3773}
3774
3775/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003776 * Call the function referred to in "rettv".
3777 */
3778 static int
3779call_func_rettv(
3780 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003781 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003782 typval_T *rettv,
3783 int evaluate,
3784 dict_T *selfdict,
3785 typval_T *basetv)
3786{
3787 partial_T *pt = NULL;
3788 funcexe_T funcexe;
3789 typval_T functv;
3790 char_u *s;
3791 int ret;
3792
3793 // need to copy the funcref so that we can clear rettv
3794 if (evaluate)
3795 {
3796 functv = *rettv;
3797 rettv->v_type = VAR_UNKNOWN;
3798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003799 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003800 if (functv.v_type == VAR_PARTIAL)
3801 {
3802 pt = functv.vval.v_partial;
3803 s = partial_name(pt);
3804 }
3805 else
3806 s = functv.vval.v_string;
3807 }
3808 else
3809 s = (char_u *)"";
3810
Bram Moolenaara80faa82020-04-12 19:37:17 +02003811 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003812 funcexe.firstline = curwin->w_cursor.lnum;
3813 funcexe.lastline = curwin->w_cursor.lnum;
3814 funcexe.evaluate = evaluate;
3815 funcexe.partial = pt;
3816 funcexe.selfdict = selfdict;
3817 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003818 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003819
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003820 // Clear the funcref afterwards, so that deleting it while
3821 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003822 if (evaluate)
3823 clear_tv(&functv);
3824
3825 return ret;
3826}
3827
3828/*
3829 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003830 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003831 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3832 */
3833 static int
3834eval_lambda(
3835 char_u **arg,
3836 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003837 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003838 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003839{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003840 int evaluate = evalarg != NULL
3841 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003842 typval_T base = *rettv;
3843 int ret;
3844
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003845 rettv->v_type = VAR_UNKNOWN;
3846
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003847 if (**arg == '{')
3848 {
3849 // ->{lambda}()
3850 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3851 }
3852 else
3853 {
3854 // ->(lambda)()
3855 ++*arg;
3856 ret = eval1(arg, rettv, evalarg);
3857 *arg = skipwhite_and_linebreak(*arg, evalarg);
3858 if (**arg != ')')
3859 {
3860 emsg(_(e_missing_close));
3861 ret = FAIL;
3862 }
3863 ++*arg;
3864 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003865 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003866 return FAIL;
3867 else if (**arg != '(')
3868 {
3869 if (verbose)
3870 {
3871 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003872 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003873 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003875 }
3876 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003877 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003878 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003879 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003880 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003881
3882 // Clear the funcref afterwards, so that deleting it while
3883 // evaluating the arguments is possible (see test55).
3884 if (evaluate)
3885 clear_tv(&base);
3886
3887 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003888}
3889
3890/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003891 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003892 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003893 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3894 */
3895 static int
3896eval_method(
3897 char_u **arg,
3898 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003899 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003900 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003901{
3902 char_u *name;
3903 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003904 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003905 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003906 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003907 int evaluate = evalarg != NULL
3908 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003909
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003910 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003911
Bram Moolenaarac92e252019-08-03 21:58:38 +02003912 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003913 len = get_name_len(arg, &alias, evaluate, TRUE);
3914 if (alias != NULL)
3915 name = alias;
3916
3917 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003918 {
3919 if (verbose)
3920 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003921 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003922 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003923 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003924 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003925 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003926 if (**arg != '(')
3927 {
3928 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003930 ret = FAIL;
3931 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003932 else if (VIM_ISWHITE((*arg)[-1]))
3933 {
3934 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003935 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003936 ret = FAIL;
3937 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003938 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003939 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003940 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003941 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003942
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003943 // Clear the funcref afterwards, so that deleting it while
3944 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003945 if (evaluate)
3946 clear_tv(&base);
3947
Bram Moolenaarac92e252019-08-03 21:58:38 +02003948 return ret;
3949}
3950
3951/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003952 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3953 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003954 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3955 */
3956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003957eval_index(
3958 char_u **arg,
3959 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003960 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003961 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003962{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003963 int evaluate = evalarg != NULL
3964 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003965 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003967 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003968 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003969 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003970 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003971
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003972 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3973 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003974
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003975 init_tv(&var1);
3976 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003977 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003978 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003979 /*
3980 * dict.name
3981 */
3982 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003983 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003984 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003985 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003986 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003987 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003988 }
3989 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003990 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003991 /*
3992 * something[idx]
3993 *
3994 * Get the (first) variable from inside the [].
3995 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003996 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003997 if (**arg == ':')
3998 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003999 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004000 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004001 else if (vim9 && **arg == ':')
4002 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004003 semsg(_(e_white_space_required_before_and_after_str_at_str),
4004 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004005 clear_tv(&var1);
4006 return FAIL;
4007 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004008 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004009 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004010#ifdef FEAT_FLOAT
4011 // allow for indexing with float
4012 if (vim9 && rettv->v_type == VAR_DICT
4013 && var1.v_type == VAR_FLOAT)
4014 {
4015 var1.vval.v_string = typval_tostring(&var1, TRUE);
4016 var1.v_type = VAR_STRING;
4017 }
4018#endif
4019 if (tv_get_string_chk(&var1) == NULL)
4020 {
4021 // not a number or string
4022 clear_tv(&var1);
4023 return FAIL;
4024 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004025 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004026
4027 /*
4028 * Get the second variable from inside the [:].
4029 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004030 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 if (**arg == ':')
4032 {
4033 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004034 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004035 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004036 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004037 semsg(_(e_white_space_required_before_and_after_str_at_str),
4038 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004039 if (!empty1)
4040 clear_tv(&var1);
4041 return FAIL;
4042 }
4043 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004044 if (**arg == ']')
4045 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004046 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004048 if (!empty1)
4049 clear_tv(&var1);
4050 return FAIL;
4051 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004052 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004053 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004054 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004055 if (!empty1)
4056 clear_tv(&var1);
4057 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004058 return FAIL;
4059 }
4060 }
4061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004062 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004063 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004064 if (**arg != ']')
4065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004066 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004067 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004068 clear_tv(&var1);
4069 if (range)
4070 clear_tv(&var2);
4071 return FAIL;
4072 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004073 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004074 }
4075
4076 if (evaluate)
4077 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004078 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004079 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004080 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004081
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004082 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004084 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004085 clear_tv(&var2);
4086 return res;
4087 }
4088 return OK;
4089}
4090
4091/*
4092 * Check if "rettv" can have an [index] or [sli:ce]
4093 */
4094 int
4095check_can_index(typval_T *rettv, int evaluate, int verbose)
4096{
4097 switch (rettv->v_type)
4098 {
4099 case VAR_FUNC:
4100 case VAR_PARTIAL:
4101 if (verbose)
4102 emsg(_("E695: Cannot index a Funcref"));
4103 return FAIL;
4104 case VAR_FLOAT:
4105#ifdef FEAT_FLOAT
4106 if (verbose)
4107 emsg(_(e_float_as_string));
4108 return FAIL;
4109#endif
4110 case VAR_BOOL:
4111 case VAR_SPECIAL:
4112 case VAR_JOB:
4113 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004114 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004115 if (verbose)
4116 emsg(_(e_cannot_index_special_variable));
4117 return FAIL;
4118 case VAR_UNKNOWN:
4119 case VAR_ANY:
4120 case VAR_VOID:
4121 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004122 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004123 emsg(_(e_cannot_index_special_variable));
4124 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004125 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004126 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004127
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004128 case VAR_STRING:
4129 case VAR_LIST:
4130 case VAR_DICT:
4131 case VAR_BLOB:
4132 break;
4133 case VAR_NUMBER:
4134 if (in_vim9script())
4135 emsg(_(e_cannot_index_number));
4136 break;
4137 }
4138 return OK;
4139}
4140
4141/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004142 * slice() function
4143 */
4144 void
4145f_slice(typval_T *argvars, typval_T *rettv)
4146{
4147 if (check_can_index(argvars, TRUE, FALSE) == OK)
4148 {
4149 copy_tv(argvars, rettv);
4150 eval_index_inner(rettv, TRUE, argvars + 1,
4151 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4152 TRUE, NULL, 0, FALSE);
4153 }
4154}
4155
4156/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004157 * Apply index or range to "rettv".
4158 * "var1" is the first index, NULL for [:expr].
4159 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004160 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4161 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4163 */
4164 int
4165eval_index_inner(
4166 typval_T *rettv,
4167 int is_range,
4168 typval_T *var1,
4169 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004170 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004171 char_u *key,
4172 int keylen,
4173 int verbose)
4174{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004175 varnumber_T n1, n2 = 0;
4176 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004177
4178 n1 = 0;
4179 if (var1 != NULL && rettv->v_type != VAR_DICT)
4180 n1 = tv_get_number(var1);
4181
4182 if (is_range)
4183 {
4184 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004185 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004186 if (verbose)
4187 emsg(_(e_cannot_slice_dictionary));
4188 return FAIL;
4189 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004190 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004191 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004192 else
4193 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004194 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004195
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004196 switch (rettv->v_type)
4197 {
4198 case VAR_UNKNOWN:
4199 case VAR_ANY:
4200 case VAR_VOID:
4201 case VAR_FUNC:
4202 case VAR_PARTIAL:
4203 case VAR_FLOAT:
4204 case VAR_BOOL:
4205 case VAR_SPECIAL:
4206 case VAR_JOB:
4207 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004208 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004209 break; // not evaluating, skipping over subscript
4210
4211 case VAR_NUMBER:
4212 case VAR_STRING:
4213 {
4214 char_u *s = tv_get_string(rettv);
4215
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004216 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004217 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004218 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004219 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004220 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004221 else
4222 s = char_from_string(s, n1);
4223 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004224 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004225 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004226 // The resulting variable is a substring. If the indexes
4227 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004228 if (n1 < 0)
4229 {
4230 n1 = len + n1;
4231 if (n1 < 0)
4232 n1 = 0;
4233 }
4234 if (n2 < 0)
4235 n2 = len + n2;
4236 else if (n2 >= len)
4237 n2 = len;
4238 if (n1 >= len || n2 < 0 || n1 > n2)
4239 s = NULL;
4240 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004241 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004242 }
4243 else
4244 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004245 // The resulting variable is a string of a single
4246 // character. If the index is too big or negative the
4247 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004248 if (n1 >= len || n1 < 0)
4249 s = NULL;
4250 else
4251 s = vim_strnsave(s + n1, 1);
4252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 clear_tv(rettv);
4254 rettv->v_type = VAR_STRING;
4255 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004256 }
4257 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004258
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004259 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004260 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4261 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004262 break;
4263
4264 case VAR_LIST:
4265 if (var1 == NULL)
4266 n1 = 0;
4267 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004268 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004269 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004270 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004271 return FAIL;
4272 break;
4273
4274 case VAR_DICT:
4275 {
4276 dictitem_T *item;
4277 typval_T tmp;
4278
4279 if (key == NULL)
4280 {
4281 key = tv_get_string_chk(var1);
4282 if (key == NULL)
4283 return FAIL;
4284 }
4285
4286 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4287
4288 if (item == NULL && verbose)
4289 semsg(_(e_dictkey), key);
4290 if (item == NULL)
4291 return FAIL;
4292
4293 copy_tv(&item->di_tv, &tmp);
4294 clear_tv(rettv);
4295 *rettv = tmp;
4296 }
4297 break;
4298 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004299 return OK;
4300}
4301
4302/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004303 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004304 */
4305 char_u *
4306partial_name(partial_T *pt)
4307{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004308 if (pt != NULL)
4309 {
4310 if (pt->pt_name != NULL)
4311 return pt->pt_name;
4312 if (pt->pt_func != NULL)
4313 return pt->pt_func->uf_name;
4314 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004315 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004316}
4317
Bram Moolenaarddecc252016-04-06 22:59:37 +02004318 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004319partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004320{
4321 int i;
4322
4323 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004324 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004325 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004326 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004327 if (pt->pt_name != NULL)
4328 {
4329 func_unref(pt->pt_name);
4330 vim_free(pt->pt_name);
4331 }
4332 else
4333 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004334
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004335 // Decrease the reference count for the context of a closure. If down
4336 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004337 if (pt->pt_funcstack != NULL)
4338 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004339 --pt->pt_funcstack->fs_refcount;
4340 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004341 }
4342
Bram Moolenaarddecc252016-04-06 22:59:37 +02004343 vim_free(pt);
4344}
4345
4346/*
4347 * Unreference a closure: decrement the reference count and free it when it
4348 * becomes zero.
4349 */
4350 void
4351partial_unref(partial_T *pt)
4352{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004353 if (pt != NULL)
4354 {
4355 if (--pt->pt_refcount <= 0)
4356 partial_free(pt);
4357
4358 // If the reference count goes down to one, the funcstack may be the
4359 // only reference and can be freed if no other partials reference it.
4360 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4361 funcstack_check_refcount(pt->pt_funcstack);
4362 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004363}
4364
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004365/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004366 * Return the next (unique) copy ID.
4367 * Used for serializing nested structures.
4368 */
4369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004370get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004371{
4372 current_copyID += COPYID_INC;
4373 return current_copyID;
4374}
4375
4376/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004377 * Garbage collection for lists and dictionaries.
4378 *
4379 * We use reference counts to be able to free most items right away when they
4380 * are no longer used. But for composite items it's possible that it becomes
4381 * unused while the reference count is > 0: When there is a recursive
4382 * reference. Example:
4383 * :let l = [1, 2, 3]
4384 * :let d = {9: l}
4385 * :let l[1] = d
4386 *
4387 * Since this is quite unusual we handle this with garbage collection: every
4388 * once in a while find out which lists and dicts are not referenced from any
4389 * variable.
4390 *
4391 * Here is a good reference text about garbage collection (refers to Python
4392 * but it applies to all reference-counting mechanisms):
4393 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004394 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004395
4396/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004397 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004398 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004399 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004400 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004401 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004402garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004403{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004404 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004405 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004406 buf_T *buf;
4407 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004408 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004409 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004410
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004411 if (!testing)
4412 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004413 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004414 want_garbage_collect = FALSE;
4415 may_garbage_collect = FALSE;
4416 garbage_collect_at_exit = FALSE;
4417 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004418
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004419 // The execution stack can grow big, limit the size.
4420 if (exestack.ga_maxlen - exestack.ga_len > 500)
4421 {
4422 size_t new_len;
4423 char_u *pp;
4424 int n;
4425
4426 // Keep 150% of the current size, with a minimum of the growth size.
4427 n = exestack.ga_len / 2;
4428 if (n < exestack.ga_growsize)
4429 n = exestack.ga_growsize;
4430
4431 // Don't make it bigger though.
4432 if (exestack.ga_len + n < exestack.ga_maxlen)
4433 {
4434 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4435 pp = vim_realloc(exestack.ga_data, new_len);
4436 if (pp == NULL)
4437 return FAIL;
4438 exestack.ga_maxlen = exestack.ga_len + n;
4439 exestack.ga_data = pp;
4440 }
4441 }
4442
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004443 // We advance by two because we add one for items referenced through
4444 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004445 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004446
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004447 /*
4448 * 1. Go through all accessible variables and mark all lists and dicts
4449 * with copyID.
4450 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004452 // Don't free variables in the previous_funccal list unless they are only
4453 // referenced through previous_funccal. This must be first, because if
4454 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004455 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004456
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004457 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004458 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004459
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004460 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004461 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004462 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4463 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004465 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004466 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004467 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4468 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004469 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004470 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4471 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004472#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004473 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004474 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4475 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004476 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004477 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004478 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4479 NULL, NULL);
4480#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004483 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004484 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4485 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004486 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004487 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004489 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004490 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004492 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004493 abort = abort || set_ref_in_functions(copyID);
4494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004495 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004496 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004498 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004499 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004500
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004501 // callbacks in buffers
4502 abort = abort || set_ref_in_buffers(copyID);
4503
Bram Moolenaar1dced572012-04-05 16:54:08 +02004504#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004505 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004506#endif
4507
Bram Moolenaardb913952012-06-29 12:54:53 +02004508#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004509 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004510#endif
4511
4512#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004513 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004514#endif
4515
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004517 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004518 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004519#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004520#ifdef FEAT_NETBEANS_INTG
4521 abort = abort || set_ref_in_nb_channel(copyID);
4522#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004523
Bram Moolenaare3188e22016-05-31 21:13:04 +02004524#ifdef FEAT_TIMERS
4525 abort = abort || set_ref_in_timer(copyID);
4526#endif
4527
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004528#ifdef FEAT_QUICKFIX
4529 abort = abort || set_ref_in_quickfix(copyID);
4530#endif
4531
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004532#ifdef FEAT_TERMINAL
4533 abort = abort || set_ref_in_term(copyID);
4534#endif
4535
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004536#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004537 abort = abort || set_ref_in_popups(copyID);
4538#endif
4539
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004540 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004541 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004542 /*
4543 * 2. Free lists and dictionaries that are not referenced.
4544 */
4545 did_free = free_unref_items(copyID);
4546
4547 /*
4548 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004550 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004551 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004552 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004553 else if (p_verbose > 0)
4554 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004555 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004556 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004557
4558 return did_free;
4559}
4560
4561/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004562 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004563 */
4564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004565free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004566{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004567 int did_free = FALSE;
4568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 // Let all "free" functions know that we are here. This means no
4570 // dictionaries, lists, channels or jobs are to be freed, because we will
4571 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004572 in_free_unref_items = TRUE;
4573
4574 /*
4575 * PASS 1: free the contents of the items. We don't free the items
4576 * themselves yet, so that it is possible to decrement refcount counters
4577 */
4578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004579 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004580 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004581
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004582 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004583 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004584
4585#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004586 // Go through the list of jobs and free items without the copyID. This
4587 // must happen before doing channels, because jobs refer to channels, but
4588 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004589 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004591 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004592 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4593#endif
4594
4595 /*
4596 * PASS 2: free the items themselves.
4597 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004598 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004599 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004600
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004601#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004602 // Go through the list of jobs and free items without the copyID. This
4603 // must happen before doing channels, because jobs refer to channels, but
4604 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004605 free_unused_jobs(copyID, COPYID_MASK);
4606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004607 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004608 free_unused_channels(copyID, COPYID_MASK);
4609#endif
4610
4611 in_free_unref_items = FALSE;
4612
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004613 return did_free;
4614}
4615
4616/*
4617 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004618 * "list_stack" is used to add lists to be marked. Can be NULL.
4619 *
4620 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004621 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004622 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004623set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004624{
4625 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004626 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004627 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004628 hashtab_T *cur_ht;
4629 ht_stack_T *ht_stack = NULL;
4630 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004631
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004632 cur_ht = ht;
4633 for (;;)
4634 {
4635 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004636 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004637 // Mark each item in the hashtab. If the item contains a hashtab
4638 // it is added to ht_stack, if it contains a list it is added to
4639 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004640 todo = (int)cur_ht->ht_used;
4641 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4642 if (!HASHITEM_EMPTY(hi))
4643 {
4644 --todo;
4645 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4646 &ht_stack, list_stack);
4647 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004648 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004649
4650 if (ht_stack == NULL)
4651 break;
4652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004653 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004654 cur_ht = ht_stack->ht;
4655 tempitem = ht_stack;
4656 ht_stack = ht_stack->prev;
4657 free(tempitem);
4658 }
4659
4660 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004661}
4662
4663/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004664 * Mark a dict and its items with "copyID".
4665 * Returns TRUE if setting references failed somehow.
4666 */
4667 int
4668set_ref_in_dict(dict_T *d, int copyID)
4669{
4670 if (d != NULL && d->dv_copyID != copyID)
4671 {
4672 d->dv_copyID = copyID;
4673 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4674 }
4675 return FALSE;
4676}
4677
4678/*
4679 * Mark a list and its items with "copyID".
4680 * Returns TRUE if setting references failed somehow.
4681 */
4682 int
4683set_ref_in_list(list_T *ll, int copyID)
4684{
4685 if (ll != NULL && ll->lv_copyID != copyID)
4686 {
4687 ll->lv_copyID = copyID;
4688 return set_ref_in_list_items(ll, copyID, NULL);
4689 }
4690 return FALSE;
4691}
4692
4693/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004694 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004695 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4696 *
4697 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004698 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004699 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004700set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004701{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004702 listitem_T *li;
4703 int abort = FALSE;
4704 list_T *cur_l;
4705 list_stack_T *list_stack = NULL;
4706 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004707
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004708 cur_l = l;
4709 for (;;)
4710 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004711 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // Mark each item in the list. If the item contains a hashtab
4713 // it is added to ht_stack, if it contains a list it is added to
4714 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004715 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4716 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4717 ht_stack, &list_stack);
4718 if (list_stack == NULL)
4719 break;
4720
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004721 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004722 cur_l = list_stack->list;
4723 tempitem = list_stack;
4724 list_stack = list_stack->prev;
4725 free(tempitem);
4726 }
4727
4728 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004729}
4730
4731/*
4732 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004733 * "list_stack" is used to add lists to be marked. Can be NULL.
4734 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4735 *
4736 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004737 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004739set_ref_in_item(
4740 typval_T *tv,
4741 int copyID,
4742 ht_stack_T **ht_stack,
4743 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004744{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004745 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004746
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004747 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004748 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004749 dict_T *dd = tv->vval.v_dict;
4750
Bram Moolenaara03f2332016-02-06 18:09:59 +01004751 if (dd != NULL && dd->dv_copyID != copyID)
4752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004753 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004754 dd->dv_copyID = copyID;
4755 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004756 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004757 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4758 }
4759 else
4760 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004761 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4762
Bram Moolenaara03f2332016-02-06 18:09:59 +01004763 if (newitem == NULL)
4764 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004765 else
4766 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004767 newitem->ht = &dd->dv_hashtab;
4768 newitem->prev = *ht_stack;
4769 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004770 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004771 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004772 }
4773 }
4774 else if (tv->v_type == VAR_LIST)
4775 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004776 list_T *ll = tv->vval.v_list;
4777
Bram Moolenaara03f2332016-02-06 18:09:59 +01004778 if (ll != NULL && ll->lv_copyID != copyID)
4779 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004781 ll->lv_copyID = copyID;
4782 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004783 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004784 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004785 }
4786 else
4787 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004788 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4789
Bram Moolenaara03f2332016-02-06 18:09:59 +01004790 if (newitem == NULL)
4791 abort = TRUE;
4792 else
4793 {
4794 newitem->list = ll;
4795 newitem->prev = *list_stack;
4796 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004797 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004798 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004799 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004800 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004801 else if (tv->v_type == VAR_FUNC)
4802 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004803 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004804 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004805 else if (tv->v_type == VAR_PARTIAL)
4806 {
4807 partial_T *pt = tv->vval.v_partial;
4808 int i;
4809
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004810 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004811 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004812 // Didn't see this partial yet.
4813 pt->pt_copyID = copyID;
4814
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004815 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004816
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 if (pt->pt_dict != NULL)
4818 {
4819 typval_T dtv;
4820
4821 dtv.v_type = VAR_DICT;
4822 dtv.vval.v_dict = pt->pt_dict;
4823 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4824 }
4825
4826 for (i = 0; i < pt->pt_argc; ++i)
4827 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4828 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004829 if (pt->pt_funcstack != NULL)
4830 {
4831 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4832
4833 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4834 abort = abort || set_ref_in_item(stack + i, copyID,
4835 ht_stack, list_stack);
4836 }
4837
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004838 }
4839 }
4840#ifdef FEAT_JOB_CHANNEL
4841 else if (tv->v_type == VAR_JOB)
4842 {
4843 job_T *job = tv->vval.v_job;
4844 typval_T dtv;
4845
4846 if (job != NULL && job->jv_copyID != copyID)
4847 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004848 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004849 if (job->jv_channel != NULL)
4850 {
4851 dtv.v_type = VAR_CHANNEL;
4852 dtv.vval.v_channel = job->jv_channel;
4853 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4854 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004855 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004856 {
4857 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004858 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004859 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4860 }
4861 }
4862 }
4863 else if (tv->v_type == VAR_CHANNEL)
4864 {
4865 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004866 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004867 typval_T dtv;
4868 jsonq_T *jq;
4869 cbq_T *cq;
4870
4871 if (ch != NULL && ch->ch_copyID != copyID)
4872 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004873 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004874 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004875 {
4876 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4877 jq = jq->jq_next)
4878 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4879 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4880 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004881 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004882 {
4883 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004884 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004885 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4886 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004887 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004888 {
4889 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004890 dtv.vval.v_partial =
4891 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004892 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4893 }
4894 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004895 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004896 {
4897 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004898 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004899 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4900 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004901 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004902 {
4903 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004904 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004905 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4906 }
4907 }
4908 }
4909#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004910 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004911}
4912
Bram Moolenaar8c711452005-01-14 21:53:12 +00004913/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 * Return a string with the string representation of a variable.
4915 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004916 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004917 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004918 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004919 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004920 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004921 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4922 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004923 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004924 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004925 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004926echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004927 typval_T *tv,
4928 char_u **tofree,
4929 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004930 int copyID,
4931 int echo_style,
4932 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004933 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004935 static int recurse = 0;
4936 char_u *r = NULL;
4937
Bram Moolenaar33570922005-01-25 22:26:29 +00004938 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004939 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004940 if (!did_echo_string_emsg)
4941 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004942 // Only give this message once for a recursive call to avoid
4943 // flooding the user with errors. And stop iterating over lists
4944 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004945 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004946 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004947 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004948 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004949 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004950 }
4951 ++recurse;
4952
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004953 switch (tv->v_type)
4954 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004955 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004956 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004957 {
4958 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004959 r = tv->vval.v_string;
4960 if (r == NULL)
4961 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004962 }
4963 else
4964 {
4965 *tofree = string_quote(tv->vval.v_string, FALSE);
4966 r = *tofree;
4967 }
4968 break;
4969
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004970 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004971 if (echo_style)
4972 {
4973 *tofree = NULL;
4974 r = tv->vval.v_string;
4975 }
4976 else
4977 {
4978 *tofree = string_quote(tv->vval.v_string, TRUE);
4979 r = *tofree;
4980 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004981 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004982
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004983 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004984 {
4985 partial_T *pt = tv->vval.v_partial;
4986 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004987 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004988 garray_T ga;
4989 int i;
4990 char_u *tf;
4991
4992 ga_init2(&ga, 1, 100);
4993 ga_concat(&ga, (char_u *)"function(");
4994 if (fname != NULL)
4995 {
4996 ga_concat(&ga, fname);
4997 vim_free(fname);
4998 }
4999 if (pt != NULL && pt->pt_argc > 0)
5000 {
5001 ga_concat(&ga, (char_u *)", [");
5002 for (i = 0; i < pt->pt_argc; ++i)
5003 {
5004 if (i > 0)
5005 ga_concat(&ga, (char_u *)", ");
5006 ga_concat(&ga,
5007 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5008 vim_free(tf);
5009 }
5010 ga_concat(&ga, (char_u *)"]");
5011 }
5012 if (pt != NULL && pt->pt_dict != NULL)
5013 {
5014 typval_T dtv;
5015
5016 ga_concat(&ga, (char_u *)", ");
5017 dtv.v_type = VAR_DICT;
5018 dtv.vval.v_dict = pt->pt_dict;
5019 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5020 vim_free(tf);
5021 }
5022 ga_concat(&ga, (char_u *)")");
5023
5024 *tofree = ga.ga_data;
5025 r = *tofree;
5026 break;
5027 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005028
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005029 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005030 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005031 break;
5032
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005033 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005034 if (tv->vval.v_list == NULL)
5035 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005036 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005037 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005038 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005039 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005040 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5041 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005042 {
5043 *tofree = NULL;
5044 r = (char_u *)"[...]";
5045 }
5046 else
5047 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005048 int old_copyID = tv->vval.v_list->lv_copyID;
5049
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005050 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005051 *tofree = list2string(tv, copyID, restore_copyID);
5052 if (restore_copyID)
5053 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005054 r = *tofree;
5055 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005056 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005057
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005059 if (tv->vval.v_dict == NULL)
5060 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005061 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005062 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005063 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005064 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005065 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5066 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005067 {
5068 *tofree = NULL;
5069 r = (char_u *)"{...}";
5070 }
5071 else
5072 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005073 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005074
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005075 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005076 *tofree = dict2string(tv, copyID, restore_copyID);
5077 if (restore_copyID)
5078 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005079 r = *tofree;
5080 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005081 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005082
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005084 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005085 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005087 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005088 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005089 break;
5090
Bram Moolenaar835dc632016-02-07 14:27:38 +01005091 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005092 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005093 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005094 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005095 if (composite_val)
5096 {
5097 *tofree = string_quote(r, FALSE);
5098 r = *tofree;
5099 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005100 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005101
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005102 case VAR_INSTR:
5103 *tofree = NULL;
5104 r = (char_u *)"instructions";
5105 break;
5106
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005107 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005108#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 *tofree = NULL;
5110 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5111 r = numbuf;
5112 break;
5113#endif
5114
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005115 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005116 case VAR_SPECIAL:
5117 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005118 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005119 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005121
Bram Moolenaar8502c702014-06-17 12:51:16 +02005122 if (--recurse == 0)
5123 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005124 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005125}
5126
5127/*
5128 * Return a string with the string representation of a variable.
5129 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5130 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005131 * Does not put quotes around strings, as ":echo" displays values.
5132 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5133 * May return NULL.
5134 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005135 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005136echo_string(
5137 typval_T *tv,
5138 char_u **tofree,
5139 char_u *numbuf,
5140 int copyID)
5141{
5142 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5143}
5144
5145/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005146 * Return string "str" in ' quotes, doubling ' characters.
5147 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005150 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005151string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005152{
Bram Moolenaar33570922005-01-25 22:26:29 +00005153 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005154 char_u *p, *r, *s;
5155
Bram Moolenaar33570922005-01-25 22:26:29 +00005156 len = (function ? 13 : 3);
5157 if (str != NULL)
5158 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005159 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005160 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005161 if (*p == '\'')
5162 ++len;
5163 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005164 s = r = alloc(len);
5165 if (r != NULL)
5166 {
5167 if (function)
5168 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005170 r += 10;
5171 }
5172 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005174 if (str != NULL)
5175 for (p = str; *p != NUL; )
5176 {
5177 if (*p == '\'')
5178 *r++ = '\'';
5179 MB_COPY_CHAR(p, r);
5180 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005182 if (function)
5183 *r++ = ')';
5184 *r++ = NUL;
5185 }
5186 return s;
5187}
5188
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005189#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005190/*
5191 * Convert the string "text" to a floating point number.
5192 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5193 * this always uses a decimal point.
5194 * Returns the length of the text that was consumed.
5195 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005196 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005197string2float(
5198 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005200{
5201 char *s = (char *)text;
5202 float_T f;
5203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005204 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005205 if (STRNICMP(text, "inf", 3) == 0)
5206 {
5207 *value = INFINITY;
5208 return 3;
5209 }
5210 if (STRNICMP(text, "-inf", 3) == 0)
5211 {
5212 *value = -INFINITY;
5213 return 4;
5214 }
5215 if (STRNICMP(text, "nan", 3) == 0)
5216 {
5217 *value = NAN;
5218 return 3;
5219 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005220 f = strtod(s, &s);
5221 *value = f;
5222 return (int)((char_u *)s - text);
5223}
5224#endif
5225
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005226/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005227 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5228 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005229 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005230 */
5231 int
5232buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5233{
5234 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005235 char_u *t;
5236 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005237
5238 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5239 return -1;
5240
5241 if (lnum > buf->b_ml.ml_line_count)
5242 lnum = buf->b_ml.ml_line_count;
5243
5244 str = ml_get_buf(buf, lnum, FALSE);
5245 if (str == NULL)
5246 return -1;
5247
5248 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005249 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005250
Bram Moolenaar91458462021-01-13 20:08:38 +01005251 // count the number of characters
5252 t = str;
5253 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5254 t += mb_ptr2len(t);
5255
5256 // In insert mode, when the cursor is at the end of a non-empty line,
5257 // byteidx points to the NUL character immediately past the end of the
5258 // string. In this case, add one to the character count.
5259 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5260 count++;
5261
5262 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005263}
5264
5265/*
5266 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005267 * byte index. Works only for loaded buffers. Returns -1 on failure.
5268 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005269 */
5270 int
5271buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5272{
5273 char_u *str;
5274 char_u *t;
5275
5276 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5277 return -1;
5278
5279 if (lnum > buf->b_ml.ml_line_count)
5280 lnum = buf->b_ml.ml_line_count;
5281
5282 str = ml_get_buf(buf, lnum, FALSE);
5283 if (str == NULL)
5284 return -1;
5285
5286 // Convert the character offset to a byte offset
5287 t = str;
5288 while (*t != NUL && --charidx > 0)
5289 t += mb_ptr2len(t);
5290
Bram Moolenaar91458462021-01-13 20:08:38 +01005291 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005292}
5293
5294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005296 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005299var2fpos(
5300 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005301 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005302 int *fnum, // set to fnum for '0, 'A, etc.
5303 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005305 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005307 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005310 if (varp->v_type == VAR_LIST)
5311 {
5312 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005313 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005314 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005315 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005316
5317 l = varp->vval.v_list;
5318 if (l == NULL)
5319 return NULL;
5320
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005321 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005322 pos.lnum = list_find_nr(l, 0L, &error);
5323 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005324 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005325 if (charcol)
5326 len = (long)mb_charlen(ml_get(pos.lnum));
5327 else
5328 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005329
Bram Moolenaarec65d772020-08-20 22:29:12 +02005330 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005331 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005332 li = list_find(l, 1L);
5333 if (li != NULL && li->li_tv.v_type == VAR_STRING
5334 && li->li_tv.vval.v_string != NULL
5335 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005336 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005337 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005338 }
5339 else
5340 {
5341 pos.col = list_find_nr(l, 1L, &error);
5342 if (error)
5343 return NULL;
5344 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005346 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005347 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005348 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005349 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005352 pos.coladd = list_find_nr(l, 2L, &error);
5353 if (error)
5354 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005355
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005356 return &pos;
5357 }
5358
Bram Moolenaarc5809432021-03-27 21:23:30 +01005359 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5360 return NULL;
5361
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005362 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 if (name == NULL)
5364 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005365 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005366 {
5367 pos = curwin->w_cursor;
5368 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005369 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005370 return &pos;
5371 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005372 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005373 {
5374 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005375 pos = VIsual;
5376 else
5377 pos = curwin->w_cursor;
5378 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005379 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005380 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005381 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005382 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005384 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5386 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005387 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005388 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 return pp;
5390 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005391
Bram Moolenaara5525202006-03-02 22:52:09 +00005392 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005393
Bram Moolenaar477933c2007-07-17 14:32:23 +00005394 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005395 {
5396 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005397 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005398 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005399 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005400 // In silent Ex mode topline is zero, but that's not a valid line
5401 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005402 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005403 return &pos;
5404 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005405 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005406 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005407 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005408 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005409 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005410 return &pos;
5411 }
5412 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005413 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005415 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 pos.lnum = curbuf->b_ml.ml_line_count;
5418 pos.col = 0;
5419 }
5420 else
5421 {
5422 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005423 if (charcol)
5424 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5425 else
5426 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
5428 return &pos;
5429 }
5430 return NULL;
5431}
5432
5433/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005434 * Convert list in "arg" into a position and optional file number.
5435 * When "fnump" is NULL there is no file number, only 3 items.
5436 * Note that the column is passed on as-is, the caller may want to decrement
5437 * it to use 1 for the first column.
5438 * Return FAIL when conversion is not possible, doesn't check the position for
5439 * validity.
5440 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005442list2fpos(
5443 typval_T *arg,
5444 pos_T *posp,
5445 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005446 colnr_T *curswantp,
5447 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005448{
5449 list_T *l = arg->vval.v_list;
5450 long i = 0;
5451 long n;
5452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5454 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005455 if (arg->v_type != VAR_LIST
5456 || l == NULL
5457 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005458 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005459 return FAIL;
5460
5461 if (fnump != NULL)
5462 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005464 if (n < 0)
5465 return FAIL;
5466 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005467 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005468 *fnump = n;
5469 }
5470
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005471 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005472 if (n < 0)
5473 return FAIL;
5474 posp->lnum = n;
5475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005476 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005477 if (n < 0)
5478 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005479 // If character position is specified, then convert to byte position
5480 if (charcol)
5481 {
5482 buf_T *buf;
5483
5484 // Get the text for the specified line in a loaded buffer
5485 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5486 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5487 return FAIL;
5488
Bram Moolenaar91458462021-01-13 20:08:38 +01005489 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005490 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005491 posp->col = n;
5492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005493 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005494 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005495 posp->coladd = 0;
5496 else
5497 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005498
Bram Moolenaar493c1782014-05-28 14:34:46 +02005499 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005501
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005502 return OK;
5503}
5504
5505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 * Get the length of an environment variable name.
5507 * Advance "arg" to the first character after the name.
5508 * Return 0 for error.
5509 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005511get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
5513 char_u *p;
5514 int len;
5515
5516 for (p = *arg; vim_isIDc(*p); ++p)
5517 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005518 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 return 0;
5520
5521 len = (int)(p - *arg);
5522 *arg = p;
5523 return len;
5524}
5525
5526/*
5527 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005528 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 * Return 0 if something is wrong.
5530 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005532get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534 char_u *p;
5535 int len;
5536
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005537 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005539 {
5540 if (*p == ':')
5541 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005542 // "s:" is start of "s:var", but "n:" is not and can be used in
5543 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005544 len = (int)(p - *arg);
5545 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5546 || len > 1)
5547 break;
5548 }
5549 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return 0;
5552
5553 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005554 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
5556 return len;
5557}
5558
5559/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005560 * Get the length of the name of a variable or function.
5561 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005563 * Return -1 if curly braces expansion failed.
5564 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 * If the name contains 'magic' {}'s, expand them and return the
5566 * expanded name in an allocated string via 'alias' - caller must free.
5567 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005568 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005569get_name_len(
5570 char_u **arg,
5571 char_u **alias,
5572 int evaluate,
5573 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
5575 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 char_u *p;
5577 char_u *expr_start;
5578 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005580 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
5582 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5583 && (*arg)[2] == (int)KE_SNR)
5584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 *arg += 3;
5587 return get_id_len(arg) + 3;
5588 }
5589 len = eval_fname_script(*arg);
5590 if (len > 0)
5591 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005592 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 *arg += len;
5594 }
5595
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005599 p = find_name_end(*arg, &expr_start, &expr_end,
5600 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 if (expr_start != NULL)
5602 {
5603 char_u *temp_string;
5604
5605 if (!evaluate)
5606 {
5607 len += (int)(p - *arg);
5608 *arg = skipwhite(p);
5609 return len;
5610 }
5611
5612 /*
5613 * Include any <SID> etc in the expanded string:
5614 * Thus the -len here.
5615 */
5616 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5617 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005618 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 *alias = temp_string;
5620 *arg = skipwhite(p);
5621 return (int)STRLEN(temp_string);
5622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005625 // Only give an error when there is something, otherwise it will be
5626 // reported at a higher level.
5627 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005628 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
5630 return len;
5631}
5632
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633/*
5634 * Find the end of a variable or function name, taking care of magic braces.
5635 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5636 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005637 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 * Return a pointer to just after the name. Equal to "arg" if there is no
5639 * valid name.
5640 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005641 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005642find_name_end(
5643 char_u *arg,
5644 char_u **expr_start,
5645 char_u **expr_end,
5646 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 int mb_nest = 0;
5649 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005651 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005652 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 *expr_start = NULL;
5657 *expr_end = NULL;
5658 }
5659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005660 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005661 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5662 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005663 return arg;
5664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 for (p = arg; *p != NUL
5666 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005667 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005668 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005669 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005671 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005673 if (*p == '\'')
5674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005675 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005676 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005677 ;
5678 if (*p == NUL)
5679 break;
5680 }
5681 else if (*p == '"')
5682 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005683 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005684 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005685 if (*p == '\\' && p[1] != NUL)
5686 ++p;
5687 if (*p == NUL)
5688 break;
5689 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005690 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5691 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005692 // "s:" is start of "s:var", but "n:" is not and can be used in
5693 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005694 len = (int)(p - arg);
5695 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005696 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005697 break;
5698 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005702 if (*p == '[')
5703 ++br_nest;
5704 else if (*p == ']')
5705 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005707
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005708 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005710 if (*p == '{')
5711 {
5712 mb_nest++;
5713 if (expr_start != NULL && *expr_start == NULL)
5714 *expr_start = p;
5715 }
5716 else if (*p == '}')
5717 {
5718 mb_nest--;
5719 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5720 *expr_end = p;
5721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724
5725 return p;
5726}
5727
5728/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005729 * Expands out the 'magic' {}'s in a variable/function name.
5730 * Note that this can call itself recursively, to deal with
5731 * constructs like foo{bar}{baz}{bam}
5732 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5733 * "in_start" ^
5734 * "expr_start" ^
5735 * "expr_end" ^
5736 * "in_end" ^
5737 *
5738 * Returns a new allocated string, which the caller must free.
5739 * Returns NULL for failure.
5740 */
5741 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005742make_expanded_name(
5743 char_u *in_start,
5744 char_u *expr_start,
5745 char_u *expr_end,
5746 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005747{
5748 char_u c1;
5749 char_u *retval = NULL;
5750 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005751
5752 if (expr_end == NULL || in_end == NULL)
5753 return NULL;
5754 *expr_start = NUL;
5755 *expr_end = NUL;
5756 c1 = *in_end;
5757 *in_end = NUL;
5758
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005759 temp_result = eval_to_string(expr_start + 1, FALSE);
5760 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005761 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005762 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5763 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005764 if (retval != NULL)
5765 {
5766 STRCPY(retval, in_start);
5767 STRCAT(retval, temp_result);
5768 STRCAT(retval, expr_end + 1);
5769 }
5770 }
5771 vim_free(temp_result);
5772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005773 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005774 *expr_start = '{';
5775 *expr_end = '}';
5776
5777 if (retval != NULL)
5778 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005779 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005780 if (expr_start != NULL)
5781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005782 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005783 temp_result = make_expanded_name(retval, expr_start,
5784 expr_end, temp_result);
5785 vim_free(retval);
5786 retval = temp_result;
5787 }
5788 }
5789
5790 return retval;
5791}
5792
5793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005795 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005798eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005800 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005801}
5802
5803/*
5804 * Return TRUE if character "c" can be used as the first character in a
5805 * variable or function name (excluding '{' and '}').
5806 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005808eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005809{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005810 return ASCII_ISALPHA(c) || c == '_';
5811}
5812
5813/*
5814 * Return TRUE if character "c" can be used as the first character of a
5815 * dictionary key.
5816 */
5817 int
5818eval_isdictc(int c)
5819{
5820 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821}
5822
5823/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005824 * Handle:
5825 * - expr[expr], expr[expr:expr] subscript
5826 * - ".name" lookup
5827 * - function call with Funcref variable: func(expr)
5828 * - method call: var->method()
5829 *
5830 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005831 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005833handle_subscript(
5834 char_u **arg,
5835 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005836 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005837 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005838{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005839 int evaluate = evalarg != NULL
5840 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005841 int ret = OK;
5842 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005843 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005844 int getnext;
5845 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005846
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005847 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005848 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005849 // When at the end of the line and ".name" or "->{" or "->X" follows in
5850 // the next line then consume the line break.
5851 p = eval_next_non_blank(*arg, evalarg, &getnext);
5852 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005853 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005854 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005855 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005856 {
5857 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005858 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005859 check_white = FALSE;
5860 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005861
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005862 if (rettv->v_type == VAR_ANY)
5863 {
5864 char_u *exp_name;
5865 int cc;
5866 int idx;
5867 ufunc_T *ufunc;
5868 type_T *type;
5869
5870 // Found script from "import * as {name}", script item name must
5871 // follow.
5872 if (**arg != '.')
5873 {
5874 if (verbose)
5875 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5876 ret = FAIL;
5877 break;
5878 }
5879 ++*arg;
5880 if (IS_WHITE_OR_NUL(**arg))
5881 {
5882 if (verbose)
5883 emsg(_(e_no_white_space_allowed_after_dot));
5884 ret = FAIL;
5885 break;
5886 }
5887
5888 // isolate the name
5889 exp_name = *arg;
5890 while (eval_isnamec(**arg))
5891 ++*arg;
5892 cc = **arg;
5893 **arg = NUL;
5894
5895 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5896 evalarg->eval_cctx, verbose);
5897 **arg = cc;
5898 *arg = skipwhite(*arg);
5899
5900 if (idx < 0 && ufunc == NULL)
5901 {
5902 ret = FAIL;
5903 break;
5904 }
5905 if (idx >= 0)
5906 {
5907 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5908 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5909
5910 copy_tv(sv->sv_tv, rettv);
5911 }
5912 else
5913 {
5914 rettv->v_type = VAR_FUNC;
5915 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5916 }
5917 }
5918
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005919 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5920 || rettv->v_type == VAR_PARTIAL))
5921 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005922 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005923 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5924 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005925
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005926 // Stop the expression evaluation when immediately aborting on
5927 // error, or when an interrupt occurred or an exception was thrown
5928 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005929 if (aborting())
5930 {
5931 if (ret == OK)
5932 clear_tv(rettv);
5933 ret = FAIL;
5934 }
5935 dict_unref(selfdict);
5936 selfdict = NULL;
5937 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005938 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005939 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005940 if (in_vim9script())
5941 *arg = skipwhite(p + 2);
5942 else
5943 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005944 if (ret == OK)
5945 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005946 if (VIM_ISWHITE(**arg))
5947 {
5948 emsg(_(e_nowhitespace));
5949 ret = FAIL;
5950 }
5951 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005952 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005953 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005954 else
5955 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005956 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005957 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005958 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005959 // "." is ".name" lookup when we found a dict or when evaluating and
5960 // scriptversion is at least 2, where string concatenation is "..".
5961 else if (**arg == '['
5962 || (**arg == '.' && (rettv->v_type == VAR_DICT
5963 || (!evaluate
5964 && (*arg)[1] != '.'
5965 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005966 {
5967 dict_unref(selfdict);
5968 if (rettv->v_type == VAR_DICT)
5969 {
5970 selfdict = rettv->vval.v_dict;
5971 if (selfdict != NULL)
5972 ++selfdict->dv_refcount;
5973 }
5974 else
5975 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005976 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005977 {
5978 clear_tv(rettv);
5979 ret = FAIL;
5980 }
5981 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005982 else
5983 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005984 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005986 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5987 // Don't do this when "Func" is already a partial that was bound
5988 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005989 if (selfdict != NULL
5990 && (rettv->v_type == VAR_FUNC
5991 || (rettv->v_type == VAR_PARTIAL
5992 && (rettv->vval.v_partial->pt_auto
5993 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005994 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005995
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005996 dict_unref(selfdict);
5997 return ret;
5998}
5999
6000/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006001 * Make a copy of an item.
6002 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006003 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6004 * reference to an already copied list/dict can be used.
6005 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008item_copy(
6009 typval_T *from,
6010 typval_T *to,
6011 int deep,
6012 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006013{
6014 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006015 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006016
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006019 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006020 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006021 }
6022 ++recurse;
6023
6024 switch (from->v_type)
6025 {
6026 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006027 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006028 case VAR_STRING:
6029 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006030 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006031 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006032 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006033 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006034 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006035 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006036 copy_tv(from, to);
6037 break;
6038 case VAR_LIST:
6039 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006040 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006041 if (from->vval.v_list == NULL)
6042 to->vval.v_list = NULL;
6043 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6044 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006045 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006046 to->vval.v_list = from->vval.v_list->lv_copylist;
6047 ++to->vval.v_list->lv_refcount;
6048 }
6049 else
6050 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6051 if (to->vval.v_list == NULL)
6052 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006054 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006056 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006057 case VAR_DICT:
6058 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006059 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006060 if (from->vval.v_dict == NULL)
6061 to->vval.v_dict = NULL;
6062 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006064 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006065 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6066 ++to->vval.v_dict->dv_refcount;
6067 }
6068 else
6069 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6070 if (to->vval.v_dict == NULL)
6071 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006072 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006073 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006074 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006076 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006077 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006078 }
6079 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006080 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006081}
6082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006083 void
6084echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6085{
6086 char_u *tofree;
6087 char_u numbuf[NUMBUFLEN];
6088 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6089
6090 if (*atstart)
6091 {
6092 *atstart = FALSE;
6093 // Call msg_start() after eval1(), evaluating the expression
6094 // may cause a message to appear.
6095 if (with_space)
6096 {
6097 // Mark the saved text as finishing the line, so that what
6098 // follows is displayed on a new line when scrolling back
6099 // at the more prompt.
6100 msg_sb_eol();
6101 msg_start();
6102 }
6103 }
6104 else if (with_space)
6105 msg_puts_attr(" ", echo_attr);
6106
6107 if (p != NULL)
6108 for ( ; *p != NUL && !got_int; ++p)
6109 {
6110 if (*p == '\n' || *p == '\r' || *p == TAB)
6111 {
6112 if (*p != TAB && *needclr)
6113 {
6114 // remove any text still there from the command
6115 msg_clr_eos();
6116 *needclr = FALSE;
6117 }
6118 msg_putchar_attr(*p, echo_attr);
6119 }
6120 else
6121 {
6122 if (has_mbyte)
6123 {
6124 int i = (*mb_ptr2len)(p);
6125
6126 (void)msg_outtrans_len_attr(p, i, echo_attr);
6127 p += i - 1;
6128 }
6129 else
6130 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6131 }
6132 }
6133 vim_free(tofree);
6134}
6135
Bram Moolenaare9a41262005-01-15 22:18:47 +00006136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 * ":echo expr1 ..." print each argument separated with a space, add a
6138 * newline at the end.
6139 * ":echon expr1 ..." print each argument plain.
6140 */
6141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006142ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
6144 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006146 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 int needclr = TRUE;
6148 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006149 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006150 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006151 evalarg_T evalarg;
6152
Bram Moolenaare707c882020-07-01 13:07:37 +02006153 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154
6155 if (eap->skip)
6156 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006157 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006159 // If eval1() causes an error message the text from the command may
6160 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006161 need_clr_eos = needclr;
6162
Bram Moolenaar68db9962021-05-09 23:19:22 +02006163 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006164 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 {
6166 /*
6167 * Report the invalid expression unless the expression evaluation
6168 * has been cancelled due to an aborting error, an interrupt, or an
6169 * exception.
6170 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006171 if (!aborting() && did_emsg == did_emsg_before
6172 && called_emsg == called_emsg_before)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006173 semsg(_(e_invexpr2), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006174 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 break;
6176 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006177 need_clr_eos = FALSE;
6178
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006180 {
6181 if (rettv.v_type == VAR_VOID)
6182 {
6183 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6184 break;
6185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006187 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006188
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006189 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 arg = skipwhite(arg);
6191 }
6192 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006193 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
6195 if (eap->skip)
6196 --emsg_skip;
6197 else
6198 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006199 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 if (needclr)
6201 msg_clr_eos();
6202 if (eap->cmdidx == CMD_echo)
6203 msg_end();
6204 }
6205}
6206
6207/*
6208 * ":echohl {name}".
6209 */
6210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006211ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006213 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214}
6215
6216/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006217 * Returns the :echo attribute
6218 */
6219 int
6220get_echo_attr(void)
6221{
6222 return echo_attr;
6223}
6224
6225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 * ":execute expr1 ..." execute the result of an expression.
6227 * ":echomsg expr1 ..." Print a message
6228 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006229 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 * Each gets spaces around each argument and a newline at the end for
6231 * echo commands
6232 */
6233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 int ret = OK;
6239 char_u *p;
6240 garray_T ga;
6241 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
6243 ga_init2(&ga, 1, 80);
6244
6245 if (eap->skip)
6246 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006247 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006249 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006250 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
6253 if (!eap->skip)
6254 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006255 char_u buf[NUMBUFLEN];
6256
6257 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006258 {
6259 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6260 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006261 semsg(_(e_using_invalid_value_as_string_str),
6262 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006263 p = NULL;
6264 }
6265 else
6266 p = tv_get_string_buf(&rettv, buf);
6267 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006268 else
6269 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006270 if (p == NULL)
6271 {
6272 clear_tv(&rettv);
6273 ret = FAIL;
6274 break;
6275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 len = (int)STRLEN(p);
6277 if (ga_grow(&ga, len + 2) == FAIL)
6278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006279 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 ret = FAIL;
6281 break;
6282 }
6283 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 ga.ga_len += len;
6287 }
6288
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006289 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 arg = skipwhite(arg);
6291 }
6292
6293 if (ret != FAIL && ga.ga_data != NULL)
6294 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006295 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6296 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006297 // Mark the already saved text as finishing the line, so that what
6298 // follows is displayed on a new line when scrolling back at the
6299 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006300 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006301 }
6302
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006304 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006305 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006306 out_flush();
6307 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006308 else if (eap->cmdidx == CMD_echoconsole)
6309 {
6310 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6311 ui_write((char_u *)"\r\n", 2, TRUE);
6312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 else if (eap->cmdidx == CMD_echoerr)
6314 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006315 int save_did_emsg = did_emsg;
6316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006317 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006318 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 if (!force_abort)
6320 did_emsg = save_did_emsg;
6321 }
6322 else if (eap->cmdidx == CMD_execute)
6323 do_cmdline((char_u *)ga.ga_data,
6324 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6325 }
6326
6327 ga_clear(&ga);
6328
6329 if (eap->skip)
6330 --emsg_skip;
6331
6332 eap->nextcmd = check_nextcmd(arg);
6333}
6334
6335/*
6336 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6337 * "arg" points to the "&" or '+' when called, to "option" when returning.
6338 * Returns NULL when no option name found. Otherwise pointer to the char
6339 * after the option name.
6340 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006341 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006342find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 char_u *p = *arg;
6345
6346 ++p;
6347 if (*p == 'g' && p[1] == ':')
6348 {
6349 *opt_flags = OPT_GLOBAL;
6350 p += 2;
6351 }
6352 else if (*p == 'l' && p[1] == ':')
6353 {
6354 *opt_flags = OPT_LOCAL;
6355 p += 2;
6356 }
6357 else
6358 *opt_flags = 0;
6359
6360 if (!ASCII_ISALPHA(*p))
6361 return NULL;
6362 *arg = p;
6363
6364 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006365 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 else
6367 while (ASCII_ISALPHA(*p))
6368 ++p;
6369 return p;
6370}
6371
6372/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006373 * Display script name where an item was last set.
6374 * Should only be invoked when 'verbose' is non-zero.
6375 */
6376 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006377last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006378{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006379 char_u *p;
6380
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006381 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006382 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006383 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006384 if (p != NULL)
6385 {
6386 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006387 msg_puts(_("\n\tLast set from "));
6388 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006389 if (script_ctx.sc_lnum > 0)
6390 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006391 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006392 msg_outnum((long)script_ctx.sc_lnum);
6393 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006394 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006395 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006396 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006397 }
6398}
6399
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006400#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402/*
6403 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006404 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 * "flags" can be "g" to do a global substitute.
6406 * Returns an allocated string, NULL for error.
6407 */
6408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409do_string_sub(
6410 char_u *str,
6411 char_u *pat,
6412 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006413 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 int sublen;
6417 regmatch_T regmatch;
6418 int i;
6419 int do_all;
6420 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006421 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 garray_T ga;
6423 char_u *ret;
6424 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006425 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006427 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006429 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430
6431 ga_init2(&ga, 1, 200);
6432
6433 do_all = (flags[0] == 'g');
6434
6435 regmatch.rm_ic = p_ic;
6436 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6437 if (regmatch.regprog != NULL)
6438 {
6439 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006440 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6442 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006443 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006444 if (regmatch.startp[0] == regmatch.endp[0])
6445 {
6446 if (zero_width == regmatch.startp[0])
6447 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006448 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006449 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006450 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6451 (size_t)i);
6452 ga.ga_len += i;
6453 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006454 continue;
6455 }
6456 zero_width = regmatch.startp[0];
6457 }
6458
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 /*
6460 * Get some space for a temporary buffer to do the substitution
6461 * into. It will contain:
6462 * - The text up to where the match is.
6463 * - The substituted text.
6464 * - The text after the match.
6465 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006466 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006467 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6469 {
6470 ga_clear(&ga);
6471 break;
6472 }
6473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006474 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 i = (int)(regmatch.startp[0] - tail);
6476 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006477 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006478 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 + ga.ga_len + i, TRUE, TRUE, FALSE);
6480 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006481 tail = regmatch.endp[0];
6482 if (*tail == NUL)
6483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (!do_all)
6485 break;
6486 }
6487
6488 if (ga.ga_data != NULL)
6489 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6490
Bram Moolenaar473de612013-06-08 18:19:48 +02006491 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 }
6493
6494 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6495 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006496 if (p_cpo == empty_option)
6497 p_cpo = save_cpo;
6498 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006499 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006500 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006501 // If it's still empty it was changed and restored, need to restore in
6502 // the complicated way.
6503 if (*p_cpo == NUL)
6504 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006505 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508 return ret;
6509}