blob: 7a05d359bac4516ad00ede475e8079b65ce300f4 [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 Moolenaar6f1d2aa2021-06-01 21:21:55 +02001465 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1466 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001467 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001469 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001470 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 if (di == NULL)
1472 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001473 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1474 {
1475 vim_free(di);
1476 return;
1477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001478 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001479 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001480 else if (op != NULL && *op != '=')
1481 {
1482 tv_op(lp->ll_tv, rettv, op);
1483 return;
1484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001487
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001488 /*
1489 * Assign the value to the variable or list item.
1490 */
1491 if (copy)
1492 copy_tv(rettv, lp->ll_tv);
1493 else
1494 {
1495 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001496 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001497 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001498 }
1499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001500}
1501
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001502/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001503 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1504 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505 * Returns OK or FAIL.
1506 */
1507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001510 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511 char_u numbuf[NUMBUFLEN];
1512 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001513 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001515 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001516 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001517 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001518 {
1519 switch (tv1->v_type)
1520 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001521 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001522 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001524 case VAR_DICT:
1525 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001526 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001527 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001528 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001529 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001530 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001531 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532 break;
1533
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001534 case VAR_BLOB:
1535 if (*op != '+' || tv2->v_type != VAR_BLOB)
1536 break;
1537 // BLOB += BLOB
1538 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1539 {
1540 blob_T *b1 = tv1->vval.v_blob;
1541 blob_T *b2 = tv2->vval.v_blob;
1542 int i, len = blob_len(b2);
1543 for (i = 0; i < len; i++)
1544 ga_append(&b1->bv_ga, blob_get(b2, i));
1545 }
1546 return OK;
1547
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001548 case VAR_LIST:
1549 if (*op != '+' || tv2->v_type != VAR_LIST)
1550 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001551 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001552 if (tv2->vval.v_list != NULL)
1553 {
1554 if (tv1->vval.v_list == NULL)
1555 {
1556 tv1->vval.v_list = tv2->vval.v_list;
1557 ++tv1->vval.v_list->lv_refcount;
1558 }
1559 else
1560 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 return OK;
1563
1564 case VAR_NUMBER:
1565 case VAR_STRING:
1566 if (tv2->v_type == VAR_LIST)
1567 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001568 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001570 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001571 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001572#ifdef FEAT_FLOAT
1573 if (tv2->v_type == VAR_FLOAT)
1574 {
1575 float_T f = n;
1576
Bram Moolenaarff697e62019-02-12 22:28:33 +01001577 if (*op == '%')
1578 break;
1579 switch (*op)
1580 {
1581 case '+': f += tv2->vval.v_float; break;
1582 case '-': f -= tv2->vval.v_float; break;
1583 case '*': f *= tv2->vval.v_float; break;
1584 case '/': f /= tv2->vval.v_float; break;
1585 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001586 clear_tv(tv1);
1587 tv1->v_type = VAR_FLOAT;
1588 tv1->vval.v_float = f;
1589 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001590 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591#endif
1592 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001593 switch (*op)
1594 {
1595 case '+': n += tv_get_number(tv2); break;
1596 case '-': n -= tv_get_number(tv2); break;
1597 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001598 case '/': n = num_divide(n, tv_get_number(tv2),
1599 &failed); break;
1600 case '%': n = num_modulus(n, tv_get_number(tv2),
1601 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001602 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 clear_tv(tv1);
1604 tv1->v_type = VAR_NUMBER;
1605 tv1->vval.v_number = n;
1606 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 }
1608 else
1609 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001610 if (tv2->v_type == VAR_FLOAT)
1611 break;
1612
Bram Moolenaarff697e62019-02-12 22:28:33 +01001613 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001614 s = tv_get_string(tv1);
1615 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001616 clear_tv(tv1);
1617 tv1->v_type = VAR_STRING;
1618 tv1->vval.v_string = s;
1619 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001620 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001622 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001623#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 {
1625 float_T f;
1626
Bram Moolenaarff697e62019-02-12 22:28:33 +01001627 if (*op == '%' || *op == '.'
1628 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001629 && tv2->v_type != VAR_NUMBER
1630 && tv2->v_type != VAR_STRING))
1631 break;
1632 if (tv2->v_type == VAR_FLOAT)
1633 f = tv2->vval.v_float;
1634 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001635 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001636 switch (*op)
1637 {
1638 case '+': tv1->vval.v_float += f; break;
1639 case '-': tv1->vval.v_float -= f; break;
1640 case '*': tv1->vval.v_float *= f; break;
1641 case '/': tv1->vval.v_float /= f; break;
1642 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001643 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001644#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001645 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 }
1647 }
1648
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001649 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001650 return FAIL;
1651}
1652
1653/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 * Evaluate the expression used in a ":for var in expr" command.
1655 * "arg" points to "var".
1656 * Set "*errp" to TRUE for an error, FALSE otherwise;
1657 * Return a pointer that holds the info. Null when there is an error.
1658 */
1659 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001660eval_for_line(
1661 char_u *arg,
1662 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001663 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001664 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665{
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001668 typval_T tv;
1669 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001670 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001672 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001674 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 if (fi == NULL)
1676 return NULL;
1677
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001678 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 if (expr == NULL)
1680 return fi;
1681
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001682 expr = skipwhite_and_linebreak(expr, evalarg);
1683 if (expr[0] != 'i' || expr[1] != 'n'
1684 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 return fi;
1688 }
1689
1690 if (skip)
1691 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001692 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1693 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 {
1695 *errp = FALSE;
1696 if (!skip)
1697 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001698 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001699 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001700 l = tv.vval.v_list;
1701 if (l == NULL)
1702 {
1703 // a null list is like an empty list: do nothing
1704 clear_tv(&tv);
1705 }
1706 else
1707 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001709 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001711 // No need to increment the refcount, it's already set for
1712 // the list being used in "tv".
1713 fi->fi_list = l;
1714 list_add_watch(l, &fi->fi_lw);
1715 fi->fi_lw.lw_item = l->lv_first;
1716 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001717 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001718 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001719 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001720 fi->fi_bi = 0;
1721 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001722 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001723 typval_T btv;
1724
1725 // Make a copy, so that the iteration still works when the
1726 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001728 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001729 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001730 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001731 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001732 else if (tv.v_type == VAR_STRING)
1733 {
1734 fi->fi_byte_idx = 0;
1735 fi->fi_string = tv.vval.v_string;
1736 tv.vval.v_string = NULL;
1737 if (fi->fi_string == NULL)
1738 fi->fi_string = vim_strsave((char_u *)"");
1739 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 else
1741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001742 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 }
1745 }
1746 }
1747 if (skip)
1748 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001749 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750
1751 return fi;
1752}
1753
1754/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001755 * Used when looping over a :for line, skip the "in expr" part.
1756 */
1757 void
1758skip_for_lines(void *fi_void, evalarg_T *evalarg)
1759{
1760 forinfo_T *fi = (forinfo_T *)fi_void;
1761 int i;
1762
1763 for (i = 0; i < fi->fi_break_count; ++i)
1764 eval_next_line(evalarg);
1765}
1766
1767/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 * Use the first item in a ":for" list. Advance to the next.
1769 * Assign the values to the variable (list). "arg" points to the first one.
1770 * Return TRUE when a valid item was found, FALSE when at end of list or
1771 * something wrong.
1772 */
1773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001776 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001778 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1779 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1780 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001783 if (fi->fi_blob != NULL)
1784 {
1785 typval_T tv;
1786
1787 if (fi->fi_bi >= blob_len(fi->fi_blob))
1788 return FALSE;
1789 tv.v_type = VAR_NUMBER;
1790 tv.v_lock = VAR_FIXED;
1791 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1792 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001793 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001794 fi->fi_varcount, flag, NULL) == OK;
1795 }
1796
1797 if (fi->fi_string != NULL)
1798 {
1799 typval_T tv;
1800 int len;
1801
1802 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1803 if (len == 0)
1804 return FALSE;
1805 tv.v_type = VAR_STRING;
1806 tv.v_lock = VAR_FIXED;
1807 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1808 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001809 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001810 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001811 vim_free(tv.vval.v_string);
1812 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001813 }
1814
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 item = fi->fi_lw.lw_item;
1816 if (item == NULL)
1817 result = FALSE;
1818 else
1819 {
1820 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001821 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001822 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 }
1824 return result;
1825}
1826
1827/*
1828 * Free the structure used to store info used by ":for".
1829 */
1830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001831free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832{
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001835 if (fi == NULL)
1836 return;
1837 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001838 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001840 list_unref(fi->fi_list);
1841 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001842 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001843 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001844 else
1845 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 vim_free(fi);
1847}
1848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001850set_context_for_expression(
1851 expand_T *xp,
1852 char_u *arg,
1853 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001855 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001859 if (cmdidx == CMD_let || cmdidx == CMD_var
1860 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 {
1862 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001863 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001865 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001866 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 {
1868 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001869 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001870 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 break;
1872 }
1873 return;
1874 }
1875 }
1876 else
1877 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1878 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 while ((xp->xp_pattern = vim_strpbrk(arg,
1880 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1881 {
1882 c = *xp->xp_pattern;
1883 if (c == '&')
1884 {
1885 c = xp->xp_pattern[1];
1886 if (c == '&')
1887 {
1888 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001889 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 }
1891 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001894 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1895 xp->xp_pattern += 2;
1896
1897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899 else if (c == '$')
1900 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001901 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 xp->xp_context = EXPAND_ENV_VARS;
1903 }
1904 else if (c == '=')
1905 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001906 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 xp->xp_context = EXPAND_EXPRESSION;
1908 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001909 else if (c == '#'
1910 && xp->xp_context == EXPAND_EXPRESSION)
1911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001912 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001913 break;
1914 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001915 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 && xp->xp_context == EXPAND_FUNCTIONS
1917 && vim_strchr(xp->xp_pattern, '(') == NULL)
1918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 break;
1921 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001922 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001924 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
1926 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1927 if (c == '\\' && xp->xp_pattern[1] != NUL)
1928 ++xp->xp_pattern;
1929 xp->xp_context = EXPAND_NOTHING;
1930 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001931 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001933 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1935 /* skip */ ;
1936 xp->xp_context = EXPAND_NOTHING;
1937 }
1938 else if (c == '|')
1939 {
1940 if (xp->xp_pattern[1] == '|')
1941 {
1942 ++xp->xp_pattern;
1943 xp->xp_context = EXPAND_EXPRESSION;
1944 }
1945 else
1946 xp->xp_context = EXPAND_COMMANDS;
1947 }
1948 else
1949 xp->xp_context = EXPAND_EXPRESSION;
1950 }
1951 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001952 // Doesn't look like something valid, expand as an expression
1953 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 arg = xp->xp_pattern;
1956 if (*arg != NUL)
1957 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1958 /* skip */ ;
1959 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001960
1961 // ":exe one two" completes "two"
1962 if ((cmdidx == CMD_execute
1963 || cmdidx == CMD_echo
1964 || cmdidx == CMD_echon
1965 || cmdidx == CMD_echomsg)
1966 && xp->xp_context == EXPAND_EXPRESSION)
1967 {
1968 for (;;)
1969 {
1970 char_u *n = skiptowhite(arg);
1971
1972 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1973 break;
1974 arg = skipwhite(n);
1975 }
1976 }
1977
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 xp->xp_pattern = arg;
1979}
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001982 * Return TRUE if "pat" matches "text".
1983 * Does not use 'cpo' and always uses 'magic'.
1984 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001985 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001986pattern_match(char_u *pat, char_u *text, int ic)
1987{
1988 int matches = FALSE;
1989 char_u *save_cpo;
1990 regmatch_T regmatch;
1991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001992 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001993 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001994 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001995 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1996 if (regmatch.regprog != NULL)
1997 {
1998 regmatch.rm_ic = ic;
1999 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2000 vim_regfree(regmatch.regprog);
2001 }
2002 p_cpo = save_cpo;
2003 return matches;
2004}
2005
2006/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002007 * Handle a name followed by "(". Both for just "name(arg)" and for
2008 * "expr->name(arg)".
2009 * Returns OK or FAIL.
2010 */
2011 static int
2012eval_func(
2013 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002014 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002015 char_u *name,
2016 int name_len,
2017 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002018 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002019 typval_T *basetv) // "expr" for "expr->name(arg)"
2020{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002021 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002022 char_u *s = name;
2023 int len = name_len;
2024 partial_T *partial;
2025 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002026 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002027
2028 if (!evaluate)
2029 check_vars(s, len);
2030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002031 // If "s" is the name of a variable of type VAR_FUNC
2032 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002033 s = deref_func_name(s, &len, &partial,
2034 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002036 // Need to make a copy, in case evaluating the arguments makes
2037 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002038 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002039 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002040 ret = FAIL;
2041 else
2042 {
2043 funcexe_T funcexe;
2044
2045 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002046 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002047 funcexe.firstline = curwin->w_cursor.lnum;
2048 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002049 funcexe.evaluate = evaluate;
2050 funcexe.partial = partial;
2051 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002052 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002053 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002054 }
2055 vim_free(s);
2056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002057 // If evaluate is FALSE rettv->v_type was not set in
2058 // get_func_tv, but it's needed in handle_subscript() to parse
2059 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002060 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2061 {
2062 rettv->vval.v_string = NULL;
2063 rettv->v_type = VAR_FUNC;
2064 }
2065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002066 // Stop the expression evaluation when immediately
2067 // aborting on error, or when an interrupt occurred or
2068 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002069 if (evaluate && aborting())
2070 {
2071 if (ret == OK)
2072 clear_tv(rettv);
2073 ret = FAIL;
2074 }
2075 return ret;
2076}
2077
2078/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002079 * Get the next line source line without advancing. But do skip over comment
2080 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002081 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002082 */
2083 static char_u *
2084getline_peek_skip_comments(evalarg_T *evalarg)
2085{
2086 for (;;)
2087 {
2088 char_u *next = getline_peek(evalarg->eval_getline,
2089 evalarg->eval_cookie);
2090 char_u *p;
2091
2092 if (next == NULL)
2093 break;
2094 p = skipwhite(next);
2095 if (*p != NUL && !vim9_comment_start(p))
2096 return next;
2097 (void)eval_next_line(evalarg);
2098 }
2099 return NULL;
2100}
2101
2102/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002103 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2104 * comment) and there is a next line, return the next line (skipping blanks)
2105 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002106 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2107 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108 * "arg" must point somewhere inside a line, not at the start.
2109 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002110 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2112{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002113 char_u *p = skipwhite(arg);
2114
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002115 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002116 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002117 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002118 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002119 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002120 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002121 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002122
2123 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002124 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002125 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002126 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002127
Bram Moolenaar9d489562020-07-30 20:08:50 +02002128 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002129 {
2130 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002131 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002132 }
2133 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002134 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002135}
2136
2137/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002138 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002139 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002140 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002141 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002142eval_next_line(evalarg_T *evalarg)
2143{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002144 garray_T *gap = &evalarg->eval_ga;
2145 char_u *line;
2146
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002147 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002148 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2149 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002150 else
2151 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002152 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002153 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2154 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002155 char_u *p = skipwhite(line);
2156
2157 // Going to concatenate the lines after parsing. For an empty or
2158 // comment line use an empty string.
2159 if (*p == NUL || vim9_comment_start(p))
2160 {
2161 vim_free(line);
2162 line = vim_strsave((char_u *)"");
2163 }
2164
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002165 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2166 ++gap->ga_len;
2167 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002168 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002169 {
2170 vim_free(evalarg->eval_tofree);
2171 evalarg->eval_tofree = line;
2172 }
2173 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002174}
2175
Bram Moolenaare6b53242020-07-01 17:28:33 +02002176/*
2177 * Call eval_next_non_blank() and get the next line if needed.
2178 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002179 char_u *
2180skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2181{
2182 int getnext;
2183 char_u *p = skipwhite(arg);
2184
Bram Moolenaar962d7212020-07-04 14:15:00 +02002185 if (evalarg == NULL)
2186 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002187 eval_next_non_blank(p, evalarg, &getnext);
2188 if (getnext)
2189 return eval_next_line(evalarg);
2190 return p;
2191}
2192
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002193/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002194 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002195 */
2196 void
2197clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2198{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002199 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002200 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002201 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002202 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002203 if (eap != NULL)
2204 {
2205 // We may need to keep the original command line, e.g. for
2206 // ":let" it has the variable names. But we may also need the
2207 // new one, "nextcmd" points into it. Keep both.
2208 vim_free(eap->cmdline_tofree);
2209 eap->cmdline_tofree = *eap->cmdlinep;
2210 *eap->cmdlinep = evalarg->eval_tofree;
2211 }
2212 else
2213 vim_free(evalarg->eval_tofree);
2214 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002215 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002216
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002217 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2218 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002219 }
2220}
2221
2222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2226 */
2227
2228/*
2229 * Handle zero level expression.
2230 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002232 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002233 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 * Return OK or FAIL.
2235 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237eval0(
2238 char_u *arg,
2239 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002240 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002241 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242{
2243 int ret;
2244 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002245 int did_emsg_before = did_emsg;
2246 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002247 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002250 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002251 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002252
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002253 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
2255 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 /*
2258 * Report the invalid expression unless the expression evaluation has
2259 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002260 * exception, or we already gave a more specific error.
2261 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002263 if (!aborting()
2264 && did_emsg == did_emsg_before
2265 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002266 && (flags & EVAL_CONSTANT) == 0
2267 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002268 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002269
2270 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002271 // a next command to avoid more errors, unless "|" is following, which
2272 // could only be a command separator.
2273 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2274 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002277
2278 if (eap != NULL)
2279 eap->nextcmd = check_nextcmd(p);
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 return ret;
2282}
2283
2284/*
2285 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002286 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002287 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 *
2289 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002290 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002292 * Note: "rettv.v_lock" is not set.
2293 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 * Return OK or FAIL.
2295 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002296 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002297eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002299 char_u *p;
2300 int getnext;
2301
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002302 CLEAR_POINTER(rettv);
2303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 /*
2305 * Get the first variable.
2306 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 return FAIL;
2309
Bram Moolenaar793648f2020-06-26 21:28:25 +02002310 p = eval_next_non_blank(*arg, evalarg, &getnext);
2311 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002313 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002314 int result;
2315 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002316 evalarg_T *evalarg_used = evalarg;
2317 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002319 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002320 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002321
2322 if (evalarg == NULL)
2323 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002324 CLEAR_FIELD(local_evalarg);
2325 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002326 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002327 orig_flags = evalarg_used->eval_flags;
2328 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002329
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002330 if (getnext)
2331 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002332 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002333 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002334 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002335 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002336 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002337 clear_tv(rettv);
2338 return FAIL;
2339 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002340 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002341 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002344 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 int error = FALSE;
2347
Bram Moolenaar13106602020-10-04 16:06:05 +02002348 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002349 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002350 else if (vim9script)
2351 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002352 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 if (error || !op_falsy || !result)
2355 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 if (error)
2357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359
2360 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002361 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002363 if (op_falsy)
2364 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002365 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002366 {
mityu4ac198c2021-05-28 17:52:40 +02002367 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002368 clear_tv(rettv);
2369 return FAIL;
2370 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002371 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002372 evalarg_used->eval_flags = (op_falsy ? !result : result)
2373 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2374 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002375 {
2376 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002378 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002379 if (!op_falsy || !result)
2380 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002382 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002384 /*
2385 * Check for the ":".
2386 */
2387 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2388 if (*p != ':')
2389 {
2390 emsg(_(e_missing_colon));
2391 if (evaluate && result)
2392 clear_tv(rettv);
2393 evalarg_used->eval_flags = orig_flags;
2394 return FAIL;
2395 }
2396 if (getnext)
2397 *arg = eval_next_line(evalarg_used);
2398 else
2399 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002400 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002401 {
2402 error_white_both(p, 1);
2403 clear_tv(rettv);
2404 evalarg_used->eval_flags = orig_flags;
2405 return FAIL;
2406 }
2407 *arg = p;
2408 }
2409
2410 /*
2411 * Get the third variable. Recursive!
2412 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002413 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002414 {
mityu4ac198c2021-05-28 17:52:40 +02002415 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002416 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002417 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002418 return FAIL;
2419 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002420 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2421 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002422 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002423 if (eval1(arg, &var2, evalarg_used) == FAIL)
2424 {
2425 if (evaluate && result)
2426 clear_tv(rettv);
2427 evalarg_used->eval_flags = orig_flags;
2428 return FAIL;
2429 }
2430 if (evaluate && !result)
2431 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433
2434 if (evalarg == NULL)
2435 clear_evalarg(&local_evalarg, NULL);
2436 else
2437 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439
2440 return OK;
2441}
2442
2443/*
2444 * Handle first level expression:
2445 * expr2 || expr2 || expr2 logical OR
2446 *
2447 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002448 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 *
2450 * Return OK or FAIL.
2451 */
2452 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002455 char_u *p;
2456 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458 /*
2459 * Get the first variable.
2460 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002461 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 return FAIL;
2463
2464 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002465 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002467 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002470 evalarg_T *evalarg_used = evalarg;
2471 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002472 int evaluate;
2473 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002474 long result = FALSE;
2475 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002476 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002477 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002478
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002479 if (evalarg == NULL)
2480 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 CLEAR_FIELD(local_evalarg);
2482 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002483 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002484 orig_flags = evalarg_used->eval_flags;
2485 evaluate = orig_flags & EVAL_EVALUATE;
2486 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002487 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002488 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002489 result = tv_get_bool_chk(rettv, &error);
2490 else if (tv_get_number_chk(rettv, &error) != 0)
2491 result = TRUE;
2492 clear_tv(rettv);
2493 if (error)
2494 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
2496
2497 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002498 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500 while (p[0] == '|' && p[1] == '|')
2501 {
2502 if (getnext)
2503 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002504 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002505 {
2506 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2507 {
2508 error_white_both(p, 2);
2509 clear_tv(rettv);
2510 return FAIL;
2511 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002512 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002513 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002514
2515 /*
2516 * Get the second variable.
2517 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002518 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2519 {
mityu4ac198c2021-05-28 17:52:40 +02002520 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002521 clear_tv(rettv);
2522 return FAIL;
2523 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002524 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2525 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002526 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002528 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529
2530 /*
2531 * Compute the result.
2532 */
2533 if (evaluate && !result)
2534 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002535 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002536 result = tv_get_bool_chk(&var2, &error);
2537 else if (tv_get_number_chk(&var2, &error) != 0)
2538 result = TRUE;
2539 clear_tv(&var2);
2540 if (error)
2541 return FAIL;
2542 }
2543 if (evaluate)
2544 {
2545 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002546 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002547 rettv->v_type = VAR_BOOL;
2548 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002549 }
2550 else
2551 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002552 rettv->v_type = VAR_NUMBER;
2553 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002554 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002555 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002556
2557 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002559
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560 if (evalarg == NULL)
2561 clear_evalarg(&local_evalarg, NULL);
2562 else
2563 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
2565
2566 return OK;
2567}
2568
2569/*
2570 * Handle second level expression:
2571 * expr3 && expr3 && expr3 logical AND
2572 *
2573 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002574 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 *
2576 * Return OK or FAIL.
2577 */
2578 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002581 char_u *p;
2582 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584 /*
2585 * Get the first variable.
2586 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 return FAIL;
2589
2590 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002593 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 evalarg_T *evalarg_used = evalarg;
2597 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002598 int orig_flags;
2599 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 long result = TRUE;
2601 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002602 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002603 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002604
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002605 if (evalarg == NULL)
2606 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002607 CLEAR_FIELD(local_evalarg);
2608 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002609 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610 orig_flags = evalarg_used->eval_flags;
2611 evaluate = orig_flags & EVAL_EVALUATE;
2612 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002613 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002614 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002615 result = tv_get_bool_chk(rettv, &error);
2616 else if (tv_get_number_chk(rettv, &error) == 0)
2617 result = FALSE;
2618 clear_tv(rettv);
2619 if (error)
2620 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
2622
2623 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626 while (p[0] == '&' && p[1] == '&')
2627 {
2628 if (getnext)
2629 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002630 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002631 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002632 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002633 {
2634 error_white_both(p, 2);
2635 clear_tv(rettv);
2636 return FAIL;
2637 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002638 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002639 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002640
2641 /*
2642 * Get the second variable.
2643 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002644 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2645 {
mityu4ac198c2021-05-28 17:52:40 +02002646 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002647 clear_tv(rettv);
2648 return FAIL;
2649 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002650 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2651 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002652 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002653 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002655 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656
2657 /*
2658 * Compute the result.
2659 */
2660 if (evaluate && result)
2661 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002662 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002663 result = tv_get_bool_chk(&var2, &error);
2664 else if (tv_get_number_chk(&var2, &error) == 0)
2665 result = FALSE;
2666 clear_tv(&var2);
2667 if (error)
2668 return FAIL;
2669 }
2670 if (evaluate)
2671 {
2672 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002673 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002674 rettv->v_type = VAR_BOOL;
2675 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002676 }
2677 else
2678 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002679 rettv->v_type = VAR_NUMBER;
2680 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002681 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002682 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002683
2684 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002686
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002687 if (evalarg == NULL)
2688 clear_evalarg(&local_evalarg, NULL);
2689 else
2690 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
2693 return OK;
2694}
2695
2696/*
2697 * Handle third level expression:
2698 * var1 == var2
2699 * var1 =~ var2
2700 * var1 != var2
2701 * var1 !~ var2
2702 * var1 > var2
2703 * var1 >= var2
2704 * var1 < var2
2705 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002706 * var1 is var2
2707 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 *
2709 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002710 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 *
2712 * Return OK or FAIL.
2713 */
2714 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002718 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002719 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002721 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 /*
2724 * Get the first variable.
2725 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002726 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 return FAIL;
2728
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002729 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002730 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002733 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002735 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002737 typval_T var2;
2738 int ic;
2739 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002740 int evaluate = evalarg == NULL
2741 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002742
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002743 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002744 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002745 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002746 p = *arg;
2747 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002748 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2749 {
mityu4ac198c2021-05-28 17:52:40 +02002750 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002751 clear_tv(rettv);
2752 return FAIL;
2753 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002754
Bram Moolenaar696ba232020-07-29 21:20:41 +02002755 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2756 {
2757 semsg(_(e_invexpr2), p);
2758 clear_tv(rettv);
2759 return FAIL;
2760 }
2761
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002762 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (p[len] == '?')
2764 {
2765 ic = TRUE;
2766 ++len;
2767 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002768 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else if (p[len] == '#')
2770 {
2771 ic = FALSE;
2772 ++len;
2773 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002774 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002776 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
2778 /*
2779 * Get the second variable.
2780 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002781 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2782 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002783 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002784 clear_tv(rettv);
2785 return FAIL;
2786 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002787 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002788 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002790 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 return FAIL;
2792 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002793 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002794 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002795 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002796
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002797 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002798 {
2799 ret = FAIL;
2800 clear_tv(rettv);
2801 }
2802 else
2803 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002804 clear_tv(&var2);
2805 return ret;
2806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808
2809 return OK;
2810}
2811
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002812/*
2813 * Make a copy of blob "tv1" and append blob "tv2".
2814 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 void
2816eval_addblob(typval_T *tv1, typval_T *tv2)
2817{
2818 blob_T *b1 = tv1->vval.v_blob;
2819 blob_T *b2 = tv2->vval.v_blob;
2820 blob_T *b = blob_alloc();
2821 int i;
2822
2823 if (b != NULL)
2824 {
2825 for (i = 0; i < blob_len(b1); i++)
2826 ga_append(&b->bv_ga, blob_get(b1, i));
2827 for (i = 0; i < blob_len(b2); i++)
2828 ga_append(&b->bv_ga, blob_get(b2, i));
2829
2830 clear_tv(tv1);
2831 rettv_blob_set(tv1, b);
2832 }
2833}
2834
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002835/*
2836 * Make a copy of list "tv1" and append list "tv2".
2837 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 int
2839eval_addlist(typval_T *tv1, typval_T *tv2)
2840{
2841 typval_T var3;
2842
2843 // concatenate Lists
2844 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2845 {
2846 clear_tv(tv1);
2847 clear_tv(tv2);
2848 return FAIL;
2849 }
2850 clear_tv(tv1);
2851 *tv1 = var3;
2852 return OK;
2853}
2854
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855/*
2856 * Handle fourth level expression:
2857 * + number addition
2858 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002859 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002860 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 *
2862 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002863 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 *
2865 * Return OK or FAIL.
2866 */
2867 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002868eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 /*
2871 * Get the first variable.
2872 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002873 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 return FAIL;
2875
2876 /*
2877 * Repeat computing, until no '+', '-' or '.' is following.
2878 */
2879 for (;;)
2880 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002881 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002882 int getnext;
2883 char_u *p;
2884 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002885 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002886 int concat;
2887 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002888 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002889
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002890 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002891 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002892 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893 p = eval_next_non_blank(*arg, evalarg, &getnext);
2894 op = *p;
2895 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002896 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2897 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002899 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2900 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002901
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002902 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002903 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002904 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002905 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002906 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002907 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002908 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002909 {
mityu4ac198c2021-05-28 17:52:40 +02002910 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002911 clear_tv(rettv);
2912 return FAIL;
2913 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002914 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002915 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002916 if ((op != '+' || (rettv->v_type != VAR_LIST
2917 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002918#ifdef FEAT_FLOAT
2919 && (op == '.' || rettv->v_type != VAR_FLOAT)
2920#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002921 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002922 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002923 int error = FALSE;
2924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002925 // For "list + ...", an illegal use of the first operand as
2926 // a number cannot be determined before evaluating the 2nd
2927 // operand: if this is also a list, all is ok.
2928 // For "something . ...", "something - ..." or "non-list + ...",
2929 // we know that the first operand needs to be a string or number
2930 // without evaluating the 2nd operand. So check before to avoid
2931 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002932 if (op != '.')
2933 tv_get_number_chk(rettv, &error);
2934 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002935 {
2936 clear_tv(rettv);
2937 return FAIL;
2938 }
2939 }
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 /*
2942 * Get the second variable.
2943 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002944 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002945 {
mityu89dcb4d2021-05-28 13:50:17 +02002946 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002947 clear_tv(rettv);
2948 return FAIL;
2949 }
2950 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002951 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 return FAIL;
2955 }
2956
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002957 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 {
2959 /*
2960 * Compute the result.
2961 */
2962 if (op == '.')
2963 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002964 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2965 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002966 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002967
Bram Moolenaar2e086612020-08-25 22:37:48 +02002968 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002969 || var2.v_type == VAR_CHANNEL
2970 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002971 semsg(_(e_using_invalid_value_as_string_str),
2972 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002973#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002974 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002975 {
2976 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2977 var2.vval.v_float);
2978 s2 = buf2;
2979 }
2980#endif
2981 else
2982 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002983 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002984 {
2985 clear_tv(rettv);
2986 clear_tv(&var2);
2987 return FAIL;
2988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002990 clear_tv(rettv);
2991 rettv->v_type = VAR_STRING;
2992 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002994 else if (op == '+' && rettv->v_type == VAR_BLOB
2995 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002997 else if (op == '+' && rettv->v_type == VAR_LIST
2998 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002999 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003001 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 else
3004 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003005 int error = FALSE;
3006 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003007#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003008 float_T f1 = 0, f2 = 0;
3009
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003012 f1 = rettv->vval.v_float;
3013 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003014 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003015 else
3016#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003017 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003018 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 if (error)
3020 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003021 // This can only happen for "list + non-list" or
3022 // "blob + non-blob". For "non-list + ..." or
3023 // "something - ...", we returned before evaluating the
3024 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003025 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003026 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003027 return FAIL;
3028 }
3029#ifdef FEAT_FLOAT
3030 if (var2.v_type == VAR_FLOAT)
3031 f1 = n1;
3032#endif
3033 }
3034#ifdef FEAT_FLOAT
3035 if (var2.v_type == VAR_FLOAT)
3036 {
3037 f2 = var2.vval.v_float;
3038 n2 = 0;
3039 }
3040 else
3041#endif
3042 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003043 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003044 if (error)
3045 {
3046 clear_tv(rettv);
3047 clear_tv(&var2);
3048 return FAIL;
3049 }
3050#ifdef FEAT_FLOAT
3051 if (rettv->v_type == VAR_FLOAT)
3052 f2 = n2;
3053#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003055 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003056
3057#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003058 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003059 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3060 {
3061 if (op == '+')
3062 f1 = f1 + f2;
3063 else
3064 f1 = f1 - f2;
3065 rettv->v_type = VAR_FLOAT;
3066 rettv->vval.v_float = f1;
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003069#endif
3070 {
3071 if (op == '+')
3072 n1 = n1 + n2;
3073 else
3074 n1 = n1 - n2;
3075 rettv->v_type = VAR_NUMBER;
3076 rettv->vval.v_number = n1;
3077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 }
3081 }
3082 return OK;
3083}
3084
3085/*
3086 * Handle fifth level expression:
3087 * * number multiplication
3088 * / number division
3089 * % number modulo
3090 *
3091 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003092 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 *
3094 * Return OK or FAIL.
3095 */
3096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003097eval6(
3098 char_u **arg,
3099 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003100 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003101 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003103#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003104 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
3107 /*
3108 * Get the first variable.
3109 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003110 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 return FAIL;
3112
3113 /*
3114 * Repeat computing, until no '*', '/' or '%' is following.
3115 */
3116 for (;;)
3117 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003118 int evaluate;
3119 int getnext;
3120 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003121 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003122 int op;
3123 varnumber_T n1, n2;
3124#ifdef FEAT_FLOAT
3125 float_T f1, f2;
3126#endif
3127 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003128
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003129 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003130 p = eval_next_non_blank(*arg, evalarg, &getnext);
3131 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003132 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003134
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003135 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003136 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003137 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003138 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003139 {
3140 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3141 {
mityu4ac198c2021-05-28 17:52:40 +02003142 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003143 clear_tv(rettv);
3144 return FAIL;
3145 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003146 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003149#ifdef FEAT_FLOAT
3150 f1 = 0;
3151 f2 = 0;
3152#endif
3153 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003154 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003156#ifdef FEAT_FLOAT
3157 if (rettv->v_type == VAR_FLOAT)
3158 {
3159 f1 = rettv->vval.v_float;
3160 use_float = TRUE;
3161 n1 = 0;
3162 }
3163 else
3164#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003165 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003166 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003167 if (error)
3168 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 else
3171 n1 = 0;
3172
3173 /*
3174 * Get the second variable.
3175 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003176 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3177 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003178 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003179 clear_tv(rettv);
3180 return FAIL;
3181 }
3182 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003183 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 return FAIL;
3185
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003186 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188#ifdef FEAT_FLOAT
3189 if (var2.v_type == VAR_FLOAT)
3190 {
3191 if (!use_float)
3192 {
3193 f1 = n1;
3194 use_float = TRUE;
3195 }
3196 f2 = var2.vval.v_float;
3197 n2 = 0;
3198 }
3199 else
3200#endif
3201 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003202 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203 clear_tv(&var2);
3204 if (error)
3205 return FAIL;
3206#ifdef FEAT_FLOAT
3207 if (use_float)
3208 f2 = n2;
3209#endif
3210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
3212 /*
3213 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003214 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003216#ifdef FEAT_FLOAT
3217 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003219 if (op == '*')
3220 f1 = f1 * f2;
3221 else if (op == '/')
3222 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003223# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003224 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003225 if (f2 == 0.0)
3226 {
3227 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003228 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003229 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003230 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003231 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003232 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003233 }
3234 else
3235 f1 = f1 / f2;
3236# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003237 // We rely on the floating point library to handle divide
3238 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003239 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003240# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003243 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003245 return FAIL;
3246 }
3247 rettv->v_type = VAR_FLOAT;
3248 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
3250 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003253 int failed = FALSE;
3254
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003255 if (op == '*')
3256 n1 = n1 * n2;
3257 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003258 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003260 n1 = num_modulus(n1, n2, &failed);
3261 if (failed)
3262 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003263
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003264 rettv->v_type = VAR_NUMBER;
3265 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268 }
3269
3270 return OK;
3271}
3272
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003273/*
3274 * Handle a type cast before a base level expression.
3275 * "arg" must point to the first non-white of the expression.
3276 * "arg" is advanced to just after the recognized expression.
3277 * Return OK or FAIL.
3278 */
3279 static int
3280eval7t(
3281 char_u **arg,
3282 typval_T *rettv,
3283 evalarg_T *evalarg,
3284 int want_string) // after "." operator
3285{
3286 type_T *want_type = NULL;
3287 garray_T type_list; // list of pointers to allocated types
3288 int res;
3289 int evaluate = evalarg == NULL ? 0
3290 : (evalarg->eval_flags & EVAL_EVALUATE);
3291
3292 // Recognize <type> in Vim9 script only.
3293 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3294 {
3295 ++*arg;
3296 ga_init2(&type_list, sizeof(type_T *), 10);
3297 want_type = parse_type(arg, &type_list, TRUE);
3298 if (want_type == NULL && (evaluate || **arg != '>'))
3299 {
3300 clear_type_list(&type_list);
3301 return FAIL;
3302 }
3303
3304 if (**arg != '>')
3305 {
3306 if (*skipwhite(*arg) == '>')
3307 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3308 else
3309 emsg(_(e_missing_gt));
3310 clear_type_list(&type_list);
3311 return FAIL;
3312 }
3313 ++*arg;
3314 *arg = skipwhite_and_linebreak(*arg, evalarg);
3315 }
3316
3317 res = eval7(arg, rettv, evalarg, want_string);
3318
3319 if (want_type != NULL && evaluate)
3320 {
3321 if (res == OK)
3322 {
3323 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3324
3325 if (!equal_type(want_type, actual))
3326 {
3327 if (want_type == &t_bool && actual != &t_bool
3328 && (actual->tt_flags & TTFLAG_BOOL_OK))
3329 {
3330 int n = tv2bool(rettv);
3331
3332 // can use "0" and "1" for boolean in some places
3333 clear_tv(rettv);
3334 rettv->v_type = VAR_BOOL;
3335 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3336 }
3337 else
3338 {
3339 where_T where;
3340
3341 where.wt_index = 0;
3342 where.wt_variable = TRUE;
3343 res = check_type(want_type, actual, TRUE, where);
3344 }
3345 }
3346 }
3347 clear_type_list(&type_list);
3348 }
3349
3350 return res;
3351}
3352
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003353 int
3354eval_leader(char_u **arg, int vim9)
3355{
3356 char_u *s = *arg;
3357 char_u *p = *arg;
3358
3359 while (*p == '!' || *p == '-' || *p == '+')
3360 {
3361 char_u *n = skipwhite(p + 1);
3362
3363 // ++, --, -+ and +- are not accepted in Vim9 script
3364 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3365 {
3366 semsg(_(e_invexpr2), s);
3367 return FAIL;
3368 }
3369 p = n;
3370 }
3371 *arg = p;
3372 return OK;
3373}
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375/*
3376 * Handle sixth level expression:
3377 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003378 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003379 * "string" string constant
3380 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 * &option-name option value
3382 * @r register contents
3383 * identifier variable value
3384 * function() function call
3385 * $VAR environment variable
3386 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003387 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003389 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003390 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 *
3392 * Also handle:
3393 * ! in front logical NOT
3394 * - in front unary minus
3395 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003396 * trailing [] subscript in String or List
3397 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003398 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 *
3400 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003401 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 *
3403 * Return OK or FAIL.
3404 */
3405 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003406eval7(
3407 char_u **arg,
3408 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003409 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003412 int evaluate = evalarg != NULL
3413 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 int len;
3415 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 char_u *start_leader, *end_leader;
3417 int ret = OK;
3418 char_u *alias;
3419
3420 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003421 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003422 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003424 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003427 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 */
3429 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003430 if (eval_leader(arg, in_vim9script()) == FAIL)
3431 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 end_leader = *arg;
3433
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003434 if (**arg == '.' && (!isdigit(*(*arg + 1))
3435#ifdef FEAT_FLOAT
3436 || current_sctx.sc_version < 2
3437#endif
3438 ))
3439 {
3440 semsg(_(e_invexpr2), *arg);
3441 ++*arg;
3442 return FAIL;
3443 }
3444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 switch (**arg)
3446 {
3447 /*
3448 * Number constant.
3449 */
3450 case '0':
3451 case '1':
3452 case '2':
3453 case '3':
3454 case '4':
3455 case '5':
3456 case '6':
3457 case '7':
3458 case '8':
3459 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003460 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003461
3462 // Apply prefixed "-" and "+" now. Matters especially when
3463 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003464 if (ret == OK && evaluate && end_leader > start_leader
3465 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003466 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 break;
3468
3469 /*
3470 * String constant: "string".
3471 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003472 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 break;
3474
3475 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003476 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003478 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003479 break;
3480
3481 /*
3482 * List: [expr, expr]
3483 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003484 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 break;
3486
3487 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003488 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003489 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003490 case '#': if (in_vim9script())
3491 {
3492 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3493 }
3494 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003495 {
3496 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003497 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003498 }
3499 else
3500 ret = NOTDONE;
3501 break;
3502
3503 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003504 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003505 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003506 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003507 case '{': if (in_vim9script())
3508 ret = NOTDONE;
3509 else
3510 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003511 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003512 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003513 break;
3514
3515 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003516 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003518 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 break;
3520
3521 /*
3522 * Environment variable: $VAR.
3523 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003524 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 break;
3526
3527 /*
3528 * Register contents: @r.
3529 */
3530 case '@': ++*arg;
3531 if (evaluate)
3532 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003533 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3534 semsg(_(e_syntax_error_at_str), *arg);
3535 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3536 emsg_invreg(**arg);
3537 else
3538 {
3539 rettv->v_type = VAR_STRING;
3540 rettv->vval.v_string = get_reg_contents(**arg,
3541 GREG_EXPR_SRC);
3542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
3544 if (**arg != NUL)
3545 ++*arg;
3546 break;
3547
3548 /*
3549 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003550 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003552 case '(': ret = NOTDONE;
3553 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003554 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003555 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003556 if (ret == OK && evaluate)
3557 {
3558 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3559
3560 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003561 if (compile_def_function(ufunc,
3562 TRUE, PROFILING(ufunc), NULL) == FAIL)
3563 {
3564 clear_tv(rettv);
3565 ret = FAIL;
3566 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003567 }
3568 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003569 if (ret == NOTDONE)
3570 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003571 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003572 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003573
Bram Moolenaar9215f012020-06-27 21:18:00 +02003574 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003575 if (**arg == ')')
3576 ++*arg;
3577 else if (ret == OK)
3578 {
3579 emsg(_(e_missing_close));
3580 clear_tv(rettv);
3581 ret = FAIL;
3582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584 break;
3585
Bram Moolenaar8c711452005-01-14 21:53:12 +00003586 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 break;
3588 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589
3590 if (ret == NOTDONE)
3591 {
3592 /*
3593 * Must be a variable or function name.
3594 * Can also be a curly-braces kind of name: {expr}.
3595 */
3596 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003597 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003598 if (alias != NULL)
3599 s = alias;
3600
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003601 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 ret = FAIL;
3603 else
3604 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003605 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3606
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003607 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003608 {
3609 emsg(_(e_cannot_use_underscore_here));
3610 ret = FAIL;
3611 }
3612 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003613 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003614 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003615 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003616 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003617 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003618 else if (flags & EVAL_CONSTANT)
3619 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003621 {
3622 // get the value of "true", "false" or a variable
3623 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3624 {
3625 rettv->v_type = VAR_BOOL;
3626 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003627 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003628 }
3629 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003630 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003631 {
3632 rettv->v_type = VAR_BOOL;
3633 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003634 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003635 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003636 else if (len == 4 && in_vim9script()
3637 && STRNCMP(s, "null", 4) == 0)
3638 {
3639 rettv->v_type = VAR_SPECIAL;
3640 rettv->vval.v_number = VVAL_NULL;
3641 ret = OK;
3642 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003643 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003644 ret = eval_variable(s, len, rettv, NULL,
3645 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003646 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003647 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003648 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003649 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003650 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003651 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003654 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 }
3656
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003657 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3658 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003659 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003660 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662 /*
3663 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3664 */
3665 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003666 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003667 return ret;
3668}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003669
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003670/*
3671 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003672 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003673 * Adjusts "end_leaderp" until it is at "start_leader".
3674 */
3675 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003676eval7_leader(
3677 typval_T *rettv,
3678 int numeric_only,
3679 char_u *start_leader,
3680 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003681{
3682 char_u *end_leader = *end_leaderp;
3683 int ret = OK;
3684 int error = FALSE;
3685 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003686 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003687#ifdef FEAT_FLOAT
3688 float_T f = 0.0;
3689
3690 if (rettv->v_type == VAR_FLOAT)
3691 f = rettv->vval.v_float;
3692 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003693#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003694 {
3695 while (VIM_ISWHITE(end_leader[-1]))
3696 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003697 if (in_vim9script() && end_leader[-1] == '!')
3698 val = tv2bool(rettv);
3699 else
3700 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003701 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003702 if (error)
3703 {
3704 clear_tv(rettv);
3705 ret = FAIL;
3706 }
3707 else
3708 {
3709 while (end_leader > start_leader)
3710 {
3711 --end_leader;
3712 if (*end_leader == '!')
3713 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003714 if (numeric_only)
3715 {
3716 ++end_leader;
3717 break;
3718 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003719#ifdef FEAT_FLOAT
3720 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003721 {
3722 if (in_vim9script())
3723 {
3724 rettv->v_type = VAR_BOOL;
3725 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3726 }
3727 else
3728 f = !f;
3729 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003730 else
3731#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003732 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003733 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003734 type = VAR_BOOL;
3735 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003736 }
3737 else if (*end_leader == '-')
3738 {
3739#ifdef FEAT_FLOAT
3740 if (rettv->v_type == VAR_FLOAT)
3741 f = -f;
3742 else
3743#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003744 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003745 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003746 type = VAR_NUMBER;
3747 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003748 }
3749 }
3750#ifdef FEAT_FLOAT
3751 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003754 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003757#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003759 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003760 if (in_vim9script())
3761 rettv->v_type = type;
3762 else
3763 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003764 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003767 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return ret;
3769}
3770
3771/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003772 * Call the function referred to in "rettv".
3773 */
3774 static int
3775call_func_rettv(
3776 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003777 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003778 typval_T *rettv,
3779 int evaluate,
3780 dict_T *selfdict,
3781 typval_T *basetv)
3782{
3783 partial_T *pt = NULL;
3784 funcexe_T funcexe;
3785 typval_T functv;
3786 char_u *s;
3787 int ret;
3788
3789 // need to copy the funcref so that we can clear rettv
3790 if (evaluate)
3791 {
3792 functv = *rettv;
3793 rettv->v_type = VAR_UNKNOWN;
3794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003795 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003796 if (functv.v_type == VAR_PARTIAL)
3797 {
3798 pt = functv.vval.v_partial;
3799 s = partial_name(pt);
3800 }
3801 else
3802 s = functv.vval.v_string;
3803 }
3804 else
3805 s = (char_u *)"";
3806
Bram Moolenaara80faa82020-04-12 19:37:17 +02003807 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003808 funcexe.firstline = curwin->w_cursor.lnum;
3809 funcexe.lastline = curwin->w_cursor.lnum;
3810 funcexe.evaluate = evaluate;
3811 funcexe.partial = pt;
3812 funcexe.selfdict = selfdict;
3813 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003814 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003816 // Clear the funcref afterwards, so that deleting it while
3817 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003818 if (evaluate)
3819 clear_tv(&functv);
3820
3821 return ret;
3822}
3823
3824/*
3825 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003826 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003827 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3828 */
3829 static int
3830eval_lambda(
3831 char_u **arg,
3832 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003833 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003834 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003835{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003836 int evaluate = evalarg != NULL
3837 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003838 typval_T base = *rettv;
3839 int ret;
3840
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003841 rettv->v_type = VAR_UNKNOWN;
3842
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003843 if (**arg == '{')
3844 {
3845 // ->{lambda}()
3846 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3847 }
3848 else
3849 {
3850 // ->(lambda)()
3851 ++*arg;
3852 ret = eval1(arg, rettv, evalarg);
3853 *arg = skipwhite_and_linebreak(*arg, evalarg);
3854 if (**arg != ')')
3855 {
3856 emsg(_(e_missing_close));
3857 ret = FAIL;
3858 }
3859 ++*arg;
3860 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003861 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003862 return FAIL;
3863 else if (**arg != '(')
3864 {
3865 if (verbose)
3866 {
3867 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003868 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003869 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003871 }
3872 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003873 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003874 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003875 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003876 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003877
3878 // Clear the funcref afterwards, so that deleting it while
3879 // evaluating the arguments is possible (see test55).
3880 if (evaluate)
3881 clear_tv(&base);
3882
3883 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003884}
3885
3886/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003887 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003888 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003889 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3890 */
3891 static int
3892eval_method(
3893 char_u **arg,
3894 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003895 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003896 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003897{
3898 char_u *name;
3899 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003900 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003901 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003902 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003903 int evaluate = evalarg != NULL
3904 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003905
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003906 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003907
Bram Moolenaarac92e252019-08-03 21:58:38 +02003908 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003909 len = get_name_len(arg, &alias, evaluate, TRUE);
3910 if (alias != NULL)
3911 name = alias;
3912
3913 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003914 {
3915 if (verbose)
3916 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003917 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003918 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003919 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003920 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003921 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003922 if (**arg != '(')
3923 {
3924 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003926 ret = FAIL;
3927 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003928 else if (VIM_ISWHITE((*arg)[-1]))
3929 {
3930 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003931 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003932 ret = FAIL;
3933 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003934 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003935 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003936 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003937 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003938
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003939 // Clear the funcref afterwards, so that deleting it while
3940 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003941 if (evaluate)
3942 clear_tv(&base);
3943
Bram Moolenaarac92e252019-08-03 21:58:38 +02003944 return ret;
3945}
3946
3947/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003948 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3949 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003950 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3951 */
3952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003953eval_index(
3954 char_u **arg,
3955 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003956 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003957 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003958{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003959 int evaluate = evalarg != NULL
3960 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003963 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003964 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003965 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003966 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003967
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003968 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3969 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003970
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003971 init_tv(&var1);
3972 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003973 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003975 /*
3976 * dict.name
3977 */
3978 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003979 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003980 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003981 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003982 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003983 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003984 }
3985 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003986 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003987 /*
3988 * something[idx]
3989 *
3990 * Get the (first) variable from inside the [].
3991 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003992 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003993 if (**arg == ':')
3994 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003995 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003996 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003997 else if (vim9 && **arg == ':')
3998 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003999 semsg(_(e_white_space_required_before_and_after_str_at_str),
4000 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004001 clear_tv(&var1);
4002 return FAIL;
4003 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004004 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004005 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004006#ifdef FEAT_FLOAT
4007 // allow for indexing with float
4008 if (vim9 && rettv->v_type == VAR_DICT
4009 && var1.v_type == VAR_FLOAT)
4010 {
4011 var1.vval.v_string = typval_tostring(&var1, TRUE);
4012 var1.v_type = VAR_STRING;
4013 }
4014#endif
4015 if (tv_get_string_chk(&var1) == NULL)
4016 {
4017 // not a number or string
4018 clear_tv(&var1);
4019 return FAIL;
4020 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004022
4023 /*
4024 * Get the second variable from inside the [:].
4025 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004026 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004027 if (**arg == ':')
4028 {
4029 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004030 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004031 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004032 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004033 semsg(_(e_white_space_required_before_and_after_str_at_str),
4034 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004035 if (!empty1)
4036 clear_tv(&var1);
4037 return FAIL;
4038 }
4039 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004040 if (**arg == ']')
4041 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004042 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004044 if (!empty1)
4045 clear_tv(&var1);
4046 return FAIL;
4047 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004048 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004049 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004050 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004051 if (!empty1)
4052 clear_tv(&var1);
4053 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004054 return FAIL;
4055 }
4056 }
4057
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004058 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004059 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004060 if (**arg != ']')
4061 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004062 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004063 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004064 clear_tv(&var1);
4065 if (range)
4066 clear_tv(&var2);
4067 return FAIL;
4068 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004069 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004070 }
4071
4072 if (evaluate)
4073 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004074 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004075 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004076 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004077
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004078 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004079 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004080 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004081 clear_tv(&var2);
4082 return res;
4083 }
4084 return OK;
4085}
4086
4087/*
4088 * Check if "rettv" can have an [index] or [sli:ce]
4089 */
4090 int
4091check_can_index(typval_T *rettv, int evaluate, int verbose)
4092{
4093 switch (rettv->v_type)
4094 {
4095 case VAR_FUNC:
4096 case VAR_PARTIAL:
4097 if (verbose)
4098 emsg(_("E695: Cannot index a Funcref"));
4099 return FAIL;
4100 case VAR_FLOAT:
4101#ifdef FEAT_FLOAT
4102 if (verbose)
4103 emsg(_(e_float_as_string));
4104 return FAIL;
4105#endif
4106 case VAR_BOOL:
4107 case VAR_SPECIAL:
4108 case VAR_JOB:
4109 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004110 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004111 if (verbose)
4112 emsg(_(e_cannot_index_special_variable));
4113 return FAIL;
4114 case VAR_UNKNOWN:
4115 case VAR_ANY:
4116 case VAR_VOID:
4117 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004118 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004119 emsg(_(e_cannot_index_special_variable));
4120 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004121 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004122 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004123
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004124 case VAR_STRING:
4125 case VAR_LIST:
4126 case VAR_DICT:
4127 case VAR_BLOB:
4128 break;
4129 case VAR_NUMBER:
4130 if (in_vim9script())
4131 emsg(_(e_cannot_index_number));
4132 break;
4133 }
4134 return OK;
4135}
4136
4137/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004138 * slice() function
4139 */
4140 void
4141f_slice(typval_T *argvars, typval_T *rettv)
4142{
4143 if (check_can_index(argvars, TRUE, FALSE) == OK)
4144 {
4145 copy_tv(argvars, rettv);
4146 eval_index_inner(rettv, TRUE, argvars + 1,
4147 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4148 TRUE, NULL, 0, FALSE);
4149 }
4150}
4151
4152/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004153 * Apply index or range to "rettv".
4154 * "var1" is the first index, NULL for [:expr].
4155 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004156 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4157 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4159 */
4160 int
4161eval_index_inner(
4162 typval_T *rettv,
4163 int is_range,
4164 typval_T *var1,
4165 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004166 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167 char_u *key,
4168 int keylen,
4169 int verbose)
4170{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004171 varnumber_T n1, n2 = 0;
4172 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173
4174 n1 = 0;
4175 if (var1 != NULL && rettv->v_type != VAR_DICT)
4176 n1 = tv_get_number(var1);
4177
4178 if (is_range)
4179 {
4180 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004181 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004182 if (verbose)
4183 emsg(_(e_cannot_slice_dictionary));
4184 return FAIL;
4185 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004186 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004187 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004188 else
4189 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004190 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004191
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004192 switch (rettv->v_type)
4193 {
4194 case VAR_UNKNOWN:
4195 case VAR_ANY:
4196 case VAR_VOID:
4197 case VAR_FUNC:
4198 case VAR_PARTIAL:
4199 case VAR_FLOAT:
4200 case VAR_BOOL:
4201 case VAR_SPECIAL:
4202 case VAR_JOB:
4203 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004204 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004205 break; // not evaluating, skipping over subscript
4206
4207 case VAR_NUMBER:
4208 case VAR_STRING:
4209 {
4210 char_u *s = tv_get_string(rettv);
4211
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004213 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004214 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004215 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004216 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004217 else
4218 s = char_from_string(s, n1);
4219 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004220 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004221 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004222 // The resulting variable is a substring. If the indexes
4223 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004224 if (n1 < 0)
4225 {
4226 n1 = len + n1;
4227 if (n1 < 0)
4228 n1 = 0;
4229 }
4230 if (n2 < 0)
4231 n2 = len + n2;
4232 else if (n2 >= len)
4233 n2 = len;
4234 if (n1 >= len || n2 < 0 || n1 > n2)
4235 s = NULL;
4236 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004237 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004238 }
4239 else
4240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004241 // The resulting variable is a string of a single
4242 // character. If the index is too big or negative the
4243 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004244 if (n1 >= len || n1 < 0)
4245 s = NULL;
4246 else
4247 s = vim_strnsave(s + n1, 1);
4248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
4250 rettv->v_type = VAR_STRING;
4251 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004252 }
4253 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004254
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004255 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004256 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4257 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 break;
4259
4260 case VAR_LIST:
4261 if (var1 == NULL)
4262 n1 = 0;
4263 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004264 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004265 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004266 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004267 return FAIL;
4268 break;
4269
4270 case VAR_DICT:
4271 {
4272 dictitem_T *item;
4273 typval_T tmp;
4274
4275 if (key == NULL)
4276 {
4277 key = tv_get_string_chk(var1);
4278 if (key == NULL)
4279 return FAIL;
4280 }
4281
4282 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4283
4284 if (item == NULL && verbose)
4285 semsg(_(e_dictkey), key);
4286 if (item == NULL)
4287 return FAIL;
4288
4289 copy_tv(&item->di_tv, &tmp);
4290 clear_tv(rettv);
4291 *rettv = tmp;
4292 }
4293 break;
4294 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004295 return OK;
4296}
4297
4298/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004299 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004300 */
4301 char_u *
4302partial_name(partial_T *pt)
4303{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004304 if (pt != NULL)
4305 {
4306 if (pt->pt_name != NULL)
4307 return pt->pt_name;
4308 if (pt->pt_func != NULL)
4309 return pt->pt_func->uf_name;
4310 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004311 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004312}
4313
Bram Moolenaarddecc252016-04-06 22:59:37 +02004314 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004315partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004316{
4317 int i;
4318
4319 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004320 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004321 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004322 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004323 if (pt->pt_name != NULL)
4324 {
4325 func_unref(pt->pt_name);
4326 vim_free(pt->pt_name);
4327 }
4328 else
4329 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004330
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004331 // Decrease the reference count for the context of a closure. If down
4332 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004333 if (pt->pt_funcstack != NULL)
4334 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004335 --pt->pt_funcstack->fs_refcount;
4336 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004337 }
4338
Bram Moolenaarddecc252016-04-06 22:59:37 +02004339 vim_free(pt);
4340}
4341
4342/*
4343 * Unreference a closure: decrement the reference count and free it when it
4344 * becomes zero.
4345 */
4346 void
4347partial_unref(partial_T *pt)
4348{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004349 if (pt != NULL)
4350 {
4351 if (--pt->pt_refcount <= 0)
4352 partial_free(pt);
4353
4354 // If the reference count goes down to one, the funcstack may be the
4355 // only reference and can be freed if no other partials reference it.
4356 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4357 funcstack_check_refcount(pt->pt_funcstack);
4358 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004359}
4360
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004361/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004362 * Return the next (unique) copy ID.
4363 * Used for serializing nested structures.
4364 */
4365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004366get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004367{
4368 current_copyID += COPYID_INC;
4369 return current_copyID;
4370}
4371
4372/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004373 * Garbage collection for lists and dictionaries.
4374 *
4375 * We use reference counts to be able to free most items right away when they
4376 * are no longer used. But for composite items it's possible that it becomes
4377 * unused while the reference count is > 0: When there is a recursive
4378 * reference. Example:
4379 * :let l = [1, 2, 3]
4380 * :let d = {9: l}
4381 * :let l[1] = d
4382 *
4383 * Since this is quite unusual we handle this with garbage collection: every
4384 * once in a while find out which lists and dicts are not referenced from any
4385 * variable.
4386 *
4387 * Here is a good reference text about garbage collection (refers to Python
4388 * but it applies to all reference-counting mechanisms):
4389 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004390 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004391
4392/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004393 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004394 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004395 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004396 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004397 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004398garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004399{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004400 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004401 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004402 buf_T *buf;
4403 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004404 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004405 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004406
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004407 if (!testing)
4408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004409 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004410 want_garbage_collect = FALSE;
4411 may_garbage_collect = FALSE;
4412 garbage_collect_at_exit = FALSE;
4413 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004414
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004415 // The execution stack can grow big, limit the size.
4416 if (exestack.ga_maxlen - exestack.ga_len > 500)
4417 {
4418 size_t new_len;
4419 char_u *pp;
4420 int n;
4421
4422 // Keep 150% of the current size, with a minimum of the growth size.
4423 n = exestack.ga_len / 2;
4424 if (n < exestack.ga_growsize)
4425 n = exestack.ga_growsize;
4426
4427 // Don't make it bigger though.
4428 if (exestack.ga_len + n < exestack.ga_maxlen)
4429 {
4430 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4431 pp = vim_realloc(exestack.ga_data, new_len);
4432 if (pp == NULL)
4433 return FAIL;
4434 exestack.ga_maxlen = exestack.ga_len + n;
4435 exestack.ga_data = pp;
4436 }
4437 }
4438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004439 // We advance by two because we add one for items referenced through
4440 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004441 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004442
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004443 /*
4444 * 1. Go through all accessible variables and mark all lists and dicts
4445 * with copyID.
4446 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004448 // Don't free variables in the previous_funccal list unless they are only
4449 // referenced through previous_funccal. This must be first, because if
4450 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004451 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004453 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004454 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004456 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004457 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004458 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4459 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004461 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004462 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004463 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4464 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004465 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004466 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4467 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004468#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004469 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004470 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4471 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004472 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004473 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004474 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4475 NULL, NULL);
4476#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004479 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004480 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4481 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004483 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004486 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004489 abort = abort || set_ref_in_functions(copyID);
4490
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004491 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004492 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004494 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004495 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004496
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004497 // callbacks in buffers
4498 abort = abort || set_ref_in_buffers(copyID);
4499
Bram Moolenaar1dced572012-04-05 16:54:08 +02004500#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004501 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004502#endif
4503
Bram Moolenaardb913952012-06-29 12:54:53 +02004504#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004505 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004506#endif
4507
4508#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004509 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004510#endif
4511
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004512#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004513 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004514 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004515#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004516#ifdef FEAT_NETBEANS_INTG
4517 abort = abort || set_ref_in_nb_channel(copyID);
4518#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004519
Bram Moolenaare3188e22016-05-31 21:13:04 +02004520#ifdef FEAT_TIMERS
4521 abort = abort || set_ref_in_timer(copyID);
4522#endif
4523
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004524#ifdef FEAT_QUICKFIX
4525 abort = abort || set_ref_in_quickfix(copyID);
4526#endif
4527
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004528#ifdef FEAT_TERMINAL
4529 abort = abort || set_ref_in_term(copyID);
4530#endif
4531
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004532#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004533 abort = abort || set_ref_in_popups(copyID);
4534#endif
4535
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004536 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004537 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004538 /*
4539 * 2. Free lists and dictionaries that are not referenced.
4540 */
4541 did_free = free_unref_items(copyID);
4542
4543 /*
4544 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004545 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004546 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004547 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004548 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004549 else if (p_verbose > 0)
4550 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004551 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004552 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004553
4554 return did_free;
4555}
4556
4557/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004558 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004559 */
4560 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004561free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004562{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004563 int did_free = FALSE;
4564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004565 // Let all "free" functions know that we are here. This means no
4566 // dictionaries, lists, channels or jobs are to be freed, because we will
4567 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004568 in_free_unref_items = TRUE;
4569
4570 /*
4571 * PASS 1: free the contents of the items. We don't free the items
4572 * themselves yet, so that it is possible to decrement refcount counters
4573 */
4574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004575 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004576 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004578 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004579 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004580
4581#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004582 // Go through the list of jobs and free items without the copyID. This
4583 // must happen before doing channels, because jobs refer to channels, but
4584 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004585 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4586
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004587 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004588 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4589#endif
4590
4591 /*
4592 * PASS 2: free the items themselves.
4593 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004594 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004595 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004596
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004597#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004598 // Go through the list of jobs and free items without the copyID. This
4599 // must happen before doing channels, because jobs refer to channels, but
4600 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004601 free_unused_jobs(copyID, COPYID_MASK);
4602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004603 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004604 free_unused_channels(copyID, COPYID_MASK);
4605#endif
4606
4607 in_free_unref_items = FALSE;
4608
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004609 return did_free;
4610}
4611
4612/*
4613 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004614 * "list_stack" is used to add lists to be marked. Can be NULL.
4615 *
4616 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004619set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004620{
4621 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004622 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004623 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004624 hashtab_T *cur_ht;
4625 ht_stack_T *ht_stack = NULL;
4626 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004627
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004628 cur_ht = ht;
4629 for (;;)
4630 {
4631 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004632 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004633 // Mark each item in the hashtab. If the item contains a hashtab
4634 // it is added to ht_stack, if it contains a list it is added to
4635 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004636 todo = (int)cur_ht->ht_used;
4637 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4638 if (!HASHITEM_EMPTY(hi))
4639 {
4640 --todo;
4641 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4642 &ht_stack, list_stack);
4643 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004644 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004645
4646 if (ht_stack == NULL)
4647 break;
4648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004649 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004650 cur_ht = ht_stack->ht;
4651 tempitem = ht_stack;
4652 ht_stack = ht_stack->prev;
4653 free(tempitem);
4654 }
4655
4656 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004657}
4658
4659/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004660 * Mark a dict and its items with "copyID".
4661 * Returns TRUE if setting references failed somehow.
4662 */
4663 int
4664set_ref_in_dict(dict_T *d, int copyID)
4665{
4666 if (d != NULL && d->dv_copyID != copyID)
4667 {
4668 d->dv_copyID = copyID;
4669 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4670 }
4671 return FALSE;
4672}
4673
4674/*
4675 * Mark a list and its items with "copyID".
4676 * Returns TRUE if setting references failed somehow.
4677 */
4678 int
4679set_ref_in_list(list_T *ll, int copyID)
4680{
4681 if (ll != NULL && ll->lv_copyID != copyID)
4682 {
4683 ll->lv_copyID = copyID;
4684 return set_ref_in_list_items(ll, copyID, NULL);
4685 }
4686 return FALSE;
4687}
4688
4689/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004690 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004691 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4692 *
4693 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004694 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004695 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004696set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004697{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004698 listitem_T *li;
4699 int abort = FALSE;
4700 list_T *cur_l;
4701 list_stack_T *list_stack = NULL;
4702 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004703
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004704 cur_l = l;
4705 for (;;)
4706 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004707 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004708 // Mark each item in the list. If the item contains a hashtab
4709 // it is added to ht_stack, if it contains a list it is added to
4710 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004711 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4712 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4713 ht_stack, &list_stack);
4714 if (list_stack == NULL)
4715 break;
4716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004717 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004718 cur_l = list_stack->list;
4719 tempitem = list_stack;
4720 list_stack = list_stack->prev;
4721 free(tempitem);
4722 }
4723
4724 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004725}
4726
4727/*
4728 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004729 * "list_stack" is used to add lists to be marked. Can be NULL.
4730 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4731 *
4732 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004733 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004734 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004735set_ref_in_item(
4736 typval_T *tv,
4737 int copyID,
4738 ht_stack_T **ht_stack,
4739 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004740{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004741 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004742
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004743 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004744 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004745 dict_T *dd = tv->vval.v_dict;
4746
Bram Moolenaara03f2332016-02-06 18:09:59 +01004747 if (dd != NULL && dd->dv_copyID != copyID)
4748 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004749 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004750 dd->dv_copyID = copyID;
4751 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004752 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004753 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4754 }
4755 else
4756 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004757 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4758
Bram Moolenaara03f2332016-02-06 18:09:59 +01004759 if (newitem == NULL)
4760 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004761 else
4762 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004763 newitem->ht = &dd->dv_hashtab;
4764 newitem->prev = *ht_stack;
4765 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004766 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004767 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004768 }
4769 }
4770 else if (tv->v_type == VAR_LIST)
4771 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004772 list_T *ll = tv->vval.v_list;
4773
Bram Moolenaara03f2332016-02-06 18:09:59 +01004774 if (ll != NULL && ll->lv_copyID != copyID)
4775 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004776 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004777 ll->lv_copyID = copyID;
4778 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004779 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004780 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004781 }
4782 else
4783 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004784 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4785
Bram Moolenaara03f2332016-02-06 18:09:59 +01004786 if (newitem == NULL)
4787 abort = TRUE;
4788 else
4789 {
4790 newitem->list = ll;
4791 newitem->prev = *list_stack;
4792 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004793 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004794 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004795 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004796 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004797 else if (tv->v_type == VAR_FUNC)
4798 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004799 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004800 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004801 else if (tv->v_type == VAR_PARTIAL)
4802 {
4803 partial_T *pt = tv->vval.v_partial;
4804 int i;
4805
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004806 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004807 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004808 // Didn't see this partial yet.
4809 pt->pt_copyID = copyID;
4810
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004811 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004812
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004813 if (pt->pt_dict != NULL)
4814 {
4815 typval_T dtv;
4816
4817 dtv.v_type = VAR_DICT;
4818 dtv.vval.v_dict = pt->pt_dict;
4819 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4820 }
4821
4822 for (i = 0; i < pt->pt_argc; ++i)
4823 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4824 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004825 if (pt->pt_funcstack != NULL)
4826 {
4827 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4828
4829 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4830 abort = abort || set_ref_in_item(stack + i, copyID,
4831 ht_stack, list_stack);
4832 }
4833
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004834 }
4835 }
4836#ifdef FEAT_JOB_CHANNEL
4837 else if (tv->v_type == VAR_JOB)
4838 {
4839 job_T *job = tv->vval.v_job;
4840 typval_T dtv;
4841
4842 if (job != NULL && job->jv_copyID != copyID)
4843 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004844 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004845 if (job->jv_channel != NULL)
4846 {
4847 dtv.v_type = VAR_CHANNEL;
4848 dtv.vval.v_channel = job->jv_channel;
4849 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4850 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004851 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004852 {
4853 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004854 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004855 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4856 }
4857 }
4858 }
4859 else if (tv->v_type == VAR_CHANNEL)
4860 {
4861 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004862 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004863 typval_T dtv;
4864 jsonq_T *jq;
4865 cbq_T *cq;
4866
4867 if (ch != NULL && ch->ch_copyID != copyID)
4868 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004869 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004870 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004871 {
4872 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4873 jq = jq->jq_next)
4874 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4875 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4876 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004877 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004878 {
4879 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004880 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004881 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4882 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004883 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004884 {
4885 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004886 dtv.vval.v_partial =
4887 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004888 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4889 }
4890 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004891 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004892 {
4893 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004894 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004895 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4896 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004897 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004898 {
4899 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004900 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004901 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4902 }
4903 }
4904 }
4905#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004906 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004907}
4908
Bram Moolenaar8c711452005-01-14 21:53:12 +00004909/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004910 * Return a string with the string representation of a variable.
4911 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004912 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004913 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004914 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004915 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004916 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004917 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4918 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004919 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004920 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004921 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004922echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004923 typval_T *tv,
4924 char_u **tofree,
4925 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004926 int copyID,
4927 int echo_style,
4928 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004929 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004930{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004931 static int recurse = 0;
4932 char_u *r = NULL;
4933
Bram Moolenaar33570922005-01-25 22:26:29 +00004934 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004935 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004936 if (!did_echo_string_emsg)
4937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004938 // Only give this message once for a recursive call to avoid
4939 // flooding the user with errors. And stop iterating over lists
4940 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004941 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004942 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004943 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004944 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004945 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004946 }
4947 ++recurse;
4948
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004949 switch (tv->v_type)
4950 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004951 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004952 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004953 {
4954 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004955 r = tv->vval.v_string;
4956 if (r == NULL)
4957 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004958 }
4959 else
4960 {
4961 *tofree = string_quote(tv->vval.v_string, FALSE);
4962 r = *tofree;
4963 }
4964 break;
4965
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004967 if (echo_style)
4968 {
4969 *tofree = NULL;
4970 r = tv->vval.v_string;
4971 }
4972 else
4973 {
4974 *tofree = string_quote(tv->vval.v_string, TRUE);
4975 r = *tofree;
4976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004977 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004978
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004979 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004980 {
4981 partial_T *pt = tv->vval.v_partial;
4982 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004983 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004984 garray_T ga;
4985 int i;
4986 char_u *tf;
4987
4988 ga_init2(&ga, 1, 100);
4989 ga_concat(&ga, (char_u *)"function(");
4990 if (fname != NULL)
4991 {
4992 ga_concat(&ga, fname);
4993 vim_free(fname);
4994 }
4995 if (pt != NULL && pt->pt_argc > 0)
4996 {
4997 ga_concat(&ga, (char_u *)", [");
4998 for (i = 0; i < pt->pt_argc; ++i)
4999 {
5000 if (i > 0)
5001 ga_concat(&ga, (char_u *)", ");
5002 ga_concat(&ga,
5003 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5004 vim_free(tf);
5005 }
5006 ga_concat(&ga, (char_u *)"]");
5007 }
5008 if (pt != NULL && pt->pt_dict != NULL)
5009 {
5010 typval_T dtv;
5011
5012 ga_concat(&ga, (char_u *)", ");
5013 dtv.v_type = VAR_DICT;
5014 dtv.vval.v_dict = pt->pt_dict;
5015 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5016 vim_free(tf);
5017 }
5018 ga_concat(&ga, (char_u *)")");
5019
5020 *tofree = ga.ga_data;
5021 r = *tofree;
5022 break;
5023 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005024
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005025 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005026 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005027 break;
5028
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005029 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005030 if (tv->vval.v_list == NULL)
5031 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005032 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005033 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005034 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005035 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005036 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5037 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005038 {
5039 *tofree = NULL;
5040 r = (char_u *)"[...]";
5041 }
5042 else
5043 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005044 int old_copyID = tv->vval.v_list->lv_copyID;
5045
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005046 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005047 *tofree = list2string(tv, copyID, restore_copyID);
5048 if (restore_copyID)
5049 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005050 r = *tofree;
5051 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005052 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005053
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005055 if (tv->vval.v_dict == NULL)
5056 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005057 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005058 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005059 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005060 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005061 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5062 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005063 {
5064 *tofree = NULL;
5065 r = (char_u *)"{...}";
5066 }
5067 else
5068 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005069 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005070
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005071 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005072 *tofree = dict2string(tv, copyID, restore_copyID);
5073 if (restore_copyID)
5074 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005075 r = *tofree;
5076 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005077 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005078
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005080 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005081 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005083 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005084 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005085 break;
5086
Bram Moolenaar835dc632016-02-07 14:27:38 +01005087 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005088 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005089 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005090 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005091 if (composite_val)
5092 {
5093 *tofree = string_quote(r, FALSE);
5094 r = *tofree;
5095 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005096 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005097
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005098 case VAR_INSTR:
5099 *tofree = NULL;
5100 r = (char_u *)"instructions";
5101 break;
5102
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005103 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005104#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 *tofree = NULL;
5106 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5107 r = numbuf;
5108 break;
5109#endif
5110
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005111 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005112 case VAR_SPECIAL:
5113 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005114 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005115 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005116 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005117
Bram Moolenaar8502c702014-06-17 12:51:16 +02005118 if (--recurse == 0)
5119 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005120 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005121}
5122
5123/*
5124 * Return a string with the string representation of a variable.
5125 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5126 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005127 * Does not put quotes around strings, as ":echo" displays values.
5128 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5129 * May return NULL.
5130 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005131 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005132echo_string(
5133 typval_T *tv,
5134 char_u **tofree,
5135 char_u *numbuf,
5136 int copyID)
5137{
5138 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5139}
5140
5141/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005142 * Return string "str" in ' quotes, doubling ' characters.
5143 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005145 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005146 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005147string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005148{
Bram Moolenaar33570922005-01-25 22:26:29 +00005149 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005150 char_u *p, *r, *s;
5151
Bram Moolenaar33570922005-01-25 22:26:29 +00005152 len = (function ? 13 : 3);
5153 if (str != NULL)
5154 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005155 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005156 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005157 if (*p == '\'')
5158 ++len;
5159 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005160 s = r = alloc(len);
5161 if (r != NULL)
5162 {
5163 if (function)
5164 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005166 r += 10;
5167 }
5168 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005170 if (str != NULL)
5171 for (p = str; *p != NUL; )
5172 {
5173 if (*p == '\'')
5174 *r++ = '\'';
5175 MB_COPY_CHAR(p, r);
5176 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005178 if (function)
5179 *r++ = ')';
5180 *r++ = NUL;
5181 }
5182 return s;
5183}
5184
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005185#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005186/*
5187 * Convert the string "text" to a floating point number.
5188 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5189 * this always uses a decimal point.
5190 * Returns the length of the text that was consumed.
5191 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005192 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005193string2float(
5194 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005195 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196{
5197 char *s = (char *)text;
5198 float_T f;
5199
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005200 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005201 if (STRNICMP(text, "inf", 3) == 0)
5202 {
5203 *value = INFINITY;
5204 return 3;
5205 }
5206 if (STRNICMP(text, "-inf", 3) == 0)
5207 {
5208 *value = -INFINITY;
5209 return 4;
5210 }
5211 if (STRNICMP(text, "nan", 3) == 0)
5212 {
5213 *value = NAN;
5214 return 3;
5215 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005216 f = strtod(s, &s);
5217 *value = f;
5218 return (int)((char_u *)s - text);
5219}
5220#endif
5221
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005222/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005223 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5224 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005225 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005226 */
5227 int
5228buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5229{
5230 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005231 char_u *t;
5232 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005233
5234 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5235 return -1;
5236
5237 if (lnum > buf->b_ml.ml_line_count)
5238 lnum = buf->b_ml.ml_line_count;
5239
5240 str = ml_get_buf(buf, lnum, FALSE);
5241 if (str == NULL)
5242 return -1;
5243
5244 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005245 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005246
Bram Moolenaar91458462021-01-13 20:08:38 +01005247 // count the number of characters
5248 t = str;
5249 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5250 t += mb_ptr2len(t);
5251
5252 // In insert mode, when the cursor is at the end of a non-empty line,
5253 // byteidx points to the NUL character immediately past the end of the
5254 // string. In this case, add one to the character count.
5255 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5256 count++;
5257
5258 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005259}
5260
5261/*
5262 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005263 * byte index. Works only for loaded buffers. Returns -1 on failure.
5264 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005265 */
5266 int
5267buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5268{
5269 char_u *str;
5270 char_u *t;
5271
5272 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5273 return -1;
5274
5275 if (lnum > buf->b_ml.ml_line_count)
5276 lnum = buf->b_ml.ml_line_count;
5277
5278 str = ml_get_buf(buf, lnum, FALSE);
5279 if (str == NULL)
5280 return -1;
5281
5282 // Convert the character offset to a byte offset
5283 t = str;
5284 while (*t != NUL && --charidx > 0)
5285 t += mb_ptr2len(t);
5286
Bram Moolenaar91458462021-01-13 20:08:38 +01005287 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005288}
5289
5290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005292 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005295var2fpos(
5296 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005297 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005298 int *fnum, // set to fnum for '0, 'A, etc.
5299 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005301 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005303 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005305 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005306 if (varp->v_type == VAR_LIST)
5307 {
5308 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005309 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005310 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005311 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005312
5313 l = varp->vval.v_list;
5314 if (l == NULL)
5315 return NULL;
5316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005317 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005318 pos.lnum = list_find_nr(l, 0L, &error);
5319 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005321 if (charcol)
5322 len = (long)mb_charlen(ml_get(pos.lnum));
5323 else
5324 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005325
Bram Moolenaarec65d772020-08-20 22:29:12 +02005326 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005328 li = list_find(l, 1L);
5329 if (li != NULL && li->li_tv.v_type == VAR_STRING
5330 && li->li_tv.vval.v_string != NULL
5331 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005332 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005333 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005334 }
5335 else
5336 {
5337 pos.col = list_find_nr(l, 1L, &error);
5338 if (error)
5339 return NULL;
5340 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005341
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005342 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005343 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005345 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005347 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005348 pos.coladd = list_find_nr(l, 2L, &error);
5349 if (error)
5350 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005351
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005352 return &pos;
5353 }
5354
Bram Moolenaarc5809432021-03-27 21:23:30 +01005355 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5356 return NULL;
5357
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005358 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005359 if (name == NULL)
5360 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005361 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005362 {
5363 pos = curwin->w_cursor;
5364 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005365 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005366 return &pos;
5367 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005368 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005369 {
5370 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005371 pos = VIsual;
5372 else
5373 pos = curwin->w_cursor;
5374 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005375 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005376 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005377 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005380 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5382 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005383 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005384 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 return pp;
5386 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005387
Bram Moolenaara5525202006-03-02 22:52:09 +00005388 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005389
Bram Moolenaar477933c2007-07-17 14:32:23 +00005390 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005391 {
5392 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005393 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005394 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005395 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005396 // In silent Ex mode topline is zero, but that's not a valid line
5397 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005398 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005399 return &pos;
5400 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005401 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005402 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005403 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005405 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005406 return &pos;
5407 }
5408 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005409 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005411 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
5413 pos.lnum = curbuf->b_ml.ml_line_count;
5414 pos.col = 0;
5415 }
5416 else
5417 {
5418 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005419 if (charcol)
5420 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5421 else
5422 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424 return &pos;
5425 }
5426 return NULL;
5427}
5428
5429/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005430 * Convert list in "arg" into a position and optional file number.
5431 * When "fnump" is NULL there is no file number, only 3 items.
5432 * Note that the column is passed on as-is, the caller may want to decrement
5433 * it to use 1 for the first column.
5434 * Return FAIL when conversion is not possible, doesn't check the position for
5435 * validity.
5436 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005437 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005438list2fpos(
5439 typval_T *arg,
5440 pos_T *posp,
5441 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005442 colnr_T *curswantp,
5443 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005444{
5445 list_T *l = arg->vval.v_list;
5446 long i = 0;
5447 long n;
5448
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5450 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005451 if (arg->v_type != VAR_LIST
5452 || l == NULL
5453 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005454 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005455 return FAIL;
5456
5457 if (fnump != NULL)
5458 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005460 if (n < 0)
5461 return FAIL;
5462 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005464 *fnump = n;
5465 }
5466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005467 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005468 if (n < 0)
5469 return FAIL;
5470 posp->lnum = n;
5471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005472 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005473 if (n < 0)
5474 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005475 // If character position is specified, then convert to byte position
5476 if (charcol)
5477 {
5478 buf_T *buf;
5479
5480 // Get the text for the specified line in a loaded buffer
5481 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5482 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5483 return FAIL;
5484
Bram Moolenaar91458462021-01-13 20:08:38 +01005485 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005486 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005487 posp->col = n;
5488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005489 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005490 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005491 posp->coladd = 0;
5492 else
5493 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005494
Bram Moolenaar493c1782014-05-28 14:34:46 +02005495 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005496 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005497
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005498 return OK;
5499}
5500
5501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 * Get the length of an environment variable name.
5503 * Advance "arg" to the first character after the name.
5504 * Return 0 for error.
5505 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005507get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 char_u *p;
5510 int len;
5511
5512 for (p = *arg; vim_isIDc(*p); ++p)
5513 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005514 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 return 0;
5516
5517 len = (int)(p - *arg);
5518 *arg = p;
5519 return len;
5520}
5521
5522/*
5523 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005524 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 * Return 0 if something is wrong.
5526 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005527 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005528get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 char_u *p;
5531 int len;
5532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005533 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005535 {
5536 if (*p == ':')
5537 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005538 // "s:" is start of "s:var", but "n:" is not and can be used in
5539 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005540 len = (int)(p - *arg);
5541 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5542 || len > 1)
5543 break;
5544 }
5545 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005546 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 return 0;
5548
5549 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005550 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 return len;
5553}
5554
5555/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005556 * Get the length of the name of a variable or function.
5557 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005559 * Return -1 if curly braces expansion failed.
5560 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 * If the name contains 'magic' {}'s, expand them and return the
5562 * expanded name in an allocated string via 'alias' - caller must free.
5563 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005564 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005565get_name_len(
5566 char_u **arg,
5567 char_u **alias,
5568 int evaluate,
5569 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570{
5571 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 char_u *p;
5573 char_u *expr_start;
5574 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005576 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
5578 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5579 && (*arg)[2] == (int)KE_SNR)
5580 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005581 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 *arg += 3;
5583 return get_id_len(arg) + 3;
5584 }
5585 len = eval_fname_script(*arg);
5586 if (len > 0)
5587 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005588 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 *arg += len;
5590 }
5591
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005595 p = find_name_end(*arg, &expr_start, &expr_end,
5596 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (expr_start != NULL)
5598 {
5599 char_u *temp_string;
5600
5601 if (!evaluate)
5602 {
5603 len += (int)(p - *arg);
5604 *arg = skipwhite(p);
5605 return len;
5606 }
5607
5608 /*
5609 * Include any <SID> etc in the expanded string:
5610 * Thus the -len here.
5611 */
5612 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5613 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005614 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 *alias = temp_string;
5616 *arg = skipwhite(p);
5617 return (int)STRLEN(temp_string);
5618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005621 // Only give an error when there is something, otherwise it will be
5622 // reported at a higher level.
5623 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005624 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
5626 return len;
5627}
5628
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629/*
5630 * Find the end of a variable or function name, taking care of magic braces.
5631 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5632 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005633 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 * Return a pointer to just after the name. Equal to "arg" if there is no
5635 * valid name.
5636 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005637 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005638find_name_end(
5639 char_u *arg,
5640 char_u **expr_start,
5641 char_u **expr_end,
5642 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 int mb_nest = 0;
5645 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005647 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005648 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005650 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 *expr_start = NULL;
5653 *expr_end = NULL;
5654 }
5655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005656 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005657 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5658 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005659 return arg;
5660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 for (p = arg; *p != NUL
5662 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005663 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005664 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005665 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005667 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005669 if (*p == '\'')
5670 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005671 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005672 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005673 ;
5674 if (*p == NUL)
5675 break;
5676 }
5677 else if (*p == '"')
5678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005679 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005680 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005681 if (*p == '\\' && p[1] != NUL)
5682 ++p;
5683 if (*p == NUL)
5684 break;
5685 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005686 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5687 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005688 // "s:" is start of "s:var", but "n:" is not and can be used in
5689 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005690 len = (int)(p - arg);
5691 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005692 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005693 break;
5694 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005698 if (*p == '[')
5699 ++br_nest;
5700 else if (*p == ']')
5701 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005703
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005704 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005706 if (*p == '{')
5707 {
5708 mb_nest++;
5709 if (expr_start != NULL && *expr_start == NULL)
5710 *expr_start = p;
5711 }
5712 else if (*p == '}')
5713 {
5714 mb_nest--;
5715 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5716 *expr_end = p;
5717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720
5721 return p;
5722}
5723
5724/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005725 * Expands out the 'magic' {}'s in a variable/function name.
5726 * Note that this can call itself recursively, to deal with
5727 * constructs like foo{bar}{baz}{bam}
5728 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5729 * "in_start" ^
5730 * "expr_start" ^
5731 * "expr_end" ^
5732 * "in_end" ^
5733 *
5734 * Returns a new allocated string, which the caller must free.
5735 * Returns NULL for failure.
5736 */
5737 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005738make_expanded_name(
5739 char_u *in_start,
5740 char_u *expr_start,
5741 char_u *expr_end,
5742 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005743{
5744 char_u c1;
5745 char_u *retval = NULL;
5746 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005747
5748 if (expr_end == NULL || in_end == NULL)
5749 return NULL;
5750 *expr_start = NUL;
5751 *expr_end = NUL;
5752 c1 = *in_end;
5753 *in_end = NUL;
5754
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005755 temp_result = eval_to_string(expr_start + 1, FALSE);
5756 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005757 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005758 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5759 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005760 if (retval != NULL)
5761 {
5762 STRCPY(retval, in_start);
5763 STRCAT(retval, temp_result);
5764 STRCAT(retval, expr_end + 1);
5765 }
5766 }
5767 vim_free(temp_result);
5768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005769 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005770 *expr_start = '{';
5771 *expr_end = '}';
5772
5773 if (retval != NULL)
5774 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005775 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005776 if (expr_start != NULL)
5777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005778 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005779 temp_result = make_expanded_name(retval, expr_start,
5780 expr_end, temp_result);
5781 vim_free(retval);
5782 retval = temp_result;
5783 }
5784 }
5785
5786 return retval;
5787}
5788
5789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005791 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005794eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005796 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005797}
5798
5799/*
5800 * Return TRUE if character "c" can be used as the first character in a
5801 * variable or function name (excluding '{' and '}').
5802 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005804eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005805{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005806 return ASCII_ISALPHA(c) || c == '_';
5807}
5808
5809/*
5810 * Return TRUE if character "c" can be used as the first character of a
5811 * dictionary key.
5812 */
5813 int
5814eval_isdictc(int c)
5815{
5816 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817}
5818
5819/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005820 * Handle:
5821 * - expr[expr], expr[expr:expr] subscript
5822 * - ".name" lookup
5823 * - function call with Funcref variable: func(expr)
5824 * - method call: var->method()
5825 *
5826 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005827 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005829handle_subscript(
5830 char_u **arg,
5831 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005832 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005833 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005834{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005835 int evaluate = evalarg != NULL
5836 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005837 int ret = OK;
5838 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005839 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005840 int getnext;
5841 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005842
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005843 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005844 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005845 // When at the end of the line and ".name" or "->{" or "->X" follows in
5846 // the next line then consume the line break.
5847 p = eval_next_non_blank(*arg, evalarg, &getnext);
5848 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005849 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005850 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005851 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005852 {
5853 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005854 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005855 check_white = FALSE;
5856 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005857
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005858 if (rettv->v_type == VAR_ANY)
5859 {
5860 char_u *exp_name;
5861 int cc;
5862 int idx;
5863 ufunc_T *ufunc;
5864 type_T *type;
5865
5866 // Found script from "import * as {name}", script item name must
5867 // follow.
5868 if (**arg != '.')
5869 {
5870 if (verbose)
5871 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5872 ret = FAIL;
5873 break;
5874 }
5875 ++*arg;
5876 if (IS_WHITE_OR_NUL(**arg))
5877 {
5878 if (verbose)
5879 emsg(_(e_no_white_space_allowed_after_dot));
5880 ret = FAIL;
5881 break;
5882 }
5883
5884 // isolate the name
5885 exp_name = *arg;
5886 while (eval_isnamec(**arg))
5887 ++*arg;
5888 cc = **arg;
5889 **arg = NUL;
5890
5891 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5892 evalarg->eval_cctx, verbose);
5893 **arg = cc;
5894 *arg = skipwhite(*arg);
5895
5896 if (idx < 0 && ufunc == NULL)
5897 {
5898 ret = FAIL;
5899 break;
5900 }
5901 if (idx >= 0)
5902 {
5903 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5904 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5905
5906 copy_tv(sv->sv_tv, rettv);
5907 }
5908 else
5909 {
5910 rettv->v_type = VAR_FUNC;
5911 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5912 }
5913 }
5914
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005915 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5916 || rettv->v_type == VAR_PARTIAL))
5917 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005918 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005919 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5920 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005921
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005922 // Stop the expression evaluation when immediately aborting on
5923 // error, or when an interrupt occurred or an exception was thrown
5924 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005925 if (aborting())
5926 {
5927 if (ret == OK)
5928 clear_tv(rettv);
5929 ret = FAIL;
5930 }
5931 dict_unref(selfdict);
5932 selfdict = NULL;
5933 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005934 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005935 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005936 if (in_vim9script())
5937 *arg = skipwhite(p + 2);
5938 else
5939 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005940 if (ret == OK)
5941 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005942 if (VIM_ISWHITE(**arg))
5943 {
5944 emsg(_(e_nowhitespace));
5945 ret = FAIL;
5946 }
5947 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005948 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005949 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005950 else
5951 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005952 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005953 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005954 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005955 // "." is ".name" lookup when we found a dict or when evaluating and
5956 // scriptversion is at least 2, where string concatenation is "..".
5957 else if (**arg == '['
5958 || (**arg == '.' && (rettv->v_type == VAR_DICT
5959 || (!evaluate
5960 && (*arg)[1] != '.'
5961 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005962 {
5963 dict_unref(selfdict);
5964 if (rettv->v_type == VAR_DICT)
5965 {
5966 selfdict = rettv->vval.v_dict;
5967 if (selfdict != NULL)
5968 ++selfdict->dv_refcount;
5969 }
5970 else
5971 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005972 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005973 {
5974 clear_tv(rettv);
5975 ret = FAIL;
5976 }
5977 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005978 else
5979 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005980 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005982 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5983 // Don't do this when "Func" is already a partial that was bound
5984 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005985 if (selfdict != NULL
5986 && (rettv->v_type == VAR_FUNC
5987 || (rettv->v_type == VAR_PARTIAL
5988 && (rettv->vval.v_partial->pt_auto
5989 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005990 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005991
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005992 dict_unref(selfdict);
5993 return ret;
5994}
5995
5996/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997 * Make a copy of an item.
5998 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005999 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6000 * reference to an already copied list/dict can be used.
6001 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006002 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004item_copy(
6005 typval_T *from,
6006 typval_T *to,
6007 int deep,
6008 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006009{
6010 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006011 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006012
Bram Moolenaar33570922005-01-25 22:26:29 +00006013 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006015 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006016 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006017 }
6018 ++recurse;
6019
6020 switch (from->v_type)
6021 {
6022 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006023 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006024 case VAR_STRING:
6025 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006026 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006027 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006028 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006029 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006030 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006031 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006032 copy_tv(from, to);
6033 break;
6034 case VAR_LIST:
6035 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006036 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006037 if (from->vval.v_list == NULL)
6038 to->vval.v_list = NULL;
6039 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006041 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006042 to->vval.v_list = from->vval.v_list->lv_copylist;
6043 ++to->vval.v_list->lv_refcount;
6044 }
6045 else
6046 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6047 if (to->vval.v_list == NULL)
6048 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006049 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006050 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006052 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053 case VAR_DICT:
6054 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006055 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006056 if (from->vval.v_dict == NULL)
6057 to->vval.v_dict = NULL;
6058 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6059 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006060 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006061 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6062 ++to->vval.v_dict->dv_refcount;
6063 }
6064 else
6065 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6066 if (to->vval.v_dict == NULL)
6067 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006068 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006069 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006070 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006072 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006073 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006074 }
6075 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006076 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006077}
6078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006079 void
6080echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6081{
6082 char_u *tofree;
6083 char_u numbuf[NUMBUFLEN];
6084 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6085
6086 if (*atstart)
6087 {
6088 *atstart = FALSE;
6089 // Call msg_start() after eval1(), evaluating the expression
6090 // may cause a message to appear.
6091 if (with_space)
6092 {
6093 // Mark the saved text as finishing the line, so that what
6094 // follows is displayed on a new line when scrolling back
6095 // at the more prompt.
6096 msg_sb_eol();
6097 msg_start();
6098 }
6099 }
6100 else if (with_space)
6101 msg_puts_attr(" ", echo_attr);
6102
6103 if (p != NULL)
6104 for ( ; *p != NUL && !got_int; ++p)
6105 {
6106 if (*p == '\n' || *p == '\r' || *p == TAB)
6107 {
6108 if (*p != TAB && *needclr)
6109 {
6110 // remove any text still there from the command
6111 msg_clr_eos();
6112 *needclr = FALSE;
6113 }
6114 msg_putchar_attr(*p, echo_attr);
6115 }
6116 else
6117 {
6118 if (has_mbyte)
6119 {
6120 int i = (*mb_ptr2len)(p);
6121
6122 (void)msg_outtrans_len_attr(p, i, echo_attr);
6123 p += i - 1;
6124 }
6125 else
6126 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6127 }
6128 }
6129 vim_free(tofree);
6130}
6131
Bram Moolenaare9a41262005-01-15 22:18:47 +00006132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 * ":echo expr1 ..." print each argument separated with a space, add a
6134 * newline at the end.
6135 * ":echon expr1 ..." print each argument plain.
6136 */
6137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006142 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 int needclr = TRUE;
6144 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006145 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006146 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006147 evalarg_T evalarg;
6148
Bram Moolenaare707c882020-07-01 13:07:37 +02006149 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
6151 if (eap->skip)
6152 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006153 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006155 // If eval1() causes an error message the text from the command may
6156 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006157 need_clr_eos = needclr;
6158
Bram Moolenaar68db9962021-05-09 23:19:22 +02006159 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006160 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {
6162 /*
6163 * Report the invalid expression unless the expression evaluation
6164 * has been cancelled due to an aborting error, an interrupt, or an
6165 * exception.
6166 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006167 if (!aborting() && did_emsg == did_emsg_before
6168 && called_emsg == called_emsg_before)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006169 semsg(_(e_invexpr2), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006170 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 break;
6172 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006173 need_clr_eos = FALSE;
6174
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006176 {
6177 if (rettv.v_type == VAR_VOID)
6178 {
6179 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6180 break;
6181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006182 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006183 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006184
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006185 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 arg = skipwhite(arg);
6187 }
6188 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006189 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
6191 if (eap->skip)
6192 --emsg_skip;
6193 else
6194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006195 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 if (needclr)
6197 msg_clr_eos();
6198 if (eap->cmdidx == CMD_echo)
6199 msg_end();
6200 }
6201}
6202
6203/*
6204 * ":echohl {name}".
6205 */
6206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006207ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006209 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210}
6211
6212/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006213 * Returns the :echo attribute
6214 */
6215 int
6216get_echo_attr(void)
6217{
6218 return echo_attr;
6219}
6220
6221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 * ":execute expr1 ..." execute the result of an expression.
6223 * ":echomsg expr1 ..." Print a message
6224 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006225 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 * Each gets spaces around each argument and a newline at the end for
6227 * echo commands
6228 */
6229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006230ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 int ret = OK;
6235 char_u *p;
6236 garray_T ga;
6237 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
6239 ga_init2(&ga, 1, 80);
6240
6241 if (eap->skip)
6242 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006243 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006245 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006246 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248
6249 if (!eap->skip)
6250 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006251 char_u buf[NUMBUFLEN];
6252
6253 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006254 {
6255 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6256 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006257 semsg(_(e_using_invalid_value_as_string_str),
6258 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006259 p = NULL;
6260 }
6261 else
6262 p = tv_get_string_buf(&rettv, buf);
6263 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006264 else
6265 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006266 if (p == NULL)
6267 {
6268 clear_tv(&rettv);
6269 ret = FAIL;
6270 break;
6271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 len = (int)STRLEN(p);
6273 if (ga_grow(&ga, len + 2) == FAIL)
6274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006275 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 ret = FAIL;
6277 break;
6278 }
6279 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 ga.ga_len += len;
6283 }
6284
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006285 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 arg = skipwhite(arg);
6287 }
6288
6289 if (ret != FAIL && ga.ga_data != NULL)
6290 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006291 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6292 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006293 // Mark the already saved text as finishing the line, so that what
6294 // follows is displayed on a new line when scrolling back at the
6295 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006296 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006297 }
6298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006300 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006301 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006302 out_flush();
6303 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006304 else if (eap->cmdidx == CMD_echoconsole)
6305 {
6306 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6307 ui_write((char_u *)"\r\n", 2, TRUE);
6308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 else if (eap->cmdidx == CMD_echoerr)
6310 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006311 int save_did_emsg = did_emsg;
6312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006313 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006314 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (!force_abort)
6316 did_emsg = save_did_emsg;
6317 }
6318 else if (eap->cmdidx == CMD_execute)
6319 do_cmdline((char_u *)ga.ga_data,
6320 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6321 }
6322
6323 ga_clear(&ga);
6324
6325 if (eap->skip)
6326 --emsg_skip;
6327
6328 eap->nextcmd = check_nextcmd(arg);
6329}
6330
6331/*
6332 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6333 * "arg" points to the "&" or '+' when called, to "option" when returning.
6334 * Returns NULL when no option name found. Otherwise pointer to the char
6335 * after the option name.
6336 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006337 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 char_u *p = *arg;
6341
6342 ++p;
6343 if (*p == 'g' && p[1] == ':')
6344 {
6345 *opt_flags = OPT_GLOBAL;
6346 p += 2;
6347 }
6348 else if (*p == 'l' && p[1] == ':')
6349 {
6350 *opt_flags = OPT_LOCAL;
6351 p += 2;
6352 }
6353 else
6354 *opt_flags = 0;
6355
6356 if (!ASCII_ISALPHA(*p))
6357 return NULL;
6358 *arg = p;
6359
6360 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006361 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 else
6363 while (ASCII_ISALPHA(*p))
6364 ++p;
6365 return p;
6366}
6367
6368/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006369 * Display script name where an item was last set.
6370 * Should only be invoked when 'verbose' is non-zero.
6371 */
6372 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006373last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006374{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006375 char_u *p;
6376
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006377 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006378 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006379 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006380 if (p != NULL)
6381 {
6382 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006383 msg_puts(_("\n\tLast set from "));
6384 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006385 if (script_ctx.sc_lnum > 0)
6386 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006387 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006388 msg_outnum((long)script_ctx.sc_lnum);
6389 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006390 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006391 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006392 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006393 }
6394}
6395
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006396#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
6398/*
6399 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006400 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 * "flags" can be "g" to do a global substitute.
6402 * Returns an allocated string, NULL for error.
6403 */
6404 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006405do_string_sub(
6406 char_u *str,
6407 char_u *pat,
6408 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006409 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006410 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 int sublen;
6413 regmatch_T regmatch;
6414 int i;
6415 int do_all;
6416 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006417 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 garray_T ga;
6419 char_u *ret;
6420 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006421 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006423 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006425 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
6427 ga_init2(&ga, 1, 200);
6428
6429 do_all = (flags[0] == 'g');
6430
6431 regmatch.rm_ic = p_ic;
6432 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6433 if (regmatch.regprog != NULL)
6434 {
6435 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006436 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6438 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006439 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006440 if (regmatch.startp[0] == regmatch.endp[0])
6441 {
6442 if (zero_width == regmatch.startp[0])
6443 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006444 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006445 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006446 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6447 (size_t)i);
6448 ga.ga_len += i;
6449 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006450 continue;
6451 }
6452 zero_width = regmatch.startp[0];
6453 }
6454
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 /*
6456 * Get some space for a temporary buffer to do the substitution
6457 * into. It will contain:
6458 * - The text up to where the match is.
6459 * - The substituted text.
6460 * - The text after the match.
6461 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006462 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006463 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6465 {
6466 ga_clear(&ga);
6467 break;
6468 }
6469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006470 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 i = (int)(regmatch.startp[0] - tail);
6472 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006473 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006474 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 + ga.ga_len + i, TRUE, TRUE, FALSE);
6476 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006477 tail = regmatch.endp[0];
6478 if (*tail == NUL)
6479 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (!do_all)
6481 break;
6482 }
6483
6484 if (ga.ga_data != NULL)
6485 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6486
Bram Moolenaar473de612013-06-08 18:19:48 +02006487 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 }
6489
6490 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6491 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006492 if (p_cpo == empty_option)
6493 p_cpo = save_cpo;
6494 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006495 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006496 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006497 // If it's still empty it was changed and restored, need to restore in
6498 // the complicated way.
6499 if (*p_cpo == NUL)
6500 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006501 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
6504 return ret;
6505}