blob: 008b032cefc935f9988826f72d3f167733345db3 [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 Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 if (lp->ll_blob != NULL)
1313 {
1314 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001315
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 if (op != NULL && *op != '=')
1317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001318 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 return;
1320 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001321 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001322 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323
1324 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1325 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001326 if (lp->ll_empty2)
1327 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1328
Bram Moolenaar68452172021-04-12 21:21:02 +02001329 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1330 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001331 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001332 }
1333 else
1334 {
1335 val = (int)tv_get_number_chk(rettv, &error);
1336 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001337 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001338 }
1339 }
1340 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001342 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001343
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001344 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1345 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001346 {
1347 emsg(_(e_cannot_mod));
1348 *endp = cc;
1349 return;
1350 }
1351
Bram Moolenaarff697e62019-02-12 22:28:33 +01001352 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001353 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001354 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001355 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001356 {
1357 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001358 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1359 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001360 && tv_op(&tv, rettv, op) == OK)
1361 set_var(lp->ll_name, &tv, FALSE);
1362 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001364 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001365 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001366 {
1367 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001368 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001369 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001370 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1371 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001372 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001373 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001375 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001376 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001377 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001378 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379 else if (lp->ll_range)
1380 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001381 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001382 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001383
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001384 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1385 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001386 {
1387 emsg(_("E996: Cannot lock a range"));
1388 return;
1389 }
1390
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001391 /*
1392 * Check whether any of the list items is locked
1393 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001394 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001395 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001396 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001397 return;
1398 ri = ri->li_next;
1399 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1400 break;
1401 ll_li = ll_li->li_next;
1402 ++ll_n1;
1403 }
1404
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 /*
1406 * Assign the List values to the list items.
1407 */
1408 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001410 if (op != NULL && *op != '=')
1411 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1412 else
1413 {
1414 clear_tv(&lp->ll_li->li_tv);
1415 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 ri = ri->li_next;
1418 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1419 break;
1420 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001421 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001422 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001423 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 ri = NULL;
1426 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001427 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001428 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001429 lp->ll_li = lp->ll_li->li_next;
1430 ++lp->ll_n1;
1431 }
1432 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001433 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 else if (lp->ll_empty2
1435 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001437 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001438 }
1439 else
1440 {
1441 /*
1442 * Assign to a List or Dictionary item.
1443 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001444 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1445 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001446 {
1447 emsg(_("E996: Cannot lock a list or dict"));
1448 return;
1449 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001450
1451 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001452 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001453 return;
1454
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 if (lp->ll_newkey != NULL)
1456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001457 if (op != NULL && *op != '=')
1458 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001459 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 return;
1461 }
1462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001463 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001464 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465 if (di == NULL)
1466 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001467 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1468 {
1469 vim_free(di);
1470 return;
1471 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001472 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001473 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001474 else if (op != NULL && *op != '=')
1475 {
1476 tv_op(lp->ll_tv, rettv, op);
1477 return;
1478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001479 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001480 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001481
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 /*
1483 * Assign the value to the variable or list item.
1484 */
1485 if (copy)
1486 copy_tv(rettv, lp->ll_tv);
1487 else
1488 {
1489 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001490 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001491 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 }
1493 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494}
1495
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001497 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1498 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499 * Returns OK or FAIL.
1500 */
1501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001503{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001504 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505 char_u numbuf[NUMBUFLEN];
1506 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001507 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001509 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001510 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001511 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001512 {
1513 switch (tv1->v_type)
1514 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001515 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001516 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001518 case VAR_DICT:
1519 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001520 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001521 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001522 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001523 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001524 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001525 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001526 break;
1527
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001528 case VAR_BLOB:
1529 if (*op != '+' || tv2->v_type != VAR_BLOB)
1530 break;
1531 // BLOB += BLOB
1532 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1533 {
1534 blob_T *b1 = tv1->vval.v_blob;
1535 blob_T *b2 = tv2->vval.v_blob;
1536 int i, len = blob_len(b2);
1537 for (i = 0; i < len; i++)
1538 ga_append(&b1->bv_ga, blob_get(b2, i));
1539 }
1540 return OK;
1541
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542 case VAR_LIST:
1543 if (*op != '+' || tv2->v_type != VAR_LIST)
1544 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001545 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001546 if (tv2->vval.v_list != NULL)
1547 {
1548 if (tv1->vval.v_list == NULL)
1549 {
1550 tv1->vval.v_list = tv2->vval.v_list;
1551 ++tv1->vval.v_list->lv_refcount;
1552 }
1553 else
1554 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1555 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 return OK;
1557
1558 case VAR_NUMBER:
1559 case VAR_STRING:
1560 if (tv2->v_type == VAR_LIST)
1561 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001562 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001563 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001564 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001565 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566#ifdef FEAT_FLOAT
1567 if (tv2->v_type == VAR_FLOAT)
1568 {
1569 float_T f = n;
1570
Bram Moolenaarff697e62019-02-12 22:28:33 +01001571 if (*op == '%')
1572 break;
1573 switch (*op)
1574 {
1575 case '+': f += tv2->vval.v_float; break;
1576 case '-': f -= tv2->vval.v_float; break;
1577 case '*': f *= tv2->vval.v_float; break;
1578 case '/': f /= tv2->vval.v_float; break;
1579 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001580 clear_tv(tv1);
1581 tv1->v_type = VAR_FLOAT;
1582 tv1->vval.v_float = f;
1583 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001585#endif
1586 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001587 switch (*op)
1588 {
1589 case '+': n += tv_get_number(tv2); break;
1590 case '-': n -= tv_get_number(tv2); break;
1591 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001592 case '/': n = num_divide(n, tv_get_number(tv2),
1593 &failed); break;
1594 case '%': n = num_modulus(n, tv_get_number(tv2),
1595 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001596 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001597 clear_tv(tv1);
1598 tv1->v_type = VAR_NUMBER;
1599 tv1->vval.v_number = n;
1600 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001601 }
1602 else
1603 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001604 if (tv2->v_type == VAR_FLOAT)
1605 break;
1606
Bram Moolenaarff697e62019-02-12 22:28:33 +01001607 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001608 s = tv_get_string(tv1);
1609 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001610 clear_tv(tv1);
1611 tv1->v_type = VAR_STRING;
1612 tv1->vval.v_string = s;
1613 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001614 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001615
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001616 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001617#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 {
1619 float_T f;
1620
Bram Moolenaarff697e62019-02-12 22:28:33 +01001621 if (*op == '%' || *op == '.'
1622 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 && tv2->v_type != VAR_NUMBER
1624 && tv2->v_type != VAR_STRING))
1625 break;
1626 if (tv2->v_type == VAR_FLOAT)
1627 f = tv2->vval.v_float;
1628 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001629 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 switch (*op)
1631 {
1632 case '+': tv1->vval.v_float += f; break;
1633 case '-': tv1->vval.v_float -= f; break;
1634 case '*': tv1->vval.v_float *= f; break;
1635 case '/': tv1->vval.v_float /= f; break;
1636 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001637 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001639 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 }
1641 }
1642
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001643 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 return FAIL;
1645}
1646
1647/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 * Evaluate the expression used in a ":for var in expr" command.
1649 * "arg" points to "var".
1650 * Set "*errp" to TRUE for an error, FALSE otherwise;
1651 * Return a pointer that holds the info. Null when there is an error.
1652 */
1653 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001654eval_for_line(
1655 char_u *arg,
1656 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001657 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001658 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659{
Bram Moolenaar33570922005-01-25 22:26:29 +00001660 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001662 typval_T tv;
1663 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001664 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001666 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001668 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 if (fi == NULL)
1670 return NULL;
1671
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001672 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 if (expr == NULL)
1674 return fi;
1675
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001676 expr = skipwhite_and_linebreak(expr, evalarg);
1677 if (expr[0] != 'i' || expr[1] != 'n'
1678 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 return fi;
1682 }
1683
1684 if (skip)
1685 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001686 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1687 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 {
1689 *errp = FALSE;
1690 if (!skip)
1691 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001692 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001693 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001694 l = tv.vval.v_list;
1695 if (l == NULL)
1696 {
1697 // a null list is like an empty list: do nothing
1698 clear_tv(&tv);
1699 }
1700 else
1701 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001703 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001705 // No need to increment the refcount, it's already set for
1706 // the list being used in "tv".
1707 fi->fi_list = l;
1708 list_add_watch(l, &fi->fi_lw);
1709 fi->fi_lw.lw_item = l->lv_first;
1710 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001711 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001712 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001713 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001714 fi->fi_bi = 0;
1715 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001716 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001717 typval_T btv;
1718
1719 // Make a copy, so that the iteration still works when the
1720 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001722 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001724 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001725 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001726 else if (tv.v_type == VAR_STRING)
1727 {
1728 fi->fi_byte_idx = 0;
1729 fi->fi_string = tv.vval.v_string;
1730 tv.vval.v_string = NULL;
1731 if (fi->fi_string == NULL)
1732 fi->fi_string = vim_strsave((char_u *)"");
1733 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734 else
1735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001736 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 }
1739 }
1740 }
1741 if (skip)
1742 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001743 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744
1745 return fi;
1746}
1747
1748/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001749 * Used when looping over a :for line, skip the "in expr" part.
1750 */
1751 void
1752skip_for_lines(void *fi_void, evalarg_T *evalarg)
1753{
1754 forinfo_T *fi = (forinfo_T *)fi_void;
1755 int i;
1756
1757 for (i = 0; i < fi->fi_break_count; ++i)
1758 eval_next_line(evalarg);
1759}
1760
1761/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762 * Use the first item in a ":for" list. Advance to the next.
1763 * Assign the values to the variable (list). "arg" points to the first one.
1764 * Return TRUE when a valid item was found, FALSE when at end of list or
1765 * something wrong.
1766 */
1767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001768next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001770 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001772 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1773 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1774 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001775 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001777 if (fi->fi_blob != NULL)
1778 {
1779 typval_T tv;
1780
1781 if (fi->fi_bi >= blob_len(fi->fi_blob))
1782 return FALSE;
1783 tv.v_type = VAR_NUMBER;
1784 tv.v_lock = VAR_FIXED;
1785 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1786 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001787 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001788 fi->fi_varcount, flag, NULL) == OK;
1789 }
1790
1791 if (fi->fi_string != NULL)
1792 {
1793 typval_T tv;
1794 int len;
1795
1796 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1797 if (len == 0)
1798 return FALSE;
1799 tv.v_type = VAR_STRING;
1800 tv.v_lock = VAR_FIXED;
1801 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1802 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001803 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001804 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001805 vim_free(tv.vval.v_string);
1806 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001807 }
1808
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 item = fi->fi_lw.lw_item;
1810 if (item == NULL)
1811 result = FALSE;
1812 else
1813 {
1814 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001815 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001816 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 }
1818 return result;
1819}
1820
1821/*
1822 * Free the structure used to store info used by ":for".
1823 */
1824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001825free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826{
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001829 if (fi == NULL)
1830 return;
1831 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001832 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001833 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001834 list_unref(fi->fi_list);
1835 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001836 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001837 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001838 else
1839 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 vim_free(fi);
1841}
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001844set_context_for_expression(
1845 expand_T *xp,
1846 char_u *arg,
1847 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001849 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001853 if (cmdidx == CMD_let || cmdidx == CMD_var
1854 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 {
1856 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001857 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001859 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001860 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 {
1862 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001863 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001864 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 break;
1866 }
1867 return;
1868 }
1869 }
1870 else
1871 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1872 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 while ((xp->xp_pattern = vim_strpbrk(arg,
1874 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1875 {
1876 c = *xp->xp_pattern;
1877 if (c == '&')
1878 {
1879 c = xp->xp_pattern[1];
1880 if (c == '&')
1881 {
1882 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001883 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 }
1885 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001888 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1889 xp->xp_pattern += 2;
1890
1891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893 else if (c == '$')
1894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001895 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 xp->xp_context = EXPAND_ENV_VARS;
1897 }
1898 else if (c == '=')
1899 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001900 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 xp->xp_context = EXPAND_EXPRESSION;
1902 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001903 else if (c == '#'
1904 && xp->xp_context == EXPAND_EXPRESSION)
1905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001906 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001907 break;
1908 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001909 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 && xp->xp_context == EXPAND_FUNCTIONS
1911 && vim_strchr(xp->xp_pattern, '(') == NULL)
1912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001913 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 break;
1915 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001916 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001918 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1921 if (c == '\\' && xp->xp_pattern[1] != NUL)
1922 ++xp->xp_pattern;
1923 xp->xp_context = EXPAND_NOTHING;
1924 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001927 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1929 /* skip */ ;
1930 xp->xp_context = EXPAND_NOTHING;
1931 }
1932 else if (c == '|')
1933 {
1934 if (xp->xp_pattern[1] == '|')
1935 {
1936 ++xp->xp_pattern;
1937 xp->xp_context = EXPAND_EXPRESSION;
1938 }
1939 else
1940 xp->xp_context = EXPAND_COMMANDS;
1941 }
1942 else
1943 xp->xp_context = EXPAND_EXPRESSION;
1944 }
1945 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001946 // Doesn't look like something valid, expand as an expression
1947 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 arg = xp->xp_pattern;
1950 if (*arg != NUL)
1951 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1952 /* skip */ ;
1953 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001954
1955 // ":exe one two" completes "two"
1956 if ((cmdidx == CMD_execute
1957 || cmdidx == CMD_echo
1958 || cmdidx == CMD_echon
1959 || cmdidx == CMD_echomsg)
1960 && xp->xp_context == EXPAND_EXPRESSION)
1961 {
1962 for (;;)
1963 {
1964 char_u *n = skiptowhite(arg);
1965
1966 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1967 break;
1968 arg = skipwhite(n);
1969 }
1970 }
1971
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 xp->xp_pattern = arg;
1973}
1974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001976 * Return TRUE if "pat" matches "text".
1977 * Does not use 'cpo' and always uses 'magic'.
1978 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001979 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001980pattern_match(char_u *pat, char_u *text, int ic)
1981{
1982 int matches = FALSE;
1983 char_u *save_cpo;
1984 regmatch_T regmatch;
1985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001986 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001987 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001988 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001989 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1990 if (regmatch.regprog != NULL)
1991 {
1992 regmatch.rm_ic = ic;
1993 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1994 vim_regfree(regmatch.regprog);
1995 }
1996 p_cpo = save_cpo;
1997 return matches;
1998}
1999
2000/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002001 * Handle a name followed by "(". Both for just "name(arg)" and for
2002 * "expr->name(arg)".
2003 * Returns OK or FAIL.
2004 */
2005 static int
2006eval_func(
2007 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002008 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002009 char_u *name,
2010 int name_len,
2011 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002012 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013 typval_T *basetv) // "expr" for "expr->name(arg)"
2014{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002015 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002016 char_u *s = name;
2017 int len = name_len;
2018 partial_T *partial;
2019 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002020 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021
2022 if (!evaluate)
2023 check_vars(s, len);
2024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002025 // If "s" is the name of a variable of type VAR_FUNC
2026 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002027 s = deref_func_name(s, &len, &partial,
2028 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002030 // Need to make a copy, in case evaluating the arguments makes
2031 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002032 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002033 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002034 ret = FAIL;
2035 else
2036 {
2037 funcexe_T funcexe;
2038
2039 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002040 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002041 funcexe.firstline = curwin->w_cursor.lnum;
2042 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002043 funcexe.evaluate = evaluate;
2044 funcexe.partial = partial;
2045 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002046 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002047 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002048 }
2049 vim_free(s);
2050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002051 // If evaluate is FALSE rettv->v_type was not set in
2052 // get_func_tv, but it's needed in handle_subscript() to parse
2053 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002054 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2055 {
2056 rettv->vval.v_string = NULL;
2057 rettv->v_type = VAR_FUNC;
2058 }
2059
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002060 // Stop the expression evaluation when immediately
2061 // aborting on error, or when an interrupt occurred or
2062 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 if (evaluate && aborting())
2064 {
2065 if (ret == OK)
2066 clear_tv(rettv);
2067 ret = FAIL;
2068 }
2069 return ret;
2070}
2071
2072/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002073 * Get the next line source line without advancing. But do skip over comment
2074 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002075 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002076 */
2077 static char_u *
2078getline_peek_skip_comments(evalarg_T *evalarg)
2079{
2080 for (;;)
2081 {
2082 char_u *next = getline_peek(evalarg->eval_getline,
2083 evalarg->eval_cookie);
2084 char_u *p;
2085
2086 if (next == NULL)
2087 break;
2088 p = skipwhite(next);
2089 if (*p != NUL && !vim9_comment_start(p))
2090 return next;
2091 (void)eval_next_line(evalarg);
2092 }
2093 return NULL;
2094}
2095
2096/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002097 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2098 * comment) and there is a next line, return the next line (skipping blanks)
2099 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002100 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2101 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002102 * "arg" must point somewhere inside a line, not at the start.
2103 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002104 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002105eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2106{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002107 char_u *p = skipwhite(arg);
2108
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002109 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002110 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002112 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002113 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002114 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002115 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002116
2117 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002118 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002119 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002120 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121
Bram Moolenaar9d489562020-07-30 20:08:50 +02002122 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002123 {
2124 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002125 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126 }
2127 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002128 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002129}
2130
2131/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002132 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002133 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002134 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002135 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002136eval_next_line(evalarg_T *evalarg)
2137{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002138 garray_T *gap = &evalarg->eval_ga;
2139 char_u *line;
2140
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002141 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002142 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2143 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002144 else
2145 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002146 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002147 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2148 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002149 char_u *p = skipwhite(line);
2150
2151 // Going to concatenate the lines after parsing. For an empty or
2152 // comment line use an empty string.
2153 if (*p == NUL || vim9_comment_start(p))
2154 {
2155 vim_free(line);
2156 line = vim_strsave((char_u *)"");
2157 }
2158
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002159 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2160 ++gap->ga_len;
2161 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002162 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002163 {
2164 vim_free(evalarg->eval_tofree);
2165 evalarg->eval_tofree = line;
2166 }
2167 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002168}
2169
Bram Moolenaare6b53242020-07-01 17:28:33 +02002170/*
2171 * Call eval_next_non_blank() and get the next line if needed.
2172 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002173 char_u *
2174skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2175{
2176 int getnext;
2177 char_u *p = skipwhite(arg);
2178
Bram Moolenaar962d7212020-07-04 14:15:00 +02002179 if (evalarg == NULL)
2180 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002181 eval_next_non_blank(p, evalarg, &getnext);
2182 if (getnext)
2183 return eval_next_line(evalarg);
2184 return p;
2185}
2186
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002187/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002188 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002189 */
2190 void
2191clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2192{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002193 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002194 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002195 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002196 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002197 if (eap != NULL)
2198 {
2199 // We may need to keep the original command line, e.g. for
2200 // ":let" it has the variable names. But we may also need the
2201 // new one, "nextcmd" points into it. Keep both.
2202 vim_free(eap->cmdline_tofree);
2203 eap->cmdline_tofree = *eap->cmdlinep;
2204 *eap->cmdlinep = evalarg->eval_tofree;
2205 }
2206 else
2207 vim_free(evalarg->eval_tofree);
2208 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002209 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002210
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002211 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2212 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002213 }
2214}
2215
2216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2220 */
2221
2222/*
2223 * Handle zero level expression.
2224 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002226 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002227 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 * Return OK or FAIL.
2229 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002230 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002231eval0(
2232 char_u *arg,
2233 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002234 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002235 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236{
2237 int ret;
2238 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002239 int did_emsg_before = did_emsg;
2240 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002241 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
2243 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002244 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002245 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002246
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002247 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 /*
2252 * Report the invalid expression unless the expression evaluation has
2253 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002254 * exception, or we already gave a more specific error.
2255 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002257 if (!aborting()
2258 && did_emsg == did_emsg_before
2259 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002260 && (flags & EVAL_CONSTANT) == 0
2261 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002262 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002263
2264 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002265 // a next command to avoid more errors, unless "|" is following, which
2266 // could only be a command separator.
2267 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2268 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002271
2272 if (eap != NULL)
2273 eap->nextcmd = check_nextcmd(p);
2274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 return ret;
2276}
2277
2278/*
2279 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002280 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002281 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 *
2283 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002284 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002286 * Note: "rettv.v_lock" is not set.
2287 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 * Return OK or FAIL.
2289 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002290 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002293 char_u *p;
2294 int getnext;
2295
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002296 CLEAR_POINTER(rettv);
2297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 /*
2299 * Get the first variable.
2300 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 return FAIL;
2303
Bram Moolenaar793648f2020-06-26 21:28:25 +02002304 p = eval_next_non_blank(*arg, evalarg, &getnext);
2305 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002307 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002308 int result;
2309 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002310 evalarg_T *evalarg_used = evalarg;
2311 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002313 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002314 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002315
2316 if (evalarg == NULL)
2317 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002318 CLEAR_FIELD(local_evalarg);
2319 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002320 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002321 orig_flags = evalarg_used->eval_flags;
2322 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002323
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002324 if (getnext)
2325 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002326 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002327 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002328 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002329 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002330 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002331 clear_tv(rettv);
2332 return FAIL;
2333 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002334 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002335 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002336
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002338 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340 int error = FALSE;
2341
Bram Moolenaar13106602020-10-04 16:06:05 +02002342 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002343 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002344 else if (vim9script)
2345 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002346 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002348 if (error || !op_falsy || !result)
2349 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 if (error)
2351 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353
2354 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002355 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002357 if (op_falsy)
2358 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002359 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002360 {
mityu4ac198c2021-05-28 17:52:40 +02002361 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002362 clear_tv(rettv);
2363 return FAIL;
2364 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002365 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002366 evalarg_used->eval_flags = (op_falsy ? !result : result)
2367 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2368 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002369 {
2370 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002372 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002373 if (!op_falsy || !result)
2374 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002376 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002378 /*
2379 * Check for the ":".
2380 */
2381 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2382 if (*p != ':')
2383 {
2384 emsg(_(e_missing_colon));
2385 if (evaluate && result)
2386 clear_tv(rettv);
2387 evalarg_used->eval_flags = orig_flags;
2388 return FAIL;
2389 }
2390 if (getnext)
2391 *arg = eval_next_line(evalarg_used);
2392 else
2393 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002394 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002395 {
2396 error_white_both(p, 1);
2397 clear_tv(rettv);
2398 evalarg_used->eval_flags = orig_flags;
2399 return FAIL;
2400 }
2401 *arg = p;
2402 }
2403
2404 /*
2405 * Get the third variable. Recursive!
2406 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002407 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002408 {
mityu4ac198c2021-05-28 17:52:40 +02002409 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002410 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002411 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002412 return FAIL;
2413 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002414 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2415 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002416 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002417 if (eval1(arg, &var2, evalarg_used) == FAIL)
2418 {
2419 if (evaluate && result)
2420 clear_tv(rettv);
2421 evalarg_used->eval_flags = orig_flags;
2422 return FAIL;
2423 }
2424 if (evaluate && !result)
2425 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002427
2428 if (evalarg == NULL)
2429 clear_evalarg(&local_evalarg, NULL);
2430 else
2431 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433
2434 return OK;
2435}
2436
2437/*
2438 * Handle first level expression:
2439 * expr2 || expr2 || expr2 logical OR
2440 *
2441 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002442 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 *
2444 * Return OK or FAIL.
2445 */
2446 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002449 char_u *p;
2450 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452 /*
2453 * Get the first variable.
2454 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002455 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 return FAIL;
2457
2458 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002459 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002461 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002464 evalarg_T *evalarg_used = evalarg;
2465 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002466 int evaluate;
2467 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468 long result = FALSE;
2469 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002470 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002471 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002472
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002473 if (evalarg == NULL)
2474 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002475 CLEAR_FIELD(local_evalarg);
2476 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002477 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478 orig_flags = evalarg_used->eval_flags;
2479 evaluate = orig_flags & EVAL_EVALUATE;
2480 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002481 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002482 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002483 result = tv_get_bool_chk(rettv, &error);
2484 else if (tv_get_number_chk(rettv, &error) != 0)
2485 result = TRUE;
2486 clear_tv(rettv);
2487 if (error)
2488 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
2490
2491 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002492 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002494 while (p[0] == '|' && p[1] == '|')
2495 {
2496 if (getnext)
2497 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002498 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002499 {
2500 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2501 {
2502 error_white_both(p, 2);
2503 clear_tv(rettv);
2504 return FAIL;
2505 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002506 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002507 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002508
2509 /*
2510 * Get the second variable.
2511 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002512 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2513 {
mityu4ac198c2021-05-28 17:52:40 +02002514 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002515 clear_tv(rettv);
2516 return FAIL;
2517 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002518 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2519 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002522 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002523
2524 /*
2525 * Compute the result.
2526 */
2527 if (evaluate && !result)
2528 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002529 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002530 result = tv_get_bool_chk(&var2, &error);
2531 else if (tv_get_number_chk(&var2, &error) != 0)
2532 result = TRUE;
2533 clear_tv(&var2);
2534 if (error)
2535 return FAIL;
2536 }
2537 if (evaluate)
2538 {
2539 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002540 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002541 rettv->v_type = VAR_BOOL;
2542 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002543 }
2544 else
2545 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002546 rettv->v_type = VAR_NUMBER;
2547 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002548 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002549 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002550
2551 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002553
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002554 if (evalarg == NULL)
2555 clear_evalarg(&local_evalarg, NULL);
2556 else
2557 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
2559
2560 return OK;
2561}
2562
2563/*
2564 * Handle second level expression:
2565 * expr3 && expr3 && expr3 logical AND
2566 *
2567 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002568 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 *
2570 * Return OK or FAIL.
2571 */
2572 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002575 char_u *p;
2576 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
2578 /*
2579 * Get the first variable.
2580 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002581 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 return FAIL;
2583
2584 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002587 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002590 evalarg_T *evalarg_used = evalarg;
2591 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002592 int orig_flags;
2593 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 long result = TRUE;
2595 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002596 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002597 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002598
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002599 if (evalarg == NULL)
2600 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002601 CLEAR_FIELD(local_evalarg);
2602 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002603 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604 orig_flags = evalarg_used->eval_flags;
2605 evaluate = orig_flags & EVAL_EVALUATE;
2606 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002607 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002608 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002609 result = tv_get_bool_chk(rettv, &error);
2610 else if (tv_get_number_chk(rettv, &error) == 0)
2611 result = FALSE;
2612 clear_tv(rettv);
2613 if (error)
2614 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
2616
2617 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 while (p[0] == '&' && p[1] == '&')
2621 {
2622 if (getnext)
2623 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002624 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002625 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002626 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002627 {
2628 error_white_both(p, 2);
2629 clear_tv(rettv);
2630 return FAIL;
2631 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002632 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002633 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002634
2635 /*
2636 * Get the second variable.
2637 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002638 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2639 {
mityu4ac198c2021-05-28 17:52:40 +02002640 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002641 clear_tv(rettv);
2642 return FAIL;
2643 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002644 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2645 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002646 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002647 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002648 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002649 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002650
2651 /*
2652 * Compute the result.
2653 */
2654 if (evaluate && result)
2655 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002656 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002657 result = tv_get_bool_chk(&var2, &error);
2658 else if (tv_get_number_chk(&var2, &error) == 0)
2659 result = FALSE;
2660 clear_tv(&var2);
2661 if (error)
2662 return FAIL;
2663 }
2664 if (evaluate)
2665 {
2666 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002667 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002668 rettv->v_type = VAR_BOOL;
2669 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002670 }
2671 else
2672 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002673 rettv->v_type = VAR_NUMBER;
2674 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002675 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002676 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002677
2678 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002680
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002681 if (evalarg == NULL)
2682 clear_evalarg(&local_evalarg, NULL);
2683 else
2684 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
2686
2687 return OK;
2688}
2689
2690/*
2691 * Handle third level expression:
2692 * var1 == var2
2693 * var1 =~ var2
2694 * var1 != var2
2695 * var1 !~ var2
2696 * var1 > var2
2697 * var1 >= var2
2698 * var1 < var2
2699 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002700 * var1 is var2
2701 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 *
2703 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002704 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 *
2706 * Return OK or FAIL.
2707 */
2708 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002709eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002712 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002713 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002715 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 /*
2718 * Get the first variable.
2719 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002720 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 return FAIL;
2722
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002723 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002724 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002727 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002729 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002731 typval_T var2;
2732 int ic;
2733 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002734 int evaluate = evalarg == NULL
2735 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002736
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002737 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002738 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002739 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002740 p = *arg;
2741 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002742 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2743 {
mityu4ac198c2021-05-28 17:52:40 +02002744 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002745 clear_tv(rettv);
2746 return FAIL;
2747 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002748
Bram Moolenaar696ba232020-07-29 21:20:41 +02002749 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2750 {
2751 semsg(_(e_invexpr2), p);
2752 clear_tv(rettv);
2753 return FAIL;
2754 }
2755
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002756 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (p[len] == '?')
2758 {
2759 ic = TRUE;
2760 ++len;
2761 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002762 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else if (p[len] == '#')
2764 {
2765 ic = FALSE;
2766 ++len;
2767 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002768 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002770 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
2772 /*
2773 * Get the second variable.
2774 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002775 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2776 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002777 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002778 clear_tv(rettv);
2779 return FAIL;
2780 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002781 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002782 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002784 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 return FAIL;
2786 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002787 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002788 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002789 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002790
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002791 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002792 {
2793 ret = FAIL;
2794 clear_tv(rettv);
2795 }
2796 else
2797 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002798 clear_tv(&var2);
2799 return ret;
2800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802
2803 return OK;
2804}
2805
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002806/*
2807 * Make a copy of blob "tv1" and append blob "tv2".
2808 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 void
2810eval_addblob(typval_T *tv1, typval_T *tv2)
2811{
2812 blob_T *b1 = tv1->vval.v_blob;
2813 blob_T *b2 = tv2->vval.v_blob;
2814 blob_T *b = blob_alloc();
2815 int i;
2816
2817 if (b != NULL)
2818 {
2819 for (i = 0; i < blob_len(b1); i++)
2820 ga_append(&b->bv_ga, blob_get(b1, i));
2821 for (i = 0; i < blob_len(b2); i++)
2822 ga_append(&b->bv_ga, blob_get(b2, i));
2823
2824 clear_tv(tv1);
2825 rettv_blob_set(tv1, b);
2826 }
2827}
2828
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002829/*
2830 * Make a copy of list "tv1" and append list "tv2".
2831 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 int
2833eval_addlist(typval_T *tv1, typval_T *tv2)
2834{
2835 typval_T var3;
2836
2837 // concatenate Lists
2838 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2839 {
2840 clear_tv(tv1);
2841 clear_tv(tv2);
2842 return FAIL;
2843 }
2844 clear_tv(tv1);
2845 *tv1 = var3;
2846 return OK;
2847}
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849/*
2850 * Handle fourth level expression:
2851 * + number addition
2852 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002853 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002854 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 *
2856 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002857 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 *
2859 * Return OK or FAIL.
2860 */
2861 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 /*
2865 * Get the first variable.
2866 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002867 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 return FAIL;
2869
2870 /*
2871 * Repeat computing, until no '+', '-' or '.' is following.
2872 */
2873 for (;;)
2874 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002875 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002876 int getnext;
2877 char_u *p;
2878 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002879 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880 int concat;
2881 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002882 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002883
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002884 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002885 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002886 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002887 p = eval_next_non_blank(*arg, evalarg, &getnext);
2888 op = *p;
2889 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002890 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2891 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002893 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2894 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002895
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002896 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002897 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002898 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002899 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002900 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002901 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002902 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002903 {
mityu4ac198c2021-05-28 17:52:40 +02002904 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002905 clear_tv(rettv);
2906 return FAIL;
2907 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002908 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002909 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002910 if ((op != '+' || (rettv->v_type != VAR_LIST
2911 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002912#ifdef FEAT_FLOAT
2913 && (op == '.' || rettv->v_type != VAR_FLOAT)
2914#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002915 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002916 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002917 int error = FALSE;
2918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002919 // For "list + ...", an illegal use of the first operand as
2920 // a number cannot be determined before evaluating the 2nd
2921 // operand: if this is also a list, all is ok.
2922 // For "something . ...", "something - ..." or "non-list + ...",
2923 // we know that the first operand needs to be a string or number
2924 // without evaluating the 2nd operand. So check before to avoid
2925 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002926 if (op != '.')
2927 tv_get_number_chk(rettv, &error);
2928 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002929 {
2930 clear_tv(rettv);
2931 return FAIL;
2932 }
2933 }
2934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 /*
2936 * Get the second variable.
2937 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002938 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002939 {
mityu89dcb4d2021-05-28 13:50:17 +02002940 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002941 clear_tv(rettv);
2942 return FAIL;
2943 }
2944 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002945 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 return FAIL;
2949 }
2950
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002951 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
2953 /*
2954 * Compute the result.
2955 */
2956 if (op == '.')
2957 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2959 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002960 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002961
Bram Moolenaar2e086612020-08-25 22:37:48 +02002962 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002963 || var2.v_type == VAR_CHANNEL
2964 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002965 semsg(_(e_using_invalid_value_as_string_str),
2966 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002967#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002968 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002969 {
2970 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2971 var2.vval.v_float);
2972 s2 = buf2;
2973 }
2974#endif
2975 else
2976 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002977 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002978 {
2979 clear_tv(rettv);
2980 clear_tv(&var2);
2981 return FAIL;
2982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 clear_tv(rettv);
2985 rettv->v_type = VAR_STRING;
2986 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002988 else if (op == '+' && rettv->v_type == VAR_BLOB
2989 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002991 else if (op == '+' && rettv->v_type == VAR_LIST
2992 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002993 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002995 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 else
2998 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999 int error = FALSE;
3000 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003001#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003002 float_T f1 = 0, f2 = 0;
3003
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003004 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003005 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 f1 = rettv->vval.v_float;
3007 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003008 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003009 else
3010#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003012 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003013 if (error)
3014 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003015 // This can only happen for "list + non-list" or
3016 // "blob + non-blob". For "non-list + ..." or
3017 // "something - ...", we returned before evaluating the
3018 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003020 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003021 return FAIL;
3022 }
3023#ifdef FEAT_FLOAT
3024 if (var2.v_type == VAR_FLOAT)
3025 f1 = n1;
3026#endif
3027 }
3028#ifdef FEAT_FLOAT
3029 if (var2.v_type == VAR_FLOAT)
3030 {
3031 f2 = var2.vval.v_float;
3032 n2 = 0;
3033 }
3034 else
3035#endif
3036 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003037 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038 if (error)
3039 {
3040 clear_tv(rettv);
3041 clear_tv(&var2);
3042 return FAIL;
3043 }
3044#ifdef FEAT_FLOAT
3045 if (rettv->v_type == VAR_FLOAT)
3046 f2 = n2;
3047#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050
3051#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003052 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003053 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3054 {
3055 if (op == '+')
3056 f1 = f1 + f2;
3057 else
3058 f1 = f1 - f2;
3059 rettv->v_type = VAR_FLOAT;
3060 rettv->vval.v_float = f1;
3061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063#endif
3064 {
3065 if (op == '+')
3066 n1 = n1 + n2;
3067 else
3068 n1 = n1 - n2;
3069 rettv->v_type = VAR_NUMBER;
3070 rettv->vval.v_number = n1;
3071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003073 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
3075 }
3076 return OK;
3077}
3078
3079/*
3080 * Handle fifth level expression:
3081 * * number multiplication
3082 * / number division
3083 * % number modulo
3084 *
3085 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003086 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 *
3088 * Return OK or FAIL.
3089 */
3090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003091eval6(
3092 char_u **arg,
3093 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003094 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003095 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003097#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003098 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 /*
3102 * Get the first variable.
3103 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003104 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 return FAIL;
3106
3107 /*
3108 * Repeat computing, until no '*', '/' or '%' is following.
3109 */
3110 for (;;)
3111 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003112 int evaluate;
3113 int getnext;
3114 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003115 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003116 int op;
3117 varnumber_T n1, n2;
3118#ifdef FEAT_FLOAT
3119 float_T f1, f2;
3120#endif
3121 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003122
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003123 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003124 p = eval_next_non_blank(*arg, evalarg, &getnext);
3125 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003126 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003128
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003129 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003130 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003131 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003132 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003133 {
3134 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3135 {
mityu4ac198c2021-05-28 17:52:40 +02003136 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003137 clear_tv(rettv);
3138 return FAIL;
3139 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003140 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003143#ifdef FEAT_FLOAT
3144 f1 = 0;
3145 f2 = 0;
3146#endif
3147 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003148 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150#ifdef FEAT_FLOAT
3151 if (rettv->v_type == VAR_FLOAT)
3152 {
3153 f1 = rettv->vval.v_float;
3154 use_float = TRUE;
3155 n1 = 0;
3156 }
3157 else
3158#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003159 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003160 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003161 if (error)
3162 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164 else
3165 n1 = 0;
3166
3167 /*
3168 * Get the second variable.
3169 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003170 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3171 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003172 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003173 clear_tv(rettv);
3174 return FAIL;
3175 }
3176 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003177 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 return FAIL;
3179
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003180 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182#ifdef FEAT_FLOAT
3183 if (var2.v_type == VAR_FLOAT)
3184 {
3185 if (!use_float)
3186 {
3187 f1 = n1;
3188 use_float = TRUE;
3189 }
3190 f2 = var2.vval.v_float;
3191 n2 = 0;
3192 }
3193 else
3194#endif
3195 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003196 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003197 clear_tv(&var2);
3198 if (error)
3199 return FAIL;
3200#ifdef FEAT_FLOAT
3201 if (use_float)
3202 f2 = n2;
3203#endif
3204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 /*
3207 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210#ifdef FEAT_FLOAT
3211 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003213 if (op == '*')
3214 f1 = f1 * f2;
3215 else if (op == '/')
3216 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003217# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003218 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003219 if (f2 == 0.0)
3220 {
3221 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003222 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003223 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003224 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003225 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003226 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003227 }
3228 else
3229 f1 = f1 / f2;
3230# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003231 // We rely on the floating point library to handle divide
3232 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003233 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003234# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003237 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003239 return FAIL;
3240 }
3241 rettv->v_type = VAR_FLOAT;
3242 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003247 int failed = FALSE;
3248
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003249 if (op == '*')
3250 n1 = n1 * n2;
3251 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003252 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003254 n1 = num_modulus(n1, n2, &failed);
3255 if (failed)
3256 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003257
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003258 rettv->v_type = VAR_NUMBER;
3259 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 }
3263
3264 return OK;
3265}
3266
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003267/*
3268 * Handle a type cast before a base level expression.
3269 * "arg" must point to the first non-white of the expression.
3270 * "arg" is advanced to just after the recognized expression.
3271 * Return OK or FAIL.
3272 */
3273 static int
3274eval7t(
3275 char_u **arg,
3276 typval_T *rettv,
3277 evalarg_T *evalarg,
3278 int want_string) // after "." operator
3279{
3280 type_T *want_type = NULL;
3281 garray_T type_list; // list of pointers to allocated types
3282 int res;
3283 int evaluate = evalarg == NULL ? 0
3284 : (evalarg->eval_flags & EVAL_EVALUATE);
3285
3286 // Recognize <type> in Vim9 script only.
3287 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3288 {
3289 ++*arg;
3290 ga_init2(&type_list, sizeof(type_T *), 10);
3291 want_type = parse_type(arg, &type_list, TRUE);
3292 if (want_type == NULL && (evaluate || **arg != '>'))
3293 {
3294 clear_type_list(&type_list);
3295 return FAIL;
3296 }
3297
3298 if (**arg != '>')
3299 {
3300 if (*skipwhite(*arg) == '>')
3301 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3302 else
3303 emsg(_(e_missing_gt));
3304 clear_type_list(&type_list);
3305 return FAIL;
3306 }
3307 ++*arg;
3308 *arg = skipwhite_and_linebreak(*arg, evalarg);
3309 }
3310
3311 res = eval7(arg, rettv, evalarg, want_string);
3312
3313 if (want_type != NULL && evaluate)
3314 {
3315 if (res == OK)
3316 {
3317 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3318
3319 if (!equal_type(want_type, actual))
3320 {
3321 if (want_type == &t_bool && actual != &t_bool
3322 && (actual->tt_flags & TTFLAG_BOOL_OK))
3323 {
3324 int n = tv2bool(rettv);
3325
3326 // can use "0" and "1" for boolean in some places
3327 clear_tv(rettv);
3328 rettv->v_type = VAR_BOOL;
3329 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3330 }
3331 else
3332 {
3333 where_T where;
3334
3335 where.wt_index = 0;
3336 where.wt_variable = TRUE;
3337 res = check_type(want_type, actual, TRUE, where);
3338 }
3339 }
3340 }
3341 clear_type_list(&type_list);
3342 }
3343
3344 return res;
3345}
3346
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003347 int
3348eval_leader(char_u **arg, int vim9)
3349{
3350 char_u *s = *arg;
3351 char_u *p = *arg;
3352
3353 while (*p == '!' || *p == '-' || *p == '+')
3354 {
3355 char_u *n = skipwhite(p + 1);
3356
3357 // ++, --, -+ and +- are not accepted in Vim9 script
3358 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3359 {
3360 semsg(_(e_invexpr2), s);
3361 return FAIL;
3362 }
3363 p = n;
3364 }
3365 *arg = p;
3366 return OK;
3367}
3368
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369/*
3370 * Handle sixth level expression:
3371 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003372 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003373 * "string" string constant
3374 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 * &option-name option value
3376 * @r register contents
3377 * identifier variable value
3378 * function() function call
3379 * $VAR environment variable
3380 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003381 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003383 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003384 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 *
3386 * Also handle:
3387 * ! in front logical NOT
3388 * - in front unary minus
3389 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003390 * trailing [] subscript in String or List
3391 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003392 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 *
3394 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003395 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 *
3397 * Return OK or FAIL.
3398 */
3399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003400eval7(
3401 char_u **arg,
3402 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003403 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003406 int evaluate = evalarg != NULL
3407 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 int len;
3409 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 char_u *start_leader, *end_leader;
3411 int ret = OK;
3412 char_u *alias;
3413
3414 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003415 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003416 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003418 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003421 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 */
3423 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003424 if (eval_leader(arg, in_vim9script()) == FAIL)
3425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 end_leader = *arg;
3427
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003428 if (**arg == '.' && (!isdigit(*(*arg + 1))
3429#ifdef FEAT_FLOAT
3430 || current_sctx.sc_version < 2
3431#endif
3432 ))
3433 {
3434 semsg(_(e_invexpr2), *arg);
3435 ++*arg;
3436 return FAIL;
3437 }
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 switch (**arg)
3440 {
3441 /*
3442 * Number constant.
3443 */
3444 case '0':
3445 case '1':
3446 case '2':
3447 case '3':
3448 case '4':
3449 case '5':
3450 case '6':
3451 case '7':
3452 case '8':
3453 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003454 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003455
3456 // Apply prefixed "-" and "+" now. Matters especially when
3457 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003458 if (ret == OK && evaluate && end_leader > start_leader
3459 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003460 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 break;
3462
3463 /*
3464 * String constant: "string".
3465 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003466 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 break;
3468
3469 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003470 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003472 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003473 break;
3474
3475 /*
3476 * List: [expr, expr]
3477 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003478 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 break;
3480
3481 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003482 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003483 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003484 case '#': if (in_vim9script())
3485 {
3486 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3487 }
3488 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003489 {
3490 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003491 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003492 }
3493 else
3494 ret = NOTDONE;
3495 break;
3496
3497 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003498 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003499 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003500 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003501 case '{': if (in_vim9script())
3502 ret = NOTDONE;
3503 else
3504 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003505 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003506 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003507 break;
3508
3509 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003510 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003512 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 break;
3514
3515 /*
3516 * Environment variable: $VAR.
3517 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003518 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 break;
3520
3521 /*
3522 * Register contents: @r.
3523 */
3524 case '@': ++*arg;
3525 if (evaluate)
3526 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003527 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3528 semsg(_(e_syntax_error_at_str), *arg);
3529 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3530 emsg_invreg(**arg);
3531 else
3532 {
3533 rettv->v_type = VAR_STRING;
3534 rettv->vval.v_string = get_reg_contents(**arg,
3535 GREG_EXPR_SRC);
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 if (**arg != NUL)
3539 ++*arg;
3540 break;
3541
3542 /*
3543 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003544 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003546 case '(': ret = NOTDONE;
3547 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003548 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003549 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003550 if (ret == OK && evaluate)
3551 {
3552 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3553
3554 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003555 if (compile_def_function(ufunc,
3556 TRUE, PROFILING(ufunc), NULL) == FAIL)
3557 {
3558 clear_tv(rettv);
3559 ret = FAIL;
3560 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003561 }
3562 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003563 if (ret == NOTDONE)
3564 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003565 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003566 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003567
Bram Moolenaar9215f012020-06-27 21:18:00 +02003568 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003569 if (**arg == ')')
3570 ++*arg;
3571 else if (ret == OK)
3572 {
3573 emsg(_(e_missing_close));
3574 clear_tv(rettv);
3575 ret = FAIL;
3576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578 break;
3579
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 break;
3582 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583
3584 if (ret == NOTDONE)
3585 {
3586 /*
3587 * Must be a variable or function name.
3588 * Can also be a curly-braces kind of name: {expr}.
3589 */
3590 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003591 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 if (alias != NULL)
3593 s = alias;
3594
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003595 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 ret = FAIL;
3597 else
3598 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003599 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3600
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003601 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003602 {
3603 emsg(_(e_cannot_use_underscore_here));
3604 ret = FAIL;
3605 }
3606 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003607 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003608 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003609 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003610 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003611 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003612 else if (flags & EVAL_CONSTANT)
3613 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003615 {
3616 // get the value of "true", "false" or a variable
3617 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3618 {
3619 rettv->v_type = VAR_BOOL;
3620 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003621 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003622 }
3623 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003624 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003625 {
3626 rettv->v_type = VAR_BOOL;
3627 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003628 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003629 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003630 else if (len == 4 && in_vim9script()
3631 && STRNCMP(s, "null", 4) == 0)
3632 {
3633 rettv->v_type = VAR_SPECIAL;
3634 rettv->vval.v_number = VVAL_NULL;
3635 ret = OK;
3636 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003637 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003638 ret = eval_variable(s, len, rettv, NULL,
3639 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003640 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003641 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003642 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003643 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003644 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003645 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003647 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003648 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649 }
3650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003651 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3652 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003653 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003654 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 /*
3657 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3658 */
3659 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003660 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003661 return ret;
3662}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003663
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003664/*
3665 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003666 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003667 * Adjusts "end_leaderp" until it is at "start_leader".
3668 */
3669 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003670eval7_leader(
3671 typval_T *rettv,
3672 int numeric_only,
3673 char_u *start_leader,
3674 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003675{
3676 char_u *end_leader = *end_leaderp;
3677 int ret = OK;
3678 int error = FALSE;
3679 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003680 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003681#ifdef FEAT_FLOAT
3682 float_T f = 0.0;
3683
3684 if (rettv->v_type == VAR_FLOAT)
3685 f = rettv->vval.v_float;
3686 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003687#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003688 {
3689 while (VIM_ISWHITE(end_leader[-1]))
3690 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003691 if (in_vim9script() && end_leader[-1] == '!')
3692 val = tv2bool(rettv);
3693 else
3694 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003695 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003696 if (error)
3697 {
3698 clear_tv(rettv);
3699 ret = FAIL;
3700 }
3701 else
3702 {
3703 while (end_leader > start_leader)
3704 {
3705 --end_leader;
3706 if (*end_leader == '!')
3707 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003708 if (numeric_only)
3709 {
3710 ++end_leader;
3711 break;
3712 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003713#ifdef FEAT_FLOAT
3714 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003715 {
3716 if (in_vim9script())
3717 {
3718 rettv->v_type = VAR_BOOL;
3719 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3720 }
3721 else
3722 f = !f;
3723 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003724 else
3725#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003726 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003727 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003728 type = VAR_BOOL;
3729 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003730 }
3731 else if (*end_leader == '-')
3732 {
3733#ifdef FEAT_FLOAT
3734 if (rettv->v_type == VAR_FLOAT)
3735 f = -f;
3736 else
3737#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003738 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003739 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003740 type = VAR_NUMBER;
3741 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003742 }
3743 }
3744#ifdef FEAT_FLOAT
3745 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003747 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003748 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003750 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003751#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003752 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003753 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003754 if (in_vim9script())
3755 rettv->v_type = type;
3756 else
3757 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003758 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003761 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 return ret;
3763}
3764
3765/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003766 * Call the function referred to in "rettv".
3767 */
3768 static int
3769call_func_rettv(
3770 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003771 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003772 typval_T *rettv,
3773 int evaluate,
3774 dict_T *selfdict,
3775 typval_T *basetv)
3776{
3777 partial_T *pt = NULL;
3778 funcexe_T funcexe;
3779 typval_T functv;
3780 char_u *s;
3781 int ret;
3782
3783 // need to copy the funcref so that we can clear rettv
3784 if (evaluate)
3785 {
3786 functv = *rettv;
3787 rettv->v_type = VAR_UNKNOWN;
3788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003789 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003790 if (functv.v_type == VAR_PARTIAL)
3791 {
3792 pt = functv.vval.v_partial;
3793 s = partial_name(pt);
3794 }
3795 else
3796 s = functv.vval.v_string;
3797 }
3798 else
3799 s = (char_u *)"";
3800
Bram Moolenaara80faa82020-04-12 19:37:17 +02003801 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003802 funcexe.firstline = curwin->w_cursor.lnum;
3803 funcexe.lastline = curwin->w_cursor.lnum;
3804 funcexe.evaluate = evaluate;
3805 funcexe.partial = pt;
3806 funcexe.selfdict = selfdict;
3807 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003808 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003810 // Clear the funcref afterwards, so that deleting it while
3811 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003812 if (evaluate)
3813 clear_tv(&functv);
3814
3815 return ret;
3816}
3817
3818/*
3819 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003820 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003821 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3822 */
3823 static int
3824eval_lambda(
3825 char_u **arg,
3826 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003827 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003828 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003829{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003830 int evaluate = evalarg != NULL
3831 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003832 typval_T base = *rettv;
3833 int ret;
3834
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003835 rettv->v_type = VAR_UNKNOWN;
3836
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003837 if (**arg == '{')
3838 {
3839 // ->{lambda}()
3840 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3841 }
3842 else
3843 {
3844 // ->(lambda)()
3845 ++*arg;
3846 ret = eval1(arg, rettv, evalarg);
3847 *arg = skipwhite_and_linebreak(*arg, evalarg);
3848 if (**arg != ')')
3849 {
3850 emsg(_(e_missing_close));
3851 ret = FAIL;
3852 }
3853 ++*arg;
3854 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003855 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003856 return FAIL;
3857 else if (**arg != '(')
3858 {
3859 if (verbose)
3860 {
3861 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003862 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003863 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003865 }
3866 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003867 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003868 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003869 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003870 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003871
3872 // Clear the funcref afterwards, so that deleting it while
3873 // evaluating the arguments is possible (see test55).
3874 if (evaluate)
3875 clear_tv(&base);
3876
3877 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003878}
3879
3880/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003881 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003882 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003883 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3884 */
3885 static int
3886eval_method(
3887 char_u **arg,
3888 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003889 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003890 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003891{
3892 char_u *name;
3893 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003894 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003895 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003896 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003897 int evaluate = evalarg != NULL
3898 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003899
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003900 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003901
Bram Moolenaarac92e252019-08-03 21:58:38 +02003902 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003903 len = get_name_len(arg, &alias, evaluate, TRUE);
3904 if (alias != NULL)
3905 name = alias;
3906
3907 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003908 {
3909 if (verbose)
3910 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003911 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003912 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003913 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003914 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003915 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003916 if (**arg != '(')
3917 {
3918 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003920 ret = FAIL;
3921 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003922 else if (VIM_ISWHITE((*arg)[-1]))
3923 {
3924 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003925 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003926 ret = FAIL;
3927 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003928 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003929 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003930 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003931 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003932
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003933 // Clear the funcref afterwards, so that deleting it while
3934 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003935 if (evaluate)
3936 clear_tv(&base);
3937
Bram Moolenaarac92e252019-08-03 21:58:38 +02003938 return ret;
3939}
3940
3941/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003942 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3943 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003944 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3945 */
3946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003947eval_index(
3948 char_u **arg,
3949 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003950 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003951 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003952{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003953 int evaluate = evalarg != NULL
3954 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003955 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003957 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003958 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003959 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003960 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003962 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3963 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003964
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003965 init_tv(&var1);
3966 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003967 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003969 /*
3970 * dict.name
3971 */
3972 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003973 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003974 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003975 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003976 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003977 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003978 }
3979 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003980 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003981 /*
3982 * something[idx]
3983 *
3984 * Get the (first) variable from inside the [].
3985 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003986 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003987 if (**arg == ':')
3988 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003989 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003990 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003991 else if (vim9 && **arg == ':')
3992 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003993 semsg(_(e_white_space_required_before_and_after_str_at_str),
3994 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003995 clear_tv(&var1);
3996 return FAIL;
3997 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003998 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003999 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004000#ifdef FEAT_FLOAT
4001 // allow for indexing with float
4002 if (vim9 && rettv->v_type == VAR_DICT
4003 && var1.v_type == VAR_FLOAT)
4004 {
4005 var1.vval.v_string = typval_tostring(&var1, TRUE);
4006 var1.v_type = VAR_STRING;
4007 }
4008#endif
4009 if (tv_get_string_chk(&var1) == NULL)
4010 {
4011 // not a number or string
4012 clear_tv(&var1);
4013 return FAIL;
4014 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004016
4017 /*
4018 * Get the second variable from inside the [:].
4019 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004020 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004021 if (**arg == ':')
4022 {
4023 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004024 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004025 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004026 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004027 semsg(_(e_white_space_required_before_and_after_str_at_str),
4028 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004029 if (!empty1)
4030 clear_tv(&var1);
4031 return FAIL;
4032 }
4033 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004034 if (**arg == ']')
4035 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004036 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004038 if (!empty1)
4039 clear_tv(&var1);
4040 return FAIL;
4041 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004042 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004044 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004045 if (!empty1)
4046 clear_tv(&var1);
4047 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004048 return FAIL;
4049 }
4050 }
4051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004052 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004053 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004054 if (**arg != ']')
4055 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004056 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004057 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004058 clear_tv(&var1);
4059 if (range)
4060 clear_tv(&var2);
4061 return FAIL;
4062 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004063 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004064 }
4065
4066 if (evaluate)
4067 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004068 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004069 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004070 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004071
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004072 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004074 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004075 clear_tv(&var2);
4076 return res;
4077 }
4078 return OK;
4079}
4080
4081/*
4082 * Check if "rettv" can have an [index] or [sli:ce]
4083 */
4084 int
4085check_can_index(typval_T *rettv, int evaluate, int verbose)
4086{
4087 switch (rettv->v_type)
4088 {
4089 case VAR_FUNC:
4090 case VAR_PARTIAL:
4091 if (verbose)
4092 emsg(_("E695: Cannot index a Funcref"));
4093 return FAIL;
4094 case VAR_FLOAT:
4095#ifdef FEAT_FLOAT
4096 if (verbose)
4097 emsg(_(e_float_as_string));
4098 return FAIL;
4099#endif
4100 case VAR_BOOL:
4101 case VAR_SPECIAL:
4102 case VAR_JOB:
4103 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004104 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004105 if (verbose)
4106 emsg(_(e_cannot_index_special_variable));
4107 return FAIL;
4108 case VAR_UNKNOWN:
4109 case VAR_ANY:
4110 case VAR_VOID:
4111 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004112 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004113 emsg(_(e_cannot_index_special_variable));
4114 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004115 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004116 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004117
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004118 case VAR_STRING:
4119 case VAR_LIST:
4120 case VAR_DICT:
4121 case VAR_BLOB:
4122 break;
4123 case VAR_NUMBER:
4124 if (in_vim9script())
4125 emsg(_(e_cannot_index_number));
4126 break;
4127 }
4128 return OK;
4129}
4130
4131/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004132 * slice() function
4133 */
4134 void
4135f_slice(typval_T *argvars, typval_T *rettv)
4136{
4137 if (check_can_index(argvars, TRUE, FALSE) == OK)
4138 {
4139 copy_tv(argvars, rettv);
4140 eval_index_inner(rettv, TRUE, argvars + 1,
4141 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4142 TRUE, NULL, 0, FALSE);
4143 }
4144}
4145
4146/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004147 * Apply index or range to "rettv".
4148 * "var1" is the first index, NULL for [:expr].
4149 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004150 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4151 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004152 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4153 */
4154 int
4155eval_index_inner(
4156 typval_T *rettv,
4157 int is_range,
4158 typval_T *var1,
4159 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004160 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004161 char_u *key,
4162 int keylen,
4163 int verbose)
4164{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004165 varnumber_T n1, n2 = 0;
4166 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167
4168 n1 = 0;
4169 if (var1 != NULL && rettv->v_type != VAR_DICT)
4170 n1 = tv_get_number(var1);
4171
4172 if (is_range)
4173 {
4174 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004175 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004176 if (verbose)
4177 emsg(_(e_cannot_slice_dictionary));
4178 return FAIL;
4179 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004180 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004181 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004182 else
4183 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004184 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004185
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004186 switch (rettv->v_type)
4187 {
4188 case VAR_UNKNOWN:
4189 case VAR_ANY:
4190 case VAR_VOID:
4191 case VAR_FUNC:
4192 case VAR_PARTIAL:
4193 case VAR_FLOAT:
4194 case VAR_BOOL:
4195 case VAR_SPECIAL:
4196 case VAR_JOB:
4197 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004198 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004199 break; // not evaluating, skipping over subscript
4200
4201 case VAR_NUMBER:
4202 case VAR_STRING:
4203 {
4204 char_u *s = tv_get_string(rettv);
4205
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004206 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004207 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004208 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004209 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004210 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004211 else
4212 s = char_from_string(s, n1);
4213 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004214 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004215 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004216 // The resulting variable is a substring. If the indexes
4217 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004218 if (n1 < 0)
4219 {
4220 n1 = len + n1;
4221 if (n1 < 0)
4222 n1 = 0;
4223 }
4224 if (n2 < 0)
4225 n2 = len + n2;
4226 else if (n2 >= len)
4227 n2 = len;
4228 if (n1 >= len || n2 < 0 || n1 > n2)
4229 s = NULL;
4230 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004231 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004232 }
4233 else
4234 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004235 // The resulting variable is a string of a single
4236 // character. If the index is too big or negative the
4237 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004238 if (n1 >= len || n1 < 0)
4239 s = NULL;
4240 else
4241 s = vim_strnsave(s + n1, 1);
4242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 clear_tv(rettv);
4244 rettv->v_type = VAR_STRING;
4245 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004246 }
4247 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004248
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004249 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004250 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4251 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004252 break;
4253
4254 case VAR_LIST:
4255 if (var1 == NULL)
4256 n1 = 0;
4257 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004258 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004259 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004260 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004261 return FAIL;
4262 break;
4263
4264 case VAR_DICT:
4265 {
4266 dictitem_T *item;
4267 typval_T tmp;
4268
4269 if (key == NULL)
4270 {
4271 key = tv_get_string_chk(var1);
4272 if (key == NULL)
4273 return FAIL;
4274 }
4275
4276 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4277
4278 if (item == NULL && verbose)
4279 semsg(_(e_dictkey), key);
4280 if (item == NULL)
4281 return FAIL;
4282
4283 copy_tv(&item->di_tv, &tmp);
4284 clear_tv(rettv);
4285 *rettv = tmp;
4286 }
4287 break;
4288 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004289 return OK;
4290}
4291
4292/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004293 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004294 */
4295 char_u *
4296partial_name(partial_T *pt)
4297{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004298 if (pt != NULL)
4299 {
4300 if (pt->pt_name != NULL)
4301 return pt->pt_name;
4302 if (pt->pt_func != NULL)
4303 return pt->pt_func->uf_name;
4304 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004305 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004306}
4307
Bram Moolenaarddecc252016-04-06 22:59:37 +02004308 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004309partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004310{
4311 int i;
4312
4313 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004314 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004315 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004316 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004317 if (pt->pt_name != NULL)
4318 {
4319 func_unref(pt->pt_name);
4320 vim_free(pt->pt_name);
4321 }
4322 else
4323 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004324
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004325 // Decrease the reference count for the context of a closure. If down
4326 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004327 if (pt->pt_funcstack != NULL)
4328 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004329 --pt->pt_funcstack->fs_refcount;
4330 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004331 }
4332
Bram Moolenaarddecc252016-04-06 22:59:37 +02004333 vim_free(pt);
4334}
4335
4336/*
4337 * Unreference a closure: decrement the reference count and free it when it
4338 * becomes zero.
4339 */
4340 void
4341partial_unref(partial_T *pt)
4342{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004343 if (pt != NULL)
4344 {
4345 if (--pt->pt_refcount <= 0)
4346 partial_free(pt);
4347
4348 // If the reference count goes down to one, the funcstack may be the
4349 // only reference and can be freed if no other partials reference it.
4350 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4351 funcstack_check_refcount(pt->pt_funcstack);
4352 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004353}
4354
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004355/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004356 * Return the next (unique) copy ID.
4357 * Used for serializing nested structures.
4358 */
4359 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004360get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004361{
4362 current_copyID += COPYID_INC;
4363 return current_copyID;
4364}
4365
4366/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004367 * Garbage collection for lists and dictionaries.
4368 *
4369 * We use reference counts to be able to free most items right away when they
4370 * are no longer used. But for composite items it's possible that it becomes
4371 * unused while the reference count is > 0: When there is a recursive
4372 * reference. Example:
4373 * :let l = [1, 2, 3]
4374 * :let d = {9: l}
4375 * :let l[1] = d
4376 *
4377 * Since this is quite unusual we handle this with garbage collection: every
4378 * once in a while find out which lists and dicts are not referenced from any
4379 * variable.
4380 *
4381 * Here is a good reference text about garbage collection (refers to Python
4382 * but it applies to all reference-counting mechanisms):
4383 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004384 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004385
4386/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004387 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004388 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004389 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004390 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004391 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004392garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004393{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004394 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004395 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004396 buf_T *buf;
4397 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004398 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004399 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004400
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004401 if (!testing)
4402 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004403 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004404 want_garbage_collect = FALSE;
4405 may_garbage_collect = FALSE;
4406 garbage_collect_at_exit = FALSE;
4407 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004408
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004409 // The execution stack can grow big, limit the size.
4410 if (exestack.ga_maxlen - exestack.ga_len > 500)
4411 {
4412 size_t new_len;
4413 char_u *pp;
4414 int n;
4415
4416 // Keep 150% of the current size, with a minimum of the growth size.
4417 n = exestack.ga_len / 2;
4418 if (n < exestack.ga_growsize)
4419 n = exestack.ga_growsize;
4420
4421 // Don't make it bigger though.
4422 if (exestack.ga_len + n < exestack.ga_maxlen)
4423 {
4424 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4425 pp = vim_realloc(exestack.ga_data, new_len);
4426 if (pp == NULL)
4427 return FAIL;
4428 exestack.ga_maxlen = exestack.ga_len + n;
4429 exestack.ga_data = pp;
4430 }
4431 }
4432
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004433 // We advance by two because we add one for items referenced through
4434 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004435 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004436
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004437 /*
4438 * 1. Go through all accessible variables and mark all lists and dicts
4439 * with copyID.
4440 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004442 // Don't free variables in the previous_funccal list unless they are only
4443 // referenced through previous_funccal. This must be first, because if
4444 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004447 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004448 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004450 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004451 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004452 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4453 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004456 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4458 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004459 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4461 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004462#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004463 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004464 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4465 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004466 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004467 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004468 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4469 NULL, NULL);
4470#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004472 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004473 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004474 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4475 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004476 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004477 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004480 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004483 abort = abort || set_ref_in_functions(copyID);
4484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004486 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004489 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004490
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004491 // callbacks in buffers
4492 abort = abort || set_ref_in_buffers(copyID);
4493
Bram Moolenaar1dced572012-04-05 16:54:08 +02004494#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004495 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004496#endif
4497
Bram Moolenaardb913952012-06-29 12:54:53 +02004498#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004499 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004500#endif
4501
4502#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004503 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004504#endif
4505
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004506#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004507 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004508 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004509#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004510#ifdef FEAT_NETBEANS_INTG
4511 abort = abort || set_ref_in_nb_channel(copyID);
4512#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004513
Bram Moolenaare3188e22016-05-31 21:13:04 +02004514#ifdef FEAT_TIMERS
4515 abort = abort || set_ref_in_timer(copyID);
4516#endif
4517
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004518#ifdef FEAT_QUICKFIX
4519 abort = abort || set_ref_in_quickfix(copyID);
4520#endif
4521
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004522#ifdef FEAT_TERMINAL
4523 abort = abort || set_ref_in_term(copyID);
4524#endif
4525
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004526#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004527 abort = abort || set_ref_in_popups(copyID);
4528#endif
4529
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004530 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004531 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004532 /*
4533 * 2. Free lists and dictionaries that are not referenced.
4534 */
4535 did_free = free_unref_items(copyID);
4536
4537 /*
4538 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004539 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004540 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004541 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004542 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 else if (p_verbose > 0)
4544 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004545 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004546 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004547
4548 return did_free;
4549}
4550
4551/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004552 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004553 */
4554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004555free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004556{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004557 int did_free = FALSE;
4558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 // Let all "free" functions know that we are here. This means no
4560 // dictionaries, lists, channels or jobs are to be freed, because we will
4561 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004562 in_free_unref_items = TRUE;
4563
4564 /*
4565 * PASS 1: free the contents of the items. We don't free the items
4566 * themselves yet, so that it is possible to decrement refcount counters
4567 */
4568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004570 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004572 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004573 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004574
4575#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004576 // Go through the list of jobs and free items without the copyID. This
4577 // must happen before doing channels, because jobs refer to channels, but
4578 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004579 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004581 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004582 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4583#endif
4584
4585 /*
4586 * PASS 2: free the items themselves.
4587 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004588 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004589 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004590
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004591#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004592 // Go through the list of jobs and free items without the copyID. This
4593 // must happen before doing channels, because jobs refer to channels, but
4594 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004595 free_unused_jobs(copyID, COPYID_MASK);
4596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004597 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004598 free_unused_channels(copyID, COPYID_MASK);
4599#endif
4600
4601 in_free_unref_items = FALSE;
4602
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004603 return did_free;
4604}
4605
4606/*
4607 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004608 * "list_stack" is used to add lists to be marked. Can be NULL.
4609 *
4610 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004611 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004613set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004614{
4615 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004616 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004618 hashtab_T *cur_ht;
4619 ht_stack_T *ht_stack = NULL;
4620 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004621
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004622 cur_ht = ht;
4623 for (;;)
4624 {
4625 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004626 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 // Mark each item in the hashtab. If the item contains a hashtab
4628 // it is added to ht_stack, if it contains a list it is added to
4629 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004630 todo = (int)cur_ht->ht_used;
4631 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4632 if (!HASHITEM_EMPTY(hi))
4633 {
4634 --todo;
4635 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4636 &ht_stack, list_stack);
4637 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004638 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004639
4640 if (ht_stack == NULL)
4641 break;
4642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004644 cur_ht = ht_stack->ht;
4645 tempitem = ht_stack;
4646 ht_stack = ht_stack->prev;
4647 free(tempitem);
4648 }
4649
4650 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004651}
4652
4653/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004654 * Mark a dict and its items with "copyID".
4655 * Returns TRUE if setting references failed somehow.
4656 */
4657 int
4658set_ref_in_dict(dict_T *d, int copyID)
4659{
4660 if (d != NULL && d->dv_copyID != copyID)
4661 {
4662 d->dv_copyID = copyID;
4663 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4664 }
4665 return FALSE;
4666}
4667
4668/*
4669 * Mark a list and its items with "copyID".
4670 * Returns TRUE if setting references failed somehow.
4671 */
4672 int
4673set_ref_in_list(list_T *ll, int copyID)
4674{
4675 if (ll != NULL && ll->lv_copyID != copyID)
4676 {
4677 ll->lv_copyID = copyID;
4678 return set_ref_in_list_items(ll, copyID, NULL);
4679 }
4680 return FALSE;
4681}
4682
4683/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004684 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004685 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4686 *
4687 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004688 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004689 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004690set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004691{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004692 listitem_T *li;
4693 int abort = FALSE;
4694 list_T *cur_l;
4695 list_stack_T *list_stack = NULL;
4696 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004697
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004698 cur_l = l;
4699 for (;;)
4700 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004701 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004702 // Mark each item in the list. If the item contains a hashtab
4703 // it is added to ht_stack, if it contains a list it is added to
4704 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004705 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4706 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4707 ht_stack, &list_stack);
4708 if (list_stack == NULL)
4709 break;
4710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004711 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004712 cur_l = list_stack->list;
4713 tempitem = list_stack;
4714 list_stack = list_stack->prev;
4715 free(tempitem);
4716 }
4717
4718 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004719}
4720
4721/*
4722 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004723 * "list_stack" is used to add lists to be marked. Can be NULL.
4724 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4725 *
4726 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004727 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004728 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004729set_ref_in_item(
4730 typval_T *tv,
4731 int copyID,
4732 ht_stack_T **ht_stack,
4733 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004734{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004735 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004736
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004737 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004738 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004739 dict_T *dd = tv->vval.v_dict;
4740
Bram Moolenaara03f2332016-02-06 18:09:59 +01004741 if (dd != NULL && dd->dv_copyID != copyID)
4742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004743 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004744 dd->dv_copyID = copyID;
4745 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004746 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004747 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4748 }
4749 else
4750 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004751 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4752
Bram Moolenaara03f2332016-02-06 18:09:59 +01004753 if (newitem == NULL)
4754 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004755 else
4756 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004757 newitem->ht = &dd->dv_hashtab;
4758 newitem->prev = *ht_stack;
4759 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004760 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004761 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004762 }
4763 }
4764 else if (tv->v_type == VAR_LIST)
4765 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004766 list_T *ll = tv->vval.v_list;
4767
Bram Moolenaara03f2332016-02-06 18:09:59 +01004768 if (ll != NULL && ll->lv_copyID != copyID)
4769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004770 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004771 ll->lv_copyID = copyID;
4772 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004773 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004774 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004775 }
4776 else
4777 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004778 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4779
Bram Moolenaara03f2332016-02-06 18:09:59 +01004780 if (newitem == NULL)
4781 abort = TRUE;
4782 else
4783 {
4784 newitem->list = ll;
4785 newitem->prev = *list_stack;
4786 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004787 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004788 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004789 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004790 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004791 else if (tv->v_type == VAR_FUNC)
4792 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004793 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004794 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004795 else if (tv->v_type == VAR_PARTIAL)
4796 {
4797 partial_T *pt = tv->vval.v_partial;
4798 int i;
4799
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004800 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004801 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004802 // Didn't see this partial yet.
4803 pt->pt_copyID = copyID;
4804
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004805 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004806
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004807 if (pt->pt_dict != NULL)
4808 {
4809 typval_T dtv;
4810
4811 dtv.v_type = VAR_DICT;
4812 dtv.vval.v_dict = pt->pt_dict;
4813 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4814 }
4815
4816 for (i = 0; i < pt->pt_argc; ++i)
4817 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4818 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004819 if (pt->pt_funcstack != NULL)
4820 {
4821 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4822
4823 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4824 abort = abort || set_ref_in_item(stack + i, copyID,
4825 ht_stack, list_stack);
4826 }
4827
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004828 }
4829 }
4830#ifdef FEAT_JOB_CHANNEL
4831 else if (tv->v_type == VAR_JOB)
4832 {
4833 job_T *job = tv->vval.v_job;
4834 typval_T dtv;
4835
4836 if (job != NULL && job->jv_copyID != copyID)
4837 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004838 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004839 if (job->jv_channel != NULL)
4840 {
4841 dtv.v_type = VAR_CHANNEL;
4842 dtv.vval.v_channel = job->jv_channel;
4843 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4844 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004845 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004846 {
4847 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004848 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004849 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4850 }
4851 }
4852 }
4853 else if (tv->v_type == VAR_CHANNEL)
4854 {
4855 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004856 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004857 typval_T dtv;
4858 jsonq_T *jq;
4859 cbq_T *cq;
4860
4861 if (ch != NULL && ch->ch_copyID != copyID)
4862 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004863 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004864 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004865 {
4866 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4867 jq = jq->jq_next)
4868 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4869 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4870 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004871 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004872 {
4873 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004874 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004875 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4876 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004877 if (ch->ch_part[part].ch_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 =
4881 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004882 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4883 }
4884 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004885 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004886 {
4887 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004888 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004889 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4890 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004891 if (ch->ch_close_cb.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_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004895 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4896 }
4897 }
4898 }
4899#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004900 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004901}
4902
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004904 * Return a string with the string representation of a variable.
4905 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004906 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004907 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004908 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004909 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004910 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004911 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4912 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004913 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004915 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004916echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004917 typval_T *tv,
4918 char_u **tofree,
4919 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004920 int copyID,
4921 int echo_style,
4922 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004923 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004924{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004925 static int recurse = 0;
4926 char_u *r = NULL;
4927
Bram Moolenaar33570922005-01-25 22:26:29 +00004928 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004929 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004930 if (!did_echo_string_emsg)
4931 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 // Only give this message once for a recursive call to avoid
4933 // flooding the user with errors. And stop iterating over lists
4934 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004935 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004936 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004937 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004938 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004939 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004940 }
4941 ++recurse;
4942
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 switch (tv->v_type)
4944 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004945 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004946 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004947 {
4948 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004949 r = tv->vval.v_string;
4950 if (r == NULL)
4951 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004952 }
4953 else
4954 {
4955 *tofree = string_quote(tv->vval.v_string, FALSE);
4956 r = *tofree;
4957 }
4958 break;
4959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004961 if (echo_style)
4962 {
4963 *tofree = NULL;
4964 r = tv->vval.v_string;
4965 }
4966 else
4967 {
4968 *tofree = string_quote(tv->vval.v_string, TRUE);
4969 r = *tofree;
4970 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004972
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004973 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004974 {
4975 partial_T *pt = tv->vval.v_partial;
4976 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004977 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004978 garray_T ga;
4979 int i;
4980 char_u *tf;
4981
4982 ga_init2(&ga, 1, 100);
4983 ga_concat(&ga, (char_u *)"function(");
4984 if (fname != NULL)
4985 {
4986 ga_concat(&ga, fname);
4987 vim_free(fname);
4988 }
4989 if (pt != NULL && pt->pt_argc > 0)
4990 {
4991 ga_concat(&ga, (char_u *)", [");
4992 for (i = 0; i < pt->pt_argc; ++i)
4993 {
4994 if (i > 0)
4995 ga_concat(&ga, (char_u *)", ");
4996 ga_concat(&ga,
4997 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4998 vim_free(tf);
4999 }
5000 ga_concat(&ga, (char_u *)"]");
5001 }
5002 if (pt != NULL && pt->pt_dict != NULL)
5003 {
5004 typval_T dtv;
5005
5006 ga_concat(&ga, (char_u *)", ");
5007 dtv.v_type = VAR_DICT;
5008 dtv.vval.v_dict = pt->pt_dict;
5009 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5010 vim_free(tf);
5011 }
5012 ga_concat(&ga, (char_u *)")");
5013
5014 *tofree = ga.ga_data;
5015 r = *tofree;
5016 break;
5017 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005018
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005019 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005020 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005021 break;
5022
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005023 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005024 if (tv->vval.v_list == NULL)
5025 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005026 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005027 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005028 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005029 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005030 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5031 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005032 {
5033 *tofree = NULL;
5034 r = (char_u *)"[...]";
5035 }
5036 else
5037 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005038 int old_copyID = tv->vval.v_list->lv_copyID;
5039
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005040 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005041 *tofree = list2string(tv, copyID, restore_copyID);
5042 if (restore_copyID)
5043 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005044 r = *tofree;
5045 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005046 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005047
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005049 if (tv->vval.v_dict == NULL)
5050 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005051 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005052 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005053 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005054 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005055 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5056 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005057 {
5058 *tofree = NULL;
5059 r = (char_u *)"{...}";
5060 }
5061 else
5062 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005063 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005064
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005065 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005066 *tofree = dict2string(tv, copyID, restore_copyID);
5067 if (restore_copyID)
5068 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005069 r = *tofree;
5070 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005071 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005072
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005074 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005075 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005077 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005078 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005079 break;
5080
Bram Moolenaar835dc632016-02-07 14:27:38 +01005081 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005082 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005083 *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 if (composite_val)
5086 {
5087 *tofree = string_quote(r, FALSE);
5088 r = *tofree;
5089 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005090 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005091
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005092 case VAR_INSTR:
5093 *tofree = NULL;
5094 r = (char_u *)"instructions";
5095 break;
5096
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005098#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005099 *tofree = NULL;
5100 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5101 r = numbuf;
5102 break;
5103#endif
5104
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005105 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005106 case VAR_SPECIAL:
5107 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005108 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005109 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111
Bram Moolenaar8502c702014-06-17 12:51:16 +02005112 if (--recurse == 0)
5113 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005114 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115}
5116
5117/*
5118 * Return a string with the string representation of a variable.
5119 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5120 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005121 * Does not put quotes around strings, as ":echo" displays values.
5122 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5123 * May return NULL.
5124 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005125 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005126echo_string(
5127 typval_T *tv,
5128 char_u **tofree,
5129 char_u *numbuf,
5130 int copyID)
5131{
5132 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5133}
5134
5135/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005136 * Return string "str" in ' quotes, doubling ' characters.
5137 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005139 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005140 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005141string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005142{
Bram Moolenaar33570922005-01-25 22:26:29 +00005143 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005144 char_u *p, *r, *s;
5145
Bram Moolenaar33570922005-01-25 22:26:29 +00005146 len = (function ? 13 : 3);
5147 if (str != NULL)
5148 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005149 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005150 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005151 if (*p == '\'')
5152 ++len;
5153 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005154 s = r = alloc(len);
5155 if (r != NULL)
5156 {
5157 if (function)
5158 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005160 r += 10;
5161 }
5162 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005164 if (str != NULL)
5165 for (p = str; *p != NUL; )
5166 {
5167 if (*p == '\'')
5168 *r++ = '\'';
5169 MB_COPY_CHAR(p, r);
5170 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005172 if (function)
5173 *r++ = ')';
5174 *r++ = NUL;
5175 }
5176 return s;
5177}
5178
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005179#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005180/*
5181 * Convert the string "text" to a floating point number.
5182 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5183 * this always uses a decimal point.
5184 * Returns the length of the text that was consumed.
5185 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005187string2float(
5188 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005189 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005190{
5191 char *s = (char *)text;
5192 float_T f;
5193
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005194 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005195 if (STRNICMP(text, "inf", 3) == 0)
5196 {
5197 *value = INFINITY;
5198 return 3;
5199 }
5200 if (STRNICMP(text, "-inf", 3) == 0)
5201 {
5202 *value = -INFINITY;
5203 return 4;
5204 }
5205 if (STRNICMP(text, "nan", 3) == 0)
5206 {
5207 *value = NAN;
5208 return 3;
5209 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005210 f = strtod(s, &s);
5211 *value = f;
5212 return (int)((char_u *)s - text);
5213}
5214#endif
5215
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005216/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005217 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5218 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005219 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005220 */
5221 int
5222buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5223{
5224 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005225 char_u *t;
5226 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005227
5228 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5229 return -1;
5230
5231 if (lnum > buf->b_ml.ml_line_count)
5232 lnum = buf->b_ml.ml_line_count;
5233
5234 str = ml_get_buf(buf, lnum, FALSE);
5235 if (str == NULL)
5236 return -1;
5237
5238 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005239 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005240
Bram Moolenaar91458462021-01-13 20:08:38 +01005241 // count the number of characters
5242 t = str;
5243 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5244 t += mb_ptr2len(t);
5245
5246 // In insert mode, when the cursor is at the end of a non-empty line,
5247 // byteidx points to the NUL character immediately past the end of the
5248 // string. In this case, add one to the character count.
5249 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5250 count++;
5251
5252 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005253}
5254
5255/*
5256 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005257 * byte index. Works only for loaded buffers. Returns -1 on failure.
5258 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005259 */
5260 int
5261buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5262{
5263 char_u *str;
5264 char_u *t;
5265
5266 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5267 return -1;
5268
5269 if (lnum > buf->b_ml.ml_line_count)
5270 lnum = buf->b_ml.ml_line_count;
5271
5272 str = ml_get_buf(buf, lnum, FALSE);
5273 if (str == NULL)
5274 return -1;
5275
5276 // Convert the character offset to a byte offset
5277 t = str;
5278 while (*t != NUL && --charidx > 0)
5279 t += mb_ptr2len(t);
5280
Bram Moolenaar91458462021-01-13 20:08:38 +01005281 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005282}
5283
5284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005286 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005289var2fpos(
5290 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005291 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005292 int *fnum, // set to fnum for '0, 'A, etc.
5293 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005295 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005297 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005299 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005300 if (varp->v_type == VAR_LIST)
5301 {
5302 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005303 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005304 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005305 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005306
5307 l = varp->vval.v_list;
5308 if (l == NULL)
5309 return NULL;
5310
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005311 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005312 pos.lnum = list_find_nr(l, 0L, &error);
5313 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005314 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005315 if (charcol)
5316 len = (long)mb_charlen(ml_get(pos.lnum));
5317 else
5318 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005319
Bram Moolenaarec65d772020-08-20 22:29:12 +02005320 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005321 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005322 li = list_find(l, 1L);
5323 if (li != NULL && li->li_tv.v_type == VAR_STRING
5324 && li->li_tv.vval.v_string != NULL
5325 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005326 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005327 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005328 }
5329 else
5330 {
5331 pos.col = list_find_nr(l, 1L, &error);
5332 if (error)
5333 return NULL;
5334 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005336 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005337 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005339 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005341 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005342 pos.coladd = list_find_nr(l, 2L, &error);
5343 if (error)
5344 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005345
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005346 return &pos;
5347 }
5348
Bram Moolenaarc5809432021-03-27 21:23:30 +01005349 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5350 return NULL;
5351
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005352 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005353 if (name == NULL)
5354 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005355 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005356 {
5357 pos = curwin->w_cursor;
5358 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005359 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005360 return &pos;
5361 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005363 {
5364 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005365 pos = VIsual;
5366 else
5367 pos = curwin->w_cursor;
5368 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005369 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005370 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005371 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005372 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005374 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5376 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005377 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005378 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 return pp;
5380 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005381
Bram Moolenaara5525202006-03-02 22:52:09 +00005382 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005383
Bram Moolenaar477933c2007-07-17 14:32:23 +00005384 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005385 {
5386 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005388 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005389 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005390 // In silent Ex mode topline is zero, but that's not a valid line
5391 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005392 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005393 return &pos;
5394 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005395 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005396 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005397 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005398 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005399 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005400 return &pos;
5401 }
5402 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005403 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005405 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407 pos.lnum = curbuf->b_ml.ml_line_count;
5408 pos.col = 0;
5409 }
5410 else
5411 {
5412 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005413 if (charcol)
5414 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5415 else
5416 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 return &pos;
5419 }
5420 return NULL;
5421}
5422
5423/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005424 * Convert list in "arg" into a position and optional file number.
5425 * When "fnump" is NULL there is no file number, only 3 items.
5426 * Note that the column is passed on as-is, the caller may want to decrement
5427 * it to use 1 for the first column.
5428 * Return FAIL when conversion is not possible, doesn't check the position for
5429 * validity.
5430 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005431 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005432list2fpos(
5433 typval_T *arg,
5434 pos_T *posp,
5435 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005436 colnr_T *curswantp,
5437 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005438{
5439 list_T *l = arg->vval.v_list;
5440 long i = 0;
5441 long n;
5442
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5444 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005445 if (arg->v_type != VAR_LIST
5446 || l == NULL
5447 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005448 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005449 return FAIL;
5450
5451 if (fnump != NULL)
5452 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005454 if (n < 0)
5455 return FAIL;
5456 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005457 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005458 *fnump = n;
5459 }
5460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005461 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005462 if (n < 0)
5463 return FAIL;
5464 posp->lnum = n;
5465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005467 if (n < 0)
5468 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005469 // If character position is specified, then convert to byte position
5470 if (charcol)
5471 {
5472 buf_T *buf;
5473
5474 // Get the text for the specified line in a loaded buffer
5475 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5476 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5477 return FAIL;
5478
Bram Moolenaar91458462021-01-13 20:08:38 +01005479 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005480 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005481 posp->col = n;
5482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005484 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005485 posp->coladd = 0;
5486 else
5487 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005488
Bram Moolenaar493c1782014-05-28 14:34:46 +02005489 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005490 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005491
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005492 return OK;
5493}
5494
5495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 * Get the length of an environment variable name.
5497 * Advance "arg" to the first character after the name.
5498 * Return 0 for error.
5499 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005500 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005501get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
5503 char_u *p;
5504 int len;
5505
5506 for (p = *arg; vim_isIDc(*p); ++p)
5507 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005508 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 return 0;
5510
5511 len = (int)(p - *arg);
5512 *arg = p;
5513 return len;
5514}
5515
5516/*
5517 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005518 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 * Return 0 if something is wrong.
5520 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005522get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
5524 char_u *p;
5525 int len;
5526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005527 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005529 {
5530 if (*p == ':')
5531 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005532 // "s:" is start of "s:var", but "n:" is not and can be used in
5533 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005534 len = (int)(p - *arg);
5535 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5536 || len > 1)
5537 break;
5538 }
5539 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005540 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 return 0;
5542
5543 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005544 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
5546 return len;
5547}
5548
5549/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005550 * Get the length of the name of a variable or function.
5551 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005553 * Return -1 if curly braces expansion failed.
5554 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 * If the name contains 'magic' {}'s, expand them and return the
5556 * expanded name in an allocated string via 'alias' - caller must free.
5557 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005558 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005559get_name_len(
5560 char_u **arg,
5561 char_u **alias,
5562 int evaluate,
5563 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 char_u *p;
5567 char_u *expr_start;
5568 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005570 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
5572 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5573 && (*arg)[2] == (int)KE_SNR)
5574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005575 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 *arg += 3;
5577 return get_id_len(arg) + 3;
5578 }
5579 len = eval_fname_script(*arg);
5580 if (len > 0)
5581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005582 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 *arg += len;
5584 }
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005587 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005589 p = find_name_end(*arg, &expr_start, &expr_end,
5590 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 if (expr_start != NULL)
5592 {
5593 char_u *temp_string;
5594
5595 if (!evaluate)
5596 {
5597 len += (int)(p - *arg);
5598 *arg = skipwhite(p);
5599 return len;
5600 }
5601
5602 /*
5603 * Include any <SID> etc in the expanded string:
5604 * Thus the -len here.
5605 */
5606 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5607 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005608 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 *alias = temp_string;
5610 *arg = skipwhite(p);
5611 return (int)STRLEN(temp_string);
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005615 // Only give an error when there is something, otherwise it will be
5616 // reported at a higher level.
5617 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005618 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 return len;
5621}
5622
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623/*
5624 * Find the end of a variable or function name, taking care of magic braces.
5625 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5626 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005627 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005628 * Return a pointer to just after the name. Equal to "arg" if there is no
5629 * valid name.
5630 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005631 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005632find_name_end(
5633 char_u *arg,
5634 char_u **expr_start,
5635 char_u **expr_end,
5636 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 int mb_nest = 0;
5639 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005641 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005642 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 *expr_start = NULL;
5647 *expr_end = NULL;
5648 }
5649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005650 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005651 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5652 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005653 return arg;
5654
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 for (p = arg; *p != NUL
5656 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005657 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005658 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005659 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005661 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005662 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005663 if (*p == '\'')
5664 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005665 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005666 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005667 ;
5668 if (*p == NUL)
5669 break;
5670 }
5671 else if (*p == '"')
5672 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005673 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005674 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005675 if (*p == '\\' && p[1] != NUL)
5676 ++p;
5677 if (*p == NUL)
5678 break;
5679 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005680 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005682 // "s:" is start of "s:var", but "n:" is not and can be used in
5683 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005684 len = (int)(p - arg);
5685 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005686 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005687 break;
5688 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 if (*p == '[')
5693 ++br_nest;
5694 else if (*p == ']')
5695 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005697
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005698 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700 if (*p == '{')
5701 {
5702 mb_nest++;
5703 if (expr_start != NULL && *expr_start == NULL)
5704 *expr_start = p;
5705 }
5706 else if (*p == '}')
5707 {
5708 mb_nest--;
5709 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5710 *expr_end = p;
5711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714
5715 return p;
5716}
5717
5718/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005719 * Expands out the 'magic' {}'s in a variable/function name.
5720 * Note that this can call itself recursively, to deal with
5721 * constructs like foo{bar}{baz}{bam}
5722 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5723 * "in_start" ^
5724 * "expr_start" ^
5725 * "expr_end" ^
5726 * "in_end" ^
5727 *
5728 * Returns a new allocated string, which the caller must free.
5729 * Returns NULL for failure.
5730 */
5731 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005732make_expanded_name(
5733 char_u *in_start,
5734 char_u *expr_start,
5735 char_u *expr_end,
5736 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005737{
5738 char_u c1;
5739 char_u *retval = NULL;
5740 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005741
5742 if (expr_end == NULL || in_end == NULL)
5743 return NULL;
5744 *expr_start = NUL;
5745 *expr_end = NUL;
5746 c1 = *in_end;
5747 *in_end = NUL;
5748
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005749 temp_result = eval_to_string(expr_start + 1, FALSE);
5750 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005751 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005752 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5753 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005754 if (retval != NULL)
5755 {
5756 STRCPY(retval, in_start);
5757 STRCAT(retval, temp_result);
5758 STRCAT(retval, expr_end + 1);
5759 }
5760 }
5761 vim_free(temp_result);
5762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005763 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005764 *expr_start = '{';
5765 *expr_end = '}';
5766
5767 if (retval != NULL)
5768 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005769 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005770 if (expr_start != NULL)
5771 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005772 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005773 temp_result = make_expanded_name(retval, expr_start,
5774 expr_end, temp_result);
5775 vim_free(retval);
5776 retval = temp_result;
5777 }
5778 }
5779
5780 return retval;
5781}
5782
5783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005785 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005787 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005788eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005790 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005791}
5792
5793/*
5794 * Return TRUE if character "c" can be used as the first character in a
5795 * variable or function name (excluding '{' and '}').
5796 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005798eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005799{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005800 return ASCII_ISALPHA(c) || c == '_';
5801}
5802
5803/*
5804 * Return TRUE if character "c" can be used as the first character of a
5805 * dictionary key.
5806 */
5807 int
5808eval_isdictc(int c)
5809{
5810 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811}
5812
5813/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005814 * Handle:
5815 * - expr[expr], expr[expr:expr] subscript
5816 * - ".name" lookup
5817 * - function call with Funcref variable: func(expr)
5818 * - method call: var->method()
5819 *
5820 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005821 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005823handle_subscript(
5824 char_u **arg,
5825 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005826 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005827 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005828{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005829 int evaluate = evalarg != NULL
5830 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005831 int ret = OK;
5832 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005833 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005834 int getnext;
5835 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005836
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005837 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005838 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005839 // When at the end of the line and ".name" or "->{" or "->X" follows in
5840 // the next line then consume the line break.
5841 p = eval_next_non_blank(*arg, evalarg, &getnext);
5842 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005843 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005844 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005845 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005846 {
5847 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005848 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005849 check_white = FALSE;
5850 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005851
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005852 if (rettv->v_type == VAR_ANY)
5853 {
5854 char_u *exp_name;
5855 int cc;
5856 int idx;
5857 ufunc_T *ufunc;
5858 type_T *type;
5859
5860 // Found script from "import * as {name}", script item name must
5861 // follow.
5862 if (**arg != '.')
5863 {
5864 if (verbose)
5865 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5866 ret = FAIL;
5867 break;
5868 }
5869 ++*arg;
5870 if (IS_WHITE_OR_NUL(**arg))
5871 {
5872 if (verbose)
5873 emsg(_(e_no_white_space_allowed_after_dot));
5874 ret = FAIL;
5875 break;
5876 }
5877
5878 // isolate the name
5879 exp_name = *arg;
5880 while (eval_isnamec(**arg))
5881 ++*arg;
5882 cc = **arg;
5883 **arg = NUL;
5884
5885 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5886 evalarg->eval_cctx, verbose);
5887 **arg = cc;
5888 *arg = skipwhite(*arg);
5889
5890 if (idx < 0 && ufunc == NULL)
5891 {
5892 ret = FAIL;
5893 break;
5894 }
5895 if (idx >= 0)
5896 {
5897 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5898 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5899
5900 copy_tv(sv->sv_tv, rettv);
5901 }
5902 else
5903 {
5904 rettv->v_type = VAR_FUNC;
5905 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5906 }
5907 }
5908
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005909 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5910 || rettv->v_type == VAR_PARTIAL))
5911 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005912 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005913 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5914 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005915
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005916 // Stop the expression evaluation when immediately aborting on
5917 // error, or when an interrupt occurred or an exception was thrown
5918 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005919 if (aborting())
5920 {
5921 if (ret == OK)
5922 clear_tv(rettv);
5923 ret = FAIL;
5924 }
5925 dict_unref(selfdict);
5926 selfdict = NULL;
5927 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005928 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005929 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005930 if (in_vim9script())
5931 *arg = skipwhite(p + 2);
5932 else
5933 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005934 if (ret == OK)
5935 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005936 if (VIM_ISWHITE(**arg))
5937 {
5938 emsg(_(e_nowhitespace));
5939 ret = FAIL;
5940 }
5941 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005942 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005943 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005944 else
5945 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005946 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005947 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005948 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005949 // "." is ".name" lookup when we found a dict or when evaluating and
5950 // scriptversion is at least 2, where string concatenation is "..".
5951 else if (**arg == '['
5952 || (**arg == '.' && (rettv->v_type == VAR_DICT
5953 || (!evaluate
5954 && (*arg)[1] != '.'
5955 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005956 {
5957 dict_unref(selfdict);
5958 if (rettv->v_type == VAR_DICT)
5959 {
5960 selfdict = rettv->vval.v_dict;
5961 if (selfdict != NULL)
5962 ++selfdict->dv_refcount;
5963 }
5964 else
5965 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005966 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005967 {
5968 clear_tv(rettv);
5969 ret = FAIL;
5970 }
5971 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005972 else
5973 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005974 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005976 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5977 // Don't do this when "Func" is already a partial that was bound
5978 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005979 if (selfdict != NULL
5980 && (rettv->v_type == VAR_FUNC
5981 || (rettv->v_type == VAR_PARTIAL
5982 && (rettv->vval.v_partial->pt_auto
5983 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005984 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005985
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005986 dict_unref(selfdict);
5987 return ret;
5988}
5989
5990/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005991 * Make a copy of an item.
5992 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005993 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5994 * reference to an already copied list/dict can be used.
5995 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005996 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998item_copy(
5999 typval_T *from,
6000 typval_T *to,
6001 int deep,
6002 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006003{
6004 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006005 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006009 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006010 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006011 }
6012 ++recurse;
6013
6014 switch (from->v_type)
6015 {
6016 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006017 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006018 case VAR_STRING:
6019 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006020 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006021 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006022 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006023 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006024 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006025 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006026 copy_tv(from, to);
6027 break;
6028 case VAR_LIST:
6029 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006030 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006031 if (from->vval.v_list == NULL)
6032 to->vval.v_list = NULL;
6033 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6034 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006035 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006036 to->vval.v_list = from->vval.v_list->lv_copylist;
6037 ++to->vval.v_list->lv_refcount;
6038 }
6039 else
6040 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6041 if (to->vval.v_list == NULL)
6042 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006043 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006044 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006046 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006047 case VAR_DICT:
6048 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006049 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006050 if (from->vval.v_dict == NULL)
6051 to->vval.v_dict = NULL;
6052 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6053 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006054 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6056 ++to->vval.v_dict->dv_refcount;
6057 }
6058 else
6059 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6060 if (to->vval.v_dict == NULL)
6061 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006062 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006063 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006064 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006066 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006067 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006068 }
6069 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006070 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006071}
6072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073 void
6074echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6075{
6076 char_u *tofree;
6077 char_u numbuf[NUMBUFLEN];
6078 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6079
6080 if (*atstart)
6081 {
6082 *atstart = FALSE;
6083 // Call msg_start() after eval1(), evaluating the expression
6084 // may cause a message to appear.
6085 if (with_space)
6086 {
6087 // Mark the saved text as finishing the line, so that what
6088 // follows is displayed on a new line when scrolling back
6089 // at the more prompt.
6090 msg_sb_eol();
6091 msg_start();
6092 }
6093 }
6094 else if (with_space)
6095 msg_puts_attr(" ", echo_attr);
6096
6097 if (p != NULL)
6098 for ( ; *p != NUL && !got_int; ++p)
6099 {
6100 if (*p == '\n' || *p == '\r' || *p == TAB)
6101 {
6102 if (*p != TAB && *needclr)
6103 {
6104 // remove any text still there from the command
6105 msg_clr_eos();
6106 *needclr = FALSE;
6107 }
6108 msg_putchar_attr(*p, echo_attr);
6109 }
6110 else
6111 {
6112 if (has_mbyte)
6113 {
6114 int i = (*mb_ptr2len)(p);
6115
6116 (void)msg_outtrans_len_attr(p, i, echo_attr);
6117 p += i - 1;
6118 }
6119 else
6120 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6121 }
6122 }
6123 vim_free(tofree);
6124}
6125
Bram Moolenaare9a41262005-01-15 22:18:47 +00006126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 * ":echo expr1 ..." print each argument separated with a space, add a
6128 * newline at the end.
6129 * ":echon expr1 ..." print each argument plain.
6130 */
6131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006132ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006136 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 int needclr = TRUE;
6138 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006139 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006140 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006141 evalarg_T evalarg;
6142
Bram Moolenaare707c882020-07-01 13:07:37 +02006143 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
6145 if (eap->skip)
6146 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006147 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006149 // If eval1() causes an error message the text from the command may
6150 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006151 need_clr_eos = needclr;
6152
Bram Moolenaar68db9962021-05-09 23:19:22 +02006153 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006154 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 {
6156 /*
6157 * Report the invalid expression unless the expression evaluation
6158 * has been cancelled due to an aborting error, an interrupt, or an
6159 * exception.
6160 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006161 if (!aborting() && did_emsg == did_emsg_before
6162 && called_emsg == called_emsg_before)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006163 semsg(_(e_invexpr2), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006164 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 break;
6166 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006167 need_clr_eos = FALSE;
6168
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006170 {
6171 if (rettv.v_type == VAR_VOID)
6172 {
6173 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6174 break;
6175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006176 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006177 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006179 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 arg = skipwhite(arg);
6181 }
6182 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006183 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
6185 if (eap->skip)
6186 --emsg_skip;
6187 else
6188 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006189 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 if (needclr)
6191 msg_clr_eos();
6192 if (eap->cmdidx == CMD_echo)
6193 msg_end();
6194 }
6195}
6196
6197/*
6198 * ":echohl {name}".
6199 */
6200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006201ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006203 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204}
6205
6206/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006207 * Returns the :echo attribute
6208 */
6209 int
6210get_echo_attr(void)
6211{
6212 return echo_attr;
6213}
6214
6215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 * ":execute expr1 ..." execute the result of an expression.
6217 * ":echomsg expr1 ..." Print a message
6218 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006219 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 * Each gets spaces around each argument and a newline at the end for
6221 * echo commands
6222 */
6223 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006224ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
6226 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006227 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 int ret = OK;
6229 char_u *p;
6230 garray_T ga;
6231 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
6233 ga_init2(&ga, 1, 80);
6234
6235 if (eap->skip)
6236 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006237 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006239 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006240 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
6243 if (!eap->skip)
6244 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006245 char_u buf[NUMBUFLEN];
6246
6247 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006248 {
6249 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6250 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006251 semsg(_(e_using_invalid_value_as_string_str),
6252 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006253 p = NULL;
6254 }
6255 else
6256 p = tv_get_string_buf(&rettv, buf);
6257 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006258 else
6259 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006260 if (p == NULL)
6261 {
6262 clear_tv(&rettv);
6263 ret = FAIL;
6264 break;
6265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 len = (int)STRLEN(p);
6267 if (ga_grow(&ga, len + 2) == FAIL)
6268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006269 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 ret = FAIL;
6271 break;
6272 }
6273 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 ga.ga_len += len;
6277 }
6278
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006279 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 arg = skipwhite(arg);
6281 }
6282
6283 if (ret != FAIL && ga.ga_data != NULL)
6284 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006285 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006287 // Mark the already saved text as finishing the line, so that what
6288 // follows is displayed on a new line when scrolling back at the
6289 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006290 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006291 }
6292
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006294 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006295 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006296 out_flush();
6297 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006298 else if (eap->cmdidx == CMD_echoconsole)
6299 {
6300 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6301 ui_write((char_u *)"\r\n", 2, TRUE);
6302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 else if (eap->cmdidx == CMD_echoerr)
6304 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006305 int save_did_emsg = did_emsg;
6306
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006307 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006308 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 if (!force_abort)
6310 did_emsg = save_did_emsg;
6311 }
6312 else if (eap->cmdidx == CMD_execute)
6313 do_cmdline((char_u *)ga.ga_data,
6314 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6315 }
6316
6317 ga_clear(&ga);
6318
6319 if (eap->skip)
6320 --emsg_skip;
6321
6322 eap->nextcmd = check_nextcmd(arg);
6323}
6324
6325/*
6326 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6327 * "arg" points to the "&" or '+' when called, to "option" when returning.
6328 * Returns NULL when no option name found. Otherwise pointer to the char
6329 * after the option name.
6330 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006331 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006332find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 char_u *p = *arg;
6335
6336 ++p;
6337 if (*p == 'g' && p[1] == ':')
6338 {
6339 *opt_flags = OPT_GLOBAL;
6340 p += 2;
6341 }
6342 else if (*p == 'l' && p[1] == ':')
6343 {
6344 *opt_flags = OPT_LOCAL;
6345 p += 2;
6346 }
6347 else
6348 *opt_flags = 0;
6349
6350 if (!ASCII_ISALPHA(*p))
6351 return NULL;
6352 *arg = p;
6353
6354 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006355 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 else
6357 while (ASCII_ISALPHA(*p))
6358 ++p;
6359 return p;
6360}
6361
6362/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006363 * Display script name where an item was last set.
6364 * Should only be invoked when 'verbose' is non-zero.
6365 */
6366 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006367last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006368{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006369 char_u *p;
6370
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006371 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006372 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006373 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006374 if (p != NULL)
6375 {
6376 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006377 msg_puts(_("\n\tLast set from "));
6378 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006379 if (script_ctx.sc_lnum > 0)
6380 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006381 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006382 msg_outnum((long)script_ctx.sc_lnum);
6383 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006384 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006385 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006386 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006387 }
6388}
6389
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006390#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391
6392/*
6393 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006394 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 * "flags" can be "g" to do a global substitute.
6396 * Returns an allocated string, NULL for error.
6397 */
6398 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006399do_string_sub(
6400 char_u *str,
6401 char_u *pat,
6402 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006403 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006404 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
6406 int sublen;
6407 regmatch_T regmatch;
6408 int i;
6409 int do_all;
6410 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006411 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 garray_T ga;
6413 char_u *ret;
6414 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006415 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006417 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006419 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421 ga_init2(&ga, 1, 200);
6422
6423 do_all = (flags[0] == 'g');
6424
6425 regmatch.rm_ic = p_ic;
6426 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6427 if (regmatch.regprog != NULL)
6428 {
6429 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006430 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6432 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006433 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006434 if (regmatch.startp[0] == regmatch.endp[0])
6435 {
6436 if (zero_width == regmatch.startp[0])
6437 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006438 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006439 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006440 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6441 (size_t)i);
6442 ga.ga_len += i;
6443 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006444 continue;
6445 }
6446 zero_width = regmatch.startp[0];
6447 }
6448
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 /*
6450 * Get some space for a temporary buffer to do the substitution
6451 * into. It will contain:
6452 * - The text up to where the match is.
6453 * - The substituted text.
6454 * - The text after the match.
6455 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006456 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006457 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6459 {
6460 ga_clear(&ga);
6461 break;
6462 }
6463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006464 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 i = (int)(regmatch.startp[0] - tail);
6466 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006467 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006468 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 + ga.ga_len + i, TRUE, TRUE, FALSE);
6470 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006471 tail = regmatch.endp[0];
6472 if (*tail == NUL)
6473 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (!do_all)
6475 break;
6476 }
6477
6478 if (ga.ga_data != NULL)
6479 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6480
Bram Moolenaar473de612013-06-08 18:19:48 +02006481 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 }
6483
6484 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6485 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006486 if (p_cpo == empty_option)
6487 p_cpo = save_cpo;
6488 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006490 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006491 // If it's still empty it was changed and restored, need to restore in
6492 // the complicated way.
6493 if (*p_cpo == NUL)
6494 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006495 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
6498 return ret;
6499}