blob: dc68c65c5601edde168615da537395ba4ae3a567 [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 Moolenaar5409f5d2020-06-24 18:37:35 +020025static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
26static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
27static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
28static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010029static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020030static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010031static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
32static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
33static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000034
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020035static 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 +020036
Bram Moolenaare21c1582019-03-02 11:57:09 +010037/*
38 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010039 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010040 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020041 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010042num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010043{
44 varnumber_T result;
45
Bram Moolenaar99880f92021-01-20 21:23:14 +010046 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010047 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010048 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010049 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010050 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010051 if (failed != NULL)
52 *failed = TRUE;
53 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010054 if (n1 == 0)
55 result = VARNUM_MIN; // similar to NaN
56 else if (n1 < 0)
57 result = -VARNUM_MAX;
58 else
59 result = VARNUM_MAX;
60 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010061 else if (n1 == VARNUM_MIN && n2 == -1)
62 {
63 // specific case: trying to do VARNUM_MIN / -1 results in a positive
64 // number that doesn't fit in varnumber_T and causes an FPE
65 result = VARNUM_MAX;
66 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010067 else
68 result = n1 / n2;
69
70 return result;
71}
72
73/*
74 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010076 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020077 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010078num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010079{
Bram Moolenaar99880f92021-01-20 21:23:14 +010080 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010081 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010082 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 if (failed != NULL)
84 *failed = TRUE;
85 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010086 return (n2 == 0) ? 0 : (n1 % n2);
87}
88
Bram Moolenaar33570922005-01-25 22:26:29 +000089/*
90 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000091 */
92 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010093eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +000094{
Bram Moolenaare5cdf152019-08-29 22:09:46 +020095 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +020096 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +000097}
98
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000099#if defined(EXITFREE) || defined(PROTO)
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100104 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200105 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000106
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200107 // autoloaded script names
108 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000109
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200110 // unreferenced lists and dicts
111 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200112
113 // functions not garbage collected
114 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000115}
116#endif
117
Bram Moolenaare6b53242020-07-01 17:28:33 +0200118 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200119fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
120{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100121 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200122 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000123
124 if (eap == NULL)
125 return;
126
127 evalarg->eval_cstack = eap->cstack;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100128 if (sourcing_a_script(eap) || eap->ea_getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200129 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100130 evalarg->eval_getline = eap->ea_getline;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200132 }
133}
134
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135/*
136 * Top level evaluation function, returning a boolean.
137 * Sets "error" to TRUE if there was an error.
138 * Return TRUE or FALSE.
139 */
140 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100141eval_to_bool(
142 char_u *arg,
143 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200144 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100145 int skip, // only parse, don't execute
146 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147{
Bram Moolenaar33570922005-01-25 22:26:29 +0000148 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200149 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200150 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100151 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200152
Bram Moolenaar37c83712020-06-30 21:18:36 +0200153 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155 if (skip)
156 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100157 if (use_simple_function)
158 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
159 else
160 r = eval0(arg, &tv, eap, &evalarg);
161 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 else
164 {
165 *error = FALSE;
166 if (!skip)
167 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200168 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200169 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200170 else
171 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000172 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 }
174 }
175 if (skip)
176 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200177 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180}
181
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100182/*
183 * Call eval1() and give an error message if not done at a lower level.
184 */
185 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200186eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100187{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100188 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100189 int ret;
190 int did_emsg_before = did_emsg;
191 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200192 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100193
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
195
196 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 if (ret == FAIL)
198 {
199 // Report the invalid expression unless the expression evaluation has
200 // been cancelled due to an aborting error, an interrupt, or an
201 // exception, or we already gave a more specific error.
202 // Also check called_emsg for when using assert_fails().
203 if (!aborting() && did_emsg == did_emsg_before
204 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200205 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200207 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 return ret;
209}
210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200212 * Return whether a typval is a valid expression to pass to eval_expr_typval()
213 * or eval_expr_to_bool(). An empty string returns FALSE;
214 */
215 int
216eval_expr_valid_arg(typval_T *tv)
217{
218 return tv->v_type != VAR_UNKNOWN
219 && (tv->v_type != VAR_STRING
220 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
221}
222
223/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100224 * When calling eval_expr_typval() many times we only need one funccall_T.
225 * Returns NULL when no funccall_T is to be used.
226 * When returning non-NULL remove_funccal() must be called later.
227 */
228 funccall_T *
229eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
230{
231 if (expr->v_type != VAR_PARTIAL)
232 return NULL;
233
234 partial_T *partial = expr->vval.v_partial;
235 if (partial == NULL)
236 return NULL;
237 if (partial->pt_func == NULL
238 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
239 return NULL;
240
241 return create_funccal(partial->pt_func, rettv);
242}
243
244/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200245 * Evaluate a partial.
246 * Pass arguments "argv[argc]".
247 * "fc_arg" is from eval_expr_get_funccal() or NULL;
248 * Return the result in "rettv" and OK or FAIL.
249 */
250 static int
251eval_expr_partial(
252 typval_T *expr,
253 typval_T *argv,
254 int argc,
255 funccall_T *fc_arg,
256 typval_T *rettv)
257{
258 partial_T *partial = expr->vval.v_partial;
259
260 if (partial == NULL)
261 return FAIL;
262
263 if (partial->pt_func != NULL
264 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
265 {
266 funccall_T *fc = fc_arg != NULL ? fc_arg
267 : create_funccal(partial->pt_func, rettv);
268 int r;
269
270 if (fc == NULL)
271 return FAIL;
272
273 // Shortcut to call a compiled function with minimal overhead.
274 r = call_def_function(partial->pt_func, argc, argv, DEF_USE_PT_ARGV,
275 partial, NULL, fc, rettv);
276 if (fc_arg == NULL)
277 remove_funccal();
278 if (r == FAIL)
279 return FAIL;
280 }
281 else
282 {
283 char_u *s = partial_name(partial);
284 funcexe_T funcexe;
285
286 if (s == NULL || *s == NUL)
287 return FAIL;
288
289 CLEAR_FIELD(funcexe);
290 funcexe.fe_evaluate = TRUE;
291 funcexe.fe_partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
295
296 return OK;
297}
298
299/*
300 * Evaluate an expression which is a function.
301 * Pass arguments "argv[argc]".
302 * Return the result in "rettv" and OK or FAIL.
303 */
304 static int
305eval_expr_func(
306 typval_T *expr,
307 typval_T *argv,
308 int argc,
309 typval_T *rettv)
310{
311 funcexe_T funcexe;
312 char_u buf[NUMBUFLEN];
313 char_u *s;
314
315 if (expr->v_type == VAR_FUNC)
316 s = expr->vval.v_string;
317 else
318 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
319 if (s == NULL || *s == NUL)
320 return FAIL;
321
322 CLEAR_FIELD(funcexe);
323 funcexe.fe_evaluate = TRUE;
324 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
325 return FAIL;
326
327 return OK;
328}
329
330/*
331 * Evaluate an expression, which is a string.
332 * Return the result in "rettv" and OK or FAIL.
333 */
334 static int
335eval_expr_string(
336 typval_T *expr,
337 typval_T *rettv)
338{
339 char_u *s;
340 char_u buf[NUMBUFLEN];
341
342 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
343 if (s == NULL)
344 return FAIL;
345
346 s = skipwhite(s);
347 if (eval1_emsg(&s, rettv, NULL) == FAIL)
348 return FAIL;
349
350 if (*skipwhite(s) != NUL) // check for trailing chars after expr
351 {
352 clear_tv(rettv);
353 semsg(_(e_invalid_expression_str), s);
354 return FAIL;
355 }
356
357 return OK;
358}
359
360/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361 * Evaluate an expression, which can be a function, partial or string.
362 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200363 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100364 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365 * Return the result in "rettv" and OK or FAIL.
366 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200367 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100368eval_expr_typval(
369 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200370 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100371 typval_T *argv,
372 int argc,
373 funccall_T *fc_arg,
374 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100375{
zeertzjqad0c4422023-08-17 22:15:47 +0200376 if (expr->v_type == VAR_PARTIAL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200377 return eval_expr_partial(expr, argv, argc, fc_arg, rettv);
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200378 else if (expr->v_type == VAR_INSTR)
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200379 return exe_typval_instr(expr, rettv);
zeertzjqad0c4422023-08-17 22:15:47 +0200380 else if (expr->v_type == VAR_FUNC || want_func)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200381 return eval_expr_func(expr, argv, argc, rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100382 else
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200383 return eval_expr_string(expr, rettv);
384
Bram Moolenaar48570482017-10-30 21:48:41 +0100385 return OK;
386}
387
388/*
389 * Like eval_to_bool() but using a typval_T instead of a string.
390 * Works for string, funcref and partial.
391 */
392 int
393eval_expr_to_bool(typval_T *expr, int *error)
394{
395 typval_T rettv;
396 int res;
397
zeertzjqad0c4422023-08-17 22:15:47 +0200398 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100399 {
400 *error = TRUE;
401 return FALSE;
402 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200403 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100404 clear_tv(&rettv);
405 return res;
406}
407
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408/*
409 * Top level evaluation function, returning a string. If "skip" is TRUE,
410 * only parsing to "nextcmd" is done, without reporting errors. Return
411 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
412 */
413 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100414eval_to_string_skip(
415 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200416 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100417 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418{
Bram Moolenaar33570922005-01-25 22:26:29 +0000419 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200421 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
Bram Moolenaar37c83712020-06-30 21:18:36 +0200423 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 if (skip)
425 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200426 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 retval = NULL;
428 else
429 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100430 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000431 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 }
433 if (skip)
434 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200435 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437 return retval;
438}
439
440/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100441 * Initialize "evalarg" for use.
442 */
443 void
444init_evalarg(evalarg_T *evalarg)
445{
446 CLEAR_POINTER(evalarg);
447 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
448}
449
450/*
451 * If "evalarg->eval_tofree" is not NULL free it later.
452 * Caller is expected to overwrite "evalarg->eval_tofree" next.
453 */
454 static void
455free_eval_tofree_later(evalarg_T *evalarg)
456{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000457 if (evalarg->eval_tofree == NULL)
458 return;
459
460 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
461 ((char_u **)evalarg->eval_tofree_ga.ga_data)
462 [evalarg->eval_tofree_ga.ga_len++]
463 = evalarg->eval_tofree;
464 else
465 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100466}
467
468/*
469 * After using "evalarg" filled from "eap": free the memory.
470 */
471 void
472clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
473{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000474 if (evalarg == NULL)
475 return;
476
477 garray_T *etga = &evalarg->eval_tofree_ga;
478
479 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100480 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000481 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100482 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000483 // We may need to keep the original command line, e.g. for
484 // ":let" it has the variable names. But we may also need
485 // the new one, "nextcmd" points into it. Keep both.
486 vim_free(eap->cmdline_tofree);
487 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100488
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000489 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
490 {
491 // "nextcmd" points into the last line in eval_tofree_ga,
492 // need to keep it around.
493 --etga->ga_len;
494 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
495 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100496 }
497 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000498 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100499 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000500 else
501 vim_free(evalarg->eval_tofree);
502 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100503 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000504
505 ga_clear_strings(etga);
506 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100507}
508
509/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000510 * Skip over an expression at "*pp".
511 * Return FAIL for an error, OK otherwise.
512 */
513 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200514skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000515{
Bram Moolenaar33570922005-01-25 22:26:29 +0000516 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000517
518 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200519 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000520}
521
522/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200523 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 * If in Vim9 script and line breaks are encountered, the lines are
525 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200526 * "arg" is advanced to just after the expression.
527 * "start" is set to the start of the expression, "end" to just after the end.
528 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200529 * Return FAIL for an error, OK otherwise.
530 */
531 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200532skip_expr_concatenate(
533 char_u **arg,
534 char_u **start,
535 char_u **end,
536 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200537{
538 typval_T rettv;
539 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200540 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200541 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200542 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200543 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200544 int evaluate = evalarg == NULL
545 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200546
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200547 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200548 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200549 {
550 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200552 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200553 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200555 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200556 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200557
558 // Don't evaluate the expression.
559 if (evalarg != NULL)
560 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200561 *arg = skipwhite(*arg);
562 res = eval1(arg, &rettv, evalarg);
563 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200564 if (evalarg != NULL)
565 evalarg->eval_flags = save_flags;
566
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200567 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200568 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200569 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200570 if (evalarg->eval_ga.ga_len == 1)
571 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200572 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200573 ga_clear(gap);
574 gap->ga_itemsize = 0;
575 }
576 else
577 {
578 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200579 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200580
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200581 // Line breaks encountered, concatenate all the lines.
582 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200583 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200584
585 // free the lines only when using getsourceline()
586 if (evalarg->eval_cookie != NULL)
587 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200588 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200589 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200590 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100591 // later. Also free "eval_tofree" later if needed.
592 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200593 evalarg->eval_tofree =
594 ((char_u **)gap->ga_data)[gap->ga_len - 1];
595 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200596 ga_clear_strings(gap);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +0200597 ga_clear(freegap);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200598 }
599 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200600 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200601 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200602
603 // free lines that were explicitly marked for freeing
604 ga_clear_strings(freegap);
605 }
606
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200607 gap->ga_itemsize = 0;
608 if (p == NULL)
609 return FAIL;
610 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200611 vim_free(evalarg->eval_tofree_lambda);
612 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200613 // Compute "end" relative to the end.
614 *end = *start + STRLEN(*start) - endoff;
615 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200616 }
617
618 return res;
619}
620
621/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100622 * Convert "tv" to a string.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200623 * When "join_list" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100624 * Returns an allocated string (NULL when out of memory).
625 */
626 char_u *
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200627typval2string(typval_T *tv, int join_list)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100628{
629 garray_T ga;
630 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100631
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200632 if (join_list && tv->v_type == VAR_LIST)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100633 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000634 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100635 if (tv->vval.v_list != NULL)
636 {
637 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
638 if (tv->vval.v_list->lv_len > 0)
639 ga_append(&ga, NL);
640 }
641 ga_append(&ga, NUL);
642 retval = (char_u *)ga.ga_data;
643 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200644 else if (tv->v_type == VAR_LIST || tv->v_type == VAR_DICT)
645 {
646 char_u *tofree;
647 char_u numbuf[NUMBUFLEN];
648
649 retval = tv2string(tv, &tofree, numbuf, 0);
650 // Make a copy if we have a value but it's not in allocated memory.
651 if (retval != NULL && tofree == NULL)
652 retval = vim_strsave(retval);
653 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100654 else
655 retval = vim_strsave(tv_get_string(tv));
656 return retval;
657}
658
659/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200660 * Top level evaluation function, returning a string. Does not handle line
661 * breaks.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200662 * When "join_list" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 * Return pointer to allocated memory, or NULL for failure.
664 */
665 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100666eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100667 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200668 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100669 exarg_T *eap,
670 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
Bram Moolenaar33570922005-01-25 22:26:29 +0000672 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100674 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100675 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100677 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100678 if (use_simple_function)
679 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
680 else
681 r = eval0(arg, &tv, NULL, &evalarg);
682 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 retval = NULL;
684 else
685 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200686 retval = typval2string(&tv, join_list);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000687 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100689 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 return retval;
692}
693
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100694 char_u *
695eval_to_string(
696 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200697 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100698 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100699{
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200700 return eval_to_string_eap(arg, join_list, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100701}
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000704 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100705 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200706 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 */
708 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100709eval_to_string_safe(
710 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000711 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100712 int keep_script_version,
713 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200716 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200717 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200718 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar9530b582022-01-22 13:39:08 +0000720 if (!keep_script_version)
721 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200722 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000723 if (use_sandbox)
724 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100725 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200726 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100727 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000728 if (use_sandbox)
729 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100730 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200731 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200732 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200733 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 return retval;
735}
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737/*
738 * Top level evaluation function, returning a number.
739 * Evaluates "expr" silently.
740 * Returns -1 for an error.
741 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200742 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100743eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaar33570922005-01-25 22:26:29 +0000745 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200746 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000747 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100748 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
750 ++emsg_off;
751
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100752 if (use_simple_function)
753 r = may_call_simple_func(expr, &rettv);
754 if (r == NOTDONE)
755 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
756 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 retval = -1;
758 else
759 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100760 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000761 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763 --emsg_off;
764
765 return retval;
766}
767
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000768/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000769 * Top level evaluation function.
770 * Returns an allocated typval_T with the result.
771 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000772 */
773 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200774eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000775{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100776 return eval_expr_ext(arg, eap, FALSE);
777}
778
779 typval_T *
780eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
781{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000782 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200783 evalarg_T evalarg;
784
785 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000786
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200787 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100788 if (tv != NULL)
789 {
790 int r = NOTDONE;
791
792 if (use_simple_function)
793 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
794 if (r == NOTDONE)
795 r = eval0(arg, tv, eap, &evalarg);
796
797 if (r == FAIL)
798 VIM_CLEAR(tv);
799 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000800
Bram Moolenaar37c83712020-06-30 21:18:36 +0200801 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000802 return tv;
803}
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000806 * "*arg" points to what can be a function name in the form of "import.Name" or
807 * "Funcref". Return the name of the function. Set "tofree" to something that
808 * was allocated.
809 * If "verbose" is FALSE no errors are given.
810 * Return NULL for any failure.
811 */
812 static char_u *
813deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000814 char_u **arg,
815 char_u **tofree,
816 evalarg_T *evalarg,
817 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000818{
819 typval_T ref;
820 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100821 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822
823 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100824 if (evalarg != NULL)
825 {
826 // need to evaluate this to get an import, like in "a.Func"
827 save_flags = evalarg->eval_flags;
828 evalarg->eval_flags |= EVAL_EVALUATE;
829 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100830 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100831 {
832 dictitem_T *v;
833
834 // If <SID>VarName was used it would not be found, try another way.
835 v = find_var_also_in_script(name, NULL, FALSE);
836 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100837 {
838 name = NULL;
839 goto theend;
840 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100841 copy_tv(&v->di_tv, &ref);
842 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 if (*skipwhite(*arg) != NUL)
844 {
845 if (verbose)
846 semsg(_(e_trailing_characters_str), *arg);
847 name = NULL;
848 }
849 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
850 {
851 name = ref.vval.v_string;
852 ref.vval.v_string = NULL;
853 *tofree = name;
854 }
855 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
856 {
857 if (ref.vval.v_partial->pt_argc > 0
858 || ref.vval.v_partial->pt_dict != NULL)
859 {
860 if (verbose)
861 emsg(_(e_cannot_use_partial_here));
862 name = NULL;
863 }
864 else
865 {
866 name = vim_strsave(partial_name(ref.vval.v_partial));
867 *tofree = name;
868 }
869 }
870 else
871 {
872 if (verbose)
873 semsg(_(e_not_callable_type_str), name);
874 name = NULL;
875 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100876
877theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000878 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100879 if (evalarg != NULL)
880 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000881 return name;
882}
883
884/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100885 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200886 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
887 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200890 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100891call_vim_function(
892 char_u *func,
893 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200894 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200895 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000897 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200898 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000899 char_u *arg;
900 char_u *name;
901 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000902 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100904 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200905 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000906 funcexe.fe_firstline = curwin->w_cursor.lnum;
907 funcexe.fe_lastline = curwin->w_cursor.lnum;
908 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000909
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000910 // The name might be "import.Func" or "Funcref". We don't know, we need to
911 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000912 // autoload script has errors. Guess that when there is a dot in the name
913 // showing errors is the right choice.
914 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000915 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000916 if (ignore_errors)
917 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000918 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000919 if (ignore_errors)
920 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000921 if (name == NULL)
922 name = func;
923
924 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
925
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000926 if (ret == FAIL)
927 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000928 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000929
930 return ret;
931}
932
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100933/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100934 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000935 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
936 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000937 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000938 */
939 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100940call_func_retstr(
941 char_u *func,
942 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200943 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000944{
945 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000946 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000947
Bram Moolenaarded27a12018-08-01 19:06:03 +0200948 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000949 return NULL;
950
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100951 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000952 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 return retval;
954}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000955
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000956/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100957 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000958 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000959 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100960 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000961 */
962 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963call_func_retlist(
964 char_u *func,
965 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200966 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000967{
968 typval_T rettv;
969
Bram Moolenaarded27a12018-08-01 19:06:03 +0200970 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000971 return NULL;
972
973 if (rettv.v_type != VAR_LIST)
974 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100975 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
976 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000977 clear_tv(&rettv);
978 return NULL;
979 }
980
981 return rettv.vval.v_list;
982}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaare70dd112022-01-21 16:31:11 +0000984#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100986 * Evaluate "arg", which is 'foldexpr'.
987 * Note: caller must set "curwin" to match "arg".
988 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
989 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 */
991 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000992eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000994 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000995 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200996 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000998 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000999 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001000 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001002 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001003 current_sctx = wp->w_p_script_ctx[WV_FDE];
1004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001006 if (use_sandbox)
1007 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001008 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001010
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001011 // Evaluate the expression. If the expression is "FuncName()" call the
1012 // function directly.
1013 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 retval = 0;
1015 else
1016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001017 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 if (tv.v_type == VAR_NUMBER)
1019 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001020 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 retval = 0;
1022 else
1023 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001024 // If the result is a string, check if there is a non-digit before
1025 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +02001027 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 *cp = *s++;
1029 retval = atol((char *)s);
1030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001034 if (use_sandbox)
1035 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001036 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001037 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001038 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001040 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041}
1042#endif
1043
Ernie Rael64885642023-10-04 20:16:22 +02001044#ifdef LOG_LOCKVAR
1045typedef struct flag_string_S
1046{
1047 int flag;
1048 char *str;
1049} flag_string_T;
1050
1051 static char *
1052flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
1053{
1054 char *p = buf;
1055 *p = NUL;
1056 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1057 {
1058 if ((fstring->flag & flags) != 0)
1059 {
1060 size_t len = STRLEN(fstring->str);
1061 if (n > p - buf + len + 7)
1062 {
1063 STRCAT(p, fstring->str);
1064 p += len;
1065 STRCAT(p, " ");
1066 ++p;
1067 }
1068 else
1069 {
1070 STRCAT(buf, "...");
1071 break;
1072 }
1073 }
1074 }
1075 return buf;
1076}
1077
1078flag_string_T glv_flag_strings[] = {
1079 { GLV_QUIET, "QUIET" },
1080 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1081 { GLV_READ_ONLY, "READ_ONLY" },
1082 { GLV_NO_DECL, "NO_DECL" },
1083 { GLV_COMPILING, "COMPILING" },
1084 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1085 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1086 { 0, NULL }
1087};
1088#endif
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090/*
Ernie Raelee865f32023-09-29 19:53:55 +02001091 * Fill in "lp" using "root". This is used in a special case when
1092 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1093 *
1094 * This is typically called with "lval_root" as "root". For a class, find
1095 * the name from lp in the class from root, fill in lval_T if found. For a
1096 * complex type, list/dict use it as the result; just put the root into
1097 * ll_tv.
1098 *
1099 * "lval_root" is a hack used during run-time/instr-execution to provide the
1100 * starting point for "get_lval()" to traverse a chain of indexes. In some
1101 * cases get_lval sees a bare name and uses this function to populate the
1102 * lval_T.
1103 *
1104 * For setting up "lval_root" (currently only used with lockvar)
1105 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1106 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1107 */
1108 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001109fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001110{
1111#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001112 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1113 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001114#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001115 if (lr->lr_tv == NULL)
1116 return;
Ernie Rael64885642023-10-04 20:16:22 +02001117 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001118 {
Ernie Rael64885642023-10-04 20:16:22 +02001119 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001120 {
1121 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001122 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001123 int m_idx;
1124 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1125 lp->ll_name_end - lp->ll_name, &m_idx);
1126 if (m != NULL)
1127 {
1128 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001129 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001130 lp->ll_oi = m_idx;
1131 lp->ll_valtype = m->ocm_type;
1132 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1133#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001134 ch_log(NULL, "LKVAR: ... class member %s.%s",
1135 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001136#endif
1137 return;
1138 }
1139 }
1140 }
1141
1142#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001143 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001144#endif
Ernie Rael64885642023-10-04 20:16:22 +02001145 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001146 lp->ll_is_root = TRUE;
1147}
1148
1149/*
Ernie Rael64885642023-10-04 20:16:22 +02001150 * Check if the class has permission to access the member.
1151 * Returns OK or FAIL.
1152 */
1153 static int
1154get_lval_check_access(
1155 class_T *cl_exec, // executing class, NULL if :def or script level
1156 class_T *cl, // class which contains the member
1157 ocmember_T *om, // member being accessed
1158 char_u *p, // char after member name
1159 int flags) // GLV flags to check if writing to lval
1160{
1161#ifdef LOG_LOCKVAR
1162 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1163 (void*)cl_exec, (void*)cl, *p);
1164#endif
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001165 if (cl_exec != NULL && cl_exec == cl)
1166 return OK;
1167
1168 char *msg = NULL;
1169 switch (om->ocm_access)
Ernie Rael64885642023-10-04 20:16:22 +02001170 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001171 case VIM_ACCESS_PRIVATE:
1172 msg = e_cannot_access_protected_variable_str;
1173 break;
1174 case VIM_ACCESS_READ:
1175 // If [idx] or .key following, read only OK.
1176 if (*p == '[' || *p == '.')
Ernie Raele6c9aa52023-10-06 19:55:52 +02001177 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001178 if ((flags & GLV_READ_ONLY) == 0)
1179 {
1180 if (IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001181 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001182 if (om->ocm_type->tt_type == VAR_OBJECT)
1183 semsg(_(e_enumvalue_str_cannot_be_modified),
1184 cl->class_name, om->ocm_name);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001185 else
1186 msg = e_variable_is_not_writable_str;
1187 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001188 else
1189 msg = e_variable_is_not_writable_str;
1190 }
1191 break;
1192 case VIM_ACCESS_ALL:
1193 break;
1194 }
1195 if (msg != NULL)
1196 {
1197 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1198 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02001199 }
1200 return OK;
1201}
1202
1203/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001204 * Get lval information for a variable imported from script "imp_sid". On
1205 * success, updates "lp" with the variable name, type, script ID and typval.
1206 * The variable name starts at or after "p".
1207 * If "rettv" is not NULL it points to the value to be assigned. This used to
1208 * match the rhs and lhs types.
1209 * Returns a pointer to the character after the variable name if the imported
1210 * variable is valid and writable.
1211 * Returns NULL if the variable is not exported or typval is not found or the
1212 * rhs type doesn't match the lhs type or the variable is not writable.
1213 */
1214 static char_u *
1215get_lval_imported(
1216 lval_T *lp,
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001217 scid_T imp_sid,
1218 char_u *p,
1219 dictitem_T **dip,
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001220 int fne_flags)
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001221{
1222 ufunc_T *ufunc;
1223 type_T *type = NULL;
1224 int cc;
1225 int rc = FAIL;
1226
1227 p = skipwhite(p);
1228
1229 import_check_sourced_sid(&imp_sid);
1230 lp->ll_sid = imp_sid;
1231 lp->ll_name = p;
1232 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1233 lp->ll_name_end = p;
1234
1235 // check the item is exported
1236 cc = *p;
1237 *p = NUL;
1238 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1239 TRUE) == -1)
1240 goto failed;
1241
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001242 // Get the typval for the exported item
1243 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1244 if (ht == NULL)
1245 goto failed;
1246
1247 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1248 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001249 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001250 goto success;
1251
1252 *dip = di;
1253
1254 // Check whether the variable is writable.
1255 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1256 if (sv != NULL && sv->sv_const != 0)
1257 {
1258 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1259 goto failed;
1260 }
1261
1262 // check whether variable is locked
1263 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1264 goto failed;
1265
1266 lp->ll_tv = &di->di_tv;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001267 lp->ll_valtype = type;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001268
1269success:
1270 rc = OK;
1271
1272failed:
1273 *p = cc;
1274 return rc == OK ? p : NULL;
1275}
1276
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001277typedef enum {
1278 GLV_FAIL,
1279 GLV_OK,
1280 GLV_STOP
1281} glv_status_T;
1282
1283/*
1284 * Get an Dict lval variable that can be assigned a value to: "name",
1285 * "name[expr]", "name[expr][expr]", "name.key", "name.key[expr]" etc.
1286 * "name" points to the start of the name.
1287 * If "rettv" is not NULL it points to the value to be assigned.
1288 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1289 * wrong; must end in space or cmd separator.
1290 *
1291 * flags:
1292 * GLV_QUIET: do not give error messages
1293 * GLV_READ_ONLY: will not change the variable
1294 * GLV_NO_AUTOLOAD: do not use script autoloading
1295 *
1296 * The Dict is returned in 'lp'. Returns GLV_OK on success and GLV_FAIL on
1297 * failure. Returns GLV_STOP to stop processing the characters following
1298 * 'key_end'.
1299 */
1300 static int
1301get_lval_dict_item(
1302 char_u *name,
1303 lval_T *lp,
1304 char_u *key,
1305 int len,
1306 char_u **key_end,
1307 typval_T *var1,
1308 int flags,
1309 int unlet,
1310 typval_T *rettv)
1311{
1312 int quiet = flags & GLV_QUIET;
1313 char_u *p = *key_end;
1314
1315 if (len == -1)
1316 {
1317 // "[key]": get key from "var1"
1318 key = tv_get_string_chk(var1); // is number or string
1319 if (key == NULL)
1320 {
1321 clear_tv(var1);
1322 return GLV_FAIL;
1323 }
1324 }
1325 lp->ll_list = NULL;
1326 lp->ll_object = NULL;
1327 lp->ll_class = NULL;
1328
1329 // a NULL dict is equivalent with an empty dict
1330 if (lp->ll_tv->vval.v_dict == NULL)
1331 {
1332 lp->ll_tv->vval.v_dict = dict_alloc();
1333 if (lp->ll_tv->vval.v_dict == NULL)
1334 {
1335 clear_tv(var1);
1336 return GLV_FAIL;
1337 }
1338 ++lp->ll_tv->vval.v_dict->dv_refcount;
1339 }
1340 lp->ll_dict = lp->ll_tv->vval.v_dict;
1341
1342 lp->ll_di = dict_find(lp->ll_dict, key, len);
1343
1344 // When assigning to a scope dictionary check that a function and
1345 // variable name is valid (only variable name unless it is l: or
1346 // g: dictionary). Disallow overwriting a builtin function.
1347 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1348 {
1349 int prevval;
1350
1351 if (len != -1)
1352 {
1353 prevval = key[len];
1354 key[len] = NUL;
1355 }
1356 else
1357 prevval = 0; // avoid compiler warning
1358 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1359 && (rettv->v_type == VAR_FUNC
1360 || rettv->v_type == VAR_PARTIAL)
1361 && var_wrong_func_name(key, lp->ll_di == NULL))
1362 || !valid_varname(key, -1, TRUE);
1363 if (len != -1)
1364 key[len] = prevval;
1365 if (wrong)
1366 {
1367 clear_tv(var1);
1368 return GLV_FAIL;
1369 }
1370 }
1371
1372 if (lp->ll_valtype != NULL)
1373 // use the type of the member
1374 lp->ll_valtype = lp->ll_valtype->tt_member;
1375
1376 if (lp->ll_di == NULL)
1377 {
1378 // Can't add "v:" or "a:" variable.
1379 if (lp->ll_dict == get_vimvar_dict()
1380 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1381 {
1382 semsg(_(e_illegal_variable_name_str), name);
1383 clear_tv(var1);
1384 return GLV_FAIL;
1385 }
1386
1387 // Key does not exist in dict: may need to add it.
1388 if (*p == '[' || *p == '.' || unlet)
1389 {
1390 if (!quiet)
1391 semsg(_(e_key_not_present_in_dictionary_str), key);
1392 clear_tv(var1);
1393 return GLV_FAIL;
1394 }
1395 if (len == -1)
1396 lp->ll_newkey = vim_strsave(key);
1397 else
1398 lp->ll_newkey = vim_strnsave(key, len);
1399 clear_tv(var1);
1400 if (lp->ll_newkey == NULL)
1401 p = NULL;
1402
1403 *key_end = p;
1404 return GLV_STOP;
1405 }
1406 // existing variable, need to check if it can be changed
1407 else if ((flags & GLV_READ_ONLY) == 0
1408 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1409 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
1410 {
1411 clear_tv(var1);
1412 return GLV_FAIL;
1413 }
1414
1415 clear_tv(var1);
1416 lp->ll_tv = &lp->ll_di->di_tv;
1417
1418 return GLV_OK;
1419}
1420
1421/*
1422 * Get an blob lval variable that can be assigned a value to: "name",
1423 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1424 *
1425 * 'var1' specifies the starting blob index and 'var2' specifies the ending
1426 * blob index. If the first index is not specified in a range, then 'empty1'
1427 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1428 * invalid indexes.
1429 *
1430 * The blob is returned in 'lp'. Returns OK on success and FAIL on failure.
1431 */
1432 static int
1433get_lval_blob(
1434 lval_T *lp,
1435 typval_T *var1,
1436 typval_T *var2,
1437 int empty1,
1438 int quiet)
1439{
1440 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1441
1442 // Get the number and item for the only or first index of the List.
1443 if (empty1)
1444 lp->ll_n1 = 0;
1445 else
1446 // is number or string
1447 lp->ll_n1 = (long)tv_get_number(var1);
1448 clear_tv(var1);
1449
1450 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
1451 {
1452 clear_tv(var2);
1453 return FAIL;
1454 }
1455 if (lp->ll_range && !lp->ll_empty2)
1456 {
1457 lp->ll_n2 = (long)tv_get_number(var2);
1458 clear_tv(var2);
1459 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL)
1460 return FAIL;
1461 }
1462
1463 if (!lp->ll_range)
1464 // Indexing a single byte in a blob. So the rhs type is a
1465 // number.
1466 lp->ll_valtype = &t_number;
1467
1468 lp->ll_blob = lp->ll_tv->vval.v_blob;
1469 lp->ll_tv = NULL;
1470
1471 return OK;
1472}
1473
1474/*
1475 * Get a List lval variable that can be assigned a value to: "name",
1476 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1477 *
1478 * 'var1' specifies the starting List index and 'var2' specifies the ending
1479 * List index. If the first index is not specified in a range, then 'empty1'
1480 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1481 * invalid indexes.
1482 *
1483 * The List is returned in 'lp'. Returns OK on success and FAIL on failure.
1484 */
1485 static int
1486get_lval_list(
1487 lval_T *lp,
1488 typval_T *var1,
1489 typval_T *var2,
1490 int empty1,
1491 int flags,
1492 int quiet)
1493{
1494 /*
1495 * Get the number and item for the only or first index of the List.
1496 */
1497 if (empty1)
1498 lp->ll_n1 = 0;
1499 else
1500 // is number or string
1501 lp->ll_n1 = (long)tv_get_number(var1);
1502 clear_tv(var1);
1503
1504 lp->ll_dict = NULL;
1505 lp->ll_object = NULL;
1506 lp->ll_class = NULL;
1507 lp->ll_list = lp->ll_tv->vval.v_list;
1508 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1509 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
1510 if (lp->ll_li == NULL)
1511 {
1512 clear_tv(var2);
1513 return FAIL;
1514 }
1515
1516 if (lp->ll_valtype != NULL && !lp->ll_range)
1517 // use the type of the member
1518 lp->ll_valtype = lp->ll_valtype->tt_member;
1519
1520 /*
1521 * May need to find the item or absolute index for the second
1522 * index of a range.
1523 * When no index given: "lp->ll_empty2" is TRUE.
1524 * Otherwise "lp->ll_n2" is set to the second index.
1525 */
1526 if (lp->ll_range && !lp->ll_empty2)
1527 {
1528 lp->ll_n2 = (long)tv_get_number(var2);
1529 // is number or string
1530 clear_tv(var2);
1531 if (check_range_index_two(lp->ll_list,
1532 &lp->ll_n1, lp->ll_li, &lp->ll_n2, quiet) == FAIL)
1533 return FAIL;
1534 }
1535
1536 lp->ll_tv = &lp->ll_li->li_tv;
1537
1538 return OK;
1539}
1540
1541/*
1542 * Get a Class or Object lval variable that can be assigned a value to:
1543 * "name", "name.key", "name.key[expr]" etc.
1544 *
1545 * 'cl_exec' is the class that is executing, or NULL. 'v_type' is VAR_CLASS or
1546 * VAR_OBJECT. 'key' points to the member variable name and 'key_end' points
1547 * to the character after 'key'. If 'quiet' is TRUE, then error messages
1548 * are not displayed for invalid indexes.
1549 *
1550 * The Class or Object is returned in 'lp'. Returns OK on success and FAIL on
1551 * failure.
1552 */
1553 static int
1554get_lval_class_or_obj(
1555 class_T *cl_exec,
1556 vartype_T v_type,
1557 lval_T *lp,
1558 char_u *key,
1559 char_u *key_end,
1560 int flags,
1561 int quiet)
1562{
1563 lp->ll_dict = NULL;
1564 lp->ll_list = NULL;
1565
1566 class_T *cl;
1567 if (v_type == VAR_OBJECT)
1568 {
1569 if (lp->ll_tv->vval.v_object == NULL)
1570 {
1571 if (!quiet)
1572 emsg(_(e_using_null_object));
1573 return FAIL;
1574 }
1575 cl = lp->ll_tv->vval.v_object->obj_class;
1576 lp->ll_object = lp->ll_tv->vval.v_object;
1577 }
1578 else
1579 {
1580 cl = lp->ll_tv->vval.v_class;
1581 lp->ll_object = NULL;
1582 }
1583 lp->ll_class = cl;
1584
1585 // TODO: what if class is NULL?
1586 if (cl != NULL)
1587 {
1588 lp->ll_valtype = NULL;
1589
1590 if (flags & GLV_PREFER_FUNC)
1591 {
1592 // First look for a function with this name.
1593 // round 1: class functions (skipped for an object)
1594 // round 2: object methods
1595 for (int round = v_type == VAR_OBJECT ? 2 : 1;
1596 round <= 2; ++round)
1597 {
1598 int m_idx;
1599 ufunc_T *fp;
1600
1601 fp = method_lookup(cl,
1602 round == 1 ? VAR_CLASS : VAR_OBJECT,
1603 key, key_end - key, &m_idx);
1604 lp->ll_oi = m_idx;
1605 if (fp != NULL)
1606 {
1607 lp->ll_ufunc = fp;
1608 lp->ll_valtype = fp->uf_func_type;
1609 break;
1610 }
1611 }
1612 }
1613
1614 if (lp->ll_valtype == NULL)
1615 {
1616 int m_idx;
1617 ocmember_T *om
1618 = member_lookup(cl, v_type, key, key_end - key, &m_idx);
1619 lp->ll_oi = m_idx;
1620 if (om != NULL)
1621 {
1622 if (get_lval_check_access(cl_exec, cl, om,
1623 key_end, flags) == FAIL)
1624 return FAIL;
1625
1626 // When lhs is used to modify the variable, check it is
1627 // not a read-only variable.
1628 if ((flags & GLV_READ_ONLY) == 0
1629 && (*key_end != '.' && *key_end != '[')
1630 && oc_var_check_ro(cl, om))
1631 return FAIL;
1632
1633 lp->ll_valtype = om->ocm_type;
1634
1635 if (v_type == VAR_OBJECT)
1636 lp->ll_tv = ((typval_T *)(
1637 lp->ll_tv->vval.v_object + 1)) + m_idx;
1638 else
1639 lp->ll_tv = &cl->class_members_tv[m_idx];
1640 }
1641 }
1642
1643 if (lp->ll_valtype == NULL)
1644 {
1645 member_not_found_msg(cl, v_type, key, key_end - key);
1646 return FAIL;
1647 }
1648 }
1649
1650 return OK;
1651}
1652
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001653/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001654 * Get an lval: variable, Dict item or List item that can be assigned a value
1655 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1656 * "name.key", "name.key[expr]" etc.
1657 * Indexing only works if "name" is an existing List or Dictionary.
1658 * "name" points to the start of the name.
1659 * If "rettv" is not NULL it points to the value to be assigned.
1660 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1661 * wrong; must end in space or cmd separator.
1662 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001663 * flags:
1664 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001665 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001666 * GLV_NO_AUTOLOAD: do not use script autoloading
1667 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001668 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001669 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001670 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001673get_lval(
1674 char_u *name,
1675 typval_T *rettv,
1676 lval_T *lp,
1677 int unlet,
1678 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001679 int flags, // GLV_ values
1680 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001683 char_u *expr_start, *expr_end;
1684 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001685 dictitem_T *v = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001686 typval_T var1;
1687 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001688 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001689 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001690 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001691 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001692 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001693 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001694 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001695 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696
Ernie Raelee865f32023-09-29 19:53:55 +02001697#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001698 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001699 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001700 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001701 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1702 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001703 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001704 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001705 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001706#endif
1707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001709 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001710
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001711 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001712 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001713 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001714 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001715 lp->ll_name_end = find_name_end(name, NULL, NULL,
1716 FNE_INCL_BR | fne_flags);
1717 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001718 }
1719
Bram Moolenaara749a422022-02-12 19:52:25 +00001720 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001721 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001722 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1723 {
1724 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1725 return NULL;
1726 }
1727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001728 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001729 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001731 if (expr_start != NULL)
1732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001733 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001734 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001735 && *p != '[' && *p != '.')
1736 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001737 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001738 return NULL;
1739 }
1740
1741 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1742 if (lp->ll_exp_name == NULL)
1743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001744 // Report an invalid expression in braces, unless the
1745 // expression evaluation has been cancelled due to an
1746 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001747 if (!aborting() && !quiet)
1748 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001749 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001750 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001751 return NULL;
1752 }
1753 }
1754 lp->ll_name = lp->ll_exp_name;
1755 }
1756 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001758 lp->ll_name = name;
1759
Bram Moolenaar4525a572022-02-13 11:57:33 +00001760 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001762 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001763 // However, "g:[key]" is indexing a dictionary.
1764 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001765 {
1766 --p;
1767 lp->ll_name_end = p;
1768 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001769 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001770 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001771 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001772
Bram Moolenaar9510d222022-09-11 15:14:05 +01001773 if (is_scoped_variable(name))
1774 {
1775 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1776 return NULL;
1777 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001778 if (VIM_ISWHITE(*p))
1779 {
1780 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1781 return NULL;
1782 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001783 if (tp == p + 1 && !quiet)
1784 {
1785 semsg(_(e_white_space_required_after_str_str), ":", p);
1786 return NULL;
1787 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001788 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001789 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001790 semsg(_(e_using_type_not_in_script_context_str), p);
1791 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001792 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001793 if (vim9script && (flags & GLV_NO_DECL) &&
1794 !(flags & GLV_FOR_LOOP))
1795 {
1796 // Using a type and not in a "var" declaration.
1797 semsg(_(e_trailing_characters_str), p);
1798 return NULL;
1799 }
1800
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001801
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001802 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001803 lp->ll_type = parse_type(&tp,
1804 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1805 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001806 if (lp->ll_type == NULL && !quiet)
1807 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001808 lp->ll_name_end = tp;
1809 }
Ernie Raelee865f32023-09-29 19:53:55 +02001810 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 }
1812 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001813 if (lp->ll_name == NULL)
1814 return p;
1815
Bram Moolenaar62aec932022-01-29 21:45:34 +00001816 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001817 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001818 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001819 if (import != NULL)
1820 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001821 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001822 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001823 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00001824 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001825 }
1826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827
Ernie Rael0f058d12023-10-14 11:25:04 +02001828 // Without [idx] or .key we are done.
1829 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001830 {
1831 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001832 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001833 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001834 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001835
Ernie Rael0f058d12023-10-14 11:25:04 +02001836 if (vim9script && lval_root != NULL)
1837 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001838 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001839 {
1840 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001841 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001842 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001843 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001844 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001845 {
1846 cc = *p;
1847 *p = NUL;
1848 // When we would write to the variable pass &ht and prevent autoload.
1849 writing = !(flags & GLV_READ_ONLY);
1850 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001851 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001852 if (v == NULL && !quiet)
1853 semsg(_(e_undefined_variable_str), lp->ll_name);
1854 *p = cc;
1855 if (v == NULL)
1856 return NULL;
1857 lp->ll_tv = &v->di_tv;
1858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859
Bram Moolenaar4525a572022-02-13 11:57:33 +00001860 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001861 {
1862 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001863 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001864 return NULL;
1865 }
1866
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001867 /*
1868 * Loop until no more [idx] or .key is following.
1869 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001870 var1.v_type = VAR_UNKNOWN;
1871 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001872 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001874 vartype_T v_type = lp->ll_tv->v_type;
1875
1876 if (*p == '.' && v_type != VAR_DICT
1877 && v_type != VAR_OBJECT
1878 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001879 {
1880 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001881 semsg(_(e_dot_not_allowed_after_str_str),
1882 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001883 return NULL;
1884 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001885 if (v_type != VAR_LIST
1886 && v_type != VAR_DICT
1887 && v_type != VAR_BLOB
1888 && v_type != VAR_OBJECT
1889 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001892 semsg(_(e_index_not_allowed_after_str_str),
1893 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001894 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001896
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001897 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001898 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001899 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001900 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001901 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001902 r = rettv_blob_alloc(lp->ll_tv);
1903 if (r == FAIL)
1904 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001909 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001912#ifdef LOG_LOCKVAR
1913 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1914 vartype_name(v_type));
1915#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001916
Bram Moolenaar4525a572022-02-13 11:57:33 +00001917 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001918 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001919 && lp->ll_tv == &v->di_tv
1920 && ht != NULL && ht == get_script_local_ht())
1921 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001922 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001923
1924 // Vim9 script local variable: get the type
1925 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001926 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001927 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001928#ifdef LOG_LOCKVAR
1929 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1930 vartype_name(lp->ll_valtype->tt_type));
1931#endif
1932 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001933 }
1934
Bram Moolenaar8c711452005-01-14 21:53:12 +00001935 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001936 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001937 {
1938 key = p + 1;
1939 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1940 ;
1941 if (len == 0)
1942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001943 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001944 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001945 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001946 }
1947 p = key + len;
1948 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001949 else
1950 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001951 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001952 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001953 if (*p == ':')
1954 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001955 else
1956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001957 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001958 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001960 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001962 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001963 clear_tv(&var1);
1964 return NULL;
1965 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001966 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001967 }
1968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001970 if (*p == ':')
1971 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001972 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001975 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001976 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001978 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001979 if (rettv != NULL
1980 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001981 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001982 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001983 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001986 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001987 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001988 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 }
1990 p = skipwhite(p + 1);
1991 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001993 else
1994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001995 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001996 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001997 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001999 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002002 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002003 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002004 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002005 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002006 clear_tv(&var2);
2007 return NULL;
2008 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002010 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002011 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002012 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002014
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002016 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00002018 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002019 clear_tv(&var1);
2020 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002022 }
2023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002024 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002025 ++p;
2026 }
Ernie Rael4c8da022023-10-11 21:35:11 +02002027#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02002028 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02002029 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
2030 empty1 ? ":" : (char*)tv_get_string(&var1));
2031 else
2032 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02002033#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00002034
Bram Moolenaard505d172022-12-18 21:42:55 +00002035 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002036 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002037 glv_status_T glv_status;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002038
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002039 glv_status = get_lval_dict_item(name, lp, key, len, &p, &var1,
2040 flags, unlet, rettv);
2041 if (glv_status == GLV_FAIL)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002042 return NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002043 if (glv_status == GLV_STOP)
2044 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002045 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002046 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002047 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002048 if (get_lval_blob(lp, &var1, &var2, empty1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002049 return NULL;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002050
Bram Moolenaar61be3762019-03-19 23:04:17 +01002051 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002052 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002053 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002054 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002055 if (get_lval_list(lp, &var1, &var2, empty1, flags, quiet) == FAIL)
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002056 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002057 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002058 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002059 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002060 if (get_lval_class_or_obj(cl_exec, v_type, lp, key, p, flags,
2061 quiet) == FAIL)
2062 return FAIL;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 }
2065
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002066 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
2067 {
2068 where_T where = WHERE_INIT;
2069
2070 // In a vim9 script, do type check and make sure the variable is
2071 // writable.
2072 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
2073 return NULL;
2074 }
2075
2076
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002077 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002079 return p;
2080}
2081
2082/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002086clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087{
2088 vim_free(lp->ll_exp_name);
2089 vim_free(lp->ll_newkey);
2090}
2091
2092/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002093 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002095 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2096 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002099set_var_lval(
2100 lval_T *lp,
2101 char_u *endp,
2102 typval_T *rettv,
2103 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002104 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
2105 char_u *op,
2106 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002107{
2108 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002109 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110
2111 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002113 cc = *endp;
2114 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01002115 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02002116 return;
2117
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002118 if (lp->ll_blob != NULL)
2119 {
2120 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002121
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002122 if (op != NULL && *op != '=')
2123 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002124 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002125 return;
2126 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002127 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02002128 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002129
2130 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2131 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002132 if (lp->ll_empty2)
2133 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2134
Bram Moolenaar68452172021-04-12 21:21:02 +02002135 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
2136 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002137 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002138 }
2139 else
2140 {
2141 val = (int)tv_get_number_chk(rettv, &error);
2142 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02002143 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002144 }
2145 }
2146 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002148 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002149
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002150 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2151 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002152 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002153 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002154 *endp = cc;
2155 return;
2156 }
2157
Bram Moolenaarff697e62019-02-12 22:28:33 +01002158 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002159 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002160 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002161 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01002162 {
Ernie Raele75fde62023-12-21 17:18:54 +01002163 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002164 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002165 clear_tv(&tv);
2166 return;
2167 }
2168
Bram Moolenaar79518e22017-02-17 16:31:35 +01002169 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002170 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2171 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002172 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002173 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002174 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002175 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002178 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002179 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002180 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2181 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002182 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002183 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002184 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002185 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002186 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002188 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002189 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002190 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002191 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 else if (lp->ll_range)
2193 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002194 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2195 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002196 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002197 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002198 return;
2199 }
2200
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002201 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2202 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 }
2204 else
2205 {
2206 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002207 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002209 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2210 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002211 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002212 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002213 return;
2214 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002215
2216 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002217 && check_typval_arg_type(lp->ll_valtype, rettv,
2218 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002219 return;
2220
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 if (lp->ll_newkey != NULL)
2222 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002223 if (op != NULL && *op != '=')
2224 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002225 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002226 return;
2227 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002228 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2229 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002230 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002232 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002233 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 if (di == NULL)
2235 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002236 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2237 {
2238 vim_free(di);
2239 return;
2240 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002242 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002243 else if (op != NULL && *op != '=')
2244 {
2245 tv_op(lp->ll_tv, rettv, op);
2246 return;
2247 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002250
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 /*
2252 * Assign the value to the variable or list item.
2253 */
2254 if (copy)
2255 copy_tv(rettv, lp->ll_tv);
2256 else
2257 {
2258 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002259 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 }
2262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263}
2264
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002265/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002266 * Handle "blob1 += blob2".
2267 * Returns OK or FAIL.
2268 */
2269 static int
2270tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
2271{
2272 if (*op != '+' || tv2->v_type != VAR_BLOB)
2273 return FAIL;
2274
2275 // Blob += Blob
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002276 if (tv2->vval.v_blob == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002277 return OK;
2278
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002279 if (tv1->vval.v_blob == NULL)
2280 {
2281 tv1->vval.v_blob = tv2->vval.v_blob;
2282 ++tv1->vval.v_blob->bv_refcount;
2283 return OK;
2284 }
2285
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002286 blob_T *b1 = tv1->vval.v_blob;
2287 blob_T *b2 = tv2->vval.v_blob;
2288 int len = blob_len(b2);
2289
2290 for (int i = 0; i < len; i++)
2291 ga_append(&b1->bv_ga, blob_get(b2, i));
2292
2293 return OK;
2294}
2295
2296/*
2297 * Handle "list1 += list2".
2298 * Returns OK or FAIL.
2299 */
2300 static int
2301tv_op_list(typval_T *tv1, typval_T *tv2, char_u *op)
2302{
2303 if (*op != '+' || tv2->v_type != VAR_LIST)
2304 return FAIL;
2305
2306 // List += List
2307 if (tv2->vval.v_list == NULL)
2308 return OK;
2309
2310 if (tv1->vval.v_list == NULL)
2311 {
2312 tv1->vval.v_list = tv2->vval.v_list;
2313 ++tv1->vval.v_list->lv_refcount;
2314 }
2315 else
2316 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2317
2318 return OK;
2319}
2320
2321/*
2322 * Handle number operations:
2323 * nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
2324 *
2325 * Returns OK or FAIL.
2326 */
2327 static int
2328tv_op_number(typval_T *tv1, typval_T *tv2, char_u *op)
2329{
2330 varnumber_T n;
2331 int failed = FALSE;
2332
2333 n = tv_get_number(tv1);
2334 if (tv2->v_type == VAR_FLOAT)
2335 {
2336 float_T f = n;
2337
2338 if (*op == '%')
2339 return FAIL;
2340 switch (*op)
2341 {
2342 case '+': f += tv2->vval.v_float; break;
2343 case '-': f -= tv2->vval.v_float; break;
2344 case '*': f *= tv2->vval.v_float; break;
2345 case '/': f /= tv2->vval.v_float; break;
2346 }
2347 clear_tv(tv1);
2348 tv1->v_type = VAR_FLOAT;
2349 tv1->vval.v_float = f;
2350 }
2351 else
2352 {
2353 switch (*op)
2354 {
2355 case '+': n += tv_get_number(tv2); break;
2356 case '-': n -= tv_get_number(tv2); break;
2357 case '*': n *= tv_get_number(tv2); break;
2358 case '/': n = num_divide(n, tv_get_number(tv2), &failed); break;
2359 case '%': n = num_modulus(n, tv_get_number(tv2), &failed); break;
2360 }
2361 clear_tv(tv1);
2362 tv1->v_type = VAR_NUMBER;
2363 tv1->vval.v_number = n;
2364 }
2365
2366 return failed ? FAIL : OK;
2367}
2368
2369/*
2370 * Handle "str1 .= str2"
2371 * Returns OK or FAIL.
2372 */
2373 static int
2374tv_op_string(typval_T *tv1, typval_T *tv2, char_u *op UNUSED)
2375{
2376 char_u numbuf[NUMBUFLEN];
2377 char_u *s;
2378
2379 if (tv2->v_type == VAR_FLOAT)
2380 return FAIL;
2381
2382 // str .= str
2383 s = tv_get_string(tv1);
2384 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
2385 clear_tv(tv1);
2386 tv1->v_type = VAR_STRING;
2387 tv1->vval.v_string = s;
2388
2389 return OK;
2390}
2391
2392/*
2393 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2394 * and "tv1 .= tv2"
2395 * Returns OK or FAIL.
2396 */
2397 static int
2398tv_op_nr_or_string(typval_T *tv1, typval_T *tv2, char_u *op)
2399{
2400 if (tv2->v_type == VAR_LIST)
2401 return FAIL;
2402
2403 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
2404 return tv_op_number(tv1, tv2, op);
2405
2406 return tv_op_string(tv1, tv2, op);
2407}
2408
2409/*
2410 * Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
2411 * Returns OK or FAIL.
2412 */
2413 static int
2414tv_op_float(typval_T *tv1, typval_T *tv2, char_u *op)
2415{
2416 float_T f;
2417
2418 if (*op == '%' || *op == '.'
2419 || (tv2->v_type != VAR_FLOAT
2420 && tv2->v_type != VAR_NUMBER
2421 && tv2->v_type != VAR_STRING))
2422 return FAIL;
2423
2424 if (tv2->v_type == VAR_FLOAT)
2425 f = tv2->vval.v_float;
2426 else
2427 f = tv_get_number(tv2);
2428 switch (*op)
2429 {
2430 case '+': tv1->vval.v_float += f; break;
2431 case '-': tv1->vval.v_float -= f; break;
2432 case '*': tv1->vval.v_float *= f; break;
2433 case '/': tv1->vval.v_float /= f; break;
2434 }
2435
2436 return OK;
2437}
2438
2439/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002440 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2441 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 * Returns OK or FAIL.
2443 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002444 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002445tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446{
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002447 // Can't do anything with a Funcref or Dict on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002448 // v:true and friends only work with "..=".
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002449 if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
2450 || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL)
2451 && *op != '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002453 semsg(_(e_wrong_variable_type_for_str_equal), op);
2454 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 }
2456
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002457 int retval = FAIL;
2458 switch (tv1->v_type)
2459 {
2460 case VAR_UNKNOWN:
2461 case VAR_ANY:
2462 case VAR_VOID:
2463 case VAR_DICT:
2464 case VAR_FUNC:
2465 case VAR_PARTIAL:
2466 case VAR_BOOL:
2467 case VAR_SPECIAL:
2468 case VAR_JOB:
2469 case VAR_CHANNEL:
2470 case VAR_INSTR:
2471 case VAR_OBJECT:
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002472 case VAR_CLASS:
2473 case VAR_TYPEALIAS:
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002474 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002475
2476 case VAR_BLOB:
2477 retval = tv_op_blob(tv1, tv2, op);
2478 break;
2479
2480 case VAR_LIST:
2481 retval = tv_op_list(tv1, tv2, op);
2482 break;
2483
2484 case VAR_NUMBER:
2485 case VAR_STRING:
2486 retval = tv_op_nr_or_string(tv1, tv2, op);
2487 break;
2488
2489 case VAR_FLOAT:
2490 retval = tv_op_float(tv1, tv2, op);
2491 break;
2492 }
2493
2494 if (retval != OK)
Ernie Raele75fde62023-12-21 17:18:54 +01002495 semsg(_(e_wrong_variable_type_for_str_equal), op);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002496
2497 return retval;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498}
2499
2500/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002501 * Evaluate the expression used in a ":for var in expr" command.
2502 * "arg" points to "var".
2503 * Set "*errp" to TRUE for an error, FALSE otherwise;
2504 * Return a pointer that holds the info. Null when there is an error.
2505 */
2506 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002507eval_for_line(
2508 char_u *arg,
2509 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002510 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002511 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002512{
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002514 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002515 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002516 typval_T tv;
2517 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002518 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002520 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002521
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002522 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523 if (fi == NULL)
2524 return NULL;
2525
Bram Moolenaar404557e2021-07-05 21:41:48 +02002526 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2527 &fi->fi_semicolon, FALSE);
2528 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002529 return fi;
2530
Bram Moolenaar404557e2021-07-05 21:41:48 +02002531 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002532 if (expr[0] != 'i' || expr[1] != 'n'
2533 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002534 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002535 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2536 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2537 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002538 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002539 return fi;
2540 }
2541
2542 if (skip)
2543 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002544 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2545 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002546 {
2547 *errp = FALSE;
2548 if (!skip)
2549 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002550 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002551 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002552 l = tv.vval.v_list;
2553 if (l == NULL)
2554 {
2555 // a null list is like an empty list: do nothing
2556 clear_tv(&tv);
2557 }
2558 else
2559 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002561 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002563 // No need to increment the refcount, it's already set for
2564 // the list being used in "tv".
2565 fi->fi_list = l;
2566 list_add_watch(l, &fi->fi_lw);
2567 fi->fi_lw.lw_item = l->lv_first;
2568 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002569 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002570 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002571 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002572 fi->fi_bi = 0;
2573 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002574 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002575 typval_T btv;
2576
2577 // Make a copy, so that the iteration still works when the
2578 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002580 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002581 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002582 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002583 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002584 else if (tv.v_type == VAR_STRING)
2585 {
2586 fi->fi_byte_idx = 0;
2587 fi->fi_string = tv.vval.v_string;
2588 tv.vval.v_string = NULL;
2589 if (fi->fi_string == NULL)
2590 fi->fi_string = vim_strsave((char_u *)"");
2591 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002592 else
2593 {
Sean Dewar80d73952021-08-04 19:25:54 +02002594 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002595 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002596 }
2597 }
2598 }
2599 if (skip)
2600 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002601 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002602
2603 return fi;
2604}
2605
2606/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002607 * Used when looping over a :for line, skip the "in expr" part.
2608 */
2609 void
2610skip_for_lines(void *fi_void, evalarg_T *evalarg)
2611{
2612 forinfo_T *fi = (forinfo_T *)fi_void;
2613 int i;
2614
2615 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002616 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002617}
2618
2619/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002620 * Use the first item in a ":for" list. Advance to the next.
2621 * Assign the values to the variable (list). "arg" points to the first one.
2622 * Return TRUE when a valid item was found, FALSE when at end of list or
2623 * something wrong.
2624 */
2625 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002626next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002627{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002628 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002629 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002630 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002631 ? (ASSIGN_FINAL
2632 // first round: error if variable exists
2633 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002634 | ASSIGN_NO_MEMBER_TYPE
2635 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002636 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002637 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002638 int skip_assign = in_vim9script() && arg[0] == '_'
2639 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002640
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002641 if (fi->fi_blob != NULL)
2642 {
2643 typval_T tv;
2644
2645 if (fi->fi_bi >= blob_len(fi->fi_blob))
2646 return FALSE;
2647 tv.v_type = VAR_NUMBER;
2648 tv.v_lock = VAR_FIXED;
2649 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2650 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002651 if (skip_assign)
2652 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002653 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002654 fi->fi_varcount, flag, NULL) == OK;
2655 }
2656
2657 if (fi->fi_string != NULL)
2658 {
2659 typval_T tv;
2660 int len;
2661
2662 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2663 if (len == 0)
2664 return FALSE;
2665 tv.v_type = VAR_STRING;
2666 tv.v_lock = VAR_FIXED;
2667 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2668 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002669 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002670 if (skip_assign)
2671 result = TRUE;
2672 else
2673 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002674 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002675 vim_free(tv.vval.v_string);
2676 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002677 }
2678
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002679 item = fi->fi_lw.lw_item;
2680 if (item == NULL)
2681 result = FALSE;
2682 else
2683 {
2684 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002685 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002686 if (skip_assign)
2687 result = TRUE;
2688 else
2689 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002690 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002691 }
2692 return result;
2693}
2694
2695/*
2696 * Free the structure used to store info used by ":for".
2697 */
2698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002699free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002700{
Bram Moolenaar33570922005-01-25 22:26:29 +00002701 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002702
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002703 if (fi == NULL)
2704 return;
2705 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002706 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002707 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002708 list_unref(fi->fi_list);
2709 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002710 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002711 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002712 else
2713 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002714 vim_free(fi);
2715}
2716
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002718set_context_for_expression(
2719 expand_T *xp,
2720 char_u *arg,
2721 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002723 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002725 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002727 if (cmdidx == CMD_let || cmdidx == CMD_var
2728 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002729 {
2730 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002731 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002733 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002734 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002735 {
2736 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002737 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002738 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002739 break;
2740 }
2741 return;
2742 }
2743 }
2744 else
2745 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2746 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 while ((xp->xp_pattern = vim_strpbrk(arg,
2748 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2749 {
2750 c = *xp->xp_pattern;
2751 if (c == '&')
2752 {
2753 c = xp->xp_pattern[1];
2754 if (c == '&')
2755 {
2756 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002757 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002762 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2763 xp->xp_pattern += 2;
2764
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767 else if (c == '$')
2768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002769 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 xp->xp_context = EXPAND_ENV_VARS;
2771 }
2772 else if (c == '=')
2773 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002774 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 xp->xp_context = EXPAND_EXPRESSION;
2776 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002777 else if (c == '#'
2778 && xp->xp_context == EXPAND_EXPRESSION)
2779 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002780 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002781 break;
2782 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002783 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 && xp->xp_context == EXPAND_FUNCTIONS
2785 && vim_strchr(xp->xp_pattern, '(') == NULL)
2786 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002787 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 break;
2789 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002790 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002792 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
2794 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2795 if (c == '\\' && xp->xp_pattern[1] != NUL)
2796 ++xp->xp_pattern;
2797 xp->xp_context = EXPAND_NOTHING;
2798 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002799 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002801 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2803 /* skip */ ;
2804 xp->xp_context = EXPAND_NOTHING;
2805 }
2806 else if (c == '|')
2807 {
2808 if (xp->xp_pattern[1] == '|')
2809 {
2810 ++xp->xp_pattern;
2811 xp->xp_context = EXPAND_EXPRESSION;
2812 }
2813 else
2814 xp->xp_context = EXPAND_COMMANDS;
2815 }
2816 else
2817 xp->xp_context = EXPAND_EXPRESSION;
2818 }
2819 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002820 // Doesn't look like something valid, expand as an expression
2821 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002822 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 arg = xp->xp_pattern;
2824 if (*arg != NUL)
2825 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2826 /* skip */ ;
2827 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002828
2829 // ":exe one two" completes "two"
2830 if ((cmdidx == CMD_execute
2831 || cmdidx == CMD_echo
2832 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002833 || cmdidx == CMD_echomsg
2834 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002835 && xp->xp_context == EXPAND_EXPRESSION)
2836 {
2837 for (;;)
2838 {
2839 char_u *n = skiptowhite(arg);
2840
2841 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2842 break;
2843 arg = skipwhite(n);
2844 }
2845 }
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 xp->xp_pattern = arg;
2848}
2849
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002851 * Return TRUE if "pat" matches "text".
2852 * Does not use 'cpo' and always uses 'magic'.
2853 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002854 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002855pattern_match(char_u *pat, char_u *text, int ic)
2856{
2857 int matches = FALSE;
2858 char_u *save_cpo;
2859 regmatch_T regmatch;
2860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002861 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002862 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002863 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002864 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2865 if (regmatch.regprog != NULL)
2866 {
2867 regmatch.rm_ic = ic;
2868 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2869 vim_regfree(regmatch.regprog);
2870 }
2871 p_cpo = save_cpo;
2872 return matches;
2873}
2874
2875/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002876 * Handle a name followed by "(". Both for just "name(arg)" and for
2877 * "expr->name(arg)".
2878 * Returns OK or FAIL.
2879 */
2880 static int
2881eval_func(
2882 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002883 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002884 char_u *name,
2885 int name_len,
2886 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002887 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002888 typval_T *basetv) // "expr" for "expr->name(arg)"
2889{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002890 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002891 char_u *s = name;
2892 int len = name_len;
2893 partial_T *partial;
2894 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002895 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002896 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002897
2898 if (!evaluate)
2899 check_vars(s, len);
2900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002901 // If "s" is the name of a variable of type VAR_FUNC
2902 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002903 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002904 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002906 // Need to make a copy, in case evaluating the arguments makes
2907 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002908 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002909 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002910 ret = FAIL;
2911 else
2912 {
2913 funcexe_T funcexe;
2914
2915 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002916 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002917 funcexe.fe_firstline = curwin->w_cursor.lnum;
2918 funcexe.fe_lastline = curwin->w_cursor.lnum;
2919 funcexe.fe_evaluate = evaluate;
2920 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002921 if (partial != NULL)
2922 {
2923 funcexe.fe_object = partial->pt_obj;
2924 if (funcexe.fe_object != NULL)
2925 ++funcexe.fe_object->obj_refcount;
2926 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002927 funcexe.fe_basetv = basetv;
2928 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002929 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002930 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002931 }
2932 vim_free(s);
2933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002934 // If evaluate is FALSE rettv->v_type was not set in
2935 // get_func_tv, but it's needed in handle_subscript() to parse
2936 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002937 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2938 {
2939 rettv->vval.v_string = NULL;
2940 rettv->v_type = VAR_FUNC;
2941 }
2942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002943 // Stop the expression evaluation when immediately
2944 // aborting on error, or when an interrupt occurred or
2945 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002946 if (evaluate && aborting())
2947 {
2948 if (ret == OK)
2949 clear_tv(rettv);
2950 ret = FAIL;
2951 }
2952 return ret;
2953}
2954
2955/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002956 * After a NL, skip over empty lines and comment-only lines.
2957 */
2958 static char_u *
2959newline_skip_comments(char_u *arg)
2960{
2961 char_u *p = arg + 1;
2962
2963 for (;;)
2964 {
2965 p = skipwhite(p);
2966
2967 if (*p == NUL)
2968 break;
2969 if (vim9_comment_start(p))
2970 {
2971 char_u *nl = vim_strchr(p, NL);
2972
2973 if (nl == NULL)
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02002974 break;
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002975 p = nl;
2976 }
2977 if (*p != NL)
2978 break;
2979 ++p; // skip another NL
2980 }
2981 return p;
2982}
2983
2984/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002985 * Get the next line source line without advancing. But do skip over comment
2986 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002987 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002988 */
2989 static char_u *
2990getline_peek_skip_comments(evalarg_T *evalarg)
2991{
2992 for (;;)
2993 {
2994 char_u *next = getline_peek(evalarg->eval_getline,
2995 evalarg->eval_cookie);
2996 char_u *p;
2997
2998 if (next == NULL)
2999 break;
3000 p = skipwhite(next);
3001 if (*p != NUL && !vim9_comment_start(p))
3002 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01003003 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00003004 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02003005 }
3006 return NULL;
3007}
3008
3009/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02003010 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
3011 * comment) and there is a next line, return the next line (skipping blanks)
3012 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003013 * Otherwise return the next non-white at or after "arg" and set "getnext" to
3014 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003015 * "arg" must point somewhere inside a line, not at the start.
3016 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003017 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003018eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
3019{
Bram Moolenaar9d489562020-07-30 20:08:50 +02003020 char_u *p = skipwhite(arg);
3021
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003022 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003023 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003024 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01003025 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
3026 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01003027 && (*p == NUL || *p == NL
3028 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003029 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02003030 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003031
Bram Moolenaare442d592022-05-05 12:20:28 +01003032 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003033 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01003034 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02003035 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003036 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02003037 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003038
Bram Moolenaar9d489562020-07-30 20:08:50 +02003039 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003040 {
Bram Moolenaar69082912022-09-22 21:35:19 +01003041 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003042 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003043 }
3044 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003045 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003046}
3047
3048/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003049 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003050 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003051 *
3052 * If "arg" is not NULL, then the caller should assign the return value to
3053 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003054 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003055 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01003056eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003057{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003058 garray_T *gap = &evalarg->eval_ga;
3059 char_u *line;
3060
Bram Moolenaar39be4982022-05-06 21:51:50 +01003061 if (arg != NULL)
3062 {
3063 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003064 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01003065 // Truncate before a trailing comment, so that concatenating the lines
3066 // won't turn the rest into a comment.
3067 if (*skipwhite(arg) == '#')
3068 *arg = NUL;
3069 }
Bram Moolenaare442d592022-05-05 12:20:28 +01003070
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003071 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02003072 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
3073 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003074 else
3075 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00003076 if (line == NULL)
3077 return NULL;
3078
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003079 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003080 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
3081 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003082 char_u *p = skipwhite(line);
3083
3084 // Going to concatenate the lines after parsing. For an empty or
3085 // comment line use an empty string.
3086 if (*p == NUL || vim9_comment_start(p))
3087 {
3088 vim_free(line);
3089 line = vim_strsave((char_u *)"");
3090 }
3091
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003092 ((char_u **)gap->ga_data)[gap->ga_len] = line;
3093 ++gap->ga_len;
3094 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003095 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003096 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01003097 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003098 evalarg->eval_tofree = line;
3099 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02003100
3101 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003102 // line. The caller assigns the return value to "arg".
3103 // If "arg" is NULL, then the return value is discarded. In that case,
3104 // "arg" still points to the previous line. So don't reset
3105 // "eval_using_cmdline".
3106 if (arg != NULL)
3107 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003108 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003109}
3110
Bram Moolenaare6b53242020-07-01 17:28:33 +02003111/*
3112 * Call eval_next_non_blank() and get the next line if needed.
3113 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02003114 char_u *
3115skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
3116{
3117 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00003118 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003119
Bram Moolenaar962d7212020-07-04 14:15:00 +02003120 if (evalarg == NULL)
3121 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003122 eval_next_non_blank(p, evalarg, &getnext);
3123 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003124 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003125 return p;
3126}
3127
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003128/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003129 * The "eval" functions have an "evalarg" argument: When NULL or
3130 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
3131 * parsed but not executed. The functions may return OK, but the rettv will be
3132 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 */
3134
3135/*
3136 * Handle zero level expression.
3137 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003138 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003139 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003140 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 * Return OK or FAIL.
3142 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003144eval0(
3145 char_u *arg,
3146 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003147 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003148 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003150 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
3151}
3152
3153/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003154 * If "arg" is a simple function call without arguments then call it and return
3155 * the result. Otherwise return NOTDONE.
3156 */
3157 int
3158may_call_simple_func(
3159 char_u *arg,
3160 typval_T *rettv)
3161{
3162 char_u *parens = (char_u *)strstr((char *)arg, "()");
3163 int r = NOTDONE;
3164
3165 // If the expression is "FuncName()" then we can skip a lot of overhead.
3166 if (parens != NULL && *skipwhite(parens + 2) == NUL)
3167 {
3168 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
3169
3170 if (to_name_end(p, TRUE) == parens)
3171 r = call_simple_func(arg, (int)(parens - arg), rettv);
3172 }
3173 return r;
3174}
3175
3176/*
3177 * Handle zero level expression with optimization for a simple function call.
3178 * Same arguments and return value as eval0().
3179 */
3180 int
3181eval0_simple_funccal(
3182 char_u *arg,
3183 typval_T *rettv,
3184 exarg_T *eap,
3185 evalarg_T *evalarg)
3186{
3187 int r = may_call_simple_func(arg, rettv);
3188
3189 if (r == NOTDONE)
3190 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
3191 return r;
3192}
3193
3194/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003195 * Like eval0() but when "retarg" is not NULL store the pointer to after the
3196 * expression and don't check what comes after the expression.
3197 */
3198 int
3199eval0_retarg(
3200 char_u *arg,
3201 typval_T *rettv,
3202 exarg_T *eap,
3203 evalarg_T *evalarg,
3204 char_u **retarg)
3205{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 int ret;
3207 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00003208 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003209 int did_emsg_before = did_emsg;
3210 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003211 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003212 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003215 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003216
Bram Moolenaar79481362022-06-27 20:15:10 +01003217 if (ret != FAIL)
3218 {
3219 expr_end = p;
3220 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003221
Bram Moolenaar79481362022-06-27 20:15:10 +01003222 // In Vim9 script a command block is not split at NL characters for
3223 // commands using an expression argument. Skip over a '#' comment to
3224 // check for a following NL. Require white space before the '#'.
3225 if (in_vim9script() && p > expr_end && retarg == NULL)
3226 while (*p == '#')
3227 {
3228 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003229
Bram Moolenaar79481362022-06-27 20:15:10 +01003230 if (nl == NULL)
3231 break;
3232 p = skipwhite(nl + 1);
3233 if (eap != NULL && *p != NUL)
3234 eap->nextcmd = p;
3235 check_for_end = FALSE;
3236 }
3237
3238 if (check_for_end)
3239 end_error = !ends_excmd2(arg, p);
3240 }
3241
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003242 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 {
3244 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003245 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 /*
3247 * Report the invalid expression unless the expression evaluation has
3248 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003249 * exception, or we already gave a more specific error.
3250 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003252 if (!aborting()
3253 && did_emsg == did_emsg_before
3254 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003255 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003256 {
3257 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003258 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003259 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003260 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003261 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003262
zeertzjqf2588b62023-05-05 17:22:35 +01003263 if (eap != NULL && p != NULL)
3264 {
3265 // Some of the expression may not have been consumed.
3266 // Only execute a next command if it cannot be a "||" operator.
3267 // The next command may be "catch".
3268 char_u *nextcmd = check_nextcmd(p);
3269 if (nextcmd != NULL && *nextcmd != '|')
3270 eap->nextcmd = nextcmd;
3271 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003274
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003275 if (retarg != NULL)
3276 *retarg = p;
3277 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003278 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 return ret;
3281}
3282
3283/*
3284 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003285 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003286 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 *
3288 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003289 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003291 * Note: "rettv.v_lock" is not set.
3292 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 * Return OK or FAIL.
3294 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003295 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003296eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003298 char_u *p;
3299 int getnext;
3300
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003301 CLEAR_POINTER(rettv);
3302
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 /*
3304 * Get the first variable.
3305 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003306 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 return FAIL;
3308
Bram Moolenaar793648f2020-06-26 21:28:25 +02003309 p = eval_next_non_blank(*arg, evalarg, &getnext);
3310 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003312 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003313 int result;
3314 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003315 evalarg_T *evalarg_used = evalarg;
3316 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003317 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003318 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003319 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003320
3321 if (evalarg == NULL)
3322 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003323 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003324 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003325 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003326 orig_flags = evalarg_used->eval_flags;
3327 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003328
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003329 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003330 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003331 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003332 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003333 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003334 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003335 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003336 clear_tv(rettv);
3337 return FAIL;
3338 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003339 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003340 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003341
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003343 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345 int error = FALSE;
3346
Bram Moolenaar13106602020-10-04 16:06:05 +02003347 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003348 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003349 else if (vim9script)
3350 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003351 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003353 if (error || !op_falsy || !result)
3354 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 if (error)
3356 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358
3359 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003360 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003362 if (op_falsy)
3363 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003364 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003365 {
mityu4ac198c2021-05-28 17:52:40 +02003366 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003367 clear_tv(rettv);
3368 return FAIL;
3369 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003370 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003371 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003372 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003373 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003374 {
3375 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003377 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003378 if (!op_falsy || !result)
3379 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003381 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003383 /*
3384 * Check for the ":".
3385 */
3386 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3387 if (*p != ':')
3388 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003389 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003390 if (evaluate && result)
3391 clear_tv(rettv);
3392 evalarg_used->eval_flags = orig_flags;
3393 return FAIL;
3394 }
3395 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003396 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003397 else
3398 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003399 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003400 {
3401 error_white_both(p, 1);
3402 clear_tv(rettv);
3403 evalarg_used->eval_flags = orig_flags;
3404 return FAIL;
3405 }
3406 *arg = p;
3407 }
3408
3409 /*
3410 * Get the third variable. Recursive!
3411 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003412 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003413 {
mityu4ac198c2021-05-28 17:52:40 +02003414 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003415 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003416 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003417 return FAIL;
3418 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003419 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3420 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003421 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003422 if (eval1(arg, &var2, evalarg_used) == FAIL)
3423 {
3424 if (evaluate && result)
3425 clear_tv(rettv);
3426 evalarg_used->eval_flags = orig_flags;
3427 return FAIL;
3428 }
3429 if (evaluate && !result)
3430 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003432
3433 if (evalarg == NULL)
3434 clear_evalarg(&local_evalarg, NULL);
3435 else
3436 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438
3439 return OK;
3440}
3441
3442/*
3443 * Handle first level expression:
3444 * expr2 || expr2 || expr2 logical OR
3445 *
3446 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003447 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 *
3449 * Return OK or FAIL.
3450 */
3451 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003452eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003454 char_u *p;
3455 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003458 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003460 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 return FAIL;
3462
3463 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003464 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003466 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003467 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003469 evalarg_T *evalarg_used = evalarg;
3470 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003471 int evaluate;
3472 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003473 long result = FALSE;
3474 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003475 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003476 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003477
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003478 if (evalarg == NULL)
3479 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003480 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003481 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003482 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003483 orig_flags = evalarg_used->eval_flags;
3484 evaluate = orig_flags & EVAL_EVALUATE;
3485 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003486 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003487 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003488 result = tv_get_bool_chk(rettv, &error);
3489 else if (tv_get_number_chk(rettv, &error) != 0)
3490 result = TRUE;
3491 clear_tv(rettv);
3492 if (error)
3493 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495
3496 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003497 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003499 while (p[0] == '|' && p[1] == '|')
3500 {
3501 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003502 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003503 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003504 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003505 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003506 {
3507 error_white_both(p, 2);
3508 clear_tv(rettv);
3509 return FAIL;
3510 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003511 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003512 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003513
3514 /*
3515 * Get the second variable.
3516 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003517 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003518 {
mityu4ac198c2021-05-28 17:52:40 +02003519 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003520 clear_tv(rettv);
3521 return FAIL;
3522 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003523 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3524 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003525 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003526 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003527 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003528
3529 /*
3530 * Compute the result.
3531 */
3532 if (evaluate && !result)
3533 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003534 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003535 result = tv_get_bool_chk(&var2, &error);
3536 else if (tv_get_number_chk(&var2, &error) != 0)
3537 result = TRUE;
3538 clear_tv(&var2);
3539 if (error)
3540 return FAIL;
3541 }
3542 if (evaluate)
3543 {
3544 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003545 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003546 rettv->v_type = VAR_BOOL;
3547 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003548 }
3549 else
3550 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003551 rettv->v_type = VAR_NUMBER;
3552 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003553 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003554 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003555
3556 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003558
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003559 if (evalarg == NULL)
3560 clear_evalarg(&local_evalarg, NULL);
3561 else
3562 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564
3565 return OK;
3566}
3567
3568/*
3569 * Handle second level expression:
3570 * expr3 && expr3 && expr3 logical AND
3571 *
3572 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003573 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 *
3575 * Return OK or FAIL.
3576 */
3577 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003578eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003580 char_u *p;
3581 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003584 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003586 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 return FAIL;
3588
3589 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003590 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003592 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003593 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003595 evalarg_T *evalarg_used = evalarg;
3596 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003597 int orig_flags;
3598 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003599 long result = TRUE;
3600 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003601 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003602 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003603
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003604 if (evalarg == NULL)
3605 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003606 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003607 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003608 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003609 orig_flags = evalarg_used->eval_flags;
3610 evaluate = orig_flags & EVAL_EVALUATE;
3611 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003612 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003613 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003614 result = tv_get_bool_chk(rettv, &error);
3615 else if (tv_get_number_chk(rettv, &error) == 0)
3616 result = FALSE;
3617 clear_tv(rettv);
3618 if (error)
3619 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
3621
3622 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003623 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003625 while (p[0] == '&' && p[1] == '&')
3626 {
3627 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003628 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003629 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003630 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003631 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003632 {
3633 error_white_both(p, 2);
3634 clear_tv(rettv);
3635 return FAIL;
3636 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003637 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003638 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003639
3640 /*
3641 * Get the second variable.
3642 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003643 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003644 {
mityu4ac198c2021-05-28 17:52:40 +02003645 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003646 clear_tv(rettv);
3647 return FAIL;
3648 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003649 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3650 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003651 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003652 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003653 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003654 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003655
3656 /*
3657 * Compute the result.
3658 */
3659 if (evaluate && result)
3660 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003661 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003662 result = tv_get_bool_chk(&var2, &error);
3663 else if (tv_get_number_chk(&var2, &error) == 0)
3664 result = FALSE;
3665 clear_tv(&var2);
3666 if (error)
3667 return FAIL;
3668 }
3669 if (evaluate)
3670 {
3671 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003672 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003673 rettv->v_type = VAR_BOOL;
3674 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003675 }
3676 else
3677 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003678 rettv->v_type = VAR_NUMBER;
3679 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003680 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003681 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003682
3683 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003685
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003686 if (evalarg == NULL)
3687 clear_evalarg(&local_evalarg, NULL);
3688 else
3689 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691
3692 return OK;
3693}
3694
3695/*
3696 * Handle third level expression:
3697 * var1 == var2
3698 * var1 =~ var2
3699 * var1 != var2
3700 * var1 !~ var2
3701 * var1 > var2
3702 * var1 >= var2
3703 * var1 < var2
3704 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003705 * var1 is var2
3706 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 *
3708 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003709 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 *
3711 * Return OK or FAIL.
3712 */
3713 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003714eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003717 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003718 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003720 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003723 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003725 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 return FAIL;
3727
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003728 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003729
Bram Moolenaar696ba232020-07-29 21:20:41 +02003730 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
3732 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003733 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003735 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003737 typval_T var2;
3738 int ic;
3739 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003740 int evaluate = evalarg == NULL
3741 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003742 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003743
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003744 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003745 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003746 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003747 p = *arg;
3748 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003749 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3750 {
mityu4ac198c2021-05-28 17:52:40 +02003751 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003752 clear_tv(rettv);
3753 return FAIL;
3754 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003755
Bram Moolenaar696ba232020-07-29 21:20:41 +02003756 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3757 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003758 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003759 clear_tv(rettv);
3760 return FAIL;
3761 }
3762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003763 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (p[len] == '?')
3765 {
3766 ic = TRUE;
3767 ++len;
3768 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003769 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 else if (p[len] == '#')
3771 {
3772 ic = FALSE;
3773 ++len;
3774 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003775 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003777 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
3779 /*
3780 * Get the second variable.
3781 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003782 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3783 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003784 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003785 clear_tv(rettv);
3786 return FAIL;
3787 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003788 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003789 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003791 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 return FAIL;
3793 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003794 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003795 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003796 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003797
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003798 // use the line of the comparison for messages
3799 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003800 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003801 {
3802 ret = FAIL;
3803 clear_tv(rettv);
3804 }
3805 else
3806 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003807 clear_tv(&var2);
3808 return ret;
3809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811
3812 return OK;
3813}
3814
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003815/*
3816 * Make a copy of blob "tv1" and append blob "tv2".
3817 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 void
3819eval_addblob(typval_T *tv1, typval_T *tv2)
3820{
3821 blob_T *b1 = tv1->vval.v_blob;
3822 blob_T *b2 = tv2->vval.v_blob;
3823 blob_T *b = blob_alloc();
3824 int i;
3825
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003826 if (b == NULL)
3827 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003829 for (i = 0; i < blob_len(b1); i++)
3830 ga_append(&b->bv_ga, blob_get(b1, i));
3831 for (i = 0; i < blob_len(b2); i++)
3832 ga_append(&b->bv_ga, blob_get(b2, i));
3833
3834 clear_tv(tv1);
3835 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836}
3837
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003838/*
3839 * Make a copy of list "tv1" and append list "tv2".
3840 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 int
3842eval_addlist(typval_T *tv1, typval_T *tv2)
3843{
3844 typval_T var3;
3845
3846 // concatenate Lists
3847 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3848 {
3849 clear_tv(tv1);
3850 clear_tv(tv2);
3851 return FAIL;
3852 }
3853 clear_tv(tv1);
3854 *tv1 = var3;
3855 return OK;
3856}
3857
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003859 * Handle the bitwise left/right shift operator expression:
3860 * var1 << var2
3861 * var1 >> var2
3862 *
3863 * "arg" must point to the first non-white of the expression.
3864 * "arg" is advanced to just after the recognized expression.
3865 *
3866 * Return OK or FAIL.
3867 */
3868 static int
3869eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3870{
3871 /*
3872 * Get the first expression.
3873 */
3874 if (eval6(arg, rettv, evalarg) == FAIL)
3875 return FAIL;
3876
3877 /*
3878 * Repeat computing, until no '<<' or '>>' is following.
3879 */
3880 for (;;)
3881 {
3882 char_u *p;
3883 int getnext;
3884 exprtype_T type;
3885 int evaluate;
3886 typval_T var2;
3887 int vim9script;
3888
3889 p = eval_next_non_blank(*arg, evalarg, &getnext);
3890 if (p[0] == '<' && p[1] == '<')
3891 type = EXPR_LSHIFT;
3892 else if (p[0] == '>' && p[1] == '>')
3893 type = EXPR_RSHIFT;
3894 else
3895 return OK;
3896
3897 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003898 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3899 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003900 {
3901 // left operand should be a number
3902 emsg(_(e_bitshift_ops_must_be_number));
3903 clear_tv(rettv);
3904 return FAIL;
3905 }
3906
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003907 vim9script = in_vim9script();
3908 if (getnext)
3909 {
3910 *arg = eval_next_line(*arg, evalarg);
3911 p = *arg;
3912 }
3913 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3914 {
3915 error_white_both(*arg, 2);
3916 clear_tv(rettv);
3917 return FAIL;
3918 }
3919
3920 /*
3921 * Get the second variable.
3922 */
3923 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3924 {
3925 error_white_both(p, 2);
3926 clear_tv(rettv);
3927 return FAIL;
3928 }
3929 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3930 if (eval6(arg, &var2, evalarg) == FAIL)
3931 {
3932 clear_tv(rettv);
3933 return FAIL;
3934 }
3935
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003936 if (evaluate)
3937 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003938 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3939 {
3940 // right operand should be a positive number
3941 if (var2.v_type != VAR_NUMBER)
3942 emsg(_(e_bitshift_ops_must_be_number));
3943 else
3944 emsg(_(e_bitshift_ops_must_be_positive));
3945 clear_tv(rettv);
3946 clear_tv(&var2);
3947 return FAIL;
3948 }
3949
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003950 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3951 // shifting more bits than we have always results in zero
3952 rettv->vval.v_number = 0;
3953 else if (type == EXPR_LSHIFT)
3954 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003955 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003956 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003957 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003958 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003959 }
3960
3961 clear_tv(&var2);
3962 }
3963
3964 return OK;
3965}
3966
3967/*
3968 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003969 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003971 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003972 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 *
3974 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003975 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 *
3977 * Return OK or FAIL.
3978 */
3979 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003980eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003983 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003985 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 return FAIL;
3987
3988 /*
3989 * Repeat computing, until no '+', '-' or '.' is following.
3990 */
3991 for (;;)
3992 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003993 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003994 int getnext;
3995 char_u *p;
3996 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003997 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003998 int concat;
3999 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02004000 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004001
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004002 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004003 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004004 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004005 p = eval_next_non_blank(*arg, evalarg, &getnext);
4006 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02004007 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004008 if ((op != '+' && op != '-' && !concat) || p[1] == '='
4009 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004011 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
4012 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004013
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004014 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004015 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004016 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004017 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004018 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004019 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02004020 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004021 {
mityu4ac198c2021-05-28 17:52:40 +02004022 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004023 clear_tv(rettv);
4024 return FAIL;
4025 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004026 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004027 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004028 if ((op != '+' || (rettv->v_type != VAR_LIST
4029 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004030 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004031 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004032 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004033 int error = FALSE;
4034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004035 // For "list + ...", an illegal use of the first operand as
4036 // a number cannot be determined before evaluating the 2nd
4037 // operand: if this is also a list, all is ok.
4038 // For "something . ...", "something - ..." or "non-list + ...",
4039 // we know that the first operand needs to be a string or number
4040 // without evaluating the 2nd operand. So check before to avoid
4041 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004042 if (op != '.')
4043 tv_get_number_chk(rettv, &error);
4044 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004045 {
4046 clear_tv(rettv);
4047 return FAIL;
4048 }
4049 }
4050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /*
4052 * Get the second variable.
4053 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02004054 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004055 {
mityu89dcb4d2021-05-28 13:50:17 +02004056 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004057 clear_tv(rettv);
4058 return FAIL;
4059 }
4060 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004061 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004063 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return FAIL;
4065 }
4066
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004067 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
4069 /*
4070 * Compute the result.
4071 */
4072 if (op == '.')
4073 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004074 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4075 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004076 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004077
Bram Moolenaar2e086612020-08-25 22:37:48 +02004078 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004079 || var2.v_type == VAR_CHANNEL
4080 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02004081 semsg(_(e_using_invalid_value_as_string_str),
4082 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02004083 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004084 {
4085 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
4086 var2.vval.v_float);
4087 s2 = buf2;
4088 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004089 else
4090 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004091 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092 {
4093 clear_tv(rettv);
4094 clear_tv(&var2);
4095 return FAIL;
4096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004097 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 clear_tv(rettv);
4099 rettv->v_type = VAR_STRING;
4100 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004102 else if (op == '+' && rettv->v_type == VAR_BLOB
4103 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004105 else if (op == '+' && rettv->v_type == VAR_LIST
4106 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004107 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004109 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 else
4112 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004113 int error = FALSE;
4114 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004115 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004116
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004117 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004118 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004119 f1 = rettv->vval.v_float;
4120 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004121 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004122 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004124 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004125 if (error)
4126 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02004127 // This can only happen for "list + non-list" or
4128 // "blob + non-blob". For "non-list + ..." or
4129 // "something - ...", we returned before evaluating the
4130 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004131 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02004132 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004133 return FAIL;
4134 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004135 if (var2.v_type == VAR_FLOAT)
4136 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004137 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004138 if (var2.v_type == VAR_FLOAT)
4139 {
4140 f2 = var2.vval.v_float;
4141 n2 = 0;
4142 }
4143 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004144 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004145 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004146 if (error)
4147 {
4148 clear_tv(rettv);
4149 clear_tv(&var2);
4150 return FAIL;
4151 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004152 if (rettv->v_type == VAR_FLOAT)
4153 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004157 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004158 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4159 {
4160 if (op == '+')
4161 f1 = f1 + f2;
4162 else
4163 f1 = f1 - f2;
4164 rettv->v_type = VAR_FLOAT;
4165 rettv->vval.v_float = f1;
4166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004168 {
4169 if (op == '+')
4170 n1 = n1 + n2;
4171 else
4172 n1 = n1 - n2;
4173 rettv->v_type = VAR_NUMBER;
4174 rettv->vval.v_number = n1;
4175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 }
4180 return OK;
4181}
4182
4183/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004184 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 * * number multiplication
4186 * / number division
4187 * % number modulo
4188 *
4189 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004190 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 *
4192 * Return OK or FAIL.
4193 */
4194 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004195eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004196 char_u **arg,
4197 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004198 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004199 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004201 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
4203 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004204 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004206 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 return FAIL;
4208
4209 /*
4210 * Repeat computing, until no '*', '/' or '%' is following.
4211 */
4212 for (;;)
4213 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004214 int evaluate;
4215 int getnext;
4216 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02004217 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004218 int op;
4219 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004220 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004221 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004222
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004223 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02004224 p = eval_next_non_blank(*arg, evalarg, &getnext);
4225 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004226 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004228
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004229 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004230 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004231 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004232 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004233 {
4234 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
4235 {
mityu4ac198c2021-05-28 17:52:40 +02004236 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004237 clear_tv(rettv);
4238 return FAIL;
4239 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004240 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004243 f1 = 0;
4244 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004245 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004246 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004248 if (rettv->v_type == VAR_FLOAT)
4249 {
4250 f1 = rettv->vval.v_float;
4251 use_float = TRUE;
4252 n1 = 0;
4253 }
4254 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004255 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 if (error)
4258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 else
4261 n1 = 0;
4262
4263 /*
4264 * Get the second variable.
4265 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004266 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4267 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004268 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004269 clear_tv(rettv);
4270 return FAIL;
4271 }
4272 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004273 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 return FAIL;
4275
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004276 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004278 if (var2.v_type == VAR_FLOAT)
4279 {
4280 if (!use_float)
4281 {
4282 f1 = n1;
4283 use_float = TRUE;
4284 }
4285 f2 = var2.vval.v_float;
4286 n2 = 0;
4287 }
4288 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004289 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004290 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004291 clear_tv(&var2);
4292 if (error)
4293 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004294 if (use_float)
4295 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
4298 /*
4299 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004300 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004302 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004304 if (op == '*')
4305 f1 = f1 * f2;
4306 else if (op == '/')
4307 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004308#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004309 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004310 if (f2 == 0.0)
4311 {
4312 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004313 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004314 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004315 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004316 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004317 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004318 }
4319 else
4320 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004321#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004322 // We rely on the floating point library to handle divide
4323 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004324 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004325#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004328 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004329 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004330 return FAIL;
4331 }
4332 rettv->v_type = VAR_FLOAT;
4333 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 else
4336 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004337 int failed = FALSE;
4338
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004339 if (op == '*')
4340 n1 = n1 * n2;
4341 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004342 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004344 n1 = num_modulus(n1, n2, &failed);
4345 if (failed)
4346 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004347
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004348 rettv->v_type = VAR_NUMBER;
4349 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 }
4353
4354 return OK;
4355}
4356
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004357/*
4358 * Handle a type cast before a base level expression.
4359 * "arg" must point to the first non-white of the expression.
4360 * "arg" is advanced to just after the recognized expression.
4361 * Return OK or FAIL.
4362 */
4363 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004364eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004365 char_u **arg,
4366 typval_T *rettv,
4367 evalarg_T *evalarg,
4368 int want_string) // after "." operator
4369{
4370 type_T *want_type = NULL;
4371 garray_T type_list; // list of pointers to allocated types
4372 int res;
4373 int evaluate = evalarg == NULL ? 0
4374 : (evalarg->eval_flags & EVAL_EVALUATE);
4375
4376 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004377 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4378 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004379 {
4380 ++*arg;
4381 ga_init2(&type_list, sizeof(type_T *), 10);
4382 want_type = parse_type(arg, &type_list, TRUE);
4383 if (want_type == NULL && (evaluate || **arg != '>'))
4384 {
4385 clear_type_list(&type_list);
4386 return FAIL;
4387 }
4388
4389 if (**arg != '>')
4390 {
4391 if (*skipwhite(*arg) == '>')
4392 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4393 else
4394 emsg(_(e_missing_gt));
4395 clear_type_list(&type_list);
4396 return FAIL;
4397 }
4398 ++*arg;
4399 *arg = skipwhite_and_linebreak(*arg, evalarg);
4400 }
4401
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004402 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004403
4404 if (want_type != NULL && evaluate)
4405 {
4406 if (res == OK)
4407 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004408 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4409 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004410
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004411 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004412 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004413 if (want_type->tt_type == VAR_BOOL
4414 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004415 && (actual->tt_flags & TTFLAG_BOOL_OK))
4416 {
4417 int n = tv2bool(rettv);
4418
4419 // can use "0" and "1" for boolean in some places
4420 clear_tv(rettv);
4421 rettv->v_type = VAR_BOOL;
4422 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4423 }
4424 else
4425 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004426 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004427
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004428 res = check_type(want_type, actual, TRUE, where);
4429 }
4430 }
4431 }
4432 clear_type_list(&type_list);
4433 }
4434
4435 return res;
4436}
4437
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004438 int
4439eval_leader(char_u **arg, int vim9)
4440{
4441 char_u *s = *arg;
4442 char_u *p = *arg;
4443
4444 while (*p == '!' || *p == '-' || *p == '+')
4445 {
4446 char_u *n = skipwhite(p + 1);
4447
4448 // ++, --, -+ and +- are not accepted in Vim9 script
4449 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4450 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004451 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004452 return FAIL;
4453 }
4454 p = n;
4455 }
4456 *arg = p;
4457 return OK;
4458}
4459
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004461 * Check for a predefined value "true", "false" and "null.*".
4462 * Return OK when recognized.
4463 */
4464 int
4465handle_predefined(char_u *s, int len, typval_T *rettv)
4466{
4467 switch (len)
4468 {
4469 case 4: if (STRNCMP(s, "true", 4) == 0)
4470 {
4471 rettv->v_type = VAR_BOOL;
4472 rettv->vval.v_number = VVAL_TRUE;
4473 return OK;
4474 }
4475 if (STRNCMP(s, "null", 4) == 0)
4476 {
4477 rettv->v_type = VAR_SPECIAL;
4478 rettv->vval.v_number = VVAL_NULL;
4479 return OK;
4480 }
4481 break;
4482 case 5: if (STRNCMP(s, "false", 5) == 0)
4483 {
4484 rettv->v_type = VAR_BOOL;
4485 rettv->vval.v_number = VVAL_FALSE;
4486 return OK;
4487 }
4488 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004489 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4490 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004491#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004492 rettv->v_type = VAR_JOB;
4493 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004494#else
4495 rettv->v_type = VAR_SPECIAL;
4496 rettv->vval.v_number = VVAL_NULL;
4497#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004498 return OK;
4499 }
4500 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004501 case 9:
4502 if (STRNCMP(s, "null_", 5) != 0)
4503 break;
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004504 // null_list
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004505 if (STRNCMP(s + 5, "list", 4) == 0)
4506 {
4507 rettv->v_type = VAR_LIST;
4508 rettv->vval.v_list = NULL;
4509 return OK;
4510 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004511 // null_dict
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004512 if (STRNCMP(s + 5, "dict", 4) == 0)
4513 {
4514 rettv->v_type = VAR_DICT;
4515 rettv->vval.v_dict = NULL;
4516 return OK;
4517 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004518 // null_blob
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004519 if (STRNCMP(s + 5, "blob", 4) == 0)
4520 {
4521 rettv->v_type = VAR_BLOB;
4522 rettv->vval.v_blob = NULL;
4523 return OK;
4524 }
4525 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004526 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4527 {
4528 rettv->v_type = VAR_CLASS;
4529 rettv->vval.v_class = NULL;
4530 return OK;
4531 }
4532 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004533 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4534 {
4535 rettv->v_type = VAR_STRING;
4536 rettv->vval.v_string = NULL;
4537 return OK;
4538 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004539 if (STRNCMP(s, "null_object", 11) == 0)
4540 {
4541 rettv->v_type = VAR_OBJECT;
4542 rettv->vval.v_object = NULL;
4543 return OK;
4544 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004545 break;
4546 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004547 if (STRNCMP(s, "null_channel", 12) == 0)
4548 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004549#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004550 rettv->v_type = VAR_CHANNEL;
4551 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004552#else
4553 rettv->v_type = VAR_SPECIAL;
4554 rettv->vval.v_number = VVAL_NULL;
4555#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004556 return OK;
4557 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004558 if (STRNCMP(s, "null_partial", 12) == 0)
4559 {
4560 rettv->v_type = VAR_PARTIAL;
4561 rettv->vval.v_partial = NULL;
4562 return OK;
4563 }
4564 break;
4565 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4566 {
4567 rettv->v_type = VAR_FUNC;
4568 rettv->vval.v_string = NULL;
4569 return OK;
4570 }
4571 break;
4572 }
4573 return FAIL;
4574}
4575
4576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 * Handle sixth level expression:
4578 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004579 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004580 * "string" string constant
4581 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 * &option-name option value
4583 * @r register contents
4584 * identifier variable value
4585 * function() function call
4586 * $VAR environment variable
4587 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004588 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004590 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004591 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 *
4593 * Also handle:
4594 * ! in front logical NOT
4595 * - in front unary minus
4596 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004597 * trailing [] subscript in String or List
4598 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004599 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 *
4601 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004602 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 *
4604 * Return OK or FAIL.
4605 */
4606 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004607eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004608 char_u **arg,
4609 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004610 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004613 int evaluate = evalarg != NULL
4614 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 int len;
4616 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004617 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 char_u *start_leader, *end_leader;
4619 int ret = OK;
4620 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004621 static int recurse = 0;
4622 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
4624 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629
4630 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004631 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 */
4633 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004634 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 end_leader = *arg;
4637
Keith Thompson184f71c2024-01-04 21:19:04 +01004638 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004639 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004640 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004641 ++*arg;
4642 return FAIL;
4643 }
4644
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004645 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004646 // and crash. With MSVC the stack is smaller.
4647 if (recurse ==
4648#ifdef _MSC_VER
4649 300
4650#else
4651 1000
4652#endif
4653 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004654 {
4655 semsg(_(e_expression_too_recursive_str), *arg);
4656 return FAIL;
4657 }
4658 ++recurse;
4659
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 switch (**arg)
4661 {
4662 /*
4663 * Number constant.
4664 */
4665 case '0':
4666 case '1':
4667 case '2':
4668 case '3':
4669 case '4':
4670 case '5':
4671 case '6':
4672 case '7':
4673 case '8':
4674 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004675 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004676
4677 // Apply prefixed "-" and "+" now. Matters especially when
4678 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004679 if (ret == OK && evaluate && end_leader > start_leader
4680 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004681 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 break;
4683
4684 /*
4685 * String constant: "string".
4686 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004687 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 break;
4689
4690 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004693 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 break;
4695
4696 /*
4697 * List: [expr, expr]
4698 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004699 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 break;
4701
4702 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004703 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004704 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004705 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004706 {
4707 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4708 }
4709 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004710 {
4711 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004712 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004713 }
4714 else
4715 ret = NOTDONE;
4716 break;
4717
4718 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004719 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004720 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004721 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004722 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004723 ret = NOTDONE;
4724 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004725 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004726 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004727 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004728 break;
4729
4730 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004731 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004733 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 break;
4735
4736 /*
4737 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004738 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 */
LemonBoy2eaef102022-05-06 13:14:50 +01004740 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4741 ret = eval_interp_string(arg, rettv, evaluate);
4742 else
4743 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 break;
4745
4746 /*
4747 * Register contents: @r.
4748 */
4749 case '@': ++*arg;
4750 if (evaluate)
4751 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004752 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004753 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004754 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004755 emsg_invreg(**arg);
4756 else
4757 {
4758 rettv->v_type = VAR_STRING;
4759 rettv->vval.v_string = get_reg_contents(**arg,
4760 GREG_EXPR_SRC);
4761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763 if (**arg != NUL)
4764 ++*arg;
4765 break;
4766
4767 /*
4768 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004769 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004771 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004772 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004773 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004774 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004775 if (ret == OK && evaluate)
4776 {
4777 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4778
Bram Moolenaara9931532021-06-12 15:58:16 +02004779 // Compile it here to get the return type. The return
4780 // type is optional, when it's missing use t_unknown.
4781 // This is recognized in compile_return().
4782 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4783 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004784 if (compile_def_function(ufunc, FALSE,
4785 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004786 {
4787 clear_tv(rettv);
4788 ret = FAIL;
4789 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004790 }
4791 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004792 if (ret == NOTDONE)
4793 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004794 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004795 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004796
Bram Moolenaar9215f012020-06-27 21:18:00 +02004797 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004798 if (**arg == ')')
4799 ++*arg;
4800 else if (ret == OK)
4801 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004802 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004803 clear_tv(rettv);
4804 ret = FAIL;
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 break;
4808
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 break;
4811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812
4813 if (ret == NOTDONE)
4814 {
4815 /*
4816 * Must be a variable or function name.
4817 * Can also be a curly-braces kind of name: {expr}.
4818 */
4819 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004820 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004821 if (alias != NULL)
4822 s = alias;
4823
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004824 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004825 ret = FAIL;
4826 else
4827 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004828 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4829
Bram Moolenaar4525a572022-02-13 11:57:33 +00004830 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004831 {
4832 emsg(_(e_cannot_use_underscore_here));
4833 ret = FAIL;
4834 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004835 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004836 && s[0] == 's' && s[1] == ':')
4837 {
4838 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4839 ret = FAIL;
4840 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004841 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004842 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004843 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004844 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004845 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004846 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004848 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004849 // get the value of "true", "false", etc. or a variable
4850 ret = FAIL;
4851 if (vim9script)
4852 ret = handle_predefined(s, len, rettv);
4853 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004854 {
4855 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004856 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004857 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004858 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004859 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004860 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004861 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004862 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004863 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004864 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004865 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004866 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004867 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004868 }
4869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004870 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4871 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004872 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004873 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 /*
4876 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4877 */
4878 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004879 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004880
4881 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004882 return ret;
4883}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004884
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004885/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004886 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004887 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004888 * Adjusts "end_leaderp" until it is at "start_leader".
4889 */
4890 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004891eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004892 typval_T *rettv,
4893 int numeric_only,
4894 char_u *start_leader,
4895 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004896{
4897 char_u *end_leader = *end_leaderp;
4898 int ret = OK;
4899 int error = FALSE;
4900 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004901 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004902 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004903 float_T f = 0.0;
4904
4905 if (rettv->v_type == VAR_FLOAT)
4906 f = rettv->vval.v_float;
4907 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004908 {
4909 while (VIM_ISWHITE(end_leader[-1]))
4910 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004911 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004912 val = tv2bool(rettv);
4913 else
4914 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004915 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004916 if (error)
4917 {
4918 clear_tv(rettv);
4919 ret = FAIL;
4920 }
4921 else
4922 {
4923 while (end_leader > start_leader)
4924 {
4925 --end_leader;
4926 if (*end_leader == '!')
4927 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004928 if (numeric_only)
4929 {
4930 ++end_leader;
4931 break;
4932 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004933 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004934 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004935 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004936 {
4937 rettv->v_type = VAR_BOOL;
4938 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4939 }
4940 else
4941 f = !f;
4942 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004943 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004944 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004945 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004946 type = VAR_BOOL;
4947 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004948 }
4949 else if (*end_leader == '-')
4950 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004951 if (rettv->v_type == VAR_FLOAT)
4952 f = -f;
4953 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004954 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004955 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004956 type = VAR_NUMBER;
4957 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004958 }
4959 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004960 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004962 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004963 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004965 else
4966 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004967 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004968 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004969 rettv->v_type = type;
4970 else
4971 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004972 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004975 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 return ret;
4977}
4978
4979/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004980 * Call the function referred to in "rettv".
4981 */
4982 static int
4983call_func_rettv(
4984 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004985 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004986 typval_T *rettv,
4987 int evaluate,
4988 dict_T *selfdict,
4989 typval_T *basetv)
4990{
4991 partial_T *pt = NULL;
4992 funcexe_T funcexe;
4993 typval_T functv;
4994 char_u *s;
4995 int ret;
4996
4997 // need to copy the funcref so that we can clear rettv
4998 if (evaluate)
4999 {
5000 functv = *rettv;
5001 rettv->v_type = VAR_UNKNOWN;
5002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005003 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005004 if (functv.v_type == VAR_PARTIAL)
5005 {
5006 pt = functv.vval.v_partial;
5007 s = partial_name(pt);
5008 }
5009 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005010 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005011 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005012 if (s == NULL || *s == NUL)
5013 {
5014 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005015 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005016 goto theend;
5017 }
5018 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005019 }
5020 else
5021 s = (char_u *)"";
5022
Bram Moolenaara80faa82020-04-12 19:37:17 +02005023 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005024 funcexe.fe_firstline = curwin->w_cursor.lnum;
5025 funcexe.fe_lastline = curwin->w_cursor.lnum;
5026 funcexe.fe_evaluate = evaluate;
5027 funcexe.fe_partial = pt;
5028 funcexe.fe_selfdict = selfdict;
5029 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005030 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005031
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005032theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005033 // Clear the funcref afterwards, so that deleting it while
5034 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005035 if (evaluate)
5036 clear_tv(&functv);
5037
5038 return ret;
5039}
5040
5041/*
5042 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005043 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005044 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5045 */
5046 static int
5047eval_lambda(
5048 char_u **arg,
5049 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005050 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005051 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005052{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005053 int evaluate = evalarg != NULL
5054 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005055 typval_T base = *rettv;
5056 int ret;
5057
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005058 rettv->v_type = VAR_UNKNOWN;
5059
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005060 if (**arg == '{')
5061 {
5062 // ->{lambda}()
5063 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5064 }
5065 else
5066 {
5067 // ->(lambda)()
5068 ++*arg;
5069 ret = eval1(arg, rettv, evalarg);
5070 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005071 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005072 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005073 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005074 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005075 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005076 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5077 && rettv->v_type != VAR_PARTIAL)
5078 {
5079 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5080 return FAIL;
5081 }
5082 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005083 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005084 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005085 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005086
5087 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005088 {
5089 if (verbose)
5090 {
5091 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005092 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005093 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005094 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005095 }
5096 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005097 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005098 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005099 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005100 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005101
5102 // Clear the funcref afterwards, so that deleting it while
5103 // evaluating the arguments is possible (see test55).
5104 if (evaluate)
5105 clear_tv(&base);
5106
5107 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005108}
5109
5110/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005111 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005112 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005113 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5114 */
5115 static int
5116eval_method(
5117 char_u **arg,
5118 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005119 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005120 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005121{
5122 char_u *name;
5123 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005124 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005125 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005126 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005127 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005128 int evaluate = evalarg != NULL
5129 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005130
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005131 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005132
Bram Moolenaarac92e252019-08-03 21:58:38 +02005133 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005134 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005135 if (alias != NULL)
5136 name = alias;
5137
5138 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005139 {
5140 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005141 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005142 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005143 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005144 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005145 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005146 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005147
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005148 // If there is no "(" immediately following, but there is further on,
5149 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5150 // Does not handle anything where "(" is part of the expression.
5151 *arg = skipwhite(*arg);
5152
5153 if (**arg != '(' && alias == NULL
5154 && (paren = vim_strchr(*arg, '(')) != NULL)
5155 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005156 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005157
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005158 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005159 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005160 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005161 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5162 if (evalarg != NULL)
5163 {
5164 getline = evalarg->eval_getline;
5165 evalarg->eval_getline = NULL;
5166 }
5167
5168 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005169 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005170 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005171 *arg = name + len;
5172 ret = FAIL;
5173 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005174 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005175 {
5176 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005177 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005178 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005179
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005180 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005181 if (getline != NULL)
5182 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005183 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005184
5185 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005186 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005187 *arg = skipwhite(*arg);
5188
5189 if (**arg != '(')
5190 {
5191 if (verbose)
5192 semsg(_(e_missing_parenthesis_str), name);
5193 ret = FAIL;
5194 }
5195 else if (VIM_ISWHITE((*arg)[-1]))
5196 {
5197 if (verbose)
5198 emsg(_(e_no_white_space_allowed_before_parenthesis));
5199 ret = FAIL;
5200 }
5201 else
5202 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005203 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005204 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005205 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005206
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005207 // Clear the funcref afterwards, so that deleting it while
5208 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005209 if (evaluate)
5210 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005211 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005212
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005213 if (alias != NULL)
5214 vim_free(alias);
5215
Bram Moolenaarac92e252019-08-03 21:58:38 +02005216 return ret;
5217}
5218
5219/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005220 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5221 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5223 */
5224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005225eval_index(
5226 char_u **arg,
5227 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005228 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005231 int evaluate = evalarg != NULL
5232 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005234 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005237 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005238 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005240 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5241 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005243 init_tv(&var1);
5244 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 /*
5248 * dict.name
5249 */
5250 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005251 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005253 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005255 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 }
5257 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 /*
5260 * something[idx]
5261 *
5262 * Get the (first) variable from inside the [].
5263 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005264 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 if (**arg == ':')
5266 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005267 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005269 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005270 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005271 semsg(_(e_white_space_required_before_and_after_str_at_str),
5272 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005273 clear_tv(&var1);
5274 return FAIL;
5275 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005276 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005278 int error = FALSE;
5279
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005280 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005281 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005282 && var1.v_type == VAR_FLOAT)
5283 {
5284 var1.vval.v_string = typval_tostring(&var1, TRUE);
5285 var1.v_type = VAR_STRING;
5286 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005287
Bram Moolenaar4525a572022-02-13 11:57:33 +00005288 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005289 tv_get_number_chk(&var1, &error);
5290 else
5291 error = tv_get_string_chk(&var1) == NULL;
5292 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005293 {
5294 // not a number or string
5295 clear_tv(&var1);
5296 return FAIL;
5297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299
5300 /*
5301 * Get the second variable from inside the [:].
5302 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005303 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 if (**arg == ':')
5305 {
5306 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005307 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005308 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005309 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005310 semsg(_(e_white_space_required_before_and_after_str_at_str),
5311 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005312 if (!empty1)
5313 clear_tv(&var1);
5314 return FAIL;
5315 }
5316 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317 if (**arg == ']')
5318 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005319 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 if (!empty1)
5322 clear_tv(&var1);
5323 return FAIL;
5324 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005325 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 if (!empty1)
5329 clear_tv(&var1);
5330 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 return FAIL;
5332 }
5333 }
5334
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005335 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005336 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005337 if (**arg != ']')
5338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005339 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005340 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 clear_tv(&var1);
5342 if (range)
5343 clear_tv(&var2);
5344 return FAIL;
5345 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005346 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 }
5348
5349 if (evaluate)
5350 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005351 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005352 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005353 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005354
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005355 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005358 clear_tv(&var2);
5359 return res;
5360 }
5361 return OK;
5362}
5363
5364/*
5365 * Check if "rettv" can have an [index] or [sli:ce]
5366 */
5367 int
5368check_can_index(typval_T *rettv, int evaluate, int verbose)
5369{
5370 switch (rettv->v_type)
5371 {
5372 case VAR_FUNC:
5373 case VAR_PARTIAL:
5374 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005375 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005376 return FAIL;
5377 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005378 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005379 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005380 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005381 case VAR_BOOL:
5382 case VAR_SPECIAL:
5383 case VAR_JOB:
5384 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005385 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005386 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005387 if (verbose)
5388 emsg(_(e_cannot_index_special_variable));
5389 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005390 case VAR_CLASS:
5391 case VAR_TYPEALIAS:
5392 if (verbose)
5393 check_typval_is_value(rettv);
5394 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005395 case VAR_UNKNOWN:
5396 case VAR_ANY:
5397 case VAR_VOID:
5398 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005400 emsg(_(e_cannot_index_special_variable));
5401 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005403 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005405 case VAR_STRING:
5406 case VAR_LIST:
5407 case VAR_DICT:
5408 case VAR_BLOB:
5409 break;
5410 case VAR_NUMBER:
5411 if (in_vim9script())
5412 emsg(_(e_cannot_index_number));
5413 break;
5414 }
5415 return OK;
5416}
5417
5418/*
5419 * Apply index or range to "rettv".
5420 * "var1" is the first index, NULL for [:expr].
5421 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005422 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5423 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005424 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5425 */
5426 int
5427eval_index_inner(
5428 typval_T *rettv,
5429 int is_range,
5430 typval_T *var1,
5431 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005432 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005433 char_u *key,
5434 int keylen,
5435 int verbose)
5436{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005437 varnumber_T n1, n2 = 0;
5438 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005439
5440 n1 = 0;
5441 if (var1 != NULL && rettv->v_type != VAR_DICT)
5442 n1 = tv_get_number(var1);
5443
5444 if (is_range)
5445 {
5446 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005448 if (verbose)
5449 emsg(_(e_cannot_slice_dictionary));
5450 return FAIL;
5451 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005452 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005453 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005454 else
5455 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005456 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005457
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005458 switch (rettv->v_type)
5459 {
5460 case VAR_UNKNOWN:
5461 case VAR_ANY:
5462 case VAR_VOID:
5463 case VAR_FUNC:
5464 case VAR_PARTIAL:
5465 case VAR_FLOAT:
5466 case VAR_BOOL:
5467 case VAR_SPECIAL:
5468 case VAR_JOB:
5469 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005470 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005471 case VAR_CLASS:
5472 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005473 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005474 break; // not evaluating, skipping over subscript
5475
5476 case VAR_NUMBER:
5477 case VAR_STRING:
5478 {
5479 char_u *s = tv_get_string(rettv);
5480
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005482 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005483 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005484 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005485 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005486 else
5487 s = char_from_string(s, n1);
5488 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005489 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005491 // The resulting variable is a substring. If the indexes
5492 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 if (n1 < 0)
5494 {
5495 n1 = len + n1;
5496 if (n1 < 0)
5497 n1 = 0;
5498 }
5499 if (n2 < 0)
5500 n2 = len + n2;
5501 else if (n2 >= len)
5502 n2 = len;
5503 if (n1 >= len || n2 < 0 || n1 > n2)
5504 s = NULL;
5505 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005506 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 }
5508 else
5509 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005510 // The resulting variable is a string of a single
5511 // character. If the index is too big or negative the
5512 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 if (n1 >= len || n1 < 0)
5514 s = NULL;
5515 else
5516 s = vim_strnsave(s + n1, 1);
5517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518 clear_tv(rettv);
5519 rettv->v_type = VAR_STRING;
5520 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005521 }
5522 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005524 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005525 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5526 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005527 break;
5528
5529 case VAR_LIST:
5530 if (var1 == NULL)
5531 n1 = 0;
5532 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005533 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005534 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005535 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005536 return FAIL;
5537 break;
5538
5539 case VAR_DICT:
5540 {
5541 dictitem_T *item;
5542 typval_T tmp;
5543
5544 if (key == NULL)
5545 {
5546 key = tv_get_string_chk(var1);
5547 if (key == NULL)
5548 return FAIL;
5549 }
5550
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005551 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005552
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005553 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005554 {
5555 if (verbose)
5556 {
5557 if (keylen > 0)
5558 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005559 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005560 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005561 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005562 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005563
5564 copy_tv(&item->di_tv, &tmp);
5565 clear_tv(rettv);
5566 *rettv = tmp;
5567 }
5568 break;
5569 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 return OK;
5571}
5572
5573/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005574 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005575 */
5576 char_u *
5577partial_name(partial_T *pt)
5578{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005579 if (pt != NULL)
5580 {
5581 if (pt->pt_name != NULL)
5582 return pt->pt_name;
5583 if (pt->pt_func != NULL)
5584 return pt->pt_func->uf_name;
5585 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005586 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005587}
5588
Bram Moolenaarddecc252016-04-06 22:59:37 +02005589 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005590partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005591{
5592 int i;
5593
5594 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005595 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005596 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005597 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005598 if (pt->pt_name != NULL)
5599 {
5600 func_unref(pt->pt_name);
5601 vim_free(pt->pt_name);
5602 }
5603 else
5604 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005605 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005606
Bram Moolenaar54656012021-06-09 20:50:46 +02005607 // "out_up" is no longer used, decrement refcount on partial that owns it.
5608 partial_unref(pt->pt_outer.out_up_partial);
5609
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005610 // Using pt_outer from another partial.
5611 partial_unref(pt->pt_outer_partial);
5612
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005613 // Decrease the reference count for the context of a closure. If down
5614 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005615 if (pt->pt_funcstack != NULL)
5616 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005617 --pt->pt_funcstack->fs_refcount;
5618 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005619 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005620 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005621 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5622 if (pt->pt_loopvars[i] != NULL)
5623 {
5624 --pt->pt_loopvars[i]->lvs_refcount;
5625 loopvars_check_refcount(pt->pt_loopvars[i]);
5626 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005627
Bram Moolenaarddecc252016-04-06 22:59:37 +02005628 vim_free(pt);
5629}
5630
5631/*
5632 * Unreference a closure: decrement the reference count and free it when it
5633 * becomes zero.
5634 */
5635 void
5636partial_unref(partial_T *pt)
5637{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005638 if (pt == NULL)
5639 return;
5640
5641 int done = FALSE;
5642
5643 if (--pt->pt_refcount <= 0)
5644 partial_free(pt);
5645
5646 // If the reference count goes down to one, the funcstack may be the
5647 // only reference and can be freed if no other partials reference it.
5648 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005649 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005650 // careful: if the funcstack is freed it may contain this partial
5651 // and it gets freed as well
5652 if (pt->pt_funcstack != NULL)
5653 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005654
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005655 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005656 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005657 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005658
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005659 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5660 if (pt->pt_loopvars[depth] != NULL
5661 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005662 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005663 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005664 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005665}
5666
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005667/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005668 * Return a textual representation of a string in "tv".
5669 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5670 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5671 * strings as "string()", otherwise does not put quotes around strings.
5672 * May return NULL.
5673 */
5674 static char_u *
5675string_tv2string(
5676 typval_T *tv,
5677 char_u **tofree,
5678 int echo_style,
5679 int composite_val)
5680{
5681 char_u *r = NULL;
5682
5683 if (echo_style && !composite_val)
5684 {
5685 *tofree = NULL;
5686 r = tv->vval.v_string;
5687 if (r == NULL)
5688 r = (char_u *)"";
5689 }
5690 else
5691 {
5692 *tofree = string_quote(tv->vval.v_string, FALSE);
5693 r = *tofree;
5694 }
5695
5696 return r;
5697}
5698
5699/*
5700 * Return a textual representation of a function in "tv".
5701 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5702 * When "echo_style" is FALSE, put quotes around the function name as
5703 * "function()", otherwise does not put quotes around function name.
5704 * May return NULL.
5705 */
5706 static char_u *
5707func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
5708{
5709 char_u *r = NULL;
5710 char_u buf[MAX_FUNC_NAME_LEN];
5711
5712 if (echo_style)
5713 {
5714 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5715 : make_ufunc_name_readable(tv->vval.v_string,
5716 buf, MAX_FUNC_NAME_LEN);
Christian Brabandtb335a932024-05-21 18:39:10 +02005717 if (r == buf && tv->vval.v_string != NULL)
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005718 {
5719 r = vim_strsave(buf);
5720 *tofree = r;
5721 }
5722 else
5723 *tofree = NULL;
5724 }
5725 else
5726 {
5727 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5728 : make_ufunc_name_readable(tv->vval.v_string,
5729 buf, MAX_FUNC_NAME_LEN), TRUE);
5730 r = *tofree;
5731 }
5732
5733 return r;
5734}
5735
5736/*
5737 * Return a textual representation of a partial in "tv".
5738 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5739 * "numbuf" is used for a number. May return NULL.
5740 */
5741 static char_u *
5742partial_tv2string(
5743 typval_T *tv,
5744 char_u **tofree,
5745 char_u *numbuf,
5746 int copyID)
5747{
5748 char_u *r = NULL;
5749 partial_T *pt;
5750 char_u *fname;
5751 garray_T ga;
5752 int i;
5753 char_u *tf;
5754
5755 pt = tv->vval.v_partial;
5756 fname = string_quote(pt == NULL ? NULL : partial_name(pt), FALSE);
5757
5758 ga_init2(&ga, 1, 100);
5759 ga_concat(&ga, (char_u *)"function(");
5760 if (fname != NULL)
5761 {
5762 // When using uf_name prepend "g:" for a global function.
5763 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
5764 && vim_isupper(fname[1]))
5765 {
5766 ga_concat(&ga, (char_u *)"'g:");
5767 ga_concat(&ga, fname + 1);
5768 }
5769 else
5770 ga_concat(&ga, fname);
5771 vim_free(fname);
5772 }
5773 if (pt != NULL && pt->pt_argc > 0)
5774 {
5775 ga_concat(&ga, (char_u *)", [");
5776 for (i = 0; i < pt->pt_argc; ++i)
5777 {
5778 if (i > 0)
5779 ga_concat(&ga, (char_u *)", ");
5780 ga_concat(&ga, tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5781 vim_free(tf);
5782 }
5783 ga_concat(&ga, (char_u *)"]");
5784 }
5785 if (pt != NULL && pt->pt_dict != NULL)
5786 {
5787 typval_T dtv;
5788
5789 ga_concat(&ga, (char_u *)", ");
5790 dtv.v_type = VAR_DICT;
5791 dtv.vval.v_dict = pt->pt_dict;
5792 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5793 vim_free(tf);
5794 }
5795 // terminate with ')' and a NUL
5796 ga_concat_len(&ga, (char_u *)")", 2);
5797
5798 *tofree = ga.ga_data;
5799 r = *tofree;
5800
5801 return r;
5802}
5803
5804/*
5805 * Return a textual representation of a List in "tv".
5806 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5807 * When "copyID" is not zero replace recursive lists with "...". When
5808 * "restore_copyID" is FALSE, repeated items in lists are replaced with "...".
5809 * May return NULL.
5810 */
5811 static char_u *
5812list_tv2string(
5813 typval_T *tv,
5814 char_u **tofree,
5815 int copyID,
5816 int restore_copyID)
5817{
5818 char_u *r = NULL;
5819
5820 if (tv->vval.v_list == NULL)
5821 {
5822 // NULL list is equivalent to empty list.
5823 *tofree = NULL;
5824 r = (char_u *)"[]";
5825 }
5826 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5827 && tv->vval.v_list->lv_len > 0)
5828 {
5829 *tofree = NULL;
5830 r = (char_u *)"[...]";
5831 }
5832 else
5833 {
Christian Brabandtb335a932024-05-21 18:39:10 +02005834 int old_copyID;
5835 if (restore_copyID)
5836 old_copyID = tv->vval.v_list->lv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005837
5838 tv->vval.v_list->lv_copyID = copyID;
5839 *tofree = list2string(tv, copyID, restore_copyID);
5840 if (restore_copyID)
5841 tv->vval.v_list->lv_copyID = old_copyID;
5842 r = *tofree;
5843 }
5844
5845 return r;
5846}
5847
5848/*
5849 * Return a textual representation of a Dict in "tv".
5850 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5851 * When "copyID" is not zero replace recursive dicts with "...".
5852 * When "restore_copyID" is FALSE, repeated items in the dictionary are
5853 * replaced with "...". May return NULL.
5854 */
5855 static char_u *
5856dict_tv2string(
5857 typval_T *tv,
5858 char_u **tofree,
5859 int copyID,
5860 int restore_copyID)
5861{
5862 char_u *r = NULL;
5863
5864 if (tv->vval.v_dict == NULL)
5865 {
5866 // NULL dict is equivalent to empty dict.
5867 *tofree = NULL;
5868 r = (char_u *)"{}";
5869 }
5870 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5871 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
5872 {
5873 *tofree = NULL;
5874 r = (char_u *)"{...}";
5875 }
5876 else
5877 {
Christian Brabandtb335a932024-05-21 18:39:10 +02005878 int old_copyID;
5879 if (restore_copyID)
5880 old_copyID = tv->vval.v_dict->dv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005881
5882 tv->vval.v_dict->dv_copyID = copyID;
5883 *tofree = dict2string(tv, copyID, restore_copyID);
5884 if (restore_copyID)
5885 tv->vval.v_dict->dv_copyID = old_copyID;
5886 r = *tofree;
5887 }
5888
5889 return r;
5890}
5891
5892/*
5893 * Return a textual representation of a job or a channel in "tv".
5894 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5895 * "numbuf" is used for a number.
5896 * When "composite_val" is FALSE, put quotes around strings as "string()",
5897 * otherwise does not put quotes around strings.
5898 * May return NULL.
5899 */
5900 static char_u *
5901jobchan_tv2string(
5902 typval_T *tv,
5903 char_u **tofree,
5904 char_u *numbuf,
5905 int composite_val)
5906{
5907 char_u *r = NULL;
5908
5909#ifdef FEAT_JOB_CHANNEL
5910 *tofree = NULL;
5911
5912 if (tv->v_type == VAR_JOB)
5913 r = job_to_string_buf(tv, numbuf);
5914 else
5915 r = channel_to_string_buf(tv, numbuf);
5916
5917 if (composite_val)
5918 {
5919 *tofree = string_quote(r, FALSE);
5920 r = *tofree;
5921 }
5922#endif
5923
5924 return r;
5925}
5926
5927/*
5928 * Return a textual representation of a class in "tv".
5929 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5930 * May return NULL.
5931 */
5932 static char_u *
5933class_tv2string(typval_T *tv, char_u **tofree)
5934{
5935 char_u *r = NULL;
5936 class_T *cl = tv->vval.v_class;
5937 char *s = "class";
5938
5939 if (cl != NULL && IS_INTERFACE(cl))
5940 s = "interface";
5941 else if (cl != NULL && IS_ENUM(cl))
5942 s = "enum";
5943 size_t len = STRLEN(s) + 1 +
5944 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
5945 r = *tofree = alloc(len);
5946 vim_snprintf((char *)r, len, "%s %s", s,
5947 cl == NULL ? "[unknown]" : (char *)cl->class_name);
5948
5949 return r;
5950}
5951
5952/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 * Return a string with the string representation of a variable.
5954 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005955 * "numbuf" is used for a number.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005956 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005957 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005958 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005959 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005960 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5961 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005962 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005964 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005965echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005966 typval_T *tv,
5967 char_u **tofree,
5968 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005969 int copyID,
5970 int echo_style,
5971 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005972 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005974 static int recurse = 0;
5975 char_u *r = NULL;
5976
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005978 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005979 if (!did_echo_string_emsg)
5980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005981 // Only give this message once for a recursive call to avoid
5982 // flooding the user with errors. And stop iterating over lists
5983 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005984 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005985 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005987 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005988 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005989 }
5990 ++recurse;
5991
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 switch (tv->v_type)
5993 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005994 case VAR_STRING:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005995 r = string_tv2string(tv, tofree, echo_style, composite_val);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005996 break;
5997
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 case VAR_FUNC:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02005999 r = func_tv2string(tv, tofree, echo_style);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006000 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006001
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006002 case VAR_PARTIAL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006003 r = partial_tv2string(tv, tofree, numbuf, copyID);
6004 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006005
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006006 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006007 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006008 break;
6009
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 case VAR_LIST:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006011 r = list_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006012 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006013
Bram Moolenaar8c711452005-01-14 21:53:12 +00006014 case VAR_DICT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006015 r = dict_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006016 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006018 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006019 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006020 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006022 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006023 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006024 break;
6025
Bram Moolenaar835dc632016-02-07 14:27:38 +01006026 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006027 case VAR_CHANNEL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006028 r = jobchan_tv2string(tv, tofree, numbuf, composite_val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006030
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006031 case VAR_INSTR:
6032 *tofree = NULL;
6033 r = (char_u *)"instructions";
6034 break;
6035
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006036 case VAR_CLASS:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006037 r = class_tv2string(tv, tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006038 break;
6039
6040 case VAR_OBJECT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006041 *tofree = r = object2string(tv->vval.v_object, numbuf, copyID,
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006042 echo_style, restore_copyID,
6043 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006044 break;
6045
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006046 case VAR_FLOAT:
6047 *tofree = NULL;
6048 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6049 r = numbuf;
6050 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006051
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006052 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006053 case VAR_SPECIAL:
6054 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006055 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006056 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006057
6058 case VAR_TYPEALIAS:
6059 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6060 r = *tofree;
6061 if (r == NULL)
6062 r = (char_u *)"";
6063 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006065
Bram Moolenaar8502c702014-06-17 12:51:16 +02006066 if (--recurse == 0)
6067 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006068 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006069}
6070
6071/*
6072 * Return a string with the string representation of a variable.
6073 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6074 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006075 * Does not put quotes around strings, as ":echo" displays values.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006076 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006077 * May return NULL.
6078 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006079 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006080echo_string(
6081 typval_T *tv,
6082 char_u **tofree,
6083 char_u *numbuf,
6084 int copyID)
6085{
6086 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6087}
6088
6089/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006090 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6091 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006092 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006093 */
6094 int
6095buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6096{
6097 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006098 char_u *t;
6099 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006100
6101 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6102 return -1;
6103
6104 if (lnum > buf->b_ml.ml_line_count)
6105 lnum = buf->b_ml.ml_line_count;
6106
6107 str = ml_get_buf(buf, lnum, FALSE);
6108 if (str == NULL)
6109 return -1;
6110
6111 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006112 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006113
Bram Moolenaar91458462021-01-13 20:08:38 +01006114 // count the number of characters
6115 t = str;
6116 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6117 t += mb_ptr2len(t);
6118
6119 // In insert mode, when the cursor is at the end of a non-empty line,
6120 // byteidx points to the NUL character immediately past the end of the
6121 // string. In this case, add one to the character count.
6122 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6123 count++;
6124
6125 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006126}
6127
6128/*
6129 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006130 * byte index. Works only for loaded buffers. Returns -1 on failure.
6131 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006132 */
6133 int
6134buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6135{
6136 char_u *str;
6137 char_u *t;
6138
6139 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6140 return -1;
6141
6142 if (lnum > buf->b_ml.ml_line_count)
6143 lnum = buf->b_ml.ml_line_count;
6144
6145 str = ml_get_buf(buf, lnum, FALSE);
6146 if (str == NULL)
6147 return -1;
6148
6149 // Convert the character offset to a byte offset
6150 t = str;
6151 while (*t != NUL && --charidx > 0)
6152 t += mb_ptr2len(t);
6153
Bram Moolenaar91458462021-01-13 20:08:38 +01006154 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006155}
6156
6157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006159 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006162var2fpos(
6163 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006164 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006165 int *fnum, // set to fnum for '0, 'A, etc.
6166 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006168 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006170 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006172 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006173 if (varp->v_type == VAR_LIST)
6174 {
6175 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006176 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006177 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006178 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006179
6180 l = varp->vval.v_list;
6181 if (l == NULL)
6182 return NULL;
6183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006184 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006185 pos.lnum = list_find_nr(l, 0L, &error);
6186 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006187 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006188 if (charcol)
6189 len = (long)mb_charlen(ml_get(pos.lnum));
6190 else
John Marriottbfcc8952024-03-11 22:04:45 +01006191 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006192
Bram Moolenaarec65d772020-08-20 22:29:12 +02006193 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006194 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006195 li = list_find(l, 1L);
6196 if (li != NULL && li->li_tv.v_type == VAR_STRING
6197 && li->li_tv.vval.v_string != NULL
6198 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006199 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006200 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006201 }
6202 else
6203 {
6204 pos.col = list_find_nr(l, 1L, &error);
6205 if (error)
6206 return NULL;
6207 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006209 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006210 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006211 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006212 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006213
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006214 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006215 pos.coladd = list_find_nr(l, 2L, &error);
6216 if (error)
6217 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006218
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006219 return &pos;
6220 }
6221
Bram Moolenaarc5809432021-03-27 21:23:30 +01006222 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6223 return NULL;
6224
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006225 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006226 if (name == NULL)
6227 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006228
6229 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006230 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006231 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006232 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006233 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006234 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006235 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006236 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006237 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006238 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006239 pos = VIsual;
6240 else
6241 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006242 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006243 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006244 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006246 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006247 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6249 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006250 pos = *pp;
6251 }
6252 if (pos.lnum != 0)
6253 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006254 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006255 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6256 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006258
Bram Moolenaara5525202006-03-02 22:52:09 +00006259 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006260
Bram Moolenaar477933c2007-07-17 14:32:23 +00006261 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006262 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006263 // the "w_valid" flags are not reset when moving the cursor, but they
6264 // do matter for update_topline() and validate_botline().
6265 check_cursor_moved(curwin);
6266
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006267 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006268 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006269 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006270 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006271 // In silent Ex mode topline is zero, but that's not a valid line
6272 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006273 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006274 return &pos;
6275 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006276 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006277 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006278 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006279 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006280 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006281 return &pos;
6282 }
6283 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006284 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006286 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
6288 pos.lnum = curbuf->b_ml.ml_line_count;
6289 pos.col = 0;
6290 }
6291 else
6292 {
6293 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006294 if (charcol)
6295 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6296 else
John Marriottbfcc8952024-03-11 22:04:45 +01006297 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
6299 return &pos;
6300 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006301 if (in_vim9script())
6302 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 return NULL;
6304}
6305
6306/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006307 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006308 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006309 * Note that the column is passed on as-is, the caller may want to decrement
6310 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006311 * If "charcol" is TRUE use the column as the character index instead of the
6312 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006313 * Return FAIL when conversion is not possible, doesn't check the position for
6314 * validity.
6315 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006317list2fpos(
6318 typval_T *arg,
6319 pos_T *posp,
6320 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006321 colnr_T *curswantp,
6322 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006323{
6324 list_T *l = arg->vval.v_list;
6325 long i = 0;
6326 long n;
6327
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006328 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6329 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006330 if (arg->v_type != VAR_LIST
6331 || l == NULL
6332 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006333 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006334 return FAIL;
6335
6336 if (fnump != NULL)
6337 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006338 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006339 if (n < 0)
6340 return FAIL;
6341 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006342 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006343 *fnump = n;
6344 }
6345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006346 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006347 if (n < 0)
6348 return FAIL;
6349 posp->lnum = n;
6350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006351 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006352 if (n < 0)
6353 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006354 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006355 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006356 if (charcol)
6357 {
6358 buf_T *buf;
6359
6360 // Get the text for the specified line in a loaded buffer
6361 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6362 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6363 return FAIL;
6364
Bram Moolenaar79f23442022-10-10 12:42:57 +01006365 n = buf_charidx_to_byteidx(buf,
6366 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006367 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006368 posp->col = n;
6369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006370 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006371 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006372 posp->coladd = 0;
6373 else
6374 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006375
Bram Moolenaar493c1782014-05-28 14:34:46 +02006376 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006377 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006378
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006379 return OK;
6380}
6381
6382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 * Get the length of an environment variable name.
6384 * Advance "arg" to the first character after the name.
6385 * Return 0 for error.
6386 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006387 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006388get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
6390 char_u *p;
6391 int len;
6392
6393 for (p = *arg; vim_isIDc(*p); ++p)
6394 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006395 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 return 0;
6397
6398 len = (int)(p - *arg);
6399 *arg = p;
6400 return len;
6401}
6402
6403/*
6404 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006405 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 * Return 0 if something is wrong.
6407 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
6411 char_u *p;
6412 int len;
6413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006414 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006416 {
6417 if (*p == ':')
6418 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006419 // "s:" is start of "s:var", but "n:" is not and can be used in
6420 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006421 len = (int)(p - *arg);
6422 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6423 || len > 1)
6424 break;
6425 }
6426 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006427 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 return 0;
6429
6430 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006431 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
6433 return len;
6434}
6435
6436/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006437 * Get the length of the name of a variable or function.
6438 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006440 * Return -1 if curly braces expansion failed.
6441 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 * If the name contains 'magic' {}'s, expand them and return the
6443 * expanded name in an allocated string via 'alias' - caller must free.
6444 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006446get_name_len(
6447 char_u **arg,
6448 char_u **alias,
6449 int evaluate,
6450 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 char_u *p;
6454 char_u *expr_start;
6455 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006457 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6460 && (*arg)[2] == (int)KE_SNR)
6461 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006462 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 *arg += 3;
6464 return get_id_len(arg) + 3;
6465 }
6466 len = eval_fname_script(*arg);
6467 if (len > 0)
6468 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006469 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 *arg += len;
6471 }
6472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006474 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006476 p = find_name_end(*arg, &expr_start, &expr_end,
6477 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (expr_start != NULL)
6479 {
6480 char_u *temp_string;
6481
6482 if (!evaluate)
6483 {
6484 len += (int)(p - *arg);
6485 *arg = skipwhite(p);
6486 return len;
6487 }
6488
6489 /*
6490 * Include any <SID> etc in the expanded string:
6491 * Thus the -len here.
6492 */
6493 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6494 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006495 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 *alias = temp_string;
6497 *arg = skipwhite(p);
6498 return (int)STRLEN(temp_string);
6499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006502 // Only give an error when there is something, otherwise it will be
6503 // reported at a higher level.
6504 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006505 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 return len;
6508}
6509
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006510/*
6511 * Find the end of a variable or function name, taking care of magic braces.
6512 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6513 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006514 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006515 * Return a pointer to just after the name. Equal to "arg" if there is no
6516 * valid name.
6517 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006518 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519find_name_end(
6520 char_u *arg,
6521 char_u **expr_start,
6522 char_u **expr_end,
6523 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 int mb_nest = 0;
6526 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006528 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02006529 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006531 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006533 *expr_start = NULL;
6534 *expr_end = NULL;
6535 }
6536
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006537 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006538 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006539 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006540 return arg;
6541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006542 for (p = arg; *p != NUL
6543 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006544 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006545 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006546 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006547 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006548 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006549 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006550 if (*p == '\'')
6551 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006552 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006553 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006554 ;
6555 if (*p == NUL)
6556 break;
6557 }
6558 else if (*p == '"')
6559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006560 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006561 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006562 if (*p == '\\' && p[1] != NUL)
6563 ++p;
6564 if (*p == NUL)
6565 break;
6566 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006567 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6568 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006569 // "s:" is start of "s:var", but "n:" is not and can be used in
6570 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006571 len = (int)(p - arg);
6572 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006573 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006574 break;
6575 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006576
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006577 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006579 if (*p == '[')
6580 ++br_nest;
6581 else if (*p == ']')
6582 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006584
zeertzjqa93d9cd2023-05-02 16:25:47 +01006585 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006587 if (*p == '{')
6588 {
6589 mb_nest++;
6590 if (expr_start != NULL && *expr_start == NULL)
6591 *expr_start = p;
6592 }
6593 else if (*p == '}')
6594 {
6595 mb_nest--;
6596 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6597 *expr_end = p;
6598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 }
6601
6602 return p;
6603}
6604
6605/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006606 * Expands out the 'magic' {}'s in a variable/function name.
6607 * Note that this can call itself recursively, to deal with
6608 * constructs like foo{bar}{baz}{bam}
6609 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6610 * "in_start" ^
6611 * "expr_start" ^
6612 * "expr_end" ^
6613 * "in_end" ^
6614 *
6615 * Returns a new allocated string, which the caller must free.
6616 * Returns NULL for failure.
6617 */
6618 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006619make_expanded_name(
6620 char_u *in_start,
6621 char_u *expr_start,
6622 char_u *expr_end,
6623 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006624{
6625 char_u c1;
6626 char_u *retval = NULL;
6627 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006628
6629 if (expr_end == NULL || in_end == NULL)
6630 return NULL;
6631 *expr_start = NUL;
6632 *expr_end = NUL;
6633 c1 = *in_end;
6634 *in_end = NUL;
6635
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006636 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006637 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006638 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006639 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6640 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006641 if (retval != NULL)
6642 {
6643 STRCPY(retval, in_start);
6644 STRCAT(retval, temp_result);
6645 STRCAT(retval, expr_end + 1);
6646 }
6647 }
6648 vim_free(temp_result);
6649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006650 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006651 *expr_start = '{';
6652 *expr_end = '}';
6653
6654 if (retval != NULL)
6655 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006656 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006657 if (expr_start != NULL)
6658 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006659 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006660 temp_result = make_expanded_name(retval, expr_start,
6661 expr_end, temp_result);
6662 vim_free(retval);
6663 retval = temp_result;
6664 }
6665 }
6666
6667 return retval;
6668}
6669
6670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006672 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006675eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006677 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006678}
6679
6680/*
6681 * Return TRUE if character "c" can be used as the first character in a
6682 * variable or function name (excluding '{' and '}').
6683 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006686{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006687 return ASCII_ISALPHA(c) || c == '_';
6688}
6689
6690/*
6691 * Return TRUE if character "c" can be used as the first character of a
6692 * dictionary key.
6693 */
6694 int
6695eval_isdictc(int c)
6696{
6697 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698}
6699
6700/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006701 * Handle:
6702 * - expr[expr], expr[expr:expr] subscript
6703 * - ".name" lookup
6704 * - function call with Funcref variable: func(expr)
6705 * - method call: var->method()
6706 *
6707 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006708 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006709 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006711handle_subscript(
6712 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006713 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006714 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006715 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006716 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006717{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006718 int evaluate = evalarg != NULL
6719 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006720 int ret = OK;
6721 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006722 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006723 int getnext;
6724 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006725
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006726 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006727 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006728 // When at the end of the line and ".name" or "->{" or "->X" follows in
6729 // the next line then consume the line break.
6730 p = eval_next_non_blank(*arg, evalarg, &getnext);
6731 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006732 && ((*p == '.'
6733 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6734 || rettv->v_type == VAR_CLASS
6735 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006736 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6737 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6738 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006739 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006740 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006741 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006742 check_white = FALSE;
6743 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006744
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006745 if (rettv->v_type == VAR_ANY)
6746 {
6747 char_u *exp_name;
6748 int cc;
6749 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006750 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006751 type_T *type;
6752
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006753 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006754 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006755 if (**arg != '.')
6756 {
6757 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006758 semsg(_(e_expected_dot_after_name_str),
6759 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006760 ret = FAIL;
6761 break;
6762 }
6763 ++*arg;
6764 if (IS_WHITE_OR_NUL(**arg))
6765 {
6766 if (verbose)
6767 emsg(_(e_no_white_space_allowed_after_dot));
6768 ret = FAIL;
6769 break;
6770 }
6771
6772 // isolate the name
6773 exp_name = *arg;
6774 while (eval_isnamec(**arg))
6775 ++*arg;
6776 cc = **arg;
6777 **arg = NUL;
6778
6779 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006780 evalarg == NULL ? NULL : evalarg->eval_cctx,
6781 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006782 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006783
6784 if (idx < 0 && ufunc == NULL)
6785 {
6786 ret = FAIL;
6787 break;
6788 }
6789 if (idx >= 0)
6790 {
6791 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6792 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6793
6794 copy_tv(sv->sv_tv, rettv);
6795 }
6796 else
6797 {
6798 rettv->v_type = VAR_FUNC;
6799 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6800 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006801 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006802 }
6803
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006804 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6805 || rettv->v_type == VAR_PARTIAL))
6806 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006807 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006808 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6809 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006810
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006811 // Stop the expression evaluation when immediately aborting on
6812 // error, or when an interrupt occurred or an exception was thrown
6813 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006814 if (aborting())
6815 {
6816 if (ret == OK)
6817 clear_tv(rettv);
6818 ret = FAIL;
6819 }
6820 dict_unref(selfdict);
6821 selfdict = NULL;
6822 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006823 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006824 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006825 if (in_vim9script())
6826 *arg = skipwhite(p + 2);
6827 else
6828 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006829 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006830 {
zeertzjqc481ad32023-03-11 16:18:51 +00006831 emsg(_(e_no_white_space_allowed_before_parenthesis));
6832 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006833 }
zeertzjqc481ad32023-03-11 16:18:51 +00006834 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6835 // expr->{lambda}() or expr->(lambda)()
6836 ret = eval_lambda(arg, rettv, evalarg, verbose);
6837 else
6838 // expr->name()
6839 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006840 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006841 // "." is ".name" lookup when we found a dict or when evaluating and
6842 // scriptversion is at least 2, where string concatenation is "..".
6843 else if (**arg == '['
6844 || (**arg == '.' && (rettv->v_type == VAR_DICT
6845 || (!evaluate
6846 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006847 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006848 {
6849 dict_unref(selfdict);
6850 if (rettv->v_type == VAR_DICT)
6851 {
6852 selfdict = rettv->vval.v_dict;
6853 if (selfdict != NULL)
6854 ++selfdict->dv_refcount;
6855 }
6856 else
6857 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006858 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006859 {
6860 clear_tv(rettv);
6861 ret = FAIL;
6862 }
6863 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006864 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6865 || rettv->v_type == VAR_OBJECT))
6866 {
6867 // class member: SomeClass.varname
6868 // class method: SomeClass.SomeMethod()
6869 // class constructor: SomeClass.new()
6870 // object member: someObject.varname
6871 // object method: someObject.SomeMethod()
6872 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6873 {
6874 clear_tv(rettv);
6875 ret = FAIL;
6876 }
6877 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006878 else
6879 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006880 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006882 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6883 // Don't do this when "Func" is already a partial that was bound
6884 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006885 if (selfdict != NULL
6886 && (rettv->v_type == VAR_FUNC
6887 || (rettv->v_type == VAR_PARTIAL
6888 && (rettv->vval.v_partial->pt_auto
6889 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006890 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006891
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006892 dict_unref(selfdict);
6893 return ret;
6894}
6895
6896/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897 * Make a copy of an item.
6898 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006899 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006900 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6901 * reference to an already copied list/dict can be used.
6902 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006903 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006904 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006905item_copy(
6906 typval_T *from,
6907 typval_T *to,
6908 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006909 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006910 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006911{
6912 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006913 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006914
Bram Moolenaar33570922005-01-25 22:26:29 +00006915 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006916 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006917 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006918 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006919 }
6920 ++recurse;
6921
6922 switch (from->v_type)
6923 {
6924 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006925 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006926 case VAR_STRING:
6927 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006928 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006929 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006930 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006931 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006932 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006933 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006934 case VAR_CLASS:
6935 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006936 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937 copy_tv(from, to);
6938 break;
6939 case VAR_LIST:
6940 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006941 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006942 if (from->vval.v_list == NULL)
6943 to->vval.v_list = NULL;
6944 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006946 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006947 to->vval.v_list = from->vval.v_list->lv_copylist;
6948 ++to->vval.v_list->lv_refcount;
6949 }
6950 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006951 to->vval.v_list = list_copy(from->vval.v_list,
6952 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006953 if (to->vval.v_list == NULL)
6954 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006955 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006956 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006958 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006959 case VAR_DICT:
6960 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006961 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006962 if (from->vval.v_dict == NULL)
6963 to->vval.v_dict = NULL;
6964 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006966 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006967 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6968 ++to->vval.v_dict->dv_refcount;
6969 }
6970 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006971 to->vval.v_dict = dict_copy(from->vval.v_dict,
6972 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006973 if (to->vval.v_dict == NULL)
6974 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006975 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006976 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006977 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006979 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006980 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006981 }
6982 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006983 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006984}
6985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 void
6987echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6988{
6989 char_u *tofree;
6990 char_u numbuf[NUMBUFLEN];
6991 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6992
6993 if (*atstart)
6994 {
6995 *atstart = FALSE;
6996 // Call msg_start() after eval1(), evaluating the expression
6997 // may cause a message to appear.
6998 if (with_space)
6999 {
7000 // Mark the saved text as finishing the line, so that what
7001 // follows is displayed on a new line when scrolling back
7002 // at the more prompt.
7003 msg_sb_eol();
7004 msg_start();
7005 }
7006 }
7007 else if (with_space)
7008 msg_puts_attr(" ", echo_attr);
7009
7010 if (p != NULL)
7011 for ( ; *p != NUL && !got_int; ++p)
7012 {
7013 if (*p == '\n' || *p == '\r' || *p == TAB)
7014 {
7015 if (*p != TAB && *needclr)
7016 {
7017 // remove any text still there from the command
7018 msg_clr_eos();
7019 *needclr = FALSE;
7020 }
7021 msg_putchar_attr(*p, echo_attr);
7022 }
7023 else
7024 {
7025 if (has_mbyte)
7026 {
7027 int i = (*mb_ptr2len)(p);
7028
7029 (void)msg_outtrans_len_attr(p, i, echo_attr);
7030 p += i - 1;
7031 }
7032 else
7033 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7034 }
7035 }
7036 vim_free(tofree);
7037}
7038
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 * ":echo expr1 ..." print each argument separated with a space, add a
7041 * newline at the end.
7042 * ":echon expr1 ..." print each argument plain.
7043 */
7044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007045ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007049 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 int needclr = TRUE;
7051 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007052 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007053 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007054 evalarg_T evalarg;
7055
Bram Moolenaare707c882020-07-01 13:07:37 +02007056 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
7058 if (eap->skip)
7059 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007060 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007062 // If eval1() causes an error message the text from the command may
7063 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007064 need_clr_eos = needclr;
7065
Bram Moolenaar68db9962021-05-09 23:19:22 +02007066 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007067 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {
7069 /*
7070 * Report the invalid expression unless the expression evaluation
7071 * has been cancelled due to an aborting error, an interrupt, or an
7072 * exception.
7073 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007074 if (!aborting() && did_emsg == did_emsg_before
7075 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007076 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007077 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 break;
7079 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007080 need_clr_eos = FALSE;
7081
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007083 {
7084 if (rettv.v_type == VAR_VOID)
7085 {
7086 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7087 break;
7088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007090 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007092 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 arg = skipwhite(arg);
7094 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007095 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007096 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
7098 if (eap->skip)
7099 --emsg_skip;
7100 else
7101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007102 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 if (needclr)
7104 msg_clr_eos();
7105 if (eap->cmdidx == CMD_echo)
7106 msg_end();
7107 }
7108}
7109
7110/*
7111 * ":echohl {name}".
7112 */
7113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007114ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007116 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117}
7118
7119/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007120 * Returns the :echo attribute
7121 */
7122 int
7123get_echo_attr(void)
7124{
7125 return echo_attr;
7126}
7127
7128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 * ":execute expr1 ..." execute the result of an expression.
7130 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007131 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007133 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 * Each gets spaces around each argument and a newline at the end for
7135 * echo commands
7136 */
7137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007138ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007141 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 int ret = OK;
7143 char_u *p;
7144 garray_T ga;
7145 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007146 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 ga_init2(&ga, 1, 80);
7149
7150 if (eap->skip)
7151 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007152 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007154 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007155 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
7158 if (!eap->skip)
7159 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007160 char_u buf[NUMBUFLEN];
7161
7162 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007163 {
7164 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7165 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007166 semsg(_(e_using_invalid_value_as_string_str),
7167 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007168 p = NULL;
7169 }
7170 else
7171 p = tv_get_string_buf(&rettv, buf);
7172 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007173 else
7174 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007175 if (p == NULL)
7176 {
7177 clear_tv(&rettv);
7178 ret = FAIL;
7179 break;
7180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 len = (int)STRLEN(p);
7182 if (ga_grow(&ga, len + 2) == FAIL)
7183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007184 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 ret = FAIL;
7186 break;
7187 }
7188 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 ga.ga_len += len;
7192 }
7193
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007194 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 arg = skipwhite(arg);
7196 }
7197
7198 if (ret != FAIL && ga.ga_data != NULL)
7199 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007200 // use the first line of continuation lines for messages
7201 SOURCING_LNUM = start_lnum;
7202
Bram Moolenaar37fef162022-08-29 18:16:32 +01007203 if (eap->cmdidx == CMD_echomsg
7204 || eap->cmdidx == CMD_echowindow
7205 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007206 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007207 // Mark the already saved text as finishing the line, so that what
7208 // follows is displayed on a new line when scrolling back at the
7209 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007210 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007211 }
7212
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007213 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007214 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007215 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007216 out_flush();
7217 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007218 else if (eap->cmdidx == CMD_echowindow)
7219 {
7220#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007221 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007222#endif
7223 msg_attr(ga.ga_data, echo_attr);
7224#ifdef HAS_MESSAGE_WINDOW
7225 end_echowindow();
7226#endif
7227 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007228 else if (eap->cmdidx == CMD_echoconsole)
7229 {
7230 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7231 ui_write((char_u *)"\r\n", 2, TRUE);
7232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 else if (eap->cmdidx == CMD_echoerr)
7234 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007235 int save_did_emsg = did_emsg;
7236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007237 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007238 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 if (!force_abort)
7240 did_emsg = save_did_emsg;
7241 }
7242 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007243 {
7244 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7245
7246 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7247 sticky_cmdmod_flags = cmdmod.cmod_flags
7248 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007250 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007251 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
7254
7255 ga_clear(&ga);
7256
7257 if (eap->skip)
7258 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007259 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260}
7261
7262/*
7263 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7264 * "arg" points to the "&" or '+' when called, to "option" when returning.
7265 * Returns NULL when no option name found. Otherwise pointer to the char
7266 * after the option name.
7267 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007268 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007269find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
7271 char_u *p = *arg;
7272
7273 ++p;
7274 if (*p == 'g' && p[1] == ':')
7275 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007276 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 p += 2;
7278 }
7279 else if (*p == 'l' && p[1] == ':')
7280 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007281 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 p += 2;
7283 }
7284 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007285 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
7287 if (!ASCII_ISALPHA(*p))
7288 return NULL;
7289 *arg = p;
7290
7291 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007292 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 else
7294 while (ASCII_ISALPHA(*p))
7295 ++p;
7296 return p;
7297}
7298
7299/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007300 * Display script name where an item was last set.
7301 * Should only be invoked when 'verbose' is non-zero.
7302 */
7303 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007304last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007305{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007306 char_u *p;
7307
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007308 if (script_ctx.sc_sid == 0)
7309 return;
7310
7311 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7312 if (p == NULL)
7313 return;
7314
7315 verbose_enter();
7316 msg_puts(_("\n\tLast set from "));
7317 msg_puts((char *)p);
7318 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007319 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007320 msg_puts(_(line_msg));
7321 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007322 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007323 verbose_leave();
7324 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007325}
7326
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007327#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
7329/*
7330 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007331 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 * "flags" can be "g" to do a global substitute.
7333 * Returns an allocated string, NULL for error.
7334 */
7335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007336do_string_sub(
7337 char_u *str,
7338 char_u *pat,
7339 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007340 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007341 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
7343 int sublen;
7344 regmatch_T regmatch;
7345 int i;
7346 int do_all;
7347 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007348 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 garray_T ga;
7350 char_u *ret;
7351 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007352 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007354 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007356 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
7358 ga_init2(&ga, 1, 200);
7359
7360 do_all = (flags[0] == 'g');
7361
7362 regmatch.rm_ic = p_ic;
7363 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7364 if (regmatch.regprog != NULL)
7365 {
7366 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007367 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007370 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007371 if (regmatch.startp[0] == regmatch.endp[0])
7372 {
7373 if (zero_width == regmatch.startp[0])
7374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007375 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007376 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007377 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7378 (size_t)i);
7379 ga.ga_len += i;
7380 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007381 continue;
7382 }
7383 zero_width = regmatch.startp[0];
7384 }
7385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 /*
7387 * Get some space for a temporary buffer to do the substitution
7388 * into. It will contain:
7389 * - The text up to where the match is.
7390 * - The substituted text.
7391 * - The text after the match.
7392 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007393 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007394 if (sublen <= 0)
7395 {
7396 ga_clear(&ga);
7397 break;
7398 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007399 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7401 {
7402 ga_clear(&ga);
7403 break;
7404 }
7405
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007406 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 i = (int)(regmatch.startp[0] - tail);
7408 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007409 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007410 (void)vim_regsub(&regmatch, sub, expr,
7411 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7412 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007414 tail = regmatch.endp[0];
7415 if (*tail == NUL)
7416 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (!do_all)
7418 break;
7419 }
7420
7421 if (ga.ga_data != NULL)
7422 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7423
Bram Moolenaar473de612013-06-08 18:19:48 +02007424 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 }
7426
7427 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7428 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007429 if (p_cpo == empty_option)
7430 p_cpo = save_cpo;
7431 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007432 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007433 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007434 // If it's still empty it was changed and restored, need to restore in
7435 // the complicated way.
7436 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007437 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007438 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
7441 return ret;
7442}