blob: e888fecc8ae7ae547d27eea111343326f39d746c [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 Raele6c9aa52023-10-06 19:55:52 +02001115 msg = e_cannot_access_private_variable_str;
1116 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 {
1886 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001887 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1888 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001889 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001890 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001891 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001892 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001895 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001896 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001897 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1898 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001899 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001900 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001901 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001902 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001903 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001905 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001906 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001907 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001908 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 else if (lp->ll_range)
1910 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001911 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1912 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001913 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001914 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001915 return;
1916 }
1917
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001918 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1919 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001920 }
1921 else
1922 {
1923 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001924 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001925 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001926 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1927 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001928 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001929 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001930 return;
1931 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001932
1933 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001934 && check_typval_arg_type(lp->ll_valtype, rettv,
1935 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001936 return;
1937
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 if (lp->ll_newkey != NULL)
1939 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 if (op != NULL && *op != '=')
1941 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001942 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 return;
1944 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001945 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1946 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001947 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001949 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001950 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 if (di == NULL)
1952 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001953 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1954 {
1955 vim_free(di);
1956 return;
1957 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001959 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 else if (op != NULL && *op != '=')
1961 {
1962 tv_op(lp->ll_tv, rettv, op);
1963 return;
1964 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001965 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 /*
1969 * Assign the value to the variable or list item.
1970 */
1971 if (copy)
1972 copy_tv(rettv, lp->ll_tv);
1973 else
1974 {
1975 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001976 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 }
1979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980}
1981
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001983 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1984 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 * Returns OK or FAIL.
1986 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001988tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001990 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001991 char_u numbuf[NUMBUFLEN];
1992 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001993 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001994
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001995 // Can't do anything with a Funcref or Dict on the right.
1996 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001997 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001998 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1999 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 {
2001 switch (tv1->v_type)
2002 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002003 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002004 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002006 case VAR_DICT:
2007 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002008 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002009 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002010 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002011 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002012 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002013 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002014 case VAR_CLASS:
2015 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002016 break;
2017
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002018 case VAR_BLOB:
2019 if (*op != '+' || tv2->v_type != VAR_BLOB)
2020 break;
2021 // BLOB += BLOB
2022 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2023 {
2024 blob_T *b1 = tv1->vval.v_blob;
2025 blob_T *b2 = tv2->vval.v_blob;
2026 int i, len = blob_len(b2);
2027 for (i = 0; i < len; i++)
2028 ga_append(&b1->bv_ga, blob_get(b2, i));
2029 }
2030 return OK;
2031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 case VAR_LIST:
2033 if (*op != '+' || tv2->v_type != VAR_LIST)
2034 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002035 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002036 if (tv2->vval.v_list != NULL)
2037 {
2038 if (tv1->vval.v_list == NULL)
2039 {
2040 tv1->vval.v_list = tv2->vval.v_list;
2041 ++tv1->vval.v_list->lv_refcount;
2042 }
2043 else
2044 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2045 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046 return OK;
2047
2048 case VAR_NUMBER:
2049 case VAR_STRING:
2050 if (tv2->v_type == VAR_LIST)
2051 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002052 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002053 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002054 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002055 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002056 if (tv2->v_type == VAR_FLOAT)
2057 {
2058 float_T f = n;
2059
Bram Moolenaarff697e62019-02-12 22:28:33 +01002060 if (*op == '%')
2061 break;
2062 switch (*op)
2063 {
2064 case '+': f += tv2->vval.v_float; break;
2065 case '-': f -= tv2->vval.v_float; break;
2066 case '*': f *= tv2->vval.v_float; break;
2067 case '/': f /= tv2->vval.v_float; break;
2068 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002069 clear_tv(tv1);
2070 tv1->v_type = VAR_FLOAT;
2071 tv1->vval.v_float = f;
2072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002073 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002074 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002075 switch (*op)
2076 {
2077 case '+': n += tv_get_number(tv2); break;
2078 case '-': n -= tv_get_number(tv2); break;
2079 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002080 case '/': n = num_divide(n, tv_get_number(tv2),
2081 &failed); break;
2082 case '%': n = num_modulus(n, tv_get_number(tv2),
2083 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002084 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002085 clear_tv(tv1);
2086 tv1->v_type = VAR_NUMBER;
2087 tv1->vval.v_number = n;
2088 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 }
2090 else
2091 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002092 if (tv2->v_type == VAR_FLOAT)
2093 break;
2094
Bram Moolenaarff697e62019-02-12 22:28:33 +01002095 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002096 s = tv_get_string(tv1);
2097 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098 clear_tv(tv1);
2099 tv1->v_type = VAR_STRING;
2100 tv1->vval.v_string = s;
2101 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002102 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002103
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002104 case VAR_FLOAT:
2105 {
2106 float_T f;
2107
Bram Moolenaarff697e62019-02-12 22:28:33 +01002108 if (*op == '%' || *op == '.'
2109 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002110 && tv2->v_type != VAR_NUMBER
2111 && tv2->v_type != VAR_STRING))
2112 break;
2113 if (tv2->v_type == VAR_FLOAT)
2114 f = tv2->vval.v_float;
2115 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002116 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002117 switch (*op)
2118 {
2119 case '+': tv1->vval.v_float += f; break;
2120 case '-': tv1->vval.v_float -= f; break;
2121 case '*': tv1->vval.v_float *= f; break;
2122 case '/': tv1->vval.v_float /= f; break;
2123 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002124 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002125 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126 }
2127 }
2128
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002129 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002130 return FAIL;
2131}
2132
2133/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134 * Evaluate the expression used in a ":for var in expr" command.
2135 * "arg" points to "var".
2136 * Set "*errp" to TRUE for an error, FALSE otherwise;
2137 * Return a pointer that holds the info. Null when there is an error.
2138 */
2139 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002140eval_for_line(
2141 char_u *arg,
2142 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002143 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002144 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002145{
Bram Moolenaar33570922005-01-25 22:26:29 +00002146 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002147 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002148 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 typval_T tv;
2150 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002151 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002152
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002153 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002154
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002155 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002156 if (fi == NULL)
2157 return NULL;
2158
Bram Moolenaar404557e2021-07-05 21:41:48 +02002159 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2160 &fi->fi_semicolon, FALSE);
2161 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162 return fi;
2163
Bram Moolenaar404557e2021-07-05 21:41:48 +02002164 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002165 if (expr[0] != 'i' || expr[1] != 'n'
2166 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002167 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002168 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2169 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2170 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002171 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002172 return fi;
2173 }
2174
2175 if (skip)
2176 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002177 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2178 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002179 {
2180 *errp = FALSE;
2181 if (!skip)
2182 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002183 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002184 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002185 l = tv.vval.v_list;
2186 if (l == NULL)
2187 {
2188 // a null list is like an empty list: do nothing
2189 clear_tv(&tv);
2190 }
2191 else
2192 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002194 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002196 // No need to increment the refcount, it's already set for
2197 // the list being used in "tv".
2198 fi->fi_list = l;
2199 list_add_watch(l, &fi->fi_lw);
2200 fi->fi_lw.lw_item = l->lv_first;
2201 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002202 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002203 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002204 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002205 fi->fi_bi = 0;
2206 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002207 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002208 typval_T btv;
2209
2210 // Make a copy, so that the iteration still works when the
2211 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002213 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002214 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002215 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002216 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002217 else if (tv.v_type == VAR_STRING)
2218 {
2219 fi->fi_byte_idx = 0;
2220 fi->fi_string = tv.vval.v_string;
2221 tv.vval.v_string = NULL;
2222 if (fi->fi_string == NULL)
2223 fi->fi_string = vim_strsave((char_u *)"");
2224 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002225 else
2226 {
Sean Dewar80d73952021-08-04 19:25:54 +02002227 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002228 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002229 }
2230 }
2231 }
2232 if (skip)
2233 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002234 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002235
2236 return fi;
2237}
2238
2239/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002240 * Used when looping over a :for line, skip the "in expr" part.
2241 */
2242 void
2243skip_for_lines(void *fi_void, evalarg_T *evalarg)
2244{
2245 forinfo_T *fi = (forinfo_T *)fi_void;
2246 int i;
2247
2248 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002249 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002250}
2251
2252/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002253 * Use the first item in a ":for" list. Advance to the next.
2254 * Assign the values to the variable (list). "arg" points to the first one.
2255 * Return TRUE when a valid item was found, FALSE when at end of list or
2256 * something wrong.
2257 */
2258 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002259next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002260{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002261 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002262 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002263 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002264 ? (ASSIGN_FINAL
2265 // first round: error if variable exists
2266 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002267 | ASSIGN_NO_MEMBER_TYPE
2268 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002269 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002270 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002271 int skip_assign = in_vim9script() && arg[0] == '_'
2272 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002273
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002274 if (fi->fi_blob != NULL)
2275 {
2276 typval_T tv;
2277
2278 if (fi->fi_bi >= blob_len(fi->fi_blob))
2279 return FALSE;
2280 tv.v_type = VAR_NUMBER;
2281 tv.v_lock = VAR_FIXED;
2282 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2283 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002284 if (skip_assign)
2285 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002286 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002287 fi->fi_varcount, flag, NULL) == OK;
2288 }
2289
2290 if (fi->fi_string != NULL)
2291 {
2292 typval_T tv;
2293 int len;
2294
2295 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2296 if (len == 0)
2297 return FALSE;
2298 tv.v_type = VAR_STRING;
2299 tv.v_lock = VAR_FIXED;
2300 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2301 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002302 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002303 if (skip_assign)
2304 result = TRUE;
2305 else
2306 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002307 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002308 vim_free(tv.vval.v_string);
2309 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002310 }
2311
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002312 item = fi->fi_lw.lw_item;
2313 if (item == NULL)
2314 result = FALSE;
2315 else
2316 {
2317 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002318 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002319 if (skip_assign)
2320 result = TRUE;
2321 else
2322 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002323 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002324 }
2325 return result;
2326}
2327
2328/*
2329 * Free the structure used to store info used by ":for".
2330 */
2331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002332free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333{
Bram Moolenaar33570922005-01-25 22:26:29 +00002334 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002335
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002336 if (fi == NULL)
2337 return;
2338 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002339 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002341 list_unref(fi->fi_list);
2342 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002343 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002344 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002345 else
2346 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002347 vim_free(fi);
2348}
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002351set_context_for_expression(
2352 expand_T *xp,
2353 char_u *arg,
2354 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002356 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002358 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002360 if (cmdidx == CMD_let || cmdidx == CMD_var
2361 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002362 {
2363 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002364 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002366 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002367 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002368 {
2369 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002370 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002371 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 break;
2373 }
2374 return;
2375 }
2376 }
2377 else
2378 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2379 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 while ((xp->xp_pattern = vim_strpbrk(arg,
2381 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2382 {
2383 c = *xp->xp_pattern;
2384 if (c == '&')
2385 {
2386 c = xp->xp_pattern[1];
2387 if (c == '&')
2388 {
2389 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002390 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002395 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2396 xp->xp_pattern += 2;
2397
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 else if (c == '$')
2401 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002402 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 xp->xp_context = EXPAND_ENV_VARS;
2404 }
2405 else if (c == '=')
2406 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002407 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 xp->xp_context = EXPAND_EXPRESSION;
2409 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002410 else if (c == '#'
2411 && xp->xp_context == EXPAND_EXPRESSION)
2412 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002413 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002414 break;
2415 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002416 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 && xp->xp_context == EXPAND_FUNCTIONS
2418 && vim_strchr(xp->xp_pattern, '(') == NULL)
2419 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002420 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 break;
2422 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002423 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002425 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2428 if (c == '\\' && xp->xp_pattern[1] != NUL)
2429 ++xp->xp_pattern;
2430 xp->xp_context = EXPAND_NOTHING;
2431 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002432 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002434 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2436 /* skip */ ;
2437 xp->xp_context = EXPAND_NOTHING;
2438 }
2439 else if (c == '|')
2440 {
2441 if (xp->xp_pattern[1] == '|')
2442 {
2443 ++xp->xp_pattern;
2444 xp->xp_context = EXPAND_EXPRESSION;
2445 }
2446 else
2447 xp->xp_context = EXPAND_COMMANDS;
2448 }
2449 else
2450 xp->xp_context = EXPAND_EXPRESSION;
2451 }
2452 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002453 // Doesn't look like something valid, expand as an expression
2454 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 arg = xp->xp_pattern;
2457 if (*arg != NUL)
2458 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2459 /* skip */ ;
2460 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002461
2462 // ":exe one two" completes "two"
2463 if ((cmdidx == CMD_execute
2464 || cmdidx == CMD_echo
2465 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002466 || cmdidx == CMD_echomsg
2467 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002468 && xp->xp_context == EXPAND_EXPRESSION)
2469 {
2470 for (;;)
2471 {
2472 char_u *n = skiptowhite(arg);
2473
2474 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2475 break;
2476 arg = skipwhite(n);
2477 }
2478 }
2479
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 xp->xp_pattern = arg;
2481}
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002484 * Return TRUE if "pat" matches "text".
2485 * Does not use 'cpo' and always uses 'magic'.
2486 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002487 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002488pattern_match(char_u *pat, char_u *text, int ic)
2489{
2490 int matches = FALSE;
2491 char_u *save_cpo;
2492 regmatch_T regmatch;
2493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002494 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002495 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002496 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002497 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2498 if (regmatch.regprog != NULL)
2499 {
2500 regmatch.rm_ic = ic;
2501 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2502 vim_regfree(regmatch.regprog);
2503 }
2504 p_cpo = save_cpo;
2505 return matches;
2506}
2507
2508/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002509 * Handle a name followed by "(". Both for just "name(arg)" and for
2510 * "expr->name(arg)".
2511 * Returns OK or FAIL.
2512 */
2513 static int
2514eval_func(
2515 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002516 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002517 char_u *name,
2518 int name_len,
2519 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002520 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002521 typval_T *basetv) // "expr" for "expr->name(arg)"
2522{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002523 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002524 char_u *s = name;
2525 int len = name_len;
2526 partial_T *partial;
2527 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002528 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002529 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002530
2531 if (!evaluate)
2532 check_vars(s, len);
2533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002534 // If "s" is the name of a variable of type VAR_FUNC
2535 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002536 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002537 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002539 // Need to make a copy, in case evaluating the arguments makes
2540 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002541 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002542 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002543 ret = FAIL;
2544 else
2545 {
2546 funcexe_T funcexe;
2547
2548 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002549 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002550 funcexe.fe_firstline = curwin->w_cursor.lnum;
2551 funcexe.fe_lastline = curwin->w_cursor.lnum;
2552 funcexe.fe_evaluate = evaluate;
2553 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002554 if (partial != NULL)
2555 {
2556 funcexe.fe_object = partial->pt_obj;
2557 if (funcexe.fe_object != NULL)
2558 ++funcexe.fe_object->obj_refcount;
2559 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002560 funcexe.fe_basetv = basetv;
2561 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002562 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002563 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002564 }
2565 vim_free(s);
2566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002567 // If evaluate is FALSE rettv->v_type was not set in
2568 // get_func_tv, but it's needed in handle_subscript() to parse
2569 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002570 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2571 {
2572 rettv->vval.v_string = NULL;
2573 rettv->v_type = VAR_FUNC;
2574 }
2575
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002576 // Stop the expression evaluation when immediately
2577 // aborting on error, or when an interrupt occurred or
2578 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002579 if (evaluate && aborting())
2580 {
2581 if (ret == OK)
2582 clear_tv(rettv);
2583 ret = FAIL;
2584 }
2585 return ret;
2586}
2587
2588/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002589 * After a NL, skip over empty lines and comment-only lines.
2590 */
2591 static char_u *
2592newline_skip_comments(char_u *arg)
2593{
2594 char_u *p = arg + 1;
2595
2596 for (;;)
2597 {
2598 p = skipwhite(p);
2599
2600 if (*p == NUL)
2601 break;
2602 if (vim9_comment_start(p))
2603 {
2604 char_u *nl = vim_strchr(p, NL);
2605
2606 if (nl == NULL)
2607 break;
2608 p = nl;
2609 }
2610 if (*p != NL)
2611 break;
2612 ++p; // skip another NL
2613 }
2614 return p;
2615}
2616
2617/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002618 * Get the next line source line without advancing. But do skip over comment
2619 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002620 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002621 */
2622 static char_u *
2623getline_peek_skip_comments(evalarg_T *evalarg)
2624{
2625 for (;;)
2626 {
2627 char_u *next = getline_peek(evalarg->eval_getline,
2628 evalarg->eval_cookie);
2629 char_u *p;
2630
2631 if (next == NULL)
2632 break;
2633 p = skipwhite(next);
2634 if (*p != NUL && !vim9_comment_start(p))
2635 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002636 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002637 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002638 }
2639 return NULL;
2640}
2641
2642/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002643 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2644 * comment) and there is a next line, return the next line (skipping blanks)
2645 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002646 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2647 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648 * "arg" must point somewhere inside a line, not at the start.
2649 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002650 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002651eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2652{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002653 char_u *p = skipwhite(arg);
2654
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002655 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002656 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002657 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002658 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2659 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002660 && (*p == NUL || *p == NL
2661 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002662 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002663 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002664
Bram Moolenaare442d592022-05-05 12:20:28 +01002665 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002666 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002667 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002668 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002669 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002670 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002671
Bram Moolenaar9d489562020-07-30 20:08:50 +02002672 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002674 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002675 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002676 }
2677 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002678 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002679}
2680
2681/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002682 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002683 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002684 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002685 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002686eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002687{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002688 garray_T *gap = &evalarg->eval_ga;
2689 char_u *line;
2690
Bram Moolenaar39be4982022-05-06 21:51:50 +01002691 if (arg != NULL)
2692 {
2693 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002694 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002695 // Truncate before a trailing comment, so that concatenating the lines
2696 // won't turn the rest into a comment.
2697 if (*skipwhite(arg) == '#')
2698 *arg = NUL;
2699 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002700
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002701 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002702 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2703 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002704 else
2705 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002706 if (line == NULL)
2707 return NULL;
2708
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002709 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002710 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2711 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002712 char_u *p = skipwhite(line);
2713
2714 // Going to concatenate the lines after parsing. For an empty or
2715 // comment line use an empty string.
2716 if (*p == NUL || vim9_comment_start(p))
2717 {
2718 vim_free(line);
2719 line = vim_strsave((char_u *)"");
2720 }
2721
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002722 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2723 ++gap->ga_len;
2724 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002725 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002726 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002727 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002728 evalarg->eval_tofree = line;
2729 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002730
2731 // Advanced to the next line, "arg" no longer points into the previous
2732 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002733 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002734 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002735}
2736
Bram Moolenaare6b53242020-07-01 17:28:33 +02002737/*
2738 * Call eval_next_non_blank() and get the next line if needed.
2739 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002740 char_u *
2741skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2742{
2743 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002744 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002745
Bram Moolenaar962d7212020-07-04 14:15:00 +02002746 if (evalarg == NULL)
2747 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002748 eval_next_non_blank(p, evalarg, &getnext);
2749 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002750 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002751 return p;
2752}
2753
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002754/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002755 * The "eval" functions have an "evalarg" argument: When NULL or
2756 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2757 * parsed but not executed. The functions may return OK, but the rettv will be
2758 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 */
2760
2761/*
2762 * Handle zero level expression.
2763 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002764 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002765 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002766 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 * Return OK or FAIL.
2768 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002770eval0(
2771 char_u *arg,
2772 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002773 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002774 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002776 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2777}
2778
2779/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002780 * If "arg" is a simple function call without arguments then call it and return
2781 * the result. Otherwise return NOTDONE.
2782 */
2783 int
2784may_call_simple_func(
2785 char_u *arg,
2786 typval_T *rettv)
2787{
2788 char_u *parens = (char_u *)strstr((char *)arg, "()");
2789 int r = NOTDONE;
2790
2791 // If the expression is "FuncName()" then we can skip a lot of overhead.
2792 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2793 {
2794 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2795
2796 if (to_name_end(p, TRUE) == parens)
2797 r = call_simple_func(arg, (int)(parens - arg), rettv);
2798 }
2799 return r;
2800}
2801
2802/*
2803 * Handle zero level expression with optimization for a simple function call.
2804 * Same arguments and return value as eval0().
2805 */
2806 int
2807eval0_simple_funccal(
2808 char_u *arg,
2809 typval_T *rettv,
2810 exarg_T *eap,
2811 evalarg_T *evalarg)
2812{
2813 int r = may_call_simple_func(arg, rettv);
2814
2815 if (r == NOTDONE)
2816 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2817 return r;
2818}
2819
2820/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002821 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2822 * expression and don't check what comes after the expression.
2823 */
2824 int
2825eval0_retarg(
2826 char_u *arg,
2827 typval_T *rettv,
2828 exarg_T *eap,
2829 evalarg_T *evalarg,
2830 char_u **retarg)
2831{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 int ret;
2833 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002834 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002835 int did_emsg_before = did_emsg;
2836 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002837 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002838 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
2840 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002841 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002842
Bram Moolenaar79481362022-06-27 20:15:10 +01002843 if (ret != FAIL)
2844 {
2845 expr_end = p;
2846 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002847
Bram Moolenaar79481362022-06-27 20:15:10 +01002848 // In Vim9 script a command block is not split at NL characters for
2849 // commands using an expression argument. Skip over a '#' comment to
2850 // check for a following NL. Require white space before the '#'.
2851 if (in_vim9script() && p > expr_end && retarg == NULL)
2852 while (*p == '#')
2853 {
2854 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002855
Bram Moolenaar79481362022-06-27 20:15:10 +01002856 if (nl == NULL)
2857 break;
2858 p = skipwhite(nl + 1);
2859 if (eap != NULL && *p != NUL)
2860 eap->nextcmd = p;
2861 check_for_end = FALSE;
2862 }
2863
2864 if (check_for_end)
2865 end_error = !ends_excmd2(arg, p);
2866 }
2867
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002868 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
2870 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002871 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 /*
2873 * Report the invalid expression unless the expression evaluation has
2874 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002875 * exception, or we already gave a more specific error.
2876 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002878 if (!aborting()
2879 && did_emsg == did_emsg_before
2880 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002881 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002882 {
2883 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002884 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002885 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002886 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002887 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002888
zeertzjqf2588b62023-05-05 17:22:35 +01002889 if (eap != NULL && p != NULL)
2890 {
2891 // Some of the expression may not have been consumed.
2892 // Only execute a next command if it cannot be a "||" operator.
2893 // The next command may be "catch".
2894 char_u *nextcmd = check_nextcmd(p);
2895 if (nextcmd != NULL && *nextcmd != '|')
2896 eap->nextcmd = nextcmd;
2897 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002898 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002900
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002901 if (retarg != NULL)
2902 *retarg = p;
2903 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002904 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002905
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 return ret;
2907}
2908
2909/*
2910 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002911 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002912 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 *
2914 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002915 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002917 * Note: "rettv.v_lock" is not set.
2918 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 * Return OK or FAIL.
2920 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002921 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002924 char_u *p;
2925 int getnext;
2926
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002927 CLEAR_POINTER(rettv);
2928
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 /*
2930 * Get the first variable.
2931 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002932 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 return FAIL;
2934
Bram Moolenaar793648f2020-06-26 21:28:25 +02002935 p = eval_next_non_blank(*arg, evalarg, &getnext);
2936 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002938 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002939 int result;
2940 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002941 evalarg_T *evalarg_used = evalarg;
2942 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002943 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002944 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002945 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002946
2947 if (evalarg == NULL)
2948 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002949 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002950 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002951 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002952 orig_flags = evalarg_used->eval_flags;
2953 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002954
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002955 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002956 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002957 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002958 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002959 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002960 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002961 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002962 clear_tv(rettv);
2963 return FAIL;
2964 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002965 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002966 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002969 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002971 int error = FALSE;
2972
Bram Moolenaar13106602020-10-04 16:06:05 +02002973 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002974 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002975 else if (vim9script)
2976 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002977 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002979 if (error || !op_falsy || !result)
2980 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002981 if (error)
2982 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984
2985 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002986 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002988 if (op_falsy)
2989 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002990 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002991 {
mityu4ac198c2021-05-28 17:52:40 +02002992 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002993 clear_tv(rettv);
2994 return FAIL;
2995 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002996 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002997 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002998 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002999 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003000 {
3001 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003003 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003004 if (!op_falsy || !result)
3005 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003007 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003009 /*
3010 * Check for the ":".
3011 */
3012 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3013 if (*p != ':')
3014 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003015 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003016 if (evaluate && result)
3017 clear_tv(rettv);
3018 evalarg_used->eval_flags = orig_flags;
3019 return FAIL;
3020 }
3021 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003022 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003023 else
3024 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003025 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003026 {
3027 error_white_both(p, 1);
3028 clear_tv(rettv);
3029 evalarg_used->eval_flags = orig_flags;
3030 return FAIL;
3031 }
3032 *arg = p;
3033 }
3034
3035 /*
3036 * Get the third variable. Recursive!
3037 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003038 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003039 {
mityu4ac198c2021-05-28 17:52:40 +02003040 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003041 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003042 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003043 return FAIL;
3044 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003045 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3046 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003047 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003048 if (eval1(arg, &var2, evalarg_used) == FAIL)
3049 {
3050 if (evaluate && result)
3051 clear_tv(rettv);
3052 evalarg_used->eval_flags = orig_flags;
3053 return FAIL;
3054 }
3055 if (evaluate && !result)
3056 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003058
3059 if (evalarg == NULL)
3060 clear_evalarg(&local_evalarg, NULL);
3061 else
3062 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064
3065 return OK;
3066}
3067
3068/*
3069 * Handle first level expression:
3070 * expr2 || expr2 || expr2 logical OR
3071 *
3072 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003073 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 *
3075 * Return OK or FAIL.
3076 */
3077 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003078eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003080 char_u *p;
3081 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
3083 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003084 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003086 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 return FAIL;
3088
3089 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003090 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003092 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003093 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003095 evalarg_T *evalarg_used = evalarg;
3096 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003097 int evaluate;
3098 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003099 long result = FALSE;
3100 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003101 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003102 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003103
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003104 if (evalarg == NULL)
3105 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003106 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003107 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003108 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003109 orig_flags = evalarg_used->eval_flags;
3110 evaluate = orig_flags & EVAL_EVALUATE;
3111 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003112 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003113 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003114 result = tv_get_bool_chk(rettv, &error);
3115 else if (tv_get_number_chk(rettv, &error) != 0)
3116 result = TRUE;
3117 clear_tv(rettv);
3118 if (error)
3119 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
3121
3122 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003123 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003125 while (p[0] == '|' && p[1] == '|')
3126 {
3127 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003128 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003129 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003130 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003131 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003132 {
3133 error_white_both(p, 2);
3134 clear_tv(rettv);
3135 return FAIL;
3136 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003137 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003138 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003139
3140 /*
3141 * Get the second variable.
3142 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003143 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003144 {
mityu4ac198c2021-05-28 17:52:40 +02003145 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003146 clear_tv(rettv);
3147 return FAIL;
3148 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003149 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3150 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003151 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003152 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003153 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003154
3155 /*
3156 * Compute the result.
3157 */
3158 if (evaluate && !result)
3159 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003160 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003161 result = tv_get_bool_chk(&var2, &error);
3162 else if (tv_get_number_chk(&var2, &error) != 0)
3163 result = TRUE;
3164 clear_tv(&var2);
3165 if (error)
3166 return FAIL;
3167 }
3168 if (evaluate)
3169 {
3170 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003171 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003172 rettv->v_type = VAR_BOOL;
3173 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003174 }
3175 else
3176 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003177 rettv->v_type = VAR_NUMBER;
3178 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003179 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003180 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003181
3182 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003184
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003185 if (evalarg == NULL)
3186 clear_evalarg(&local_evalarg, NULL);
3187 else
3188 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
3190
3191 return OK;
3192}
3193
3194/*
3195 * Handle second level expression:
3196 * expr3 && expr3 && expr3 logical AND
3197 *
3198 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003199 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 *
3201 * Return OK or FAIL.
3202 */
3203 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003204eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003206 char_u *p;
3207 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003210 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003212 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 return FAIL;
3214
3215 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003216 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003218 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003219 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003221 evalarg_T *evalarg_used = evalarg;
3222 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003223 int orig_flags;
3224 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003225 long result = TRUE;
3226 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003227 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003228 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003229
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003230 if (evalarg == NULL)
3231 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003232 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003233 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003234 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003235 orig_flags = evalarg_used->eval_flags;
3236 evaluate = orig_flags & EVAL_EVALUATE;
3237 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003238 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003239 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003240 result = tv_get_bool_chk(rettv, &error);
3241 else if (tv_get_number_chk(rettv, &error) == 0)
3242 result = FALSE;
3243 clear_tv(rettv);
3244 if (error)
3245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247
3248 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003249 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003251 while (p[0] == '&' && p[1] == '&')
3252 {
3253 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003254 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003255 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003256 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003257 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003258 {
3259 error_white_both(p, 2);
3260 clear_tv(rettv);
3261 return FAIL;
3262 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003263 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003264 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003265
3266 /*
3267 * Get the second variable.
3268 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003269 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003270 {
mityu4ac198c2021-05-28 17:52:40 +02003271 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003272 clear_tv(rettv);
3273 return FAIL;
3274 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003275 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3276 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003277 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003278 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003279 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003280 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003281
3282 /*
3283 * Compute the result.
3284 */
3285 if (evaluate && result)
3286 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003287 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003288 result = tv_get_bool_chk(&var2, &error);
3289 else if (tv_get_number_chk(&var2, &error) == 0)
3290 result = FALSE;
3291 clear_tv(&var2);
3292 if (error)
3293 return FAIL;
3294 }
3295 if (evaluate)
3296 {
3297 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003298 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003299 rettv->v_type = VAR_BOOL;
3300 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003301 }
3302 else
3303 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003304 rettv->v_type = VAR_NUMBER;
3305 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003306 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003307 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003308
3309 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003311
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003312 if (evalarg == NULL)
3313 clear_evalarg(&local_evalarg, NULL);
3314 else
3315 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317
3318 return OK;
3319}
3320
3321/*
3322 * Handle third level expression:
3323 * var1 == var2
3324 * var1 =~ var2
3325 * var1 != var2
3326 * var1 !~ var2
3327 * var1 > var2
3328 * var1 >= var2
3329 * var1 < var2
3330 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003331 * var1 is var2
3332 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 *
3334 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003335 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 *
3337 * Return OK or FAIL.
3338 */
3339 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003340eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003343 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003344 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003346 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003349 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003351 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 return FAIL;
3353
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003354 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003355
Bram Moolenaar696ba232020-07-29 21:20:41 +02003356 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003359 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003361 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003363 typval_T var2;
3364 int ic;
3365 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003366 int evaluate = evalarg == NULL
3367 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003368 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003369
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003370 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003371 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003372 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003373 p = *arg;
3374 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003375 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3376 {
mityu4ac198c2021-05-28 17:52:40 +02003377 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003378 clear_tv(rettv);
3379 return FAIL;
3380 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003381
Bram Moolenaar696ba232020-07-29 21:20:41 +02003382 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3383 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003384 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003385 clear_tv(rettv);
3386 return FAIL;
3387 }
3388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003389 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (p[len] == '?')
3391 {
3392 ic = TRUE;
3393 ++len;
3394 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003395 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 else if (p[len] == '#')
3397 {
3398 ic = FALSE;
3399 ++len;
3400 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003401 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003403 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
3405 /*
3406 * Get the second variable.
3407 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003408 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3409 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003410 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003411 clear_tv(rettv);
3412 return FAIL;
3413 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003414 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003415 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003417 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 return FAIL;
3419 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003420 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003421 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003422 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003423
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003424 // use the line of the comparison for messages
3425 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003426 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003427 {
3428 ret = FAIL;
3429 clear_tv(rettv);
3430 }
3431 else
3432 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003433 clear_tv(&var2);
3434 return ret;
3435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437
3438 return OK;
3439}
3440
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003441/*
3442 * Make a copy of blob "tv1" and append blob "tv2".
3443 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 void
3445eval_addblob(typval_T *tv1, typval_T *tv2)
3446{
3447 blob_T *b1 = tv1->vval.v_blob;
3448 blob_T *b2 = tv2->vval.v_blob;
3449 blob_T *b = blob_alloc();
3450 int i;
3451
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003452 if (b == NULL)
3453 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003455 for (i = 0; i < blob_len(b1); i++)
3456 ga_append(&b->bv_ga, blob_get(b1, i));
3457 for (i = 0; i < blob_len(b2); i++)
3458 ga_append(&b->bv_ga, blob_get(b2, i));
3459
3460 clear_tv(tv1);
3461 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462}
3463
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003464/*
3465 * Make a copy of list "tv1" and append list "tv2".
3466 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 int
3468eval_addlist(typval_T *tv1, typval_T *tv2)
3469{
3470 typval_T var3;
3471
3472 // concatenate Lists
3473 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3474 {
3475 clear_tv(tv1);
3476 clear_tv(tv2);
3477 return FAIL;
3478 }
3479 clear_tv(tv1);
3480 *tv1 = var3;
3481 return OK;
3482}
3483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003485 * Handle the bitwise left/right shift operator expression:
3486 * var1 << var2
3487 * var1 >> var2
3488 *
3489 * "arg" must point to the first non-white of the expression.
3490 * "arg" is advanced to just after the recognized expression.
3491 *
3492 * Return OK or FAIL.
3493 */
3494 static int
3495eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3496{
3497 /*
3498 * Get the first expression.
3499 */
3500 if (eval6(arg, rettv, evalarg) == FAIL)
3501 return FAIL;
3502
3503 /*
3504 * Repeat computing, until no '<<' or '>>' is following.
3505 */
3506 for (;;)
3507 {
3508 char_u *p;
3509 int getnext;
3510 exprtype_T type;
3511 int evaluate;
3512 typval_T var2;
3513 int vim9script;
3514
3515 p = eval_next_non_blank(*arg, evalarg, &getnext);
3516 if (p[0] == '<' && p[1] == '<')
3517 type = EXPR_LSHIFT;
3518 else if (p[0] == '>' && p[1] == '>')
3519 type = EXPR_RSHIFT;
3520 else
3521 return OK;
3522
3523 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003524 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3525 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003526 {
3527 // left operand should be a number
3528 emsg(_(e_bitshift_ops_must_be_number));
3529 clear_tv(rettv);
3530 return FAIL;
3531 }
3532
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003533 vim9script = in_vim9script();
3534 if (getnext)
3535 {
3536 *arg = eval_next_line(*arg, evalarg);
3537 p = *arg;
3538 }
3539 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3540 {
3541 error_white_both(*arg, 2);
3542 clear_tv(rettv);
3543 return FAIL;
3544 }
3545
3546 /*
3547 * Get the second variable.
3548 */
3549 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3550 {
3551 error_white_both(p, 2);
3552 clear_tv(rettv);
3553 return FAIL;
3554 }
3555 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3556 if (eval6(arg, &var2, evalarg) == FAIL)
3557 {
3558 clear_tv(rettv);
3559 return FAIL;
3560 }
3561
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003562 if (evaluate)
3563 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003564 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3565 {
3566 // right operand should be a positive number
3567 if (var2.v_type != VAR_NUMBER)
3568 emsg(_(e_bitshift_ops_must_be_number));
3569 else
3570 emsg(_(e_bitshift_ops_must_be_positive));
3571 clear_tv(rettv);
3572 clear_tv(&var2);
3573 return FAIL;
3574 }
3575
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003576 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3577 // shifting more bits than we have always results in zero
3578 rettv->vval.v_number = 0;
3579 else if (type == EXPR_LSHIFT)
3580 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003581 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003582 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003583 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003584 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003585 }
3586
3587 clear_tv(&var2);
3588 }
3589
3590 return OK;
3591}
3592
3593/*
3594 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003595 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003597 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003598 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 *
3600 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003601 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 *
3603 * Return OK or FAIL.
3604 */
3605 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003606eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003609 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003611 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 return FAIL;
3613
3614 /*
3615 * Repeat computing, until no '+', '-' or '.' is following.
3616 */
3617 for (;;)
3618 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003619 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003620 int getnext;
3621 char_u *p;
3622 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003623 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003624 int concat;
3625 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003626 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003627
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003628 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003629 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003630 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003631 p = eval_next_non_blank(*arg, evalarg, &getnext);
3632 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003633 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003634 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3635 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003637 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3638 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003639
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003640 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003641 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003642 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003643 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003644 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003645 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003646 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003647 {
mityu4ac198c2021-05-28 17:52:40 +02003648 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003649 clear_tv(rettv);
3650 return FAIL;
3651 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003652 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003653 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003654 if ((op != '+' || (rettv->v_type != VAR_LIST
3655 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003656 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003657 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003658 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003659 int error = FALSE;
3660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003661 // For "list + ...", an illegal use of the first operand as
3662 // a number cannot be determined before evaluating the 2nd
3663 // operand: if this is also a list, all is ok.
3664 // For "something . ...", "something - ..." or "non-list + ...",
3665 // we know that the first operand needs to be a string or number
3666 // without evaluating the 2nd operand. So check before to avoid
3667 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003668 if (op != '.')
3669 tv_get_number_chk(rettv, &error);
3670 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003671 {
3672 clear_tv(rettv);
3673 return FAIL;
3674 }
3675 }
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 /*
3678 * Get the second variable.
3679 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003680 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003681 {
mityu89dcb4d2021-05-28 13:50:17 +02003682 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003683 clear_tv(rettv);
3684 return FAIL;
3685 }
3686 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003687 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003689 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 return FAIL;
3691 }
3692
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003693 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
3695 /*
3696 * Compute the result.
3697 */
3698 if (op == '.')
3699 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003700 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3701 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003702 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003703
Bram Moolenaar2e086612020-08-25 22:37:48 +02003704 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003705 || var2.v_type == VAR_CHANNEL
3706 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003707 semsg(_(e_using_invalid_value_as_string_str),
3708 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003709 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003710 {
3711 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3712 var2.vval.v_float);
3713 s2 = buf2;
3714 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003715 else
3716 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003717 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003718 {
3719 clear_tv(rettv);
3720 clear_tv(&var2);
3721 return FAIL;
3722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003723 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 clear_tv(rettv);
3725 rettv->v_type = VAR_STRING;
3726 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003728 else if (op == '+' && rettv->v_type == VAR_BLOB
3729 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003731 else if (op == '+' && rettv->v_type == VAR_LIST
3732 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003733 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003735 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 else
3738 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003739 int error = FALSE;
3740 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003741 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003742
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003743 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003745 f1 = rettv->vval.v_float;
3746 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003747 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003748 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003749 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003750 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003751 if (error)
3752 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003753 // This can only happen for "list + non-list" or
3754 // "blob + non-blob". For "non-list + ..." or
3755 // "something - ...", we returned before evaluating the
3756 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003757 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003758 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003759 return FAIL;
3760 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003761 if (var2.v_type == VAR_FLOAT)
3762 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003763 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003764 if (var2.v_type == VAR_FLOAT)
3765 {
3766 f2 = var2.vval.v_float;
3767 n2 = 0;
3768 }
3769 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003770 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003771 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003772 if (error)
3773 {
3774 clear_tv(rettv);
3775 clear_tv(&var2);
3776 return FAIL;
3777 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003778 if (rettv->v_type == VAR_FLOAT)
3779 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003783 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003784 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3785 {
3786 if (op == '+')
3787 f1 = f1 + f2;
3788 else
3789 f1 = f1 - f2;
3790 rettv->v_type = VAR_FLOAT;
3791 rettv->vval.v_float = f1;
3792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003794 {
3795 if (op == '+')
3796 n1 = n1 + n2;
3797 else
3798 n1 = n1 - n2;
3799 rettv->v_type = VAR_NUMBER;
3800 rettv->vval.v_number = n1;
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 }
3806 return OK;
3807}
3808
3809/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003810 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 * * number multiplication
3812 * / number division
3813 * % number modulo
3814 *
3815 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003816 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 *
3818 * Return OK or FAIL.
3819 */
3820 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003821eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003822 char_u **arg,
3823 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003824 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003825 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003827 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003830 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003832 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 return FAIL;
3834
3835 /*
3836 * Repeat computing, until no '*', '/' or '%' is following.
3837 */
3838 for (;;)
3839 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003840 int evaluate;
3841 int getnext;
3842 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003843 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003844 int op;
3845 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003846 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003847 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003848
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003849 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003850 p = eval_next_non_blank(*arg, evalarg, &getnext);
3851 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003852 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003854
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003855 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003856 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003857 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003858 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003859 {
3860 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3861 {
mityu4ac198c2021-05-28 17:52:40 +02003862 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003863 clear_tv(rettv);
3864 return FAIL;
3865 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003866 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003869 f1 = 0;
3870 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003871 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003872 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003874 if (rettv->v_type == VAR_FLOAT)
3875 {
3876 f1 = rettv->vval.v_float;
3877 use_float = TRUE;
3878 n1 = 0;
3879 }
3880 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003881 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 if (error)
3884 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886 else
3887 n1 = 0;
3888
3889 /*
3890 * Get the second variable.
3891 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003892 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3893 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003894 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003895 clear_tv(rettv);
3896 return FAIL;
3897 }
3898 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003899 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 return FAIL;
3901
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003902 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003904 if (var2.v_type == VAR_FLOAT)
3905 {
3906 if (!use_float)
3907 {
3908 f1 = n1;
3909 use_float = TRUE;
3910 }
3911 f2 = var2.vval.v_float;
3912 n2 = 0;
3913 }
3914 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003916 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003917 clear_tv(&var2);
3918 if (error)
3919 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003920 if (use_float)
3921 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
3924 /*
3925 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003926 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003928 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003930 if (op == '*')
3931 f1 = f1 * f2;
3932 else if (op == '/')
3933 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003934#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003935 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003936 if (f2 == 0.0)
3937 {
3938 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003939 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003940 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003941 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003942 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003943 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003944 }
3945 else
3946 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003947#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003948 // We rely on the floating point library to handle divide
3949 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003950 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003951#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003954 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003955 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003956 return FAIL;
3957 }
3958 rettv->v_type = VAR_FLOAT;
3959 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 else
3962 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003963 int failed = FALSE;
3964
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003965 if (op == '*')
3966 n1 = n1 * n2;
3967 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003968 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003970 n1 = num_modulus(n1, n2, &failed);
3971 if (failed)
3972 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003974 rettv->v_type = VAR_NUMBER;
3975 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 }
3979
3980 return OK;
3981}
3982
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003983/*
3984 * Handle a type cast before a base level expression.
3985 * "arg" must point to the first non-white of the expression.
3986 * "arg" is advanced to just after the recognized expression.
3987 * Return OK or FAIL.
3988 */
3989 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003990eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003991 char_u **arg,
3992 typval_T *rettv,
3993 evalarg_T *evalarg,
3994 int want_string) // after "." operator
3995{
3996 type_T *want_type = NULL;
3997 garray_T type_list; // list of pointers to allocated types
3998 int res;
3999 int evaluate = evalarg == NULL ? 0
4000 : (evalarg->eval_flags & EVAL_EVALUATE);
4001
4002 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004003 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4004 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004005 {
4006 ++*arg;
4007 ga_init2(&type_list, sizeof(type_T *), 10);
4008 want_type = parse_type(arg, &type_list, TRUE);
4009 if (want_type == NULL && (evaluate || **arg != '>'))
4010 {
4011 clear_type_list(&type_list);
4012 return FAIL;
4013 }
4014
4015 if (**arg != '>')
4016 {
4017 if (*skipwhite(*arg) == '>')
4018 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4019 else
4020 emsg(_(e_missing_gt));
4021 clear_type_list(&type_list);
4022 return FAIL;
4023 }
4024 ++*arg;
4025 *arg = skipwhite_and_linebreak(*arg, evalarg);
4026 }
4027
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004028 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004029
4030 if (want_type != NULL && evaluate)
4031 {
4032 if (res == OK)
4033 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004034 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4035 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004036
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004037 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004038 {
4039 if (want_type == &t_bool && actual != &t_bool
4040 && (actual->tt_flags & TTFLAG_BOOL_OK))
4041 {
4042 int n = tv2bool(rettv);
4043
4044 // can use "0" and "1" for boolean in some places
4045 clear_tv(rettv);
4046 rettv->v_type = VAR_BOOL;
4047 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4048 }
4049 else
4050 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004051 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004052
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004053 res = check_type(want_type, actual, TRUE, where);
4054 }
4055 }
4056 }
4057 clear_type_list(&type_list);
4058 }
4059
4060 return res;
4061}
4062
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004063 int
4064eval_leader(char_u **arg, int vim9)
4065{
4066 char_u *s = *arg;
4067 char_u *p = *arg;
4068
4069 while (*p == '!' || *p == '-' || *p == '+')
4070 {
4071 char_u *n = skipwhite(p + 1);
4072
4073 // ++, --, -+ and +- are not accepted in Vim9 script
4074 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4075 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004076 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004077 return FAIL;
4078 }
4079 p = n;
4080 }
4081 *arg = p;
4082 return OK;
4083}
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004086 * Check for a predefined value "true", "false" and "null.*".
4087 * Return OK when recognized.
4088 */
4089 int
4090handle_predefined(char_u *s, int len, typval_T *rettv)
4091{
4092 switch (len)
4093 {
4094 case 4: if (STRNCMP(s, "true", 4) == 0)
4095 {
4096 rettv->v_type = VAR_BOOL;
4097 rettv->vval.v_number = VVAL_TRUE;
4098 return OK;
4099 }
4100 if (STRNCMP(s, "null", 4) == 0)
4101 {
4102 rettv->v_type = VAR_SPECIAL;
4103 rettv->vval.v_number = VVAL_NULL;
4104 return OK;
4105 }
4106 break;
4107 case 5: if (STRNCMP(s, "false", 5) == 0)
4108 {
4109 rettv->v_type = VAR_BOOL;
4110 rettv->vval.v_number = VVAL_FALSE;
4111 return OK;
4112 }
4113 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004114 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4115 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004116#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004117 rettv->v_type = VAR_JOB;
4118 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004119#else
4120 rettv->v_type = VAR_SPECIAL;
4121 rettv->vval.v_number = VVAL_NULL;
4122#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004123 return OK;
4124 }
4125 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004126 case 9:
4127 if (STRNCMP(s, "null_", 5) != 0)
4128 break;
4129 if (STRNCMP(s + 5, "list", 4) == 0)
4130 {
4131 rettv->v_type = VAR_LIST;
4132 rettv->vval.v_list = NULL;
4133 return OK;
4134 }
4135 if (STRNCMP(s + 5, "dict", 4) == 0)
4136 {
4137 rettv->v_type = VAR_DICT;
4138 rettv->vval.v_dict = NULL;
4139 return OK;
4140 }
4141 if (STRNCMP(s + 5, "blob", 4) == 0)
4142 {
4143 rettv->v_type = VAR_BLOB;
4144 rettv->vval.v_blob = NULL;
4145 return OK;
4146 }
4147 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004148 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4149 {
4150 rettv->v_type = VAR_CLASS;
4151 rettv->vval.v_class = NULL;
4152 return OK;
4153 }
4154 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004155 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4156 {
4157 rettv->v_type = VAR_STRING;
4158 rettv->vval.v_string = NULL;
4159 return OK;
4160 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004161 if (STRNCMP(s, "null_object", 11) == 0)
4162 {
4163 rettv->v_type = VAR_OBJECT;
4164 rettv->vval.v_object = NULL;
4165 return OK;
4166 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004167 break;
4168 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004169 if (STRNCMP(s, "null_channel", 12) == 0)
4170 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004171#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004172 rettv->v_type = VAR_CHANNEL;
4173 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004174#else
4175 rettv->v_type = VAR_SPECIAL;
4176 rettv->vval.v_number = VVAL_NULL;
4177#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004178 return OK;
4179 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004180 if (STRNCMP(s, "null_partial", 12) == 0)
4181 {
4182 rettv->v_type = VAR_PARTIAL;
4183 rettv->vval.v_partial = NULL;
4184 return OK;
4185 }
4186 break;
4187 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4188 {
4189 rettv->v_type = VAR_FUNC;
4190 rettv->vval.v_string = NULL;
4191 return OK;
4192 }
4193 break;
4194 }
4195 return FAIL;
4196}
4197
4198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 * Handle sixth level expression:
4200 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004201 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004202 * "string" string constant
4203 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 * &option-name option value
4205 * @r register contents
4206 * identifier variable value
4207 * function() function call
4208 * $VAR environment variable
4209 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004210 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004212 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004213 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 *
4215 * Also handle:
4216 * ! in front logical NOT
4217 * - in front unary minus
4218 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004219 * trailing [] subscript in String or List
4220 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004221 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 *
4223 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004224 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 *
4226 * Return OK or FAIL.
4227 */
4228 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004229eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004230 char_u **arg,
4231 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004232 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004235 int evaluate = evalarg != NULL
4236 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 int len;
4238 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004239 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 char_u *start_leader, *end_leader;
4241 int ret = OK;
4242 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004243 static int recurse = 0;
4244 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004248 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004250 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004253 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 */
4255 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004256 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 end_leader = *arg;
4259
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004260 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004261 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004262 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004263 ++*arg;
4264 return FAIL;
4265 }
4266
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004267 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004268 // and crash. With MSVC the stack is smaller.
4269 if (recurse ==
4270#ifdef _MSC_VER
4271 300
4272#else
4273 1000
4274#endif
4275 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004276 {
4277 semsg(_(e_expression_too_recursive_str), *arg);
4278 return FAIL;
4279 }
4280 ++recurse;
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 switch (**arg)
4283 {
4284 /*
4285 * Number constant.
4286 */
4287 case '0':
4288 case '1':
4289 case '2':
4290 case '3':
4291 case '4':
4292 case '5':
4293 case '6':
4294 case '7':
4295 case '8':
4296 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004297 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004298
4299 // Apply prefixed "-" and "+" now. Matters especially when
4300 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004301 if (ret == OK && evaluate && end_leader > start_leader
4302 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004303 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 break;
4305
4306 /*
4307 * String constant: "string".
4308 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004309 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 break;
4311
4312 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004313 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004315 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004316 break;
4317
4318 /*
4319 * List: [expr, expr]
4320 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004321 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 break;
4323
4324 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004325 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004326 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004327 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004328 {
4329 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4330 }
4331 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004332 {
4333 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004334 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004335 }
4336 else
4337 ret = NOTDONE;
4338 break;
4339
4340 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004341 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004342 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004343 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004344 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004345 ret = NOTDONE;
4346 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004347 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004348 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004349 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004350 break;
4351
4352 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004353 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004355 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 break;
4357
4358 /*
4359 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004360 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
LemonBoy2eaef102022-05-06 13:14:50 +01004362 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4363 ret = eval_interp_string(arg, rettv, evaluate);
4364 else
4365 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 break;
4367
4368 /*
4369 * Register contents: @r.
4370 */
4371 case '@': ++*arg;
4372 if (evaluate)
4373 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004374 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004375 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004376 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004377 emsg_invreg(**arg);
4378 else
4379 {
4380 rettv->v_type = VAR_STRING;
4381 rettv->vval.v_string = get_reg_contents(**arg,
4382 GREG_EXPR_SRC);
4383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 }
4385 if (**arg != NUL)
4386 ++*arg;
4387 break;
4388
4389 /*
4390 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004391 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004393 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004394 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004395 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004396 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004397 if (ret == OK && evaluate)
4398 {
4399 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4400
Bram Moolenaara9931532021-06-12 15:58:16 +02004401 // Compile it here to get the return type. The return
4402 // type is optional, when it's missing use t_unknown.
4403 // This is recognized in compile_return().
4404 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4405 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004406 if (compile_def_function(ufunc, FALSE,
4407 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004408 {
4409 clear_tv(rettv);
4410 ret = FAIL;
4411 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004412 }
4413 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004414 if (ret == NOTDONE)
4415 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004416 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004417 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004418
Bram Moolenaar9215f012020-06-27 21:18:00 +02004419 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004420 if (**arg == ')')
4421 ++*arg;
4422 else if (ret == OK)
4423 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004424 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004425 clear_tv(rettv);
4426 ret = FAIL;
4427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 break;
4430
Bram Moolenaar8c711452005-01-14 21:53:12 +00004431 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 break;
4433 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004434
4435 if (ret == NOTDONE)
4436 {
4437 /*
4438 * Must be a variable or function name.
4439 * Can also be a curly-braces kind of name: {expr}.
4440 */
4441 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004442 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004443 if (alias != NULL)
4444 s = alias;
4445
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004446 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004447 ret = FAIL;
4448 else
4449 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004450 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4451
Bram Moolenaar4525a572022-02-13 11:57:33 +00004452 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004453 {
4454 emsg(_(e_cannot_use_underscore_here));
4455 ret = FAIL;
4456 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004457 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004458 && s[0] == 's' && s[1] == ':')
4459 {
4460 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4461 ret = FAIL;
4462 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004463 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004464 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004465 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004466 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004467 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004468 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004469 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004470 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004471 // get the value of "true", "false", etc. or a variable
4472 ret = FAIL;
4473 if (vim9script)
4474 ret = handle_predefined(s, len, rettv);
4475 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004476 {
4477 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004478 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004479 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004480 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004481 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004482 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004483 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004484 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004485 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004486 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004488 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004489 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 }
4491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004492 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4493 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004494 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004495 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 /*
4498 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4499 */
4500 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004501 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004502
4503 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004504 return ret;
4505}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004506
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004507/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004508 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004509 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004510 * Adjusts "end_leaderp" until it is at "start_leader".
4511 */
4512 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004513eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004514 typval_T *rettv,
4515 int numeric_only,
4516 char_u *start_leader,
4517 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004518{
4519 char_u *end_leader = *end_leaderp;
4520 int ret = OK;
4521 int error = FALSE;
4522 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004523 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004524 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004525 float_T f = 0.0;
4526
4527 if (rettv->v_type == VAR_FLOAT)
4528 f = rettv->vval.v_float;
4529 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004530 {
4531 while (VIM_ISWHITE(end_leader[-1]))
4532 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004533 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004534 val = tv2bool(rettv);
4535 else
4536 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004537 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004538 if (error)
4539 {
4540 clear_tv(rettv);
4541 ret = FAIL;
4542 }
4543 else
4544 {
4545 while (end_leader > start_leader)
4546 {
4547 --end_leader;
4548 if (*end_leader == '!')
4549 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004550 if (numeric_only)
4551 {
4552 ++end_leader;
4553 break;
4554 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004555 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004556 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004557 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004558 {
4559 rettv->v_type = VAR_BOOL;
4560 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4561 }
4562 else
4563 f = !f;
4564 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004565 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004566 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004567 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004568 type = VAR_BOOL;
4569 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004570 }
4571 else if (*end_leader == '-')
4572 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004573 if (rettv->v_type == VAR_FLOAT)
4574 f = -f;
4575 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_NUMBER;
4579 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004580 }
4581 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004582 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004584 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004585 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004587 else
4588 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004589 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004590 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004591 rettv->v_type = type;
4592 else
4593 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004594 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004597 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 return ret;
4599}
4600
4601/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004602 * Call the function referred to in "rettv".
4603 */
4604 static int
4605call_func_rettv(
4606 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004607 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004608 typval_T *rettv,
4609 int evaluate,
4610 dict_T *selfdict,
4611 typval_T *basetv)
4612{
4613 partial_T *pt = NULL;
4614 funcexe_T funcexe;
4615 typval_T functv;
4616 char_u *s;
4617 int ret;
4618
4619 // need to copy the funcref so that we can clear rettv
4620 if (evaluate)
4621 {
4622 functv = *rettv;
4623 rettv->v_type = VAR_UNKNOWN;
4624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004625 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004626 if (functv.v_type == VAR_PARTIAL)
4627 {
4628 pt = functv.vval.v_partial;
4629 s = partial_name(pt);
4630 }
4631 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004632 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004633 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004634 if (s == NULL || *s == NUL)
4635 {
4636 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004637 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004638 goto theend;
4639 }
4640 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004641 }
4642 else
4643 s = (char_u *)"";
4644
Bram Moolenaara80faa82020-04-12 19:37:17 +02004645 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004646 funcexe.fe_firstline = curwin->w_cursor.lnum;
4647 funcexe.fe_lastline = curwin->w_cursor.lnum;
4648 funcexe.fe_evaluate = evaluate;
4649 funcexe.fe_partial = pt;
4650 funcexe.fe_selfdict = selfdict;
4651 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004652 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004653
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004654theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004655 // Clear the funcref afterwards, so that deleting it while
4656 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004657 if (evaluate)
4658 clear_tv(&functv);
4659
4660 return ret;
4661}
4662
4663/*
4664 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004665 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004666 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4667 */
4668 static int
4669eval_lambda(
4670 char_u **arg,
4671 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004672 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004673 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004674{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004675 int evaluate = evalarg != NULL
4676 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004677 typval_T base = *rettv;
4678 int ret;
4679
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004680 rettv->v_type = VAR_UNKNOWN;
4681
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004682 if (**arg == '{')
4683 {
4684 // ->{lambda}()
4685 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4686 }
4687 else
4688 {
4689 // ->(lambda)()
4690 ++*arg;
4691 ret = eval1(arg, rettv, evalarg);
4692 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004693 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004694 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004695 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004696 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004697 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004698 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4699 && rettv->v_type != VAR_PARTIAL)
4700 {
4701 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4702 return FAIL;
4703 }
4704 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004705 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004706 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004707 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004708
4709 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004710 {
4711 if (verbose)
4712 {
4713 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004714 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004715 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004716 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004717 }
4718 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004719 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004720 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004721 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004722 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004723
4724 // Clear the funcref afterwards, so that deleting it while
4725 // evaluating the arguments is possible (see test55).
4726 if (evaluate)
4727 clear_tv(&base);
4728
4729 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004730}
4731
4732/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004733 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004734 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004735 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4736 */
4737 static int
4738eval_method(
4739 char_u **arg,
4740 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004741 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004742 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004743{
4744 char_u *name;
4745 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004746 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004747 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004748 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004749 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004750 int evaluate = evalarg != NULL
4751 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004752
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004753 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004754
Bram Moolenaarac92e252019-08-03 21:58:38 +02004755 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004756 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004757 if (alias != NULL)
4758 name = alias;
4759
4760 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004761 {
4762 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004763 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004764 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004765 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004766 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004767 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004768 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004769
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004770 // If there is no "(" immediately following, but there is further on,
4771 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4772 // Does not handle anything where "(" is part of the expression.
4773 *arg = skipwhite(*arg);
4774
4775 if (**arg != '(' && alias == NULL
4776 && (paren = vim_strchr(*arg, '(')) != NULL)
4777 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004778 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004779
4780 // Truncate the name a the "(". Avoid trying to get another line
4781 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004782 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004783 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4784 if (evalarg != NULL)
4785 {
4786 getline = evalarg->eval_getline;
4787 evalarg->eval_getline = NULL;
4788 }
4789
4790 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004791 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004792 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004793 *arg = name + len;
4794 ret = FAIL;
4795 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004796 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004797 {
4798 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004799 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004800 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004801
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004802 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004803 if (getline != NULL)
4804 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004805 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004806
4807 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004808 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004809 *arg = skipwhite(*arg);
4810
4811 if (**arg != '(')
4812 {
4813 if (verbose)
4814 semsg(_(e_missing_parenthesis_str), name);
4815 ret = FAIL;
4816 }
4817 else if (VIM_ISWHITE((*arg)[-1]))
4818 {
4819 if (verbose)
4820 emsg(_(e_no_white_space_allowed_before_parenthesis));
4821 ret = FAIL;
4822 }
4823 else
4824 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004825 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004826 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004827 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004828
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004829 // Clear the funcref afterwards, so that deleting it while
4830 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004831 if (evaluate)
4832 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004833 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004834
Bram Moolenaarac92e252019-08-03 21:58:38 +02004835 return ret;
4836}
4837
4838/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004839 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4840 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004841 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4842 */
4843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004844eval_index(
4845 char_u **arg,
4846 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004847 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004848 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004850 int evaluate = evalarg != NULL
4851 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004853 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004854 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004856 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004857 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004858
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004859 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4860 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004862 init_tv(&var1);
4863 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004864 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004866 /*
4867 * dict.name
4868 */
4869 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004870 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004871 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004872 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004873 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004874 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 }
4876 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004878 /*
4879 * something[idx]
4880 *
4881 * Get the (first) variable from inside the [].
4882 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004883 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004884 if (**arg == ':')
4885 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004886 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004887 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004888 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004889 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004890 semsg(_(e_white_space_required_before_and_after_str_at_str),
4891 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004892 clear_tv(&var1);
4893 return FAIL;
4894 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004895 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004896 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004897 int error = FALSE;
4898
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004899 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004900 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004901 && var1.v_type == VAR_FLOAT)
4902 {
4903 var1.vval.v_string = typval_tostring(&var1, TRUE);
4904 var1.v_type = VAR_STRING;
4905 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004906
Bram Moolenaar4525a572022-02-13 11:57:33 +00004907 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004908 tv_get_number_chk(&var1, &error);
4909 else
4910 error = tv_get_string_chk(&var1) == NULL;
4911 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004912 {
4913 // not a number or string
4914 clear_tv(&var1);
4915 return FAIL;
4916 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004917 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004918
4919 /*
4920 * Get the second variable from inside the [:].
4921 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004922 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004923 if (**arg == ':')
4924 {
4925 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004926 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004927 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004928 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004929 semsg(_(e_white_space_required_before_and_after_str_at_str),
4930 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004931 if (!empty1)
4932 clear_tv(&var1);
4933 return FAIL;
4934 }
4935 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004936 if (**arg == ']')
4937 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004938 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004940 if (!empty1)
4941 clear_tv(&var1);
4942 return FAIL;
4943 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004944 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004946 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004947 if (!empty1)
4948 clear_tv(&var1);
4949 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 return FAIL;
4951 }
4952 }
4953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004955 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 if (**arg != ']')
4957 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004958 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004959 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 clear_tv(&var1);
4961 if (range)
4962 clear_tv(&var2);
4963 return FAIL;
4964 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004965 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 }
4967
4968 if (evaluate)
4969 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004970 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004971 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004972 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004973
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004974 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004975 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004976 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004977 clear_tv(&var2);
4978 return res;
4979 }
4980 return OK;
4981}
4982
4983/*
4984 * Check if "rettv" can have an [index] or [sli:ce]
4985 */
4986 int
4987check_can_index(typval_T *rettv, int evaluate, int verbose)
4988{
4989 switch (rettv->v_type)
4990 {
4991 case VAR_FUNC:
4992 case VAR_PARTIAL:
4993 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004994 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004995 return FAIL;
4996 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004997 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004998 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004999 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005000 case VAR_BOOL:
5001 case VAR_SPECIAL:
5002 case VAR_JOB:
5003 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005004 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005005 case VAR_CLASS:
5006 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005007 if (verbose)
5008 emsg(_(e_cannot_index_special_variable));
5009 return FAIL;
5010 case VAR_UNKNOWN:
5011 case VAR_ANY:
5012 case VAR_VOID:
5013 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005014 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005015 emsg(_(e_cannot_index_special_variable));
5016 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005017 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005018 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005019
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005020 case VAR_STRING:
5021 case VAR_LIST:
5022 case VAR_DICT:
5023 case VAR_BLOB:
5024 break;
5025 case VAR_NUMBER:
5026 if (in_vim9script())
5027 emsg(_(e_cannot_index_number));
5028 break;
5029 }
5030 return OK;
5031}
5032
5033/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005034 * slice() function
5035 */
5036 void
5037f_slice(typval_T *argvars, typval_T *rettv)
5038{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005039 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005040 && ((argvars[0].v_type != VAR_STRING
5041 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005042 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005043 && check_for_list_arg(argvars, 0) == FAIL)
5044 || check_for_number_arg(argvars, 1) == FAIL
5045 || check_for_opt_number_arg(argvars, 2) == FAIL))
5046 return;
5047
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005048 if (check_can_index(argvars, TRUE, FALSE) != OK)
5049 return;
5050
5051 copy_tv(argvars, rettv);
5052 eval_index_inner(rettv, TRUE, argvars + 1,
5053 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5054 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005055}
5056
5057/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005058 * Apply index or range to "rettv".
5059 * "var1" is the first index, NULL for [:expr].
5060 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005061 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5062 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005063 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5064 */
5065 int
5066eval_index_inner(
5067 typval_T *rettv,
5068 int is_range,
5069 typval_T *var1,
5070 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005071 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005072 char_u *key,
5073 int keylen,
5074 int verbose)
5075{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005076 varnumber_T n1, n2 = 0;
5077 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005078
5079 n1 = 0;
5080 if (var1 != NULL && rettv->v_type != VAR_DICT)
5081 n1 = tv_get_number(var1);
5082
5083 if (is_range)
5084 {
5085 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005087 if (verbose)
5088 emsg(_(e_cannot_slice_dictionary));
5089 return FAIL;
5090 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005091 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005092 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005093 else
5094 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005095 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005096
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005097 switch (rettv->v_type)
5098 {
5099 case VAR_UNKNOWN:
5100 case VAR_ANY:
5101 case VAR_VOID:
5102 case VAR_FUNC:
5103 case VAR_PARTIAL:
5104 case VAR_FLOAT:
5105 case VAR_BOOL:
5106 case VAR_SPECIAL:
5107 case VAR_JOB:
5108 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005109 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005110 case VAR_CLASS:
5111 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005112 break; // not evaluating, skipping over subscript
5113
5114 case VAR_NUMBER:
5115 case VAR_STRING:
5116 {
5117 char_u *s = tv_get_string(rettv);
5118
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005120 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005121 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005122 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005123 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005124 else
5125 s = char_from_string(s, n1);
5126 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005127 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005129 // The resulting variable is a substring. If the indexes
5130 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 if (n1 < 0)
5132 {
5133 n1 = len + n1;
5134 if (n1 < 0)
5135 n1 = 0;
5136 }
5137 if (n2 < 0)
5138 n2 = len + n2;
5139 else if (n2 >= len)
5140 n2 = len;
5141 if (n1 >= len || n2 < 0 || n1 > n2)
5142 s = NULL;
5143 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005144 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005145 }
5146 else
5147 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005148 // The resulting variable is a string of a single
5149 // character. If the index is too big or negative the
5150 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 if (n1 >= len || n1 < 0)
5152 s = NULL;
5153 else
5154 s = vim_strnsave(s + n1, 1);
5155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 clear_tv(rettv);
5157 rettv->v_type = VAR_STRING;
5158 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005159 }
5160 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005162 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005163 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5164 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005165 break;
5166
5167 case VAR_LIST:
5168 if (var1 == NULL)
5169 n1 = 0;
5170 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005171 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005172 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005173 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005174 return FAIL;
5175 break;
5176
5177 case VAR_DICT:
5178 {
5179 dictitem_T *item;
5180 typval_T tmp;
5181
5182 if (key == NULL)
5183 {
5184 key = tv_get_string_chk(var1);
5185 if (key == NULL)
5186 return FAIL;
5187 }
5188
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005189 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005190
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005191 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005192 {
5193 if (verbose)
5194 {
5195 if (keylen > 0)
5196 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005197 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005198 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005199 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005200 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005201
5202 copy_tv(&item->di_tv, &tmp);
5203 clear_tv(rettv);
5204 *rettv = tmp;
5205 }
5206 break;
5207 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 return OK;
5209}
5210
5211/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005212 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005213 */
5214 char_u *
5215partial_name(partial_T *pt)
5216{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005217 if (pt != NULL)
5218 {
5219 if (pt->pt_name != NULL)
5220 return pt->pt_name;
5221 if (pt->pt_func != NULL)
5222 return pt->pt_func->uf_name;
5223 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005224 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005225}
5226
Bram Moolenaarddecc252016-04-06 22:59:37 +02005227 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005228partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005229{
5230 int i;
5231
5232 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005233 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005234 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005235 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005236 if (pt->pt_name != NULL)
5237 {
5238 func_unref(pt->pt_name);
5239 vim_free(pt->pt_name);
5240 }
5241 else
5242 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005243 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005244
Bram Moolenaar54656012021-06-09 20:50:46 +02005245 // "out_up" is no longer used, decrement refcount on partial that owns it.
5246 partial_unref(pt->pt_outer.out_up_partial);
5247
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005248 // Using pt_outer from another partial.
5249 partial_unref(pt->pt_outer_partial);
5250
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005251 // Decrease the reference count for the context of a closure. If down
5252 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005253 if (pt->pt_funcstack != NULL)
5254 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005255 --pt->pt_funcstack->fs_refcount;
5256 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005257 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005258 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005259 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5260 if (pt->pt_loopvars[i] != NULL)
5261 {
5262 --pt->pt_loopvars[i]->lvs_refcount;
5263 loopvars_check_refcount(pt->pt_loopvars[i]);
5264 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005265
Bram Moolenaarddecc252016-04-06 22:59:37 +02005266 vim_free(pt);
5267}
5268
5269/*
5270 * Unreference a closure: decrement the reference count and free it when it
5271 * becomes zero.
5272 */
5273 void
5274partial_unref(partial_T *pt)
5275{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005276 if (pt == NULL)
5277 return;
5278
5279 int done = FALSE;
5280
5281 if (--pt->pt_refcount <= 0)
5282 partial_free(pt);
5283
5284 // If the reference count goes down to one, the funcstack may be the
5285 // only reference and can be freed if no other partials reference it.
5286 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005287 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005288 // careful: if the funcstack is freed it may contain this partial
5289 // and it gets freed as well
5290 if (pt->pt_funcstack != NULL)
5291 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005292
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005293 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005294 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005295 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005296
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005297 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5298 if (pt->pt_loopvars[depth] != NULL
5299 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005300 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005301 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005302 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005303}
5304
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005305/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005306 * Return the next (unique) copy ID.
5307 * Used for serializing nested structures.
5308 */
5309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005310get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005311{
5312 current_copyID += COPYID_INC;
5313 return current_copyID;
5314}
5315
5316/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005317 * Garbage collection for lists and dictionaries.
5318 *
5319 * We use reference counts to be able to free most items right away when they
5320 * are no longer used. But for composite items it's possible that it becomes
5321 * unused while the reference count is > 0: When there is a recursive
5322 * reference. Example:
5323 * :let l = [1, 2, 3]
5324 * :let d = {9: l}
5325 * :let l[1] = d
5326 *
5327 * Since this is quite unusual we handle this with garbage collection: every
5328 * once in a while find out which lists and dicts are not referenced from any
5329 * variable.
5330 *
5331 * Here is a good reference text about garbage collection (refers to Python
5332 * but it applies to all reference-counting mechanisms):
5333 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005334 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005335
5336/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005337 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005338 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005339 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005340 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005341 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005342garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005343{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005344 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005345 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005346 buf_T *buf;
5347 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005348 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005349 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005350
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005351 if (!testing)
5352 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005353 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005354 want_garbage_collect = FALSE;
5355 may_garbage_collect = FALSE;
5356 garbage_collect_at_exit = FALSE;
5357 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005358
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005359 // The execution stack can grow big, limit the size.
5360 if (exestack.ga_maxlen - exestack.ga_len > 500)
5361 {
5362 size_t new_len;
5363 char_u *pp;
5364 int n;
5365
5366 // Keep 150% of the current size, with a minimum of the growth size.
5367 n = exestack.ga_len / 2;
5368 if (n < exestack.ga_growsize)
5369 n = exestack.ga_growsize;
5370
5371 // Don't make it bigger though.
5372 if (exestack.ga_len + n < exestack.ga_maxlen)
5373 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005374 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005375 pp = vim_realloc(exestack.ga_data, new_len);
5376 if (pp == NULL)
5377 return FAIL;
5378 exestack.ga_maxlen = exestack.ga_len + n;
5379 exestack.ga_data = pp;
5380 }
5381 }
5382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 // We advance by two because we add one for items referenced through
5384 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005385 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005386
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005387 /*
5388 * 1. Go through all accessible variables and mark all lists and dicts
5389 * with copyID.
5390 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005391
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005392 // Don't free variables in the previous_funccal list unless they are only
5393 // referenced through previous_funccal. This must be first, because if
5394 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005395 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005396
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005397 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005398 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005400 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005401 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005402 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5403 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005405 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005406 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005407 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5408 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005409 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005410 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005411 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005412 abort = abort || set_ref_in_item(
5413 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005414#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005415 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005416 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5417 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005418 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005419 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005420 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5421 NULL, NULL);
5422#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005424 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005425 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005426 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5427 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005428 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005429 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005431 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005432 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005433
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005434 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005435 abort = abort || set_ref_in_functions(copyID);
5436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005437 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005439
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005440 // funcstacks keep variables for closures
5441 abort = abort || set_ref_in_funcstacks(copyID);
5442
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005443 // loopvars keep variables for loop blocks
5444 abort = abort || set_ref_in_loopvars(copyID);
5445
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005446 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005447 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005448
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005449 // callbacks in buffers
5450 abort = abort || set_ref_in_buffers(copyID);
5451
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005452 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5453 abort = abort || set_ref_in_insexpand_funcs(copyID);
5454
5455 // 'operatorfunc' callback
5456 abort = abort || set_ref_in_opfunc(copyID);
5457
5458 // 'tagfunc' callback
5459 abort = abort || set_ref_in_tagfunc(copyID);
5460
5461 // 'imactivatefunc' and 'imstatusfunc' callbacks
5462 abort = abort || set_ref_in_im_funcs(copyID);
5463
Bram Moolenaar1dced572012-04-05 16:54:08 +02005464#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005466#endif
5467
Bram Moolenaardb913952012-06-29 12:54:53 +02005468#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005469 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005470#endif
5471
5472#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005473 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005474#endif
5475
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005476#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005477 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005478 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005479#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005480#ifdef FEAT_NETBEANS_INTG
5481 abort = abort || set_ref_in_nb_channel(copyID);
5482#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005483
Bram Moolenaare3188e22016-05-31 21:13:04 +02005484#ifdef FEAT_TIMERS
5485 abort = abort || set_ref_in_timer(copyID);
5486#endif
5487
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005488#ifdef FEAT_QUICKFIX
5489 abort = abort || set_ref_in_quickfix(copyID);
5490#endif
5491
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005492#ifdef FEAT_TERMINAL
5493 abort = abort || set_ref_in_term(copyID);
5494#endif
5495
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005496#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005497 abort = abort || set_ref_in_popups(copyID);
5498#endif
5499
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005500 abort = abort || set_ref_in_classes(copyID);
5501
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005502 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005503 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005504 /*
5505 * 2. Free lists and dictionaries that are not referenced.
5506 */
5507 did_free = free_unref_items(copyID);
5508
5509 /*
5510 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005511 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005512 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005513 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005514 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005515 else if (p_verbose > 0)
5516 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005517 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005518 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005519
5520 return did_free;
5521}
5522
5523/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005524 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005525 */
5526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005527free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005528{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005529 int did_free = FALSE;
5530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005531 // Let all "free" functions know that we are here. This means no
5532 // dictionaries, lists, channels or jobs are to be freed, because we will
5533 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005534 in_free_unref_items = TRUE;
5535
5536 /*
5537 * PASS 1: free the contents of the items. We don't free the items
5538 * themselves yet, so that it is possible to decrement refcount counters
5539 */
5540
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005541 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005542 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005543
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005544 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005545 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005546
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005547 // Go through the list of objects and free items without this copyID.
5548 did_free |= object_free_nonref(copyID);
5549
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005550 // Go through the list of classes and free items without this copyID.
5551 did_free |= class_free_nonref(copyID);
5552
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005553#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005554 // Go through the list of jobs and free items without the copyID. This
5555 // must happen before doing channels, because jobs refer to channels, but
5556 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005557 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005559 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005560 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5561#endif
5562
5563 /*
5564 * PASS 2: free the items themselves.
5565 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005566 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005567 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005568 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005569
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005571 // Go through the list of jobs and free items without the copyID. This
5572 // must happen before doing channels, because jobs refer to channels, but
5573 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005574 free_unused_jobs(copyID, COPYID_MASK);
5575
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005576 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005577 free_unused_channels(copyID, COPYID_MASK);
5578#endif
5579
5580 in_free_unref_items = FALSE;
5581
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005582 return did_free;
5583}
5584
5585/*
5586 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005587 * "list_stack" is used to add lists to be marked. Can be NULL.
5588 *
5589 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005590 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005592set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005593{
5594 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005595 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005596 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005597 hashtab_T *cur_ht;
5598 ht_stack_T *ht_stack = NULL;
5599 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005600
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005601 cur_ht = ht;
5602 for (;;)
5603 {
5604 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005606 // Mark each item in the hashtab. If the item contains a hashtab
5607 // it is added to ht_stack, if it contains a list it is added to
5608 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005609 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005610 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005611 if (!HASHITEM_EMPTY(hi))
5612 {
5613 --todo;
5614 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5615 &ht_stack, list_stack);
5616 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005617 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005618
5619 if (ht_stack == NULL)
5620 break;
5621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005622 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005623 cur_ht = ht_stack->ht;
5624 tempitem = ht_stack;
5625 ht_stack = ht_stack->prev;
5626 free(tempitem);
5627 }
5628
5629 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005630}
5631
Dominique Pelle748b3082022-01-08 12:41:16 +00005632#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5633 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005634/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005635 * Mark a dict and its items with "copyID".
5636 * Returns TRUE if setting references failed somehow.
5637 */
5638 int
5639set_ref_in_dict(dict_T *d, int copyID)
5640{
5641 if (d != NULL && d->dv_copyID != copyID)
5642 {
5643 d->dv_copyID = copyID;
5644 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5645 }
5646 return FALSE;
5647}
Dominique Pelle748b3082022-01-08 12:41:16 +00005648#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005649
5650/*
5651 * Mark a list and its items with "copyID".
5652 * Returns TRUE if setting references failed somehow.
5653 */
5654 int
5655set_ref_in_list(list_T *ll, int copyID)
5656{
5657 if (ll != NULL && ll->lv_copyID != copyID)
5658 {
5659 ll->lv_copyID = copyID;
5660 return set_ref_in_list_items(ll, copyID, NULL);
5661 }
5662 return FALSE;
5663}
5664
5665/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005666 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005667 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5668 *
5669 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005670 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005671 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005672set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005673{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005674 listitem_T *li;
5675 int abort = FALSE;
5676 list_T *cur_l;
5677 list_stack_T *list_stack = NULL;
5678 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005679
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005680 cur_l = l;
5681 for (;;)
5682 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005683 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005684 // Mark each item in the list. If the item contains a hashtab
5685 // it is added to ht_stack, if it contains a list it is added to
5686 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005687 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5688 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5689 ht_stack, &list_stack);
5690 if (list_stack == NULL)
5691 break;
5692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005693 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005694 cur_l = list_stack->list;
5695 tempitem = list_stack;
5696 list_stack = list_stack->prev;
5697 free(tempitem);
5698 }
5699
5700 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005701}
5702
5703/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005704 * Mark the partial in callback 'cb' with "copyID".
5705 */
5706 int
5707set_ref_in_callback(callback_T *cb, int copyID)
5708{
5709 typval_T tv;
5710
5711 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5712 return FALSE;
5713
5714 tv.v_type = VAR_PARTIAL;
5715 tv.vval.v_partial = cb->cb_partial;
5716 return set_ref_in_item(&tv, copyID, NULL, NULL);
5717}
5718
5719/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005720 * Mark the dict "dd" with "copyID".
5721 * Also see set_ref_in_item().
5722 */
5723 static int
5724set_ref_in_item_dict(
5725 dict_T *dd,
5726 int copyID,
5727 ht_stack_T **ht_stack,
5728 list_stack_T **list_stack)
5729{
5730 if (dd == NULL || dd->dv_copyID == copyID)
5731 return FALSE;
5732
5733 // Didn't see this dict yet.
5734 dd->dv_copyID = copyID;
5735 if (ht_stack == NULL)
5736 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5737
5738 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5739 if (newitem == NULL)
5740 return TRUE;
5741
5742 newitem->ht = &dd->dv_hashtab;
5743 newitem->prev = *ht_stack;
5744 *ht_stack = newitem;
5745
5746 return FALSE;
5747}
5748
5749/*
5750 * Mark the list "ll" with "copyID".
5751 * Also see set_ref_in_item().
5752 */
5753 static int
5754set_ref_in_item_list(
5755 list_T *ll,
5756 int copyID,
5757 ht_stack_T **ht_stack,
5758 list_stack_T **list_stack)
5759{
5760 if (ll == NULL || ll->lv_copyID == copyID)
5761 return FALSE;
5762
5763 // Didn't see this list yet.
5764 ll->lv_copyID = copyID;
5765 if (list_stack == NULL)
5766 return set_ref_in_list_items(ll, copyID, ht_stack);
5767
5768 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5769 if (newitem == NULL)
5770 return TRUE;
5771
5772 newitem->list = ll;
5773 newitem->prev = *list_stack;
5774 *list_stack = newitem;
5775
5776 return FALSE;
5777}
5778
5779/*
5780 * Mark the partial "pt" with "copyID".
5781 * Also see set_ref_in_item().
5782 */
5783 static int
5784set_ref_in_item_partial(
5785 partial_T *pt,
5786 int copyID,
5787 ht_stack_T **ht_stack,
5788 list_stack_T **list_stack)
5789{
5790 if (pt == NULL || pt->pt_copyID == copyID)
5791 return FALSE;
5792
5793 // Didn't see this partial yet.
5794 pt->pt_copyID = copyID;
5795
5796 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5797
5798 if (pt->pt_dict != NULL)
5799 {
5800 typval_T dtv;
5801
5802 dtv.v_type = VAR_DICT;
5803 dtv.vval.v_dict = pt->pt_dict;
5804 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5805 }
5806
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005807 if (pt->pt_obj != NULL)
5808 {
5809 typval_T objtv;
5810
5811 objtv.v_type = VAR_OBJECT;
5812 objtv.vval.v_object = pt->pt_obj;
5813 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5814 }
5815
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005816 for (int i = 0; i < pt->pt_argc; ++i)
5817 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5818 ht_stack, list_stack);
5819 // pt_funcstack is handled in set_ref_in_funcstacks()
5820 // pt_loopvars is handled in set_ref_in_loopvars()
5821
5822 return abort;
5823}
5824
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005825#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005826/*
5827 * Mark the job "pt" with "copyID".
5828 * Also see set_ref_in_item().
5829 */
5830 static int
5831set_ref_in_item_job(
5832 job_T *job,
5833 int copyID,
5834 ht_stack_T **ht_stack,
5835 list_stack_T **list_stack)
5836{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005837 typval_T dtv;
5838
5839 if (job == NULL || job->jv_copyID == copyID)
5840 return FALSE;
5841
5842 job->jv_copyID = copyID;
5843 if (job->jv_channel != NULL)
5844 {
5845 dtv.v_type = VAR_CHANNEL;
5846 dtv.vval.v_channel = job->jv_channel;
5847 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5848 }
5849 if (job->jv_exit_cb.cb_partial != NULL)
5850 {
5851 dtv.v_type = VAR_PARTIAL;
5852 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5853 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5854 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005855
5856 return FALSE;
5857}
5858
5859/*
5860 * Mark the channel "ch" with "copyID".
5861 * Also see set_ref_in_item().
5862 */
5863 static int
5864set_ref_in_item_channel(
5865 channel_T *ch,
5866 int copyID,
5867 ht_stack_T **ht_stack,
5868 list_stack_T **list_stack)
5869{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005870 typval_T dtv;
5871
5872 if (ch == NULL || ch->ch_copyID == copyID)
5873 return FALSE;
5874
5875 ch->ch_copyID = copyID;
5876 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5877 {
5878 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5879 jq != NULL; jq = jq->jq_next)
5880 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5881 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5882 cq = cq->cq_next)
5883 if (cq->cq_callback.cb_partial != NULL)
5884 {
5885 dtv.v_type = VAR_PARTIAL;
5886 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5887 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5888 }
5889 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5890 {
5891 dtv.v_type = VAR_PARTIAL;
5892 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5893 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5894 }
5895 }
5896 if (ch->ch_callback.cb_partial != NULL)
5897 {
5898 dtv.v_type = VAR_PARTIAL;
5899 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5900 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5901 }
5902 if (ch->ch_close_cb.cb_partial != NULL)
5903 {
5904 dtv.v_type = VAR_PARTIAL;
5905 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5906 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5907 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005908
5909 return FALSE;
5910}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005911#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005912
5913/*
5914 * Mark the class "cl" with "copyID".
5915 * Also see set_ref_in_item().
5916 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005917 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005918set_ref_in_item_class(
5919 class_T *cl,
5920 int copyID,
5921 ht_stack_T **ht_stack,
5922 list_stack_T **list_stack)
5923{
5924 int abort = FALSE;
5925
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005926 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005927 return FALSE;
5928
5929 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005930 if (cl->class_members_tv != NULL)
5931 {
5932 // The "class_members_tv" table is allocated only for regular classes
5933 // and not for interfaces.
5934 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5935 abort = abort || set_ref_in_item(
5936 &cl->class_members_tv[i],
5937 copyID, ht_stack, list_stack);
5938 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005939
5940 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5941 abort = abort || set_ref_in_func(NULL,
5942 cl->class_class_functions[i], copyID);
5943
5944 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5945 abort = abort || set_ref_in_func(NULL,
5946 cl->class_obj_methods[i], copyID);
5947
5948 return abort;
5949}
5950
5951/*
5952 * Mark the object "cl" with "copyID".
5953 * Also see set_ref_in_item().
5954 */
5955 static int
5956set_ref_in_item_object(
5957 object_T *obj,
5958 int copyID,
5959 ht_stack_T **ht_stack,
5960 list_stack_T **list_stack)
5961{
5962 int abort = FALSE;
5963
5964 if (obj == NULL || obj->obj_copyID == copyID)
5965 return FALSE;
5966
5967 obj->obj_copyID = copyID;
5968
5969 // The typval_T array is right after the object_T.
5970 typval_T *mtv = (typval_T *)(obj + 1);
5971 for (int i = 0; !abort
5972 && i < obj->obj_class->class_obj_member_count; ++i)
5973 abort = abort || set_ref_in_item(mtv + i, copyID,
5974 ht_stack, list_stack);
5975
5976 return abort;
5977}
5978
5979/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005980 * Mark all lists, dicts and other container types referenced through typval
5981 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005982 * "list_stack" is used to add lists to be marked. Can be NULL.
5983 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5984 *
5985 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005986 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005988set_ref_in_item(
5989 typval_T *tv,
5990 int copyID,
5991 ht_stack_T **ht_stack,
5992 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005993{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005994 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005995
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005996 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005997 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005998 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005999 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6000 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006001
6002 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006003 return set_ref_in_item_list(tv->vval.v_list, copyID,
6004 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006005
6006 case VAR_FUNC:
6007 {
6008 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6009 break;
6010 }
6011
6012 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006013 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6014 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006015
6016 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006017#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006018 return set_ref_in_item_job(tv->vval.v_job, copyID,
6019 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006020#else
6021 break;
6022#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006023
6024 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006025#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006026 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006027 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006028#else
6029 break;
6030#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006031
6032 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006033 return set_ref_in_item_class(tv->vval.v_class, copyID,
6034 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006035
6036 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006037 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006038 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006039
6040 case VAR_UNKNOWN:
6041 case VAR_ANY:
6042 case VAR_VOID:
6043 case VAR_BOOL:
6044 case VAR_SPECIAL:
6045 case VAR_NUMBER:
6046 case VAR_FLOAT:
6047 case VAR_STRING:
6048 case VAR_BLOB:
6049 case VAR_INSTR:
6050 // Types that do not contain any other item
6051 break;
6052 }
6053
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006054 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006055}
6056
Bram Moolenaar8c711452005-01-14 21:53:12 +00006057/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 * Return a string with the string representation of a variable.
6059 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006060 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006061 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006062 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006063 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006064 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006065 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6066 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006067 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006069 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006070echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006071 typval_T *tv,
6072 char_u **tofree,
6073 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006074 int copyID,
6075 int echo_style,
6076 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006077 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006079 static int recurse = 0;
6080 char_u *r = NULL;
6081
Bram Moolenaar33570922005-01-25 22:26:29 +00006082 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006083 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006084 if (!did_echo_string_emsg)
6085 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006086 // Only give this message once for a recursive call to avoid
6087 // flooding the user with errors. And stop iterating over lists
6088 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006089 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006090 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006091 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006092 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006093 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006094 }
6095 ++recurse;
6096
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 switch (tv->v_type)
6098 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006099 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006100 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006101 {
6102 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006103 r = tv->vval.v_string;
6104 if (r == NULL)
6105 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006106 }
6107 else
6108 {
6109 *tofree = string_quote(tv->vval.v_string, FALSE);
6110 r = *tofree;
6111 }
6112 break;
6113
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006115 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006116 char_u buf[MAX_FUNC_NAME_LEN];
6117
6118 if (echo_style)
6119 {
LemonBoya5d35902022-04-29 21:15:02 +01006120 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6121 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006122 buf, MAX_FUNC_NAME_LEN);
6123 if (r == buf)
6124 {
6125 r = vim_strsave(buf);
6126 *tofree = r;
6127 }
6128 else
6129 *tofree = NULL;
6130 }
6131 else
6132 {
6133 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6134 : make_ufunc_name_readable(
6135 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6136 TRUE);
6137 r = *tofree;
6138 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006139 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006140 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006141
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006142 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006143 {
6144 partial_T *pt = tv->vval.v_partial;
6145 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006146 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006147 garray_T ga;
6148 int i;
6149 char_u *tf;
6150
6151 ga_init2(&ga, 1, 100);
6152 ga_concat(&ga, (char_u *)"function(");
6153 if (fname != NULL)
6154 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006155 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006156 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006157 && vim_isupper(fname[1]))
6158 {
6159 ga_concat(&ga, (char_u *)"'g:");
6160 ga_concat(&ga, fname + 1);
6161 }
6162 else
6163 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006164 vim_free(fname);
6165 }
6166 if (pt != NULL && pt->pt_argc > 0)
6167 {
6168 ga_concat(&ga, (char_u *)", [");
6169 for (i = 0; i < pt->pt_argc; ++i)
6170 {
6171 if (i > 0)
6172 ga_concat(&ga, (char_u *)", ");
6173 ga_concat(&ga,
6174 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6175 vim_free(tf);
6176 }
6177 ga_concat(&ga, (char_u *)"]");
6178 }
6179 if (pt != NULL && pt->pt_dict != NULL)
6180 {
6181 typval_T dtv;
6182
6183 ga_concat(&ga, (char_u *)", ");
6184 dtv.v_type = VAR_DICT;
6185 dtv.vval.v_dict = pt->pt_dict;
6186 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6187 vim_free(tf);
6188 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006189 // terminate with ')' and a NUL
6190 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006191
6192 *tofree = ga.ga_data;
6193 r = *tofree;
6194 break;
6195 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006196
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006197 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006198 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006199 break;
6200
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006202 if (tv->vval.v_list == NULL)
6203 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006204 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006205 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006206 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006207 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006208 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6209 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006210 {
6211 *tofree = NULL;
6212 r = (char_u *)"[...]";
6213 }
6214 else
6215 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006216 int old_copyID = tv->vval.v_list->lv_copyID;
6217
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006219 *tofree = list2string(tv, copyID, restore_copyID);
6220 if (restore_copyID)
6221 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006222 r = *tofree;
6223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006224 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006225
Bram Moolenaar8c711452005-01-14 21:53:12 +00006226 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006227 if (tv->vval.v_dict == NULL)
6228 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006229 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006230 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006231 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006232 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006233 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6234 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006235 {
6236 *tofree = NULL;
6237 r = (char_u *)"{...}";
6238 }
6239 else
6240 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006241 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006242
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006243 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006244 *tofree = dict2string(tv, copyID, restore_copyID);
6245 if (restore_copyID)
6246 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006247 r = *tofree;
6248 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006249 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006250
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006251 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006252 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006253 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006255 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006256 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006257 break;
6258
Bram Moolenaar835dc632016-02-07 14:27:38 +01006259 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006260 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006261#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006262 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006263 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6264 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006265 if (composite_val)
6266 {
6267 *tofree = string_quote(r, FALSE);
6268 r = *tofree;
6269 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006270#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006272
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006273 case VAR_INSTR:
6274 *tofree = NULL;
6275 r = (char_u *)"instructions";
6276 break;
6277
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006278 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006279 {
6280 class_T *cl = tv->vval.v_class;
6281 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6282 r = *tofree = alloc(len);
6283 vim_snprintf((char *)r, len, "class %s",
6284 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6285 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006286 break;
6287
6288 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006289 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006290 garray_T ga;
6291 ga_init2(&ga, 1, 50);
6292 ga_concat(&ga, (char_u *)"object of ");
6293 object_T *obj = tv->vval.v_object;
6294 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6295 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6296 : cl->class_name);
6297 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006298 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006299 ga_concat(&ga, (char_u *)" {");
6300 for (int i = 0; i < cl->class_obj_member_count; ++i)
6301 {
6302 if (i > 0)
6303 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006304 ocmember_T *m = &cl->class_obj_members[i];
6305 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006306 ga_concat(&ga, (char_u *)": ");
6307 char_u *tf = NULL;
6308 ga_concat(&ga, echo_string_core(
6309 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006310 &tf, numbuf, copyID, echo_style,
6311 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006312 vim_free(tf);
6313 }
6314 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006315 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006316
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006317 *tofree = r = ga.ga_data;
6318 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006319 break;
6320
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006321 case VAR_FLOAT:
6322 *tofree = NULL;
6323 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6324 r = numbuf;
6325 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006326
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006327 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006328 case VAR_SPECIAL:
6329 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006330 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006331 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006332 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006333
Bram Moolenaar8502c702014-06-17 12:51:16 +02006334 if (--recurse == 0)
6335 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006336 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006337}
6338
6339/*
6340 * Return a string with the string representation of a variable.
6341 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6342 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006343 * Does not put quotes around strings, as ":echo" displays values.
6344 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6345 * May return NULL.
6346 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006347 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006348echo_string(
6349 typval_T *tv,
6350 char_u **tofree,
6351 char_u *numbuf,
6352 int copyID)
6353{
6354 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6355}
6356
6357/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006358 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6359 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006360 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006361 */
6362 int
6363buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6364{
6365 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006366 char_u *t;
6367 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006368
6369 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6370 return -1;
6371
6372 if (lnum > buf->b_ml.ml_line_count)
6373 lnum = buf->b_ml.ml_line_count;
6374
6375 str = ml_get_buf(buf, lnum, FALSE);
6376 if (str == NULL)
6377 return -1;
6378
6379 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006380 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006381
Bram Moolenaar91458462021-01-13 20:08:38 +01006382 // count the number of characters
6383 t = str;
6384 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6385 t += mb_ptr2len(t);
6386
6387 // In insert mode, when the cursor is at the end of a non-empty line,
6388 // byteidx points to the NUL character immediately past the end of the
6389 // string. In this case, add one to the character count.
6390 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6391 count++;
6392
6393 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006394}
6395
6396/*
6397 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006398 * byte index. Works only for loaded buffers. Returns -1 on failure.
6399 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006400 */
6401 int
6402buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6403{
6404 char_u *str;
6405 char_u *t;
6406
6407 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6408 return -1;
6409
6410 if (lnum > buf->b_ml.ml_line_count)
6411 lnum = buf->b_ml.ml_line_count;
6412
6413 str = ml_get_buf(buf, lnum, FALSE);
6414 if (str == NULL)
6415 return -1;
6416
6417 // Convert the character offset to a byte offset
6418 t = str;
6419 while (*t != NUL && --charidx > 0)
6420 t += mb_ptr2len(t);
6421
Bram Moolenaar91458462021-01-13 20:08:38 +01006422 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006423}
6424
6425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006427 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006430var2fpos(
6431 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006432 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006433 int *fnum, // set to fnum for '0, 'A, etc.
6434 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006436 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006438 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006440 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006441 if (varp->v_type == VAR_LIST)
6442 {
6443 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006444 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006445 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006446 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006447
6448 l = varp->vval.v_list;
6449 if (l == NULL)
6450 return NULL;
6451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006452 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006453 pos.lnum = list_find_nr(l, 0L, &error);
6454 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006455 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006456 if (charcol)
6457 len = (long)mb_charlen(ml_get(pos.lnum));
6458 else
6459 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006460
Bram Moolenaarec65d772020-08-20 22:29:12 +02006461 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006462 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006463 li = list_find(l, 1L);
6464 if (li != NULL && li->li_tv.v_type == VAR_STRING
6465 && li->li_tv.vval.v_string != NULL
6466 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006467 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006468 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006469 }
6470 else
6471 {
6472 pos.col = list_find_nr(l, 1L, &error);
6473 if (error)
6474 return NULL;
6475 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006477 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006478 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006479 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006480 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006483 pos.coladd = list_find_nr(l, 2L, &error);
6484 if (error)
6485 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006486
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006487 return &pos;
6488 }
6489
Bram Moolenaarc5809432021-03-27 21:23:30 +01006490 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6491 return NULL;
6492
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006493 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006494 if (name == NULL)
6495 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006496
6497 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006498 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006499 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006500 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006501 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006502 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006503 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006504 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006505 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006506 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006507 pos = VIsual;
6508 else
6509 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006510 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006511 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006512 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006514 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006515 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6517 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006518 pos = *pp;
6519 }
6520 if (pos.lnum != 0)
6521 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006522 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006523 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6524 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006526
Bram Moolenaara5525202006-03-02 22:52:09 +00006527 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006528
Bram Moolenaar477933c2007-07-17 14:32:23 +00006529 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006530 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006531 // the "w_valid" flags are not reset when moving the cursor, but they
6532 // do matter for update_topline() and validate_botline().
6533 check_cursor_moved(curwin);
6534
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006535 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006536 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006537 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006538 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006539 // In silent Ex mode topline is zero, but that's not a valid line
6540 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006541 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006542 return &pos;
6543 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006544 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006545 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006546 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006547 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006548 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006549 return &pos;
6550 }
6551 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006552 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006554 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
6556 pos.lnum = curbuf->b_ml.ml_line_count;
6557 pos.col = 0;
6558 }
6559 else
6560 {
6561 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006562 if (charcol)
6563 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6564 else
6565 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 }
6567 return &pos;
6568 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006569 if (in_vim9script())
6570 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 return NULL;
6572}
6573
6574/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006575 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006576 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006577 * Note that the column is passed on as-is, the caller may want to decrement
6578 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006579 * If "charcol" is TRUE use the column as the character index instead of the
6580 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006581 * Return FAIL when conversion is not possible, doesn't check the position for
6582 * validity.
6583 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006585list2fpos(
6586 typval_T *arg,
6587 pos_T *posp,
6588 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006589 colnr_T *curswantp,
6590 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006591{
6592 list_T *l = arg->vval.v_list;
6593 long i = 0;
6594 long n;
6595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006596 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6597 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006598 if (arg->v_type != VAR_LIST
6599 || l == NULL
6600 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006601 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006602 return FAIL;
6603
6604 if (fnump != NULL)
6605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006606 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006607 if (n < 0)
6608 return FAIL;
6609 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006610 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006611 *fnump = n;
6612 }
6613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006614 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006615 if (n < 0)
6616 return FAIL;
6617 posp->lnum = n;
6618
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006619 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006620 if (n < 0)
6621 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006622 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006623 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006624 if (charcol)
6625 {
6626 buf_T *buf;
6627
6628 // Get the text for the specified line in a loaded buffer
6629 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6630 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6631 return FAIL;
6632
Bram Moolenaar79f23442022-10-10 12:42:57 +01006633 n = buf_charidx_to_byteidx(buf,
6634 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006635 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006636 posp->col = n;
6637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006638 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006639 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006640 posp->coladd = 0;
6641 else
6642 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006643
Bram Moolenaar493c1782014-05-28 14:34:46 +02006644 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006646
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006647 return OK;
6648}
6649
6650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 * Get the length of an environment variable name.
6652 * Advance "arg" to the first character after the name.
6653 * Return 0 for error.
6654 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006656get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 char_u *p;
6659 int len;
6660
6661 for (p = *arg; vim_isIDc(*p); ++p)
6662 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006663 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 return 0;
6665
6666 len = (int)(p - *arg);
6667 *arg = p;
6668 return len;
6669}
6670
6671/*
6672 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006673 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 * Return 0 if something is wrong.
6675 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006677get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678{
6679 char_u *p;
6680 int len;
6681
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006682 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006684 {
6685 if (*p == ':')
6686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006687 // "s:" is start of "s:var", but "n:" is not and can be used in
6688 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006689 len = (int)(p - *arg);
6690 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6691 || len > 1)
6692 break;
6693 }
6694 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006695 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 return 0;
6697
6698 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006699 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701 return len;
6702}
6703
6704/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006705 * Get the length of the name of a variable or function.
6706 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006708 * Return -1 if curly braces expansion failed.
6709 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 * If the name contains 'magic' {}'s, expand them and return the
6711 * expanded name in an allocated string via 'alias' - caller must free.
6712 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006714get_name_len(
6715 char_u **arg,
6716 char_u **alias,
6717 int evaluate,
6718 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 char_u *p;
6722 char_u *expr_start;
6723 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006725 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
6727 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6728 && (*arg)[2] == (int)KE_SNR)
6729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006730 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 *arg += 3;
6732 return get_id_len(arg) + 3;
6733 }
6734 len = eval_fname_script(*arg);
6735 if (len > 0)
6736 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006737 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 *arg += len;
6739 }
6740
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006742 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006744 p = find_name_end(*arg, &expr_start, &expr_end,
6745 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 if (expr_start != NULL)
6747 {
6748 char_u *temp_string;
6749
6750 if (!evaluate)
6751 {
6752 len += (int)(p - *arg);
6753 *arg = skipwhite(p);
6754 return len;
6755 }
6756
6757 /*
6758 * Include any <SID> etc in the expanded string:
6759 * Thus the -len here.
6760 */
6761 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6762 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006763 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 *alias = temp_string;
6765 *arg = skipwhite(p);
6766 return (int)STRLEN(temp_string);
6767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768
6769 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006770 // Only give an error when there is something, otherwise it will be
6771 // reported at a higher level.
6772 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006773 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
6775 return len;
6776}
6777
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006778/*
6779 * Find the end of a variable or function name, taking care of magic braces.
6780 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6781 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006782 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006783 * Return a pointer to just after the name. Equal to "arg" if there is no
6784 * valid name.
6785 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006786 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006787find_name_end(
6788 char_u *arg,
6789 char_u **expr_start,
6790 char_u **expr_end,
6791 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006793 int mb_nest = 0;
6794 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006796 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006797 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006799 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006801 *expr_start = NULL;
6802 *expr_end = NULL;
6803 }
6804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006805 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006806 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006807 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006808 return arg;
6809
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 for (p = arg; *p != NUL
6811 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006812 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006813 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006814 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006816 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006818 if (*p == '\'')
6819 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006820 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006821 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006822 ;
6823 if (*p == NUL)
6824 break;
6825 }
6826 else if (*p == '"')
6827 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006828 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006829 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006830 if (*p == '\\' && p[1] != NUL)
6831 ++p;
6832 if (*p == NUL)
6833 break;
6834 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006835 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6836 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006837 // "s:" is start of "s:var", but "n:" is not and can be used in
6838 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006839 len = (int)(p - arg);
6840 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006841 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006842 break;
6843 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006844
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006845 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006847 if (*p == '[')
6848 ++br_nest;
6849 else if (*p == ']')
6850 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006852
zeertzjqa93d9cd2023-05-02 16:25:47 +01006853 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006855 if (*p == '{')
6856 {
6857 mb_nest++;
6858 if (expr_start != NULL && *expr_start == NULL)
6859 *expr_start = p;
6860 }
6861 else if (*p == '}')
6862 {
6863 mb_nest--;
6864 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6865 *expr_end = p;
6866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 }
6869
6870 return p;
6871}
6872
6873/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006874 * Expands out the 'magic' {}'s in a variable/function name.
6875 * Note that this can call itself recursively, to deal with
6876 * constructs like foo{bar}{baz}{bam}
6877 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6878 * "in_start" ^
6879 * "expr_start" ^
6880 * "expr_end" ^
6881 * "in_end" ^
6882 *
6883 * Returns a new allocated string, which the caller must free.
6884 * Returns NULL for failure.
6885 */
6886 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006887make_expanded_name(
6888 char_u *in_start,
6889 char_u *expr_start,
6890 char_u *expr_end,
6891 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006892{
6893 char_u c1;
6894 char_u *retval = NULL;
6895 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006896
6897 if (expr_end == NULL || in_end == NULL)
6898 return NULL;
6899 *expr_start = NUL;
6900 *expr_end = NUL;
6901 c1 = *in_end;
6902 *in_end = NUL;
6903
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006904 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006905 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006906 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006907 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6908 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006909 if (retval != NULL)
6910 {
6911 STRCPY(retval, in_start);
6912 STRCAT(retval, temp_result);
6913 STRCAT(retval, expr_end + 1);
6914 }
6915 }
6916 vim_free(temp_result);
6917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006918 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006919 *expr_start = '{';
6920 *expr_end = '}';
6921
6922 if (retval != NULL)
6923 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006924 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006925 if (expr_start != NULL)
6926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006927 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006928 temp_result = make_expanded_name(retval, expr_start,
6929 expr_end, temp_result);
6930 vim_free(retval);
6931 retval = temp_result;
6932 }
6933 }
6934
6935 return retval;
6936}
6937
6938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006940 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006942 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006943eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006945 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006946}
6947
6948/*
6949 * Return TRUE if character "c" can be used as the first character in a
6950 * variable or function name (excluding '{' and '}').
6951 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006953eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006954{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006955 return ASCII_ISALPHA(c) || c == '_';
6956}
6957
6958/*
6959 * Return TRUE if character "c" can be used as the first character of a
6960 * dictionary key.
6961 */
6962 int
6963eval_isdictc(int c)
6964{
6965 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966}
6967
6968/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006969 * Handle:
6970 * - expr[expr], expr[expr:expr] subscript
6971 * - ".name" lookup
6972 * - function call with Funcref variable: func(expr)
6973 * - method call: var->method()
6974 *
6975 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006976 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006977 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006979handle_subscript(
6980 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006981 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006982 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006983 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006984 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006985{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006986 int evaluate = evalarg != NULL
6987 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006988 int ret = OK;
6989 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006990 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006991 int getnext;
6992 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006993
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006994 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006995 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006996 // When at the end of the line and ".name" or "->{" or "->X" follows in
6997 // the next line then consume the line break.
6998 p = eval_next_non_blank(*arg, evalarg, &getnext);
6999 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007000 && ((*p == '.'
7001 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7002 || rettv->v_type == VAR_CLASS
7003 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007004 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7005 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7006 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007007 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007008 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007009 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007010 check_white = FALSE;
7011 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007012
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007013 if (rettv->v_type == VAR_ANY)
7014 {
7015 char_u *exp_name;
7016 int cc;
7017 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007018 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007019 type_T *type;
7020
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007021 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007022 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007023 if (**arg != '.')
7024 {
7025 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007026 semsg(_(e_expected_dot_after_name_str),
7027 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007028 ret = FAIL;
7029 break;
7030 }
7031 ++*arg;
7032 if (IS_WHITE_OR_NUL(**arg))
7033 {
7034 if (verbose)
7035 emsg(_(e_no_white_space_allowed_after_dot));
7036 ret = FAIL;
7037 break;
7038 }
7039
7040 // isolate the name
7041 exp_name = *arg;
7042 while (eval_isnamec(**arg))
7043 ++*arg;
7044 cc = **arg;
7045 **arg = NUL;
7046
7047 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007048 evalarg == NULL ? NULL : evalarg->eval_cctx,
7049 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007050 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007051
7052 if (idx < 0 && ufunc == NULL)
7053 {
7054 ret = FAIL;
7055 break;
7056 }
7057 if (idx >= 0)
7058 {
7059 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7060 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7061
7062 copy_tv(sv->sv_tv, rettv);
7063 }
7064 else
7065 {
7066 rettv->v_type = VAR_FUNC;
7067 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7068 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007069 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007070 }
7071
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007072 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7073 || rettv->v_type == VAR_PARTIAL))
7074 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007075 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007076 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7077 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007078
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007079 // Stop the expression evaluation when immediately aborting on
7080 // error, or when an interrupt occurred or an exception was thrown
7081 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007082 if (aborting())
7083 {
7084 if (ret == OK)
7085 clear_tv(rettv);
7086 ret = FAIL;
7087 }
7088 dict_unref(selfdict);
7089 selfdict = NULL;
7090 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007091 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007092 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007093 if (in_vim9script())
7094 *arg = skipwhite(p + 2);
7095 else
7096 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007097 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007098 {
zeertzjqc481ad32023-03-11 16:18:51 +00007099 emsg(_(e_no_white_space_allowed_before_parenthesis));
7100 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007101 }
zeertzjqc481ad32023-03-11 16:18:51 +00007102 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7103 // expr->{lambda}() or expr->(lambda)()
7104 ret = eval_lambda(arg, rettv, evalarg, verbose);
7105 else
7106 // expr->name()
7107 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007108 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007109 // "." is ".name" lookup when we found a dict or when evaluating and
7110 // scriptversion is at least 2, where string concatenation is "..".
7111 else if (**arg == '['
7112 || (**arg == '.' && (rettv->v_type == VAR_DICT
7113 || (!evaluate
7114 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007115 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007116 {
7117 dict_unref(selfdict);
7118 if (rettv->v_type == VAR_DICT)
7119 {
7120 selfdict = rettv->vval.v_dict;
7121 if (selfdict != NULL)
7122 ++selfdict->dv_refcount;
7123 }
7124 else
7125 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007126 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007127 {
7128 clear_tv(rettv);
7129 ret = FAIL;
7130 }
7131 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007132 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7133 || rettv->v_type == VAR_OBJECT))
7134 {
7135 // class member: SomeClass.varname
7136 // class method: SomeClass.SomeMethod()
7137 // class constructor: SomeClass.new()
7138 // object member: someObject.varname
7139 // object method: someObject.SomeMethod()
7140 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7141 {
7142 clear_tv(rettv);
7143 ret = FAIL;
7144 }
7145 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007146 else
7147 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007148 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007149
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007150 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7151 // Don't do this when "Func" is already a partial that was bound
7152 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007153 if (selfdict != NULL
7154 && (rettv->v_type == VAR_FUNC
7155 || (rettv->v_type == VAR_PARTIAL
7156 && (rettv->vval.v_partial->pt_auto
7157 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007158 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007159
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007160 dict_unref(selfdict);
7161 return ret;
7162}
7163
7164/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007165 * Make a copy of an item.
7166 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007167 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007168 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7169 * reference to an already copied list/dict can be used.
7170 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007172 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007173item_copy(
7174 typval_T *from,
7175 typval_T *to,
7176 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007177 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007178 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007179{
7180 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007181 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007182
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007184 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007185 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007186 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007187 }
7188 ++recurse;
7189
7190 switch (from->v_type)
7191 {
7192 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007193 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 case VAR_STRING:
7195 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007196 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007197 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007198 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007199 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007200 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007201 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007202 case VAR_CLASS:
7203 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 copy_tv(from, to);
7205 break;
7206 case VAR_LIST:
7207 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007208 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007209 if (from->vval.v_list == NULL)
7210 to->vval.v_list = NULL;
7211 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007213 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007214 to->vval.v_list = from->vval.v_list->lv_copylist;
7215 ++to->vval.v_list->lv_refcount;
7216 }
7217 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007218 to->vval.v_list = list_copy(from->vval.v_list,
7219 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 if (to->vval.v_list == NULL)
7221 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007222 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007223 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007225 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007226 case VAR_DICT:
7227 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007228 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007229 if (from->vval.v_dict == NULL)
7230 to->vval.v_dict = NULL;
7231 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7232 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007233 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007234 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7235 ++to->vval.v_dict->dv_refcount;
7236 }
7237 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007238 to->vval.v_dict = dict_copy(from->vval.v_dict,
7239 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007240 if (to->vval.v_dict == NULL)
7241 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007243 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007244 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007246 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007247 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007248 }
7249 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007250 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007251}
7252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 void
7254echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7255{
7256 char_u *tofree;
7257 char_u numbuf[NUMBUFLEN];
7258 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7259
7260 if (*atstart)
7261 {
7262 *atstart = FALSE;
7263 // Call msg_start() after eval1(), evaluating the expression
7264 // may cause a message to appear.
7265 if (with_space)
7266 {
7267 // Mark the saved text as finishing the line, so that what
7268 // follows is displayed on a new line when scrolling back
7269 // at the more prompt.
7270 msg_sb_eol();
7271 msg_start();
7272 }
7273 }
7274 else if (with_space)
7275 msg_puts_attr(" ", echo_attr);
7276
7277 if (p != NULL)
7278 for ( ; *p != NUL && !got_int; ++p)
7279 {
7280 if (*p == '\n' || *p == '\r' || *p == TAB)
7281 {
7282 if (*p != TAB && *needclr)
7283 {
7284 // remove any text still there from the command
7285 msg_clr_eos();
7286 *needclr = FALSE;
7287 }
7288 msg_putchar_attr(*p, echo_attr);
7289 }
7290 else
7291 {
7292 if (has_mbyte)
7293 {
7294 int i = (*mb_ptr2len)(p);
7295
7296 (void)msg_outtrans_len_attr(p, i, echo_attr);
7297 p += i - 1;
7298 }
7299 else
7300 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7301 }
7302 }
7303 vim_free(tofree);
7304}
7305
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 * ":echo expr1 ..." print each argument separated with a space, add a
7308 * newline at the end.
7309 * ":echon expr1 ..." print each argument plain.
7310 */
7311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313{
7314 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007316 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 int needclr = TRUE;
7318 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007319 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007320 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007321 evalarg_T evalarg;
7322
Bram Moolenaare707c882020-07-01 13:07:37 +02007323 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
7325 if (eap->skip)
7326 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007327 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007329 // If eval1() causes an error message the text from the command may
7330 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007331 need_clr_eos = needclr;
7332
Bram Moolenaar68db9962021-05-09 23:19:22 +02007333 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007334 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 /*
7337 * Report the invalid expression unless the expression evaluation
7338 * has been cancelled due to an aborting error, an interrupt, or an
7339 * exception.
7340 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007341 if (!aborting() && did_emsg == did_emsg_before
7342 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007343 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007344 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 break;
7346 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007347 need_clr_eos = FALSE;
7348
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007350 {
7351 if (rettv.v_type == VAR_VOID)
7352 {
7353 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7354 break;
7355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007357 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007359 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 arg = skipwhite(arg);
7361 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007362 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007363 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
7365 if (eap->skip)
7366 --emsg_skip;
7367 else
7368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007369 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 if (needclr)
7371 msg_clr_eos();
7372 if (eap->cmdidx == CMD_echo)
7373 msg_end();
7374 }
7375}
7376
7377/*
7378 * ":echohl {name}".
7379 */
7380 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007381ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007383 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384}
7385
7386/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007387 * Returns the :echo attribute
7388 */
7389 int
7390get_echo_attr(void)
7391{
7392 return echo_attr;
7393}
7394
7395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 * ":execute expr1 ..." execute the result of an expression.
7397 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007398 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007400 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 * Each gets spaces around each argument and a newline at the end for
7402 * echo commands
7403 */
7404 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007405ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406{
7407 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 int ret = OK;
7410 char_u *p;
7411 garray_T ga;
7412 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007413 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
7415 ga_init2(&ga, 1, 80);
7416
7417 if (eap->skip)
7418 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007419 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007421 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007422 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
7425 if (!eap->skip)
7426 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007427 char_u buf[NUMBUFLEN];
7428
7429 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007430 {
7431 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7432 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007433 semsg(_(e_using_invalid_value_as_string_str),
7434 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007435 p = NULL;
7436 }
7437 else
7438 p = tv_get_string_buf(&rettv, buf);
7439 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007440 else
7441 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007442 if (p == NULL)
7443 {
7444 clear_tv(&rettv);
7445 ret = FAIL;
7446 break;
7447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 len = (int)STRLEN(p);
7449 if (ga_grow(&ga, len + 2) == FAIL)
7450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007451 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 ret = FAIL;
7453 break;
7454 }
7455 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 ga.ga_len += len;
7459 }
7460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007461 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 arg = skipwhite(arg);
7463 }
7464
7465 if (ret != FAIL && ga.ga_data != NULL)
7466 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007467 // use the first line of continuation lines for messages
7468 SOURCING_LNUM = start_lnum;
7469
Bram Moolenaar37fef162022-08-29 18:16:32 +01007470 if (eap->cmdidx == CMD_echomsg
7471 || eap->cmdidx == CMD_echowindow
7472 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007473 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007474 // Mark the already saved text as finishing the line, so that what
7475 // follows is displayed on a new line when scrolling back at the
7476 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007477 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007478 }
7479
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007480 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007481 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007482 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007483 out_flush();
7484 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007485 else if (eap->cmdidx == CMD_echowindow)
7486 {
7487#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007488 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007489#endif
7490 msg_attr(ga.ga_data, echo_attr);
7491#ifdef HAS_MESSAGE_WINDOW
7492 end_echowindow();
7493#endif
7494 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007495 else if (eap->cmdidx == CMD_echoconsole)
7496 {
7497 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7498 ui_write((char_u *)"\r\n", 2, TRUE);
7499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 else if (eap->cmdidx == CMD_echoerr)
7501 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007502 int save_did_emsg = did_emsg;
7503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007504 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007505 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 if (!force_abort)
7507 did_emsg = save_did_emsg;
7508 }
7509 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007510 {
7511 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7512
7513 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7514 sticky_cmdmod_flags = cmdmod.cmod_flags
7515 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 do_cmdline((char_u *)ga.ga_data,
7517 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007518 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 }
7521
7522 ga_clear(&ga);
7523
7524 if (eap->skip)
7525 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007526 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527}
7528
7529/*
7530 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7531 * "arg" points to the "&" or '+' when called, to "option" when returning.
7532 * Returns NULL when no option name found. Otherwise pointer to the char
7533 * after the option name.
7534 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007535 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007536find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 char_u *p = *arg;
7539
7540 ++p;
7541 if (*p == 'g' && p[1] == ':')
7542 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007543 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 p += 2;
7545 }
7546 else if (*p == 'l' && p[1] == ':')
7547 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007548 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 p += 2;
7550 }
7551 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007552 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554 if (!ASCII_ISALPHA(*p))
7555 return NULL;
7556 *arg = p;
7557
7558 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007559 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 else
7561 while (ASCII_ISALPHA(*p))
7562 ++p;
7563 return p;
7564}
7565
7566/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007567 * Display script name where an item was last set.
7568 * Should only be invoked when 'verbose' is non-zero.
7569 */
7570 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007571last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007572{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007573 char_u *p;
7574
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007575 if (script_ctx.sc_sid == 0)
7576 return;
7577
7578 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7579 if (p == NULL)
7580 return;
7581
7582 verbose_enter();
7583 msg_puts(_("\n\tLast set from "));
7584 msg_puts((char *)p);
7585 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007586 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007587 msg_puts(_(line_msg));
7588 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007589 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007590 verbose_leave();
7591 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007592}
7593
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007594#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595
7596/*
7597 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007598 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 * "flags" can be "g" to do a global substitute.
7600 * Returns an allocated string, NULL for error.
7601 */
7602 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007603do_string_sub(
7604 char_u *str,
7605 char_u *pat,
7606 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007607 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007608 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 int sublen;
7611 regmatch_T regmatch;
7612 int i;
7613 int do_all;
7614 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007615 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 garray_T ga;
7617 char_u *ret;
7618 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007619 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007621 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007623 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
7625 ga_init2(&ga, 1, 200);
7626
7627 do_all = (flags[0] == 'g');
7628
7629 regmatch.rm_ic = p_ic;
7630 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7631 if (regmatch.regprog != NULL)
7632 {
7633 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007634 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7636 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007637 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007638 if (regmatch.startp[0] == regmatch.endp[0])
7639 {
7640 if (zero_width == regmatch.startp[0])
7641 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007642 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007643 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007644 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7645 (size_t)i);
7646 ga.ga_len += i;
7647 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007648 continue;
7649 }
7650 zero_width = regmatch.startp[0];
7651 }
7652
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 /*
7654 * Get some space for a temporary buffer to do the substitution
7655 * into. It will contain:
7656 * - The text up to where the match is.
7657 * - The substituted text.
7658 * - The text after the match.
7659 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007660 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007661 if (sublen <= 0)
7662 {
7663 ga_clear(&ga);
7664 break;
7665 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007666 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7668 {
7669 ga_clear(&ga);
7670 break;
7671 }
7672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007673 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 i = (int)(regmatch.startp[0] - tail);
7675 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007676 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007677 (void)vim_regsub(&regmatch, sub, expr,
7678 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7679 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007681 tail = regmatch.endp[0];
7682 if (*tail == NUL)
7683 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 if (!do_all)
7685 break;
7686 }
7687
7688 if (ga.ga_data != NULL)
7689 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7690
Bram Moolenaar473de612013-06-08 18:19:48 +02007691 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693
7694 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7695 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007696 if (p_cpo == empty_option)
7697 p_cpo = save_cpo;
7698 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007699 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007700 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007701 // If it's still empty it was changed and restored, need to restore in
7702 // the complicated way.
7703 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007704 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007705 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707
7708 return ret;
7709}