blob: f25454960d39bc4c90965e65b0ce483adaf3f450 [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 }
312 else
313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100314 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100315 if (s == NULL)
316 return FAIL;
317 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200318 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100319 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200320 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100321 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200322 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100323 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100324 return FAIL;
325 }
326 }
327 return OK;
328}
329
330/*
331 * Like eval_to_bool() but using a typval_T instead of a string.
332 * Works for string, funcref and partial.
333 */
334 int
335eval_expr_to_bool(typval_T *expr, int *error)
336{
337 typval_T rettv;
338 int res;
339
340 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
341 {
342 *error = TRUE;
343 return FALSE;
344 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200345 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100346 clear_tv(&rettv);
347 return res;
348}
349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350/*
351 * Top level evaluation function, returning a string. If "skip" is TRUE,
352 * only parsing to "nextcmd" is done, without reporting errors. Return
353 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
354 */
355 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100356eval_to_string_skip(
357 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200358 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100359 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
Bram Moolenaar33570922005-01-25 22:26:29 +0000361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200363 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
Bram Moolenaar37c83712020-06-30 21:18:36 +0200365 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 if (skip)
367 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200368 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 retval = NULL;
370 else
371 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100372 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
375 if (skip)
376 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200377 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378
379 return retval;
380}
381
382/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000383 * Skip over an expression at "*pp".
384 * Return FAIL for an error, OK otherwise.
385 */
386 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200387skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000388{
Bram Moolenaar33570922005-01-25 22:26:29 +0000389 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000390
391 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200392 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000393}
394
395/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200396 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397 * If in Vim9 script and line breaks are encountered, the lines are
398 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200399 * "arg" is advanced to just after the expression.
400 * "start" is set to the start of the expression, "end" to just after the end.
401 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 * Return FAIL for an error, OK otherwise.
403 */
404 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200405skip_expr_concatenate(
406 char_u **arg,
407 char_u **start,
408 char_u **end,
409 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410{
411 typval_T rettv;
412 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200413 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200414 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200416 int evaluate = evalarg == NULL
417 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200418
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200419 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200421 {
422 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200425 ++gap->ga_len;
426 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200427 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200428
429 // Don't evaluate the expression.
430 if (evalarg != NULL)
431 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200432 *arg = skipwhite(*arg);
433 res = eval1(arg, &rettv, evalarg);
434 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435 if (evalarg != NULL)
436 evalarg->eval_flags = save_flags;
437
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200438 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200439 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200440 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200441 if (evalarg->eval_ga.ga_len == 1)
442 {
443 // just one line, no need to concatenate
444 ga_clear(gap);
445 gap->ga_itemsize = 0;
446 }
447 else
448 {
449 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200450 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200451
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200452 // Line breaks encountered, concatenate all the lines.
453 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200454 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200455
456 // free the lines only when using getsourceline()
457 if (evalarg->eval_cookie != NULL)
458 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200460 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200461 // Do not free the last line, "arg" points into it, free it
462 // later.
463 vim_free(evalarg->eval_tofree);
464 evalarg->eval_tofree =
465 ((char_u **)gap->ga_data)[gap->ga_len - 1];
466 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200467 ga_clear_strings(gap);
468 }
469 else
470 ga_clear(gap);
471 gap->ga_itemsize = 0;
472 if (p == NULL)
473 return FAIL;
474 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200475 vim_free(evalarg->eval_tofree_lambda);
476 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200477 // Compute "end" relative to the end.
478 *end = *start + STRLEN(*start) - endoff;
479 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200480 }
481
482 return res;
483}
484
485/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100486 * Convert "tv" to a string.
487 * When "convert" is TRUE convert a List into a sequence of lines and convert
488 * a Float to a String.
489 * Returns an allocated string (NULL when out of memory).
490 */
491 char_u *
492typval2string(typval_T *tv, int convert)
493{
494 garray_T ga;
495 char_u *retval;
496#ifdef FEAT_FLOAT
497 char_u numbuf[NUMBUFLEN];
498#endif
499
500 if (convert && tv->v_type == VAR_LIST)
501 {
502 ga_init2(&ga, (int)sizeof(char), 80);
503 if (tv->vval.v_list != NULL)
504 {
505 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
506 if (tv->vval.v_list->lv_len > 0)
507 ga_append(&ga, NL);
508 }
509 ga_append(&ga, NUL);
510 retval = (char_u *)ga.ga_data;
511 }
512#ifdef FEAT_FLOAT
513 else if (convert && tv->v_type == VAR_FLOAT)
514 {
515 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
516 retval = vim_strsave(numbuf);
517 }
518#endif
519 else
520 retval = vim_strsave(tv_get_string(tv));
521 return retval;
522}
523
524/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 * Top level evaluation function, returning a string. Does not handle line
526 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000527 * When "convert" is TRUE convert a List into a sequence of lines and convert
528 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 * Return pointer to allocated memory, or NULL for failure.
530 */
531 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100532eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100533 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100534 int convert,
535 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaar33570922005-01-25 22:26:29 +0000537 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100539 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100541 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
542 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 retval = NULL;
544 else
545 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100546 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000547 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100549 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
551 return retval;
552}
553
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100554 char_u *
555eval_to_string(
556 char_u *arg,
557 int convert)
558{
559 return eval_to_string_eap(arg, convert, NULL);
560}
561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000563 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200564 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200565 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 */
567 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100568eval_to_string_safe(
569 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100570 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571{
572 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200573 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200574 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200576 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200577 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000578 if (use_sandbox)
579 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200580 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200581 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000582 if (use_sandbox)
583 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200584 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200585 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200586 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 return retval;
588}
589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590/*
591 * Top level evaluation function, returning a number.
592 * Evaluates "expr" silently.
593 * Returns -1 for an error.
594 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200595 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100596eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
Bram Moolenaar33570922005-01-25 22:26:29 +0000598 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200599 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000600 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
602 ++emsg_off;
603
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200604 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 retval = -1;
606 else
607 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100608 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000609 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
611 --emsg_off;
612
613 return retval;
614}
615
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000616/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000617 * Top level evaluation function.
618 * Returns an allocated typval_T with the result.
619 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 */
621 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200622eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000623{
624 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200625 evalarg_T evalarg;
626
627 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000628
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200629 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200630 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100631 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000632
Bram Moolenaar37c83712020-06-30 21:18:36 +0200633 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000634 return tv;
635}
636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100638 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200639 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
640 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000641 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100644call_vim_function(
645 char_u *func,
646 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200647 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200648 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000650 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200651 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100653 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200654 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200655 funcexe.firstline = curwin->w_cursor.lnum;
656 funcexe.lastline = curwin->w_cursor.lnum;
657 funcexe.evaluate = TRUE;
658 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000659 if (ret == FAIL)
660 clear_tv(rettv);
661
662 return ret;
663}
664
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100665/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100666 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200668 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
669 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100670 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200671 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100672call_func_retnr(
673 char_u *func,
674 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200675 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100676{
677 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200678 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100679
Bram Moolenaarded27a12018-08-01 19:06:03 +0200680 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100681 return -1;
682
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100683 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100684 clear_tv(&rettv);
685 return retval;
686}
687
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000688/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100689 * Call Vim script function like call_func_retnr() and drop the result.
690 * Returns FAIL when calling the function fails.
691 */
692 int
693call_func_noret(
694 char_u *func,
695 int argc,
696 typval_T *argv)
697{
698 typval_T rettv;
699
700 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
701 return FAIL;
702 clear_tv(&rettv);
703 return OK;
704}
705
706/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100707 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100708 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000709 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000710 */
711 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100712call_func_retstr(
713 char_u *func,
714 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200715 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716{
717 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000718 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000719
Bram Moolenaarded27a12018-08-01 19:06:03 +0200720 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000721 return NULL;
722
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100723 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000724 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 return retval;
726}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000727
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000728/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100729 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100730 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000731 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000732 */
733 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100734call_func_retlist(
735 char_u *func,
736 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200737 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000738{
739 typval_T rettv;
740
Bram Moolenaarded27a12018-08-01 19:06:03 +0200741 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000742 return NULL;
743
744 if (rettv.v_type != VAR_LIST)
745 {
746 clear_tv(&rettv);
747 return NULL;
748 }
749
750 return rettv.vval.v_list;
751}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#ifdef FEAT_FOLDING
754/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100755 * Evaluate "arg", which is 'foldexpr'.
756 * Note: caller must set "curwin" to match "arg".
757 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
758 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 */
760 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100761eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
Bram Moolenaar33570922005-01-25 22:26:29 +0000763 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200764 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000766 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
767 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000770 if (use_sandbox)
771 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200772 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200774 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 retval = 0;
776 else
777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100778 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 if (tv.v_type == VAR_NUMBER)
780 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000781 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 retval = 0;
783 else
784 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100785 // If the result is a string, check if there is a non-digit before
786 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000787 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 if (!VIM_ISDIGIT(*s) && *s != '-')
789 *cp = *s++;
790 retval = atol((char *)s);
791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000792 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000795 if (use_sandbox)
796 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200797 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200798 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200800 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802#endif
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 * Get an lval: variable, Dict item or List item that can be assigned a value
806 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
807 * "name.key", "name.key[expr]" etc.
808 * Indexing only works if "name" is an existing List or Dictionary.
809 * "name" points to the start of the name.
810 * If "rettv" is not NULL it points to the value to be assigned.
811 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
812 * wrong; must end in space or cmd separator.
813 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100814 * flags:
815 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100816 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100817 * GLV_NO_AUTOLOAD: do not use script autoloading
818 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000820 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200823 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100824get_lval(
825 char_u *name,
826 typval_T *rettv,
827 lval_T *lp,
828 int unlet,
829 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100830 int flags, // GLV_ values
831 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000833 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834 char_u *expr_start, *expr_end;
835 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000836 dictitem_T *v;
837 typval_T var1;
838 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000840 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000842 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100843 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100844 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100845 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100847 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200848 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100850 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100852 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000853 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200854 lp->ll_name_end = find_name_end(name, NULL, NULL,
855 FNE_INCL_BR | fne_flags);
856 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 }
858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100859 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000860 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 if (expr_start != NULL)
863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100864 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100865 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 && *p != '[' && *p != '.')
867 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200868 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 return NULL;
870 }
871
872 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
873 if (lp->ll_exp_name == NULL)
874 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100875 // Report an invalid expression in braces, unless the
876 // expression evaluation has been cancelled due to an
877 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 if (!aborting() && !quiet)
879 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000880 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100881 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 return NULL;
883 }
884 }
885 lp->ll_name = lp->ll_exp_name;
886 }
887 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 lp->ll_name = name;
890
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200891 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200893 // "a: type" is declaring variable "a" with a type, not "a:".
894 if (p == name + 2 && p[-1] == ':')
895 {
896 --p;
897 lp->ll_name_end = p;
898 }
899 if (*p == ':')
900 {
901 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
902 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200904 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100905 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
906 if (lp->ll_type == NULL && !quiet)
907 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200908 lp->ll_name_end = tp;
909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910 }
911 }
912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100913 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000914 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
915 return p;
916
917 cc = *p;
918 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100919 // When we would write to the variable pass &ht and prevent autoload.
920 writing = !(flags & GLV_READ_ONLY);
921 v = find_var(lp->ll_name, writing ? &ht : NULL,
922 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000923 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200924 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000925 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000926 if (v == NULL)
927 return NULL;
928
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100929 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
930 {
931 if (!quiet)
932 semsg(_(e_variable_already_declared), lp->ll_name);
933 return NULL;
934 }
935
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000936 /*
937 * Loop until no more [idx] or .key is following.
938 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100940 var1.v_type = VAR_UNKNOWN;
941 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200945 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100946 && !(lp->ll_tv->v_type == VAR_BLOB
947 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100950 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000952 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100956 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000957 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000958 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000959
Bram Moolenaar10c65862020-10-08 21:16:42 +0200960 if (in_vim9script() && lp->ll_valtype == NULL
961 && lp->ll_tv == &v->di_tv
962 && ht != NULL && ht == get_script_local_ht())
963 {
964 svar_T *sv = find_typval_in_script(lp->ll_tv);
965
966 // Vim9 script local variable: get the type
967 if (sv != NULL)
968 lp->ll_valtype = sv->sv_type;
969 }
970
Bram Moolenaar8c711452005-01-14 21:53:12 +0000971 len = -1;
972 if (*p == '.')
973 {
974 key = p + 1;
975 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
976 ;
977 if (len == 0)
978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100980 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000981 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000982 }
983 p = key + len;
984 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000985 else
986 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100987 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000988 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000989 if (*p == ':')
990 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000991 else
992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200994 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000995 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100996 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000997 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000999 clear_tv(&var1);
1000 return NULL;
1001 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001002 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 }
1004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001005 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 if (*p == ':')
1007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001011 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001012 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001013 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001014 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001015 if (rettv != NULL
1016 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001017 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001018 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001019 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001022 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001023 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 }
1026 p = skipwhite(p + 1);
1027 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 else
1030 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001032 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001033 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 {
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 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001038 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001040 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001041 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001042 clear_tv(&var2);
1043 return NULL;
1044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001047 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001050
Bram Moolenaar8c711452005-01-14 21:53:12 +00001051 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001052 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001053 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001054 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001055 clear_tv(&var1);
1056 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001058 }
1059
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001060 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001061 ++p;
1062 }
1063
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 {
1066 if (len == -1)
1067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001068 // "[key]": get key from "var1"
1069 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001070 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001074 }
1075 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001076 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001077
1078 // a NULL dict is equivalent with an empty dict
1079 if (lp->ll_tv->vval.v_dict == NULL)
1080 {
1081 lp->ll_tv->vval.v_dict = dict_alloc();
1082 if (lp->ll_tv->vval.v_dict == NULL)
1083 {
1084 clear_tv(&var1);
1085 return NULL;
1086 }
1087 ++lp->ll_tv->vval.v_dict->dv_refcount;
1088 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001089 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001090
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001091 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001093 // When assigning to a scope dictionary check that a function and
1094 // variable name is valid (only variable name unless it is l: or
1095 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001096 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001097 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001098 int prevval;
1099 int wrong;
1100
1101 if (len != -1)
1102 {
1103 prevval = key[len];
1104 key[len] = NUL;
1105 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001106 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001107 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001108 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1109 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001110 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001111 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001112 if (len != -1)
1113 key[len] = prevval;
1114 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001115 {
1116 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001117 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001118 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001119 }
1120
Bram Moolenaar10c65862020-10-08 21:16:42 +02001121 if (lp->ll_valtype != NULL)
1122 // use the type of the member
1123 lp->ll_valtype = lp->ll_valtype->tt_member;
1124
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001126 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001127 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001128 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001129 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001131 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001132 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001133 return NULL;
1134 }
1135
Bram Moolenaar31b81602019-02-10 22:14:27 +01001136 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001138 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001140 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001141 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 }
1144 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001148 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 p = NULL;
1151 break;
1152 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001153 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001154 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001155 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1156 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001157 {
1158 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001159 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001160 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001161
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001162 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001163 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001164 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001165 else if (lp->ll_tv->v_type == VAR_BLOB)
1166 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001167 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1168
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001169 /*
1170 * Get the number and item for the only or first index of the List.
1171 */
1172 if (empty1)
1173 lp->ll_n1 = 0;
1174 else
1175 // is number or string
1176 lp->ll_n1 = (long)tv_get_number(&var1);
1177 clear_tv(&var1);
1178
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001179 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001180 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001181 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001182 return NULL;
1183 }
1184 if (lp->ll_range && !lp->ll_empty2)
1185 {
1186 lp->ll_n2 = (long)tv_get_number(&var2);
1187 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001188 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1189 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001190 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001191 }
1192 lp->ll_blob = lp->ll_tv->vval.v_blob;
1193 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001194 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001195 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001196 else
1197 {
1198 /*
1199 * Get the number and item for the only or first index of the List.
1200 */
1201 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001203 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001204 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001205 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001206 clear_tv(&var1);
1207
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001208 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001210 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001211 if (lp->ll_li == NULL)
1212 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001213 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001214 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001215 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001217 }
1218
Bram Moolenaar10c65862020-10-08 21:16:42 +02001219 if (lp->ll_valtype != NULL)
1220 // use the type of the member
1221 lp->ll_valtype = lp->ll_valtype->tt_member;
1222
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 /*
1224 * May need to find the item or absolute index for the second
1225 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 * When no index given: "lp->ll_empty2" is TRUE.
1227 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001228 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001231 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001232 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001233 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001238 {
1239 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001240 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001242 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 }
1245
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001246 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 if (lp->ll_n1 < 0)
1248 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1249 if (lp->ll_n2 < lp->ll_n1)
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 Moolenaar6cc16192005-01-08 21:49:45 +00001255 }
1256
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001258 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259 }
1260
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001261 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 return p;
1264}
1265
1266/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001268 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001270clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271{
1272 vim_free(lp->ll_exp_name);
1273 vim_free(lp->ll_newkey);
1274}
1275
1276/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001277 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001278 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001279 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1280 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001281 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001283set_var_lval(
1284 lval_T *lp,
1285 char_u *endp,
1286 typval_T *rettv,
1287 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001288 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1289 char_u *op,
1290 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291{
1292 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001293 listitem_T *ri;
1294 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295
1296 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001298 cc = *endp;
1299 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300 if (lp->ll_blob != NULL)
1301 {
1302 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001303
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 if (op != NULL && *op != '=')
1305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001306 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 return;
1308 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001309 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001310 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311
1312 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1313 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001314 if (lp->ll_empty2)
1315 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1316
Bram Moolenaar68452172021-04-12 21:21:02 +02001317 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1318 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001320 }
1321 else
1322 {
1323 val = (int)tv_get_number_chk(rettv, &error);
1324 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001325 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326 }
1327 }
1328 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001330 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001331
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001332 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1333 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001334 {
1335 emsg(_(e_cannot_mod));
1336 *endp = cc;
1337 return;
1338 }
1339
Bram Moolenaarff697e62019-02-12 22:28:33 +01001340 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001341 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001342 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001343 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001344 {
1345 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001346 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1347 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001348 && tv_op(&tv, rettv, op) == OK)
1349 set_var(lp->ll_name, &tv, FALSE);
1350 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001351 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001353 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001354 {
1355 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001356 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001357 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001358 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1359 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001360 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001361 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001362 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001363 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001364 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001365 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001366 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001367 else if (lp->ll_range)
1368 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001369 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001370 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001371
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001372 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1373 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001374 {
1375 emsg(_("E996: Cannot lock a range"));
1376 return;
1377 }
1378
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001379 /*
1380 * Check whether any of the list items is locked
1381 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001382 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001383 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001384 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001385 return;
1386 ri = ri->li_next;
1387 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1388 break;
1389 ll_li = ll_li->li_next;
1390 ++ll_n1;
1391 }
1392
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001393 /*
1394 * Assign the List values to the list items.
1395 */
1396 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001397 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001398 if (op != NULL && *op != '=')
1399 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1400 else
1401 {
1402 clear_tv(&lp->ll_li->li_tv);
1403 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1404 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 ri = ri->li_next;
1406 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1407 break;
1408 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001409 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001410 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001411 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001412 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 ri = NULL;
1414 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001415 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 lp->ll_li = lp->ll_li->li_next;
1418 ++lp->ll_n1;
1419 }
1420 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001421 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001422 else if (lp->ll_empty2
1423 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001425 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 }
1427 else
1428 {
1429 /*
1430 * Assign to a List or Dictionary item.
1431 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001432 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1433 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001434 {
1435 emsg(_("E996: Cannot lock a list or dict"));
1436 return;
1437 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001438
1439 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001440 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001441 return;
1442
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 if (lp->ll_newkey != NULL)
1444 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001445 if (op != NULL && *op != '=')
1446 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001447 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448 return;
1449 }
1450
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001451 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001452 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001453 if (di == NULL)
1454 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001455 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1456 {
1457 vim_free(di);
1458 return;
1459 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001460 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001461 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001462 else if (op != NULL && *op != '=')
1463 {
1464 tv_op(lp->ll_tv, rettv, op);
1465 return;
1466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001468 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001469
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001470 /*
1471 * Assign the value to the variable or list item.
1472 */
1473 if (copy)
1474 copy_tv(rettv, lp->ll_tv);
1475 else
1476 {
1477 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001478 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001480 }
1481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482}
1483
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001484/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001485 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1486 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001487 * Returns OK or FAIL.
1488 */
1489 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001490tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001491{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001492 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001493 char_u numbuf[NUMBUFLEN];
1494 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001495 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001497 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001498 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001499 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001500 {
1501 switch (tv1->v_type)
1502 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001503 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001504 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 case VAR_DICT:
1507 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001508 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001509 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001510 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001511 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001512 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513 break;
1514
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001515 case VAR_BLOB:
1516 if (*op != '+' || tv2->v_type != VAR_BLOB)
1517 break;
1518 // BLOB += BLOB
1519 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1520 {
1521 blob_T *b1 = tv1->vval.v_blob;
1522 blob_T *b2 = tv2->vval.v_blob;
1523 int i, len = blob_len(b2);
1524 for (i = 0; i < len; i++)
1525 ga_append(&b1->bv_ga, blob_get(b2, i));
1526 }
1527 return OK;
1528
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 case VAR_LIST:
1530 if (*op != '+' || tv2->v_type != VAR_LIST)
1531 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001532 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001533 if (tv2->vval.v_list != NULL)
1534 {
1535 if (tv1->vval.v_list == NULL)
1536 {
1537 tv1->vval.v_list = tv2->vval.v_list;
1538 ++tv1->vval.v_list->lv_refcount;
1539 }
1540 else
1541 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1542 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001543 return OK;
1544
1545 case VAR_NUMBER:
1546 case VAR_STRING:
1547 if (tv2->v_type == VAR_LIST)
1548 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001549 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001551 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001552 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001553#ifdef FEAT_FLOAT
1554 if (tv2->v_type == VAR_FLOAT)
1555 {
1556 float_T f = n;
1557
Bram Moolenaarff697e62019-02-12 22:28:33 +01001558 if (*op == '%')
1559 break;
1560 switch (*op)
1561 {
1562 case '+': f += tv2->vval.v_float; break;
1563 case '-': f -= tv2->vval.v_float; break;
1564 case '*': f *= tv2->vval.v_float; break;
1565 case '/': f /= tv2->vval.v_float; break;
1566 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567 clear_tv(tv1);
1568 tv1->v_type = VAR_FLOAT;
1569 tv1->vval.v_float = f;
1570 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001572#endif
1573 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001574 switch (*op)
1575 {
1576 case '+': n += tv_get_number(tv2); break;
1577 case '-': n -= tv_get_number(tv2); break;
1578 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001579 case '/': n = num_divide(n, tv_get_number(tv2),
1580 &failed); break;
1581 case '%': n = num_modulus(n, tv_get_number(tv2),
1582 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001583 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 clear_tv(tv1);
1585 tv1->v_type = VAR_NUMBER;
1586 tv1->vval.v_number = n;
1587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588 }
1589 else
1590 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591 if (tv2->v_type == VAR_FLOAT)
1592 break;
1593
Bram Moolenaarff697e62019-02-12 22:28:33 +01001594 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001595 s = tv_get_string(tv1);
1596 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001597 clear_tv(tv1);
1598 tv1->v_type = VAR_STRING;
1599 tv1->vval.v_string = s;
1600 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001601 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001604#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001605 {
1606 float_T f;
1607
Bram Moolenaarff697e62019-02-12 22:28:33 +01001608 if (*op == '%' || *op == '.'
1609 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001610 && tv2->v_type != VAR_NUMBER
1611 && tv2->v_type != VAR_STRING))
1612 break;
1613 if (tv2->v_type == VAR_FLOAT)
1614 f = tv2->vval.v_float;
1615 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001616 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001617 switch (*op)
1618 {
1619 case '+': tv1->vval.v_float += f; break;
1620 case '-': tv1->vval.v_float -= f; break;
1621 case '*': tv1->vval.v_float *= f; break;
1622 case '/': tv1->vval.v_float /= f; break;
1623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001626 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 }
1628 }
1629
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001630 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 return FAIL;
1632}
1633
1634/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 * Evaluate the expression used in a ":for var in expr" command.
1636 * "arg" points to "var".
1637 * Set "*errp" to TRUE for an error, FALSE otherwise;
1638 * Return a pointer that holds the info. Null when there is an error.
1639 */
1640 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001641eval_for_line(
1642 char_u *arg,
1643 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001644 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001645 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646{
Bram Moolenaar33570922005-01-25 22:26:29 +00001647 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001649 typval_T tv;
1650 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001651 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001653 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001655 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 if (fi == NULL)
1657 return NULL;
1658
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001659 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 if (expr == NULL)
1661 return fi;
1662
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001663 expr = skipwhite_and_linebreak(expr, evalarg);
1664 if (expr[0] != 'i' || expr[1] != 'n'
1665 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 return fi;
1669 }
1670
1671 if (skip)
1672 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001673 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1674 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 {
1676 *errp = FALSE;
1677 if (!skip)
1678 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001680 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001681 l = tv.vval.v_list;
1682 if (l == NULL)
1683 {
1684 // a null list is like an empty list: do nothing
1685 clear_tv(&tv);
1686 }
1687 else
1688 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001690 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001692 // No need to increment the refcount, it's already set for
1693 // the list being used in "tv".
1694 fi->fi_list = l;
1695 list_add_watch(l, &fi->fi_lw);
1696 fi->fi_lw.lw_item = l->lv_first;
1697 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001698 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001699 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001700 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001701 fi->fi_bi = 0;
1702 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001703 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001704 typval_T btv;
1705
1706 // Make a copy, so that the iteration still works when the
1707 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001709 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001710 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001711 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001712 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001713 else if (tv.v_type == VAR_STRING)
1714 {
1715 fi->fi_byte_idx = 0;
1716 fi->fi_string = tv.vval.v_string;
1717 tv.vval.v_string = NULL;
1718 if (fi->fi_string == NULL)
1719 fi->fi_string = vim_strsave((char_u *)"");
1720 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721 else
1722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001723 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001724 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 }
1726 }
1727 }
1728 if (skip)
1729 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001730 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731
1732 return fi;
1733}
1734
1735/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001736 * Used when looping over a :for line, skip the "in expr" part.
1737 */
1738 void
1739skip_for_lines(void *fi_void, evalarg_T *evalarg)
1740{
1741 forinfo_T *fi = (forinfo_T *)fi_void;
1742 int i;
1743
1744 for (i = 0; i < fi->fi_break_count; ++i)
1745 eval_next_line(evalarg);
1746}
1747
1748/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 * Use the first item in a ":for" list. Advance to the next.
1750 * Assign the values to the variable (list). "arg" points to the first one.
1751 * Return TRUE when a valid item was found, FALSE when at end of list or
1752 * something wrong.
1753 */
1754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001755next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001757 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001759 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1760 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1761 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001762 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001764 if (fi->fi_blob != NULL)
1765 {
1766 typval_T tv;
1767
1768 if (fi->fi_bi >= blob_len(fi->fi_blob))
1769 return FALSE;
1770 tv.v_type = VAR_NUMBER;
1771 tv.v_lock = VAR_FIXED;
1772 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1773 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001774 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001775 fi->fi_varcount, flag, NULL) == OK;
1776 }
1777
1778 if (fi->fi_string != NULL)
1779 {
1780 typval_T tv;
1781 int len;
1782
1783 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1784 if (len == 0)
1785 return FALSE;
1786 tv.v_type = VAR_STRING;
1787 tv.v_lock = VAR_FIXED;
1788 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1789 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001790 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001791 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001792 vim_free(tv.vval.v_string);
1793 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001794 }
1795
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 item = fi->fi_lw.lw_item;
1797 if (item == NULL)
1798 result = FALSE;
1799 else
1800 {
1801 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001802 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001803 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 }
1805 return result;
1806}
1807
1808/*
1809 * Free the structure used to store info used by ":for".
1810 */
1811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813{
Bram Moolenaar33570922005-01-25 22:26:29 +00001814 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001816 if (fi == NULL)
1817 return;
1818 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001819 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001821 list_unref(fi->fi_list);
1822 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001823 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001824 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001825 else
1826 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827 vim_free(fi);
1828}
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001831set_context_for_expression(
1832 expand_T *xp,
1833 char_u *arg,
1834 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001836 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001840 if (cmdidx == CMD_let || cmdidx == CMD_var
1841 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 {
1843 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001844 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001846 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001847 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 {
1849 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001850 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001851 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 break;
1853 }
1854 return;
1855 }
1856 }
1857 else
1858 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1859 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 while ((xp->xp_pattern = vim_strpbrk(arg,
1861 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1862 {
1863 c = *xp->xp_pattern;
1864 if (c == '&')
1865 {
1866 c = xp->xp_pattern[1];
1867 if (c == '&')
1868 {
1869 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001870 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001875 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1876 xp->xp_pattern += 2;
1877
1878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880 else if (c == '$')
1881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001882 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 xp->xp_context = EXPAND_ENV_VARS;
1884 }
1885 else if (c == '=')
1886 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001887 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 xp->xp_context = EXPAND_EXPRESSION;
1889 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001890 else if (c == '#'
1891 && xp->xp_context == EXPAND_EXPRESSION)
1892 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001893 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001894 break;
1895 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001896 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 && xp->xp_context == EXPAND_FUNCTIONS
1898 && vim_strchr(xp->xp_pattern, '(') == NULL)
1899 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001900 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 break;
1902 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001903 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001905 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1908 if (c == '\\' && xp->xp_pattern[1] != NUL)
1909 ++xp->xp_pattern;
1910 xp->xp_context = EXPAND_NOTHING;
1911 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001912 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001914 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1916 /* skip */ ;
1917 xp->xp_context = EXPAND_NOTHING;
1918 }
1919 else if (c == '|')
1920 {
1921 if (xp->xp_pattern[1] == '|')
1922 {
1923 ++xp->xp_pattern;
1924 xp->xp_context = EXPAND_EXPRESSION;
1925 }
1926 else
1927 xp->xp_context = EXPAND_COMMANDS;
1928 }
1929 else
1930 xp->xp_context = EXPAND_EXPRESSION;
1931 }
1932 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001933 // Doesn't look like something valid, expand as an expression
1934 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 arg = xp->xp_pattern;
1937 if (*arg != NUL)
1938 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1939 /* skip */ ;
1940 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001941
1942 // ":exe one two" completes "two"
1943 if ((cmdidx == CMD_execute
1944 || cmdidx == CMD_echo
1945 || cmdidx == CMD_echon
1946 || cmdidx == CMD_echomsg)
1947 && xp->xp_context == EXPAND_EXPRESSION)
1948 {
1949 for (;;)
1950 {
1951 char_u *n = skiptowhite(arg);
1952
1953 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1954 break;
1955 arg = skipwhite(n);
1956 }
1957 }
1958
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 xp->xp_pattern = arg;
1960}
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001963 * Return TRUE if "pat" matches "text".
1964 * Does not use 'cpo' and always uses 'magic'.
1965 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001966 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001967pattern_match(char_u *pat, char_u *text, int ic)
1968{
1969 int matches = FALSE;
1970 char_u *save_cpo;
1971 regmatch_T regmatch;
1972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001973 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001974 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001975 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001976 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1977 if (regmatch.regprog != NULL)
1978 {
1979 regmatch.rm_ic = ic;
1980 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1981 vim_regfree(regmatch.regprog);
1982 }
1983 p_cpo = save_cpo;
1984 return matches;
1985}
1986
1987/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001988 * Handle a name followed by "(". Both for just "name(arg)" and for
1989 * "expr->name(arg)".
1990 * Returns OK or FAIL.
1991 */
1992 static int
1993eval_func(
1994 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001995 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001996 char_u *name,
1997 int name_len,
1998 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001999 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002000 typval_T *basetv) // "expr" for "expr->name(arg)"
2001{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002002 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002003 char_u *s = name;
2004 int len = name_len;
2005 partial_T *partial;
2006 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002007 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002008
2009 if (!evaluate)
2010 check_vars(s, len);
2011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002012 // If "s" is the name of a variable of type VAR_FUNC
2013 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002014 s = deref_func_name(s, &len, &partial,
2015 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002017 // Need to make a copy, in case evaluating the arguments makes
2018 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002019 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002020 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021 ret = FAIL;
2022 else
2023 {
2024 funcexe_T funcexe;
2025
2026 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002027 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002028 funcexe.firstline = curwin->w_cursor.lnum;
2029 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002030 funcexe.evaluate = evaluate;
2031 funcexe.partial = partial;
2032 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002033 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002034 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002035 }
2036 vim_free(s);
2037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002038 // If evaluate is FALSE rettv->v_type was not set in
2039 // get_func_tv, but it's needed in handle_subscript() to parse
2040 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002041 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2042 {
2043 rettv->vval.v_string = NULL;
2044 rettv->v_type = VAR_FUNC;
2045 }
2046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002047 // Stop the expression evaluation when immediately
2048 // aborting on error, or when an interrupt occurred or
2049 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002050 if (evaluate && aborting())
2051 {
2052 if (ret == OK)
2053 clear_tv(rettv);
2054 ret = FAIL;
2055 }
2056 return ret;
2057}
2058
2059/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002060 * Get the next line source line without advancing. But do skip over comment
2061 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002062 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002063 */
2064 static char_u *
2065getline_peek_skip_comments(evalarg_T *evalarg)
2066{
2067 for (;;)
2068 {
2069 char_u *next = getline_peek(evalarg->eval_getline,
2070 evalarg->eval_cookie);
2071 char_u *p;
2072
2073 if (next == NULL)
2074 break;
2075 p = skipwhite(next);
2076 if (*p != NUL && !vim9_comment_start(p))
2077 return next;
2078 (void)eval_next_line(evalarg);
2079 }
2080 return NULL;
2081}
2082
2083/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002084 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2085 * comment) and there is a next line, return the next line (skipping blanks)
2086 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002087 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2088 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002089 * "arg" must point somewhere inside a line, not at the start.
2090 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002091 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002092eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2093{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002094 char_u *p = skipwhite(arg);
2095
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002096 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002097 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002099 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002100 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002102 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002103
2104 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002105 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002106 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002107 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110 {
2111 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002112 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113 }
2114 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002115 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116}
2117
2118/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002119 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002120 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002121 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002122 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002123eval_next_line(evalarg_T *evalarg)
2124{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002125 garray_T *gap = &evalarg->eval_ga;
2126 char_u *line;
2127
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002128 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002129 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2130 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002131 else
2132 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002133 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002134 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2135 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002136 char_u *p = skipwhite(line);
2137
2138 // Going to concatenate the lines after parsing. For an empty or
2139 // comment line use an empty string.
2140 if (*p == NUL || vim9_comment_start(p))
2141 {
2142 vim_free(line);
2143 line = vim_strsave((char_u *)"");
2144 }
2145
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002146 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2147 ++gap->ga_len;
2148 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002149 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002150 {
2151 vim_free(evalarg->eval_tofree);
2152 evalarg->eval_tofree = line;
2153 }
2154 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002155}
2156
Bram Moolenaare6b53242020-07-01 17:28:33 +02002157/*
2158 * Call eval_next_non_blank() and get the next line if needed.
2159 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002160 char_u *
2161skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2162{
2163 int getnext;
2164 char_u *p = skipwhite(arg);
2165
Bram Moolenaar962d7212020-07-04 14:15:00 +02002166 if (evalarg == NULL)
2167 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002168 eval_next_non_blank(p, evalarg, &getnext);
2169 if (getnext)
2170 return eval_next_line(evalarg);
2171 return p;
2172}
2173
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002174/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002175 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002176 */
2177 void
2178clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2179{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002180 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002181 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002182 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002183 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002184 if (eap != NULL)
2185 {
2186 // We may need to keep the original command line, e.g. for
2187 // ":let" it has the variable names. But we may also need the
2188 // new one, "nextcmd" points into it. Keep both.
2189 vim_free(eap->cmdline_tofree);
2190 eap->cmdline_tofree = *eap->cmdlinep;
2191 *eap->cmdlinep = evalarg->eval_tofree;
2192 }
2193 else
2194 vim_free(evalarg->eval_tofree);
2195 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002196 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002197
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002198 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2199 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002200 }
2201}
2202
2203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2207 */
2208
2209/*
2210 * Handle zero level expression.
2211 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002213 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002214 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 * Return OK or FAIL.
2216 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002217 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218eval0(
2219 char_u *arg,
2220 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002221 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002222 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224 int ret;
2225 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002226 int did_emsg_before = did_emsg;
2227 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002228 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002231 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002232 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002233
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002234 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 /*
2239 * Report the invalid expression unless the expression evaluation has
2240 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002241 * exception, or we already gave a more specific error.
2242 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002244 if (!aborting()
2245 && did_emsg == did_emsg_before
2246 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002247 && (flags & EVAL_CONSTANT) == 0
2248 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002249 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002250
2251 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002252 // a next command to avoid more errors, unless "|" is following, which
2253 // could only be a command separator.
2254 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2255 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002256 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002258
2259 if (eap != NULL)
2260 eap->nextcmd = check_nextcmd(p);
2261
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 return ret;
2263}
2264
2265/*
2266 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002267 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002268 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 *
2270 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002271 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002273 * Note: "rettv.v_lock" is not set.
2274 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 * Return OK or FAIL.
2276 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002277 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002278eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002280 char_u *p;
2281 int getnext;
2282
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002283 CLEAR_POINTER(rettv);
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 /*
2286 * Get the first variable.
2287 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002288 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 return FAIL;
2290
Bram Moolenaar793648f2020-06-26 21:28:25 +02002291 p = eval_next_non_blank(*arg, evalarg, &getnext);
2292 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002294 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002295 int result;
2296 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002297 evalarg_T *evalarg_used = evalarg;
2298 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002299 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002300 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002301 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002302
2303 if (evalarg == NULL)
2304 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002305 CLEAR_FIELD(local_evalarg);
2306 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002308 orig_flags = evalarg_used->eval_flags;
2309 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002310
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002311 if (getnext)
2312 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002313 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002314 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002315 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002316 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002317 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002318 clear_tv(rettv);
2319 return FAIL;
2320 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002321 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002322 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002327 int error = FALSE;
2328
Bram Moolenaar13106602020-10-04 16:06:05 +02002329 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002330 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002331 else if (vim9script)
2332 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002333 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002335 if (error || !op_falsy || !result)
2336 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002337 if (error)
2338 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340
2341 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002342 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002344 if (op_falsy)
2345 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002346 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002347 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002348 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002349 clear_tv(rettv);
2350 return FAIL;
2351 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002352 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002353 evalarg_used->eval_flags = (op_falsy ? !result : result)
2354 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2355 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002356 {
2357 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002359 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002360 if (!op_falsy || !result)
2361 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002363 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002365 /*
2366 * Check for the ":".
2367 */
2368 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2369 if (*p != ':')
2370 {
2371 emsg(_(e_missing_colon));
2372 if (evaluate && result)
2373 clear_tv(rettv);
2374 evalarg_used->eval_flags = orig_flags;
2375 return FAIL;
2376 }
2377 if (getnext)
2378 *arg = eval_next_line(evalarg_used);
2379 else
2380 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002381 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002382 {
2383 error_white_both(p, 1);
2384 clear_tv(rettv);
2385 evalarg_used->eval_flags = orig_flags;
2386 return FAIL;
2387 }
2388 *arg = p;
2389 }
2390
2391 /*
2392 * Get the third variable. Recursive!
2393 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002394 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002395 {
2396 error_white_both(p, 1);
2397 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002398 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002399 return FAIL;
2400 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002401 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2402 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002403 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002404 if (eval1(arg, &var2, evalarg_used) == FAIL)
2405 {
2406 if (evaluate && result)
2407 clear_tv(rettv);
2408 evalarg_used->eval_flags = orig_flags;
2409 return FAIL;
2410 }
2411 if (evaluate && !result)
2412 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002414
2415 if (evalarg == NULL)
2416 clear_evalarg(&local_evalarg, NULL);
2417 else
2418 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420
2421 return OK;
2422}
2423
2424/*
2425 * Handle first level expression:
2426 * expr2 || expr2 || expr2 logical OR
2427 *
2428 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002429 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 *
2431 * Return OK or FAIL.
2432 */
2433 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002436 char_u *p;
2437 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439 /*
2440 * Get the first variable.
2441 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 return FAIL;
2444
2445 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002446 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002448 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002449 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002451 evalarg_T *evalarg_used = evalarg;
2452 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 int evaluate;
2454 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002455 long result = FALSE;
2456 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002457 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002458 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002459
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002460 if (evalarg == NULL)
2461 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462 CLEAR_FIELD(local_evalarg);
2463 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002464 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002465 orig_flags = evalarg_used->eval_flags;
2466 evaluate = orig_flags & EVAL_EVALUATE;
2467 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002468 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002469 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002470 result = tv_get_bool_chk(rettv, &error);
2471 else if (tv_get_number_chk(rettv, &error) != 0)
2472 result = TRUE;
2473 clear_tv(rettv);
2474 if (error)
2475 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477
2478 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002479 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 while (p[0] == '|' && p[1] == '|')
2482 {
2483 if (getnext)
2484 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002485 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002486 {
2487 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2488 {
2489 error_white_both(p, 2);
2490 clear_tv(rettv);
2491 return FAIL;
2492 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002493 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002494 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495
2496 /*
2497 * Get the second variable.
2498 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002499 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2500 {
2501 error_white_both(p, 2);
2502 clear_tv(rettv);
2503 return FAIL;
2504 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002505 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2506 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002507 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002508 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002509 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002510
2511 /*
2512 * Compute the result.
2513 */
2514 if (evaluate && !result)
2515 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002516 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002517 result = tv_get_bool_chk(&var2, &error);
2518 else if (tv_get_number_chk(&var2, &error) != 0)
2519 result = TRUE;
2520 clear_tv(&var2);
2521 if (error)
2522 return FAIL;
2523 }
2524 if (evaluate)
2525 {
2526 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002527 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002528 rettv->v_type = VAR_BOOL;
2529 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002530 }
2531 else
2532 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002533 rettv->v_type = VAR_NUMBER;
2534 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002535 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002536 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537
2538 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002540
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 if (evalarg == NULL)
2542 clear_evalarg(&local_evalarg, NULL);
2543 else
2544 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546
2547 return OK;
2548}
2549
2550/*
2551 * Handle second level expression:
2552 * expr3 && expr3 && expr3 logical AND
2553 *
2554 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002555 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 *
2557 * Return OK or FAIL.
2558 */
2559 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002560eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002562 char_u *p;
2563 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
2565 /*
2566 * Get the first variable.
2567 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002568 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 return FAIL;
2570
2571 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002572 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002574 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002575 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 evalarg_T *evalarg_used = evalarg;
2578 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 int orig_flags;
2580 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002581 long result = TRUE;
2582 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002583 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002584 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002585
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002586 if (evalarg == NULL)
2587 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588 CLEAR_FIELD(local_evalarg);
2589 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002590 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 orig_flags = evalarg_used->eval_flags;
2592 evaluate = orig_flags & EVAL_EVALUATE;
2593 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002594 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002595 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002596 result = tv_get_bool_chk(rettv, &error);
2597 else if (tv_get_number_chk(rettv, &error) == 0)
2598 result = FALSE;
2599 clear_tv(rettv);
2600 if (error)
2601 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
2603
2604 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002605 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002607 while (p[0] == '&' && p[1] == '&')
2608 {
2609 if (getnext)
2610 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002611 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002612 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002613 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002614 {
2615 error_white_both(p, 2);
2616 clear_tv(rettv);
2617 return FAIL;
2618 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002619 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002620 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621
2622 /*
2623 * Get the second variable.
2624 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002625 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2626 {
2627 error_white_both(p, 2);
2628 clear_tv(rettv);
2629 return FAIL;
2630 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002631 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2632 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002633 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002634 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002635 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637
2638 /*
2639 * Compute the result.
2640 */
2641 if (evaluate && result)
2642 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002643 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002644 result = tv_get_bool_chk(&var2, &error);
2645 else if (tv_get_number_chk(&var2, &error) == 0)
2646 result = FALSE;
2647 clear_tv(&var2);
2648 if (error)
2649 return FAIL;
2650 }
2651 if (evaluate)
2652 {
2653 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002654 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002655 rettv->v_type = VAR_BOOL;
2656 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002657 }
2658 else
2659 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002660 rettv->v_type = VAR_NUMBER;
2661 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002662 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002664
2665 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002667
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002668 if (evalarg == NULL)
2669 clear_evalarg(&local_evalarg, NULL);
2670 else
2671 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 }
2673
2674 return OK;
2675}
2676
2677/*
2678 * Handle third level expression:
2679 * var1 == var2
2680 * var1 =~ var2
2681 * var1 != var2
2682 * var1 !~ var2
2683 * var1 > var2
2684 * var1 >= var2
2685 * var1 < var2
2686 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002687 * var1 is var2
2688 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 *
2690 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002691 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 *
2693 * Return OK or FAIL.
2694 */
2695 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002696eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002699 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002700 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002702 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704 /*
2705 * Get the first variable.
2706 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002707 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 return FAIL;
2709
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002710 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002711 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
2713 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002714 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002716 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002718 typval_T var2;
2719 int ic;
2720 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002721 int evaluate = evalarg == NULL
2722 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002723
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002724 if (getnext)
2725 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002726 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2727 {
2728 error_white_both(p, len);
2729 clear_tv(rettv);
2730 return FAIL;
2731 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002732
Bram Moolenaar696ba232020-07-29 21:20:41 +02002733 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2734 {
2735 semsg(_(e_invexpr2), p);
2736 clear_tv(rettv);
2737 return FAIL;
2738 }
2739
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002740 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 if (p[len] == '?')
2742 {
2743 ic = TRUE;
2744 ++len;
2745 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002746 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 else if (p[len] == '#')
2748 {
2749 ic = FALSE;
2750 ++len;
2751 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002752 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002754 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
2756 /*
2757 * Get the second variable.
2758 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002759 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2760 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002761 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002762 clear_tv(rettv);
2763 return FAIL;
2764 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002765 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002766 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002768 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 return FAIL;
2770 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002771 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002772 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002773 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002774
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002775 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002776 {
2777 ret = FAIL;
2778 clear_tv(rettv);
2779 }
2780 else
2781 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002782 clear_tv(&var2);
2783 return ret;
2784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786
2787 return OK;
2788}
2789
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002790/*
2791 * Make a copy of blob "tv1" and append blob "tv2".
2792 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 void
2794eval_addblob(typval_T *tv1, typval_T *tv2)
2795{
2796 blob_T *b1 = tv1->vval.v_blob;
2797 blob_T *b2 = tv2->vval.v_blob;
2798 blob_T *b = blob_alloc();
2799 int i;
2800
2801 if (b != NULL)
2802 {
2803 for (i = 0; i < blob_len(b1); i++)
2804 ga_append(&b->bv_ga, blob_get(b1, i));
2805 for (i = 0; i < blob_len(b2); i++)
2806 ga_append(&b->bv_ga, blob_get(b2, i));
2807
2808 clear_tv(tv1);
2809 rettv_blob_set(tv1, b);
2810 }
2811}
2812
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002813/*
2814 * Make a copy of list "tv1" and append list "tv2".
2815 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 int
2817eval_addlist(typval_T *tv1, typval_T *tv2)
2818{
2819 typval_T var3;
2820
2821 // concatenate Lists
2822 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2823 {
2824 clear_tv(tv1);
2825 clear_tv(tv2);
2826 return FAIL;
2827 }
2828 clear_tv(tv1);
2829 *tv1 = var3;
2830 return OK;
2831}
2832
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833/*
2834 * Handle fourth level expression:
2835 * + number addition
2836 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002837 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002838 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 *
2840 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002841 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 *
2843 * Return OK or FAIL.
2844 */
2845 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002846eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 /*
2849 * Get the first variable.
2850 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 return FAIL;
2853
2854 /*
2855 * Repeat computing, until no '+', '-' or '.' is following.
2856 */
2857 for (;;)
2858 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002859 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002860 int getnext;
2861 char_u *p;
2862 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002863 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002864 int concat;
2865 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002866 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002867
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002868 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002869 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002870 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002871 p = eval_next_non_blank(*arg, evalarg, &getnext);
2872 op = *p;
2873 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002874 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2875 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002877 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2878 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002879
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002880 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002881 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002882 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002883 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002884 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002885 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002886 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002887 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002888 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002889 clear_tv(rettv);
2890 return FAIL;
2891 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002892 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002893 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002894 if ((op != '+' || (rettv->v_type != VAR_LIST
2895 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002896#ifdef FEAT_FLOAT
2897 && (op == '.' || rettv->v_type != VAR_FLOAT)
2898#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002899 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002900 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002901 int error = FALSE;
2902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002903 // For "list + ...", an illegal use of the first operand as
2904 // a number cannot be determined before evaluating the 2nd
2905 // operand: if this is also a list, all is ok.
2906 // For "something . ...", "something - ..." or "non-list + ...",
2907 // we know that the first operand needs to be a string or number
2908 // without evaluating the 2nd operand. So check before to avoid
2909 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002910 if (op != '.')
2911 tv_get_number_chk(rettv, &error);
2912 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002913 {
2914 clear_tv(rettv);
2915 return FAIL;
2916 }
2917 }
2918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 /*
2920 * Get the second variable.
2921 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002922 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002923 {
2924 error_white_both(p, oplen);
2925 clear_tv(rettv);
2926 return FAIL;
2927 }
2928 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002929 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002931 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 return FAIL;
2933 }
2934
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002935 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 {
2937 /*
2938 * Compute the result.
2939 */
2940 if (op == '.')
2941 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002942 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2943 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002944 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002945
Bram Moolenaar2e086612020-08-25 22:37:48 +02002946 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002947 || var2.v_type == VAR_CHANNEL
2948 || var2.v_type == VAR_JOB))
2949 emsg(_(e_inval_string));
2950#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002951 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002952 {
2953 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2954 var2.vval.v_float);
2955 s2 = buf2;
2956 }
2957#endif
2958 else
2959 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002960 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002961 {
2962 clear_tv(rettv);
2963 clear_tv(&var2);
2964 return FAIL;
2965 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002966 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967 clear_tv(rettv);
2968 rettv->v_type = VAR_STRING;
2969 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002971 else if (op == '+' && rettv->v_type == VAR_BLOB
2972 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002974 else if (op == '+' && rettv->v_type == VAR_LIST
2975 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002976 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002978 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 else
2981 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 int error = FALSE;
2983 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002984#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002985 float_T f1 = 0, f2 = 0;
2986
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002987 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002988 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002989 f1 = rettv->vval.v_float;
2990 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002991 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002992 else
2993#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002995 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002996 if (error)
2997 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002998 // This can only happen for "list + non-list" or
2999 // "blob + non-blob". For "non-list + ..." or
3000 // "something - ...", we returned before evaluating the
3001 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003002 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003003 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003004 return FAIL;
3005 }
3006#ifdef FEAT_FLOAT
3007 if (var2.v_type == VAR_FLOAT)
3008 f1 = n1;
3009#endif
3010 }
3011#ifdef FEAT_FLOAT
3012 if (var2.v_type == VAR_FLOAT)
3013 {
3014 f2 = var2.vval.v_float;
3015 n2 = 0;
3016 }
3017 else
3018#endif
3019 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003020 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003021 if (error)
3022 {
3023 clear_tv(rettv);
3024 clear_tv(&var2);
3025 return FAIL;
3026 }
3027#ifdef FEAT_FLOAT
3028 if (rettv->v_type == VAR_FLOAT)
3029 f2 = n2;
3030#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003031 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003032 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003033
3034#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003035 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003036 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3037 {
3038 if (op == '+')
3039 f1 = f1 + f2;
3040 else
3041 f1 = f1 - f2;
3042 rettv->v_type = VAR_FLOAT;
3043 rettv->vval.v_float = f1;
3044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046#endif
3047 {
3048 if (op == '+')
3049 n1 = n1 + n2;
3050 else
3051 n1 = n1 - n2;
3052 rettv->v_type = VAR_NUMBER;
3053 rettv->vval.v_number = n1;
3054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
3058 }
3059 return OK;
3060}
3061
3062/*
3063 * Handle fifth level expression:
3064 * * number multiplication
3065 * / number division
3066 * % number modulo
3067 *
3068 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003069 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 *
3071 * Return OK or FAIL.
3072 */
3073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003074eval6(
3075 char_u **arg,
3076 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003077 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003078 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003080#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003081 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 /*
3085 * Get the first variable.
3086 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003087 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 return FAIL;
3089
3090 /*
3091 * Repeat computing, until no '*', '/' or '%' is following.
3092 */
3093 for (;;)
3094 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003095 int evaluate;
3096 int getnext;
3097 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003098 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003099 int op;
3100 varnumber_T n1, n2;
3101#ifdef FEAT_FLOAT
3102 float_T f1, f2;
3103#endif
3104 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003105
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003106 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003107 p = eval_next_non_blank(*arg, evalarg, &getnext);
3108 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003109 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003111
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003112 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003113 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003114 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003115 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003116 {
3117 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3118 {
3119 error_white_both(p, 1);
3120 clear_tv(rettv);
3121 return FAIL;
3122 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003123 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003126#ifdef FEAT_FLOAT
3127 f1 = 0;
3128 f2 = 0;
3129#endif
3130 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003131 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133#ifdef FEAT_FLOAT
3134 if (rettv->v_type == VAR_FLOAT)
3135 {
3136 f1 = rettv->vval.v_float;
3137 use_float = TRUE;
3138 n1 = 0;
3139 }
3140 else
3141#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003142 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003143 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003144 if (error)
3145 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 }
3147 else
3148 n1 = 0;
3149
3150 /*
3151 * Get the second variable.
3152 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003153 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3154 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003155 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003156 clear_tv(rettv);
3157 return FAIL;
3158 }
3159 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003160 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return FAIL;
3162
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003163 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165#ifdef FEAT_FLOAT
3166 if (var2.v_type == VAR_FLOAT)
3167 {
3168 if (!use_float)
3169 {
3170 f1 = n1;
3171 use_float = TRUE;
3172 }
3173 f2 = var2.vval.v_float;
3174 n2 = 0;
3175 }
3176 else
3177#endif
3178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003179 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 clear_tv(&var2);
3181 if (error)
3182 return FAIL;
3183#ifdef FEAT_FLOAT
3184 if (use_float)
3185 f2 = n2;
3186#endif
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 /*
3190 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003193#ifdef FEAT_FLOAT
3194 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196 if (op == '*')
3197 f1 = f1 * f2;
3198 else if (op == '/')
3199 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003200# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003201 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003202 if (f2 == 0.0)
3203 {
3204 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003205 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003206 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003207 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003208 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003209 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003210 }
3211 else
3212 f1 = f1 / f2;
3213# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003214 // We rely on the floating point library to handle divide
3215 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003216 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003217# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003222 return FAIL;
3223 }
3224 rettv->v_type = VAR_FLOAT;
3225 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003230 int failed = FALSE;
3231
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003232 if (op == '*')
3233 n1 = n1 * n2;
3234 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003235 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003237 n1 = num_modulus(n1, n2, &failed);
3238 if (failed)
3239 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003240
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003241 rettv->v_type = VAR_NUMBER;
3242 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
3245 }
3246
3247 return OK;
3248}
3249
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003250/*
3251 * Handle a type cast before a base level expression.
3252 * "arg" must point to the first non-white of the expression.
3253 * "arg" is advanced to just after the recognized expression.
3254 * Return OK or FAIL.
3255 */
3256 static int
3257eval7t(
3258 char_u **arg,
3259 typval_T *rettv,
3260 evalarg_T *evalarg,
3261 int want_string) // after "." operator
3262{
3263 type_T *want_type = NULL;
3264 garray_T type_list; // list of pointers to allocated types
3265 int res;
3266 int evaluate = evalarg == NULL ? 0
3267 : (evalarg->eval_flags & EVAL_EVALUATE);
3268
3269 // Recognize <type> in Vim9 script only.
3270 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3271 {
3272 ++*arg;
3273 ga_init2(&type_list, sizeof(type_T *), 10);
3274 want_type = parse_type(arg, &type_list, TRUE);
3275 if (want_type == NULL && (evaluate || **arg != '>'))
3276 {
3277 clear_type_list(&type_list);
3278 return FAIL;
3279 }
3280
3281 if (**arg != '>')
3282 {
3283 if (*skipwhite(*arg) == '>')
3284 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3285 else
3286 emsg(_(e_missing_gt));
3287 clear_type_list(&type_list);
3288 return FAIL;
3289 }
3290 ++*arg;
3291 *arg = skipwhite_and_linebreak(*arg, evalarg);
3292 }
3293
3294 res = eval7(arg, rettv, evalarg, want_string);
3295
3296 if (want_type != NULL && evaluate)
3297 {
3298 if (res == OK)
3299 {
3300 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3301
3302 if (!equal_type(want_type, actual))
3303 {
3304 if (want_type == &t_bool && actual != &t_bool
3305 && (actual->tt_flags & TTFLAG_BOOL_OK))
3306 {
3307 int n = tv2bool(rettv);
3308
3309 // can use "0" and "1" for boolean in some places
3310 clear_tv(rettv);
3311 rettv->v_type = VAR_BOOL;
3312 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3313 }
3314 else
3315 {
3316 where_T where;
3317
3318 where.wt_index = 0;
3319 where.wt_variable = TRUE;
3320 res = check_type(want_type, actual, TRUE, where);
3321 }
3322 }
3323 }
3324 clear_type_list(&type_list);
3325 }
3326
3327 return res;
3328}
3329
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003330 int
3331eval_leader(char_u **arg, int vim9)
3332{
3333 char_u *s = *arg;
3334 char_u *p = *arg;
3335
3336 while (*p == '!' || *p == '-' || *p == '+')
3337 {
3338 char_u *n = skipwhite(p + 1);
3339
3340 // ++, --, -+ and +- are not accepted in Vim9 script
3341 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3342 {
3343 semsg(_(e_invexpr2), s);
3344 return FAIL;
3345 }
3346 p = n;
3347 }
3348 *arg = p;
3349 return OK;
3350}
3351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352/*
3353 * Handle sixth level expression:
3354 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003355 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003356 * "string" string constant
3357 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 * &option-name option value
3359 * @r register contents
3360 * identifier variable value
3361 * function() function call
3362 * $VAR environment variable
3363 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003364 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003366 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003367 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 *
3369 * Also handle:
3370 * ! in front logical NOT
3371 * - in front unary minus
3372 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003373 * trailing [] subscript in String or List
3374 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003375 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 *
3377 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003378 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 *
3380 * Return OK or FAIL.
3381 */
3382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003383eval7(
3384 char_u **arg,
3385 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003386 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003389 int evaluate = evalarg != NULL
3390 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 int len;
3392 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 char_u *start_leader, *end_leader;
3394 int ret = OK;
3395 char_u *alias;
3396
3397 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003398 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003399 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003401 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
3403 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003404 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 */
3406 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003407 if (eval_leader(arg, in_vim9script()) == FAIL)
3408 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 end_leader = *arg;
3410
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003411 if (**arg == '.' && (!isdigit(*(*arg + 1))
3412#ifdef FEAT_FLOAT
3413 || current_sctx.sc_version < 2
3414#endif
3415 ))
3416 {
3417 semsg(_(e_invexpr2), *arg);
3418 ++*arg;
3419 return FAIL;
3420 }
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 switch (**arg)
3423 {
3424 /*
3425 * Number constant.
3426 */
3427 case '0':
3428 case '1':
3429 case '2':
3430 case '3':
3431 case '4':
3432 case '5':
3433 case '6':
3434 case '7':
3435 case '8':
3436 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003437 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003438
3439 // Apply prefixed "-" and "+" now. Matters especially when
3440 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003441 if (ret == OK && evaluate && end_leader > start_leader
3442 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003443 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 break;
3445
3446 /*
3447 * String constant: "string".
3448 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003449 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 break;
3451
3452 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003453 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003455 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003456 break;
3457
3458 /*
3459 * List: [expr, expr]
3460 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003461 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 break;
3463
3464 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003465 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003466 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003467 case '#': if (in_vim9script())
3468 {
3469 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3470 }
3471 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003472 {
3473 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003474 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003475 }
3476 else
3477 ret = NOTDONE;
3478 break;
3479
3480 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003481 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003482 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003483 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003484 case '{': if (in_vim9script())
3485 ret = NOTDONE;
3486 else
3487 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003488 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003489 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003490 break;
3491
3492 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003493 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003495 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 break;
3497
3498 /*
3499 * Environment variable: $VAR.
3500 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003501 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 break;
3503
3504 /*
3505 * Register contents: @r.
3506 */
3507 case '@': ++*arg;
3508 if (evaluate)
3509 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003510 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3511 semsg(_(e_syntax_error_at_str), *arg);
3512 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3513 emsg_invreg(**arg);
3514 else
3515 {
3516 rettv->v_type = VAR_STRING;
3517 rettv->vval.v_string = get_reg_contents(**arg,
3518 GREG_EXPR_SRC);
3519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 if (**arg != NUL)
3522 ++*arg;
3523 break;
3524
3525 /*
3526 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003527 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003529 case '(': ret = NOTDONE;
3530 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003531 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003532 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003533 if (ret == OK && evaluate)
3534 {
3535 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3536
3537 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003538 if (compile_def_function(ufunc,
3539 TRUE, PROFILING(ufunc), NULL) == FAIL)
3540 {
3541 clear_tv(rettv);
3542 ret = FAIL;
3543 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003544 }
3545 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003546 if (ret == NOTDONE)
3547 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003548 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003549 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003550
Bram Moolenaar9215f012020-06-27 21:18:00 +02003551 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003552 if (**arg == ')')
3553 ++*arg;
3554 else if (ret == OK)
3555 {
3556 emsg(_(e_missing_close));
3557 clear_tv(rettv);
3558 ret = FAIL;
3559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561 break;
3562
Bram Moolenaar8c711452005-01-14 21:53:12 +00003563 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 break;
3565 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003566
3567 if (ret == NOTDONE)
3568 {
3569 /*
3570 * Must be a variable or function name.
3571 * Can also be a curly-braces kind of name: {expr}.
3572 */
3573 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003574 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003575 if (alias != NULL)
3576 s = alias;
3577
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003578 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003579 ret = FAIL;
3580 else
3581 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003582 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3583
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003584 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003585 {
3586 emsg(_(e_cannot_use_underscore_here));
3587 ret = FAIL;
3588 }
3589 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003590 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003591 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003592 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003593 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003594 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003595 else if (flags & EVAL_CONSTANT)
3596 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003598 {
3599 // get the value of "true", "false" or a variable
3600 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3601 {
3602 rettv->v_type = VAR_BOOL;
3603 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003604 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003605 }
3606 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003607 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003608 {
3609 rettv->v_type = VAR_BOOL;
3610 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003611 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003612 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003613 else if (len == 4 && in_vim9script()
3614 && STRNCMP(s, "null", 4) == 0)
3615 {
3616 rettv->v_type = VAR_SPECIAL;
3617 rettv->vval.v_number = VVAL_NULL;
3618 ret = OK;
3619 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003620 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003621 ret = eval_variable(s, len, rettv, NULL,
3622 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003623 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003624 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003625 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003626 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003627 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003628 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003631 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 }
3633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003634 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3635 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003636 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003637 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 /*
3640 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3641 */
3642 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003643 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003644 return ret;
3645}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003646
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003647/*
3648 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003649 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003650 * Adjusts "end_leaderp" until it is at "start_leader".
3651 */
3652 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003653eval7_leader(
3654 typval_T *rettv,
3655 int numeric_only,
3656 char_u *start_leader,
3657 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003658{
3659 char_u *end_leader = *end_leaderp;
3660 int ret = OK;
3661 int error = FALSE;
3662 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003663 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003664#ifdef FEAT_FLOAT
3665 float_T f = 0.0;
3666
3667 if (rettv->v_type == VAR_FLOAT)
3668 f = rettv->vval.v_float;
3669 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003670#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003671 {
3672 while (VIM_ISWHITE(end_leader[-1]))
3673 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003674 if (in_vim9script() && end_leader[-1] == '!')
3675 val = tv2bool(rettv);
3676 else
3677 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003678 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003679 if (error)
3680 {
3681 clear_tv(rettv);
3682 ret = FAIL;
3683 }
3684 else
3685 {
3686 while (end_leader > start_leader)
3687 {
3688 --end_leader;
3689 if (*end_leader == '!')
3690 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003691 if (numeric_only)
3692 {
3693 ++end_leader;
3694 break;
3695 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003696#ifdef FEAT_FLOAT
3697 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003698 {
3699 if (in_vim9script())
3700 {
3701 rettv->v_type = VAR_BOOL;
3702 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3703 }
3704 else
3705 f = !f;
3706 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003707 else
3708#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003709 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003710 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003711 type = VAR_BOOL;
3712 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003713 }
3714 else if (*end_leader == '-')
3715 {
3716#ifdef FEAT_FLOAT
3717 if (rettv->v_type == VAR_FLOAT)
3718 f = -f;
3719 else
3720#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003721 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003722 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003723 type = VAR_NUMBER;
3724 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003725 }
3726 }
3727#ifdef FEAT_FLOAT
3728 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003730 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003731 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003733 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003734#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003736 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003737 if (in_vim9script())
3738 rettv->v_type = type;
3739 else
3740 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003741 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003744 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 return ret;
3746}
3747
3748/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003749 * Call the function referred to in "rettv".
3750 */
3751 static int
3752call_func_rettv(
3753 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003754 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003755 typval_T *rettv,
3756 int evaluate,
3757 dict_T *selfdict,
3758 typval_T *basetv)
3759{
3760 partial_T *pt = NULL;
3761 funcexe_T funcexe;
3762 typval_T functv;
3763 char_u *s;
3764 int ret;
3765
3766 // need to copy the funcref so that we can clear rettv
3767 if (evaluate)
3768 {
3769 functv = *rettv;
3770 rettv->v_type = VAR_UNKNOWN;
3771
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003772 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003773 if (functv.v_type == VAR_PARTIAL)
3774 {
3775 pt = functv.vval.v_partial;
3776 s = partial_name(pt);
3777 }
3778 else
3779 s = functv.vval.v_string;
3780 }
3781 else
3782 s = (char_u *)"";
3783
Bram Moolenaara80faa82020-04-12 19:37:17 +02003784 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003785 funcexe.firstline = curwin->w_cursor.lnum;
3786 funcexe.lastline = curwin->w_cursor.lnum;
3787 funcexe.evaluate = evaluate;
3788 funcexe.partial = pt;
3789 funcexe.selfdict = selfdict;
3790 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003791 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003793 // Clear the funcref afterwards, so that deleting it while
3794 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003795 if (evaluate)
3796 clear_tv(&functv);
3797
3798 return ret;
3799}
3800
3801/*
3802 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003803 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003804 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3805 */
3806 static int
3807eval_lambda(
3808 char_u **arg,
3809 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003810 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003811 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003812{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003813 int evaluate = evalarg != NULL
3814 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003815 typval_T base = *rettv;
3816 int ret;
3817
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003818 rettv->v_type = VAR_UNKNOWN;
3819
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003820 if (**arg == '{')
3821 {
3822 // ->{lambda}()
3823 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3824 }
3825 else
3826 {
3827 // ->(lambda)()
3828 ++*arg;
3829 ret = eval1(arg, rettv, evalarg);
3830 *arg = skipwhite_and_linebreak(*arg, evalarg);
3831 if (**arg != ')')
3832 {
3833 emsg(_(e_missing_close));
3834 ret = FAIL;
3835 }
3836 ++*arg;
3837 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003838 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003839 return FAIL;
3840 else if (**arg != '(')
3841 {
3842 if (verbose)
3843 {
3844 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003845 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003846 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003848 }
3849 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003850 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003851 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003852 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003853 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003854
3855 // Clear the funcref afterwards, so that deleting it while
3856 // evaluating the arguments is possible (see test55).
3857 if (evaluate)
3858 clear_tv(&base);
3859
3860 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003861}
3862
3863/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003864 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003865 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003866 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3867 */
3868 static int
3869eval_method(
3870 char_u **arg,
3871 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003872 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003873 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003874{
3875 char_u *name;
3876 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003877 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003878 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003879 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003880 int evaluate = evalarg != NULL
3881 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003882
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003883 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003884
Bram Moolenaarac92e252019-08-03 21:58:38 +02003885 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003886 len = get_name_len(arg, &alias, evaluate, TRUE);
3887 if (alias != NULL)
3888 name = alias;
3889
3890 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003891 {
3892 if (verbose)
3893 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003894 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003895 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003896 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003897 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003898 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003899 if (**arg != '(')
3900 {
3901 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003903 ret = FAIL;
3904 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003905 else if (VIM_ISWHITE((*arg)[-1]))
3906 {
3907 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003908 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003909 ret = FAIL;
3910 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003911 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003912 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003913 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003914 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003915
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003916 // Clear the funcref afterwards, so that deleting it while
3917 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003918 if (evaluate)
3919 clear_tv(&base);
3920
Bram Moolenaarac92e252019-08-03 21:58:38 +02003921 return ret;
3922}
3923
3924/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003925 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3926 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003927 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3928 */
3929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003930eval_index(
3931 char_u **arg,
3932 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003933 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003934 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003935{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003936 int evaluate = evalarg != NULL
3937 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003938 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003940 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003941 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003942 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003943 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003944
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003945 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3946 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003947
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003948 init_tv(&var1);
3949 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003950 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003951 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003952 /*
3953 * dict.name
3954 */
3955 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003956 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003957 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003958 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003959 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003960 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961 }
3962 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003964 /*
3965 * something[idx]
3966 *
3967 * Get the (first) variable from inside the [].
3968 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003969 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003970 if (**arg == ':')
3971 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003972 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003973 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003974 else if (vim9 && **arg == ':')
3975 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003976 semsg(_(e_white_space_required_before_and_after_str_at_str),
3977 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003978 clear_tv(&var1);
3979 return FAIL;
3980 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003981 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003982 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003983#ifdef FEAT_FLOAT
3984 // allow for indexing with float
3985 if (vim9 && rettv->v_type == VAR_DICT
3986 && var1.v_type == VAR_FLOAT)
3987 {
3988 var1.vval.v_string = typval_tostring(&var1, TRUE);
3989 var1.v_type = VAR_STRING;
3990 }
3991#endif
3992 if (tv_get_string_chk(&var1) == NULL)
3993 {
3994 // not a number or string
3995 clear_tv(&var1);
3996 return FAIL;
3997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003999
4000 /*
4001 * Get the second variable from inside the [:].
4002 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004003 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004004 if (**arg == ':')
4005 {
4006 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004007 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004008 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004009 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004010 semsg(_(e_white_space_required_before_and_after_str_at_str),
4011 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004012 if (!empty1)
4013 clear_tv(&var1);
4014 return FAIL;
4015 }
4016 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004017 if (**arg == ']')
4018 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004019 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004021 if (!empty1)
4022 clear_tv(&var1);
4023 return FAIL;
4024 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004025 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004026 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004027 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004028 if (!empty1)
4029 clear_tv(&var1);
4030 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 return FAIL;
4032 }
4033 }
4034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004035 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004036 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004037 if (**arg != ']')
4038 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004039 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004040 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004041 clear_tv(&var1);
4042 if (range)
4043 clear_tv(&var2);
4044 return FAIL;
4045 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004046 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004047 }
4048
4049 if (evaluate)
4050 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004051 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004052 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004053 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004054
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004055 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004057 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004058 clear_tv(&var2);
4059 return res;
4060 }
4061 return OK;
4062}
4063
4064/*
4065 * Check if "rettv" can have an [index] or [sli:ce]
4066 */
4067 int
4068check_can_index(typval_T *rettv, int evaluate, int verbose)
4069{
4070 switch (rettv->v_type)
4071 {
4072 case VAR_FUNC:
4073 case VAR_PARTIAL:
4074 if (verbose)
4075 emsg(_("E695: Cannot index a Funcref"));
4076 return FAIL;
4077 case VAR_FLOAT:
4078#ifdef FEAT_FLOAT
4079 if (verbose)
4080 emsg(_(e_float_as_string));
4081 return FAIL;
4082#endif
4083 case VAR_BOOL:
4084 case VAR_SPECIAL:
4085 case VAR_JOB:
4086 case VAR_CHANNEL:
4087 if (verbose)
4088 emsg(_(e_cannot_index_special_variable));
4089 return FAIL;
4090 case VAR_UNKNOWN:
4091 case VAR_ANY:
4092 case VAR_VOID:
4093 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004094 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004095 emsg(_(e_cannot_index_special_variable));
4096 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004097 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004098 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004099
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004100 case VAR_STRING:
4101 case VAR_LIST:
4102 case VAR_DICT:
4103 case VAR_BLOB:
4104 break;
4105 case VAR_NUMBER:
4106 if (in_vim9script())
4107 emsg(_(e_cannot_index_number));
4108 break;
4109 }
4110 return OK;
4111}
4112
4113/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004114 * slice() function
4115 */
4116 void
4117f_slice(typval_T *argvars, typval_T *rettv)
4118{
4119 if (check_can_index(argvars, TRUE, FALSE) == OK)
4120 {
4121 copy_tv(argvars, rettv);
4122 eval_index_inner(rettv, TRUE, argvars + 1,
4123 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4124 TRUE, NULL, 0, FALSE);
4125 }
4126}
4127
4128/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004129 * Apply index or range to "rettv".
4130 * "var1" is the first index, NULL for [:expr].
4131 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004132 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4133 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004134 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4135 */
4136 int
4137eval_index_inner(
4138 typval_T *rettv,
4139 int is_range,
4140 typval_T *var1,
4141 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004142 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004143 char_u *key,
4144 int keylen,
4145 int verbose)
4146{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004147 varnumber_T n1, n2 = 0;
4148 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149
4150 n1 = 0;
4151 if (var1 != NULL && rettv->v_type != VAR_DICT)
4152 n1 = tv_get_number(var1);
4153
4154 if (is_range)
4155 {
4156 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004157 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 if (verbose)
4159 emsg(_(e_cannot_slice_dictionary));
4160 return FAIL;
4161 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004162 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004163 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004164 else
4165 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004166 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004167
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004168 switch (rettv->v_type)
4169 {
4170 case VAR_UNKNOWN:
4171 case VAR_ANY:
4172 case VAR_VOID:
4173 case VAR_FUNC:
4174 case VAR_PARTIAL:
4175 case VAR_FLOAT:
4176 case VAR_BOOL:
4177 case VAR_SPECIAL:
4178 case VAR_JOB:
4179 case VAR_CHANNEL:
4180 break; // not evaluating, skipping over subscript
4181
4182 case VAR_NUMBER:
4183 case VAR_STRING:
4184 {
4185 char_u *s = tv_get_string(rettv);
4186
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004187 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004188 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004189 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004190 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004191 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004192 else
4193 s = char_from_string(s, n1);
4194 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004195 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004197 // The resulting variable is a substring. If the indexes
4198 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004199 if (n1 < 0)
4200 {
4201 n1 = len + n1;
4202 if (n1 < 0)
4203 n1 = 0;
4204 }
4205 if (n2 < 0)
4206 n2 = len + n2;
4207 else if (n2 >= len)
4208 n2 = len;
4209 if (n1 >= len || n2 < 0 || n1 > n2)
4210 s = NULL;
4211 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004212 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004213 }
4214 else
4215 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004216 // The resulting variable is a string of a single
4217 // character. If the index is too big or negative the
4218 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004219 if (n1 >= len || n1 < 0)
4220 s = NULL;
4221 else
4222 s = vim_strnsave(s + n1, 1);
4223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 clear_tv(rettv);
4225 rettv->v_type = VAR_STRING;
4226 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004227 }
4228 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004229
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004230 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004231 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4232 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004233 break;
4234
4235 case VAR_LIST:
4236 if (var1 == NULL)
4237 n1 = 0;
4238 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004239 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004240 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004241 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004242 return FAIL;
4243 break;
4244
4245 case VAR_DICT:
4246 {
4247 dictitem_T *item;
4248 typval_T tmp;
4249
4250 if (key == NULL)
4251 {
4252 key = tv_get_string_chk(var1);
4253 if (key == NULL)
4254 return FAIL;
4255 }
4256
4257 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4258
4259 if (item == NULL && verbose)
4260 semsg(_(e_dictkey), key);
4261 if (item == NULL)
4262 return FAIL;
4263
4264 copy_tv(&item->di_tv, &tmp);
4265 clear_tv(rettv);
4266 *rettv = tmp;
4267 }
4268 break;
4269 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004270 return OK;
4271}
4272
4273/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004274 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004275 */
4276 char_u *
4277partial_name(partial_T *pt)
4278{
4279 if (pt->pt_name != NULL)
4280 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004281 if (pt->pt_func != NULL)
4282 return pt->pt_func->uf_name;
4283 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284}
4285
Bram Moolenaarddecc252016-04-06 22:59:37 +02004286 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004287partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004288{
4289 int i;
4290
4291 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004292 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004293 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004294 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004295 if (pt->pt_name != NULL)
4296 {
4297 func_unref(pt->pt_name);
4298 vim_free(pt->pt_name);
4299 }
4300 else
4301 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004302
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004303 // Decrease the reference count for the context of a closure. If down
4304 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004305 if (pt->pt_funcstack != NULL)
4306 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004307 --pt->pt_funcstack->fs_refcount;
4308 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004309 }
4310
Bram Moolenaarddecc252016-04-06 22:59:37 +02004311 vim_free(pt);
4312}
4313
4314/*
4315 * Unreference a closure: decrement the reference count and free it when it
4316 * becomes zero.
4317 */
4318 void
4319partial_unref(partial_T *pt)
4320{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004321 if (pt != NULL)
4322 {
4323 if (--pt->pt_refcount <= 0)
4324 partial_free(pt);
4325
4326 // If the reference count goes down to one, the funcstack may be the
4327 // only reference and can be freed if no other partials reference it.
4328 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4329 funcstack_check_refcount(pt->pt_funcstack);
4330 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004331}
4332
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004333/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004334 * Return the next (unique) copy ID.
4335 * Used for serializing nested structures.
4336 */
4337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004338get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004339{
4340 current_copyID += COPYID_INC;
4341 return current_copyID;
4342}
4343
4344/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004345 * Garbage collection for lists and dictionaries.
4346 *
4347 * We use reference counts to be able to free most items right away when they
4348 * are no longer used. But for composite items it's possible that it becomes
4349 * unused while the reference count is > 0: When there is a recursive
4350 * reference. Example:
4351 * :let l = [1, 2, 3]
4352 * :let d = {9: l}
4353 * :let l[1] = d
4354 *
4355 * Since this is quite unusual we handle this with garbage collection: every
4356 * once in a while find out which lists and dicts are not referenced from any
4357 * variable.
4358 *
4359 * Here is a good reference text about garbage collection (refers to Python
4360 * but it applies to all reference-counting mechanisms):
4361 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004362 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004363
4364/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004365 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004366 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004367 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004368 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004369 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004370garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004371{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004372 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004373 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004374 buf_T *buf;
4375 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004376 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004377 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004378
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004379 if (!testing)
4380 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004381 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004382 want_garbage_collect = FALSE;
4383 may_garbage_collect = FALSE;
4384 garbage_collect_at_exit = FALSE;
4385 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004386
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004387 // The execution stack can grow big, limit the size.
4388 if (exestack.ga_maxlen - exestack.ga_len > 500)
4389 {
4390 size_t new_len;
4391 char_u *pp;
4392 int n;
4393
4394 // Keep 150% of the current size, with a minimum of the growth size.
4395 n = exestack.ga_len / 2;
4396 if (n < exestack.ga_growsize)
4397 n = exestack.ga_growsize;
4398
4399 // Don't make it bigger though.
4400 if (exestack.ga_len + n < exestack.ga_maxlen)
4401 {
4402 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4403 pp = vim_realloc(exestack.ga_data, new_len);
4404 if (pp == NULL)
4405 return FAIL;
4406 exestack.ga_maxlen = exestack.ga_len + n;
4407 exestack.ga_data = pp;
4408 }
4409 }
4410
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004411 // We advance by two because we add one for items referenced through
4412 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004413 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004414
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004415 /*
4416 * 1. Go through all accessible variables and mark all lists and dicts
4417 * with copyID.
4418 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004420 // Don't free variables in the previous_funccal list unless they are only
4421 // referenced through previous_funccal. This must be first, because if
4422 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004423 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004425 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004426 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004427
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004428 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004429 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004430 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4431 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004432
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004433 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004434 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004435 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4436 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004437 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004438 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4439 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004440#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004441 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004442 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4443 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004444 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004445 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004446 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4447 NULL, NULL);
4448#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004450 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004451 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004452 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4453 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004454 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004455 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004456
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004457 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004458 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004459
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004460 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004461 abort = abort || set_ref_in_functions(copyID);
4462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004463 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004466 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004467 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004468
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004469 // callbacks in buffers
4470 abort = abort || set_ref_in_buffers(copyID);
4471
Bram Moolenaar1dced572012-04-05 16:54:08 +02004472#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004473 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004474#endif
4475
Bram Moolenaardb913952012-06-29 12:54:53 +02004476#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004477 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004478#endif
4479
4480#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004481 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004482#endif
4483
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004484#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004485 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004486 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004487#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004488#ifdef FEAT_NETBEANS_INTG
4489 abort = abort || set_ref_in_nb_channel(copyID);
4490#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004491
Bram Moolenaare3188e22016-05-31 21:13:04 +02004492#ifdef FEAT_TIMERS
4493 abort = abort || set_ref_in_timer(copyID);
4494#endif
4495
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004496#ifdef FEAT_QUICKFIX
4497 abort = abort || set_ref_in_quickfix(copyID);
4498#endif
4499
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004500#ifdef FEAT_TERMINAL
4501 abort = abort || set_ref_in_term(copyID);
4502#endif
4503
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004504#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004505 abort = abort || set_ref_in_popups(copyID);
4506#endif
4507
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004508 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004509 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004510 /*
4511 * 2. Free lists and dictionaries that are not referenced.
4512 */
4513 did_free = free_unref_items(copyID);
4514
4515 /*
4516 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004517 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004518 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004519 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004520 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 else if (p_verbose > 0)
4522 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004523 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004524 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004525
4526 return did_free;
4527}
4528
4529/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004530 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004531 */
4532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004533free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004534{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004535 int did_free = FALSE;
4536
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004537 // Let all "free" functions know that we are here. This means no
4538 // dictionaries, lists, channels or jobs are to be freed, because we will
4539 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004540 in_free_unref_items = TRUE;
4541
4542 /*
4543 * PASS 1: free the contents of the items. We don't free the items
4544 * themselves yet, so that it is possible to decrement refcount counters
4545 */
4546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004548 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004551 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004552
4553#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004554 // Go through the list of jobs and free items without the copyID. This
4555 // must happen before doing channels, because jobs refer to channels, but
4556 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004557 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004560 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4561#endif
4562
4563 /*
4564 * PASS 2: free the items themselves.
4565 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004566 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004567 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004568
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004569#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004570 // Go through the list of jobs and free items without the copyID. This
4571 // must happen before doing channels, because jobs refer to channels, but
4572 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004573 free_unused_jobs(copyID, COPYID_MASK);
4574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004575 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004576 free_unused_channels(copyID, COPYID_MASK);
4577#endif
4578
4579 in_free_unref_items = FALSE;
4580
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004581 return did_free;
4582}
4583
4584/*
4585 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004586 * "list_stack" is used to add lists to be marked. Can be NULL.
4587 *
4588 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004589 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004590 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004591set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004592{
4593 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004594 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004595 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004596 hashtab_T *cur_ht;
4597 ht_stack_T *ht_stack = NULL;
4598 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004599
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004600 cur_ht = ht;
4601 for (;;)
4602 {
4603 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004604 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004605 // Mark each item in the hashtab. If the item contains a hashtab
4606 // it is added to ht_stack, if it contains a list it is added to
4607 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004608 todo = (int)cur_ht->ht_used;
4609 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4610 if (!HASHITEM_EMPTY(hi))
4611 {
4612 --todo;
4613 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4614 &ht_stack, list_stack);
4615 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004616 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004617
4618 if (ht_stack == NULL)
4619 break;
4620
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004621 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004622 cur_ht = ht_stack->ht;
4623 tempitem = ht_stack;
4624 ht_stack = ht_stack->prev;
4625 free(tempitem);
4626 }
4627
4628 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004629}
4630
4631/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004632 * Mark a dict and its items with "copyID".
4633 * Returns TRUE if setting references failed somehow.
4634 */
4635 int
4636set_ref_in_dict(dict_T *d, int copyID)
4637{
4638 if (d != NULL && d->dv_copyID != copyID)
4639 {
4640 d->dv_copyID = copyID;
4641 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4642 }
4643 return FALSE;
4644}
4645
4646/*
4647 * Mark a list and its items with "copyID".
4648 * Returns TRUE if setting references failed somehow.
4649 */
4650 int
4651set_ref_in_list(list_T *ll, int copyID)
4652{
4653 if (ll != NULL && ll->lv_copyID != copyID)
4654 {
4655 ll->lv_copyID = copyID;
4656 return set_ref_in_list_items(ll, copyID, NULL);
4657 }
4658 return FALSE;
4659}
4660
4661/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004662 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004663 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4664 *
4665 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004666 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004667 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004668set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004669{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004670 listitem_T *li;
4671 int abort = FALSE;
4672 list_T *cur_l;
4673 list_stack_T *list_stack = NULL;
4674 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004675
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004676 cur_l = l;
4677 for (;;)
4678 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004679 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004680 // Mark each item in the list. If the item contains a hashtab
4681 // it is added to ht_stack, if it contains a list it is added to
4682 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004683 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4684 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4685 ht_stack, &list_stack);
4686 if (list_stack == NULL)
4687 break;
4688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004689 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004690 cur_l = list_stack->list;
4691 tempitem = list_stack;
4692 list_stack = list_stack->prev;
4693 free(tempitem);
4694 }
4695
4696 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004697}
4698
4699/*
4700 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701 * "list_stack" is used to add lists to be marked. Can be NULL.
4702 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4703 *
4704 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004705 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004707set_ref_in_item(
4708 typval_T *tv,
4709 int copyID,
4710 ht_stack_T **ht_stack,
4711 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004712{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004713 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004714
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004715 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004716 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004717 dict_T *dd = tv->vval.v_dict;
4718
Bram Moolenaara03f2332016-02-06 18:09:59 +01004719 if (dd != NULL && dd->dv_copyID != copyID)
4720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004721 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004722 dd->dv_copyID = copyID;
4723 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004724 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004725 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4726 }
4727 else
4728 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004729 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4730
Bram Moolenaara03f2332016-02-06 18:09:59 +01004731 if (newitem == NULL)
4732 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004733 else
4734 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004735 newitem->ht = &dd->dv_hashtab;
4736 newitem->prev = *ht_stack;
4737 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004738 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004739 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004740 }
4741 }
4742 else if (tv->v_type == VAR_LIST)
4743 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004744 list_T *ll = tv->vval.v_list;
4745
Bram Moolenaara03f2332016-02-06 18:09:59 +01004746 if (ll != NULL && ll->lv_copyID != copyID)
4747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004749 ll->lv_copyID = copyID;
4750 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004751 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004752 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004753 }
4754 else
4755 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004756 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4757
Bram Moolenaara03f2332016-02-06 18:09:59 +01004758 if (newitem == NULL)
4759 abort = TRUE;
4760 else
4761 {
4762 newitem->list = ll;
4763 newitem->prev = *list_stack;
4764 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004765 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004766 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004767 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004768 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004769 else if (tv->v_type == VAR_FUNC)
4770 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004771 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004772 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004773 else if (tv->v_type == VAR_PARTIAL)
4774 {
4775 partial_T *pt = tv->vval.v_partial;
4776 int i;
4777
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004778 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004779 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004780 // Didn't see this partial yet.
4781 pt->pt_copyID = copyID;
4782
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004783 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004784
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004785 if (pt->pt_dict != NULL)
4786 {
4787 typval_T dtv;
4788
4789 dtv.v_type = VAR_DICT;
4790 dtv.vval.v_dict = pt->pt_dict;
4791 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4792 }
4793
4794 for (i = 0; i < pt->pt_argc; ++i)
4795 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4796 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004797 if (pt->pt_funcstack != NULL)
4798 {
4799 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4800
4801 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4802 abort = abort || set_ref_in_item(stack + i, copyID,
4803 ht_stack, list_stack);
4804 }
4805
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004806 }
4807 }
4808#ifdef FEAT_JOB_CHANNEL
4809 else if (tv->v_type == VAR_JOB)
4810 {
4811 job_T *job = tv->vval.v_job;
4812 typval_T dtv;
4813
4814 if (job != NULL && job->jv_copyID != copyID)
4815 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004816 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 if (job->jv_channel != NULL)
4818 {
4819 dtv.v_type = VAR_CHANNEL;
4820 dtv.vval.v_channel = job->jv_channel;
4821 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4822 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004823 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004824 {
4825 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004826 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004827 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4828 }
4829 }
4830 }
4831 else if (tv->v_type == VAR_CHANNEL)
4832 {
4833 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004834 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004835 typval_T dtv;
4836 jsonq_T *jq;
4837 cbq_T *cq;
4838
4839 if (ch != NULL && ch->ch_copyID != copyID)
4840 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004841 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004842 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004843 {
4844 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4845 jq = jq->jq_next)
4846 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4847 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4848 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004849 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004850 {
4851 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004852 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004853 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4854 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004855 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004856 {
4857 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004858 dtv.vval.v_partial =
4859 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004860 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4861 }
4862 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004863 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004864 {
4865 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004866 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004867 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4868 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004869 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004870 {
4871 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004872 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004873 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4874 }
4875 }
4876 }
4877#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004878 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004879}
4880
Bram Moolenaar8c711452005-01-14 21:53:12 +00004881/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004882 * Return a string with the string representation of a variable.
4883 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004884 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004885 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004886 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004887 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004888 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004889 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4890 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004891 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004893 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004894echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004895 typval_T *tv,
4896 char_u **tofree,
4897 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004898 int copyID,
4899 int echo_style,
4900 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004901 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004902{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004903 static int recurse = 0;
4904 char_u *r = NULL;
4905
Bram Moolenaar33570922005-01-25 22:26:29 +00004906 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004907 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004908 if (!did_echo_string_emsg)
4909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004910 // Only give this message once for a recursive call to avoid
4911 // flooding the user with errors. And stop iterating over lists
4912 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004913 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004914 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004916 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004917 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004918 }
4919 ++recurse;
4920
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004921 switch (tv->v_type)
4922 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004923 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004924 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004925 {
4926 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004927 r = tv->vval.v_string;
4928 if (r == NULL)
4929 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004930 }
4931 else
4932 {
4933 *tofree = string_quote(tv->vval.v_string, FALSE);
4934 r = *tofree;
4935 }
4936 break;
4937
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004938 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004939 if (echo_style)
4940 {
4941 *tofree = NULL;
4942 r = tv->vval.v_string;
4943 }
4944 else
4945 {
4946 *tofree = string_quote(tv->vval.v_string, TRUE);
4947 r = *tofree;
4948 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004949 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004950
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004951 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004952 {
4953 partial_T *pt = tv->vval.v_partial;
4954 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004955 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004956 garray_T ga;
4957 int i;
4958 char_u *tf;
4959
4960 ga_init2(&ga, 1, 100);
4961 ga_concat(&ga, (char_u *)"function(");
4962 if (fname != NULL)
4963 {
4964 ga_concat(&ga, fname);
4965 vim_free(fname);
4966 }
4967 if (pt != NULL && pt->pt_argc > 0)
4968 {
4969 ga_concat(&ga, (char_u *)", [");
4970 for (i = 0; i < pt->pt_argc; ++i)
4971 {
4972 if (i > 0)
4973 ga_concat(&ga, (char_u *)", ");
4974 ga_concat(&ga,
4975 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4976 vim_free(tf);
4977 }
4978 ga_concat(&ga, (char_u *)"]");
4979 }
4980 if (pt != NULL && pt->pt_dict != NULL)
4981 {
4982 typval_T dtv;
4983
4984 ga_concat(&ga, (char_u *)", ");
4985 dtv.v_type = VAR_DICT;
4986 dtv.vval.v_dict = pt->pt_dict;
4987 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4988 vim_free(tf);
4989 }
4990 ga_concat(&ga, (char_u *)")");
4991
4992 *tofree = ga.ga_data;
4993 r = *tofree;
4994 break;
4995 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004996
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004997 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004998 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004999 break;
5000
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005001 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005002 if (tv->vval.v_list == NULL)
5003 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005004 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005005 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005006 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005007 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005008 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5009 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005010 {
5011 *tofree = NULL;
5012 r = (char_u *)"[...]";
5013 }
5014 else
5015 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005016 int old_copyID = tv->vval.v_list->lv_copyID;
5017
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005018 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005019 *tofree = list2string(tv, copyID, restore_copyID);
5020 if (restore_copyID)
5021 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005022 r = *tofree;
5023 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005024 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005025
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005027 if (tv->vval.v_dict == NULL)
5028 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005029 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005030 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005031 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005032 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005033 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5034 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005035 {
5036 *tofree = NULL;
5037 r = (char_u *)"{...}";
5038 }
5039 else
5040 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005041 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005042
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005043 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005044 *tofree = dict2string(tv, copyID, restore_copyID);
5045 if (restore_copyID)
5046 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005047 r = *tofree;
5048 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005049 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005050
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005052 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005053 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005055 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005056 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005057 break;
5058
Bram Moolenaar835dc632016-02-07 14:27:38 +01005059 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005060 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005061 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005062 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005063 if (composite_val)
5064 {
5065 *tofree = string_quote(r, FALSE);
5066 r = *tofree;
5067 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005068 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005069
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005070 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005071#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005072 *tofree = NULL;
5073 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5074 r = numbuf;
5075 break;
5076#endif
5077
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005078 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005079 case VAR_SPECIAL:
5080 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005081 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005082 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005084
Bram Moolenaar8502c702014-06-17 12:51:16 +02005085 if (--recurse == 0)
5086 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005087 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005088}
5089
5090/*
5091 * Return a string with the string representation of a variable.
5092 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5093 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005094 * Does not put quotes around strings, as ":echo" displays values.
5095 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5096 * May return NULL.
5097 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005098 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005099echo_string(
5100 typval_T *tv,
5101 char_u **tofree,
5102 char_u *numbuf,
5103 int copyID)
5104{
5105 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5106}
5107
5108/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005109 * Return string "str" in ' quotes, doubling ' characters.
5110 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005113 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005114string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115{
Bram Moolenaar33570922005-01-25 22:26:29 +00005116 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005117 char_u *p, *r, *s;
5118
Bram Moolenaar33570922005-01-25 22:26:29 +00005119 len = (function ? 13 : 3);
5120 if (str != NULL)
5121 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005122 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005123 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005124 if (*p == '\'')
5125 ++len;
5126 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005127 s = r = alloc(len);
5128 if (r != NULL)
5129 {
5130 if (function)
5131 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005133 r += 10;
5134 }
5135 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005137 if (str != NULL)
5138 for (p = str; *p != NUL; )
5139 {
5140 if (*p == '\'')
5141 *r++ = '\'';
5142 MB_COPY_CHAR(p, r);
5143 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005145 if (function)
5146 *r++ = ')';
5147 *r++ = NUL;
5148 }
5149 return s;
5150}
5151
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005152#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005153/*
5154 * Convert the string "text" to a floating point number.
5155 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5156 * this always uses a decimal point.
5157 * Returns the length of the text that was consumed.
5158 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005159 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005160string2float(
5161 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005162 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005163{
5164 char *s = (char *)text;
5165 float_T f;
5166
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005167 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005168 if (STRNICMP(text, "inf", 3) == 0)
5169 {
5170 *value = INFINITY;
5171 return 3;
5172 }
5173 if (STRNICMP(text, "-inf", 3) == 0)
5174 {
5175 *value = -INFINITY;
5176 return 4;
5177 }
5178 if (STRNICMP(text, "nan", 3) == 0)
5179 {
5180 *value = NAN;
5181 return 3;
5182 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005183 f = strtod(s, &s);
5184 *value = f;
5185 return (int)((char_u *)s - text);
5186}
5187#endif
5188
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005189/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005190 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5191 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005192 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005193 */
5194 int
5195buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5196{
5197 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005198 char_u *t;
5199 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005200
5201 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5202 return -1;
5203
5204 if (lnum > buf->b_ml.ml_line_count)
5205 lnum = buf->b_ml.ml_line_count;
5206
5207 str = ml_get_buf(buf, lnum, FALSE);
5208 if (str == NULL)
5209 return -1;
5210
5211 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005212 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005213
Bram Moolenaar91458462021-01-13 20:08:38 +01005214 // count the number of characters
5215 t = str;
5216 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5217 t += mb_ptr2len(t);
5218
5219 // In insert mode, when the cursor is at the end of a non-empty line,
5220 // byteidx points to the NUL character immediately past the end of the
5221 // string. In this case, add one to the character count.
5222 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5223 count++;
5224
5225 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005226}
5227
5228/*
5229 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005230 * byte index. Works only for loaded buffers. Returns -1 on failure.
5231 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005232 */
5233 int
5234buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5235{
5236 char_u *str;
5237 char_u *t;
5238
5239 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5240 return -1;
5241
5242 if (lnum > buf->b_ml.ml_line_count)
5243 lnum = buf->b_ml.ml_line_count;
5244
5245 str = ml_get_buf(buf, lnum, FALSE);
5246 if (str == NULL)
5247 return -1;
5248
5249 // Convert the character offset to a byte offset
5250 t = str;
5251 while (*t != NUL && --charidx > 0)
5252 t += mb_ptr2len(t);
5253
Bram Moolenaar91458462021-01-13 20:08:38 +01005254 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005255}
5256
5257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005259 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005261 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005262var2fpos(
5263 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005264 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005265 int *fnum, // set to fnum for '0, 'A, etc.
5266 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005268 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005270 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005273 if (varp->v_type == VAR_LIST)
5274 {
5275 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005276 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005277 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005278 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005279
5280 l = varp->vval.v_list;
5281 if (l == NULL)
5282 return NULL;
5283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005284 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005285 pos.lnum = list_find_nr(l, 0L, &error);
5286 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005287 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005288 if (charcol)
5289 len = (long)mb_charlen(ml_get(pos.lnum));
5290 else
5291 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005292
Bram Moolenaarec65d772020-08-20 22:29:12 +02005293 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005294 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005295 li = list_find(l, 1L);
5296 if (li != NULL && li->li_tv.v_type == VAR_STRING
5297 && li->li_tv.vval.v_string != NULL
5298 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005299 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005300 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005301 }
5302 else
5303 {
5304 pos.col = list_find_nr(l, 1L, &error);
5305 if (error)
5306 return NULL;
5307 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005310 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005311 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005312 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005313
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005314 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005315 pos.coladd = list_find_nr(l, 2L, &error);
5316 if (error)
5317 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005318
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005319 return &pos;
5320 }
5321
Bram Moolenaarc5809432021-03-27 21:23:30 +01005322 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5323 return NULL;
5324
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005325 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 if (name == NULL)
5327 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005328 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005329 {
5330 pos = curwin->w_cursor;
5331 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005332 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005333 return &pos;
5334 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005335 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005336 {
5337 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005338 pos = VIsual;
5339 else
5340 pos = curwin->w_cursor;
5341 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005342 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005343 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005344 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005345 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005347 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5349 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005350 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005351 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 return pp;
5353 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005354
Bram Moolenaara5525202006-03-02 22:52:09 +00005355 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005356
Bram Moolenaar477933c2007-07-17 14:32:23 +00005357 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005358 {
5359 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005360 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005361 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005362 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005363 // In silent Ex mode topline is zero, but that's not a valid line
5364 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005365 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005366 return &pos;
5367 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005368 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005369 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005370 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005371 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005372 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005373 return &pos;
5374 }
5375 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005376 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005378 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 pos.lnum = curbuf->b_ml.ml_line_count;
5381 pos.col = 0;
5382 }
5383 else
5384 {
5385 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005386 if (charcol)
5387 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5388 else
5389 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391 return &pos;
5392 }
5393 return NULL;
5394}
5395
5396/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005397 * Convert list in "arg" into a position and optional file number.
5398 * When "fnump" is NULL there is no file number, only 3 items.
5399 * Note that the column is passed on as-is, the caller may want to decrement
5400 * it to use 1 for the first column.
5401 * Return FAIL when conversion is not possible, doesn't check the position for
5402 * validity.
5403 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005404 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005405list2fpos(
5406 typval_T *arg,
5407 pos_T *posp,
5408 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005409 colnr_T *curswantp,
5410 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005411{
5412 list_T *l = arg->vval.v_list;
5413 long i = 0;
5414 long n;
5415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005416 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5417 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005418 if (arg->v_type != VAR_LIST
5419 || l == NULL
5420 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005421 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005422 return FAIL;
5423
5424 if (fnump != NULL)
5425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005426 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005427 if (n < 0)
5428 return FAIL;
5429 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005430 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005431 *fnump = n;
5432 }
5433
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005434 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005435 if (n < 0)
5436 return FAIL;
5437 posp->lnum = n;
5438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005439 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005440 if (n < 0)
5441 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005442 // If character position is specified, then convert to byte position
5443 if (charcol)
5444 {
5445 buf_T *buf;
5446
5447 // Get the text for the specified line in a loaded buffer
5448 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5449 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5450 return FAIL;
5451
Bram Moolenaar91458462021-01-13 20:08:38 +01005452 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005453 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005454 posp->col = n;
5455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005457 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005458 posp->coladd = 0;
5459 else
5460 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005461
Bram Moolenaar493c1782014-05-28 14:34:46 +02005462 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005464
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005465 return OK;
5466}
5467
5468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 * Get the length of an environment variable name.
5470 * Advance "arg" to the first character after the name.
5471 * Return 0 for error.
5472 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005474get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 char_u *p;
5477 int len;
5478
5479 for (p = *arg; vim_isIDc(*p); ++p)
5480 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005481 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 return 0;
5483
5484 len = (int)(p - *arg);
5485 *arg = p;
5486 return len;
5487}
5488
5489/*
5490 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005491 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 * Return 0 if something is wrong.
5493 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005495get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
5497 char_u *p;
5498 int len;
5499
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005502 {
5503 if (*p == ':')
5504 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 // "s:" is start of "s:var", but "n:" is not and can be used in
5506 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005507 len = (int)(p - *arg);
5508 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5509 || len > 1)
5510 break;
5511 }
5512 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005513 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 return 0;
5515
5516 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005517 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518
5519 return len;
5520}
5521
5522/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005523 * Get the length of the name of a variable or function.
5524 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005526 * Return -1 if curly braces expansion failed.
5527 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 * If the name contains 'magic' {}'s, expand them and return the
5529 * expanded name in an allocated string via 'alias' - caller must free.
5530 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005532get_name_len(
5533 char_u **arg,
5534 char_u **alias,
5535 int evaluate,
5536 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 char_u *p;
5540 char_u *expr_start;
5541 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
5545 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5546 && (*arg)[2] == (int)KE_SNR)
5547 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005548 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 *arg += 3;
5550 return get_id_len(arg) + 3;
5551 }
5552 len = eval_fname_script(*arg);
5553 if (len > 0)
5554 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005555 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 *arg += len;
5557 }
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005562 p = find_name_end(*arg, &expr_start, &expr_end,
5563 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 if (expr_start != NULL)
5565 {
5566 char_u *temp_string;
5567
5568 if (!evaluate)
5569 {
5570 len += (int)(p - *arg);
5571 *arg = skipwhite(p);
5572 return len;
5573 }
5574
5575 /*
5576 * Include any <SID> etc in the expanded string:
5577 * Thus the -len here.
5578 */
5579 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5580 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005581 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 *alias = temp_string;
5583 *arg = skipwhite(p);
5584 return (int)STRLEN(temp_string);
5585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
5587 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005588 // Only give an error when there is something, otherwise it will be
5589 // reported at a higher level.
5590 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005591 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
5593 return len;
5594}
5595
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596/*
5597 * Find the end of a variable or function name, taking care of magic braces.
5598 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5599 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005600 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601 * Return a pointer to just after the name. Equal to "arg" if there is no
5602 * valid name.
5603 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005604 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005605find_name_end(
5606 char_u *arg,
5607 char_u **expr_start,
5608 char_u **expr_end,
5609 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 int mb_nest = 0;
5612 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005614 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005615 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005617 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619 *expr_start = NULL;
5620 *expr_end = NULL;
5621 }
5622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005623 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005624 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5625 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005626 return arg;
5627
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005628 for (p = arg; *p != NUL
5629 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005630 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005631 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005632 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005634 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005636 if (*p == '\'')
5637 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005638 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005639 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005640 ;
5641 if (*p == NUL)
5642 break;
5643 }
5644 else if (*p == '"')
5645 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005646 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005647 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005648 if (*p == '\\' && p[1] != NUL)
5649 ++p;
5650 if (*p == NUL)
5651 break;
5652 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005653 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005655 // "s:" is start of "s:var", but "n:" is not and can be used in
5656 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005657 len = (int)(p - arg);
5658 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005659 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005660 break;
5661 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 if (*p == '[')
5666 ++br_nest;
5667 else if (*p == ']')
5668 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005670
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005671 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 if (*p == '{')
5674 {
5675 mb_nest++;
5676 if (expr_start != NULL && *expr_start == NULL)
5677 *expr_start = p;
5678 }
5679 else if (*p == '}')
5680 {
5681 mb_nest--;
5682 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5683 *expr_end = p;
5684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687
5688 return p;
5689}
5690
5691/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005692 * Expands out the 'magic' {}'s in a variable/function name.
5693 * Note that this can call itself recursively, to deal with
5694 * constructs like foo{bar}{baz}{bam}
5695 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5696 * "in_start" ^
5697 * "expr_start" ^
5698 * "expr_end" ^
5699 * "in_end" ^
5700 *
5701 * Returns a new allocated string, which the caller must free.
5702 * Returns NULL for failure.
5703 */
5704 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705make_expanded_name(
5706 char_u *in_start,
5707 char_u *expr_start,
5708 char_u *expr_end,
5709 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005710{
5711 char_u c1;
5712 char_u *retval = NULL;
5713 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005714
5715 if (expr_end == NULL || in_end == NULL)
5716 return NULL;
5717 *expr_start = NUL;
5718 *expr_end = NUL;
5719 c1 = *in_end;
5720 *in_end = NUL;
5721
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005722 temp_result = eval_to_string(expr_start + 1, FALSE);
5723 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005724 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005725 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5726 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005727 if (retval != NULL)
5728 {
5729 STRCPY(retval, in_start);
5730 STRCAT(retval, temp_result);
5731 STRCAT(retval, expr_end + 1);
5732 }
5733 }
5734 vim_free(temp_result);
5735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005736 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005737 *expr_start = '{';
5738 *expr_end = '}';
5739
5740 if (retval != NULL)
5741 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005742 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005743 if (expr_start != NULL)
5744 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005745 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005746 temp_result = make_expanded_name(retval, expr_start,
5747 expr_end, temp_result);
5748 vim_free(retval);
5749 retval = temp_result;
5750 }
5751 }
5752
5753 return retval;
5754}
5755
5756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005758 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005760 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005761eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005763 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005764}
5765
5766/*
5767 * Return TRUE if character "c" can be used as the first character in a
5768 * variable or function name (excluding '{' and '}').
5769 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005771eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005772{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005773 return ASCII_ISALPHA(c) || c == '_';
5774}
5775
5776/*
5777 * Return TRUE if character "c" can be used as the first character of a
5778 * dictionary key.
5779 */
5780 int
5781eval_isdictc(int c)
5782{
5783 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784}
5785
5786/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005787 * Handle:
5788 * - expr[expr], expr[expr:expr] subscript
5789 * - ".name" lookup
5790 * - function call with Funcref variable: func(expr)
5791 * - method call: var->method()
5792 *
5793 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005794 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005796handle_subscript(
5797 char_u **arg,
5798 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005799 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005800 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005801{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005802 int evaluate = evalarg != NULL
5803 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005804 int ret = OK;
5805 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005806 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005807 int getnext;
5808 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005809
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005810 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005811 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005812 // When at the end of the line and ".name" or "->{" or "->X" follows in
5813 // the next line then consume the line break.
5814 p = eval_next_non_blank(*arg, evalarg, &getnext);
5815 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005816 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005817 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005818 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005819 {
5820 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005821 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005822 check_white = FALSE;
5823 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005824
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005825 if (rettv->v_type == VAR_ANY)
5826 {
5827 char_u *exp_name;
5828 int cc;
5829 int idx;
5830 ufunc_T *ufunc;
5831 type_T *type;
5832
5833 // Found script from "import * as {name}", script item name must
5834 // follow.
5835 if (**arg != '.')
5836 {
5837 if (verbose)
5838 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5839 ret = FAIL;
5840 break;
5841 }
5842 ++*arg;
5843 if (IS_WHITE_OR_NUL(**arg))
5844 {
5845 if (verbose)
5846 emsg(_(e_no_white_space_allowed_after_dot));
5847 ret = FAIL;
5848 break;
5849 }
5850
5851 // isolate the name
5852 exp_name = *arg;
5853 while (eval_isnamec(**arg))
5854 ++*arg;
5855 cc = **arg;
5856 **arg = NUL;
5857
5858 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5859 evalarg->eval_cctx, verbose);
5860 **arg = cc;
5861 *arg = skipwhite(*arg);
5862
5863 if (idx < 0 && ufunc == NULL)
5864 {
5865 ret = FAIL;
5866 break;
5867 }
5868 if (idx >= 0)
5869 {
5870 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5871 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5872
5873 copy_tv(sv->sv_tv, rettv);
5874 }
5875 else
5876 {
5877 rettv->v_type = VAR_FUNC;
5878 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5879 }
5880 }
5881
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005882 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5883 || rettv->v_type == VAR_PARTIAL))
5884 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005885 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005886 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5887 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005888
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005889 // Stop the expression evaluation when immediately aborting on
5890 // error, or when an interrupt occurred or an exception was thrown
5891 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005892 if (aborting())
5893 {
5894 if (ret == OK)
5895 clear_tv(rettv);
5896 ret = FAIL;
5897 }
5898 dict_unref(selfdict);
5899 selfdict = NULL;
5900 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005901 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005902 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005903 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005904 if (ret == OK)
5905 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005906 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005907 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005908 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005909 else
5910 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005911 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005912 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005913 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005914 // "." is ".name" lookup when we found a dict or when evaluating and
5915 // scriptversion is at least 2, where string concatenation is "..".
5916 else if (**arg == '['
5917 || (**arg == '.' && (rettv->v_type == VAR_DICT
5918 || (!evaluate
5919 && (*arg)[1] != '.'
5920 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005921 {
5922 dict_unref(selfdict);
5923 if (rettv->v_type == VAR_DICT)
5924 {
5925 selfdict = rettv->vval.v_dict;
5926 if (selfdict != NULL)
5927 ++selfdict->dv_refcount;
5928 }
5929 else
5930 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005931 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005932 {
5933 clear_tv(rettv);
5934 ret = FAIL;
5935 }
5936 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005937 else
5938 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005939 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005941 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5942 // Don't do this when "Func" is already a partial that was bound
5943 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005944 if (selfdict != NULL
5945 && (rettv->v_type == VAR_FUNC
5946 || (rettv->v_type == VAR_PARTIAL
5947 && (rettv->vval.v_partial->pt_auto
5948 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005949 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005950
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005951 dict_unref(selfdict);
5952 return ret;
5953}
5954
5955/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005956 * Make a copy of an item.
5957 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005958 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5959 * reference to an already copied list/dict can be used.
5960 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005961 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005963item_copy(
5964 typval_T *from,
5965 typval_T *to,
5966 int deep,
5967 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005968{
5969 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005970 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005971
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005974 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005976 }
5977 ++recurse;
5978
5979 switch (from->v_type)
5980 {
5981 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005982 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005983 case VAR_STRING:
5984 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005985 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005986 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005987 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005988 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005989 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990 copy_tv(from, to);
5991 break;
5992 case VAR_LIST:
5993 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005994 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005995 if (from->vval.v_list == NULL)
5996 to->vval.v_list = NULL;
5997 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5998 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005999 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006000 to->vval.v_list = from->vval.v_list->lv_copylist;
6001 ++to->vval.v_list->lv_refcount;
6002 }
6003 else
6004 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6005 if (to->vval.v_list == NULL)
6006 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006007 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006008 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006009 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006010 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006011 case VAR_DICT:
6012 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006013 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006014 if (from->vval.v_dict == NULL)
6015 to->vval.v_dict = NULL;
6016 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6017 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006018 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006019 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6020 ++to->vval.v_dict->dv_refcount;
6021 }
6022 else
6023 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6024 if (to->vval.v_dict == NULL)
6025 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006026 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006027 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006028 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006030 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006031 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006032 }
6033 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006034 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006035}
6036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 void
6038echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6039{
6040 char_u *tofree;
6041 char_u numbuf[NUMBUFLEN];
6042 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6043
6044 if (*atstart)
6045 {
6046 *atstart = FALSE;
6047 // Call msg_start() after eval1(), evaluating the expression
6048 // may cause a message to appear.
6049 if (with_space)
6050 {
6051 // Mark the saved text as finishing the line, so that what
6052 // follows is displayed on a new line when scrolling back
6053 // at the more prompt.
6054 msg_sb_eol();
6055 msg_start();
6056 }
6057 }
6058 else if (with_space)
6059 msg_puts_attr(" ", echo_attr);
6060
6061 if (p != NULL)
6062 for ( ; *p != NUL && !got_int; ++p)
6063 {
6064 if (*p == '\n' || *p == '\r' || *p == TAB)
6065 {
6066 if (*p != TAB && *needclr)
6067 {
6068 // remove any text still there from the command
6069 msg_clr_eos();
6070 *needclr = FALSE;
6071 }
6072 msg_putchar_attr(*p, echo_attr);
6073 }
6074 else
6075 {
6076 if (has_mbyte)
6077 {
6078 int i = (*mb_ptr2len)(p);
6079
6080 (void)msg_outtrans_len_attr(p, i, echo_attr);
6081 p += i - 1;
6082 }
6083 else
6084 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6085 }
6086 }
6087 vim_free(tofree);
6088}
6089
Bram Moolenaare9a41262005-01-15 22:18:47 +00006090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 * ":echo expr1 ..." print each argument separated with a space, add a
6092 * newline at the end.
6093 * ":echon expr1 ..." print each argument plain.
6094 */
6095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 char_u *p;
6101 int needclr = TRUE;
6102 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006103 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006104 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006105 evalarg_T evalarg;
6106
Bram Moolenaare707c882020-07-01 13:07:37 +02006107 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109 if (eap->skip)
6110 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006111 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006113 // If eval1() causes an error message the text from the command may
6114 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006115 need_clr_eos = needclr;
6116
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006118 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
6120 /*
6121 * Report the invalid expression unless the expression evaluation
6122 * has been cancelled due to an aborting error, an interrupt, or an
6123 * exception.
6124 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006125 if (!aborting() && did_emsg == did_emsg_before
6126 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006127 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006128 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 break;
6130 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006131 need_clr_eos = FALSE;
6132
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006135
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006136 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 arg = skipwhite(arg);
6138 }
6139 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006140 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141
6142 if (eap->skip)
6143 --emsg_skip;
6144 else
6145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006146 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 if (needclr)
6148 msg_clr_eos();
6149 if (eap->cmdidx == CMD_echo)
6150 msg_end();
6151 }
6152}
6153
6154/*
6155 * ":echohl {name}".
6156 */
6157 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006158ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006160 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161}
6162
6163/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006164 * Returns the :echo attribute
6165 */
6166 int
6167get_echo_attr(void)
6168{
6169 return echo_attr;
6170}
6171
6172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 * ":execute expr1 ..." execute the result of an expression.
6174 * ":echomsg expr1 ..." Print a message
6175 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006176 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 * Each gets spaces around each argument and a newline at the end for
6178 * echo commands
6179 */
6180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006181ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
6183 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 int ret = OK;
6186 char_u *p;
6187 garray_T ga;
6188 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
6190 ga_init2(&ga, 1, 80);
6191
6192 if (eap->skip)
6193 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006194 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006196 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006197 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199
6200 if (!eap->skip)
6201 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006202 char_u buf[NUMBUFLEN];
6203
6204 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006205 {
6206 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6207 {
6208 emsg(_(e_inval_string));
6209 p = NULL;
6210 }
6211 else
6212 p = tv_get_string_buf(&rettv, buf);
6213 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006214 else
6215 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006216 if (p == NULL)
6217 {
6218 clear_tv(&rettv);
6219 ret = FAIL;
6220 break;
6221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 len = (int)STRLEN(p);
6223 if (ga_grow(&ga, len + 2) == FAIL)
6224 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006225 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 ret = FAIL;
6227 break;
6228 }
6229 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 ga.ga_len += len;
6233 }
6234
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006235 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 arg = skipwhite(arg);
6237 }
6238
6239 if (ret != FAIL && ga.ga_data != NULL)
6240 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006241 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6242 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006243 // Mark the already saved text as finishing the line, so that what
6244 // follows is displayed on a new line when scrolling back at the
6245 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006246 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006247 }
6248
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006250 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006251 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006252 out_flush();
6253 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006254 else if (eap->cmdidx == CMD_echoconsole)
6255 {
6256 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6257 ui_write((char_u *)"\r\n", 2, TRUE);
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 else if (eap->cmdidx == CMD_echoerr)
6260 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006261 int save_did_emsg = did_emsg;
6262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006263 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006264 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (!force_abort)
6266 did_emsg = save_did_emsg;
6267 }
6268 else if (eap->cmdidx == CMD_execute)
6269 do_cmdline((char_u *)ga.ga_data,
6270 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6271 }
6272
6273 ga_clear(&ga);
6274
6275 if (eap->skip)
6276 --emsg_skip;
6277
6278 eap->nextcmd = check_nextcmd(arg);
6279}
6280
6281/*
6282 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6283 * "arg" points to the "&" or '+' when called, to "option" when returning.
6284 * Returns NULL when no option name found. Otherwise pointer to the char
6285 * after the option name.
6286 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006287 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006288find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 char_u *p = *arg;
6291
6292 ++p;
6293 if (*p == 'g' && p[1] == ':')
6294 {
6295 *opt_flags = OPT_GLOBAL;
6296 p += 2;
6297 }
6298 else if (*p == 'l' && p[1] == ':')
6299 {
6300 *opt_flags = OPT_LOCAL;
6301 p += 2;
6302 }
6303 else
6304 *opt_flags = 0;
6305
6306 if (!ASCII_ISALPHA(*p))
6307 return NULL;
6308 *arg = p;
6309
6310 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006311 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 else
6313 while (ASCII_ISALPHA(*p))
6314 ++p;
6315 return p;
6316}
6317
6318/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006319 * Display script name where an item was last set.
6320 * Should only be invoked when 'verbose' is non-zero.
6321 */
6322 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006323last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006324{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006325 char_u *p;
6326
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006327 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006328 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006329 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006330 if (p != NULL)
6331 {
6332 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006333 msg_puts(_("\n\tLast set from "));
6334 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006335 if (script_ctx.sc_lnum > 0)
6336 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006337 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006338 msg_outnum((long)script_ctx.sc_lnum);
6339 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006340 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006341 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006342 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006343 }
6344}
6345
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006346#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
6348/*
6349 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006350 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 * "flags" can be "g" to do a global substitute.
6352 * Returns an allocated string, NULL for error.
6353 */
6354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006355do_string_sub(
6356 char_u *str,
6357 char_u *pat,
6358 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006359 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006360 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
6362 int sublen;
6363 regmatch_T regmatch;
6364 int i;
6365 int do_all;
6366 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006367 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 garray_T ga;
6369 char_u *ret;
6370 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006371 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006373 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006375 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
6377 ga_init2(&ga, 1, 200);
6378
6379 do_all = (flags[0] == 'g');
6380
6381 regmatch.rm_ic = p_ic;
6382 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6383 if (regmatch.regprog != NULL)
6384 {
6385 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006386 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6388 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006389 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006390 if (regmatch.startp[0] == regmatch.endp[0])
6391 {
6392 if (zero_width == regmatch.startp[0])
6393 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006394 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006395 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006396 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6397 (size_t)i);
6398 ga.ga_len += i;
6399 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006400 continue;
6401 }
6402 zero_width = regmatch.startp[0];
6403 }
6404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 /*
6406 * Get some space for a temporary buffer to do the substitution
6407 * into. It will contain:
6408 * - The text up to where the match is.
6409 * - The substituted text.
6410 * - The text after the match.
6411 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006412 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006413 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6415 {
6416 ga_clear(&ga);
6417 break;
6418 }
6419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006420 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 i = (int)(regmatch.startp[0] - tail);
6422 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006423 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006424 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 + ga.ga_len + i, TRUE, TRUE, FALSE);
6426 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006427 tail = regmatch.endp[0];
6428 if (*tail == NUL)
6429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 if (!do_all)
6431 break;
6432 }
6433
6434 if (ga.ga_data != NULL)
6435 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6436
Bram Moolenaar473de612013-06-08 18:19:48 +02006437 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 }
6439
6440 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6441 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006442 if (p_cpo == empty_option)
6443 p_cpo = save_cpo;
6444 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006445 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006446 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006447 // If it's still empty it was changed and restored, need to restore in
6448 // the complicated way.
6449 if (*p_cpo == NUL)
6450 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006451 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
6454 return ret;
6455}