blob: bbfe566fa3f20aa60a36becfc4967e50757d9fe2 [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
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100110 // unreferenced lists, tuples and dicts
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200111 (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.
Yegappan Lakshmanan481992c2024-12-06 18:35:12 +0100274 if (partial->pt_obj != NULL)
275 partial->pt_obj->obj_refcount++;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200276 r = call_def_function(partial->pt_func, argc, argv, DEF_USE_PT_ARGV,
Yegappan Lakshmanan481992c2024-12-06 18:35:12 +0100277 partial, partial->pt_obj, fc, rettv);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200278 if (fc_arg == NULL)
279 remove_funccal();
280 if (r == FAIL)
281 return FAIL;
282 }
283 else
284 {
285 char_u *s = partial_name(partial);
286 funcexe_T funcexe;
287
288 if (s == NULL || *s == NUL)
289 return FAIL;
290
291 CLEAR_FIELD(funcexe);
292 funcexe.fe_evaluate = TRUE;
293 funcexe.fe_partial = partial;
294 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
295 return FAIL;
296 }
297
298 return OK;
299}
300
301/*
302 * Evaluate an expression which is a function.
303 * Pass arguments "argv[argc]".
304 * Return the result in "rettv" and OK or FAIL.
305 */
306 static int
307eval_expr_func(
308 typval_T *expr,
309 typval_T *argv,
310 int argc,
311 typval_T *rettv)
312{
313 funcexe_T funcexe;
314 char_u buf[NUMBUFLEN];
315 char_u *s;
316
317 if (expr->v_type == VAR_FUNC)
318 s = expr->vval.v_string;
319 else
320 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
321 if (s == NULL || *s == NUL)
322 return FAIL;
323
324 CLEAR_FIELD(funcexe);
325 funcexe.fe_evaluate = TRUE;
326 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
327 return FAIL;
328
329 return OK;
330}
331
332/*
333 * Evaluate an expression, which is a string.
334 * Return the result in "rettv" and OK or FAIL.
335 */
336 static int
337eval_expr_string(
338 typval_T *expr,
339 typval_T *rettv)
340{
341 char_u *s;
342 char_u buf[NUMBUFLEN];
343
344 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
345 if (s == NULL)
346 return FAIL;
347
348 s = skipwhite(s);
349 if (eval1_emsg(&s, rettv, NULL) == FAIL)
350 return FAIL;
351
352 if (*skipwhite(s) != NUL) // check for trailing chars after expr
353 {
354 clear_tv(rettv);
355 semsg(_(e_invalid_expression_str), s);
356 return FAIL;
357 }
358
359 return OK;
360}
361
362/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363 * Evaluate an expression, which can be a function, partial or string.
364 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200365 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100366 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 * Return the result in "rettv" and OK or FAIL.
368 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200369 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100370eval_expr_typval(
371 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200372 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100373 typval_T *argv,
374 int argc,
375 funccall_T *fc_arg,
376 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100377{
zeertzjqad0c4422023-08-17 22:15:47 +0200378 if (expr->v_type == VAR_PARTIAL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200379 return eval_expr_partial(expr, argv, argc, fc_arg, rettv);
John Marriottbd4614f2024-11-18 20:25:21 +0100380 if (expr->v_type == VAR_INSTR)
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200381 return exe_typval_instr(expr, rettv);
John Marriottbd4614f2024-11-18 20:25:21 +0100382 if (expr->v_type == VAR_FUNC || want_func)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200383 return eval_expr_func(expr, argv, argc, rettv);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200384
John Marriottbd4614f2024-11-18 20:25:21 +0100385 return eval_expr_string(expr, rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100386}
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 Lakshmanan9cb865e2025-03-23 16:42:16 +0100623 * When "join_list" is TRUE convert a List or a Tuple 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;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100630 char_u *retval = NULL;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100631
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100632 if (join_list && (tv->v_type == VAR_LIST || tv->v_type == VAR_TUPLE))
Bram Moolenaar883cf972021-01-15 18:04:43 +0100633 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100634 if (tv->v_type == VAR_LIST)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100635 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100636 ga_init2(&ga, sizeof(char), 80);
637 if (tv->vval.v_list != NULL)
638 {
639 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE,
640 0);
641 if (tv->vval.v_list->lv_len > 0)
642 ga_append(&ga, NL);
643 }
644 ga_append(&ga, NUL);
645 retval = (char_u *)ga.ga_data;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100646 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100647 else
648 {
649 // tuple
650 ga_init2(&ga, sizeof(char), 80);
651 if (tv->vval.v_tuple != NULL)
652 {
653 tuple_join(&ga, tv->vval.v_tuple, (char_u *)"\n", TRUE, FALSE,
654 0);
655 if (TUPLE_LEN(tv->vval.v_tuple) > 0)
656 ga_append(&ga, NL);
657 }
658 ga_append(&ga, NUL);
659 retval = (char_u *)ga.ga_data;
660 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100661 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100662 else if (tv->v_type == VAR_LIST
663 || tv->v_type == VAR_TUPLE
664 || tv->v_type == VAR_DICT)
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200665 {
666 char_u *tofree;
667 char_u numbuf[NUMBUFLEN];
668
669 retval = tv2string(tv, &tofree, numbuf, 0);
670 // Make a copy if we have a value but it's not in allocated memory.
671 if (retval != NULL && tofree == NULL)
672 retval = vim_strsave(retval);
673 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100674 else
675 retval = vim_strsave(tv_get_string(tv));
676 return retval;
677}
678
679/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200680 * Top level evaluation function, returning a string. Does not handle line
681 * breaks.
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100682 * When "join_list" is TRUE convert a List and a Tuple into a sequence of
683 * lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 * Return pointer to allocated memory, or NULL for failure.
685 */
686 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100687eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100688 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200689 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100690 exarg_T *eap,
691 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar33570922005-01-25 22:26:29 +0000693 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100695 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100698 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100699 if (use_simple_function)
700 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
701 else
702 r = eval0(arg, &tv, NULL, &evalarg);
703 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 retval = NULL;
705 else
706 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200707 retval = typval2string(&tv, join_list);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000708 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100710 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712 return retval;
713}
714
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100715 char_u *
716eval_to_string(
717 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200718 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100719 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100720{
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200721 return eval_to_string_eap(arg, join_list, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100722}
723
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000725 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100726 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200727 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
729 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100730eval_to_string_safe(
731 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000732 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100733 int keep_script_version,
734 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200737 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200738 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200739 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaar9530b582022-01-22 13:39:08 +0000741 if (!keep_script_version)
742 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200743 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000744 if (use_sandbox)
745 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100746 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200747 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100748 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000749 if (use_sandbox)
750 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100751 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200752 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200753 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200754 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 return retval;
756}
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758/*
759 * Top level evaluation function, returning a number.
760 * Evaluates "expr" silently.
761 * Returns -1 for an error.
762 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200763 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100764eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
Bram Moolenaar33570922005-01-25 22:26:29 +0000766 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200767 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000768 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100769 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
771 ++emsg_off;
772
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100773 if (use_simple_function)
774 r = may_call_simple_func(expr, &rettv);
775 if (r == NOTDONE)
776 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
777 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 retval = -1;
779 else
780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100781 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000782 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 --emsg_off;
785
786 return retval;
787}
788
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000789/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000790 * Top level evaluation function.
791 * Returns an allocated typval_T with the result.
792 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000793 */
794 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200795eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000796{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100797 return eval_expr_ext(arg, eap, FALSE);
798}
799
800 typval_T *
801eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
802{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000803 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200804 evalarg_T evalarg;
805
806 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000807
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200808 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100809 if (tv != NULL)
810 {
811 int r = NOTDONE;
812
813 if (use_simple_function)
814 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
815 if (r == NOTDONE)
816 r = eval0(arg, tv, eap, &evalarg);
817
818 if (r == FAIL)
819 VIM_CLEAR(tv);
820 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000821
Bram Moolenaar37c83712020-06-30 21:18:36 +0200822 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000823 return tv;
824}
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000827 * "*arg" points to what can be a function name in the form of "import.Name" or
828 * "Funcref". Return the name of the function. Set "tofree" to something that
829 * was allocated.
830 * If "verbose" is FALSE no errors are given.
831 * Return NULL for any failure.
832 */
833 static char_u *
834deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000835 char_u **arg,
836 char_u **tofree,
837 evalarg_T *evalarg,
838 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000839{
840 typval_T ref;
841 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100842 int save_flags = 0;
Yegappan Lakshmananb0206e92024-12-30 09:52:16 +0100843 int evaluate = evalarg != NULL
844 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000845
846 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100847 if (evalarg != NULL)
848 {
849 // need to evaluate this to get an import, like in "a.Func"
850 save_flags = evalarg->eval_flags;
851 evalarg->eval_flags |= EVAL_EVALUATE;
852 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100853 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100854 {
855 dictitem_T *v;
856
857 // If <SID>VarName was used it would not be found, try another way.
858 v = find_var_also_in_script(name, NULL, FALSE);
859 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100860 {
861 name = NULL;
862 goto theend;
863 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100864 copy_tv(&v->di_tv, &ref);
865 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000866 if (*skipwhite(*arg) != NUL)
867 {
868 if (verbose)
869 semsg(_(e_trailing_characters_str), *arg);
870 name = NULL;
871 }
872 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
873 {
874 name = ref.vval.v_string;
875 ref.vval.v_string = NULL;
876 *tofree = name;
877 }
878 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
879 {
880 if (ref.vval.v_partial->pt_argc > 0
881 || ref.vval.v_partial->pt_dict != NULL)
882 {
883 if (verbose)
884 emsg(_(e_cannot_use_partial_here));
885 name = NULL;
886 }
887 else
888 {
889 name = vim_strsave(partial_name(ref.vval.v_partial));
890 *tofree = name;
891 }
892 }
Yegappan Lakshmananb0206e92024-12-30 09:52:16 +0100893 else if (evaluate)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000894 {
895 if (verbose)
896 semsg(_(e_not_callable_type_str), name);
897 name = NULL;
898 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100899
900theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000901 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100902 if (evalarg != NULL)
903 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000904 return name;
905}
906
907/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100908 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200909 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
910 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200913 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100914call_vim_function(
915 char_u *func,
916 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200917 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200918 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000920 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200921 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000922 char_u *arg;
923 char_u *name;
924 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000925 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100927 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200928 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000929 funcexe.fe_firstline = curwin->w_cursor.lnum;
930 funcexe.fe_lastline = curwin->w_cursor.lnum;
931 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000932
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000933 // The name might be "import.Func" or "Funcref". We don't know, we need to
934 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000935 // autoload script has errors. Guess that when there is a dot in the name
936 // showing errors is the right choice.
937 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000938 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000939 if (ignore_errors)
940 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000941 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000942 if (ignore_errors)
943 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000944 if (name == NULL)
945 name = func;
946
947 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
948
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000949 if (ret == FAIL)
950 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000951 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000952
953 return ret;
954}
955
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100956/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100957 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000958 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
959 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000960 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000961 */
962 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963call_func_retstr(
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;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000969 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000970
Bram Moolenaarded27a12018-08-01 19:06:03 +0200971 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000972 return NULL;
973
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100974 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000975 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 return retval;
977}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000978
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000979/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100980 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000981 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000982 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100983 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000984 */
985 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100986call_func_retlist(
987 char_u *func,
988 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200989 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000990{
991 typval_T rettv;
992
Bram Moolenaarded27a12018-08-01 19:06:03 +0200993 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000994 return NULL;
995
996 if (rettv.v_type != VAR_LIST)
997 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100998 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
999 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001000 clear_tv(&rettv);
1001 return NULL;
1002 }
1003
1004 return rettv.vval.v_list;
1005}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaare70dd112022-01-21 16:31:11 +00001007#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001009 * Evaluate "arg", which is 'foldexpr'.
1010 * Note: caller must set "curwin" to match "arg".
1011 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
1012 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 */
1014 int
Bram Moolenaare70dd112022-01-21 16:31:11 +00001015eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaare70dd112022-01-21 16:31:11 +00001017 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00001018 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001019 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +00001021 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001022 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001023 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001025 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001026 current_sctx = wp->w_p_script_ctx[WV_FDE];
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001029 if (use_sandbox)
1030 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001031 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001033
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001034 // Evaluate the expression. If the expression is "FuncName()" call the
1035 // function directly.
1036 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 retval = 0;
1038 else
1039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001040 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001041 if (tv.v_type == VAR_NUMBER)
1042 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001043 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 retval = 0;
1045 else
1046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001047 // If the result is a string, check if there is a non-digit before
1048 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001049 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +02001050 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 *cp = *s++;
1052 retval = atol((char *)s);
1053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001054 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 }
1056 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001057 if (use_sandbox)
1058 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001059 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001060 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001061 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001063 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064}
1065#endif
1066
Ernie Rael64885642023-10-04 20:16:22 +02001067#ifdef LOG_LOCKVAR
1068typedef struct flag_string_S
1069{
1070 int flag;
1071 char *str;
1072} flag_string_T;
1073
1074 static char *
1075flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
1076{
1077 char *p = buf;
1078 *p = NUL;
1079 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1080 {
1081 if ((fstring->flag & flags) != 0)
1082 {
1083 size_t len = STRLEN(fstring->str);
1084 if (n > p - buf + len + 7)
1085 {
1086 STRCAT(p, fstring->str);
1087 p += len;
1088 STRCAT(p, " ");
1089 ++p;
1090 }
1091 else
1092 {
1093 STRCAT(buf, "...");
1094 break;
1095 }
1096 }
1097 }
1098 return buf;
1099}
1100
1101flag_string_T glv_flag_strings[] = {
1102 { GLV_QUIET, "QUIET" },
1103 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1104 { GLV_READ_ONLY, "READ_ONLY" },
1105 { GLV_NO_DECL, "NO_DECL" },
1106 { GLV_COMPILING, "COMPILING" },
1107 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1108 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1109 { 0, NULL }
1110};
1111#endif
1112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113/*
Ernie Raelee865f32023-09-29 19:53:55 +02001114 * Fill in "lp" using "root". This is used in a special case when
1115 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1116 *
1117 * This is typically called with "lval_root" as "root". For a class, find
1118 * the name from lp in the class from root, fill in lval_T if found. For a
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001119 * complex type, list/tuple/dict use it as the result; just put the root into
Ernie Raelee865f32023-09-29 19:53:55 +02001120 * ll_tv.
1121 *
1122 * "lval_root" is a hack used during run-time/instr-execution to provide the
1123 * starting point for "get_lval()" to traverse a chain of indexes. In some
1124 * cases get_lval sees a bare name and uses this function to populate the
1125 * lval_T.
1126 *
1127 * For setting up "lval_root" (currently only used with lockvar)
1128 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1129 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1130 */
1131 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001132fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001133{
1134#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001135 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1136 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001137#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001138 if (lr->lr_tv == NULL)
1139 return;
Ernie Rael64885642023-10-04 20:16:22 +02001140 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001141 {
Ernie Rael64885642023-10-04 20:16:22 +02001142 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001143 {
1144 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001145 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001146 int m_idx;
1147 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1148 lp->ll_name_end - lp->ll_name, &m_idx);
1149 if (m != NULL)
1150 {
1151 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001152 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001153 lp->ll_oi = m_idx;
1154 lp->ll_valtype = m->ocm_type;
1155 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1156#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001157 ch_log(NULL, "LKVAR: ... class member %s.%s",
1158 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001159#endif
1160 return;
1161 }
1162 }
1163 }
1164
1165#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001166 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001167#endif
Ernie Rael64885642023-10-04 20:16:22 +02001168 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001169 lp->ll_is_root = TRUE;
1170}
1171
1172/*
Ernie Rael64885642023-10-04 20:16:22 +02001173 * Check if the class has permission to access the member.
1174 * Returns OK or FAIL.
1175 */
1176 static int
1177get_lval_check_access(
1178 class_T *cl_exec, // executing class, NULL if :def or script level
1179 class_T *cl, // class which contains the member
1180 ocmember_T *om, // member being accessed
1181 char_u *p, // char after member name
1182 int flags) // GLV flags to check if writing to lval
1183{
1184#ifdef LOG_LOCKVAR
1185 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1186 (void*)cl_exec, (void*)cl, *p);
1187#endif
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001188 if (cl_exec != NULL && cl_exec == cl)
1189 return OK;
1190
1191 char *msg = NULL;
1192 switch (om->ocm_access)
Ernie Rael64885642023-10-04 20:16:22 +02001193 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001194 case VIM_ACCESS_PRIVATE:
1195 msg = e_cannot_access_protected_variable_str;
1196 break;
1197 case VIM_ACCESS_READ:
1198 // If [idx] or .key following, read only OK.
1199 if (*p == '[' || *p == '.')
Ernie Raele6c9aa52023-10-06 19:55:52 +02001200 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001201 if ((flags & GLV_READ_ONLY) == 0)
1202 {
1203 if (IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001204 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001205 if (om->ocm_type->tt_type == VAR_OBJECT)
1206 semsg(_(e_enumvalue_str_cannot_be_modified),
1207 cl->class_name, om->ocm_name);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001208 else
1209 msg = e_variable_is_not_writable_str;
1210 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001211 else
1212 msg = e_variable_is_not_writable_str;
1213 }
1214 break;
1215 case VIM_ACCESS_ALL:
1216 break;
1217 }
1218 if (msg != NULL)
1219 {
1220 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1221 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02001222 }
1223 return OK;
1224}
1225
1226/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001227 * Get lval information for a variable imported from script "imp_sid". On
1228 * success, updates "lp" with the variable name, type, script ID and typval.
1229 * The variable name starts at or after "p".
1230 * If "rettv" is not NULL it points to the value to be assigned. This used to
1231 * match the rhs and lhs types.
1232 * Returns a pointer to the character after the variable name if the imported
1233 * variable is valid and writable.
1234 * Returns NULL if the variable is not exported or typval is not found or the
1235 * rhs type doesn't match the lhs type or the variable is not writable.
1236 */
1237 static char_u *
1238get_lval_imported(
1239 lval_T *lp,
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001240 scid_T imp_sid,
1241 char_u *p,
1242 dictitem_T **dip,
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001243 int fne_flags)
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001244{
1245 ufunc_T *ufunc;
1246 type_T *type = NULL;
1247 int cc;
1248 int rc = FAIL;
1249
1250 p = skipwhite(p);
1251
1252 import_check_sourced_sid(&imp_sid);
1253 lp->ll_sid = imp_sid;
1254 lp->ll_name = p;
1255 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1256 lp->ll_name_end = p;
1257
1258 // check the item is exported
1259 cc = *p;
1260 *p = NUL;
1261 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1262 TRUE) == -1)
1263 goto failed;
1264
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001265 // Get the typval for the exported item
1266 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1267 if (ht == NULL)
1268 goto failed;
1269
1270 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1271 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001272 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001273 goto success;
1274
1275 *dip = di;
1276
1277 // Check whether the variable is writable.
1278 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1279 if (sv != NULL && sv->sv_const != 0)
1280 {
1281 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1282 goto failed;
1283 }
1284
1285 // check whether variable is locked
1286 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1287 goto failed;
1288
1289 lp->ll_tv = &di->di_tv;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001290 lp->ll_valtype = type;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001291
1292success:
1293 rc = OK;
1294
1295failed:
1296 *p = cc;
1297 return rc == OK ? p : NULL;
1298}
1299
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001300typedef enum {
1301 GLV_FAIL,
1302 GLV_OK,
1303 GLV_STOP
1304} glv_status_T;
1305
1306/*
1307 * Get an Dict lval variable that can be assigned a value to: "name",
1308 * "name[expr]", "name[expr][expr]", "name.key", "name.key[expr]" etc.
1309 * "name" points to the start of the name.
1310 * If "rettv" is not NULL it points to the value to be assigned.
1311 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1312 * wrong; must end in space or cmd separator.
1313 *
1314 * flags:
1315 * GLV_QUIET: do not give error messages
1316 * GLV_READ_ONLY: will not change the variable
1317 * GLV_NO_AUTOLOAD: do not use script autoloading
1318 *
1319 * The Dict is returned in 'lp'. Returns GLV_OK on success and GLV_FAIL on
1320 * failure. Returns GLV_STOP to stop processing the characters following
1321 * 'key_end'.
1322 */
1323 static int
1324get_lval_dict_item(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001325 lval_T *lp,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001326 char_u *name,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001327 char_u *key,
1328 int len,
1329 char_u **key_end,
1330 typval_T *var1,
1331 int flags,
1332 int unlet,
1333 typval_T *rettv)
1334{
1335 int quiet = flags & GLV_QUIET;
1336 char_u *p = *key_end;
1337
1338 if (len == -1)
1339 {
1340 // "[key]": get key from "var1"
1341 key = tv_get_string_chk(var1); // is number or string
1342 if (key == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001343 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001344 }
1345 lp->ll_list = NULL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001346 lp->ll_list = NULL;
1347 lp->ll_blob = NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001348 lp->ll_object = NULL;
1349 lp->ll_class = NULL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001350 lp->ll_tuple = NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001351
1352 // a NULL dict is equivalent with an empty dict
1353 if (lp->ll_tv->vval.v_dict == NULL)
1354 {
1355 lp->ll_tv->vval.v_dict = dict_alloc();
1356 if (lp->ll_tv->vval.v_dict == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001357 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001358 ++lp->ll_tv->vval.v_dict->dv_refcount;
1359 }
1360 lp->ll_dict = lp->ll_tv->vval.v_dict;
1361
1362 lp->ll_di = dict_find(lp->ll_dict, key, len);
1363
1364 // When assigning to a scope dictionary check that a function and
1365 // variable name is valid (only variable name unless it is l: or
1366 // g: dictionary). Disallow overwriting a builtin function.
1367 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1368 {
1369 int prevval;
1370
1371 if (len != -1)
1372 {
1373 prevval = key[len];
1374 key[len] = NUL;
1375 }
1376 else
1377 prevval = 0; // avoid compiler warning
1378 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1379 && (rettv->v_type == VAR_FUNC
1380 || rettv->v_type == VAR_PARTIAL)
1381 && var_wrong_func_name(key, lp->ll_di == NULL))
1382 || !valid_varname(key, -1, TRUE);
1383 if (len != -1)
1384 key[len] = prevval;
1385 if (wrong)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001386 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001387 }
1388
1389 if (lp->ll_valtype != NULL)
1390 // use the type of the member
1391 lp->ll_valtype = lp->ll_valtype->tt_member;
1392
1393 if (lp->ll_di == NULL)
1394 {
1395 // Can't add "v:" or "a:" variable.
1396 if (lp->ll_dict == get_vimvar_dict()
1397 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1398 {
1399 semsg(_(e_illegal_variable_name_str), name);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001400 return GLV_FAIL;
1401 }
1402
1403 // Key does not exist in dict: may need to add it.
1404 if (*p == '[' || *p == '.' || unlet)
1405 {
1406 if (!quiet)
1407 semsg(_(e_key_not_present_in_dictionary_str), key);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001408 return GLV_FAIL;
1409 }
1410 if (len == -1)
1411 lp->ll_newkey = vim_strsave(key);
1412 else
1413 lp->ll_newkey = vim_strnsave(key, len);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001414 if (lp->ll_newkey == NULL)
1415 p = NULL;
1416
1417 *key_end = p;
1418 return GLV_STOP;
1419 }
1420 // existing variable, need to check if it can be changed
1421 else if ((flags & GLV_READ_ONLY) == 0
1422 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1423 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001424 return GLV_FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001425
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001426 lp->ll_tv = &lp->ll_di->di_tv;
1427
1428 return GLV_OK;
1429}
1430
1431/*
1432 * Get an blob lval variable that can be assigned a value to: "name",
1433 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1434 *
1435 * 'var1' specifies the starting blob index and 'var2' specifies the ending
1436 * blob index. If the first index is not specified in a range, then 'empty1'
1437 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1438 * invalid indexes.
1439 *
1440 * The blob is returned in 'lp'. Returns OK on success and FAIL on failure.
1441 */
1442 static int
1443get_lval_blob(
1444 lval_T *lp,
1445 typval_T *var1,
1446 typval_T *var2,
1447 int empty1,
1448 int quiet)
1449{
1450 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1451
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001452 lp->ll_list = NULL;
1453 lp->ll_dict = NULL;
1454 lp->ll_object = NULL;
1455 lp->ll_class = NULL;
1456 lp->ll_tuple = NULL;
1457
1458 // Get the number and item for the only or first index of a List or Tuple.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001459 if (empty1)
1460 lp->ll_n1 = 0;
1461 else
1462 // is number or string
1463 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001464
1465 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001466 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001467 if (lp->ll_range && !lp->ll_empty2)
1468 {
1469 lp->ll_n2 = (long)tv_get_number(var2);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001470 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL)
1471 return FAIL;
1472 }
1473
1474 if (!lp->ll_range)
1475 // Indexing a single byte in a blob. So the rhs type is a
1476 // number.
1477 lp->ll_valtype = &t_number;
1478
1479 lp->ll_blob = lp->ll_tv->vval.v_blob;
1480 lp->ll_tv = NULL;
1481
1482 return OK;
1483}
1484
1485/*
1486 * Get a List lval variable that can be assigned a value to: "name",
1487 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1488 *
1489 * 'var1' specifies the starting List index and 'var2' specifies the ending
1490 * List index. If the first index is not specified in a range, then 'empty1'
1491 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1492 * invalid indexes.
1493 *
1494 * The List is returned in 'lp'. Returns OK on success and FAIL on failure.
1495 */
1496 static int
1497get_lval_list(
1498 lval_T *lp,
1499 typval_T *var1,
1500 typval_T *var2,
1501 int empty1,
1502 int flags,
1503 int quiet)
1504{
1505 /*
1506 * Get the number and item for the only or first index of the List.
1507 */
1508 if (empty1)
1509 lp->ll_n1 = 0;
1510 else
1511 // is number or string
1512 lp->ll_n1 = (long)tv_get_number(var1);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001513
1514 lp->ll_dict = NULL;
1515 lp->ll_object = NULL;
1516 lp->ll_class = NULL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001517 lp->ll_tuple = NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001518 lp->ll_list = lp->ll_tv->vval.v_list;
1519 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1520 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
1521 if (lp->ll_li == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001522 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001523
1524 if (lp->ll_valtype != NULL && !lp->ll_range)
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01001525 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001526 // use the type of the member
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01001527 if (lp->ll_valtype->tt_member != NULL)
1528 lp->ll_valtype = lp->ll_valtype->tt_member;
1529 else
1530 // If the LHS member type is not known (VAR_ANY), then get it from
1531 // the list item (after indexing)
1532 lp->ll_valtype = typval2type(&lp->ll_li->li_tv, get_copyID(),
1533 &lp->ll_type_list, TVTT_DO_MEMBER);
1534
1535 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001536
1537 /*
1538 * May need to find the item or absolute index for the second
1539 * index of a range.
1540 * When no index given: "lp->ll_empty2" is TRUE.
1541 * Otherwise "lp->ll_n2" is set to the second index.
1542 */
1543 if (lp->ll_range && !lp->ll_empty2)
1544 {
1545 lp->ll_n2 = (long)tv_get_number(var2);
1546 // is number or string
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001547 if (check_range_index_two(lp->ll_list,
1548 &lp->ll_n1, lp->ll_li, &lp->ll_n2, quiet) == FAIL)
1549 return FAIL;
1550 }
1551
1552 lp->ll_tv = &lp->ll_li->li_tv;
1553
1554 return OK;
1555}
1556
1557/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001558 * Get a tuple lval variable that can be assigned a value to: "name",
1559 * "na{me}", "name[expr]", "name[expr][expr]", etc.
1560 *
1561 * 'idx' specifies the tuple index.
1562 * If 'quiet' is TRUE, then error messages are not displayed for an invalid
1563 * index.
1564 *
1565 * The typval is returned in 'lp'. Returns GLV_OK on success and GLV_FAIL on
1566 * failure.
1567 */
1568 static int
1569get_lval_tuple(
1570 lval_T *lp,
1571 typval_T *idx,
1572 int quiet)
1573{
1574 // is number or string
1575 lp->ll_n1 = (long)tv_get_number(idx);
1576
1577 lp->ll_list = NULL;
1578 lp->ll_dict = NULL;
1579 lp->ll_blob = NULL;
1580 lp->ll_object = NULL;
1581 lp->ll_class = NULL;
1582
1583 lp->ll_tuple = lp->ll_tv->vval.v_tuple;
1584 lp->ll_tv = tuple_find(lp->ll_tuple, lp->ll_n1);
1585 if (lp->ll_tv == NULL)
1586 {
1587 if (!quiet)
1588 semsg(_(e_tuple_index_out_of_range_nr), lp->ll_n1);
1589 return GLV_FAIL;
1590 }
1591
1592 // use the type of the member
1593 if (lp->ll_valtype != NULL)
1594 {
1595 if (lp->ll_valtype != NULL
1596 && lp->ll_valtype->tt_type == VAR_TUPLE
1597 && lp->ll_valtype->tt_argcount == 1)
1598 {
1599 // a variadic tuple or a single item tuple
1600 if (lp->ll_valtype->tt_flags & TTFLAG_VARARGS)
1601 lp->ll_valtype = lp->ll_valtype->tt_args[0]->tt_member;
1602 else
1603 lp->ll_valtype = lp->ll_valtype->tt_args[0];
1604 }
1605 else
1606 // If the LHS member type is not known (VAR_ANY), then get it from
1607 // the tuple item (after indexing)
1608 lp->ll_valtype = typval2type(lp->ll_tv, get_copyID(),
1609 &lp->ll_type_list, TVTT_DO_MEMBER);
1610 }
1611
1612 return GLV_OK;
1613}
1614
1615/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001616 * Get a class or object lval method in class "cl". The 'key' argument points
1617 * to the method name and 'key_end' points to the character after 'key'.
1618 * 'v_type' is VAR_CLASS or VAR_OBJECT.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001619 *
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001620 * The method index, method function pointer and method type are returned in
1621 * "lp".
1622 */
1623 static void
1624get_lval_oc_method(
1625 lval_T *lp,
1626 class_T *cl,
1627 char_u *key,
1628 char_u *key_end,
1629 vartype_T v_type)
1630{
1631 // Look for a method with this name.
1632 // round 1: class functions (skipped for an object)
1633 // round 2: object methods
1634 for (int round = v_type == VAR_OBJECT ? 2 : 1; round <= 2; ++round)
1635 {
1636 int m_idx;
1637 ufunc_T *fp;
1638
1639 fp = method_lookup(cl, round == 1 ? VAR_CLASS : VAR_OBJECT,
1640 key, key_end - key, &m_idx);
1641 lp->ll_oi = m_idx;
1642 if (fp != NULL)
1643 {
1644 lp->ll_ufunc = fp;
1645 lp->ll_valtype = fp->uf_func_type;
1646 break;
1647 }
1648 }
1649}
1650
1651/*
1652 * Get a class or object lval variable in class "cl". The "key" argument
1653 * points to the variable name and "key_end" points to the character after
1654 * "key". "v_type" is VAR_CLASS or VAR_OBJECT. "cl_exec" is the class that is
1655 * executing, or NULL.
1656 *
1657 * The variable index, typval and type are returned in "lp". Returns FAIL if
1658 * the variable is not writable. Otherwise returns OK.
1659 */
1660 static int
1661get_lval_oc_variable(
1662 lval_T *lp,
1663 class_T *cl,
1664 char_u *key,
1665 char_u *key_end,
1666 vartype_T v_type,
1667 class_T *cl_exec,
1668 int flags)
1669{
1670 int m_idx;
1671 ocmember_T *om;
1672
1673 om = member_lookup(cl, v_type, key, key_end - key, &m_idx);
1674 lp->ll_oi = m_idx;
1675 if (om == NULL)
1676 return OK;
1677
1678 // Check variable is accessible
1679 if (get_lval_check_access(cl_exec, cl, om, key_end, flags) == FAIL)
1680 return FAIL;
1681
1682 // When lhs is used to modify the variable, check it is not a read-only
1683 // variable.
1684 if ((flags & GLV_READ_ONLY) == 0 && (*key_end != '.' && *key_end != '[')
1685 && oc_var_check_ro(cl, om))
1686 return FAIL;
1687
1688 lp->ll_valtype = om->ocm_type;
1689
1690 if (v_type == VAR_OBJECT)
1691 lp->ll_tv = ((typval_T *)(lp->ll_tv->vval.v_object + 1)) + m_idx;
1692 else
1693 lp->ll_tv = &cl->class_members_tv[m_idx];
1694
1695 return OK;
1696}
1697
1698/*
1699 * Get a Class or Object lval variable or method that can be assigned a value
1700 * to: "name", "name.key", "name.key[expr]" etc.
1701 *
1702 * The 'key' argument points to the member name and 'key_end' points to the
1703 * character after 'key'. 'v_type' is VAR_CLASS or VAR_OBJECT. 'cl_exec' is
1704 * the class that is executing, or NULL. If 'quiet' is TRUE, then error
1705 * messages are not displayed for invalid indexes.
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001706 *
1707 * The Class or Object is returned in 'lp'. Returns OK on success and FAIL on
1708 * failure.
1709 */
1710 static int
1711get_lval_class_or_obj(
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001712 lval_T *lp,
1713 char_u *key,
1714 char_u *key_end,
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001715 vartype_T v_type,
1716 class_T *cl_exec,
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001717 int flags,
1718 int quiet)
1719{
1720 lp->ll_dict = NULL;
1721 lp->ll_list = NULL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001722 lp->ll_tuple = NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001723
1724 class_T *cl;
1725 if (v_type == VAR_OBJECT)
1726 {
1727 if (lp->ll_tv->vval.v_object == NULL)
1728 {
1729 if (!quiet)
1730 emsg(_(e_using_null_object));
1731 return FAIL;
1732 }
1733 cl = lp->ll_tv->vval.v_object->obj_class;
1734 lp->ll_object = lp->ll_tv->vval.v_object;
1735 }
1736 else
1737 {
1738 cl = lp->ll_tv->vval.v_class;
1739 lp->ll_object = NULL;
1740 }
1741 lp->ll_class = cl;
1742
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001743 if (cl == NULL)
1744 // TODO: what if class is NULL?
1745 return OK;
1746
1747 lp->ll_valtype = NULL;
1748
1749 if (flags & GLV_PREFER_FUNC)
1750 get_lval_oc_method(lp, cl, key, key_end, v_type);
1751
1752 // Look for object/class member variable
1753 if (lp->ll_valtype == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001754 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001755 if (get_lval_oc_variable(lp, cl, key, key_end, v_type, cl_exec, flags)
1756 == FAIL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001757 return FAIL;
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001758 }
1759
1760 if (lp->ll_valtype == NULL)
1761 {
1762 member_not_found_msg(cl, v_type, key, key_end - key);
1763 return FAIL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001764 }
1765
1766 return OK;
1767}
1768
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001769/*
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001770 * Check whether dot (".") is allowed after the variable "name" with type
1771 * "v_type". Only Dict, Class and Object types support a dot after the name.
1772 * Returns TRUE if dot is allowed after the name.
1773 */
1774 static int
1775dot_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1776{
1777 if (v_type != VAR_DICT && v_type != VAR_OBJECT && v_type != VAR_CLASS)
1778 {
1779 if (!quiet)
1780 semsg(_(e_dot_not_allowed_after_str_str),
1781 vartype_name(v_type), name);
1782 return FALSE;
1783 }
1784
1785 return TRUE;
1786}
1787
1788/*
1789 * Check whether left bracket ("[") is allowed after the variable "name" with
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001790 * type "v_type". Only Dict, List, Tuple and Blob types support a bracket
1791 * after the variable name. Returns TRUE if bracket is allowed after the name.
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001792 */
1793 static int
1794bracket_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1795{
1796 if (v_type == VAR_CLASS || v_type == VAR_OBJECT)
1797 {
1798 if (!quiet)
1799 semsg(_(e_index_not_allowed_after_str_str),
1800 vartype_name(v_type), name);
1801 return FALSE;
1802 }
1803
1804 return TRUE;
1805}
1806
1807/*
1808 * Check whether the variable "name" with type "v_type" can be followed by an
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001809 * index. Only Dict, List, Tuple, Blob, Object and Class types support
1810 * indexing. Returns TRUE if indexing is allowed after the name.
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001811 */
1812 static int
1813index_allowed_after_type(char_u *name, vartype_T v_type, int quiet)
1814{
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001815 if (v_type != VAR_LIST
1816 && v_type != VAR_TUPLE
1817 && v_type != VAR_DICT
1818 && v_type != VAR_BLOB
1819 && v_type != VAR_OBJECT
1820 && v_type != VAR_CLASS)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001821 {
1822 if (!quiet)
1823 semsg(_(e_index_not_allowed_after_str_str),
1824 vartype_name(v_type), name);
1825 return FALSE;
1826 }
1827
1828 return TRUE;
1829}
1830
1831/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001832 * Get the lval of a list/tuple/dict/blob/object/class subitem starting at "p".
1833 * Loop until no more [idx] or .key is following.
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001834 *
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001835 * If "rettv" is not NULL it points to the value to be assigned.
1836 * "unlet" is TRUE for ":unlet".
1837 *
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001838 * Returns a pointer to the character after the subscript on success or NULL on
1839 * failure.
1840 */
1841 static char_u *
1842get_lval_subscript(
1843 lval_T *lp,
1844 char_u *p,
1845 char_u *name,
1846 typval_T *rettv,
1847 hashtab_T *ht,
1848 dictitem_T *v,
1849 int unlet,
1850 int flags, // GLV_ values
1851 class_T *cl_exec)
1852{
1853 int vim9script = in_vim9script();
1854 int quiet = flags & GLV_QUIET;
1855 char_u *key = NULL;
1856 int len;
1857 typval_T var1;
1858 typval_T var2;
1859 int empty1 = FALSE;
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001860 int rc = FAIL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001861
1862 /*
1863 * Loop until no more [idx] or .key is following.
1864 */
1865 var1.v_type = VAR_UNKNOWN;
1866 var2.v_type = VAR_UNKNOWN;
1867
1868 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
1869 {
1870 vartype_T v_type = lp->ll_tv->v_type;
1871
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001872 if (*p == '.' && !dot_allowed_after_type(name, v_type, quiet))
1873 goto done;
1874
1875 if (*p == '[' && !bracket_allowed_after_type(name, v_type, quiet))
1876 goto done;
1877
1878 if (!index_allowed_after_type(name, v_type, quiet))
1879 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001880
1881 // A NULL list/blob works like an empty list/blob, allocate one now.
1882 int r = OK;
1883 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1884 r = rettv_list_alloc(lp->ll_tv);
1885 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
1886 r = rettv_blob_alloc(lp->ll_tv);
1887 if (r == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001888 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001889
1890 if (lp->ll_range)
1891 {
1892 if (!quiet)
1893 emsg(_(e_slice_must_come_last));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001894 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001895 }
1896#ifdef LOG_LOCKVAR
1897 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1898 vartype_name(v_type));
1899#endif
1900
1901 if (vim9script && lp->ll_valtype == NULL
1902 && v != NULL
1903 && lp->ll_tv == &v->di_tv
1904 && ht != NULL && ht == get_script_local_ht())
1905 {
1906 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
1907
1908 // Vim9 script local variable: get the type
1909 if (sv != NULL)
1910 {
1911 lp->ll_valtype = sv->sv_type;
1912#ifdef LOG_LOCKVAR
1913 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1914 vartype_name(lp->ll_valtype->tt_type));
1915#endif
1916 }
1917 }
1918
1919 len = -1;
1920 if (*p == '.')
1921 {
1922 key = p + 1;
1923
1924 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1925 ;
1926 if (len == 0)
1927 {
1928 if (!quiet)
1929 emsg(_(e_cannot_use_empty_key_for_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001930 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001931 }
1932 p = key + len;
1933 }
1934 else
1935 {
1936 // Get the index [expr] or the first index [expr: ].
1937 p = skipwhite(p + 1);
1938 if (*p == ':')
1939 empty1 = TRUE;
1940 else
1941 {
1942 empty1 = FALSE;
1943 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001944 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001945 if (tv_get_string_chk(&var1) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001946 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001947 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001948 p = skipwhite(p);
1949 }
1950
1951 // Optionally get the second index [ :expr].
1952 if (*p == ':')
1953 {
1954 if (v_type == VAR_DICT)
1955 {
1956 if (!quiet)
1957 emsg(_(e_cannot_slice_dictionary));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001958 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001959 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001960 if (v_type == VAR_TUPLE)
1961 {
1962 if (!quiet)
1963 emsg(_(e_cannot_slice_tuple));
1964 goto done;
1965 }
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001966 if (rettv != NULL
1967 && !(rettv->v_type == VAR_LIST
1968 && rettv->vval.v_list != NULL)
1969 && !(rettv->v_type == VAR_BLOB
1970 && rettv->vval.v_blob != NULL))
1971 {
1972 if (!quiet)
1973 emsg(_(e_slice_requires_list_or_blob_value));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001974 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001975 }
1976 p = skipwhite(p + 1);
1977 if (*p == ']')
1978 lp->ll_empty2 = TRUE;
1979 else
1980 {
1981 lp->ll_empty2 = FALSE;
1982 // recursive!
1983 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001984 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001985 if (tv_get_string_chk(&var2) == NULL)
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001986 // not a number or string
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001987 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001988 }
1989 lp->ll_range = TRUE;
1990 }
1991 else
1992 lp->ll_range = FALSE;
1993
1994 if (*p != ']')
1995 {
1996 if (!quiet)
1997 emsg(_(e_missing_closing_square_brace));
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02001998 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02001999 }
2000
2001 // Skip to past ']'.
2002 ++p;
2003 }
2004#ifdef LOG_LOCKVAR
2005 if (len == -1)
2006 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
2007 empty1 ? ":" : (char*)tv_get_string(&var1));
2008 else
2009 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
2010#endif
2011
2012 if (v_type == VAR_DICT)
2013 {
2014 glv_status_T glv_status;
2015
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02002016 glv_status = get_lval_dict_item(lp, name, key, len, &p, &var1,
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002017 flags, unlet, rettv);
2018 if (glv_status == GLV_FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002019 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002020 if (glv_status == GLV_STOP)
2021 break;
2022 }
2023 else if (v_type == VAR_BLOB)
2024 {
2025 if (get_lval_blob(lp, &var1, &var2, empty1, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002026 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002027
2028 break;
2029 }
2030 else if (v_type == VAR_LIST)
2031 {
2032 if (get_lval_list(lp, &var1, &var2, empty1, flags, quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002033 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002034 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002035 else if (v_type == VAR_TUPLE)
2036 {
2037 if (get_lval_tuple(lp, &var1, quiet) == FAIL)
2038 goto done;
2039 }
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002040 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
2041 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02002042 if (get_lval_class_or_obj(lp, key, p, v_type, cl_exec, flags,
2043 quiet) == FAIL)
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002044 goto done;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002045 }
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002046
2047 clear_tv(&var1);
2048 clear_tv(&var2);
2049 var1.v_type = VAR_UNKNOWN;
2050 var2.v_type = VAR_UNKNOWN;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002051 }
2052
Yegappan Lakshmanan038be272025-03-26 18:46:21 +01002053 if (lp->ll_tuple != NULL && (flags & GLV_READ_ONLY) == 0)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002054 {
2055 if (!quiet)
2056 emsg(_(e_tuple_is_immutable));
2057 goto done;
2058 }
2059
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002060 rc = OK;
2061
2062done:
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002063 clear_tv(&var1);
Yegappan Lakshmanandbac0da2024-05-25 20:23:54 +02002064 clear_tv(&var2);
2065 return rc == OK ? p : NULL;
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002066}
2067
2068/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 * Get an lval: variable, Dict item or List item that can be assigned a value
2070 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2071 * "name.key", "name.key[expr]" etc.
2072 * Indexing only works if "name" is an existing List or Dictionary.
2073 * "name" points to the start of the name.
2074 * If "rettv" is not NULL it points to the value to be assigned.
2075 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2076 * wrong; must end in space or cmd separator.
2077 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002078 * flags:
2079 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002080 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002081 * GLV_NO_AUTOLOAD: do not use script autoloading
2082 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002087 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002088get_lval(
2089 char_u *name,
2090 typval_T *rettv,
2091 lval_T *lp,
2092 int unlet,
2093 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002094 int flags, // GLV_ values
2095 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 char_u *expr_start, *expr_end;
2099 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002100 dictitem_T *v = NULL;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01002101 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002102 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02002103 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00002104 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02002105 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106
Ernie Raelee865f32023-09-29 19:53:55 +02002107#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002108 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02002109 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02002110 else
Ernie Rael4c8da022023-10-11 21:35:11 +02002111 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
2112 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02002113 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02002114 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02002115 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02002116#endif
2117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002118 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02002119 CLEAR_POINTER(lp);
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002120 ga_init2(&lp->ll_type_list, sizeof(type_T *), 10);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01002122 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01002124 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002125 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02002126 lp->ll_name_end = find_name_end(name, NULL, NULL,
2127 FNE_INCL_BR | fne_flags);
2128 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129 }
2130
Bram Moolenaara749a422022-02-12 19:52:25 +00002131 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00002132 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00002133 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
2134 {
2135 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
2136 return NULL;
2137 }
2138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002139 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002140 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 if (expr_start != NULL)
2143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002144 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01002145 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 && *p != '[' && *p != '.')
2147 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002148 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 return NULL;
2150 }
2151
2152 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2153 if (lp->ll_exp_name == NULL)
2154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002155 // Report an invalid expression in braces, unless the
2156 // expression evaluation has been cancelled due to an
2157 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 if (!aborting() && !quiet)
2159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002160 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002161 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 return NULL;
2163 }
2164 }
2165 lp->ll_name = lp->ll_exp_name;
2166 }
2167 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002169 lp->ll_name = name;
2170
Bram Moolenaar4525a572022-02-13 11:57:33 +00002171 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002173 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00002174 // However, "g:[key]" is indexing a dictionary.
2175 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002176 {
2177 --p;
2178 lp->ll_name_end = p;
2179 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002180 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002181 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002182 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02002183
Bram Moolenaar9510d222022-09-11 15:14:05 +01002184 if (is_scoped_variable(name))
2185 {
2186 semsg(_(e_cannot_use_type_with_this_variable_str), name);
2187 return NULL;
2188 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00002189 if (VIM_ISWHITE(*p))
2190 {
2191 semsg(_(e_no_white_space_allowed_before_colon_str), p);
2192 return NULL;
2193 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02002194 if (tp == p + 1 && !quiet)
2195 {
2196 semsg(_(e_white_space_required_after_str_str), ":", p);
2197 return NULL;
2198 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002199 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002200 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002201 semsg(_(e_using_type_not_in_script_context_str), p);
2202 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002203 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02002204 if (vim9script && (flags & GLV_NO_DECL) &&
2205 !(flags & GLV_FOR_LOOP))
2206 {
2207 // Using a type and not in a "var" declaration.
2208 semsg(_(e_trailing_characters_str), p);
2209 return NULL;
2210 }
2211
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00002212
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002213 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00002214 lp->ll_type = parse_type(&tp,
2215 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
2216 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01002217 if (lp->ll_type == NULL && !quiet)
2218 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02002219 lp->ll_name_end = tp;
2220 }
Ernie Raelee865f32023-09-29 19:53:55 +02002221 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 }
2223 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002224 if (lp->ll_name == NULL)
2225 return p;
2226
Bram Moolenaar62aec932022-01-29 21:45:34 +00002227 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002228 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002229 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002230 if (import != NULL)
2231 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002232 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002233 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002234 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00002235 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002236 }
2237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238
Ernie Rael0f058d12023-10-14 11:25:04 +02002239 // Without [idx] or .key we are done.
2240 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02002241 {
2242 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02002243 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02002245 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246
Ernie Rael0f058d12023-10-14 11:25:04 +02002247 if (vim9script && lval_root != NULL)
2248 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02002249 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002250 {
2251 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02002252 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02002253 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002254 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02002255 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002256 {
2257 cc = *p;
2258 *p = NUL;
2259 // When we would write to the variable pass &ht and prevent autoload.
2260 writing = !(flags & GLV_READ_ONLY);
2261 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002262 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002263 if (v == NULL && !quiet)
2264 semsg(_(e_undefined_variable_str), lp->ll_name);
2265 *p = cc;
2266 if (v == NULL)
2267 return NULL;
2268 lp->ll_tv = &v->di_tv;
2269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270
Bram Moolenaar4525a572022-02-13 11:57:33 +00002271 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002272 {
2273 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002274 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01002275 return NULL;
2276 }
2277
Yegappan Lakshmanan44cadaa2024-05-24 07:44:10 +02002278 // If the next character is a "." or a "[", then process the subitem.
2279 p = get_lval_subscript(lp, p, name, rettv, ht, v, unlet, flags, cl_exec);
2280 if (p == NULL)
2281 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002283 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
2284 {
2285 where_T where = WHERE_INIT;
2286
Hirohito Higashifbe4a8f2025-04-27 15:28:30 +02002287 // In a Vim9 script, do type check and make sure the variable is
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002288 // writable.
2289 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
2290 return NULL;
2291 }
2292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 return p;
2295}
2296
2297/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002298 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002301clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302{
2303 vim_free(lp->ll_exp_name);
2304 vim_free(lp->ll_newkey);
Yegappan Lakshmanan92195ae2024-12-22 14:44:35 +01002305 clear_type_list(&lp->ll_type_list);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306}
2307
2308/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002309 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002311 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2312 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002315set_var_lval(
2316 lval_T *lp,
2317 char_u *endp,
2318 typval_T *rettv,
2319 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002320 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
2321 char_u *op,
2322 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323{
2324 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326
2327 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002329 cc = *endp;
2330 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01002331 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02002332 return;
2333
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002334 if (lp->ll_blob != NULL)
2335 {
2336 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002337
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002338 if (op != NULL && *op != '=')
2339 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002340 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002341 return;
2342 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002343 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02002344 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002345
2346 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2347 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002348 if (lp->ll_empty2)
2349 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2350
Bram Moolenaar68452172021-04-12 21:21:02 +02002351 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
2352 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002353 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002354 }
2355 else
2356 {
2357 val = (int)tv_get_number_chk(rettv, &error);
2358 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02002359 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002360 }
2361 }
2362 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002364 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002366 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2367 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002368 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002369 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002370 *endp = cc;
2371 return;
2372 }
2373
Bram Moolenaarff697e62019-02-12 22:28:33 +01002374 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002375 di = NULL;
Christian Brabandt06774a22025-03-26 19:25:57 +01002376 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002377 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01002378 {
Ernie Raele75fde62023-12-21 17:18:54 +01002379 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002380 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002381 clear_tv(&tv);
2382 return;
2383 }
2384
Bram Moolenaar79518e22017-02-17 16:31:35 +01002385 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002386 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2387 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002388 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002389 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002390 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002391 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002394 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002395 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002396 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2397 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002398 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002399 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002400 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002401 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002402 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002404 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002405 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002406 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002407 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 else if (lp->ll_range)
2409 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002410 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2411 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002412 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002413 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002414 return;
2415 }
2416
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002417 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2418 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 }
2420 else
2421 {
2422 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002423 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002425 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2426 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002427 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002428 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002429 return;
2430 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002431
2432 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002433 && check_typval_arg_type(lp->ll_valtype, rettv,
2434 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002435 return;
2436
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 if (lp->ll_newkey != NULL)
2438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 if (op != NULL && *op != '=')
2440 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002441 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 return;
2443 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002444 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2445 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002446 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002448 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002449 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 if (di == NULL)
2451 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002452 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2453 {
2454 vim_free(di);
2455 return;
2456 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002458 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 else if (op != NULL && *op != '=')
2460 {
2461 tv_op(lp->ll_tv, rettv, op);
2462 return;
2463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 /*
2468 * Assign the value to the variable or list item.
2469 */
2470 if (copy)
2471 copy_tv(rettv, lp->ll_tv);
2472 else
2473 {
2474 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002475 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479}
2480
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002481/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002482 * Handle "blob1 += blob2".
2483 * Returns OK or FAIL.
2484 */
2485 static int
2486tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
2487{
2488 if (*op != '+' || tv2->v_type != VAR_BLOB)
2489 return FAIL;
2490
2491 // Blob += Blob
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002492 if (tv2->vval.v_blob == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002493 return OK;
2494
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002495 if (tv1->vval.v_blob == NULL)
2496 {
2497 tv1->vval.v_blob = tv2->vval.v_blob;
2498 ++tv1->vval.v_blob->bv_refcount;
2499 return OK;
2500 }
2501
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002502 blob_T *b1 = tv1->vval.v_blob;
2503 blob_T *b2 = tv2->vval.v_blob;
2504 int len = blob_len(b2);
2505
2506 for (int i = 0; i < len; i++)
2507 ga_append(&b1->bv_ga, blob_get(b2, i));
2508
2509 return OK;
2510}
2511
2512/*
2513 * Handle "list1 += list2".
2514 * Returns OK or FAIL.
2515 */
2516 static int
2517tv_op_list(typval_T *tv1, typval_T *tv2, char_u *op)
2518{
2519 if (*op != '+' || tv2->v_type != VAR_LIST)
2520 return FAIL;
2521
2522 // List += List
2523 if (tv2->vval.v_list == NULL)
2524 return OK;
2525
2526 if (tv1->vval.v_list == NULL)
2527 {
2528 tv1->vval.v_list = tv2->vval.v_list;
2529 ++tv1->vval.v_list->lv_refcount;
2530 }
2531 else
2532 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2533
2534 return OK;
2535}
2536
2537/*
2538 * Handle number operations:
2539 * nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
2540 *
2541 * Returns OK or FAIL.
2542 */
2543 static int
2544tv_op_number(typval_T *tv1, typval_T *tv2, char_u *op)
2545{
2546 varnumber_T n;
2547 int failed = FALSE;
2548
2549 n = tv_get_number(tv1);
2550 if (tv2->v_type == VAR_FLOAT)
2551 {
2552 float_T f = n;
2553
2554 if (*op == '%')
2555 return FAIL;
2556 switch (*op)
2557 {
2558 case '+': f += tv2->vval.v_float; break;
2559 case '-': f -= tv2->vval.v_float; break;
2560 case '*': f *= tv2->vval.v_float; break;
2561 case '/': f /= tv2->vval.v_float; break;
2562 }
2563 clear_tv(tv1);
2564 tv1->v_type = VAR_FLOAT;
2565 tv1->vval.v_float = f;
2566 }
2567 else
2568 {
2569 switch (*op)
2570 {
2571 case '+': n += tv_get_number(tv2); break;
2572 case '-': n -= tv_get_number(tv2); break;
2573 case '*': n *= tv_get_number(tv2); break;
2574 case '/': n = num_divide(n, tv_get_number(tv2), &failed); break;
2575 case '%': n = num_modulus(n, tv_get_number(tv2), &failed); break;
2576 }
2577 clear_tv(tv1);
2578 tv1->v_type = VAR_NUMBER;
2579 tv1->vval.v_number = n;
2580 }
2581
2582 return failed ? FAIL : OK;
2583}
2584
2585/*
2586 * Handle "str1 .= str2"
2587 * Returns OK or FAIL.
2588 */
2589 static int
2590tv_op_string(typval_T *tv1, typval_T *tv2, char_u *op UNUSED)
2591{
2592 char_u numbuf[NUMBUFLEN];
2593 char_u *s;
2594
2595 if (tv2->v_type == VAR_FLOAT)
2596 return FAIL;
2597
2598 // str .= str
2599 s = tv_get_string(tv1);
2600 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
2601 clear_tv(tv1);
2602 tv1->v_type = VAR_STRING;
2603 tv1->vval.v_string = s;
2604
2605 return OK;
2606}
2607
2608/*
2609 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2610 * and "tv1 .= tv2"
2611 * Returns OK or FAIL.
2612 */
2613 static int
2614tv_op_nr_or_string(typval_T *tv1, typval_T *tv2, char_u *op)
2615{
2616 if (tv2->v_type == VAR_LIST)
2617 return FAIL;
2618
2619 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
2620 return tv_op_number(tv1, tv2, op);
2621
2622 return tv_op_string(tv1, tv2, op);
2623}
2624
2625/*
2626 * Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
2627 * Returns OK or FAIL.
2628 */
2629 static int
2630tv_op_float(typval_T *tv1, typval_T *tv2, char_u *op)
2631{
2632 float_T f;
2633
2634 if (*op == '%' || *op == '.'
2635 || (tv2->v_type != VAR_FLOAT
2636 && tv2->v_type != VAR_NUMBER
2637 && tv2->v_type != VAR_STRING))
2638 return FAIL;
2639
2640 if (tv2->v_type == VAR_FLOAT)
2641 f = tv2->vval.v_float;
2642 else
2643 f = tv_get_number(tv2);
2644 switch (*op)
2645 {
2646 case '+': tv1->vval.v_float += f; break;
2647 case '-': tv1->vval.v_float -= f; break;
2648 case '*': tv1->vval.v_float *= f; break;
2649 case '/': tv1->vval.v_float /= f; break;
2650 }
2651
2652 return OK;
2653}
2654
2655/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002656 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2657 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002658 * Returns OK or FAIL.
2659 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002660 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002661tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002662{
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002663 // Can't do anything with a Funcref or Dict on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002664 // v:true and friends only work with "..=".
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002665 if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
2666 || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL)
2667 && *op != '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002669 semsg(_(e_wrong_variable_type_for_str_equal), op);
2670 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 }
2672
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002673 int retval = FAIL;
2674 switch (tv1->v_type)
2675 {
2676 case VAR_UNKNOWN:
2677 case VAR_ANY:
2678 case VAR_VOID:
2679 case VAR_DICT:
2680 case VAR_FUNC:
2681 case VAR_PARTIAL:
2682 case VAR_BOOL:
2683 case VAR_SPECIAL:
2684 case VAR_JOB:
2685 case VAR_CHANNEL:
2686 case VAR_INSTR:
2687 case VAR_OBJECT:
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002688 case VAR_CLASS:
2689 case VAR_TYPEALIAS:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002690 case VAR_TUPLE:
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002691 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002692
2693 case VAR_BLOB:
2694 retval = tv_op_blob(tv1, tv2, op);
2695 break;
2696
2697 case VAR_LIST:
2698 retval = tv_op_list(tv1, tv2, op);
2699 break;
2700
2701 case VAR_NUMBER:
2702 case VAR_STRING:
2703 retval = tv_op_nr_or_string(tv1, tv2, op);
2704 break;
2705
2706 case VAR_FLOAT:
2707 retval = tv_op_float(tv1, tv2, op);
2708 break;
2709 }
2710
2711 if (retval != OK)
Ernie Raele75fde62023-12-21 17:18:54 +01002712 semsg(_(e_wrong_variable_type_for_str_equal), op);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002713
2714 return retval;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715}
2716
2717/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002718 * Evaluate the expression used in a ":for var in expr" command.
2719 * "arg" points to "var".
2720 * Set "*errp" to TRUE for an error, FALSE otherwise;
2721 * Return a pointer that holds the info. Null when there is an error.
2722 */
2723 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002724eval_for_line(
2725 char_u *arg,
2726 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002727 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002728 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002729{
Bram Moolenaar33570922005-01-25 22:26:29 +00002730 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002731 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002732 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002733 typval_T tv;
2734 list_T *l;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002735 tuple_T *tuple;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002736 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002738 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002739
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002740 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002741 if (fi == NULL)
2742 return NULL;
2743
Bram Moolenaar404557e2021-07-05 21:41:48 +02002744 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2745 &fi->fi_semicolon, FALSE);
2746 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747 return fi;
2748
Bram Moolenaar404557e2021-07-05 21:41:48 +02002749 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002750 if (expr[0] != 'i' || expr[1] != 'n'
2751 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002752 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002753 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2754 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2755 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002756 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002757 return fi;
2758 }
2759
2760 if (skip)
2761 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002762 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2763 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002764 {
2765 *errp = FALSE;
2766 if (!skip)
2767 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002768 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002769 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002770 l = tv.vval.v_list;
2771 if (l == NULL)
2772 {
2773 // a null list is like an empty list: do nothing
2774 clear_tv(&tv);
2775 }
2776 else
2777 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002779 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002781 // No need to increment the refcount, it's already set for
2782 // the list being used in "tv".
2783 fi->fi_list = l;
2784 list_add_watch(l, &fi->fi_lw);
2785 fi->fi_lw.lw_item = l->lv_first;
2786 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002787 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002788 else if (tv.v_type == VAR_TUPLE)
2789 {
2790 tuple = tv.vval.v_tuple;
2791 if (tuple == NULL)
2792 {
2793 // a null tuple is like an empty tuple: do nothing
2794 clear_tv(&tv);
2795 }
2796 else
2797 {
2798 // No need to increment the refcount, it's already set for
2799 // the tuple being used in "tv".
2800 fi->fi_tuple = tuple;
2801 fi->fi_tuple_idx = 0;
2802 }
2803 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002804 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002805 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002806 fi->fi_bi = 0;
2807 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002808 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002809 typval_T btv;
2810
2811 // Make a copy, so that the iteration still works when the
2812 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002814 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002815 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002816 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002817 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002818 else if (tv.v_type == VAR_STRING)
2819 {
2820 fi->fi_byte_idx = 0;
2821 fi->fi_string = tv.vval.v_string;
2822 tv.vval.v_string = NULL;
2823 if (fi->fi_string == NULL)
2824 fi->fi_string = vim_strsave((char_u *)"");
2825 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002826 else
2827 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002828 emsg(_(e_string_list_tuple_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002829 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 }
2831 }
2832 }
2833 if (skip)
2834 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002835 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002836
2837 return fi;
2838}
2839
2840/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002841 * Used when looping over a :for line, skip the "in expr" part.
2842 */
2843 void
2844skip_for_lines(void *fi_void, evalarg_T *evalarg)
2845{
2846 forinfo_T *fi = (forinfo_T *)fi_void;
2847 int i;
2848
2849 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002850 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002851}
2852
2853/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 * Use the first item in a ":for" list. Advance to the next.
2855 * Assign the values to the variable (list). "arg" points to the first one.
2856 * Return TRUE when a valid item was found, FALSE when at end of list or
2857 * something wrong.
2858 */
2859 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002860next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002861{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002862 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002864 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002865 ? (ASSIGN_FINAL
2866 // first round: error if variable exists
2867 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002868 | ASSIGN_NO_MEMBER_TYPE
2869 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002870 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002872 int skip_assign = in_vim9script() && arg[0] == '_'
2873 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002875 if (fi->fi_blob != NULL)
2876 {
2877 typval_T tv;
2878
2879 if (fi->fi_bi >= blob_len(fi->fi_blob))
2880 return FALSE;
2881 tv.v_type = VAR_NUMBER;
2882 tv.v_lock = VAR_FIXED;
2883 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2884 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002885 if (skip_assign)
2886 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002887 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002888 fi->fi_varcount, flag, NULL) == OK;
2889 }
2890
2891 if (fi->fi_string != NULL)
2892 {
2893 typval_T tv;
2894 int len;
2895
2896 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2897 if (len == 0)
2898 return FALSE;
2899 tv.v_type = VAR_STRING;
2900 tv.v_lock = VAR_FIXED;
2901 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2902 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002903 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002904 if (skip_assign)
2905 result = TRUE;
2906 else
2907 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002908 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002909 vim_free(tv.vval.v_string);
2910 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002911 }
2912
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002913 if (fi->fi_tuple != NULL)
2914 {
2915 typval_T tv;
2916
2917 if (fi->fi_tuple_idx >= TUPLE_LEN(fi->fi_tuple))
2918 return FALSE;
2919
2920 copy_tv(TUPLE_ITEM(fi->fi_tuple, fi->fi_tuple_idx), &tv);
2921 ++fi->fi_tuple_idx;
2922 ++fi->fi_bi;
2923 if (skip_assign)
2924 return TRUE;
2925 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2926 fi->fi_varcount, flag, NULL) == OK;
2927 }
2928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002929 item = fi->fi_lw.lw_item;
2930 if (item == NULL)
2931 result = FALSE;
2932 else
2933 {
2934 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002935 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002936 if (skip_assign)
2937 result = TRUE;
2938 else
2939 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002940 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941 }
2942 return result;
2943}
2944
2945/*
2946 * Free the structure used to store info used by ":for".
2947 */
2948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002949free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002950{
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002952
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002953 if (fi == NULL)
2954 return;
2955 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002956 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002957 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002958 list_unref(fi->fi_list);
2959 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002960 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002961 blob_unref(fi->fi_blob);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002962 else if (fi->fi_tuple != NULL)
2963 tuple_unref(fi->fi_tuple);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002964 else
2965 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002966 vim_free(fi);
2967}
2968
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002970set_context_for_expression(
2971 expand_T *xp,
2972 char_u *arg,
2973 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002975 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002977 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002979 if (cmdidx == CMD_let || cmdidx == CMD_var
2980 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981 {
2982 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002983 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002985 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002986 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002987 {
2988 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002989 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002990 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991 break;
2992 }
2993 return;
2994 }
2995 }
2996 else
2997 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2998 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 while ((xp->xp_pattern = vim_strpbrk(arg,
3000 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3001 {
3002 c = *xp->xp_pattern;
3003 if (c == '&')
3004 {
3005 c = xp->xp_pattern[1];
3006 if (c == '&')
3007 {
3008 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003009 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
3011 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003014 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3015 xp->xp_pattern += 2;
3016
3017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
3019 else if (c == '$')
3020 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003021 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 xp->xp_context = EXPAND_ENV_VARS;
3023 }
3024 else if (c == '=')
3025 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003026 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 xp->xp_context = EXPAND_EXPRESSION;
3028 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003029 else if (c == '#'
3030 && xp->xp_context == EXPAND_EXPRESSION)
3031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003032 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02003033 break;
3034 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003035 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 && xp->xp_context == EXPAND_FUNCTIONS
3037 && vim_strchr(xp->xp_pattern, '(') == NULL)
3038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003039 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 break;
3041 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003042 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003044 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 {
3046 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3047 if (c == '\\' && xp->xp_pattern[1] != NUL)
3048 ++xp->xp_pattern;
3049 xp->xp_context = EXPAND_NOTHING;
3050 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003051 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003053 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3055 /* skip */ ;
3056 xp->xp_context = EXPAND_NOTHING;
3057 }
3058 else if (c == '|')
3059 {
3060 if (xp->xp_pattern[1] == '|')
3061 {
3062 ++xp->xp_pattern;
3063 xp->xp_context = EXPAND_EXPRESSION;
3064 }
3065 else
3066 xp->xp_context = EXPAND_COMMANDS;
3067 }
3068 else
3069 xp->xp_context = EXPAND_EXPRESSION;
3070 }
3071 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003072 // Doesn't look like something valid, expand as an expression
3073 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003074 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 arg = xp->xp_pattern;
3076 if (*arg != NUL)
3077 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3078 /* skip */ ;
3079 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01003080
3081 // ":exe one two" completes "two"
3082 if ((cmdidx == CMD_execute
3083 || cmdidx == CMD_echo
3084 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01003085 || cmdidx == CMD_echomsg
3086 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01003087 && xp->xp_context == EXPAND_EXPRESSION)
3088 {
3089 for (;;)
3090 {
3091 char_u *n = skiptowhite(arg);
3092
3093 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
3094 break;
3095 arg = skipwhite(n);
3096 }
3097 }
3098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 xp->xp_pattern = arg;
3100}
3101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003103 * Return TRUE if "pat" matches "text".
3104 * Does not use 'cpo' and always uses 'magic'.
3105 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003106 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003107pattern_match(char_u *pat, char_u *text, int ic)
3108{
3109 int matches = FALSE;
3110 char_u *save_cpo;
3111 regmatch_T regmatch;
3112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003113 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003114 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01003115 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003116 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3117 if (regmatch.regprog != NULL)
3118 {
3119 regmatch.rm_ic = ic;
3120 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3121 vim_regfree(regmatch.regprog);
3122 }
3123 p_cpo = save_cpo;
3124 return matches;
3125}
3126
3127/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003128 * Handle a name followed by "(". Both for just "name(arg)" and for
3129 * "expr->name(arg)".
3130 * Returns OK or FAIL.
3131 */
3132 static int
3133eval_func(
3134 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02003135 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003136 char_u *name,
3137 int name_len,
3138 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003139 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003140 typval_T *basetv) // "expr" for "expr->name(arg)"
3141{
Bram Moolenaar32e35112020-05-14 22:41:15 +02003142 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003143 char_u *s = name;
3144 int len = name_len;
3145 partial_T *partial;
3146 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003147 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003148 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003149
3150 if (!evaluate)
3151 check_vars(s, len);
3152
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003153 // If "s" is the name of a variable of type VAR_FUNC
3154 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003155 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003156 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003158 // Need to make a copy, in case evaluating the arguments makes
3159 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003160 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01003161 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003162 ret = FAIL;
3163 else
3164 {
3165 funcexe_T funcexe;
3166
3167 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02003168 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003169 funcexe.fe_firstline = curwin->w_cursor.lnum;
3170 funcexe.fe_lastline = curwin->w_cursor.lnum;
3171 funcexe.fe_evaluate = evaluate;
3172 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003173 if (partial != NULL)
3174 {
3175 funcexe.fe_object = partial->pt_obj;
3176 if (funcexe.fe_object != NULL)
3177 ++funcexe.fe_object->obj_refcount;
3178 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003179 funcexe.fe_basetv = basetv;
3180 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003181 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003182 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003183 }
3184 vim_free(s);
3185
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003186 // If evaluate is FALSE rettv->v_type was not set in
3187 // get_func_tv, but it's needed in handle_subscript() to parse
3188 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003189 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3190 {
3191 rettv->vval.v_string = NULL;
3192 rettv->v_type = VAR_FUNC;
3193 }
3194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003195 // Stop the expression evaluation when immediately
3196 // aborting on error, or when an interrupt occurred or
3197 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003198 if (evaluate && aborting())
3199 {
3200 if (ret == OK)
3201 clear_tv(rettv);
3202 ret = FAIL;
3203 }
3204 return ret;
3205}
3206
3207/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003208 * After a NL, skip over empty lines and comment-only lines.
3209 */
3210 static char_u *
3211newline_skip_comments(char_u *arg)
3212{
3213 char_u *p = arg + 1;
3214
3215 for (;;)
3216 {
3217 p = skipwhite(p);
3218
3219 if (*p == NUL)
3220 break;
3221 if (vim9_comment_start(p))
3222 {
3223 char_u *nl = vim_strchr(p, NL);
3224
3225 if (nl == NULL)
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02003226 break;
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003227 p = nl;
3228 }
3229 if (*p != NL)
3230 break;
3231 ++p; // skip another NL
3232 }
3233 return p;
3234}
3235
3236/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02003237 * Get the next line source line without advancing. But do skip over comment
3238 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003239 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02003240 */
3241 static char_u *
3242getline_peek_skip_comments(evalarg_T *evalarg)
3243{
3244 for (;;)
3245 {
3246 char_u *next = getline_peek(evalarg->eval_getline,
3247 evalarg->eval_cookie);
3248 char_u *p;
3249
3250 if (next == NULL)
3251 break;
3252 p = skipwhite(next);
3253 if (*p != NUL && !vim9_comment_start(p))
3254 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01003255 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00003256 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02003257 }
3258 return NULL;
3259}
3260
3261/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02003262 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
3263 * comment) and there is a next line, return the next line (skipping blanks)
3264 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003265 * Otherwise return the next non-white at or after "arg" and set "getnext" to
3266 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003267 * "arg" must point somewhere inside a line, not at the start.
3268 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003269 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003270eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
3271{
Bram Moolenaar9d489562020-07-30 20:08:50 +02003272 char_u *p = skipwhite(arg);
3273
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003274 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003275 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003276 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01003277 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
3278 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01003279 && (*p == NUL || *p == NL
3280 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003281 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02003282 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003283
Bram Moolenaare442d592022-05-05 12:20:28 +01003284 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003285 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01003286 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02003287 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003288 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02003289 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003290
Bram Moolenaar9d489562020-07-30 20:08:50 +02003291 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003292 {
Bram Moolenaar69082912022-09-22 21:35:19 +01003293 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003294 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003295 }
3296 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003297 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003298}
3299
3300/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003301 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003302 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003303 *
3304 * If "arg" is not NULL, then the caller should assign the return value to
3305 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003306 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003307 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01003308eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003309{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003310 garray_T *gap = &evalarg->eval_ga;
3311 char_u *line;
3312
Bram Moolenaar39be4982022-05-06 21:51:50 +01003313 if (arg != NULL)
3314 {
3315 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003316 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01003317 // Truncate before a trailing comment, so that concatenating the lines
3318 // won't turn the rest into a comment.
3319 if (*skipwhite(arg) == '#')
3320 *arg = NUL;
3321 }
Bram Moolenaare442d592022-05-05 12:20:28 +01003322
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003323 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02003324 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
3325 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003326 else
3327 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00003328 if (line == NULL)
3329 return NULL;
3330
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003331 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003332 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
3333 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003334 char_u *p = skipwhite(line);
3335
3336 // Going to concatenate the lines after parsing. For an empty or
3337 // comment line use an empty string.
3338 if (*p == NUL || vim9_comment_start(p))
3339 {
3340 vim_free(line);
3341 line = vim_strsave((char_u *)"");
3342 }
3343
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003344 ((char_u **)gap->ga_data)[gap->ga_len] = line;
3345 ++gap->ga_len;
3346 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003347 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003348 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01003349 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003350 evalarg->eval_tofree = line;
3351 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02003352
3353 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003354 // line. The caller assigns the return value to "arg".
3355 // If "arg" is NULL, then the return value is discarded. In that case,
3356 // "arg" still points to the previous line. So don't reset
3357 // "eval_using_cmdline".
3358 if (arg != NULL)
3359 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003360 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003361}
3362
Bram Moolenaare6b53242020-07-01 17:28:33 +02003363/*
3364 * Call eval_next_non_blank() and get the next line if needed.
3365 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02003366 char_u *
3367skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
3368{
3369 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00003370 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003371
Bram Moolenaar962d7212020-07-04 14:15:00 +02003372 if (evalarg == NULL)
3373 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003374 eval_next_non_blank(p, evalarg, &getnext);
3375 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003376 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003377 return p;
3378}
3379
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003380/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003381 * The "eval" functions have an "evalarg" argument: When NULL or
3382 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
3383 * parsed but not executed. The functions may return OK, but the rettv will be
3384 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 */
3386
3387/*
3388 * Handle zero level expression.
3389 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003390 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003391 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003392 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Return OK or FAIL.
3394 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003396eval0(
3397 char_u *arg,
3398 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003399 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003400 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003402 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
3403}
3404
3405/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003406 * If "arg" is a simple function call without arguments then call it and return
3407 * the result. Otherwise return NOTDONE.
3408 */
3409 int
3410may_call_simple_func(
3411 char_u *arg,
3412 typval_T *rettv)
3413{
3414 char_u *parens = (char_u *)strstr((char *)arg, "()");
3415 int r = NOTDONE;
3416
3417 // If the expression is "FuncName()" then we can skip a lot of overhead.
3418 if (parens != NULL && *skipwhite(parens + 2) == NUL)
3419 {
3420 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
3421
3422 if (to_name_end(p, TRUE) == parens)
zeertzjqc1ed7882024-08-01 22:46:54 +02003423 r = call_simple_func(arg, (size_t)(parens - arg), rettv);
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003424 }
3425 return r;
3426}
3427
3428/*
3429 * Handle zero level expression with optimization for a simple function call.
3430 * Same arguments and return value as eval0().
3431 */
3432 int
3433eval0_simple_funccal(
3434 char_u *arg,
3435 typval_T *rettv,
3436 exarg_T *eap,
3437 evalarg_T *evalarg)
3438{
3439 int r = may_call_simple_func(arg, rettv);
3440
3441 if (r == NOTDONE)
3442 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
3443 return r;
3444}
3445
3446/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003447 * Like eval0() but when "retarg" is not NULL store the pointer to after the
3448 * expression and don't check what comes after the expression.
3449 */
3450 int
3451eval0_retarg(
3452 char_u *arg,
3453 typval_T *rettv,
3454 exarg_T *eap,
3455 evalarg_T *evalarg,
3456 char_u **retarg)
3457{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 int ret;
3459 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00003460 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003461 int did_emsg_before = did_emsg;
3462 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003463 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003464 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
3466 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003467 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003468
Bram Moolenaar79481362022-06-27 20:15:10 +01003469 if (ret != FAIL)
3470 {
3471 expr_end = p;
3472 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003473
Bram Moolenaar79481362022-06-27 20:15:10 +01003474 // In Vim9 script a command block is not split at NL characters for
3475 // commands using an expression argument. Skip over a '#' comment to
3476 // check for a following NL. Require white space before the '#'.
3477 if (in_vim9script() && p > expr_end && retarg == NULL)
3478 while (*p == '#')
3479 {
3480 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003481
Bram Moolenaar79481362022-06-27 20:15:10 +01003482 if (nl == NULL)
3483 break;
3484 p = skipwhite(nl + 1);
3485 if (eap != NULL && *p != NUL)
3486 eap->nextcmd = p;
3487 check_for_end = FALSE;
3488 }
3489
3490 if (check_for_end)
3491 end_error = !ends_excmd2(arg, p);
3492 }
3493
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003494 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 /*
3499 * Report the invalid expression unless the expression evaluation has
3500 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003501 * exception, or we already gave a more specific error.
3502 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003504 if (!aborting()
3505 && did_emsg == did_emsg_before
3506 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003507 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003508 {
3509 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003510 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003511 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003512 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003513 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003514
zeertzjqf2588b62023-05-05 17:22:35 +01003515 if (eap != NULL && p != NULL)
3516 {
3517 // Some of the expression may not have been consumed.
3518 // Only execute a next command if it cannot be a "||" operator.
3519 // The next command may be "catch".
3520 char_u *nextcmd = check_nextcmd(p);
3521 if (nextcmd != NULL && *nextcmd != '|')
3522 eap->nextcmd = nextcmd;
3523 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003524 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003526
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003527 if (retarg != NULL)
3528 *retarg = p;
3529 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003530 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 return ret;
3533}
3534
3535/*
3536 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003537 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003538 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 *
3540 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003541 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003543 * Note: "rettv.v_lock" is not set.
3544 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 * Return OK or FAIL.
3546 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003547 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003548eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003550 char_u *p;
3551 int getnext;
3552
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003553 CLEAR_POINTER(rettv);
3554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /*
3556 * Get the first variable.
3557 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003558 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 return FAIL;
3560
Bram Moolenaar793648f2020-06-26 21:28:25 +02003561 p = eval_next_non_blank(*arg, evalarg, &getnext);
3562 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003564 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003565 int result;
3566 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003567 evalarg_T *evalarg_used = evalarg;
3568 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003569 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003570 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003571 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003572
3573 if (evalarg == NULL)
3574 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003575 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003576 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003577 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003578 orig_flags = evalarg_used->eval_flags;
3579 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003580
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003581 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003582 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003583 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003584 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003585 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003586 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003587 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003588 clear_tv(rettv);
3589 return FAIL;
3590 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003591 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003592 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003595 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003597 int error = FALSE;
3598
Bram Moolenaar13106602020-10-04 16:06:05 +02003599 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003600 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003601 else if (vim9script)
3602 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003603 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003605 if (error || !op_falsy || !result)
3606 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003607 if (error)
3608 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
3610
3611 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003612 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003614 if (op_falsy)
3615 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003616 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003617 {
mityu4ac198c2021-05-28 17:52:40 +02003618 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003619 clear_tv(rettv);
3620 return FAIL;
3621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003622 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003623 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003624 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003625 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003626 {
3627 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003629 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003630 if (!op_falsy || !result)
3631 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003633 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003635 /*
3636 * Check for the ":".
3637 */
3638 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3639 if (*p != ':')
3640 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003641 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003642 if (evaluate && result)
3643 clear_tv(rettv);
3644 evalarg_used->eval_flags = orig_flags;
3645 return FAIL;
3646 }
3647 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003648 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003649 else
3650 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003651 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003652 {
3653 error_white_both(p, 1);
3654 clear_tv(rettv);
3655 evalarg_used->eval_flags = orig_flags;
3656 return FAIL;
3657 }
3658 *arg = p;
3659 }
3660
3661 /*
3662 * Get the third variable. Recursive!
3663 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003664 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003665 {
mityu4ac198c2021-05-28 17:52:40 +02003666 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003667 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003668 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003669 return FAIL;
3670 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003671 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3672 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003673 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003674 if (eval1(arg, &var2, evalarg_used) == FAIL)
3675 {
3676 if (evaluate && result)
3677 clear_tv(rettv);
3678 evalarg_used->eval_flags = orig_flags;
3679 return FAIL;
3680 }
3681 if (evaluate && !result)
3682 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003684
3685 if (evalarg == NULL)
3686 clear_evalarg(&local_evalarg, NULL);
3687 else
3688 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690
3691 return OK;
3692}
3693
3694/*
3695 * Handle first level expression:
3696 * expr2 || expr2 || expr2 logical OR
3697 *
3698 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003699 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 *
3701 * Return OK or FAIL.
3702 */
3703 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003704eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003706 char_u *p;
3707 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003710 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003712 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 return FAIL;
3714
3715 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003716 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003718 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003719 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003721 evalarg_T *evalarg_used = evalarg;
3722 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003723 int evaluate;
3724 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003725 long result = FALSE;
3726 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003727 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003728 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003729
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003730 if (evalarg == NULL)
3731 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003732 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003733 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003734 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003735 orig_flags = evalarg_used->eval_flags;
3736 evaluate = orig_flags & EVAL_EVALUATE;
3737 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003738 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003739 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003740 result = tv_get_bool_chk(rettv, &error);
3741 else if (tv_get_number_chk(rettv, &error) != 0)
3742 result = TRUE;
3743 clear_tv(rettv);
3744 if (error)
3745 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747
3748 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003749 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003751 while (p[0] == '|' && p[1] == '|')
3752 {
3753 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003754 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003755 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003756 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003757 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003758 {
3759 error_white_both(p, 2);
3760 clear_tv(rettv);
3761 return FAIL;
3762 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003763 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003764 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003765
3766 /*
3767 * Get the second variable.
3768 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003769 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003770 {
mityu4ac198c2021-05-28 17:52:40 +02003771 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003772 clear_tv(rettv);
3773 return FAIL;
3774 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003775 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3776 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003777 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003778 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003780
3781 /*
3782 * Compute the result.
3783 */
3784 if (evaluate && !result)
3785 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003786 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003787 result = tv_get_bool_chk(&var2, &error);
3788 else if (tv_get_number_chk(&var2, &error) != 0)
3789 result = TRUE;
3790 clear_tv(&var2);
3791 if (error)
3792 return FAIL;
3793 }
3794 if (evaluate)
3795 {
3796 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003797 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003798 rettv->v_type = VAR_BOOL;
3799 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003800 }
3801 else
3802 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003803 rettv->v_type = VAR_NUMBER;
3804 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003805 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003806 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003807
3808 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003810
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003811 if (evalarg == NULL)
3812 clear_evalarg(&local_evalarg, NULL);
3813 else
3814 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816
3817 return OK;
3818}
3819
3820/*
3821 * Handle second level expression:
3822 * expr3 && expr3 && expr3 logical AND
3823 *
3824 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003825 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 *
3827 * Return OK or FAIL.
3828 */
3829 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003830eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003832 char_u *p;
3833 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003836 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003838 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return FAIL;
3840
3841 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003842 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003844 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003845 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003847 evalarg_T *evalarg_used = evalarg;
3848 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003849 int orig_flags;
3850 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003851 long result = TRUE;
3852 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003853 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003854 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003855
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003856 if (evalarg == NULL)
3857 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003858 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003859 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003860 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003861 orig_flags = evalarg_used->eval_flags;
3862 evaluate = orig_flags & EVAL_EVALUATE;
3863 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003864 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003865 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003866 result = tv_get_bool_chk(rettv, &error);
3867 else if (tv_get_number_chk(rettv, &error) == 0)
3868 result = FALSE;
3869 clear_tv(rettv);
3870 if (error)
3871 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873
3874 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003875 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003877 while (p[0] == '&' && p[1] == '&')
3878 {
3879 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003880 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003881 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003882 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003883 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003884 {
3885 error_white_both(p, 2);
3886 clear_tv(rettv);
3887 return FAIL;
3888 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003889 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003890 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003891
3892 /*
3893 * Get the second variable.
3894 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003895 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003896 {
mityu4ac198c2021-05-28 17:52:40 +02003897 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003898 clear_tv(rettv);
3899 return FAIL;
3900 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003901 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3902 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003903 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003904 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003905 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003906 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003907
3908 /*
3909 * Compute the result.
3910 */
3911 if (evaluate && result)
3912 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003913 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003914 result = tv_get_bool_chk(&var2, &error);
3915 else if (tv_get_number_chk(&var2, &error) == 0)
3916 result = FALSE;
3917 clear_tv(&var2);
3918 if (error)
3919 return FAIL;
3920 }
3921 if (evaluate)
3922 {
3923 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003924 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003925 rettv->v_type = VAR_BOOL;
3926 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003927 }
3928 else
3929 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003930 rettv->v_type = VAR_NUMBER;
3931 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003932 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003933 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003934
3935 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003937
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003938 if (evalarg == NULL)
3939 clear_evalarg(&local_evalarg, NULL);
3940 else
3941 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943
3944 return OK;
3945}
3946
3947/*
3948 * Handle third level expression:
3949 * var1 == var2
3950 * var1 =~ var2
3951 * var1 != var2
3952 * var1 !~ var2
3953 * var1 > var2
3954 * var1 >= var2
3955 * var1 < var2
3956 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003957 * var1 is var2
3958 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 *
3960 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003961 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 *
3963 * Return OK or FAIL.
3964 */
3965 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003966eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003969 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003970 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003972 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003975 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003977 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 return FAIL;
3979
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003980 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003981
Bram Moolenaar696ba232020-07-29 21:20:41 +02003982 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003985 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003987 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003989 typval_T var2;
3990 int ic;
3991 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003992 int evaluate = evalarg == NULL
3993 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003994 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003995
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003996 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003997 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003998 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003999 p = *arg;
4000 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004001 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
4002 {
mityu4ac198c2021-05-28 17:52:40 +02004003 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004004 clear_tv(rettv);
4005 return FAIL;
4006 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02004007
Bram Moolenaar696ba232020-07-29 21:20:41 +02004008 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
4009 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004010 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02004011 clear_tv(rettv);
4012 return FAIL;
4013 }
4014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004015 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (p[len] == '?')
4017 {
4018 ic = TRUE;
4019 ++len;
4020 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004021 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 else if (p[len] == '#')
4023 {
4024 ic = FALSE;
4025 ++len;
4026 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02004027 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02004029 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
4031 /*
4032 * Get the second variable.
4033 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004034 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
4035 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02004036 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004037 clear_tv(rettv);
4038 return FAIL;
4039 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02004040 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004041 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return FAIL;
4045 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004046 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01004047 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004048 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01004049
Bram Moolenaar1b1df952022-03-10 20:01:50 +00004050 // use the line of the comparison for messages
4051 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02004052 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004053 {
4054 ret = FAIL;
4055 clear_tv(rettv);
4056 }
4057 else
4058 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01004059 clear_tv(&var2);
4060 return ret;
4061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063
4064 return OK;
4065}
4066
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004067/*
4068 * Make a copy of blob "tv1" and append blob "tv2".
4069 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 void
4071eval_addblob(typval_T *tv1, typval_T *tv2)
4072{
4073 blob_T *b1 = tv1->vval.v_blob;
4074 blob_T *b2 = tv2->vval.v_blob;
4075 blob_T *b = blob_alloc();
4076 int i;
4077
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004078 if (b == NULL)
4079 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004081 for (i = 0; i < blob_len(b1); i++)
4082 ga_append(&b->bv_ga, blob_get(b1, i));
4083 for (i = 0; i < blob_len(b2); i++)
4084 ga_append(&b->bv_ga, blob_get(b2, i));
4085
4086 clear_tv(tv1);
4087 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088}
4089
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004090/*
4091 * Make a copy of list "tv1" and append list "tv2".
4092 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 int
4094eval_addlist(typval_T *tv1, typval_T *tv2)
4095{
4096 typval_T var3;
4097
4098 // concatenate Lists
4099 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
4100 {
4101 clear_tv(tv1);
4102 clear_tv(tv2);
4103 return FAIL;
4104 }
4105 clear_tv(tv1);
4106 *tv1 = var3;
4107 return OK;
4108}
4109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004111 * Make a copy of tuple "tv1" and append tuple "tv2".
4112 */
4113 int
4114eval_addtuple(typval_T *tv1, typval_T *tv2)
4115{
4116 int vim9script = in_vim9script();
4117 typval_T var3;
4118
4119 if (vim9script && tv1->vval.v_tuple != NULL && tv2->vval.v_tuple != NULL
4120 && tv1->vval.v_tuple->tv_type != NULL
4121 && tv2->vval.v_tuple->tv_type != NULL)
4122 {
4123 if (!check_tuples_addable(tv1->vval.v_tuple->tv_type,
4124 tv2->vval.v_tuple->tv_type))
4125 return FAIL;
4126 }
4127
4128 // concatenate tuples
4129 if (tuple_concat(tv1->vval.v_tuple, tv2->vval.v_tuple, &var3) == FAIL)
4130 {
4131 clear_tv(tv1);
4132 clear_tv(tv2);
4133 return FAIL;
4134 }
4135 clear_tv(tv1);
4136 *tv1 = var3;
4137 return OK;
4138}
4139
4140/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004141 * Left or right shift the number "tv1" by the number "tv2" and store the
4142 * result in "tv1".
4143 *
4144 * Return OK or FAIL.
4145 */
4146 static int
4147eval_shift_number(typval_T *tv1, typval_T *tv2, int shift_type)
4148{
4149 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
4150 {
4151 // right operand should be a positive number
4152 if (tv2->v_type != VAR_NUMBER)
4153 emsg(_(e_bitshift_ops_must_be_number));
4154 else
4155 emsg(_(e_bitshift_ops_must_be_positive));
4156 clear_tv(tv1);
4157 clear_tv(tv2);
4158 return FAIL;
4159 }
4160
4161 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
4162 // shifting more bits than we have always results in zero
4163 tv1->vval.v_number = 0;
4164 else if (shift_type == EXPR_LSHIFT)
4165 tv1->vval.v_number =
4166 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
4167 else
4168 tv1->vval.v_number =
4169 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
4170
4171 return OK;
4172}
4173
4174/*
Yegappan Lakshmanana81cf8b2025-01-19 22:20:34 +01004175 * Handle fourth level expression (bitwise left/right shift operators):
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004176 * var1 << var2
4177 * var1 >> var2
4178 *
4179 * "arg" must point to the first non-white of the expression.
4180 * "arg" is advanced to just after the recognized expression.
4181 *
4182 * Return OK or FAIL.
4183 */
4184 static int
4185eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
4186{
4187 /*
4188 * Get the first expression.
4189 */
4190 if (eval6(arg, rettv, evalarg) == FAIL)
4191 return FAIL;
4192
4193 /*
4194 * Repeat computing, until no '<<' or '>>' is following.
4195 */
4196 for (;;)
4197 {
4198 char_u *p;
4199 int getnext;
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004200 exprtype_T exprtype;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004201 int evaluate;
4202 typval_T var2;
4203 int vim9script;
4204
4205 p = eval_next_non_blank(*arg, evalarg, &getnext);
4206 if (p[0] == '<' && p[1] == '<')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004207 exprtype = EXPR_LSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004208 else if (p[0] == '>' && p[1] == '>')
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004209 exprtype = EXPR_RSHIFT;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004210 else
4211 return OK;
4212
4213 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004214 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
4215 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004216 {
4217 // left operand should be a number
4218 emsg(_(e_bitshift_ops_must_be_number));
4219 clear_tv(rettv);
4220 return FAIL;
4221 }
4222
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004223 vim9script = in_vim9script();
4224 if (getnext)
4225 {
4226 *arg = eval_next_line(*arg, evalarg);
4227 p = *arg;
4228 }
4229 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
4230 {
4231 error_white_both(*arg, 2);
4232 clear_tv(rettv);
4233 return FAIL;
4234 }
4235
4236 /*
4237 * Get the second variable.
4238 */
4239 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
4240 {
4241 error_white_both(p, 2);
4242 clear_tv(rettv);
4243 return FAIL;
4244 }
4245 *arg = skipwhite_and_linebreak(p + 2, evalarg);
4246 if (eval6(arg, &var2, evalarg) == FAIL)
4247 {
4248 clear_tv(rettv);
4249 return FAIL;
4250 }
4251
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004252 if (evaluate)
4253 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004254 if (eval_shift_number(rettv, &var2, exprtype) == FAIL)
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02004255 return FAIL;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004256 }
4257
4258 clear_tv(&var2);
4259 }
4260
4261 return OK;
4262}
4263
4264/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004265 * Concatenate strings "tv1" and "tv2" and store the result in "tv1".
4266 */
4267 static int
4268eval_concat_str(typval_T *tv1, typval_T *tv2)
4269{
4270 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4271 char_u *s1 = tv_get_string_buf(tv1, buf1);
4272 char_u *s2 = NULL;
4273 char_u *p;
4274 int vim9script = in_vim9script();
4275
4276 if (vim9script && (tv2->v_type == VAR_VOID
4277 || tv2->v_type == VAR_CHANNEL
4278 || tv2->v_type == VAR_JOB))
4279 semsg(_(e_using_invalid_value_as_string_str),
4280 vartype_name(tv2->v_type));
4281 else if (vim9script && tv2->v_type == VAR_FLOAT)
4282 {
4283 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
4284 tv2->vval.v_float);
4285 s2 = buf2;
4286 }
4287 else
4288 s2 = tv_get_string_buf_chk(tv2, buf2);
4289 if (s2 == NULL) // type error ?
4290 {
4291 clear_tv(tv1);
4292 clear_tv(tv2);
4293 return FAIL;
4294 }
4295
4296 p = concat_str(s1, s2);
4297 clear_tv(tv1);
4298 tv1->v_type = VAR_STRING;
4299 tv1->vval.v_string = p;
4300
4301 return OK;
4302}
4303
4304/*
4305 * Add or subtract numbers "tv1" and "tv2" and store the result in "tv1".
4306 * The numbers can be whole numbers or floats.
4307 */
4308 static int
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004309eval_addsub_number(typval_T *tv1, typval_T *tv2, int op)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004310{
4311 int error = FALSE;
4312 varnumber_T n1, n2;
4313 float_T f1 = 0, f2 = 0;
4314
4315 if (tv1->v_type == VAR_FLOAT)
4316 {
4317 f1 = tv1->vval.v_float;
4318 n1 = 0;
4319 }
4320 else
4321 {
4322 n1 = tv_get_number_chk(tv1, &error);
4323 if (error)
4324 {
4325 // This can only happen for "list + non-list" or
4326 // "blob + non-blob". For "non-list + ..." or
4327 // "something - ...", we returned before evaluating the
4328 // 2nd operand.
4329 clear_tv(tv1);
4330 clear_tv(tv2);
4331 return FAIL;
4332 }
4333 if (tv2->v_type == VAR_FLOAT)
4334 f1 = n1;
4335 }
4336 if (tv2->v_type == VAR_FLOAT)
4337 {
4338 f2 = tv2->vval.v_float;
4339 n2 = 0;
4340 }
4341 else
4342 {
4343 n2 = tv_get_number_chk(tv2, &error);
4344 if (error)
4345 {
4346 clear_tv(tv1);
4347 clear_tv(tv2);
4348 return FAIL;
4349 }
4350 if (tv1->v_type == VAR_FLOAT)
4351 f2 = n2;
4352 }
4353 clear_tv(tv1);
4354
4355 // If there is a float on either side the result is a float.
4356 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4357 {
4358 if (op == '+')
4359 f1 = f1 + f2;
4360 else
4361 f1 = f1 - f2;
4362 tv1->v_type = VAR_FLOAT;
4363 tv1->vval.v_float = f1;
4364 }
4365 else
4366 {
4367 if (op == '+')
4368 n1 = n1 + n2;
4369 else
4370 n1 = n1 - n2;
4371 tv1->v_type = VAR_NUMBER;
4372 tv1->vval.v_number = n1;
4373 }
4374
4375 return OK;
4376}
4377
4378/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004379 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00004380 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004382 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004383 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 *
4385 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004386 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 *
4388 * Return OK or FAIL.
4389 */
4390 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004391eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004394 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004396 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
4398
4399 /*
4400 * Repeat computing, until no '+', '-' or '.' is following.
4401 */
4402 for (;;)
4403 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004404 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004405 int getnext;
4406 char_u *p;
4407 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004408 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004409 int concat;
4410 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02004411 int vim9script = in_vim9script();
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004412 long op_lnum = SOURCING_LNUM;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004413
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004414 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004415 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004416 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004417 p = eval_next_non_blank(*arg, evalarg, &getnext);
4418 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02004419 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004420 if ((op != '+' && op != '-' && !concat) || p[1] == '='
4421 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004423 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
4424 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004425
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004426 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004427 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004428 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004429 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004430 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004431 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02004432 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004433 {
mityu4ac198c2021-05-28 17:52:40 +02004434 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004435 clear_tv(rettv);
4436 return FAIL;
4437 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004438 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004439 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004440 if ((op != '+' || (rettv->v_type != VAR_LIST
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004441 && rettv->v_type != VAR_TUPLE
4442 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004444 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004446 int error = FALSE;
4447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004448 // For "list + ...", an illegal use of the first operand as
4449 // a number cannot be determined before evaluating the 2nd
4450 // operand: if this is also a list, all is ok.
4451 // For "something . ...", "something - ..." or "non-list + ...",
4452 // we know that the first operand needs to be a string or number
4453 // without evaluating the 2nd operand. So check before to avoid
4454 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004455 if (op != '.')
4456 tv_get_number_chk(rettv, &error);
4457 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004458 {
4459 clear_tv(rettv);
4460 return FAIL;
4461 }
4462 }
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 /*
4465 * Get the second variable.
4466 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02004467 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004468 {
mityu89dcb4d2021-05-28 13:50:17 +02004469 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004470 clear_tv(rettv);
4471 return FAIL;
4472 }
4473 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004474 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 return FAIL;
4478 }
4479
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004480 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
4482 /*
4483 * Compute the result.
4484 */
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004485 // use the line of the operation for messages
4486 SOURCING_LNUM = op_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (op == '.')
4488 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004489 if (eval_concat_str(rettv, &var2) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004490 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004492 else if (op == '+' && rettv->v_type == VAR_BLOB
4493 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004495 else if (op == '+' && rettv->v_type == VAR_LIST
4496 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004500 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004501 else if (op == '+' && rettv->v_type == VAR_TUPLE
4502 && var2.v_type == VAR_TUPLE)
4503 {
4504 if (eval_addtuple(rettv, &var2) == FAIL)
4505 return FAIL;
4506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 else
4508 {
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004509 if (eval_addsub_number(rettv, &var2, op) == FAIL)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004510 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004512 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514 }
4515 return OK;
4516}
4517
4518/*
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004519 * Multiply or divide or compute the modulo of numbers "tv1" and "tv2" and
4520 * store the result in "tv1". The numbers can be whole numbers or floats.
4521 */
4522 static int
4523eval_multdiv_number(typval_T *tv1, typval_T *tv2, int op)
4524{
4525 varnumber_T n1, n2;
4526 float_T f1, f2;
4527 int error;
4528 int use_float = FALSE;
4529
4530 f1 = 0;
4531 f2 = 0;
4532 error = FALSE;
4533 if (tv1->v_type == VAR_FLOAT)
4534 {
4535 f1 = tv1->vval.v_float;
4536 use_float = TRUE;
4537 n1 = 0;
4538 }
4539 else
4540 n1 = tv_get_number_chk(tv1, &error);
4541 clear_tv(tv1);
4542 if (error)
4543 {
4544 clear_tv(tv2);
4545 return FAIL;
4546 }
4547
4548 if (tv2->v_type == VAR_FLOAT)
4549 {
4550 if (!use_float)
4551 {
4552 f1 = n1;
4553 use_float = TRUE;
4554 }
4555 f2 = tv2->vval.v_float;
4556 n2 = 0;
4557 }
4558 else
4559 {
4560 n2 = tv_get_number_chk(tv2, &error);
4561 clear_tv(tv2);
4562 if (error)
4563 return FAIL;
4564 if (use_float)
4565 f2 = n2;
4566 }
4567
4568 /*
4569 * Compute the result.
4570 * When either side is a float the result is a float.
4571 */
4572 if (use_float)
4573 {
4574 if (op == '*')
4575 f1 = f1 * f2;
4576 else if (op == '/')
4577 {
4578#ifdef VMS
4579 // VMS crashes on divide by zero, work around it
4580 if (f2 == 0.0)
4581 {
4582 if (f1 == 0)
4583 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
4584 else if (f1 < 0)
4585 f1 = -1 * __F_FLT_MAX;
4586 else
4587 f1 = __F_FLT_MAX;
4588 }
4589 else
4590 f1 = f1 / f2;
4591#else
4592 // We rely on the floating point library to handle divide
4593 // by zero to result in "inf" and not a crash.
4594 f1 = f1 / f2;
4595#endif
4596 }
4597 else
4598 {
4599 emsg(_(e_cannot_use_percent_with_float));
4600 return FAIL;
4601 }
4602 tv1->v_type = VAR_FLOAT;
4603 tv1->vval.v_float = f1;
4604 }
4605 else
4606 {
4607 int failed = FALSE;
4608
4609 if (op == '*')
4610 n1 = n1 * n2;
4611 else if (op == '/')
4612 n1 = num_divide(n1, n2, &failed);
4613 else
4614 n1 = num_modulus(n1, n2, &failed);
4615 if (failed)
4616 return FAIL;
4617
4618 tv1->v_type = VAR_NUMBER;
4619 tv1->vval.v_number = n1;
4620 }
4621
4622 return OK;
4623}
4624
4625/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004626 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 * * number multiplication
4628 * / number division
4629 * % number modulo
4630 *
4631 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004632 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 *
4634 * Return OK or FAIL.
4635 */
4636 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004637eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004638 char_u **arg,
4639 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004640 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004641 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004644 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004646 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 return FAIL;
4648
4649 /*
4650 * Repeat computing, until no '*', '/' or '%' is following.
4651 */
4652 for (;;)
4653 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004654 int evaluate;
4655 int getnext;
4656 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02004657 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004658 int op;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004659
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004660 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02004661 p = eval_next_non_blank(*arg, evalarg, &getnext);
4662 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004663 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004665
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004666 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004667 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004668 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004669 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004670 {
4671 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
4672 {
mityu4ac198c2021-05-28 17:52:40 +02004673 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004674 clear_tv(rettv);
4675 return FAIL;
4676 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004677 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 /*
4681 * Get the second variable.
4682 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004683 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4684 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004685 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004686 clear_tv(rettv);
4687 return FAIL;
4688 }
4689 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004690 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 return FAIL;
4692
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004693 if (evaluate)
Yegappan Lakshmanan734286e2024-06-03 18:52:22 +02004694 // Compute the result.
4695 if (eval_multdiv_number(rettv, &var2, op) == FAIL)
4696 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698
4699 return OK;
4700}
4701
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004702/*
Yegappan Lakshmanana81cf8b2025-01-19 22:20:34 +01004703 * Handle seventh level expression:
4704 * a type cast before a base level expression.
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004705 * "arg" must point to the first non-white of the expression.
4706 * "arg" is advanced to just after the recognized expression.
4707 * Return OK or FAIL.
4708 */
4709 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004710eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004711 char_u **arg,
4712 typval_T *rettv,
4713 evalarg_T *evalarg,
4714 int want_string) // after "." operator
4715{
4716 type_T *want_type = NULL;
4717 garray_T type_list; // list of pointers to allocated types
4718 int res;
4719 int evaluate = evalarg == NULL ? 0
4720 : (evalarg->eval_flags & EVAL_EVALUATE);
4721
4722 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004723 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4724 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004725 {
4726 ++*arg;
4727 ga_init2(&type_list, sizeof(type_T *), 10);
4728 want_type = parse_type(arg, &type_list, TRUE);
4729 if (want_type == NULL && (evaluate || **arg != '>'))
4730 {
4731 clear_type_list(&type_list);
4732 return FAIL;
4733 }
4734
4735 if (**arg != '>')
4736 {
4737 if (*skipwhite(*arg) == '>')
4738 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4739 else
4740 emsg(_(e_missing_gt));
4741 clear_type_list(&type_list);
4742 return FAIL;
4743 }
4744 ++*arg;
4745 *arg = skipwhite_and_linebreak(*arg, evalarg);
4746 }
4747
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004748 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004749
4750 if (want_type != NULL && evaluate)
4751 {
4752 if (res == OK)
4753 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004754 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4755 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004756
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004757 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004758 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004759 if (want_type->tt_type == VAR_BOOL
4760 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004761 && (actual->tt_flags & TTFLAG_BOOL_OK))
4762 {
4763 int n = tv2bool(rettv);
4764
4765 // can use "0" and "1" for boolean in some places
4766 clear_tv(rettv);
4767 rettv->v_type = VAR_BOOL;
4768 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4769 }
4770 else
4771 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004772 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004773
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004774 res = check_type(want_type, actual, TRUE, where);
4775 }
4776 }
4777 }
4778 clear_type_list(&type_list);
4779 }
4780
4781 return res;
4782}
4783
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004784 int
4785eval_leader(char_u **arg, int vim9)
4786{
4787 char_u *s = *arg;
4788 char_u *p = *arg;
4789
4790 while (*p == '!' || *p == '-' || *p == '+')
4791 {
4792 char_u *n = skipwhite(p + 1);
4793
4794 // ++, --, -+ and +- are not accepted in Vim9 script
4795 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4796 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004797 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004798 return FAIL;
4799 }
4800 p = n;
4801 }
4802 *arg = p;
4803 return OK;
4804}
4805
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004807 * Check for a predefined value "true", "false" and "null.*".
4808 * Return OK when recognized.
4809 */
4810 int
4811handle_predefined(char_u *s, int len, typval_T *rettv)
4812{
4813 switch (len)
4814 {
4815 case 4: if (STRNCMP(s, "true", 4) == 0)
4816 {
4817 rettv->v_type = VAR_BOOL;
4818 rettv->vval.v_number = VVAL_TRUE;
4819 return OK;
4820 }
4821 if (STRNCMP(s, "null", 4) == 0)
4822 {
4823 rettv->v_type = VAR_SPECIAL;
4824 rettv->vval.v_number = VVAL_NULL;
4825 return OK;
4826 }
4827 break;
4828 case 5: if (STRNCMP(s, "false", 5) == 0)
4829 {
4830 rettv->v_type = VAR_BOOL;
4831 rettv->vval.v_number = VVAL_FALSE;
4832 return OK;
4833 }
4834 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004835 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4836 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004837#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004838 rettv->v_type = VAR_JOB;
4839 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004840#else
4841 rettv->v_type = VAR_SPECIAL;
4842 rettv->vval.v_number = VVAL_NULL;
4843#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004844 return OK;
4845 }
4846 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004847 case 9:
4848 if (STRNCMP(s, "null_", 5) != 0)
4849 break;
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004850 // null_list
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004851 if (STRNCMP(s + 5, "list", 4) == 0)
4852 {
4853 rettv->v_type = VAR_LIST;
4854 rettv->vval.v_list = NULL;
4855 return OK;
4856 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004857 // null_dict
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004858 if (STRNCMP(s + 5, "dict", 4) == 0)
4859 {
4860 rettv->v_type = VAR_DICT;
4861 rettv->vval.v_dict = NULL;
4862 return OK;
4863 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004864 // null_blob
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004865 if (STRNCMP(s + 5, "blob", 4) == 0)
4866 {
4867 rettv->v_type = VAR_BLOB;
4868 rettv->vval.v_blob = NULL;
4869 return OK;
4870 }
4871 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004872 case 10:
4873 if (STRNCMP(s, "null_", 5) != 0)
4874 break;
4875 // null_class
4876 if (STRNCMP(s + 5, "class", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004877 {
4878 rettv->v_type = VAR_CLASS;
4879 rettv->vval.v_class = NULL;
4880 return OK;
4881 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004882 if (STRNCMP(s + 5, "tuple", 5) == 0)
4883 {
4884 rettv->v_type = VAR_TUPLE;
4885 rettv->vval.v_tuple = NULL;
4886 return OK;
4887 }
4888 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004889 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4890 {
4891 rettv->v_type = VAR_STRING;
4892 rettv->vval.v_string = NULL;
4893 return OK;
4894 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004895 if (STRNCMP(s, "null_object", 11) == 0)
4896 {
4897 rettv->v_type = VAR_OBJECT;
4898 rettv->vval.v_object = NULL;
4899 return OK;
4900 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004901 break;
4902 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004903 if (STRNCMP(s, "null_channel", 12) == 0)
4904 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004905#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004906 rettv->v_type = VAR_CHANNEL;
4907 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004908#else
4909 rettv->v_type = VAR_SPECIAL;
4910 rettv->vval.v_number = VVAL_NULL;
4911#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004912 return OK;
4913 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004914 if (STRNCMP(s, "null_partial", 12) == 0)
4915 {
4916 rettv->v_type = VAR_PARTIAL;
4917 rettv->vval.v_partial = NULL;
4918 return OK;
4919 }
4920 break;
4921 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4922 {
4923 rettv->v_type = VAR_FUNC;
4924 rettv->vval.v_string = NULL;
4925 return OK;
4926 }
4927 break;
4928 }
4929 return FAIL;
4930}
4931
4932/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004933 * Handle register contents: @r.
4934 */
4935 static void
4936eval9_reg_contents(
4937 char_u **arg,
4938 typval_T *rettv,
4939 int evaluate)
4940{
4941 int vim9script = in_vim9script();
4942
4943 ++*arg; // skip '@'
4944 if (evaluate)
4945 {
4946 if (vim9script && IS_WHITE_OR_NUL(**arg))
4947 semsg(_(e_syntax_error_at_str), *arg);
4948 else if (vim9script && !valid_yank_reg(**arg, FALSE))
4949 emsg_invreg(**arg);
4950 else
4951 {
4952 rettv->v_type = VAR_STRING;
4953 rettv->vval.v_string = get_reg_contents(**arg,
4954 GREG_EXPR_SRC);
4955 }
4956 }
4957 if (**arg != NUL)
4958 ++*arg;
4959}
4960
4961/*
4962 * Handle a nested expression: (expression) or lambda: (arg) => expr
4963 */
4964 static int
4965eval9_nested_expr(
4966 char_u **arg,
4967 typval_T *rettv,
4968 evalarg_T *evalarg,
4969 int evaluate)
4970{
4971 int ret = NOTDONE;
4972 int vim9script = in_vim9script();
4973
4974 if (vim9script)
4975 {
4976 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
4977 if (ret == OK && evaluate)
4978 {
4979 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4980
4981 // Compile it here to get the return type. The return
4982 // type is optional, when it's missing use t_unknown.
4983 // This is recognized in compile_return().
4984 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4985 ufunc->uf_ret_type = &t_unknown;
4986 if (compile_def_function(ufunc, FALSE,
4987 get_compile_type(ufunc), NULL) == FAIL)
4988 {
4989 clear_tv(rettv);
4990 ret = FAIL;
4991 }
4992 }
4993 }
4994 if (ret == NOTDONE)
4995 {
4996 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02004997 if (**arg == ')')
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004998 // empty tuple
4999 ret = eval_tuple(arg, rettv, evalarg, TRUE);
5000 else
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005001 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005002 ret = eval1(arg, rettv, evalarg); // recursive!
Yegappan Lakshmanan13077432025-05-21 20:52:44 +02005003 if (ret != OK)
5004 return ret;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005005
5006 *arg = skipwhite_and_linebreak(*arg, evalarg);
5007
5008 if (**arg == ',')
5009 // tuple
5010 ret = eval_tuple(arg, rettv, evalarg, TRUE);
5011 else if (**arg == ')')
5012 ++*arg;
5013 else if (ret == OK)
5014 {
5015 emsg(_(e_missing_closing_paren));
5016 clear_tv(rettv);
5017 ret = FAIL;
5018 }
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005019 }
5020 }
5021
5022 return ret;
5023}
5024
5025/*
5026* Handle be a variable or function name.
5027* Can also be a curly-braces kind of name: {expr}.
5028*/
5029 static int
5030eval9_var_func_name(
5031 char_u **arg,
5032 typval_T *rettv,
5033 evalarg_T *evalarg,
5034 int evaluate,
5035 char_u **name_start)
5036{
5037 char_u *s;
5038 int len;
5039 char_u *alias;
5040 int ret = OK;
5041 int vim9script = in_vim9script();
5042
5043 s = *arg;
5044 len = get_name_len(arg, &alias, evaluate, TRUE);
5045 if (alias != NULL)
5046 s = alias;
5047
5048 if (len <= 0)
5049 ret = FAIL;
5050 else
5051 {
5052 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
5053
5054 if (evaluate && vim9script && len == 1 && *s == '_')
5055 {
5056 emsg(_(e_cannot_use_underscore_here));
5057 ret = FAIL;
5058 }
5059 else if (evaluate && vim9script && len > 2
5060 && s[0] == 's' && s[1] == ':')
5061 {
5062 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
5063 ret = FAIL;
5064 }
5065 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
5066 {
5067 // "name(..." recursive!
5068 *arg = skipwhite(*arg);
5069 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
5070 }
5071 else if (evaluate)
5072 {
5073 // get the value of "true", "false", etc. or a variable
5074 ret = FAIL;
5075 if (vim9script)
5076 ret = handle_predefined(s, len, rettv);
5077 if (ret == FAIL)
5078 {
5079 *name_start = s;
5080 ret = eval_variable(s, len, 0, rettv, NULL,
5081 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
5082 }
5083 }
5084 else
5085 {
5086 // skip the name
5087 check_vars(s, len);
5088 ret = OK;
5089 }
5090 }
5091 vim_free(alias);
5092
5093 return ret;
5094}
5095
5096/*
Yegappan Lakshmanana81cf8b2025-01-19 22:20:34 +01005097 * Handle eighth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005099 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005100 * "string" string constant
5101 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 * &option-name option value
5103 * @r register contents
5104 * identifier variable value
5105 * function() function call
5106 * $VAR environment variable
5107 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005108 * [expr, expr] List
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005109 * (expr, expr) Tuple
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005111 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02005112 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 *
5114 * Also handle:
5115 * ! in front logical NOT
5116 * - in front unary minus
5117 * + in front unary plus (ignored)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005118 * trailing [] subscript in String or List or Tuple
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02005120 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 *
5122 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02005123 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 *
5125 * Return OK or FAIL.
5126 */
5127 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005128eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005129 char_u **arg,
5130 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005131 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005134 int evaluate = evalarg != NULL
5135 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005136 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 char_u *start_leader, *end_leader;
5138 int ret = OK;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005139 static int recurse = 0;
5140 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005146 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02005149 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
5151 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005152 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01005153 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 end_leader = *arg;
5155
Keith Thompson184f71c2024-01-04 21:19:04 +01005156 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005157 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005158 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005159 ++*arg;
5160 return FAIL;
5161 }
5162
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005163 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00005164 // and crash. With MSVC the stack is smaller.
5165 if (recurse ==
5166#ifdef _MSC_VER
5167 300
5168#else
5169 1000
5170#endif
5171 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005172 {
5173 semsg(_(e_expression_too_recursive_str), *arg);
5174 return FAIL;
5175 }
5176 ++recurse;
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 switch (**arg)
5179 {
5180 /*
5181 * Number constant.
5182 */
5183 case '0':
5184 case '1':
5185 case '2':
5186 case '3':
5187 case '4':
5188 case '5':
5189 case '6':
5190 case '7':
5191 case '8':
5192 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005193 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005194
5195 // Apply prefixed "-" and "+" now. Matters especially when
5196 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02005197 if (ret == OK && evaluate && end_leader > start_leader
5198 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005199 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 break;
5201
5202 /*
5203 * String constant: "string".
5204 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01005205 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 break;
5207
5208 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01005211 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212 break;
5213
5214 /*
5215 * List: [expr, expr]
5216 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005217 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 break;
5219
5220 /*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005221 * Literal Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005222 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005223 case '#': ret = eval_lit_dict(arg, rettv, evalarg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005224 break;
5225
5226 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005227 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02005228 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00005230 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005231 ret = NOTDONE;
5232 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00005233 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02005234 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02005235 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 break;
5237
5238 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005239 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02005241 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 break;
5243
5244 /*
5245 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01005246 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 */
LemonBoy2eaef102022-05-06 13:14:50 +01005248 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
5249 ret = eval_interp_string(arg, rettv, evaluate);
5250 else
5251 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 break;
5253
5254 /*
5255 * Register contents: @r.
5256 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005257 case '@': eval9_reg_contents(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 break;
5259
5260 /*
5261 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02005262 * or lambda: (arg) => expr
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005263 * or tuple
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005265 case '(': ret = eval9_nested_expr(arg, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 break;
5267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 break;
5270 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271
5272 if (ret == NOTDONE)
5273 {
5274 /*
5275 * Must be a variable or function name.
5276 * Can also be a curly-braces kind of name: {expr}.
5277 */
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02005278 ret = eval9_var_func_name(arg, rettv, evalarg, evaluate, &name_start);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 }
5280
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005281 // Handle following '[', '(' and '.' for expr[expr], expr.name,
5282 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005284 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 /*
5287 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5288 */
5289 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005290 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00005291
5292 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005293 return ret;
5294}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005296/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005297 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005298 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005299 * Adjusts "end_leaderp" until it is at "start_leader".
5300 */
5301 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01005302eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005303 typval_T *rettv,
5304 int numeric_only,
5305 char_u *start_leader,
5306 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005307{
5308 char_u *end_leader = *end_leaderp;
5309 int ret = OK;
5310 int error = FALSE;
5311 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005312 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005313 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005314 float_T f = 0.0;
5315
5316 if (rettv->v_type == VAR_FLOAT)
5317 f = rettv->vval.v_float;
5318 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005319 {
5320 while (VIM_ISWHITE(end_leader[-1]))
5321 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005322 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02005323 val = tv2bool(rettv);
5324 else
5325 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02005326 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005327 if (error)
5328 {
5329 clear_tv(rettv);
5330 ret = FAIL;
5331 }
5332 else
5333 {
5334 while (end_leader > start_leader)
5335 {
5336 --end_leader;
5337 if (*end_leader == '!')
5338 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005339 if (numeric_only)
5340 {
5341 ++end_leader;
5342 break;
5343 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005344 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005345 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00005346 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01005347 {
5348 rettv->v_type = VAR_BOOL;
5349 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
5350 }
5351 else
5352 f = !f;
5353 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005354 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005355 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005356 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005357 type = VAR_BOOL;
5358 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005359 }
5360 else if (*end_leader == '-')
5361 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005362 if (rettv->v_type == VAR_FLOAT)
5363 f = -f;
5364 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005365 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005366 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005367 type = VAR_NUMBER;
5368 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005369 }
5370 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005371 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005373 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005374 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005376 else
5377 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005378 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005379 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02005380 rettv->v_type = type;
5381 else
5382 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005383 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005386 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 return ret;
5388}
5389
5390/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005391 * Call the function referred to in "rettv".
5392 */
5393 static int
5394call_func_rettv(
5395 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005396 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005397 typval_T *rettv,
5398 int evaluate,
5399 dict_T *selfdict,
5400 typval_T *basetv)
5401{
5402 partial_T *pt = NULL;
5403 funcexe_T funcexe;
5404 typval_T functv;
5405 char_u *s;
5406 int ret;
5407
5408 // need to copy the funcref so that we can clear rettv
5409 if (evaluate)
5410 {
5411 functv = *rettv;
5412 rettv->v_type = VAR_UNKNOWN;
5413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005415 if (functv.v_type == VAR_PARTIAL)
5416 {
5417 pt = functv.vval.v_partial;
5418 s = partial_name(pt);
5419 }
5420 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005421 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005422 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005423 if (s == NULL || *s == NUL)
5424 {
5425 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005426 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005427 goto theend;
5428 }
5429 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005430 }
5431 else
5432 s = (char_u *)"";
5433
Bram Moolenaara80faa82020-04-12 19:37:17 +02005434 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005435 funcexe.fe_firstline = curwin->w_cursor.lnum;
5436 funcexe.fe_lastline = curwin->w_cursor.lnum;
5437 funcexe.fe_evaluate = evaluate;
5438 funcexe.fe_partial = pt;
5439 funcexe.fe_selfdict = selfdict;
5440 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005441 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005442
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005443theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005444 // Clear the funcref afterwards, so that deleting it while
5445 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005446 if (evaluate)
5447 clear_tv(&functv);
5448
5449 return ret;
5450}
5451
5452/*
5453 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005454 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005455 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5456 */
5457 static int
5458eval_lambda(
5459 char_u **arg,
5460 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005461 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005462 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005463{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005464 int evaluate = evalarg != NULL
5465 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005466 typval_T base = *rettv;
5467 int ret;
5468
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005469 rettv->v_type = VAR_UNKNOWN;
5470
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005471 if (**arg == '{')
5472 {
5473 // ->{lambda}()
5474 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5475 }
5476 else
5477 {
5478 // ->(lambda)()
5479 ++*arg;
5480 ret = eval1(arg, rettv, evalarg);
5481 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005482 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005483 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005484 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005485 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005486 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005487 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5488 && rettv->v_type != VAR_PARTIAL)
5489 {
5490 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5491 return FAIL;
5492 }
5493 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005494 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005495 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005496 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005497
5498 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005499 {
5500 if (verbose)
5501 {
5502 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005503 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005504 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005505 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005506 }
5507 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005508 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005509 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005510 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005511 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005512
5513 // Clear the funcref afterwards, so that deleting it while
5514 // evaluating the arguments is possible (see test55).
5515 if (evaluate)
5516 clear_tv(&base);
5517
5518 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005519}
5520
5521/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005522 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005523 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005524 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5525 */
5526 static int
5527eval_method(
5528 char_u **arg,
5529 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005530 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005531 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005532{
5533 char_u *name;
5534 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005535 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005536 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005537 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005538 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005539 int evaluate = evalarg != NULL
5540 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005541
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005542 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005543
Bram Moolenaarac92e252019-08-03 21:58:38 +02005544 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005545 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005546 if (alias != NULL)
5547 name = alias;
5548
5549 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005550 {
5551 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005552 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005553 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005554 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005555 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005556 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005557 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005558
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005559 // If there is no "(" immediately following, but there is further on,
5560 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5561 // Does not handle anything where "(" is part of the expression.
5562 *arg = skipwhite(*arg);
5563
5564 if (**arg != '(' && alias == NULL
5565 && (paren = vim_strchr(*arg, '(')) != NULL)
5566 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005567 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005568
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005569 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005570 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005571 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005572 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5573 if (evalarg != NULL)
5574 {
5575 getline = evalarg->eval_getline;
5576 evalarg->eval_getline = NULL;
5577 }
5578
5579 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005580 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005581 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005582 *arg = name + len;
5583 ret = FAIL;
5584 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005585 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005586 {
5587 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005588 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005589 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005590
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005591 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005592 if (getline != NULL)
5593 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005594 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005595
5596 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005597 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005598 *arg = skipwhite(*arg);
5599
5600 if (**arg != '(')
5601 {
5602 if (verbose)
5603 semsg(_(e_missing_parenthesis_str), name);
5604 ret = FAIL;
5605 }
5606 else if (VIM_ISWHITE((*arg)[-1]))
5607 {
5608 if (verbose)
5609 emsg(_(e_no_white_space_allowed_before_parenthesis));
5610 ret = FAIL;
5611 }
5612 else
5613 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005614 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005615 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005616 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005617
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005618 // Clear the funcref afterwards, so that deleting it while
5619 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005620 if (evaluate)
5621 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005622 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005623
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005624 if (alias != NULL)
5625 vim_free(alias);
5626
Bram Moolenaarac92e252019-08-03 21:58:38 +02005627 return ret;
5628}
5629
5630/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005631 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5632 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5634 */
5635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005636eval_index(
5637 char_u **arg,
5638 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005639 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005640 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005641{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005642 int evaluate = evalarg != NULL
5643 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005645 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005648 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005649 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005651 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5652 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005654 init_tv(&var1);
5655 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 /*
5659 * dict.name
5660 */
5661 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005662 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005664 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005666 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667 }
5668 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 /*
5671 * something[idx]
5672 *
5673 * Get the (first) variable from inside the [].
5674 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005675 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 if (**arg == ':')
5677 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005678 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005680 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005681 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005682 semsg(_(e_white_space_required_before_and_after_str_at_str),
5683 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005684 clear_tv(&var1);
5685 return FAIL;
5686 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005687 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005688 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005689 int error = FALSE;
5690
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005691 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005692 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005693 && var1.v_type == VAR_FLOAT)
5694 {
5695 var1.vval.v_string = typval_tostring(&var1, TRUE);
5696 var1.v_type = VAR_STRING;
5697 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005698
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005699 if (vim9script && (rettv->v_type == VAR_LIST
5700 || rettv->v_type == VAR_TUPLE))
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005701 tv_get_number_chk(&var1, &error);
5702 else
5703 error = tv_get_string_chk(&var1) == NULL;
5704 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005705 {
5706 // not a number or string
5707 clear_tv(&var1);
5708 return FAIL;
5709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711
5712 /*
5713 * Get the second variable from inside the [:].
5714 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005715 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 if (**arg == ':')
5717 {
5718 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005719 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005720 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005721 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005722 semsg(_(e_white_space_required_before_and_after_str_at_str),
5723 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005724 if (!empty1)
5725 clear_tv(&var1);
5726 return FAIL;
5727 }
5728 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 if (**arg == ']')
5730 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005731 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005733 if (!empty1)
5734 clear_tv(&var1);
5735 return FAIL;
5736 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005737 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005738 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005739 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005740 if (!empty1)
5741 clear_tv(&var1);
5742 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 return FAIL;
5744 }
5745 }
5746
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005747 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005748 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 if (**arg != ']')
5750 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005751 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005752 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 clear_tv(&var1);
5754 if (range)
5755 clear_tv(&var2);
5756 return FAIL;
5757 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005758 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 }
5760
5761 if (evaluate)
5762 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005763 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005764 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005765 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005766
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005767 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005768 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005770 clear_tv(&var2);
5771 return res;
5772 }
5773 return OK;
5774}
5775
5776/*
5777 * Check if "rettv" can have an [index] or [sli:ce]
5778 */
5779 int
5780check_can_index(typval_T *rettv, int evaluate, int verbose)
5781{
5782 switch (rettv->v_type)
5783 {
5784 case VAR_FUNC:
5785 case VAR_PARTIAL:
5786 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005787 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005788 return FAIL;
5789 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005790 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005791 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005792 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005793 case VAR_BOOL:
5794 case VAR_SPECIAL:
5795 case VAR_JOB:
5796 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005797 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005798 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005799 if (verbose)
5800 emsg(_(e_cannot_index_special_variable));
5801 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005802 case VAR_CLASS:
5803 case VAR_TYPEALIAS:
5804 if (verbose)
5805 check_typval_is_value(rettv);
5806 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005807 case VAR_UNKNOWN:
5808 case VAR_ANY:
5809 case VAR_VOID:
5810 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005812 emsg(_(e_cannot_index_special_variable));
5813 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005815 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005817 case VAR_STRING:
5818 case VAR_LIST:
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005819 case VAR_TUPLE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005820 case VAR_DICT:
5821 case VAR_BLOB:
5822 break;
5823 case VAR_NUMBER:
5824 if (in_vim9script())
5825 emsg(_(e_cannot_index_number));
5826 break;
5827 }
5828 return OK;
5829}
5830
5831/*
5832 * Apply index or range to "rettv".
5833 * "var1" is the first index, NULL for [:expr].
5834 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005835 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5836 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005837 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5838 */
5839 int
5840eval_index_inner(
5841 typval_T *rettv,
5842 int is_range,
5843 typval_T *var1,
5844 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005845 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005846 char_u *key,
5847 int keylen,
5848 int verbose)
5849{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005850 varnumber_T n1, n2 = 0;
5851 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005852
5853 n1 = 0;
5854 if (var1 != NULL && rettv->v_type != VAR_DICT)
5855 n1 = tv_get_number(var1);
5856
5857 if (is_range)
5858 {
5859 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005861 if (verbose)
5862 emsg(_(e_cannot_slice_dictionary));
5863 return FAIL;
5864 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005865 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005866 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005867 else
5868 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005869 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005870
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005871 switch (rettv->v_type)
5872 {
5873 case VAR_UNKNOWN:
5874 case VAR_ANY:
5875 case VAR_VOID:
5876 case VAR_FUNC:
5877 case VAR_PARTIAL:
5878 case VAR_FLOAT:
5879 case VAR_BOOL:
5880 case VAR_SPECIAL:
5881 case VAR_JOB:
5882 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005883 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005884 case VAR_CLASS:
5885 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005886 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005887 break; // not evaluating, skipping over subscript
5888
5889 case VAR_NUMBER:
5890 case VAR_STRING:
5891 {
5892 char_u *s = tv_get_string(rettv);
5893
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005895 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005896 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005897 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005898 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005899 else
5900 s = char_from_string(s, n1);
5901 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005902 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005904 // The resulting variable is a substring. If the indexes
5905 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 if (n1 < 0)
5907 {
5908 n1 = len + n1;
5909 if (n1 < 0)
5910 n1 = 0;
5911 }
5912 if (n2 < 0)
5913 n2 = len + n2;
5914 else if (n2 >= len)
5915 n2 = len;
5916 if (n1 >= len || n2 < 0 || n1 > n2)
5917 s = NULL;
5918 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005919 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 }
5921 else
5922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005923 // The resulting variable is a string of a single
5924 // character. If the index is too big or negative the
5925 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 if (n1 >= len || n1 < 0)
5927 s = NULL;
5928 else
5929 s = vim_strnsave(s + n1, 1);
5930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005931 clear_tv(rettv);
5932 rettv->v_type = VAR_STRING;
5933 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005934 }
5935 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005937 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005938 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5939 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005940 break;
5941
5942 case VAR_LIST:
5943 if (var1 == NULL)
5944 n1 = 0;
5945 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005946 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005947 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005948 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005949 return FAIL;
5950 break;
5951
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01005952 case VAR_TUPLE:
5953 if (var1 == NULL)
5954 n1 = 0;
5955 if (var2 == NULL)
5956 n2 = VARNUM_MAX;
5957 if (tuple_slice_or_index(rettv->vval.v_tuple,
5958 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
5959 return FAIL;
5960 break;
5961
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005962 case VAR_DICT:
5963 {
5964 dictitem_T *item;
5965 typval_T tmp;
5966
5967 if (key == NULL)
5968 {
5969 key = tv_get_string_chk(var1);
5970 if (key == NULL)
5971 return FAIL;
5972 }
5973
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005974 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005975
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005976 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005977 {
5978 if (verbose)
5979 {
5980 if (keylen > 0)
5981 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005982 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005983 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005984 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005985 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005986
5987 copy_tv(&item->di_tv, &tmp);
5988 clear_tv(rettv);
5989 *rettv = tmp;
5990 }
5991 break;
5992 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993 return OK;
5994}
5995
5996/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005997 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005998 */
5999 char_u *
6000partial_name(partial_T *pt)
6001{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02006002 if (pt != NULL)
6003 {
6004 if (pt->pt_name != NULL)
6005 return pt->pt_name;
6006 if (pt->pt_func != NULL)
6007 return pt->pt_func->uf_name;
6008 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02006009 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006010}
6011
Bram Moolenaarddecc252016-04-06 22:59:37 +02006012 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006013partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02006014{
6015 int i;
6016
6017 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006018 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006019 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006020 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006021 if (pt->pt_name != NULL)
6022 {
6023 func_unref(pt->pt_name);
6024 vim_free(pt->pt_name);
6025 }
6026 else
6027 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02006028 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02006029
Bram Moolenaar54656012021-06-09 20:50:46 +02006030 // "out_up" is no longer used, decrement refcount on partial that owns it.
6031 partial_unref(pt->pt_outer.out_up_partial);
6032
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006033 // Using pt_outer from another partial.
6034 partial_unref(pt->pt_outer_partial);
6035
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02006036 // Decrease the reference count for the context of a closure. If down
6037 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02006038 if (pt->pt_funcstack != NULL)
6039 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02006040 --pt->pt_funcstack->fs_refcount;
6041 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02006042 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006043 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01006044 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
6045 if (pt->pt_loopvars[i] != NULL)
6046 {
6047 --pt->pt_loopvars[i]->lvs_refcount;
6048 loopvars_check_refcount(pt->pt_loopvars[i]);
6049 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02006050
Bram Moolenaarddecc252016-04-06 22:59:37 +02006051 vim_free(pt);
6052}
6053
6054/*
6055 * Unreference a closure: decrement the reference count and free it when it
6056 * becomes zero.
6057 */
6058 void
6059partial_unref(partial_T *pt)
6060{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006061 if (pt == NULL)
6062 return;
6063
6064 int done = FALSE;
6065
6066 if (--pt->pt_refcount <= 0)
6067 partial_free(pt);
6068
6069 // If the reference count goes down to one, the funcstack may be the
6070 // only reference and can be freed if no other partials reference it.
6071 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02006072 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006073 // careful: if the funcstack is freed it may contain this partial
6074 // and it gets freed as well
6075 if (pt->pt_funcstack != NULL)
6076 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01006077
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006078 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006079 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006080 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01006081
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00006082 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
6083 if (pt->pt_loopvars[depth] != NULL
6084 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01006085 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006086 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02006087 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02006088}
6089
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006091 * Return a textual representation of a string in "tv".
6092 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6093 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6094 * strings as "string()", otherwise does not put quotes around strings.
6095 * May return NULL.
6096 */
6097 static char_u *
6098string_tv2string(
6099 typval_T *tv,
6100 char_u **tofree,
6101 int echo_style,
6102 int composite_val)
6103{
6104 char_u *r = NULL;
6105
6106 if (echo_style && !composite_val)
6107 {
6108 *tofree = NULL;
6109 r = tv->vval.v_string;
6110 if (r == NULL)
6111 r = (char_u *)"";
6112 }
6113 else
6114 {
6115 *tofree = string_quote(tv->vval.v_string, FALSE);
6116 r = *tofree;
6117 }
6118
6119 return r;
6120}
6121
6122/*
6123 * Return a textual representation of a function in "tv".
6124 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6125 * When "echo_style" is FALSE, put quotes around the function name as
6126 * "function()", otherwise does not put quotes around function name.
6127 * May return NULL.
6128 */
6129 static char_u *
6130func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
6131{
6132 char_u *r = NULL;
6133 char_u buf[MAX_FUNC_NAME_LEN];
6134
6135 if (echo_style)
6136 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02006137 *tofree = NULL;
6138
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02006139 if (tv->vval.v_string == NULL)
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02006140 r = (char_u *)"function()";
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006141 else
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02006142 {
6143 r = make_ufunc_name_readable(tv->vval.v_string, buf,
6144 MAX_FUNC_NAME_LEN);
6145 if (r == buf)
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02006146 r = *tofree = vim_strsave(buf);
Yegappan Lakshmanan8904d672024-05-29 07:51:50 +02006147 }
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006148 }
6149 else
6150 {
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02006151 char_u *s = NULL;
6152
6153 if (tv->vval.v_string != NULL)
6154 s = make_ufunc_name_readable(tv->vval.v_string, buf,
6155 MAX_FUNC_NAME_LEN);
6156
6157 r = *tofree = string_quote(s, TRUE);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006158 }
6159
6160 return r;
6161}
6162
6163/*
Ernie Rael9d779c52024-07-07 20:41:44 +02006164 * Return a textual representation of the object method in "tv", a VAR_PARTIAL.
6165 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6166 * When "echo_style" is FALSE, put quotes around the function name as
6167 * "function()", otherwise does not put quotes around function name.
6168 * May return NULL.
6169 */
6170 static char_u *
6171method_tv2string(typval_T *tv, char_u **tofree, int echo_style)
6172{
6173 char_u buf[MAX_FUNC_NAME_LEN];
6174 partial_T *pt = tv->vval.v_partial;
6175
6176 size_t len = vim_snprintf((char *)buf, sizeof(buf), "<SNR>%d_%s.%s",
6177 pt->pt_func->uf_script_ctx.sc_sid,
6178 pt->pt_func->uf_class->class_name,
6179 pt->pt_func->uf_name);
6180 if (len >= sizeof(buf))
6181 {
6182 if (echo_style)
6183 {
6184 *tofree = NULL;
6185 return (char_u *)"function()";
6186 }
6187 else
6188 return *tofree = string_quote((char_u*)"", TRUE);
6189 }
6190
6191 return *tofree = echo_style ? vim_strsave(buf) : string_quote(buf, TRUE);
6192}
6193
6194/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006195 * Return a textual representation of a partial in "tv".
6196 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6197 * "numbuf" is used for a number. May return NULL.
6198 */
6199 static char_u *
6200partial_tv2string(
6201 typval_T *tv,
6202 char_u **tofree,
6203 char_u *numbuf,
6204 int copyID)
6205{
6206 char_u *r = NULL;
6207 partial_T *pt;
6208 char_u *fname;
6209 garray_T ga;
6210 int i;
6211 char_u *tf;
6212
6213 pt = tv->vval.v_partial;
6214 fname = string_quote(pt == NULL ? NULL : partial_name(pt), FALSE);
6215
6216 ga_init2(&ga, 1, 100);
6217 ga_concat(&ga, (char_u *)"function(");
6218 if (fname != NULL)
6219 {
6220 // When using uf_name prepend "g:" for a global function.
6221 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
6222 && vim_isupper(fname[1]))
6223 {
6224 ga_concat(&ga, (char_u *)"'g:");
6225 ga_concat(&ga, fname + 1);
6226 }
6227 else
6228 ga_concat(&ga, fname);
6229 vim_free(fname);
6230 }
6231 if (pt != NULL && pt->pt_argc > 0)
6232 {
6233 ga_concat(&ga, (char_u *)", [");
6234 for (i = 0; i < pt->pt_argc; ++i)
6235 {
6236 if (i > 0)
6237 ga_concat(&ga, (char_u *)", ");
6238 ga_concat(&ga, tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6239 vim_free(tf);
6240 }
6241 ga_concat(&ga, (char_u *)"]");
6242 }
6243 if (pt != NULL && pt->pt_dict != NULL)
6244 {
6245 typval_T dtv;
6246
6247 ga_concat(&ga, (char_u *)", ");
6248 dtv.v_type = VAR_DICT;
6249 dtv.vval.v_dict = pt->pt_dict;
6250 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6251 vim_free(tf);
6252 }
6253 // terminate with ')' and a NUL
6254 ga_concat_len(&ga, (char_u *)")", 2);
6255
6256 *tofree = ga.ga_data;
6257 r = *tofree;
6258
6259 return r;
6260}
6261
6262/*
6263 * Return a textual representation of a List in "tv".
6264 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6265 * When "copyID" is not zero replace recursive lists with "...". When
6266 * "restore_copyID" is FALSE, repeated items in lists are replaced with "...".
6267 * May return NULL.
6268 */
6269 static char_u *
6270list_tv2string(
6271 typval_T *tv,
6272 char_u **tofree,
6273 int copyID,
6274 int restore_copyID)
6275{
6276 char_u *r = NULL;
6277
6278 if (tv->vval.v_list == NULL)
6279 {
6280 // NULL list is equivalent to empty list.
6281 *tofree = NULL;
6282 r = (char_u *)"[]";
6283 }
6284 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6285 && tv->vval.v_list->lv_len > 0)
6286 {
6287 *tofree = NULL;
6288 r = (char_u *)"[...]";
6289 }
6290 else
6291 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006292 int old_copyID;
6293 if (restore_copyID)
6294 old_copyID = tv->vval.v_list->lv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006295
6296 tv->vval.v_list->lv_copyID = copyID;
6297 *tofree = list2string(tv, copyID, restore_copyID);
6298 if (restore_copyID)
6299 tv->vval.v_list->lv_copyID = old_copyID;
6300 r = *tofree;
6301 }
6302
6303 return r;
6304}
6305
6306/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006307 * Return a textual representation of a Tuple in "tv".
6308 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6309 * When "copyID" is not zero replace recursive lists with "...". When
6310 * "restore_copyID" is FALSE, repeated items in tuples are replaced with "...".
6311 * May return NULL.
6312 */
6313 static char_u *
6314tuple_tv2string(
6315 typval_T *tv,
6316 char_u **tofree,
6317 int copyID,
6318 int restore_copyID)
6319{
6320 tuple_T *tuple = tv->vval.v_tuple;
6321 char_u *r = NULL;
6322
6323 if (tuple == NULL)
6324 {
6325 // NULL tuple is equivalent to an empty tuple.
6326 *tofree = NULL;
6327 r = (char_u *)"()";
6328 }
6329 else if (copyID != 0 && tuple->tv_copyID == copyID
6330 && tuple->tv_items.ga_len > 0)
6331 {
6332 *tofree = NULL;
6333 r = (char_u *)"(...)";
6334 }
6335 else
6336 {
6337 int old_copyID;
6338 if (restore_copyID)
6339 old_copyID = tuple->tv_copyID;
6340
6341 tuple->tv_copyID = copyID;
6342 *tofree = tuple2string(tv, copyID, restore_copyID);
6343 if (restore_copyID)
6344 tuple->tv_copyID = old_copyID;
6345 r = *tofree;
6346 }
6347
6348 return r;
6349}
6350
6351/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006352 * Return a textual representation of a Dict in "tv".
6353 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6354 * When "copyID" is not zero replace recursive dicts with "...".
6355 * When "restore_copyID" is FALSE, repeated items in the dictionary are
6356 * replaced with "...". May return NULL.
6357 */
6358 static char_u *
6359dict_tv2string(
6360 typval_T *tv,
6361 char_u **tofree,
6362 int copyID,
6363 int restore_copyID)
6364{
6365 char_u *r = NULL;
6366
6367 if (tv->vval.v_dict == NULL)
6368 {
6369 // NULL dict is equivalent to empty dict.
6370 *tofree = NULL;
6371 r = (char_u *)"{}";
6372 }
6373 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6374 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
6375 {
6376 *tofree = NULL;
6377 r = (char_u *)"{...}";
6378 }
6379 else
6380 {
Christian Brabandtb335a932024-05-21 18:39:10 +02006381 int old_copyID;
6382 if (restore_copyID)
6383 old_copyID = tv->vval.v_dict->dv_copyID;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006384
6385 tv->vval.v_dict->dv_copyID = copyID;
6386 *tofree = dict2string(tv, copyID, restore_copyID);
6387 if (restore_copyID)
6388 tv->vval.v_dict->dv_copyID = old_copyID;
6389 r = *tofree;
6390 }
6391
6392 return r;
6393}
6394
6395/*
6396 * Return a textual representation of a job or a channel in "tv".
6397 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6398 * "numbuf" is used for a number.
6399 * When "composite_val" is FALSE, put quotes around strings as "string()",
6400 * otherwise does not put quotes around strings.
6401 * May return NULL.
6402 */
6403 static char_u *
6404jobchan_tv2string(
Dominique Pellé0268ff32024-07-28 21:12:20 +02006405 typval_T *tv UNUSED,
6406 char_u **tofree UNUSED,
6407 char_u *numbuf UNUSED,
6408 int composite_val UNUSED)
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006409{
6410 char_u *r = NULL;
6411
6412#ifdef FEAT_JOB_CHANNEL
6413 *tofree = NULL;
6414
6415 if (tv->v_type == VAR_JOB)
6416 r = job_to_string_buf(tv, numbuf);
6417 else
6418 r = channel_to_string_buf(tv, numbuf);
6419
6420 if (composite_val)
6421 {
6422 *tofree = string_quote(r, FALSE);
6423 r = *tofree;
6424 }
6425#endif
6426
6427 return r;
6428}
6429
6430/*
6431 * Return a textual representation of a class in "tv".
6432 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6433 * May return NULL.
6434 */
6435 static char_u *
6436class_tv2string(typval_T *tv, char_u **tofree)
6437{
6438 char_u *r = NULL;
John Marriottbd4614f2024-11-18 20:25:21 +01006439 size_t rsize;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006440 class_T *cl = tv->vval.v_class;
John Marriottbd4614f2024-11-18 20:25:21 +01006441 char_u *class_name = (char_u *)"[unknown]";
6442 size_t class_namelen = 9;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006443 char *s = "class";
John Marriottbd4614f2024-11-18 20:25:21 +01006444 size_t slen = 5;
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006445
John Marriottbd4614f2024-11-18 20:25:21 +01006446 if (cl != NULL)
6447 {
6448 class_name = cl->class_name;
6449 class_namelen = STRLEN(cl->class_name);
6450 if (IS_INTERFACE(cl))
6451 {
6452 s = "interface";
6453 slen = 9;
6454 }
6455 else if (IS_ENUM(cl))
6456 {
6457 s = "enum";
6458 slen = 4;
6459 }
6460 }
6461
6462 rsize = slen + 1 + class_namelen + 1;
6463 r = *tofree = alloc(rsize);
6464 if (r != NULL)
6465 vim_snprintf((char *)r, rsize, "%s %s", s, (char *)class_name);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006466
6467 return r;
6468}
6469
6470/*
Ernie Rael05ff4e42024-07-04 16:50:11 +02006471 * Return a textual representation of an Object in "tv".
6472 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6473 * When "copyID" is not zero replace recursive object with "...".
6474 * When "restore_copyID" is FALSE, repeated items in the object are
6475 * replaced with "...". May return NULL.
6476 */
6477 static char_u *
6478object_tv2string(
6479 typval_T *tv,
6480 char_u **tofree,
6481 int copyID,
6482 int restore_copyID,
6483 char_u *numbuf,
6484 int echo_style,
6485 int composite_val)
6486{
6487 char_u *r = NULL;
6488
6489 object_T *obj = tv->vval.v_object;
6490 if (obj == NULL || obj->obj_class == NULL)
6491 {
6492 *tofree = NULL;
6493 r = (char_u *)"object of [unknown]";
6494 }
6495 else if (copyID != 0 && obj->obj_copyID == copyID
zeertzjqd9be94c2024-07-14 10:20:20 +02006496 && obj->obj_class->class_obj_member_count != 0)
Ernie Rael05ff4e42024-07-04 16:50:11 +02006497 {
John Marriottbd4614f2024-11-18 20:25:21 +01006498 size_t n = 25 + STRLEN((char *)obj->obj_class->class_name);
Ernie Rael05ff4e42024-07-04 16:50:11 +02006499 r = alloc(n);
6500 if (r != NULL)
Ernie Rael5285b3c2024-07-08 20:42:45 +02006501 (void)vim_snprintf((char *)r, n, "object of %s {...}",
Ernie Rael05ff4e42024-07-04 16:50:11 +02006502 obj->obj_class->class_name);
6503 *tofree = r;
6504 }
6505 else
6506 {
6507 int old_copyID;
6508 if (restore_copyID)
6509 old_copyID = obj->obj_copyID;
6510
6511 obj->obj_copyID = copyID;
6512 *tofree = object2string(obj, numbuf, copyID, echo_style,
6513 restore_copyID, composite_val);
6514 if (restore_copyID)
6515 obj->obj_copyID = old_copyID;
6516 r = *tofree;
6517 }
6518
6519 return r;
6520}
6521
6522/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 * Return a string with the string representation of a variable.
6524 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 * "numbuf" is used for a number.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006526 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006527 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006528 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006529 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006530 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6531 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006532 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006534 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006535echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006536 typval_T *tv,
6537 char_u **tofree,
6538 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006539 int copyID,
6540 int echo_style,
6541 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006542 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 static int recurse = 0;
6545 char_u *r = NULL;
6546
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006549 if (!did_echo_string_emsg)
6550 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006551 // Only give this message once for a recursive call to avoid
6552 // flooding the user with errors. And stop iterating over lists
Ernie Rael05ff4e42024-07-04 16:50:11 +02006553 // and dicts and objects.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006554 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006555 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006558 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006559 }
6560 ++recurse;
6561
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 switch (tv->v_type)
6563 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006564 case VAR_STRING:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006565 r = string_tv2string(tv, tofree, echo_style, composite_val);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006566 break;
6567
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 case VAR_FUNC:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006569 r = func_tv2string(tv, tofree, echo_style);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006570 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006571
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006572 case VAR_PARTIAL:
Ernie Rael9d779c52024-07-07 20:41:44 +02006573 if (tv->vval.v_partial == NULL
6574 || tv->vval.v_partial->pt_obj == NULL)
6575 r = partial_tv2string(tv, tofree, numbuf, copyID);
6576 else
6577 r = method_tv2string(tv, tofree, echo_style);
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006578 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006579
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006580 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006581 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006582 break;
6583
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584 case VAR_LIST:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006585 r = list_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006586 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006587
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01006588 case VAR_TUPLE:
6589 r = tuple_tv2string(tv, tofree, copyID, restore_copyID);
6590 break;
6591
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 case VAR_DICT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006593 r = dict_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006594 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006595
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006596 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006597 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006598 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006600 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006601 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006602 break;
6603
Bram Moolenaar835dc632016-02-07 14:27:38 +01006604 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006605 case VAR_CHANNEL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006606 r = jobchan_tv2string(tv, tofree, numbuf, composite_val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006608
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006609 case VAR_INSTR:
6610 *tofree = NULL;
6611 r = (char_u *)"instructions";
6612 break;
6613
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006614 case VAR_CLASS:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006615 r = class_tv2string(tv, tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006616 break;
6617
6618 case VAR_OBJECT:
Ernie Rael05ff4e42024-07-04 16:50:11 +02006619 r = object_tv2string(tv, tofree, copyID, restore_copyID,
6620 numbuf, echo_style, composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006621 break;
6622
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006623 case VAR_FLOAT:
6624 *tofree = NULL;
6625 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6626 r = numbuf;
6627 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006628
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006629 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006630 case VAR_SPECIAL:
6631 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006632 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006633 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006634
6635 case VAR_TYPEALIAS:
6636 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6637 r = *tofree;
6638 if (r == NULL)
6639 r = (char_u *)"";
6640 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642
Bram Moolenaar8502c702014-06-17 12:51:16 +02006643 if (--recurse == 0)
6644 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006645 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006646}
6647
6648/*
6649 * Return a string with the string representation of a variable.
6650 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6651 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006652 * Does not put quotes around strings, as ":echo" displays values.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006653 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006654 * May return NULL.
6655 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006656 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006657echo_string(
6658 typval_T *tv,
6659 char_u **tofree,
6660 char_u *numbuf,
6661 int copyID)
6662{
6663 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6664}
6665
6666/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006667 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6668 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006669 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006670 */
6671 int
6672buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6673{
6674 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006675 char_u *t;
6676 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006677
6678 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6679 return -1;
6680
6681 if (lnum > buf->b_ml.ml_line_count)
6682 lnum = buf->b_ml.ml_line_count;
6683
6684 str = ml_get_buf(buf, lnum, FALSE);
6685 if (str == NULL)
6686 return -1;
6687
6688 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006689 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006690
Bram Moolenaar91458462021-01-13 20:08:38 +01006691 // count the number of characters
6692 t = str;
6693 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6694 t += mb_ptr2len(t);
6695
6696 // In insert mode, when the cursor is at the end of a non-empty line,
6697 // byteidx points to the NUL character immediately past the end of the
6698 // string. In this case, add one to the character count.
6699 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6700 count++;
6701
6702 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006703}
6704
6705/*
6706 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006707 * byte index. Works only for loaded buffers. Returns -1 on failure.
6708 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006709 */
6710 int
6711buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6712{
6713 char_u *str;
6714 char_u *t;
6715
6716 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6717 return -1;
6718
6719 if (lnum > buf->b_ml.ml_line_count)
6720 lnum = buf->b_ml.ml_line_count;
6721
6722 str = ml_get_buf(buf, lnum, FALSE);
6723 if (str == NULL)
6724 return -1;
6725
6726 // Convert the character offset to a byte offset
6727 t = str;
6728 while (*t != NUL && --charidx > 0)
6729 t += mb_ptr2len(t);
6730
Bram Moolenaar91458462021-01-13 20:08:38 +01006731 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006732}
6733
6734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006736 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006739var2fpos(
6740 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006741 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006742 int *fnum, // set to fnum for '0, 'A, etc.
6743 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006745 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006747 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006749 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006750 if (varp->v_type == VAR_LIST)
6751 {
6752 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006753 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006754 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006755 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006756
6757 l = varp->vval.v_list;
6758 if (l == NULL)
6759 return NULL;
6760
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006761 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006762 pos.lnum = list_find_nr(l, 0L, &error);
6763 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006764 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006765 if (charcol)
6766 len = (long)mb_charlen(ml_get(pos.lnum));
6767 else
John Marriottbfcc8952024-03-11 22:04:45 +01006768 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006769
Bram Moolenaarec65d772020-08-20 22:29:12 +02006770 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006771 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006772 li = list_find(l, 1L);
6773 if (li != NULL && li->li_tv.v_type == VAR_STRING
6774 && li->li_tv.vval.v_string != NULL
6775 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006776 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006777 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006778 }
6779 else
6780 {
6781 pos.col = list_find_nr(l, 1L, &error);
6782 if (error)
6783 return NULL;
6784 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006786 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006787 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006788 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006789 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006791 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006792 pos.coladd = list_find_nr(l, 2L, &error);
6793 if (error)
6794 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006795
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006796 return &pos;
6797 }
6798
Bram Moolenaarc5809432021-03-27 21:23:30 +01006799 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6800 return NULL;
6801
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006802 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006803 if (name == NULL)
6804 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006805
6806 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006807 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006808 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006809 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006810 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006811 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006812 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006813 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006814 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006815 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006816 pos = VIsual;
6817 else
6818 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006819 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006820 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006821 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006823 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006824 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6826 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006827 pos = *pp;
6828 }
6829 if (pos.lnum != 0)
6830 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006831 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006832 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6833 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006835
Bram Moolenaara5525202006-03-02 22:52:09 +00006836 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006837
Bram Moolenaar477933c2007-07-17 14:32:23 +00006838 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006839 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006840 // the "w_valid" flags are not reset when moving the cursor, but they
6841 // do matter for update_topline() and validate_botline().
6842 check_cursor_moved(curwin);
6843
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006844 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006845 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006846 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006847 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006848 // In silent Ex mode topline is zero, but that's not a valid line
6849 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006850 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006851 return &pos;
6852 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006853 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006854 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006855 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006856 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006857 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006858 return &pos;
6859 }
6860 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006861 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006863 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {
6865 pos.lnum = curbuf->b_ml.ml_line_count;
6866 pos.col = 0;
6867 }
6868 else
6869 {
6870 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006871 if (charcol)
6872 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6873 else
John Marriottbfcc8952024-03-11 22:04:45 +01006874 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876 return &pos;
6877 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006878 if (in_vim9script())
6879 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 return NULL;
6881}
6882
6883/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006884 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006885 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006886 * Note that the column is passed on as-is, the caller may want to decrement
6887 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006888 * If "charcol" is TRUE use the column as the character index instead of the
6889 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006890 * Return FAIL when conversion is not possible, doesn't check the position for
6891 * validity.
6892 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006894list2fpos(
6895 typval_T *arg,
6896 pos_T *posp,
6897 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006898 colnr_T *curswantp,
6899 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006900{
6901 list_T *l = arg->vval.v_list;
6902 long i = 0;
6903 long n;
6904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006905 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6906 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006907 if (arg->v_type != VAR_LIST
6908 || l == NULL
6909 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006910 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006911 return FAIL;
6912
6913 if (fnump != NULL)
6914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006915 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006916 if (n < 0)
6917 return FAIL;
6918 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006919 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006920 *fnump = n;
6921 }
6922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006923 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006924 if (n < 0)
6925 return FAIL;
6926 posp->lnum = n;
6927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006928 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006929 if (n < 0)
6930 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006931 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006932 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006933 if (charcol)
6934 {
6935 buf_T *buf;
6936
6937 // Get the text for the specified line in a loaded buffer
6938 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6939 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6940 return FAIL;
6941
Bram Moolenaar79f23442022-10-10 12:42:57 +01006942 n = buf_charidx_to_byteidx(buf,
6943 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006944 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006945 posp->col = n;
6946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006947 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006948 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006949 posp->coladd = 0;
6950 else
6951 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006952
Bram Moolenaar493c1782014-05-28 14:34:46 +02006953 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006954 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006955
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006956 return OK;
6957}
6958
6959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 * Get the length of an environment variable name.
6961 * Advance "arg" to the first character after the name.
6962 * Return 0 for error.
6963 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 char_u *p;
6968 int len;
6969
6970 for (p = *arg; vim_isIDc(*p); ++p)
6971 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006972 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 return 0;
6974
6975 len = (int)(p - *arg);
6976 *arg = p;
6977 return len;
6978}
6979
6980/*
6981 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006982 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 * Return 0 if something is wrong.
6984 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 char_u *p;
6989 int len;
6990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006991 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006993 {
6994 if (*p == ':')
6995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006996 // "s:" is start of "s:var", but "n:" is not and can be used in
6997 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006998 len = (int)(p - *arg);
6999 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
7000 || len > 1)
7001 break;
7002 }
7003 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007004 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 return 0;
7006
7007 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007008 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
7010 return len;
7011}
7012
7013/*
Bram Moolenaara7043832005-01-21 11:56:39 +00007014 * Get the length of the name of a variable or function.
7015 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007017 * Return -1 if curly braces expansion failed.
7018 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 * If the name contains 'magic' {}'s, expand them and return the
7020 * expanded name in an allocated string via 'alias' - caller must free.
7021 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023get_name_len(
7024 char_u **arg,
7025 char_u **alias,
7026 int evaluate,
7027 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028{
7029 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 char_u *p;
7031 char_u *expr_start;
7032 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007034 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
7036 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
7037 && (*arg)[2] == (int)KE_SNR)
7038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007039 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 *arg += 3;
7041 return get_id_len(arg) + 3;
7042 }
7043 len = eval_fname_script(*arg);
7044 if (len > 0)
7045 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007046 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 *arg += len;
7048 }
7049
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007051 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007053 p = find_name_end(*arg, &expr_start, &expr_end,
7054 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 if (expr_start != NULL)
7056 {
7057 char_u *temp_string;
7058
7059 if (!evaluate)
7060 {
7061 len += (int)(p - *arg);
7062 *arg = skipwhite(p);
7063 return len;
7064 }
7065
7066 /*
7067 * Include any <SID> etc in the expanded string:
7068 * Thus the -len here.
7069 */
7070 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
7071 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007072 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 *alias = temp_string;
7074 *arg = skipwhite(p);
7075 return (int)STRLEN(temp_string);
7076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
7078 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01007079 // Only give an error when there is something, otherwise it will be
7080 // reported at a higher level.
7081 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007082 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
7084 return len;
7085}
7086
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007087/*
7088 * Find the end of a variable or function name, taking care of magic braces.
7089 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
7090 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007091 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007092 * Return a pointer to just after the name. Equal to "arg" if there is no
7093 * valid name.
7094 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007096find_name_end(
7097 char_u *arg,
7098 char_u **expr_start,
7099 char_u **expr_end,
7100 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007102 int mb_nest = 0;
7103 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007105 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02007106 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007108 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007110 *expr_start = NULL;
7111 *expr_end = NULL;
7112 }
7113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007114 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007115 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007116 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007117 return arg;
7118
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007119 for (p = arg; *p != NUL
7120 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007121 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02007122 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007123 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007124 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007125 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007126 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00007127 if (*p == '\'')
7128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007129 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007130 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007131 ;
7132 if (*p == NUL)
7133 break;
7134 }
7135 else if (*p == '"')
7136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007137 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007138 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007139 if (*p == '\\' && p[1] != NUL)
7140 ++p;
7141 if (*p == NUL)
7142 break;
7143 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007144 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
7145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007146 // "s:" is start of "s:var", but "n:" is not and can be used in
7147 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007148 len = (int)(p - arg);
7149 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007150 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007151 break;
7152 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007154 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007156 if (*p == '[')
7157 ++br_nest;
7158 else if (*p == ']')
7159 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007161
zeertzjqa93d9cd2023-05-02 16:25:47 +01007162 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007164 if (*p == '{')
7165 {
7166 mb_nest++;
7167 if (expr_start != NULL && *expr_start == NULL)
7168 *expr_start = p;
7169 }
7170 else if (*p == '}')
7171 {
7172 mb_nest--;
7173 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
7174 *expr_end = p;
7175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 }
7178
7179 return p;
7180}
7181
7182/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007183 * Expands out the 'magic' {}'s in a variable/function name.
7184 * Note that this can call itself recursively, to deal with
7185 * constructs like foo{bar}{baz}{bam}
7186 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7187 * "in_start" ^
7188 * "expr_start" ^
7189 * "expr_end" ^
7190 * "in_end" ^
7191 *
7192 * Returns a new allocated string, which the caller must free.
7193 * Returns NULL for failure.
7194 */
7195 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007196make_expanded_name(
7197 char_u *in_start,
7198 char_u *expr_start,
7199 char_u *expr_end,
7200 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007201{
7202 char_u c1;
7203 char_u *retval = NULL;
7204 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007205
7206 if (expr_end == NULL || in_end == NULL)
7207 return NULL;
7208 *expr_start = NUL;
7209 *expr_end = NUL;
7210 c1 = *in_end;
7211 *in_end = NUL;
7212
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007213 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007214 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007215 {
John Marriottbd4614f2024-11-18 20:25:21 +01007216 size_t retvalsize = (size_t)(expr_start - in_start)
7217 + STRLEN(temp_result)
7218 + (size_t)(in_end - expr_end) + 1;
7219
7220 retval = alloc(retvalsize);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007221 if (retval != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01007222 vim_snprintf((char *)retval, retvalsize, "%s%s%s",
7223 in_start, temp_result, expr_end + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007224 }
7225 vim_free(temp_result);
7226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007227 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007228 *expr_start = '{';
7229 *expr_end = '}';
7230
7231 if (retval != NULL)
7232 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007233 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007234 if (expr_start != NULL)
7235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007236 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007237 temp_result = make_expanded_name(retval, expr_start,
7238 expr_end, temp_result);
7239 vim_free(retval);
7240 retval = temp_result;
7241 }
7242 }
7243
7244 return retval;
7245}
7246
7247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007249 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007252eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007254 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007255}
7256
7257/*
7258 * Return TRUE if character "c" can be used as the first character in a
7259 * variable or function name (excluding '{' and '}').
7260 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007262eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007263{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007264 return ASCII_ISALPHA(c) || c == '_';
7265}
7266
7267/*
7268 * Return TRUE if character "c" can be used as the first character of a
7269 * dictionary key.
7270 */
7271 int
7272eval_isdictc(int c)
7273{
7274 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275}
7276
7277/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007278 * Handle:
7279 * - expr[expr], expr[expr:expr] subscript
7280 * - ".name" lookup
7281 * - function call with Funcref variable: func(expr)
7282 * - method call: var->method()
7283 *
7284 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007285 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007286 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007288handle_subscript(
7289 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007290 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007291 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007292 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007293 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007294{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007295 int evaluate = evalarg != NULL
7296 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007297 int ret = OK;
7298 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007299 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007300 int getnext;
7301 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007302
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007303 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007304 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007305 // When at the end of the line and ".name" or "->{" or "->X" follows in
7306 // the next line then consume the line break.
7307 p = eval_next_non_blank(*arg, evalarg, &getnext);
7308 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007309 && ((*p == '.'
7310 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7311 || rettv->v_type == VAR_CLASS
7312 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007313 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7314 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7315 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007316 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007317 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007318 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007319 check_white = FALSE;
7320 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007321
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007322 if (rettv->v_type == VAR_ANY)
7323 {
7324 char_u *exp_name;
7325 int cc;
7326 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007327 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007328 type_T *type;
7329
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007330 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007331 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007332 if (**arg != '.')
7333 {
7334 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007335 semsg(_(e_expected_dot_after_name_str),
7336 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007337 ret = FAIL;
7338 break;
7339 }
7340 ++*arg;
7341 if (IS_WHITE_OR_NUL(**arg))
7342 {
7343 if (verbose)
7344 emsg(_(e_no_white_space_allowed_after_dot));
7345 ret = FAIL;
7346 break;
7347 }
7348
7349 // isolate the name
7350 exp_name = *arg;
7351 while (eval_isnamec(**arg))
7352 ++*arg;
7353 cc = **arg;
7354 **arg = NUL;
7355
7356 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007357 evalarg == NULL ? NULL : evalarg->eval_cctx,
7358 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007359 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007360
7361 if (idx < 0 && ufunc == NULL)
7362 {
7363 ret = FAIL;
7364 break;
7365 }
7366 if (idx >= 0)
7367 {
7368 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7369 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7370
7371 copy_tv(sv->sv_tv, rettv);
7372 }
7373 else
7374 {
7375 rettv->v_type = VAR_FUNC;
John Marriottb32800f2025-02-01 15:25:34 +01007376 rettv->vval.v_string = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007377 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007378 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007379 }
7380
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007381 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7382 || rettv->v_type == VAR_PARTIAL))
7383 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007384 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007385 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7386 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007387
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007388 // Stop the expression evaluation when immediately aborting on
7389 // error, or when an interrupt occurred or an exception was thrown
7390 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007391 if (aborting())
7392 {
7393 if (ret == OK)
7394 clear_tv(rettv);
7395 ret = FAIL;
7396 }
7397 dict_unref(selfdict);
7398 selfdict = NULL;
7399 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007400 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007401 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007402 if (in_vim9script())
7403 *arg = skipwhite(p + 2);
7404 else
7405 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007406 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007407 {
zeertzjqc481ad32023-03-11 16:18:51 +00007408 emsg(_(e_no_white_space_allowed_before_parenthesis));
7409 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007410 }
zeertzjqc481ad32023-03-11 16:18:51 +00007411 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7412 // expr->{lambda}() or expr->(lambda)()
7413 ret = eval_lambda(arg, rettv, evalarg, verbose);
7414 else
7415 // expr->name()
7416 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007417 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007418 // "." is ".name" lookup when we found a dict or when evaluating and
7419 // scriptversion is at least 2, where string concatenation is "..".
7420 else if (**arg == '['
7421 || (**arg == '.' && (rettv->v_type == VAR_DICT
7422 || (!evaluate
7423 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007424 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007425 {
7426 dict_unref(selfdict);
7427 if (rettv->v_type == VAR_DICT)
7428 {
7429 selfdict = rettv->vval.v_dict;
7430 if (selfdict != NULL)
7431 ++selfdict->dv_refcount;
7432 }
7433 else
7434 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007435 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007436 {
7437 clear_tv(rettv);
7438 ret = FAIL;
7439 }
7440 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007441 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7442 || rettv->v_type == VAR_OBJECT))
7443 {
7444 // class member: SomeClass.varname
7445 // class method: SomeClass.SomeMethod()
7446 // class constructor: SomeClass.new()
7447 // object member: someObject.varname
7448 // object method: someObject.SomeMethod()
7449 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7450 {
7451 clear_tv(rettv);
7452 ret = FAIL;
7453 }
7454 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007455 else
7456 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007457 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007459 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7460 // Don't do this when "Func" is already a partial that was bound
7461 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007462 if (selfdict != NULL
7463 && (rettv->v_type == VAR_FUNC
7464 || (rettv->v_type == VAR_PARTIAL
7465 && (rettv->vval.v_partial->pt_auto
7466 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007467 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007468
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007469 dict_unref(selfdict);
7470 return ret;
7471}
7472
7473/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474 * Make a copy of an item.
7475 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007476 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007477 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7478 * reference to an already copied list/dict can be used.
7479 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007482item_copy(
7483 typval_T *from,
7484 typval_T *to,
7485 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007486 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007487 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488{
7489 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007490 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007494 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007495 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007496 }
7497 ++recurse;
7498
7499 switch (from->v_type)
7500 {
7501 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007502 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503 case VAR_STRING:
7504 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007505 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007506 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007507 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007508 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007509 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007510 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007511 case VAR_CLASS:
7512 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007513 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007514 copy_tv(from, to);
7515 break;
7516 case VAR_LIST:
7517 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007518 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007519 if (from->vval.v_list == NULL)
7520 to->vval.v_list = NULL;
7521 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007523 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007524 to->vval.v_list = from->vval.v_list->lv_copylist;
7525 ++to->vval.v_list->lv_refcount;
7526 }
7527 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007528 to->vval.v_list = list_copy(from->vval.v_list,
7529 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007530 if (to->vval.v_list == NULL)
7531 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007532 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007533 case VAR_TUPLE:
7534 to->v_type = VAR_TUPLE;
7535 to->v_lock = 0;
7536 if (from->vval.v_tuple == NULL)
7537 to->vval.v_tuple = NULL;
7538 else if (copyID != 0 && from->vval.v_tuple->tv_copyID == copyID)
7539 {
7540 // use the copy made earlier
7541 to->vval.v_tuple = from->vval.v_tuple->tv_copytuple;
7542 ++to->vval.v_tuple->tv_refcount;
7543 }
7544 else
7545 to->vval.v_tuple = tuple_copy(from->vval.v_tuple,
7546 deep, top, copyID);
7547 if (to->vval.v_tuple == NULL)
7548 ret = FAIL;
7549 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007550 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007552 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007553 case VAR_DICT:
7554 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007555 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007556 if (from->vval.v_dict == NULL)
7557 to->vval.v_dict = NULL;
7558 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007560 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007561 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7562 ++to->vval.v_dict->dv_refcount;
7563 }
7564 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007565 to->vval.v_dict = dict_copy(from->vval.v_dict,
7566 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007567 if (to->vval.v_dict == NULL)
7568 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007569 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007570 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007571 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007573 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007574 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 }
7576 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007577 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007578}
7579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580 void
7581echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7582{
7583 char_u *tofree;
7584 char_u numbuf[NUMBUFLEN];
7585 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7586
7587 if (*atstart)
7588 {
7589 *atstart = FALSE;
7590 // Call msg_start() after eval1(), evaluating the expression
7591 // may cause a message to appear.
7592 if (with_space)
7593 {
7594 // Mark the saved text as finishing the line, so that what
7595 // follows is displayed on a new line when scrolling back
7596 // at the more prompt.
7597 msg_sb_eol();
7598 msg_start();
7599 }
7600 }
7601 else if (with_space)
7602 msg_puts_attr(" ", echo_attr);
7603
7604 if (p != NULL)
7605 for ( ; *p != NUL && !got_int; ++p)
7606 {
7607 if (*p == '\n' || *p == '\r' || *p == TAB)
7608 {
7609 if (*p != TAB && *needclr)
7610 {
7611 // remove any text still there from the command
7612 msg_clr_eos();
7613 *needclr = FALSE;
7614 }
7615 msg_putchar_attr(*p, echo_attr);
7616 }
7617 else
7618 {
7619 if (has_mbyte)
7620 {
7621 int i = (*mb_ptr2len)(p);
7622
7623 (void)msg_outtrans_len_attr(p, i, echo_attr);
7624 p += i - 1;
7625 }
7626 else
7627 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7628 }
7629 }
7630 vim_free(tofree);
7631}
7632
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 * ":echo expr1 ..." print each argument separated with a space, add a
7635 * newline at the end.
7636 * ":echon expr1 ..." print each argument plain.
7637 */
7638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007639ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007643 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 int needclr = TRUE;
7645 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007646 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007647 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007648 evalarg_T evalarg;
7649
Bram Moolenaare707c882020-07-01 13:07:37 +02007650 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651
7652 if (eap->skip)
7653 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007654 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007656 // If eval1() causes an error message the text from the command may
7657 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007658 need_clr_eos = needclr;
7659
Bram Moolenaar68db9962021-05-09 23:19:22 +02007660 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007661 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {
7663 /*
7664 * Report the invalid expression unless the expression evaluation
7665 * has been cancelled due to an aborting error, an interrupt, or an
7666 * exception.
7667 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007668 if (!aborting() && did_emsg == did_emsg_before
7669 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007670 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007671 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 break;
7673 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007674 need_clr_eos = FALSE;
7675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007677 {
7678 if (rettv.v_type == VAR_VOID)
7679 {
7680 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7681 break;
7682 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007684 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007685
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 arg = skipwhite(arg);
7688 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007689 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007690 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691
7692 if (eap->skip)
7693 --emsg_skip;
7694 else
7695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007696 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 if (needclr)
7698 msg_clr_eos();
7699 if (eap->cmdidx == CMD_echo)
7700 msg_end();
7701 }
7702}
7703
7704/*
7705 * ":echohl {name}".
7706 */
7707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007708ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007710 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
7712
7713/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007714 * Returns the :echo attribute
7715 */
7716 int
7717get_echo_attr(void)
7718{
7719 return echo_attr;
7720}
7721
7722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 * ":execute expr1 ..." execute the result of an expression.
7724 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007725 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007727 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 * Each gets spaces around each argument and a newline at the end for
7729 * echo commands
7730 */
7731 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007732ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 int ret = OK;
7737 char_u *p;
7738 garray_T ga;
7739 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007740 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741
7742 ga_init2(&ga, 1, 80);
7743
7744 if (eap->skip)
7745 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007746 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007748 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007749 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
7752 if (!eap->skip)
7753 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007754 char_u buf[NUMBUFLEN];
7755
7756 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007757 {
7758 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7759 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007760 semsg(_(e_using_invalid_value_as_string_str),
7761 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007762 p = NULL;
7763 }
7764 else
7765 p = tv_get_string_buf(&rettv, buf);
7766 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007767 else
7768 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007769 if (p == NULL)
7770 {
7771 clear_tv(&rettv);
7772 ret = FAIL;
7773 break;
7774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 len = (int)STRLEN(p);
7776 if (ga_grow(&ga, len + 2) == FAIL)
7777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 ret = FAIL;
7780 break;
7781 }
7782 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 ga.ga_len += len;
7786 }
7787
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 arg = skipwhite(arg);
7790 }
7791
7792 if (ret != FAIL && ga.ga_data != NULL)
7793 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007794 // use the first line of continuation lines for messages
7795 SOURCING_LNUM = start_lnum;
7796
Bram Moolenaar37fef162022-08-29 18:16:32 +01007797 if (eap->cmdidx == CMD_echomsg
7798 || eap->cmdidx == CMD_echowindow
7799 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007801 // Mark the already saved text as finishing the line, so that what
7802 // follows is displayed on a new line when scrolling back at the
7803 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007804 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007805 }
7806
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007807 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007808 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007809 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007810 out_flush();
7811 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007812 else if (eap->cmdidx == CMD_echowindow)
7813 {
7814#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007815 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007816#endif
7817 msg_attr(ga.ga_data, echo_attr);
7818#ifdef HAS_MESSAGE_WINDOW
7819 end_echowindow();
7820#endif
7821 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007822 else if (eap->cmdidx == CMD_echoconsole)
7823 {
7824 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7825 ui_write((char_u *)"\r\n", 2, TRUE);
7826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 else if (eap->cmdidx == CMD_echoerr)
7828 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007829 int save_did_emsg = did_emsg;
7830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007831 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007832 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 if (!force_abort)
7834 did_emsg = save_did_emsg;
7835 }
7836 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007837 {
7838 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7839
7840 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7841 sticky_cmdmod_flags = cmdmod.cmod_flags
7842 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007844 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007845 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848
7849 ga_clear(&ga);
7850
7851 if (eap->skip)
7852 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007853 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854}
7855
7856/*
7857 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7858 * "arg" points to the "&" or '+' when called, to "option" when returning.
7859 * Returns NULL when no option name found. Otherwise pointer to the char
7860 * after the option name.
7861 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007862 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007863find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 char_u *p = *arg;
7866
7867 ++p;
7868 if (*p == 'g' && p[1] == ':')
7869 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007870 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 p += 2;
7872 }
7873 else if (*p == 'l' && p[1] == ':')
7874 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007875 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 p += 2;
7877 }
7878 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007879 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880
7881 if (!ASCII_ISALPHA(*p))
7882 return NULL;
7883 *arg = p;
7884
7885 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007886 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 else
7888 while (ASCII_ISALPHA(*p))
7889 ++p;
7890 return p;
7891}
7892
7893/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007894 * Display script name where an item was last set.
7895 * Should only be invoked when 'verbose' is non-zero.
7896 */
7897 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007898last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007899{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007900 char_u *p;
7901
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007902 if (script_ctx.sc_sid == 0)
7903 return;
7904
7905 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7906 if (p == NULL)
7907 return;
7908
7909 verbose_enter();
7910 msg_puts(_("\n\tLast set from "));
7911 msg_puts((char *)p);
7912 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007913 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007914 msg_puts(_(line_msg));
7915 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007916 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007917 verbose_leave();
7918 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007919}
7920
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007921#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922
7923/*
7924 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007925 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 * "flags" can be "g" to do a global substitute.
7927 * Returns an allocated string, NULL for error.
7928 */
7929 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007930do_string_sub(
7931 char_u *str,
John Marriottbd4614f2024-11-18 20:25:21 +01007932 size_t len,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007933 char_u *pat,
7934 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007935 typval_T *expr,
John Marriottbd4614f2024-11-18 20:25:21 +01007936 char_u *flags,
7937 size_t *ret_len) // length of returned buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 garray_T ga;
7941 char_u *ret;
7942 char_u *save_cpo;
7943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007944 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007946 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
7948 ga_init2(&ga, 1, 200);
7949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 regmatch.rm_ic = p_ic;
7951 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7952 if (regmatch.regprog != NULL)
7953 {
John Marriottbd4614f2024-11-18 20:25:21 +01007954 char_u *tail = str;
7955 char_u *end = str + len;
7956 int do_all = (flags[0] == 'g');
7957 int sublen;
7958 int i;
7959 char_u *zero_width = NULL;
7960
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007963 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007964 if (regmatch.startp[0] == regmatch.endp[0])
7965 {
7966 if (zero_width == regmatch.startp[0])
7967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007968 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007969 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007970 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7971 (size_t)i);
7972 ga.ga_len += i;
7973 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007974 continue;
7975 }
7976 zero_width = regmatch.startp[0];
7977 }
7978
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 /*
7980 * Get some space for a temporary buffer to do the substitution
7981 * into. It will contain:
7982 * - The text up to where the match is.
7983 * - The substituted text.
7984 * - The text after the match.
7985 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007986 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007987 if (sublen <= 0)
7988 {
7989 ga_clear(&ga);
7990 break;
7991 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007992 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7994 {
7995 ga_clear(&ga);
7996 break;
7997 }
7998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007999 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 i = (int)(regmatch.startp[0] - tail);
8001 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008002 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01008003 (void)vim_regsub(&regmatch, sub, expr,
8004 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
8005 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02008007 tail = regmatch.endp[0];
8008 if (*tail == NUL)
8009 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (!do_all)
8011 break;
8012 }
8013
8014 if (ga.ga_data != NULL)
John Marriottbd4614f2024-11-18 20:25:21 +01008015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
John Marriottbd4614f2024-11-18 20:25:21 +01008017 ga.ga_len += (int)(end - tail);
8018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
Bram Moolenaar473de612013-06-08 18:19:48 +02008020 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
8022
John Marriottbd4614f2024-11-18 20:25:21 +01008023 if (ga.ga_data != NULL)
8024 {
8025 str = (char_u *)ga.ga_data;
8026 len = (size_t)ga.ga_len;
8027 }
8028 ret = vim_strnsave(str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008030 if (p_cpo == empty_option)
8031 p_cpo = save_cpo;
8032 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008034 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008035 // If it's still empty it was changed and restored, need to restore in
8036 // the complicated way.
8037 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008038 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008039 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041
John Marriottbd4614f2024-11-18 20:25:21 +01008042 if (ret_len != NULL)
8043 *ret_len = len;
8044
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 return ret;
8046}