blob: f75263d9e8c0b8390d24cc88c5baf4b5451f2051 [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 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002361 error_white_both(p, 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 {
2409 error_white_both(p, 1);
2410 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 {
2514 error_white_both(p, 2);
2515 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 {
2640 error_white_both(p, 2);
2641 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)
2738 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002739 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2740 {
2741 error_white_both(p, len);
2742 clear_tv(rettv);
2743 return FAIL;
2744 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002745
Bram Moolenaar696ba232020-07-29 21:20:41 +02002746 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2747 {
2748 semsg(_(e_invexpr2), p);
2749 clear_tv(rettv);
2750 return FAIL;
2751 }
2752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002753 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 if (p[len] == '?')
2755 {
2756 ic = TRUE;
2757 ++len;
2758 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002759 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 else if (p[len] == '#')
2761 {
2762 ic = FALSE;
2763 ++len;
2764 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002765 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002767 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
2769 /*
2770 * Get the second variable.
2771 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002772 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2773 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002774 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002775 clear_tv(rettv);
2776 return FAIL;
2777 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002778 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002779 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002781 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 return FAIL;
2783 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002784 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002785 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002786 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002787
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002788 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002789 {
2790 ret = FAIL;
2791 clear_tv(rettv);
2792 }
2793 else
2794 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002795 clear_tv(&var2);
2796 return ret;
2797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799
2800 return OK;
2801}
2802
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002803/*
2804 * Make a copy of blob "tv1" and append blob "tv2".
2805 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 void
2807eval_addblob(typval_T *tv1, typval_T *tv2)
2808{
2809 blob_T *b1 = tv1->vval.v_blob;
2810 blob_T *b2 = tv2->vval.v_blob;
2811 blob_T *b = blob_alloc();
2812 int i;
2813
2814 if (b != NULL)
2815 {
2816 for (i = 0; i < blob_len(b1); i++)
2817 ga_append(&b->bv_ga, blob_get(b1, i));
2818 for (i = 0; i < blob_len(b2); i++)
2819 ga_append(&b->bv_ga, blob_get(b2, i));
2820
2821 clear_tv(tv1);
2822 rettv_blob_set(tv1, b);
2823 }
2824}
2825
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002826/*
2827 * Make a copy of list "tv1" and append list "tv2".
2828 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 int
2830eval_addlist(typval_T *tv1, typval_T *tv2)
2831{
2832 typval_T var3;
2833
2834 // concatenate Lists
2835 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2836 {
2837 clear_tv(tv1);
2838 clear_tv(tv2);
2839 return FAIL;
2840 }
2841 clear_tv(tv1);
2842 *tv1 = var3;
2843 return OK;
2844}
2845
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846/*
2847 * Handle fourth level expression:
2848 * + number addition
2849 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002850 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002851 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 *
2853 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002854 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 *
2856 * Return OK or FAIL.
2857 */
2858 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002859eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 /*
2862 * Get the first variable.
2863 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002864 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 return FAIL;
2866
2867 /*
2868 * Repeat computing, until no '+', '-' or '.' is following.
2869 */
2870 for (;;)
2871 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002872 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002873 int getnext;
2874 char_u *p;
2875 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002876 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877 int concat;
2878 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002879 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002881 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002882 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002883 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002884 p = eval_next_non_blank(*arg, evalarg, &getnext);
2885 op = *p;
2886 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002887 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2888 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002890 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2891 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002892
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002893 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002894 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002895 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002896 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002897 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002898 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002899 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002900 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002901 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002902 clear_tv(rettv);
2903 return FAIL;
2904 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002905 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002906 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002907 if ((op != '+' || (rettv->v_type != VAR_LIST
2908 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002909#ifdef FEAT_FLOAT
2910 && (op == '.' || rettv->v_type != VAR_FLOAT)
2911#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002912 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002913 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002914 int error = FALSE;
2915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002916 // For "list + ...", an illegal use of the first operand as
2917 // a number cannot be determined before evaluating the 2nd
2918 // operand: if this is also a list, all is ok.
2919 // For "something . ...", "something - ..." or "non-list + ...",
2920 // we know that the first operand needs to be a string or number
2921 // without evaluating the 2nd operand. So check before to avoid
2922 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002923 if (op != '.')
2924 tv_get_number_chk(rettv, &error);
2925 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002926 {
2927 clear_tv(rettv);
2928 return FAIL;
2929 }
2930 }
2931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 /*
2933 * Get the second variable.
2934 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002935 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002936 {
mityu89dcb4d2021-05-28 13:50:17 +02002937 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002938 clear_tv(rettv);
2939 return FAIL;
2940 }
2941 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002942 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 return FAIL;
2946 }
2947
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002948 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
2950 /*
2951 * Compute the result.
2952 */
2953 if (op == '.')
2954 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002955 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2956 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002957 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958
Bram Moolenaar2e086612020-08-25 22:37:48 +02002959 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002960 || var2.v_type == VAR_CHANNEL
2961 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002962 semsg(_(e_using_invalid_value_as_string_str),
2963 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002964#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002965 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002966 {
2967 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2968 var2.vval.v_float);
2969 s2 = buf2;
2970 }
2971#endif
2972 else
2973 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002974 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 {
2976 clear_tv(rettv);
2977 clear_tv(&var2);
2978 return FAIL;
2979 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002980 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002981 clear_tv(rettv);
2982 rettv->v_type = VAR_STRING;
2983 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002985 else if (op == '+' && rettv->v_type == VAR_BLOB
2986 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002988 else if (op == '+' && rettv->v_type == VAR_LIST
2989 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002990 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002992 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 else
2995 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002996 int error = FALSE;
2997 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002998#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999 float_T f1 = 0, f2 = 0;
3000
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003001 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003 f1 = rettv->vval.v_float;
3004 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003005 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 else
3007#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003009 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010 if (error)
3011 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003012 // This can only happen for "list + non-list" or
3013 // "blob + non-blob". For "non-list + ..." or
3014 // "something - ...", we returned before evaluating the
3015 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003017 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003018 return FAIL;
3019 }
3020#ifdef FEAT_FLOAT
3021 if (var2.v_type == VAR_FLOAT)
3022 f1 = n1;
3023#endif
3024 }
3025#ifdef FEAT_FLOAT
3026 if (var2.v_type == VAR_FLOAT)
3027 {
3028 f2 = var2.vval.v_float;
3029 n2 = 0;
3030 }
3031 else
3032#endif
3033 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003034 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003035 if (error)
3036 {
3037 clear_tv(rettv);
3038 clear_tv(&var2);
3039 return FAIL;
3040 }
3041#ifdef FEAT_FLOAT
3042 if (rettv->v_type == VAR_FLOAT)
3043 f2 = n2;
3044#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047
3048#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003049 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3051 {
3052 if (op == '+')
3053 f1 = f1 + f2;
3054 else
3055 f1 = f1 - f2;
3056 rettv->v_type = VAR_FLOAT;
3057 rettv->vval.v_float = f1;
3058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060#endif
3061 {
3062 if (op == '+')
3063 n1 = n1 + n2;
3064 else
3065 n1 = n1 - n2;
3066 rettv->v_type = VAR_NUMBER;
3067 rettv->vval.v_number = n1;
3068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072 }
3073 return OK;
3074}
3075
3076/*
3077 * Handle fifth level expression:
3078 * * number multiplication
3079 * / number division
3080 * % number modulo
3081 *
3082 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003083 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 *
3085 * Return OK or FAIL.
3086 */
3087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003088eval6(
3089 char_u **arg,
3090 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003091 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003092 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003094#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003095 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 /*
3099 * Get the first variable.
3100 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003101 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 return FAIL;
3103
3104 /*
3105 * Repeat computing, until no '*', '/' or '%' is following.
3106 */
3107 for (;;)
3108 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003109 int evaluate;
3110 int getnext;
3111 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003112 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003113 int op;
3114 varnumber_T n1, n2;
3115#ifdef FEAT_FLOAT
3116 float_T f1, f2;
3117#endif
3118 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003119
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003120 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003121 p = eval_next_non_blank(*arg, evalarg, &getnext);
3122 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003123 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003125
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003126 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003127 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003128 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003129 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003130 {
3131 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3132 {
3133 error_white_both(p, 1);
3134 clear_tv(rettv);
3135 return FAIL;
3136 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003137 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003140#ifdef FEAT_FLOAT
3141 f1 = 0;
3142 f2 = 0;
3143#endif
3144 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003145 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147#ifdef FEAT_FLOAT
3148 if (rettv->v_type == VAR_FLOAT)
3149 {
3150 f1 = rettv->vval.v_float;
3151 use_float = TRUE;
3152 n1 = 0;
3153 }
3154 else
3155#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003156 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003157 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003158 if (error)
3159 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161 else
3162 n1 = 0;
3163
3164 /*
3165 * Get the second variable.
3166 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003167 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3168 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003169 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003170 clear_tv(rettv);
3171 return FAIL;
3172 }
3173 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003174 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 return FAIL;
3176
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003177 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179#ifdef FEAT_FLOAT
3180 if (var2.v_type == VAR_FLOAT)
3181 {
3182 if (!use_float)
3183 {
3184 f1 = n1;
3185 use_float = TRUE;
3186 }
3187 f2 = var2.vval.v_float;
3188 n2 = 0;
3189 }
3190 else
3191#endif
3192 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003193 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194 clear_tv(&var2);
3195 if (error)
3196 return FAIL;
3197#ifdef FEAT_FLOAT
3198 if (use_float)
3199 f2 = n2;
3200#endif
3201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
3203 /*
3204 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003205 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207#ifdef FEAT_FLOAT
3208 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210 if (op == '*')
3211 f1 = f1 * f2;
3212 else if (op == '/')
3213 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003214# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003215 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003216 if (f2 == 0.0)
3217 {
3218 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003219 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003220 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003221 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003222 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003223 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003224 }
3225 else
3226 f1 = f1 / f2;
3227# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003228 // We rely on the floating point library to handle divide
3229 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003230 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003231# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003234 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003236 return FAIL;
3237 }
3238 rettv->v_type = VAR_FLOAT;
3239 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 }
3241 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003244 int failed = FALSE;
3245
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003246 if (op == '*')
3247 n1 = n1 * n2;
3248 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003249 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003251 n1 = num_modulus(n1, n2, &failed);
3252 if (failed)
3253 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003254
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003255 rettv->v_type = VAR_NUMBER;
3256 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 }
3259 }
3260
3261 return OK;
3262}
3263
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003264/*
3265 * Handle a type cast before a base level expression.
3266 * "arg" must point to the first non-white of the expression.
3267 * "arg" is advanced to just after the recognized expression.
3268 * Return OK or FAIL.
3269 */
3270 static int
3271eval7t(
3272 char_u **arg,
3273 typval_T *rettv,
3274 evalarg_T *evalarg,
3275 int want_string) // after "." operator
3276{
3277 type_T *want_type = NULL;
3278 garray_T type_list; // list of pointers to allocated types
3279 int res;
3280 int evaluate = evalarg == NULL ? 0
3281 : (evalarg->eval_flags & EVAL_EVALUATE);
3282
3283 // Recognize <type> in Vim9 script only.
3284 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3285 {
3286 ++*arg;
3287 ga_init2(&type_list, sizeof(type_T *), 10);
3288 want_type = parse_type(arg, &type_list, TRUE);
3289 if (want_type == NULL && (evaluate || **arg != '>'))
3290 {
3291 clear_type_list(&type_list);
3292 return FAIL;
3293 }
3294
3295 if (**arg != '>')
3296 {
3297 if (*skipwhite(*arg) == '>')
3298 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3299 else
3300 emsg(_(e_missing_gt));
3301 clear_type_list(&type_list);
3302 return FAIL;
3303 }
3304 ++*arg;
3305 *arg = skipwhite_and_linebreak(*arg, evalarg);
3306 }
3307
3308 res = eval7(arg, rettv, evalarg, want_string);
3309
3310 if (want_type != NULL && evaluate)
3311 {
3312 if (res == OK)
3313 {
3314 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3315
3316 if (!equal_type(want_type, actual))
3317 {
3318 if (want_type == &t_bool && actual != &t_bool
3319 && (actual->tt_flags & TTFLAG_BOOL_OK))
3320 {
3321 int n = tv2bool(rettv);
3322
3323 // can use "0" and "1" for boolean in some places
3324 clear_tv(rettv);
3325 rettv->v_type = VAR_BOOL;
3326 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3327 }
3328 else
3329 {
3330 where_T where;
3331
3332 where.wt_index = 0;
3333 where.wt_variable = TRUE;
3334 res = check_type(want_type, actual, TRUE, where);
3335 }
3336 }
3337 }
3338 clear_type_list(&type_list);
3339 }
3340
3341 return res;
3342}
3343
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003344 int
3345eval_leader(char_u **arg, int vim9)
3346{
3347 char_u *s = *arg;
3348 char_u *p = *arg;
3349
3350 while (*p == '!' || *p == '-' || *p == '+')
3351 {
3352 char_u *n = skipwhite(p + 1);
3353
3354 // ++, --, -+ and +- are not accepted in Vim9 script
3355 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3356 {
3357 semsg(_(e_invexpr2), s);
3358 return FAIL;
3359 }
3360 p = n;
3361 }
3362 *arg = p;
3363 return OK;
3364}
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
3367 * Handle sixth level expression:
3368 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003369 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003370 * "string" string constant
3371 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 * &option-name option value
3373 * @r register contents
3374 * identifier variable value
3375 * function() function call
3376 * $VAR environment variable
3377 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003378 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003380 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003381 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 *
3383 * Also handle:
3384 * ! in front logical NOT
3385 * - in front unary minus
3386 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003387 * trailing [] subscript in String or List
3388 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003389 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 *
3391 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003392 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 *
3394 * Return OK or FAIL.
3395 */
3396 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003397eval7(
3398 char_u **arg,
3399 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003400 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003403 int evaluate = evalarg != NULL
3404 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 int len;
3406 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 char_u *start_leader, *end_leader;
3408 int ret = OK;
3409 char_u *alias;
3410
3411 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003412 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003413 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003415 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003418 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 */
3420 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003421 if (eval_leader(arg, in_vim9script()) == FAIL)
3422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 end_leader = *arg;
3424
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003425 if (**arg == '.' && (!isdigit(*(*arg + 1))
3426#ifdef FEAT_FLOAT
3427 || current_sctx.sc_version < 2
3428#endif
3429 ))
3430 {
3431 semsg(_(e_invexpr2), *arg);
3432 ++*arg;
3433 return FAIL;
3434 }
3435
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 switch (**arg)
3437 {
3438 /*
3439 * Number constant.
3440 */
3441 case '0':
3442 case '1':
3443 case '2':
3444 case '3':
3445 case '4':
3446 case '5':
3447 case '6':
3448 case '7':
3449 case '8':
3450 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003451 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003452
3453 // Apply prefixed "-" and "+" now. Matters especially when
3454 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003455 if (ret == OK && evaluate && end_leader > start_leader
3456 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003457 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 break;
3459
3460 /*
3461 * String constant: "string".
3462 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003463 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 break;
3465
3466 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003467 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003469 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003470 break;
3471
3472 /*
3473 * List: [expr, expr]
3474 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003475 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 break;
3477
3478 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003479 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003480 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003481 case '#': if (in_vim9script())
3482 {
3483 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3484 }
3485 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003486 {
3487 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003488 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003489 }
3490 else
3491 ret = NOTDONE;
3492 break;
3493
3494 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003495 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003496 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003497 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003498 case '{': if (in_vim9script())
3499 ret = NOTDONE;
3500 else
3501 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003502 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003503 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003504 break;
3505
3506 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003507 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003509 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 break;
3511
3512 /*
3513 * Environment variable: $VAR.
3514 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003515 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 break;
3517
3518 /*
3519 * Register contents: @r.
3520 */
3521 case '@': ++*arg;
3522 if (evaluate)
3523 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003524 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3525 semsg(_(e_syntax_error_at_str), *arg);
3526 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3527 emsg_invreg(**arg);
3528 else
3529 {
3530 rettv->v_type = VAR_STRING;
3531 rettv->vval.v_string = get_reg_contents(**arg,
3532 GREG_EXPR_SRC);
3533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 if (**arg != NUL)
3536 ++*arg;
3537 break;
3538
3539 /*
3540 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003541 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003543 case '(': ret = NOTDONE;
3544 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003545 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003546 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003547 if (ret == OK && evaluate)
3548 {
3549 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3550
3551 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003552 if (compile_def_function(ufunc,
3553 TRUE, PROFILING(ufunc), NULL) == FAIL)
3554 {
3555 clear_tv(rettv);
3556 ret = FAIL;
3557 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003558 }
3559 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003560 if (ret == NOTDONE)
3561 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003562 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003563 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003564
Bram Moolenaar9215f012020-06-27 21:18:00 +02003565 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003566 if (**arg == ')')
3567 ++*arg;
3568 else if (ret == OK)
3569 {
3570 emsg(_(e_missing_close));
3571 clear_tv(rettv);
3572 ret = FAIL;
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 break;
3576
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 break;
3579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580
3581 if (ret == NOTDONE)
3582 {
3583 /*
3584 * Must be a variable or function name.
3585 * Can also be a curly-braces kind of name: {expr}.
3586 */
3587 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003588 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 if (alias != NULL)
3590 s = alias;
3591
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003592 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593 ret = FAIL;
3594 else
3595 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003596 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3597
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003598 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003599 {
3600 emsg(_(e_cannot_use_underscore_here));
3601 ret = FAIL;
3602 }
3603 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003604 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003605 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003606 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003607 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003608 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003609 else if (flags & EVAL_CONSTANT)
3610 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003612 {
3613 // get the value of "true", "false" or a variable
3614 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3615 {
3616 rettv->v_type = VAR_BOOL;
3617 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003618 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003619 }
3620 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003621 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003622 {
3623 rettv->v_type = VAR_BOOL;
3624 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003625 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003626 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003627 else if (len == 4 && in_vim9script()
3628 && STRNCMP(s, "null", 4) == 0)
3629 {
3630 rettv->v_type = VAR_SPECIAL;
3631 rettv->vval.v_number = VVAL_NULL;
3632 ret = OK;
3633 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003634 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003635 ret = eval_variable(s, len, rettv, NULL,
3636 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003637 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003638 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003639 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003640 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003641 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003642 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003643 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003645 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003646 }
3647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003648 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3649 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003650 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003651 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
3653 /*
3654 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3655 */
3656 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003657 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003658 return ret;
3659}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003660
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003661/*
3662 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003663 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003664 * Adjusts "end_leaderp" until it is at "start_leader".
3665 */
3666 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003667eval7_leader(
3668 typval_T *rettv,
3669 int numeric_only,
3670 char_u *start_leader,
3671 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003672{
3673 char_u *end_leader = *end_leaderp;
3674 int ret = OK;
3675 int error = FALSE;
3676 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003677 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003678#ifdef FEAT_FLOAT
3679 float_T f = 0.0;
3680
3681 if (rettv->v_type == VAR_FLOAT)
3682 f = rettv->vval.v_float;
3683 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003684#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003685 {
3686 while (VIM_ISWHITE(end_leader[-1]))
3687 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003688 if (in_vim9script() && end_leader[-1] == '!')
3689 val = tv2bool(rettv);
3690 else
3691 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003692 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003693 if (error)
3694 {
3695 clear_tv(rettv);
3696 ret = FAIL;
3697 }
3698 else
3699 {
3700 while (end_leader > start_leader)
3701 {
3702 --end_leader;
3703 if (*end_leader == '!')
3704 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003705 if (numeric_only)
3706 {
3707 ++end_leader;
3708 break;
3709 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003710#ifdef FEAT_FLOAT
3711 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003712 {
3713 if (in_vim9script())
3714 {
3715 rettv->v_type = VAR_BOOL;
3716 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3717 }
3718 else
3719 f = !f;
3720 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003721 else
3722#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003723 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003724 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003725 type = VAR_BOOL;
3726 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003727 }
3728 else if (*end_leader == '-')
3729 {
3730#ifdef FEAT_FLOAT
3731 if (rettv->v_type == VAR_FLOAT)
3732 f = -f;
3733 else
3734#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003735 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003736 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003737 type = VAR_NUMBER;
3738 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003739 }
3740 }
3741#ifdef FEAT_FLOAT
3742 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003745 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003747 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003748#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003749 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003750 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003751 if (in_vim9script())
3752 rettv->v_type = type;
3753 else
3754 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003755 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003758 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 return ret;
3760}
3761
3762/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003763 * Call the function referred to in "rettv".
3764 */
3765 static int
3766call_func_rettv(
3767 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003768 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003769 typval_T *rettv,
3770 int evaluate,
3771 dict_T *selfdict,
3772 typval_T *basetv)
3773{
3774 partial_T *pt = NULL;
3775 funcexe_T funcexe;
3776 typval_T functv;
3777 char_u *s;
3778 int ret;
3779
3780 // need to copy the funcref so that we can clear rettv
3781 if (evaluate)
3782 {
3783 functv = *rettv;
3784 rettv->v_type = VAR_UNKNOWN;
3785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003787 if (functv.v_type == VAR_PARTIAL)
3788 {
3789 pt = functv.vval.v_partial;
3790 s = partial_name(pt);
3791 }
3792 else
3793 s = functv.vval.v_string;
3794 }
3795 else
3796 s = (char_u *)"";
3797
Bram Moolenaara80faa82020-04-12 19:37:17 +02003798 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003799 funcexe.firstline = curwin->w_cursor.lnum;
3800 funcexe.lastline = curwin->w_cursor.lnum;
3801 funcexe.evaluate = evaluate;
3802 funcexe.partial = pt;
3803 funcexe.selfdict = selfdict;
3804 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003805 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003807 // Clear the funcref afterwards, so that deleting it while
3808 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003809 if (evaluate)
3810 clear_tv(&functv);
3811
3812 return ret;
3813}
3814
3815/*
3816 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003817 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003818 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3819 */
3820 static int
3821eval_lambda(
3822 char_u **arg,
3823 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003824 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003825 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003826{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003827 int evaluate = evalarg != NULL
3828 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003829 typval_T base = *rettv;
3830 int ret;
3831
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003832 rettv->v_type = VAR_UNKNOWN;
3833
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003834 if (**arg == '{')
3835 {
3836 // ->{lambda}()
3837 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3838 }
3839 else
3840 {
3841 // ->(lambda)()
3842 ++*arg;
3843 ret = eval1(arg, rettv, evalarg);
3844 *arg = skipwhite_and_linebreak(*arg, evalarg);
3845 if (**arg != ')')
3846 {
3847 emsg(_(e_missing_close));
3848 ret = FAIL;
3849 }
3850 ++*arg;
3851 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003852 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003853 return FAIL;
3854 else if (**arg != '(')
3855 {
3856 if (verbose)
3857 {
3858 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003859 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003860 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003862 }
3863 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003864 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003865 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003866 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003867 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003868
3869 // Clear the funcref afterwards, so that deleting it while
3870 // evaluating the arguments is possible (see test55).
3871 if (evaluate)
3872 clear_tv(&base);
3873
3874 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003875}
3876
3877/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003878 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003879 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003880 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3881 */
3882 static int
3883eval_method(
3884 char_u **arg,
3885 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003886 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003887 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003888{
3889 char_u *name;
3890 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003891 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003892 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003893 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003894 int evaluate = evalarg != NULL
3895 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003896
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003897 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003898
Bram Moolenaarac92e252019-08-03 21:58:38 +02003899 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003900 len = get_name_len(arg, &alias, evaluate, TRUE);
3901 if (alias != NULL)
3902 name = alias;
3903
3904 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003905 {
3906 if (verbose)
3907 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003908 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003909 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003910 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003911 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003912 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003913 if (**arg != '(')
3914 {
3915 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003917 ret = FAIL;
3918 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003919 else if (VIM_ISWHITE((*arg)[-1]))
3920 {
3921 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003922 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003923 ret = FAIL;
3924 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003925 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003926 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003927 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003928 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003929
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003930 // Clear the funcref afterwards, so that deleting it while
3931 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003932 if (evaluate)
3933 clear_tv(&base);
3934
Bram Moolenaarac92e252019-08-03 21:58:38 +02003935 return ret;
3936}
3937
3938/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003939 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3940 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003941 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3942 */
3943 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003944eval_index(
3945 char_u **arg,
3946 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003947 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003948 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003949{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003950 int evaluate = evalarg != NULL
3951 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003952 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003954 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003955 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003956 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003957 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003958
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003959 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3960 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003962 init_tv(&var1);
3963 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003964 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003966 /*
3967 * dict.name
3968 */
3969 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003970 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003971 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003972 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003973 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003974 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003975 }
3976 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003978 /*
3979 * something[idx]
3980 *
3981 * Get the (first) variable from inside the [].
3982 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003983 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003984 if (**arg == ':')
3985 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003986 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003987 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003988 else if (vim9 && **arg == ':')
3989 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003990 semsg(_(e_white_space_required_before_and_after_str_at_str),
3991 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003992 clear_tv(&var1);
3993 return FAIL;
3994 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003995 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003997#ifdef FEAT_FLOAT
3998 // allow for indexing with float
3999 if (vim9 && rettv->v_type == VAR_DICT
4000 && var1.v_type == VAR_FLOAT)
4001 {
4002 var1.vval.v_string = typval_tostring(&var1, TRUE);
4003 var1.v_type = VAR_STRING;
4004 }
4005#endif
4006 if (tv_get_string_chk(&var1) == NULL)
4007 {
4008 // not a number or string
4009 clear_tv(&var1);
4010 return FAIL;
4011 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004013
4014 /*
4015 * Get the second variable from inside the [:].
4016 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004017 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004018 if (**arg == ':')
4019 {
4020 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004021 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004022 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004023 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004024 semsg(_(e_white_space_required_before_and_after_str_at_str),
4025 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004026 if (!empty1)
4027 clear_tv(&var1);
4028 return FAIL;
4029 }
4030 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 if (**arg == ']')
4032 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004033 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004034 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004035 if (!empty1)
4036 clear_tv(&var1);
4037 return FAIL;
4038 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004039 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004041 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004042 if (!empty1)
4043 clear_tv(&var1);
4044 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004045 return FAIL;
4046 }
4047 }
4048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004049 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004050 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004051 if (**arg != ']')
4052 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004053 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004054 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004055 clear_tv(&var1);
4056 if (range)
4057 clear_tv(&var2);
4058 return FAIL;
4059 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004060 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004061 }
4062
4063 if (evaluate)
4064 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004065 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004066 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004067 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004068
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004069 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004071 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004072 clear_tv(&var2);
4073 return res;
4074 }
4075 return OK;
4076}
4077
4078/*
4079 * Check if "rettv" can have an [index] or [sli:ce]
4080 */
4081 int
4082check_can_index(typval_T *rettv, int evaluate, int verbose)
4083{
4084 switch (rettv->v_type)
4085 {
4086 case VAR_FUNC:
4087 case VAR_PARTIAL:
4088 if (verbose)
4089 emsg(_("E695: Cannot index a Funcref"));
4090 return FAIL;
4091 case VAR_FLOAT:
4092#ifdef FEAT_FLOAT
4093 if (verbose)
4094 emsg(_(e_float_as_string));
4095 return FAIL;
4096#endif
4097 case VAR_BOOL:
4098 case VAR_SPECIAL:
4099 case VAR_JOB:
4100 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004101 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004102 if (verbose)
4103 emsg(_(e_cannot_index_special_variable));
4104 return FAIL;
4105 case VAR_UNKNOWN:
4106 case VAR_ANY:
4107 case VAR_VOID:
4108 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004109 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004110 emsg(_(e_cannot_index_special_variable));
4111 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004112 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004113 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004114
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004115 case VAR_STRING:
4116 case VAR_LIST:
4117 case VAR_DICT:
4118 case VAR_BLOB:
4119 break;
4120 case VAR_NUMBER:
4121 if (in_vim9script())
4122 emsg(_(e_cannot_index_number));
4123 break;
4124 }
4125 return OK;
4126}
4127
4128/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004129 * slice() function
4130 */
4131 void
4132f_slice(typval_T *argvars, typval_T *rettv)
4133{
4134 if (check_can_index(argvars, TRUE, FALSE) == OK)
4135 {
4136 copy_tv(argvars, rettv);
4137 eval_index_inner(rettv, TRUE, argvars + 1,
4138 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4139 TRUE, NULL, 0, FALSE);
4140 }
4141}
4142
4143/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004144 * Apply index or range to "rettv".
4145 * "var1" is the first index, NULL for [:expr].
4146 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004147 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4148 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4150 */
4151 int
4152eval_index_inner(
4153 typval_T *rettv,
4154 int is_range,
4155 typval_T *var1,
4156 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004157 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 char_u *key,
4159 int keylen,
4160 int verbose)
4161{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004162 varnumber_T n1, n2 = 0;
4163 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004164
4165 n1 = 0;
4166 if (var1 != NULL && rettv->v_type != VAR_DICT)
4167 n1 = tv_get_number(var1);
4168
4169 if (is_range)
4170 {
4171 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173 if (verbose)
4174 emsg(_(e_cannot_slice_dictionary));
4175 return FAIL;
4176 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004177 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004178 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004179 else
4180 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004181 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004182
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004183 switch (rettv->v_type)
4184 {
4185 case VAR_UNKNOWN:
4186 case VAR_ANY:
4187 case VAR_VOID:
4188 case VAR_FUNC:
4189 case VAR_PARTIAL:
4190 case VAR_FLOAT:
4191 case VAR_BOOL:
4192 case VAR_SPECIAL:
4193 case VAR_JOB:
4194 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004195 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004196 break; // not evaluating, skipping over subscript
4197
4198 case VAR_NUMBER:
4199 case VAR_STRING:
4200 {
4201 char_u *s = tv_get_string(rettv);
4202
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004203 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004204 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004205 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004206 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004207 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004208 else
4209 s = char_from_string(s, n1);
4210 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004211 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004213 // The resulting variable is a substring. If the indexes
4214 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004215 if (n1 < 0)
4216 {
4217 n1 = len + n1;
4218 if (n1 < 0)
4219 n1 = 0;
4220 }
4221 if (n2 < 0)
4222 n2 = len + n2;
4223 else if (n2 >= len)
4224 n2 = len;
4225 if (n1 >= len || n2 < 0 || n1 > n2)
4226 s = NULL;
4227 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004228 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004229 }
4230 else
4231 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004232 // The resulting variable is a string of a single
4233 // character. If the index is too big or negative the
4234 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235 if (n1 >= len || n1 < 0)
4236 s = NULL;
4237 else
4238 s = vim_strnsave(s + n1, 1);
4239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
4241 rettv->v_type = VAR_STRING;
4242 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004243 }
4244 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004245
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004246 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004247 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4248 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004249 break;
4250
4251 case VAR_LIST:
4252 if (var1 == NULL)
4253 n1 = 0;
4254 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004255 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004256 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004257 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 return FAIL;
4259 break;
4260
4261 case VAR_DICT:
4262 {
4263 dictitem_T *item;
4264 typval_T tmp;
4265
4266 if (key == NULL)
4267 {
4268 key = tv_get_string_chk(var1);
4269 if (key == NULL)
4270 return FAIL;
4271 }
4272
4273 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4274
4275 if (item == NULL && verbose)
4276 semsg(_(e_dictkey), key);
4277 if (item == NULL)
4278 return FAIL;
4279
4280 copy_tv(&item->di_tv, &tmp);
4281 clear_tv(rettv);
4282 *rettv = tmp;
4283 }
4284 break;
4285 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004286 return OK;
4287}
4288
4289/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004290 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004291 */
4292 char_u *
4293partial_name(partial_T *pt)
4294{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004295 if (pt != NULL)
4296 {
4297 if (pt->pt_name != NULL)
4298 return pt->pt_name;
4299 if (pt->pt_func != NULL)
4300 return pt->pt_func->uf_name;
4301 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004302 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004303}
4304
Bram Moolenaarddecc252016-04-06 22:59:37 +02004305 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004306partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004307{
4308 int i;
4309
4310 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004311 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004312 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004313 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004314 if (pt->pt_name != NULL)
4315 {
4316 func_unref(pt->pt_name);
4317 vim_free(pt->pt_name);
4318 }
4319 else
4320 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004321
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004322 // Decrease the reference count for the context of a closure. If down
4323 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004324 if (pt->pt_funcstack != NULL)
4325 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004326 --pt->pt_funcstack->fs_refcount;
4327 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004328 }
4329
Bram Moolenaarddecc252016-04-06 22:59:37 +02004330 vim_free(pt);
4331}
4332
4333/*
4334 * Unreference a closure: decrement the reference count and free it when it
4335 * becomes zero.
4336 */
4337 void
4338partial_unref(partial_T *pt)
4339{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004340 if (pt != NULL)
4341 {
4342 if (--pt->pt_refcount <= 0)
4343 partial_free(pt);
4344
4345 // If the reference count goes down to one, the funcstack may be the
4346 // only reference and can be freed if no other partials reference it.
4347 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4348 funcstack_check_refcount(pt->pt_funcstack);
4349 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004350}
4351
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004352/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004353 * Return the next (unique) copy ID.
4354 * Used for serializing nested structures.
4355 */
4356 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004357get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004358{
4359 current_copyID += COPYID_INC;
4360 return current_copyID;
4361}
4362
4363/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004364 * Garbage collection for lists and dictionaries.
4365 *
4366 * We use reference counts to be able to free most items right away when they
4367 * are no longer used. But for composite items it's possible that it becomes
4368 * unused while the reference count is > 0: When there is a recursive
4369 * reference. Example:
4370 * :let l = [1, 2, 3]
4371 * :let d = {9: l}
4372 * :let l[1] = d
4373 *
4374 * Since this is quite unusual we handle this with garbage collection: every
4375 * once in a while find out which lists and dicts are not referenced from any
4376 * variable.
4377 *
4378 * Here is a good reference text about garbage collection (refers to Python
4379 * but it applies to all reference-counting mechanisms):
4380 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004381 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004382
4383/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004384 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004385 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004386 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004387 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004388 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004389garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004390{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004391 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004392 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004393 buf_T *buf;
4394 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004395 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004396 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004397
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004398 if (!testing)
4399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004400 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004401 want_garbage_collect = FALSE;
4402 may_garbage_collect = FALSE;
4403 garbage_collect_at_exit = FALSE;
4404 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004405
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004406 // The execution stack can grow big, limit the size.
4407 if (exestack.ga_maxlen - exestack.ga_len > 500)
4408 {
4409 size_t new_len;
4410 char_u *pp;
4411 int n;
4412
4413 // Keep 150% of the current size, with a minimum of the growth size.
4414 n = exestack.ga_len / 2;
4415 if (n < exestack.ga_growsize)
4416 n = exestack.ga_growsize;
4417
4418 // Don't make it bigger though.
4419 if (exestack.ga_len + n < exestack.ga_maxlen)
4420 {
4421 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4422 pp = vim_realloc(exestack.ga_data, new_len);
4423 if (pp == NULL)
4424 return FAIL;
4425 exestack.ga_maxlen = exestack.ga_len + n;
4426 exestack.ga_data = pp;
4427 }
4428 }
4429
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004430 // We advance by two because we add one for items referenced through
4431 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004432 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004433
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004434 /*
4435 * 1. Go through all accessible variables and mark all lists and dicts
4436 * with copyID.
4437 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004439 // Don't free variables in the previous_funccal list unless they are only
4440 // referenced through previous_funccal. This must be first, because if
4441 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004444 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004445 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004447 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004448 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004449 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4450 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004452 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004453 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004454 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4455 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004456 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4458 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004459#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004460 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004461 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4462 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004463 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004464 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004465 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4466 NULL, NULL);
4467#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004469 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004470 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004471 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4472 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004473 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004474 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004476 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004480 abort = abort || set_ref_in_functions(copyID);
4481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004486 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004487
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004488 // callbacks in buffers
4489 abort = abort || set_ref_in_buffers(copyID);
4490
Bram Moolenaar1dced572012-04-05 16:54:08 +02004491#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004492 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004493#endif
4494
Bram Moolenaardb913952012-06-29 12:54:53 +02004495#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004496 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004497#endif
4498
4499#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004500 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004501#endif
4502
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004504 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004505 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004506#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004507#ifdef FEAT_NETBEANS_INTG
4508 abort = abort || set_ref_in_nb_channel(copyID);
4509#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004510
Bram Moolenaare3188e22016-05-31 21:13:04 +02004511#ifdef FEAT_TIMERS
4512 abort = abort || set_ref_in_timer(copyID);
4513#endif
4514
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004515#ifdef FEAT_QUICKFIX
4516 abort = abort || set_ref_in_quickfix(copyID);
4517#endif
4518
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004519#ifdef FEAT_TERMINAL
4520 abort = abort || set_ref_in_term(copyID);
4521#endif
4522
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004523#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004524 abort = abort || set_ref_in_popups(copyID);
4525#endif
4526
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004527 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004528 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 /*
4530 * 2. Free lists and dictionaries that are not referenced.
4531 */
4532 did_free = free_unref_items(copyID);
4533
4534 /*
4535 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004536 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004539 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004540 else if (p_verbose > 0)
4541 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004542 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004544
4545 return did_free;
4546}
4547
4548/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004549 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004550 */
4551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004552free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004553{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004554 int did_free = FALSE;
4555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004556 // Let all "free" functions know that we are here. This means no
4557 // dictionaries, lists, channels or jobs are to be freed, because we will
4558 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004559 in_free_unref_items = TRUE;
4560
4561 /*
4562 * PASS 1: free the contents of the items. We don't free the items
4563 * themselves yet, so that it is possible to decrement refcount counters
4564 */
4565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004566 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004567 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004570 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004571
4572#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004573 // Go through the list of jobs and free items without the copyID. This
4574 // must happen before doing channels, because jobs refer to channels, but
4575 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004576 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004578 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004579 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4580#endif
4581
4582 /*
4583 * PASS 2: free the items themselves.
4584 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004585 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004586 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004587
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004588#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004589 // Go through the list of jobs and free items without the copyID. This
4590 // must happen before doing channels, because jobs refer to channels, but
4591 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004592 free_unused_jobs(copyID, COPYID_MASK);
4593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004594 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004595 free_unused_channels(copyID, COPYID_MASK);
4596#endif
4597
4598 in_free_unref_items = FALSE;
4599
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004600 return did_free;
4601}
4602
4603/*
4604 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605 * "list_stack" is used to add lists to be marked. Can be NULL.
4606 *
4607 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004608 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004609 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004610set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004611{
4612 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004613 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004614 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004615 hashtab_T *cur_ht;
4616 ht_stack_T *ht_stack = NULL;
4617 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004618
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004619 cur_ht = ht;
4620 for (;;)
4621 {
4622 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004624 // Mark each item in the hashtab. If the item contains a hashtab
4625 // it is added to ht_stack, if it contains a list it is added to
4626 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004627 todo = (int)cur_ht->ht_used;
4628 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4629 if (!HASHITEM_EMPTY(hi))
4630 {
4631 --todo;
4632 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4633 &ht_stack, list_stack);
4634 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004635 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004636
4637 if (ht_stack == NULL)
4638 break;
4639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004640 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004641 cur_ht = ht_stack->ht;
4642 tempitem = ht_stack;
4643 ht_stack = ht_stack->prev;
4644 free(tempitem);
4645 }
4646
4647 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004648}
4649
4650/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004651 * Mark a dict and its items with "copyID".
4652 * Returns TRUE if setting references failed somehow.
4653 */
4654 int
4655set_ref_in_dict(dict_T *d, int copyID)
4656{
4657 if (d != NULL && d->dv_copyID != copyID)
4658 {
4659 d->dv_copyID = copyID;
4660 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4661 }
4662 return FALSE;
4663}
4664
4665/*
4666 * Mark a list and its items with "copyID".
4667 * Returns TRUE if setting references failed somehow.
4668 */
4669 int
4670set_ref_in_list(list_T *ll, int copyID)
4671{
4672 if (ll != NULL && ll->lv_copyID != copyID)
4673 {
4674 ll->lv_copyID = copyID;
4675 return set_ref_in_list_items(ll, copyID, NULL);
4676 }
4677 return FALSE;
4678}
4679
4680/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004681 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004682 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4683 *
4684 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004685 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004686 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004687set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004688{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004689 listitem_T *li;
4690 int abort = FALSE;
4691 list_T *cur_l;
4692 list_stack_T *list_stack = NULL;
4693 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004694
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004695 cur_l = l;
4696 for (;;)
4697 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004698 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004699 // Mark each item in the list. If the item contains a hashtab
4700 // it is added to ht_stack, if it contains a list it is added to
4701 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004702 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4703 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4704 ht_stack, &list_stack);
4705 if (list_stack == NULL)
4706 break;
4707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004708 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004709 cur_l = list_stack->list;
4710 tempitem = list_stack;
4711 list_stack = list_stack->prev;
4712 free(tempitem);
4713 }
4714
4715 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004716}
4717
4718/*
4719 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004720 * "list_stack" is used to add lists to be marked. Can be NULL.
4721 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4722 *
4723 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004724 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004726set_ref_in_item(
4727 typval_T *tv,
4728 int copyID,
4729 ht_stack_T **ht_stack,
4730 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004731{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004732 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004733
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004734 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004735 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004736 dict_T *dd = tv->vval.v_dict;
4737
Bram Moolenaara03f2332016-02-06 18:09:59 +01004738 if (dd != NULL && dd->dv_copyID != copyID)
4739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004740 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004741 dd->dv_copyID = copyID;
4742 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004743 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004744 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4745 }
4746 else
4747 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004748 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4749
Bram Moolenaara03f2332016-02-06 18:09:59 +01004750 if (newitem == NULL)
4751 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004752 else
4753 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004754 newitem->ht = &dd->dv_hashtab;
4755 newitem->prev = *ht_stack;
4756 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004757 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004758 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004759 }
4760 }
4761 else if (tv->v_type == VAR_LIST)
4762 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004763 list_T *ll = tv->vval.v_list;
4764
Bram Moolenaara03f2332016-02-06 18:09:59 +01004765 if (ll != NULL && ll->lv_copyID != copyID)
4766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004767 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004768 ll->lv_copyID = copyID;
4769 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004770 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004771 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004772 }
4773 else
4774 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004775 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4776
Bram Moolenaara03f2332016-02-06 18:09:59 +01004777 if (newitem == NULL)
4778 abort = TRUE;
4779 else
4780 {
4781 newitem->list = ll;
4782 newitem->prev = *list_stack;
4783 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004784 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004785 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004786 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004787 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004788 else if (tv->v_type == VAR_FUNC)
4789 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004790 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004791 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004792 else if (tv->v_type == VAR_PARTIAL)
4793 {
4794 partial_T *pt = tv->vval.v_partial;
4795 int i;
4796
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004797 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004798 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004799 // Didn't see this partial yet.
4800 pt->pt_copyID = copyID;
4801
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004802 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004803
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004804 if (pt->pt_dict != NULL)
4805 {
4806 typval_T dtv;
4807
4808 dtv.v_type = VAR_DICT;
4809 dtv.vval.v_dict = pt->pt_dict;
4810 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4811 }
4812
4813 for (i = 0; i < pt->pt_argc; ++i)
4814 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4815 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004816 if (pt->pt_funcstack != NULL)
4817 {
4818 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4819
4820 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4821 abort = abort || set_ref_in_item(stack + i, copyID,
4822 ht_stack, list_stack);
4823 }
4824
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004825 }
4826 }
4827#ifdef FEAT_JOB_CHANNEL
4828 else if (tv->v_type == VAR_JOB)
4829 {
4830 job_T *job = tv->vval.v_job;
4831 typval_T dtv;
4832
4833 if (job != NULL && job->jv_copyID != copyID)
4834 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004835 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004836 if (job->jv_channel != NULL)
4837 {
4838 dtv.v_type = VAR_CHANNEL;
4839 dtv.vval.v_channel = job->jv_channel;
4840 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4841 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004842 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004843 {
4844 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004845 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004846 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4847 }
4848 }
4849 }
4850 else if (tv->v_type == VAR_CHANNEL)
4851 {
4852 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004853 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004854 typval_T dtv;
4855 jsonq_T *jq;
4856 cbq_T *cq;
4857
4858 if (ch != NULL && ch->ch_copyID != copyID)
4859 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004860 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004861 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004862 {
4863 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4864 jq = jq->jq_next)
4865 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4866 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4867 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004868 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004869 {
4870 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004871 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004872 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4873 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004874 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004875 {
4876 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004877 dtv.vval.v_partial =
4878 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004879 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4880 }
4881 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004882 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004883 {
4884 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004885 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004886 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4887 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004888 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004889 {
4890 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004891 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004892 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4893 }
4894 }
4895 }
4896#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004897 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004898}
4899
Bram Moolenaar8c711452005-01-14 21:53:12 +00004900/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 * Return a string with the string representation of a variable.
4902 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004903 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004904 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004905 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004906 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004907 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004908 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4909 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004910 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004911 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004912 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004913echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004914 typval_T *tv,
4915 char_u **tofree,
4916 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004917 int copyID,
4918 int echo_style,
4919 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004920 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004921{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004922 static int recurse = 0;
4923 char_u *r = NULL;
4924
Bram Moolenaar33570922005-01-25 22:26:29 +00004925 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004926 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004927 if (!did_echo_string_emsg)
4928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004929 // Only give this message once for a recursive call to avoid
4930 // flooding the user with errors. And stop iterating over lists
4931 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004932 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004933 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004935 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004936 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004937 }
4938 ++recurse;
4939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 switch (tv->v_type)
4941 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004942 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004943 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004944 {
4945 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004946 r = tv->vval.v_string;
4947 if (r == NULL)
4948 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004949 }
4950 else
4951 {
4952 *tofree = string_quote(tv->vval.v_string, FALSE);
4953 r = *tofree;
4954 }
4955 break;
4956
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004958 if (echo_style)
4959 {
4960 *tofree = NULL;
4961 r = tv->vval.v_string;
4962 }
4963 else
4964 {
4965 *tofree = string_quote(tv->vval.v_string, TRUE);
4966 r = *tofree;
4967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004968 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004969
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004970 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004971 {
4972 partial_T *pt = tv->vval.v_partial;
4973 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004974 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004975 garray_T ga;
4976 int i;
4977 char_u *tf;
4978
4979 ga_init2(&ga, 1, 100);
4980 ga_concat(&ga, (char_u *)"function(");
4981 if (fname != NULL)
4982 {
4983 ga_concat(&ga, fname);
4984 vim_free(fname);
4985 }
4986 if (pt != NULL && pt->pt_argc > 0)
4987 {
4988 ga_concat(&ga, (char_u *)", [");
4989 for (i = 0; i < pt->pt_argc; ++i)
4990 {
4991 if (i > 0)
4992 ga_concat(&ga, (char_u *)", ");
4993 ga_concat(&ga,
4994 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4995 vim_free(tf);
4996 }
4997 ga_concat(&ga, (char_u *)"]");
4998 }
4999 if (pt != NULL && pt->pt_dict != NULL)
5000 {
5001 typval_T dtv;
5002
5003 ga_concat(&ga, (char_u *)", ");
5004 dtv.v_type = VAR_DICT;
5005 dtv.vval.v_dict = pt->pt_dict;
5006 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5007 vim_free(tf);
5008 }
5009 ga_concat(&ga, (char_u *)")");
5010
5011 *tofree = ga.ga_data;
5012 r = *tofree;
5013 break;
5014 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005015
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005016 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005017 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005018 break;
5019
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005020 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005021 if (tv->vval.v_list == NULL)
5022 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005023 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005024 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005025 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005026 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005027 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5028 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005029 {
5030 *tofree = NULL;
5031 r = (char_u *)"[...]";
5032 }
5033 else
5034 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005035 int old_copyID = tv->vval.v_list->lv_copyID;
5036
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005037 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005038 *tofree = list2string(tv, copyID, restore_copyID);
5039 if (restore_copyID)
5040 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005041 r = *tofree;
5042 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005043 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005044
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005046 if (tv->vval.v_dict == NULL)
5047 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005048 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005049 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005050 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005051 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005052 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5053 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005054 {
5055 *tofree = NULL;
5056 r = (char_u *)"{...}";
5057 }
5058 else
5059 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005060 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005061
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005062 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005063 *tofree = dict2string(tv, copyID, restore_copyID);
5064 if (restore_copyID)
5065 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005066 r = *tofree;
5067 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005068 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005069
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005071 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005072 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005074 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005075 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005076 break;
5077
Bram Moolenaar835dc632016-02-07 14:27:38 +01005078 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005079 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005080 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005081 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005082 if (composite_val)
5083 {
5084 *tofree = string_quote(r, FALSE);
5085 r = *tofree;
5086 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005087 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005088
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005089 case VAR_INSTR:
5090 *tofree = NULL;
5091 r = (char_u *)"instructions";
5092 break;
5093
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005095#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 *tofree = NULL;
5097 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5098 r = numbuf;
5099 break;
5100#endif
5101
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005102 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005103 case VAR_SPECIAL:
5104 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005105 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005106 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005108
Bram Moolenaar8502c702014-06-17 12:51:16 +02005109 if (--recurse == 0)
5110 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112}
5113
5114/*
5115 * Return a string with the string representation of a variable.
5116 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5117 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005118 * Does not put quotes around strings, as ":echo" displays values.
5119 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5120 * May return NULL.
5121 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005122 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005123echo_string(
5124 typval_T *tv,
5125 char_u **tofree,
5126 char_u *numbuf,
5127 int copyID)
5128{
5129 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5130}
5131
5132/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005133 * Return string "str" in ' quotes, doubling ' characters.
5134 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005137 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005138string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005139{
Bram Moolenaar33570922005-01-25 22:26:29 +00005140 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005141 char_u *p, *r, *s;
5142
Bram Moolenaar33570922005-01-25 22:26:29 +00005143 len = (function ? 13 : 3);
5144 if (str != NULL)
5145 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005146 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005147 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005148 if (*p == '\'')
5149 ++len;
5150 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005151 s = r = alloc(len);
5152 if (r != NULL)
5153 {
5154 if (function)
5155 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005157 r += 10;
5158 }
5159 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005161 if (str != NULL)
5162 for (p = str; *p != NUL; )
5163 {
5164 if (*p == '\'')
5165 *r++ = '\'';
5166 MB_COPY_CHAR(p, r);
5167 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005169 if (function)
5170 *r++ = ')';
5171 *r++ = NUL;
5172 }
5173 return s;
5174}
5175
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005176#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005177/*
5178 * Convert the string "text" to a floating point number.
5179 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5180 * this always uses a decimal point.
5181 * Returns the length of the text that was consumed.
5182 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005184string2float(
5185 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005186 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005187{
5188 char *s = (char *)text;
5189 float_T f;
5190
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005191 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005192 if (STRNICMP(text, "inf", 3) == 0)
5193 {
5194 *value = INFINITY;
5195 return 3;
5196 }
5197 if (STRNICMP(text, "-inf", 3) == 0)
5198 {
5199 *value = -INFINITY;
5200 return 4;
5201 }
5202 if (STRNICMP(text, "nan", 3) == 0)
5203 {
5204 *value = NAN;
5205 return 3;
5206 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005207 f = strtod(s, &s);
5208 *value = f;
5209 return (int)((char_u *)s - text);
5210}
5211#endif
5212
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005213/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005214 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5215 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005216 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005217 */
5218 int
5219buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5220{
5221 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005222 char_u *t;
5223 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005224
5225 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5226 return -1;
5227
5228 if (lnum > buf->b_ml.ml_line_count)
5229 lnum = buf->b_ml.ml_line_count;
5230
5231 str = ml_get_buf(buf, lnum, FALSE);
5232 if (str == NULL)
5233 return -1;
5234
5235 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005236 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005237
Bram Moolenaar91458462021-01-13 20:08:38 +01005238 // count the number of characters
5239 t = str;
5240 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5241 t += mb_ptr2len(t);
5242
5243 // In insert mode, when the cursor is at the end of a non-empty line,
5244 // byteidx points to the NUL character immediately past the end of the
5245 // string. In this case, add one to the character count.
5246 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5247 count++;
5248
5249 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005250}
5251
5252/*
5253 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005254 * byte index. Works only for loaded buffers. Returns -1 on failure.
5255 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005256 */
5257 int
5258buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5259{
5260 char_u *str;
5261 char_u *t;
5262
5263 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5264 return -1;
5265
5266 if (lnum > buf->b_ml.ml_line_count)
5267 lnum = buf->b_ml.ml_line_count;
5268
5269 str = ml_get_buf(buf, lnum, FALSE);
5270 if (str == NULL)
5271 return -1;
5272
5273 // Convert the character offset to a byte offset
5274 t = str;
5275 while (*t != NUL && --charidx > 0)
5276 t += mb_ptr2len(t);
5277
Bram Moolenaar91458462021-01-13 20:08:38 +01005278 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005279}
5280
5281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005283 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005286var2fpos(
5287 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005288 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005289 int *fnum, // set to fnum for '0, 'A, etc.
5290 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005292 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005294 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005296 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005297 if (varp->v_type == VAR_LIST)
5298 {
5299 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005300 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005301 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005302 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005303
5304 l = varp->vval.v_list;
5305 if (l == NULL)
5306 return NULL;
5307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005308 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005309 pos.lnum = list_find_nr(l, 0L, &error);
5310 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005311 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005312 if (charcol)
5313 len = (long)mb_charlen(ml_get(pos.lnum));
5314 else
5315 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005316
Bram Moolenaarec65d772020-08-20 22:29:12 +02005317 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005318 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005319 li = list_find(l, 1L);
5320 if (li != NULL && li->li_tv.v_type == VAR_STRING
5321 && li->li_tv.vval.v_string != NULL
5322 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005323 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005324 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005325 }
5326 else
5327 {
5328 pos.col = list_find_nr(l, 1L, &error);
5329 if (error)
5330 return NULL;
5331 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005333 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005334 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005335 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005336 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005339 pos.coladd = list_find_nr(l, 2L, &error);
5340 if (error)
5341 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005342
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005343 return &pos;
5344 }
5345
Bram Moolenaarc5809432021-03-27 21:23:30 +01005346 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5347 return NULL;
5348
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005349 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 if (name == NULL)
5351 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005353 {
5354 pos = curwin->w_cursor;
5355 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005356 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005357 return &pos;
5358 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005360 {
5361 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005362 pos = VIsual;
5363 else
5364 pos = curwin->w_cursor;
5365 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005366 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005367 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005368 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005369 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005371 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5373 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005374 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005375 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 return pp;
5377 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005378
Bram Moolenaara5525202006-03-02 22:52:09 +00005379 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005380
Bram Moolenaar477933c2007-07-17 14:32:23 +00005381 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005382 {
5383 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005384 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005385 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005386 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 // In silent Ex mode topline is zero, but that's not a valid line
5388 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005389 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005390 return &pos;
5391 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005392 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005393 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005394 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005395 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005396 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005397 return &pos;
5398 }
5399 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005400 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005402 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
5404 pos.lnum = curbuf->b_ml.ml_line_count;
5405 pos.col = 0;
5406 }
5407 else
5408 {
5409 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005410 if (charcol)
5411 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5412 else
5413 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 }
5415 return &pos;
5416 }
5417 return NULL;
5418}
5419
5420/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005421 * Convert list in "arg" into a position and optional file number.
5422 * When "fnump" is NULL there is no file number, only 3 items.
5423 * Note that the column is passed on as-is, the caller may want to decrement
5424 * it to use 1 for the first column.
5425 * Return FAIL when conversion is not possible, doesn't check the position for
5426 * validity.
5427 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005429list2fpos(
5430 typval_T *arg,
5431 pos_T *posp,
5432 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005433 colnr_T *curswantp,
5434 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005435{
5436 list_T *l = arg->vval.v_list;
5437 long i = 0;
5438 long n;
5439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5441 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005442 if (arg->v_type != VAR_LIST
5443 || l == NULL
5444 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005445 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005446 return FAIL;
5447
5448 if (fnump != NULL)
5449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005451 if (n < 0)
5452 return FAIL;
5453 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005454 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005455 *fnump = n;
5456 }
5457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005459 if (n < 0)
5460 return FAIL;
5461 posp->lnum = n;
5462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005464 if (n < 0)
5465 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005466 // If character position is specified, then convert to byte position
5467 if (charcol)
5468 {
5469 buf_T *buf;
5470
5471 // Get the text for the specified line in a loaded buffer
5472 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5473 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5474 return FAIL;
5475
Bram Moolenaar91458462021-01-13 20:08:38 +01005476 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005477 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005478 posp->col = n;
5479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005480 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005481 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005482 posp->coladd = 0;
5483 else
5484 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005485
Bram Moolenaar493c1782014-05-28 14:34:46 +02005486 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005487 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005488
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005489 return OK;
5490}
5491
5492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 * Get the length of an environment variable name.
5494 * Advance "arg" to the first character after the name.
5495 * Return 0 for error.
5496 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005498get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 char_u *p;
5501 int len;
5502
5503 for (p = *arg; vim_isIDc(*p); ++p)
5504 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 return 0;
5507
5508 len = (int)(p - *arg);
5509 *arg = p;
5510 return len;
5511}
5512
5513/*
5514 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005515 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 * Return 0 if something is wrong.
5517 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005519get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 char_u *p;
5522 int len;
5523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005524 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005526 {
5527 if (*p == ':')
5528 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005529 // "s:" is start of "s:var", but "n:" is not and can be used in
5530 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005531 len = (int)(p - *arg);
5532 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5533 || len > 1)
5534 break;
5535 }
5536 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005537 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 return 0;
5539
5540 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005541 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
5543 return len;
5544}
5545
5546/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005547 * Get the length of the name of a variable or function.
5548 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005550 * Return -1 if curly braces expansion failed.
5551 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 * If the name contains 'magic' {}'s, expand them and return the
5553 * expanded name in an allocated string via 'alias' - caller must free.
5554 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005555 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005556get_name_len(
5557 char_u **arg,
5558 char_u **alias,
5559 int evaluate,
5560 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 char_u *p;
5564 char_u *expr_start;
5565 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005567 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5570 && (*arg)[2] == (int)KE_SNR)
5571 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005572 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 *arg += 3;
5574 return get_id_len(arg) + 3;
5575 }
5576 len = eval_fname_script(*arg);
5577 if (len > 0)
5578 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005579 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 *arg += len;
5581 }
5582
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005586 p = find_name_end(*arg, &expr_start, &expr_end,
5587 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 if (expr_start != NULL)
5589 {
5590 char_u *temp_string;
5591
5592 if (!evaluate)
5593 {
5594 len += (int)(p - *arg);
5595 *arg = skipwhite(p);
5596 return len;
5597 }
5598
5599 /*
5600 * Include any <SID> etc in the expanded string:
5601 * Thus the -len here.
5602 */
5603 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5604 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005605 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 *alias = temp_string;
5607 *arg = skipwhite(p);
5608 return (int)STRLEN(temp_string);
5609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
5611 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005612 // Only give an error when there is something, otherwise it will be
5613 // reported at a higher level.
5614 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005615 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
5617 return len;
5618}
5619
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620/*
5621 * Find the end of a variable or function name, taking care of magic braces.
5622 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5623 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005624 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625 * Return a pointer to just after the name. Equal to "arg" if there is no
5626 * valid name.
5627 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005628 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005629find_name_end(
5630 char_u *arg,
5631 char_u **expr_start,
5632 char_u **expr_end,
5633 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 int mb_nest = 0;
5636 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005638 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005639 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005641 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 *expr_start = NULL;
5644 *expr_end = NULL;
5645 }
5646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005647 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005648 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5649 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005650 return arg;
5651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 for (p = arg; *p != NUL
5653 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005654 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005655 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005656 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005658 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005660 if (*p == '\'')
5661 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005662 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005663 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005664 ;
5665 if (*p == NUL)
5666 break;
5667 }
5668 else if (*p == '"')
5669 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005670 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005671 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005672 if (*p == '\\' && p[1] != NUL)
5673 ++p;
5674 if (*p == NUL)
5675 break;
5676 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005677 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005679 // "s:" is start of "s:var", but "n:" is not and can be used in
5680 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005681 len = (int)(p - arg);
5682 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005683 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005684 break;
5685 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 if (*p == '[')
5690 ++br_nest;
5691 else if (*p == ']')
5692 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005694
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005695 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 if (*p == '{')
5698 {
5699 mb_nest++;
5700 if (expr_start != NULL && *expr_start == NULL)
5701 *expr_start = p;
5702 }
5703 else if (*p == '}')
5704 {
5705 mb_nest--;
5706 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5707 *expr_end = p;
5708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711
5712 return p;
5713}
5714
5715/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005716 * Expands out the 'magic' {}'s in a variable/function name.
5717 * Note that this can call itself recursively, to deal with
5718 * constructs like foo{bar}{baz}{bam}
5719 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5720 * "in_start" ^
5721 * "expr_start" ^
5722 * "expr_end" ^
5723 * "in_end" ^
5724 *
5725 * Returns a new allocated string, which the caller must free.
5726 * Returns NULL for failure.
5727 */
5728 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005729make_expanded_name(
5730 char_u *in_start,
5731 char_u *expr_start,
5732 char_u *expr_end,
5733 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005734{
5735 char_u c1;
5736 char_u *retval = NULL;
5737 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005738
5739 if (expr_end == NULL || in_end == NULL)
5740 return NULL;
5741 *expr_start = NUL;
5742 *expr_end = NUL;
5743 c1 = *in_end;
5744 *in_end = NUL;
5745
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005746 temp_result = eval_to_string(expr_start + 1, FALSE);
5747 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005748 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005749 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5750 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005751 if (retval != NULL)
5752 {
5753 STRCPY(retval, in_start);
5754 STRCAT(retval, temp_result);
5755 STRCAT(retval, expr_end + 1);
5756 }
5757 }
5758 vim_free(temp_result);
5759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005760 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005761 *expr_start = '{';
5762 *expr_end = '}';
5763
5764 if (retval != NULL)
5765 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005766 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005767 if (expr_start != NULL)
5768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005769 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005770 temp_result = make_expanded_name(retval, expr_start,
5771 expr_end, temp_result);
5772 vim_free(retval);
5773 retval = temp_result;
5774 }
5775 }
5776
5777 return retval;
5778}
5779
5780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005782 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005785eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005787 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005788}
5789
5790/*
5791 * Return TRUE if character "c" can be used as the first character in a
5792 * variable or function name (excluding '{' and '}').
5793 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005794 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005795eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005796{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005797 return ASCII_ISALPHA(c) || c == '_';
5798}
5799
5800/*
5801 * Return TRUE if character "c" can be used as the first character of a
5802 * dictionary key.
5803 */
5804 int
5805eval_isdictc(int c)
5806{
5807 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808}
5809
5810/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005811 * Handle:
5812 * - expr[expr], expr[expr:expr] subscript
5813 * - ".name" lookup
5814 * - function call with Funcref variable: func(expr)
5815 * - method call: var->method()
5816 *
5817 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005818 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005819 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005820handle_subscript(
5821 char_u **arg,
5822 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005823 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005824 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005825{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005826 int evaluate = evalarg != NULL
5827 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005828 int ret = OK;
5829 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005830 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005831 int getnext;
5832 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005833
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005834 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005835 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005836 // When at the end of the line and ".name" or "->{" or "->X" follows in
5837 // the next line then consume the line break.
5838 p = eval_next_non_blank(*arg, evalarg, &getnext);
5839 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005840 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005841 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005842 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005843 {
5844 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005845 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005846 check_white = FALSE;
5847 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005848
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005849 if (rettv->v_type == VAR_ANY)
5850 {
5851 char_u *exp_name;
5852 int cc;
5853 int idx;
5854 ufunc_T *ufunc;
5855 type_T *type;
5856
5857 // Found script from "import * as {name}", script item name must
5858 // follow.
5859 if (**arg != '.')
5860 {
5861 if (verbose)
5862 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5863 ret = FAIL;
5864 break;
5865 }
5866 ++*arg;
5867 if (IS_WHITE_OR_NUL(**arg))
5868 {
5869 if (verbose)
5870 emsg(_(e_no_white_space_allowed_after_dot));
5871 ret = FAIL;
5872 break;
5873 }
5874
5875 // isolate the name
5876 exp_name = *arg;
5877 while (eval_isnamec(**arg))
5878 ++*arg;
5879 cc = **arg;
5880 **arg = NUL;
5881
5882 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5883 evalarg->eval_cctx, verbose);
5884 **arg = cc;
5885 *arg = skipwhite(*arg);
5886
5887 if (idx < 0 && ufunc == NULL)
5888 {
5889 ret = FAIL;
5890 break;
5891 }
5892 if (idx >= 0)
5893 {
5894 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5895 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5896
5897 copy_tv(sv->sv_tv, rettv);
5898 }
5899 else
5900 {
5901 rettv->v_type = VAR_FUNC;
5902 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5903 }
5904 }
5905
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005906 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5907 || rettv->v_type == VAR_PARTIAL))
5908 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005909 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005910 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5911 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005912
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005913 // Stop the expression evaluation when immediately aborting on
5914 // error, or when an interrupt occurred or an exception was thrown
5915 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005916 if (aborting())
5917 {
5918 if (ret == OK)
5919 clear_tv(rettv);
5920 ret = FAIL;
5921 }
5922 dict_unref(selfdict);
5923 selfdict = NULL;
5924 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005925 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005926 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005927 if (in_vim9script())
5928 *arg = skipwhite(p + 2);
5929 else
5930 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005931 if (ret == OK)
5932 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005933 if (VIM_ISWHITE(**arg))
5934 {
5935 emsg(_(e_nowhitespace));
5936 ret = FAIL;
5937 }
5938 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005939 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005940 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005941 else
5942 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005943 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005944 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005945 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005946 // "." is ".name" lookup when we found a dict or when evaluating and
5947 // scriptversion is at least 2, where string concatenation is "..".
5948 else if (**arg == '['
5949 || (**arg == '.' && (rettv->v_type == VAR_DICT
5950 || (!evaluate
5951 && (*arg)[1] != '.'
5952 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005953 {
5954 dict_unref(selfdict);
5955 if (rettv->v_type == VAR_DICT)
5956 {
5957 selfdict = rettv->vval.v_dict;
5958 if (selfdict != NULL)
5959 ++selfdict->dv_refcount;
5960 }
5961 else
5962 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005963 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005964 {
5965 clear_tv(rettv);
5966 ret = FAIL;
5967 }
5968 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005969 else
5970 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005971 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005973 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5974 // Don't do this when "Func" is already a partial that was bound
5975 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005976 if (selfdict != NULL
5977 && (rettv->v_type == VAR_FUNC
5978 || (rettv->v_type == VAR_PARTIAL
5979 && (rettv->vval.v_partial->pt_auto
5980 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005982
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005983 dict_unref(selfdict);
5984 return ret;
5985}
5986
5987/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005988 * Make a copy of an item.
5989 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005990 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5991 * reference to an already copied list/dict can be used.
5992 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005993 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005995item_copy(
5996 typval_T *from,
5997 typval_T *to,
5998 int deep,
5999 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006000{
6001 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006002 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006003
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006006 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006007 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006008 }
6009 ++recurse;
6010
6011 switch (from->v_type)
6012 {
6013 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006014 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006015 case VAR_STRING:
6016 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006017 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006018 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006019 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006020 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006021 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006022 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006023 copy_tv(from, to);
6024 break;
6025 case VAR_LIST:
6026 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006027 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006028 if (from->vval.v_list == NULL)
6029 to->vval.v_list = NULL;
6030 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006032 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006033 to->vval.v_list = from->vval.v_list->lv_copylist;
6034 ++to->vval.v_list->lv_refcount;
6035 }
6036 else
6037 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6038 if (to->vval.v_list == NULL)
6039 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006040 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006041 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006043 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006044 case VAR_DICT:
6045 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006046 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006047 if (from->vval.v_dict == NULL)
6048 to->vval.v_dict = NULL;
6049 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6050 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006051 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006052 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6053 ++to->vval.v_dict->dv_refcount;
6054 }
6055 else
6056 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6057 if (to->vval.v_dict == NULL)
6058 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006059 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006060 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006061 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006063 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006064 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006065 }
6066 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006067 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006068}
6069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070 void
6071echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6072{
6073 char_u *tofree;
6074 char_u numbuf[NUMBUFLEN];
6075 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6076
6077 if (*atstart)
6078 {
6079 *atstart = FALSE;
6080 // Call msg_start() after eval1(), evaluating the expression
6081 // may cause a message to appear.
6082 if (with_space)
6083 {
6084 // Mark the saved text as finishing the line, so that what
6085 // follows is displayed on a new line when scrolling back
6086 // at the more prompt.
6087 msg_sb_eol();
6088 msg_start();
6089 }
6090 }
6091 else if (with_space)
6092 msg_puts_attr(" ", echo_attr);
6093
6094 if (p != NULL)
6095 for ( ; *p != NUL && !got_int; ++p)
6096 {
6097 if (*p == '\n' || *p == '\r' || *p == TAB)
6098 {
6099 if (*p != TAB && *needclr)
6100 {
6101 // remove any text still there from the command
6102 msg_clr_eos();
6103 *needclr = FALSE;
6104 }
6105 msg_putchar_attr(*p, echo_attr);
6106 }
6107 else
6108 {
6109 if (has_mbyte)
6110 {
6111 int i = (*mb_ptr2len)(p);
6112
6113 (void)msg_outtrans_len_attr(p, i, echo_attr);
6114 p += i - 1;
6115 }
6116 else
6117 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6118 }
6119 }
6120 vim_free(tofree);
6121}
6122
Bram Moolenaare9a41262005-01-15 22:18:47 +00006123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 * ":echo expr1 ..." print each argument separated with a space, add a
6125 * newline at the end.
6126 * ":echon expr1 ..." print each argument plain.
6127 */
6128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
6131 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006133 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 int needclr = TRUE;
6135 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006136 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006137 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006138 evalarg_T evalarg;
6139
Bram Moolenaare707c882020-07-01 13:07:37 +02006140 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141
6142 if (eap->skip)
6143 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006144 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006146 // If eval1() causes an error message the text from the command may
6147 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006148 need_clr_eos = needclr;
6149
Bram Moolenaar68db9962021-05-09 23:19:22 +02006150 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006151 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 {
6153 /*
6154 * Report the invalid expression unless the expression evaluation
6155 * has been cancelled due to an aborting error, an interrupt, or an
6156 * exception.
6157 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006158 if (!aborting() && did_emsg == did_emsg_before
6159 && called_emsg == called_emsg_before)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006160 semsg(_(e_invexpr2), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006161 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 break;
6163 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006164 need_clr_eos = FALSE;
6165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006167 {
6168 if (rettv.v_type == VAR_VOID)
6169 {
6170 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6171 break;
6172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006174 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006175
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006176 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 arg = skipwhite(arg);
6178 }
6179 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006180 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
6182 if (eap->skip)
6183 --emsg_skip;
6184 else
6185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006186 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (needclr)
6188 msg_clr_eos();
6189 if (eap->cmdidx == CMD_echo)
6190 msg_end();
6191 }
6192}
6193
6194/*
6195 * ":echohl {name}".
6196 */
6197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006198ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006200 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201}
6202
6203/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006204 * Returns the :echo attribute
6205 */
6206 int
6207get_echo_attr(void)
6208{
6209 return echo_attr;
6210}
6211
6212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 * ":execute expr1 ..." execute the result of an expression.
6214 * ":echomsg expr1 ..." Print a message
6215 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006216 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 * Each gets spaces around each argument and a newline at the end for
6218 * echo commands
6219 */
6220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006221ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006224 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 int ret = OK;
6226 char_u *p;
6227 garray_T ga;
6228 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229
6230 ga_init2(&ga, 1, 80);
6231
6232 if (eap->skip)
6233 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006234 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006236 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006237 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
6240 if (!eap->skip)
6241 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006242 char_u buf[NUMBUFLEN];
6243
6244 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006245 {
6246 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6247 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006248 semsg(_(e_using_invalid_value_as_string_str),
6249 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006250 p = NULL;
6251 }
6252 else
6253 p = tv_get_string_buf(&rettv, buf);
6254 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006255 else
6256 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006257 if (p == NULL)
6258 {
6259 clear_tv(&rettv);
6260 ret = FAIL;
6261 break;
6262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 len = (int)STRLEN(p);
6264 if (ga_grow(&ga, len + 2) == FAIL)
6265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006266 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 ret = FAIL;
6268 break;
6269 }
6270 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 ga.ga_len += len;
6274 }
6275
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006276 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 arg = skipwhite(arg);
6278 }
6279
6280 if (ret != FAIL && ga.ga_data != NULL)
6281 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006282 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006284 // Mark the already saved text as finishing the line, so that what
6285 // follows is displayed on a new line when scrolling back at the
6286 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006287 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006288 }
6289
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006291 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006292 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006293 out_flush();
6294 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006295 else if (eap->cmdidx == CMD_echoconsole)
6296 {
6297 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6298 ui_write((char_u *)"\r\n", 2, TRUE);
6299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 else if (eap->cmdidx == CMD_echoerr)
6301 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006302 int save_did_emsg = did_emsg;
6303
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006304 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006305 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 if (!force_abort)
6307 did_emsg = save_did_emsg;
6308 }
6309 else if (eap->cmdidx == CMD_execute)
6310 do_cmdline((char_u *)ga.ga_data,
6311 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6312 }
6313
6314 ga_clear(&ga);
6315
6316 if (eap->skip)
6317 --emsg_skip;
6318
6319 eap->nextcmd = check_nextcmd(arg);
6320}
6321
6322/*
6323 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6324 * "arg" points to the "&" or '+' when called, to "option" when returning.
6325 * Returns NULL when no option name found. Otherwise pointer to the char
6326 * after the option name.
6327 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006329find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 char_u *p = *arg;
6332
6333 ++p;
6334 if (*p == 'g' && p[1] == ':')
6335 {
6336 *opt_flags = OPT_GLOBAL;
6337 p += 2;
6338 }
6339 else if (*p == 'l' && p[1] == ':')
6340 {
6341 *opt_flags = OPT_LOCAL;
6342 p += 2;
6343 }
6344 else
6345 *opt_flags = 0;
6346
6347 if (!ASCII_ISALPHA(*p))
6348 return NULL;
6349 *arg = p;
6350
6351 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006352 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 else
6354 while (ASCII_ISALPHA(*p))
6355 ++p;
6356 return p;
6357}
6358
6359/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006360 * Display script name where an item was last set.
6361 * Should only be invoked when 'verbose' is non-zero.
6362 */
6363 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006364last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006365{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006366 char_u *p;
6367
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006368 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006369 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006370 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006371 if (p != NULL)
6372 {
6373 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006374 msg_puts(_("\n\tLast set from "));
6375 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006376 if (script_ctx.sc_lnum > 0)
6377 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006378 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006379 msg_outnum((long)script_ctx.sc_lnum);
6380 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006381 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006382 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006383 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006384 }
6385}
6386
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006387#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388
6389/*
6390 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006391 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 * "flags" can be "g" to do a global substitute.
6393 * Returns an allocated string, NULL for error.
6394 */
6395 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006396do_string_sub(
6397 char_u *str,
6398 char_u *pat,
6399 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006400 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006401 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 int sublen;
6404 regmatch_T regmatch;
6405 int i;
6406 int do_all;
6407 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006408 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 garray_T ga;
6410 char_u *ret;
6411 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006412 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006414 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006416 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
6418 ga_init2(&ga, 1, 200);
6419
6420 do_all = (flags[0] == 'g');
6421
6422 regmatch.rm_ic = p_ic;
6423 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6424 if (regmatch.regprog != NULL)
6425 {
6426 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006427 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6429 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006430 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006431 if (regmatch.startp[0] == regmatch.endp[0])
6432 {
6433 if (zero_width == regmatch.startp[0])
6434 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006435 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006436 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006437 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6438 (size_t)i);
6439 ga.ga_len += i;
6440 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006441 continue;
6442 }
6443 zero_width = regmatch.startp[0];
6444 }
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 /*
6447 * Get some space for a temporary buffer to do the substitution
6448 * into. It will contain:
6449 * - The text up to where the match is.
6450 * - The substituted text.
6451 * - The text after the match.
6452 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006453 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006454 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6456 {
6457 ga_clear(&ga);
6458 break;
6459 }
6460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006461 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 i = (int)(regmatch.startp[0] - tail);
6463 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006464 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006465 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 + ga.ga_len + i, TRUE, TRUE, FALSE);
6467 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006468 tail = regmatch.endp[0];
6469 if (*tail == NUL)
6470 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 if (!do_all)
6472 break;
6473 }
6474
6475 if (ga.ga_data != NULL)
6476 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6477
Bram Moolenaar473de612013-06-08 18:19:48 +02006478 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 }
6480
6481 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6482 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006483 if (p_cpo == empty_option)
6484 p_cpo = save_cpo;
6485 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006486 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006487 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006488 // If it's still empty it was changed and restored, need to restore in
6489 // the complicated way.
6490 if (*p_cpo == NUL)
6491 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006492 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
6495 return ret;
6496}