blob: 93dc09192e89ae3af8437257456d7036755e4bec [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static 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 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131
132 if (eap == NULL)
133 return;
134
135 evalarg->eval_cstack = eap->cstack;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100136 if (sourcing_a_script(eap) || eap->ea_getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100138 evalarg->eval_getline = eap->ea_getline;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000139 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 }
141}
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Top level evaluation function, returning a boolean.
145 * Sets "error" to TRUE if there was an error.
146 * Return TRUE or FALSE.
147 */
148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_to_bool(
150 char_u *arg,
151 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200152 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100153 int skip, // only parse, don't execute
154 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
Bram Moolenaar33570922005-01-25 22:26:29 +0000156 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200157 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100159 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200160
Bram Moolenaar37c83712020-06-30 21:18:36 +0200161 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163 if (skip)
164 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100165 if (use_simple_function)
166 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
167 else
168 r = eval0(arg, &tv, eap, &evalarg);
169 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 {
173 *error = FALSE;
174 if (!skip)
175 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200176 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200177 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 else
179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200185 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200187 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190/*
191 * Call eval1() and give an error message if not done at a lower level.
192 */
193 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100196 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 int ret;
198 int did_emsg_before = did_emsg;
199 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
203
204 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200213 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 return ret;
217}
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200220 * Return whether a typval is a valid expression to pass to eval_expr_typval()
221 * or eval_expr_to_bool(). An empty string returns FALSE;
222 */
223 int
224eval_expr_valid_arg(typval_T *tv)
225{
226 return tv->v_type != VAR_UNKNOWN
227 && (tv->v_type != VAR_STRING
228 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
229}
230
231/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100232 * When calling eval_expr_typval() many times we only need one funccall_T.
233 * Returns NULL when no funccall_T is to be used.
234 * When returning non-NULL remove_funccal() must be called later.
235 */
236 funccall_T *
237eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
238{
239 if (expr->v_type != VAR_PARTIAL)
240 return NULL;
241
242 partial_T *partial = expr->vval.v_partial;
243 if (partial == NULL)
244 return NULL;
245 if (partial->pt_func == NULL
246 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
247 return NULL;
248
249 return create_funccal(partial->pt_func, rettv);
250}
251
252/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200253 * Evaluate a partial.
254 * Pass arguments "argv[argc]".
255 * "fc_arg" is from eval_expr_get_funccal() or NULL;
256 * Return the result in "rettv" and OK or FAIL.
257 */
258 static int
259eval_expr_partial(
260 typval_T *expr,
261 typval_T *argv,
262 int argc,
263 funccall_T *fc_arg,
264 typval_T *rettv)
265{
266 partial_T *partial = expr->vval.v_partial;
267
268 if (partial == NULL)
269 return FAIL;
270
271 if (partial->pt_func != NULL
272 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
273 {
274 funccall_T *fc = fc_arg != NULL ? fc_arg
275 : create_funccal(partial->pt_func, rettv);
276 int r;
277
278 if (fc == NULL)
279 return FAIL;
280
281 // Shortcut to call a compiled function with minimal overhead.
282 r = call_def_function(partial->pt_func, argc, argv, DEF_USE_PT_ARGV,
283 partial, NULL, fc, rettv);
284 if (fc_arg == NULL)
285 remove_funccal();
286 if (r == FAIL)
287 return FAIL;
288 }
289 else
290 {
291 char_u *s = partial_name(partial);
292 funcexe_T funcexe;
293
294 if (s == NULL || *s == NUL)
295 return FAIL;
296
297 CLEAR_FIELD(funcexe);
298 funcexe.fe_evaluate = TRUE;
299 funcexe.fe_partial = partial;
300 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
301 return FAIL;
302 }
303
304 return OK;
305}
306
307/*
308 * Evaluate an expression which is a function.
309 * Pass arguments "argv[argc]".
310 * Return the result in "rettv" and OK or FAIL.
311 */
312 static int
313eval_expr_func(
314 typval_T *expr,
315 typval_T *argv,
316 int argc,
317 typval_T *rettv)
318{
319 funcexe_T funcexe;
320 char_u buf[NUMBUFLEN];
321 char_u *s;
322
323 if (expr->v_type == VAR_FUNC)
324 s = expr->vval.v_string;
325 else
326 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
327 if (s == NULL || *s == NUL)
328 return FAIL;
329
330 CLEAR_FIELD(funcexe);
331 funcexe.fe_evaluate = TRUE;
332 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
333 return FAIL;
334
335 return OK;
336}
337
338/*
339 * Evaluate an expression, which is a string.
340 * Return the result in "rettv" and OK or FAIL.
341 */
342 static int
343eval_expr_string(
344 typval_T *expr,
345 typval_T *rettv)
346{
347 char_u *s;
348 char_u buf[NUMBUFLEN];
349
350 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
351 if (s == NULL)
352 return FAIL;
353
354 s = skipwhite(s);
355 if (eval1_emsg(&s, rettv, NULL) == FAIL)
356 return FAIL;
357
358 if (*skipwhite(s) != NUL) // check for trailing chars after expr
359 {
360 clear_tv(rettv);
361 semsg(_(e_invalid_expression_str), s);
362 return FAIL;
363 }
364
365 return OK;
366}
367
368/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 * Evaluate an expression, which can be a function, partial or string.
370 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200371 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100372 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 * Return the result in "rettv" and OK or FAIL.
374 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200375 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100376eval_expr_typval(
377 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200378 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100379 typval_T *argv,
380 int argc,
381 funccall_T *fc_arg,
382 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100383{
zeertzjqad0c4422023-08-17 22:15:47 +0200384 if (expr->v_type == VAR_PARTIAL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200385 return eval_expr_partial(expr, argv, argc, fc_arg, rettv);
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200386 else if (expr->v_type == VAR_INSTR)
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200387 return exe_typval_instr(expr, rettv);
zeertzjqad0c4422023-08-17 22:15:47 +0200388 else if (expr->v_type == VAR_FUNC || want_func)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200389 return eval_expr_func(expr, argv, argc, rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100390 else
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200391 return eval_expr_string(expr, rettv);
392
Bram Moolenaar48570482017-10-30 21:48:41 +0100393 return OK;
394}
395
396/*
397 * Like eval_to_bool() but using a typval_T instead of a string.
398 * Works for string, funcref and partial.
399 */
400 int
401eval_expr_to_bool(typval_T *expr, int *error)
402{
403 typval_T rettv;
404 int res;
405
zeertzjqad0c4422023-08-17 22:15:47 +0200406 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100407 {
408 *error = TRUE;
409 return FALSE;
410 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200411 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100412 clear_tv(&rettv);
413 return res;
414}
415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416/*
417 * Top level evaluation function, returning a string. If "skip" is TRUE,
418 * only parsing to "nextcmd" is done, without reporting errors. Return
419 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
420 */
421 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100422eval_to_string_skip(
423 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200424 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100425 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426{
Bram Moolenaar33570922005-01-25 22:26:29 +0000427 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200429 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
Bram Moolenaar37c83712020-06-30 21:18:36 +0200431 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 if (skip)
433 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200434 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 retval = NULL;
436 else
437 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100438 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000439 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 }
441 if (skip)
442 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200443 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445 return retval;
446}
447
448/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100449 * Initialize "evalarg" for use.
450 */
451 void
452init_evalarg(evalarg_T *evalarg)
453{
454 CLEAR_POINTER(evalarg);
455 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
456}
457
458/*
459 * If "evalarg->eval_tofree" is not NULL free it later.
460 * Caller is expected to overwrite "evalarg->eval_tofree" next.
461 */
462 static void
463free_eval_tofree_later(evalarg_T *evalarg)
464{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000465 if (evalarg->eval_tofree == NULL)
466 return;
467
468 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
469 ((char_u **)evalarg->eval_tofree_ga.ga_data)
470 [evalarg->eval_tofree_ga.ga_len++]
471 = evalarg->eval_tofree;
472 else
473 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100474}
475
476/*
477 * After using "evalarg" filled from "eap": free the memory.
478 */
479 void
480clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
481{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000482 if (evalarg == NULL)
483 return;
484
485 garray_T *etga = &evalarg->eval_tofree_ga;
486
487 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100488 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000489 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100490 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000491 // We may need to keep the original command line, e.g. for
492 // ":let" it has the variable names. But we may also need
493 // the new one, "nextcmd" points into it. Keep both.
494 vim_free(eap->cmdline_tofree);
495 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100496
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000497 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
498 {
499 // "nextcmd" points into the last line in eval_tofree_ga,
500 // need to keep it around.
501 --etga->ga_len;
502 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
503 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100504 }
505 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000506 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100507 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000508 else
509 vim_free(evalarg->eval_tofree);
510 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100511 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000512
513 ga_clear_strings(etga);
514 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100515}
516
517/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000518 * Skip over an expression at "*pp".
519 * Return FAIL for an error, OK otherwise.
520 */
521 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200522skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000523{
Bram Moolenaar33570922005-01-25 22:26:29 +0000524 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000525
526 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200527 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000528}
529
530/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200531 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200532 * If in Vim9 script and line breaks are encountered, the lines are
533 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 * "arg" is advanced to just after the expression.
535 * "start" is set to the start of the expression, "end" to just after the end.
536 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200537 * Return FAIL for an error, OK otherwise.
538 */
539 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200540skip_expr_concatenate(
541 char_u **arg,
542 char_u **start,
543 char_u **end,
544 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200545{
546 typval_T rettv;
547 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200548 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200549 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200550 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200551 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200552 int evaluate = evalarg == NULL
553 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200554
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200555 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200556 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200557 {
558 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200559 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200560 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200561 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200562 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200563 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200564 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200565
566 // Don't evaluate the expression.
567 if (evalarg != NULL)
568 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200569 *arg = skipwhite(*arg);
570 res = eval1(arg, &rettv, evalarg);
571 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200572 if (evalarg != NULL)
573 evalarg->eval_flags = save_flags;
574
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200575 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200576 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200577 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200578 if (evalarg->eval_ga.ga_len == 1)
579 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200580 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200581 ga_clear(gap);
582 gap->ga_itemsize = 0;
583 }
584 else
585 {
586 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200587 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200588
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200589 // Line breaks encountered, concatenate all the lines.
590 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200591 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200592
593 // free the lines only when using getsourceline()
594 if (evalarg->eval_cookie != NULL)
595 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200596 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200597 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200598 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100599 // later. Also free "eval_tofree" later if needed.
600 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200601 evalarg->eval_tofree =
602 ((char_u **)gap->ga_data)[gap->ga_len - 1];
603 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 ga_clear_strings(gap);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +0200605 ga_clear(freegap);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200606 }
607 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200608 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200609 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200610
611 // free lines that were explicitly marked for freeing
612 ga_clear_strings(freegap);
613 }
614
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200615 gap->ga_itemsize = 0;
616 if (p == NULL)
617 return FAIL;
618 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200619 vim_free(evalarg->eval_tofree_lambda);
620 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200621 // Compute "end" relative to the end.
622 *end = *start + STRLEN(*start) - endoff;
623 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200624 }
625
626 return res;
627}
628
629/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 * Convert "tv" to a string.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200631 * When "join_list" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100632 * Returns an allocated string (NULL when out of memory).
633 */
634 char_u *
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200635typval2string(typval_T *tv, int join_list)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100636{
637 garray_T ga;
638 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100639
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200640 if (join_list && tv->v_type == VAR_LIST)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100641 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000642 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100643 if (tv->vval.v_list != NULL)
644 {
645 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
646 if (tv->vval.v_list->lv_len > 0)
647 ga_append(&ga, NL);
648 }
649 ga_append(&ga, NUL);
650 retval = (char_u *)ga.ga_data;
651 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200652 else if (tv->v_type == VAR_LIST || tv->v_type == VAR_DICT)
653 {
654 char_u *tofree;
655 char_u numbuf[NUMBUFLEN];
656
657 retval = tv2string(tv, &tofree, numbuf, 0);
658 // Make a copy if we have a value but it's not in allocated memory.
659 if (retval != NULL && tofree == NULL)
660 retval = vim_strsave(retval);
661 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100662 else
663 retval = vim_strsave(tv_get_string(tv));
664 return retval;
665}
666
667/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200668 * Top level evaluation function, returning a string. Does not handle line
669 * breaks.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200670 * When "join_list" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 * Return pointer to allocated memory, or NULL for failure.
672 */
673 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100674eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100675 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200676 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100677 exarg_T *eap,
678 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
Bram Moolenaar33570922005-01-25 22:26:29 +0000680 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100682 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100683 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100685 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100686 if (use_simple_function)
687 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
688 else
689 r = eval0(arg, &tv, NULL, &evalarg);
690 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 retval = NULL;
692 else
693 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200694 retval = typval2string(&tv, join_list);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000695 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100697 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
699 return retval;
700}
701
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100702 char_u *
703eval_to_string(
704 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200705 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100706 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100707{
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200708 return eval_to_string_eap(arg, join_list, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100709}
710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000712 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100713 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200714 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 */
716 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100717eval_to_string_safe(
718 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000719 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 int keep_script_version,
721 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200724 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200725 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200726 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaar9530b582022-01-22 13:39:08 +0000728 if (!keep_script_version)
729 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200730 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000731 if (use_sandbox)
732 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100733 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200734 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100735 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000736 if (use_sandbox)
737 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100738 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200739 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200740 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200741 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 return retval;
743}
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745/*
746 * Top level evaluation function, returning a number.
747 * Evaluates "expr" silently.
748 * Returns -1 for an error.
749 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200750 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100751eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
Bram Moolenaar33570922005-01-25 22:26:29 +0000753 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200754 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000755 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100756 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 ++emsg_off;
759
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100760 if (use_simple_function)
761 r = may_call_simple_func(expr, &rettv);
762 if (r == NOTDONE)
763 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
764 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 retval = -1;
766 else
767 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100768 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000769 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
771 --emsg_off;
772
773 return retval;
774}
775
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000776/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000777 * Top level evaluation function.
778 * Returns an allocated typval_T with the result.
779 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000780 */
781 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200782eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000783{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100784 return eval_expr_ext(arg, eap, FALSE);
785}
786
787 typval_T *
788eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
789{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000790 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200791 evalarg_T evalarg;
792
793 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000794
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200795 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100796 if (tv != NULL)
797 {
798 int r = NOTDONE;
799
800 if (use_simple_function)
801 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
802 if (r == NOTDONE)
803 r = eval0(arg, tv, eap, &evalarg);
804
805 if (r == FAIL)
806 VIM_CLEAR(tv);
807 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000808
Bram Moolenaar37c83712020-06-30 21:18:36 +0200809 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000810 return tv;
811}
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000814 * "*arg" points to what can be a function name in the form of "import.Name" or
815 * "Funcref". Return the name of the function. Set "tofree" to something that
816 * was allocated.
817 * If "verbose" is FALSE no errors are given.
818 * Return NULL for any failure.
819 */
820 static char_u *
821deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000822 char_u **arg,
823 char_u **tofree,
824 evalarg_T *evalarg,
825 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000826{
827 typval_T ref;
828 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100829 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000830
831 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100832 if (evalarg != NULL)
833 {
834 // need to evaluate this to get an import, like in "a.Func"
835 save_flags = evalarg->eval_flags;
836 evalarg->eval_flags |= EVAL_EVALUATE;
837 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100838 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100839 {
840 dictitem_T *v;
841
842 // If <SID>VarName was used it would not be found, try another way.
843 v = find_var_also_in_script(name, NULL, FALSE);
844 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100845 {
846 name = NULL;
847 goto theend;
848 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100849 copy_tv(&v->di_tv, &ref);
850 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000851 if (*skipwhite(*arg) != NUL)
852 {
853 if (verbose)
854 semsg(_(e_trailing_characters_str), *arg);
855 name = NULL;
856 }
857 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
858 {
859 name = ref.vval.v_string;
860 ref.vval.v_string = NULL;
861 *tofree = name;
862 }
863 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
864 {
865 if (ref.vval.v_partial->pt_argc > 0
866 || ref.vval.v_partial->pt_dict != NULL)
867 {
868 if (verbose)
869 emsg(_(e_cannot_use_partial_here));
870 name = NULL;
871 }
872 else
873 {
874 name = vim_strsave(partial_name(ref.vval.v_partial));
875 *tofree = name;
876 }
877 }
878 else
879 {
880 if (verbose)
881 semsg(_(e_not_callable_type_str), name);
882 name = NULL;
883 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100884
885theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000886 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100887 if (evalarg != NULL)
888 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000889 return name;
890}
891
892/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100893 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200894 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
895 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200898 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100899call_vim_function(
900 char_u *func,
901 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200902 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200903 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200906 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000907 char_u *arg;
908 char_u *name;
909 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000910 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100912 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200913 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000914 funcexe.fe_firstline = curwin->w_cursor.lnum;
915 funcexe.fe_lastline = curwin->w_cursor.lnum;
916 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000917
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000918 // The name might be "import.Func" or "Funcref". We don't know, we need to
919 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000920 // autoload script has errors. Guess that when there is a dot in the name
921 // showing errors is the right choice.
922 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000923 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000924 if (ignore_errors)
925 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000926 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000927 if (ignore_errors)
928 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000929 if (name == NULL)
930 name = func;
931
932 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
933
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000934 if (ret == FAIL)
935 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000936 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000937
938 return ret;
939}
940
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100941/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100942 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000943 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
944 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000945 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000946 */
947 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100948call_func_retstr(
949 char_u *func,
950 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200951 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000952{
953 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000954 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000955
Bram Moolenaarded27a12018-08-01 19:06:03 +0200956 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000957 return NULL;
958
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100959 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000960 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 return retval;
962}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000963
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000964/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100965 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000966 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000967 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100968 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000969 */
970 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100971call_func_retlist(
972 char_u *func,
973 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200974 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000975{
976 typval_T rettv;
977
Bram Moolenaarded27a12018-08-01 19:06:03 +0200978 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000979 return NULL;
980
981 if (rettv.v_type != VAR_LIST)
982 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100983 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
984 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000985 clear_tv(&rettv);
986 return NULL;
987 }
988
989 return rettv.vval.v_list;
990}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
Bram Moolenaare70dd112022-01-21 16:31:11 +0000992#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100994 * Evaluate "arg", which is 'foldexpr'.
995 * Note: caller must set "curwin" to match "arg".
996 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
997 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 */
999 int
Bram Moolenaare70dd112022-01-21 16:31:11 +00001000eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
Bram Moolenaare70dd112022-01-21 16:31:11 +00001002 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00001003 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001004 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +00001006 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001007 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001008 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001010 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001011 current_sctx = wp->w_p_script_ctx[WV_FDE];
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001014 if (use_sandbox)
1015 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001016 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001018
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001019 // Evaluate the expression. If the expression is "FuncName()" call the
1020 // function directly.
1021 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 retval = 0;
1023 else
1024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001025 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 if (tv.v_type == VAR_NUMBER)
1027 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001028 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 retval = 0;
1030 else
1031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001032 // If the result is a string, check if there is a non-digit before
1033 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001034 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +02001035 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 *cp = *s++;
1037 retval = atol((char *)s);
1038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001042 if (use_sandbox)
1043 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001044 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001045 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001046 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001048 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050#endif
1051
Ernie Rael64885642023-10-04 20:16:22 +02001052#ifdef LOG_LOCKVAR
1053typedef struct flag_string_S
1054{
1055 int flag;
1056 char *str;
1057} flag_string_T;
1058
1059 static char *
1060flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
1061{
1062 char *p = buf;
1063 *p = NUL;
1064 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1065 {
1066 if ((fstring->flag & flags) != 0)
1067 {
1068 size_t len = STRLEN(fstring->str);
1069 if (n > p - buf + len + 7)
1070 {
1071 STRCAT(p, fstring->str);
1072 p += len;
1073 STRCAT(p, " ");
1074 ++p;
1075 }
1076 else
1077 {
1078 STRCAT(buf, "...");
1079 break;
1080 }
1081 }
1082 }
1083 return buf;
1084}
1085
1086flag_string_T glv_flag_strings[] = {
1087 { GLV_QUIET, "QUIET" },
1088 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1089 { GLV_READ_ONLY, "READ_ONLY" },
1090 { GLV_NO_DECL, "NO_DECL" },
1091 { GLV_COMPILING, "COMPILING" },
1092 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1093 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1094 { 0, NULL }
1095};
1096#endif
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098/*
Ernie Raelee865f32023-09-29 19:53:55 +02001099 * Fill in "lp" using "root". This is used in a special case when
1100 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1101 *
1102 * This is typically called with "lval_root" as "root". For a class, find
1103 * the name from lp in the class from root, fill in lval_T if found. For a
1104 * complex type, list/dict use it as the result; just put the root into
1105 * ll_tv.
1106 *
1107 * "lval_root" is a hack used during run-time/instr-execution to provide the
1108 * starting point for "get_lval()" to traverse a chain of indexes. In some
1109 * cases get_lval sees a bare name and uses this function to populate the
1110 * lval_T.
1111 *
1112 * For setting up "lval_root" (currently only used with lockvar)
1113 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1114 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1115 */
1116 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001117fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001118{
1119#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001120 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1121 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001122#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001123 if (lr->lr_tv == NULL)
1124 return;
Ernie Rael64885642023-10-04 20:16:22 +02001125 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001126 {
Ernie Rael64885642023-10-04 20:16:22 +02001127 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001128 {
1129 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001130 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001131 int m_idx;
1132 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1133 lp->ll_name_end - lp->ll_name, &m_idx);
1134 if (m != NULL)
1135 {
1136 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001137 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001138 lp->ll_oi = m_idx;
1139 lp->ll_valtype = m->ocm_type;
1140 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1141#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001142 ch_log(NULL, "LKVAR: ... class member %s.%s",
1143 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001144#endif
1145 return;
1146 }
1147 }
1148 }
1149
1150#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001151 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001152#endif
Ernie Rael64885642023-10-04 20:16:22 +02001153 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001154 lp->ll_is_root = TRUE;
1155}
1156
1157/*
Ernie Rael64885642023-10-04 20:16:22 +02001158 * Check if the class has permission to access the member.
1159 * Returns OK or FAIL.
1160 */
1161 static int
1162get_lval_check_access(
1163 class_T *cl_exec, // executing class, NULL if :def or script level
1164 class_T *cl, // class which contains the member
1165 ocmember_T *om, // member being accessed
1166 char_u *p, // char after member name
1167 int flags) // GLV flags to check if writing to lval
1168{
1169#ifdef LOG_LOCKVAR
1170 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1171 (void*)cl_exec, (void*)cl, *p);
1172#endif
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001173 if (cl_exec != NULL && cl_exec == cl)
1174 return OK;
1175
1176 char *msg = NULL;
1177 switch (om->ocm_access)
Ernie Rael64885642023-10-04 20:16:22 +02001178 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001179 case VIM_ACCESS_PRIVATE:
1180 msg = e_cannot_access_protected_variable_str;
1181 break;
1182 case VIM_ACCESS_READ:
1183 // If [idx] or .key following, read only OK.
1184 if (*p == '[' || *p == '.')
Ernie Raele6c9aa52023-10-06 19:55:52 +02001185 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001186 if ((flags & GLV_READ_ONLY) == 0)
1187 {
1188 if (IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001189 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001190 if (om->ocm_type->tt_type == VAR_OBJECT)
1191 semsg(_(e_enumvalue_str_cannot_be_modified),
1192 cl->class_name, om->ocm_name);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001193 else
1194 msg = e_variable_is_not_writable_str;
1195 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001196 else
1197 msg = e_variable_is_not_writable_str;
1198 }
1199 break;
1200 case VIM_ACCESS_ALL:
1201 break;
1202 }
1203 if (msg != NULL)
1204 {
1205 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1206 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02001207 }
1208 return OK;
1209}
1210
1211/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001212 * Get lval information for a variable imported from script "imp_sid". On
1213 * success, updates "lp" with the variable name, type, script ID and typval.
1214 * The variable name starts at or after "p".
1215 * If "rettv" is not NULL it points to the value to be assigned. This used to
1216 * match the rhs and lhs types.
1217 * Returns a pointer to the character after the variable name if the imported
1218 * variable is valid and writable.
1219 * Returns NULL if the variable is not exported or typval is not found or the
1220 * rhs type doesn't match the lhs type or the variable is not writable.
1221 */
1222 static char_u *
1223get_lval_imported(
1224 lval_T *lp,
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001225 scid_T imp_sid,
1226 char_u *p,
1227 dictitem_T **dip,
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001228 int fne_flags)
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001229{
1230 ufunc_T *ufunc;
1231 type_T *type = NULL;
1232 int cc;
1233 int rc = FAIL;
1234
1235 p = skipwhite(p);
1236
1237 import_check_sourced_sid(&imp_sid);
1238 lp->ll_sid = imp_sid;
1239 lp->ll_name = p;
1240 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1241 lp->ll_name_end = p;
1242
1243 // check the item is exported
1244 cc = *p;
1245 *p = NUL;
1246 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1247 TRUE) == -1)
1248 goto failed;
1249
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001250 // Get the typval for the exported item
1251 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1252 if (ht == NULL)
1253 goto failed;
1254
1255 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1256 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001257 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001258 goto success;
1259
1260 *dip = di;
1261
1262 // Check whether the variable is writable.
1263 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1264 if (sv != NULL && sv->sv_const != 0)
1265 {
1266 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1267 goto failed;
1268 }
1269
1270 // check whether variable is locked
1271 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1272 goto failed;
1273
1274 lp->ll_tv = &di->di_tv;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001275 lp->ll_valtype = type;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001276
1277success:
1278 rc = OK;
1279
1280failed:
1281 *p = cc;
1282 return rc == OK ? p : NULL;
1283}
1284
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001285typedef enum {
1286 GLV_FAIL,
1287 GLV_OK,
1288 GLV_STOP
1289} glv_status_T;
1290
1291/*
1292 * Get an Dict lval variable that can be assigned a value to: "name",
1293 * "name[expr]", "name[expr][expr]", "name.key", "name.key[expr]" etc.
1294 * "name" points to the start of the name.
1295 * If "rettv" is not NULL it points to the value to be assigned.
1296 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1297 * wrong; must end in space or cmd separator.
1298 *
1299 * flags:
1300 * GLV_QUIET: do not give error messages
1301 * GLV_READ_ONLY: will not change the variable
1302 * GLV_NO_AUTOLOAD: do not use script autoloading
1303 *
1304 * The Dict is returned in 'lp'. Returns GLV_OK on success and GLV_FAIL on
1305 * failure. Returns GLV_STOP to stop processing the characters following
1306 * 'key_end'.
1307 */
1308 static int
1309get_lval_dict_item(
1310 char_u *name,
1311 lval_T *lp,
1312 char_u *key,
1313 int len,
1314 char_u **key_end,
1315 typval_T *var1,
1316 int flags,
1317 int unlet,
1318 typval_T *rettv)
1319{
1320 int quiet = flags & GLV_QUIET;
1321 char_u *p = *key_end;
1322
1323 if (len == -1)
1324 {
1325 // "[key]": get key from "var1"
1326 key = tv_get_string_chk(var1); // is number or string
1327 if (key == NULL)
1328 {
1329 clear_tv(var1);
1330 return GLV_FAIL;
1331 }
1332 }
1333 lp->ll_list = NULL;
1334 lp->ll_object = NULL;
1335 lp->ll_class = NULL;
1336
1337 // a NULL dict is equivalent with an empty dict
1338 if (lp->ll_tv->vval.v_dict == NULL)
1339 {
1340 lp->ll_tv->vval.v_dict = dict_alloc();
1341 if (lp->ll_tv->vval.v_dict == NULL)
1342 {
1343 clear_tv(var1);
1344 return GLV_FAIL;
1345 }
1346 ++lp->ll_tv->vval.v_dict->dv_refcount;
1347 }
1348 lp->ll_dict = lp->ll_tv->vval.v_dict;
1349
1350 lp->ll_di = dict_find(lp->ll_dict, key, len);
1351
1352 // When assigning to a scope dictionary check that a function and
1353 // variable name is valid (only variable name unless it is l: or
1354 // g: dictionary). Disallow overwriting a builtin function.
1355 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1356 {
1357 int prevval;
1358
1359 if (len != -1)
1360 {
1361 prevval = key[len];
1362 key[len] = NUL;
1363 }
1364 else
1365 prevval = 0; // avoid compiler warning
1366 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1367 && (rettv->v_type == VAR_FUNC
1368 || rettv->v_type == VAR_PARTIAL)
1369 && var_wrong_func_name(key, lp->ll_di == NULL))
1370 || !valid_varname(key, -1, TRUE);
1371 if (len != -1)
1372 key[len] = prevval;
1373 if (wrong)
1374 {
1375 clear_tv(var1);
1376 return GLV_FAIL;
1377 }
1378 }
1379
1380 if (lp->ll_valtype != NULL)
1381 // use the type of the member
1382 lp->ll_valtype = lp->ll_valtype->tt_member;
1383
1384 if (lp->ll_di == NULL)
1385 {
1386 // Can't add "v:" or "a:" variable.
1387 if (lp->ll_dict == get_vimvar_dict()
1388 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1389 {
1390 semsg(_(e_illegal_variable_name_str), name);
1391 clear_tv(var1);
1392 return GLV_FAIL;
1393 }
1394
1395 // Key does not exist in dict: may need to add it.
1396 if (*p == '[' || *p == '.' || unlet)
1397 {
1398 if (!quiet)
1399 semsg(_(e_key_not_present_in_dictionary_str), key);
1400 clear_tv(var1);
1401 return GLV_FAIL;
1402 }
1403 if (len == -1)
1404 lp->ll_newkey = vim_strsave(key);
1405 else
1406 lp->ll_newkey = vim_strnsave(key, len);
1407 clear_tv(var1);
1408 if (lp->ll_newkey == NULL)
1409 p = NULL;
1410
1411 *key_end = p;
1412 return GLV_STOP;
1413 }
1414 // existing variable, need to check if it can be changed
1415 else if ((flags & GLV_READ_ONLY) == 0
1416 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1417 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
1418 {
1419 clear_tv(var1);
1420 return GLV_FAIL;
1421 }
1422
1423 clear_tv(var1);
1424 lp->ll_tv = &lp->ll_di->di_tv;
1425
1426 return GLV_OK;
1427}
1428
1429/*
1430 * Get an blob lval variable that can be assigned a value to: "name",
1431 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1432 *
1433 * 'var1' specifies the starting blob index and 'var2' specifies the ending
1434 * blob index. If the first index is not specified in a range, then 'empty1'
1435 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1436 * invalid indexes.
1437 *
1438 * The blob is returned in 'lp'. Returns OK on success and FAIL on failure.
1439 */
1440 static int
1441get_lval_blob(
1442 lval_T *lp,
1443 typval_T *var1,
1444 typval_T *var2,
1445 int empty1,
1446 int quiet)
1447{
1448 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1449
1450 // Get the number and item for the only or first index of the List.
1451 if (empty1)
1452 lp->ll_n1 = 0;
1453 else
1454 // is number or string
1455 lp->ll_n1 = (long)tv_get_number(var1);
1456 clear_tv(var1);
1457
1458 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
1459 {
1460 clear_tv(var2);
1461 return FAIL;
1462 }
1463 if (lp->ll_range && !lp->ll_empty2)
1464 {
1465 lp->ll_n2 = (long)tv_get_number(var2);
1466 clear_tv(var2);
1467 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL)
1468 return FAIL;
1469 }
1470
1471 if (!lp->ll_range)
1472 // Indexing a single byte in a blob. So the rhs type is a
1473 // number.
1474 lp->ll_valtype = &t_number;
1475
1476 lp->ll_blob = lp->ll_tv->vval.v_blob;
1477 lp->ll_tv = NULL;
1478
1479 return OK;
1480}
1481
1482/*
1483 * Get a List lval variable that can be assigned a value to: "name",
1484 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1485 *
1486 * 'var1' specifies the starting List index and 'var2' specifies the ending
1487 * List index. If the first index is not specified in a range, then 'empty1'
1488 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1489 * invalid indexes.
1490 *
1491 * The List is returned in 'lp'. Returns OK on success and FAIL on failure.
1492 */
1493 static int
1494get_lval_list(
1495 lval_T *lp,
1496 typval_T *var1,
1497 typval_T *var2,
1498 int empty1,
1499 int flags,
1500 int quiet)
1501{
1502 /*
1503 * Get the number and item for the only or first index of the List.
1504 */
1505 if (empty1)
1506 lp->ll_n1 = 0;
1507 else
1508 // is number or string
1509 lp->ll_n1 = (long)tv_get_number(var1);
1510 clear_tv(var1);
1511
1512 lp->ll_dict = NULL;
1513 lp->ll_object = NULL;
1514 lp->ll_class = NULL;
1515 lp->ll_list = lp->ll_tv->vval.v_list;
1516 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1517 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
1518 if (lp->ll_li == NULL)
1519 {
1520 clear_tv(var2);
1521 return FAIL;
1522 }
1523
1524 if (lp->ll_valtype != NULL && !lp->ll_range)
1525 // use the type of the member
1526 lp->ll_valtype = lp->ll_valtype->tt_member;
1527
1528 /*
1529 * May need to find the item or absolute index for the second
1530 * index of a range.
1531 * When no index given: "lp->ll_empty2" is TRUE.
1532 * Otherwise "lp->ll_n2" is set to the second index.
1533 */
1534 if (lp->ll_range && !lp->ll_empty2)
1535 {
1536 lp->ll_n2 = (long)tv_get_number(var2);
1537 // is number or string
1538 clear_tv(var2);
1539 if (check_range_index_two(lp->ll_list,
1540 &lp->ll_n1, lp->ll_li, &lp->ll_n2, quiet) == FAIL)
1541 return FAIL;
1542 }
1543
1544 lp->ll_tv = &lp->ll_li->li_tv;
1545
1546 return OK;
1547}
1548
1549/*
1550 * Get a Class or Object lval variable that can be assigned a value to:
1551 * "name", "name.key", "name.key[expr]" etc.
1552 *
1553 * 'cl_exec' is the class that is executing, or NULL. 'v_type' is VAR_CLASS or
1554 * VAR_OBJECT. 'key' points to the member variable name and 'key_end' points
1555 * to the character after 'key'. If 'quiet' is TRUE, then error messages
1556 * are not displayed for invalid indexes.
1557 *
1558 * The Class or Object is returned in 'lp'. Returns OK on success and FAIL on
1559 * failure.
1560 */
1561 static int
1562get_lval_class_or_obj(
1563 class_T *cl_exec,
1564 vartype_T v_type,
1565 lval_T *lp,
1566 char_u *key,
1567 char_u *key_end,
1568 int flags,
1569 int quiet)
1570{
1571 lp->ll_dict = NULL;
1572 lp->ll_list = NULL;
1573
1574 class_T *cl;
1575 if (v_type == VAR_OBJECT)
1576 {
1577 if (lp->ll_tv->vval.v_object == NULL)
1578 {
1579 if (!quiet)
1580 emsg(_(e_using_null_object));
1581 return FAIL;
1582 }
1583 cl = lp->ll_tv->vval.v_object->obj_class;
1584 lp->ll_object = lp->ll_tv->vval.v_object;
1585 }
1586 else
1587 {
1588 cl = lp->ll_tv->vval.v_class;
1589 lp->ll_object = NULL;
1590 }
1591 lp->ll_class = cl;
1592
1593 // TODO: what if class is NULL?
1594 if (cl != NULL)
1595 {
1596 lp->ll_valtype = NULL;
1597
1598 if (flags & GLV_PREFER_FUNC)
1599 {
1600 // First look for a function with this name.
1601 // round 1: class functions (skipped for an object)
1602 // round 2: object methods
1603 for (int round = v_type == VAR_OBJECT ? 2 : 1;
1604 round <= 2; ++round)
1605 {
1606 int m_idx;
1607 ufunc_T *fp;
1608
1609 fp = method_lookup(cl,
1610 round == 1 ? VAR_CLASS : VAR_OBJECT,
1611 key, key_end - key, &m_idx);
1612 lp->ll_oi = m_idx;
1613 if (fp != NULL)
1614 {
1615 lp->ll_ufunc = fp;
1616 lp->ll_valtype = fp->uf_func_type;
1617 break;
1618 }
1619 }
1620 }
1621
1622 if (lp->ll_valtype == NULL)
1623 {
1624 int m_idx;
1625 ocmember_T *om
1626 = member_lookup(cl, v_type, key, key_end - key, &m_idx);
1627 lp->ll_oi = m_idx;
1628 if (om != NULL)
1629 {
1630 if (get_lval_check_access(cl_exec, cl, om,
1631 key_end, flags) == FAIL)
1632 return FAIL;
1633
1634 // When lhs is used to modify the variable, check it is
1635 // not a read-only variable.
1636 if ((flags & GLV_READ_ONLY) == 0
1637 && (*key_end != '.' && *key_end != '[')
1638 && oc_var_check_ro(cl, om))
1639 return FAIL;
1640
1641 lp->ll_valtype = om->ocm_type;
1642
1643 if (v_type == VAR_OBJECT)
1644 lp->ll_tv = ((typval_T *)(
1645 lp->ll_tv->vval.v_object + 1)) + m_idx;
1646 else
1647 lp->ll_tv = &cl->class_members_tv[m_idx];
1648 }
1649 }
1650
1651 if (lp->ll_valtype == NULL)
1652 {
1653 member_not_found_msg(cl, v_type, key, key_end - key);
1654 return FAIL;
1655 }
1656 }
1657
1658 return OK;
1659}
1660
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001661/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001662 * Get an lval: variable, Dict item or List item that can be assigned a value
1663 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1664 * "name.key", "name.key[expr]" etc.
1665 * Indexing only works if "name" is an existing List or Dictionary.
1666 * "name" points to the start of the name.
1667 * If "rettv" is not NULL it points to the value to be assigned.
1668 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1669 * wrong; must end in space or cmd separator.
1670 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001671 * flags:
1672 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001673 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001674 * GLV_NO_AUTOLOAD: do not use script autoloading
1675 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001676 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001677 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001678 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001681get_lval(
1682 char_u *name,
1683 typval_T *rettv,
1684 lval_T *lp,
1685 int unlet,
1686 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001687 int flags, // GLV_ values
1688 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001690 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001691 char_u *expr_start, *expr_end;
1692 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001693 dictitem_T *v = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 typval_T var1;
1695 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001696 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001697 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001698 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001699 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001700 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001701 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001702 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001703 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704
Ernie Raelee865f32023-09-29 19:53:55 +02001705#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001706 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001707 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001708 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001709 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1710 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001711 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001712 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001713 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001714#endif
1715
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001716 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001717 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001718
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001719 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001720 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001721 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001722 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001723 lp->ll_name_end = find_name_end(name, NULL, NULL,
1724 FNE_INCL_BR | fne_flags);
1725 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001726 }
1727
Bram Moolenaara749a422022-02-12 19:52:25 +00001728 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001729 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001730 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1731 {
1732 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1733 return NULL;
1734 }
1735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001736 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001737 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001739 if (expr_start != NULL)
1740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001741 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001742 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001743 && *p != '[' && *p != '.')
1744 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001745 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001746 return NULL;
1747 }
1748
1749 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1750 if (lp->ll_exp_name == NULL)
1751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001752 // Report an invalid expression in braces, unless the
1753 // expression evaluation has been cancelled due to an
1754 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001755 if (!aborting() && !quiet)
1756 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001757 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001758 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001759 return NULL;
1760 }
1761 }
1762 lp->ll_name = lp->ll_exp_name;
1763 }
1764 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001766 lp->ll_name = name;
1767
Bram Moolenaar4525a572022-02-13 11:57:33 +00001768 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001770 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001771 // However, "g:[key]" is indexing a dictionary.
1772 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001773 {
1774 --p;
1775 lp->ll_name_end = p;
1776 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001777 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001778 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001779 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001780
Bram Moolenaar9510d222022-09-11 15:14:05 +01001781 if (is_scoped_variable(name))
1782 {
1783 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1784 return NULL;
1785 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001786 if (VIM_ISWHITE(*p))
1787 {
1788 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1789 return NULL;
1790 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001791 if (tp == p + 1 && !quiet)
1792 {
1793 semsg(_(e_white_space_required_after_str_str), ":", p);
1794 return NULL;
1795 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001796 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001797 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001798 semsg(_(e_using_type_not_in_script_context_str), p);
1799 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001800 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001801 if (vim9script && (flags & GLV_NO_DECL) &&
1802 !(flags & GLV_FOR_LOOP))
1803 {
1804 // Using a type and not in a "var" declaration.
1805 semsg(_(e_trailing_characters_str), p);
1806 return NULL;
1807 }
1808
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001809
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001810 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001811 lp->ll_type = parse_type(&tp,
1812 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1813 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001814 if (lp->ll_type == NULL && !quiet)
1815 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001816 lp->ll_name_end = tp;
1817 }
Ernie Raelee865f32023-09-29 19:53:55 +02001818 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 }
1820 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001821 if (lp->ll_name == NULL)
1822 return p;
1823
Bram Moolenaar62aec932022-01-29 21:45:34 +00001824 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001825 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001826 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001827 if (import != NULL)
1828 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001829 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001830 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001831 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00001832 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001833 }
1834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835
Ernie Rael0f058d12023-10-14 11:25:04 +02001836 // Without [idx] or .key we are done.
1837 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001838 {
1839 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001840 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001841 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001842 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843
Ernie Rael0f058d12023-10-14 11:25:04 +02001844 if (vim9script && lval_root != NULL)
1845 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001846 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001847 {
1848 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001849 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001850 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001851 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001852 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001853 {
1854 cc = *p;
1855 *p = NUL;
1856 // When we would write to the variable pass &ht and prevent autoload.
1857 writing = !(flags & GLV_READ_ONLY);
1858 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001859 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001860 if (v == NULL && !quiet)
1861 semsg(_(e_undefined_variable_str), lp->ll_name);
1862 *p = cc;
1863 if (v == NULL)
1864 return NULL;
1865 lp->ll_tv = &v->di_tv;
1866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867
Bram Moolenaar4525a572022-02-13 11:57:33 +00001868 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001869 {
1870 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001871 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001872 return NULL;
1873 }
1874
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001875 /*
1876 * Loop until no more [idx] or .key is following.
1877 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001878 var1.v_type = VAR_UNKNOWN;
1879 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001880 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001882 vartype_T v_type = lp->ll_tv->v_type;
1883
1884 if (*p == '.' && v_type != VAR_DICT
1885 && v_type != VAR_OBJECT
1886 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001887 {
1888 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001889 semsg(_(e_dot_not_allowed_after_str_str),
1890 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001891 return NULL;
1892 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001893 if (v_type != VAR_LIST
1894 && v_type != VAR_DICT
1895 && v_type != VAR_BLOB
1896 && v_type != VAR_OBJECT
1897 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001900 semsg(_(e_index_not_allowed_after_str_str),
1901 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001904
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001905 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001906 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001907 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001908 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001909 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001910 r = rettv_blob_alloc(lp->ll_tv);
1911 if (r == FAIL)
1912 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001917 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001918 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001920#ifdef LOG_LOCKVAR
1921 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1922 vartype_name(v_type));
1923#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001924
Bram Moolenaar4525a572022-02-13 11:57:33 +00001925 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001926 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001927 && lp->ll_tv == &v->di_tv
1928 && ht != NULL && ht == get_script_local_ht())
1929 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001930 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001931
1932 // Vim9 script local variable: get the type
1933 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001934 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001935 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001936#ifdef LOG_LOCKVAR
1937 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1938 vartype_name(lp->ll_valtype->tt_type));
1939#endif
1940 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001941 }
1942
Bram Moolenaar8c711452005-01-14 21:53:12 +00001943 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001944 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001945 {
1946 key = p + 1;
1947 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1948 ;
1949 if (len == 0)
1950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001952 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001954 }
1955 p = key + len;
1956 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001957 else
1958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001960 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 if (*p == ':')
1962 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001963 else
1964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001966 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001967 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001968 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001970 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001971 clear_tv(&var1);
1972 return NULL;
1973 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001974 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001975 }
1976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 if (*p == ':')
1979 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001980 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001983 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001984 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001986 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001987 if (rettv != NULL
1988 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001989 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001990 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001991 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001994 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001995 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001997 }
1998 p = skipwhite(p + 1);
1999 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 else
2002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002004 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002005 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002006 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002007 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002010 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002012 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002013 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002014 clear_tv(&var2);
2015 return NULL;
2016 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002017 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002019 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002020 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002022
Bram Moolenaar8c711452005-01-14 21:53:12 +00002023 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00002026 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002027 clear_tv(&var1);
2028 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002029 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 }
2031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002032 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 ++p;
2034 }
Ernie Rael4c8da022023-10-11 21:35:11 +02002035#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02002036 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02002037 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
2038 empty1 ? ":" : (char*)tv_get_string(&var1));
2039 else
2040 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02002041#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042
Bram Moolenaard505d172022-12-18 21:42:55 +00002043 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002045 glv_status_T glv_status;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002046
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002047 glv_status = get_lval_dict_item(name, lp, key, len, &p, &var1,
2048 flags, unlet, rettv);
2049 if (glv_status == GLV_FAIL)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002050 return NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002051 if (glv_status == GLV_STOP)
2052 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002053 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002054 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002055 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002056 if (get_lval_blob(lp, &var1, &var2, empty1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002057 return NULL;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002058
Bram Moolenaar61be3762019-03-19 23:04:17 +01002059 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002060 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002061 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002062 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002063 if (get_lval_list(lp, &var1, &var2, empty1, flags, quiet) == FAIL)
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002064 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002065 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002066 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002067 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002068 if (get_lval_class_or_obj(cl_exec, v_type, lp, key, p, flags,
2069 quiet) == FAIL)
2070 return FAIL;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
2073
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002074 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
2075 {
2076 where_T where = WHERE_INIT;
2077
2078 // In a vim9 script, do type check and make sure the variable is
2079 // writable.
2080 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
2081 return NULL;
2082 }
2083
2084
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002085 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087 return p;
2088}
2089
2090/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002094clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095{
2096 vim_free(lp->ll_exp_name);
2097 vim_free(lp->ll_newkey);
2098}
2099
2100/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002101 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002103 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2104 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002107set_var_lval(
2108 lval_T *lp,
2109 char_u *endp,
2110 typval_T *rettv,
2111 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002112 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
2113 char_u *op,
2114 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115{
2116 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118
2119 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002120 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002121 cc = *endp;
2122 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01002123 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02002124 return;
2125
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002126 if (lp->ll_blob != NULL)
2127 {
2128 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002129
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002130 if (op != NULL && *op != '=')
2131 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002132 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002133 return;
2134 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002135 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02002136 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002137
2138 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2139 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002140 if (lp->ll_empty2)
2141 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2142
Bram Moolenaar68452172021-04-12 21:21:02 +02002143 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
2144 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002145 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002146 }
2147 else
2148 {
2149 val = (int)tv_get_number_chk(rettv, &error);
2150 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02002151 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002152 }
2153 }
2154 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002156 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002157
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002158 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2159 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002160 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002161 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002162 *endp = cc;
2163 return;
2164 }
2165
Bram Moolenaarff697e62019-02-12 22:28:33 +01002166 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002167 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002168 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002169 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01002170 {
Ernie Raele75fde62023-12-21 17:18:54 +01002171 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002172 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002173 clear_tv(&tv);
2174 return;
2175 }
2176
Bram Moolenaar79518e22017-02-17 16:31:35 +01002177 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002178 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2179 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002180 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002181 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002182 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002183 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002186 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002187 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002188 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2189 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002190 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002191 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002192 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002193 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002194 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002196 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002197 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002198 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002199 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 else if (lp->ll_range)
2201 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002202 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2203 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002204 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002205 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002206 return;
2207 }
2208
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002209 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2210 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 }
2212 else
2213 {
2214 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002215 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002217 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2218 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002219 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002220 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002221 return;
2222 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002223
2224 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002225 && check_typval_arg_type(lp->ll_valtype, rettv,
2226 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002227 return;
2228
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 if (lp->ll_newkey != NULL)
2230 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002231 if (op != NULL && *op != '=')
2232 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002233 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002234 return;
2235 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002236 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2237 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002238 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002239
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002240 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002241 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 if (di == NULL)
2243 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002244 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2245 {
2246 vim_free(di);
2247 return;
2248 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002250 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002251 else if (op != NULL && *op != '=')
2252 {
2253 tv_op(lp->ll_tv, rettv, op);
2254 return;
2255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002258
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 /*
2260 * Assign the value to the variable or list item.
2261 */
2262 if (copy)
2263 copy_tv(rettv, lp->ll_tv);
2264 else
2265 {
2266 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002267 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
2270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271}
2272
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002273/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002274 * Handle "blob1 += blob2".
2275 * Returns OK or FAIL.
2276 */
2277 static int
2278tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
2279{
2280 if (*op != '+' || tv2->v_type != VAR_BLOB)
2281 return FAIL;
2282
2283 // Blob += Blob
2284 if (tv1->vval.v_blob == NULL || tv2->vval.v_blob == NULL)
2285 return OK;
2286
2287 blob_T *b1 = tv1->vval.v_blob;
2288 blob_T *b2 = tv2->vval.v_blob;
2289 int len = blob_len(b2);
2290
2291 for (int i = 0; i < len; i++)
2292 ga_append(&b1->bv_ga, blob_get(b2, i));
2293
2294 return OK;
2295}
2296
2297/*
2298 * Handle "list1 += list2".
2299 * Returns OK or FAIL.
2300 */
2301 static int
2302tv_op_list(typval_T *tv1, typval_T *tv2, char_u *op)
2303{
2304 if (*op != '+' || tv2->v_type != VAR_LIST)
2305 return FAIL;
2306
2307 // List += List
2308 if (tv2->vval.v_list == NULL)
2309 return OK;
2310
2311 if (tv1->vval.v_list == NULL)
2312 {
2313 tv1->vval.v_list = tv2->vval.v_list;
2314 ++tv1->vval.v_list->lv_refcount;
2315 }
2316 else
2317 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2318
2319 return OK;
2320}
2321
2322/*
2323 * Handle number operations:
2324 * nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
2325 *
2326 * Returns OK or FAIL.
2327 */
2328 static int
2329tv_op_number(typval_T *tv1, typval_T *tv2, char_u *op)
2330{
2331 varnumber_T n;
2332 int failed = FALSE;
2333
2334 n = tv_get_number(tv1);
2335 if (tv2->v_type == VAR_FLOAT)
2336 {
2337 float_T f = n;
2338
2339 if (*op == '%')
2340 return FAIL;
2341 switch (*op)
2342 {
2343 case '+': f += tv2->vval.v_float; break;
2344 case '-': f -= tv2->vval.v_float; break;
2345 case '*': f *= tv2->vval.v_float; break;
2346 case '/': f /= tv2->vval.v_float; break;
2347 }
2348 clear_tv(tv1);
2349 tv1->v_type = VAR_FLOAT;
2350 tv1->vval.v_float = f;
2351 }
2352 else
2353 {
2354 switch (*op)
2355 {
2356 case '+': n += tv_get_number(tv2); break;
2357 case '-': n -= tv_get_number(tv2); break;
2358 case '*': n *= tv_get_number(tv2); break;
2359 case '/': n = num_divide(n, tv_get_number(tv2), &failed); break;
2360 case '%': n = num_modulus(n, tv_get_number(tv2), &failed); break;
2361 }
2362 clear_tv(tv1);
2363 tv1->v_type = VAR_NUMBER;
2364 tv1->vval.v_number = n;
2365 }
2366
2367 return failed ? FAIL : OK;
2368}
2369
2370/*
2371 * Handle "str1 .= str2"
2372 * Returns OK or FAIL.
2373 */
2374 static int
2375tv_op_string(typval_T *tv1, typval_T *tv2, char_u *op UNUSED)
2376{
2377 char_u numbuf[NUMBUFLEN];
2378 char_u *s;
2379
2380 if (tv2->v_type == VAR_FLOAT)
2381 return FAIL;
2382
2383 // str .= str
2384 s = tv_get_string(tv1);
2385 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
2386 clear_tv(tv1);
2387 tv1->v_type = VAR_STRING;
2388 tv1->vval.v_string = s;
2389
2390 return OK;
2391}
2392
2393/*
2394 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2395 * and "tv1 .= tv2"
2396 * Returns OK or FAIL.
2397 */
2398 static int
2399tv_op_nr_or_string(typval_T *tv1, typval_T *tv2, char_u *op)
2400{
2401 if (tv2->v_type == VAR_LIST)
2402 return FAIL;
2403
2404 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
2405 return tv_op_number(tv1, tv2, op);
2406
2407 return tv_op_string(tv1, tv2, op);
2408}
2409
2410/*
2411 * Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
2412 * Returns OK or FAIL.
2413 */
2414 static int
2415tv_op_float(typval_T *tv1, typval_T *tv2, char_u *op)
2416{
2417 float_T f;
2418
2419 if (*op == '%' || *op == '.'
2420 || (tv2->v_type != VAR_FLOAT
2421 && tv2->v_type != VAR_NUMBER
2422 && tv2->v_type != VAR_STRING))
2423 return FAIL;
2424
2425 if (tv2->v_type == VAR_FLOAT)
2426 f = tv2->vval.v_float;
2427 else
2428 f = tv_get_number(tv2);
2429 switch (*op)
2430 {
2431 case '+': tv1->vval.v_float += f; break;
2432 case '-': tv1->vval.v_float -= f; break;
2433 case '*': tv1->vval.v_float *= f; break;
2434 case '/': tv1->vval.v_float /= f; break;
2435 }
2436
2437 return OK;
2438}
2439
2440/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002441 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2442 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 * Returns OK or FAIL.
2444 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002446tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447{
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002448 // Can't do anything with a Funcref or Dict on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002449 // v:true and friends only work with "..=".
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002450 if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
2451 || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL)
2452 && *op != '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002454 semsg(_(e_wrong_variable_type_for_str_equal), op);
2455 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 }
2457
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002458 if (tv2->v_type == VAR_CLASS || tv2->v_type == VAR_TYPEALIAS)
2459 {
2460 check_typval_is_value(tv2);
2461 return FAIL;
2462 }
2463
2464 int retval = FAIL;
2465 switch (tv1->v_type)
2466 {
2467 case VAR_UNKNOWN:
2468 case VAR_ANY:
2469 case VAR_VOID:
2470 case VAR_DICT:
2471 case VAR_FUNC:
2472 case VAR_PARTIAL:
2473 case VAR_BOOL:
2474 case VAR_SPECIAL:
2475 case VAR_JOB:
2476 case VAR_CHANNEL:
2477 case VAR_INSTR:
2478 case VAR_OBJECT:
2479 break;
2480
2481 case VAR_CLASS:
2482 case VAR_TYPEALIAS:
2483 check_typval_is_value(tv1);
2484 return FAIL;
2485
2486 case VAR_BLOB:
2487 retval = tv_op_blob(tv1, tv2, op);
2488 break;
2489
2490 case VAR_LIST:
2491 retval = tv_op_list(tv1, tv2, op);
2492 break;
2493
2494 case VAR_NUMBER:
2495 case VAR_STRING:
2496 retval = tv_op_nr_or_string(tv1, tv2, op);
2497 break;
2498
2499 case VAR_FLOAT:
2500 retval = tv_op_float(tv1, tv2, op);
2501 break;
2502 }
2503
2504 if (retval != OK)
Ernie Raele75fde62023-12-21 17:18:54 +01002505 semsg(_(e_wrong_variable_type_for_str_equal), op);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002506
2507 return retval;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508}
2509
2510/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002511 * Evaluate the expression used in a ":for var in expr" command.
2512 * "arg" points to "var".
2513 * Set "*errp" to TRUE for an error, FALSE otherwise;
2514 * Return a pointer that holds the info. Null when there is an error.
2515 */
2516 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002517eval_for_line(
2518 char_u *arg,
2519 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002520 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002521 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002522{
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002524 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002525 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 typval_T tv;
2527 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002528 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002530 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002531
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002532 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002533 if (fi == NULL)
2534 return NULL;
2535
Bram Moolenaar404557e2021-07-05 21:41:48 +02002536 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2537 &fi->fi_semicolon, FALSE);
2538 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002539 return fi;
2540
Bram Moolenaar404557e2021-07-05 21:41:48 +02002541 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002542 if (expr[0] != 'i' || expr[1] != 'n'
2543 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002544 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002545 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2546 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2547 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002548 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 return fi;
2550 }
2551
2552 if (skip)
2553 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002554 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2555 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002556 {
2557 *errp = FALSE;
2558 if (!skip)
2559 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002560 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002561 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002562 l = tv.vval.v_list;
2563 if (l == NULL)
2564 {
2565 // a null list is like an empty list: do nothing
2566 clear_tv(&tv);
2567 }
2568 else
2569 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002571 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002573 // No need to increment the refcount, it's already set for
2574 // the list being used in "tv".
2575 fi->fi_list = l;
2576 list_add_watch(l, &fi->fi_lw);
2577 fi->fi_lw.lw_item = l->lv_first;
2578 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002579 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002580 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002581 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002582 fi->fi_bi = 0;
2583 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002584 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002585 typval_T btv;
2586
2587 // Make a copy, so that the iteration still works when the
2588 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002590 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002591 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002592 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002593 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002594 else if (tv.v_type == VAR_STRING)
2595 {
2596 fi->fi_byte_idx = 0;
2597 fi->fi_string = tv.vval.v_string;
2598 tv.vval.v_string = NULL;
2599 if (fi->fi_string == NULL)
2600 fi->fi_string = vim_strsave((char_u *)"");
2601 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002602 else
2603 {
Sean Dewar80d73952021-08-04 19:25:54 +02002604 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002605 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002606 }
2607 }
2608 }
2609 if (skip)
2610 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002611 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002612
2613 return fi;
2614}
2615
2616/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002617 * Used when looping over a :for line, skip the "in expr" part.
2618 */
2619 void
2620skip_for_lines(void *fi_void, evalarg_T *evalarg)
2621{
2622 forinfo_T *fi = (forinfo_T *)fi_void;
2623 int i;
2624
2625 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002626 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002627}
2628
2629/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002630 * Use the first item in a ":for" list. Advance to the next.
2631 * Assign the values to the variable (list). "arg" points to the first one.
2632 * Return TRUE when a valid item was found, FALSE when at end of list or
2633 * something wrong.
2634 */
2635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002636next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002637{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002638 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002639 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002640 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002641 ? (ASSIGN_FINAL
2642 // first round: error if variable exists
2643 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002644 | ASSIGN_NO_MEMBER_TYPE
2645 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002646 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002647 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002648 int skip_assign = in_vim9script() && arg[0] == '_'
2649 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002650
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002651 if (fi->fi_blob != NULL)
2652 {
2653 typval_T tv;
2654
2655 if (fi->fi_bi >= blob_len(fi->fi_blob))
2656 return FALSE;
2657 tv.v_type = VAR_NUMBER;
2658 tv.v_lock = VAR_FIXED;
2659 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2660 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002661 if (skip_assign)
2662 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002663 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002664 fi->fi_varcount, flag, NULL) == OK;
2665 }
2666
2667 if (fi->fi_string != NULL)
2668 {
2669 typval_T tv;
2670 int len;
2671
2672 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2673 if (len == 0)
2674 return FALSE;
2675 tv.v_type = VAR_STRING;
2676 tv.v_lock = VAR_FIXED;
2677 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2678 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002679 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002680 if (skip_assign)
2681 result = TRUE;
2682 else
2683 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002684 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002685 vim_free(tv.vval.v_string);
2686 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002687 }
2688
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002689 item = fi->fi_lw.lw_item;
2690 if (item == NULL)
2691 result = FALSE;
2692 else
2693 {
2694 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002695 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002696 if (skip_assign)
2697 result = TRUE;
2698 else
2699 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002700 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002701 }
2702 return result;
2703}
2704
2705/*
2706 * Free the structure used to store info used by ":for".
2707 */
2708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002709free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710{
Bram Moolenaar33570922005-01-25 22:26:29 +00002711 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002712
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002713 if (fi == NULL)
2714 return;
2715 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002716 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002717 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002718 list_unref(fi->fi_list);
2719 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002720 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002721 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002722 else
2723 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002724 vim_free(fi);
2725}
2726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002728set_context_for_expression(
2729 expand_T *xp,
2730 char_u *arg,
2731 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002733 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002735 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002737 if (cmdidx == CMD_let || cmdidx == CMD_var
2738 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002739 {
2740 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002741 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002743 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002744 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002745 {
2746 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002747 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002748 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002749 break;
2750 }
2751 return;
2752 }
2753 }
2754 else
2755 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2756 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 while ((xp->xp_pattern = vim_strpbrk(arg,
2758 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2759 {
2760 c = *xp->xp_pattern;
2761 if (c == '&')
2762 {
2763 c = xp->xp_pattern[1];
2764 if (c == '&')
2765 {
2766 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002767 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002772 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2773 xp->xp_pattern += 2;
2774
2775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777 else if (c == '$')
2778 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002779 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 xp->xp_context = EXPAND_ENV_VARS;
2781 }
2782 else if (c == '=')
2783 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002784 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 xp->xp_context = EXPAND_EXPRESSION;
2786 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002787 else if (c == '#'
2788 && xp->xp_context == EXPAND_EXPRESSION)
2789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002790 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002791 break;
2792 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002793 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 && xp->xp_context == EXPAND_FUNCTIONS
2795 && vim_strchr(xp->xp_pattern, '(') == NULL)
2796 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002797 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 break;
2799 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002800 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002802 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
2804 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2805 if (c == '\\' && xp->xp_pattern[1] != NUL)
2806 ++xp->xp_pattern;
2807 xp->xp_context = EXPAND_NOTHING;
2808 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002809 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002811 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2813 /* skip */ ;
2814 xp->xp_context = EXPAND_NOTHING;
2815 }
2816 else if (c == '|')
2817 {
2818 if (xp->xp_pattern[1] == '|')
2819 {
2820 ++xp->xp_pattern;
2821 xp->xp_context = EXPAND_EXPRESSION;
2822 }
2823 else
2824 xp->xp_context = EXPAND_COMMANDS;
2825 }
2826 else
2827 xp->xp_context = EXPAND_EXPRESSION;
2828 }
2829 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002830 // Doesn't look like something valid, expand as an expression
2831 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002832 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 arg = xp->xp_pattern;
2834 if (*arg != NUL)
2835 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2836 /* skip */ ;
2837 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002838
2839 // ":exe one two" completes "two"
2840 if ((cmdidx == CMD_execute
2841 || cmdidx == CMD_echo
2842 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002843 || cmdidx == CMD_echomsg
2844 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002845 && xp->xp_context == EXPAND_EXPRESSION)
2846 {
2847 for (;;)
2848 {
2849 char_u *n = skiptowhite(arg);
2850
2851 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2852 break;
2853 arg = skipwhite(n);
2854 }
2855 }
2856
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 xp->xp_pattern = arg;
2858}
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002861 * Return TRUE if "pat" matches "text".
2862 * Does not use 'cpo' and always uses 'magic'.
2863 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002864 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002865pattern_match(char_u *pat, char_u *text, int ic)
2866{
2867 int matches = FALSE;
2868 char_u *save_cpo;
2869 regmatch_T regmatch;
2870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002871 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002872 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002873 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002874 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2875 if (regmatch.regprog != NULL)
2876 {
2877 regmatch.rm_ic = ic;
2878 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2879 vim_regfree(regmatch.regprog);
2880 }
2881 p_cpo = save_cpo;
2882 return matches;
2883}
2884
2885/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002886 * Handle a name followed by "(". Both for just "name(arg)" and for
2887 * "expr->name(arg)".
2888 * Returns OK or FAIL.
2889 */
2890 static int
2891eval_func(
2892 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002893 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002894 char_u *name,
2895 int name_len,
2896 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002897 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002898 typval_T *basetv) // "expr" for "expr->name(arg)"
2899{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002900 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002901 char_u *s = name;
2902 int len = name_len;
2903 partial_T *partial;
2904 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002905 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002906 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002907
2908 if (!evaluate)
2909 check_vars(s, len);
2910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002911 // If "s" is the name of a variable of type VAR_FUNC
2912 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002913 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002914 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002916 // Need to make a copy, in case evaluating the arguments makes
2917 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002918 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002919 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002920 ret = FAIL;
2921 else
2922 {
2923 funcexe_T funcexe;
2924
2925 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002926 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002927 funcexe.fe_firstline = curwin->w_cursor.lnum;
2928 funcexe.fe_lastline = curwin->w_cursor.lnum;
2929 funcexe.fe_evaluate = evaluate;
2930 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002931 if (partial != NULL)
2932 {
2933 funcexe.fe_object = partial->pt_obj;
2934 if (funcexe.fe_object != NULL)
2935 ++funcexe.fe_object->obj_refcount;
2936 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002937 funcexe.fe_basetv = basetv;
2938 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002939 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002940 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002941 }
2942 vim_free(s);
2943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002944 // If evaluate is FALSE rettv->v_type was not set in
2945 // get_func_tv, but it's needed in handle_subscript() to parse
2946 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002947 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2948 {
2949 rettv->vval.v_string = NULL;
2950 rettv->v_type = VAR_FUNC;
2951 }
2952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002953 // Stop the expression evaluation when immediately
2954 // aborting on error, or when an interrupt occurred or
2955 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002956 if (evaluate && aborting())
2957 {
2958 if (ret == OK)
2959 clear_tv(rettv);
2960 ret = FAIL;
2961 }
2962 return ret;
2963}
2964
2965/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002966 * After a NL, skip over empty lines and comment-only lines.
2967 */
2968 static char_u *
2969newline_skip_comments(char_u *arg)
2970{
2971 char_u *p = arg + 1;
2972
2973 for (;;)
2974 {
2975 p = skipwhite(p);
2976
2977 if (*p == NUL)
2978 break;
2979 if (vim9_comment_start(p))
2980 {
2981 char_u *nl = vim_strchr(p, NL);
2982
2983 if (nl == NULL)
2984 break;
2985 p = nl;
2986 }
2987 if (*p != NL)
2988 break;
2989 ++p; // skip another NL
2990 }
2991 return p;
2992}
2993
2994/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002995 * Get the next line source line without advancing. But do skip over comment
2996 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002997 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002998 */
2999 static char_u *
3000getline_peek_skip_comments(evalarg_T *evalarg)
3001{
3002 for (;;)
3003 {
3004 char_u *next = getline_peek(evalarg->eval_getline,
3005 evalarg->eval_cookie);
3006 char_u *p;
3007
3008 if (next == NULL)
3009 break;
3010 p = skipwhite(next);
3011 if (*p != NUL && !vim9_comment_start(p))
3012 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01003013 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00003014 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02003015 }
3016 return NULL;
3017}
3018
3019/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02003020 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
3021 * comment) and there is a next line, return the next line (skipping blanks)
3022 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003023 * Otherwise return the next non-white at or after "arg" and set "getnext" to
3024 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003025 * "arg" must point somewhere inside a line, not at the start.
3026 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003027 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003028eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
3029{
Bram Moolenaar9d489562020-07-30 20:08:50 +02003030 char_u *p = skipwhite(arg);
3031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003032 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003033 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003034 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01003035 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
3036 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01003037 && (*p == NUL || *p == NL
3038 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003039 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02003040 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003041
Bram Moolenaare442d592022-05-05 12:20:28 +01003042 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003043 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01003044 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02003045 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003046 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02003047 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003048
Bram Moolenaar9d489562020-07-30 20:08:50 +02003049 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003050 {
Bram Moolenaar69082912022-09-22 21:35:19 +01003051 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003052 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003053 }
3054 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003055 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003056}
3057
3058/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003059 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003060 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003061 *
3062 * If "arg" is not NULL, then the caller should assign the return value to
3063 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003064 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003065 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01003066eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003067{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003068 garray_T *gap = &evalarg->eval_ga;
3069 char_u *line;
3070
Bram Moolenaar39be4982022-05-06 21:51:50 +01003071 if (arg != NULL)
3072 {
3073 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003074 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01003075 // Truncate before a trailing comment, so that concatenating the lines
3076 // won't turn the rest into a comment.
3077 if (*skipwhite(arg) == '#')
3078 *arg = NUL;
3079 }
Bram Moolenaare442d592022-05-05 12:20:28 +01003080
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003081 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02003082 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
3083 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003084 else
3085 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00003086 if (line == NULL)
3087 return NULL;
3088
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003089 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003090 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
3091 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003092 char_u *p = skipwhite(line);
3093
3094 // Going to concatenate the lines after parsing. For an empty or
3095 // comment line use an empty string.
3096 if (*p == NUL || vim9_comment_start(p))
3097 {
3098 vim_free(line);
3099 line = vim_strsave((char_u *)"");
3100 }
3101
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003102 ((char_u **)gap->ga_data)[gap->ga_len] = line;
3103 ++gap->ga_len;
3104 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003105 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003106 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01003107 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003108 evalarg->eval_tofree = line;
3109 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02003110
3111 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003112 // line. The caller assigns the return value to "arg".
3113 // If "arg" is NULL, then the return value is discarded. In that case,
3114 // "arg" still points to the previous line. So don't reset
3115 // "eval_using_cmdline".
3116 if (arg != NULL)
3117 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003118 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003119}
3120
Bram Moolenaare6b53242020-07-01 17:28:33 +02003121/*
3122 * Call eval_next_non_blank() and get the next line if needed.
3123 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02003124 char_u *
3125skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
3126{
3127 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00003128 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003129
Bram Moolenaar962d7212020-07-04 14:15:00 +02003130 if (evalarg == NULL)
3131 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003132 eval_next_non_blank(p, evalarg, &getnext);
3133 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003134 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003135 return p;
3136}
3137
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003138/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003139 * The "eval" functions have an "evalarg" argument: When NULL or
3140 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
3141 * parsed but not executed. The functions may return OK, but the rettv will be
3142 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 */
3144
3145/*
3146 * Handle zero level expression.
3147 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003148 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003149 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003150 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 * Return OK or FAIL.
3152 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003154eval0(
3155 char_u *arg,
3156 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003157 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003158 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003160 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
3161}
3162
3163/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003164 * If "arg" is a simple function call without arguments then call it and return
3165 * the result. Otherwise return NOTDONE.
3166 */
3167 int
3168may_call_simple_func(
3169 char_u *arg,
3170 typval_T *rettv)
3171{
3172 char_u *parens = (char_u *)strstr((char *)arg, "()");
3173 int r = NOTDONE;
3174
3175 // If the expression is "FuncName()" then we can skip a lot of overhead.
3176 if (parens != NULL && *skipwhite(parens + 2) == NUL)
3177 {
3178 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
3179
3180 if (to_name_end(p, TRUE) == parens)
3181 r = call_simple_func(arg, (int)(parens - arg), rettv);
3182 }
3183 return r;
3184}
3185
3186/*
3187 * Handle zero level expression with optimization for a simple function call.
3188 * Same arguments and return value as eval0().
3189 */
3190 int
3191eval0_simple_funccal(
3192 char_u *arg,
3193 typval_T *rettv,
3194 exarg_T *eap,
3195 evalarg_T *evalarg)
3196{
3197 int r = may_call_simple_func(arg, rettv);
3198
3199 if (r == NOTDONE)
3200 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
3201 return r;
3202}
3203
3204/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003205 * Like eval0() but when "retarg" is not NULL store the pointer to after the
3206 * expression and don't check what comes after the expression.
3207 */
3208 int
3209eval0_retarg(
3210 char_u *arg,
3211 typval_T *rettv,
3212 exarg_T *eap,
3213 evalarg_T *evalarg,
3214 char_u **retarg)
3215{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 int ret;
3217 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00003218 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003219 int did_emsg_before = did_emsg;
3220 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003221 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003222 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
3224 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003225 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003226
Bram Moolenaar79481362022-06-27 20:15:10 +01003227 if (ret != FAIL)
3228 {
3229 expr_end = p;
3230 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003231
Bram Moolenaar79481362022-06-27 20:15:10 +01003232 // In Vim9 script a command block is not split at NL characters for
3233 // commands using an expression argument. Skip over a '#' comment to
3234 // check for a following NL. Require white space before the '#'.
3235 if (in_vim9script() && p > expr_end && retarg == NULL)
3236 while (*p == '#')
3237 {
3238 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003239
Bram Moolenaar79481362022-06-27 20:15:10 +01003240 if (nl == NULL)
3241 break;
3242 p = skipwhite(nl + 1);
3243 if (eap != NULL && *p != NUL)
3244 eap->nextcmd = p;
3245 check_for_end = FALSE;
3246 }
3247
3248 if (check_for_end)
3249 end_error = !ends_excmd2(arg, p);
3250 }
3251
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003252 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
3254 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003255 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 /*
3257 * Report the invalid expression unless the expression evaluation has
3258 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003259 * exception, or we already gave a more specific error.
3260 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003262 if (!aborting()
3263 && did_emsg == did_emsg_before
3264 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003265 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003266 {
3267 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003268 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003269 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003270 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003271 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003272
zeertzjqf2588b62023-05-05 17:22:35 +01003273 if (eap != NULL && p != NULL)
3274 {
3275 // Some of the expression may not have been consumed.
3276 // Only execute a next command if it cannot be a "||" operator.
3277 // The next command may be "catch".
3278 char_u *nextcmd = check_nextcmd(p);
3279 if (nextcmd != NULL && *nextcmd != '|')
3280 eap->nextcmd = nextcmd;
3281 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003282 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003284
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003285 if (retarg != NULL)
3286 *retarg = p;
3287 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003288 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 return ret;
3291}
3292
3293/*
3294 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003295 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003296 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 *
3298 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003299 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003301 * Note: "rettv.v_lock" is not set.
3302 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 * Return OK or FAIL.
3304 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003305 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003306eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003308 char_u *p;
3309 int getnext;
3310
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003311 CLEAR_POINTER(rettv);
3312
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 /*
3314 * Get the first variable.
3315 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003316 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return FAIL;
3318
Bram Moolenaar793648f2020-06-26 21:28:25 +02003319 p = eval_next_non_blank(*arg, evalarg, &getnext);
3320 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003322 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003323 int result;
3324 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003325 evalarg_T *evalarg_used = evalarg;
3326 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003327 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003328 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003329 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003330
3331 if (evalarg == NULL)
3332 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003333 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003334 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003335 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003336 orig_flags = evalarg_used->eval_flags;
3337 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003338
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003339 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003340 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003341 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003342 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003343 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003344 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003345 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003346 clear_tv(rettv);
3347 return FAIL;
3348 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003349 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003350 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003353 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 int error = FALSE;
3356
Bram Moolenaar13106602020-10-04 16:06:05 +02003357 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003358 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003359 else if (vim9script)
3360 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003361 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003363 if (error || !op_falsy || !result)
3364 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003365 if (error)
3366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368
3369 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003370 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003372 if (op_falsy)
3373 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003374 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003375 {
mityu4ac198c2021-05-28 17:52:40 +02003376 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003377 clear_tv(rettv);
3378 return FAIL;
3379 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003380 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003381 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003382 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003383 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003384 {
3385 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003387 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003388 if (!op_falsy || !result)
3389 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003391 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003393 /*
3394 * Check for the ":".
3395 */
3396 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3397 if (*p != ':')
3398 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003399 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003400 if (evaluate && result)
3401 clear_tv(rettv);
3402 evalarg_used->eval_flags = orig_flags;
3403 return FAIL;
3404 }
3405 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003406 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003407 else
3408 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003409 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003410 {
3411 error_white_both(p, 1);
3412 clear_tv(rettv);
3413 evalarg_used->eval_flags = orig_flags;
3414 return FAIL;
3415 }
3416 *arg = p;
3417 }
3418
3419 /*
3420 * Get the third variable. Recursive!
3421 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003422 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003423 {
mityu4ac198c2021-05-28 17:52:40 +02003424 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003425 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003426 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003427 return FAIL;
3428 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003429 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3430 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003431 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003432 if (eval1(arg, &var2, evalarg_used) == FAIL)
3433 {
3434 if (evaluate && result)
3435 clear_tv(rettv);
3436 evalarg_used->eval_flags = orig_flags;
3437 return FAIL;
3438 }
3439 if (evaluate && !result)
3440 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003442
3443 if (evalarg == NULL)
3444 clear_evalarg(&local_evalarg, NULL);
3445 else
3446 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448
3449 return OK;
3450}
3451
3452/*
3453 * Handle first level expression:
3454 * expr2 || expr2 || expr2 logical OR
3455 *
3456 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003457 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 *
3459 * Return OK or FAIL.
3460 */
3461 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003462eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003464 char_u *p;
3465 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003468 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003470 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 return FAIL;
3472
3473 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003474 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003476 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003477 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003479 evalarg_T *evalarg_used = evalarg;
3480 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003481 int evaluate;
3482 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003483 long result = FALSE;
3484 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003485 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003486 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003487
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003488 if (evalarg == NULL)
3489 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003490 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003491 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003492 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003493 orig_flags = evalarg_used->eval_flags;
3494 evaluate = orig_flags & EVAL_EVALUATE;
3495 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003496 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003497 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003498 result = tv_get_bool_chk(rettv, &error);
3499 else if (tv_get_number_chk(rettv, &error) != 0)
3500 result = TRUE;
3501 clear_tv(rettv);
3502 if (error)
3503 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505
3506 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003507 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003509 while (p[0] == '|' && p[1] == '|')
3510 {
3511 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003512 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003513 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003514 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003515 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003516 {
3517 error_white_both(p, 2);
3518 clear_tv(rettv);
3519 return FAIL;
3520 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003521 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003522 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003523
3524 /*
3525 * Get the second variable.
3526 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003527 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003528 {
mityu4ac198c2021-05-28 17:52:40 +02003529 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003530 clear_tv(rettv);
3531 return FAIL;
3532 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003533 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3534 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003535 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003536 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003537 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003538
3539 /*
3540 * Compute the result.
3541 */
3542 if (evaluate && !result)
3543 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003544 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003545 result = tv_get_bool_chk(&var2, &error);
3546 else if (tv_get_number_chk(&var2, &error) != 0)
3547 result = TRUE;
3548 clear_tv(&var2);
3549 if (error)
3550 return FAIL;
3551 }
3552 if (evaluate)
3553 {
3554 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003555 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003556 rettv->v_type = VAR_BOOL;
3557 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003558 }
3559 else
3560 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003561 rettv->v_type = VAR_NUMBER;
3562 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003563 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003564 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003565
3566 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003568
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003569 if (evalarg == NULL)
3570 clear_evalarg(&local_evalarg, NULL);
3571 else
3572 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574
3575 return OK;
3576}
3577
3578/*
3579 * Handle second level expression:
3580 * expr3 && expr3 && expr3 logical AND
3581 *
3582 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003583 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 *
3585 * Return OK or FAIL.
3586 */
3587 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003588eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003590 char_u *p;
3591 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
3593 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003594 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003596 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 return FAIL;
3598
3599 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003600 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003602 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003603 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003605 evalarg_T *evalarg_used = evalarg;
3606 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003607 int orig_flags;
3608 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003609 long result = TRUE;
3610 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003611 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003612 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003613
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003614 if (evalarg == NULL)
3615 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003616 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003617 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003618 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003619 orig_flags = evalarg_used->eval_flags;
3620 evaluate = orig_flags & EVAL_EVALUATE;
3621 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003622 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003623 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003624 result = tv_get_bool_chk(rettv, &error);
3625 else if (tv_get_number_chk(rettv, &error) == 0)
3626 result = FALSE;
3627 clear_tv(rettv);
3628 if (error)
3629 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631
3632 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003633 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003635 while (p[0] == '&' && p[1] == '&')
3636 {
3637 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003638 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003639 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003640 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003641 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003642 {
3643 error_white_both(p, 2);
3644 clear_tv(rettv);
3645 return FAIL;
3646 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003647 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003648 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003649
3650 /*
3651 * Get the second variable.
3652 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003653 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003654 {
mityu4ac198c2021-05-28 17:52:40 +02003655 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003656 clear_tv(rettv);
3657 return FAIL;
3658 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003659 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3660 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003661 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003662 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003663 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003664 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003665
3666 /*
3667 * Compute the result.
3668 */
3669 if (evaluate && result)
3670 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003671 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003672 result = tv_get_bool_chk(&var2, &error);
3673 else if (tv_get_number_chk(&var2, &error) == 0)
3674 result = FALSE;
3675 clear_tv(&var2);
3676 if (error)
3677 return FAIL;
3678 }
3679 if (evaluate)
3680 {
3681 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003682 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003683 rettv->v_type = VAR_BOOL;
3684 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003685 }
3686 else
3687 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003688 rettv->v_type = VAR_NUMBER;
3689 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003690 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003691 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003692
3693 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003695
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003696 if (evalarg == NULL)
3697 clear_evalarg(&local_evalarg, NULL);
3698 else
3699 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701
3702 return OK;
3703}
3704
3705/*
3706 * Handle third level expression:
3707 * var1 == var2
3708 * var1 =~ var2
3709 * var1 != var2
3710 * var1 !~ var2
3711 * var1 > var2
3712 * var1 >= var2
3713 * var1 < var2
3714 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003715 * var1 is var2
3716 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 *
3718 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003719 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 *
3721 * Return OK or FAIL.
3722 */
3723 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003724eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003727 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003728 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003730 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
3732 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003733 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003735 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 return FAIL;
3737
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003738 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003739
Bram Moolenaar696ba232020-07-29 21:20:41 +02003740 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003743 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003745 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003747 typval_T var2;
3748 int ic;
3749 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003750 int evaluate = evalarg == NULL
3751 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003752 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003753
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003754 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003755 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003756 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003757 p = *arg;
3758 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003759 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3760 {
mityu4ac198c2021-05-28 17:52:40 +02003761 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003762 clear_tv(rettv);
3763 return FAIL;
3764 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003765
Bram Moolenaar696ba232020-07-29 21:20:41 +02003766 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3767 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003768 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003769 clear_tv(rettv);
3770 return FAIL;
3771 }
3772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003773 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (p[len] == '?')
3775 {
3776 ic = TRUE;
3777 ++len;
3778 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003779 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 else if (p[len] == '#')
3781 {
3782 ic = FALSE;
3783 ++len;
3784 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003785 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003787 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
3789 /*
3790 * Get the second variable.
3791 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003792 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3793 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003794 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003795 clear_tv(rettv);
3796 return FAIL;
3797 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003798 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003799 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003801 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return FAIL;
3803 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003804 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003805 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003806 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003807
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003808 // use the line of the comparison for messages
3809 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003810 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003811 {
3812 ret = FAIL;
3813 clear_tv(rettv);
3814 }
3815 else
3816 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003817 clear_tv(&var2);
3818 return ret;
3819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821
3822 return OK;
3823}
3824
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003825/*
3826 * Make a copy of blob "tv1" and append blob "tv2".
3827 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 void
3829eval_addblob(typval_T *tv1, typval_T *tv2)
3830{
3831 blob_T *b1 = tv1->vval.v_blob;
3832 blob_T *b2 = tv2->vval.v_blob;
3833 blob_T *b = blob_alloc();
3834 int i;
3835
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003836 if (b == NULL)
3837 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003839 for (i = 0; i < blob_len(b1); i++)
3840 ga_append(&b->bv_ga, blob_get(b1, i));
3841 for (i = 0; i < blob_len(b2); i++)
3842 ga_append(&b->bv_ga, blob_get(b2, i));
3843
3844 clear_tv(tv1);
3845 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846}
3847
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003848/*
3849 * Make a copy of list "tv1" and append list "tv2".
3850 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 int
3852eval_addlist(typval_T *tv1, typval_T *tv2)
3853{
3854 typval_T var3;
3855
3856 // concatenate Lists
3857 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3858 {
3859 clear_tv(tv1);
3860 clear_tv(tv2);
3861 return FAIL;
3862 }
3863 clear_tv(tv1);
3864 *tv1 = var3;
3865 return OK;
3866}
3867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003869 * Handle the bitwise left/right shift operator expression:
3870 * var1 << var2
3871 * var1 >> var2
3872 *
3873 * "arg" must point to the first non-white of the expression.
3874 * "arg" is advanced to just after the recognized expression.
3875 *
3876 * Return OK or FAIL.
3877 */
3878 static int
3879eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3880{
3881 /*
3882 * Get the first expression.
3883 */
3884 if (eval6(arg, rettv, evalarg) == FAIL)
3885 return FAIL;
3886
3887 /*
3888 * Repeat computing, until no '<<' or '>>' is following.
3889 */
3890 for (;;)
3891 {
3892 char_u *p;
3893 int getnext;
3894 exprtype_T type;
3895 int evaluate;
3896 typval_T var2;
3897 int vim9script;
3898
3899 p = eval_next_non_blank(*arg, evalarg, &getnext);
3900 if (p[0] == '<' && p[1] == '<')
3901 type = EXPR_LSHIFT;
3902 else if (p[0] == '>' && p[1] == '>')
3903 type = EXPR_RSHIFT;
3904 else
3905 return OK;
3906
3907 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003908 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3909 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003910 {
3911 // left operand should be a number
3912 emsg(_(e_bitshift_ops_must_be_number));
3913 clear_tv(rettv);
3914 return FAIL;
3915 }
3916
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003917 vim9script = in_vim9script();
3918 if (getnext)
3919 {
3920 *arg = eval_next_line(*arg, evalarg);
3921 p = *arg;
3922 }
3923 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3924 {
3925 error_white_both(*arg, 2);
3926 clear_tv(rettv);
3927 return FAIL;
3928 }
3929
3930 /*
3931 * Get the second variable.
3932 */
3933 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3934 {
3935 error_white_both(p, 2);
3936 clear_tv(rettv);
3937 return FAIL;
3938 }
3939 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3940 if (eval6(arg, &var2, evalarg) == FAIL)
3941 {
3942 clear_tv(rettv);
3943 return FAIL;
3944 }
3945
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003946 if (evaluate)
3947 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003948 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3949 {
3950 // right operand should be a positive number
3951 if (var2.v_type != VAR_NUMBER)
3952 emsg(_(e_bitshift_ops_must_be_number));
3953 else
3954 emsg(_(e_bitshift_ops_must_be_positive));
3955 clear_tv(rettv);
3956 clear_tv(&var2);
3957 return FAIL;
3958 }
3959
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003960 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3961 // shifting more bits than we have always results in zero
3962 rettv->vval.v_number = 0;
3963 else if (type == EXPR_LSHIFT)
3964 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003965 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003966 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003967 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003968 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003969 }
3970
3971 clear_tv(&var2);
3972 }
3973
3974 return OK;
3975}
3976
3977/*
3978 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003979 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003981 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003982 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 *
3984 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003985 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 *
3987 * Return OK or FAIL.
3988 */
3989 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003990eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003993 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003995 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 return FAIL;
3997
3998 /*
3999 * Repeat computing, until no '+', '-' or '.' is following.
4000 */
4001 for (;;)
4002 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004003 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004004 int getnext;
4005 char_u *p;
4006 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004007 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004008 int concat;
4009 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02004010 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004011
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004012 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004013 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004014 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004015 p = eval_next_non_blank(*arg, evalarg, &getnext);
4016 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02004017 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004018 if ((op != '+' && op != '-' && !concat) || p[1] == '='
4019 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004021 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
4022 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004023
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004024 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004025 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004026 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004027 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004028 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004029 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02004030 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004031 {
mityu4ac198c2021-05-28 17:52:40 +02004032 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004033 clear_tv(rettv);
4034 return FAIL;
4035 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004036 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004037 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004038 if ((op != '+' || (rettv->v_type != VAR_LIST
4039 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004040 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004041 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004042 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004043 int error = FALSE;
4044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004045 // For "list + ...", an illegal use of the first operand as
4046 // a number cannot be determined before evaluating the 2nd
4047 // operand: if this is also a list, all is ok.
4048 // For "something . ...", "something - ..." or "non-list + ...",
4049 // we know that the first operand needs to be a string or number
4050 // without evaluating the 2nd operand. So check before to avoid
4051 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004052 if (op != '.')
4053 tv_get_number_chk(rettv, &error);
4054 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004055 {
4056 clear_tv(rettv);
4057 return FAIL;
4058 }
4059 }
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 /*
4062 * Get the second variable.
4063 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02004064 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004065 {
mityu89dcb4d2021-05-28 13:50:17 +02004066 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004067 clear_tv(rettv);
4068 return FAIL;
4069 }
4070 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004071 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 return FAIL;
4075 }
4076
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004077 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
4079 /*
4080 * Compute the result.
4081 */
4082 if (op == '.')
4083 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004084 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4085 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004086 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004087
Bram Moolenaar2e086612020-08-25 22:37:48 +02004088 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004089 || var2.v_type == VAR_CHANNEL
4090 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02004091 semsg(_(e_using_invalid_value_as_string_str),
4092 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02004093 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004094 {
4095 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
4096 var2.vval.v_float);
4097 s2 = buf2;
4098 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004099 else
4100 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004102 {
4103 clear_tv(rettv);
4104 clear_tv(&var2);
4105 return FAIL;
4106 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004107 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 clear_tv(rettv);
4109 rettv->v_type = VAR_STRING;
4110 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004112 else if (op == '+' && rettv->v_type == VAR_BLOB
4113 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004115 else if (op == '+' && rettv->v_type == VAR_LIST
4116 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004117 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004119 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 else
4122 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004123 int error = FALSE;
4124 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004125 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004126
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004127 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004129 f1 = rettv->vval.v_float;
4130 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004131 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004132 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004133 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004134 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004135 if (error)
4136 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02004137 // This can only happen for "list + non-list" or
4138 // "blob + non-blob". For "non-list + ..." or
4139 // "something - ...", we returned before evaluating the
4140 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004141 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02004142 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004143 return FAIL;
4144 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004145 if (var2.v_type == VAR_FLOAT)
4146 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004147 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004148 if (var2.v_type == VAR_FLOAT)
4149 {
4150 f2 = var2.vval.v_float;
4151 n2 = 0;
4152 }
4153 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004154 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004155 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004156 if (error)
4157 {
4158 clear_tv(rettv);
4159 clear_tv(&var2);
4160 return FAIL;
4161 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004162 if (rettv->v_type == VAR_FLOAT)
4163 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004166
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004167 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004168 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4169 {
4170 if (op == '+')
4171 f1 = f1 + f2;
4172 else
4173 f1 = f1 - f2;
4174 rettv->v_type = VAR_FLOAT;
4175 rettv->vval.v_float = f1;
4176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004178 {
4179 if (op == '+')
4180 n1 = n1 + n2;
4181 else
4182 n1 = n1 - n2;
4183 rettv->v_type = VAR_NUMBER;
4184 rettv->vval.v_number = n1;
4185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189 }
4190 return OK;
4191}
4192
4193/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004194 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 * * number multiplication
4196 * / number division
4197 * % number modulo
4198 *
4199 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004200 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 *
4202 * Return OK or FAIL.
4203 */
4204 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004205eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004206 char_u **arg,
4207 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004208 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004209 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004211 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004214 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004216 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218
4219 /*
4220 * Repeat computing, until no '*', '/' or '%' is following.
4221 */
4222 for (;;)
4223 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004224 int evaluate;
4225 int getnext;
4226 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02004227 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004228 int op;
4229 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004230 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004231 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004232
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004233 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02004234 p = eval_next_non_blank(*arg, evalarg, &getnext);
4235 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004236 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004238
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004239 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004240 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004241 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004242 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004243 {
4244 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
4245 {
mityu4ac198c2021-05-28 17:52:40 +02004246 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004247 clear_tv(rettv);
4248 return FAIL;
4249 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004250 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004253 f1 = 0;
4254 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004255 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004256 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004258 if (rettv->v_type == VAR_FLOAT)
4259 {
4260 f1 = rettv->vval.v_float;
4261 use_float = TRUE;
4262 n1 = 0;
4263 }
4264 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004265 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (error)
4268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 else
4271 n1 = 0;
4272
4273 /*
4274 * Get the second variable.
4275 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004276 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4277 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004278 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004279 clear_tv(rettv);
4280 return FAIL;
4281 }
4282 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004283 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 return FAIL;
4285
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004286 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004288 if (var2.v_type == VAR_FLOAT)
4289 {
4290 if (!use_float)
4291 {
4292 f1 = n1;
4293 use_float = TRUE;
4294 }
4295 f2 = var2.vval.v_float;
4296 n2 = 0;
4297 }
4298 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004300 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004301 clear_tv(&var2);
4302 if (error)
4303 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004304 if (use_float)
4305 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 /*
4309 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004310 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004312 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004314 if (op == '*')
4315 f1 = f1 * f2;
4316 else if (op == '/')
4317 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004318#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004319 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004320 if (f2 == 0.0)
4321 {
4322 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004323 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004324 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004325 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004326 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004327 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004328 }
4329 else
4330 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004331#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004332 // We rely on the floating point library to handle divide
4333 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004334 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004335#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004338 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004339 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004340 return FAIL;
4341 }
4342 rettv->v_type = VAR_FLOAT;
4343 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 else
4346 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004347 int failed = FALSE;
4348
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004349 if (op == '*')
4350 n1 = n1 * n2;
4351 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004352 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004354 n1 = num_modulus(n1, n2, &failed);
4355 if (failed)
4356 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004357
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004358 rettv->v_type = VAR_NUMBER;
4359 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 }
4363
4364 return OK;
4365}
4366
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004367/*
4368 * Handle a type cast before a base level expression.
4369 * "arg" must point to the first non-white of the expression.
4370 * "arg" is advanced to just after the recognized expression.
4371 * Return OK or FAIL.
4372 */
4373 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004374eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004375 char_u **arg,
4376 typval_T *rettv,
4377 evalarg_T *evalarg,
4378 int want_string) // after "." operator
4379{
4380 type_T *want_type = NULL;
4381 garray_T type_list; // list of pointers to allocated types
4382 int res;
4383 int evaluate = evalarg == NULL ? 0
4384 : (evalarg->eval_flags & EVAL_EVALUATE);
4385
4386 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004387 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4388 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004389 {
4390 ++*arg;
4391 ga_init2(&type_list, sizeof(type_T *), 10);
4392 want_type = parse_type(arg, &type_list, TRUE);
4393 if (want_type == NULL && (evaluate || **arg != '>'))
4394 {
4395 clear_type_list(&type_list);
4396 return FAIL;
4397 }
4398
4399 if (**arg != '>')
4400 {
4401 if (*skipwhite(*arg) == '>')
4402 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4403 else
4404 emsg(_(e_missing_gt));
4405 clear_type_list(&type_list);
4406 return FAIL;
4407 }
4408 ++*arg;
4409 *arg = skipwhite_and_linebreak(*arg, evalarg);
4410 }
4411
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004412 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004413
4414 if (want_type != NULL && evaluate)
4415 {
4416 if (res == OK)
4417 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004418 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4419 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004420
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004421 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004422 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004423 if (want_type->tt_type == VAR_BOOL
4424 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004425 && (actual->tt_flags & TTFLAG_BOOL_OK))
4426 {
4427 int n = tv2bool(rettv);
4428
4429 // can use "0" and "1" for boolean in some places
4430 clear_tv(rettv);
4431 rettv->v_type = VAR_BOOL;
4432 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4433 }
4434 else
4435 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004436 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004437
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004438 res = check_type(want_type, actual, TRUE, where);
4439 }
4440 }
4441 }
4442 clear_type_list(&type_list);
4443 }
4444
4445 return res;
4446}
4447
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004448 int
4449eval_leader(char_u **arg, int vim9)
4450{
4451 char_u *s = *arg;
4452 char_u *p = *arg;
4453
4454 while (*p == '!' || *p == '-' || *p == '+')
4455 {
4456 char_u *n = skipwhite(p + 1);
4457
4458 // ++, --, -+ and +- are not accepted in Vim9 script
4459 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4460 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004461 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004462 return FAIL;
4463 }
4464 p = n;
4465 }
4466 *arg = p;
4467 return OK;
4468}
4469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004471 * Check for a predefined value "true", "false" and "null.*".
4472 * Return OK when recognized.
4473 */
4474 int
4475handle_predefined(char_u *s, int len, typval_T *rettv)
4476{
4477 switch (len)
4478 {
4479 case 4: if (STRNCMP(s, "true", 4) == 0)
4480 {
4481 rettv->v_type = VAR_BOOL;
4482 rettv->vval.v_number = VVAL_TRUE;
4483 return OK;
4484 }
4485 if (STRNCMP(s, "null", 4) == 0)
4486 {
4487 rettv->v_type = VAR_SPECIAL;
4488 rettv->vval.v_number = VVAL_NULL;
4489 return OK;
4490 }
4491 break;
4492 case 5: if (STRNCMP(s, "false", 5) == 0)
4493 {
4494 rettv->v_type = VAR_BOOL;
4495 rettv->vval.v_number = VVAL_FALSE;
4496 return OK;
4497 }
4498 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004499 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4500 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004501#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004502 rettv->v_type = VAR_JOB;
4503 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004504#else
4505 rettv->v_type = VAR_SPECIAL;
4506 rettv->vval.v_number = VVAL_NULL;
4507#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004508 return OK;
4509 }
4510 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004511 case 9:
4512 if (STRNCMP(s, "null_", 5) != 0)
4513 break;
4514 if (STRNCMP(s + 5, "list", 4) == 0)
4515 {
4516 rettv->v_type = VAR_LIST;
4517 rettv->vval.v_list = NULL;
4518 return OK;
4519 }
4520 if (STRNCMP(s + 5, "dict", 4) == 0)
4521 {
4522 rettv->v_type = VAR_DICT;
4523 rettv->vval.v_dict = NULL;
4524 return OK;
4525 }
4526 if (STRNCMP(s + 5, "blob", 4) == 0)
4527 {
4528 rettv->v_type = VAR_BLOB;
4529 rettv->vval.v_blob = NULL;
4530 return OK;
4531 }
4532 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004533 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4534 {
4535 rettv->v_type = VAR_CLASS;
4536 rettv->vval.v_class = NULL;
4537 return OK;
4538 }
4539 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004540 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4541 {
4542 rettv->v_type = VAR_STRING;
4543 rettv->vval.v_string = NULL;
4544 return OK;
4545 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004546 if (STRNCMP(s, "null_object", 11) == 0)
4547 {
4548 rettv->v_type = VAR_OBJECT;
4549 rettv->vval.v_object = NULL;
4550 return OK;
4551 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004552 break;
4553 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004554 if (STRNCMP(s, "null_channel", 12) == 0)
4555 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004556#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004557 rettv->v_type = VAR_CHANNEL;
4558 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004559#else
4560 rettv->v_type = VAR_SPECIAL;
4561 rettv->vval.v_number = VVAL_NULL;
4562#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004563 return OK;
4564 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004565 if (STRNCMP(s, "null_partial", 12) == 0)
4566 {
4567 rettv->v_type = VAR_PARTIAL;
4568 rettv->vval.v_partial = NULL;
4569 return OK;
4570 }
4571 break;
4572 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4573 {
4574 rettv->v_type = VAR_FUNC;
4575 rettv->vval.v_string = NULL;
4576 return OK;
4577 }
4578 break;
4579 }
4580 return FAIL;
4581}
4582
4583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 * Handle sixth level expression:
4585 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004586 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004587 * "string" string constant
4588 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 * &option-name option value
4590 * @r register contents
4591 * identifier variable value
4592 * function() function call
4593 * $VAR environment variable
4594 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004595 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004597 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004598 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 *
4600 * Also handle:
4601 * ! in front logical NOT
4602 * - in front unary minus
4603 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004604 * trailing [] subscript in String or List
4605 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004606 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 *
4608 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004609 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 *
4611 * Return OK or FAIL.
4612 */
4613 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004614eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004615 char_u **arg,
4616 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004617 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004620 int evaluate = evalarg != NULL
4621 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 int len;
4623 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004624 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 char_u *start_leader, *end_leader;
4626 int ret = OK;
4627 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004628 static int recurse = 0;
4629 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004635 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
4637 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004638 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 */
4640 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004641 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004642 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 end_leader = *arg;
4644
Keith Thompson184f71c2024-01-04 21:19:04 +01004645 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004646 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004647 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004648 ++*arg;
4649 return FAIL;
4650 }
4651
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004652 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004653 // and crash. With MSVC the stack is smaller.
4654 if (recurse ==
4655#ifdef _MSC_VER
4656 300
4657#else
4658 1000
4659#endif
4660 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004661 {
4662 semsg(_(e_expression_too_recursive_str), *arg);
4663 return FAIL;
4664 }
4665 ++recurse;
4666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 switch (**arg)
4668 {
4669 /*
4670 * Number constant.
4671 */
4672 case '0':
4673 case '1':
4674 case '2':
4675 case '3':
4676 case '4':
4677 case '5':
4678 case '6':
4679 case '7':
4680 case '8':
4681 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004682 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004683
4684 // Apply prefixed "-" and "+" now. Matters especially when
4685 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004686 if (ret == OK && evaluate && end_leader > start_leader
4687 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004688 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 break;
4690
4691 /*
4692 * String constant: "string".
4693 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004694 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 break;
4696
4697 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004698 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004700 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701 break;
4702
4703 /*
4704 * List: [expr, expr]
4705 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004706 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 break;
4708
4709 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004710 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004711 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004712 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004713 {
4714 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4715 }
4716 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004717 {
4718 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004719 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004720 }
4721 else
4722 ret = NOTDONE;
4723 break;
4724
4725 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004726 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004727 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004728 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004729 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004730 ret = NOTDONE;
4731 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004732 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004733 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004734 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 break;
4736
4737 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004738 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004740 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 break;
4742
4743 /*
4744 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004745 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 */
LemonBoy2eaef102022-05-06 13:14:50 +01004747 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4748 ret = eval_interp_string(arg, rettv, evaluate);
4749 else
4750 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 break;
4752
4753 /*
4754 * Register contents: @r.
4755 */
4756 case '@': ++*arg;
4757 if (evaluate)
4758 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004759 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004760 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004761 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004762 emsg_invreg(**arg);
4763 else
4764 {
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = get_reg_contents(**arg,
4767 GREG_EXPR_SRC);
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770 if (**arg != NUL)
4771 ++*arg;
4772 break;
4773
4774 /*
4775 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004776 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004778 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004779 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004780 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004781 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004782 if (ret == OK && evaluate)
4783 {
4784 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4785
Bram Moolenaara9931532021-06-12 15:58:16 +02004786 // Compile it here to get the return type. The return
4787 // type is optional, when it's missing use t_unknown.
4788 // This is recognized in compile_return().
4789 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4790 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004791 if (compile_def_function(ufunc, FALSE,
4792 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004793 {
4794 clear_tv(rettv);
4795 ret = FAIL;
4796 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004797 }
4798 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004799 if (ret == NOTDONE)
4800 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004801 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004802 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004803
Bram Moolenaar9215f012020-06-27 21:18:00 +02004804 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004805 if (**arg == ')')
4806 ++*arg;
4807 else if (ret == OK)
4808 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004809 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004810 clear_tv(rettv);
4811 ret = FAIL;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
4814 break;
4815
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 break;
4818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004819
4820 if (ret == NOTDONE)
4821 {
4822 /*
4823 * Must be a variable or function name.
4824 * Can also be a curly-braces kind of name: {expr}.
4825 */
4826 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004827 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828 if (alias != NULL)
4829 s = alias;
4830
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004831 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004832 ret = FAIL;
4833 else
4834 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004835 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4836
Bram Moolenaar4525a572022-02-13 11:57:33 +00004837 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004838 {
4839 emsg(_(e_cannot_use_underscore_here));
4840 ret = FAIL;
4841 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004842 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004843 && s[0] == 's' && s[1] == ':')
4844 {
4845 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4846 ret = FAIL;
4847 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004848 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004849 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004850 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004851 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004852 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004854 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004855 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004856 // get the value of "true", "false", etc. or a variable
4857 ret = FAIL;
4858 if (vim9script)
4859 ret = handle_predefined(s, len, rettv);
4860 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004861 {
4862 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004863 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004864 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004865 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004866 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004867 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004868 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004869 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004870 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004871 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004872 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004873 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004874 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004875 }
4876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004877 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4878 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004879 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004880 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 /*
4883 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4884 */
4885 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004886 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004887
4888 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004889 return ret;
4890}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004891
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004892/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004893 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004894 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004895 * Adjusts "end_leaderp" until it is at "start_leader".
4896 */
4897 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004898eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004899 typval_T *rettv,
4900 int numeric_only,
4901 char_u *start_leader,
4902 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004903{
4904 char_u *end_leader = *end_leaderp;
4905 int ret = OK;
4906 int error = FALSE;
4907 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004908 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004909 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004910 float_T f = 0.0;
4911
4912 if (rettv->v_type == VAR_FLOAT)
4913 f = rettv->vval.v_float;
4914 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004915 {
4916 while (VIM_ISWHITE(end_leader[-1]))
4917 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004918 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004919 val = tv2bool(rettv);
4920 else
4921 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004922 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004923 if (error)
4924 {
4925 clear_tv(rettv);
4926 ret = FAIL;
4927 }
4928 else
4929 {
4930 while (end_leader > start_leader)
4931 {
4932 --end_leader;
4933 if (*end_leader == '!')
4934 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004935 if (numeric_only)
4936 {
4937 ++end_leader;
4938 break;
4939 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004940 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004941 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004942 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004943 {
4944 rettv->v_type = VAR_BOOL;
4945 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4946 }
4947 else
4948 f = !f;
4949 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004950 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004951 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004952 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004953 type = VAR_BOOL;
4954 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004955 }
4956 else if (*end_leader == '-')
4957 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004958 if (rettv->v_type == VAR_FLOAT)
4959 f = -f;
4960 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004961 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004962 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004963 type = VAR_NUMBER;
4964 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004965 }
4966 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004967 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004969 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004970 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004972 else
4973 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004974 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004975 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004976 rettv->v_type = type;
4977 else
4978 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004979 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004982 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 return ret;
4984}
4985
4986/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004987 * Call the function referred to in "rettv".
4988 */
4989 static int
4990call_func_rettv(
4991 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004992 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004993 typval_T *rettv,
4994 int evaluate,
4995 dict_T *selfdict,
4996 typval_T *basetv)
4997{
4998 partial_T *pt = NULL;
4999 funcexe_T funcexe;
5000 typval_T functv;
5001 char_u *s;
5002 int ret;
5003
5004 // need to copy the funcref so that we can clear rettv
5005 if (evaluate)
5006 {
5007 functv = *rettv;
5008 rettv->v_type = VAR_UNKNOWN;
5009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005010 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005011 if (functv.v_type == VAR_PARTIAL)
5012 {
5013 pt = functv.vval.v_partial;
5014 s = partial_name(pt);
5015 }
5016 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005017 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005018 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005019 if (s == NULL || *s == NUL)
5020 {
5021 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005022 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005023 goto theend;
5024 }
5025 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005026 }
5027 else
5028 s = (char_u *)"";
5029
Bram Moolenaara80faa82020-04-12 19:37:17 +02005030 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005031 funcexe.fe_firstline = curwin->w_cursor.lnum;
5032 funcexe.fe_lastline = curwin->w_cursor.lnum;
5033 funcexe.fe_evaluate = evaluate;
5034 funcexe.fe_partial = pt;
5035 funcexe.fe_selfdict = selfdict;
5036 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005037 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005038
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005039theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005040 // Clear the funcref afterwards, so that deleting it while
5041 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005042 if (evaluate)
5043 clear_tv(&functv);
5044
5045 return ret;
5046}
5047
5048/*
5049 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005050 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005051 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5052 */
5053 static int
5054eval_lambda(
5055 char_u **arg,
5056 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005057 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005058 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005059{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005060 int evaluate = evalarg != NULL
5061 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005062 typval_T base = *rettv;
5063 int ret;
5064
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005065 rettv->v_type = VAR_UNKNOWN;
5066
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005067 if (**arg == '{')
5068 {
5069 // ->{lambda}()
5070 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5071 }
5072 else
5073 {
5074 // ->(lambda)()
5075 ++*arg;
5076 ret = eval1(arg, rettv, evalarg);
5077 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005078 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005079 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005080 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005081 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005082 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005083 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5084 && rettv->v_type != VAR_PARTIAL)
5085 {
5086 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5087 return FAIL;
5088 }
5089 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005090 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005091 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005092 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005093
5094 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005095 {
5096 if (verbose)
5097 {
5098 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005099 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005100 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005101 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005102 }
5103 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005104 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005105 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005106 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005107 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005108
5109 // Clear the funcref afterwards, so that deleting it while
5110 // evaluating the arguments is possible (see test55).
5111 if (evaluate)
5112 clear_tv(&base);
5113
5114 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005115}
5116
5117/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005118 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005119 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005120 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5121 */
5122 static int
5123eval_method(
5124 char_u **arg,
5125 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005126 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005127 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005128{
5129 char_u *name;
5130 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005131 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005132 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005133 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005134 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005135 int evaluate = evalarg != NULL
5136 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005137
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005138 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005139
Bram Moolenaarac92e252019-08-03 21:58:38 +02005140 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005141 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005142 if (alias != NULL)
5143 name = alias;
5144
5145 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005146 {
5147 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005148 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005149 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005150 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005151 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005152 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005153 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005154
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005155 // If there is no "(" immediately following, but there is further on,
5156 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5157 // Does not handle anything where "(" is part of the expression.
5158 *arg = skipwhite(*arg);
5159
5160 if (**arg != '(' && alias == NULL
5161 && (paren = vim_strchr(*arg, '(')) != NULL)
5162 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005163 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005164
5165 // Truncate the name a the "(". Avoid trying to get another line
5166 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005167 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005168 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5169 if (evalarg != NULL)
5170 {
5171 getline = evalarg->eval_getline;
5172 evalarg->eval_getline = NULL;
5173 }
5174
5175 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005176 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005177 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005178 *arg = name + len;
5179 ret = FAIL;
5180 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005181 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005182 {
5183 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005184 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005185 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005186
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005187 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005188 if (getline != NULL)
5189 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005190 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005191
5192 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005193 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005194 *arg = skipwhite(*arg);
5195
5196 if (**arg != '(')
5197 {
5198 if (verbose)
5199 semsg(_(e_missing_parenthesis_str), name);
5200 ret = FAIL;
5201 }
5202 else if (VIM_ISWHITE((*arg)[-1]))
5203 {
5204 if (verbose)
5205 emsg(_(e_no_white_space_allowed_before_parenthesis));
5206 ret = FAIL;
5207 }
5208 else
5209 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005210 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005211 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005212 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005213
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005214 // Clear the funcref afterwards, so that deleting it while
5215 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005216 if (evaluate)
5217 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005218 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005219
Bram Moolenaarac92e252019-08-03 21:58:38 +02005220 return ret;
5221}
5222
5223/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005224 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5225 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5227 */
5228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005229eval_index(
5230 char_u **arg,
5231 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005232 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005234{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005235 int evaluate = evalarg != NULL
5236 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005241 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005242 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005244 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5245 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005247 init_tv(&var1);
5248 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 /*
5252 * dict.name
5253 */
5254 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005255 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005257 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005259 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 }
5261 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /*
5264 * something[idx]
5265 *
5266 * Get the (first) variable from inside the [].
5267 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005268 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 if (**arg == ':')
5270 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005271 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005273 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005274 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005275 semsg(_(e_white_space_required_before_and_after_str_at_str),
5276 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005277 clear_tv(&var1);
5278 return FAIL;
5279 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005280 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005282 int error = FALSE;
5283
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005284 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005285 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005286 && var1.v_type == VAR_FLOAT)
5287 {
5288 var1.vval.v_string = typval_tostring(&var1, TRUE);
5289 var1.v_type = VAR_STRING;
5290 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005291
Bram Moolenaar4525a572022-02-13 11:57:33 +00005292 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005293 tv_get_number_chk(&var1, &error);
5294 else
5295 error = tv_get_string_chk(&var1) == NULL;
5296 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005297 {
5298 // not a number or string
5299 clear_tv(&var1);
5300 return FAIL;
5301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303
5304 /*
5305 * Get the second variable from inside the [:].
5306 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005307 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308 if (**arg == ':')
5309 {
5310 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005311 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005312 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005313 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005314 semsg(_(e_white_space_required_before_and_after_str_at_str),
5315 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005316 if (!empty1)
5317 clear_tv(&var1);
5318 return FAIL;
5319 }
5320 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 if (**arg == ']')
5322 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005323 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 if (!empty1)
5326 clear_tv(&var1);
5327 return FAIL;
5328 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005329 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005330 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005331 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 if (!empty1)
5333 clear_tv(&var1);
5334 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005335 return FAIL;
5336 }
5337 }
5338
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005339 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005340 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 if (**arg != ']')
5342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005343 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005344 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005345 clear_tv(&var1);
5346 if (range)
5347 clear_tv(&var2);
5348 return FAIL;
5349 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005350 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 }
5352
5353 if (evaluate)
5354 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005355 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005356 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005357 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005358
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005359 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005362 clear_tv(&var2);
5363 return res;
5364 }
5365 return OK;
5366}
5367
5368/*
5369 * Check if "rettv" can have an [index] or [sli:ce]
5370 */
5371 int
5372check_can_index(typval_T *rettv, int evaluate, int verbose)
5373{
5374 switch (rettv->v_type)
5375 {
5376 case VAR_FUNC:
5377 case VAR_PARTIAL:
5378 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005379 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005380 return FAIL;
5381 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005382 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005383 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005384 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005385 case VAR_BOOL:
5386 case VAR_SPECIAL:
5387 case VAR_JOB:
5388 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005389 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005390 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005391 if (verbose)
5392 emsg(_(e_cannot_index_special_variable));
5393 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005394 case VAR_CLASS:
5395 case VAR_TYPEALIAS:
5396 if (verbose)
5397 check_typval_is_value(rettv);
5398 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005399 case VAR_UNKNOWN:
5400 case VAR_ANY:
5401 case VAR_VOID:
5402 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005404 emsg(_(e_cannot_index_special_variable));
5405 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005407 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005409 case VAR_STRING:
5410 case VAR_LIST:
5411 case VAR_DICT:
5412 case VAR_BLOB:
5413 break;
5414 case VAR_NUMBER:
5415 if (in_vim9script())
5416 emsg(_(e_cannot_index_number));
5417 break;
5418 }
5419 return OK;
5420}
5421
5422/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005423 * slice() function
5424 */
5425 void
5426f_slice(typval_T *argvars, typval_T *rettv)
5427{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005428 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005429 && ((argvars[0].v_type != VAR_STRING
5430 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005431 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005432 && check_for_list_arg(argvars, 0) == FAIL)
5433 || check_for_number_arg(argvars, 1) == FAIL
5434 || check_for_opt_number_arg(argvars, 2) == FAIL))
5435 return;
5436
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005437 if (check_can_index(argvars, TRUE, FALSE) != OK)
5438 return;
5439
5440 copy_tv(argvars, rettv);
5441 eval_index_inner(rettv, TRUE, argvars + 1,
5442 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5443 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005444}
5445
5446/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005447 * Apply index or range to "rettv".
5448 * "var1" is the first index, NULL for [:expr].
5449 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005450 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5451 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005452 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5453 */
5454 int
5455eval_index_inner(
5456 typval_T *rettv,
5457 int is_range,
5458 typval_T *var1,
5459 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005460 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005461 char_u *key,
5462 int keylen,
5463 int verbose)
5464{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005465 varnumber_T n1, n2 = 0;
5466 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005467
5468 n1 = 0;
5469 if (var1 != NULL && rettv->v_type != VAR_DICT)
5470 n1 = tv_get_number(var1);
5471
5472 if (is_range)
5473 {
5474 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005476 if (verbose)
5477 emsg(_(e_cannot_slice_dictionary));
5478 return FAIL;
5479 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005480 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005481 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005482 else
5483 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005484 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005485
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005486 switch (rettv->v_type)
5487 {
5488 case VAR_UNKNOWN:
5489 case VAR_ANY:
5490 case VAR_VOID:
5491 case VAR_FUNC:
5492 case VAR_PARTIAL:
5493 case VAR_FLOAT:
5494 case VAR_BOOL:
5495 case VAR_SPECIAL:
5496 case VAR_JOB:
5497 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005498 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005499 case VAR_CLASS:
5500 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005501 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005502 break; // not evaluating, skipping over subscript
5503
5504 case VAR_NUMBER:
5505 case VAR_STRING:
5506 {
5507 char_u *s = tv_get_string(rettv);
5508
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005510 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005511 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005512 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005513 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005514 else
5515 s = char_from_string(s, n1);
5516 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005517 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // The resulting variable is a substring. If the indexes
5520 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 if (n1 < 0)
5522 {
5523 n1 = len + n1;
5524 if (n1 < 0)
5525 n1 = 0;
5526 }
5527 if (n2 < 0)
5528 n2 = len + n2;
5529 else if (n2 >= len)
5530 n2 = len;
5531 if (n1 >= len || n2 < 0 || n1 > n2)
5532 s = NULL;
5533 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005534 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 }
5536 else
5537 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005538 // The resulting variable is a string of a single
5539 // character. If the index is too big or negative the
5540 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 if (n1 >= len || n1 < 0)
5542 s = NULL;
5543 else
5544 s = vim_strnsave(s + n1, 1);
5545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 clear_tv(rettv);
5547 rettv->v_type = VAR_STRING;
5548 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005549 }
5550 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005552 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005553 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5554 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005555 break;
5556
5557 case VAR_LIST:
5558 if (var1 == NULL)
5559 n1 = 0;
5560 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005561 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005562 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005563 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005564 return FAIL;
5565 break;
5566
5567 case VAR_DICT:
5568 {
5569 dictitem_T *item;
5570 typval_T tmp;
5571
5572 if (key == NULL)
5573 {
5574 key = tv_get_string_chk(var1);
5575 if (key == NULL)
5576 return FAIL;
5577 }
5578
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005579 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005580
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005581 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005582 {
5583 if (verbose)
5584 {
5585 if (keylen > 0)
5586 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005587 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005588 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005589 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005590 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005591
5592 copy_tv(&item->di_tv, &tmp);
5593 clear_tv(rettv);
5594 *rettv = tmp;
5595 }
5596 break;
5597 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 return OK;
5599}
5600
5601/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005602 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005603 */
5604 char_u *
5605partial_name(partial_T *pt)
5606{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005607 if (pt != NULL)
5608 {
5609 if (pt->pt_name != NULL)
5610 return pt->pt_name;
5611 if (pt->pt_func != NULL)
5612 return pt->pt_func->uf_name;
5613 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005614 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005615}
5616
Bram Moolenaarddecc252016-04-06 22:59:37 +02005617 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005618partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005619{
5620 int i;
5621
5622 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005623 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005624 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005625 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005626 if (pt->pt_name != NULL)
5627 {
5628 func_unref(pt->pt_name);
5629 vim_free(pt->pt_name);
5630 }
5631 else
5632 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005633 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005634
Bram Moolenaar54656012021-06-09 20:50:46 +02005635 // "out_up" is no longer used, decrement refcount on partial that owns it.
5636 partial_unref(pt->pt_outer.out_up_partial);
5637
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005638 // Using pt_outer from another partial.
5639 partial_unref(pt->pt_outer_partial);
5640
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005641 // Decrease the reference count for the context of a closure. If down
5642 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005643 if (pt->pt_funcstack != NULL)
5644 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005645 --pt->pt_funcstack->fs_refcount;
5646 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005647 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005648 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005649 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5650 if (pt->pt_loopvars[i] != NULL)
5651 {
5652 --pt->pt_loopvars[i]->lvs_refcount;
5653 loopvars_check_refcount(pt->pt_loopvars[i]);
5654 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005655
Bram Moolenaarddecc252016-04-06 22:59:37 +02005656 vim_free(pt);
5657}
5658
5659/*
5660 * Unreference a closure: decrement the reference count and free it when it
5661 * becomes zero.
5662 */
5663 void
5664partial_unref(partial_T *pt)
5665{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005666 if (pt == NULL)
5667 return;
5668
5669 int done = FALSE;
5670
5671 if (--pt->pt_refcount <= 0)
5672 partial_free(pt);
5673
5674 // If the reference count goes down to one, the funcstack may be the
5675 // only reference and can be freed if no other partials reference it.
5676 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005677 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005678 // careful: if the funcstack is freed it may contain this partial
5679 // and it gets freed as well
5680 if (pt->pt_funcstack != NULL)
5681 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005682
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005683 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005684 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005685 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005686
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005687 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5688 if (pt->pt_loopvars[depth] != NULL
5689 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005690 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005691 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005692 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005693}
5694
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005695/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005696 * Return the next (unique) copy ID.
5697 * Used for serializing nested structures.
5698 */
5699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005700get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005701{
5702 current_copyID += COPYID_INC;
5703 return current_copyID;
5704}
5705
5706/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005707 * Garbage collection for lists and dictionaries.
5708 *
5709 * We use reference counts to be able to free most items right away when they
5710 * are no longer used. But for composite items it's possible that it becomes
5711 * unused while the reference count is > 0: When there is a recursive
5712 * reference. Example:
5713 * :let l = [1, 2, 3]
5714 * :let d = {9: l}
5715 * :let l[1] = d
5716 *
5717 * Since this is quite unusual we handle this with garbage collection: every
5718 * once in a while find out which lists and dicts are not referenced from any
5719 * variable.
5720 *
5721 * Here is a good reference text about garbage collection (refers to Python
5722 * but it applies to all reference-counting mechanisms):
5723 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005724 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005725
5726/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005727 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005728 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005729 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005730 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005731 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005732garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005733{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005734 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005735 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005736 buf_T *buf;
5737 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005738 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005739 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005740
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005741 if (!testing)
5742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005743 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005744 want_garbage_collect = FALSE;
5745 may_garbage_collect = FALSE;
5746 garbage_collect_at_exit = FALSE;
5747 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005748
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005749 // The execution stack can grow big, limit the size.
5750 if (exestack.ga_maxlen - exestack.ga_len > 500)
5751 {
5752 size_t new_len;
5753 char_u *pp;
5754 int n;
5755
5756 // Keep 150% of the current size, with a minimum of the growth size.
5757 n = exestack.ga_len / 2;
5758 if (n < exestack.ga_growsize)
5759 n = exestack.ga_growsize;
5760
5761 // Don't make it bigger though.
5762 if (exestack.ga_len + n < exestack.ga_maxlen)
5763 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005764 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005765 pp = vim_realloc(exestack.ga_data, new_len);
5766 if (pp == NULL)
5767 return FAIL;
5768 exestack.ga_maxlen = exestack.ga_len + n;
5769 exestack.ga_data = pp;
5770 }
5771 }
5772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005773 // We advance by two because we add one for items referenced through
5774 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005775 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005776
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005777 /*
5778 * 1. Go through all accessible variables and mark all lists and dicts
5779 * with copyID.
5780 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005782 // Don't free variables in the previous_funccal list unless they are only
5783 // referenced through previous_funccal. This must be first, because if
5784 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005785 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005787 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005788 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005790 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005791 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005792 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5793 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005795 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005796 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005797 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5798 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005799 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005800 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005801 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005802 abort = abort || set_ref_in_item(
5803 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005804#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005805 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005806 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5807 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005808 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005809 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005810 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5811 NULL, NULL);
5812#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005814 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005815 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005816 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5817 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005818 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005819 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005821 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005822 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005824 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005825 abort = abort || set_ref_in_functions(copyID);
5826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005827 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005828 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005829
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005830 // funcstacks keep variables for closures
5831 abort = abort || set_ref_in_funcstacks(copyID);
5832
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005833 // loopvars keep variables for loop blocks
5834 abort = abort || set_ref_in_loopvars(copyID);
5835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005836 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005837 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005838
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005839 // callbacks in buffers
5840 abort = abort || set_ref_in_buffers(copyID);
5841
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005842 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5843 abort = abort || set_ref_in_insexpand_funcs(copyID);
5844
5845 // 'operatorfunc' callback
5846 abort = abort || set_ref_in_opfunc(copyID);
5847
5848 // 'tagfunc' callback
5849 abort = abort || set_ref_in_tagfunc(copyID);
5850
5851 // 'imactivatefunc' and 'imstatusfunc' callbacks
5852 abort = abort || set_ref_in_im_funcs(copyID);
5853
Bram Moolenaar1dced572012-04-05 16:54:08 +02005854#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005855 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005856#endif
5857
Bram Moolenaardb913952012-06-29 12:54:53 +02005858#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005859 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005860#endif
5861
5862#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005863 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005864#endif
5865
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005866#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005867 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005868 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005869#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005870#ifdef FEAT_NETBEANS_INTG
5871 abort = abort || set_ref_in_nb_channel(copyID);
5872#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005873
Bram Moolenaare3188e22016-05-31 21:13:04 +02005874#ifdef FEAT_TIMERS
5875 abort = abort || set_ref_in_timer(copyID);
5876#endif
5877
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005878#ifdef FEAT_QUICKFIX
5879 abort = abort || set_ref_in_quickfix(copyID);
5880#endif
5881
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005882#ifdef FEAT_TERMINAL
5883 abort = abort || set_ref_in_term(copyID);
5884#endif
5885
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005886#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005887 abort = abort || set_ref_in_popups(copyID);
5888#endif
5889
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005890 abort = abort || set_ref_in_classes(copyID);
5891
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005892 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005893 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005894 /*
5895 * 2. Free lists and dictionaries that are not referenced.
5896 */
5897 did_free = free_unref_items(copyID);
5898
5899 /*
5900 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005901 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005902 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005903 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005904 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005905 else if (p_verbose > 0)
5906 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005907 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005908 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005909
5910 return did_free;
5911}
5912
5913/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005914 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005915 */
5916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005917free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005918{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005919 int did_free = FALSE;
5920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005921 // Let all "free" functions know that we are here. This means no
5922 // dictionaries, lists, channels or jobs are to be freed, because we will
5923 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005924 in_free_unref_items = TRUE;
5925
5926 /*
5927 * PASS 1: free the contents of the items. We don't free the items
5928 * themselves yet, so that it is possible to decrement refcount counters
5929 */
5930
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005931 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005932 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005933
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005934 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005935 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005936
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005937 // Go through the list of objects and free items without this copyID.
5938 did_free |= object_free_nonref(copyID);
5939
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005940 // Go through the list of classes and free items without this copyID.
5941 did_free |= class_free_nonref(copyID);
5942
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005943#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005944 // Go through the list of jobs and free items without the copyID. This
5945 // must happen before doing channels, because jobs refer to channels, but
5946 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005947 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005949 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005950 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5951#endif
5952
5953 /*
5954 * PASS 2: free the items themselves.
5955 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005956 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005957 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005958 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005959
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005960#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005961 // Go through the list of jobs and free items without the copyID. This
5962 // must happen before doing channels, because jobs refer to channels, but
5963 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005964 free_unused_jobs(copyID, COPYID_MASK);
5965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005966 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005967 free_unused_channels(copyID, COPYID_MASK);
5968#endif
5969
5970 in_free_unref_items = FALSE;
5971
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005972 return did_free;
5973}
5974
5975/*
5976 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005977 * "list_stack" is used to add lists to be marked. Can be NULL.
5978 *
5979 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005980 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005982set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005983{
5984 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005985 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005986 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005987 hashtab_T *cur_ht;
5988 ht_stack_T *ht_stack = NULL;
5989 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005990
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005991 cur_ht = ht;
5992 for (;;)
5993 {
5994 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005996 // Mark each item in the hashtab. If the item contains a hashtab
5997 // it is added to ht_stack, if it contains a list it is added to
5998 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005999 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00006000 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006001 if (!HASHITEM_EMPTY(hi))
6002 {
6003 --todo;
6004 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6005 &ht_stack, list_stack);
6006 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006007 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006008
6009 if (ht_stack == NULL)
6010 break;
6011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006013 cur_ht = ht_stack->ht;
6014 tempitem = ht_stack;
6015 ht_stack = ht_stack->prev;
6016 free(tempitem);
6017 }
6018
6019 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006020}
6021
Dominique Pelle748b3082022-01-08 12:41:16 +00006022#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6023 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006024/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006025 * Mark a dict and its items with "copyID".
6026 * Returns TRUE if setting references failed somehow.
6027 */
6028 int
6029set_ref_in_dict(dict_T *d, int copyID)
6030{
6031 if (d != NULL && d->dv_copyID != copyID)
6032 {
6033 d->dv_copyID = copyID;
6034 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
6035 }
6036 return FALSE;
6037}
Dominique Pelle748b3082022-01-08 12:41:16 +00006038#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006039
6040/*
6041 * Mark a list and its items with "copyID".
6042 * Returns TRUE if setting references failed somehow.
6043 */
6044 int
6045set_ref_in_list(list_T *ll, int copyID)
6046{
6047 if (ll != NULL && ll->lv_copyID != copyID)
6048 {
6049 ll->lv_copyID = copyID;
6050 return set_ref_in_list_items(ll, copyID, NULL);
6051 }
6052 return FALSE;
6053}
6054
6055/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006056 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006057 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6058 *
6059 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006060 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006061 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006062set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006063{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006064 listitem_T *li;
6065 int abort = FALSE;
6066 list_T *cur_l;
6067 list_stack_T *list_stack = NULL;
6068 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006070 cur_l = l;
6071 for (;;)
6072 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006073 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006074 // Mark each item in the list. If the item contains a hashtab
6075 // it is added to ht_stack, if it contains a list it is added to
6076 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006077 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
6078 abort = abort || set_ref_in_item(&li->li_tv, copyID,
6079 ht_stack, &list_stack);
6080 if (list_stack == NULL)
6081 break;
6082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006083 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006084 cur_l = list_stack->list;
6085 tempitem = list_stack;
6086 list_stack = list_stack->prev;
6087 free(tempitem);
6088 }
6089
6090 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006091}
6092
6093/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00006094 * Mark the partial in callback 'cb' with "copyID".
6095 */
6096 int
6097set_ref_in_callback(callback_T *cb, int copyID)
6098{
6099 typval_T tv;
6100
6101 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
6102 return FALSE;
6103
6104 tv.v_type = VAR_PARTIAL;
6105 tv.vval.v_partial = cb->cb_partial;
6106 return set_ref_in_item(&tv, copyID, NULL, NULL);
6107}
6108
6109/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006110 * Mark the dict "dd" with "copyID".
6111 * Also see set_ref_in_item().
6112 */
6113 static int
6114set_ref_in_item_dict(
6115 dict_T *dd,
6116 int copyID,
6117 ht_stack_T **ht_stack,
6118 list_stack_T **list_stack)
6119{
6120 if (dd == NULL || dd->dv_copyID == copyID)
6121 return FALSE;
6122
6123 // Didn't see this dict yet.
6124 dd->dv_copyID = copyID;
6125 if (ht_stack == NULL)
6126 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
6127
6128 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
6129 if (newitem == NULL)
6130 return TRUE;
6131
6132 newitem->ht = &dd->dv_hashtab;
6133 newitem->prev = *ht_stack;
6134 *ht_stack = newitem;
6135
6136 return FALSE;
6137}
6138
6139/*
6140 * Mark the list "ll" with "copyID".
6141 * Also see set_ref_in_item().
6142 */
6143 static int
6144set_ref_in_item_list(
6145 list_T *ll,
6146 int copyID,
6147 ht_stack_T **ht_stack,
6148 list_stack_T **list_stack)
6149{
6150 if (ll == NULL || ll->lv_copyID == copyID)
6151 return FALSE;
6152
6153 // Didn't see this list yet.
6154 ll->lv_copyID = copyID;
6155 if (list_stack == NULL)
6156 return set_ref_in_list_items(ll, copyID, ht_stack);
6157
6158 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
6159 if (newitem == NULL)
6160 return TRUE;
6161
6162 newitem->list = ll;
6163 newitem->prev = *list_stack;
6164 *list_stack = newitem;
6165
6166 return FALSE;
6167}
6168
6169/*
6170 * Mark the partial "pt" with "copyID".
6171 * Also see set_ref_in_item().
6172 */
6173 static int
6174set_ref_in_item_partial(
6175 partial_T *pt,
6176 int copyID,
6177 ht_stack_T **ht_stack,
6178 list_stack_T **list_stack)
6179{
6180 if (pt == NULL || pt->pt_copyID == copyID)
6181 return FALSE;
6182
6183 // Didn't see this partial yet.
6184 pt->pt_copyID = copyID;
6185
6186 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
6187
6188 if (pt->pt_dict != NULL)
6189 {
6190 typval_T dtv;
6191
6192 dtv.v_type = VAR_DICT;
6193 dtv.vval.v_dict = pt->pt_dict;
6194 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6195 }
6196
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02006197 if (pt->pt_obj != NULL)
6198 {
6199 typval_T objtv;
6200
6201 objtv.v_type = VAR_OBJECT;
6202 objtv.vval.v_object = pt->pt_obj;
6203 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
6204 }
6205
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006206 for (int i = 0; i < pt->pt_argc; ++i)
6207 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6208 ht_stack, list_stack);
6209 // pt_funcstack is handled in set_ref_in_funcstacks()
6210 // pt_loopvars is handled in set_ref_in_loopvars()
6211
6212 return abort;
6213}
6214
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006215#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006216/*
6217 * Mark the job "pt" with "copyID".
6218 * Also see set_ref_in_item().
6219 */
6220 static int
6221set_ref_in_item_job(
6222 job_T *job,
6223 int copyID,
6224 ht_stack_T **ht_stack,
6225 list_stack_T **list_stack)
6226{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006227 typval_T dtv;
6228
6229 if (job == NULL || job->jv_copyID == copyID)
6230 return FALSE;
6231
6232 job->jv_copyID = copyID;
6233 if (job->jv_channel != NULL)
6234 {
6235 dtv.v_type = VAR_CHANNEL;
6236 dtv.vval.v_channel = job->jv_channel;
6237 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6238 }
6239 if (job->jv_exit_cb.cb_partial != NULL)
6240 {
6241 dtv.v_type = VAR_PARTIAL;
6242 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
6243 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6244 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006245
6246 return FALSE;
6247}
6248
6249/*
6250 * Mark the channel "ch" with "copyID".
6251 * Also see set_ref_in_item().
6252 */
6253 static int
6254set_ref_in_item_channel(
6255 channel_T *ch,
6256 int copyID,
6257 ht_stack_T **ht_stack,
6258 list_stack_T **list_stack)
6259{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006260 typval_T dtv;
6261
6262 if (ch == NULL || ch->ch_copyID == copyID)
6263 return FALSE;
6264
6265 ch->ch_copyID = copyID;
6266 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
6267 {
6268 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
6269 jq != NULL; jq = jq->jq_next)
6270 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6271 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6272 cq = cq->cq_next)
6273 if (cq->cq_callback.cb_partial != NULL)
6274 {
6275 dtv.v_type = VAR_PARTIAL;
6276 dtv.vval.v_partial = cq->cq_callback.cb_partial;
6277 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6278 }
6279 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
6280 {
6281 dtv.v_type = VAR_PARTIAL;
6282 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
6283 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6284 }
6285 }
6286 if (ch->ch_callback.cb_partial != NULL)
6287 {
6288 dtv.v_type = VAR_PARTIAL;
6289 dtv.vval.v_partial = ch->ch_callback.cb_partial;
6290 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6291 }
6292 if (ch->ch_close_cb.cb_partial != NULL)
6293 {
6294 dtv.v_type = VAR_PARTIAL;
6295 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
6296 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6297 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006298
6299 return FALSE;
6300}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006301#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006302
6303/*
6304 * Mark the class "cl" with "copyID".
6305 * Also see set_ref_in_item().
6306 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006307 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006308set_ref_in_item_class(
6309 class_T *cl,
6310 int copyID,
6311 ht_stack_T **ht_stack,
6312 list_stack_T **list_stack)
6313{
6314 int abort = FALSE;
6315
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006316 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006317 return FALSE;
6318
6319 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02006320 if (cl->class_members_tv != NULL)
6321 {
6322 // The "class_members_tv" table is allocated only for regular classes
6323 // and not for interfaces.
6324 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
6325 abort = abort || set_ref_in_item(
6326 &cl->class_members_tv[i],
6327 copyID, ht_stack, list_stack);
6328 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006329
6330 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
6331 abort = abort || set_ref_in_func(NULL,
6332 cl->class_class_functions[i], copyID);
6333
6334 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
6335 abort = abort || set_ref_in_func(NULL,
6336 cl->class_obj_methods[i], copyID);
6337
6338 return abort;
6339}
6340
6341/*
6342 * Mark the object "cl" with "copyID".
6343 * Also see set_ref_in_item().
6344 */
6345 static int
6346set_ref_in_item_object(
6347 object_T *obj,
6348 int copyID,
6349 ht_stack_T **ht_stack,
6350 list_stack_T **list_stack)
6351{
6352 int abort = FALSE;
6353
6354 if (obj == NULL || obj->obj_copyID == copyID)
6355 return FALSE;
6356
6357 obj->obj_copyID = copyID;
6358
6359 // The typval_T array is right after the object_T.
6360 typval_T *mtv = (typval_T *)(obj + 1);
6361 for (int i = 0; !abort
6362 && i < obj->obj_class->class_obj_member_count; ++i)
6363 abort = abort || set_ref_in_item(mtv + i, copyID,
6364 ht_stack, list_stack);
6365
6366 return abort;
6367}
6368
6369/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006370 * Mark all lists, dicts and other container types referenced through typval
6371 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006372 * "list_stack" is used to add lists to be marked. Can be NULL.
6373 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6374 *
6375 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006376 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006377 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378set_ref_in_item(
6379 typval_T *tv,
6380 int copyID,
6381 ht_stack_T **ht_stack,
6382 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006383{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006384 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006385
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006386 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006387 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006388 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006389 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6390 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006391
6392 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006393 return set_ref_in_item_list(tv->vval.v_list, copyID,
6394 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006395
6396 case VAR_FUNC:
6397 {
6398 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6399 break;
6400 }
6401
6402 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006403 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6404 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006405
6406 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006407#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006408 return set_ref_in_item_job(tv->vval.v_job, copyID,
6409 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006410#else
6411 break;
6412#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006413
6414 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006415#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006416 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006417 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006418#else
6419 break;
6420#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006421
6422 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006423 return set_ref_in_item_class(tv->vval.v_class, copyID,
6424 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006425
6426 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006427 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006428 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006429
6430 case VAR_UNKNOWN:
6431 case VAR_ANY:
6432 case VAR_VOID:
6433 case VAR_BOOL:
6434 case VAR_SPECIAL:
6435 case VAR_NUMBER:
6436 case VAR_FLOAT:
6437 case VAR_STRING:
6438 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006439 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006440 case VAR_INSTR:
6441 // Types that do not contain any other item
6442 break;
6443 }
6444
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006445 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006446}
6447
Bram Moolenaar8c711452005-01-14 21:53:12 +00006448/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006449 * Return a string with the string representation of a variable.
6450 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006452 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006453 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006454 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006455 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006456 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6457 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006458 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006460 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006461echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006462 typval_T *tv,
6463 char_u **tofree,
6464 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006465 int copyID,
6466 int echo_style,
6467 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006468 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006470 static int recurse = 0;
6471 char_u *r = NULL;
6472
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006474 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006475 if (!did_echo_string_emsg)
6476 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006477 // Only give this message once for a recursive call to avoid
6478 // flooding the user with errors. And stop iterating over lists
6479 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006480 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006481 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006484 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 }
6486 ++recurse;
6487
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 switch (tv->v_type)
6489 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006490 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006491 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006492 {
6493 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006494 r = tv->vval.v_string;
6495 if (r == NULL)
6496 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006497 }
6498 else
6499 {
6500 *tofree = string_quote(tv->vval.v_string, FALSE);
6501 r = *tofree;
6502 }
6503 break;
6504
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006505 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006506 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006507 char_u buf[MAX_FUNC_NAME_LEN];
6508
6509 if (echo_style)
6510 {
LemonBoya5d35902022-04-29 21:15:02 +01006511 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6512 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006513 buf, MAX_FUNC_NAME_LEN);
6514 if (r == buf)
6515 {
6516 r = vim_strsave(buf);
6517 *tofree = r;
6518 }
6519 else
6520 *tofree = NULL;
6521 }
6522 else
6523 {
6524 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6525 : make_ufunc_name_readable(
6526 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6527 TRUE);
6528 r = *tofree;
6529 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006530 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006531 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006532
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006533 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006534 {
6535 partial_T *pt = tv->vval.v_partial;
6536 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006537 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006538 garray_T ga;
6539 int i;
6540 char_u *tf;
6541
6542 ga_init2(&ga, 1, 100);
6543 ga_concat(&ga, (char_u *)"function(");
6544 if (fname != NULL)
6545 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006546 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006547 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006548 && vim_isupper(fname[1]))
6549 {
6550 ga_concat(&ga, (char_u *)"'g:");
6551 ga_concat(&ga, fname + 1);
6552 }
6553 else
6554 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006555 vim_free(fname);
6556 }
6557 if (pt != NULL && pt->pt_argc > 0)
6558 {
6559 ga_concat(&ga, (char_u *)", [");
6560 for (i = 0; i < pt->pt_argc; ++i)
6561 {
6562 if (i > 0)
6563 ga_concat(&ga, (char_u *)", ");
6564 ga_concat(&ga,
6565 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6566 vim_free(tf);
6567 }
6568 ga_concat(&ga, (char_u *)"]");
6569 }
6570 if (pt != NULL && pt->pt_dict != NULL)
6571 {
6572 typval_T dtv;
6573
6574 ga_concat(&ga, (char_u *)", ");
6575 dtv.v_type = VAR_DICT;
6576 dtv.vval.v_dict = pt->pt_dict;
6577 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6578 vim_free(tf);
6579 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006580 // terminate with ')' and a NUL
6581 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006582
6583 *tofree = ga.ga_data;
6584 r = *tofree;
6585 break;
6586 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006587
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006588 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006589 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006590 break;
6591
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006593 if (tv->vval.v_list == NULL)
6594 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006595 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006596 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006597 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006598 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006599 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6600 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006601 {
6602 *tofree = NULL;
6603 r = (char_u *)"[...]";
6604 }
6605 else
6606 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006607 int old_copyID = tv->vval.v_list->lv_copyID;
6608
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006609 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006610 *tofree = list2string(tv, copyID, restore_copyID);
6611 if (restore_copyID)
6612 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006613 r = *tofree;
6614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006616
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006618 if (tv->vval.v_dict == NULL)
6619 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006620 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006621 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006622 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006623 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006624 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6625 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626 {
6627 *tofree = NULL;
6628 r = (char_u *)"{...}";
6629 }
6630 else
6631 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006632 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006633
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006634 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006635 *tofree = dict2string(tv, copyID, restore_copyID);
6636 if (restore_copyID)
6637 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006638 r = *tofree;
6639 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006640 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006641
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006642 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006643 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006644 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006646 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006647 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006648 break;
6649
Bram Moolenaar835dc632016-02-07 14:27:38 +01006650 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006651 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006652#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006653 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006654 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6655 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006656 if (composite_val)
6657 {
6658 *tofree = string_quote(r, FALSE);
6659 r = *tofree;
6660 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006661#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006663
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006664 case VAR_INSTR:
6665 *tofree = NULL;
6666 r = (char_u *)"instructions";
6667 break;
6668
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006669 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006670 {
6671 class_T *cl = tv->vval.v_class;
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006672 char *s = "class";
Yegappan Lakshmananda9d3452024-05-02 13:02:36 +02006673 if (cl != NULL && IS_INTERFACE(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006674 s = "interface";
Yegappan Lakshmananda9d3452024-05-02 13:02:36 +02006675 else if (cl != NULL && IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006676 s = "enum";
6677 size_t len = STRLEN(s) + 1 +
6678 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006679 r = *tofree = alloc(len);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006680 vim_snprintf((char *)r, len, "%s %s", s,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006681 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6682 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006683 break;
6684
6685 case VAR_OBJECT:
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006686 *tofree = r = object_string(tv->vval.v_object, numbuf, copyID,
6687 echo_style, restore_copyID,
6688 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006689 break;
6690
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006691 case VAR_FLOAT:
6692 *tofree = NULL;
6693 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6694 r = numbuf;
6695 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006696
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006697 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006698 case VAR_SPECIAL:
6699 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006700 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006701 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006702
6703 case VAR_TYPEALIAS:
6704 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6705 r = *tofree;
6706 if (r == NULL)
6707 r = (char_u *)"";
6708 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710
Bram Moolenaar8502c702014-06-17 12:51:16 +02006711 if (--recurse == 0)
6712 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006713 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714}
6715
6716/*
6717 * Return a string with the string representation of a variable.
6718 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6719 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006720 * Does not put quotes around strings, as ":echo" displays values.
6721 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6722 * May return NULL.
6723 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006724 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006725echo_string(
6726 typval_T *tv,
6727 char_u **tofree,
6728 char_u *numbuf,
6729 int copyID)
6730{
6731 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6732}
6733
6734/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006735 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6736 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006737 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006738 */
6739 int
6740buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6741{
6742 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006743 char_u *t;
6744 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006745
6746 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6747 return -1;
6748
6749 if (lnum > buf->b_ml.ml_line_count)
6750 lnum = buf->b_ml.ml_line_count;
6751
6752 str = ml_get_buf(buf, lnum, FALSE);
6753 if (str == NULL)
6754 return -1;
6755
6756 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006757 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006758
Bram Moolenaar91458462021-01-13 20:08:38 +01006759 // count the number of characters
6760 t = str;
6761 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6762 t += mb_ptr2len(t);
6763
6764 // In insert mode, when the cursor is at the end of a non-empty line,
6765 // byteidx points to the NUL character immediately past the end of the
6766 // string. In this case, add one to the character count.
6767 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6768 count++;
6769
6770 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006771}
6772
6773/*
6774 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006775 * byte index. Works only for loaded buffers. Returns -1 on failure.
6776 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006777 */
6778 int
6779buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6780{
6781 char_u *str;
6782 char_u *t;
6783
6784 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6785 return -1;
6786
6787 if (lnum > buf->b_ml.ml_line_count)
6788 lnum = buf->b_ml.ml_line_count;
6789
6790 str = ml_get_buf(buf, lnum, FALSE);
6791 if (str == NULL)
6792 return -1;
6793
6794 // Convert the character offset to a byte offset
6795 t = str;
6796 while (*t != NUL && --charidx > 0)
6797 t += mb_ptr2len(t);
6798
Bram Moolenaar91458462021-01-13 20:08:38 +01006799 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006800}
6801
6802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006804 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006806 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006807var2fpos(
6808 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006809 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006810 int *fnum, // set to fnum for '0, 'A, etc.
6811 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006813 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006815 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006817 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006818 if (varp->v_type == VAR_LIST)
6819 {
6820 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006821 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006822 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006823 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006824
6825 l = varp->vval.v_list;
6826 if (l == NULL)
6827 return NULL;
6828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006829 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006830 pos.lnum = list_find_nr(l, 0L, &error);
6831 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006832 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006833 if (charcol)
6834 len = (long)mb_charlen(ml_get(pos.lnum));
6835 else
John Marriottbfcc8952024-03-11 22:04:45 +01006836 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006837
Bram Moolenaarec65d772020-08-20 22:29:12 +02006838 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006839 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006840 li = list_find(l, 1L);
6841 if (li != NULL && li->li_tv.v_type == VAR_STRING
6842 && li->li_tv.vval.v_string != NULL
6843 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006844 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006845 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006846 }
6847 else
6848 {
6849 pos.col = list_find_nr(l, 1L, &error);
6850 if (error)
6851 return NULL;
6852 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006854 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006855 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006856 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006857 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006859 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006860 pos.coladd = list_find_nr(l, 2L, &error);
6861 if (error)
6862 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006863
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006864 return &pos;
6865 }
6866
Bram Moolenaarc5809432021-03-27 21:23:30 +01006867 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6868 return NULL;
6869
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006870 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006871 if (name == NULL)
6872 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006873
6874 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006875 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006876 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006877 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006878 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006879 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006880 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006881 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006882 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006883 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006884 pos = VIsual;
6885 else
6886 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006887 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006888 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006889 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006891 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006892 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6894 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006895 pos = *pp;
6896 }
6897 if (pos.lnum != 0)
6898 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006899 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006900 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6901 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006903
Bram Moolenaara5525202006-03-02 22:52:09 +00006904 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006905
Bram Moolenaar477933c2007-07-17 14:32:23 +00006906 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006907 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006908 // the "w_valid" flags are not reset when moving the cursor, but they
6909 // do matter for update_topline() and validate_botline().
6910 check_cursor_moved(curwin);
6911
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006912 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006913 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006914 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006915 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006916 // In silent Ex mode topline is zero, but that's not a valid line
6917 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006918 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006919 return &pos;
6920 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006921 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006922 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006923 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006924 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006925 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006926 return &pos;
6927 }
6928 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006929 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006931 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {
6933 pos.lnum = curbuf->b_ml.ml_line_count;
6934 pos.col = 0;
6935 }
6936 else
6937 {
6938 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006939 if (charcol)
6940 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6941 else
John Marriottbfcc8952024-03-11 22:04:45 +01006942 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944 return &pos;
6945 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006946 if (in_vim9script())
6947 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 return NULL;
6949}
6950
6951/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006952 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006953 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006954 * Note that the column is passed on as-is, the caller may want to decrement
6955 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006956 * If "charcol" is TRUE use the column as the character index instead of the
6957 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006958 * Return FAIL when conversion is not possible, doesn't check the position for
6959 * validity.
6960 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006962list2fpos(
6963 typval_T *arg,
6964 pos_T *posp,
6965 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006966 colnr_T *curswantp,
6967 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006968{
6969 list_T *l = arg->vval.v_list;
6970 long i = 0;
6971 long n;
6972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006973 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6974 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006975 if (arg->v_type != VAR_LIST
6976 || l == NULL
6977 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006978 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006979 return FAIL;
6980
6981 if (fnump != NULL)
6982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006983 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006984 if (n < 0)
6985 return FAIL;
6986 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006987 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006988 *fnump = n;
6989 }
6990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006991 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006992 if (n < 0)
6993 return FAIL;
6994 posp->lnum = n;
6995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006996 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006997 if (n < 0)
6998 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006999 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01007000 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007001 if (charcol)
7002 {
7003 buf_T *buf;
7004
7005 // Get the text for the specified line in a loaded buffer
7006 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
7007 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
7008 return FAIL;
7009
Bram Moolenaar79f23442022-10-10 12:42:57 +01007010 n = buf_charidx_to_byteidx(buf,
7011 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007012 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007013 posp->col = n;
7014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007015 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007016 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00007017 posp->coladd = 0;
7018 else
7019 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007020
Bram Moolenaar493c1782014-05-28 14:34:46 +02007021 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007022 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02007023
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007024 return OK;
7025}
7026
7027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 * Get the length of an environment variable name.
7029 * Advance "arg" to the first character after the name.
7030 * Return 0 for error.
7031 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007032 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007033get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 char_u *p;
7036 int len;
7037
7038 for (p = *arg; vim_isIDc(*p); ++p)
7039 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007040 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 return 0;
7042
7043 len = (int)(p - *arg);
7044 *arg = p;
7045 return len;
7046}
7047
7048/*
7049 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007050 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 * Return 0 if something is wrong.
7052 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007054get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 char_u *p;
7057 int len;
7058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007059 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007061 {
7062 if (*p == ':')
7063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007064 // "s:" is start of "s:var", but "n:" is not and can be used in
7065 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007066 len = (int)(p - *arg);
7067 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
7068 || len > 1)
7069 break;
7070 }
7071 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007072 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 return 0;
7074
7075 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007076 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
7078 return len;
7079}
7080
7081/*
Bram Moolenaara7043832005-01-21 11:56:39 +00007082 * Get the length of the name of a variable or function.
7083 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007085 * Return -1 if curly braces expansion failed.
7086 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 * If the name contains 'magic' {}'s, expand them and return the
7088 * expanded name in an allocated string via 'alias' - caller must free.
7089 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007090 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007091get_name_len(
7092 char_u **arg,
7093 char_u **alias,
7094 int evaluate,
7095 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 char_u *p;
7099 char_u *expr_start;
7100 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007102 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
7104 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
7105 && (*arg)[2] == (int)KE_SNR)
7106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007107 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 *arg += 3;
7109 return get_id_len(arg) + 3;
7110 }
7111 len = eval_fname_script(*arg);
7112 if (len > 0)
7113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007114 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 *arg += len;
7116 }
7117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007119 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007121 p = find_name_end(*arg, &expr_start, &expr_end,
7122 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 if (expr_start != NULL)
7124 {
7125 char_u *temp_string;
7126
7127 if (!evaluate)
7128 {
7129 len += (int)(p - *arg);
7130 *arg = skipwhite(p);
7131 return len;
7132 }
7133
7134 /*
7135 * Include any <SID> etc in the expanded string:
7136 * Thus the -len here.
7137 */
7138 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
7139 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007140 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 *alias = temp_string;
7142 *arg = skipwhite(p);
7143 return (int)STRLEN(temp_string);
7144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01007147 // Only give an error when there is something, otherwise it will be
7148 // reported at a higher level.
7149 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007150 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
7152 return len;
7153}
7154
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007155/*
7156 * Find the end of a variable or function name, taking care of magic braces.
7157 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
7158 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007159 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007160 * Return a pointer to just after the name. Equal to "arg" if there is no
7161 * valid name.
7162 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007163 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007164find_name_end(
7165 char_u *arg,
7166 char_u **expr_start,
7167 char_u **expr_end,
7168 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007170 int mb_nest = 0;
7171 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007173 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02007174 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007176 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007178 *expr_start = NULL;
7179 *expr_end = NULL;
7180 }
7181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007182 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007183 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007184 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007185 return arg;
7186
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007187 for (p = arg; *p != NUL
7188 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007189 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02007190 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007191 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007192 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007193 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007194 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00007195 if (*p == '\'')
7196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007197 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007198 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007199 ;
7200 if (*p == NUL)
7201 break;
7202 }
7203 else if (*p == '"')
7204 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007205 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007206 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007207 if (*p == '\\' && p[1] != NUL)
7208 ++p;
7209 if (*p == NUL)
7210 break;
7211 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007212 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
7213 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007214 // "s:" is start of "s:var", but "n:" is not and can be used in
7215 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007216 len = (int)(p - arg);
7217 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007218 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007219 break;
7220 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007222 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007224 if (*p == '[')
7225 ++br_nest;
7226 else if (*p == ']')
7227 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007229
zeertzjqa93d9cd2023-05-02 16:25:47 +01007230 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007232 if (*p == '{')
7233 {
7234 mb_nest++;
7235 if (expr_start != NULL && *expr_start == NULL)
7236 *expr_start = p;
7237 }
7238 else if (*p == '}')
7239 {
7240 mb_nest--;
7241 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
7242 *expr_end = p;
7243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 }
7246
7247 return p;
7248}
7249
7250/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007251 * Expands out the 'magic' {}'s in a variable/function name.
7252 * Note that this can call itself recursively, to deal with
7253 * constructs like foo{bar}{baz}{bam}
7254 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7255 * "in_start" ^
7256 * "expr_start" ^
7257 * "expr_end" ^
7258 * "in_end" ^
7259 *
7260 * Returns a new allocated string, which the caller must free.
7261 * Returns NULL for failure.
7262 */
7263 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007264make_expanded_name(
7265 char_u *in_start,
7266 char_u *expr_start,
7267 char_u *expr_end,
7268 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007269{
7270 char_u c1;
7271 char_u *retval = NULL;
7272 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007273
7274 if (expr_end == NULL || in_end == NULL)
7275 return NULL;
7276 *expr_start = NUL;
7277 *expr_end = NUL;
7278 c1 = *in_end;
7279 *in_end = NUL;
7280
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007281 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007282 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007283 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007284 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7285 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007286 if (retval != NULL)
7287 {
7288 STRCPY(retval, in_start);
7289 STRCAT(retval, temp_result);
7290 STRCAT(retval, expr_end + 1);
7291 }
7292 }
7293 vim_free(temp_result);
7294
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007295 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007296 *expr_start = '{';
7297 *expr_end = '}';
7298
7299 if (retval != NULL)
7300 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007301 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007302 if (expr_start != NULL)
7303 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007304 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007305 temp_result = make_expanded_name(retval, expr_start,
7306 expr_end, temp_result);
7307 vim_free(retval);
7308 retval = temp_result;
7309 }
7310 }
7311
7312 return retval;
7313}
7314
7315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007320eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007322 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007323}
7324
7325/*
7326 * Return TRUE if character "c" can be used as the first character in a
7327 * variable or function name (excluding '{' and '}').
7328 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007330eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007331{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007332 return ASCII_ISALPHA(c) || c == '_';
7333}
7334
7335/*
7336 * Return TRUE if character "c" can be used as the first character of a
7337 * dictionary key.
7338 */
7339 int
7340eval_isdictc(int c)
7341{
7342 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343}
7344
7345/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007346 * Handle:
7347 * - expr[expr], expr[expr:expr] subscript
7348 * - ".name" lookup
7349 * - function call with Funcref variable: func(expr)
7350 * - method call: var->method()
7351 *
7352 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007353 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007354 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007356handle_subscript(
7357 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007358 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007359 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007360 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007361 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007362{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007363 int evaluate = evalarg != NULL
7364 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007365 int ret = OK;
7366 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007367 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007368 int getnext;
7369 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007370
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007371 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007372 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007373 // When at the end of the line and ".name" or "->{" or "->X" follows in
7374 // the next line then consume the line break.
7375 p = eval_next_non_blank(*arg, evalarg, &getnext);
7376 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007377 && ((*p == '.'
7378 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7379 || rettv->v_type == VAR_CLASS
7380 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007381 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7382 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7383 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007384 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007385 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007386 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007387 check_white = FALSE;
7388 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007389
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007390 if (rettv->v_type == VAR_ANY)
7391 {
7392 char_u *exp_name;
7393 int cc;
7394 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007395 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007396 type_T *type;
7397
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007398 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007399 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007400 if (**arg != '.')
7401 {
7402 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007403 semsg(_(e_expected_dot_after_name_str),
7404 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007405 ret = FAIL;
7406 break;
7407 }
7408 ++*arg;
7409 if (IS_WHITE_OR_NUL(**arg))
7410 {
7411 if (verbose)
7412 emsg(_(e_no_white_space_allowed_after_dot));
7413 ret = FAIL;
7414 break;
7415 }
7416
7417 // isolate the name
7418 exp_name = *arg;
7419 while (eval_isnamec(**arg))
7420 ++*arg;
7421 cc = **arg;
7422 **arg = NUL;
7423
7424 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007425 evalarg == NULL ? NULL : evalarg->eval_cctx,
7426 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007427 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007428
7429 if (idx < 0 && ufunc == NULL)
7430 {
7431 ret = FAIL;
7432 break;
7433 }
7434 if (idx >= 0)
7435 {
7436 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7437 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7438
7439 copy_tv(sv->sv_tv, rettv);
7440 }
7441 else
7442 {
7443 rettv->v_type = VAR_FUNC;
7444 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7445 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007446 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007447 }
7448
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007449 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7450 || rettv->v_type == VAR_PARTIAL))
7451 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007452 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007453 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7454 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007455
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007456 // Stop the expression evaluation when immediately aborting on
7457 // error, or when an interrupt occurred or an exception was thrown
7458 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007459 if (aborting())
7460 {
7461 if (ret == OK)
7462 clear_tv(rettv);
7463 ret = FAIL;
7464 }
7465 dict_unref(selfdict);
7466 selfdict = NULL;
7467 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007468 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007469 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007470 if (in_vim9script())
7471 *arg = skipwhite(p + 2);
7472 else
7473 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007474 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007475 {
zeertzjqc481ad32023-03-11 16:18:51 +00007476 emsg(_(e_no_white_space_allowed_before_parenthesis));
7477 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007478 }
zeertzjqc481ad32023-03-11 16:18:51 +00007479 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7480 // expr->{lambda}() or expr->(lambda)()
7481 ret = eval_lambda(arg, rettv, evalarg, verbose);
7482 else
7483 // expr->name()
7484 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007485 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007486 // "." is ".name" lookup when we found a dict or when evaluating and
7487 // scriptversion is at least 2, where string concatenation is "..".
7488 else if (**arg == '['
7489 || (**arg == '.' && (rettv->v_type == VAR_DICT
7490 || (!evaluate
7491 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007492 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007493 {
7494 dict_unref(selfdict);
7495 if (rettv->v_type == VAR_DICT)
7496 {
7497 selfdict = rettv->vval.v_dict;
7498 if (selfdict != NULL)
7499 ++selfdict->dv_refcount;
7500 }
7501 else
7502 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007503 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007504 {
7505 clear_tv(rettv);
7506 ret = FAIL;
7507 }
7508 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007509 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7510 || rettv->v_type == VAR_OBJECT))
7511 {
7512 // class member: SomeClass.varname
7513 // class method: SomeClass.SomeMethod()
7514 // class constructor: SomeClass.new()
7515 // object member: someObject.varname
7516 // object method: someObject.SomeMethod()
7517 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7518 {
7519 clear_tv(rettv);
7520 ret = FAIL;
7521 }
7522 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007523 else
7524 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007525 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007527 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7528 // Don't do this when "Func" is already a partial that was bound
7529 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007530 if (selfdict != NULL
7531 && (rettv->v_type == VAR_FUNC
7532 || (rettv->v_type == VAR_PARTIAL
7533 && (rettv->vval.v_partial->pt_auto
7534 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007535 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007536
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007537 dict_unref(selfdict);
7538 return ret;
7539}
7540
7541/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007542 * Make a copy of an item.
7543 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007544 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007545 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7546 * reference to an already copied list/dict can be used.
7547 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007548 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007549 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007550item_copy(
7551 typval_T *from,
7552 typval_T *to,
7553 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007554 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007555 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007556{
7557 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007558 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007559
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007562 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007563 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007564 }
7565 ++recurse;
7566
7567 switch (from->v_type)
7568 {
7569 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007570 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 case VAR_STRING:
7572 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007573 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007574 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007575 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007576 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007577 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007578 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007579 case VAR_CLASS:
7580 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007581 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007582 copy_tv(from, to);
7583 break;
7584 case VAR_LIST:
7585 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007586 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007587 if (from->vval.v_list == NULL)
7588 to->vval.v_list = NULL;
7589 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7590 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007591 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007592 to->vval.v_list = from->vval.v_list->lv_copylist;
7593 ++to->vval.v_list->lv_refcount;
7594 }
7595 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007596 to->vval.v_list = list_copy(from->vval.v_list,
7597 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007598 if (to->vval.v_list == NULL)
7599 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007600 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007601 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007603 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007604 case VAR_DICT:
7605 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007606 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007607 if (from->vval.v_dict == NULL)
7608 to->vval.v_dict = NULL;
7609 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007611 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007612 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7613 ++to->vval.v_dict->dv_refcount;
7614 }
7615 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007616 to->vval.v_dict = dict_copy(from->vval.v_dict,
7617 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007618 if (to->vval.v_dict == NULL)
7619 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007621 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007622 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007624 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007625 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007626 }
7627 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007628 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007629}
7630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 void
7632echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7633{
7634 char_u *tofree;
7635 char_u numbuf[NUMBUFLEN];
7636 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7637
7638 if (*atstart)
7639 {
7640 *atstart = FALSE;
7641 // Call msg_start() after eval1(), evaluating the expression
7642 // may cause a message to appear.
7643 if (with_space)
7644 {
7645 // Mark the saved text as finishing the line, so that what
7646 // follows is displayed on a new line when scrolling back
7647 // at the more prompt.
7648 msg_sb_eol();
7649 msg_start();
7650 }
7651 }
7652 else if (with_space)
7653 msg_puts_attr(" ", echo_attr);
7654
7655 if (p != NULL)
7656 for ( ; *p != NUL && !got_int; ++p)
7657 {
7658 if (*p == '\n' || *p == '\r' || *p == TAB)
7659 {
7660 if (*p != TAB && *needclr)
7661 {
7662 // remove any text still there from the command
7663 msg_clr_eos();
7664 *needclr = FALSE;
7665 }
7666 msg_putchar_attr(*p, echo_attr);
7667 }
7668 else
7669 {
7670 if (has_mbyte)
7671 {
7672 int i = (*mb_ptr2len)(p);
7673
7674 (void)msg_outtrans_len_attr(p, i, echo_attr);
7675 p += i - 1;
7676 }
7677 else
7678 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7679 }
7680 }
7681 vim_free(tofree);
7682}
7683
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 * ":echo expr1 ..." print each argument separated with a space, add a
7686 * newline at the end.
7687 * ":echon expr1 ..." print each argument plain.
7688 */
7689 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007690ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691{
7692 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007694 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 int needclr = TRUE;
7696 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007697 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007698 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007699 evalarg_T evalarg;
7700
Bram Moolenaare707c882020-07-01 13:07:37 +02007701 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702
7703 if (eap->skip)
7704 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007705 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007707 // If eval1() causes an error message the text from the command may
7708 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007709 need_clr_eos = needclr;
7710
Bram Moolenaar68db9962021-05-09 23:19:22 +02007711 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007712 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {
7714 /*
7715 * Report the invalid expression unless the expression evaluation
7716 * has been cancelled due to an aborting error, an interrupt, or an
7717 * exception.
7718 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007719 if (!aborting() && did_emsg == did_emsg_before
7720 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007721 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007722 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 break;
7724 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007725 need_clr_eos = FALSE;
7726
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007728 {
7729 if (rettv.v_type == VAR_VOID)
7730 {
7731 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7732 break;
7733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007735 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007737 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 arg = skipwhite(arg);
7739 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007740 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007741 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742
7743 if (eap->skip)
7744 --emsg_skip;
7745 else
7746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007747 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (needclr)
7749 msg_clr_eos();
7750 if (eap->cmdidx == CMD_echo)
7751 msg_end();
7752 }
7753}
7754
7755/*
7756 * ":echohl {name}".
7757 */
7758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007759ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007761 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762}
7763
7764/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007765 * Returns the :echo attribute
7766 */
7767 int
7768get_echo_attr(void)
7769{
7770 return echo_attr;
7771}
7772
7773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 * ":execute expr1 ..." execute the result of an expression.
7775 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007776 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007778 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 * Each gets spaces around each argument and a newline at the end for
7780 * echo commands
7781 */
7782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007783ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 int ret = OK;
7788 char_u *p;
7789 garray_T ga;
7790 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007791 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
7793 ga_init2(&ga, 1, 80);
7794
7795 if (eap->skip)
7796 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007797 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007799 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007800 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802
7803 if (!eap->skip)
7804 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007805 char_u buf[NUMBUFLEN];
7806
7807 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007808 {
7809 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7810 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007811 semsg(_(e_using_invalid_value_as_string_str),
7812 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007813 p = NULL;
7814 }
7815 else
7816 p = tv_get_string_buf(&rettv, buf);
7817 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007818 else
7819 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007820 if (p == NULL)
7821 {
7822 clear_tv(&rettv);
7823 ret = FAIL;
7824 break;
7825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 len = (int)STRLEN(p);
7827 if (ga_grow(&ga, len + 2) == FAIL)
7828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007829 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 ret = FAIL;
7831 break;
7832 }
7833 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 ga.ga_len += len;
7837 }
7838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 arg = skipwhite(arg);
7841 }
7842
7843 if (ret != FAIL && ga.ga_data != NULL)
7844 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007845 // use the first line of continuation lines for messages
7846 SOURCING_LNUM = start_lnum;
7847
Bram Moolenaar37fef162022-08-29 18:16:32 +01007848 if (eap->cmdidx == CMD_echomsg
7849 || eap->cmdidx == CMD_echowindow
7850 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007852 // Mark the already saved text as finishing the line, so that what
7853 // follows is displayed on a new line when scrolling back at the
7854 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007855 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007856 }
7857
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007858 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007859 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007860 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007861 out_flush();
7862 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007863 else if (eap->cmdidx == CMD_echowindow)
7864 {
7865#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007866 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007867#endif
7868 msg_attr(ga.ga_data, echo_attr);
7869#ifdef HAS_MESSAGE_WINDOW
7870 end_echowindow();
7871#endif
7872 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007873 else if (eap->cmdidx == CMD_echoconsole)
7874 {
7875 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7876 ui_write((char_u *)"\r\n", 2, TRUE);
7877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 else if (eap->cmdidx == CMD_echoerr)
7879 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007880 int save_did_emsg = did_emsg;
7881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007882 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007883 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 if (!force_abort)
7885 did_emsg = save_did_emsg;
7886 }
7887 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007888 {
7889 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7890
7891 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7892 sticky_cmdmod_flags = cmdmod.cmod_flags
7893 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007895 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007896 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 }
7899
7900 ga_clear(&ga);
7901
7902 if (eap->skip)
7903 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007904 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905}
7906
7907/*
7908 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7909 * "arg" points to the "&" or '+' when called, to "option" when returning.
7910 * Returns NULL when no option name found. Otherwise pointer to the char
7911 * after the option name.
7912 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007913 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007914find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 char_u *p = *arg;
7917
7918 ++p;
7919 if (*p == 'g' && p[1] == ':')
7920 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007921 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 p += 2;
7923 }
7924 else if (*p == 'l' && p[1] == ':')
7925 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007926 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 p += 2;
7928 }
7929 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007930 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
7932 if (!ASCII_ISALPHA(*p))
7933 return NULL;
7934 *arg = p;
7935
7936 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007937 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 else
7939 while (ASCII_ISALPHA(*p))
7940 ++p;
7941 return p;
7942}
7943
7944/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007945 * Display script name where an item was last set.
7946 * Should only be invoked when 'verbose' is non-zero.
7947 */
7948 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007949last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007950{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007951 char_u *p;
7952
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007953 if (script_ctx.sc_sid == 0)
7954 return;
7955
7956 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7957 if (p == NULL)
7958 return;
7959
7960 verbose_enter();
7961 msg_puts(_("\n\tLast set from "));
7962 msg_puts((char *)p);
7963 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007964 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007965 msg_puts(_(line_msg));
7966 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007967 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007968 verbose_leave();
7969 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007970}
7971
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007972#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
7974/*
7975 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007976 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 * "flags" can be "g" to do a global substitute.
7978 * Returns an allocated string, NULL for error.
7979 */
7980 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007981do_string_sub(
7982 char_u *str,
7983 char_u *pat,
7984 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007985 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007986 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 int sublen;
7989 regmatch_T regmatch;
7990 int i;
7991 int do_all;
7992 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007993 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 garray_T ga;
7995 char_u *ret;
7996 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007997 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007999 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008001 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002
8003 ga_init2(&ga, 1, 200);
8004
8005 do_all = (flags[0] == 'g');
8006
8007 regmatch.rm_ic = p_ic;
8008 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8009 if (regmatch.regprog != NULL)
8010 {
8011 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01008012 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
8014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008015 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01008016 if (regmatch.startp[0] == regmatch.endp[0])
8017 {
8018 if (zero_width == regmatch.startp[0])
8019 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008020 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02008021 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02008022 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
8023 (size_t)i);
8024 ga.ga_len += i;
8025 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01008026 continue;
8027 }
8028 zero_width = regmatch.startp[0];
8029 }
8030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 /*
8032 * Get some space for a temporary buffer to do the substitution
8033 * into. It will contain:
8034 * - The text up to where the match is.
8035 * - The substituted text.
8036 * - The text after the match.
8037 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01008038 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00008039 if (sublen <= 0)
8040 {
8041 ga_clear(&ga);
8042 break;
8043 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01008044 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
8046 {
8047 ga_clear(&ga);
8048 break;
8049 }
8050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008051 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 i = (int)(regmatch.startp[0] - tail);
8053 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008054 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01008055 (void)vim_regsub(&regmatch, sub, expr,
8056 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
8057 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02008059 tail = regmatch.endp[0];
8060 if (*tail == NUL)
8061 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 if (!do_all)
8063 break;
8064 }
8065
8066 if (ga.ga_data != NULL)
8067 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
8068
Bram Moolenaar473de612013-06-08 18:19:48 +02008069 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 }
8071
8072 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
8073 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008074 if (p_cpo == empty_option)
8075 p_cpo = save_cpo;
8076 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008077 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008078 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008079 // If it's still empty it was changed and restored, need to restore in
8080 // the complicated way.
8081 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008082 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008083 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085
8086 return ret;
8087}