blob: 80ff5a3cbaf7100d7872ea104b2b0f3df1513739 [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 Rael4c8da022023-10-11 21:35:11 +02001190 int log_sync_root_key = FALSE;
Ernie Raelee865f32023-09-29 19:53:55 +02001191#endif
1192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001193 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001194 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001196 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001198 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001200 lp->ll_name_end = find_name_end(name, NULL, NULL,
1201 FNE_INCL_BR | fne_flags);
1202 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001203 }
1204
Bram Moolenaara749a422022-02-12 19:52:25 +00001205 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001206 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001207 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1208 {
1209 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1210 return NULL;
1211 }
1212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001213 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001214 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 if (expr_start != NULL)
1217 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001218 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001219 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 && *p != '[' && *p != '.')
1221 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001222 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001223 return NULL;
1224 }
1225
1226 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1227 if (lp->ll_exp_name == NULL)
1228 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001229 // Report an invalid expression in braces, unless the
1230 // expression evaluation has been cancelled due to an
1231 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 if (!aborting() && !quiet)
1233 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001234 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001235 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 return NULL;
1237 }
1238 }
1239 lp->ll_name = lp->ll_exp_name;
1240 }
1241 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 lp->ll_name = name;
1244
Bram Moolenaar4525a572022-02-13 11:57:33 +00001245 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001247 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001248 // However, "g:[key]" is indexing a dictionary.
1249 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001250 {
1251 --p;
1252 lp->ll_name_end = p;
1253 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001254 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001255 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001256 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001257
Bram Moolenaar9510d222022-09-11 15:14:05 +01001258 if (is_scoped_variable(name))
1259 {
1260 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1261 return NULL;
1262 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001263 if (VIM_ISWHITE(*p))
1264 {
1265 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1266 return NULL;
1267 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001268 if (tp == p + 1 && !quiet)
1269 {
1270 semsg(_(e_white_space_required_after_str_str), ":", p);
1271 return NULL;
1272 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001273 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001274 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001275 semsg(_(e_using_type_not_in_script_context_str), p);
1276 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001277 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001278 if (vim9script && (flags & GLV_NO_DECL) &&
1279 !(flags & GLV_FOR_LOOP))
1280 {
1281 // Using a type and not in a "var" declaration.
1282 semsg(_(e_trailing_characters_str), p);
1283 return NULL;
1284 }
1285
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001286
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001287 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001288 lp->ll_type = parse_type(&tp,
1289 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1290 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001291 if (lp->ll_type == NULL && !quiet)
1292 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001293 lp->ll_name_end = tp;
1294 }
Ernie Raelee865f32023-09-29 19:53:55 +02001295 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 }
1297 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001298 if (lp->ll_name == NULL)
1299 return p;
1300
Bram Moolenaar62aec932022-01-29 21:45:34 +00001301 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001302 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001303 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001304
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001305 if (import != NULL)
1306 {
1307 ufunc_T *ufunc;
1308 type_T *type;
1309
Bram Moolenaar753885b2022-08-24 16:30:36 +01001310 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001311 lp->ll_sid = import->imp_sid;
1312 lp->ll_name = skipwhite(p + 1);
1313 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1314 lp->ll_name_end = p;
1315
1316 // check the item is exported
1317 cc = *p;
1318 *p = NUL;
1319 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001320 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001321 {
1322 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001323 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001324 }
1325 *p = cc;
1326 }
1327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328
Ernie Rael4c8da022023-10-11 21:35:11 +02001329 int sync_root = FALSE;
1330 if (vim9script && lval_root != NULL)
1331 {
1332 cl_exec = lval_root->lr_cl_exec;
1333 sync_root = lval_root->lr_sync_root;
1334 }
1335
1336 // Without [idx] or .key we are done, unless doing sync_root.
1337 if (*p != '[' && *p != '.' && (*name == NUL || !sync_root))
Ernie Raelee865f32023-09-29 19:53:55 +02001338 {
1339 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001340 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001342 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343
Ernie Rael4c8da022023-10-11 21:35:11 +02001344 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001345 {
1346 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001347 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001348 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001349 }
1350 else
1351 {
1352 cc = *p;
1353 *p = NUL;
1354 // When we would write to the variable pass &ht and prevent autoload.
1355 writing = !(flags & GLV_READ_ONLY);
1356 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001357 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001358 if (v == NULL && !quiet)
1359 semsg(_(e_undefined_variable_str), lp->ll_name);
1360 *p = cc;
1361 if (v == NULL)
1362 return NULL;
1363 lp->ll_tv = &v->di_tv;
1364 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001365
Bram Moolenaar4525a572022-02-13 11:57:33 +00001366 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001367 {
1368 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001369 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001370 return NULL;
1371 }
1372
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 /*
1374 * Loop until no more [idx] or .key is following.
1375 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001376 var1.v_type = VAR_UNKNOWN;
1377 var2.v_type = VAR_UNKNOWN;
Ernie Rael4c8da022023-10-11 21:35:11 +02001378 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.') || sync_root)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001380 vartype_T v_type = lp->ll_tv->v_type;
1381
1382 if (*p == '.' && v_type != VAR_DICT
1383 && v_type != VAR_OBJECT
1384 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001385 {
1386 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001387 semsg(_(e_dot_not_allowed_after_str_str),
1388 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001389 return NULL;
1390 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001391 if (v_type != VAR_LIST
1392 && v_type != VAR_DICT
1393 && v_type != VAR_BLOB
1394 && v_type != VAR_OBJECT
1395 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001397 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001398 semsg(_(e_index_not_allowed_after_str_str),
1399 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001402
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001403 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001404 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001405 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001406 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001407 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001408 r = rettv_blob_alloc(lp->ll_tv);
1409 if (r == FAIL)
1410 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001411
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001415 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001418#ifdef LOG_LOCKVAR
1419 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1420 vartype_name(v_type));
1421#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001422
Bram Moolenaar4525a572022-02-13 11:57:33 +00001423 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001424 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001425 && lp->ll_tv == &v->di_tv
1426 && ht != NULL && ht == get_script_local_ht())
1427 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001428 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001429
1430 // Vim9 script local variable: get the type
1431 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001432 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001433 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001434#ifdef LOG_LOCKVAR
1435 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1436 vartype_name(lp->ll_valtype->tt_type));
1437#endif
1438 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001439 }
1440
Bram Moolenaar8c711452005-01-14 21:53:12 +00001441 len = -1;
Ernie Rael4c8da022023-10-11 21:35:11 +02001442 if (sync_root)
1443 {
1444 // For example, the first token is a member variable name and
1445 // lp->ll_tv is a class/object.
1446 // Process it directly without looking for "[idx]" or ".name".
1447 key = name;
1448 sync_root = FALSE; // only first time through
1449#ifdef LOG_LOCKVAR
1450 log_sync_root_key = TRUE;
1451 ch_log(NULL, "LKVAR: ... loop: name: %s, sync_root", name);
1452#endif
1453 }
1454 else if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001455 {
1456 key = p + 1;
1457 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1458 ;
1459 if (len == 0)
1460 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001462 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001464 }
1465 p = key + len;
1466 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001467 else
1468 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001469 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001470 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001471 if (*p == ':')
1472 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001473 else
1474 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001475 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001476 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001478 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001479 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001480 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001481 clear_tv(&var1);
1482 return NULL;
1483 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001484 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001485 }
1486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001487 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001488 if (*p == ':')
1489 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001490 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001491 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001493 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001494 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001495 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001496 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001497 if (rettv != NULL
1498 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001499 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001500 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001501 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001502 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001503 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001504 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001505 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001507 }
1508 p = skipwhite(p + 1);
1509 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001510 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001511 else
1512 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001514 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001515 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001516 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001517 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001518 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001519 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001520 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001521 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001522 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001523 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001524 clear_tv(&var2);
1525 return NULL;
1526 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001527 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001528 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001529 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001530 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001531 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001532
Bram Moolenaar8c711452005-01-14 21:53:12 +00001533 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001534 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001535 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001536 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001537 clear_tv(&var1);
1538 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001539 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001540 }
1541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001542 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001543 ++p;
1544 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001545#ifdef LOG_LOCKVAR
1546 if (log_sync_root_key)
1547 ch_log(NULL, "LKVAR: ... loop: p: %s, sync_root key: %s", p,
1548 key);
1549 else if (len == -1)
1550 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1551 empty1 ? ":" : (char*)tv_get_string(&var1));
1552 else
1553 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
1554 log_sync_root_key = FALSE;
1555#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001556
Bram Moolenaard505d172022-12-18 21:42:55 +00001557 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001558 {
1559 if (len == -1)
1560 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001561 // "[key]": get key from "var1"
1562 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001563 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001564 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001565 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001566 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001567 }
1568 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001570 lp->ll_object = NULL;
1571 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001572
1573 // a NULL dict is equivalent with an empty dict
1574 if (lp->ll_tv->vval.v_dict == NULL)
1575 {
1576 lp->ll_tv->vval.v_dict = dict_alloc();
1577 if (lp->ll_tv->vval.v_dict == NULL)
1578 {
1579 clear_tv(&var1);
1580 return NULL;
1581 }
1582 ++lp->ll_tv->vval.v_dict->dv_refcount;
1583 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001585
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001586 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001588 // When assigning to a scope dictionary check that a function and
1589 // variable name is valid (only variable name unless it is l: or
1590 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001591 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001592 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001593 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001594
1595 if (len != -1)
1596 {
1597 prevval = key[len];
1598 key[len] = NUL;
1599 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001600 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001601 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001602 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001603 && (rettv->v_type == VAR_FUNC
1604 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001605 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001606 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001607 if (len != -1)
1608 key[len] = prevval;
1609 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001610 {
1611 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001612 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001613 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001614 }
1615
Bram Moolenaar10c65862020-10-08 21:16:42 +02001616 if (lp->ll_valtype != NULL)
1617 // use the type of the member
1618 lp->ll_valtype = lp->ll_valtype->tt_member;
1619
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001620 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001621 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001622 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001623 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001624 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001625 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001626 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001627 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001628 return NULL;
1629 }
1630
Bram Moolenaar31b81602019-02-10 22:14:27 +01001631 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001632 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001634 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001635 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001636 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001637 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001638 }
1639 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001640 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001641 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001642 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001643 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001644 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001645 p = NULL;
1646 break;
1647 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001648 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001649 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001650 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1651 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001652 {
1653 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001654 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001655 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001656
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001657 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001658 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001659 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001660 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001661 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001662 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1663
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 /*
1665 * Get the number and item for the only or first index of the List.
1666 */
1667 if (empty1)
1668 lp->ll_n1 = 0;
1669 else
1670 // is number or string
1671 lp->ll_n1 = (long)tv_get_number(&var1);
1672 clear_tv(&var1);
1673
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001674 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001675 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001676 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001677 return NULL;
1678 }
1679 if (lp->ll_range && !lp->ll_empty2)
1680 {
1681 lp->ll_n2 = (long)tv_get_number(&var2);
1682 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001683 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1684 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001685 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001686 }
1687 lp->ll_blob = lp->ll_tv->vval.v_blob;
1688 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001689 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001690 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001691 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001692 {
1693 /*
1694 * Get the number and item for the only or first index of the List.
1695 */
1696 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001697 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001698 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001699 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001700 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001701 clear_tv(&var1);
1702
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001703 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001704 lp->ll_object = NULL;
1705 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001706 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001707 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1708 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001709 if (lp->ll_li == NULL)
1710 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001711 clear_tv(&var2);
1712 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001713 }
1714
Bram Moolenaar10c65862020-10-08 21:16:42 +02001715 if (lp->ll_valtype != NULL)
1716 // use the type of the member
1717 lp->ll_valtype = lp->ll_valtype->tt_member;
1718
Bram Moolenaar8c711452005-01-14 21:53:12 +00001719 /*
1720 * May need to find the item or absolute index for the second
1721 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001722 * When no index given: "lp->ll_empty2" is TRUE.
1723 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001724 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001725 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001727 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001728 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001729 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001730 if (check_range_index_two(lp->ll_list,
1731 &lp->ll_n1, lp->ll_li,
1732 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001733 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001734 }
1735
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001736 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001737 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001738 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001739 {
Ernie Raelee865f32023-09-29 19:53:55 +02001740 lp->ll_dict = NULL;
1741 lp->ll_list = NULL;
1742
1743 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001744 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001745 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001746 if (lp->ll_tv->vval.v_object == NULL)
1747 {
1748 if (!quiet)
1749 emsg(_(e_using_null_object));
1750 return NULL;
1751 }
Ernie Raelee865f32023-09-29 19:53:55 +02001752 cl = lp->ll_tv->vval.v_object->obj_class;
1753 lp->ll_object = lp->ll_tv->vval.v_object;
1754 }
1755 else
1756 {
1757 cl = lp->ll_tv->vval.v_class;
1758 lp->ll_object = NULL;
1759 }
1760 lp->ll_class = cl;
1761
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001762 // TODO: what if class is NULL?
1763 if (cl != NULL)
1764 {
1765 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001766
1767 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001768 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001769 // First look for a function with this name.
1770 // round 1: class functions (skipped for an object)
1771 // round 2: object methods
1772 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001773 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001774 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001775 int m_idx;
1776 ufunc_T *fp;
1777
1778 fp = method_lookup(cl,
1779 round == 1 ? VAR_CLASS : VAR_OBJECT,
1780 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001781 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001782 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001783 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001784 lp->ll_ufunc = fp;
1785 lp->ll_valtype = fp->uf_func_type;
1786 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001787 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001788 }
1789 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001790
1791 if (lp->ll_valtype == NULL)
1792 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001793 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001794 ocmember_T *om
1795 = member_lookup(cl, v_type, key, p - key, &m_idx);
1796 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001797 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001798 {
Ernie Rael64885642023-10-04 20:16:22 +02001799 if (get_lval_check_access(cl_exec, cl, om,
1800 p, flags) == FAIL)
1801 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001802
1803 lp->ll_valtype = om->ocm_type;
1804
1805 if (v_type == VAR_OBJECT)
1806 lp->ll_tv = ((typval_T *)(
1807 lp->ll_tv->vval.v_object + 1)) + m_idx;
1808 else
1809 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001810 }
1811 }
1812
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001813 if (lp->ll_valtype == NULL)
1814 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001815 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001816 return NULL;
1817 }
1818 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 }
1821
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001822 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001824 return p;
1825}
1826
1827/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001828 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001829 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001831clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001832{
1833 vim_free(lp->ll_exp_name);
1834 vim_free(lp->ll_newkey);
1835}
1836
1837/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001838 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001839 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001840 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1841 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001842 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001843 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001844set_var_lval(
1845 lval_T *lp,
1846 char_u *endp,
1847 typval_T *rettv,
1848 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001849 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1850 char_u *op,
1851 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852{
1853 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001854 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001855
1856 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001858 cc = *endp;
1859 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001860 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001861 return;
1862
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001863 if (lp->ll_blob != NULL)
1864 {
1865 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001866
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001867 if (op != NULL && *op != '=')
1868 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001869 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001870 return;
1871 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001872 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001873 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001874
1875 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1876 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001877 if (lp->ll_empty2)
1878 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1879
Bram Moolenaar68452172021-04-12 21:21:02 +02001880 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1881 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001882 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001883 }
1884 else
1885 {
1886 val = (int)tv_get_number_chk(rettv, &error);
1887 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001888 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001889 }
1890 }
1891 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001893 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001895 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1896 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001897 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001898 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001899 *endp = cc;
1900 return;
1901 }
1902
Bram Moolenaarff697e62019-02-12 22:28:33 +01001903 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001904 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001905 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001906 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001907 {
1908 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001909 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1910 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001911 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001912 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001913 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001914 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001917 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001918 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001919 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1920 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001921 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001922 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001923 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001924 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001925 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001926 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001927 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001928 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001929 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001930 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001931 else if (lp->ll_range)
1932 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001933 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1934 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001935 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001936 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001937 return;
1938 }
1939
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001940 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1941 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001942 }
1943 else
1944 {
1945 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001946 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001947 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001948 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1949 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001950 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001951 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001952 return;
1953 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001954
1955 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001956 && check_typval_arg_type(lp->ll_valtype, rettv,
1957 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001958 return;
1959
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 if (lp->ll_newkey != NULL)
1961 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 if (op != NULL && *op != '=')
1963 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001964 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 return;
1966 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001967 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1968 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001969 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001971 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001972 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 if (di == NULL)
1974 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001975 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1976 {
1977 vim_free(di);
1978 return;
1979 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001981 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 else if (op != NULL && *op != '=')
1983 {
1984 tv_op(lp->ll_tv, rettv, op);
1985 return;
1986 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001987 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001988 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 /*
1991 * Assign the value to the variable or list item.
1992 */
1993 if (copy)
1994 copy_tv(rettv, lp->ll_tv);
1995 else
1996 {
1997 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001998 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000 }
2001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002}
2003
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002005 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2006 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002007 * Returns OK or FAIL.
2008 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002009 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002010tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002012 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002013 char_u numbuf[NUMBUFLEN];
2014 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002015 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002016
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002017 // Can't do anything with a Funcref or Dict on the right.
2018 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002019 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002020 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2021 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 {
2023 switch (tv1->v_type)
2024 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002025 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002026 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 case VAR_DICT:
2029 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002030 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002031 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002032 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002033 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002034 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002035 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002036 case VAR_CLASS:
2037 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 break;
2039
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002040 case VAR_BLOB:
2041 if (*op != '+' || tv2->v_type != VAR_BLOB)
2042 break;
2043 // BLOB += BLOB
2044 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2045 {
2046 blob_T *b1 = tv1->vval.v_blob;
2047 blob_T *b2 = tv2->vval.v_blob;
2048 int i, len = blob_len(b2);
2049 for (i = 0; i < len; i++)
2050 ga_append(&b1->bv_ga, blob_get(b2, i));
2051 }
2052 return OK;
2053
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002054 case VAR_LIST:
2055 if (*op != '+' || tv2->v_type != VAR_LIST)
2056 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002057 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002058 if (tv2->vval.v_list != NULL)
2059 {
2060 if (tv1->vval.v_list == NULL)
2061 {
2062 tv1->vval.v_list = tv2->vval.v_list;
2063 ++tv1->vval.v_list->lv_refcount;
2064 }
2065 else
2066 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002068 return OK;
2069
2070 case VAR_NUMBER:
2071 case VAR_STRING:
2072 if (tv2->v_type == VAR_LIST)
2073 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002074 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002075 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002076 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002077 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002078 if (tv2->v_type == VAR_FLOAT)
2079 {
2080 float_T f = n;
2081
Bram Moolenaarff697e62019-02-12 22:28:33 +01002082 if (*op == '%')
2083 break;
2084 switch (*op)
2085 {
2086 case '+': f += tv2->vval.v_float; break;
2087 case '-': f -= tv2->vval.v_float; break;
2088 case '*': f *= tv2->vval.v_float; break;
2089 case '/': f /= tv2->vval.v_float; break;
2090 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002091 clear_tv(tv1);
2092 tv1->v_type = VAR_FLOAT;
2093 tv1->vval.v_float = f;
2094 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002095 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002096 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002097 switch (*op)
2098 {
2099 case '+': n += tv_get_number(tv2); break;
2100 case '-': n -= tv_get_number(tv2); break;
2101 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002102 case '/': n = num_divide(n, tv_get_number(tv2),
2103 &failed); break;
2104 case '%': n = num_modulus(n, tv_get_number(tv2),
2105 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002106 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002107 clear_tv(tv1);
2108 tv1->v_type = VAR_NUMBER;
2109 tv1->vval.v_number = n;
2110 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002111 }
2112 else
2113 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002114 if (tv2->v_type == VAR_FLOAT)
2115 break;
2116
Bram Moolenaarff697e62019-02-12 22:28:33 +01002117 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002118 s = tv_get_string(tv1);
2119 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002120 clear_tv(tv1);
2121 tv1->v_type = VAR_STRING;
2122 tv1->vval.v_string = s;
2123 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002124 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002125
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002126 case VAR_FLOAT:
2127 {
2128 float_T f;
2129
Bram Moolenaarff697e62019-02-12 22:28:33 +01002130 if (*op == '%' || *op == '.'
2131 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002132 && tv2->v_type != VAR_NUMBER
2133 && tv2->v_type != VAR_STRING))
2134 break;
2135 if (tv2->v_type == VAR_FLOAT)
2136 f = tv2->vval.v_float;
2137 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002138 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002139 switch (*op)
2140 {
2141 case '+': tv1->vval.v_float += f; break;
2142 case '-': tv1->vval.v_float -= f; break;
2143 case '*': tv1->vval.v_float *= f; break;
2144 case '/': tv1->vval.v_float /= f; break;
2145 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002146 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002147 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002148 }
2149 }
2150
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002151 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 return FAIL;
2153}
2154
2155/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002156 * Evaluate the expression used in a ":for var in expr" command.
2157 * "arg" points to "var".
2158 * Set "*errp" to TRUE for an error, FALSE otherwise;
2159 * Return a pointer that holds the info. Null when there is an error.
2160 */
2161 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002162eval_for_line(
2163 char_u *arg,
2164 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002165 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002166 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002167{
Bram Moolenaar33570922005-01-25 22:26:29 +00002168 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002169 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002170 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002171 typval_T tv;
2172 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002173 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002175 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002176
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002177 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002178 if (fi == NULL)
2179 return NULL;
2180
Bram Moolenaar404557e2021-07-05 21:41:48 +02002181 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2182 &fi->fi_semicolon, FALSE);
2183 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002184 return fi;
2185
Bram Moolenaar404557e2021-07-05 21:41:48 +02002186 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002187 if (expr[0] != 'i' || expr[1] != 'n'
2188 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002189 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002190 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2191 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2192 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002193 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002194 return fi;
2195 }
2196
2197 if (skip)
2198 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002199 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2200 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002201 {
2202 *errp = FALSE;
2203 if (!skip)
2204 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002205 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002206 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002207 l = tv.vval.v_list;
2208 if (l == NULL)
2209 {
2210 // a null list is like an empty list: do nothing
2211 clear_tv(&tv);
2212 }
2213 else
2214 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002216 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002218 // No need to increment the refcount, it's already set for
2219 // the list being used in "tv".
2220 fi->fi_list = l;
2221 list_add_watch(l, &fi->fi_lw);
2222 fi->fi_lw.lw_item = l->lv_first;
2223 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002224 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002225 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002226 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002227 fi->fi_bi = 0;
2228 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002229 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002230 typval_T btv;
2231
2232 // Make a copy, so that the iteration still works when the
2233 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002235 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002236 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002237 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002238 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002239 else if (tv.v_type == VAR_STRING)
2240 {
2241 fi->fi_byte_idx = 0;
2242 fi->fi_string = tv.vval.v_string;
2243 tv.vval.v_string = NULL;
2244 if (fi->fi_string == NULL)
2245 fi->fi_string = vim_strsave((char_u *)"");
2246 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002247 else
2248 {
Sean Dewar80d73952021-08-04 19:25:54 +02002249 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002250 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002251 }
2252 }
2253 }
2254 if (skip)
2255 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002256 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002257
2258 return fi;
2259}
2260
2261/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002262 * Used when looping over a :for line, skip the "in expr" part.
2263 */
2264 void
2265skip_for_lines(void *fi_void, evalarg_T *evalarg)
2266{
2267 forinfo_T *fi = (forinfo_T *)fi_void;
2268 int i;
2269
2270 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002271 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002272}
2273
2274/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002275 * Use the first item in a ":for" list. Advance to the next.
2276 * Assign the values to the variable (list). "arg" points to the first one.
2277 * Return TRUE when a valid item was found, FALSE when at end of list or
2278 * something wrong.
2279 */
2280 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002281next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002282{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002283 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002284 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002285 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002286 ? (ASSIGN_FINAL
2287 // first round: error if variable exists
2288 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002289 | ASSIGN_NO_MEMBER_TYPE
2290 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002291 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002292 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002293 int skip_assign = in_vim9script() && arg[0] == '_'
2294 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002295
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002296 if (fi->fi_blob != NULL)
2297 {
2298 typval_T tv;
2299
2300 if (fi->fi_bi >= blob_len(fi->fi_blob))
2301 return FALSE;
2302 tv.v_type = VAR_NUMBER;
2303 tv.v_lock = VAR_FIXED;
2304 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2305 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002306 if (skip_assign)
2307 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002308 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002309 fi->fi_varcount, flag, NULL) == OK;
2310 }
2311
2312 if (fi->fi_string != NULL)
2313 {
2314 typval_T tv;
2315 int len;
2316
2317 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2318 if (len == 0)
2319 return FALSE;
2320 tv.v_type = VAR_STRING;
2321 tv.v_lock = VAR_FIXED;
2322 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2323 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002324 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002325 if (skip_assign)
2326 result = TRUE;
2327 else
2328 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002329 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002330 vim_free(tv.vval.v_string);
2331 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002332 }
2333
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002334 item = fi->fi_lw.lw_item;
2335 if (item == NULL)
2336 result = FALSE;
2337 else
2338 {
2339 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002340 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002341 if (skip_assign)
2342 result = TRUE;
2343 else
2344 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002345 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346 }
2347 return result;
2348}
2349
2350/*
2351 * Free the structure used to store info used by ":for".
2352 */
2353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002354free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002355{
Bram Moolenaar33570922005-01-25 22:26:29 +00002356 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002357
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002358 if (fi == NULL)
2359 return;
2360 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002361 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002362 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002363 list_unref(fi->fi_list);
2364 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002365 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002366 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002367 else
2368 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002369 vim_free(fi);
2370}
2371
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002373set_context_for_expression(
2374 expand_T *xp,
2375 char_u *arg,
2376 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002378 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002382 if (cmdidx == CMD_let || cmdidx == CMD_var
2383 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002384 {
2385 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002386 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002387 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002388 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002389 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002390 {
2391 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002392 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002393 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002394 break;
2395 }
2396 return;
2397 }
2398 }
2399 else
2400 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2401 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 while ((xp->xp_pattern = vim_strpbrk(arg,
2403 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2404 {
2405 c = *xp->xp_pattern;
2406 if (c == '&')
2407 {
2408 c = xp->xp_pattern[1];
2409 if (c == '&')
2410 {
2411 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002412 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
2414 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002417 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2418 xp->xp_pattern += 2;
2419
2420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422 else if (c == '$')
2423 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002424 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 xp->xp_context = EXPAND_ENV_VARS;
2426 }
2427 else if (c == '=')
2428 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002429 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 xp->xp_context = EXPAND_EXPRESSION;
2431 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002432 else if (c == '#'
2433 && xp->xp_context == EXPAND_EXPRESSION)
2434 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002435 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002436 break;
2437 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002438 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 && xp->xp_context == EXPAND_FUNCTIONS
2440 && vim_strchr(xp->xp_pattern, '(') == NULL)
2441 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002442 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 break;
2444 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002445 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002447 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
2449 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2450 if (c == '\\' && xp->xp_pattern[1] != NUL)
2451 ++xp->xp_pattern;
2452 xp->xp_context = EXPAND_NOTHING;
2453 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002454 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002456 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2458 /* skip */ ;
2459 xp->xp_context = EXPAND_NOTHING;
2460 }
2461 else if (c == '|')
2462 {
2463 if (xp->xp_pattern[1] == '|')
2464 {
2465 ++xp->xp_pattern;
2466 xp->xp_context = EXPAND_EXPRESSION;
2467 }
2468 else
2469 xp->xp_context = EXPAND_COMMANDS;
2470 }
2471 else
2472 xp->xp_context = EXPAND_EXPRESSION;
2473 }
2474 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002475 // Doesn't look like something valid, expand as an expression
2476 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 arg = xp->xp_pattern;
2479 if (*arg != NUL)
2480 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2481 /* skip */ ;
2482 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002483
2484 // ":exe one two" completes "two"
2485 if ((cmdidx == CMD_execute
2486 || cmdidx == CMD_echo
2487 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002488 || cmdidx == CMD_echomsg
2489 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002490 && xp->xp_context == EXPAND_EXPRESSION)
2491 {
2492 for (;;)
2493 {
2494 char_u *n = skiptowhite(arg);
2495
2496 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2497 break;
2498 arg = skipwhite(n);
2499 }
2500 }
2501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 xp->xp_pattern = arg;
2503}
2504
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002506 * Return TRUE if "pat" matches "text".
2507 * Does not use 'cpo' and always uses 'magic'.
2508 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002509 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002510pattern_match(char_u *pat, char_u *text, int ic)
2511{
2512 int matches = FALSE;
2513 char_u *save_cpo;
2514 regmatch_T regmatch;
2515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002516 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002517 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002518 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002519 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2520 if (regmatch.regprog != NULL)
2521 {
2522 regmatch.rm_ic = ic;
2523 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2524 vim_regfree(regmatch.regprog);
2525 }
2526 p_cpo = save_cpo;
2527 return matches;
2528}
2529
2530/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002531 * Handle a name followed by "(". Both for just "name(arg)" and for
2532 * "expr->name(arg)".
2533 * Returns OK or FAIL.
2534 */
2535 static int
2536eval_func(
2537 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002538 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002539 char_u *name,
2540 int name_len,
2541 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002542 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002543 typval_T *basetv) // "expr" for "expr->name(arg)"
2544{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002545 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002546 char_u *s = name;
2547 int len = name_len;
2548 partial_T *partial;
2549 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002550 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002551 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002552
2553 if (!evaluate)
2554 check_vars(s, len);
2555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002556 // If "s" is the name of a variable of type VAR_FUNC
2557 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002558 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002559 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002561 // Need to make a copy, in case evaluating the arguments makes
2562 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002563 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002564 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002565 ret = FAIL;
2566 else
2567 {
2568 funcexe_T funcexe;
2569
2570 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002571 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002572 funcexe.fe_firstline = curwin->w_cursor.lnum;
2573 funcexe.fe_lastline = curwin->w_cursor.lnum;
2574 funcexe.fe_evaluate = evaluate;
2575 funcexe.fe_partial = partial;
2576 funcexe.fe_basetv = basetv;
2577 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002578 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002579 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002580 }
2581 vim_free(s);
2582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002583 // If evaluate is FALSE rettv->v_type was not set in
2584 // get_func_tv, but it's needed in handle_subscript() to parse
2585 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002586 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2587 {
2588 rettv->vval.v_string = NULL;
2589 rettv->v_type = VAR_FUNC;
2590 }
2591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002592 // Stop the expression evaluation when immediately
2593 // aborting on error, or when an interrupt occurred or
2594 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002595 if (evaluate && aborting())
2596 {
2597 if (ret == OK)
2598 clear_tv(rettv);
2599 ret = FAIL;
2600 }
2601 return ret;
2602}
2603
2604/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002605 * After a NL, skip over empty lines and comment-only lines.
2606 */
2607 static char_u *
2608newline_skip_comments(char_u *arg)
2609{
2610 char_u *p = arg + 1;
2611
2612 for (;;)
2613 {
2614 p = skipwhite(p);
2615
2616 if (*p == NUL)
2617 break;
2618 if (vim9_comment_start(p))
2619 {
2620 char_u *nl = vim_strchr(p, NL);
2621
2622 if (nl == NULL)
2623 break;
2624 p = nl;
2625 }
2626 if (*p != NL)
2627 break;
2628 ++p; // skip another NL
2629 }
2630 return p;
2631}
2632
2633/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002634 * Get the next line source line without advancing. But do skip over comment
2635 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002636 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002637 */
2638 static char_u *
2639getline_peek_skip_comments(evalarg_T *evalarg)
2640{
2641 for (;;)
2642 {
2643 char_u *next = getline_peek(evalarg->eval_getline,
2644 evalarg->eval_cookie);
2645 char_u *p;
2646
2647 if (next == NULL)
2648 break;
2649 p = skipwhite(next);
2650 if (*p != NUL && !vim9_comment_start(p))
2651 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002652 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002653 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002654 }
2655 return NULL;
2656}
2657
2658/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002659 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2660 * comment) and there is a next line, return the next line (skipping blanks)
2661 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002662 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2663 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002664 * "arg" must point somewhere inside a line, not at the start.
2665 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002666 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002667eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2668{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002669 char_u *p = skipwhite(arg);
2670
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002671 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002672 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002674 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2675 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002676 && (*p == NUL || *p == NL
2677 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002678 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002679 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002680
Bram Moolenaare442d592022-05-05 12:20:28 +01002681 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002682 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002683 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002684 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002685 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002686 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002687
Bram Moolenaar9d489562020-07-30 20:08:50 +02002688 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002689 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002690 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002691 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002692 }
2693 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002694 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002695}
2696
2697/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002698 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002699 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002700 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002701 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002702eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002703{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002704 garray_T *gap = &evalarg->eval_ga;
2705 char_u *line;
2706
Bram Moolenaar39be4982022-05-06 21:51:50 +01002707 if (arg != NULL)
2708 {
2709 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002710 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002711 // Truncate before a trailing comment, so that concatenating the lines
2712 // won't turn the rest into a comment.
2713 if (*skipwhite(arg) == '#')
2714 *arg = NUL;
2715 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002716
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002717 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002718 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2719 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002720 else
2721 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002722 if (line == NULL)
2723 return NULL;
2724
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002725 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002726 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2727 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002728 char_u *p = skipwhite(line);
2729
2730 // Going to concatenate the lines after parsing. For an empty or
2731 // comment line use an empty string.
2732 if (*p == NUL || vim9_comment_start(p))
2733 {
2734 vim_free(line);
2735 line = vim_strsave((char_u *)"");
2736 }
2737
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002738 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2739 ++gap->ga_len;
2740 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002741 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002742 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002743 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002744 evalarg->eval_tofree = line;
2745 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002746
2747 // Advanced to the next line, "arg" no longer points into the previous
2748 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002749 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002750 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002751}
2752
Bram Moolenaare6b53242020-07-01 17:28:33 +02002753/*
2754 * Call eval_next_non_blank() and get the next line if needed.
2755 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002756 char_u *
2757skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2758{
2759 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002760 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002761
Bram Moolenaar962d7212020-07-04 14:15:00 +02002762 if (evalarg == NULL)
2763 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002764 eval_next_non_blank(p, evalarg, &getnext);
2765 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002766 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002767 return p;
2768}
2769
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002770/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002771 * The "eval" functions have an "evalarg" argument: When NULL or
2772 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2773 * parsed but not executed. The functions may return OK, but the rettv will be
2774 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 */
2776
2777/*
2778 * Handle zero level expression.
2779 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002780 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002781 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002782 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 * Return OK or FAIL.
2784 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002786eval0(
2787 char_u *arg,
2788 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002789 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002790 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002792 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2793}
2794
2795/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002796 * If "arg" is a simple function call without arguments then call it and return
2797 * the result. Otherwise return NOTDONE.
2798 */
2799 int
2800may_call_simple_func(
2801 char_u *arg,
2802 typval_T *rettv)
2803{
2804 char_u *parens = (char_u *)strstr((char *)arg, "()");
2805 int r = NOTDONE;
2806
2807 // If the expression is "FuncName()" then we can skip a lot of overhead.
2808 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2809 {
2810 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2811
2812 if (to_name_end(p, TRUE) == parens)
2813 r = call_simple_func(arg, (int)(parens - arg), rettv);
2814 }
2815 return r;
2816}
2817
2818/*
2819 * Handle zero level expression with optimization for a simple function call.
2820 * Same arguments and return value as eval0().
2821 */
2822 int
2823eval0_simple_funccal(
2824 char_u *arg,
2825 typval_T *rettv,
2826 exarg_T *eap,
2827 evalarg_T *evalarg)
2828{
2829 int r = may_call_simple_func(arg, rettv);
2830
2831 if (r == NOTDONE)
2832 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2833 return r;
2834}
2835
2836/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002837 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2838 * expression and don't check what comes after the expression.
2839 */
2840 int
2841eval0_retarg(
2842 char_u *arg,
2843 typval_T *rettv,
2844 exarg_T *eap,
2845 evalarg_T *evalarg,
2846 char_u **retarg)
2847{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 int ret;
2849 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002850 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002851 int did_emsg_before = did_emsg;
2852 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002853 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002854 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002857 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002858
Bram Moolenaar79481362022-06-27 20:15:10 +01002859 if (ret != FAIL)
2860 {
2861 expr_end = p;
2862 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002863
Bram Moolenaar79481362022-06-27 20:15:10 +01002864 // In Vim9 script a command block is not split at NL characters for
2865 // commands using an expression argument. Skip over a '#' comment to
2866 // check for a following NL. Require white space before the '#'.
2867 if (in_vim9script() && p > expr_end && retarg == NULL)
2868 while (*p == '#')
2869 {
2870 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002871
Bram Moolenaar79481362022-06-27 20:15:10 +01002872 if (nl == NULL)
2873 break;
2874 p = skipwhite(nl + 1);
2875 if (eap != NULL && *p != NUL)
2876 eap->nextcmd = p;
2877 check_for_end = FALSE;
2878 }
2879
2880 if (check_for_end)
2881 end_error = !ends_excmd2(arg, p);
2882 }
2883
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002884 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
2886 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002887 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 /*
2889 * Report the invalid expression unless the expression evaluation has
2890 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002891 * exception, or we already gave a more specific error.
2892 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002894 if (!aborting()
2895 && did_emsg == did_emsg_before
2896 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002897 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002898 {
2899 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002900 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002901 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002902 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002903 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002904
zeertzjqf2588b62023-05-05 17:22:35 +01002905 if (eap != NULL && p != NULL)
2906 {
2907 // Some of the expression may not have been consumed.
2908 // Only execute a next command if it cannot be a "||" operator.
2909 // The next command may be "catch".
2910 char_u *nextcmd = check_nextcmd(p);
2911 if (nextcmd != NULL && *nextcmd != '|')
2912 eap->nextcmd = nextcmd;
2913 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002914 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002916
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002917 if (retarg != NULL)
2918 *retarg = p;
2919 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002920 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002921
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 return ret;
2923}
2924
2925/*
2926 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002927 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002928 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 *
2930 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002931 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002933 * Note: "rettv.v_lock" is not set.
2934 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 * Return OK or FAIL.
2936 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002937 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002938eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002940 char_u *p;
2941 int getnext;
2942
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002943 CLEAR_POINTER(rettv);
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 /*
2946 * Get the first variable.
2947 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002948 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 return FAIL;
2950
Bram Moolenaar793648f2020-06-26 21:28:25 +02002951 p = eval_next_non_blank(*arg, evalarg, &getnext);
2952 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002954 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002955 int result;
2956 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002957 evalarg_T *evalarg_used = evalarg;
2958 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002959 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002960 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002961 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002962
2963 if (evalarg == NULL)
2964 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002965 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002966 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002967 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002968 orig_flags = evalarg_used->eval_flags;
2969 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002970
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002971 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002972 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002973 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002974 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002975 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002976 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002977 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002978 clear_tv(rettv);
2979 return FAIL;
2980 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002981 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002982 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002985 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002987 int error = FALSE;
2988
Bram Moolenaar13106602020-10-04 16:06:05 +02002989 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002990 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002991 else if (vim9script)
2992 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002993 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002995 if (error || !op_falsy || !result)
2996 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002997 if (error)
2998 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
3000
3001 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003002 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003004 if (op_falsy)
3005 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003006 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003007 {
mityu4ac198c2021-05-28 17:52:40 +02003008 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003009 clear_tv(rettv);
3010 return FAIL;
3011 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003012 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003013 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003014 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003015 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003016 {
3017 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003019 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003020 if (!op_falsy || !result)
3021 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003023 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003025 /*
3026 * Check for the ":".
3027 */
3028 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3029 if (*p != ':')
3030 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003031 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003032 if (evaluate && result)
3033 clear_tv(rettv);
3034 evalarg_used->eval_flags = orig_flags;
3035 return FAIL;
3036 }
3037 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003038 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003039 else
3040 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003041 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003042 {
3043 error_white_both(p, 1);
3044 clear_tv(rettv);
3045 evalarg_used->eval_flags = orig_flags;
3046 return FAIL;
3047 }
3048 *arg = p;
3049 }
3050
3051 /*
3052 * Get the third variable. Recursive!
3053 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003054 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003055 {
mityu4ac198c2021-05-28 17:52:40 +02003056 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003057 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003058 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003059 return FAIL;
3060 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003061 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3062 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003063 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003064 if (eval1(arg, &var2, evalarg_used) == FAIL)
3065 {
3066 if (evaluate && result)
3067 clear_tv(rettv);
3068 evalarg_used->eval_flags = orig_flags;
3069 return FAIL;
3070 }
3071 if (evaluate && !result)
3072 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003074
3075 if (evalarg == NULL)
3076 clear_evalarg(&local_evalarg, NULL);
3077 else
3078 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 }
3080
3081 return OK;
3082}
3083
3084/*
3085 * Handle first level expression:
3086 * expr2 || expr2 || expr2 logical OR
3087 *
3088 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003089 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 *
3091 * Return OK or FAIL.
3092 */
3093 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003094eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003096 char_u *p;
3097 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003100 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003102 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 return FAIL;
3104
3105 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003106 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003108 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003109 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003111 evalarg_T *evalarg_used = evalarg;
3112 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003113 int evaluate;
3114 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003115 long result = FALSE;
3116 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003117 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003118 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003119
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003120 if (evalarg == NULL)
3121 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003122 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003123 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003124 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003125 orig_flags = evalarg_used->eval_flags;
3126 evaluate = orig_flags & EVAL_EVALUATE;
3127 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003128 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003129 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003130 result = tv_get_bool_chk(rettv, &error);
3131 else if (tv_get_number_chk(rettv, &error) != 0)
3132 result = TRUE;
3133 clear_tv(rettv);
3134 if (error)
3135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 }
3137
3138 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003139 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003141 while (p[0] == '|' && p[1] == '|')
3142 {
3143 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003144 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003145 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003146 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003147 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003148 {
3149 error_white_both(p, 2);
3150 clear_tv(rettv);
3151 return FAIL;
3152 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003153 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003154 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003155
3156 /*
3157 * Get the second variable.
3158 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003159 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003160 {
mityu4ac198c2021-05-28 17:52:40 +02003161 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003162 clear_tv(rettv);
3163 return FAIL;
3164 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003165 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3166 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003167 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003168 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003169 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003170
3171 /*
3172 * Compute the result.
3173 */
3174 if (evaluate && !result)
3175 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003176 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003177 result = tv_get_bool_chk(&var2, &error);
3178 else if (tv_get_number_chk(&var2, &error) != 0)
3179 result = TRUE;
3180 clear_tv(&var2);
3181 if (error)
3182 return FAIL;
3183 }
3184 if (evaluate)
3185 {
3186 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003187 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003188 rettv->v_type = VAR_BOOL;
3189 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003190 }
3191 else
3192 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003193 rettv->v_type = VAR_NUMBER;
3194 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003195 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003196 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003197
3198 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003200
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003201 if (evalarg == NULL)
3202 clear_evalarg(&local_evalarg, NULL);
3203 else
3204 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
3206
3207 return OK;
3208}
3209
3210/*
3211 * Handle second level expression:
3212 * expr3 && expr3 && expr3 logical AND
3213 *
3214 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003215 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 *
3217 * Return OK or FAIL.
3218 */
3219 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003222 char_u *p;
3223 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
3225 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003226 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003228 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 return FAIL;
3230
3231 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003232 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003234 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003235 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003237 evalarg_T *evalarg_used = evalarg;
3238 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003239 int orig_flags;
3240 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003241 long result = TRUE;
3242 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003243 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003244 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003245
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003246 if (evalarg == NULL)
3247 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003248 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003249 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003250 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003251 orig_flags = evalarg_used->eval_flags;
3252 evaluate = orig_flags & EVAL_EVALUATE;
3253 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003254 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003255 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003256 result = tv_get_bool_chk(rettv, &error);
3257 else if (tv_get_number_chk(rettv, &error) == 0)
3258 result = FALSE;
3259 clear_tv(rettv);
3260 if (error)
3261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263
3264 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003265 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003267 while (p[0] == '&' && p[1] == '&')
3268 {
3269 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003270 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003271 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003272 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003273 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003274 {
3275 error_white_both(p, 2);
3276 clear_tv(rettv);
3277 return FAIL;
3278 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003279 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003280 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003281
3282 /*
3283 * Get the second variable.
3284 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003285 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003286 {
mityu4ac198c2021-05-28 17:52:40 +02003287 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003288 clear_tv(rettv);
3289 return FAIL;
3290 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003291 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3292 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003293 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003294 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003295 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003296 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003297
3298 /*
3299 * Compute the result.
3300 */
3301 if (evaluate && result)
3302 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003303 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003304 result = tv_get_bool_chk(&var2, &error);
3305 else if (tv_get_number_chk(&var2, &error) == 0)
3306 result = FALSE;
3307 clear_tv(&var2);
3308 if (error)
3309 return FAIL;
3310 }
3311 if (evaluate)
3312 {
3313 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003314 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003315 rettv->v_type = VAR_BOOL;
3316 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003317 }
3318 else
3319 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003320 rettv->v_type = VAR_NUMBER;
3321 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003322 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003323 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003324
3325 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003327
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003328 if (evalarg == NULL)
3329 clear_evalarg(&local_evalarg, NULL);
3330 else
3331 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 }
3333
3334 return OK;
3335}
3336
3337/*
3338 * Handle third level expression:
3339 * var1 == var2
3340 * var1 =~ var2
3341 * var1 != var2
3342 * var1 !~ var2
3343 * var1 > var2
3344 * var1 >= var2
3345 * var1 < var2
3346 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003347 * var1 is var2
3348 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 *
3350 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003351 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 *
3353 * Return OK or FAIL.
3354 */
3355 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003356eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003359 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003360 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003362 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003365 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003367 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 return FAIL;
3369
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003370 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003371
Bram Moolenaar696ba232020-07-29 21:20:41 +02003372 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003375 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003377 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003379 typval_T var2;
3380 int ic;
3381 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003382 int evaluate = evalarg == NULL
3383 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003384 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003385
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003386 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003387 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003388 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003389 p = *arg;
3390 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003391 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3392 {
mityu4ac198c2021-05-28 17:52:40 +02003393 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003394 clear_tv(rettv);
3395 return FAIL;
3396 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003397
Bram Moolenaar696ba232020-07-29 21:20:41 +02003398 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3399 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003400 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003401 clear_tv(rettv);
3402 return FAIL;
3403 }
3404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003405 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (p[len] == '?')
3407 {
3408 ic = TRUE;
3409 ++len;
3410 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003411 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 else if (p[len] == '#')
3413 {
3414 ic = FALSE;
3415 ++len;
3416 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003417 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003419 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
3421 /*
3422 * Get the second variable.
3423 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003424 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3425 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003426 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003427 clear_tv(rettv);
3428 return FAIL;
3429 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003430 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003431 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003433 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 return FAIL;
3435 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003436 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003437 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003438 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003439
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003440 // use the line of the comparison for messages
3441 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003442 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003443 {
3444 ret = FAIL;
3445 clear_tv(rettv);
3446 }
3447 else
3448 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003449 clear_tv(&var2);
3450 return ret;
3451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453
3454 return OK;
3455}
3456
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003457/*
3458 * Make a copy of blob "tv1" and append blob "tv2".
3459 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 void
3461eval_addblob(typval_T *tv1, typval_T *tv2)
3462{
3463 blob_T *b1 = tv1->vval.v_blob;
3464 blob_T *b2 = tv2->vval.v_blob;
3465 blob_T *b = blob_alloc();
3466 int i;
3467
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003468 if (b == NULL)
3469 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003471 for (i = 0; i < blob_len(b1); i++)
3472 ga_append(&b->bv_ga, blob_get(b1, i));
3473 for (i = 0; i < blob_len(b2); i++)
3474 ga_append(&b->bv_ga, blob_get(b2, i));
3475
3476 clear_tv(tv1);
3477 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478}
3479
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003480/*
3481 * Make a copy of list "tv1" and append list "tv2".
3482 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 int
3484eval_addlist(typval_T *tv1, typval_T *tv2)
3485{
3486 typval_T var3;
3487
3488 // concatenate Lists
3489 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3490 {
3491 clear_tv(tv1);
3492 clear_tv(tv2);
3493 return FAIL;
3494 }
3495 clear_tv(tv1);
3496 *tv1 = var3;
3497 return OK;
3498}
3499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003501 * Handle the bitwise left/right shift operator expression:
3502 * var1 << var2
3503 * var1 >> var2
3504 *
3505 * "arg" must point to the first non-white of the expression.
3506 * "arg" is advanced to just after the recognized expression.
3507 *
3508 * Return OK or FAIL.
3509 */
3510 static int
3511eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3512{
3513 /*
3514 * Get the first expression.
3515 */
3516 if (eval6(arg, rettv, evalarg) == FAIL)
3517 return FAIL;
3518
3519 /*
3520 * Repeat computing, until no '<<' or '>>' is following.
3521 */
3522 for (;;)
3523 {
3524 char_u *p;
3525 int getnext;
3526 exprtype_T type;
3527 int evaluate;
3528 typval_T var2;
3529 int vim9script;
3530
3531 p = eval_next_non_blank(*arg, evalarg, &getnext);
3532 if (p[0] == '<' && p[1] == '<')
3533 type = EXPR_LSHIFT;
3534 else if (p[0] == '>' && p[1] == '>')
3535 type = EXPR_RSHIFT;
3536 else
3537 return OK;
3538
3539 // Handle a bitwise left or right shift operator
3540 if (rettv->v_type != VAR_NUMBER)
3541 {
3542 // left operand should be a number
3543 emsg(_(e_bitshift_ops_must_be_number));
3544 clear_tv(rettv);
3545 return FAIL;
3546 }
3547
3548 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3549 vim9script = in_vim9script();
3550 if (getnext)
3551 {
3552 *arg = eval_next_line(*arg, evalarg);
3553 p = *arg;
3554 }
3555 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3556 {
3557 error_white_both(*arg, 2);
3558 clear_tv(rettv);
3559 return FAIL;
3560 }
3561
3562 /*
3563 * Get the second variable.
3564 */
3565 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3566 {
3567 error_white_both(p, 2);
3568 clear_tv(rettv);
3569 return FAIL;
3570 }
3571 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3572 if (eval6(arg, &var2, evalarg) == FAIL)
3573 {
3574 clear_tv(rettv);
3575 return FAIL;
3576 }
3577
3578 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3579 {
3580 // right operand should be a positive number
3581 if (var2.v_type != VAR_NUMBER)
3582 emsg(_(e_bitshift_ops_must_be_number));
3583 else
dundargocc57b5bc2022-11-02 13:30:51 +00003584 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003585 clear_tv(rettv);
3586 clear_tv(&var2);
3587 return FAIL;
3588 }
3589
3590 if (evaluate)
3591 {
3592 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3593 // shifting more bits than we have always results in zero
3594 rettv->vval.v_number = 0;
3595 else if (type == EXPR_LSHIFT)
3596 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003597 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003598 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003599 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003600 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003601 }
3602
3603 clear_tv(&var2);
3604 }
3605
3606 return OK;
3607}
3608
3609/*
3610 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003611 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003613 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003614 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 *
3616 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003617 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 *
3619 * Return OK or FAIL.
3620 */
3621 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003622eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003625 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003627 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 return FAIL;
3629
3630 /*
3631 * Repeat computing, until no '+', '-' or '.' is following.
3632 */
3633 for (;;)
3634 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003635 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003636 int getnext;
3637 char_u *p;
3638 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003639 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003640 int concat;
3641 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003642 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003643
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003644 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003645 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003646 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003647 p = eval_next_non_blank(*arg, evalarg, &getnext);
3648 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003649 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003650 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3651 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003653 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3654 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003655
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003656 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003657 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003658 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003659 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003660 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003661 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003662 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003663 {
mityu4ac198c2021-05-28 17:52:40 +02003664 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003665 clear_tv(rettv);
3666 return FAIL;
3667 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003668 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003669 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003670 if ((op != '+' || (rettv->v_type != VAR_LIST
3671 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003672 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003673 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003674 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003675 int error = FALSE;
3676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003677 // For "list + ...", an illegal use of the first operand as
3678 // a number cannot be determined before evaluating the 2nd
3679 // operand: if this is also a list, all is ok.
3680 // For "something . ...", "something - ..." or "non-list + ...",
3681 // we know that the first operand needs to be a string or number
3682 // without evaluating the 2nd operand. So check before to avoid
3683 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003684 if (op != '.')
3685 tv_get_number_chk(rettv, &error);
3686 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003687 {
3688 clear_tv(rettv);
3689 return FAIL;
3690 }
3691 }
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 /*
3694 * Get the second variable.
3695 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003696 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003697 {
mityu89dcb4d2021-05-28 13:50:17 +02003698 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003699 clear_tv(rettv);
3700 return FAIL;
3701 }
3702 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003703 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003705 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 return FAIL;
3707 }
3708
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003709 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 {
3711 /*
3712 * Compute the result.
3713 */
3714 if (op == '.')
3715 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003716 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3717 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003718 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003719
Bram Moolenaar2e086612020-08-25 22:37:48 +02003720 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003721 || var2.v_type == VAR_CHANNEL
3722 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003723 semsg(_(e_using_invalid_value_as_string_str),
3724 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003725 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003726 {
3727 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3728 var2.vval.v_float);
3729 s2 = buf2;
3730 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003731 else
3732 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003733 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003734 {
3735 clear_tv(rettv);
3736 clear_tv(&var2);
3737 return FAIL;
3738 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003739 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003740 clear_tv(rettv);
3741 rettv->v_type = VAR_STRING;
3742 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003744 else if (op == '+' && rettv->v_type == VAR_BLOB
3745 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003747 else if (op == '+' && rettv->v_type == VAR_LIST
3748 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003749 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003751 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 else
3754 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003755 int error = FALSE;
3756 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003757 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003758
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003759 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003760 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003761 f1 = rettv->vval.v_float;
3762 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003764 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003766 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003767 if (error)
3768 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003769 // This can only happen for "list + non-list" or
3770 // "blob + non-blob". For "non-list + ..." or
3771 // "something - ...", we returned before evaluating the
3772 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003773 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003774 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775 return FAIL;
3776 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003777 if (var2.v_type == VAR_FLOAT)
3778 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003779 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003780 if (var2.v_type == VAR_FLOAT)
3781 {
3782 f2 = var2.vval.v_float;
3783 n2 = 0;
3784 }
3785 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003787 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003788 if (error)
3789 {
3790 clear_tv(rettv);
3791 clear_tv(&var2);
3792 return FAIL;
3793 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003794 if (rettv->v_type == VAR_FLOAT)
3795 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003799 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003800 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3801 {
3802 if (op == '+')
3803 f1 = f1 + f2;
3804 else
3805 f1 = f1 - f2;
3806 rettv->v_type = VAR_FLOAT;
3807 rettv->vval.v_float = f1;
3808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003810 {
3811 if (op == '+')
3812 n1 = n1 + n2;
3813 else
3814 n1 = n1 - n2;
3815 rettv->v_type = VAR_NUMBER;
3816 rettv->vval.v_number = n1;
3817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003819 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 }
3822 return OK;
3823}
3824
3825/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003826 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 * * number multiplication
3828 * / number division
3829 * % number modulo
3830 *
3831 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003832 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 *
3834 * Return OK or FAIL.
3835 */
3836 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003837eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003838 char_u **arg,
3839 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003840 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003841 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003843 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
3845 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003846 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003848 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 return FAIL;
3850
3851 /*
3852 * Repeat computing, until no '*', '/' or '%' is following.
3853 */
3854 for (;;)
3855 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003856 int evaluate;
3857 int getnext;
3858 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003859 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003860 int op;
3861 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003862 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003863 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003864
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003865 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003866 p = eval_next_non_blank(*arg, evalarg, &getnext);
3867 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003868 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003870
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003871 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003872 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003873 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003874 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003875 {
3876 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3877 {
mityu4ac198c2021-05-28 17:52:40 +02003878 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003879 clear_tv(rettv);
3880 return FAIL;
3881 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003882 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003885 f1 = 0;
3886 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003887 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003888 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003890 if (rettv->v_type == VAR_FLOAT)
3891 {
3892 f1 = rettv->vval.v_float;
3893 use_float = TRUE;
3894 n1 = 0;
3895 }
3896 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003897 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003898 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003899 if (error)
3900 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902 else
3903 n1 = 0;
3904
3905 /*
3906 * Get the second variable.
3907 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003908 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3909 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003910 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003911 clear_tv(rettv);
3912 return FAIL;
3913 }
3914 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003915 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 return FAIL;
3917
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003918 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003920 if (var2.v_type == VAR_FLOAT)
3921 {
3922 if (!use_float)
3923 {
3924 f1 = n1;
3925 use_float = TRUE;
3926 }
3927 f2 = var2.vval.v_float;
3928 n2 = 0;
3929 }
3930 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003931 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003932 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003933 clear_tv(&var2);
3934 if (error)
3935 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003936 if (use_float)
3937 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
3940 /*
3941 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003942 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003944 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003946 if (op == '*')
3947 f1 = f1 * f2;
3948 else if (op == '/')
3949 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003950#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003951 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003952 if (f2 == 0.0)
3953 {
3954 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003955 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003956 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003957 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003958 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003959 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003960 }
3961 else
3962 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003963#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003964 // We rely on the floating point library to handle divide
3965 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003966 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003967#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003970 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003971 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003972 return FAIL;
3973 }
3974 rettv->v_type = VAR_FLOAT;
3975 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
3977 else
3978 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003979 int failed = FALSE;
3980
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003981 if (op == '*')
3982 n1 = n1 * n2;
3983 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003984 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003986 n1 = num_modulus(n1, n2, &failed);
3987 if (failed)
3988 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003989
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003990 rettv->v_type = VAR_NUMBER;
3991 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 }
3995
3996 return OK;
3997}
3998
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003999/*
4000 * Handle a type cast before a base level expression.
4001 * "arg" must point to the first non-white of the expression.
4002 * "arg" is advanced to just after the recognized expression.
4003 * Return OK or FAIL.
4004 */
4005 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004006eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004007 char_u **arg,
4008 typval_T *rettv,
4009 evalarg_T *evalarg,
4010 int want_string) // after "." operator
4011{
4012 type_T *want_type = NULL;
4013 garray_T type_list; // list of pointers to allocated types
4014 int res;
4015 int evaluate = evalarg == NULL ? 0
4016 : (evalarg->eval_flags & EVAL_EVALUATE);
4017
4018 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004019 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4020 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004021 {
4022 ++*arg;
4023 ga_init2(&type_list, sizeof(type_T *), 10);
4024 want_type = parse_type(arg, &type_list, TRUE);
4025 if (want_type == NULL && (evaluate || **arg != '>'))
4026 {
4027 clear_type_list(&type_list);
4028 return FAIL;
4029 }
4030
4031 if (**arg != '>')
4032 {
4033 if (*skipwhite(*arg) == '>')
4034 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4035 else
4036 emsg(_(e_missing_gt));
4037 clear_type_list(&type_list);
4038 return FAIL;
4039 }
4040 ++*arg;
4041 *arg = skipwhite_and_linebreak(*arg, evalarg);
4042 }
4043
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004044 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004045
4046 if (want_type != NULL && evaluate)
4047 {
4048 if (res == OK)
4049 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004050 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4051 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004052
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004053 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004054 {
4055 if (want_type == &t_bool && actual != &t_bool
4056 && (actual->tt_flags & TTFLAG_BOOL_OK))
4057 {
4058 int n = tv2bool(rettv);
4059
4060 // can use "0" and "1" for boolean in some places
4061 clear_tv(rettv);
4062 rettv->v_type = VAR_BOOL;
4063 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4064 }
4065 else
4066 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004067 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004068
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004069 res = check_type(want_type, actual, TRUE, where);
4070 }
4071 }
4072 }
4073 clear_type_list(&type_list);
4074 }
4075
4076 return res;
4077}
4078
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004079 int
4080eval_leader(char_u **arg, int vim9)
4081{
4082 char_u *s = *arg;
4083 char_u *p = *arg;
4084
4085 while (*p == '!' || *p == '-' || *p == '+')
4086 {
4087 char_u *n = skipwhite(p + 1);
4088
4089 // ++, --, -+ and +- are not accepted in Vim9 script
4090 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4091 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004092 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004093 return FAIL;
4094 }
4095 p = n;
4096 }
4097 *arg = p;
4098 return OK;
4099}
4100
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004102 * Check for a predefined value "true", "false" and "null.*".
4103 * Return OK when recognized.
4104 */
4105 int
4106handle_predefined(char_u *s, int len, typval_T *rettv)
4107{
4108 switch (len)
4109 {
4110 case 4: if (STRNCMP(s, "true", 4) == 0)
4111 {
4112 rettv->v_type = VAR_BOOL;
4113 rettv->vval.v_number = VVAL_TRUE;
4114 return OK;
4115 }
4116 if (STRNCMP(s, "null", 4) == 0)
4117 {
4118 rettv->v_type = VAR_SPECIAL;
4119 rettv->vval.v_number = VVAL_NULL;
4120 return OK;
4121 }
4122 break;
4123 case 5: if (STRNCMP(s, "false", 5) == 0)
4124 {
4125 rettv->v_type = VAR_BOOL;
4126 rettv->vval.v_number = VVAL_FALSE;
4127 return OK;
4128 }
4129 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004130 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4131 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004132#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004133 rettv->v_type = VAR_JOB;
4134 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004135#else
4136 rettv->v_type = VAR_SPECIAL;
4137 rettv->vval.v_number = VVAL_NULL;
4138#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004139 return OK;
4140 }
4141 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004142 case 9:
4143 if (STRNCMP(s, "null_", 5) != 0)
4144 break;
4145 if (STRNCMP(s + 5, "list", 4) == 0)
4146 {
4147 rettv->v_type = VAR_LIST;
4148 rettv->vval.v_list = NULL;
4149 return OK;
4150 }
4151 if (STRNCMP(s + 5, "dict", 4) == 0)
4152 {
4153 rettv->v_type = VAR_DICT;
4154 rettv->vval.v_dict = NULL;
4155 return OK;
4156 }
4157 if (STRNCMP(s + 5, "blob", 4) == 0)
4158 {
4159 rettv->v_type = VAR_BLOB;
4160 rettv->vval.v_blob = NULL;
4161 return OK;
4162 }
4163 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004164 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4165 {
4166 rettv->v_type = VAR_CLASS;
4167 rettv->vval.v_class = NULL;
4168 return OK;
4169 }
4170 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004171 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4172 {
4173 rettv->v_type = VAR_STRING;
4174 rettv->vval.v_string = NULL;
4175 return OK;
4176 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004177 if (STRNCMP(s, "null_object", 11) == 0)
4178 {
4179 rettv->v_type = VAR_OBJECT;
4180 rettv->vval.v_object = NULL;
4181 return OK;
4182 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004183 break;
4184 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004185 if (STRNCMP(s, "null_channel", 12) == 0)
4186 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004187#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004188 rettv->v_type = VAR_CHANNEL;
4189 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004190#else
4191 rettv->v_type = VAR_SPECIAL;
4192 rettv->vval.v_number = VVAL_NULL;
4193#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004194 return OK;
4195 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004196 if (STRNCMP(s, "null_partial", 12) == 0)
4197 {
4198 rettv->v_type = VAR_PARTIAL;
4199 rettv->vval.v_partial = NULL;
4200 return OK;
4201 }
4202 break;
4203 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4204 {
4205 rettv->v_type = VAR_FUNC;
4206 rettv->vval.v_string = NULL;
4207 return OK;
4208 }
4209 break;
4210 }
4211 return FAIL;
4212}
4213
4214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 * Handle sixth level expression:
4216 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004217 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004218 * "string" string constant
4219 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 * &option-name option value
4221 * @r register contents
4222 * identifier variable value
4223 * function() function call
4224 * $VAR environment variable
4225 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004226 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004228 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004229 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 *
4231 * Also handle:
4232 * ! in front logical NOT
4233 * - in front unary minus
4234 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004235 * trailing [] subscript in String or List
4236 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004237 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 *
4239 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004240 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 *
4242 * Return OK or FAIL.
4243 */
4244 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004245eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246 char_u **arg,
4247 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004248 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004251 int evaluate = evalarg != NULL
4252 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 int len;
4254 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004255 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 char_u *start_leader, *end_leader;
4257 int ret = OK;
4258 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004259 static int recurse = 0;
4260 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
4262 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004264 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
4268 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004269 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 */
4271 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004272 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 end_leader = *arg;
4275
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004276 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004277 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004278 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004279 ++*arg;
4280 return FAIL;
4281 }
4282
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004283 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004284 // and crash. With MSVC the stack is smaller.
4285 if (recurse ==
4286#ifdef _MSC_VER
4287 300
4288#else
4289 1000
4290#endif
4291 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004292 {
4293 semsg(_(e_expression_too_recursive_str), *arg);
4294 return FAIL;
4295 }
4296 ++recurse;
4297
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 switch (**arg)
4299 {
4300 /*
4301 * Number constant.
4302 */
4303 case '0':
4304 case '1':
4305 case '2':
4306 case '3':
4307 case '4':
4308 case '5':
4309 case '6':
4310 case '7':
4311 case '8':
4312 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004313 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004314
4315 // Apply prefixed "-" and "+" now. Matters especially when
4316 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004317 if (ret == OK && evaluate && end_leader > start_leader
4318 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004319 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 break;
4321
4322 /*
4323 * String constant: "string".
4324 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004325 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 break;
4327
4328 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004329 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004331 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004332 break;
4333
4334 /*
4335 * List: [expr, expr]
4336 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004337 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 break;
4339
4340 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004341 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004342 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004343 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004344 {
4345 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4346 }
4347 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004348 {
4349 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004350 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004351 }
4352 else
4353 ret = NOTDONE;
4354 break;
4355
4356 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004357 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004358 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004359 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004360 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004361 ret = NOTDONE;
4362 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004363 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004364 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004365 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004366 break;
4367
4368 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004369 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004371 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 break;
4373
4374 /*
4375 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004376 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 */
LemonBoy2eaef102022-05-06 13:14:50 +01004378 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4379 ret = eval_interp_string(arg, rettv, evaluate);
4380 else
4381 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 break;
4383
4384 /*
4385 * Register contents: @r.
4386 */
4387 case '@': ++*arg;
4388 if (evaluate)
4389 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004390 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004391 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004392 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004393 emsg_invreg(**arg);
4394 else
4395 {
4396 rettv->v_type = VAR_STRING;
4397 rettv->vval.v_string = get_reg_contents(**arg,
4398 GREG_EXPR_SRC);
4399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
4401 if (**arg != NUL)
4402 ++*arg;
4403 break;
4404
4405 /*
4406 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004407 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004409 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004410 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004411 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004412 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004413 if (ret == OK && evaluate)
4414 {
4415 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4416
Bram Moolenaara9931532021-06-12 15:58:16 +02004417 // Compile it here to get the return type. The return
4418 // type is optional, when it's missing use t_unknown.
4419 // This is recognized in compile_return().
4420 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4421 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004422 if (compile_def_function(ufunc, FALSE,
4423 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004424 {
4425 clear_tv(rettv);
4426 ret = FAIL;
4427 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004428 }
4429 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004430 if (ret == NOTDONE)
4431 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004432 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004433 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004434
Bram Moolenaar9215f012020-06-27 21:18:00 +02004435 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004436 if (**arg == ')')
4437 ++*arg;
4438 else if (ret == OK)
4439 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004440 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004441 clear_tv(rettv);
4442 ret = FAIL;
4443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445 break;
4446
Bram Moolenaar8c711452005-01-14 21:53:12 +00004447 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 break;
4449 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450
4451 if (ret == NOTDONE)
4452 {
4453 /*
4454 * Must be a variable or function name.
4455 * Can also be a curly-braces kind of name: {expr}.
4456 */
4457 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004458 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 if (alias != NULL)
4460 s = alias;
4461
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004462 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463 ret = FAIL;
4464 else
4465 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004466 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4467
Bram Moolenaar4525a572022-02-13 11:57:33 +00004468 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004469 {
4470 emsg(_(e_cannot_use_underscore_here));
4471 ret = FAIL;
4472 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004473 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004474 && s[0] == 's' && s[1] == ':')
4475 {
4476 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4477 ret = FAIL;
4478 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004479 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004480 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004481 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004482 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004483 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004484 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004485 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004486 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004487 // get the value of "true", "false", etc. or a variable
4488 ret = FAIL;
4489 if (vim9script)
4490 ret = handle_predefined(s, len, rettv);
4491 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004492 {
4493 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004494 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004495 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004496 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004498 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004499 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004500 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004501 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004502 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004505 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 }
4507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004508 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4509 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004510 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004511 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
4513 /*
4514 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4515 */
4516 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004517 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004518
4519 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004520 return ret;
4521}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004522
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004523/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004524 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004525 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004526 * Adjusts "end_leaderp" until it is at "start_leader".
4527 */
4528 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004529eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004530 typval_T *rettv,
4531 int numeric_only,
4532 char_u *start_leader,
4533 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004534{
4535 char_u *end_leader = *end_leaderp;
4536 int ret = OK;
4537 int error = FALSE;
4538 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004539 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004540 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004541 float_T f = 0.0;
4542
4543 if (rettv->v_type == VAR_FLOAT)
4544 f = rettv->vval.v_float;
4545 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004546 {
4547 while (VIM_ISWHITE(end_leader[-1]))
4548 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004549 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004550 val = tv2bool(rettv);
4551 else
4552 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004553 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004554 if (error)
4555 {
4556 clear_tv(rettv);
4557 ret = FAIL;
4558 }
4559 else
4560 {
4561 while (end_leader > start_leader)
4562 {
4563 --end_leader;
4564 if (*end_leader == '!')
4565 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004566 if (numeric_only)
4567 {
4568 ++end_leader;
4569 break;
4570 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004571 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004572 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004573 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004574 {
4575 rettv->v_type = VAR_BOOL;
4576 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4577 }
4578 else
4579 f = !f;
4580 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004581 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004582 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004583 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004584 type = VAR_BOOL;
4585 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004586 }
4587 else if (*end_leader == '-')
4588 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004589 if (rettv->v_type == VAR_FLOAT)
4590 f = -f;
4591 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004592 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004593 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004594 type = VAR_NUMBER;
4595 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004596 }
4597 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004598 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004600 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004601 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004603 else
4604 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004605 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004606 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004607 rettv->v_type = type;
4608 else
4609 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004610 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004613 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 return ret;
4615}
4616
4617/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004618 * Call the function referred to in "rettv".
4619 */
4620 static int
4621call_func_rettv(
4622 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004623 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004624 typval_T *rettv,
4625 int evaluate,
4626 dict_T *selfdict,
4627 typval_T *basetv)
4628{
4629 partial_T *pt = NULL;
4630 funcexe_T funcexe;
4631 typval_T functv;
4632 char_u *s;
4633 int ret;
4634
4635 // need to copy the funcref so that we can clear rettv
4636 if (evaluate)
4637 {
4638 functv = *rettv;
4639 rettv->v_type = VAR_UNKNOWN;
4640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004641 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004642 if (functv.v_type == VAR_PARTIAL)
4643 {
4644 pt = functv.vval.v_partial;
4645 s = partial_name(pt);
4646 }
4647 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004648 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004649 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004650 if (s == NULL || *s == NUL)
4651 {
4652 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004653 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004654 goto theend;
4655 }
4656 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004657 }
4658 else
4659 s = (char_u *)"";
4660
Bram Moolenaara80faa82020-04-12 19:37:17 +02004661 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004662 funcexe.fe_firstline = curwin->w_cursor.lnum;
4663 funcexe.fe_lastline = curwin->w_cursor.lnum;
4664 funcexe.fe_evaluate = evaluate;
4665 funcexe.fe_partial = pt;
4666 funcexe.fe_selfdict = selfdict;
4667 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004668 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004669
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004670theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004671 // Clear the funcref afterwards, so that deleting it while
4672 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004673 if (evaluate)
4674 clear_tv(&functv);
4675
4676 return ret;
4677}
4678
4679/*
4680 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004681 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004682 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4683 */
4684 static int
4685eval_lambda(
4686 char_u **arg,
4687 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004688 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004689 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004690{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004691 int evaluate = evalarg != NULL
4692 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004693 typval_T base = *rettv;
4694 int ret;
4695
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004696 rettv->v_type = VAR_UNKNOWN;
4697
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004698 if (**arg == '{')
4699 {
4700 // ->{lambda}()
4701 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4702 }
4703 else
4704 {
4705 // ->(lambda)()
4706 ++*arg;
4707 ret = eval1(arg, rettv, evalarg);
4708 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004709 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004710 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004711 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004712 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004713 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004714 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4715 && rettv->v_type != VAR_PARTIAL)
4716 {
4717 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4718 return FAIL;
4719 }
4720 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004721 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004722 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004723 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004724
4725 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004726 {
4727 if (verbose)
4728 {
4729 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004730 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004731 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004732 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004733 }
4734 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004735 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004736 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004737 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004738 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004739
4740 // Clear the funcref afterwards, so that deleting it while
4741 // evaluating the arguments is possible (see test55).
4742 if (evaluate)
4743 clear_tv(&base);
4744
4745 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004746}
4747
4748/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004749 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004750 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004751 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4752 */
4753 static int
4754eval_method(
4755 char_u **arg,
4756 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004757 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004758 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004759{
4760 char_u *name;
4761 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004762 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004763 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004764 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004765 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004766 int evaluate = evalarg != NULL
4767 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004768
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004769 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004770
Bram Moolenaarac92e252019-08-03 21:58:38 +02004771 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004772 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004773 if (alias != NULL)
4774 name = alias;
4775
4776 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004777 {
4778 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004779 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004780 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004781 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004782 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004783 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004784 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004785
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004786 // If there is no "(" immediately following, but there is further on,
4787 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4788 // Does not handle anything where "(" is part of the expression.
4789 *arg = skipwhite(*arg);
4790
4791 if (**arg != '(' && alias == NULL
4792 && (paren = vim_strchr(*arg, '(')) != NULL)
4793 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004794 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004795
4796 // Truncate the name a the "(". Avoid trying to get another line
4797 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004798 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004799 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4800 if (evalarg != NULL)
4801 {
4802 getline = evalarg->eval_getline;
4803 evalarg->eval_getline = NULL;
4804 }
4805
4806 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004807 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004808 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004809 *arg = name + len;
4810 ret = FAIL;
4811 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004812 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004813 {
4814 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004815 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004816 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004817
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004818 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004819 if (getline != NULL)
4820 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004821 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004822
4823 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004824 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004825 *arg = skipwhite(*arg);
4826
4827 if (**arg != '(')
4828 {
4829 if (verbose)
4830 semsg(_(e_missing_parenthesis_str), name);
4831 ret = FAIL;
4832 }
4833 else if (VIM_ISWHITE((*arg)[-1]))
4834 {
4835 if (verbose)
4836 emsg(_(e_no_white_space_allowed_before_parenthesis));
4837 ret = FAIL;
4838 }
4839 else
4840 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004841 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004842 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004843 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004844
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004845 // Clear the funcref afterwards, so that deleting it while
4846 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004847 if (evaluate)
4848 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004849 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004850
Bram Moolenaarac92e252019-08-03 21:58:38 +02004851 return ret;
4852}
4853
4854/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004855 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4856 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004857 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4858 */
4859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004860eval_index(
4861 char_u **arg,
4862 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004863 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004864 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004866 int evaluate = evalarg != NULL
4867 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004869 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004870 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004871 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004872 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004873 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004874
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004875 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4876 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004878 init_tv(&var1);
4879 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004880 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004882 /*
4883 * dict.name
4884 */
4885 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004886 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004887 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004888 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004889 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004890 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004891 }
4892 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004894 /*
4895 * something[idx]
4896 *
4897 * Get the (first) variable from inside the [].
4898 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004899 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004900 if (**arg == ':')
4901 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004902 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004904 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004905 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004906 semsg(_(e_white_space_required_before_and_after_str_at_str),
4907 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004908 clear_tv(&var1);
4909 return FAIL;
4910 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004911 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004913 int error = FALSE;
4914
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004915 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004916 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004917 && var1.v_type == VAR_FLOAT)
4918 {
4919 var1.vval.v_string = typval_tostring(&var1, TRUE);
4920 var1.v_type = VAR_STRING;
4921 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004922
Bram Moolenaar4525a572022-02-13 11:57:33 +00004923 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004924 tv_get_number_chk(&var1, &error);
4925 else
4926 error = tv_get_string_chk(&var1) == NULL;
4927 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004928 {
4929 // not a number or string
4930 clear_tv(&var1);
4931 return FAIL;
4932 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004933 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934
4935 /*
4936 * Get the second variable from inside the [:].
4937 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004938 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004939 if (**arg == ':')
4940 {
4941 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004942 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004943 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004944 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004945 semsg(_(e_white_space_required_before_and_after_str_at_str),
4946 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004947 if (!empty1)
4948 clear_tv(&var1);
4949 return FAIL;
4950 }
4951 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004952 if (**arg == ']')
4953 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004954 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004956 if (!empty1)
4957 clear_tv(&var1);
4958 return FAIL;
4959 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004960 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004963 if (!empty1)
4964 clear_tv(&var1);
4965 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 return FAIL;
4967 }
4968 }
4969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004970 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004971 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 if (**arg != ']')
4973 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004974 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004975 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 clear_tv(&var1);
4977 if (range)
4978 clear_tv(&var2);
4979 return FAIL;
4980 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004981 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 }
4983
4984 if (evaluate)
4985 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004986 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004987 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004988 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004989
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004990 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004991 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004992 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004993 clear_tv(&var2);
4994 return res;
4995 }
4996 return OK;
4997}
4998
4999/*
5000 * Check if "rettv" can have an [index] or [sli:ce]
5001 */
5002 int
5003check_can_index(typval_T *rettv, int evaluate, int verbose)
5004{
5005 switch (rettv->v_type)
5006 {
5007 case VAR_FUNC:
5008 case VAR_PARTIAL:
5009 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005010 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005011 return FAIL;
5012 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005013 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005014 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005015 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005016 case VAR_BOOL:
5017 case VAR_SPECIAL:
5018 case VAR_JOB:
5019 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005020 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005021 case VAR_CLASS:
5022 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005023 if (verbose)
5024 emsg(_(e_cannot_index_special_variable));
5025 return FAIL;
5026 case VAR_UNKNOWN:
5027 case VAR_ANY:
5028 case VAR_VOID:
5029 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005030 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005031 emsg(_(e_cannot_index_special_variable));
5032 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005033 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005034 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005035
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005036 case VAR_STRING:
5037 case VAR_LIST:
5038 case VAR_DICT:
5039 case VAR_BLOB:
5040 break;
5041 case VAR_NUMBER:
5042 if (in_vim9script())
5043 emsg(_(e_cannot_index_number));
5044 break;
5045 }
5046 return OK;
5047}
5048
5049/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005050 * slice() function
5051 */
5052 void
5053f_slice(typval_T *argvars, typval_T *rettv)
5054{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005055 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005056 && ((argvars[0].v_type != VAR_STRING
5057 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005058 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005059 && check_for_list_arg(argvars, 0) == FAIL)
5060 || check_for_number_arg(argvars, 1) == FAIL
5061 || check_for_opt_number_arg(argvars, 2) == FAIL))
5062 return;
5063
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005064 if (check_can_index(argvars, TRUE, FALSE) != OK)
5065 return;
5066
5067 copy_tv(argvars, rettv);
5068 eval_index_inner(rettv, TRUE, argvars + 1,
5069 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5070 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005071}
5072
5073/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005074 * Apply index or range to "rettv".
5075 * "var1" is the first index, NULL for [:expr].
5076 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005077 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5078 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005079 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5080 */
5081 int
5082eval_index_inner(
5083 typval_T *rettv,
5084 int is_range,
5085 typval_T *var1,
5086 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005087 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005088 char_u *key,
5089 int keylen,
5090 int verbose)
5091{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005092 varnumber_T n1, n2 = 0;
5093 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005094
5095 n1 = 0;
5096 if (var1 != NULL && rettv->v_type != VAR_DICT)
5097 n1 = tv_get_number(var1);
5098
5099 if (is_range)
5100 {
5101 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005102 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005103 if (verbose)
5104 emsg(_(e_cannot_slice_dictionary));
5105 return FAIL;
5106 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005107 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005108 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005109 else
5110 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005111 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005112
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005113 switch (rettv->v_type)
5114 {
5115 case VAR_UNKNOWN:
5116 case VAR_ANY:
5117 case VAR_VOID:
5118 case VAR_FUNC:
5119 case VAR_PARTIAL:
5120 case VAR_FLOAT:
5121 case VAR_BOOL:
5122 case VAR_SPECIAL:
5123 case VAR_JOB:
5124 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005125 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005126 case VAR_CLASS:
5127 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005128 break; // not evaluating, skipping over subscript
5129
5130 case VAR_NUMBER:
5131 case VAR_STRING:
5132 {
5133 char_u *s = tv_get_string(rettv);
5134
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005136 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005137 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005138 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005139 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005140 else
5141 s = char_from_string(s, n1);
5142 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005143 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005145 // The resulting variable is a substring. If the indexes
5146 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 if (n1 < 0)
5148 {
5149 n1 = len + n1;
5150 if (n1 < 0)
5151 n1 = 0;
5152 }
5153 if (n2 < 0)
5154 n2 = len + n2;
5155 else if (n2 >= len)
5156 n2 = len;
5157 if (n1 >= len || n2 < 0 || n1 > n2)
5158 s = NULL;
5159 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005160 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161 }
5162 else
5163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005164 // The resulting variable is a string of a single
5165 // character. If the index is too big or negative the
5166 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167 if (n1 >= len || n1 < 0)
5168 s = NULL;
5169 else
5170 s = vim_strnsave(s + n1, 1);
5171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 clear_tv(rettv);
5173 rettv->v_type = VAR_STRING;
5174 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005175 }
5176 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005178 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005179 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5180 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005181 break;
5182
5183 case VAR_LIST:
5184 if (var1 == NULL)
5185 n1 = 0;
5186 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005187 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005188 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005189 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005190 return FAIL;
5191 break;
5192
5193 case VAR_DICT:
5194 {
5195 dictitem_T *item;
5196 typval_T tmp;
5197
5198 if (key == NULL)
5199 {
5200 key = tv_get_string_chk(var1);
5201 if (key == NULL)
5202 return FAIL;
5203 }
5204
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005205 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005206
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005207 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005208 {
5209 if (verbose)
5210 {
5211 if (keylen > 0)
5212 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005213 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005214 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005215 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005216 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005217
5218 copy_tv(&item->di_tv, &tmp);
5219 clear_tv(rettv);
5220 *rettv = tmp;
5221 }
5222 break;
5223 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 return OK;
5225}
5226
5227/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005228 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005229 */
5230 char_u *
5231partial_name(partial_T *pt)
5232{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005233 if (pt != NULL)
5234 {
5235 if (pt->pt_name != NULL)
5236 return pt->pt_name;
5237 if (pt->pt_func != NULL)
5238 return pt->pt_func->uf_name;
5239 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005240 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005241}
5242
Bram Moolenaarddecc252016-04-06 22:59:37 +02005243 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005244partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005245{
5246 int i;
5247
5248 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005249 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005250 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005251 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005252 if (pt->pt_name != NULL)
5253 {
5254 func_unref(pt->pt_name);
5255 vim_free(pt->pt_name);
5256 }
5257 else
5258 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005259 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005260
Bram Moolenaar54656012021-06-09 20:50:46 +02005261 // "out_up" is no longer used, decrement refcount on partial that owns it.
5262 partial_unref(pt->pt_outer.out_up_partial);
5263
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005264 // Using pt_outer from another partial.
5265 partial_unref(pt->pt_outer_partial);
5266
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005267 // Decrease the reference count for the context of a closure. If down
5268 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005269 if (pt->pt_funcstack != NULL)
5270 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005271 --pt->pt_funcstack->fs_refcount;
5272 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005273 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005274 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005275 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5276 if (pt->pt_loopvars[i] != NULL)
5277 {
5278 --pt->pt_loopvars[i]->lvs_refcount;
5279 loopvars_check_refcount(pt->pt_loopvars[i]);
5280 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005281
Bram Moolenaarddecc252016-04-06 22:59:37 +02005282 vim_free(pt);
5283}
5284
5285/*
5286 * Unreference a closure: decrement the reference count and free it when it
5287 * becomes zero.
5288 */
5289 void
5290partial_unref(partial_T *pt)
5291{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005292 if (pt == NULL)
5293 return;
5294
5295 int done = FALSE;
5296
5297 if (--pt->pt_refcount <= 0)
5298 partial_free(pt);
5299
5300 // If the reference count goes down to one, the funcstack may be the
5301 // only reference and can be freed if no other partials reference it.
5302 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005303 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005304 // careful: if the funcstack is freed it may contain this partial
5305 // and it gets freed as well
5306 if (pt->pt_funcstack != NULL)
5307 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005308
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005309 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005310 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005311 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005312
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005313 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5314 if (pt->pt_loopvars[depth] != NULL
5315 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005316 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005317 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005318 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005319}
5320
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005321/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005322 * Return the next (unique) copy ID.
5323 * Used for serializing nested structures.
5324 */
5325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005326get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005327{
5328 current_copyID += COPYID_INC;
5329 return current_copyID;
5330}
5331
5332/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005333 * Garbage collection for lists and dictionaries.
5334 *
5335 * We use reference counts to be able to free most items right away when they
5336 * are no longer used. But for composite items it's possible that it becomes
5337 * unused while the reference count is > 0: When there is a recursive
5338 * reference. Example:
5339 * :let l = [1, 2, 3]
5340 * :let d = {9: l}
5341 * :let l[1] = d
5342 *
5343 * Since this is quite unusual we handle this with garbage collection: every
5344 * once in a while find out which lists and dicts are not referenced from any
5345 * variable.
5346 *
5347 * Here is a good reference text about garbage collection (refers to Python
5348 * but it applies to all reference-counting mechanisms):
5349 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005350 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005351
5352/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005353 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005354 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005355 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005356 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005357 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005358garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005359{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005360 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005361 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005362 buf_T *buf;
5363 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005364 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005365 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005366
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005367 if (!testing)
5368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005369 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005370 want_garbage_collect = FALSE;
5371 may_garbage_collect = FALSE;
5372 garbage_collect_at_exit = FALSE;
5373 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005374
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005375 // The execution stack can grow big, limit the size.
5376 if (exestack.ga_maxlen - exestack.ga_len > 500)
5377 {
5378 size_t new_len;
5379 char_u *pp;
5380 int n;
5381
5382 // Keep 150% of the current size, with a minimum of the growth size.
5383 n = exestack.ga_len / 2;
5384 if (n < exestack.ga_growsize)
5385 n = exestack.ga_growsize;
5386
5387 // Don't make it bigger though.
5388 if (exestack.ga_len + n < exestack.ga_maxlen)
5389 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005390 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005391 pp = vim_realloc(exestack.ga_data, new_len);
5392 if (pp == NULL)
5393 return FAIL;
5394 exestack.ga_maxlen = exestack.ga_len + n;
5395 exestack.ga_data = pp;
5396 }
5397 }
5398
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005399 // We advance by two because we add one for items referenced through
5400 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005401 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005402
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403 /*
5404 * 1. Go through all accessible variables and mark all lists and dicts
5405 * with copyID.
5406 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005408 // Don't free variables in the previous_funccal list unless they are only
5409 // referenced through previous_funccal. This must be first, because if
5410 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005411 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005413 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005414 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005416 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005417 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5419 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005420
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005421 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005422 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005423 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5424 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005425 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005426 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005427 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005428 abort = abort || set_ref_in_item(
5429 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005430#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005431 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005432 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5433 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005434 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005435 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005436 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5437 NULL, NULL);
5438#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005441 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005442 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5443 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005444 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005445 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005448 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005451 abort = abort || set_ref_in_functions(copyID);
5452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005454 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005455
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005456 // funcstacks keep variables for closures
5457 abort = abort || set_ref_in_funcstacks(copyID);
5458
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005459 // loopvars keep variables for loop blocks
5460 abort = abort || set_ref_in_loopvars(copyID);
5461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005462 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005463 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005464
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005465 // callbacks in buffers
5466 abort = abort || set_ref_in_buffers(copyID);
5467
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005468 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5469 abort = abort || set_ref_in_insexpand_funcs(copyID);
5470
5471 // 'operatorfunc' callback
5472 abort = abort || set_ref_in_opfunc(copyID);
5473
5474 // 'tagfunc' callback
5475 abort = abort || set_ref_in_tagfunc(copyID);
5476
5477 // 'imactivatefunc' and 'imstatusfunc' callbacks
5478 abort = abort || set_ref_in_im_funcs(copyID);
5479
Bram Moolenaar1dced572012-04-05 16:54:08 +02005480#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005481 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005482#endif
5483
Bram Moolenaardb913952012-06-29 12:54:53 +02005484#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005486#endif
5487
5488#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005489 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005490#endif
5491
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005492#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005493 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005494 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005495#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005496#ifdef FEAT_NETBEANS_INTG
5497 abort = abort || set_ref_in_nb_channel(copyID);
5498#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005499
Bram Moolenaare3188e22016-05-31 21:13:04 +02005500#ifdef FEAT_TIMERS
5501 abort = abort || set_ref_in_timer(copyID);
5502#endif
5503
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005504#ifdef FEAT_QUICKFIX
5505 abort = abort || set_ref_in_quickfix(copyID);
5506#endif
5507
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005508#ifdef FEAT_TERMINAL
5509 abort = abort || set_ref_in_term(copyID);
5510#endif
5511
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005512#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005513 abort = abort || set_ref_in_popups(copyID);
5514#endif
5515
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005516 abort = abort || set_ref_in_classes(copyID);
5517
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005518 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005519 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005520 /*
5521 * 2. Free lists and dictionaries that are not referenced.
5522 */
5523 did_free = free_unref_items(copyID);
5524
5525 /*
5526 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005527 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005528 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005529 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005530 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005531 else if (p_verbose > 0)
5532 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005533 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005534 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005535
5536 return did_free;
5537}
5538
5539/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005540 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005541 */
5542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005543free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005544{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005545 int did_free = FALSE;
5546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005547 // Let all "free" functions know that we are here. This means no
5548 // dictionaries, lists, channels or jobs are to be freed, because we will
5549 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005550 in_free_unref_items = TRUE;
5551
5552 /*
5553 * PASS 1: free the contents of the items. We don't free the items
5554 * themselves yet, so that it is possible to decrement refcount counters
5555 */
5556
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005557 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005558 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005559
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005560 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005561 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005562
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005563 // Go through the list of objects and free items without this copyID.
5564 did_free |= object_free_nonref(copyID);
5565
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005566 // Go through the list of classes and free items without this copyID.
5567 did_free |= class_free_nonref(copyID);
5568
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005569#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005570 // Go through the list of jobs and free items without the copyID. This
5571 // must happen before doing channels, because jobs refer to channels, but
5572 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005573 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005575 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005576 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5577#endif
5578
5579 /*
5580 * PASS 2: free the items themselves.
5581 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005582 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005583 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005584 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005585
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005586#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005587 // Go through the list of jobs and free items without the copyID. This
5588 // must happen before doing channels, because jobs refer to channels, but
5589 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005590 free_unused_jobs(copyID, COPYID_MASK);
5591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005592 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005593 free_unused_channels(copyID, COPYID_MASK);
5594#endif
5595
5596 in_free_unref_items = FALSE;
5597
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005598 return did_free;
5599}
5600
5601/*
5602 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005603 * "list_stack" is used to add lists to be marked. Can be NULL.
5604 *
5605 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005606 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005608set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005609{
5610 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005611 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005612 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005613 hashtab_T *cur_ht;
5614 ht_stack_T *ht_stack = NULL;
5615 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005616
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005617 cur_ht = ht;
5618 for (;;)
5619 {
5620 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005622 // Mark each item in the hashtab. If the item contains a hashtab
5623 // it is added to ht_stack, if it contains a list it is added to
5624 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005625 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005626 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005627 if (!HASHITEM_EMPTY(hi))
5628 {
5629 --todo;
5630 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5631 &ht_stack, list_stack);
5632 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005633 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005634
5635 if (ht_stack == NULL)
5636 break;
5637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005638 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005639 cur_ht = ht_stack->ht;
5640 tempitem = ht_stack;
5641 ht_stack = ht_stack->prev;
5642 free(tempitem);
5643 }
5644
5645 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005646}
5647
Dominique Pelle748b3082022-01-08 12:41:16 +00005648#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5649 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005650/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005651 * Mark a dict and its items with "copyID".
5652 * Returns TRUE if setting references failed somehow.
5653 */
5654 int
5655set_ref_in_dict(dict_T *d, int copyID)
5656{
5657 if (d != NULL && d->dv_copyID != copyID)
5658 {
5659 d->dv_copyID = copyID;
5660 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5661 }
5662 return FALSE;
5663}
Dominique Pelle748b3082022-01-08 12:41:16 +00005664#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005665
5666/*
5667 * Mark a list and its items with "copyID".
5668 * Returns TRUE if setting references failed somehow.
5669 */
5670 int
5671set_ref_in_list(list_T *ll, int copyID)
5672{
5673 if (ll != NULL && ll->lv_copyID != copyID)
5674 {
5675 ll->lv_copyID = copyID;
5676 return set_ref_in_list_items(ll, copyID, NULL);
5677 }
5678 return FALSE;
5679}
5680
5681/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005682 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005683 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5684 *
5685 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005686 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005687 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005688set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005689{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005690 listitem_T *li;
5691 int abort = FALSE;
5692 list_T *cur_l;
5693 list_stack_T *list_stack = NULL;
5694 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005695
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005696 cur_l = l;
5697 for (;;)
5698 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005699 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005700 // Mark each item in the list. If the item contains a hashtab
5701 // it is added to ht_stack, if it contains a list it is added to
5702 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005703 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5704 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5705 ht_stack, &list_stack);
5706 if (list_stack == NULL)
5707 break;
5708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005709 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005710 cur_l = list_stack->list;
5711 tempitem = list_stack;
5712 list_stack = list_stack->prev;
5713 free(tempitem);
5714 }
5715
5716 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005717}
5718
5719/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005720 * Mark the partial in callback 'cb' with "copyID".
5721 */
5722 int
5723set_ref_in_callback(callback_T *cb, int copyID)
5724{
5725 typval_T tv;
5726
5727 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5728 return FALSE;
5729
5730 tv.v_type = VAR_PARTIAL;
5731 tv.vval.v_partial = cb->cb_partial;
5732 return set_ref_in_item(&tv, copyID, NULL, NULL);
5733}
5734
5735/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005736 * Mark the dict "dd" with "copyID".
5737 * Also see set_ref_in_item().
5738 */
5739 static int
5740set_ref_in_item_dict(
5741 dict_T *dd,
5742 int copyID,
5743 ht_stack_T **ht_stack,
5744 list_stack_T **list_stack)
5745{
5746 if (dd == NULL || dd->dv_copyID == copyID)
5747 return FALSE;
5748
5749 // Didn't see this dict yet.
5750 dd->dv_copyID = copyID;
5751 if (ht_stack == NULL)
5752 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5753
5754 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5755 if (newitem == NULL)
5756 return TRUE;
5757
5758 newitem->ht = &dd->dv_hashtab;
5759 newitem->prev = *ht_stack;
5760 *ht_stack = newitem;
5761
5762 return FALSE;
5763}
5764
5765/*
5766 * Mark the list "ll" with "copyID".
5767 * Also see set_ref_in_item().
5768 */
5769 static int
5770set_ref_in_item_list(
5771 list_T *ll,
5772 int copyID,
5773 ht_stack_T **ht_stack,
5774 list_stack_T **list_stack)
5775{
5776 if (ll == NULL || ll->lv_copyID == copyID)
5777 return FALSE;
5778
5779 // Didn't see this list yet.
5780 ll->lv_copyID = copyID;
5781 if (list_stack == NULL)
5782 return set_ref_in_list_items(ll, copyID, ht_stack);
5783
5784 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5785 if (newitem == NULL)
5786 return TRUE;
5787
5788 newitem->list = ll;
5789 newitem->prev = *list_stack;
5790 *list_stack = newitem;
5791
5792 return FALSE;
5793}
5794
5795/*
5796 * Mark the partial "pt" with "copyID".
5797 * Also see set_ref_in_item().
5798 */
5799 static int
5800set_ref_in_item_partial(
5801 partial_T *pt,
5802 int copyID,
5803 ht_stack_T **ht_stack,
5804 list_stack_T **list_stack)
5805{
5806 if (pt == NULL || pt->pt_copyID == copyID)
5807 return FALSE;
5808
5809 // Didn't see this partial yet.
5810 pt->pt_copyID = copyID;
5811
5812 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5813
5814 if (pt->pt_dict != NULL)
5815 {
5816 typval_T dtv;
5817
5818 dtv.v_type = VAR_DICT;
5819 dtv.vval.v_dict = pt->pt_dict;
5820 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5821 }
5822
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005823 if (pt->pt_obj != NULL)
5824 {
5825 typval_T objtv;
5826
5827 objtv.v_type = VAR_OBJECT;
5828 objtv.vval.v_object = pt->pt_obj;
5829 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5830 }
5831
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005832 for (int i = 0; i < pt->pt_argc; ++i)
5833 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5834 ht_stack, list_stack);
5835 // pt_funcstack is handled in set_ref_in_funcstacks()
5836 // pt_loopvars is handled in set_ref_in_loopvars()
5837
5838 return abort;
5839}
5840
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005841#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005842/*
5843 * Mark the job "pt" with "copyID".
5844 * Also see set_ref_in_item().
5845 */
5846 static int
5847set_ref_in_item_job(
5848 job_T *job,
5849 int copyID,
5850 ht_stack_T **ht_stack,
5851 list_stack_T **list_stack)
5852{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005853 typval_T dtv;
5854
5855 if (job == NULL || job->jv_copyID == copyID)
5856 return FALSE;
5857
5858 job->jv_copyID = copyID;
5859 if (job->jv_channel != NULL)
5860 {
5861 dtv.v_type = VAR_CHANNEL;
5862 dtv.vval.v_channel = job->jv_channel;
5863 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5864 }
5865 if (job->jv_exit_cb.cb_partial != NULL)
5866 {
5867 dtv.v_type = VAR_PARTIAL;
5868 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5869 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5870 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005871
5872 return FALSE;
5873}
5874
5875/*
5876 * Mark the channel "ch" with "copyID".
5877 * Also see set_ref_in_item().
5878 */
5879 static int
5880set_ref_in_item_channel(
5881 channel_T *ch,
5882 int copyID,
5883 ht_stack_T **ht_stack,
5884 list_stack_T **list_stack)
5885{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005886 typval_T dtv;
5887
5888 if (ch == NULL || ch->ch_copyID == copyID)
5889 return FALSE;
5890
5891 ch->ch_copyID = copyID;
5892 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5893 {
5894 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5895 jq != NULL; jq = jq->jq_next)
5896 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5897 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5898 cq = cq->cq_next)
5899 if (cq->cq_callback.cb_partial != NULL)
5900 {
5901 dtv.v_type = VAR_PARTIAL;
5902 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5903 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5904 }
5905 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5906 {
5907 dtv.v_type = VAR_PARTIAL;
5908 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5909 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5910 }
5911 }
5912 if (ch->ch_callback.cb_partial != NULL)
5913 {
5914 dtv.v_type = VAR_PARTIAL;
5915 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5916 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5917 }
5918 if (ch->ch_close_cb.cb_partial != NULL)
5919 {
5920 dtv.v_type = VAR_PARTIAL;
5921 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5922 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5923 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005924
5925 return FALSE;
5926}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005927#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005928
5929/*
5930 * Mark the class "cl" with "copyID".
5931 * Also see set_ref_in_item().
5932 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005933 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005934set_ref_in_item_class(
5935 class_T *cl,
5936 int copyID,
5937 ht_stack_T **ht_stack,
5938 list_stack_T **list_stack)
5939{
5940 int abort = FALSE;
5941
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005942 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005943 return FALSE;
5944
5945 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005946 if (cl->class_members_tv != NULL)
5947 {
5948 // The "class_members_tv" table is allocated only for regular classes
5949 // and not for interfaces.
5950 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5951 abort = abort || set_ref_in_item(
5952 &cl->class_members_tv[i],
5953 copyID, ht_stack, list_stack);
5954 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005955
5956 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5957 abort = abort || set_ref_in_func(NULL,
5958 cl->class_class_functions[i], copyID);
5959
5960 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5961 abort = abort || set_ref_in_func(NULL,
5962 cl->class_obj_methods[i], copyID);
5963
5964 return abort;
5965}
5966
5967/*
5968 * Mark the object "cl" with "copyID".
5969 * Also see set_ref_in_item().
5970 */
5971 static int
5972set_ref_in_item_object(
5973 object_T *obj,
5974 int copyID,
5975 ht_stack_T **ht_stack,
5976 list_stack_T **list_stack)
5977{
5978 int abort = FALSE;
5979
5980 if (obj == NULL || obj->obj_copyID == copyID)
5981 return FALSE;
5982
5983 obj->obj_copyID = copyID;
5984
5985 // The typval_T array is right after the object_T.
5986 typval_T *mtv = (typval_T *)(obj + 1);
5987 for (int i = 0; !abort
5988 && i < obj->obj_class->class_obj_member_count; ++i)
5989 abort = abort || set_ref_in_item(mtv + i, copyID,
5990 ht_stack, list_stack);
5991
5992 return abort;
5993}
5994
5995/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005996 * Mark all lists, dicts and other container types referenced through typval
5997 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005998 * "list_stack" is used to add lists to be marked. Can be NULL.
5999 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6000 *
6001 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006002 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004set_ref_in_item(
6005 typval_T *tv,
6006 int copyID,
6007 ht_stack_T **ht_stack,
6008 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006009{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006010 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006011
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006012 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006013 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006014 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006015 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6016 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006017
6018 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006019 return set_ref_in_item_list(tv->vval.v_list, copyID,
6020 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006021
6022 case VAR_FUNC:
6023 {
6024 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6025 break;
6026 }
6027
6028 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006029 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6030 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006031
6032 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006033#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006034 return set_ref_in_item_job(tv->vval.v_job, copyID,
6035 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006036#else
6037 break;
6038#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006039
6040 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006041#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006042 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006043 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006044#else
6045 break;
6046#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006047
6048 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006049 return set_ref_in_item_class(tv->vval.v_class, copyID,
6050 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006051
6052 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006053 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006054 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006055
6056 case VAR_UNKNOWN:
6057 case VAR_ANY:
6058 case VAR_VOID:
6059 case VAR_BOOL:
6060 case VAR_SPECIAL:
6061 case VAR_NUMBER:
6062 case VAR_FLOAT:
6063 case VAR_STRING:
6064 case VAR_BLOB:
6065 case VAR_INSTR:
6066 // Types that do not contain any other item
6067 break;
6068 }
6069
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006070 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006071}
6072
Bram Moolenaar8c711452005-01-14 21:53:12 +00006073/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 * Return a string with the string representation of a variable.
6075 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006077 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006078 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006079 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006080 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006081 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6082 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006083 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006085 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006086echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006087 typval_T *tv,
6088 char_u **tofree,
6089 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006090 int copyID,
6091 int echo_style,
6092 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006093 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006095 static int recurse = 0;
6096 char_u *r = NULL;
6097
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006100 if (!did_echo_string_emsg)
6101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006102 // Only give this message once for a recursive call to avoid
6103 // flooding the user with errors. And stop iterating over lists
6104 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006105 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006106 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006107 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006108 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006109 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006110 }
6111 ++recurse;
6112
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006113 switch (tv->v_type)
6114 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006115 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006116 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006117 {
6118 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006119 r = tv->vval.v_string;
6120 if (r == NULL)
6121 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006122 }
6123 else
6124 {
6125 *tofree = string_quote(tv->vval.v_string, FALSE);
6126 r = *tofree;
6127 }
6128 break;
6129
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006131 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006132 char_u buf[MAX_FUNC_NAME_LEN];
6133
6134 if (echo_style)
6135 {
LemonBoya5d35902022-04-29 21:15:02 +01006136 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6137 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006138 buf, MAX_FUNC_NAME_LEN);
6139 if (r == buf)
6140 {
6141 r = vim_strsave(buf);
6142 *tofree = r;
6143 }
6144 else
6145 *tofree = NULL;
6146 }
6147 else
6148 {
6149 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6150 : make_ufunc_name_readable(
6151 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6152 TRUE);
6153 r = *tofree;
6154 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006155 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006156 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006157
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006158 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006159 {
6160 partial_T *pt = tv->vval.v_partial;
6161 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006162 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006163 garray_T ga;
6164 int i;
6165 char_u *tf;
6166
6167 ga_init2(&ga, 1, 100);
6168 ga_concat(&ga, (char_u *)"function(");
6169 if (fname != NULL)
6170 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006171 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006172 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006173 && vim_isupper(fname[1]))
6174 {
6175 ga_concat(&ga, (char_u *)"'g:");
6176 ga_concat(&ga, fname + 1);
6177 }
6178 else
6179 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006180 vim_free(fname);
6181 }
6182 if (pt != NULL && pt->pt_argc > 0)
6183 {
6184 ga_concat(&ga, (char_u *)", [");
6185 for (i = 0; i < pt->pt_argc; ++i)
6186 {
6187 if (i > 0)
6188 ga_concat(&ga, (char_u *)", ");
6189 ga_concat(&ga,
6190 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6191 vim_free(tf);
6192 }
6193 ga_concat(&ga, (char_u *)"]");
6194 }
6195 if (pt != NULL && pt->pt_dict != NULL)
6196 {
6197 typval_T dtv;
6198
6199 ga_concat(&ga, (char_u *)", ");
6200 dtv.v_type = VAR_DICT;
6201 dtv.vval.v_dict = pt->pt_dict;
6202 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6203 vim_free(tf);
6204 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006205 // terminate with ')' and a NUL
6206 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006207
6208 *tofree = ga.ga_data;
6209 r = *tofree;
6210 break;
6211 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006212
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006213 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006214 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006215 break;
6216
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218 if (tv->vval.v_list == NULL)
6219 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006220 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006221 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006222 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006223 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006224 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6225 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006226 {
6227 *tofree = NULL;
6228 r = (char_u *)"[...]";
6229 }
6230 else
6231 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006232 int old_copyID = tv->vval.v_list->lv_copyID;
6233
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006234 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006235 *tofree = list2string(tv, copyID, restore_copyID);
6236 if (restore_copyID)
6237 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006238 r = *tofree;
6239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006241
Bram Moolenaar8c711452005-01-14 21:53:12 +00006242 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006243 if (tv->vval.v_dict == NULL)
6244 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006245 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006246 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006247 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006248 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006249 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6250 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006251 {
6252 *tofree = NULL;
6253 r = (char_u *)"{...}";
6254 }
6255 else
6256 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006257 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006258
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006259 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006260 *tofree = dict2string(tv, copyID, restore_copyID);
6261 if (restore_copyID)
6262 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006263 r = *tofree;
6264 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006265 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006266
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006267 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006268 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006269 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006271 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006272 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006273 break;
6274
Bram Moolenaar835dc632016-02-07 14:27:38 +01006275 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006276 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006277#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006278 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006279 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6280 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006281 if (composite_val)
6282 {
6283 *tofree = string_quote(r, FALSE);
6284 r = *tofree;
6285 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006286#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006288
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006289 case VAR_INSTR:
6290 *tofree = NULL;
6291 r = (char_u *)"instructions";
6292 break;
6293
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006294 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006295 {
6296 class_T *cl = tv->vval.v_class;
6297 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6298 r = *tofree = alloc(len);
6299 vim_snprintf((char *)r, len, "class %s",
6300 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6301 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006302 break;
6303
6304 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006305 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006306 garray_T ga;
6307 ga_init2(&ga, 1, 50);
6308 ga_concat(&ga, (char_u *)"object of ");
6309 object_T *obj = tv->vval.v_object;
6310 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6311 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6312 : cl->class_name);
6313 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006314 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006315 ga_concat(&ga, (char_u *)" {");
6316 for (int i = 0; i < cl->class_obj_member_count; ++i)
6317 {
6318 if (i > 0)
6319 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006320 ocmember_T *m = &cl->class_obj_members[i];
6321 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006322 ga_concat(&ga, (char_u *)": ");
6323 char_u *tf = NULL;
6324 ga_concat(&ga, echo_string_core(
6325 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006326 &tf, numbuf, copyID, echo_style,
6327 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006328 vim_free(tf);
6329 }
6330 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006331 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006332
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006333 *tofree = r = ga.ga_data;
6334 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006335 break;
6336
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006337 case VAR_FLOAT:
6338 *tofree = NULL;
6339 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6340 r = numbuf;
6341 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006342
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006343 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006344 case VAR_SPECIAL:
6345 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006346 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006347 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006348 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006349
Bram Moolenaar8502c702014-06-17 12:51:16 +02006350 if (--recurse == 0)
6351 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006353}
6354
6355/*
6356 * Return a string with the string representation of a variable.
6357 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6358 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006359 * Does not put quotes around strings, as ":echo" displays values.
6360 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6361 * May return NULL.
6362 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006363 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006364echo_string(
6365 typval_T *tv,
6366 char_u **tofree,
6367 char_u *numbuf,
6368 int copyID)
6369{
6370 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6371}
6372
6373/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006374 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6375 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006376 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006377 */
6378 int
6379buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6380{
6381 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006382 char_u *t;
6383 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006384
6385 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6386 return -1;
6387
6388 if (lnum > buf->b_ml.ml_line_count)
6389 lnum = buf->b_ml.ml_line_count;
6390
6391 str = ml_get_buf(buf, lnum, FALSE);
6392 if (str == NULL)
6393 return -1;
6394
6395 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006396 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006397
Bram Moolenaar91458462021-01-13 20:08:38 +01006398 // count the number of characters
6399 t = str;
6400 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6401 t += mb_ptr2len(t);
6402
6403 // In insert mode, when the cursor is at the end of a non-empty line,
6404 // byteidx points to the NUL character immediately past the end of the
6405 // string. In this case, add one to the character count.
6406 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6407 count++;
6408
6409 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006410}
6411
6412/*
6413 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006414 * byte index. Works only for loaded buffers. Returns -1 on failure.
6415 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006416 */
6417 int
6418buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6419{
6420 char_u *str;
6421 char_u *t;
6422
6423 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6424 return -1;
6425
6426 if (lnum > buf->b_ml.ml_line_count)
6427 lnum = buf->b_ml.ml_line_count;
6428
6429 str = ml_get_buf(buf, lnum, FALSE);
6430 if (str == NULL)
6431 return -1;
6432
6433 // Convert the character offset to a byte offset
6434 t = str;
6435 while (*t != NUL && --charidx > 0)
6436 t += mb_ptr2len(t);
6437
Bram Moolenaar91458462021-01-13 20:08:38 +01006438 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006439}
6440
6441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006443 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006445 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006446var2fpos(
6447 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006448 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006449 int *fnum, // set to fnum for '0, 'A, etc.
6450 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006452 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006454 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006456 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006457 if (varp->v_type == VAR_LIST)
6458 {
6459 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006460 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006461 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006462 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006463
6464 l = varp->vval.v_list;
6465 if (l == NULL)
6466 return NULL;
6467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006468 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006469 pos.lnum = list_find_nr(l, 0L, &error);
6470 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006471 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006472 if (charcol)
6473 len = (long)mb_charlen(ml_get(pos.lnum));
6474 else
6475 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006476
Bram Moolenaarec65d772020-08-20 22:29:12 +02006477 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006478 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006479 li = list_find(l, 1L);
6480 if (li != NULL && li->li_tv.v_type == VAR_STRING
6481 && li->li_tv.vval.v_string != NULL
6482 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006483 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006484 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006485 }
6486 else
6487 {
6488 pos.col = list_find_nr(l, 1L, &error);
6489 if (error)
6490 return NULL;
6491 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006493 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006494 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006495 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006496 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006498 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006499 pos.coladd = list_find_nr(l, 2L, &error);
6500 if (error)
6501 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006502
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006503 return &pos;
6504 }
6505
Bram Moolenaarc5809432021-03-27 21:23:30 +01006506 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6507 return NULL;
6508
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006509 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006510 if (name == NULL)
6511 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006512
6513 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006514 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006515 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006516 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006517 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006518 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006519 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006520 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006521 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006522 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006523 pos = VIsual;
6524 else
6525 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006526 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006527 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006528 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006530 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006531 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6533 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006534 pos = *pp;
6535 }
6536 if (pos.lnum != 0)
6537 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006538 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006539 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6540 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006542
Bram Moolenaara5525202006-03-02 22:52:09 +00006543 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006544
Bram Moolenaar477933c2007-07-17 14:32:23 +00006545 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006546 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006547 // the "w_valid" flags are not reset when moving the cursor, but they
6548 // do matter for update_topline() and validate_botline().
6549 check_cursor_moved(curwin);
6550
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006551 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006552 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006553 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006554 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006555 // In silent Ex mode topline is zero, but that's not a valid line
6556 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006557 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006558 return &pos;
6559 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006560 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006561 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006562 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006563 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006564 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006565 return &pos;
6566 }
6567 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006568 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006570 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {
6572 pos.lnum = curbuf->b_ml.ml_line_count;
6573 pos.col = 0;
6574 }
6575 else
6576 {
6577 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006578 if (charcol)
6579 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6580 else
6581 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 }
6583 return &pos;
6584 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006585 if (in_vim9script())
6586 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 return NULL;
6588}
6589
6590/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006591 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006592 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006593 * Note that the column is passed on as-is, the caller may want to decrement
6594 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006595 * If "charcol" is TRUE use the column as the character index instead of the
6596 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006597 * Return FAIL when conversion is not possible, doesn't check the position for
6598 * validity.
6599 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006601list2fpos(
6602 typval_T *arg,
6603 pos_T *posp,
6604 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006605 colnr_T *curswantp,
6606 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006607{
6608 list_T *l = arg->vval.v_list;
6609 long i = 0;
6610 long n;
6611
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006612 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6613 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006614 if (arg->v_type != VAR_LIST
6615 || l == NULL
6616 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006617 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006618 return FAIL;
6619
6620 if (fnump != NULL)
6621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006622 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006623 if (n < 0)
6624 return FAIL;
6625 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006626 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006627 *fnump = n;
6628 }
6629
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006630 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006631 if (n < 0)
6632 return FAIL;
6633 posp->lnum = n;
6634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006635 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006636 if (n < 0)
6637 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006638 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006639 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006640 if (charcol)
6641 {
6642 buf_T *buf;
6643
6644 // Get the text for the specified line in a loaded buffer
6645 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6646 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6647 return FAIL;
6648
Bram Moolenaar79f23442022-10-10 12:42:57 +01006649 n = buf_charidx_to_byteidx(buf,
6650 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006651 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006652 posp->col = n;
6653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006654 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006655 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006656 posp->coladd = 0;
6657 else
6658 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006659
Bram Moolenaar493c1782014-05-28 14:34:46 +02006660 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006661 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006662
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006663 return OK;
6664}
6665
6666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 * Get the length of an environment variable name.
6668 * Advance "arg" to the first character after the name.
6669 * Return 0 for error.
6670 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006671 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006672get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
6674 char_u *p;
6675 int len;
6676
6677 for (p = *arg; vim_isIDc(*p); ++p)
6678 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006679 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 return 0;
6681
6682 len = (int)(p - *arg);
6683 *arg = p;
6684 return len;
6685}
6686
6687/*
6688 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006689 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 * Return 0 if something is wrong.
6691 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006692 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 char_u *p;
6696 int len;
6697
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006698 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006700 {
6701 if (*p == ':')
6702 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006703 // "s:" is start of "s:var", but "n:" is not and can be used in
6704 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006705 len = (int)(p - *arg);
6706 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6707 || len > 1)
6708 break;
6709 }
6710 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006711 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 return 0;
6713
6714 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006715 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
6717 return len;
6718}
6719
6720/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006721 * Get the length of the name of a variable or function.
6722 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006724 * Return -1 if curly braces expansion failed.
6725 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 * If the name contains 'magic' {}'s, expand them and return the
6727 * expanded name in an allocated string via 'alias' - caller must free.
6728 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006730get_name_len(
6731 char_u **arg,
6732 char_u **alias,
6733 int evaluate,
6734 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 char_u *p;
6738 char_u *expr_start;
6739 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006741 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
6743 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6744 && (*arg)[2] == (int)KE_SNR)
6745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006746 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 *arg += 3;
6748 return get_id_len(arg) + 3;
6749 }
6750 len = eval_fname_script(*arg);
6751 if (len > 0)
6752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006753 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 *arg += len;
6755 }
6756
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006758 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006760 p = find_name_end(*arg, &expr_start, &expr_end,
6761 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (expr_start != NULL)
6763 {
6764 char_u *temp_string;
6765
6766 if (!evaluate)
6767 {
6768 len += (int)(p - *arg);
6769 *arg = skipwhite(p);
6770 return len;
6771 }
6772
6773 /*
6774 * Include any <SID> etc in the expanded string:
6775 * Thus the -len here.
6776 */
6777 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6778 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006779 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 *alias = temp_string;
6781 *arg = skipwhite(p);
6782 return (int)STRLEN(temp_string);
6783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
6785 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006786 // Only give an error when there is something, otherwise it will be
6787 // reported at a higher level.
6788 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006789 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791 return len;
6792}
6793
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006794/*
6795 * Find the end of a variable or function name, taking care of magic braces.
6796 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6797 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006798 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006799 * Return a pointer to just after the name. Equal to "arg" if there is no
6800 * valid name.
6801 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006802 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006803find_name_end(
6804 char_u *arg,
6805 char_u **expr_start,
6806 char_u **expr_end,
6807 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006809 int mb_nest = 0;
6810 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006812 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006813 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 *expr_start = NULL;
6818 *expr_end = NULL;
6819 }
6820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006821 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006822 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006823 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006824 return arg;
6825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006826 for (p = arg; *p != NUL
6827 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006828 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006829 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006830 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006831 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006832 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006833 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006834 if (*p == '\'')
6835 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006836 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006837 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006838 ;
6839 if (*p == NUL)
6840 break;
6841 }
6842 else if (*p == '"')
6843 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006844 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006845 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006846 if (*p == '\\' && p[1] != NUL)
6847 ++p;
6848 if (*p == NUL)
6849 break;
6850 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006851 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006853 // "s:" is start of "s:var", but "n:" is not and can be used in
6854 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006855 len = (int)(p - arg);
6856 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006857 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006858 break;
6859 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006861 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006863 if (*p == '[')
6864 ++br_nest;
6865 else if (*p == ']')
6866 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006868
zeertzjqa93d9cd2023-05-02 16:25:47 +01006869 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006871 if (*p == '{')
6872 {
6873 mb_nest++;
6874 if (expr_start != NULL && *expr_start == NULL)
6875 *expr_start = p;
6876 }
6877 else if (*p == '}')
6878 {
6879 mb_nest--;
6880 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6881 *expr_end = p;
6882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
6885
6886 return p;
6887}
6888
6889/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006890 * Expands out the 'magic' {}'s in a variable/function name.
6891 * Note that this can call itself recursively, to deal with
6892 * constructs like foo{bar}{baz}{bam}
6893 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6894 * "in_start" ^
6895 * "expr_start" ^
6896 * "expr_end" ^
6897 * "in_end" ^
6898 *
6899 * Returns a new allocated string, which the caller must free.
6900 * Returns NULL for failure.
6901 */
6902 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006903make_expanded_name(
6904 char_u *in_start,
6905 char_u *expr_start,
6906 char_u *expr_end,
6907 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006908{
6909 char_u c1;
6910 char_u *retval = NULL;
6911 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006912
6913 if (expr_end == NULL || in_end == NULL)
6914 return NULL;
6915 *expr_start = NUL;
6916 *expr_end = NUL;
6917 c1 = *in_end;
6918 *in_end = NUL;
6919
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006920 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006921 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006922 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006923 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6924 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006925 if (retval != NULL)
6926 {
6927 STRCPY(retval, in_start);
6928 STRCAT(retval, temp_result);
6929 STRCAT(retval, expr_end + 1);
6930 }
6931 }
6932 vim_free(temp_result);
6933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006934 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006935 *expr_start = '{';
6936 *expr_end = '}';
6937
6938 if (retval != NULL)
6939 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006940 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006941 if (expr_start != NULL)
6942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006943 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006944 temp_result = make_expanded_name(retval, expr_start,
6945 expr_end, temp_result);
6946 vim_free(retval);
6947 retval = temp_result;
6948 }
6949 }
6950
6951 return retval;
6952}
6953
6954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006956 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006959eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006961 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006962}
6963
6964/*
6965 * Return TRUE if character "c" can be used as the first character in a
6966 * variable or function name (excluding '{' and '}').
6967 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006968 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006969eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006970{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006971 return ASCII_ISALPHA(c) || c == '_';
6972}
6973
6974/*
6975 * Return TRUE if character "c" can be used as the first character of a
6976 * dictionary key.
6977 */
6978 int
6979eval_isdictc(int c)
6980{
6981 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982}
6983
6984/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006985 * Handle:
6986 * - expr[expr], expr[expr:expr] subscript
6987 * - ".name" lookup
6988 * - function call with Funcref variable: func(expr)
6989 * - method call: var->method()
6990 *
6991 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006992 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006993 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006995handle_subscript(
6996 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006997 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006998 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006999 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007000 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007001{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007002 int evaluate = evalarg != NULL
7003 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007004 int ret = OK;
7005 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007006 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007007 int getnext;
7008 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007009
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007010 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007011 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007012 // When at the end of the line and ".name" or "->{" or "->X" follows in
7013 // the next line then consume the line break.
7014 p = eval_next_non_blank(*arg, evalarg, &getnext);
7015 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007016 && ((*p == '.'
7017 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7018 || rettv->v_type == VAR_CLASS
7019 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007020 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7021 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7022 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007023 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007024 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007025 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007026 check_white = FALSE;
7027 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007028
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007029 if (rettv->v_type == VAR_ANY)
7030 {
7031 char_u *exp_name;
7032 int cc;
7033 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007034 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007035 type_T *type;
7036
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007037 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007038 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007039 if (**arg != '.')
7040 {
7041 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007042 semsg(_(e_expected_dot_after_name_str),
7043 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007044 ret = FAIL;
7045 break;
7046 }
7047 ++*arg;
7048 if (IS_WHITE_OR_NUL(**arg))
7049 {
7050 if (verbose)
7051 emsg(_(e_no_white_space_allowed_after_dot));
7052 ret = FAIL;
7053 break;
7054 }
7055
7056 // isolate the name
7057 exp_name = *arg;
7058 while (eval_isnamec(**arg))
7059 ++*arg;
7060 cc = **arg;
7061 **arg = NUL;
7062
7063 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007064 evalarg == NULL ? NULL : evalarg->eval_cctx,
7065 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007066 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007067
7068 if (idx < 0 && ufunc == NULL)
7069 {
7070 ret = FAIL;
7071 break;
7072 }
7073 if (idx >= 0)
7074 {
7075 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7076 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7077
7078 copy_tv(sv->sv_tv, rettv);
7079 }
7080 else
7081 {
7082 rettv->v_type = VAR_FUNC;
7083 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7084 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007085 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007086 }
7087
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007088 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7089 || rettv->v_type == VAR_PARTIAL))
7090 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007091 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007092 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7093 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007094
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007095 // Stop the expression evaluation when immediately aborting on
7096 // error, or when an interrupt occurred or an exception was thrown
7097 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007098 if (aborting())
7099 {
7100 if (ret == OK)
7101 clear_tv(rettv);
7102 ret = FAIL;
7103 }
7104 dict_unref(selfdict);
7105 selfdict = NULL;
7106 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007107 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007108 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007109 if (in_vim9script())
7110 *arg = skipwhite(p + 2);
7111 else
7112 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007113 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007114 {
zeertzjqc481ad32023-03-11 16:18:51 +00007115 emsg(_(e_no_white_space_allowed_before_parenthesis));
7116 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007117 }
zeertzjqc481ad32023-03-11 16:18:51 +00007118 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7119 // expr->{lambda}() or expr->(lambda)()
7120 ret = eval_lambda(arg, rettv, evalarg, verbose);
7121 else
7122 // expr->name()
7123 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007124 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007125 // "." is ".name" lookup when we found a dict or when evaluating and
7126 // scriptversion is at least 2, where string concatenation is "..".
7127 else if (**arg == '['
7128 || (**arg == '.' && (rettv->v_type == VAR_DICT
7129 || (!evaluate
7130 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007131 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007132 {
7133 dict_unref(selfdict);
7134 if (rettv->v_type == VAR_DICT)
7135 {
7136 selfdict = rettv->vval.v_dict;
7137 if (selfdict != NULL)
7138 ++selfdict->dv_refcount;
7139 }
7140 else
7141 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007142 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007143 {
7144 clear_tv(rettv);
7145 ret = FAIL;
7146 }
7147 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007148 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7149 || rettv->v_type == VAR_OBJECT))
7150 {
7151 // class member: SomeClass.varname
7152 // class method: SomeClass.SomeMethod()
7153 // class constructor: SomeClass.new()
7154 // object member: someObject.varname
7155 // object method: someObject.SomeMethod()
7156 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7157 {
7158 clear_tv(rettv);
7159 ret = FAIL;
7160 }
7161 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007162 else
7163 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007164 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007165
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007166 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7167 // Don't do this when "Func" is already a partial that was bound
7168 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007169 if (selfdict != NULL
7170 && (rettv->v_type == VAR_FUNC
7171 || (rettv->v_type == VAR_PARTIAL
7172 && (rettv->vval.v_partial->pt_auto
7173 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007174 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007175
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007176 dict_unref(selfdict);
7177 return ret;
7178}
7179
7180/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 * Make a copy of an item.
7182 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007183 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007184 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7185 * reference to an already copied list/dict can be used.
7186 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007187 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007189item_copy(
7190 typval_T *from,
7191 typval_T *to,
7192 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007193 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195{
7196 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007201 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007202 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 }
7204 ++recurse;
7205
7206 switch (from->v_type)
7207 {
7208 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007209 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007210 case VAR_STRING:
7211 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007212 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007213 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007214 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007215 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007216 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007217 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007218 case VAR_CLASS:
7219 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 copy_tv(from, to);
7221 break;
7222 case VAR_LIST:
7223 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007224 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007225 if (from->vval.v_list == NULL)
7226 to->vval.v_list = NULL;
7227 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7228 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007229 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007230 to->vval.v_list = from->vval.v_list->lv_copylist;
7231 ++to->vval.v_list->lv_refcount;
7232 }
7233 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007234 to->vval.v_list = list_copy(from->vval.v_list,
7235 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007236 if (to->vval.v_list == NULL)
7237 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007239 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007240 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007241 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 case VAR_DICT:
7243 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007244 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007245 if (from->vval.v_dict == NULL)
7246 to->vval.v_dict = NULL;
7247 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7248 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007249 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007250 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7251 ++to->vval.v_dict->dv_refcount;
7252 }
7253 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007254 to->vval.v_dict = dict_copy(from->vval.v_dict,
7255 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007256 if (to->vval.v_dict == NULL)
7257 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007258 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007259 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007260 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007262 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007263 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007264 }
7265 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007266 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007267}
7268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 void
7270echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7271{
7272 char_u *tofree;
7273 char_u numbuf[NUMBUFLEN];
7274 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7275
7276 if (*atstart)
7277 {
7278 *atstart = FALSE;
7279 // Call msg_start() after eval1(), evaluating the expression
7280 // may cause a message to appear.
7281 if (with_space)
7282 {
7283 // Mark the saved text as finishing the line, so that what
7284 // follows is displayed on a new line when scrolling back
7285 // at the more prompt.
7286 msg_sb_eol();
7287 msg_start();
7288 }
7289 }
7290 else if (with_space)
7291 msg_puts_attr(" ", echo_attr);
7292
7293 if (p != NULL)
7294 for ( ; *p != NUL && !got_int; ++p)
7295 {
7296 if (*p == '\n' || *p == '\r' || *p == TAB)
7297 {
7298 if (*p != TAB && *needclr)
7299 {
7300 // remove any text still there from the command
7301 msg_clr_eos();
7302 *needclr = FALSE;
7303 }
7304 msg_putchar_attr(*p, echo_attr);
7305 }
7306 else
7307 {
7308 if (has_mbyte)
7309 {
7310 int i = (*mb_ptr2len)(p);
7311
7312 (void)msg_outtrans_len_attr(p, i, echo_attr);
7313 p += i - 1;
7314 }
7315 else
7316 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7317 }
7318 }
7319 vim_free(tofree);
7320}
7321
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 * ":echo expr1 ..." print each argument separated with a space, add a
7324 * newline at the end.
7325 * ":echon expr1 ..." print each argument plain.
7326 */
7327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007328ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
7330 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007332 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 int needclr = TRUE;
7334 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007335 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007336 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007337 evalarg_T evalarg;
7338
Bram Moolenaare707c882020-07-01 13:07:37 +02007339 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340
7341 if (eap->skip)
7342 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007343 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007345 // If eval1() causes an error message the text from the command may
7346 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007347 need_clr_eos = needclr;
7348
Bram Moolenaar68db9962021-05-09 23:19:22 +02007349 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007350 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 {
7352 /*
7353 * Report the invalid expression unless the expression evaluation
7354 * has been cancelled due to an aborting error, an interrupt, or an
7355 * exception.
7356 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007357 if (!aborting() && did_emsg == did_emsg_before
7358 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007359 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007360 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 break;
7362 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007363 need_clr_eos = FALSE;
7364
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007366 {
7367 if (rettv.v_type == VAR_VOID)
7368 {
7369 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7370 break;
7371 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007373 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007375 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 arg = skipwhite(arg);
7377 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007378 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007379 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380
7381 if (eap->skip)
7382 --emsg_skip;
7383 else
7384 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007385 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 if (needclr)
7387 msg_clr_eos();
7388 if (eap->cmdidx == CMD_echo)
7389 msg_end();
7390 }
7391}
7392
7393/*
7394 * ":echohl {name}".
7395 */
7396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007397ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007399 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400}
7401
7402/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007403 * Returns the :echo attribute
7404 */
7405 int
7406get_echo_attr(void)
7407{
7408 return echo_attr;
7409}
7410
7411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 * ":execute expr1 ..." execute the result of an expression.
7413 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007414 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007416 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 * Each gets spaces around each argument and a newline at the end for
7418 * echo commands
7419 */
7420 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007421ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
7423 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 int ret = OK;
7426 char_u *p;
7427 garray_T ga;
7428 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007429 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431 ga_init2(&ga, 1, 80);
7432
7433 if (eap->skip)
7434 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007435 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007437 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007438 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
7441 if (!eap->skip)
7442 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007443 char_u buf[NUMBUFLEN];
7444
7445 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007446 {
7447 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7448 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007449 semsg(_(e_using_invalid_value_as_string_str),
7450 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007451 p = NULL;
7452 }
7453 else
7454 p = tv_get_string_buf(&rettv, buf);
7455 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007456 else
7457 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007458 if (p == NULL)
7459 {
7460 clear_tv(&rettv);
7461 ret = FAIL;
7462 break;
7463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 len = (int)STRLEN(p);
7465 if (ga_grow(&ga, len + 2) == FAIL)
7466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007467 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 ret = FAIL;
7469 break;
7470 }
7471 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 ga.ga_len += len;
7475 }
7476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007477 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 arg = skipwhite(arg);
7479 }
7480
7481 if (ret != FAIL && ga.ga_data != NULL)
7482 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007483 // use the first line of continuation lines for messages
7484 SOURCING_LNUM = start_lnum;
7485
Bram Moolenaar37fef162022-08-29 18:16:32 +01007486 if (eap->cmdidx == CMD_echomsg
7487 || eap->cmdidx == CMD_echowindow
7488 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007490 // Mark the already saved text as finishing the line, so that what
7491 // follows is displayed on a new line when scrolling back at the
7492 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007493 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007494 }
7495
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007496 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007497 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007498 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007499 out_flush();
7500 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007501 else if (eap->cmdidx == CMD_echowindow)
7502 {
7503#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007504 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007505#endif
7506 msg_attr(ga.ga_data, echo_attr);
7507#ifdef HAS_MESSAGE_WINDOW
7508 end_echowindow();
7509#endif
7510 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007511 else if (eap->cmdidx == CMD_echoconsole)
7512 {
7513 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7514 ui_write((char_u *)"\r\n", 2, TRUE);
7515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 else if (eap->cmdidx == CMD_echoerr)
7517 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007518 int save_did_emsg = did_emsg;
7519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007520 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007521 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 if (!force_abort)
7523 did_emsg = save_did_emsg;
7524 }
7525 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007526 {
7527 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7528
7529 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7530 sticky_cmdmod_flags = cmdmod.cmod_flags
7531 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 do_cmdline((char_u *)ga.ga_data,
7533 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007534 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 }
7537
7538 ga_clear(&ga);
7539
7540 if (eap->skip)
7541 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007542 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543}
7544
7545/*
7546 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7547 * "arg" points to the "&" or '+' when called, to "option" when returning.
7548 * Returns NULL when no option name found. Otherwise pointer to the char
7549 * after the option name.
7550 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007551 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007552find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
7554 char_u *p = *arg;
7555
7556 ++p;
7557 if (*p == 'g' && p[1] == ':')
7558 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007559 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 p += 2;
7561 }
7562 else if (*p == 'l' && p[1] == ':')
7563 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007564 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 p += 2;
7566 }
7567 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007568 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569
7570 if (!ASCII_ISALPHA(*p))
7571 return NULL;
7572 *arg = p;
7573
7574 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007575 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 else
7577 while (ASCII_ISALPHA(*p))
7578 ++p;
7579 return p;
7580}
7581
7582/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007583 * Display script name where an item was last set.
7584 * Should only be invoked when 'verbose' is non-zero.
7585 */
7586 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007587last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007588{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007589 char_u *p;
7590
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007591 if (script_ctx.sc_sid == 0)
7592 return;
7593
7594 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7595 if (p == NULL)
7596 return;
7597
7598 verbose_enter();
7599 msg_puts(_("\n\tLast set from "));
7600 msg_puts((char *)p);
7601 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007602 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007603 msg_puts(_(line_msg));
7604 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007605 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007606 verbose_leave();
7607 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007608}
7609
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007610#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
7612/*
7613 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007614 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 * "flags" can be "g" to do a global substitute.
7616 * Returns an allocated string, NULL for error.
7617 */
7618 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007619do_string_sub(
7620 char_u *str,
7621 char_u *pat,
7622 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007623 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007624 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625{
7626 int sublen;
7627 regmatch_T regmatch;
7628 int i;
7629 int do_all;
7630 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007631 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 garray_T ga;
7633 char_u *ret;
7634 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007635 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007637 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007639 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640
7641 ga_init2(&ga, 1, 200);
7642
7643 do_all = (flags[0] == 'g');
7644
7645 regmatch.rm_ic = p_ic;
7646 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7647 if (regmatch.regprog != NULL)
7648 {
7649 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007650 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7652 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007653 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007654 if (regmatch.startp[0] == regmatch.endp[0])
7655 {
7656 if (zero_width == regmatch.startp[0])
7657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007658 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007659 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007660 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7661 (size_t)i);
7662 ga.ga_len += i;
7663 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007664 continue;
7665 }
7666 zero_width = regmatch.startp[0];
7667 }
7668
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 /*
7670 * Get some space for a temporary buffer to do the substitution
7671 * into. It will contain:
7672 * - The text up to where the match is.
7673 * - The substituted text.
7674 * - The text after the match.
7675 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007676 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007677 if (sublen <= 0)
7678 {
7679 ga_clear(&ga);
7680 break;
7681 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007682 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7684 {
7685 ga_clear(&ga);
7686 break;
7687 }
7688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007689 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 i = (int)(regmatch.startp[0] - tail);
7691 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007692 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007693 (void)vim_regsub(&regmatch, sub, expr,
7694 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7695 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007697 tail = regmatch.endp[0];
7698 if (*tail == NUL)
7699 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 if (!do_all)
7701 break;
7702 }
7703
7704 if (ga.ga_data != NULL)
7705 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7706
Bram Moolenaar473de612013-06-08 18:19:48 +02007707 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
7709
7710 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7711 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007712 if (p_cpo == empty_option)
7713 p_cpo = save_cpo;
7714 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007716 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007717 // If it's still empty it was changed and restored, need to restore in
7718 // the complicated way.
7719 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007720 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007721 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723
7724 return ret;
7725}