blob: 0579a85f69e976ec3aa742404bb9e984f642c34c [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;
136 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000138 evalarg->eval_getline = eap->getline;
139 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/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Evaluate an expression, which can be a function, partial or string.
254 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200255 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100256 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 * Return the result in "rettv" and OK or FAIL.
258 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200259 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100260eval_expr_typval(
261 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200262 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100263 typval_T *argv,
264 int argc,
265 funccall_T *fc_arg,
266 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267{
268 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100271
zeertzjqad0c4422023-08-17 22:15:47 +0200272 if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100282 funccall_T *fc = fc_arg != NULL ? fc_arg
283 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100284 int r;
285
286 if (fc == NULL)
287 return FAIL;
288
Bram Moolenaar69082912022-09-22 21:35:19 +0100289 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100290 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100292 if (fc_arg == NULL)
293 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100294 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000303 funcexe.fe_evaluate = TRUE;
304 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200309 else if (expr->v_type == VAR_INSTR)
310 {
311 return exe_typval_instr(expr, rettv);
312 }
zeertzjqad0c4422023-08-17 22:15:47 +0200313 else if (expr->v_type == VAR_FUNC || want_func)
314 {
315 s = expr->v_type == VAR_FUNC
316 ? expr->vval.v_string
317 : tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
318 if (s == NULL || *s == NUL)
319 return FAIL;
320 CLEAR_FIELD(funcexe);
321 funcexe.fe_evaluate = TRUE;
322 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
323 return FAIL;
324 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 else
326 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000327 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 if (s == NULL)
329 return FAIL;
330 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200331 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200333 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100334 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200335 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200336 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 return FAIL;
338 }
339 }
340 return OK;
341}
342
343/*
344 * Like eval_to_bool() but using a typval_T instead of a string.
345 * Works for string, funcref and partial.
346 */
347 int
348eval_expr_to_bool(typval_T *expr, int *error)
349{
350 typval_T rettv;
351 int res;
352
zeertzjqad0c4422023-08-17 22:15:47 +0200353 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 {
355 *error = TRUE;
356 return FALSE;
357 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200358 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100359 clear_tv(&rettv);
360 return res;
361}
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Top level evaluation function, returning a string. If "skip" is TRUE,
365 * only parsing to "nextcmd" is done, without reporting errors. Return
366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
367 */
368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100369eval_to_string_skip(
370 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200371 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100372 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar33570922005-01-25 22:26:29 +0000374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar37c83712020-06-30 21:18:36 +0200378 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (skip)
380 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 retval = NULL;
383 else
384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 if (skip)
389 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200390 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 return retval;
393}
394
395/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100396 * Initialize "evalarg" for use.
397 */
398 void
399init_evalarg(evalarg_T *evalarg)
400{
401 CLEAR_POINTER(evalarg);
402 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
403}
404
405/*
406 * If "evalarg->eval_tofree" is not NULL free it later.
407 * Caller is expected to overwrite "evalarg->eval_tofree" next.
408 */
409 static void
410free_eval_tofree_later(evalarg_T *evalarg)
411{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000412 if (evalarg->eval_tofree == NULL)
413 return;
414
415 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
416 ((char_u **)evalarg->eval_tofree_ga.ga_data)
417 [evalarg->eval_tofree_ga.ga_len++]
418 = evalarg->eval_tofree;
419 else
420 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100421}
422
423/*
424 * After using "evalarg" filled from "eap": free the memory.
425 */
426 void
427clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
428{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000429 if (evalarg == NULL)
430 return;
431
432 garray_T *etga = &evalarg->eval_tofree_ga;
433
434 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000436 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100437 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000438 // We may need to keep the original command line, e.g. for
439 // ":let" it has the variable names. But we may also need
440 // the new one, "nextcmd" points into it. Keep both.
441 vim_free(eap->cmdline_tofree);
442 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000444 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
445 {
446 // "nextcmd" points into the last line in eval_tofree_ga,
447 // need to keep it around.
448 --etga->ga_len;
449 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
450 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100451 }
452 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000453 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455 else
456 vim_free(evalarg->eval_tofree);
457 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000459
460 ga_clear_strings(etga);
461 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100462}
463
464/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465 * Skip over an expression at "*pp".
466 * Return FAIL for an error, OK otherwise.
467 */
468 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470{
Bram Moolenaar33570922005-01-25 22:26:29 +0000471 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000472
473 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200474 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000475}
476
477/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200478 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * If in Vim9 script and line breaks are encountered, the lines are
480 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200481 * "arg" is advanced to just after the expression.
482 * "start" is set to the start of the expression, "end" to just after the end.
483 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 * Return FAIL for an error, OK otherwise.
485 */
486 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487skip_expr_concatenate(
488 char_u **arg,
489 char_u **start,
490 char_u **end,
491 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492{
493 typval_T rettv;
494 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200495 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200496 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200497 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200498 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200499 int evaluate = evalarg == NULL
500 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200502 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 {
505 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200510 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512
513 // Don't evaluate the expression.
514 if (evalarg != NULL)
515 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 *arg = skipwhite(*arg);
517 res = eval1(arg, &rettv, evalarg);
518 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 if (evalarg != NULL)
520 evalarg->eval_flags = save_flags;
521
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200522 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 if (evalarg->eval_ga.ga_len == 1)
526 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200527 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528 ga_clear(gap);
529 gap->ga_itemsize = 0;
530 }
531 else
532 {
533 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200536 // Line breaks encountered, concatenate all the lines.
537 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200538 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539
540 // free the lines only when using getsourceline()
541 if (evalarg->eval_cookie != NULL)
542 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200545 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100546 // later. Also free "eval_tofree" later if needed.
547 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 evalarg->eval_tofree =
549 ((char_u **)gap->ga_data)[gap->ga_len - 1];
550 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear_strings(gap);
552 }
553 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200555 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200556
557 // free lines that were explicitly marked for freeing
558 ga_clear_strings(freegap);
559 }
560
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200561 gap->ga_itemsize = 0;
562 if (p == NULL)
563 return FAIL;
564 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200565 vim_free(evalarg->eval_tofree_lambda);
566 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200567 // Compute "end" relative to the end.
568 *end = *start + STRLEN(*start) - endoff;
569 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200570 }
571
572 return res;
573}
574
575/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100576 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100577 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100578 * Returns an allocated string (NULL when out of memory).
579 */
580 char_u *
581typval2string(typval_T *tv, int convert)
582{
583 garray_T ga;
584 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585
586 if (convert && tv->v_type == VAR_LIST)
587 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000588 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100589 if (tv->vval.v_list != NULL)
590 {
591 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
592 if (tv->vval.v_list->lv_len > 0)
593 ga_append(&ga, NL);
594 }
595 ga_append(&ga, NUL);
596 retval = (char_u *)ga.ga_data;
597 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100598 else
599 retval = vim_strsave(tv_get_string(tv));
600 return retval;
601}
602
603/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 * Top level evaluation function, returning a string. Does not handle line
605 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100606 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * Return pointer to allocated memory, or NULL for failure.
608 */
609 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100610eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100611 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100612 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100613 exarg_T *eap,
614 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100619 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 if (use_simple_function)
623 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
624 else
625 r = eval0(arg, &tv, NULL, &evalarg);
626 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 retval = NULL;
628 else
629 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100633 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 return retval;
636}
637
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100638 char_u *
639eval_to_string(
640 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100641 int convert,
642 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100643{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000648 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100649 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200650 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653eval_to_string_safe(
654 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000655 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100656 int keep_script_version,
657 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200660 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200661 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200662 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaar9530b582022-01-22 13:39:08 +0000664 if (!keep_script_version)
665 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200666 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000667 if (use_sandbox)
668 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100669 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200670 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100671 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 if (use_sandbox)
673 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100674 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200675 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200676 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200677 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return retval;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Top level evaluation function, returning a number.
683 * Evaluates "expr" silently.
684 * Returns -1 for an error.
685 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200686 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100687eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar33570922005-01-25 22:26:29 +0000689 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000691 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100692 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 ++emsg_off;
695
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 if (use_simple_function)
697 r = may_call_simple_func(expr, &rettv);
698 if (r == NOTDONE)
699 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
700 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 retval = -1;
702 else
703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100704 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 --emsg_off;
708
709 return retval;
710}
711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000713 * Top level evaluation function.
714 * Returns an allocated typval_T with the result.
715 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716 */
717 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200718eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 return eval_expr_ext(arg, eap, FALSE);
721}
722
723 typval_T *
724eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
725{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200727 evalarg_T evalarg;
728
729 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200731 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100732 if (tv != NULL)
733 {
734 int r = NOTDONE;
735
736 if (use_simple_function)
737 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
738 if (r == NOTDONE)
739 r = eval0(arg, tv, eap, &evalarg);
740
741 if (r == FAIL)
742 VIM_CLEAR(tv);
743 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000744
Bram Moolenaar37c83712020-06-30 21:18:36 +0200745 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000746 return tv;
747}
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000750 * "*arg" points to what can be a function name in the form of "import.Name" or
751 * "Funcref". Return the name of the function. Set "tofree" to something that
752 * was allocated.
753 * If "verbose" is FALSE no errors are given.
754 * Return NULL for any failure.
755 */
756 static char_u *
757deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000758 char_u **arg,
759 char_u **tofree,
760 evalarg_T *evalarg,
761 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000762{
763 typval_T ref;
764 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100765 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766
767 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100768 if (evalarg != NULL)
769 {
770 // need to evaluate this to get an import, like in "a.Func"
771 save_flags = evalarg->eval_flags;
772 evalarg->eval_flags |= EVAL_EVALUATE;
773 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100774 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100775 {
776 dictitem_T *v;
777
778 // If <SID>VarName was used it would not be found, try another way.
779 v = find_var_also_in_script(name, NULL, FALSE);
780 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100781 {
782 name = NULL;
783 goto theend;
784 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100785 copy_tv(&v->di_tv, &ref);
786 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 if (*skipwhite(*arg) != NUL)
788 {
789 if (verbose)
790 semsg(_(e_trailing_characters_str), *arg);
791 name = NULL;
792 }
793 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
794 {
795 name = ref.vval.v_string;
796 ref.vval.v_string = NULL;
797 *tofree = name;
798 }
799 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
800 {
801 if (ref.vval.v_partial->pt_argc > 0
802 || ref.vval.v_partial->pt_dict != NULL)
803 {
804 if (verbose)
805 emsg(_(e_cannot_use_partial_here));
806 name = NULL;
807 }
808 else
809 {
810 name = vim_strsave(partial_name(ref.vval.v_partial));
811 *tofree = name;
812 }
813 }
814 else
815 {
816 if (verbose)
817 semsg(_(e_not_callable_type_str), name);
818 name = NULL;
819 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100820
821theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823 if (evalarg != NULL)
824 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 return name;
826}
827
828/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100829 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200830 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
831 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835call_vim_function(
836 char_u *func,
837 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200838 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200839 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000841 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200842 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 char_u *arg;
844 char_u *name;
845 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000846 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200849 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000850 funcexe.fe_firstline = curwin->w_cursor.lnum;
851 funcexe.fe_lastline = curwin->w_cursor.lnum;
852 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000853
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000854 // The name might be "import.Func" or "Funcref". We don't know, we need to
855 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000856 // autoload script has errors. Guess that when there is a dot in the name
857 // showing errors is the right choice.
858 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000859 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000860 if (ignore_errors)
861 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 if (name == NULL)
866 name = func;
867
868 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
869
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000870 if (ret == FAIL)
871 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000872 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873
874 return ret;
875}
876
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100877/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100878 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000879 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
880 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000881 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000882 */
883 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884call_func_retstr(
885 char_u *func,
886 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200887 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888{
889 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000890 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891
Bram Moolenaarded27a12018-08-01 19:06:03 +0200892 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 return NULL;
894
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100895 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 return retval;
898}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000900/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100901 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000902 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000903 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100904 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 */
906 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100907call_func_retlist(
908 char_u *func,
909 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200910 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911{
912 typval_T rettv;
913
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915 return NULL;
916
917 if (rettv.v_type != VAR_LIST)
918 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100919 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
920 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000921 clear_tv(&rettv);
922 return NULL;
923 }
924
925 return rettv.vval.v_list;
926}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaare70dd112022-01-21 16:31:11 +0000928#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100930 * Evaluate "arg", which is 'foldexpr'.
931 * Note: caller must set "curwin" to match "arg".
932 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
933 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
935 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000936eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000938 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200940 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000943 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100944 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100946 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000947 current_sctx = wp->w_p_script_ctx[WV_FDE];
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000950 if (use_sandbox)
951 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100952 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100954
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100955 // Evaluate the expression. If the expression is "FuncName()" call the
956 // function directly.
957 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = 0;
959 else
960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100961 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 if (tv.v_type == VAR_NUMBER)
963 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000964 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 retval = 0;
966 else
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // If the result is a string, check if there is a non-digit before
969 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +0200971 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 *cp = *s++;
973 retval = atol((char *)s);
974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000978 if (use_sandbox)
979 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100980 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200981 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000982 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200984 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986#endif
987
Ernie Rael64885642023-10-04 20:16:22 +0200988#ifdef LOG_LOCKVAR
989typedef struct flag_string_S
990{
991 int flag;
992 char *str;
993} flag_string_T;
994
995 static char *
996flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
997{
998 char *p = buf;
999 *p = NUL;
1000 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1001 {
1002 if ((fstring->flag & flags) != 0)
1003 {
1004 size_t len = STRLEN(fstring->str);
1005 if (n > p - buf + len + 7)
1006 {
1007 STRCAT(p, fstring->str);
1008 p += len;
1009 STRCAT(p, " ");
1010 ++p;
1011 }
1012 else
1013 {
1014 STRCAT(buf, "...");
1015 break;
1016 }
1017 }
1018 }
1019 return buf;
1020}
1021
1022flag_string_T glv_flag_strings[] = {
1023 { GLV_QUIET, "QUIET" },
1024 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1025 { GLV_READ_ONLY, "READ_ONLY" },
1026 { GLV_NO_DECL, "NO_DECL" },
1027 { GLV_COMPILING, "COMPILING" },
1028 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1029 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1030 { 0, NULL }
1031};
1032#endif
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/*
Ernie Raelee865f32023-09-29 19:53:55 +02001035 * Fill in "lp" using "root". This is used in a special case when
1036 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1037 *
1038 * This is typically called with "lval_root" as "root". For a class, find
1039 * the name from lp in the class from root, fill in lval_T if found. For a
1040 * complex type, list/dict use it as the result; just put the root into
1041 * ll_tv.
1042 *
1043 * "lval_root" is a hack used during run-time/instr-execution to provide the
1044 * starting point for "get_lval()" to traverse a chain of indexes. In some
1045 * cases get_lval sees a bare name and uses this function to populate the
1046 * lval_T.
1047 *
1048 * For setting up "lval_root" (currently only used with lockvar)
1049 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1050 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1051 */
1052 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001053fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001054{
1055#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001056 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1057 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001058#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001059 if (lr->lr_tv == NULL)
1060 return;
Ernie Rael64885642023-10-04 20:16:22 +02001061 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001062 {
Ernie Rael64885642023-10-04 20:16:22 +02001063 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001064 {
1065 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001066 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001067 int m_idx;
1068 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1069 lp->ll_name_end - lp->ll_name, &m_idx);
1070 if (m != NULL)
1071 {
1072 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001073 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001074 lp->ll_oi = m_idx;
1075 lp->ll_valtype = m->ocm_type;
1076 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1077#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001078 ch_log(NULL, "LKVAR: ... class member %s.%s",
1079 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001080#endif
1081 return;
1082 }
1083 }
1084 }
1085
1086#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001087 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001088#endif
Ernie Rael64885642023-10-04 20:16:22 +02001089 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001090 lp->ll_is_root = TRUE;
1091}
1092
1093/*
Ernie Rael64885642023-10-04 20:16:22 +02001094 * Check if the class has permission to access the member.
1095 * Returns OK or FAIL.
1096 */
1097 static int
1098get_lval_check_access(
1099 class_T *cl_exec, // executing class, NULL if :def or script level
1100 class_T *cl, // class which contains the member
1101 ocmember_T *om, // member being accessed
1102 char_u *p, // char after member name
1103 int flags) // GLV flags to check if writing to lval
1104{
1105#ifdef LOG_LOCKVAR
1106 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1107 (void*)cl_exec, (void*)cl, *p);
1108#endif
1109 if (cl_exec == NULL || cl_exec != cl)
1110 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02001111 char *msg = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001112 switch (om->ocm_access)
1113 {
1114 case VIM_ACCESS_PRIVATE:
Ernie Rael03042a22023-11-11 08:53:32 +01001115 msg = e_cannot_access_protected_variable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001116 break;
Ernie Rael64885642023-10-04 20:16:22 +02001117 case VIM_ACCESS_READ:
1118 // If [idx] or .key following, read only OK.
1119 if (*p == '[' || *p == '.')
1120 break;
1121 if ((flags & GLV_READ_ONLY) == 0)
Ernie Raele6c9aa52023-10-06 19:55:52 +02001122 msg = e_variable_is_not_writable_str;
Ernie Rael64885642023-10-04 20:16:22 +02001123 break;
1124 case VIM_ACCESS_ALL:
1125 break;
1126 }
Ernie Raele6c9aa52023-10-06 19:55:52 +02001127 if (msg != NULL)
1128 {
1129 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1130 return FAIL;
1131 }
1132
Ernie Rael64885642023-10-04 20:16:22 +02001133 }
1134 return OK;
1135}
1136
1137/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 * Get an lval: variable, Dict item or List item that can be assigned a value
1139 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1140 * "name.key", "name.key[expr]" etc.
1141 * Indexing only works if "name" is an existing List or Dictionary.
1142 * "name" points to the start of the name.
1143 * If "rettv" is not NULL it points to the value to be assigned.
1144 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1145 * wrong; must end in space or cmd separator.
1146 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001147 * flags:
1148 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001149 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001150 * GLV_NO_AUTOLOAD: do not use script autoloading
1151 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001153 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001155 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001156 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001157get_lval(
1158 char_u *name,
1159 typval_T *rettv,
1160 lval_T *lp,
1161 int unlet,
1162 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001163 int flags, // GLV_ values
1164 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001165{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 char_u *expr_start, *expr_end;
1168 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001169 dictitem_T *v;
1170 typval_T var1;
1171 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001172 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001175 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001176 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001177 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001178 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001179 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001180
Ernie Raelee865f32023-09-29 19:53:55 +02001181#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001182 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001183 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001184 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001185 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1186 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001187 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001188 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001189 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001190#endif
1191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001192 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001193 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001195 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001196 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001197 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001199 lp->ll_name_end = find_name_end(name, NULL, NULL,
1200 FNE_INCL_BR | fne_flags);
1201 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 }
1203
Bram Moolenaara749a422022-02-12 19:52:25 +00001204 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001205 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001206 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1207 {
1208 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1209 return NULL;
1210 }
1211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001212 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001213 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 if (expr_start != NULL)
1216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001217 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001218 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 && *p != '[' && *p != '.')
1220 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001221 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
1223 }
1224
1225 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1226 if (lp->ll_exp_name == NULL)
1227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001228 // Report an invalid expression in braces, unless the
1229 // expression evaluation has been cancelled due to an
1230 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 if (!aborting() && !quiet)
1232 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001233 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001234 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 return NULL;
1236 }
1237 }
1238 lp->ll_name = lp->ll_exp_name;
1239 }
1240 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 lp->ll_name = name;
1243
Bram Moolenaar4525a572022-02-13 11:57:33 +00001244 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001246 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001247 // However, "g:[key]" is indexing a dictionary.
1248 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001249 {
1250 --p;
1251 lp->ll_name_end = p;
1252 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001253 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001254 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001255 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001256
Bram Moolenaar9510d222022-09-11 15:14:05 +01001257 if (is_scoped_variable(name))
1258 {
1259 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1260 return NULL;
1261 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001262 if (VIM_ISWHITE(*p))
1263 {
1264 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1265 return NULL;
1266 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001267 if (tp == p + 1 && !quiet)
1268 {
1269 semsg(_(e_white_space_required_after_str_str), ":", p);
1270 return NULL;
1271 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001272 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001273 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001274 semsg(_(e_using_type_not_in_script_context_str), p);
1275 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001276 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001277 if (vim9script && (flags & GLV_NO_DECL) &&
1278 !(flags & GLV_FOR_LOOP))
1279 {
1280 // Using a type and not in a "var" declaration.
1281 semsg(_(e_trailing_characters_str), p);
1282 return NULL;
1283 }
1284
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001285
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001286 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001287 lp->ll_type = parse_type(&tp,
1288 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1289 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001290 if (lp->ll_type == NULL && !quiet)
1291 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001292 lp->ll_name_end = tp;
1293 }
Ernie Raelee865f32023-09-29 19:53:55 +02001294 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 }
1296 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001297 if (lp->ll_name == NULL)
1298 return p;
1299
Bram Moolenaar62aec932022-01-29 21:45:34 +00001300 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001301 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001302 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001303
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001304 if (import != NULL)
1305 {
1306 ufunc_T *ufunc;
1307 type_T *type;
1308
Bram Moolenaar753885b2022-08-24 16:30:36 +01001309 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001310 lp->ll_sid = import->imp_sid;
1311 lp->ll_name = skipwhite(p + 1);
1312 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1313 lp->ll_name_end = p;
1314
1315 // check the item is exported
1316 cc = *p;
1317 *p = NUL;
1318 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001319 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001320 {
1321 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001322 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001323 }
1324 *p = cc;
1325 }
1326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
Ernie Rael0f058d12023-10-14 11:25:04 +02001328 // Without [idx] or .key we are done.
1329 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001330 {
1331 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001332 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001334 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335
Ernie Rael0f058d12023-10-14 11:25:04 +02001336 if (vim9script && lval_root != NULL)
1337 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001338 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001339 {
1340 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001341 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001342 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001343 }
1344 else
1345 {
1346 cc = *p;
1347 *p = NUL;
1348 // When we would write to the variable pass &ht and prevent autoload.
1349 writing = !(flags & GLV_READ_ONLY);
1350 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001351 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001352 if (v == NULL && !quiet)
1353 semsg(_(e_undefined_variable_str), lp->ll_name);
1354 *p = cc;
1355 if (v == NULL)
1356 return NULL;
1357 lp->ll_tv = &v->di_tv;
1358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359
Bram Moolenaar4525a572022-02-13 11:57:33 +00001360 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001361 {
1362 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001363 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001364 return NULL;
1365 }
1366
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001367 /*
1368 * Loop until no more [idx] or .key is following.
1369 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001370 var1.v_type = VAR_UNKNOWN;
1371 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001372 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001374 vartype_T v_type = lp->ll_tv->v_type;
1375
1376 if (*p == '.' && v_type != VAR_DICT
1377 && v_type != VAR_OBJECT
1378 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001379 {
1380 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001381 semsg(_(e_dot_not_allowed_after_str_str),
1382 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001383 return NULL;
1384 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001385 if (v_type != VAR_LIST
1386 && v_type != VAR_DICT
1387 && v_type != VAR_BLOB
1388 && v_type != VAR_OBJECT
1389 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001392 semsg(_(e_index_not_allowed_after_str_str),
1393 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001396
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001397 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001398 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001399 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001400 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001401 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001402 r = rettv_blob_alloc(lp->ll_tv);
1403 if (r == FAIL)
1404 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001405
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001408 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001409 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001412#ifdef LOG_LOCKVAR
1413 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1414 vartype_name(v_type));
1415#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001416
Bram Moolenaar4525a572022-02-13 11:57:33 +00001417 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001418 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001419 && lp->ll_tv == &v->di_tv
1420 && ht != NULL && ht == get_script_local_ht())
1421 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001422 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001423
1424 // Vim9 script local variable: get the type
1425 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001426 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001427 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001428#ifdef LOG_LOCKVAR
1429 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1430 vartype_name(lp->ll_valtype->tt_type));
1431#endif
1432 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001433 }
1434
Bram Moolenaar8c711452005-01-14 21:53:12 +00001435 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001436 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001437 {
1438 key = p + 1;
1439 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1440 ;
1441 if (len == 0)
1442 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001444 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001445 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001446 }
1447 p = key + len;
1448 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001449 else
1450 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001451 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001452 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001453 if (*p == ':')
1454 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001455 else
1456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001457 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001458 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001460 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001461 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001462 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001463 clear_tv(&var1);
1464 return NULL;
1465 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001466 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001467 }
1468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001469 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470 if (*p == ':')
1471 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001472 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001475 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001476 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001478 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001479 if (rettv != NULL
1480 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001481 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001482 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001483 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001486 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001487 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001488 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001489 }
1490 p = skipwhite(p + 1);
1491 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493 else
1494 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001495 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001496 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001497 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001498 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001499 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001501 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001502 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001503 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001504 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001505 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001506 clear_tv(&var2);
1507 return NULL;
1508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001509 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001510 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001512 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001514
Bram Moolenaar8c711452005-01-14 21:53:12 +00001515 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001516 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001517 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001518 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001519 clear_tv(&var1);
1520 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001522 }
1523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001524 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001525 ++p;
1526 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001527#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02001528 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02001529 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1530 empty1 ? ":" : (char*)tv_get_string(&var1));
1531 else
1532 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02001533#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001534
Bram Moolenaard505d172022-12-18 21:42:55 +00001535 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001536 {
1537 if (len == -1)
1538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001539 // "[key]": get key from "var1"
1540 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001541 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001542 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001543 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001544 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001545 }
1546 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001548 lp->ll_object = NULL;
1549 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001550
1551 // a NULL dict is equivalent with an empty dict
1552 if (lp->ll_tv->vval.v_dict == NULL)
1553 {
1554 lp->ll_tv->vval.v_dict = dict_alloc();
1555 if (lp->ll_tv->vval.v_dict == NULL)
1556 {
1557 clear_tv(&var1);
1558 return NULL;
1559 }
1560 ++lp->ll_tv->vval.v_dict->dv_refcount;
1561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001563
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001564 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001566 // When assigning to a scope dictionary check that a function and
1567 // variable name is valid (only variable name unless it is l: or
1568 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001569 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001570 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001571 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001572
1573 if (len != -1)
1574 {
1575 prevval = key[len];
1576 key[len] = NUL;
1577 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001578 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001579 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001580 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001581 && (rettv->v_type == VAR_FUNC
1582 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001583 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001584 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001585 if (len != -1)
1586 key[len] = prevval;
1587 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001588 {
1589 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001590 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001591 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001592 }
1593
Bram Moolenaar10c65862020-10-08 21:16:42 +02001594 if (lp->ll_valtype != NULL)
1595 // use the type of the member
1596 lp->ll_valtype = lp->ll_valtype->tt_member;
1597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001598 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001599 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001600 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001601 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001602 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001603 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001604 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001605 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001606 return NULL;
1607 }
1608
Bram Moolenaar31b81602019-02-10 22:14:27 +01001609 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001610 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001612 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001613 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001614 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001615 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001616 }
1617 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001618 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001619 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001620 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001621 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001622 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001623 p = NULL;
1624 break;
1625 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001626 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001627 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001628 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1629 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001630 {
1631 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001632 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001633 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001634
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001635 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001636 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001637 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001638 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001639 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001640 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1641
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001642 /*
1643 * Get the number and item for the only or first index of the List.
1644 */
1645 if (empty1)
1646 lp->ll_n1 = 0;
1647 else
1648 // is number or string
1649 lp->ll_n1 = (long)tv_get_number(&var1);
1650 clear_tv(&var1);
1651
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001652 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001653 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001654 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001655 return NULL;
1656 }
1657 if (lp->ll_range && !lp->ll_empty2)
1658 {
1659 lp->ll_n2 = (long)tv_get_number(&var2);
1660 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001661 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1662 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001663 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 }
1665 lp->ll_blob = lp->ll_tv->vval.v_blob;
1666 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001667 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001668 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001669 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001670 {
1671 /*
1672 * Get the number and item for the only or first index of the List.
1673 */
1674 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001675 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001676 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001677 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001678 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001679 clear_tv(&var1);
1680
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001682 lp->ll_object = NULL;
1683 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001684 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001685 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1686 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001687 if (lp->ll_li == NULL)
1688 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001689 clear_tv(&var2);
1690 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001691 }
1692
Bram Moolenaar10c65862020-10-08 21:16:42 +02001693 if (lp->ll_valtype != NULL)
1694 // use the type of the member
1695 lp->ll_valtype = lp->ll_valtype->tt_member;
1696
Bram Moolenaar8c711452005-01-14 21:53:12 +00001697 /*
1698 * May need to find the item or absolute index for the second
1699 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001700 * When no index given: "lp->ll_empty2" is TRUE.
1701 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001702 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001704 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001705 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001706 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001707 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001708 if (check_range_index_two(lp->ll_list,
1709 &lp->ll_n1, lp->ll_li,
1710 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001711 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001712 }
1713
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001714 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001715 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001716 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001717 {
Ernie Raelee865f32023-09-29 19:53:55 +02001718 lp->ll_dict = NULL;
1719 lp->ll_list = NULL;
1720
1721 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001722 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001723 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001724 if (lp->ll_tv->vval.v_object == NULL)
1725 {
1726 if (!quiet)
1727 emsg(_(e_using_null_object));
1728 return NULL;
1729 }
Ernie Raelee865f32023-09-29 19:53:55 +02001730 cl = lp->ll_tv->vval.v_object->obj_class;
1731 lp->ll_object = lp->ll_tv->vval.v_object;
1732 }
1733 else
1734 {
1735 cl = lp->ll_tv->vval.v_class;
1736 lp->ll_object = NULL;
1737 }
1738 lp->ll_class = cl;
1739
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001740 // TODO: what if class is NULL?
1741 if (cl != NULL)
1742 {
1743 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001744
1745 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001746 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001747 // First look for a function with this name.
1748 // round 1: class functions (skipped for an object)
1749 // round 2: object methods
1750 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001751 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001752 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001753 int m_idx;
1754 ufunc_T *fp;
1755
1756 fp = method_lookup(cl,
1757 round == 1 ? VAR_CLASS : VAR_OBJECT,
1758 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001759 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001760 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001761 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001762 lp->ll_ufunc = fp;
1763 lp->ll_valtype = fp->uf_func_type;
1764 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001765 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001766 }
1767 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001768
1769 if (lp->ll_valtype == NULL)
1770 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001771 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001772 ocmember_T *om
1773 = member_lookup(cl, v_type, key, p - key, &m_idx);
1774 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001775 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001776 {
Ernie Rael64885642023-10-04 20:16:22 +02001777 if (get_lval_check_access(cl_exec, cl, om,
1778 p, flags) == FAIL)
1779 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001780
1781 lp->ll_valtype = om->ocm_type;
1782
1783 if (v_type == VAR_OBJECT)
1784 lp->ll_tv = ((typval_T *)(
1785 lp->ll_tv->vval.v_object + 1)) + m_idx;
1786 else
1787 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001788 }
1789 }
1790
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001791 if (lp->ll_valtype == NULL)
1792 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001793 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001794 return NULL;
1795 }
1796 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 }
1799
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001800 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001802 return p;
1803}
1804
1805/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001806 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001807 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001809clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001810{
1811 vim_free(lp->ll_exp_name);
1812 vim_free(lp->ll_newkey);
1813}
1814
1815/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001816 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001817 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001818 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1819 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001820 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822set_var_lval(
1823 lval_T *lp,
1824 char_u *endp,
1825 typval_T *rettv,
1826 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001827 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1828 char_u *op,
1829 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001830{
1831 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001832 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001833
1834 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001836 cc = *endp;
1837 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001838 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001839 return;
1840
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001841 if (lp->ll_blob != NULL)
1842 {
1843 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001844
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001845 if (op != NULL && *op != '=')
1846 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001847 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001848 return;
1849 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001850 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001851 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001852
1853 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1854 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001855 if (lp->ll_empty2)
1856 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1857
Bram Moolenaar68452172021-04-12 21:21:02 +02001858 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1859 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001860 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001861 }
1862 else
1863 {
1864 val = (int)tv_get_number_chk(rettv, &error);
1865 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001866 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001867 }
1868 }
1869 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001871 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001873 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1874 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001875 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001876 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001877 *endp = cc;
1878 return;
1879 }
1880
Bram Moolenaarff697e62019-02-12 22:28:33 +01001881 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001882 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001883 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001884 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001885 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001886 if (di != NULL && di->di_tv.v_type == VAR_TYPEALIAS)
1887 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001888 semsg(_(e_cannot_modify_typealias),
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001889 di->di_tv.vval.v_typealias->ta_name);
1890 clear_tv(&tv);
1891 return;
1892 }
1893
Bram Moolenaar79518e22017-02-17 16:31:35 +01001894 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001895 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1896 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001897 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001898 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001899 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001900 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001903 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001904 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001905 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1906 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001907 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001908 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001909 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001910 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001911 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001912 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001913 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001914 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001915 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001916 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001917 else if (lp->ll_range)
1918 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001919 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1920 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001921 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001922 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001923 return;
1924 }
1925
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001926 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1927 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001928 }
1929 else
1930 {
1931 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001932 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001933 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001934 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1935 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001936 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001937 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001938 return;
1939 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001940
1941 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001942 && check_typval_arg_type(lp->ll_valtype, rettv,
1943 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001944 return;
1945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946 if (lp->ll_newkey != NULL)
1947 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 if (op != NULL && *op != '=')
1949 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001950 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 return;
1952 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001953 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1954 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001955 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001957 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001958 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 if (di == NULL)
1960 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001961 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1962 {
1963 vim_free(di);
1964 return;
1965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001967 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 else if (op != NULL && *op != '=')
1969 {
1970 tv_op(lp->ll_tv, rettv, op);
1971 return;
1972 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001975
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 /*
1977 * Assign the value to the variable or list item.
1978 */
1979 if (copy)
1980 copy_tv(rettv, lp->ll_tv);
1981 else
1982 {
1983 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001984 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 }
1987 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988}
1989
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001991 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1992 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 * Returns OK or FAIL.
1994 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001996tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001998 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001999 char_u numbuf[NUMBUFLEN];
2000 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002001 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002002
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002003 // Can't do anything with a Funcref or Dict on the right.
2004 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002005 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002006 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2007 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002008 {
2009 switch (tv1->v_type)
2010 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002011 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002012 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002014 case VAR_DICT:
2015 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002016 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002017 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002018 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002019 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002020 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002021 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002022 case VAR_CLASS:
2023 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002024 case VAR_TYPEALIAS:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 break;
2026
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002027 case VAR_BLOB:
2028 if (*op != '+' || tv2->v_type != VAR_BLOB)
2029 break;
2030 // BLOB += BLOB
2031 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2032 {
2033 blob_T *b1 = tv1->vval.v_blob;
2034 blob_T *b2 = tv2->vval.v_blob;
2035 int i, len = blob_len(b2);
2036 for (i = 0; i < len; i++)
2037 ga_append(&b1->bv_ga, blob_get(b2, i));
2038 }
2039 return OK;
2040
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002041 case VAR_LIST:
2042 if (*op != '+' || tv2->v_type != VAR_LIST)
2043 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002044 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002045 if (tv2->vval.v_list != NULL)
2046 {
2047 if (tv1->vval.v_list == NULL)
2048 {
2049 tv1->vval.v_list = tv2->vval.v_list;
2050 ++tv1->vval.v_list->lv_refcount;
2051 }
2052 else
2053 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2054 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002055 return OK;
2056
2057 case VAR_NUMBER:
2058 case VAR_STRING:
2059 if (tv2->v_type == VAR_LIST)
2060 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002061 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002063 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002064 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002065 if (tv2->v_type == VAR_FLOAT)
2066 {
2067 float_T f = n;
2068
Bram Moolenaarff697e62019-02-12 22:28:33 +01002069 if (*op == '%')
2070 break;
2071 switch (*op)
2072 {
2073 case '+': f += tv2->vval.v_float; break;
2074 case '-': f -= tv2->vval.v_float; break;
2075 case '*': f *= tv2->vval.v_float; break;
2076 case '/': f /= tv2->vval.v_float; break;
2077 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002078 clear_tv(tv1);
2079 tv1->v_type = VAR_FLOAT;
2080 tv1->vval.v_float = f;
2081 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002082 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002083 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002084 switch (*op)
2085 {
2086 case '+': n += tv_get_number(tv2); break;
2087 case '-': n -= tv_get_number(tv2); break;
2088 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002089 case '/': n = num_divide(n, tv_get_number(tv2),
2090 &failed); break;
2091 case '%': n = num_modulus(n, tv_get_number(tv2),
2092 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002093 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002094 clear_tv(tv1);
2095 tv1->v_type = VAR_NUMBER;
2096 tv1->vval.v_number = n;
2097 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098 }
2099 else
2100 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002101 if (tv2->v_type == VAR_FLOAT)
2102 break;
2103
Bram Moolenaarff697e62019-02-12 22:28:33 +01002104 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002105 s = tv_get_string(tv1);
2106 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002107 clear_tv(tv1);
2108 tv1->v_type = VAR_STRING;
2109 tv1->vval.v_string = s;
2110 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002111 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002112
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002113 case VAR_FLOAT:
2114 {
2115 float_T f;
2116
Bram Moolenaarff697e62019-02-12 22:28:33 +01002117 if (*op == '%' || *op == '.'
2118 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002119 && tv2->v_type != VAR_NUMBER
2120 && tv2->v_type != VAR_STRING))
2121 break;
2122 if (tv2->v_type == VAR_FLOAT)
2123 f = tv2->vval.v_float;
2124 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002125 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002126 switch (*op)
2127 {
2128 case '+': tv1->vval.v_float += f; break;
2129 case '-': tv1->vval.v_float -= f; break;
2130 case '*': tv1->vval.v_float *= f; break;
2131 case '/': tv1->vval.v_float /= f; break;
2132 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002133 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002134 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002135 }
2136 }
2137
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002138 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002139 return FAIL;
2140}
2141
2142/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002143 * Evaluate the expression used in a ":for var in expr" command.
2144 * "arg" points to "var".
2145 * Set "*errp" to TRUE for an error, FALSE otherwise;
2146 * Return a pointer that holds the info. Null when there is an error.
2147 */
2148 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002149eval_for_line(
2150 char_u *arg,
2151 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002152 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002153 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002154{
Bram Moolenaar33570922005-01-25 22:26:29 +00002155 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002156 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002157 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002158 typval_T tv;
2159 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002160 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002162 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002163
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002164 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002165 if (fi == NULL)
2166 return NULL;
2167
Bram Moolenaar404557e2021-07-05 21:41:48 +02002168 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2169 &fi->fi_semicolon, FALSE);
2170 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002171 return fi;
2172
Bram Moolenaar404557e2021-07-05 21:41:48 +02002173 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002174 if (expr[0] != 'i' || expr[1] != 'n'
2175 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002176 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002177 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2178 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2179 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002180 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002181 return fi;
2182 }
2183
2184 if (skip)
2185 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002186 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2187 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002188 {
2189 *errp = FALSE;
2190 if (!skip)
2191 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002192 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002193 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002194 l = tv.vval.v_list;
2195 if (l == NULL)
2196 {
2197 // a null list is like an empty list: do nothing
2198 clear_tv(&tv);
2199 }
2200 else
2201 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002203 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002205 // No need to increment the refcount, it's already set for
2206 // the list being used in "tv".
2207 fi->fi_list = l;
2208 list_add_watch(l, &fi->fi_lw);
2209 fi->fi_lw.lw_item = l->lv_first;
2210 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002211 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002212 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002213 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002214 fi->fi_bi = 0;
2215 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002216 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002217 typval_T btv;
2218
2219 // Make a copy, so that the iteration still works when the
2220 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002222 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002223 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002224 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002225 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002226 else if (tv.v_type == VAR_STRING)
2227 {
2228 fi->fi_byte_idx = 0;
2229 fi->fi_string = tv.vval.v_string;
2230 tv.vval.v_string = NULL;
2231 if (fi->fi_string == NULL)
2232 fi->fi_string = vim_strsave((char_u *)"");
2233 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002234 else
2235 {
Sean Dewar80d73952021-08-04 19:25:54 +02002236 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002237 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002238 }
2239 }
2240 }
2241 if (skip)
2242 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002243 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002244
2245 return fi;
2246}
2247
2248/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002249 * Used when looping over a :for line, skip the "in expr" part.
2250 */
2251 void
2252skip_for_lines(void *fi_void, evalarg_T *evalarg)
2253{
2254 forinfo_T *fi = (forinfo_T *)fi_void;
2255 int i;
2256
2257 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002258 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002259}
2260
2261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002262 * Use the first item in a ":for" list. Advance to the next.
2263 * Assign the values to the variable (list). "arg" points to the first one.
2264 * Return TRUE when a valid item was found, FALSE when at end of list or
2265 * something wrong.
2266 */
2267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002268next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002269{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002270 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002271 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002272 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002273 ? (ASSIGN_FINAL
2274 // first round: error if variable exists
2275 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002276 | ASSIGN_NO_MEMBER_TYPE
2277 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002278 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002279 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002280 int skip_assign = in_vim9script() && arg[0] == '_'
2281 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002282
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002283 if (fi->fi_blob != NULL)
2284 {
2285 typval_T tv;
2286
2287 if (fi->fi_bi >= blob_len(fi->fi_blob))
2288 return FALSE;
2289 tv.v_type = VAR_NUMBER;
2290 tv.v_lock = VAR_FIXED;
2291 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2292 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002293 if (skip_assign)
2294 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002295 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002296 fi->fi_varcount, flag, NULL) == OK;
2297 }
2298
2299 if (fi->fi_string != NULL)
2300 {
2301 typval_T tv;
2302 int len;
2303
2304 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2305 if (len == 0)
2306 return FALSE;
2307 tv.v_type = VAR_STRING;
2308 tv.v_lock = VAR_FIXED;
2309 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2310 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002311 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002312 if (skip_assign)
2313 result = TRUE;
2314 else
2315 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002316 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002317 vim_free(tv.vval.v_string);
2318 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002319 }
2320
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002321 item = fi->fi_lw.lw_item;
2322 if (item == NULL)
2323 result = FALSE;
2324 else
2325 {
2326 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002327 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002328 if (skip_assign)
2329 result = TRUE;
2330 else
2331 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002332 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333 }
2334 return result;
2335}
2336
2337/*
2338 * Free the structure used to store info used by ":for".
2339 */
2340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002341free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002342{
Bram Moolenaar33570922005-01-25 22:26:29 +00002343 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002344
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002345 if (fi == NULL)
2346 return;
2347 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002348 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002349 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002350 list_unref(fi->fi_list);
2351 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002352 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002353 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002354 else
2355 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 vim_free(fi);
2357}
2358
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002360set_context_for_expression(
2361 expand_T *xp,
2362 char_u *arg,
2363 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002365 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002367 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002369 if (cmdidx == CMD_let || cmdidx == CMD_var
2370 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002371 {
2372 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002373 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002375 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002376 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002377 {
2378 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002379 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002380 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002381 break;
2382 }
2383 return;
2384 }
2385 }
2386 else
2387 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2388 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 while ((xp->xp_pattern = vim_strpbrk(arg,
2390 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2391 {
2392 c = *xp->xp_pattern;
2393 if (c == '&')
2394 {
2395 c = xp->xp_pattern[1];
2396 if (c == '&')
2397 {
2398 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002399 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002404 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2405 xp->xp_pattern += 2;
2406
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 else if (c == '$')
2410 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002411 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 xp->xp_context = EXPAND_ENV_VARS;
2413 }
2414 else if (c == '=')
2415 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002416 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 xp->xp_context = EXPAND_EXPRESSION;
2418 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002419 else if (c == '#'
2420 && xp->xp_context == EXPAND_EXPRESSION)
2421 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002422 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002423 break;
2424 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002425 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 && xp->xp_context == EXPAND_FUNCTIONS
2427 && vim_strchr(xp->xp_pattern, '(') == NULL)
2428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002429 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 break;
2431 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002432 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002434 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2437 if (c == '\\' && xp->xp_pattern[1] != NUL)
2438 ++xp->xp_pattern;
2439 xp->xp_context = EXPAND_NOTHING;
2440 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002441 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002443 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2445 /* skip */ ;
2446 xp->xp_context = EXPAND_NOTHING;
2447 }
2448 else if (c == '|')
2449 {
2450 if (xp->xp_pattern[1] == '|')
2451 {
2452 ++xp->xp_pattern;
2453 xp->xp_context = EXPAND_EXPRESSION;
2454 }
2455 else
2456 xp->xp_context = EXPAND_COMMANDS;
2457 }
2458 else
2459 xp->xp_context = EXPAND_EXPRESSION;
2460 }
2461 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002462 // Doesn't look like something valid, expand as an expression
2463 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 arg = xp->xp_pattern;
2466 if (*arg != NUL)
2467 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2468 /* skip */ ;
2469 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002470
2471 // ":exe one two" completes "two"
2472 if ((cmdidx == CMD_execute
2473 || cmdidx == CMD_echo
2474 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002475 || cmdidx == CMD_echomsg
2476 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002477 && xp->xp_context == EXPAND_EXPRESSION)
2478 {
2479 for (;;)
2480 {
2481 char_u *n = skiptowhite(arg);
2482
2483 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2484 break;
2485 arg = skipwhite(n);
2486 }
2487 }
2488
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 xp->xp_pattern = arg;
2490}
2491
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002493 * Return TRUE if "pat" matches "text".
2494 * Does not use 'cpo' and always uses 'magic'.
2495 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002496 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002497pattern_match(char_u *pat, char_u *text, int ic)
2498{
2499 int matches = FALSE;
2500 char_u *save_cpo;
2501 regmatch_T regmatch;
2502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002503 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002504 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002505 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002506 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2507 if (regmatch.regprog != NULL)
2508 {
2509 regmatch.rm_ic = ic;
2510 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2511 vim_regfree(regmatch.regprog);
2512 }
2513 p_cpo = save_cpo;
2514 return matches;
2515}
2516
2517/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002518 * Handle a name followed by "(". Both for just "name(arg)" and for
2519 * "expr->name(arg)".
2520 * Returns OK or FAIL.
2521 */
2522 static int
2523eval_func(
2524 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002525 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002526 char_u *name,
2527 int name_len,
2528 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002529 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002530 typval_T *basetv) // "expr" for "expr->name(arg)"
2531{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002532 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002533 char_u *s = name;
2534 int len = name_len;
2535 partial_T *partial;
2536 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002537 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002538 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002539
2540 if (!evaluate)
2541 check_vars(s, len);
2542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002543 // If "s" is the name of a variable of type VAR_FUNC
2544 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002545 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002546 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002548 // Need to make a copy, in case evaluating the arguments makes
2549 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002550 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002551 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002552 ret = FAIL;
2553 else
2554 {
2555 funcexe_T funcexe;
2556
2557 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002558 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002559 funcexe.fe_firstline = curwin->w_cursor.lnum;
2560 funcexe.fe_lastline = curwin->w_cursor.lnum;
2561 funcexe.fe_evaluate = evaluate;
2562 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002563 if (partial != NULL)
2564 {
2565 funcexe.fe_object = partial->pt_obj;
2566 if (funcexe.fe_object != NULL)
2567 ++funcexe.fe_object->obj_refcount;
2568 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002569 funcexe.fe_basetv = basetv;
2570 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002571 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002572 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002573 }
2574 vim_free(s);
2575
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002576 // If evaluate is FALSE rettv->v_type was not set in
2577 // get_func_tv, but it's needed in handle_subscript() to parse
2578 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002579 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2580 {
2581 rettv->vval.v_string = NULL;
2582 rettv->v_type = VAR_FUNC;
2583 }
2584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002585 // Stop the expression evaluation when immediately
2586 // aborting on error, or when an interrupt occurred or
2587 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002588 if (evaluate && aborting())
2589 {
2590 if (ret == OK)
2591 clear_tv(rettv);
2592 ret = FAIL;
2593 }
2594 return ret;
2595}
2596
2597/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002598 * After a NL, skip over empty lines and comment-only lines.
2599 */
2600 static char_u *
2601newline_skip_comments(char_u *arg)
2602{
2603 char_u *p = arg + 1;
2604
2605 for (;;)
2606 {
2607 p = skipwhite(p);
2608
2609 if (*p == NUL)
2610 break;
2611 if (vim9_comment_start(p))
2612 {
2613 char_u *nl = vim_strchr(p, NL);
2614
2615 if (nl == NULL)
2616 break;
2617 p = nl;
2618 }
2619 if (*p != NL)
2620 break;
2621 ++p; // skip another NL
2622 }
2623 return p;
2624}
2625
2626/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002627 * Get the next line source line without advancing. But do skip over comment
2628 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002629 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002630 */
2631 static char_u *
2632getline_peek_skip_comments(evalarg_T *evalarg)
2633{
2634 for (;;)
2635 {
2636 char_u *next = getline_peek(evalarg->eval_getline,
2637 evalarg->eval_cookie);
2638 char_u *p;
2639
2640 if (next == NULL)
2641 break;
2642 p = skipwhite(next);
2643 if (*p != NUL && !vim9_comment_start(p))
2644 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002645 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002646 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002647 }
2648 return NULL;
2649}
2650
2651/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002652 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2653 * comment) and there is a next line, return the next line (skipping blanks)
2654 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002655 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2656 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002657 * "arg" must point somewhere inside a line, not at the start.
2658 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002659 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002660eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2661{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002662 char_u *p = skipwhite(arg);
2663
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002664 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002665 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002666 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002667 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2668 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002669 && (*p == NUL || *p == NL
2670 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002671 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002672 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002673
Bram Moolenaare442d592022-05-05 12:20:28 +01002674 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002675 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002676 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002677 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002678 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002679 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002680
Bram Moolenaar9d489562020-07-30 20:08:50 +02002681 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002682 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002683 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002684 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685 }
2686 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002687 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002688}
2689
2690/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002691 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002692 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002693 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002694 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002695eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002696{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002697 garray_T *gap = &evalarg->eval_ga;
2698 char_u *line;
2699
Bram Moolenaar39be4982022-05-06 21:51:50 +01002700 if (arg != NULL)
2701 {
2702 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002703 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002704 // Truncate before a trailing comment, so that concatenating the lines
2705 // won't turn the rest into a comment.
2706 if (*skipwhite(arg) == '#')
2707 *arg = NUL;
2708 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002709
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002710 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002711 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2712 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002713 else
2714 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002715 if (line == NULL)
2716 return NULL;
2717
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002718 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002719 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2720 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002721 char_u *p = skipwhite(line);
2722
2723 // Going to concatenate the lines after parsing. For an empty or
2724 // comment line use an empty string.
2725 if (*p == NUL || vim9_comment_start(p))
2726 {
2727 vim_free(line);
2728 line = vim_strsave((char_u *)"");
2729 }
2730
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002731 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2732 ++gap->ga_len;
2733 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002734 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002735 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002736 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002737 evalarg->eval_tofree = line;
2738 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002739
2740 // Advanced to the next line, "arg" no longer points into the previous
2741 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002742 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002743 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002744}
2745
Bram Moolenaare6b53242020-07-01 17:28:33 +02002746/*
2747 * Call eval_next_non_blank() and get the next line if needed.
2748 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002749 char_u *
2750skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2751{
2752 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002753 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002754
Bram Moolenaar962d7212020-07-04 14:15:00 +02002755 if (evalarg == NULL)
2756 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002757 eval_next_non_blank(p, evalarg, &getnext);
2758 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002759 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002760 return p;
2761}
2762
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002763/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002764 * The "eval" functions have an "evalarg" argument: When NULL or
2765 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2766 * parsed but not executed. The functions may return OK, but the rettv will be
2767 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 */
2769
2770/*
2771 * Handle zero level expression.
2772 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002773 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002774 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002775 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 * Return OK or FAIL.
2777 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002779eval0(
2780 char_u *arg,
2781 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002782 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002783 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002785 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2786}
2787
2788/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002789 * If "arg" is a simple function call without arguments then call it and return
2790 * the result. Otherwise return NOTDONE.
2791 */
2792 int
2793may_call_simple_func(
2794 char_u *arg,
2795 typval_T *rettv)
2796{
2797 char_u *parens = (char_u *)strstr((char *)arg, "()");
2798 int r = NOTDONE;
2799
2800 // If the expression is "FuncName()" then we can skip a lot of overhead.
2801 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2802 {
2803 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2804
2805 if (to_name_end(p, TRUE) == parens)
2806 r = call_simple_func(arg, (int)(parens - arg), rettv);
2807 }
2808 return r;
2809}
2810
2811/*
2812 * Handle zero level expression with optimization for a simple function call.
2813 * Same arguments and return value as eval0().
2814 */
2815 int
2816eval0_simple_funccal(
2817 char_u *arg,
2818 typval_T *rettv,
2819 exarg_T *eap,
2820 evalarg_T *evalarg)
2821{
2822 int r = may_call_simple_func(arg, rettv);
2823
2824 if (r == NOTDONE)
2825 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2826 return r;
2827}
2828
2829/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002830 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2831 * expression and don't check what comes after the expression.
2832 */
2833 int
2834eval0_retarg(
2835 char_u *arg,
2836 typval_T *rettv,
2837 exarg_T *eap,
2838 evalarg_T *evalarg,
2839 char_u **retarg)
2840{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 int ret;
2842 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002843 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002844 int did_emsg_before = did_emsg;
2845 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002846 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002847 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
2849 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002850 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002851
Bram Moolenaar79481362022-06-27 20:15:10 +01002852 if (ret != FAIL)
2853 {
2854 expr_end = p;
2855 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002856
Bram Moolenaar79481362022-06-27 20:15:10 +01002857 // In Vim9 script a command block is not split at NL characters for
2858 // commands using an expression argument. Skip over a '#' comment to
2859 // check for a following NL. Require white space before the '#'.
2860 if (in_vim9script() && p > expr_end && retarg == NULL)
2861 while (*p == '#')
2862 {
2863 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002864
Bram Moolenaar79481362022-06-27 20:15:10 +01002865 if (nl == NULL)
2866 break;
2867 p = skipwhite(nl + 1);
2868 if (eap != NULL && *p != NUL)
2869 eap->nextcmd = p;
2870 check_for_end = FALSE;
2871 }
2872
2873 if (check_for_end)
2874 end_error = !ends_excmd2(arg, p);
2875 }
2876
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002877 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
2879 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002880 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 /*
2882 * Report the invalid expression unless the expression evaluation has
2883 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002884 * exception, or we already gave a more specific error.
2885 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002887 if (!aborting()
2888 && did_emsg == did_emsg_before
2889 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002890 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002891 {
2892 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002893 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002894 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002895 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002896 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002897
zeertzjqf2588b62023-05-05 17:22:35 +01002898 if (eap != NULL && p != NULL)
2899 {
2900 // Some of the expression may not have been consumed.
2901 // Only execute a next command if it cannot be a "||" operator.
2902 // The next command may be "catch".
2903 char_u *nextcmd = check_nextcmd(p);
2904 if (nextcmd != NULL && *nextcmd != '|')
2905 eap->nextcmd = nextcmd;
2906 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002907 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002909
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002910 if (retarg != NULL)
2911 *retarg = p;
2912 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002913 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 return ret;
2916}
2917
2918/*
2919 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002920 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002921 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 *
2923 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002924 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002926 * Note: "rettv.v_lock" is not set.
2927 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 * Return OK or FAIL.
2929 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002930 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002931eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002933 char_u *p;
2934 int getnext;
2935
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002936 CLEAR_POINTER(rettv);
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 /*
2939 * Get the first variable.
2940 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002941 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 return FAIL;
2943
Bram Moolenaar793648f2020-06-26 21:28:25 +02002944 p = eval_next_non_blank(*arg, evalarg, &getnext);
2945 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002947 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002948 int result;
2949 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002950 evalarg_T *evalarg_used = evalarg;
2951 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002952 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002953 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002954 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002955
2956 if (evalarg == NULL)
2957 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002958 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002959 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002960 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002961 orig_flags = evalarg_used->eval_flags;
2962 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002963
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002964 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002965 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002966 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002967 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002968 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002969 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002970 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002971 clear_tv(rettv);
2972 return FAIL;
2973 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002974 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002975 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002976
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002978 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002980 int error = FALSE;
2981
Bram Moolenaar13106602020-10-04 16:06:05 +02002982 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002983 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002984 else if (vim9script)
2985 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002986 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002988 if (error || !op_falsy || !result)
2989 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002990 if (error)
2991 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
2993
2994 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002995 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002997 if (op_falsy)
2998 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002999 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003000 {
mityu4ac198c2021-05-28 17:52:40 +02003001 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003002 clear_tv(rettv);
3003 return FAIL;
3004 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003005 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003006 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003007 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003008 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003009 {
3010 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003012 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003013 if (!op_falsy || !result)
3014 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003016 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003018 /*
3019 * Check for the ":".
3020 */
3021 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3022 if (*p != ':')
3023 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003024 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003025 if (evaluate && result)
3026 clear_tv(rettv);
3027 evalarg_used->eval_flags = orig_flags;
3028 return FAIL;
3029 }
3030 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003031 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003032 else
3033 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003034 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003035 {
3036 error_white_both(p, 1);
3037 clear_tv(rettv);
3038 evalarg_used->eval_flags = orig_flags;
3039 return FAIL;
3040 }
3041 *arg = p;
3042 }
3043
3044 /*
3045 * Get the third variable. Recursive!
3046 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003047 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003048 {
mityu4ac198c2021-05-28 17:52:40 +02003049 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003050 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003051 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003052 return FAIL;
3053 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003054 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3055 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003056 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003057 if (eval1(arg, &var2, evalarg_used) == FAIL)
3058 {
3059 if (evaluate && result)
3060 clear_tv(rettv);
3061 evalarg_used->eval_flags = orig_flags;
3062 return FAIL;
3063 }
3064 if (evaluate && !result)
3065 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003067
3068 if (evalarg == NULL)
3069 clear_evalarg(&local_evalarg, NULL);
3070 else
3071 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
3073
3074 return OK;
3075}
3076
3077/*
3078 * Handle first level expression:
3079 * expr2 || expr2 || expr2 logical OR
3080 *
3081 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003082 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 *
3084 * Return OK or FAIL.
3085 */
3086 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003087eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003089 char_u *p;
3090 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003093 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003095 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 return FAIL;
3097
3098 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003099 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003101 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003102 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003104 evalarg_T *evalarg_used = evalarg;
3105 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003106 int evaluate;
3107 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003108 long result = FALSE;
3109 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003110 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003111 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003112
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003113 if (evalarg == NULL)
3114 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003115 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003116 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003117 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003118 orig_flags = evalarg_used->eval_flags;
3119 evaluate = orig_flags & EVAL_EVALUATE;
3120 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003121 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003122 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003123 result = tv_get_bool_chk(rettv, &error);
3124 else if (tv_get_number_chk(rettv, &error) != 0)
3125 result = TRUE;
3126 clear_tv(rettv);
3127 if (error)
3128 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
3130
3131 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003132 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003134 while (p[0] == '|' && p[1] == '|')
3135 {
3136 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003137 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003138 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003139 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003140 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003141 {
3142 error_white_both(p, 2);
3143 clear_tv(rettv);
3144 return FAIL;
3145 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003146 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003147 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003148
3149 /*
3150 * Get the second variable.
3151 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003152 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003153 {
mityu4ac198c2021-05-28 17:52:40 +02003154 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003155 clear_tv(rettv);
3156 return FAIL;
3157 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003158 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3159 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003160 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003161 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003162 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003163
3164 /*
3165 * Compute the result.
3166 */
3167 if (evaluate && !result)
3168 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003169 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003170 result = tv_get_bool_chk(&var2, &error);
3171 else if (tv_get_number_chk(&var2, &error) != 0)
3172 result = TRUE;
3173 clear_tv(&var2);
3174 if (error)
3175 return FAIL;
3176 }
3177 if (evaluate)
3178 {
3179 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003180 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003181 rettv->v_type = VAR_BOOL;
3182 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003183 }
3184 else
3185 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003186 rettv->v_type = VAR_NUMBER;
3187 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003188 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003189 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003190
3191 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003193
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003194 if (evalarg == NULL)
3195 clear_evalarg(&local_evalarg, NULL);
3196 else
3197 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199
3200 return OK;
3201}
3202
3203/*
3204 * Handle second level expression:
3205 * expr3 && expr3 && expr3 logical AND
3206 *
3207 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003208 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 *
3210 * Return OK or FAIL.
3211 */
3212 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003213eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003215 char_u *p;
3216 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003219 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003221 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 return FAIL;
3223
3224 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003225 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003227 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003228 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003230 evalarg_T *evalarg_used = evalarg;
3231 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003232 int orig_flags;
3233 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003234 long result = TRUE;
3235 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003236 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003237 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003238
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003239 if (evalarg == NULL)
3240 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003241 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003242 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003243 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003244 orig_flags = evalarg_used->eval_flags;
3245 evaluate = orig_flags & EVAL_EVALUATE;
3246 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003247 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003248 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003249 result = tv_get_bool_chk(rettv, &error);
3250 else if (tv_get_number_chk(rettv, &error) == 0)
3251 result = FALSE;
3252 clear_tv(rettv);
3253 if (error)
3254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
3256
3257 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003258 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003260 while (p[0] == '&' && p[1] == '&')
3261 {
3262 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003263 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003264 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003265 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003266 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003267 {
3268 error_white_both(p, 2);
3269 clear_tv(rettv);
3270 return FAIL;
3271 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003272 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003273 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003274
3275 /*
3276 * Get the second variable.
3277 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003278 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003279 {
mityu4ac198c2021-05-28 17:52:40 +02003280 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003281 clear_tv(rettv);
3282 return FAIL;
3283 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003284 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3285 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003286 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003287 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003288 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003290
3291 /*
3292 * Compute the result.
3293 */
3294 if (evaluate && result)
3295 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003296 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003297 result = tv_get_bool_chk(&var2, &error);
3298 else if (tv_get_number_chk(&var2, &error) == 0)
3299 result = FALSE;
3300 clear_tv(&var2);
3301 if (error)
3302 return FAIL;
3303 }
3304 if (evaluate)
3305 {
3306 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003307 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003308 rettv->v_type = VAR_BOOL;
3309 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003310 }
3311 else
3312 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003313 rettv->v_type = VAR_NUMBER;
3314 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003315 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003316 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003317
3318 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003320
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003321 if (evalarg == NULL)
3322 clear_evalarg(&local_evalarg, NULL);
3323 else
3324 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326
3327 return OK;
3328}
3329
3330/*
3331 * Handle third level expression:
3332 * var1 == var2
3333 * var1 =~ var2
3334 * var1 != var2
3335 * var1 !~ var2
3336 * var1 > var2
3337 * var1 >= var2
3338 * var1 < var2
3339 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003340 * var1 is var2
3341 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 *
3343 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003344 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 *
3346 * Return OK or FAIL.
3347 */
3348 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003349eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003352 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003353 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003355 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
3357 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003358 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003360 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 return FAIL;
3362
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003363 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003364
Bram Moolenaar696ba232020-07-29 21:20:41 +02003365 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003368 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003370 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003372 typval_T var2;
3373 int ic;
3374 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003375 int evaluate = evalarg == NULL
3376 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003377 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003378
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003379 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003380 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003381 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003382 p = *arg;
3383 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003384 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3385 {
mityu4ac198c2021-05-28 17:52:40 +02003386 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003387 clear_tv(rettv);
3388 return FAIL;
3389 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003390
Bram Moolenaar696ba232020-07-29 21:20:41 +02003391 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3392 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003393 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003394 clear_tv(rettv);
3395 return FAIL;
3396 }
3397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003398 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (p[len] == '?')
3400 {
3401 ic = TRUE;
3402 ++len;
3403 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003404 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 else if (p[len] == '#')
3406 {
3407 ic = FALSE;
3408 ++len;
3409 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003410 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003412 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
3414 /*
3415 * Get the second variable.
3416 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003417 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3418 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003419 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003420 clear_tv(rettv);
3421 return FAIL;
3422 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003423 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003424 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003426 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 return FAIL;
3428 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003429 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003430 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003431 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003432
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003433 // use the line of the comparison for messages
3434 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003435 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003436 {
3437 ret = FAIL;
3438 clear_tv(rettv);
3439 }
3440 else
3441 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003442 clear_tv(&var2);
3443 return ret;
3444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446
3447 return OK;
3448}
3449
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003450/*
3451 * Make a copy of blob "tv1" and append blob "tv2".
3452 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 void
3454eval_addblob(typval_T *tv1, typval_T *tv2)
3455{
3456 blob_T *b1 = tv1->vval.v_blob;
3457 blob_T *b2 = tv2->vval.v_blob;
3458 blob_T *b = blob_alloc();
3459 int i;
3460
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003461 if (b == NULL)
3462 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003464 for (i = 0; i < blob_len(b1); i++)
3465 ga_append(&b->bv_ga, blob_get(b1, i));
3466 for (i = 0; i < blob_len(b2); i++)
3467 ga_append(&b->bv_ga, blob_get(b2, i));
3468
3469 clear_tv(tv1);
3470 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471}
3472
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003473/*
3474 * Make a copy of list "tv1" and append list "tv2".
3475 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 int
3477eval_addlist(typval_T *tv1, typval_T *tv2)
3478{
3479 typval_T var3;
3480
3481 // concatenate Lists
3482 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3483 {
3484 clear_tv(tv1);
3485 clear_tv(tv2);
3486 return FAIL;
3487 }
3488 clear_tv(tv1);
3489 *tv1 = var3;
3490 return OK;
3491}
3492
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003494 * Handle the bitwise left/right shift operator expression:
3495 * var1 << var2
3496 * var1 >> var2
3497 *
3498 * "arg" must point to the first non-white of the expression.
3499 * "arg" is advanced to just after the recognized expression.
3500 *
3501 * Return OK or FAIL.
3502 */
3503 static int
3504eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3505{
3506 /*
3507 * Get the first expression.
3508 */
3509 if (eval6(arg, rettv, evalarg) == FAIL)
3510 return FAIL;
3511
3512 /*
3513 * Repeat computing, until no '<<' or '>>' is following.
3514 */
3515 for (;;)
3516 {
3517 char_u *p;
3518 int getnext;
3519 exprtype_T type;
3520 int evaluate;
3521 typval_T var2;
3522 int vim9script;
3523
3524 p = eval_next_non_blank(*arg, evalarg, &getnext);
3525 if (p[0] == '<' && p[1] == '<')
3526 type = EXPR_LSHIFT;
3527 else if (p[0] == '>' && p[1] == '>')
3528 type = EXPR_RSHIFT;
3529 else
3530 return OK;
3531
3532 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003533 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3534 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003535 {
3536 // left operand should be a number
3537 emsg(_(e_bitshift_ops_must_be_number));
3538 clear_tv(rettv);
3539 return FAIL;
3540 }
3541
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003542 vim9script = in_vim9script();
3543 if (getnext)
3544 {
3545 *arg = eval_next_line(*arg, evalarg);
3546 p = *arg;
3547 }
3548 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3549 {
3550 error_white_both(*arg, 2);
3551 clear_tv(rettv);
3552 return FAIL;
3553 }
3554
3555 /*
3556 * Get the second variable.
3557 */
3558 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3559 {
3560 error_white_both(p, 2);
3561 clear_tv(rettv);
3562 return FAIL;
3563 }
3564 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3565 if (eval6(arg, &var2, evalarg) == FAIL)
3566 {
3567 clear_tv(rettv);
3568 return FAIL;
3569 }
3570
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003571 if (evaluate)
3572 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003573 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3574 {
3575 // right operand should be a positive number
3576 if (var2.v_type != VAR_NUMBER)
3577 emsg(_(e_bitshift_ops_must_be_number));
3578 else
3579 emsg(_(e_bitshift_ops_must_be_positive));
3580 clear_tv(rettv);
3581 clear_tv(&var2);
3582 return FAIL;
3583 }
3584
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003585 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3586 // shifting more bits than we have always results in zero
3587 rettv->vval.v_number = 0;
3588 else if (type == EXPR_LSHIFT)
3589 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003590 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003591 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003592 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003593 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003594 }
3595
3596 clear_tv(&var2);
3597 }
3598
3599 return OK;
3600}
3601
3602/*
3603 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003604 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003606 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003607 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 *
3609 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003610 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 *
3612 * Return OK or FAIL.
3613 */
3614 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003615eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003618 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003620 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 return FAIL;
3622
3623 /*
3624 * Repeat computing, until no '+', '-' or '.' is following.
3625 */
3626 for (;;)
3627 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003628 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003629 int getnext;
3630 char_u *p;
3631 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003632 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003633 int concat;
3634 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003635 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003636
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003637 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003638 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003639 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003640 p = eval_next_non_blank(*arg, evalarg, &getnext);
3641 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003642 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003643 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3644 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003646 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3647 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003648
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003649 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003650 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003651 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003652 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003653 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003654 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003655 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003656 {
mityu4ac198c2021-05-28 17:52:40 +02003657 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003658 clear_tv(rettv);
3659 return FAIL;
3660 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003661 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003662 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003663 if ((op != '+' || (rettv->v_type != VAR_LIST
3664 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003665 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003666 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003667 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003668 int error = FALSE;
3669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003670 // For "list + ...", an illegal use of the first operand as
3671 // a number cannot be determined before evaluating the 2nd
3672 // operand: if this is also a list, all is ok.
3673 // For "something . ...", "something - ..." or "non-list + ...",
3674 // we know that the first operand needs to be a string or number
3675 // without evaluating the 2nd operand. So check before to avoid
3676 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003677 if (op != '.')
3678 tv_get_number_chk(rettv, &error);
3679 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003680 {
3681 clear_tv(rettv);
3682 return FAIL;
3683 }
3684 }
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 /*
3687 * Get the second variable.
3688 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003689 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003690 {
mityu89dcb4d2021-05-28 13:50:17 +02003691 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003692 clear_tv(rettv);
3693 return FAIL;
3694 }
3695 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003696 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003698 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 return FAIL;
3700 }
3701
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003702 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 /*
3705 * Compute the result.
3706 */
3707 if (op == '.')
3708 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003709 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3710 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003711 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003712
Bram Moolenaar2e086612020-08-25 22:37:48 +02003713 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003714 || var2.v_type == VAR_CHANNEL
3715 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003716 semsg(_(e_using_invalid_value_as_string_str),
3717 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003718 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003719 {
3720 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3721 var2.vval.v_float);
3722 s2 = buf2;
3723 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003724 else
3725 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003726 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003727 {
3728 clear_tv(rettv);
3729 clear_tv(&var2);
3730 return FAIL;
3731 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003732 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733 clear_tv(rettv);
3734 rettv->v_type = VAR_STRING;
3735 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003737 else if (op == '+' && rettv->v_type == VAR_BLOB
3738 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003740 else if (op == '+' && rettv->v_type == VAR_LIST
3741 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003742 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003744 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 else
3747 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003748 int error = FALSE;
3749 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003750 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003751
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003752 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003754 f1 = rettv->vval.v_float;
3755 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003757 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003759 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003760 if (error)
3761 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003762 // This can only happen for "list + non-list" or
3763 // "blob + non-blob". For "non-list + ..." or
3764 // "something - ...", we returned before evaluating the
3765 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003766 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003767 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003768 return FAIL;
3769 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003770 if (var2.v_type == VAR_FLOAT)
3771 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003772 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003773 if (var2.v_type == VAR_FLOAT)
3774 {
3775 f2 = var2.vval.v_float;
3776 n2 = 0;
3777 }
3778 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003780 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003781 if (error)
3782 {
3783 clear_tv(rettv);
3784 clear_tv(&var2);
3785 return FAIL;
3786 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003787 if (rettv->v_type == VAR_FLOAT)
3788 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003790 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003792 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003793 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3794 {
3795 if (op == '+')
3796 f1 = f1 + f2;
3797 else
3798 f1 = f1 - f2;
3799 rettv->v_type = VAR_FLOAT;
3800 rettv->vval.v_float = f1;
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003803 {
3804 if (op == '+')
3805 n1 = n1 + n2;
3806 else
3807 n1 = n1 - n2;
3808 rettv->v_type = VAR_NUMBER;
3809 rettv->vval.v_number = n1;
3810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814 }
3815 return OK;
3816}
3817
3818/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003819 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 * * number multiplication
3821 * / number division
3822 * % number modulo
3823 *
3824 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003825 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 *
3827 * Return OK or FAIL.
3828 */
3829 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003830eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003831 char_u **arg,
3832 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003833 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003834 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003836 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
3838 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003839 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003841 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 return FAIL;
3843
3844 /*
3845 * Repeat computing, until no '*', '/' or '%' is following.
3846 */
3847 for (;;)
3848 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003849 int evaluate;
3850 int getnext;
3851 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003852 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003853 int op;
3854 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003855 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003856 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003857
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003858 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003859 p = eval_next_non_blank(*arg, evalarg, &getnext);
3860 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003861 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003863
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003864 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003865 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003866 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003867 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003868 {
3869 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3870 {
mityu4ac198c2021-05-28 17:52:40 +02003871 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003872 clear_tv(rettv);
3873 return FAIL;
3874 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003875 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003878 f1 = 0;
3879 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003880 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003881 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003883 if (rettv->v_type == VAR_FLOAT)
3884 {
3885 f1 = rettv->vval.v_float;
3886 use_float = TRUE;
3887 n1 = 0;
3888 }
3889 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003890 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003892 if (error)
3893 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895 else
3896 n1 = 0;
3897
3898 /*
3899 * Get the second variable.
3900 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003901 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3902 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003903 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003904 clear_tv(rettv);
3905 return FAIL;
3906 }
3907 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003908 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return FAIL;
3910
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003911 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003913 if (var2.v_type == VAR_FLOAT)
3914 {
3915 if (!use_float)
3916 {
3917 f1 = n1;
3918 use_float = TRUE;
3919 }
3920 f2 = var2.vval.v_float;
3921 n2 = 0;
3922 }
3923 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003925 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003926 clear_tv(&var2);
3927 if (error)
3928 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003929 if (use_float)
3930 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
3933 /*
3934 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003935 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003937 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003939 if (op == '*')
3940 f1 = f1 * f2;
3941 else if (op == '/')
3942 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003943#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003944 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003945 if (f2 == 0.0)
3946 {
3947 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003948 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003949 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003950 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003951 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003952 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003953 }
3954 else
3955 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003956#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003957 // We rely on the floating point library to handle divide
3958 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003959 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003960#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003963 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003964 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003965 return FAIL;
3966 }
3967 rettv->v_type = VAR_FLOAT;
3968 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 else
3971 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003972 int failed = FALSE;
3973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003974 if (op == '*')
3975 n1 = n1 * n2;
3976 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003977 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003979 n1 = num_modulus(n1, n2, &failed);
3980 if (failed)
3981 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003982
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003983 rettv->v_type = VAR_NUMBER;
3984 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 }
3988
3989 return OK;
3990}
3991
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003992/*
3993 * Handle a type cast before a base level expression.
3994 * "arg" must point to the first non-white of the expression.
3995 * "arg" is advanced to just after the recognized expression.
3996 * Return OK or FAIL.
3997 */
3998 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003999eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004000 char_u **arg,
4001 typval_T *rettv,
4002 evalarg_T *evalarg,
4003 int want_string) // after "." operator
4004{
4005 type_T *want_type = NULL;
4006 garray_T type_list; // list of pointers to allocated types
4007 int res;
4008 int evaluate = evalarg == NULL ? 0
4009 : (evalarg->eval_flags & EVAL_EVALUATE);
4010
4011 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004012 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4013 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004014 {
4015 ++*arg;
4016 ga_init2(&type_list, sizeof(type_T *), 10);
4017 want_type = parse_type(arg, &type_list, TRUE);
4018 if (want_type == NULL && (evaluate || **arg != '>'))
4019 {
4020 clear_type_list(&type_list);
4021 return FAIL;
4022 }
4023
4024 if (**arg != '>')
4025 {
4026 if (*skipwhite(*arg) == '>')
4027 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4028 else
4029 emsg(_(e_missing_gt));
4030 clear_type_list(&type_list);
4031 return FAIL;
4032 }
4033 ++*arg;
4034 *arg = skipwhite_and_linebreak(*arg, evalarg);
4035 }
4036
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004037 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004038
4039 if (want_type != NULL && evaluate)
4040 {
4041 if (res == OK)
4042 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004043 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4044 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004045
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004046 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004047 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004048 if (want_type->tt_type == VAR_BOOL
4049 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004050 && (actual->tt_flags & TTFLAG_BOOL_OK))
4051 {
4052 int n = tv2bool(rettv);
4053
4054 // can use "0" and "1" for boolean in some places
4055 clear_tv(rettv);
4056 rettv->v_type = VAR_BOOL;
4057 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4058 }
4059 else
4060 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004061 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004062
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004063 res = check_type(want_type, actual, TRUE, where);
4064 }
4065 }
4066 }
4067 clear_type_list(&type_list);
4068 }
4069
4070 return res;
4071}
4072
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004073 int
4074eval_leader(char_u **arg, int vim9)
4075{
4076 char_u *s = *arg;
4077 char_u *p = *arg;
4078
4079 while (*p == '!' || *p == '-' || *p == '+')
4080 {
4081 char_u *n = skipwhite(p + 1);
4082
4083 // ++, --, -+ and +- are not accepted in Vim9 script
4084 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4085 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004086 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004087 return FAIL;
4088 }
4089 p = n;
4090 }
4091 *arg = p;
4092 return OK;
4093}
4094
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004096 * Check for a predefined value "true", "false" and "null.*".
4097 * Return OK when recognized.
4098 */
4099 int
4100handle_predefined(char_u *s, int len, typval_T *rettv)
4101{
4102 switch (len)
4103 {
4104 case 4: if (STRNCMP(s, "true", 4) == 0)
4105 {
4106 rettv->v_type = VAR_BOOL;
4107 rettv->vval.v_number = VVAL_TRUE;
4108 return OK;
4109 }
4110 if (STRNCMP(s, "null", 4) == 0)
4111 {
4112 rettv->v_type = VAR_SPECIAL;
4113 rettv->vval.v_number = VVAL_NULL;
4114 return OK;
4115 }
4116 break;
4117 case 5: if (STRNCMP(s, "false", 5) == 0)
4118 {
4119 rettv->v_type = VAR_BOOL;
4120 rettv->vval.v_number = VVAL_FALSE;
4121 return OK;
4122 }
4123 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004124 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4125 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004126#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004127 rettv->v_type = VAR_JOB;
4128 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004129#else
4130 rettv->v_type = VAR_SPECIAL;
4131 rettv->vval.v_number = VVAL_NULL;
4132#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004133 return OK;
4134 }
4135 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004136 case 9:
4137 if (STRNCMP(s, "null_", 5) != 0)
4138 break;
4139 if (STRNCMP(s + 5, "list", 4) == 0)
4140 {
4141 rettv->v_type = VAR_LIST;
4142 rettv->vval.v_list = NULL;
4143 return OK;
4144 }
4145 if (STRNCMP(s + 5, "dict", 4) == 0)
4146 {
4147 rettv->v_type = VAR_DICT;
4148 rettv->vval.v_dict = NULL;
4149 return OK;
4150 }
4151 if (STRNCMP(s + 5, "blob", 4) == 0)
4152 {
4153 rettv->v_type = VAR_BLOB;
4154 rettv->vval.v_blob = NULL;
4155 return OK;
4156 }
4157 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004158 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4159 {
4160 rettv->v_type = VAR_CLASS;
4161 rettv->vval.v_class = NULL;
4162 return OK;
4163 }
4164 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004165 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4166 {
4167 rettv->v_type = VAR_STRING;
4168 rettv->vval.v_string = NULL;
4169 return OK;
4170 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004171 if (STRNCMP(s, "null_object", 11) == 0)
4172 {
4173 rettv->v_type = VAR_OBJECT;
4174 rettv->vval.v_object = NULL;
4175 return OK;
4176 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004177 break;
4178 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004179 if (STRNCMP(s, "null_channel", 12) == 0)
4180 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004181#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004182 rettv->v_type = VAR_CHANNEL;
4183 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004184#else
4185 rettv->v_type = VAR_SPECIAL;
4186 rettv->vval.v_number = VVAL_NULL;
4187#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004188 return OK;
4189 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004190 if (STRNCMP(s, "null_partial", 12) == 0)
4191 {
4192 rettv->v_type = VAR_PARTIAL;
4193 rettv->vval.v_partial = NULL;
4194 return OK;
4195 }
4196 break;
4197 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4198 {
4199 rettv->v_type = VAR_FUNC;
4200 rettv->vval.v_string = NULL;
4201 return OK;
4202 }
4203 break;
4204 }
4205 return FAIL;
4206}
4207
4208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * Handle sixth level expression:
4210 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004211 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004212 * "string" string constant
4213 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 * &option-name option value
4215 * @r register contents
4216 * identifier variable value
4217 * function() function call
4218 * $VAR environment variable
4219 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004220 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004222 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004223 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 *
4225 * Also handle:
4226 * ! in front logical NOT
4227 * - in front unary minus
4228 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004229 * trailing [] subscript in String or List
4230 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004231 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 *
4233 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004234 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 *
4236 * Return OK or FAIL.
4237 */
4238 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004239eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004240 char_u **arg,
4241 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004242 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004245 int evaluate = evalarg != NULL
4246 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 int len;
4248 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004249 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 char_u *start_leader, *end_leader;
4251 int ret = OK;
4252 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004253 static int recurse = 0;
4254 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
4256 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004258 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
4262 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004263 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 */
4265 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004266 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 end_leader = *arg;
4269
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004270 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004271 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004272 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004273 ++*arg;
4274 return FAIL;
4275 }
4276
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004277 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004278 // and crash. With MSVC the stack is smaller.
4279 if (recurse ==
4280#ifdef _MSC_VER
4281 300
4282#else
4283 1000
4284#endif
4285 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004286 {
4287 semsg(_(e_expression_too_recursive_str), *arg);
4288 return FAIL;
4289 }
4290 ++recurse;
4291
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 switch (**arg)
4293 {
4294 /*
4295 * Number constant.
4296 */
4297 case '0':
4298 case '1':
4299 case '2':
4300 case '3':
4301 case '4':
4302 case '5':
4303 case '6':
4304 case '7':
4305 case '8':
4306 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004307 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004308
4309 // Apply prefixed "-" and "+" now. Matters especially when
4310 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004311 if (ret == OK && evaluate && end_leader > start_leader
4312 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004313 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 break;
4315
4316 /*
4317 * String constant: "string".
4318 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004319 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 break;
4321
4322 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004325 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004326 break;
4327
4328 /*
4329 * List: [expr, expr]
4330 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004331 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 break;
4333
4334 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004335 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004336 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004337 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004338 {
4339 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4340 }
4341 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004342 {
4343 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004344 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004345 }
4346 else
4347 ret = NOTDONE;
4348 break;
4349
4350 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004351 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004352 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004353 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004354 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004355 ret = NOTDONE;
4356 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004357 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004358 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004359 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004360 break;
4361
4362 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004363 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004365 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 break;
4367
4368 /*
4369 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004370 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 */
LemonBoy2eaef102022-05-06 13:14:50 +01004372 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4373 ret = eval_interp_string(arg, rettv, evaluate);
4374 else
4375 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 break;
4377
4378 /*
4379 * Register contents: @r.
4380 */
4381 case '@': ++*arg;
4382 if (evaluate)
4383 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004384 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004385 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004386 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004387 emsg_invreg(**arg);
4388 else
4389 {
4390 rettv->v_type = VAR_STRING;
4391 rettv->vval.v_string = get_reg_contents(**arg,
4392 GREG_EXPR_SRC);
4393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 if (**arg != NUL)
4396 ++*arg;
4397 break;
4398
4399 /*
4400 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004401 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004403 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004404 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004405 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004406 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004407 if (ret == OK && evaluate)
4408 {
4409 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4410
Bram Moolenaara9931532021-06-12 15:58:16 +02004411 // Compile it here to get the return type. The return
4412 // type is optional, when it's missing use t_unknown.
4413 // This is recognized in compile_return().
4414 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4415 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004416 if (compile_def_function(ufunc, FALSE,
4417 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004418 {
4419 clear_tv(rettv);
4420 ret = FAIL;
4421 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004422 }
4423 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004424 if (ret == NOTDONE)
4425 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004426 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004427 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004428
Bram Moolenaar9215f012020-06-27 21:18:00 +02004429 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004430 if (**arg == ')')
4431 ++*arg;
4432 else if (ret == OK)
4433 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004434 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004435 clear_tv(rettv);
4436 ret = FAIL;
4437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439 break;
4440
Bram Moolenaar8c711452005-01-14 21:53:12 +00004441 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 break;
4443 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004444
4445 if (ret == NOTDONE)
4446 {
4447 /*
4448 * Must be a variable or function name.
4449 * Can also be a curly-braces kind of name: {expr}.
4450 */
4451 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004452 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004453 if (alias != NULL)
4454 s = alias;
4455
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004456 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 ret = FAIL;
4458 else
4459 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004460 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4461
Bram Moolenaar4525a572022-02-13 11:57:33 +00004462 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004463 {
4464 emsg(_(e_cannot_use_underscore_here));
4465 ret = FAIL;
4466 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004467 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004468 && s[0] == 's' && s[1] == ':')
4469 {
4470 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4471 ret = FAIL;
4472 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004473 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004474 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004475 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004476 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004477 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004478 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004479 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004480 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004481 // get the value of "true", "false", etc. or a variable
4482 ret = FAIL;
4483 if (vim9script)
4484 ret = handle_predefined(s, len, rettv);
4485 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004486 {
4487 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004488 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004489 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004490 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004491 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004492 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004493 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004494 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004495 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004496 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004498 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004499 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004500 }
4501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004502 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4503 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004504 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004505 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 /*
4508 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4509 */
4510 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004511 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004512
4513 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004514 return ret;
4515}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004516
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004517/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004518 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004519 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004520 * Adjusts "end_leaderp" until it is at "start_leader".
4521 */
4522 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004523eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004524 typval_T *rettv,
4525 int numeric_only,
4526 char_u *start_leader,
4527 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004528{
4529 char_u *end_leader = *end_leaderp;
4530 int ret = OK;
4531 int error = FALSE;
4532 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004533 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004534 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004535 float_T f = 0.0;
4536
4537 if (rettv->v_type == VAR_FLOAT)
4538 f = rettv->vval.v_float;
4539 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004540 {
4541 while (VIM_ISWHITE(end_leader[-1]))
4542 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004543 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004544 val = tv2bool(rettv);
4545 else
4546 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004547 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004548 if (error)
4549 {
4550 clear_tv(rettv);
4551 ret = FAIL;
4552 }
4553 else
4554 {
4555 while (end_leader > start_leader)
4556 {
4557 --end_leader;
4558 if (*end_leader == '!')
4559 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004560 if (numeric_only)
4561 {
4562 ++end_leader;
4563 break;
4564 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004565 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004566 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004567 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004568 {
4569 rettv->v_type = VAR_BOOL;
4570 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4571 }
4572 else
4573 f = !f;
4574 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004575 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004576 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004577 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004578 type = VAR_BOOL;
4579 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004580 }
4581 else if (*end_leader == '-')
4582 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004583 if (rettv->v_type == VAR_FLOAT)
4584 f = -f;
4585 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004586 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004587 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004588 type = VAR_NUMBER;
4589 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004590 }
4591 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004592 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004594 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004595 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004597 else
4598 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004599 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004600 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004601 rettv->v_type = type;
4602 else
4603 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004604 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004607 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 return ret;
4609}
4610
4611/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004612 * Call the function referred to in "rettv".
4613 */
4614 static int
4615call_func_rettv(
4616 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004617 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004618 typval_T *rettv,
4619 int evaluate,
4620 dict_T *selfdict,
4621 typval_T *basetv)
4622{
4623 partial_T *pt = NULL;
4624 funcexe_T funcexe;
4625 typval_T functv;
4626 char_u *s;
4627 int ret;
4628
4629 // need to copy the funcref so that we can clear rettv
4630 if (evaluate)
4631 {
4632 functv = *rettv;
4633 rettv->v_type = VAR_UNKNOWN;
4634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004635 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004636 if (functv.v_type == VAR_PARTIAL)
4637 {
4638 pt = functv.vval.v_partial;
4639 s = partial_name(pt);
4640 }
4641 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004642 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004643 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004644 if (s == NULL || *s == NUL)
4645 {
4646 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004647 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004648 goto theend;
4649 }
4650 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004651 }
4652 else
4653 s = (char_u *)"";
4654
Bram Moolenaara80faa82020-04-12 19:37:17 +02004655 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004656 funcexe.fe_firstline = curwin->w_cursor.lnum;
4657 funcexe.fe_lastline = curwin->w_cursor.lnum;
4658 funcexe.fe_evaluate = evaluate;
4659 funcexe.fe_partial = pt;
4660 funcexe.fe_selfdict = selfdict;
4661 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004662 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004663
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004664theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004665 // Clear the funcref afterwards, so that deleting it while
4666 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004667 if (evaluate)
4668 clear_tv(&functv);
4669
4670 return ret;
4671}
4672
4673/*
4674 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004675 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004676 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4677 */
4678 static int
4679eval_lambda(
4680 char_u **arg,
4681 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004682 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004683 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004684{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004685 int evaluate = evalarg != NULL
4686 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004687 typval_T base = *rettv;
4688 int ret;
4689
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004690 rettv->v_type = VAR_UNKNOWN;
4691
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004692 if (**arg == '{')
4693 {
4694 // ->{lambda}()
4695 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4696 }
4697 else
4698 {
4699 // ->(lambda)()
4700 ++*arg;
4701 ret = eval1(arg, rettv, evalarg);
4702 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004703 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004704 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004705 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004706 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004707 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004708 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4709 && rettv->v_type != VAR_PARTIAL)
4710 {
4711 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4712 return FAIL;
4713 }
4714 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004715 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004716 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004717 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004718
4719 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004720 {
4721 if (verbose)
4722 {
4723 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004724 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004725 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004726 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004727 }
4728 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004729 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004730 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004731 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004732 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004733
4734 // Clear the funcref afterwards, so that deleting it while
4735 // evaluating the arguments is possible (see test55).
4736 if (evaluate)
4737 clear_tv(&base);
4738
4739 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004740}
4741
4742/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004743 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004744 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004745 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4746 */
4747 static int
4748eval_method(
4749 char_u **arg,
4750 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004751 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004752 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004753{
4754 char_u *name;
4755 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004756 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004757 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004758 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004759 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004760 int evaluate = evalarg != NULL
4761 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004762
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004763 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004764
Bram Moolenaarac92e252019-08-03 21:58:38 +02004765 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004766 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004767 if (alias != NULL)
4768 name = alias;
4769
4770 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004771 {
4772 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004773 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004774 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004775 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004776 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004777 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004778 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004779
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004780 // If there is no "(" immediately following, but there is further on,
4781 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4782 // Does not handle anything where "(" is part of the expression.
4783 *arg = skipwhite(*arg);
4784
4785 if (**arg != '(' && alias == NULL
4786 && (paren = vim_strchr(*arg, '(')) != NULL)
4787 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004788 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004789
4790 // Truncate the name a the "(". Avoid trying to get another line
4791 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004792 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004793 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4794 if (evalarg != NULL)
4795 {
4796 getline = evalarg->eval_getline;
4797 evalarg->eval_getline = NULL;
4798 }
4799
4800 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004801 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004802 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004803 *arg = name + len;
4804 ret = FAIL;
4805 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004806 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004807 {
4808 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004809 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004810 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004811
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004812 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004813 if (getline != NULL)
4814 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004815 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004816
4817 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004818 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004819 *arg = skipwhite(*arg);
4820
4821 if (**arg != '(')
4822 {
4823 if (verbose)
4824 semsg(_(e_missing_parenthesis_str), name);
4825 ret = FAIL;
4826 }
4827 else if (VIM_ISWHITE((*arg)[-1]))
4828 {
4829 if (verbose)
4830 emsg(_(e_no_white_space_allowed_before_parenthesis));
4831 ret = FAIL;
4832 }
4833 else
4834 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004835 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004836 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004837 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004838
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004839 // Clear the funcref afterwards, so that deleting it while
4840 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004841 if (evaluate)
4842 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004843 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004844
Bram Moolenaarac92e252019-08-03 21:58:38 +02004845 return ret;
4846}
4847
4848/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004849 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4850 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004851 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4852 */
4853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004854eval_index(
4855 char_u **arg,
4856 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004857 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004858 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004860 int evaluate = evalarg != NULL
4861 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004863 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004864 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004865 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004866 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004867 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004869 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4870 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004872 init_tv(&var1);
4873 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004874 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004876 /*
4877 * dict.name
4878 */
4879 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004880 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004881 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004882 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004884 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004885 }
4886 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004888 /*
4889 * something[idx]
4890 *
4891 * Get the (first) variable from inside the [].
4892 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004893 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004894 if (**arg == ':')
4895 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004896 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004897 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004898 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004899 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004900 semsg(_(e_white_space_required_before_and_after_str_at_str),
4901 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004902 clear_tv(&var1);
4903 return FAIL;
4904 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004905 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004906 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004907 int error = FALSE;
4908
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004909 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004910 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004911 && var1.v_type == VAR_FLOAT)
4912 {
4913 var1.vval.v_string = typval_tostring(&var1, TRUE);
4914 var1.v_type = VAR_STRING;
4915 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004916
Bram Moolenaar4525a572022-02-13 11:57:33 +00004917 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004918 tv_get_number_chk(&var1, &error);
4919 else
4920 error = tv_get_string_chk(&var1) == NULL;
4921 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004922 {
4923 // not a number or string
4924 clear_tv(&var1);
4925 return FAIL;
4926 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004927 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004928
4929 /*
4930 * Get the second variable from inside the [:].
4931 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004932 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004933 if (**arg == ':')
4934 {
4935 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004936 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004937 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004938 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004939 semsg(_(e_white_space_required_before_and_after_str_at_str),
4940 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004941 if (!empty1)
4942 clear_tv(&var1);
4943 return FAIL;
4944 }
4945 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004946 if (**arg == ']')
4947 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004948 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004950 if (!empty1)
4951 clear_tv(&var1);
4952 return FAIL;
4953 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004954 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004956 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004957 if (!empty1)
4958 clear_tv(&var1);
4959 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 return FAIL;
4961 }
4962 }
4963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004964 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004965 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 if (**arg != ']')
4967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004968 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004969 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 clear_tv(&var1);
4971 if (range)
4972 clear_tv(&var2);
4973 return FAIL;
4974 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004975 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004976 }
4977
4978 if (evaluate)
4979 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004980 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004981 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004982 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004983
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004984 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004985 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004986 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004987 clear_tv(&var2);
4988 return res;
4989 }
4990 return OK;
4991}
4992
4993/*
4994 * Check if "rettv" can have an [index] or [sli:ce]
4995 */
4996 int
4997check_can_index(typval_T *rettv, int evaluate, int verbose)
4998{
4999 switch (rettv->v_type)
5000 {
5001 case VAR_FUNC:
5002 case VAR_PARTIAL:
5003 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005004 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005005 return FAIL;
5006 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005007 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005008 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005009 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005010 case VAR_BOOL:
5011 case VAR_SPECIAL:
5012 case VAR_JOB:
5013 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005014 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005015 case VAR_CLASS:
5016 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005017 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005018 if (verbose)
5019 emsg(_(e_cannot_index_special_variable));
5020 return FAIL;
5021 case VAR_UNKNOWN:
5022 case VAR_ANY:
5023 case VAR_VOID:
5024 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005025 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005026 emsg(_(e_cannot_index_special_variable));
5027 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005028 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005029 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005030
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005031 case VAR_STRING:
5032 case VAR_LIST:
5033 case VAR_DICT:
5034 case VAR_BLOB:
5035 break;
5036 case VAR_NUMBER:
5037 if (in_vim9script())
5038 emsg(_(e_cannot_index_number));
5039 break;
5040 }
5041 return OK;
5042}
5043
5044/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005045 * slice() function
5046 */
5047 void
5048f_slice(typval_T *argvars, typval_T *rettv)
5049{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005050 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005051 && ((argvars[0].v_type != VAR_STRING
5052 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005053 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005054 && check_for_list_arg(argvars, 0) == FAIL)
5055 || check_for_number_arg(argvars, 1) == FAIL
5056 || check_for_opt_number_arg(argvars, 2) == FAIL))
5057 return;
5058
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005059 if (check_can_index(argvars, TRUE, FALSE) != OK)
5060 return;
5061
5062 copy_tv(argvars, rettv);
5063 eval_index_inner(rettv, TRUE, argvars + 1,
5064 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5065 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005066}
5067
5068/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005069 * Apply index or range to "rettv".
5070 * "var1" is the first index, NULL for [:expr].
5071 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005072 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5073 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005074 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5075 */
5076 int
5077eval_index_inner(
5078 typval_T *rettv,
5079 int is_range,
5080 typval_T *var1,
5081 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005082 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005083 char_u *key,
5084 int keylen,
5085 int verbose)
5086{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005087 varnumber_T n1, n2 = 0;
5088 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005089
5090 n1 = 0;
5091 if (var1 != NULL && rettv->v_type != VAR_DICT)
5092 n1 = tv_get_number(var1);
5093
5094 if (is_range)
5095 {
5096 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005097 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005098 if (verbose)
5099 emsg(_(e_cannot_slice_dictionary));
5100 return FAIL;
5101 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005102 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005103 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005104 else
5105 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005106 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005107
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005108 switch (rettv->v_type)
5109 {
5110 case VAR_UNKNOWN:
5111 case VAR_ANY:
5112 case VAR_VOID:
5113 case VAR_FUNC:
5114 case VAR_PARTIAL:
5115 case VAR_FLOAT:
5116 case VAR_BOOL:
5117 case VAR_SPECIAL:
5118 case VAR_JOB:
5119 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005120 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005121 case VAR_CLASS:
5122 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005123 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005124 break; // not evaluating, skipping over subscript
5125
5126 case VAR_NUMBER:
5127 case VAR_STRING:
5128 {
5129 char_u *s = tv_get_string(rettv);
5130
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005132 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005133 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005134 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005135 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005136 else
5137 s = char_from_string(s, n1);
5138 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005139 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005140 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005141 // The resulting variable is a substring. If the indexes
5142 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 if (n1 < 0)
5144 {
5145 n1 = len + n1;
5146 if (n1 < 0)
5147 n1 = 0;
5148 }
5149 if (n2 < 0)
5150 n2 = len + n2;
5151 else if (n2 >= len)
5152 n2 = len;
5153 if (n1 >= len || n2 < 0 || n1 > n2)
5154 s = NULL;
5155 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005156 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 }
5158 else
5159 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005160 // The resulting variable is a string of a single
5161 // character. If the index is too big or negative the
5162 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005163 if (n1 >= len || n1 < 0)
5164 s = NULL;
5165 else
5166 s = vim_strnsave(s + n1, 1);
5167 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 clear_tv(rettv);
5169 rettv->v_type = VAR_STRING;
5170 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005171 }
5172 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005174 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005175 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5176 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005177 break;
5178
5179 case VAR_LIST:
5180 if (var1 == NULL)
5181 n1 = 0;
5182 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005183 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005184 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005185 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005186 return FAIL;
5187 break;
5188
5189 case VAR_DICT:
5190 {
5191 dictitem_T *item;
5192 typval_T tmp;
5193
5194 if (key == NULL)
5195 {
5196 key = tv_get_string_chk(var1);
5197 if (key == NULL)
5198 return FAIL;
5199 }
5200
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005201 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005202
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005203 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005204 {
5205 if (verbose)
5206 {
5207 if (keylen > 0)
5208 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005209 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005210 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005211 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005212 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005213
5214 copy_tv(&item->di_tv, &tmp);
5215 clear_tv(rettv);
5216 *rettv = tmp;
5217 }
5218 break;
5219 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005220 return OK;
5221}
5222
5223/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005224 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005225 */
5226 char_u *
5227partial_name(partial_T *pt)
5228{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005229 if (pt != NULL)
5230 {
5231 if (pt->pt_name != NULL)
5232 return pt->pt_name;
5233 if (pt->pt_func != NULL)
5234 return pt->pt_func->uf_name;
5235 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005236 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005237}
5238
Bram Moolenaarddecc252016-04-06 22:59:37 +02005239 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005240partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005241{
5242 int i;
5243
5244 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005245 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005246 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005247 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005248 if (pt->pt_name != NULL)
5249 {
5250 func_unref(pt->pt_name);
5251 vim_free(pt->pt_name);
5252 }
5253 else
5254 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005255 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005256
Bram Moolenaar54656012021-06-09 20:50:46 +02005257 // "out_up" is no longer used, decrement refcount on partial that owns it.
5258 partial_unref(pt->pt_outer.out_up_partial);
5259
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005260 // Using pt_outer from another partial.
5261 partial_unref(pt->pt_outer_partial);
5262
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005263 // Decrease the reference count for the context of a closure. If down
5264 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005265 if (pt->pt_funcstack != NULL)
5266 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005267 --pt->pt_funcstack->fs_refcount;
5268 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005269 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005270 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005271 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5272 if (pt->pt_loopvars[i] != NULL)
5273 {
5274 --pt->pt_loopvars[i]->lvs_refcount;
5275 loopvars_check_refcount(pt->pt_loopvars[i]);
5276 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005277
Bram Moolenaarddecc252016-04-06 22:59:37 +02005278 vim_free(pt);
5279}
5280
5281/*
5282 * Unreference a closure: decrement the reference count and free it when it
5283 * becomes zero.
5284 */
5285 void
5286partial_unref(partial_T *pt)
5287{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005288 if (pt == NULL)
5289 return;
5290
5291 int done = FALSE;
5292
5293 if (--pt->pt_refcount <= 0)
5294 partial_free(pt);
5295
5296 // If the reference count goes down to one, the funcstack may be the
5297 // only reference and can be freed if no other partials reference it.
5298 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005299 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005300 // careful: if the funcstack is freed it may contain this partial
5301 // and it gets freed as well
5302 if (pt->pt_funcstack != NULL)
5303 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005304
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005305 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005306 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005307 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005308
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005309 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5310 if (pt->pt_loopvars[depth] != NULL
5311 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005312 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005313 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005314 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005315}
5316
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005317/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005318 * Return the next (unique) copy ID.
5319 * Used for serializing nested structures.
5320 */
5321 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005322get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005323{
5324 current_copyID += COPYID_INC;
5325 return current_copyID;
5326}
5327
5328/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005329 * Garbage collection for lists and dictionaries.
5330 *
5331 * We use reference counts to be able to free most items right away when they
5332 * are no longer used. But for composite items it's possible that it becomes
5333 * unused while the reference count is > 0: When there is a recursive
5334 * reference. Example:
5335 * :let l = [1, 2, 3]
5336 * :let d = {9: l}
5337 * :let l[1] = d
5338 *
5339 * Since this is quite unusual we handle this with garbage collection: every
5340 * once in a while find out which lists and dicts are not referenced from any
5341 * variable.
5342 *
5343 * Here is a good reference text about garbage collection (refers to Python
5344 * but it applies to all reference-counting mechanisms):
5345 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005346 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005347
5348/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005349 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005350 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005351 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005352 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005353 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005354garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005355{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005356 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005357 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005358 buf_T *buf;
5359 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005360 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005361 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005362
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005363 if (!testing)
5364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005365 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005366 want_garbage_collect = FALSE;
5367 may_garbage_collect = FALSE;
5368 garbage_collect_at_exit = FALSE;
5369 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005370
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005371 // The execution stack can grow big, limit the size.
5372 if (exestack.ga_maxlen - exestack.ga_len > 500)
5373 {
5374 size_t new_len;
5375 char_u *pp;
5376 int n;
5377
5378 // Keep 150% of the current size, with a minimum of the growth size.
5379 n = exestack.ga_len / 2;
5380 if (n < exestack.ga_growsize)
5381 n = exestack.ga_growsize;
5382
5383 // Don't make it bigger though.
5384 if (exestack.ga_len + n < exestack.ga_maxlen)
5385 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005386 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005387 pp = vim_realloc(exestack.ga_data, new_len);
5388 if (pp == NULL)
5389 return FAIL;
5390 exestack.ga_maxlen = exestack.ga_len + n;
5391 exestack.ga_data = pp;
5392 }
5393 }
5394
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005395 // We advance by two because we add one for items referenced through
5396 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005397 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005398
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005399 /*
5400 * 1. Go through all accessible variables and mark all lists and dicts
5401 * with copyID.
5402 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 // Don't free variables in the previous_funccal list unless they are only
5405 // referenced through previous_funccal. This must be first, because if
5406 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005407 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005409 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005410 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005411
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005412 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005413 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005414 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5415 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005417 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005418 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005419 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5420 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005421 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005422 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005423 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005424 abort = abort || set_ref_in_item(
5425 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005426#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005427 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005428 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5429 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005430 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005431 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005432 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5433 NULL, NULL);
5434#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005437 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005438 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5439 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005441 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005442
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005444 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005445
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005446 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005447 abort = abort || set_ref_in_functions(copyID);
5448
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005450 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005451
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005452 // funcstacks keep variables for closures
5453 abort = abort || set_ref_in_funcstacks(copyID);
5454
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005455 // loopvars keep variables for loop blocks
5456 abort = abort || set_ref_in_loopvars(copyID);
5457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005459 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005460
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005461 // callbacks in buffers
5462 abort = abort || set_ref_in_buffers(copyID);
5463
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005464 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5465 abort = abort || set_ref_in_insexpand_funcs(copyID);
5466
5467 // 'operatorfunc' callback
5468 abort = abort || set_ref_in_opfunc(copyID);
5469
5470 // 'tagfunc' callback
5471 abort = abort || set_ref_in_tagfunc(copyID);
5472
5473 // 'imactivatefunc' and 'imstatusfunc' callbacks
5474 abort = abort || set_ref_in_im_funcs(copyID);
5475
Bram Moolenaar1dced572012-04-05 16:54:08 +02005476#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005477 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005478#endif
5479
Bram Moolenaardb913952012-06-29 12:54:53 +02005480#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005481 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005482#endif
5483
5484#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005486#endif
5487
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005488#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005489 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005490 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005491#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005492#ifdef FEAT_NETBEANS_INTG
5493 abort = abort || set_ref_in_nb_channel(copyID);
5494#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005495
Bram Moolenaare3188e22016-05-31 21:13:04 +02005496#ifdef FEAT_TIMERS
5497 abort = abort || set_ref_in_timer(copyID);
5498#endif
5499
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005500#ifdef FEAT_QUICKFIX
5501 abort = abort || set_ref_in_quickfix(copyID);
5502#endif
5503
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005504#ifdef FEAT_TERMINAL
5505 abort = abort || set_ref_in_term(copyID);
5506#endif
5507
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005508#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005509 abort = abort || set_ref_in_popups(copyID);
5510#endif
5511
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005512 abort = abort || set_ref_in_classes(copyID);
5513
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005514 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005515 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005516 /*
5517 * 2. Free lists and dictionaries that are not referenced.
5518 */
5519 did_free = free_unref_items(copyID);
5520
5521 /*
5522 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005524 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005525 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005526 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005527 else if (p_verbose > 0)
5528 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005529 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005530 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005531
5532 return did_free;
5533}
5534
5535/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005536 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005537 */
5538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005539free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005540{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005541 int did_free = FALSE;
5542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 // Let all "free" functions know that we are here. This means no
5544 // dictionaries, lists, channels or jobs are to be freed, because we will
5545 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005546 in_free_unref_items = TRUE;
5547
5548 /*
5549 * PASS 1: free the contents of the items. We don't free the items
5550 * themselves yet, so that it is possible to decrement refcount counters
5551 */
5552
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005553 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005554 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005555
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005556 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005557 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005558
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005559 // Go through the list of objects and free items without this copyID.
5560 did_free |= object_free_nonref(copyID);
5561
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005562 // Go through the list of classes and free items without this copyID.
5563 did_free |= class_free_nonref(copyID);
5564
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005565#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005566 // Go through the list of jobs and free items without the copyID. This
5567 // must happen before doing channels, because jobs refer to channels, but
5568 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005569 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5570
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005571 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005572 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5573#endif
5574
5575 /*
5576 * PASS 2: free the items themselves.
5577 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005578 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005579 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005580 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005581
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005582#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005583 // Go through the list of jobs and free items without the copyID. This
5584 // must happen before doing channels, because jobs refer to channels, but
5585 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005586 free_unused_jobs(copyID, COPYID_MASK);
5587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005588 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005589 free_unused_channels(copyID, COPYID_MASK);
5590#endif
5591
5592 in_free_unref_items = FALSE;
5593
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005594 return did_free;
5595}
5596
5597/*
5598 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005599 * "list_stack" is used to add lists to be marked. Can be NULL.
5600 *
5601 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005602 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005604set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005605{
5606 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005607 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005608 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005609 hashtab_T *cur_ht;
5610 ht_stack_T *ht_stack = NULL;
5611 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005612
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005613 cur_ht = ht;
5614 for (;;)
5615 {
5616 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005617 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005618 // Mark each item in the hashtab. If the item contains a hashtab
5619 // it is added to ht_stack, if it contains a list it is added to
5620 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005621 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005622 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005623 if (!HASHITEM_EMPTY(hi))
5624 {
5625 --todo;
5626 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5627 &ht_stack, list_stack);
5628 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005629 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005630
5631 if (ht_stack == NULL)
5632 break;
5633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005634 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005635 cur_ht = ht_stack->ht;
5636 tempitem = ht_stack;
5637 ht_stack = ht_stack->prev;
5638 free(tempitem);
5639 }
5640
5641 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005642}
5643
Dominique Pelle748b3082022-01-08 12:41:16 +00005644#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5645 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005646/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005647 * Mark a dict and its items with "copyID".
5648 * Returns TRUE if setting references failed somehow.
5649 */
5650 int
5651set_ref_in_dict(dict_T *d, int copyID)
5652{
5653 if (d != NULL && d->dv_copyID != copyID)
5654 {
5655 d->dv_copyID = copyID;
5656 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5657 }
5658 return FALSE;
5659}
Dominique Pelle748b3082022-01-08 12:41:16 +00005660#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005661
5662/*
5663 * Mark a list and its items with "copyID".
5664 * Returns TRUE if setting references failed somehow.
5665 */
5666 int
5667set_ref_in_list(list_T *ll, int copyID)
5668{
5669 if (ll != NULL && ll->lv_copyID != copyID)
5670 {
5671 ll->lv_copyID = copyID;
5672 return set_ref_in_list_items(ll, copyID, NULL);
5673 }
5674 return FALSE;
5675}
5676
5677/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005678 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005679 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5680 *
5681 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005682 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005683 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005684set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005685{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005686 listitem_T *li;
5687 int abort = FALSE;
5688 list_T *cur_l;
5689 list_stack_T *list_stack = NULL;
5690 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005691
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005692 cur_l = l;
5693 for (;;)
5694 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005695 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005696 // Mark each item in the list. If the item contains a hashtab
5697 // it is added to ht_stack, if it contains a list it is added to
5698 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005699 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5700 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5701 ht_stack, &list_stack);
5702 if (list_stack == NULL)
5703 break;
5704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005705 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005706 cur_l = list_stack->list;
5707 tempitem = list_stack;
5708 list_stack = list_stack->prev;
5709 free(tempitem);
5710 }
5711
5712 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005713}
5714
5715/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005716 * Mark the partial in callback 'cb' with "copyID".
5717 */
5718 int
5719set_ref_in_callback(callback_T *cb, int copyID)
5720{
5721 typval_T tv;
5722
5723 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5724 return FALSE;
5725
5726 tv.v_type = VAR_PARTIAL;
5727 tv.vval.v_partial = cb->cb_partial;
5728 return set_ref_in_item(&tv, copyID, NULL, NULL);
5729}
5730
5731/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005732 * Mark the dict "dd" with "copyID".
5733 * Also see set_ref_in_item().
5734 */
5735 static int
5736set_ref_in_item_dict(
5737 dict_T *dd,
5738 int copyID,
5739 ht_stack_T **ht_stack,
5740 list_stack_T **list_stack)
5741{
5742 if (dd == NULL || dd->dv_copyID == copyID)
5743 return FALSE;
5744
5745 // Didn't see this dict yet.
5746 dd->dv_copyID = copyID;
5747 if (ht_stack == NULL)
5748 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5749
5750 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5751 if (newitem == NULL)
5752 return TRUE;
5753
5754 newitem->ht = &dd->dv_hashtab;
5755 newitem->prev = *ht_stack;
5756 *ht_stack = newitem;
5757
5758 return FALSE;
5759}
5760
5761/*
5762 * Mark the list "ll" with "copyID".
5763 * Also see set_ref_in_item().
5764 */
5765 static int
5766set_ref_in_item_list(
5767 list_T *ll,
5768 int copyID,
5769 ht_stack_T **ht_stack,
5770 list_stack_T **list_stack)
5771{
5772 if (ll == NULL || ll->lv_copyID == copyID)
5773 return FALSE;
5774
5775 // Didn't see this list yet.
5776 ll->lv_copyID = copyID;
5777 if (list_stack == NULL)
5778 return set_ref_in_list_items(ll, copyID, ht_stack);
5779
5780 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5781 if (newitem == NULL)
5782 return TRUE;
5783
5784 newitem->list = ll;
5785 newitem->prev = *list_stack;
5786 *list_stack = newitem;
5787
5788 return FALSE;
5789}
5790
5791/*
5792 * Mark the partial "pt" with "copyID".
5793 * Also see set_ref_in_item().
5794 */
5795 static int
5796set_ref_in_item_partial(
5797 partial_T *pt,
5798 int copyID,
5799 ht_stack_T **ht_stack,
5800 list_stack_T **list_stack)
5801{
5802 if (pt == NULL || pt->pt_copyID == copyID)
5803 return FALSE;
5804
5805 // Didn't see this partial yet.
5806 pt->pt_copyID = copyID;
5807
5808 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5809
5810 if (pt->pt_dict != NULL)
5811 {
5812 typval_T dtv;
5813
5814 dtv.v_type = VAR_DICT;
5815 dtv.vval.v_dict = pt->pt_dict;
5816 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5817 }
5818
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005819 if (pt->pt_obj != NULL)
5820 {
5821 typval_T objtv;
5822
5823 objtv.v_type = VAR_OBJECT;
5824 objtv.vval.v_object = pt->pt_obj;
5825 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5826 }
5827
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005828 for (int i = 0; i < pt->pt_argc; ++i)
5829 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5830 ht_stack, list_stack);
5831 // pt_funcstack is handled in set_ref_in_funcstacks()
5832 // pt_loopvars is handled in set_ref_in_loopvars()
5833
5834 return abort;
5835}
5836
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005837#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005838/*
5839 * Mark the job "pt" with "copyID".
5840 * Also see set_ref_in_item().
5841 */
5842 static int
5843set_ref_in_item_job(
5844 job_T *job,
5845 int copyID,
5846 ht_stack_T **ht_stack,
5847 list_stack_T **list_stack)
5848{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005849 typval_T dtv;
5850
5851 if (job == NULL || job->jv_copyID == copyID)
5852 return FALSE;
5853
5854 job->jv_copyID = copyID;
5855 if (job->jv_channel != NULL)
5856 {
5857 dtv.v_type = VAR_CHANNEL;
5858 dtv.vval.v_channel = job->jv_channel;
5859 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5860 }
5861 if (job->jv_exit_cb.cb_partial != NULL)
5862 {
5863 dtv.v_type = VAR_PARTIAL;
5864 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5865 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5866 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005867
5868 return FALSE;
5869}
5870
5871/*
5872 * Mark the channel "ch" with "copyID".
5873 * Also see set_ref_in_item().
5874 */
5875 static int
5876set_ref_in_item_channel(
5877 channel_T *ch,
5878 int copyID,
5879 ht_stack_T **ht_stack,
5880 list_stack_T **list_stack)
5881{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005882 typval_T dtv;
5883
5884 if (ch == NULL || ch->ch_copyID == copyID)
5885 return FALSE;
5886
5887 ch->ch_copyID = copyID;
5888 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5889 {
5890 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5891 jq != NULL; jq = jq->jq_next)
5892 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5893 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5894 cq = cq->cq_next)
5895 if (cq->cq_callback.cb_partial != NULL)
5896 {
5897 dtv.v_type = VAR_PARTIAL;
5898 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5899 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5900 }
5901 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5902 {
5903 dtv.v_type = VAR_PARTIAL;
5904 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5905 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5906 }
5907 }
5908 if (ch->ch_callback.cb_partial != NULL)
5909 {
5910 dtv.v_type = VAR_PARTIAL;
5911 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5912 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5913 }
5914 if (ch->ch_close_cb.cb_partial != NULL)
5915 {
5916 dtv.v_type = VAR_PARTIAL;
5917 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5918 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5919 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005920
5921 return FALSE;
5922}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005923#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005924
5925/*
5926 * Mark the class "cl" with "copyID".
5927 * Also see set_ref_in_item().
5928 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005929 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005930set_ref_in_item_class(
5931 class_T *cl,
5932 int copyID,
5933 ht_stack_T **ht_stack,
5934 list_stack_T **list_stack)
5935{
5936 int abort = FALSE;
5937
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005938 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005939 return FALSE;
5940
5941 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005942 if (cl->class_members_tv != NULL)
5943 {
5944 // The "class_members_tv" table is allocated only for regular classes
5945 // and not for interfaces.
5946 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5947 abort = abort || set_ref_in_item(
5948 &cl->class_members_tv[i],
5949 copyID, ht_stack, list_stack);
5950 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005951
5952 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5953 abort = abort || set_ref_in_func(NULL,
5954 cl->class_class_functions[i], copyID);
5955
5956 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5957 abort = abort || set_ref_in_func(NULL,
5958 cl->class_obj_methods[i], copyID);
5959
5960 return abort;
5961}
5962
5963/*
5964 * Mark the object "cl" with "copyID".
5965 * Also see set_ref_in_item().
5966 */
5967 static int
5968set_ref_in_item_object(
5969 object_T *obj,
5970 int copyID,
5971 ht_stack_T **ht_stack,
5972 list_stack_T **list_stack)
5973{
5974 int abort = FALSE;
5975
5976 if (obj == NULL || obj->obj_copyID == copyID)
5977 return FALSE;
5978
5979 obj->obj_copyID = copyID;
5980
5981 // The typval_T array is right after the object_T.
5982 typval_T *mtv = (typval_T *)(obj + 1);
5983 for (int i = 0; !abort
5984 && i < obj->obj_class->class_obj_member_count; ++i)
5985 abort = abort || set_ref_in_item(mtv + i, copyID,
5986 ht_stack, list_stack);
5987
5988 return abort;
5989}
5990
5991/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005992 * Mark all lists, dicts and other container types referenced through typval
5993 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005994 * "list_stack" is used to add lists to be marked. Can be NULL.
5995 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5996 *
5997 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005998 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005999 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006000set_ref_in_item(
6001 typval_T *tv,
6002 int copyID,
6003 ht_stack_T **ht_stack,
6004 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006005{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006006 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006007
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006008 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006009 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006010 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006011 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6012 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006013
6014 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006015 return set_ref_in_item_list(tv->vval.v_list, copyID,
6016 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006017
6018 case VAR_FUNC:
6019 {
6020 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6021 break;
6022 }
6023
6024 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006025 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6026 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006027
6028 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006029#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006030 return set_ref_in_item_job(tv->vval.v_job, copyID,
6031 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006032#else
6033 break;
6034#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006035
6036 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006037#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006038 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006039 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006040#else
6041 break;
6042#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006043
6044 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006045 return set_ref_in_item_class(tv->vval.v_class, copyID,
6046 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006047
6048 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006049 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006050 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006051
6052 case VAR_UNKNOWN:
6053 case VAR_ANY:
6054 case VAR_VOID:
6055 case VAR_BOOL:
6056 case VAR_SPECIAL:
6057 case VAR_NUMBER:
6058 case VAR_FLOAT:
6059 case VAR_STRING:
6060 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006061 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006062 case VAR_INSTR:
6063 // Types that do not contain any other item
6064 break;
6065 }
6066
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006067 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006068}
6069
Bram Moolenaar8c711452005-01-14 21:53:12 +00006070/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 * Return a string with the string representation of a variable.
6072 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006073 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006074 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006075 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006076 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006077 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006078 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6079 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006080 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006082 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006083echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006084 typval_T *tv,
6085 char_u **tofree,
6086 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006087 int copyID,
6088 int echo_style,
6089 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006090 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006092 static int recurse = 0;
6093 char_u *r = NULL;
6094
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006096 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006097 if (!did_echo_string_emsg)
6098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006099 // Only give this message once for a recursive call to avoid
6100 // flooding the user with errors. And stop iterating over lists
6101 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006102 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006103 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006104 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006105 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006106 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107 }
6108 ++recurse;
6109
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006110 switch (tv->v_type)
6111 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006112 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006113 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006114 {
6115 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006116 r = tv->vval.v_string;
6117 if (r == NULL)
6118 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006119 }
6120 else
6121 {
6122 *tofree = string_quote(tv->vval.v_string, FALSE);
6123 r = *tofree;
6124 }
6125 break;
6126
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006127 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006128 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006129 char_u buf[MAX_FUNC_NAME_LEN];
6130
6131 if (echo_style)
6132 {
LemonBoya5d35902022-04-29 21:15:02 +01006133 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6134 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006135 buf, MAX_FUNC_NAME_LEN);
6136 if (r == buf)
6137 {
6138 r = vim_strsave(buf);
6139 *tofree = r;
6140 }
6141 else
6142 *tofree = NULL;
6143 }
6144 else
6145 {
6146 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6147 : make_ufunc_name_readable(
6148 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6149 TRUE);
6150 r = *tofree;
6151 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006152 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006153 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006154
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006155 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006156 {
6157 partial_T *pt = tv->vval.v_partial;
6158 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006159 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006160 garray_T ga;
6161 int i;
6162 char_u *tf;
6163
6164 ga_init2(&ga, 1, 100);
6165 ga_concat(&ga, (char_u *)"function(");
6166 if (fname != NULL)
6167 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006168 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006169 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006170 && vim_isupper(fname[1]))
6171 {
6172 ga_concat(&ga, (char_u *)"'g:");
6173 ga_concat(&ga, fname + 1);
6174 }
6175 else
6176 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006177 vim_free(fname);
6178 }
6179 if (pt != NULL && pt->pt_argc > 0)
6180 {
6181 ga_concat(&ga, (char_u *)", [");
6182 for (i = 0; i < pt->pt_argc; ++i)
6183 {
6184 if (i > 0)
6185 ga_concat(&ga, (char_u *)", ");
6186 ga_concat(&ga,
6187 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6188 vim_free(tf);
6189 }
6190 ga_concat(&ga, (char_u *)"]");
6191 }
6192 if (pt != NULL && pt->pt_dict != NULL)
6193 {
6194 typval_T dtv;
6195
6196 ga_concat(&ga, (char_u *)", ");
6197 dtv.v_type = VAR_DICT;
6198 dtv.vval.v_dict = pt->pt_dict;
6199 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6200 vim_free(tf);
6201 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006202 // terminate with ')' and a NUL
6203 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006204
6205 *tofree = ga.ga_data;
6206 r = *tofree;
6207 break;
6208 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006209
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006210 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006211 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006212 break;
6213
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006214 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006215 if (tv->vval.v_list == NULL)
6216 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006217 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006219 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006220 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006221 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6222 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006223 {
6224 *tofree = NULL;
6225 r = (char_u *)"[...]";
6226 }
6227 else
6228 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006229 int old_copyID = tv->vval.v_list->lv_copyID;
6230
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006231 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006232 *tofree = list2string(tv, copyID, restore_copyID);
6233 if (restore_copyID)
6234 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006235 r = *tofree;
6236 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006237 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006238
Bram Moolenaar8c711452005-01-14 21:53:12 +00006239 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006240 if (tv->vval.v_dict == NULL)
6241 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006242 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006243 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006244 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006245 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006246 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6247 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006248 {
6249 *tofree = NULL;
6250 r = (char_u *)"{...}";
6251 }
6252 else
6253 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006254 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006255
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006256 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006257 *tofree = dict2string(tv, copyID, restore_copyID);
6258 if (restore_copyID)
6259 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006260 r = *tofree;
6261 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006262 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006263
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006264 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006265 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006266 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006268 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006269 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006270 break;
6271
Bram Moolenaar835dc632016-02-07 14:27:38 +01006272 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006273 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006274#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006275 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006276 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6277 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006278 if (composite_val)
6279 {
6280 *tofree = string_quote(r, FALSE);
6281 r = *tofree;
6282 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006283#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006284 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006285
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006286 case VAR_INSTR:
6287 *tofree = NULL;
6288 r = (char_u *)"instructions";
6289 break;
6290
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006291 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006292 {
6293 class_T *cl = tv->vval.v_class;
6294 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6295 r = *tofree = alloc(len);
6296 vim_snprintf((char *)r, len, "class %s",
6297 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6298 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006299 break;
6300
6301 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006302 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006303 garray_T ga;
6304 ga_init2(&ga, 1, 50);
6305 ga_concat(&ga, (char_u *)"object of ");
6306 object_T *obj = tv->vval.v_object;
6307 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6308 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6309 : cl->class_name);
6310 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006311 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006312 ga_concat(&ga, (char_u *)" {");
6313 for (int i = 0; i < cl->class_obj_member_count; ++i)
6314 {
6315 if (i > 0)
6316 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006317 ocmember_T *m = &cl->class_obj_members[i];
6318 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006319 ga_concat(&ga, (char_u *)": ");
6320 char_u *tf = NULL;
6321 ga_concat(&ga, echo_string_core(
6322 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006323 &tf, numbuf, copyID, echo_style,
6324 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006325 vim_free(tf);
6326 }
6327 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006328 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006329
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006330 *tofree = r = ga.ga_data;
6331 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006332 break;
6333
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006334 case VAR_FLOAT:
6335 *tofree = NULL;
6336 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6337 r = numbuf;
6338 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006339
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006340 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006341 case VAR_SPECIAL:
6342 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006343 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006344 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006345
6346 case VAR_TYPEALIAS:
6347 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6348 r = *tofree;
6349 if (r == NULL)
6350 r = (char_u *)"";
6351 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006352 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006353
Bram Moolenaar8502c702014-06-17 12:51:16 +02006354 if (--recurse == 0)
6355 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006356 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006357}
6358
6359/*
6360 * Return a string with the string representation of a variable.
6361 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6362 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006363 * Does not put quotes around strings, as ":echo" displays values.
6364 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6365 * May return NULL.
6366 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006367 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006368echo_string(
6369 typval_T *tv,
6370 char_u **tofree,
6371 char_u *numbuf,
6372 int copyID)
6373{
6374 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6375}
6376
6377/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006378 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6379 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006380 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006381 */
6382 int
6383buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6384{
6385 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006386 char_u *t;
6387 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006388
6389 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6390 return -1;
6391
6392 if (lnum > buf->b_ml.ml_line_count)
6393 lnum = buf->b_ml.ml_line_count;
6394
6395 str = ml_get_buf(buf, lnum, FALSE);
6396 if (str == NULL)
6397 return -1;
6398
6399 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006400 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006401
Bram Moolenaar91458462021-01-13 20:08:38 +01006402 // count the number of characters
6403 t = str;
6404 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6405 t += mb_ptr2len(t);
6406
6407 // In insert mode, when the cursor is at the end of a non-empty line,
6408 // byteidx points to the NUL character immediately past the end of the
6409 // string. In this case, add one to the character count.
6410 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6411 count++;
6412
6413 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006414}
6415
6416/*
6417 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006418 * byte index. Works only for loaded buffers. Returns -1 on failure.
6419 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006420 */
6421 int
6422buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6423{
6424 char_u *str;
6425 char_u *t;
6426
6427 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6428 return -1;
6429
6430 if (lnum > buf->b_ml.ml_line_count)
6431 lnum = buf->b_ml.ml_line_count;
6432
6433 str = ml_get_buf(buf, lnum, FALSE);
6434 if (str == NULL)
6435 return -1;
6436
6437 // Convert the character offset to a byte offset
6438 t = str;
6439 while (*t != NUL && --charidx > 0)
6440 t += mb_ptr2len(t);
6441
Bram Moolenaar91458462021-01-13 20:08:38 +01006442 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006443}
6444
6445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006447 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006449 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006450var2fpos(
6451 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006452 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006453 int *fnum, // set to fnum for '0, 'A, etc.
6454 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006456 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006458 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006460 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006461 if (varp->v_type == VAR_LIST)
6462 {
6463 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006464 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006465 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006466 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006467
6468 l = varp->vval.v_list;
6469 if (l == NULL)
6470 return NULL;
6471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006472 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006473 pos.lnum = list_find_nr(l, 0L, &error);
6474 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006475 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006476 if (charcol)
6477 len = (long)mb_charlen(ml_get(pos.lnum));
6478 else
6479 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006480
Bram Moolenaarec65d772020-08-20 22:29:12 +02006481 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006483 li = list_find(l, 1L);
6484 if (li != NULL && li->li_tv.v_type == VAR_STRING
6485 && li->li_tv.vval.v_string != NULL
6486 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006487 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006488 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006489 }
6490 else
6491 {
6492 pos.col = list_find_nr(l, 1L, &error);
6493 if (error)
6494 return NULL;
6495 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006497 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006498 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006499 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006500 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006502 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006503 pos.coladd = list_find_nr(l, 2L, &error);
6504 if (error)
6505 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006506
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006507 return &pos;
6508 }
6509
Bram Moolenaarc5809432021-03-27 21:23:30 +01006510 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6511 return NULL;
6512
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006513 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006514 if (name == NULL)
6515 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006516
6517 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006518 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006519 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006520 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006521 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006522 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006523 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006524 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006525 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006526 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006527 pos = VIsual;
6528 else
6529 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006530 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006531 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006532 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006534 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006535 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6537 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006538 pos = *pp;
6539 }
6540 if (pos.lnum != 0)
6541 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006542 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006543 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6544 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006546
Bram Moolenaara5525202006-03-02 22:52:09 +00006547 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006548
Bram Moolenaar477933c2007-07-17 14:32:23 +00006549 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006550 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006551 // the "w_valid" flags are not reset when moving the cursor, but they
6552 // do matter for update_topline() and validate_botline().
6553 check_cursor_moved(curwin);
6554
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006555 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006556 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006557 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006558 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006559 // In silent Ex mode topline is zero, but that's not a valid line
6560 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006561 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006562 return &pos;
6563 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006564 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006565 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006566 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006567 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006568 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006569 return &pos;
6570 }
6571 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006572 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006574 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 {
6576 pos.lnum = curbuf->b_ml.ml_line_count;
6577 pos.col = 0;
6578 }
6579 else
6580 {
6581 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006582 if (charcol)
6583 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6584 else
6585 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 }
6587 return &pos;
6588 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006589 if (in_vim9script())
6590 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 return NULL;
6592}
6593
6594/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006595 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006596 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006597 * Note that the column is passed on as-is, the caller may want to decrement
6598 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006599 * If "charcol" is TRUE use the column as the character index instead of the
6600 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006601 * Return FAIL when conversion is not possible, doesn't check the position for
6602 * validity.
6603 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006605list2fpos(
6606 typval_T *arg,
6607 pos_T *posp,
6608 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006609 colnr_T *curswantp,
6610 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006611{
6612 list_T *l = arg->vval.v_list;
6613 long i = 0;
6614 long n;
6615
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006616 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6617 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006618 if (arg->v_type != VAR_LIST
6619 || l == NULL
6620 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006621 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006622 return FAIL;
6623
6624 if (fnump != NULL)
6625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006626 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006627 if (n < 0)
6628 return FAIL;
6629 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006630 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006631 *fnump = n;
6632 }
6633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006634 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006635 if (n < 0)
6636 return FAIL;
6637 posp->lnum = n;
6638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006639 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006640 if (n < 0)
6641 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006642 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006643 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006644 if (charcol)
6645 {
6646 buf_T *buf;
6647
6648 // Get the text for the specified line in a loaded buffer
6649 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6650 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6651 return FAIL;
6652
Bram Moolenaar79f23442022-10-10 12:42:57 +01006653 n = buf_charidx_to_byteidx(buf,
6654 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006655 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006656 posp->col = n;
6657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006658 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006659 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006660 posp->coladd = 0;
6661 else
6662 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006663
Bram Moolenaar493c1782014-05-28 14:34:46 +02006664 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006665 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006666
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006667 return OK;
6668}
6669
6670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 * Get the length of an environment variable name.
6672 * Advance "arg" to the first character after the name.
6673 * Return 0 for error.
6674 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 char_u *p;
6679 int len;
6680
6681 for (p = *arg; vim_isIDc(*p); ++p)
6682 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006683 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 return 0;
6685
6686 len = (int)(p - *arg);
6687 *arg = p;
6688 return len;
6689}
6690
6691/*
6692 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006693 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 * Return 0 if something is wrong.
6695 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006696 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006697get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698{
6699 char_u *p;
6700 int len;
6701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006702 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006704 {
6705 if (*p == ':')
6706 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006707 // "s:" is start of "s:var", but "n:" is not and can be used in
6708 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006709 len = (int)(p - *arg);
6710 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6711 || len > 1)
6712 break;
6713 }
6714 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006715 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 return 0;
6717
6718 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006719 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 return len;
6722}
6723
6724/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006725 * Get the length of the name of a variable or function.
6726 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006728 * Return -1 if curly braces expansion failed.
6729 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 * If the name contains 'magic' {}'s, expand them and return the
6731 * expanded name in an allocated string via 'alias' - caller must free.
6732 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006734get_name_len(
6735 char_u **arg,
6736 char_u **alias,
6737 int evaluate,
6738 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 char_u *p;
6742 char_u *expr_start;
6743 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006745 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746
6747 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6748 && (*arg)[2] == (int)KE_SNR)
6749 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006750 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 *arg += 3;
6752 return get_id_len(arg) + 3;
6753 }
6754 len = eval_fname_script(*arg);
6755 if (len > 0)
6756 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006757 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 *arg += len;
6759 }
6760
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006762 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006764 p = find_name_end(*arg, &expr_start, &expr_end,
6765 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (expr_start != NULL)
6767 {
6768 char_u *temp_string;
6769
6770 if (!evaluate)
6771 {
6772 len += (int)(p - *arg);
6773 *arg = skipwhite(p);
6774 return len;
6775 }
6776
6777 /*
6778 * Include any <SID> etc in the expanded string:
6779 * Thus the -len here.
6780 */
6781 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6782 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006783 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 *alias = temp_string;
6785 *arg = skipwhite(p);
6786 return (int)STRLEN(temp_string);
6787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788
6789 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006790 // Only give an error when there is something, otherwise it will be
6791 // reported at a higher level.
6792 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006793 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
6795 return len;
6796}
6797
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006798/*
6799 * Find the end of a variable or function name, taking care of magic braces.
6800 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6801 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006802 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006803 * Return a pointer to just after the name. Equal to "arg" if there is no
6804 * valid name.
6805 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006806 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006807find_name_end(
6808 char_u *arg,
6809 char_u **expr_start,
6810 char_u **expr_end,
6811 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006813 int mb_nest = 0;
6814 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006816 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006817 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006819 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006821 *expr_start = NULL;
6822 *expr_end = NULL;
6823 }
6824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006825 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006826 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006827 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006828 return arg;
6829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006830 for (p = arg; *p != NUL
6831 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006832 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006833 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006834 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006835 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006836 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006837 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006838 if (*p == '\'')
6839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006840 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006841 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006842 ;
6843 if (*p == NUL)
6844 break;
6845 }
6846 else if (*p == '"')
6847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006848 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006849 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006850 if (*p == '\\' && p[1] != NUL)
6851 ++p;
6852 if (*p == NUL)
6853 break;
6854 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006855 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006857 // "s:" is start of "s:var", but "n:" is not and can be used in
6858 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006859 len = (int)(p - arg);
6860 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006861 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006862 break;
6863 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006865 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006867 if (*p == '[')
6868 ++br_nest;
6869 else if (*p == ']')
6870 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006872
zeertzjqa93d9cd2023-05-02 16:25:47 +01006873 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006875 if (*p == '{')
6876 {
6877 mb_nest++;
6878 if (expr_start != NULL && *expr_start == NULL)
6879 *expr_start = p;
6880 }
6881 else if (*p == '}')
6882 {
6883 mb_nest--;
6884 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6885 *expr_end = p;
6886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
6889
6890 return p;
6891}
6892
6893/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006894 * Expands out the 'magic' {}'s in a variable/function name.
6895 * Note that this can call itself recursively, to deal with
6896 * constructs like foo{bar}{baz}{bam}
6897 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6898 * "in_start" ^
6899 * "expr_start" ^
6900 * "expr_end" ^
6901 * "in_end" ^
6902 *
6903 * Returns a new allocated string, which the caller must free.
6904 * Returns NULL for failure.
6905 */
6906 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006907make_expanded_name(
6908 char_u *in_start,
6909 char_u *expr_start,
6910 char_u *expr_end,
6911 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006912{
6913 char_u c1;
6914 char_u *retval = NULL;
6915 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006916
6917 if (expr_end == NULL || in_end == NULL)
6918 return NULL;
6919 *expr_start = NUL;
6920 *expr_end = NUL;
6921 c1 = *in_end;
6922 *in_end = NUL;
6923
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006924 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006925 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006926 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006927 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6928 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006929 if (retval != NULL)
6930 {
6931 STRCPY(retval, in_start);
6932 STRCAT(retval, temp_result);
6933 STRCAT(retval, expr_end + 1);
6934 }
6935 }
6936 vim_free(temp_result);
6937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006938 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006939 *expr_start = '{';
6940 *expr_end = '}';
6941
6942 if (retval != NULL)
6943 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006944 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006945 if (expr_start != NULL)
6946 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006947 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006948 temp_result = make_expanded_name(retval, expr_start,
6949 expr_end, temp_result);
6950 vim_free(retval);
6951 retval = temp_result;
6952 }
6953 }
6954
6955 return retval;
6956}
6957
6958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006960 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006963eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006965 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006966}
6967
6968/*
6969 * Return TRUE if character "c" can be used as the first character in a
6970 * variable or function name (excluding '{' and '}').
6971 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006972 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006973eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006974{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006975 return ASCII_ISALPHA(c) || c == '_';
6976}
6977
6978/*
6979 * Return TRUE if character "c" can be used as the first character of a
6980 * dictionary key.
6981 */
6982 int
6983eval_isdictc(int c)
6984{
6985 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986}
6987
6988/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006989 * Handle:
6990 * - expr[expr], expr[expr:expr] subscript
6991 * - ".name" lookup
6992 * - function call with Funcref variable: func(expr)
6993 * - method call: var->method()
6994 *
6995 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006996 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006997 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006999handle_subscript(
7000 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007001 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007002 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007003 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007004 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007005{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007006 int evaluate = evalarg != NULL
7007 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007008 int ret = OK;
7009 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007010 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007011 int getnext;
7012 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007013
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007014 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007015 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007016 // When at the end of the line and ".name" or "->{" or "->X" follows in
7017 // the next line then consume the line break.
7018 p = eval_next_non_blank(*arg, evalarg, &getnext);
7019 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007020 && ((*p == '.'
7021 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7022 || rettv->v_type == VAR_CLASS
7023 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007024 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7025 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7026 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007027 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007028 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007029 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007030 check_white = FALSE;
7031 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007032
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007033 if (rettv->v_type == VAR_ANY)
7034 {
7035 char_u *exp_name;
7036 int cc;
7037 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007038 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007039 type_T *type;
7040
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007041 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007042 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007043 if (**arg != '.')
7044 {
7045 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007046 semsg(_(e_expected_dot_after_name_str),
7047 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007048 ret = FAIL;
7049 break;
7050 }
7051 ++*arg;
7052 if (IS_WHITE_OR_NUL(**arg))
7053 {
7054 if (verbose)
7055 emsg(_(e_no_white_space_allowed_after_dot));
7056 ret = FAIL;
7057 break;
7058 }
7059
7060 // isolate the name
7061 exp_name = *arg;
7062 while (eval_isnamec(**arg))
7063 ++*arg;
7064 cc = **arg;
7065 **arg = NUL;
7066
7067 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007068 evalarg == NULL ? NULL : evalarg->eval_cctx,
7069 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007070 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007071
7072 if (idx < 0 && ufunc == NULL)
7073 {
7074 ret = FAIL;
7075 break;
7076 }
7077 if (idx >= 0)
7078 {
7079 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7080 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7081
7082 copy_tv(sv->sv_tv, rettv);
7083 }
7084 else
7085 {
7086 rettv->v_type = VAR_FUNC;
7087 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7088 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007089 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007090 }
7091
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007092 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7093 || rettv->v_type == VAR_PARTIAL))
7094 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007095 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007096 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7097 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007098
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007099 // Stop the expression evaluation when immediately aborting on
7100 // error, or when an interrupt occurred or an exception was thrown
7101 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007102 if (aborting())
7103 {
7104 if (ret == OK)
7105 clear_tv(rettv);
7106 ret = FAIL;
7107 }
7108 dict_unref(selfdict);
7109 selfdict = NULL;
7110 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007111 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007112 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007113 if (in_vim9script())
7114 *arg = skipwhite(p + 2);
7115 else
7116 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007117 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007118 {
zeertzjqc481ad32023-03-11 16:18:51 +00007119 emsg(_(e_no_white_space_allowed_before_parenthesis));
7120 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007121 }
zeertzjqc481ad32023-03-11 16:18:51 +00007122 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7123 // expr->{lambda}() or expr->(lambda)()
7124 ret = eval_lambda(arg, rettv, evalarg, verbose);
7125 else
7126 // expr->name()
7127 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007128 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007129 // "." is ".name" lookup when we found a dict or when evaluating and
7130 // scriptversion is at least 2, where string concatenation is "..".
7131 else if (**arg == '['
7132 || (**arg == '.' && (rettv->v_type == VAR_DICT
7133 || (!evaluate
7134 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007135 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007136 {
7137 dict_unref(selfdict);
7138 if (rettv->v_type == VAR_DICT)
7139 {
7140 selfdict = rettv->vval.v_dict;
7141 if (selfdict != NULL)
7142 ++selfdict->dv_refcount;
7143 }
7144 else
7145 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007146 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007147 {
7148 clear_tv(rettv);
7149 ret = FAIL;
7150 }
7151 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007152 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7153 || rettv->v_type == VAR_OBJECT))
7154 {
7155 // class member: SomeClass.varname
7156 // class method: SomeClass.SomeMethod()
7157 // class constructor: SomeClass.new()
7158 // object member: someObject.varname
7159 // object method: someObject.SomeMethod()
7160 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7161 {
7162 clear_tv(rettv);
7163 ret = FAIL;
7164 }
7165 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007166 else
7167 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007168 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007169
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007170 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7171 // Don't do this when "Func" is already a partial that was bound
7172 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007173 if (selfdict != NULL
7174 && (rettv->v_type == VAR_FUNC
7175 || (rettv->v_type == VAR_PARTIAL
7176 && (rettv->vval.v_partial->pt_auto
7177 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007178 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007179
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007180 dict_unref(selfdict);
7181 return ret;
7182}
7183
7184/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185 * Make a copy of an item.
7186 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007187 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007188 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7189 * reference to an already copied list/dict can be used.
7190 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007192 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007193item_copy(
7194 typval_T *from,
7195 typval_T *to,
7196 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007197 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007198 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199{
7200 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007201 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007205 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007206 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007207 }
7208 ++recurse;
7209
7210 switch (from->v_type)
7211 {
7212 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007213 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214 case VAR_STRING:
7215 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007216 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007217 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007218 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007219 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007220 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007221 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007222 case VAR_CLASS:
7223 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007224 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007225 copy_tv(from, to);
7226 break;
7227 case VAR_LIST:
7228 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007229 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007230 if (from->vval.v_list == NULL)
7231 to->vval.v_list = NULL;
7232 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7233 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007234 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007235 to->vval.v_list = from->vval.v_list->lv_copylist;
7236 ++to->vval.v_list->lv_refcount;
7237 }
7238 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007239 to->vval.v_list = list_copy(from->vval.v_list,
7240 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007241 if (to->vval.v_list == NULL)
7242 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007243 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007244 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007246 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007247 case VAR_DICT:
7248 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007249 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007250 if (from->vval.v_dict == NULL)
7251 to->vval.v_dict = NULL;
7252 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7253 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007254 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007255 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7256 ++to->vval.v_dict->dv_refcount;
7257 }
7258 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007259 to->vval.v_dict = dict_copy(from->vval.v_dict,
7260 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007261 if (to->vval.v_dict == NULL)
7262 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007263 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007264 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007265 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007267 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007268 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007269 }
7270 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007271 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007272}
7273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 void
7275echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7276{
7277 char_u *tofree;
7278 char_u numbuf[NUMBUFLEN];
7279 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7280
7281 if (*atstart)
7282 {
7283 *atstart = FALSE;
7284 // Call msg_start() after eval1(), evaluating the expression
7285 // may cause a message to appear.
7286 if (with_space)
7287 {
7288 // Mark the saved text as finishing the line, so that what
7289 // follows is displayed on a new line when scrolling back
7290 // at the more prompt.
7291 msg_sb_eol();
7292 msg_start();
7293 }
7294 }
7295 else if (with_space)
7296 msg_puts_attr(" ", echo_attr);
7297
7298 if (p != NULL)
7299 for ( ; *p != NUL && !got_int; ++p)
7300 {
7301 if (*p == '\n' || *p == '\r' || *p == TAB)
7302 {
7303 if (*p != TAB && *needclr)
7304 {
7305 // remove any text still there from the command
7306 msg_clr_eos();
7307 *needclr = FALSE;
7308 }
7309 msg_putchar_attr(*p, echo_attr);
7310 }
7311 else
7312 {
7313 if (has_mbyte)
7314 {
7315 int i = (*mb_ptr2len)(p);
7316
7317 (void)msg_outtrans_len_attr(p, i, echo_attr);
7318 p += i - 1;
7319 }
7320 else
7321 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7322 }
7323 }
7324 vim_free(tofree);
7325}
7326
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 * ":echo expr1 ..." print each argument separated with a space, add a
7329 * newline at the end.
7330 * ":echon expr1 ..." print each argument plain.
7331 */
7332 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007333ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
7335 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007337 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 int needclr = TRUE;
7339 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007340 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007341 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007342 evalarg_T evalarg;
7343
Bram Moolenaare707c882020-07-01 13:07:37 +02007344 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
7346 if (eap->skip)
7347 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007348 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007350 // If eval1() causes an error message the text from the command may
7351 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007352 need_clr_eos = needclr;
7353
Bram Moolenaar68db9962021-05-09 23:19:22 +02007354 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007355 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 {
7357 /*
7358 * Report the invalid expression unless the expression evaluation
7359 * has been cancelled due to an aborting error, an interrupt, or an
7360 * exception.
7361 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007362 if (!aborting() && did_emsg == did_emsg_before
7363 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007364 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007365 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 break;
7367 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007368 need_clr_eos = FALSE;
7369
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007371 {
7372 if (rettv.v_type == VAR_VOID)
7373 {
7374 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7375 break;
7376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007378 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007380 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 arg = skipwhite(arg);
7382 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007383 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007384 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
7386 if (eap->skip)
7387 --emsg_skip;
7388 else
7389 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007390 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if (needclr)
7392 msg_clr_eos();
7393 if (eap->cmdidx == CMD_echo)
7394 msg_end();
7395 }
7396}
7397
7398/*
7399 * ":echohl {name}".
7400 */
7401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007402ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007404 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405}
7406
7407/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007408 * Returns the :echo attribute
7409 */
7410 int
7411get_echo_attr(void)
7412{
7413 return echo_attr;
7414}
7415
7416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 * ":execute expr1 ..." execute the result of an expression.
7418 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007419 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007421 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 * Each gets spaces around each argument and a newline at the end for
7423 * echo commands
7424 */
7425 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007426ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 int ret = OK;
7431 char_u *p;
7432 garray_T ga;
7433 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007434 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
7436 ga_init2(&ga, 1, 80);
7437
7438 if (eap->skip)
7439 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007440 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007442 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007443 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445
7446 if (!eap->skip)
7447 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007448 char_u buf[NUMBUFLEN];
7449
7450 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007451 {
7452 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7453 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007454 semsg(_(e_using_invalid_value_as_string_str),
7455 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007456 p = NULL;
7457 }
7458 else
7459 p = tv_get_string_buf(&rettv, buf);
7460 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007461 else
7462 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007463 if (p == NULL)
7464 {
7465 clear_tv(&rettv);
7466 ret = FAIL;
7467 break;
7468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 len = (int)STRLEN(p);
7470 if (ga_grow(&ga, len + 2) == FAIL)
7471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007472 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 ret = FAIL;
7474 break;
7475 }
7476 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 ga.ga_len += len;
7480 }
7481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 arg = skipwhite(arg);
7484 }
7485
7486 if (ret != FAIL && ga.ga_data != NULL)
7487 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007488 // use the first line of continuation lines for messages
7489 SOURCING_LNUM = start_lnum;
7490
Bram Moolenaar37fef162022-08-29 18:16:32 +01007491 if (eap->cmdidx == CMD_echomsg
7492 || eap->cmdidx == CMD_echowindow
7493 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007494 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007495 // Mark the already saved text as finishing the line, so that what
7496 // follows is displayed on a new line when scrolling back at the
7497 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007498 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007499 }
7500
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007501 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007502 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007503 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007504 out_flush();
7505 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007506 else if (eap->cmdidx == CMD_echowindow)
7507 {
7508#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007509 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007510#endif
7511 msg_attr(ga.ga_data, echo_attr);
7512#ifdef HAS_MESSAGE_WINDOW
7513 end_echowindow();
7514#endif
7515 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007516 else if (eap->cmdidx == CMD_echoconsole)
7517 {
7518 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7519 ui_write((char_u *)"\r\n", 2, TRUE);
7520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 else if (eap->cmdidx == CMD_echoerr)
7522 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007523 int save_did_emsg = did_emsg;
7524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007525 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007526 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 if (!force_abort)
7528 did_emsg = save_did_emsg;
7529 }
7530 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007531 {
7532 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7533
7534 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7535 sticky_cmdmod_flags = cmdmod.cmod_flags
7536 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 do_cmdline((char_u *)ga.ga_data,
7538 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007539 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 }
7542
7543 ga_clear(&ga);
7544
7545 if (eap->skip)
7546 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007547 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548}
7549
7550/*
7551 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7552 * "arg" points to the "&" or '+' when called, to "option" when returning.
7553 * Returns NULL when no option name found. Otherwise pointer to the char
7554 * after the option name.
7555 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007556 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007557find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558{
7559 char_u *p = *arg;
7560
7561 ++p;
7562 if (*p == 'g' && p[1] == ':')
7563 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007564 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 p += 2;
7566 }
7567 else if (*p == 'l' && p[1] == ':')
7568 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007569 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 p += 2;
7571 }
7572 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007573 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574
7575 if (!ASCII_ISALPHA(*p))
7576 return NULL;
7577 *arg = p;
7578
7579 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007580 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 else
7582 while (ASCII_ISALPHA(*p))
7583 ++p;
7584 return p;
7585}
7586
7587/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007588 * Display script name where an item was last set.
7589 * Should only be invoked when 'verbose' is non-zero.
7590 */
7591 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007592last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007593{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007594 char_u *p;
7595
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007596 if (script_ctx.sc_sid == 0)
7597 return;
7598
7599 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7600 if (p == NULL)
7601 return;
7602
7603 verbose_enter();
7604 msg_puts(_("\n\tLast set from "));
7605 msg_puts((char *)p);
7606 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007607 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007608 msg_puts(_(line_msg));
7609 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007610 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007611 verbose_leave();
7612 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007613}
7614
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007615#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616
7617/*
7618 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007619 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 * "flags" can be "g" to do a global substitute.
7621 * Returns an allocated string, NULL for error.
7622 */
7623 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007624do_string_sub(
7625 char_u *str,
7626 char_u *pat,
7627 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007628 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007629 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 int sublen;
7632 regmatch_T regmatch;
7633 int i;
7634 int do_all;
7635 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007636 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 garray_T ga;
7638 char_u *ret;
7639 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007640 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007642 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007644 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645
7646 ga_init2(&ga, 1, 200);
7647
7648 do_all = (flags[0] == 'g');
7649
7650 regmatch.rm_ic = p_ic;
7651 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7652 if (regmatch.regprog != NULL)
7653 {
7654 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007655 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007658 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007659 if (regmatch.startp[0] == regmatch.endp[0])
7660 {
7661 if (zero_width == regmatch.startp[0])
7662 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007663 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007664 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007665 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7666 (size_t)i);
7667 ga.ga_len += i;
7668 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007669 continue;
7670 }
7671 zero_width = regmatch.startp[0];
7672 }
7673
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 /*
7675 * Get some space for a temporary buffer to do the substitution
7676 * into. It will contain:
7677 * - The text up to where the match is.
7678 * - The substituted text.
7679 * - The text after the match.
7680 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007681 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007682 if (sublen <= 0)
7683 {
7684 ga_clear(&ga);
7685 break;
7686 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007687 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7689 {
7690 ga_clear(&ga);
7691 break;
7692 }
7693
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007694 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 i = (int)(regmatch.startp[0] - tail);
7696 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007697 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007698 (void)vim_regsub(&regmatch, sub, expr,
7699 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7700 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007702 tail = regmatch.endp[0];
7703 if (*tail == NUL)
7704 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 if (!do_all)
7706 break;
7707 }
7708
7709 if (ga.ga_data != NULL)
7710 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7711
Bram Moolenaar473de612013-06-08 18:19:48 +02007712 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 }
7714
7715 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7716 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007717 if (p_cpo == empty_option)
7718 p_cpo = save_cpo;
7719 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007721 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007722 // If it's still empty it was changed and restored, need to restore in
7723 // the complicated way.
7724 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007725 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007726 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
7729 return ret;
7730}